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