2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/MSG-ITER"
9 #include "lib/logging.h"
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"
39 #include "component-class.h"
40 #include "component.h"
41 #include "component-sink.h"
42 #include "component-source.h"
43 #include "connection.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"
56 * TODO: Use graph's state (number of active iterators, etc.) and
57 * possibly system specifications to make a better guess than this.
59 #define MSG_BATCH_SIZE 15
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))
70 struct per_stream_state
72 bt_packet
*cur_packet
;
77 clear_per_stream_state (struct bt_message_iterator
*iterator
)
80 g_hash_table_remove_all(iterator
->per_stream_state
);
82 BT_USE_EXPR(iterator
);
87 void set_msg_iterator_state(struct bt_message_iterator
*iterator
,
88 enum bt_message_iterator_state state
)
90 BT_ASSERT_DBG(iterator
);
91 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
92 bt_message_iterator_state_string(state
));
93 iterator
->state
= state
;
97 void bt_message_iterator_destroy(struct bt_object
*obj
)
99 struct bt_message_iterator
*iterator
;
104 * The message iterator's reference count is 0 if we're
105 * here. Increment it to avoid a double-destroy (possibly
106 * infinitely recursive). This could happen for example if the
107 * message iterator's finalization function does
108 * bt_object_get_ref() (or anything that causes
109 * bt_object_get_ref() to be called) on itself (ref. count goes
110 * from 0 to 1), and then bt_object_put_ref(): the reference
111 * count would go from 1 to 0 again and this function would be
115 iterator
= (void *) obj
;
116 BT_LIB_LOGI("Destroying self component input port message iterator object: "
118 bt_message_iterator_try_finalize(iterator
);
120 if (iterator
->connection
) {
122 * Remove ourself from the originating connection so
123 * that it does not try to finalize a dangling pointer
126 bt_connection_remove_iterator(iterator
->connection
, iterator
);
127 iterator
->connection
= NULL
;
130 if (iterator
->auto_seek
.msgs
) {
131 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
132 bt_object_put_ref_no_null_check(
133 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
136 g_queue_free(iterator
->auto_seek
.msgs
);
137 iterator
->auto_seek
.msgs
= NULL
;
140 if (iterator
->upstream_msg_iters
) {
142 * At this point the message iterator is finalized, so
143 * it's detached from any upstream message iterator.
145 BT_ASSERT(iterator
->upstream_msg_iters
->len
== 0);
146 g_ptr_array_free(iterator
->upstream_msg_iters
, TRUE
);
147 iterator
->upstream_msg_iters
= NULL
;
150 if (iterator
->msgs
) {
151 g_ptr_array_free(iterator
->msgs
, TRUE
);
152 iterator
->msgs
= NULL
;
156 g_hash_table_destroy(iterator
->per_stream_state
);
162 void bt_message_iterator_try_finalize(
163 struct bt_message_iterator
*iterator
)
166 bool call_user_finalize
= true;
170 switch (iterator
->state
) {
171 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
:
173 * If this function is called while the iterator is in the
174 * NON_INITIALIZED state, it means the user initialization
175 * method has either not been called, or has failed. We
176 * therefore don't want to call the user finalization method.
177 * However, the initialization method might have created some
178 * upstream message iterators before failing, so we want to
179 * execute the rest of this function, which unlinks the related
182 call_user_finalize
= false;
184 case BT_MESSAGE_ITERATOR_STATE_FINALIZED
:
185 /* Already finalized */
186 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
189 case BT_MESSAGE_ITERATOR_STATE_FINALIZING
:
191 BT_LIB_LOGF("Message iterator is already being finalized: "
198 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator
);
199 set_msg_iterator_state(iterator
,
200 BT_MESSAGE_ITERATOR_STATE_FINALIZING
);
201 BT_ASSERT(iterator
->upstream_component
);
203 /* Call user-defined destroy method */
204 if (call_user_finalize
) {
205 typedef void (*method_t
)(void *);
207 struct bt_component_class
*comp_class
=
208 iterator
->upstream_component
->class;
209 struct bt_component_class_with_iterator_class
*class_with_iter_class
;
211 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class
));
212 class_with_iter_class
= container_of(comp_class
,
213 struct bt_component_class_with_iterator_class
, parent
);
214 method
= (method_t
) class_with_iter_class
->msg_iter_cls
->methods
.finalize
;
217 const bt_error
*saved_error
;
219 saved_error
= bt_current_thread_take_error();
221 BT_LIB_LOGD("Calling user's finalization method: %!+i",
224 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
227 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
232 /* Detach upstream message iterators */
233 for (i
= 0; i
< iterator
->upstream_msg_iters
->len
; i
++) {
234 struct bt_message_iterator
*upstream_msg_iter
=
235 iterator
->upstream_msg_iters
->pdata
[i
];
237 upstream_msg_iter
->downstream_msg_iter
= NULL
;
240 g_ptr_array_set_size(iterator
->upstream_msg_iters
, 0);
242 /* Detach downstream message iterator */
243 if (iterator
->downstream_msg_iter
) {
246 BT_ASSERT(iterator
->downstream_msg_iter
->upstream_msg_iters
);
247 existed
= g_ptr_array_remove_fast(
248 iterator
->downstream_msg_iter
->upstream_msg_iters
,
253 iterator
->upstream_component
= NULL
;
254 iterator
->upstream_port
= NULL
;
255 set_msg_iterator_state(iterator
,
256 BT_MESSAGE_ITERATOR_STATE_FINALIZED
);
257 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator
);
263 void bt_message_iterator_set_connection(
264 struct bt_message_iterator
*iterator
,
265 struct bt_connection
*connection
)
268 iterator
->connection
= connection
;
269 BT_LIB_LOGI("Set message iterator's connection: "
270 "%![iter-]+i, %![conn-]+x", iterator
, connection
);
274 enum bt_message_iterator_can_seek_beginning_status
can_seek_ns_from_origin_true(
275 struct bt_message_iterator
*iterator
__attribute__((unused
)),
276 int64_t ns_from_origin
__attribute__((unused
)),
281 return BT_FUNC_STATUS_OK
;
285 enum bt_message_iterator_can_seek_beginning_status
can_seek_beginning_true(
286 struct bt_message_iterator
*iterator
__attribute__((unused
)),
291 return BT_FUNC_STATUS_OK
;
295 int create_self_component_input_port_message_iterator(
296 struct bt_self_message_iterator
*self_downstream_msg_iter
,
297 struct bt_self_component_port_input
*self_port
,
298 struct bt_message_iterator
**message_iterator
,
299 const char *api_func
)
301 bt_message_iterator_class_initialize_method init_method
= NULL
;
302 struct bt_message_iterator
*iterator
=
304 struct bt_message_iterator
*downstream_msg_iter
=
305 (void *) self_downstream_msg_iter
;
306 struct bt_port
*port
= (void *) self_port
;
307 struct bt_port
*upstream_port
;
308 struct bt_component
*comp
;
309 struct bt_component
*upstream_comp
;
310 struct bt_component_class
*upstream_comp_cls
;
311 struct bt_component_class_with_iterator_class
*upstream_comp_cls_with_iter_cls
;
314 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "message-iterator-output",
315 message_iterator
, "Created message iterator (output)");
316 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func
, "input-port", port
,
318 comp
= bt_port_borrow_component_inline(port
);
319 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-is-connected",
320 bt_port_is_connected(port
),
321 "Input port is not connected: %![port-]+p", port
);
322 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-has-component",
323 comp
, "Input port is not part of a component: %![port-]+p",
325 BT_ASSERT(port
->connection
);
326 upstream_port
= port
->connection
->upstream_port
;
327 BT_ASSERT(upstream_port
);
328 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
329 BT_ASSERT(upstream_comp
);
330 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-configured",
331 bt_component_borrow_graph(upstream_comp
)->config_state
==
332 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
||
333 bt_component_borrow_graph(upstream_comp
)->config_state
==
334 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
,
335 "Graph is not configured: %!+g",
336 bt_component_borrow_graph(upstream_comp
));
337 upstream_comp_cls
= upstream_comp
->class;
338 BT_ASSERT(upstream_comp
->class->type
==
339 BT_COMPONENT_CLASS_TYPE_SOURCE
||
340 upstream_comp
->class->type
==
341 BT_COMPONENT_CLASS_TYPE_FILTER
);
342 BT_LIB_LOGI("Creating message iterator on self component input port: "
343 "%![up-comp-]+c, %![up-port-]+p", upstream_comp
, upstream_port
);
345 struct bt_message_iterator
, 1);
347 BT_LIB_LOGE_APPEND_CAUSE(
348 "Failed to allocate one self component input port "
349 "message iterator.");
350 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
354 bt_object_init_shared(&iterator
->base
,
355 bt_message_iterator_destroy
);
356 iterator
->msgs
= g_ptr_array_new();
357 if (!iterator
->msgs
) {
358 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
359 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
363 g_ptr_array_set_size(iterator
->msgs
, MSG_BATCH_SIZE
);
364 iterator
->last_ns_from_origin
= INT64_MIN
;
367 /* The per-stream state is only used for dev assertions right now. */
368 iterator
->per_stream_state
= g_hash_table_new_full(
375 iterator
->auto_seek
.msgs
= g_queue_new();
376 if (!iterator
->auto_seek
.msgs
) {
377 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
378 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
382 iterator
->upstream_msg_iters
= g_ptr_array_new();
383 if (!iterator
->upstream_msg_iters
) {
384 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
385 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
389 iterator
->upstream_component
= upstream_comp
;
390 iterator
->upstream_port
= upstream_port
;
391 iterator
->connection
= iterator
->upstream_port
->connection
;
392 iterator
->graph
= bt_component_borrow_graph(upstream_comp
);
393 set_msg_iterator_state(iterator
,
394 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED
);
396 /* Copy methods from the message iterator class to the message iterator. */
397 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls
));
398 upstream_comp_cls_with_iter_cls
= container_of(upstream_comp_cls
,
399 struct bt_component_class_with_iterator_class
, parent
);
401 iterator
->methods
.next
=
402 (bt_message_iterator_next_method
)
403 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.next
;
404 iterator
->methods
.seek_ns_from_origin
=
405 (bt_message_iterator_seek_ns_from_origin_method
)
406 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_ns_from_origin
;
407 iterator
->methods
.seek_beginning
=
408 (bt_message_iterator_seek_beginning_method
)
409 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.seek_beginning
;
410 iterator
->methods
.can_seek_ns_from_origin
=
411 (bt_message_iterator_can_seek_ns_from_origin_method
)
412 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_ns_from_origin
;
413 iterator
->methods
.can_seek_beginning
=
414 (bt_message_iterator_can_seek_beginning_method
)
415 upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.can_seek_beginning
;
417 if (iterator
->methods
.seek_ns_from_origin
&&
418 !iterator
->methods
.can_seek_ns_from_origin
) {
419 iterator
->methods
.can_seek_ns_from_origin
=
420 (bt_message_iterator_can_seek_ns_from_origin_method
)
421 can_seek_ns_from_origin_true
;
424 if (iterator
->methods
.seek_beginning
&&
425 !iterator
->methods
.can_seek_beginning
) {
426 iterator
->methods
.can_seek_beginning
=
427 (bt_message_iterator_can_seek_beginning_method
)
428 can_seek_beginning_true
;
431 /* Call iterator's init method. */
432 init_method
= upstream_comp_cls_with_iter_cls
->msg_iter_cls
->methods
.initialize
;
435 enum bt_message_iterator_class_initialize_method_status iter_status
;
437 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator
);
438 iter_status
= init_method(
439 (struct bt_self_message_iterator
*) iterator
,
441 (struct bt_self_component_port_output
*) upstream_port
);
442 BT_LOGD("User method returned: status=%s",
443 bt_common_func_status_string(iter_status
));
444 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
445 "bt_message_iterator_class_initialize_method",
447 if (iter_status
!= BT_FUNC_STATUS_OK
) {
448 BT_LIB_LOGW_APPEND_CAUSE(
449 "Component input port message iterator initialization method failed: "
450 "%![iter-]+i, status=%s",
452 bt_common_func_status_string(iter_status
));
453 status
= iter_status
;
457 iterator
->config
.frozen
= true;
460 if (downstream_msg_iter
) {
461 /* Set this message iterator's downstream message iterator */
462 iterator
->downstream_msg_iter
= downstream_msg_iter
;
465 * Add this message iterator to the downstream message
466 * iterator's array of upstream message iterators.
468 g_ptr_array_add(downstream_msg_iter
->upstream_msg_iters
,
472 set_msg_iterator_state(iterator
,
473 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
474 g_ptr_array_add(port
->connection
->iterators
, iterator
);
475 BT_LIB_LOGI("Created message iterator on self component input port: "
476 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
477 upstream_port
, upstream_comp
, iterator
);
479 *message_iterator
= iterator
;
480 status
= BT_FUNC_STATUS_OK
;
484 BT_OBJECT_PUT_REF_AND_RESET(iterator
);
491 bt_message_iterator_create_from_message_iterator_status
492 bt_message_iterator_create_from_message_iterator(
493 struct bt_self_message_iterator
*self_msg_iter
,
494 struct bt_self_component_port_input
*input_port
,
495 struct bt_message_iterator
**message_iterator
)
497 BT_ASSERT_PRE_NO_ERROR();
498 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter
);
499 return create_self_component_input_port_message_iterator(self_msg_iter
,
500 input_port
, message_iterator
, __func__
);
504 bt_message_iterator_create_from_sink_component_status
505 bt_message_iterator_create_from_sink_component(
506 struct bt_self_component_sink
*self_comp
,
507 struct bt_self_component_port_input
*input_port
,
508 struct bt_message_iterator
**message_iterator
)
510 BT_ASSERT_PRE_NO_ERROR();
511 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp
, "Sink component");
512 return create_self_component_input_port_message_iterator(NULL
,
513 input_port
, message_iterator
, __func__
);
517 void *bt_self_message_iterator_get_data(
518 const struct bt_self_message_iterator
*self_iterator
)
520 struct bt_message_iterator
*iterator
=
521 (void *) self_iterator
;
523 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
524 return iterator
->user_data
;
528 void bt_self_message_iterator_set_data(
529 struct bt_self_message_iterator
*self_iterator
, void *data
)
531 struct bt_message_iterator
*iterator
=
532 (void *) self_iterator
;
534 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
535 iterator
->user_data
= data
;
536 BT_LIB_LOGD("Set message iterator's user data: "
537 "%!+i, user-data-addr=%p", iterator
, data
);
541 void bt_self_message_iterator_configuration_set_can_seek_forward(
542 bt_self_message_iterator_configuration
*config
,
543 bt_bool can_seek_forward
)
545 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config
,
546 "Message iterator configuration");
547 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config
,
548 "Message iterator configuration", "");
550 config
->can_seek_forward
= can_seek_forward
;
554 * Validate that the default clock snapshot in `msg` doesn't make us go back in
558 BT_ASSERT_COND_DEV_FUNC
560 bool clock_snapshots_are_monotonic_one(
561 struct bt_message_iterator
*iterator
,
562 const bt_message
*msg
)
564 const struct bt_clock_snapshot
*clock_snapshot
= NULL
;
565 bt_message_type message_type
= bt_message_get_type(msg
);
566 int64_t ns_from_origin
;
567 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status
;
570 * The default is true: if we can't figure out the clock snapshot
571 * (or there is none), assume it is fine.
575 switch (message_type
) {
576 case BT_MESSAGE_TYPE_EVENT
:
578 struct bt_message_event
*event_msg
= (struct bt_message_event
*) msg
;
579 clock_snapshot
= event_msg
->default_cs
;
582 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
584 struct bt_message_message_iterator_inactivity
*inactivity_msg
=
585 (struct bt_message_message_iterator_inactivity
*) msg
;
586 clock_snapshot
= inactivity_msg
->cs
;
589 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
590 case BT_MESSAGE_TYPE_PACKET_END
:
592 struct bt_message_packet
*packet_msg
= (struct bt_message_packet
*) msg
;
593 clock_snapshot
= packet_msg
->default_cs
;
596 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
597 case BT_MESSAGE_TYPE_STREAM_END
:
599 struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
600 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
604 clock_snapshot
= stream_msg
->default_cs
;
607 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
608 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
610 struct bt_message_discarded_items
*discarded_msg
=
611 (struct bt_message_discarded_items
*) msg
;
613 clock_snapshot
= discarded_msg
->default_begin_cs
;
618 if (!clock_snapshot
) {
622 clock_snapshot_status
= bt_clock_snapshot_get_ns_from_origin(
623 clock_snapshot
, &ns_from_origin
);
624 if (clock_snapshot_status
!= BT_FUNC_STATUS_OK
) {
626 * bt_clock_snapshot_get_ns_from_origin can return
627 * OVERFLOW_ERROR. We don't really want to report an error to
628 * our caller, so just clear it.
630 bt_current_thread_clear_error();
634 result
= ns_from_origin
>= iterator
->last_ns_from_origin
;
635 iterator
->last_ns_from_origin
= ns_from_origin
;
640 BT_ASSERT_COND_DEV_FUNC
642 bool clock_snapshots_are_monotonic(
643 struct bt_message_iterator
*iterator
,
644 bt_message_array_const msgs
, uint64_t msg_count
)
649 for (i
= 0; i
< msg_count
; i
++) {
650 if (!clock_snapshots_are_monotonic_one(iterator
, msgs
[i
])) {
663 * When a new stream begins, verify that the clock class tied to this
664 * stream is compatible with what we've seen before.
667 BT_ASSERT_COND_DEV_FUNC
669 bool clock_classes_are_compatible_one(struct bt_message_iterator
*iterator
,
670 const struct bt_message
*msg
)
672 enum bt_message_type message_type
= bt_message_get_type(msg
);
675 if (message_type
== BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
676 const struct bt_message_stream
*stream_msg
= (struct bt_message_stream
*) msg
;
677 const struct bt_clock_class
*clock_class
= stream_msg
->stream
->class->default_clock_class
;
678 bt_uuid clock_class_uuid
= NULL
;
681 clock_class_uuid
= bt_clock_class_get_uuid(clock_class
);
684 switch (iterator
->clock_expectation
.type
) {
685 case CLOCK_EXPECTATION_UNSET
:
687 * This is the first time we see a message with a clock
688 * snapshot: record the properties of that clock, against
689 * which we'll compare the clock properties of the following
694 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_NONE
;
695 } else if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
696 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_UNIX
;
697 } else if (clock_class_uuid
) {
698 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
;
699 bt_uuid_copy(iterator
->clock_expectation
.uuid
, clock_class_uuid
);
701 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
;
705 case CLOCK_EXPECTATION_NONE
:
707 BT_ASSERT_COND_DEV_MSG(
708 "Expecting no clock class, got one: %![cc-]+K",
716 case CLOCK_EXPECTATION_ORIGIN_UNIX
:
718 BT_ASSERT_COND_DEV_MSG(
719 "Expecting a clock class, got none.");
724 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
725 BT_ASSERT_COND_DEV_MSG(
726 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
733 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID
:
735 BT_ASSERT_COND_DEV_MSG(
736 "Expecting a clock class, got none.");
741 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
742 BT_ASSERT_COND_DEV_MSG(
743 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
749 if (!clock_class_uuid
) {
750 BT_ASSERT_COND_DEV_MSG(
751 "Expecting a clock class with UUID: %![cc-]+K",
757 if (bt_uuid_compare(iterator
->clock_expectation
.uuid
, clock_class_uuid
)) {
758 BT_ASSERT_COND_DEV_MSG(
759 "Expecting a clock class with UUID, got one "
760 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
761 clock_class
, iterator
->clock_expectation
.uuid
);
767 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID
:
769 BT_ASSERT_COND_DEV_MSG(
770 "Expecting a clock class, got none.");
775 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
776 BT_ASSERT_COND_DEV_MSG(
777 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
783 if (clock_class_uuid
) {
784 BT_ASSERT_COND_DEV_MSG(
785 "Expecting a clock class without UUID: %![cc-]+K",
800 BT_ASSERT_COND_DEV_FUNC
802 bool clock_classes_are_compatible(
803 struct bt_message_iterator
*iterator
,
804 bt_message_array_const msgs
, uint64_t msg_count
)
809 for (i
= 0; i
< msg_count
; i
++) {
810 if (!clock_classes_are_compatible_one(iterator
, msgs
[i
])) {
824 const bt_stream
*get_stream_from_msg(const struct bt_message
*msg
)
826 struct bt_stream
*stream
;
829 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
830 case BT_MESSAGE_TYPE_STREAM_END
:
832 struct bt_message_stream
*msg_stream
=
833 (struct bt_message_stream
*) msg
;
834 stream
= msg_stream
->stream
;
837 case BT_MESSAGE_TYPE_EVENT
:
839 struct bt_message_event
*msg_event
=
840 (struct bt_message_event
*) msg
;
841 stream
= msg_event
->event
->stream
;
844 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
845 case BT_MESSAGE_TYPE_PACKET_END
:
847 struct bt_message_packet
*msg_packet
=
848 (struct bt_message_packet
*) msg
;
849 stream
= msg_packet
->packet
->stream
;
852 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
853 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
855 struct bt_message_discarded_items
*msg_discarded
=
856 (struct bt_message_discarded_items
*) msg
;
857 stream
= msg_discarded
->stream
;
860 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
871 struct per_stream_state
*get_per_stream_state(
872 struct bt_message_iterator
*iterator
,
873 const struct bt_stream
*stream
)
875 struct per_stream_state
*state
= g_hash_table_lookup(
876 iterator
->per_stream_state
, stream
);
879 state
= g_new0(struct per_stream_state
, 1);
880 g_hash_table_insert(iterator
->per_stream_state
,
881 (gpointer
) stream
, state
);
888 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
892 void assert_post_dev_expected_packet(struct bt_message_iterator
*iterator
,
893 const struct bt_message
*msg
)
895 const bt_stream
*stream
= get_stream_from_msg(msg
);
896 struct per_stream_state
*state
;
897 const bt_packet
*actual_packet
= NULL
;
898 const bt_packet
*expected_packet
= NULL
;
904 state
= get_per_stream_state(iterator
, stream
);
907 case BT_MESSAGE_TYPE_EVENT
:
909 const struct bt_message_event
*msg_event
=
910 (const struct bt_message_event
*) msg
;
912 actual_packet
= msg_event
->event
->packet
;
913 expected_packet
= state
->cur_packet
;
916 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
918 const struct bt_message_packet
*msg_packet
=
919 (const struct bt_message_packet
*) msg
;
921 BT_ASSERT(!state
->cur_packet
);
922 state
->cur_packet
= msg_packet
->packet
;
925 case BT_MESSAGE_TYPE_PACKET_END
:
927 const struct bt_message_packet
*msg_packet
=
928 (const struct bt_message_packet
*) msg
;
930 actual_packet
= msg_packet
->packet
;
931 expected_packet
= state
->cur_packet
;
932 BT_ASSERT(state
->cur_packet
);
933 state
->cur_packet
= NULL
;
940 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
941 "message-packet-is-expected",
942 actual_packet
== expected_packet
,
943 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
944 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
945 stream
, iterator
, msg
, actual_packet
, expected_packet
);
952 void assert_post_dev_next(
953 struct bt_message_iterator
*iterator
,
954 bt_message_iterator_class_next_method_status status
,
955 bt_message_array_const msgs
, uint64_t msg_count
)
957 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
960 for (i
= 0; i
< msg_count
; i
++) {
961 assert_post_dev_expected_packet(iterator
, msgs
[i
]);
968 * Call the `next` method of the iterator. Do some validation on the returned
973 enum bt_message_iterator_class_next_method_status
974 call_iterator_next_method(
975 struct bt_message_iterator
*iterator
,
976 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *user_count
)
978 enum bt_message_iterator_class_next_method_status status
;
980 BT_ASSERT_DBG(iterator
->methods
.next
);
981 BT_LOGD_STR("Calling user's \"next\" method.");
982 status
= iterator
->methods
.next(iterator
, msgs
, capacity
, user_count
);
983 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
984 bt_common_func_status_string(status
), *user_count
);
986 if (status
== BT_FUNC_STATUS_OK
) {
987 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
988 "message-clock-classes-are-compatible",
989 clock_classes_are_compatible(iterator
, msgs
,
991 "Clocks are not compatible");
992 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
993 "message-clock-snapshots-are-monotonic",
994 clock_snapshots_are_monotonic(iterator
, msgs
,
996 "Clock snapshots are not monotonic");
1000 assert_post_dev_next(iterator
, status
, msgs
, *user_count
);
1003 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME
,
1010 enum bt_message_iterator_next_status
1011 bt_message_iterator_next(
1012 struct bt_message_iterator
*iterator
,
1013 bt_message_array_const
*msgs
, uint64_t *user_count
)
1015 enum bt_message_iterator_next_status status
= BT_FUNC_STATUS_OK
;
1017 BT_ASSERT_PRE_DEV_NO_ERROR();
1018 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1019 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs
,
1020 "Message array (output)");
1021 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count
,
1022 "Message count (output)");
1023 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1024 iterator
->state
== BT_MESSAGE_ITERATOR_STATE_ACTIVE
,
1025 "Message iterator's \"next\" called, but "
1026 "message iterator is in the wrong state: %!+i", iterator
);
1027 BT_ASSERT_DBG(iterator
->upstream_component
);
1028 BT_ASSERT_DBG(iterator
->upstream_component
->class);
1029 BT_ASSERT_PRE_DEV("graph-is-configured",
1030 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1031 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1032 "Graph is not configured: %!+g",
1033 bt_component_borrow_graph(iterator
->upstream_component
));
1034 BT_LIB_LOGD("Getting next self component input port "
1035 "message iterator's messages: %!+i, batch-size=%u",
1036 iterator
, MSG_BATCH_SIZE
);
1039 * Call the user's "next" method to get the next messages
1043 status
= (int) call_iterator_next_method(iterator
,
1044 (void *) iterator
->msgs
->pdata
, MSG_BATCH_SIZE
,
1046 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64
,
1047 bt_common_func_status_string(status
), *user_count
);
1049 BT_LIB_LOGW_APPEND_CAUSE(
1050 "Component input port message iterator's \"next\" method failed: "
1051 "%![iter-]+i, status=%s",
1052 iterator
, bt_common_func_status_string(status
));
1057 * There is no way that this iterator could have been finalized
1058 * during its "next" method, as the only way to do this is to
1059 * put the last iterator's reference, and this can only be done
1060 * by its downstream owner.
1062 * For the same reason, there is no way that this iterator could
1063 * have seeked (cannot seek a self message iterator).
1065 BT_ASSERT_DBG(iterator
->state
==
1066 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1069 case BT_FUNC_STATUS_OK
:
1070 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
, "count-lteq-capacity",
1071 *user_count
<= MSG_BATCH_SIZE
,
1072 "Invalid returned message count: greater than "
1073 "batch size: count=%" PRIu64
", batch-size=%u",
1074 *user_count
, MSG_BATCH_SIZE
);
1075 *msgs
= (void *) iterator
->msgs
->pdata
;
1077 case BT_FUNC_STATUS_AGAIN
:
1079 case BT_FUNC_STATUS_END
:
1080 set_msg_iterator_state(iterator
,
1081 BT_MESSAGE_ITERATOR_STATE_ENDED
);
1084 /* Unknown non-error status */
1093 struct bt_component
*
1094 bt_message_iterator_borrow_component(
1095 struct bt_message_iterator
*iterator
)
1097 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1098 return iterator
->upstream_component
;
1102 struct bt_self_component
*bt_self_message_iterator_borrow_component(
1103 struct bt_self_message_iterator
*self_iterator
)
1105 struct bt_message_iterator
*iterator
=
1106 (void *) self_iterator
;
1108 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1109 return (void *) iterator
->upstream_component
;
1113 struct bt_self_component_port_output
*bt_self_message_iterator_borrow_port(
1114 struct bt_self_message_iterator
*self_iterator
)
1116 struct bt_message_iterator
*iterator
=
1117 (void *) self_iterator
;
1119 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator
);
1120 return (void *) iterator
->upstream_port
;
1123 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1124 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1127 enum bt_message_iterator_can_seek_ns_from_origin_status
1128 bt_message_iterator_can_seek_ns_from_origin(
1129 struct bt_message_iterator
*iterator
,
1130 int64_t ns_from_origin
, bt_bool
*can_seek
)
1132 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1134 BT_ASSERT_PRE_NO_ERROR();
1135 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1136 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1137 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1138 BT_ASSERT_PRE("graph-is-configured",
1139 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1140 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1141 "Graph is not configured: %!+g",
1142 bt_component_borrow_graph(iterator
->upstream_component
));
1144 if (iterator
->methods
.can_seek_ns_from_origin
) {
1146 * Initialize to an invalid value, so we can post-assert that
1147 * the method returned a valid value.
1151 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1154 status
= (int) iterator
->methods
.can_seek_ns_from_origin(iterator
,
1155 ns_from_origin
, can_seek
);
1157 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1158 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1160 if (status
!= BT_FUNC_STATUS_OK
) {
1161 BT_LIB_LOGW_APPEND_CAUSE(
1162 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1163 "%![iter-]+i, status=%s",
1164 iterator
, bt_common_func_status_string(status
));
1168 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME
,
1169 "valid-return-value",
1170 *can_seek
== BT_TRUE
|| *can_seek
== BT_FALSE
,
1171 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1172 *can_seek
, iterator
);
1175 "User's \"can seek nanoseconds from origin\" returned successfully: "
1176 "%![iter-]+i, can-seek=%d",
1177 iterator
, *can_seek
);
1185 * Automatic seeking fall back: if we can seek to the beginning and the
1186 * iterator supports forward seeking then we can automatically seek to
1189 status
= (int) bt_message_iterator_can_seek_beginning(
1190 iterator
, can_seek
);
1191 if (status
!= BT_FUNC_STATUS_OK
) {
1195 *can_seek
= *can_seek
&& iterator
->config
.can_seek_forward
;
1201 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1202 "bt_message_iterator_class_can_seek_beginning"
1205 enum bt_message_iterator_can_seek_beginning_status
1206 bt_message_iterator_can_seek_beginning(
1207 struct bt_message_iterator
*iterator
,
1210 enum bt_message_iterator_can_seek_beginning_status status
;
1212 BT_ASSERT_PRE_NO_ERROR();
1213 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1214 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek
);
1215 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1216 BT_ASSERT_PRE("graph-is-configured",
1217 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1218 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1219 "Graph is not configured: %!+g",
1220 bt_component_borrow_graph(iterator
->upstream_component
));
1222 if (iterator
->methods
.can_seek_beginning
) {
1224 * Initialize to an invalid value, so we can post-assert that
1225 * the method returned a valid value.
1229 status
= (int) iterator
->methods
.can_seek_beginning(iterator
, can_seek
);
1231 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME
,
1232 "valid-return-value",
1233 status
!= BT_FUNC_STATUS_OK
||
1234 *can_seek
== BT_TRUE
||
1235 *can_seek
== BT_FALSE
,
1236 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1237 *can_seek
, iterator
);
1238 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1239 CAN_SEEK_BEGINNING_METHOD_NAME
, status
);
1241 *can_seek
= BT_FALSE
;
1242 status
= BT_FUNC_STATUS_OK
;
1249 void set_iterator_state_after_seeking(
1250 struct bt_message_iterator
*iterator
,
1253 enum bt_message_iterator_state new_state
= 0;
1255 /* Set iterator's state depending on seeking status */
1257 case BT_FUNC_STATUS_OK
:
1258 new_state
= BT_MESSAGE_ITERATOR_STATE_ACTIVE
;
1260 case BT_FUNC_STATUS_AGAIN
:
1261 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN
;
1263 case BT_FUNC_STATUS_ERROR
:
1264 case BT_FUNC_STATUS_MEMORY_ERROR
:
1265 new_state
= BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR
;
1267 case BT_FUNC_STATUS_END
:
1268 new_state
= BT_MESSAGE_ITERATOR_STATE_ENDED
;
1274 set_msg_iterator_state(iterator
, new_state
);
1278 void reset_iterator_expectations(
1279 struct bt_message_iterator
*iterator
)
1281 iterator
->last_ns_from_origin
= INT64_MIN
;
1282 iterator
->clock_expectation
.type
= CLOCK_EXPECTATION_UNSET
;
1286 bool message_iterator_can_seek_beginning(
1287 struct bt_message_iterator
*iterator
)
1289 enum bt_message_iterator_can_seek_beginning_status status
;
1292 status
= bt_message_iterator_can_seek_beginning(
1293 iterator
, &can_seek
);
1294 if (status
!= BT_FUNC_STATUS_OK
) {
1295 can_seek
= BT_FALSE
;
1301 #define SEEK_BEGINNING_METHOD_NAME \
1302 "bt_message_iterator_class_seek_beginning_method"
1305 enum bt_message_iterator_seek_beginning_status
1306 bt_message_iterator_seek_beginning(struct bt_message_iterator
*iterator
)
1310 BT_ASSERT_PRE_NO_ERROR();
1311 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1312 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1313 BT_ASSERT_PRE("graph-is-configured",
1314 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1315 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1316 "Graph is not configured: %!+g",
1317 bt_component_borrow_graph(iterator
->upstream_component
));
1318 BT_ASSERT_PRE("can-seek-beginning",
1319 message_iterator_can_seek_beginning(iterator
),
1320 "Message iterator cannot seek beginning: %!+i", iterator
);
1323 * We are seeking, reset our expectations about how the following
1324 * messages should look like.
1326 reset_iterator_expectations(iterator
);
1328 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator
);
1329 set_msg_iterator_state(iterator
,
1330 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1331 status
= iterator
->methods
.seek_beginning(iterator
);
1332 BT_LOGD("User method returned: status=%s",
1333 bt_common_func_status_string(status
));
1334 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1335 status
== BT_FUNC_STATUS_OK
||
1336 status
== BT_FUNC_STATUS_ERROR
||
1337 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1338 status
== BT_FUNC_STATUS_AGAIN
,
1339 "Unexpected status: %![iter-]+i, status=%s",
1340 iterator
, bt_common_func_status_string(status
));
1341 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME
,
1344 BT_LIB_LOGW_APPEND_CAUSE(
1345 "Component input port message iterator's \"seek beginning\" method failed: "
1346 "%![iter-]+i, status=%s",
1347 iterator
, bt_common_func_status_string(status
));
1350 clear_per_stream_state(iterator
);
1352 set_iterator_state_after_seeking(iterator
, status
);
1358 bt_message_iterator_can_seek_forward(
1359 bt_message_iterator
*iterator
)
1361 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1363 return iterator
->config
.can_seek_forward
;
1367 * Structure used to record the state of a given stream during the fast-forward
1368 * phase of an auto-seek.
1370 struct auto_seek_stream_state
{
1372 * Value representing which step of this timeline we are at.
1375 * [SB] 1 [PB] 2 [PE] 1 [SE]
1377 * At each point in the timeline, the messages we need to replicate are:
1379 * 1: Stream beginning
1380 * 2: Stream beginning, packet beginning
1382 * Before "Stream beginning" and after "Stream end", we don't need to
1383 * replicate anything as the stream doesn't exist.
1386 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
,
1387 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
,
1391 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1392 * in. This is a weak reference, since the packet will always be
1393 * alive by the time we use it.
1395 struct bt_packet
*packet
;
1397 /* Have we see a message with a clock snapshot yet? */
1398 bool seen_clock_snapshot
;
1402 struct auto_seek_stream_state
*create_auto_seek_stream_state(void)
1404 return g_new0(struct auto_seek_stream_state
, 1);
1408 void destroy_auto_seek_stream_state(void *ptr
)
1414 GHashTable
*create_auto_seek_stream_states(void)
1416 return g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
1417 destroy_auto_seek_stream_state
);
1421 void destroy_auto_seek_stream_states(GHashTable
*stream_states
)
1423 g_hash_table_destroy(stream_states
);
1427 * Handle one message while we are in the fast-forward phase of an auto-seek.
1429 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1430 * `ns_from_origin`. In other words, if this is the first message after our
1433 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1434 * `struct auto_seek_stream_state` used to keep the state of each stream
1435 * during the fast-forward.
1439 int auto_seek_handle_message(
1440 struct bt_message_iterator
*iterator
,
1441 int64_t ns_from_origin
, const struct bt_message
*msg
,
1442 bool *got_first
, GHashTable
*stream_states
)
1444 int status
= BT_FUNC_STATUS_OK
;
1445 int64_t msg_ns_from_origin
;
1446 const struct bt_clock_snapshot
*clk_snapshot
= NULL
;
1450 BT_ASSERT_DBG(got_first
);
1452 switch (msg
->type
) {
1453 case BT_MESSAGE_TYPE_EVENT
:
1455 const struct bt_message_event
*event_msg
=
1458 clk_snapshot
= event_msg
->default_cs
;
1459 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1460 "event-message-has-default-clock-snapshot",
1462 "Event message has no default clock snapshot: %!+n",
1466 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
1468 const struct bt_message_message_iterator_inactivity
*inactivity_msg
=
1471 clk_snapshot
= inactivity_msg
->cs
;
1472 BT_ASSERT_DBG(clk_snapshot
);
1475 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1476 case BT_MESSAGE_TYPE_PACKET_END
:
1478 const struct bt_message_packet
*packet_msg
=
1481 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
1482 && !packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1486 if (msg
->type
== BT_MESSAGE_TYPE_PACKET_END
1487 && !packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1491 clk_snapshot
= packet_msg
->default_cs
;
1492 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1493 "packet-message-has-default-clock-snapshot",
1495 "Packet message has no default clock snapshot: %!+n",
1499 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1500 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1502 struct bt_message_discarded_items
*msg_disc_items
=
1505 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&&
1506 !msg_disc_items
->stream
->class->discarded_events_have_default_clock_snapshots
) {
1510 if (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&&
1511 !msg_disc_items
->stream
->class->discarded_packets_have_default_clock_snapshots
) {
1515 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1516 "discarded-events-packets-message-has-default-clock-snapshot",
1517 msg_disc_items
->default_begin_cs
&&
1518 msg_disc_items
->default_end_cs
,
1519 "Discarded events/packets message has no default clock snapshots: %!+n",
1521 ret
= bt_clock_snapshot_get_ns_from_origin(
1522 msg_disc_items
->default_begin_cs
,
1523 &msg_ns_from_origin
);
1525 status
= BT_FUNC_STATUS_ERROR
;
1529 if (msg_ns_from_origin
>= ns_from_origin
) {
1534 ret
= bt_clock_snapshot_get_ns_from_origin(
1535 msg_disc_items
->default_end_cs
,
1536 &msg_ns_from_origin
);
1538 status
= BT_FUNC_STATUS_ERROR
;
1542 if (msg_ns_from_origin
>= ns_from_origin
) {
1544 * The discarded items message's beginning time
1545 * is before the requested seeking time, but its
1546 * end time is after. Modify the message so as
1547 * to set its beginning time to the requested
1548 * seeking time, and make its item count unknown
1549 * as we don't know if items were really
1550 * discarded within the new time range.
1552 uint64_t new_begin_raw_value
= 0;
1554 ret
= bt_clock_class_clock_value_from_ns_from_origin(
1555 msg_disc_items
->default_end_cs
->clock_class
,
1556 ns_from_origin
, &new_begin_raw_value
);
1558 status
= BT_FUNC_STATUS_ERROR
;
1562 bt_clock_snapshot_set_raw_value(
1563 msg_disc_items
->default_begin_cs
,
1564 new_begin_raw_value
);
1565 msg_disc_items
->count
.base
.avail
=
1566 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
;
1569 * It is safe to push it because its beginning
1570 * time is exactly the requested seeking time.
1577 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1578 case BT_MESSAGE_TYPE_STREAM_END
:
1580 struct bt_message_stream
*stream_msg
=
1581 (struct bt_message_stream
*) msg
;
1583 if (stream_msg
->default_cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1588 clk_snapshot
= stream_msg
->default_cs
;
1595 BT_ASSERT_DBG(clk_snapshot
);
1596 ret
= bt_clock_snapshot_get_ns_from_origin(clk_snapshot
,
1597 &msg_ns_from_origin
);
1599 status
= BT_FUNC_STATUS_ERROR
;
1603 if (msg_ns_from_origin
>= ns_from_origin
) {
1609 /* This message won't be sent downstream. */
1610 switch (msg
->type
) {
1611 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1613 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1614 struct auto_seek_stream_state
*stream_state
;
1616 /* Update stream's state: stream began. */
1617 stream_state
= create_auto_seek_stream_state();
1618 if (!stream_state
) {
1619 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1623 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1625 if (stream_msg
->default_cs_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
1626 stream_state
->seen_clock_snapshot
= true;
1629 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states
, stream_msg
->stream
));
1630 g_hash_table_insert(stream_states
, stream_msg
->stream
, stream_state
);
1633 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1635 const struct bt_message_packet
*packet_msg
=
1637 struct auto_seek_stream_state
*stream_state
;
1639 /* Update stream's state: packet began. */
1640 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1641 BT_ASSERT_DBG(stream_state
);
1642 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1643 stream_state
->state
= AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
;
1644 BT_ASSERT_DBG(!stream_state
->packet
);
1645 stream_state
->packet
= packet_msg
->packet
;
1647 if (packet_msg
->packet
->stream
->class->packets_have_beginning_default_clock_snapshot
) {
1648 stream_state
->seen_clock_snapshot
= true;
1653 case BT_MESSAGE_TYPE_EVENT
:
1655 const struct bt_message_event
*event_msg
= (const void *) msg
;
1656 struct auto_seek_stream_state
*stream_state
;
1658 stream_state
= g_hash_table_lookup(stream_states
,
1659 event_msg
->event
->stream
);
1660 BT_ASSERT_DBG(stream_state
);
1662 // HELPME: are we sure that event messages have clock snapshots at this point?
1663 stream_state
->seen_clock_snapshot
= true;
1667 case BT_MESSAGE_TYPE_PACKET_END
:
1669 const struct bt_message_packet
*packet_msg
=
1671 struct auto_seek_stream_state
*stream_state
;
1673 /* Update stream's state: packet ended. */
1674 stream_state
= g_hash_table_lookup(stream_states
, packet_msg
->packet
->stream
);
1675 BT_ASSERT_DBG(stream_state
);
1676 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
);
1677 stream_state
->state
= AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
;
1678 BT_ASSERT_DBG(stream_state
->packet
);
1679 stream_state
->packet
= NULL
;
1681 if (packet_msg
->packet
->stream
->class->packets_have_end_default_clock_snapshot
) {
1682 stream_state
->seen_clock_snapshot
= true;
1687 case BT_MESSAGE_TYPE_STREAM_END
:
1689 const struct bt_message_stream
*stream_msg
= (const void *) msg
;
1690 struct auto_seek_stream_state
*stream_state
;
1692 stream_state
= g_hash_table_lookup(stream_states
, stream_msg
->stream
);
1693 BT_ASSERT_DBG(stream_state
);
1694 BT_ASSERT_DBG(stream_state
->state
== AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
);
1695 BT_ASSERT_DBG(!stream_state
->packet
);
1697 /* Update stream's state: this stream doesn't exist anymore. */
1698 g_hash_table_remove(stream_states
, stream_msg
->stream
);
1701 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1702 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1704 const struct bt_message_discarded_items
*discarded_msg
=
1706 struct auto_seek_stream_state
*stream_state
;
1708 stream_state
= g_hash_table_lookup(stream_states
, discarded_msg
->stream
);
1709 BT_ASSERT_DBG(stream_state
);
1711 if ((msg
->type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
&& discarded_msg
->stream
->class->discarded_events_have_default_clock_snapshots
) ||
1712 (msg
->type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
&& discarded_msg
->stream
->class->discarded_packets_have_default_clock_snapshots
)) {
1713 stream_state
->seen_clock_snapshot
= true;
1722 bt_object_put_ref_no_null_check(msg
);
1727 g_queue_push_tail(iterator
->auto_seek
.msgs
, (void *) msg
);
1731 BT_ASSERT_DBG(!msg
|| status
!= BT_FUNC_STATUS_OK
);
1736 int find_message_ge_ns_from_origin(
1737 struct bt_message_iterator
*iterator
,
1738 int64_t ns_from_origin
, GHashTable
*stream_states
)
1740 int status
= BT_FUNC_STATUS_OK
;
1741 enum bt_message_iterator_state init_state
=
1743 const struct bt_message
*messages
[MSG_BATCH_SIZE
];
1744 uint64_t user_count
= 0;
1746 bool got_first
= false;
1748 BT_ASSERT_DBG(iterator
);
1749 memset(&messages
[0], 0, sizeof(messages
[0]) * MSG_BATCH_SIZE
);
1752 * Make this iterator temporarily active (not seeking) to call
1753 * the "next" method.
1755 set_msg_iterator_state(iterator
,
1756 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1758 BT_ASSERT_DBG(iterator
->methods
.next
);
1760 while (!got_first
) {
1762 * Call the user's "next" method to get the next
1763 * messages and status.
1765 status
= call_iterator_next_method(iterator
,
1766 &messages
[0], MSG_BATCH_SIZE
, &user_count
);
1767 BT_LOGD("User method returned: status=%s",
1768 bt_common_func_status_string(status
));
1770 BT_LIB_LOGW_APPEND_CAUSE(
1771 "Component input port message iterator's \"next\" method failed: "
1772 "%![iter-]+i, status=%s",
1773 iterator
, bt_common_func_status_string(status
));
1777 * The user's "next" method must not do any action which
1778 * would change the iterator's state.
1780 BT_ASSERT_DBG(iterator
->state
==
1781 BT_MESSAGE_ITERATOR_STATE_ACTIVE
);
1784 case BT_FUNC_STATUS_OK
:
1785 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME
,
1786 "count-lteq-capacity",
1787 user_count
<= MSG_BATCH_SIZE
,
1788 "Invalid returned message count: greater than "
1789 "batch size: count=%" PRIu64
", batch-size=%u",
1790 user_count
, MSG_BATCH_SIZE
);
1792 case BT_FUNC_STATUS_AGAIN
:
1793 case BT_FUNC_STATUS_ERROR
:
1794 case BT_FUNC_STATUS_MEMORY_ERROR
:
1795 case BT_FUNC_STATUS_END
:
1801 for (i
= 0; i
< user_count
; i
++) {
1803 g_queue_push_tail(iterator
->auto_seek
.msgs
,
1804 (void *) messages
[i
]);
1809 status
= auto_seek_handle_message(iterator
,
1810 ns_from_origin
, messages
[i
], &got_first
,
1812 if (status
== BT_FUNC_STATUS_OK
) {
1813 /* Message was either pushed or moved */
1822 for (i
= 0; i
< user_count
; i
++) {
1824 bt_object_put_ref_no_null_check(messages
[i
]);
1828 set_msg_iterator_state(iterator
, init_state
);
1833 * This function is installed as the iterator's next callback after we have
1834 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1835 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1836 * next callback is put back.
1840 enum bt_message_iterator_class_next_method_status
post_auto_seek_next(
1841 struct bt_message_iterator
*iterator
,
1842 bt_message_array_const msgs
, uint64_t capacity
,
1845 BT_ASSERT(!g_queue_is_empty(iterator
->auto_seek
.msgs
));
1849 * Move auto-seek messages to the output array (which is this
1850 * iterator's base message array).
1852 while (capacity
> 0 && !g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1853 msgs
[*count
] = g_queue_pop_head(iterator
->auto_seek
.msgs
);
1858 BT_ASSERT(*count
> 0);
1860 if (g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
1861 /* No more auto-seek messages, restore user's next callback. */
1862 BT_ASSERT(iterator
->auto_seek
.original_next_callback
);
1863 iterator
->methods
.next
= iterator
->auto_seek
.original_next_callback
;
1864 iterator
->auto_seek
.original_next_callback
= NULL
;
1867 return BT_FUNC_STATUS_OK
;
1871 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1872 int64_t ns_from_origin
, uint64_t *raw_value
)
1875 int64_t cc_offset_s
= clock_class
->offset_seconds
;
1876 uint64_t cc_offset_cycles
= clock_class
->offset_cycles
;
1877 uint64_t cc_freq
= clock_class
->frequency
;
1879 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1880 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1884 bool message_iterator_can_seek_ns_from_origin(
1885 struct bt_message_iterator
*iterator
,
1886 int64_t ns_from_origin
)
1888 enum bt_message_iterator_can_seek_ns_from_origin_status status
;
1891 status
= bt_message_iterator_can_seek_ns_from_origin(
1892 iterator
, ns_from_origin
, &can_seek
);
1893 if (status
!= BT_FUNC_STATUS_OK
) {
1894 can_seek
= BT_FALSE
;
1900 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1901 "bt_message_iterator_class_seek_ns_from_origin_method"
1905 enum bt_message_iterator_seek_ns_from_origin_status
1906 bt_message_iterator_seek_ns_from_origin(
1907 struct bt_message_iterator
*iterator
,
1908 int64_t ns_from_origin
)
1911 GHashTable
*stream_states
= NULL
;
1912 bt_bool can_seek_by_itself
;
1914 BT_ASSERT_PRE_NO_ERROR();
1915 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
1916 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator
);
1917 BT_ASSERT_PRE("graph-is-configured",
1918 bt_component_borrow_graph(iterator
->upstream_component
)->config_state
!=
1919 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1920 "Graph is not configured: %!+g",
1921 bt_component_borrow_graph(iterator
->upstream_component
));
1922 /* The iterator must be able to seek ns from origin one way or another. */
1923 BT_ASSERT_PRE("can-seek-ns-from-origin",
1924 message_iterator_can_seek_ns_from_origin(iterator
, ns_from_origin
),
1925 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1926 "ns-from-origin=%" PRId64
, iterator
, ns_from_origin
);
1927 set_msg_iterator_state(iterator
,
1928 BT_MESSAGE_ITERATOR_STATE_SEEKING
);
1931 * We are seeking, reset our expectations about how the following
1932 * messages should look like.
1934 reset_iterator_expectations(iterator
);
1936 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1937 if (iterator
->methods
.can_seek_ns_from_origin
) {
1938 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1942 iterator
->methods
.can_seek_ns_from_origin(
1943 iterator
, ns_from_origin
, &can_seek_by_itself
);
1944 if (can_seek_status
!= BT_FUNC_STATUS_OK
) {
1945 status
= can_seek_status
;
1949 can_seek_by_itself
= false;
1952 if (can_seek_by_itself
) {
1953 /* The iterator knows how to seek to a particular time, let it handle this. */
1954 BT_ASSERT(iterator
->methods
.seek_ns_from_origin
);
1955 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1956 "%![iter-]+i, ns=%" PRId64
, iterator
, ns_from_origin
);
1957 status
= iterator
->methods
.seek_ns_from_origin(iterator
,
1959 BT_LOGD("User method returned: status=%s",
1960 bt_common_func_status_string(status
));
1961 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME
, "valid-status",
1962 status
== BT_FUNC_STATUS_OK
||
1963 status
== BT_FUNC_STATUS_ERROR
||
1964 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1965 status
== BT_FUNC_STATUS_AGAIN
,
1966 "Unexpected status: %![iter-]+i, status=%s",
1967 iterator
, bt_common_func_status_string(status
));
1968 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1969 SEEK_NS_FROM_ORIGIN_METHOD_NAME
, status
);
1971 BT_LIB_LOGW_APPEND_CAUSE(
1972 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1973 "%![iter-]+i, status=%s",
1974 iterator
, bt_common_func_status_string(status
));
1978 * The iterator doesn't know how to seek by itself to a
1979 * particular time. We will seek to the beginning and fast
1980 * forward to the right place.
1982 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status
;
1983 bt_bool can_seek_beginning
;
1985 can_seek_status
= iterator
->methods
.can_seek_beginning(iterator
,
1986 &can_seek_beginning
);
1987 BT_ASSERT(can_seek_status
== BT_FUNC_STATUS_OK
);
1988 BT_ASSERT(can_seek_beginning
);
1989 BT_ASSERT(iterator
->methods
.seek_beginning
);
1990 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1992 status
= iterator
->methods
.seek_beginning(iterator
);
1993 BT_LOGD("User method returned: status=%s",
1994 bt_common_func_status_string(status
));
1995 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME
, "valid-status",
1996 status
== BT_FUNC_STATUS_OK
||
1997 status
== BT_FUNC_STATUS_ERROR
||
1998 status
== BT_FUNC_STATUS_MEMORY_ERROR
||
1999 status
== BT_FUNC_STATUS_AGAIN
,
2000 "Unexpected status: %![iter-]+i, status=%s",
2001 iterator
, bt_common_func_status_string(status
));
2003 BT_LIB_LOGW_APPEND_CAUSE(
2004 "Component input port message iterator's \"seek beginning\" method failed: "
2005 "%![iter-]+i, status=%s",
2006 iterator
, bt_common_func_status_string(status
));
2009 clear_per_stream_state(iterator
);
2012 case BT_FUNC_STATUS_OK
:
2014 case BT_FUNC_STATUS_ERROR
:
2015 case BT_FUNC_STATUS_MEMORY_ERROR
:
2016 case BT_FUNC_STATUS_AGAIN
:
2023 * Find the first message which has a default clock
2024 * snapshot greater than or equal to the requested
2025 * seeking time, and move the received messages from
2026 * this point in the batch to this iterator's auto-seek
2029 while (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2030 bt_object_put_ref_no_null_check(
2031 g_queue_pop_tail(iterator
->auto_seek
.msgs
));
2034 stream_states
= create_auto_seek_stream_states();
2035 if (!stream_states
) {
2036 BT_LIB_LOGE_APPEND_CAUSE(
2037 "Failed to allocate one GHashTable.");
2038 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2042 status
= find_message_ge_ns_from_origin(iterator
,
2043 ns_from_origin
, stream_states
);
2045 case BT_FUNC_STATUS_OK
:
2046 case BT_FUNC_STATUS_END
:
2048 GHashTableIter iter
;
2049 gpointer key
, value
;
2052 * If some streams exist at the seek time, prepend the
2053 * required messages to put those streams in the right
2056 g_hash_table_iter_init(&iter
, stream_states
);
2057 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
2058 const bt_stream
*stream
= key
;
2059 struct auto_seek_stream_state
*stream_state
=
2060 (struct auto_seek_stream_state
*) value
;
2062 const bt_clock_class
*clock_class
= bt_stream_class_borrow_default_clock_class_const(
2063 bt_stream_borrow_class_const(stream
));
2064 /* Initialize to silence maybe-uninitialized warning. */
2065 uint64_t raw_value
= 0;
2068 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2069 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2071 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2072 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2073 * seen a message with a clock snapshot in our seeking, then we are sure that the
2074 * seek time is not below the clock range, and we know the stream was active at that
2075 * time (and that we cut it short).
2077 if (stream_state
->seen_clock_snapshot
) {
2078 if (clock_raw_value_from_ns_from_origin(clock_class
, ns_from_origin
, &raw_value
) != 0) {
2079 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64
", %![cc-]+K",
2080 ns_from_origin
, clock_class
);
2081 status
= BT_FUNC_STATUS_ERROR
;
2086 switch (stream_state
->state
) {
2087 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN
:
2088 BT_ASSERT(stream_state
->packet
);
2089 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state
->packet
);
2091 if (stream
->class->packets_have_beginning_default_clock_snapshot
) {
2093 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2094 * message. If "packet beginning" packets have clock snapshots, then we must have
2095 * seen a clock snapshot.
2097 BT_ASSERT(stream_state
->seen_clock_snapshot
);
2099 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
2100 (bt_self_message_iterator
*) iterator
, stream_state
->packet
, raw_value
);
2102 msg
= bt_message_packet_beginning_create((bt_self_message_iterator
*) iterator
,
2103 stream_state
->packet
);
2107 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2111 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2115 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN
:
2116 msg
= bt_message_stream_beginning_create(
2117 (bt_self_message_iterator
*) iterator
, stream
);
2119 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
2123 if (stream_state
->seen_clock_snapshot
) {
2124 bt_message_stream_beginning_set_default_clock_snapshot(msg
, raw_value
);
2127 g_queue_push_head(iterator
->auto_seek
.msgs
, msg
);
2134 * If there are messages in the auto-seek
2135 * message queue, replace the user's "next"
2136 * method with a custom, temporary "next" method
2137 * which returns them.
2139 if (!g_queue_is_empty(iterator
->auto_seek
.msgs
)) {
2140 BT_ASSERT(!iterator
->auto_seek
.original_next_callback
);
2141 iterator
->auto_seek
.original_next_callback
= iterator
->methods
.next
;
2143 iterator
->methods
.next
=
2144 (bt_message_iterator_next_method
)
2145 post_auto_seek_next
;
2149 * `BT_FUNC_STATUS_END` becomes
2150 * `BT_FUNC_STATUS_OK`: the next
2151 * time this iterator's "next" method is called,
2153 * `BT_FUNC_STATUS_END`.
2155 status
= BT_FUNC_STATUS_OK
;
2158 case BT_FUNC_STATUS_ERROR
:
2159 case BT_FUNC_STATUS_MEMORY_ERROR
:
2160 case BT_FUNC_STATUS_AGAIN
:
2167 clear_per_stream_state(iterator
);
2170 * The following messages returned by the next method (including
2171 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2173 iterator
->last_ns_from_origin
= ns_from_origin
;
2176 if (stream_states
) {
2177 destroy_auto_seek_stream_states(stream_states
);
2178 stream_states
= NULL
;
2181 set_iterator_state_after_seeking(iterator
, status
);
2186 bt_bool
bt_self_message_iterator_is_interrupted(
2187 const struct bt_self_message_iterator
*self_msg_iter
)
2189 const struct bt_message_iterator
*iterator
=
2190 (const void *) self_msg_iter
;
2192 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator
);
2193 return (bt_bool
) bt_graph_is_interrupted(iterator
->graph
);
2197 void bt_message_iterator_get_ref(
2198 const struct bt_message_iterator
*iterator
)
2200 bt_object_get_ref(iterator
);
2204 void bt_message_iterator_put_ref(
2205 const struct bt_message_iterator
*iterator
)
2207 bt_object_put_ref(iterator
);