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