ccc12d615f9c203d5002580702c8f07317c08e16
[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 BT_HIDDEN
142 void bt_message_iterator_try_finalize(
143 struct bt_message_iterator *iterator)
144 {
145 uint64_t i;
146 bool call_user_finalize = true;
147
148 BT_ASSERT(iterator);
149
150 switch (iterator->state) {
151 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
152 /*
153 * If this function is called while the iterator is in the
154 * NON_INITIALIZED state, it means the user initialization
155 * method has either not been called, or has failed. We
156 * therefore don't want to call the user finalization method.
157 * However, the initialization method might have created some
158 * upstream message iterators before failing, so we want to
159 * execute the rest of this function, which unlinks the related
160 * iterators.
161 */
162 call_user_finalize = false;
163 break;
164 case BT_MESSAGE_ITERATOR_STATE_FINALIZED:
165 /* Already finalized */
166 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
167 "%!+i", iterator);
168 goto end;
169 case BT_MESSAGE_ITERATOR_STATE_FINALIZING:
170 /* Finalizing */
171 BT_LIB_LOGF("Message iterator is already being finalized: "
172 "%!+i", iterator);
173 bt_common_abort();
174 default:
175 break;
176 }
177
178 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
179 set_msg_iterator_state(iterator,
180 BT_MESSAGE_ITERATOR_STATE_FINALIZING);
181 BT_ASSERT(iterator->upstream_component);
182
183 /* Call user-defined destroy method */
184 if (call_user_finalize) {
185 typedef void (*method_t)(void *);
186 method_t method;
187 struct bt_component_class *comp_class =
188 iterator->upstream_component->class;
189 struct bt_component_class_with_iterator_class *class_with_iter_class;
190
191 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class));
192 class_with_iter_class = container_of(comp_class,
193 struct bt_component_class_with_iterator_class, parent);
194 method = (method_t) class_with_iter_class->msg_iter_cls->methods.finalize;
195
196 if (method) {
197 const bt_error *saved_error;
198
199 saved_error = bt_current_thread_take_error();
200
201 BT_LIB_LOGD("Calling user's finalization method: %!+i",
202 iterator);
203 method(iterator);
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 BT_HIDDEN
243 void bt_message_iterator_set_connection(
244 struct bt_message_iterator *iterator,
245 struct bt_connection *connection)
246 {
247 BT_ASSERT(iterator);
248 iterator->connection = connection;
249 BT_LIB_LOGI("Set message iterator's connection: "
250 "%![iter-]+i, %![conn-]+x", iterator, connection);
251 }
252
253 static
254 enum bt_message_iterator_can_seek_beginning_status can_seek_ns_from_origin_true(
255 struct bt_message_iterator *iterator,
256 int64_t ns_from_origin, 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,
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_message_iterator_create_from_message_iterator_status
460 bt_message_iterator_create_from_message_iterator(
461 struct bt_self_message_iterator *self_msg_iter,
462 struct bt_self_component_port_input *input_port,
463 struct bt_message_iterator **message_iterator)
464 {
465 BT_ASSERT_PRE_NO_ERROR();
466 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter);
467 return create_self_component_input_port_message_iterator(self_msg_iter,
468 input_port, message_iterator, __func__);
469 }
470
471 bt_message_iterator_create_from_sink_component_status
472 bt_message_iterator_create_from_sink_component(
473 struct bt_self_component_sink *self_comp,
474 struct bt_self_component_port_input *input_port,
475 struct bt_message_iterator **message_iterator)
476 {
477 BT_ASSERT_PRE_NO_ERROR();
478 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp, "Sink component");
479 return create_self_component_input_port_message_iterator(NULL,
480 input_port, message_iterator, __func__);
481 }
482
483 void *bt_self_message_iterator_get_data(
484 const struct bt_self_message_iterator *self_iterator)
485 {
486 struct bt_message_iterator *iterator =
487 (void *) self_iterator;
488
489 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
490 return iterator->user_data;
491 }
492
493 void bt_self_message_iterator_set_data(
494 struct bt_self_message_iterator *self_iterator, void *data)
495 {
496 struct bt_message_iterator *iterator =
497 (void *) self_iterator;
498
499 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
500 iterator->user_data = data;
501 BT_LIB_LOGD("Set message iterator's user data: "
502 "%!+i, user-data-addr=%p", iterator, data);
503 }
504
505 void bt_self_message_iterator_configuration_set_can_seek_forward(
506 bt_self_message_iterator_configuration *config,
507 bt_bool can_seek_forward)
508 {
509 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config,
510 "Message iterator configuration");
511 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config,
512 "Message iterator configuration", "");
513
514 config->can_seek_forward = can_seek_forward;
515 }
516
517 /*
518 * Validate that the default clock snapshot in `msg` doesn't make us go back in
519 * time.
520 */
521
522 BT_ASSERT_COND_DEV_FUNC
523 static
524 bool clock_snapshots_are_monotonic_one(
525 struct bt_message_iterator *iterator,
526 const bt_message *msg)
527 {
528 const struct bt_clock_snapshot *clock_snapshot = NULL;
529 bt_message_type message_type = bt_message_get_type(msg);
530 int64_t ns_from_origin;
531 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status;
532
533 /*
534 * The default is true: if we can't figure out the clock snapshot
535 * (or there is none), assume it is fine.
536 */
537 bool result = true;
538
539 switch (message_type) {
540 case BT_MESSAGE_TYPE_EVENT:
541 {
542 struct bt_message_event *event_msg = (struct bt_message_event *) msg;
543 clock_snapshot = event_msg->default_cs;
544 break;
545 }
546 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
547 {
548 struct bt_message_message_iterator_inactivity *inactivity_msg =
549 (struct bt_message_message_iterator_inactivity *) msg;
550 clock_snapshot = inactivity_msg->cs;
551 break;
552 }
553 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
554 case BT_MESSAGE_TYPE_PACKET_END:
555 {
556 struct bt_message_packet *packet_msg = (struct bt_message_packet *) msg;
557 clock_snapshot = packet_msg->default_cs;
558 break;
559 }
560 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
561 case BT_MESSAGE_TYPE_STREAM_END:
562 {
563 struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
564 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
565 goto end;
566 }
567
568 clock_snapshot = stream_msg->default_cs;
569 break;
570 }
571 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
572 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
573 {
574 struct bt_message_discarded_items *discarded_msg =
575 (struct bt_message_discarded_items *) msg;
576
577 clock_snapshot = discarded_msg->default_begin_cs;
578 break;
579 }
580 }
581
582 if (!clock_snapshot) {
583 goto end;
584 }
585
586 clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(
587 clock_snapshot, &ns_from_origin);
588 if (clock_snapshot_status != BT_FUNC_STATUS_OK) {
589 /*
590 * bt_clock_snapshot_get_ns_from_origin can return
591 * OVERFLOW_ERROR. We don't really want to report an error to
592 * our caller, so just clear it.
593 */
594 bt_current_thread_clear_error();
595 goto end;
596 }
597
598 result = ns_from_origin >= iterator->last_ns_from_origin;
599 iterator->last_ns_from_origin = ns_from_origin;
600 end:
601 return result;
602 }
603
604 BT_ASSERT_COND_DEV_FUNC
605 static
606 bool clock_snapshots_are_monotonic(
607 struct bt_message_iterator *iterator,
608 bt_message_array_const msgs, uint64_t msg_count)
609 {
610 uint64_t i;
611 bool result;
612
613 for (i = 0; i < msg_count; i++) {
614 if (!clock_snapshots_are_monotonic_one(iterator, msgs[i])) {
615 result = false;
616 goto end;
617 }
618 }
619
620 result = true;
621
622 end:
623 return result;
624 }
625
626 /*
627 * When a new stream begins, verify that the clock class tied to this
628 * stream is compatible with what we've seen before.
629 */
630
631 BT_ASSERT_COND_DEV_FUNC
632 static
633 bool clock_classes_are_compatible_one(struct bt_message_iterator *iterator,
634 const struct bt_message *msg)
635 {
636 enum bt_message_type message_type = bt_message_get_type(msg);
637 bool result;
638
639 if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) {
640 const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
641 const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class;
642 bt_uuid clock_class_uuid = NULL;
643
644 if (clock_class) {
645 clock_class_uuid = bt_clock_class_get_uuid(clock_class);
646 }
647
648 switch (iterator->clock_expectation.type) {
649 case CLOCK_EXPECTATION_UNSET:
650 /*
651 * This is the first time we see a message with a clock
652 * snapshot: record the properties of that clock, against
653 * which we'll compare the clock properties of the following
654 * messages.
655 */
656
657 if (!clock_class) {
658 iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE;
659 } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
660 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX;
661 } else if (clock_class_uuid) {
662 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID;
663 bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
664 } else {
665 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID;
666 }
667 break;
668
669 case CLOCK_EXPECTATION_NONE:
670 if (clock_class) {
671 BT_ASSERT_COND_DEV_MSG(
672 "Expecting no clock class, got one: %![cc-]+K",
673 clock_class);
674 result = false;
675 goto end;
676 }
677
678 break;
679
680 case CLOCK_EXPECTATION_ORIGIN_UNIX:
681 if (!clock_class) {
682 BT_ASSERT_COND_DEV_MSG(
683 "Expecting a clock class, got none.");
684 result = false;
685 goto end;
686 }
687
688 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
689 BT_ASSERT_COND_DEV_MSG(
690 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
691 clock_class);
692 result = false;
693 goto end;
694 }
695 break;
696
697 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID:
698 if (!clock_class) {
699 BT_ASSERT_COND_DEV_MSG(
700 "Expecting a clock class, got none.");
701 result = false;
702 goto end;
703 }
704
705 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
706 BT_ASSERT_COND_DEV_MSG(
707 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
708 clock_class);
709 result = false;
710 goto end;
711 }
712
713 if (!clock_class_uuid) {
714 BT_ASSERT_COND_DEV_MSG(
715 "Expecting a clock class with UUID: %![cc-]+K",
716 clock_class);
717 result = false;
718 goto end;
719 }
720
721 if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
722 BT_ASSERT_COND_DEV_MSG(
723 "Expecting a clock class with UUID, got one "
724 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
725 clock_class, iterator->clock_expectation.uuid);
726 result = false;
727 goto end;
728 }
729 break;
730
731 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID:
732 if (!clock_class) {
733 BT_ASSERT_COND_DEV_MSG(
734 "Expecting a clock class, got none.");
735 result = false;
736 goto end;
737 }
738
739 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
740 BT_ASSERT_COND_DEV_MSG(
741 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
742 clock_class);
743 result = false;
744 goto end;
745 }
746
747 if (clock_class_uuid) {
748 BT_ASSERT_COND_DEV_MSG(
749 "Expecting a clock class without UUID: %![cc-]+K",
750 clock_class);
751 result = false;
752 goto end;
753 }
754 break;
755 }
756 }
757
758 result = true;
759
760 end:
761 return result;
762 }
763
764 BT_ASSERT_COND_DEV_FUNC
765 static
766 bool clock_classes_are_compatible(
767 struct bt_message_iterator *iterator,
768 bt_message_array_const msgs, uint64_t msg_count)
769 {
770 uint64_t i;
771 bool result;
772
773 for (i = 0; i < msg_count; i++) {
774 if (!clock_classes_are_compatible_one(iterator, msgs[i])) {
775 result = false;
776 goto end;
777 }
778 }
779
780 result = true;
781
782 end:
783 return result;
784 }
785
786 /*
787 * Call the `next` method of the iterator. Do some validation on the returned
788 * messages.
789 */
790
791 #define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
792
793 static
794 enum bt_message_iterator_class_next_method_status
795 call_iterator_next_method(
796 struct bt_message_iterator *iterator,
797 bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
798 {
799 enum bt_message_iterator_class_next_method_status status;
800
801 BT_ASSERT_DBG(iterator->methods.next);
802 BT_LOGD_STR("Calling user's \"next\" method.");
803 status = iterator->methods.next(iterator, msgs, capacity, user_count);
804 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
805 bt_common_func_status_string(status), *user_count);
806
807 if (status == BT_FUNC_STATUS_OK) {
808 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
809 "message-clock-classes-are-compatible",
810 clock_classes_are_compatible(iterator, msgs,
811 *user_count),
812 "Clocks are not compatible");
813 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
814 "message-clock-snapshots-are-monotonic",
815 clock_snapshots_are_monotonic(iterator, msgs,
816 *user_count),
817 "Clock snapshots are not monotonic");
818 }
819
820 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME,
821 status);
822
823 return status;
824 }
825
826 enum bt_message_iterator_next_status
827 bt_message_iterator_next(
828 struct bt_message_iterator *iterator,
829 bt_message_array_const *msgs, uint64_t *user_count)
830 {
831 enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
832
833 BT_ASSERT_PRE_DEV_NO_ERROR();
834 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
835 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs,
836 "Message array (output)");
837 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count,
838 "Message count (output)");
839 BT_ASSERT_PRE_DEV("message-iterator-is-active",
840 iterator->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE,
841 "Message iterator's \"next\" called, but "
842 "message iterator is in the wrong state: %!+i", iterator);
843 BT_ASSERT_DBG(iterator->upstream_component);
844 BT_ASSERT_DBG(iterator->upstream_component->class);
845 BT_ASSERT_PRE_DEV("graph-is-configured",
846 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
847 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
848 "Graph is not configured: %!+g",
849 bt_component_borrow_graph(iterator->upstream_component));
850 BT_LIB_LOGD("Getting next self component input port "
851 "message iterator's messages: %!+i, batch-size=%u",
852 iterator, MSG_BATCH_SIZE);
853
854 /*
855 * Call the user's "next" method to get the next messages
856 * and status.
857 */
858 *user_count = 0;
859 status = (int) call_iterator_next_method(iterator,
860 (void *) iterator->msgs->pdata, MSG_BATCH_SIZE,
861 user_count);
862 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
863 bt_common_func_status_string(status), *user_count);
864 if (status < 0) {
865 BT_LIB_LOGW_APPEND_CAUSE(
866 "Component input port message iterator's \"next\" method failed: "
867 "%![iter-]+i, status=%s",
868 iterator, bt_common_func_status_string(status));
869 goto end;
870 }
871
872 /*
873 * There is no way that this iterator could have been finalized
874 * during its "next" method, as the only way to do this is to
875 * put the last iterator's reference, and this can only be done
876 * by its downstream owner.
877 *
878 * For the same reason, there is no way that this iterator could
879 * have seeked (cannot seek a self message iterator).
880 */
881 BT_ASSERT_DBG(iterator->state ==
882 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
883
884 switch (status) {
885 case BT_FUNC_STATUS_OK:
886 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME, "count-lteq-capacity",
887 *user_count <= MSG_BATCH_SIZE,
888 "Invalid returned message count: greater than "
889 "batch size: count=%" PRIu64 ", batch-size=%u",
890 *user_count, MSG_BATCH_SIZE);
891 *msgs = (void *) iterator->msgs->pdata;
892 break;
893 case BT_FUNC_STATUS_AGAIN:
894 goto end;
895 case BT_FUNC_STATUS_END:
896 set_msg_iterator_state(iterator,
897 BT_MESSAGE_ITERATOR_STATE_ENDED);
898 goto end;
899 default:
900 /* Unknown non-error status */
901 bt_common_abort();
902 }
903
904 end:
905 return status;
906 }
907
908 struct bt_component *
909 bt_message_iterator_borrow_component(
910 struct bt_message_iterator *iterator)
911 {
912 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
913 return iterator->upstream_component;
914 }
915
916 struct bt_self_component *bt_self_message_iterator_borrow_component(
917 struct bt_self_message_iterator *self_iterator)
918 {
919 struct bt_message_iterator *iterator =
920 (void *) self_iterator;
921
922 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
923 return (void *) iterator->upstream_component;
924 }
925
926 struct bt_self_component_port_output *bt_self_message_iterator_borrow_port(
927 struct bt_self_message_iterator *self_iterator)
928 {
929 struct bt_message_iterator *iterator =
930 (void *) self_iterator;
931
932 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
933 return (void *) iterator->upstream_port;
934 }
935
936 #define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
937 "bt_message_iterator_class_can_seek_ns_from_origin_method"
938
939 enum bt_message_iterator_can_seek_ns_from_origin_status
940 bt_message_iterator_can_seek_ns_from_origin(
941 struct bt_message_iterator *iterator,
942 int64_t ns_from_origin, bt_bool *can_seek)
943 {
944 enum bt_message_iterator_can_seek_ns_from_origin_status status;
945
946 BT_ASSERT_PRE_NO_ERROR();
947 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
948 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek);
949 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
950 BT_ASSERT_PRE("graph-is-configured",
951 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
952 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
953 "Graph is not configured: %!+g",
954 bt_component_borrow_graph(iterator->upstream_component));
955
956 if (iterator->methods.can_seek_ns_from_origin) {
957 /*
958 * Initialize to an invalid value, so we can post-assert that
959 * the method returned a valid value.
960 */
961 *can_seek = -1;
962
963 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
964 iterator);
965
966 status = (int) iterator->methods.can_seek_ns_from_origin(iterator,
967 ns_from_origin, can_seek);
968
969 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
970 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME, status);
971
972 if (status != BT_FUNC_STATUS_OK) {
973 BT_LIB_LOGW_APPEND_CAUSE(
974 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
975 "%![iter-]+i, status=%s",
976 iterator, bt_common_func_status_string(status));
977 goto end;
978 }
979
980 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME,
981 "valid-return-value",
982 *can_seek == BT_TRUE || *can_seek == BT_FALSE,
983 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
984 *can_seek, iterator);
985
986 BT_LIB_LOGD(
987 "User's \"can seek nanoseconds from origin\" returned successfully: "
988 "%![iter-]+i, can-seek=%d",
989 iterator, *can_seek);
990
991 if (*can_seek) {
992 goto end;
993 }
994 }
995
996 /*
997 * Automatic seeking fall back: if we can seek to the beginning and the
998 * iterator supports forward seeking then we can automatically seek to
999 * any timestamp.
1000 */
1001 status = (int) bt_message_iterator_can_seek_beginning(
1002 iterator, can_seek);
1003 if (status != BT_FUNC_STATUS_OK) {
1004 goto end;
1005 }
1006
1007 *can_seek = *can_seek && iterator->config.can_seek_forward;
1008
1009 end:
1010 return status;
1011 }
1012
1013 #define CAN_SEEK_BEGINNING_METHOD_NAME \
1014 "bt_message_iterator_class_can_seek_beginning"
1015
1016 enum bt_message_iterator_can_seek_beginning_status
1017 bt_message_iterator_can_seek_beginning(
1018 struct bt_message_iterator *iterator,
1019 bt_bool *can_seek)
1020 {
1021 enum bt_message_iterator_can_seek_beginning_status status;
1022
1023 BT_ASSERT_PRE_NO_ERROR();
1024 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1025 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek);
1026 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1027 BT_ASSERT_PRE("graph-is-configured",
1028 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1029 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1030 "Graph is not configured: %!+g",
1031 bt_component_borrow_graph(iterator->upstream_component));
1032
1033 if (iterator->methods.can_seek_beginning) {
1034 /*
1035 * Initialize to an invalid value, so we can post-assert that
1036 * the method returned a valid value.
1037 */
1038 *can_seek = -1;
1039
1040 status = (int) iterator->methods.can_seek_beginning(iterator, can_seek);
1041
1042 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME,
1043 "valid-return-value",
1044 status != BT_FUNC_STATUS_OK ||
1045 *can_seek == BT_TRUE ||
1046 *can_seek == BT_FALSE,
1047 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1048 *can_seek, iterator);
1049 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1050 CAN_SEEK_BEGINNING_METHOD_NAME, status);
1051 } else {
1052 *can_seek = BT_FALSE;
1053 status = BT_FUNC_STATUS_OK;
1054 }
1055
1056 return status;
1057 }
1058
1059 static inline
1060 void set_iterator_state_after_seeking(
1061 struct bt_message_iterator *iterator,
1062 int status)
1063 {
1064 enum bt_message_iterator_state new_state = 0;
1065
1066 /* Set iterator's state depending on seeking status */
1067 switch (status) {
1068 case BT_FUNC_STATUS_OK:
1069 new_state = BT_MESSAGE_ITERATOR_STATE_ACTIVE;
1070 break;
1071 case BT_FUNC_STATUS_AGAIN:
1072 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
1073 break;
1074 case BT_FUNC_STATUS_ERROR:
1075 case BT_FUNC_STATUS_MEMORY_ERROR:
1076 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
1077 break;
1078 case BT_FUNC_STATUS_END:
1079 new_state = BT_MESSAGE_ITERATOR_STATE_ENDED;
1080 break;
1081 default:
1082 bt_common_abort();
1083 }
1084
1085 set_msg_iterator_state(iterator, new_state);
1086 }
1087
1088 static
1089 void reset_iterator_expectations(
1090 struct bt_message_iterator *iterator)
1091 {
1092 iterator->last_ns_from_origin = INT64_MIN;
1093 iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET;
1094 }
1095
1096 static
1097 bool message_iterator_can_seek_beginning(
1098 struct bt_message_iterator *iterator)
1099 {
1100 enum bt_message_iterator_can_seek_beginning_status status;
1101 bt_bool can_seek;
1102
1103 status = bt_message_iterator_can_seek_beginning(
1104 iterator, &can_seek);
1105 if (status != BT_FUNC_STATUS_OK) {
1106 can_seek = BT_FALSE;
1107 }
1108
1109 return can_seek;
1110 }
1111
1112 #define SEEK_BEGINNING_METHOD_NAME \
1113 "bt_message_iterator_class_seek_beginning_method"
1114
1115 enum bt_message_iterator_seek_beginning_status
1116 bt_message_iterator_seek_beginning(struct bt_message_iterator *iterator)
1117 {
1118 int status;
1119
1120 BT_ASSERT_PRE_NO_ERROR();
1121 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1122 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1123 BT_ASSERT_PRE("graph-is-configured",
1124 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1125 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1126 "Graph is not configured: %!+g",
1127 bt_component_borrow_graph(iterator->upstream_component));
1128 BT_ASSERT_PRE("can-seek-beginning",
1129 message_iterator_can_seek_beginning(iterator),
1130 "Message iterator cannot seek beginning: %!+i", iterator);
1131
1132 /*
1133 * We are seeking, reset our expectations about how the following
1134 * messages should look like.
1135 */
1136 reset_iterator_expectations(iterator);
1137
1138 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
1139 set_msg_iterator_state(iterator,
1140 BT_MESSAGE_ITERATOR_STATE_SEEKING);
1141 status = iterator->methods.seek_beginning(iterator);
1142 BT_LOGD("User method returned: status=%s",
1143 bt_common_func_status_string(status));
1144 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME, "valid-status",
1145 status == BT_FUNC_STATUS_OK ||
1146 status == BT_FUNC_STATUS_ERROR ||
1147 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1148 status == BT_FUNC_STATUS_AGAIN,
1149 "Unexpected status: %![iter-]+i, status=%s",
1150 iterator, bt_common_func_status_string(status));
1151 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME,
1152 status);
1153 if (status < 0) {
1154 BT_LIB_LOGW_APPEND_CAUSE(
1155 "Component input port message iterator's \"seek beginning\" method failed: "
1156 "%![iter-]+i, status=%s",
1157 iterator, bt_common_func_status_string(status));
1158 }
1159
1160 set_iterator_state_after_seeking(iterator, status);
1161 return status;
1162 }
1163
1164 bt_bool
1165 bt_message_iterator_can_seek_forward(
1166 bt_message_iterator *iterator)
1167 {
1168 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1169
1170 return iterator->config.can_seek_forward;
1171 }
1172
1173 /*
1174 * Structure used to record the state of a given stream during the fast-forward
1175 * phase of an auto-seek.
1176 */
1177 struct auto_seek_stream_state {
1178 /*
1179 * Value representing which step of this timeline we are at.
1180 *
1181 * time --->
1182 * [SB] 1 [PB] 2 [PE] 1 [SE]
1183 *
1184 * At each point in the timeline, the messages we need to replicate are:
1185 *
1186 * 1: Stream beginning
1187 * 2: Stream beginning, packet beginning
1188 *
1189 * Before "Stream beginning" and after "Stream end", we don't need to
1190 * replicate anything as the stream doesn't exist.
1191 */
1192 enum {
1193 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN,
1194 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN,
1195 } state;
1196
1197 /*
1198 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1199 * in. This is a weak reference, since the packet will always be
1200 * alive by the time we use it.
1201 */
1202 struct bt_packet *packet;
1203
1204 /* Have we see a message with a clock snapshot yet? */
1205 bool seen_clock_snapshot;
1206 };
1207
1208 static
1209 struct auto_seek_stream_state *create_auto_seek_stream_state(void)
1210 {
1211 return g_new0(struct auto_seek_stream_state, 1);
1212 }
1213
1214 static
1215 void destroy_auto_seek_stream_state(void *ptr)
1216 {
1217 g_free(ptr);
1218 }
1219
1220 static
1221 GHashTable *create_auto_seek_stream_states(void)
1222 {
1223 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
1224 destroy_auto_seek_stream_state);
1225 }
1226
1227 static
1228 void destroy_auto_seek_stream_states(GHashTable *stream_states)
1229 {
1230 g_hash_table_destroy(stream_states);
1231 }
1232
1233 /*
1234 * Handle one message while we are in the fast-forward phase of an auto-seek.
1235 *
1236 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1237 * `ns_from_origin`. In other words, if this is the first message after our
1238 * seek point.
1239 *
1240 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1241 * `struct auto_seek_stream_state` used to keep the state of each stream
1242 * during the fast-forward.
1243 */
1244
1245 static inline
1246 int auto_seek_handle_message(
1247 struct bt_message_iterator *iterator,
1248 int64_t ns_from_origin, const struct bt_message *msg,
1249 bool *got_first, GHashTable *stream_states)
1250 {
1251 int status = BT_FUNC_STATUS_OK;
1252 int64_t msg_ns_from_origin;
1253 const struct bt_clock_snapshot *clk_snapshot = NULL;
1254 int ret;
1255
1256 BT_ASSERT_DBG(msg);
1257 BT_ASSERT_DBG(got_first);
1258
1259 switch (msg->type) {
1260 case BT_MESSAGE_TYPE_EVENT:
1261 {
1262 const struct bt_message_event *event_msg =
1263 (const void *) msg;
1264
1265 clk_snapshot = event_msg->default_cs;
1266 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1267 "event-message-has-default-clock-snapshot",
1268 clk_snapshot,
1269 "Event message has no default clock snapshot: %!+n",
1270 event_msg);
1271 break;
1272 }
1273 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
1274 {
1275 const struct bt_message_message_iterator_inactivity *inactivity_msg =
1276 (const void *) msg;
1277
1278 clk_snapshot = inactivity_msg->cs;
1279 BT_ASSERT_DBG(clk_snapshot);
1280 break;
1281 }
1282 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1283 case BT_MESSAGE_TYPE_PACKET_END:
1284 {
1285 const struct bt_message_packet *packet_msg =
1286 (const void *) msg;
1287
1288 clk_snapshot = packet_msg->default_cs;
1289 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1290 "packet-message-has-default-clock-snapshot",
1291 clk_snapshot,
1292 "Packet message has no default clock snapshot: %!+n",
1293 packet_msg);
1294 break;
1295 }
1296 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1297 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1298 {
1299 struct bt_message_discarded_items *msg_disc_items =
1300 (void *) msg;
1301
1302 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1303 "discarded-events-packets-message-has-default-clock-snapshot",
1304 msg_disc_items->default_begin_cs &&
1305 msg_disc_items->default_end_cs,
1306 "Discarded events/packets message has no default clock snapshots: %!+n",
1307 msg_disc_items);
1308 ret = bt_clock_snapshot_get_ns_from_origin(
1309 msg_disc_items->default_begin_cs,
1310 &msg_ns_from_origin);
1311 if (ret) {
1312 status = BT_FUNC_STATUS_ERROR;
1313 goto end;
1314 }
1315
1316 if (msg_ns_from_origin >= ns_from_origin) {
1317 *got_first = true;
1318 goto push_msg;
1319 }
1320
1321 ret = bt_clock_snapshot_get_ns_from_origin(
1322 msg_disc_items->default_end_cs,
1323 &msg_ns_from_origin);
1324 if (ret) {
1325 status = BT_FUNC_STATUS_ERROR;
1326 goto end;
1327 }
1328
1329 if (msg_ns_from_origin >= ns_from_origin) {
1330 /*
1331 * The discarded items message's beginning time
1332 * is before the requested seeking time, but its
1333 * end time is after. Modify the message so as
1334 * to set its beginning time to the requested
1335 * seeking time, and make its item count unknown
1336 * as we don't know if items were really
1337 * discarded within the new time range.
1338 */
1339 uint64_t new_begin_raw_value = 0;
1340
1341 ret = bt_clock_class_clock_value_from_ns_from_origin(
1342 msg_disc_items->default_end_cs->clock_class,
1343 ns_from_origin, &new_begin_raw_value);
1344 if (ret) {
1345 status = BT_FUNC_STATUS_ERROR;
1346 goto end;
1347 }
1348
1349 bt_clock_snapshot_set_raw_value(
1350 msg_disc_items->default_begin_cs,
1351 new_begin_raw_value);
1352 msg_disc_items->count.base.avail =
1353 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
1354
1355 /*
1356 * It is safe to push it because its beginning
1357 * time is exactly the requested seeking time.
1358 */
1359 goto push_msg;
1360 } else {
1361 goto skip_msg;
1362 }
1363 }
1364 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1365 case BT_MESSAGE_TYPE_STREAM_END:
1366 {
1367 struct bt_message_stream *stream_msg =
1368 (struct bt_message_stream *) msg;
1369
1370 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1371 /* Ignore */
1372 goto skip_msg;
1373 }
1374
1375 clk_snapshot = stream_msg->default_cs;
1376 break;
1377 }
1378 default:
1379 bt_common_abort();
1380 }
1381
1382 BT_ASSERT_DBG(clk_snapshot);
1383 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1384 &msg_ns_from_origin);
1385 if (ret) {
1386 status = BT_FUNC_STATUS_ERROR;
1387 goto end;
1388 }
1389
1390 if (msg_ns_from_origin >= ns_from_origin) {
1391 *got_first = true;
1392 goto push_msg;
1393 }
1394
1395 skip_msg:
1396 /* This message won't be sent downstream. */
1397 switch (msg->type) {
1398 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1399 {
1400 const struct bt_message_stream *stream_msg = (const void *) msg;
1401 struct auto_seek_stream_state *stream_state;
1402
1403 /* Update stream's state: stream began. */
1404 stream_state = create_auto_seek_stream_state();
1405 if (!stream_state) {
1406 status = BT_FUNC_STATUS_MEMORY_ERROR;
1407 goto end;
1408 }
1409
1410 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
1411
1412 if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1413 stream_state->seen_clock_snapshot = true;
1414 }
1415
1416 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
1417 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
1418 break;
1419 }
1420 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1421 {
1422 const struct bt_message_packet *packet_msg =
1423 (const void *) msg;
1424 struct auto_seek_stream_state *stream_state;
1425
1426 /* Update stream's state: packet began. */
1427 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1428 BT_ASSERT_DBG(stream_state);
1429 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1430 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
1431 BT_ASSERT_DBG(!stream_state->packet);
1432 stream_state->packet = packet_msg->packet;
1433
1434 if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1435 stream_state->seen_clock_snapshot = true;
1436 }
1437
1438 break;
1439 }
1440 case BT_MESSAGE_TYPE_EVENT:
1441 {
1442 const struct bt_message_event *event_msg = (const void *) msg;
1443 struct auto_seek_stream_state *stream_state;
1444
1445 stream_state = g_hash_table_lookup(stream_states,
1446 event_msg->event->stream);
1447 BT_ASSERT_DBG(stream_state);
1448
1449 // HELPME: are we sure that event messages have clock snapshots at this point?
1450 stream_state->seen_clock_snapshot = true;
1451
1452 break;
1453 }
1454 case BT_MESSAGE_TYPE_PACKET_END:
1455 {
1456 const struct bt_message_packet *packet_msg =
1457 (const void *) msg;
1458 struct auto_seek_stream_state *stream_state;
1459
1460 /* Update stream's state: packet ended. */
1461 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1462 BT_ASSERT_DBG(stream_state);
1463 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
1464 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
1465 BT_ASSERT_DBG(stream_state->packet);
1466 stream_state->packet = NULL;
1467
1468 if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1469 stream_state->seen_clock_snapshot = true;
1470 }
1471
1472 break;
1473 }
1474 case BT_MESSAGE_TYPE_STREAM_END:
1475 {
1476 const struct bt_message_stream *stream_msg = (const void *) msg;
1477 struct auto_seek_stream_state *stream_state;
1478
1479 stream_state = g_hash_table_lookup(stream_states, stream_msg->stream);
1480 BT_ASSERT_DBG(stream_state);
1481 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1482 BT_ASSERT_DBG(!stream_state->packet);
1483
1484 /* Update stream's state: this stream doesn't exist anymore. */
1485 g_hash_table_remove(stream_states, stream_msg->stream);
1486 break;
1487 }
1488 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1489 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1490 {
1491 const struct bt_message_discarded_items *discarded_msg =
1492 (const void *) msg;
1493 struct auto_seek_stream_state *stream_state;
1494
1495 stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream);
1496 BT_ASSERT_DBG(stream_state);
1497
1498 if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) ||
1499 (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) {
1500 stream_state->seen_clock_snapshot = true;
1501 }
1502
1503 break;
1504 }
1505 default:
1506 break;
1507 }
1508
1509 bt_object_put_ref_no_null_check(msg);
1510 msg = NULL;
1511 goto end;
1512
1513 push_msg:
1514 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
1515 msg = NULL;
1516
1517 end:
1518 BT_ASSERT_DBG(!msg || status != BT_FUNC_STATUS_OK);
1519 return status;
1520 }
1521
1522 static
1523 int find_message_ge_ns_from_origin(
1524 struct bt_message_iterator *iterator,
1525 int64_t ns_from_origin, GHashTable *stream_states)
1526 {
1527 int status = BT_FUNC_STATUS_OK;
1528 enum bt_message_iterator_state init_state =
1529 iterator->state;
1530 const struct bt_message *messages[MSG_BATCH_SIZE];
1531 uint64_t user_count = 0;
1532 uint64_t i;
1533 bool got_first = false;
1534
1535 BT_ASSERT_DBG(iterator);
1536 memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE);
1537
1538 /*
1539 * Make this iterator temporarily active (not seeking) to call
1540 * the "next" method.
1541 */
1542 set_msg_iterator_state(iterator,
1543 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
1544
1545 BT_ASSERT_DBG(iterator->methods.next);
1546
1547 while (!got_first) {
1548 /*
1549 * Call the user's "next" method to get the next
1550 * messages and status.
1551 */
1552 status = call_iterator_next_method(iterator,
1553 &messages[0], MSG_BATCH_SIZE, &user_count);
1554 BT_LOGD("User method returned: status=%s",
1555 bt_common_func_status_string(status));
1556 if (status < 0) {
1557 BT_LIB_LOGW_APPEND_CAUSE(
1558 "Component input port message iterator's \"next\" method failed: "
1559 "%![iter-]+i, status=%s",
1560 iterator, bt_common_func_status_string(status));
1561 }
1562
1563 /*
1564 * The user's "next" method must not do any action which
1565 * would change the iterator's state.
1566 */
1567 BT_ASSERT_DBG(iterator->state ==
1568 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
1569
1570 switch (status) {
1571 case BT_FUNC_STATUS_OK:
1572 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1573 "count-lteq-capacity",
1574 user_count <= MSG_BATCH_SIZE,
1575 "Invalid returned message count: greater than "
1576 "batch size: count=%" PRIu64 ", batch-size=%u",
1577 user_count, MSG_BATCH_SIZE);
1578 break;
1579 case BT_FUNC_STATUS_AGAIN:
1580 case BT_FUNC_STATUS_ERROR:
1581 case BT_FUNC_STATUS_MEMORY_ERROR:
1582 case BT_FUNC_STATUS_END:
1583 goto end;
1584 default:
1585 bt_common_abort();
1586 }
1587
1588 for (i = 0; i < user_count; i++) {
1589 if (got_first) {
1590 g_queue_push_tail(iterator->auto_seek.msgs,
1591 (void *) messages[i]);
1592 messages[i] = NULL;
1593 continue;
1594 }
1595
1596 status = auto_seek_handle_message(iterator,
1597 ns_from_origin, messages[i], &got_first,
1598 stream_states);
1599 if (status == BT_FUNC_STATUS_OK) {
1600 /* Message was either pushed or moved */
1601 messages[i] = NULL;
1602 } else {
1603 goto end;
1604 }
1605 }
1606 }
1607
1608 end:
1609 for (i = 0; i < user_count; i++) {
1610 if (messages[i]) {
1611 bt_object_put_ref_no_null_check(messages[i]);
1612 }
1613 }
1614
1615 set_msg_iterator_state(iterator, init_state);
1616 return status;
1617 }
1618
1619 /*
1620 * This function is installed as the iterator's next callback after we have
1621 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1622 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1623 * next callback is put back.
1624 */
1625
1626 static
1627 enum bt_message_iterator_class_next_method_status post_auto_seek_next(
1628 struct bt_message_iterator *iterator,
1629 bt_message_array_const msgs, uint64_t capacity,
1630 uint64_t *count)
1631 {
1632 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
1633 *count = 0;
1634
1635 /*
1636 * Move auto-seek messages to the output array (which is this
1637 * iterator's base message array).
1638 */
1639 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
1640 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
1641 capacity--;
1642 (*count)++;
1643 }
1644
1645 BT_ASSERT(*count > 0);
1646
1647 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
1648 /* No more auto-seek messages, restore user's next callback. */
1649 BT_ASSERT(iterator->auto_seek.original_next_callback);
1650 iterator->methods.next = iterator->auto_seek.original_next_callback;
1651 iterator->auto_seek.original_next_callback = NULL;
1652 }
1653
1654 return BT_FUNC_STATUS_OK;
1655 }
1656
1657 static inline
1658 int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class,
1659 int64_t ns_from_origin, uint64_t *raw_value)
1660 {
1661
1662 int64_t cc_offset_s = clock_class->offset_seconds;
1663 uint64_t cc_offset_cycles = clock_class->offset_cycles;
1664 uint64_t cc_freq = clock_class->frequency;
1665
1666 return bt_common_clock_value_from_ns_from_origin(cc_offset_s,
1667 cc_offset_cycles, cc_freq, ns_from_origin, raw_value);
1668 }
1669
1670 static
1671 bool message_iterator_can_seek_ns_from_origin(
1672 struct bt_message_iterator *iterator,
1673 int64_t ns_from_origin)
1674 {
1675 enum bt_message_iterator_can_seek_ns_from_origin_status status;
1676 bt_bool can_seek;
1677
1678 status = bt_message_iterator_can_seek_ns_from_origin(
1679 iterator, ns_from_origin, &can_seek);
1680 if (status != BT_FUNC_STATUS_OK) {
1681 can_seek = BT_FALSE;
1682 }
1683
1684 return can_seek;
1685 }
1686
1687 #define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1688 "bt_message_iterator_class_seek_ns_from_origin_method"
1689
1690
1691 enum bt_message_iterator_seek_ns_from_origin_status
1692 bt_message_iterator_seek_ns_from_origin(
1693 struct bt_message_iterator *iterator,
1694 int64_t ns_from_origin)
1695 {
1696 int status;
1697 GHashTable *stream_states = NULL;
1698 bt_bool can_seek_by_itself;
1699
1700 BT_ASSERT_PRE_NO_ERROR();
1701 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1702 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1703 BT_ASSERT_PRE("graph-is-configured",
1704 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1705 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1706 "Graph is not configured: %!+g",
1707 bt_component_borrow_graph(iterator->upstream_component));
1708 /* The iterator must be able to seek ns from origin one way or another. */
1709 BT_ASSERT_PRE("can-seek-ns-from-origin",
1710 message_iterator_can_seek_ns_from_origin(iterator, ns_from_origin),
1711 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1712 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
1713 set_msg_iterator_state(iterator,
1714 BT_MESSAGE_ITERATOR_STATE_SEEKING);
1715
1716 /*
1717 * We are seeking, reset our expectations about how the following
1718 * messages should look like.
1719 */
1720 reset_iterator_expectations(iterator);
1721
1722 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
1723 if (iterator->methods.can_seek_ns_from_origin) {
1724 bt_message_iterator_class_can_seek_ns_from_origin_method_status
1725 can_seek_status;
1726
1727 can_seek_status =
1728 iterator->methods.can_seek_ns_from_origin(
1729 iterator, ns_from_origin, &can_seek_by_itself);
1730 if (can_seek_status != BT_FUNC_STATUS_OK) {
1731 status = can_seek_status;
1732 goto end;
1733 }
1734 } else {
1735 can_seek_by_itself = false;
1736 }
1737
1738 if (can_seek_by_itself) {
1739 /* The iterator knows how to seek to a particular time, let it handle this. */
1740 BT_ASSERT(iterator->methods.seek_ns_from_origin);
1741 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1742 "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin);
1743 status = iterator->methods.seek_ns_from_origin(iterator,
1744 ns_from_origin);
1745 BT_LOGD("User method returned: status=%s",
1746 bt_common_func_status_string(status));
1747 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME, "valid-status",
1748 status == BT_FUNC_STATUS_OK ||
1749 status == BT_FUNC_STATUS_ERROR ||
1750 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1751 status == BT_FUNC_STATUS_AGAIN,
1752 "Unexpected status: %![iter-]+i, status=%s",
1753 iterator, bt_common_func_status_string(status));
1754 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1755 SEEK_NS_FROM_ORIGIN_METHOD_NAME, status);
1756 if (status < 0) {
1757 BT_LIB_LOGW_APPEND_CAUSE(
1758 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1759 "%![iter-]+i, status=%s",
1760 iterator, bt_common_func_status_string(status));
1761 }
1762 } else {
1763 /*
1764 * The iterator doesn't know how to seek by itself to a
1765 * particular time. We will seek to the beginning and fast
1766 * forward to the right place.
1767 */
1768 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status;
1769 bt_bool can_seek_beginning;
1770
1771 can_seek_status = iterator->methods.can_seek_beginning(iterator,
1772 &can_seek_beginning);
1773 BT_ASSERT(can_seek_status == BT_FUNC_STATUS_OK);
1774 BT_ASSERT(can_seek_beginning);
1775 BT_ASSERT(iterator->methods.seek_beginning);
1776 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1777 iterator);
1778 status = iterator->methods.seek_beginning(iterator);
1779 BT_LOGD("User method returned: status=%s",
1780 bt_common_func_status_string(status));
1781 BT_ASSERT_POST(SEEK_BEGINNING_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 if (status < 0) {
1789 BT_LIB_LOGW_APPEND_CAUSE(
1790 "Component input port message iterator's \"seek beginning\" method failed: "
1791 "%![iter-]+i, status=%s",
1792 iterator, bt_common_func_status_string(status));
1793 }
1794
1795 switch (status) {
1796 case BT_FUNC_STATUS_OK:
1797 break;
1798 case BT_FUNC_STATUS_ERROR:
1799 case BT_FUNC_STATUS_MEMORY_ERROR:
1800 case BT_FUNC_STATUS_AGAIN:
1801 goto end;
1802 default:
1803 bt_common_abort();
1804 }
1805
1806 /*
1807 * Find the first message which has a default clock
1808 * snapshot greater than or equal to the requested
1809 * seeking time, and move the received messages from
1810 * this point in the batch to this iterator's auto-seek
1811 * message queue.
1812 */
1813 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
1814 bt_object_put_ref_no_null_check(
1815 g_queue_pop_tail(iterator->auto_seek.msgs));
1816 }
1817
1818 stream_states = create_auto_seek_stream_states();
1819 if (!stream_states) {
1820 BT_LIB_LOGE_APPEND_CAUSE(
1821 "Failed to allocate one GHashTable.");
1822 status = BT_FUNC_STATUS_MEMORY_ERROR;
1823 goto end;
1824 }
1825
1826 status = find_message_ge_ns_from_origin(iterator,
1827 ns_from_origin, stream_states);
1828 switch (status) {
1829 case BT_FUNC_STATUS_OK:
1830 case BT_FUNC_STATUS_END:
1831 {
1832 GHashTableIter iter;
1833 gpointer key, value;
1834
1835 /*
1836 * If some streams exist at the seek time, prepend the
1837 * required messages to put those streams in the right
1838 * state.
1839 */
1840 g_hash_table_iter_init(&iter, stream_states);
1841 while (g_hash_table_iter_next (&iter, &key, &value)) {
1842 const bt_stream *stream = key;
1843 struct auto_seek_stream_state *stream_state =
1844 (struct auto_seek_stream_state *) value;
1845 bt_message *msg;
1846 const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const(
1847 bt_stream_borrow_class_const(stream));
1848 /* Initialize to silence maybe-uninitialized warning. */
1849 uint64_t raw_value = 0;
1850
1851 /*
1852 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1853 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1854 *
1855 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1856 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1857 * seen a message with a clock snapshot in our seeking, then we are sure that the
1858 * seek time is not below the clock range, and we know the stream was active at that
1859 * time (and that we cut it short).
1860 */
1861 if (stream_state->seen_clock_snapshot) {
1862 if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) {
1863 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K",
1864 ns_from_origin, clock_class);
1865 status = BT_FUNC_STATUS_ERROR;
1866 goto end;
1867 }
1868 }
1869
1870 switch (stream_state->state) {
1871 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN:
1872 BT_ASSERT(stream_state->packet);
1873 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet);
1874
1875 if (stream->class->packets_have_beginning_default_clock_snapshot) {
1876 /*
1877 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1878 * message. If "packet beginning" packets have clock snapshots, then we must have
1879 * seen a clock snapshot.
1880 */
1881 BT_ASSERT(stream_state->seen_clock_snapshot);
1882
1883 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
1884 (bt_self_message_iterator *) iterator, stream_state->packet, raw_value);
1885 } else {
1886 msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator,
1887 stream_state->packet);
1888 }
1889
1890 if (!msg) {
1891 status = BT_FUNC_STATUS_MEMORY_ERROR;
1892 goto end;
1893 }
1894
1895 g_queue_push_head(iterator->auto_seek.msgs, msg);
1896 msg = NULL;
1897 /* fall-thru */
1898
1899 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN:
1900 msg = bt_message_stream_beginning_create(
1901 (bt_self_message_iterator *) iterator, stream);
1902 if (!msg) {
1903 status = BT_FUNC_STATUS_MEMORY_ERROR;
1904 goto end;
1905 }
1906
1907 if (stream_state->seen_clock_snapshot) {
1908 bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value);
1909 }
1910
1911 g_queue_push_head(iterator->auto_seek.msgs, msg);
1912 msg = NULL;
1913 break;
1914 }
1915 }
1916
1917 /*
1918 * If there are messages in the auto-seek
1919 * message queue, replace the user's "next"
1920 * method with a custom, temporary "next" method
1921 * which returns them.
1922 */
1923 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
1924 BT_ASSERT(!iterator->auto_seek.original_next_callback);
1925 iterator->auto_seek.original_next_callback = iterator->methods.next;
1926
1927 iterator->methods.next =
1928 (bt_message_iterator_next_method)
1929 post_auto_seek_next;
1930 }
1931
1932 /*
1933 * `BT_FUNC_STATUS_END` becomes
1934 * `BT_FUNC_STATUS_OK`: the next
1935 * time this iterator's "next" method is called,
1936 * it will return
1937 * `BT_FUNC_STATUS_END`.
1938 */
1939 status = BT_FUNC_STATUS_OK;
1940 break;
1941 }
1942 case BT_FUNC_STATUS_ERROR:
1943 case BT_FUNC_STATUS_MEMORY_ERROR:
1944 case BT_FUNC_STATUS_AGAIN:
1945 goto end;
1946 default:
1947 bt_common_abort();
1948 }
1949 }
1950
1951 /*
1952 * The following messages returned by the next method (including
1953 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1954 */
1955 iterator->last_ns_from_origin = ns_from_origin;
1956
1957 end:
1958 if (stream_states) {
1959 destroy_auto_seek_stream_states(stream_states);
1960 stream_states = NULL;
1961 }
1962
1963 set_iterator_state_after_seeking(iterator, status);
1964 return status;
1965 }
1966
1967 bt_bool bt_self_message_iterator_is_interrupted(
1968 const struct bt_self_message_iterator *self_msg_iter)
1969 {
1970 const struct bt_message_iterator *iterator =
1971 (const void *) self_msg_iter;
1972
1973 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1974 return (bt_bool) bt_graph_is_interrupted(iterator->graph);
1975 }
1976
1977 void bt_message_iterator_get_ref(
1978 const struct bt_message_iterator *iterator)
1979 {
1980 bt_object_get_ref(iterator);
1981 }
1982
1983 void bt_message_iterator_put_ref(
1984 const struct bt_message_iterator *iterator)
1985 {
1986 bt_object_put_ref(iterator);
1987 }
This page took 0.114358 seconds and 3 git commands to generate.