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