configure: re-enable '-Wunused-parameter'
[babeltrace.git] / src / lib / graph / iterator.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/MSG-ITER"
9 #include "lib/logging.h"
10
11 #include "compat/compiler.h"
12 #include "compat/glib.h"
13 #include "lib/trace-ir/clock-class.h"
14 #include "lib/trace-ir/clock-snapshot.h"
15 #include <babeltrace2/trace-ir/field.h>
16 #include <babeltrace2/trace-ir/event.h>
17 #include "lib/trace-ir/event.h"
18 #include <babeltrace2/trace-ir/packet.h>
19 #include "lib/trace-ir/packet.h"
20 #include "lib/trace-ir/stream.h"
21 #include <babeltrace2/trace-ir/clock-class.h>
22 #include <babeltrace2/trace-ir/stream-class.h>
23 #include <babeltrace2/trace-ir/stream.h>
24 #include <babeltrace2/graph/connection.h>
25 #include <babeltrace2/graph/component.h>
26 #include <babeltrace2/graph/message.h>
27 #include <babeltrace2/graph/self-component.h>
28 #include <babeltrace2/graph/port.h>
29 #include <babeltrace2/graph/graph.h>
30 #include <babeltrace2/graph/message-iterator.h>
31 #include <babeltrace2/types.h>
32 #include "common/assert.h"
33 #include "lib/assert-cond.h"
34 #include <stdint.h>
35 #include <inttypes.h>
36 #include <stdbool.h>
37 #include <stdlib.h>
38
39 #include "component-class.h"
40 #include "component.h"
41 #include "component-sink.h"
42 #include "component-source.h"
43 #include "connection.h"
44 #include "graph.h"
45 #include "message-iterator-class.h"
46 #include "message/discarded-items.h"
47 #include "message/event.h"
48 #include "message/iterator.h"
49 #include "message/message.h"
50 #include "message/message-iterator-inactivity.h"
51 #include "message/stream.h"
52 #include "message/packet.h"
53 #include "lib/func-status.h"
54
55 /*
56 * TODO: Use graph's state (number of active iterators, etc.) and
57 * possibly system specifications to make a better guess than this.
58 */
59 #define MSG_BATCH_SIZE 15
60
61 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
62 BT_ASSERT_PRE("has-state-to-seek", \
63 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
64 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
65 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
66 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
67 "Message iterator is in the wrong state: %!+i", (_iter))
68
69 static inline
70 void set_msg_iterator_state(struct bt_message_iterator *iterator,
71 enum bt_message_iterator_state state)
72 {
73 BT_ASSERT_DBG(iterator);
74 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
75 bt_message_iterator_state_string(state));
76 iterator->state = state;
77 }
78
79 static
80 void bt_message_iterator_destroy(struct bt_object *obj)
81 {
82 struct bt_message_iterator *iterator;
83
84 BT_ASSERT(obj);
85
86 /*
87 * The message iterator's reference count is 0 if we're
88 * here. Increment it to avoid a double-destroy (possibly
89 * infinitely recursive). This could happen for example if the
90 * message iterator's finalization function does
91 * bt_object_get_ref() (or anything that causes
92 * bt_object_get_ref() to be called) on itself (ref. count goes
93 * from 0 to 1), and then bt_object_put_ref(): the reference
94 * count would go from 1 to 0 again and this function would be
95 * called again.
96 */
97 obj->ref_count++;
98 iterator = (void *) obj;
99 BT_LIB_LOGI("Destroying self component input port message iterator object: "
100 "%!+i", iterator);
101 bt_message_iterator_try_finalize(iterator);
102
103 if (iterator->connection) {
104 /*
105 * Remove ourself from the originating connection so
106 * that it does not try to finalize a dangling pointer
107 * later.
108 */
109 bt_connection_remove_iterator(iterator->connection, iterator);
110 iterator->connection = NULL;
111 }
112
113 if (iterator->auto_seek.msgs) {
114 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
115 bt_object_put_ref_no_null_check(
116 g_queue_pop_tail(iterator->auto_seek.msgs));
117 }
118
119 g_queue_free(iterator->auto_seek.msgs);
120 iterator->auto_seek.msgs = NULL;
121 }
122
123 if (iterator->upstream_msg_iters) {
124 /*
125 * At this point the message iterator is finalized, so
126 * it's detached from any upstream message iterator.
127 */
128 BT_ASSERT(iterator->upstream_msg_iters->len == 0);
129 g_ptr_array_free(iterator->upstream_msg_iters, TRUE);
130 iterator->upstream_msg_iters = NULL;
131 }
132
133 if (iterator->msgs) {
134 g_ptr_array_free(iterator->msgs, TRUE);
135 iterator->msgs = NULL;
136 }
137
138 g_free(iterator);
139 }
140
141 void bt_message_iterator_try_finalize(
142 struct bt_message_iterator *iterator)
143 {
144 uint64_t i;
145 bool call_user_finalize = true;
146
147 BT_ASSERT(iterator);
148
149 switch (iterator->state) {
150 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
151 /*
152 * If this function is called while the iterator is in the
153 * NON_INITIALIZED state, it means the user initialization
154 * method has either not been called, or has failed. We
155 * therefore don't want to call the user finalization method.
156 * However, the initialization method might have created some
157 * upstream message iterators before failing, so we want to
158 * execute the rest of this function, which unlinks the related
159 * iterators.
160 */
161 call_user_finalize = false;
162 break;
163 case BT_MESSAGE_ITERATOR_STATE_FINALIZED:
164 /* Already finalized */
165 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
166 "%!+i", iterator);
167 goto end;
168 case BT_MESSAGE_ITERATOR_STATE_FINALIZING:
169 /* Finalizing */
170 BT_LIB_LOGF("Message iterator is already being finalized: "
171 "%!+i", iterator);
172 bt_common_abort();
173 default:
174 break;
175 }
176
177 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
178 set_msg_iterator_state(iterator,
179 BT_MESSAGE_ITERATOR_STATE_FINALIZING);
180 BT_ASSERT(iterator->upstream_component);
181
182 /* Call user-defined destroy method */
183 if (call_user_finalize) {
184 typedef void (*method_t)(void *);
185 method_t method;
186 struct bt_component_class *comp_class =
187 iterator->upstream_component->class;
188 struct bt_component_class_with_iterator_class *class_with_iter_class;
189
190 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class));
191 class_with_iter_class = container_of(comp_class,
192 struct bt_component_class_with_iterator_class, parent);
193 method = (method_t) class_with_iter_class->msg_iter_cls->methods.finalize;
194
195 if (method) {
196 const bt_error *saved_error;
197
198 saved_error = bt_current_thread_take_error();
199
200 BT_LIB_LOGD("Calling user's finalization method: %!+i",
201 iterator);
202 method(iterator);
203 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
204
205 if (saved_error) {
206 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
207 }
208 }
209 }
210
211 /* Detach upstream message iterators */
212 for (i = 0; i < iterator->upstream_msg_iters->len; i++) {
213 struct bt_message_iterator *upstream_msg_iter =
214 iterator->upstream_msg_iters->pdata[i];
215
216 upstream_msg_iter->downstream_msg_iter = NULL;
217 }
218
219 g_ptr_array_set_size(iterator->upstream_msg_iters, 0);
220
221 /* Detach downstream message iterator */
222 if (iterator->downstream_msg_iter) {
223 gboolean existed;
224
225 BT_ASSERT(iterator->downstream_msg_iter->upstream_msg_iters);
226 existed = g_ptr_array_remove_fast(
227 iterator->downstream_msg_iter->upstream_msg_iters,
228 iterator);
229 BT_ASSERT(existed);
230 }
231
232 iterator->upstream_component = NULL;
233 iterator->upstream_port = NULL;
234 set_msg_iterator_state(iterator,
235 BT_MESSAGE_ITERATOR_STATE_FINALIZED);
236 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
237
238 end:
239 return;
240 }
241
242 void bt_message_iterator_set_connection(
243 struct bt_message_iterator *iterator,
244 struct bt_connection *connection)
245 {
246 BT_ASSERT(iterator);
247 iterator->connection = connection;
248 BT_LIB_LOGI("Set message iterator's connection: "
249 "%![iter-]+i, %![conn-]+x", iterator, connection);
250 }
251
252 static
253 enum bt_message_iterator_can_seek_beginning_status can_seek_ns_from_origin_true(
254 struct bt_message_iterator *iterator __attribute__((unused)),
255 int64_t ns_from_origin __attribute__((unused)),
256 bt_bool *can_seek)
257 {
258 *can_seek = BT_TRUE;
259
260 return BT_FUNC_STATUS_OK;
261 }
262
263 static
264 enum bt_message_iterator_can_seek_beginning_status can_seek_beginning_true(
265 struct bt_message_iterator *iterator __attribute__((unused)),
266 bt_bool *can_seek)
267 {
268 *can_seek = BT_TRUE;
269
270 return BT_FUNC_STATUS_OK;
271 }
272
273 static
274 int create_self_component_input_port_message_iterator(
275 struct bt_self_message_iterator *self_downstream_msg_iter,
276 struct bt_self_component_port_input *self_port,
277 struct bt_message_iterator **message_iterator,
278 const char *api_func)
279 {
280 bt_message_iterator_class_initialize_method init_method = NULL;
281 struct bt_message_iterator *iterator =
282 NULL;
283 struct bt_message_iterator *downstream_msg_iter =
284 (void *) self_downstream_msg_iter;
285 struct bt_port *port = (void *) self_port;
286 struct bt_port *upstream_port;
287 struct bt_component *comp;
288 struct bt_component *upstream_comp;
289 struct bt_component_class *upstream_comp_cls;
290 struct bt_component_class_with_iterator_class *upstream_comp_cls_with_iter_cls;
291 int status;
292
293 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func, "message-iterator-output",
294 message_iterator, "Created message iterator (output)");
295 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func, "input-port", port,
296 "Input port");
297 comp = bt_port_borrow_component_inline(port);
298 BT_ASSERT_PRE_FROM_FUNC(api_func, "input-port-is-connected",
299 bt_port_is_connected(port),
300 "Input port is not connected: %![port-]+p", port);
301 BT_ASSERT_PRE_FROM_FUNC(api_func, "input-port-has-component",
302 comp, "Input port is not part of a component: %![port-]+p",
303 port);
304 BT_ASSERT(port->connection);
305 upstream_port = port->connection->upstream_port;
306 BT_ASSERT(upstream_port);
307 upstream_comp = bt_port_borrow_component_inline(upstream_port);
308 BT_ASSERT(upstream_comp);
309 BT_ASSERT_PRE_FROM_FUNC(api_func, "graph-is-configured",
310 bt_component_borrow_graph(upstream_comp)->config_state ==
311 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED ||
312 bt_component_borrow_graph(upstream_comp)->config_state ==
313 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED,
314 "Graph is not configured: %!+g",
315 bt_component_borrow_graph(upstream_comp));
316 upstream_comp_cls = upstream_comp->class;
317 BT_ASSERT(upstream_comp->class->type ==
318 BT_COMPONENT_CLASS_TYPE_SOURCE ||
319 upstream_comp->class->type ==
320 BT_COMPONENT_CLASS_TYPE_FILTER);
321 BT_LIB_LOGI("Creating message iterator on self component input port: "
322 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
323 iterator = g_new0(
324 struct bt_message_iterator, 1);
325 if (!iterator) {
326 BT_LIB_LOGE_APPEND_CAUSE(
327 "Failed to allocate one self component input port "
328 "message iterator.");
329 status = BT_FUNC_STATUS_MEMORY_ERROR;
330 goto error;
331 }
332
333 bt_object_init_shared(&iterator->base,
334 bt_message_iterator_destroy);
335 iterator->msgs = g_ptr_array_new();
336 if (!iterator->msgs) {
337 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
338 status = BT_FUNC_STATUS_MEMORY_ERROR;
339 goto error;
340 }
341
342 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
343 iterator->last_ns_from_origin = INT64_MIN;
344 iterator->auto_seek.msgs = g_queue_new();
345 if (!iterator->auto_seek.msgs) {
346 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
347 status = BT_FUNC_STATUS_MEMORY_ERROR;
348 goto error;
349 }
350
351 iterator->upstream_msg_iters = g_ptr_array_new();
352 if (!iterator->upstream_msg_iters) {
353 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
354 status = BT_FUNC_STATUS_MEMORY_ERROR;
355 goto error;
356 }
357
358 iterator->upstream_component = upstream_comp;
359 iterator->upstream_port = upstream_port;
360 iterator->connection = iterator->upstream_port->connection;
361 iterator->graph = bt_component_borrow_graph(upstream_comp);
362 set_msg_iterator_state(iterator,
363 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
364
365 /* Copy methods from the message iterator class to the message iterator. */
366 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls));
367 upstream_comp_cls_with_iter_cls = container_of(upstream_comp_cls,
368 struct bt_component_class_with_iterator_class, parent);
369
370 iterator->methods.next =
371 (bt_message_iterator_next_method)
372 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.next;
373 iterator->methods.seek_ns_from_origin =
374 (bt_message_iterator_seek_ns_from_origin_method)
375 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_ns_from_origin;
376 iterator->methods.seek_beginning =
377 (bt_message_iterator_seek_beginning_method)
378 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_beginning;
379 iterator->methods.can_seek_ns_from_origin =
380 (bt_message_iterator_can_seek_ns_from_origin_method)
381 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
382 iterator->methods.can_seek_beginning =
383 (bt_message_iterator_can_seek_beginning_method)
384 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_beginning;
385
386 if (iterator->methods.seek_ns_from_origin &&
387 !iterator->methods.can_seek_ns_from_origin) {
388 iterator->methods.can_seek_ns_from_origin =
389 (bt_message_iterator_can_seek_ns_from_origin_method)
390 can_seek_ns_from_origin_true;
391 }
392
393 if (iterator->methods.seek_beginning &&
394 !iterator->methods.can_seek_beginning) {
395 iterator->methods.can_seek_beginning =
396 (bt_message_iterator_can_seek_beginning_method)
397 can_seek_beginning_true;
398 }
399
400 /* Call iterator's init method. */
401 init_method = upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.initialize;
402
403 if (init_method) {
404 enum bt_message_iterator_class_initialize_method_status iter_status;
405
406 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
407 iter_status = init_method(
408 (struct bt_self_message_iterator *) iterator,
409 &iterator->config,
410 (struct bt_self_component_port_output *) upstream_port);
411 BT_LOGD("User method returned: status=%s",
412 bt_common_func_status_string(iter_status));
413 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
414 "bt_message_iterator_class_initialize_method",
415 iter_status);
416 if (iter_status != BT_FUNC_STATUS_OK) {
417 BT_LIB_LOGW_APPEND_CAUSE(
418 "Component input port message iterator initialization method failed: "
419 "%![iter-]+i, status=%s",
420 iterator,
421 bt_common_func_status_string(iter_status));
422 status = iter_status;
423 goto error;
424 }
425
426 iterator->config.frozen = true;
427 }
428
429 if (downstream_msg_iter) {
430 /* Set this message iterator's downstream message iterator */
431 iterator->downstream_msg_iter = downstream_msg_iter;
432
433 /*
434 * Add this message iterator to the downstream message
435 * iterator's array of upstream message iterators.
436 */
437 g_ptr_array_add(downstream_msg_iter->upstream_msg_iters,
438 iterator);
439 }
440
441 set_msg_iterator_state(iterator,
442 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
443 g_ptr_array_add(port->connection->iterators, iterator);
444 BT_LIB_LOGI("Created message iterator on self component input port: "
445 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
446 upstream_port, upstream_comp, iterator);
447
448 *message_iterator = iterator;
449 status = BT_FUNC_STATUS_OK;
450 goto end;
451
452 error:
453 BT_OBJECT_PUT_REF_AND_RESET(iterator);
454
455 end:
456 return status;
457 }
458
459 BT_EXPORT
460 bt_message_iterator_create_from_message_iterator_status
461 bt_message_iterator_create_from_message_iterator(
462 struct bt_self_message_iterator *self_msg_iter,
463 struct bt_self_component_port_input *input_port,
464 struct bt_message_iterator **message_iterator)
465 {
466 BT_ASSERT_PRE_NO_ERROR();
467 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter);
468 return create_self_component_input_port_message_iterator(self_msg_iter,
469 input_port, message_iterator, __func__);
470 }
471
472 BT_EXPORT
473 bt_message_iterator_create_from_sink_component_status
474 bt_message_iterator_create_from_sink_component(
475 struct bt_self_component_sink *self_comp,
476 struct bt_self_component_port_input *input_port,
477 struct bt_message_iterator **message_iterator)
478 {
479 BT_ASSERT_PRE_NO_ERROR();
480 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp, "Sink component");
481 return create_self_component_input_port_message_iterator(NULL,
482 input_port, message_iterator, __func__);
483 }
484
485 BT_EXPORT
486 void *bt_self_message_iterator_get_data(
487 const struct bt_self_message_iterator *self_iterator)
488 {
489 struct bt_message_iterator *iterator =
490 (void *) self_iterator;
491
492 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
493 return iterator->user_data;
494 }
495
496 BT_EXPORT
497 void bt_self_message_iterator_set_data(
498 struct bt_self_message_iterator *self_iterator, void *data)
499 {
500 struct bt_message_iterator *iterator =
501 (void *) self_iterator;
502
503 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
504 iterator->user_data = data;
505 BT_LIB_LOGD("Set message iterator's user data: "
506 "%!+i, user-data-addr=%p", iterator, data);
507 }
508
509 BT_EXPORT
510 void bt_self_message_iterator_configuration_set_can_seek_forward(
511 bt_self_message_iterator_configuration *config,
512 bt_bool can_seek_forward)
513 {
514 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config,
515 "Message iterator configuration");
516 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config,
517 "Message iterator configuration", "");
518
519 config->can_seek_forward = can_seek_forward;
520 }
521
522 /*
523 * Validate that the default clock snapshot in `msg` doesn't make us go back in
524 * time.
525 */
526
527 BT_ASSERT_COND_DEV_FUNC
528 static
529 bool clock_snapshots_are_monotonic_one(
530 struct bt_message_iterator *iterator,
531 const bt_message *msg)
532 {
533 const struct bt_clock_snapshot *clock_snapshot = NULL;
534 bt_message_type message_type = bt_message_get_type(msg);
535 int64_t ns_from_origin;
536 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status;
537
538 /*
539 * The default is true: if we can't figure out the clock snapshot
540 * (or there is none), assume it is fine.
541 */
542 bool result = true;
543
544 switch (message_type) {
545 case BT_MESSAGE_TYPE_EVENT:
546 {
547 struct bt_message_event *event_msg = (struct bt_message_event *) msg;
548 clock_snapshot = event_msg->default_cs;
549 break;
550 }
551 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
552 {
553 struct bt_message_message_iterator_inactivity *inactivity_msg =
554 (struct bt_message_message_iterator_inactivity *) msg;
555 clock_snapshot = inactivity_msg->cs;
556 break;
557 }
558 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
559 case BT_MESSAGE_TYPE_PACKET_END:
560 {
561 struct bt_message_packet *packet_msg = (struct bt_message_packet *) msg;
562 clock_snapshot = packet_msg->default_cs;
563 break;
564 }
565 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
566 case BT_MESSAGE_TYPE_STREAM_END:
567 {
568 struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
569 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
570 goto end;
571 }
572
573 clock_snapshot = stream_msg->default_cs;
574 break;
575 }
576 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
577 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
578 {
579 struct bt_message_discarded_items *discarded_msg =
580 (struct bt_message_discarded_items *) msg;
581
582 clock_snapshot = discarded_msg->default_begin_cs;
583 break;
584 }
585 }
586
587 if (!clock_snapshot) {
588 goto end;
589 }
590
591 clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(
592 clock_snapshot, &ns_from_origin);
593 if (clock_snapshot_status != BT_FUNC_STATUS_OK) {
594 /*
595 * bt_clock_snapshot_get_ns_from_origin can return
596 * OVERFLOW_ERROR. We don't really want to report an error to
597 * our caller, so just clear it.
598 */
599 bt_current_thread_clear_error();
600 goto end;
601 }
602
603 result = ns_from_origin >= iterator->last_ns_from_origin;
604 iterator->last_ns_from_origin = ns_from_origin;
605 end:
606 return result;
607 }
608
609 BT_ASSERT_COND_DEV_FUNC
610 static
611 bool clock_snapshots_are_monotonic(
612 struct bt_message_iterator *iterator,
613 bt_message_array_const msgs, uint64_t msg_count)
614 {
615 uint64_t i;
616 bool result;
617
618 for (i = 0; i < msg_count; i++) {
619 if (!clock_snapshots_are_monotonic_one(iterator, msgs[i])) {
620 result = false;
621 goto end;
622 }
623 }
624
625 result = true;
626
627 end:
628 return result;
629 }
630
631 /*
632 * When a new stream begins, verify that the clock class tied to this
633 * stream is compatible with what we've seen before.
634 */
635
636 BT_ASSERT_COND_DEV_FUNC
637 static
638 bool clock_classes_are_compatible_one(struct bt_message_iterator *iterator,
639 const struct bt_message *msg)
640 {
641 enum bt_message_type message_type = bt_message_get_type(msg);
642 bool result;
643
644 if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) {
645 const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
646 const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class;
647 bt_uuid clock_class_uuid = NULL;
648
649 if (clock_class) {
650 clock_class_uuid = bt_clock_class_get_uuid(clock_class);
651 }
652
653 switch (iterator->clock_expectation.type) {
654 case CLOCK_EXPECTATION_UNSET:
655 /*
656 * This is the first time we see a message with a clock
657 * snapshot: record the properties of that clock, against
658 * which we'll compare the clock properties of the following
659 * messages.
660 */
661
662 if (!clock_class) {
663 iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE;
664 } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
665 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX;
666 } else if (clock_class_uuid) {
667 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID;
668 bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
669 } else {
670 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID;
671 }
672 break;
673
674 case CLOCK_EXPECTATION_NONE:
675 if (clock_class) {
676 BT_ASSERT_COND_DEV_MSG(
677 "Expecting no clock class, got one: %![cc-]+K",
678 clock_class);
679 result = false;
680 goto end;
681 }
682
683 break;
684
685 case CLOCK_EXPECTATION_ORIGIN_UNIX:
686 if (!clock_class) {
687 BT_ASSERT_COND_DEV_MSG(
688 "Expecting a clock class, got none.");
689 result = false;
690 goto end;
691 }
692
693 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
694 BT_ASSERT_COND_DEV_MSG(
695 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
696 clock_class);
697 result = false;
698 goto end;
699 }
700 break;
701
702 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID:
703 if (!clock_class) {
704 BT_ASSERT_COND_DEV_MSG(
705 "Expecting a clock class, got none.");
706 result = false;
707 goto end;
708 }
709
710 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
711 BT_ASSERT_COND_DEV_MSG(
712 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
713 clock_class);
714 result = false;
715 goto end;
716 }
717
718 if (!clock_class_uuid) {
719 BT_ASSERT_COND_DEV_MSG(
720 "Expecting a clock class with UUID: %![cc-]+K",
721 clock_class);
722 result = false;
723 goto end;
724 }
725
726 if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
727 BT_ASSERT_COND_DEV_MSG(
728 "Expecting a clock class with UUID, got one "
729 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
730 clock_class, iterator->clock_expectation.uuid);
731 result = false;
732 goto end;
733 }
734 break;
735
736 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID:
737 if (!clock_class) {
738 BT_ASSERT_COND_DEV_MSG(
739 "Expecting a clock class, got none.");
740 result = false;
741 goto end;
742 }
743
744 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
745 BT_ASSERT_COND_DEV_MSG(
746 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
747 clock_class);
748 result = false;
749 goto end;
750 }
751
752 if (clock_class_uuid) {
753 BT_ASSERT_COND_DEV_MSG(
754 "Expecting a clock class without UUID: %![cc-]+K",
755 clock_class);
756 result = false;
757 goto end;
758 }
759 break;
760 }
761 }
762
763 result = true;
764
765 end:
766 return result;
767 }
768
769 BT_ASSERT_COND_DEV_FUNC
770 static
771 bool clock_classes_are_compatible(
772 struct bt_message_iterator *iterator,
773 bt_message_array_const msgs, uint64_t msg_count)
774 {
775 uint64_t i;
776 bool result;
777
778 for (i = 0; i < msg_count; i++) {
779 if (!clock_classes_are_compatible_one(iterator, msgs[i])) {
780 result = false;
781 goto end;
782 }
783 }
784
785 result = true;
786
787 end:
788 return result;
789 }
790
791 /*
792 * Call the `next` method of the iterator. Do some validation on the returned
793 * messages.
794 */
795
796 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
797
798 static
799 enum bt_message_iterator_class_next_method_status
800 call_iterator_next_method(
801 struct bt_message_iterator *iterator,
802 bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
803 {
804 enum bt_message_iterator_class_next_method_status status;
805
806 BT_ASSERT_DBG(iterator->methods.next);
807 BT_LOGD_STR("Calling user's \"next\" method.");
808 status = iterator->methods.next(iterator, msgs, capacity, user_count);
809 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
810 bt_common_func_status_string(status), *user_count);
811
812 if (status == BT_FUNC_STATUS_OK) {
813 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
814 "message-clock-classes-are-compatible",
815 clock_classes_are_compatible(iterator, msgs,
816 *user_count),
817 "Clocks are not compatible");
818 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
819 "message-clock-snapshots-are-monotonic",
820 clock_snapshots_are_monotonic(iterator, msgs,
821 *user_count),
822 "Clock snapshots are not monotonic");
823 }
824
825 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME,
826 status);
827
828 return status;
829 }
830
831 BT_EXPORT
832 enum bt_message_iterator_next_status
833 bt_message_iterator_next(
834 struct bt_message_iterator *iterator,
835 bt_message_array_const *msgs, uint64_t *user_count)
836 {
837 enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
838
839 BT_ASSERT_PRE_DEV_NO_ERROR();
840 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
841 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs,
842 "Message array (output)");
843 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count,
844 "Message count (output)");
845 BT_ASSERT_PRE_DEV("message-iterator-is-active",
846 iterator->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE,
847 "Message iterator's \"next\" called, but "
848 "message iterator is in the wrong state: %!+i", iterator);
849 BT_ASSERT_DBG(iterator->upstream_component);
850 BT_ASSERT_DBG(iterator->upstream_component->class);
851 BT_ASSERT_PRE_DEV("graph-is-configured",
852 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
853 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
854 "Graph is not configured: %!+g",
855 bt_component_borrow_graph(iterator->upstream_component));
856 BT_LIB_LOGD("Getting next self component input port "
857 "message iterator's messages: %!+i, batch-size=%u",
858 iterator, MSG_BATCH_SIZE);
859
860 /*
861 * Call the user's "next" method to get the next messages
862 * and status.
863 */
864 *user_count = 0;
865 status = (int) call_iterator_next_method(iterator,
866 (void *) iterator->msgs->pdata, MSG_BATCH_SIZE,
867 user_count);
868 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
869 bt_common_func_status_string(status), *user_count);
870 if (status < 0) {
871 BT_LIB_LOGW_APPEND_CAUSE(
872 "Component input port message iterator's \"next\" method failed: "
873 "%![iter-]+i, status=%s",
874 iterator, bt_common_func_status_string(status));
875 goto end;
876 }
877
878 /*
879 * There is no way that this iterator could have been finalized
880 * during its "next" method, as the only way to do this is to
881 * put the last iterator's reference, and this can only be done
882 * by its downstream owner.
883 *
884 * For the same reason, there is no way that this iterator could
885 * have seeked (cannot seek a self message iterator).
886 */
887 BT_ASSERT_DBG(iterator->state ==
888 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
889
890 switch (status) {
891 case BT_FUNC_STATUS_OK:
892 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME, "count-lteq-capacity",
893 *user_count <= MSG_BATCH_SIZE,
894 "Invalid returned message count: greater than "
895 "batch size: count=%" PRIu64 ", batch-size=%u",
896 *user_count, MSG_BATCH_SIZE);
897 *msgs = (void *) iterator->msgs->pdata;
898 break;
899 case BT_FUNC_STATUS_AGAIN:
900 goto end;
901 case BT_FUNC_STATUS_END:
902 set_msg_iterator_state(iterator,
903 BT_MESSAGE_ITERATOR_STATE_ENDED);
904 goto end;
905 default:
906 /* Unknown non-error status */
907 bt_common_abort();
908 }
909
910 end:
911 return status;
912 }
913
914 BT_EXPORT
915 struct bt_component *
916 bt_message_iterator_borrow_component(
917 struct bt_message_iterator *iterator)
918 {
919 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
920 return iterator->upstream_component;
921 }
922
923 BT_EXPORT
924 struct bt_self_component *bt_self_message_iterator_borrow_component(
925 struct bt_self_message_iterator *self_iterator)
926 {
927 struct bt_message_iterator *iterator =
928 (void *) self_iterator;
929
930 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
931 return (void *) iterator->upstream_component;
932 }
933
934 BT_EXPORT
935 struct bt_self_component_port_output *bt_self_message_iterator_borrow_port(
936 struct bt_self_message_iterator *self_iterator)
937 {
938 struct bt_message_iterator *iterator =
939 (void *) self_iterator;
940
941 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
942 return (void *) iterator->upstream_port;
943 }
944
945 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
946 "bt_message_iterator_class_can_seek_ns_from_origin_method"
947
948 BT_EXPORT
949 enum bt_message_iterator_can_seek_ns_from_origin_status
950 bt_message_iterator_can_seek_ns_from_origin(
951 struct bt_message_iterator *iterator,
952 int64_t ns_from_origin, bt_bool *can_seek)
953 {
954 enum bt_message_iterator_can_seek_ns_from_origin_status status;
955
956 BT_ASSERT_PRE_NO_ERROR();
957 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
958 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek);
959 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
960 BT_ASSERT_PRE("graph-is-configured",
961 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
962 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
963 "Graph is not configured: %!+g",
964 bt_component_borrow_graph(iterator->upstream_component));
965
966 if (iterator->methods.can_seek_ns_from_origin) {
967 /*
968 * Initialize to an invalid value, so we can post-assert that
969 * the method returned a valid value.
970 */
971 *can_seek = -1;
972
973 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
974 iterator);
975
976 status = (int) iterator->methods.can_seek_ns_from_origin(iterator,
977 ns_from_origin, can_seek);
978
979 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
980 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME, status);
981
982 if (status != BT_FUNC_STATUS_OK) {
983 BT_LIB_LOGW_APPEND_CAUSE(
984 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
985 "%![iter-]+i, status=%s",
986 iterator, bt_common_func_status_string(status));
987 goto end;
988 }
989
990 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME,
991 "valid-return-value",
992 *can_seek == BT_TRUE || *can_seek == BT_FALSE,
993 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
994 *can_seek, iterator);
995
996 BT_LIB_LOGD(
997 "User's \"can seek nanoseconds from origin\" returned successfully: "
998 "%![iter-]+i, can-seek=%d",
999 iterator, *can_seek);
1000
1001 if (*can_seek) {
1002 goto end;
1003 }
1004 }
1005
1006 /*
1007 * Automatic seeking fall back: if we can seek to the beginning and the
1008 * iterator supports forward seeking then we can automatically seek to
1009 * any timestamp.
1010 */
1011 status = (int) bt_message_iterator_can_seek_beginning(
1012 iterator, can_seek);
1013 if (status != BT_FUNC_STATUS_OK) {
1014 goto end;
1015 }
1016
1017 *can_seek = *can_seek && iterator->config.can_seek_forward;
1018
1019 end:
1020 return status;
1021 }
1022
1023 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1024 "bt_message_iterator_class_can_seek_beginning"
1025
1026 BT_EXPORT
1027 enum bt_message_iterator_can_seek_beginning_status
1028 bt_message_iterator_can_seek_beginning(
1029 struct bt_message_iterator *iterator,
1030 bt_bool *can_seek)
1031 {
1032 enum bt_message_iterator_can_seek_beginning_status status;
1033
1034 BT_ASSERT_PRE_NO_ERROR();
1035 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1036 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek);
1037 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1038 BT_ASSERT_PRE("graph-is-configured",
1039 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1040 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1041 "Graph is not configured: %!+g",
1042 bt_component_borrow_graph(iterator->upstream_component));
1043
1044 if (iterator->methods.can_seek_beginning) {
1045 /*
1046 * Initialize to an invalid value, so we can post-assert that
1047 * the method returned a valid value.
1048 */
1049 *can_seek = -1;
1050
1051 status = (int) iterator->methods.can_seek_beginning(iterator, can_seek);
1052
1053 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME,
1054 "valid-return-value",
1055 status != BT_FUNC_STATUS_OK ||
1056 *can_seek == BT_TRUE ||
1057 *can_seek == BT_FALSE,
1058 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1059 *can_seek, iterator);
1060 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1061 CAN_SEEK_BEGINNING_METHOD_NAME, status);
1062 } else {
1063 *can_seek = BT_FALSE;
1064 status = BT_FUNC_STATUS_OK;
1065 }
1066
1067 return status;
1068 }
1069
1070 static inline
1071 void set_iterator_state_after_seeking(
1072 struct bt_message_iterator *iterator,
1073 int status)
1074 {
1075 enum bt_message_iterator_state new_state = 0;
1076
1077 /* Set iterator's state depending on seeking status */
1078 switch (status) {
1079 case BT_FUNC_STATUS_OK:
1080 new_state = BT_MESSAGE_ITERATOR_STATE_ACTIVE;
1081 break;
1082 case BT_FUNC_STATUS_AGAIN:
1083 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
1084 break;
1085 case BT_FUNC_STATUS_ERROR:
1086 case BT_FUNC_STATUS_MEMORY_ERROR:
1087 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
1088 break;
1089 case BT_FUNC_STATUS_END:
1090 new_state = BT_MESSAGE_ITERATOR_STATE_ENDED;
1091 break;
1092 default:
1093 bt_common_abort();
1094 }
1095
1096 set_msg_iterator_state(iterator, new_state);
1097 }
1098
1099 static
1100 void reset_iterator_expectations(
1101 struct bt_message_iterator *iterator)
1102 {
1103 iterator->last_ns_from_origin = INT64_MIN;
1104 iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET;
1105 }
1106
1107 static
1108 bool message_iterator_can_seek_beginning(
1109 struct bt_message_iterator *iterator)
1110 {
1111 enum bt_message_iterator_can_seek_beginning_status status;
1112 bt_bool can_seek;
1113
1114 status = bt_message_iterator_can_seek_beginning(
1115 iterator, &can_seek);
1116 if (status != BT_FUNC_STATUS_OK) {
1117 can_seek = BT_FALSE;
1118 }
1119
1120 return can_seek;
1121 }
1122
1123 #define SEEK_BEGINNING_METHOD_NAME \
1124 "bt_message_iterator_class_seek_beginning_method"
1125
1126 BT_EXPORT
1127 enum bt_message_iterator_seek_beginning_status
1128 bt_message_iterator_seek_beginning(struct bt_message_iterator *iterator)
1129 {
1130 int status;
1131
1132 BT_ASSERT_PRE_NO_ERROR();
1133 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1134 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1135 BT_ASSERT_PRE("graph-is-configured",
1136 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1137 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1138 "Graph is not configured: %!+g",
1139 bt_component_borrow_graph(iterator->upstream_component));
1140 BT_ASSERT_PRE("can-seek-beginning",
1141 message_iterator_can_seek_beginning(iterator),
1142 "Message iterator cannot seek beginning: %!+i", iterator);
1143
1144 /*
1145 * We are seeking, reset our expectations about how the following
1146 * messages should look like.
1147 */
1148 reset_iterator_expectations(iterator);
1149
1150 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
1151 set_msg_iterator_state(iterator,
1152 BT_MESSAGE_ITERATOR_STATE_SEEKING);
1153 status = iterator->methods.seek_beginning(iterator);
1154 BT_LOGD("User method returned: status=%s",
1155 bt_common_func_status_string(status));
1156 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME, "valid-status",
1157 status == BT_FUNC_STATUS_OK ||
1158 status == BT_FUNC_STATUS_ERROR ||
1159 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1160 status == BT_FUNC_STATUS_AGAIN,
1161 "Unexpected status: %![iter-]+i, status=%s",
1162 iterator, bt_common_func_status_string(status));
1163 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME,
1164 status);
1165 if (status < 0) {
1166 BT_LIB_LOGW_APPEND_CAUSE(
1167 "Component input port message iterator's \"seek beginning\" method failed: "
1168 "%![iter-]+i, status=%s",
1169 iterator, bt_common_func_status_string(status));
1170 }
1171
1172 set_iterator_state_after_seeking(iterator, status);
1173 return status;
1174 }
1175
1176 BT_EXPORT
1177 bt_bool
1178 bt_message_iterator_can_seek_forward(
1179 bt_message_iterator *iterator)
1180 {
1181 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1182
1183 return iterator->config.can_seek_forward;
1184 }
1185
1186 /*
1187 * Structure used to record the state of a given stream during the fast-forward
1188 * phase of an auto-seek.
1189 */
1190 struct auto_seek_stream_state {
1191 /*
1192 * Value representing which step of this timeline we are at.
1193 *
1194 * time --->
1195 * [SB] 1 [PB] 2 [PE] 1 [SE]
1196 *
1197 * At each point in the timeline, the messages we need to replicate are:
1198 *
1199 * 1: Stream beginning
1200 * 2: Stream beginning, packet beginning
1201 *
1202 * Before "Stream beginning" and after "Stream end", we don't need to
1203 * replicate anything as the stream doesn't exist.
1204 */
1205 enum {
1206 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN,
1207 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN,
1208 } state;
1209
1210 /*
1211 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1212 * in. This is a weak reference, since the packet will always be
1213 * alive by the time we use it.
1214 */
1215 struct bt_packet *packet;
1216
1217 /* Have we see a message with a clock snapshot yet? */
1218 bool seen_clock_snapshot;
1219 };
1220
1221 static
1222 struct auto_seek_stream_state *create_auto_seek_stream_state(void)
1223 {
1224 return g_new0(struct auto_seek_stream_state, 1);
1225 }
1226
1227 static
1228 void destroy_auto_seek_stream_state(void *ptr)
1229 {
1230 g_free(ptr);
1231 }
1232
1233 static
1234 GHashTable *create_auto_seek_stream_states(void)
1235 {
1236 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
1237 destroy_auto_seek_stream_state);
1238 }
1239
1240 static
1241 void destroy_auto_seek_stream_states(GHashTable *stream_states)
1242 {
1243 g_hash_table_destroy(stream_states);
1244 }
1245
1246 /*
1247 * Handle one message while we are in the fast-forward phase of an auto-seek.
1248 *
1249 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1250 * `ns_from_origin`. In other words, if this is the first message after our
1251 * seek point.
1252 *
1253 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1254 * `struct auto_seek_stream_state` used to keep the state of each stream
1255 * during the fast-forward.
1256 */
1257
1258 static inline
1259 int auto_seek_handle_message(
1260 struct bt_message_iterator *iterator,
1261 int64_t ns_from_origin, const struct bt_message *msg,
1262 bool *got_first, GHashTable *stream_states)
1263 {
1264 int status = BT_FUNC_STATUS_OK;
1265 int64_t msg_ns_from_origin;
1266 const struct bt_clock_snapshot *clk_snapshot = NULL;
1267 int ret;
1268
1269 BT_ASSERT_DBG(msg);
1270 BT_ASSERT_DBG(got_first);
1271
1272 switch (msg->type) {
1273 case BT_MESSAGE_TYPE_EVENT:
1274 {
1275 const struct bt_message_event *event_msg =
1276 (const void *) msg;
1277
1278 clk_snapshot = event_msg->default_cs;
1279 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1280 "event-message-has-default-clock-snapshot",
1281 clk_snapshot,
1282 "Event message has no default clock snapshot: %!+n",
1283 event_msg);
1284 break;
1285 }
1286 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
1287 {
1288 const struct bt_message_message_iterator_inactivity *inactivity_msg =
1289 (const void *) msg;
1290
1291 clk_snapshot = inactivity_msg->cs;
1292 BT_ASSERT_DBG(clk_snapshot);
1293 break;
1294 }
1295 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1296 case BT_MESSAGE_TYPE_PACKET_END:
1297 {
1298 const struct bt_message_packet *packet_msg =
1299 (const void *) msg;
1300
1301 if (msg->type == BT_MESSAGE_TYPE_PACKET_BEGINNING
1302 && !packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1303 goto skip_msg;
1304 }
1305
1306 if (msg->type == BT_MESSAGE_TYPE_PACKET_END
1307 && !packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1308 goto skip_msg;
1309 }
1310
1311 clk_snapshot = packet_msg->default_cs;
1312 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1313 "packet-message-has-default-clock-snapshot",
1314 clk_snapshot,
1315 "Packet message has no default clock snapshot: %!+n",
1316 packet_msg);
1317 break;
1318 }
1319 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1320 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1321 {
1322 struct bt_message_discarded_items *msg_disc_items =
1323 (void *) msg;
1324
1325 if (msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS &&
1326 !msg_disc_items->stream->class->discarded_events_have_default_clock_snapshots) {
1327 goto skip_msg;
1328 }
1329
1330 if (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS &&
1331 !msg_disc_items->stream->class->discarded_packets_have_default_clock_snapshots) {
1332 goto skip_msg;
1333 }
1334
1335 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1336 "discarded-events-packets-message-has-default-clock-snapshot",
1337 msg_disc_items->default_begin_cs &&
1338 msg_disc_items->default_end_cs,
1339 "Discarded events/packets message has no default clock snapshots: %!+n",
1340 msg_disc_items);
1341 ret = bt_clock_snapshot_get_ns_from_origin(
1342 msg_disc_items->default_begin_cs,
1343 &msg_ns_from_origin);
1344 if (ret) {
1345 status = BT_FUNC_STATUS_ERROR;
1346 goto end;
1347 }
1348
1349 if (msg_ns_from_origin >= ns_from_origin) {
1350 *got_first = true;
1351 goto push_msg;
1352 }
1353
1354 ret = bt_clock_snapshot_get_ns_from_origin(
1355 msg_disc_items->default_end_cs,
1356 &msg_ns_from_origin);
1357 if (ret) {
1358 status = BT_FUNC_STATUS_ERROR;
1359 goto end;
1360 }
1361
1362 if (msg_ns_from_origin >= ns_from_origin) {
1363 /*
1364 * The discarded items message's beginning time
1365 * is before the requested seeking time, but its
1366 * end time is after. Modify the message so as
1367 * to set its beginning time to the requested
1368 * seeking time, and make its item count unknown
1369 * as we don't know if items were really
1370 * discarded within the new time range.
1371 */
1372 uint64_t new_begin_raw_value = 0;
1373
1374 ret = bt_clock_class_clock_value_from_ns_from_origin(
1375 msg_disc_items->default_end_cs->clock_class,
1376 ns_from_origin, &new_begin_raw_value);
1377 if (ret) {
1378 status = BT_FUNC_STATUS_ERROR;
1379 goto end;
1380 }
1381
1382 bt_clock_snapshot_set_raw_value(
1383 msg_disc_items->default_begin_cs,
1384 new_begin_raw_value);
1385 msg_disc_items->count.base.avail =
1386 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
1387
1388 /*
1389 * It is safe to push it because its beginning
1390 * time is exactly the requested seeking time.
1391 */
1392 goto push_msg;
1393 } else {
1394 goto skip_msg;
1395 }
1396 }
1397 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1398 case BT_MESSAGE_TYPE_STREAM_END:
1399 {
1400 struct bt_message_stream *stream_msg =
1401 (struct bt_message_stream *) msg;
1402
1403 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1404 /* Ignore */
1405 goto skip_msg;
1406 }
1407
1408 clk_snapshot = stream_msg->default_cs;
1409 break;
1410 }
1411 default:
1412 bt_common_abort();
1413 }
1414
1415 BT_ASSERT_DBG(clk_snapshot);
1416 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1417 &msg_ns_from_origin);
1418 if (ret) {
1419 status = BT_FUNC_STATUS_ERROR;
1420 goto end;
1421 }
1422
1423 if (msg_ns_from_origin >= ns_from_origin) {
1424 *got_first = true;
1425 goto push_msg;
1426 }
1427
1428 skip_msg:
1429 /* This message won't be sent downstream. */
1430 switch (msg->type) {
1431 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1432 {
1433 const struct bt_message_stream *stream_msg = (const void *) msg;
1434 struct auto_seek_stream_state *stream_state;
1435
1436 /* Update stream's state: stream began. */
1437 stream_state = create_auto_seek_stream_state();
1438 if (!stream_state) {
1439 status = BT_FUNC_STATUS_MEMORY_ERROR;
1440 goto end;
1441 }
1442
1443 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
1444
1445 if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1446 stream_state->seen_clock_snapshot = true;
1447 }
1448
1449 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
1450 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
1451 break;
1452 }
1453 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1454 {
1455 const struct bt_message_packet *packet_msg =
1456 (const void *) msg;
1457 struct auto_seek_stream_state *stream_state;
1458
1459 /* Update stream's state: packet began. */
1460 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1461 BT_ASSERT_DBG(stream_state);
1462 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1463 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
1464 BT_ASSERT_DBG(!stream_state->packet);
1465 stream_state->packet = packet_msg->packet;
1466
1467 if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1468 stream_state->seen_clock_snapshot = true;
1469 }
1470
1471 break;
1472 }
1473 case BT_MESSAGE_TYPE_EVENT:
1474 {
1475 const struct bt_message_event *event_msg = (const void *) msg;
1476 struct auto_seek_stream_state *stream_state;
1477
1478 stream_state = g_hash_table_lookup(stream_states,
1479 event_msg->event->stream);
1480 BT_ASSERT_DBG(stream_state);
1481
1482 // HELPME: are we sure that event messages have clock snapshots at this point?
1483 stream_state->seen_clock_snapshot = true;
1484
1485 break;
1486 }
1487 case BT_MESSAGE_TYPE_PACKET_END:
1488 {
1489 const struct bt_message_packet *packet_msg =
1490 (const void *) msg;
1491 struct auto_seek_stream_state *stream_state;
1492
1493 /* Update stream's state: packet ended. */
1494 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1495 BT_ASSERT_DBG(stream_state);
1496 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
1497 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
1498 BT_ASSERT_DBG(stream_state->packet);
1499 stream_state->packet = NULL;
1500
1501 if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1502 stream_state->seen_clock_snapshot = true;
1503 }
1504
1505 break;
1506 }
1507 case BT_MESSAGE_TYPE_STREAM_END:
1508 {
1509 const struct bt_message_stream *stream_msg = (const void *) msg;
1510 struct auto_seek_stream_state *stream_state;
1511
1512 stream_state = g_hash_table_lookup(stream_states, stream_msg->stream);
1513 BT_ASSERT_DBG(stream_state);
1514 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1515 BT_ASSERT_DBG(!stream_state->packet);
1516
1517 /* Update stream's state: this stream doesn't exist anymore. */
1518 g_hash_table_remove(stream_states, stream_msg->stream);
1519 break;
1520 }
1521 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1522 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1523 {
1524 const struct bt_message_discarded_items *discarded_msg =
1525 (const void *) msg;
1526 struct auto_seek_stream_state *stream_state;
1527
1528 stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream);
1529 BT_ASSERT_DBG(stream_state);
1530
1531 if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) ||
1532 (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) {
1533 stream_state->seen_clock_snapshot = true;
1534 }
1535
1536 break;
1537 }
1538 default:
1539 break;
1540 }
1541
1542 bt_object_put_ref_no_null_check(msg);
1543 msg = NULL;
1544 goto end;
1545
1546 push_msg:
1547 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
1548 msg = NULL;
1549
1550 end:
1551 BT_ASSERT_DBG(!msg || status != BT_FUNC_STATUS_OK);
1552 return status;
1553 }
1554
1555 static
1556 int find_message_ge_ns_from_origin(
1557 struct bt_message_iterator *iterator,
1558 int64_t ns_from_origin, GHashTable *stream_states)
1559 {
1560 int status = BT_FUNC_STATUS_OK;
1561 enum bt_message_iterator_state init_state =
1562 iterator->state;
1563 const struct bt_message *messages[MSG_BATCH_SIZE];
1564 uint64_t user_count = 0;
1565 uint64_t i;
1566 bool got_first = false;
1567
1568 BT_ASSERT_DBG(iterator);
1569 memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE);
1570
1571 /*
1572 * Make this iterator temporarily active (not seeking) to call
1573 * the "next" method.
1574 */
1575 set_msg_iterator_state(iterator,
1576 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
1577
1578 BT_ASSERT_DBG(iterator->methods.next);
1579
1580 while (!got_first) {
1581 /*
1582 * Call the user's "next" method to get the next
1583 * messages and status.
1584 */
1585 status = call_iterator_next_method(iterator,
1586 &messages[0], MSG_BATCH_SIZE, &user_count);
1587 BT_LOGD("User method returned: status=%s",
1588 bt_common_func_status_string(status));
1589 if (status < 0) {
1590 BT_LIB_LOGW_APPEND_CAUSE(
1591 "Component input port message iterator's \"next\" method failed: "
1592 "%![iter-]+i, status=%s",
1593 iterator, bt_common_func_status_string(status));
1594 }
1595
1596 /*
1597 * The user's "next" method must not do any action which
1598 * would change the iterator's state.
1599 */
1600 BT_ASSERT_DBG(iterator->state ==
1601 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
1602
1603 switch (status) {
1604 case BT_FUNC_STATUS_OK:
1605 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1606 "count-lteq-capacity",
1607 user_count <= MSG_BATCH_SIZE,
1608 "Invalid returned message count: greater than "
1609 "batch size: count=%" PRIu64 ", batch-size=%u",
1610 user_count, MSG_BATCH_SIZE);
1611 break;
1612 case BT_FUNC_STATUS_AGAIN:
1613 case BT_FUNC_STATUS_ERROR:
1614 case BT_FUNC_STATUS_MEMORY_ERROR:
1615 case BT_FUNC_STATUS_END:
1616 goto end;
1617 default:
1618 bt_common_abort();
1619 }
1620
1621 for (i = 0; i < user_count; i++) {
1622 if (got_first) {
1623 g_queue_push_tail(iterator->auto_seek.msgs,
1624 (void *) messages[i]);
1625 messages[i] = NULL;
1626 continue;
1627 }
1628
1629 status = auto_seek_handle_message(iterator,
1630 ns_from_origin, messages[i], &got_first,
1631 stream_states);
1632 if (status == BT_FUNC_STATUS_OK) {
1633 /* Message was either pushed or moved */
1634 messages[i] = NULL;
1635 } else {
1636 goto end;
1637 }
1638 }
1639 }
1640
1641 end:
1642 for (i = 0; i < user_count; i++) {
1643 if (messages[i]) {
1644 bt_object_put_ref_no_null_check(messages[i]);
1645 }
1646 }
1647
1648 set_msg_iterator_state(iterator, init_state);
1649 return status;
1650 }
1651
1652 /*
1653 * This function is installed as the iterator's next callback after we have
1654 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1655 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1656 * next callback is put back.
1657 */
1658
1659 static
1660 enum bt_message_iterator_class_next_method_status post_auto_seek_next(
1661 struct bt_message_iterator *iterator,
1662 bt_message_array_const msgs, uint64_t capacity,
1663 uint64_t *count)
1664 {
1665 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
1666 *count = 0;
1667
1668 /*
1669 * Move auto-seek messages to the output array (which is this
1670 * iterator's base message array).
1671 */
1672 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
1673 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
1674 capacity--;
1675 (*count)++;
1676 }
1677
1678 BT_ASSERT(*count > 0);
1679
1680 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
1681 /* No more auto-seek messages, restore user's next callback. */
1682 BT_ASSERT(iterator->auto_seek.original_next_callback);
1683 iterator->methods.next = iterator->auto_seek.original_next_callback;
1684 iterator->auto_seek.original_next_callback = NULL;
1685 }
1686
1687 return BT_FUNC_STATUS_OK;
1688 }
1689
1690 static inline
1691 int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class,
1692 int64_t ns_from_origin, uint64_t *raw_value)
1693 {
1694
1695 int64_t cc_offset_s = clock_class->offset_seconds;
1696 uint64_t cc_offset_cycles = clock_class->offset_cycles;
1697 uint64_t cc_freq = clock_class->frequency;
1698
1699 return bt_common_clock_value_from_ns_from_origin(cc_offset_s,
1700 cc_offset_cycles, cc_freq, ns_from_origin, raw_value);
1701 }
1702
1703 static
1704 bool message_iterator_can_seek_ns_from_origin(
1705 struct bt_message_iterator *iterator,
1706 int64_t ns_from_origin)
1707 {
1708 enum bt_message_iterator_can_seek_ns_from_origin_status status;
1709 bt_bool can_seek;
1710
1711 status = bt_message_iterator_can_seek_ns_from_origin(
1712 iterator, ns_from_origin, &can_seek);
1713 if (status != BT_FUNC_STATUS_OK) {
1714 can_seek = BT_FALSE;
1715 }
1716
1717 return can_seek;
1718 }
1719
1720 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1721 "bt_message_iterator_class_seek_ns_from_origin_method"
1722
1723
1724 BT_EXPORT
1725 enum bt_message_iterator_seek_ns_from_origin_status
1726 bt_message_iterator_seek_ns_from_origin(
1727 struct bt_message_iterator *iterator,
1728 int64_t ns_from_origin)
1729 {
1730 int status;
1731 GHashTable *stream_states = NULL;
1732 bt_bool can_seek_by_itself;
1733
1734 BT_ASSERT_PRE_NO_ERROR();
1735 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1736 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1737 BT_ASSERT_PRE("graph-is-configured",
1738 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1739 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1740 "Graph is not configured: %!+g",
1741 bt_component_borrow_graph(iterator->upstream_component));
1742 /* The iterator must be able to seek ns from origin one way or another. */
1743 BT_ASSERT_PRE("can-seek-ns-from-origin",
1744 message_iterator_can_seek_ns_from_origin(iterator, ns_from_origin),
1745 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1746 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
1747 set_msg_iterator_state(iterator,
1748 BT_MESSAGE_ITERATOR_STATE_SEEKING);
1749
1750 /*
1751 * We are seeking, reset our expectations about how the following
1752 * messages should look like.
1753 */
1754 reset_iterator_expectations(iterator);
1755
1756 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1757 if (iterator->methods.can_seek_ns_from_origin) {
1758 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1759 can_seek_status;
1760
1761 can_seek_status =
1762 iterator->methods.can_seek_ns_from_origin(
1763 iterator, ns_from_origin, &can_seek_by_itself);
1764 if (can_seek_status != BT_FUNC_STATUS_OK) {
1765 status = can_seek_status;
1766 goto end;
1767 }
1768 } else {
1769 can_seek_by_itself = false;
1770 }
1771
1772 if (can_seek_by_itself) {
1773 /* The iterator knows how to seek to a particular time, let it handle this. */
1774 BT_ASSERT(iterator->methods.seek_ns_from_origin);
1775 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1776 "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin);
1777 status = iterator->methods.seek_ns_from_origin(iterator,
1778 ns_from_origin);
1779 BT_LOGD("User method returned: status=%s",
1780 bt_common_func_status_string(status));
1781 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME, "valid-status",
1782 status == BT_FUNC_STATUS_OK ||
1783 status == BT_FUNC_STATUS_ERROR ||
1784 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1785 status == BT_FUNC_STATUS_AGAIN,
1786 "Unexpected status: %![iter-]+i, status=%s",
1787 iterator, bt_common_func_status_string(status));
1788 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1789 SEEK_NS_FROM_ORIGIN_METHOD_NAME, status);
1790 if (status < 0) {
1791 BT_LIB_LOGW_APPEND_CAUSE(
1792 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1793 "%![iter-]+i, status=%s",
1794 iterator, bt_common_func_status_string(status));
1795 }
1796 } else {
1797 /*
1798 * The iterator doesn't know how to seek by itself to a
1799 * particular time. We will seek to the beginning and fast
1800 * forward to the right place.
1801 */
1802 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status;
1803 bt_bool can_seek_beginning;
1804
1805 can_seek_status = iterator->methods.can_seek_beginning(iterator,
1806 &can_seek_beginning);
1807 BT_ASSERT(can_seek_status == BT_FUNC_STATUS_OK);
1808 BT_ASSERT(can_seek_beginning);
1809 BT_ASSERT(iterator->methods.seek_beginning);
1810 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1811 iterator);
1812 status = iterator->methods.seek_beginning(iterator);
1813 BT_LOGD("User method returned: status=%s",
1814 bt_common_func_status_string(status));
1815 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME, "valid-status",
1816 status == BT_FUNC_STATUS_OK ||
1817 status == BT_FUNC_STATUS_ERROR ||
1818 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1819 status == BT_FUNC_STATUS_AGAIN,
1820 "Unexpected status: %![iter-]+i, status=%s",
1821 iterator, bt_common_func_status_string(status));
1822 if (status < 0) {
1823 BT_LIB_LOGW_APPEND_CAUSE(
1824 "Component input port message iterator's \"seek beginning\" method failed: "
1825 "%![iter-]+i, status=%s",
1826 iterator, bt_common_func_status_string(status));
1827 }
1828
1829 switch (status) {
1830 case BT_FUNC_STATUS_OK:
1831 break;
1832 case BT_FUNC_STATUS_ERROR:
1833 case BT_FUNC_STATUS_MEMORY_ERROR:
1834 case BT_FUNC_STATUS_AGAIN:
1835 goto end;
1836 default:
1837 bt_common_abort();
1838 }
1839
1840 /*
1841 * Find the first message which has a default clock
1842 * snapshot greater than or equal to the requested
1843 * seeking time, and move the received messages from
1844 * this point in the batch to this iterator's auto-seek
1845 * message queue.
1846 */
1847 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
1848 bt_object_put_ref_no_null_check(
1849 g_queue_pop_tail(iterator->auto_seek.msgs));
1850 }
1851
1852 stream_states = create_auto_seek_stream_states();
1853 if (!stream_states) {
1854 BT_LIB_LOGE_APPEND_CAUSE(
1855 "Failed to allocate one GHashTable.");
1856 status = BT_FUNC_STATUS_MEMORY_ERROR;
1857 goto end;
1858 }
1859
1860 status = find_message_ge_ns_from_origin(iterator,
1861 ns_from_origin, stream_states);
1862 switch (status) {
1863 case BT_FUNC_STATUS_OK:
1864 case BT_FUNC_STATUS_END:
1865 {
1866 GHashTableIter iter;
1867 gpointer key, value;
1868
1869 /*
1870 * If some streams exist at the seek time, prepend the
1871 * required messages to put those streams in the right
1872 * state.
1873 */
1874 g_hash_table_iter_init(&iter, stream_states);
1875 while (g_hash_table_iter_next (&iter, &key, &value)) {
1876 const bt_stream *stream = key;
1877 struct auto_seek_stream_state *stream_state =
1878 (struct auto_seek_stream_state *) value;
1879 bt_message *msg;
1880 const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const(
1881 bt_stream_borrow_class_const(stream));
1882 /* Initialize to silence maybe-uninitialized warning. */
1883 uint64_t raw_value = 0;
1884
1885 /*
1886 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1887 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1888 *
1889 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1890 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1891 * seen a message with a clock snapshot in our seeking, then we are sure that the
1892 * seek time is not below the clock range, and we know the stream was active at that
1893 * time (and that we cut it short).
1894 */
1895 if (stream_state->seen_clock_snapshot) {
1896 if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) {
1897 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K",
1898 ns_from_origin, clock_class);
1899 status = BT_FUNC_STATUS_ERROR;
1900 goto end;
1901 }
1902 }
1903
1904 switch (stream_state->state) {
1905 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN:
1906 BT_ASSERT(stream_state->packet);
1907 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet);
1908
1909 if (stream->class->packets_have_beginning_default_clock_snapshot) {
1910 /*
1911 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1912 * message. If "packet beginning" packets have clock snapshots, then we must have
1913 * seen a clock snapshot.
1914 */
1915 BT_ASSERT(stream_state->seen_clock_snapshot);
1916
1917 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
1918 (bt_self_message_iterator *) iterator, stream_state->packet, raw_value);
1919 } else {
1920 msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator,
1921 stream_state->packet);
1922 }
1923
1924 if (!msg) {
1925 status = BT_FUNC_STATUS_MEMORY_ERROR;
1926 goto end;
1927 }
1928
1929 g_queue_push_head(iterator->auto_seek.msgs, msg);
1930 msg = NULL;
1931 /* fall-thru */
1932
1933 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN:
1934 msg = bt_message_stream_beginning_create(
1935 (bt_self_message_iterator *) iterator, stream);
1936 if (!msg) {
1937 status = BT_FUNC_STATUS_MEMORY_ERROR;
1938 goto end;
1939 }
1940
1941 if (stream_state->seen_clock_snapshot) {
1942 bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value);
1943 }
1944
1945 g_queue_push_head(iterator->auto_seek.msgs, msg);
1946 msg = NULL;
1947 break;
1948 }
1949 }
1950
1951 /*
1952 * If there are messages in the auto-seek
1953 * message queue, replace the user's "next"
1954 * method with a custom, temporary "next" method
1955 * which returns them.
1956 */
1957 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
1958 BT_ASSERT(!iterator->auto_seek.original_next_callback);
1959 iterator->auto_seek.original_next_callback = iterator->methods.next;
1960
1961 iterator->methods.next =
1962 (bt_message_iterator_next_method)
1963 post_auto_seek_next;
1964 }
1965
1966 /*
1967 * `BT_FUNC_STATUS_END` becomes
1968 * `BT_FUNC_STATUS_OK`: the next
1969 * time this iterator's "next" method is called,
1970 * it will return
1971 * `BT_FUNC_STATUS_END`.
1972 */
1973 status = BT_FUNC_STATUS_OK;
1974 break;
1975 }
1976 case BT_FUNC_STATUS_ERROR:
1977 case BT_FUNC_STATUS_MEMORY_ERROR:
1978 case BT_FUNC_STATUS_AGAIN:
1979 goto end;
1980 default:
1981 bt_common_abort();
1982 }
1983 }
1984
1985 /*
1986 * The following messages returned by the next method (including
1987 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1988 */
1989 iterator->last_ns_from_origin = ns_from_origin;
1990
1991 end:
1992 if (stream_states) {
1993 destroy_auto_seek_stream_states(stream_states);
1994 stream_states = NULL;
1995 }
1996
1997 set_iterator_state_after_seeking(iterator, status);
1998 return status;
1999 }
2000
2001 BT_EXPORT
2002 bt_bool bt_self_message_iterator_is_interrupted(
2003 const struct bt_self_message_iterator *self_msg_iter)
2004 {
2005 const struct bt_message_iterator *iterator =
2006 (const void *) self_msg_iter;
2007
2008 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
2009 return (bt_bool) bt_graph_is_interrupted(iterator->graph);
2010 }
2011
2012 BT_EXPORT
2013 void bt_message_iterator_get_ref(
2014 const struct bt_message_iterator *iterator)
2015 {
2016 bt_object_get_ref(iterator);
2017 }
2018
2019 BT_EXPORT
2020 void bt_message_iterator_put_ref(
2021 const struct bt_message_iterator *iterator)
2022 {
2023 bt_object_put_ref(iterator);
2024 }
This page took 0.118267 seconds and 4 git commands to generate.