lib: remove BT_ASSERT_COND_SUPPORTED check in graph/graph.h
[babeltrace.git] / src / lib / graph / iterator.c
CommitLineData
47e5a032 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
47e5a032 5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
47e5a032
JG
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/MSG-ITER"
c2d9d9cf 9#include "lib/logging.h"
5af447e5 10
578e048b 11#include "compat/compiler.h"
e74dbb33 12#include "compat/glib.h"
578e048b
MJ
13#include "lib/trace-ir/clock-class.h"
14#include "lib/trace-ir/clock-snapshot.h"
3fadfbc0 15#include <babeltrace2/trace-ir/field.h>
43c59509 16#include <babeltrace2/trace-ir/event.h>
578e048b 17#include "lib/trace-ir/event.h"
43c59509 18#include <babeltrace2/trace-ir/packet.h>
578e048b
MJ
19#include "lib/trace-ir/packet.h"
20#include "lib/trace-ir/stream.h"
43c59509
PP
21#include <babeltrace2/trace-ir/clock-class.h>
22#include <babeltrace2/trace-ir/stream-class.h>
23#include <babeltrace2/trace-ir/stream.h>
24#include <babeltrace2/graph/connection.h>
25#include <babeltrace2/graph/component.h>
26#include <babeltrace2/graph/message.h>
27#include <babeltrace2/graph/self-component.h>
28#include <babeltrace2/graph/port.h>
3fadfbc0 29#include <babeltrace2/graph/graph.h>
43c59509 30#include <babeltrace2/graph/message-iterator.h>
3fadfbc0 31#include <babeltrace2/types.h>
578e048b 32#include "common/assert.h"
d98421f2 33#include "lib/assert-cond.h"
fa054faf 34#include <stdint.h>
2ec84d26 35#include <inttypes.h>
c4f23e30 36#include <stdbool.h>
0fbb9a9f 37#include <stdlib.h>
3230ee6b 38
578e048b 39#include "component-class.h"
578e048b 40#include "component.h"
578e048b
MJ
41#include "connection.h"
42#include "graph.h"
5155b005 43#include "iterator.h"
a3f0c7db 44#include "message-iterator-class.h"
578e048b
MJ
45#include "message/discarded-items.h"
46#include "message/event.h"
578e048b
MJ
47#include "message/message.h"
48#include "message/message-iterator-inactivity.h"
49#include "message/stream.h"
50#include "message/packet.h"
d24d5663 51#include "lib/func-status.h"
578e048b 52
d4393e08
PP
53/*
54 * TODO: Use graph's state (number of active iterators, etc.) and
55 * possibly system specifications to make a better guess than this.
56 */
d6e69534 57#define MSG_BATCH_SIZE 15
d4393e08 58
7474e7d3 59#define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
1778c2a4
PP
60 BT_ASSERT_PRE("has-state-to-seek", \
61 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \
d5b13b9b 62 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \
9a2c8b8e
PP
63 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
64 (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
1778c2a4 65 "Message iterator is in the wrong state: %!+i", (_iter))
47e5a032 66
9340eff9
SM
67#ifdef BT_DEV_MODE
68struct per_stream_state
69{
70 bt_packet *cur_packet;
0121936a
SM
71
72 /* Bit mask of expected message types. */
73 guint expected_msg_types;
9340eff9
SM
74};
75#endif
76
77static void
78clear_per_stream_state (struct bt_message_iterator *iterator)
79{
80#ifdef BT_DEV_MODE
81 g_hash_table_remove_all(iterator->per_stream_state);
82#else
83 BT_USE_EXPR(iterator);
84#endif
85}
86
d0fea130 87static inline
9a2c8b8e
PP
88void set_msg_iterator_state(struct bt_message_iterator *iterator,
89 enum bt_message_iterator_state state)
d0fea130 90{
98b15851 91 BT_ASSERT_DBG(iterator);
7474e7d3 92 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
9a2c8b8e 93 bt_message_iterator_state_string(state));
d0fea130
PP
94 iterator->state = state;
95}
96
47e5a032 97static
9a2c8b8e 98void bt_message_iterator_destroy(struct bt_object *obj)
47e5a032 99{
9a2c8b8e 100 struct bt_message_iterator *iterator;
8738a040 101
f6ccaed9 102 BT_ASSERT(obj);
d3eb6e8f 103
bd14d768 104 /*
d6e69534 105 * The message iterator's reference count is 0 if we're
bd14d768
PP
106 * here. Increment it to avoid a double-destroy (possibly
107 * infinitely recursive). This could happen for example if the
d6e69534 108 * message iterator's finalization function does
d94d92ac
PP
109 * bt_object_get_ref() (or anything that causes
110 * bt_object_get_ref() to be called) on itself (ref. count goes
111 * from 0 to 1), and then bt_object_put_ref(): the reference
112 * count would go from 1 to 0 again and this function would be
113 * called again.
bd14d768 114 */
3fea54f6 115 obj->ref_count++;
07245ac2 116 iterator = (void *) obj;
3f7d4d90 117 BT_LIB_LOGI("Destroying self component input port message iterator object: "
d94d92ac 118 "%!+i", iterator);
9a2c8b8e 119 bt_message_iterator_try_finalize(iterator);
d3eb6e8f 120
29e191fc
SM
121 if (iterator->clock_expectation.type ==
122 CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID) {
123 BT_CLOCK_CLASS_PUT_REF_AND_RESET(
124 iterator->clock_expectation.clock_class);
125 }
126
bd14d768
PP
127 if (iterator->connection) {
128 /*
129 * Remove ourself from the originating connection so
130 * that it does not try to finalize a dangling pointer
131 * later.
132 */
133 bt_connection_remove_iterator(iterator->connection, iterator);
d94d92ac 134 iterator->connection = NULL;
bd14d768
PP
135 }
136
da9c4c52
SM
137 if (iterator->auto_seek.msgs) {
138 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
6871026b 139 bt_object_put_ref_no_null_check(
da9c4c52 140 g_queue_pop_tail(iterator->auto_seek.msgs));
7474e7d3
PP
141 }
142
da9c4c52
SM
143 g_queue_free(iterator->auto_seek.msgs);
144 iterator->auto_seek.msgs = NULL;
7474e7d3
PP
145 }
146
ca02df0a
PP
147 if (iterator->upstream_msg_iters) {
148 /*
149 * At this point the message iterator is finalized, so
150 * it's detached from any upstream message iterator.
151 */
152 BT_ASSERT(iterator->upstream_msg_iters->len == 0);
153 g_ptr_array_free(iterator->upstream_msg_iters, TRUE);
154 iterator->upstream_msg_iters = NULL;
155 }
156
6c373cc9
PP
157 if (iterator->msgs) {
158 g_ptr_array_free(iterator->msgs, TRUE);
159 iterator->msgs = NULL;
160 }
161
9340eff9
SM
162#ifdef BT_DEV_MODE
163 g_hash_table_destroy(iterator->per_stream_state);
164#endif
165
6c373cc9 166 g_free(iterator);
47e5a032
JG
167}
168
9a2c8b8e
PP
169void bt_message_iterator_try_finalize(
170 struct bt_message_iterator *iterator)
bd14d768 171{
ca02df0a 172 uint64_t i;
fca28f75 173 bool call_user_finalize = true;
bd14d768 174
f6ccaed9 175 BT_ASSERT(iterator);
bd14d768
PP
176
177 switch (iterator->state) {
9a2c8b8e 178 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
fca28f75
SM
179 /*
180 * If this function is called while the iterator is in the
181 * NON_INITIALIZED state, it means the user initialization
182 * method has either not been called, or has failed. We
183 * therefore don't want to call the user finalization method.
184 * However, the initialization method might have created some
185 * upstream message iterators before failing, so we want to
186 * execute the rest of this function, which unlinks the related
187 * iterators.
188 */
189 call_user_finalize = false;
190 break;
9a2c8b8e 191 case BT_MESSAGE_ITERATOR_STATE_FINALIZED:
bd14d768 192 /* Already finalized */
d6e69534 193 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
d94d92ac 194 "%!+i", iterator);
d0fea130 195 goto end;
9a2c8b8e 196 case BT_MESSAGE_ITERATOR_STATE_FINALIZING:
870631a2 197 /* Finalizing */
d0fea130
PP
198 BT_LIB_LOGF("Message iterator is already being finalized: "
199 "%!+i", iterator);
498e7994 200 bt_common_abort();
bd14d768
PP
201 default:
202 break;
203 }
204
d6e69534 205 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
9a2c8b8e
PP
206 set_msg_iterator_state(iterator,
207 BT_MESSAGE_ITERATOR_STATE_FINALIZING);
f6ccaed9 208 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
209
210 /* Call user-defined destroy method */
fca28f75 211 if (call_user_finalize) {
a3f0c7db 212 typedef void (*method_t)(void *);
41a3efcd 213 method_t method;
fca28f75
SM
214 struct bt_component_class *comp_class =
215 iterator->upstream_component->class;
41a3efcd 216 struct bt_component_class_with_iterator_class *class_with_iter_class;
fca28f75 217
41a3efcd
SM
218 BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class));
219 class_with_iter_class = container_of(comp_class,
220 struct bt_component_class_with_iterator_class, parent);
221 method = (method_t) class_with_iter_class->msg_iter_cls->methods.finalize;
bd14d768 222
fca28f75
SM
223 if (method) {
224 const bt_error *saved_error;
42a63165 225
fca28f75 226 saved_error = bt_current_thread_take_error();
42a63165 227
fca28f75
SM
228 BT_LIB_LOGD("Calling user's finalization method: %!+i",
229 iterator);
230 method(iterator);
4fb18ad2 231 BT_ASSERT_POST_NO_ERROR("bt_message_iterator_class_finalize_method");
42a63165 232
fca28f75
SM
233 if (saved_error) {
234 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
235 }
42a63165 236 }
bd14d768
PP
237 }
238
ca02df0a
PP
239 /* Detach upstream message iterators */
240 for (i = 0; i < iterator->upstream_msg_iters->len; i++) {
9a2c8b8e 241 struct bt_message_iterator *upstream_msg_iter =
ca02df0a
PP
242 iterator->upstream_msg_iters->pdata[i];
243
244 upstream_msg_iter->downstream_msg_iter = NULL;
245 }
246
247 g_ptr_array_set_size(iterator->upstream_msg_iters, 0);
248
249 /* Detach downstream message iterator */
250 if (iterator->downstream_msg_iter) {
251 gboolean existed;
252
253 BT_ASSERT(iterator->downstream_msg_iter->upstream_msg_iters);
254 existed = g_ptr_array_remove_fast(
255 iterator->downstream_msg_iter->upstream_msg_iters,
256 iterator);
257 BT_ASSERT(existed);
258 }
259
bd14d768
PP
260 iterator->upstream_component = NULL;
261 iterator->upstream_port = NULL;
9a2c8b8e
PP
262 set_msg_iterator_state(iterator,
263 BT_MESSAGE_ITERATOR_STATE_FINALIZED);
d6e69534 264 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
d0fea130
PP
265
266end:
267 return;
bd14d768
PP
268}
269
9a2c8b8e
PP
270void bt_message_iterator_set_connection(
271 struct bt_message_iterator *iterator,
bd14d768
PP
272 struct bt_connection *connection)
273{
f6ccaed9 274 BT_ASSERT(iterator);
bd14d768 275 iterator->connection = connection;
3f7d4d90 276 BT_LIB_LOGI("Set message iterator's connection: "
d94d92ac 277 "%![iter-]+i, %![conn-]+x", iterator, connection);
bd14d768
PP
278}
279
7474e7d3 280static
f2fb1b32 281enum bt_message_iterator_can_seek_beginning_status can_seek_ns_from_origin_true(
ecd7492f
MJ
282 struct bt_message_iterator *iterator __attribute__((unused)),
283 int64_t ns_from_origin __attribute__((unused)),
284 bt_bool *can_seek)
7474e7d3 285{
f2fb1b32
SM
286 *can_seek = BT_TRUE;
287
288 return BT_FUNC_STATUS_OK;
7474e7d3
PP
289}
290
291static
f2fb1b32 292enum bt_message_iterator_can_seek_beginning_status can_seek_beginning_true(
ecd7492f 293 struct bt_message_iterator *iterator __attribute__((unused)),
f2fb1b32 294 bt_bool *can_seek)
7474e7d3 295{
f2fb1b32
SM
296 *can_seek = BT_TRUE;
297
298 return BT_FUNC_STATUS_OK;
7474e7d3
PP
299}
300
d94d92ac 301static
e803df70 302int create_self_component_input_port_message_iterator(
ca02df0a 303 struct bt_self_message_iterator *self_downstream_msg_iter,
e803df70 304 struct bt_self_component_port_input *self_port,
1778c2a4
PP
305 struct bt_message_iterator **message_iterator,
306 const char *api_func)
47e5a032 307{
a3f0c7db 308 bt_message_iterator_class_initialize_method init_method = NULL;
9a2c8b8e 309 struct bt_message_iterator *iterator =
ca02df0a 310 NULL;
9a2c8b8e 311 struct bt_message_iterator *downstream_msg_iter =
ca02df0a
PP
312 (void *) self_downstream_msg_iter;
313 struct bt_port *port = (void *) self_port;
314 struct bt_port *upstream_port;
315 struct bt_component *comp;
316 struct bt_component *upstream_comp;
317 struct bt_component_class *upstream_comp_cls;
41a3efcd 318 struct bt_component_class_with_iterator_class *upstream_comp_cls_with_iter_cls;
e803df70 319 int status;
47e5a032 320
1778c2a4
PP
321 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func, "message-iterator-output",
322 message_iterator, "Created message iterator (output)");
323 BT_ASSERT_PRE_NON_NULL_FROM_FUNC(api_func, "input-port", port,
324 "Input port");
ca02df0a 325 comp = bt_port_borrow_component_inline(port);
1778c2a4
PP
326 BT_ASSERT_PRE_FROM_FUNC(api_func, "input-port-is-connected",
327 bt_port_is_connected(port),
ca02df0a 328 "Input port is not connected: %![port-]+p", port);
1778c2a4
PP
329 BT_ASSERT_PRE_FROM_FUNC(api_func, "input-port-has-component",
330 comp, "Input port is not part of a component: %![port-]+p",
ca02df0a 331 port);
ca02df0a
PP
332 BT_ASSERT(port->connection);
333 upstream_port = port->connection->upstream_port;
f6ccaed9 334 BT_ASSERT(upstream_port);
ca02df0a
PP
335 upstream_comp = bt_port_borrow_component_inline(upstream_port);
336 BT_ASSERT(upstream_comp);
1778c2a4 337 BT_ASSERT_PRE_FROM_FUNC(api_func, "graph-is-configured",
9b4f9b42
PP
338 bt_component_borrow_graph(upstream_comp)->config_state ==
339 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED ||
340 bt_component_borrow_graph(upstream_comp)->config_state ==
341 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED,
ca02df0a
PP
342 "Graph is not configured: %!+g",
343 bt_component_borrow_graph(upstream_comp));
344 upstream_comp_cls = upstream_comp->class;
345 BT_ASSERT(upstream_comp->class->type ==
d94d92ac 346 BT_COMPONENT_CLASS_TYPE_SOURCE ||
ca02df0a 347 upstream_comp->class->type ==
d94d92ac 348 BT_COMPONENT_CLASS_TYPE_FILTER);
ca02df0a
PP
349 BT_LIB_LOGI("Creating message iterator on self component input port: "
350 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
d94d92ac 351 iterator = g_new0(
9a2c8b8e 352 struct bt_message_iterator, 1);
47e5a032 353 if (!iterator) {
870631a2
PP
354 BT_LIB_LOGE_APPEND_CAUSE(
355 "Failed to allocate one self component input port "
d6e69534 356 "message iterator.");
e803df70 357 status = BT_FUNC_STATUS_MEMORY_ERROR;
870631a2 358 goto error;
47e5a032
JG
359 }
360
6c373cc9 361 bt_object_init_shared(&iterator->base,
9a2c8b8e 362 bt_message_iterator_destroy);
6c373cc9
PP
363 iterator->msgs = g_ptr_array_new();
364 if (!iterator->msgs) {
365 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
e803df70 366 status = BT_FUNC_STATUS_MEMORY_ERROR;
870631a2 367 goto error;
d4393e08 368 }
3230ee6b 369
6c373cc9 370 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
54b135a0 371 iterator->last_ns_from_origin = INT64_MIN;
9340eff9
SM
372
373#ifdef BT_DEV_MODE
374 /* The per-stream state is only used for dev assertions right now. */
375 iterator->per_stream_state = g_hash_table_new_full(
376 g_direct_hash,
377 g_direct_equal,
378 NULL,
379 g_free);
380#endif
381
da9c4c52
SM
382 iterator->auto_seek.msgs = g_queue_new();
383 if (!iterator->auto_seek.msgs) {
870631a2 384 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
e803df70 385 status = BT_FUNC_STATUS_MEMORY_ERROR;
ca02df0a
PP
386 goto error;
387 }
388
389 iterator->upstream_msg_iters = g_ptr_array_new();
390 if (!iterator->upstream_msg_iters) {
391 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
e803df70 392 status = BT_FUNC_STATUS_MEMORY_ERROR;
ca02df0a 393 goto error;
3230ee6b
PP
394 }
395
bd14d768
PP
396 iterator->upstream_component = upstream_comp;
397 iterator->upstream_port = upstream_port;
d94d92ac 398 iterator->connection = iterator->upstream_port->connection;
5c563278 399 iterator->graph = bt_component_borrow_graph(upstream_comp);
9a2c8b8e
PP
400 set_msg_iterator_state(iterator,
401 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
7474e7d3 402
41a3efcd
SM
403 /* Copy methods from the message iterator class to the message iterator. */
404 BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls));
405 upstream_comp_cls_with_iter_cls = container_of(upstream_comp_cls,
406 struct bt_component_class_with_iterator_class, parent);
407
408 iterator->methods.next =
9a2c8b8e 409 (bt_message_iterator_next_method)
41a3efcd
SM
410 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.next;
411 iterator->methods.seek_ns_from_origin =
9a2c8b8e 412 (bt_message_iterator_seek_ns_from_origin_method)
41a3efcd
SM
413 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_ns_from_origin;
414 iterator->methods.seek_beginning =
9a2c8b8e 415 (bt_message_iterator_seek_beginning_method)
41a3efcd
SM
416 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_beginning;
417 iterator->methods.can_seek_ns_from_origin =
9a2c8b8e 418 (bt_message_iterator_can_seek_ns_from_origin_method)
41a3efcd
SM
419 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
420 iterator->methods.can_seek_beginning =
9a2c8b8e 421 (bt_message_iterator_can_seek_beginning_method)
41a3efcd 422 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_beginning;
7474e7d3
PP
423
424 if (iterator->methods.seek_ns_from_origin &&
425 !iterator->methods.can_seek_ns_from_origin) {
426 iterator->methods.can_seek_ns_from_origin =
9a2c8b8e 427 (bt_message_iterator_can_seek_ns_from_origin_method)
7474e7d3
PP
428 can_seek_ns_from_origin_true;
429 }
430
431 if (iterator->methods.seek_beginning &&
432 !iterator->methods.can_seek_beginning) {
433 iterator->methods.can_seek_beginning =
9a2c8b8e 434 (bt_message_iterator_can_seek_beginning_method)
7474e7d3
PP
435 can_seek_beginning_true;
436 }
437
41a3efcd
SM
438 /* Call iterator's init method. */
439 init_method = upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.initialize;
d94d92ac
PP
440
441 if (init_method) {
a3f0c7db 442 enum bt_message_iterator_class_initialize_method_status iter_status;
d94d92ac
PP
443
444 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
a3f0c7db
SM
445 iter_status = init_method(
446 (struct bt_self_message_iterator *) iterator,
447 &iterator->config,
a3f0c7db 448 (struct bt_self_component_port_output *) upstream_port);
d94d92ac 449 BT_LOGD("User method returned: status=%s",
d24d5663 450 bt_common_func_status_string(iter_status));
1778c2a4
PP
451 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
452 "bt_message_iterator_class_initialize_method",
453 iter_status);
d24d5663 454 if (iter_status != BT_FUNC_STATUS_OK) {
870631a2
PP
455 BT_LIB_LOGW_APPEND_CAUSE(
456 "Component input port message iterator initialization method failed: "
457 "%![iter-]+i, status=%s",
458 iterator,
459 bt_common_func_status_string(iter_status));
e803df70 460 status = iter_status;
870631a2 461 goto error;
d94d92ac 462 }
8d8b141d
SM
463
464 iterator->config.frozen = true;
d94d92ac
PP
465 }
466
ca02df0a
PP
467 if (downstream_msg_iter) {
468 /* Set this message iterator's downstream message iterator */
469 iterator->downstream_msg_iter = downstream_msg_iter;
470
471 /*
472 * Add this message iterator to the downstream message
473 * iterator's array of upstream message iterators.
474 */
475 g_ptr_array_add(downstream_msg_iter->upstream_msg_iters,
476 iterator);
477 }
478
9a2c8b8e
PP
479 set_msg_iterator_state(iterator,
480 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
d94d92ac 481 g_ptr_array_add(port->connection->iterators, iterator);
3f7d4d90 482 BT_LIB_LOGI("Created message iterator on self component input port: "
d94d92ac
PP
483 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
484 upstream_port, upstream_comp, iterator);
e803df70
SM
485
486 *message_iterator = iterator;
487 status = BT_FUNC_STATUS_OK;
870631a2
PP
488 goto end;
489
490error:
491 BT_OBJECT_PUT_REF_AND_RESET(iterator);
d94d92ac
PP
492
493end:
e803df70 494 return status;
ea8d3e58
JG
495}
496
1353b066 497BT_EXPORT
9a2c8b8e
PP
498bt_message_iterator_create_from_message_iterator_status
499bt_message_iterator_create_from_message_iterator(
ca02df0a 500 struct bt_self_message_iterator *self_msg_iter,
e803df70 501 struct bt_self_component_port_input *input_port,
9a2c8b8e 502 struct bt_message_iterator **message_iterator)
ca02df0a 503{
17f3083a 504 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 505 BT_ASSERT_PRE_MSG_ITER_NON_NULL(self_msg_iter);
ca02df0a 506 return create_self_component_input_port_message_iterator(self_msg_iter,
1778c2a4 507 input_port, message_iterator, __func__);
ca02df0a
PP
508}
509
1353b066 510BT_EXPORT
9a2c8b8e
PP
511bt_message_iterator_create_from_sink_component_status
512bt_message_iterator_create_from_sink_component(
ca02df0a 513 struct bt_self_component_sink *self_comp,
e803df70 514 struct bt_self_component_port_input *input_port,
9a2c8b8e 515 struct bt_message_iterator **message_iterator)
ca02df0a 516{
17f3083a 517 BT_ASSERT_PRE_NO_ERROR();
1778c2a4 518 BT_ASSERT_PRE_NON_NULL("sink-component", self_comp, "Sink component");
ca02df0a 519 return create_self_component_input_port_message_iterator(NULL,
1778c2a4 520 input_port, message_iterator, __func__);
ca02df0a
PP
521}
522
1353b066 523BT_EXPORT
d6e69534
PP
524void *bt_self_message_iterator_get_data(
525 const struct bt_self_message_iterator *self_iterator)
ea8d3e58 526{
9a2c8b8e 527 struct bt_message_iterator *iterator =
d94d92ac 528 (void *) self_iterator;
ea8d3e58 529
d5b13b9b 530 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
d94d92ac 531 return iterator->user_data;
8738a040 532}
413bc2c4 533
1353b066 534BT_EXPORT
d6e69534
PP
535void bt_self_message_iterator_set_data(
536 struct bt_self_message_iterator *self_iterator, void *data)
5c563278 537{
9a2c8b8e 538 struct bt_message_iterator *iterator =
d94d92ac 539 (void *) self_iterator;
5c563278 540
d5b13b9b 541 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(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
1353b066 547BT_EXPORT
8d8b141d
SM
548void bt_self_message_iterator_configuration_set_can_seek_forward(
549 bt_self_message_iterator_configuration *config,
550 bt_bool can_seek_forward)
551{
1778c2a4
PP
552 BT_ASSERT_PRE_NON_NULL("message-iterator-configuration", config,
553 "Message iterator configuration");
554 BT_ASSERT_PRE_DEV_HOT("message-iterator-configuration", config,
555 "Message iterator configuration", "");
8d8b141d
SM
556
557 config->can_seek_forward = can_seek_forward;
558}
559
54b135a0
SM
560/*
561 * Validate that the default clock snapshot in `msg` doesn't make us go back in
562 * time.
563 */
564
d98421f2 565BT_ASSERT_COND_DEV_FUNC
54b135a0
SM
566static
567bool clock_snapshots_are_monotonic_one(
9a2c8b8e 568 struct bt_message_iterator *iterator,
54b135a0
SM
569 const bt_message *msg)
570{
571 const struct bt_clock_snapshot *clock_snapshot = NULL;
572 bt_message_type message_type = bt_message_get_type(msg);
573 int64_t ns_from_origin;
d24d5663 574 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status;
54b135a0
SM
575
576 /*
577 * The default is true: if we can't figure out the clock snapshot
578 * (or there is none), assume it is fine.
579 */
580 bool result = true;
581
582 switch (message_type) {
583 case BT_MESSAGE_TYPE_EVENT:
584 {
585 struct bt_message_event *event_msg = (struct bt_message_event *) msg;
586 clock_snapshot = event_msg->default_cs;
587 break;
588 }
589 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
590 {
591 struct bt_message_message_iterator_inactivity *inactivity_msg =
592 (struct bt_message_message_iterator_inactivity *) msg;
60d02328 593 clock_snapshot = inactivity_msg->cs;
54b135a0
SM
594 break;
595 }
596 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
597 case BT_MESSAGE_TYPE_PACKET_END:
598 {
599 struct bt_message_packet *packet_msg = (struct bt_message_packet *) msg;
600 clock_snapshot = packet_msg->default_cs;
601 break;
602 }
188edac1
SM
603 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
604 case BT_MESSAGE_TYPE_STREAM_END:
54b135a0 605 {
188edac1
SM
606 struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
607 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
608 goto end;
54b135a0 609 }
188edac1
SM
610
611 clock_snapshot = stream_msg->default_cs;
54b135a0
SM
612 break;
613 }
54b135a0
SM
614 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
615 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
616 {
617 struct bt_message_discarded_items *discarded_msg =
618 (struct bt_message_discarded_items *) msg;
619
620 clock_snapshot = discarded_msg->default_begin_cs;
621 break;
622 }
623 }
624
625 if (!clock_snapshot) {
626 goto end;
627 }
628
d747e85f
SM
629 clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(
630 clock_snapshot, &ns_from_origin);
d24d5663 631 if (clock_snapshot_status != BT_FUNC_STATUS_OK) {
d747e85f
SM
632 /*
633 * bt_clock_snapshot_get_ns_from_origin can return
634 * OVERFLOW_ERROR. We don't really want to report an error to
635 * our caller, so just clear it.
636 */
637 bt_current_thread_clear_error();
54b135a0
SM
638 goto end;
639 }
640
641 result = ns_from_origin >= iterator->last_ns_from_origin;
642 iterator->last_ns_from_origin = ns_from_origin;
643end:
644 return result;
645}
646
d98421f2 647BT_ASSERT_COND_DEV_FUNC
54b135a0
SM
648static
649bool clock_snapshots_are_monotonic(
9a2c8b8e 650 struct bt_message_iterator *iterator,
54b135a0
SM
651 bt_message_array_const msgs, uint64_t msg_count)
652{
653 uint64_t i;
654 bool result;
655
656 for (i = 0; i < msg_count; i++) {
657 if (!clock_snapshots_are_monotonic_one(iterator, msgs[i])) {
658 result = false;
659 goto end;
660 }
661 }
662
663 result = true;
664
665end:
666 return result;
667}
668
669/*
670 * When a new stream begins, verify that the clock class tied to this
671 * stream is compatible with what we've seen before.
672 */
673
d98421f2 674BT_ASSERT_COND_DEV_FUNC
54b135a0 675static
9a2c8b8e 676bool clock_classes_are_compatible_one(struct bt_message_iterator *iterator,
54b135a0
SM
677 const struct bt_message *msg)
678{
679 enum bt_message_type message_type = bt_message_get_type(msg);
680 bool result;
681
682 if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) {
683 const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
684 const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class;
685 bt_uuid clock_class_uuid = NULL;
686
687 if (clock_class) {
688 clock_class_uuid = bt_clock_class_get_uuid(clock_class);
689 }
690
691 switch (iterator->clock_expectation.type) {
692 case CLOCK_EXPECTATION_UNSET:
693 /*
694 * This is the first time we see a message with a clock
695 * snapshot: record the properties of that clock, against
696 * which we'll compare the clock properties of the following
697 * messages.
698 */
699
700 if (!clock_class) {
701 iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE;
702 } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
703 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX;
704 } else if (clock_class_uuid) {
705 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID;
6162e6b7 706 bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
54b135a0
SM
707 } else {
708 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID;
29e191fc
SM
709 iterator->clock_expectation.clock_class = clock_class;
710 bt_clock_class_get_ref(iterator->clock_expectation.clock_class);
54b135a0
SM
711 }
712 break;
713
714 case CLOCK_EXPECTATION_NONE:
715 if (clock_class) {
d98421f2 716 BT_ASSERT_COND_DEV_MSG(
bdb288b3 717 "Expecting no clock class, got one: %![cc-]+K",
54b135a0
SM
718 clock_class);
719 result = false;
720 goto end;
721 }
722
723 break;
724
725 case CLOCK_EXPECTATION_ORIGIN_UNIX:
726 if (!clock_class) {
d98421f2 727 BT_ASSERT_COND_DEV_MSG(
bdb288b3 728 "Expecting a clock class, got none.");
54b135a0
SM
729 result = false;
730 goto end;
731 }
732
733 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
d98421f2 734 BT_ASSERT_COND_DEV_MSG(
bdb288b3 735 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
54b135a0
SM
736 clock_class);
737 result = false;
738 goto end;
739 }
740 break;
741
742 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID:
743 if (!clock_class) {
d98421f2 744 BT_ASSERT_COND_DEV_MSG(
bdb288b3 745 "Expecting a clock class, got none.");
54b135a0
SM
746 result = false;
747 goto end;
748 }
749
750 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
d98421f2 751 BT_ASSERT_COND_DEV_MSG(
bdb288b3 752 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
54b135a0
SM
753 clock_class);
754 result = false;
755 goto end;
756 }
757
758 if (!clock_class_uuid) {
d98421f2 759 BT_ASSERT_COND_DEV_MSG(
bdb288b3 760 "Expecting a clock class with UUID: %![cc-]+K",
54b135a0
SM
761 clock_class);
762 result = false;
763 goto end;
764 }
765
766 if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
d98421f2 767 BT_ASSERT_COND_DEV_MSG(
bdb288b3 768 "Expecting a clock class with UUID, got one "
54b135a0
SM
769 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
770 clock_class, iterator->clock_expectation.uuid);
771 result = false;
772 goto end;
773 }
774 break;
775
776 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID:
777 if (!clock_class) {
d98421f2 778 BT_ASSERT_COND_DEV_MSG(
29e191fc
SM
779 "Expecting clock class %![cc-]+K, got none.",
780 iterator->clock_expectation.clock_class);
54b135a0
SM
781 result = false;
782 goto end;
783 }
784
29e191fc 785 if (clock_class != iterator->clock_expectation.clock_class) {
d98421f2 786 BT_ASSERT_COND_DEV_MSG(
29e191fc
SM
787 "Expecting clock class %![cc-]+K, got %![cc-]+K.",
788 iterator->clock_expectation.clock_class,
54b135a0
SM
789 clock_class);
790 result = false;
791 goto end;
792 }
793
54b135a0
SM
794 break;
795 }
796 }
797
798 result = true;
799
800end:
801 return result;
802}
803
d98421f2 804BT_ASSERT_COND_DEV_FUNC
54b135a0
SM
805static
806bool clock_classes_are_compatible(
9a2c8b8e 807 struct bt_message_iterator *iterator,
54b135a0
SM
808 bt_message_array_const msgs, uint64_t msg_count)
809{
810 uint64_t i;
811 bool result;
812
813 for (i = 0; i < msg_count; i++) {
814 if (!clock_classes_are_compatible_one(iterator, msgs[i])) {
815 result = false;
816 goto end;
817 }
818 }
819
820 result = true;
821
822end:
823 return result;
824}
825
9340eff9
SM
826#ifdef BT_DEV_MODE
827static
828const bt_stream *get_stream_from_msg(const struct bt_message *msg)
829{
830 struct bt_stream *stream;
831
832 switch (msg->type) {
833 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
834 case BT_MESSAGE_TYPE_STREAM_END:
835 {
836 struct bt_message_stream *msg_stream =
837 (struct bt_message_stream *) msg;
838 stream = msg_stream->stream;
839 break;
840 }
841 case BT_MESSAGE_TYPE_EVENT:
842 {
843 struct bt_message_event *msg_event =
844 (struct bt_message_event *) msg;
845 stream = msg_event->event->stream;
846 break;
847 }
848 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
849 case BT_MESSAGE_TYPE_PACKET_END:
850 {
851 struct bt_message_packet *msg_packet =
852 (struct bt_message_packet *) msg;
853 stream = msg_packet->packet->stream;
854 break;
855 }
856 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
857 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
858 {
859 struct bt_message_discarded_items *msg_discarded =
860 (struct bt_message_discarded_items *) msg;
861 stream = msg_discarded->stream;
862 break;
863 }
864 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
865 stream = NULL;
866 break;
867 default:
868 bt_common_abort();
869 }
870
871 return stream;
872}
873
0121936a
SM
874static
875GString *message_types_to_string(guint msg_types)
876{
877 GString *str = g_string_new("");
878
879 for (int msg_type = 1; msg_type <= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY;
880 msg_type <<= 1) {
881 if (msg_type & msg_types) {
882 if (str->len > 0) {
883 g_string_append_c(str, '|');
884 }
885
886 g_string_append(str,
887 bt_common_message_type_string(msg_type));
888 }
889 }
890
891 return str;
892}
893
894static
895void update_expected_msg_type(const struct bt_stream *stream,
896 struct per_stream_state *state,
897 const struct bt_message *msg)
898{
899 switch (msg->type) {
900 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
901 state->expected_msg_types = BT_MESSAGE_TYPE_STREAM_END;
902
903 if (stream->class->supports_packets) {
904 state->expected_msg_types |=
905 BT_MESSAGE_TYPE_PACKET_BEGINNING;
906
907 if (stream->class->supports_discarded_packets) {
908 state->expected_msg_types |=
909 BT_MESSAGE_TYPE_DISCARDED_PACKETS;
910 }
911 } else {
912 state->expected_msg_types |= BT_MESSAGE_TYPE_EVENT;
913 }
914
915 if (stream->class->supports_discarded_events) {
916 state->expected_msg_types |=
917 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
918 }
919
920 break;
921 case BT_MESSAGE_TYPE_STREAM_END:
922 state->expected_msg_types = 0;
923 break;
924 case BT_MESSAGE_TYPE_EVENT:
925 {
926 state->expected_msg_types = BT_MESSAGE_TYPE_EVENT;
927
928 if (stream->class->supports_packets) {
929 state->expected_msg_types |= BT_MESSAGE_TYPE_PACKET_END;
930 } else {
931 state->expected_msg_types |= BT_MESSAGE_TYPE_STREAM_END;
932 }
933
934 if (stream->class->supports_discarded_events) {
935 state->expected_msg_types |=
936 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
937 }
938
939 break;
940 }
941 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
942 {
943 state->expected_msg_types = BT_MESSAGE_TYPE_EVENT |
944 BT_MESSAGE_TYPE_PACKET_END;
945
946 if (stream->class->supports_discarded_events) {
947 state->expected_msg_types |=
948 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
949 }
950
951 break;
952 }
953 case BT_MESSAGE_TYPE_PACKET_END:
954 {
955 state->expected_msg_types = BT_MESSAGE_TYPE_PACKET_BEGINNING |
956 BT_MESSAGE_TYPE_STREAM_END;
957
958 if (stream->class->supports_discarded_events) {
959 state->expected_msg_types |=
960 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
961 }
962
963 if (stream->class->supports_discarded_packets) {
964 state->expected_msg_types |=
965 BT_MESSAGE_TYPE_DISCARDED_PACKETS;
966 }
967
968 break;
969 }
970 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
971 state->expected_msg_types = BT_MESSAGE_TYPE_DISCARDED_EVENTS;
972
973 if (state->cur_packet) {
974 state->expected_msg_types |= BT_MESSAGE_TYPE_EVENT |
975 BT_MESSAGE_TYPE_PACKET_END;
976 } else {
977 state->expected_msg_types |= BT_MESSAGE_TYPE_STREAM_END;
978
979 if (stream->class->supports_packets) {
980 state->expected_msg_types |=
981 BT_MESSAGE_TYPE_PACKET_BEGINNING;
982
983 if (stream->class->supports_discarded_packets) {
984 state->expected_msg_types |=
985 BT_MESSAGE_TYPE_DISCARDED_PACKETS;
986 }
987 } else {
988 state->expected_msg_types |=
989 BT_MESSAGE_TYPE_EVENT;
990 }
991 }
992
993 break;
994 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
995 state->expected_msg_types = BT_MESSAGE_TYPE_DISCARDED_PACKETS |
996 BT_MESSAGE_TYPE_PACKET_BEGINNING |
997 BT_MESSAGE_TYPE_STREAM_END;
998
999 if (stream->class->supports_discarded_events) {
1000 state->expected_msg_types |=
1001 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
1002 }
1003 break;
1004 default:
1005 /*
1006 * Other message types are not associated to a stream, so we
1007 * should not get them here.
1008 */
1009 bt_common_abort();
1010 }
1011}
1012
9340eff9
SM
1013static
1014struct per_stream_state *get_per_stream_state(
1015 struct bt_message_iterator *iterator,
1016 const struct bt_stream *stream)
1017{
1018 struct per_stream_state *state = g_hash_table_lookup(
1019 iterator->per_stream_state, stream);
1020
1021 if (!state) {
1022 state = g_new0(struct per_stream_state, 1);
0121936a 1023 state->expected_msg_types = BT_MESSAGE_TYPE_STREAM_BEGINNING;
9340eff9
SM
1024 g_hash_table_insert(iterator->per_stream_state,
1025 (gpointer) stream, state);
1026 }
1027
1028 return state;
1029}
1030#endif
1031
1032#define NEXT_METHOD_NAME "bt_message_iterator_class_next_method"
1033
1034#ifdef BT_DEV_MODE
0121936a
SM
1035static
1036void assert_post_dev_expected_sequence(struct bt_message_iterator *iterator,
1037 const struct bt_message *msg)
1038{
1039 const bt_stream *stream = get_stream_from_msg(msg);
1040 struct per_stream_state *state;
1041
1042 if (!stream) {
1043 goto end;
1044 }
1045
1046 state = get_per_stream_state(iterator, stream);
1047
1048 /*
1049 * We don't free the return value of message_types_to_string(), but
1050 * that's because we know the program is going to abort anyway, and
1051 * we don't want to call it if the assertion holds.
1052 */
1053 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1054 "message-type-is-expected",
1055 msg->type & state->expected_msg_types,
1056 "Unexpected message type: %![stream-]s, %![iterator-]i, "
1057 "%![message-]n, expected-msg-types=%s",
1058 stream, iterator, msg,
1059 message_types_to_string(state->expected_msg_types)->str);
1060
1061 update_expected_msg_type(stream, state, msg);
1062
1063end:
1064 return;
1065}
1066
9340eff9
SM
1067static
1068void assert_post_dev_expected_packet(struct bt_message_iterator *iterator,
1069 const struct bt_message *msg)
1070{
1071 const bt_stream *stream = get_stream_from_msg(msg);
1072 struct per_stream_state *state;
1073 const bt_packet *actual_packet = NULL;
1074 const bt_packet *expected_packet = NULL;
1075
1076 if (!stream) {
1077 goto end;
1078 }
1079
1080 state = get_per_stream_state(iterator, stream);
1081
1082 switch (msg->type) {
1083 case BT_MESSAGE_TYPE_EVENT:
1084 {
1085 const struct bt_message_event *msg_event =
1086 (const struct bt_message_event *) msg;
1087
1088 actual_packet = msg_event->event->packet;
1089 expected_packet = state->cur_packet;
1090 break;
1091 }
1092 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1093 {
1094 const struct bt_message_packet *msg_packet =
1095 (const struct bt_message_packet *) msg;
1096
1097 BT_ASSERT(!state->cur_packet);
1098 state->cur_packet = msg_packet->packet;
1099 break;
1100 }
1101 case BT_MESSAGE_TYPE_PACKET_END:
1102 {
1103 const struct bt_message_packet *msg_packet =
1104 (const struct bt_message_packet *) msg;
1105
1106 actual_packet = msg_packet->packet;
1107 expected_packet = state->cur_packet;
1108 BT_ASSERT(state->cur_packet);
1109 state->cur_packet = NULL;
1110 break;
1111 }
1112 default:
1113 break;
1114 }
1115
1116 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1117 "message-packet-is-expected",
1118 actual_packet == expected_packet,
1119 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
1120 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
1121 stream, iterator, msg, actual_packet, expected_packet);
1122
1123end:
1124 return;
1125}
1126
1127static
1128void assert_post_dev_next(
1129 struct bt_message_iterator *iterator,
1130 bt_message_iterator_class_next_method_status status,
1131 bt_message_array_const msgs, uint64_t msg_count)
1132{
1133 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1134 uint64_t i;
1135
1136 for (i = 0; i < msg_count; i++) {
0121936a 1137 assert_post_dev_expected_sequence(iterator, msgs[i]);
9340eff9
SM
1138 assert_post_dev_expected_packet(iterator, msgs[i]);
1139 }
0121936a
SM
1140 } else if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END) {
1141 GHashTableIter iter;
1142
1143 gpointer stream_v, stream_state_v;
1144
1145 g_hash_table_iter_init(&iter, iterator->per_stream_state);
1146 while (g_hash_table_iter_next(&iter, &stream_v,
1147 &stream_state_v)) {
1148 struct bt_stream *stream = stream_v;
1149 struct per_stream_state *stream_state = stream_state_v;
1150
1151 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1152 "stream-is-ended",
1153 stream_state->expected_msg_types == 0,
1154 "Stream is not ended: %![stream-]s, "
1155 "%![iterator-]i, expected-msg-types=%s",
1156 stream, iterator,
1157 message_types_to_string(
1158 stream_state->expected_msg_types)->str);
1159 }
1160
9340eff9
SM
1161 }
1162}
1163#endif
1164
54b135a0
SM
1165/*
1166 * Call the `next` method of the iterator. Do some validation on the returned
1167 * messages.
1168 */
1169
1170static
a3f0c7db 1171enum bt_message_iterator_class_next_method_status
d24d5663 1172call_iterator_next_method(
9a2c8b8e 1173 struct bt_message_iterator *iterator,
54b135a0
SM
1174 bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
1175{
a3f0c7db 1176 enum bt_message_iterator_class_next_method_status status;
54b135a0 1177
98b15851 1178 BT_ASSERT_DBG(iterator->methods.next);
54b135a0 1179 BT_LOGD_STR("Calling user's \"next\" method.");
54b135a0 1180 status = iterator->methods.next(iterator, msgs, capacity, user_count);
f6f301d7 1181 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
d24d5663 1182 bt_common_func_status_string(status), *user_count);
54b135a0 1183
d24d5663 1184 if (status == BT_FUNC_STATUS_OK) {
1778c2a4
PP
1185 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1186 "message-clock-classes-are-compatible",
1187 clock_classes_are_compatible(iterator, msgs,
1188 *user_count),
54b135a0 1189 "Clocks are not compatible");
1778c2a4
PP
1190 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1191 "message-clock-snapshots-are-monotonic",
1192 clock_snapshots_are_monotonic(iterator, msgs,
1193 *user_count),
54b135a0
SM
1194 "Clock snapshots are not monotonic");
1195 }
1196
9340eff9
SM
1197#ifdef BT_DEV_MODE
1198 assert_post_dev_next(iterator, status, msgs, *user_count);
1199#endif
1200
1778c2a4
PP
1201 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME,
1202 status);
6ecdcca3 1203
54b135a0
SM
1204 return status;
1205}
1206
1353b066 1207BT_EXPORT
d24d5663 1208enum bt_message_iterator_next_status
9a2c8b8e
PP
1209bt_message_iterator_next(
1210 struct bt_message_iterator *iterator,
d6e69534 1211 bt_message_array_const *msgs, uint64_t *user_count)
3230ee6b 1212{
d24d5663 1213 enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
d94d92ac 1214
17f3083a 1215 BT_ASSERT_PRE_DEV_NO_ERROR();
d5b13b9b 1216 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
1778c2a4
PP
1217 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs,
1218 "Message array (output)");
1219 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count,
1220 "Message count (output)");
1221 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1222 iterator->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE,
d6e69534 1223 "Message iterator's \"next\" called, but "
7474e7d3 1224 "message iterator is in the wrong state: %!+i", iterator);
98b15851
PP
1225 BT_ASSERT_DBG(iterator->upstream_component);
1226 BT_ASSERT_DBG(iterator->upstream_component->class);
1778c2a4 1227 BT_ASSERT_PRE_DEV("graph-is-configured",
5badd463
PP
1228 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1229 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
4725a201
PP
1230 "Graph is not configured: %!+g",
1231 bt_component_borrow_graph(iterator->upstream_component));
d94d92ac 1232 BT_LIB_LOGD("Getting next self component input port "
3f7d4d90
PP
1233 "message iterator's messages: %!+i, batch-size=%u",
1234 iterator, MSG_BATCH_SIZE);
d3eb6e8f 1235
3230ee6b 1236 /*
d6e69534 1237 * Call the user's "next" method to get the next messages
fa054faf 1238 * and status.
3230ee6b 1239 */
3f7d4d90 1240 *user_count = 0;
d24d5663 1241 status = (int) call_iterator_next_method(iterator,
6c373cc9 1242 (void *) iterator->msgs->pdata, MSG_BATCH_SIZE,
7474e7d3 1243 user_count);
870631a2
PP
1244 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
1245 bt_common_func_status_string(status), *user_count);
d4393e08 1246 if (status < 0) {
870631a2
PP
1247 BT_LIB_LOGW_APPEND_CAUSE(
1248 "Component input port message iterator's \"next\" method failed: "
1249 "%![iter-]+i, status=%s",
1250 iterator, bt_common_func_status_string(status));
f42867e2
PP
1251 goto end;
1252 }
3230ee6b 1253
d0fea130
PP
1254 /*
1255 * There is no way that this iterator could have been finalized
1256 * during its "next" method, as the only way to do this is to
1257 * put the last iterator's reference, and this can only be done
1258 * by its downstream owner.
7474e7d3
PP
1259 *
1260 * For the same reason, there is no way that this iterator could
e7401568 1261 * have sought (cannot seek a self message iterator).
d0fea130 1262 */
98b15851 1263 BT_ASSERT_DBG(iterator->state ==
9a2c8b8e 1264 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
8cf27cc5 1265
d4393e08 1266 switch (status) {
d24d5663 1267 case BT_FUNC_STATUS_OK:
1778c2a4
PP
1268 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME, "count-lteq-capacity",
1269 *user_count <= MSG_BATCH_SIZE,
7474e7d3
PP
1270 "Invalid returned message count: greater than "
1271 "batch size: count=%" PRIu64 ", batch-size=%u",
1272 *user_count, MSG_BATCH_SIZE);
6c373cc9 1273 *msgs = (void *) iterator->msgs->pdata;
d4393e08 1274 break;
d24d5663 1275 case BT_FUNC_STATUS_AGAIN:
d4393e08 1276 goto end;
d24d5663 1277 case BT_FUNC_STATUS_END:
9a2c8b8e
PP
1278 set_msg_iterator_state(iterator,
1279 BT_MESSAGE_ITERATOR_STATE_ENDED);
f42867e2 1280 goto end;
f42867e2
PP
1281 default:
1282 /* Unknown non-error status */
498e7994 1283 bt_common_abort();
41a2b7ae
PP
1284 }
1285
1286end:
3230ee6b
PP
1287 return status;
1288}
1289
1353b066 1290BT_EXPORT
7474e7d3 1291struct bt_component *
9a2c8b8e
PP
1292bt_message_iterator_borrow_component(
1293 struct bt_message_iterator *iterator)
d94d92ac 1294{
d5b13b9b 1295 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
d94d92ac
PP
1296 return iterator->upstream_component;
1297}
1298
1353b066 1299BT_EXPORT
d6e69534
PP
1300struct bt_self_component *bt_self_message_iterator_borrow_component(
1301 struct bt_self_message_iterator *self_iterator)
413bc2c4 1302{
9a2c8b8e 1303 struct bt_message_iterator *iterator =
d94d92ac 1304 (void *) self_iterator;
90157d89 1305
d5b13b9b 1306 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
d94d92ac 1307 return (void *) iterator->upstream_component;
413bc2c4
JG
1308}
1309
1353b066 1310BT_EXPORT
50e763f6 1311struct bt_self_component_port_output *bt_self_message_iterator_borrow_port(
d6e69534 1312 struct bt_self_message_iterator *self_iterator)
91457551 1313{
9a2c8b8e 1314 struct bt_message_iterator *iterator =
d94d92ac
PP
1315 (void *) self_iterator;
1316
d5b13b9b 1317 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
d94d92ac 1318 return (void *) iterator->upstream_port;
91457551 1319}
8ed535b5 1320
1778c2a4
PP
1321#define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1322 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1323
1353b066 1324BT_EXPORT
f2fb1b32 1325enum bt_message_iterator_can_seek_ns_from_origin_status
9a2c8b8e
PP
1326bt_message_iterator_can_seek_ns_from_origin(
1327 struct bt_message_iterator *iterator,
f2fb1b32 1328 int64_t ns_from_origin, bt_bool *can_seek)
7474e7d3 1329{
f2fb1b32 1330 enum bt_message_iterator_can_seek_ns_from_origin_status status;
7474e7d3 1331
17f3083a 1332 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
1333 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1334 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek);
7474e7d3 1335 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1778c2a4 1336 BT_ASSERT_PRE("graph-is-configured",
5badd463
PP
1337 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1338 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1339 "Graph is not configured: %!+g",
1340 bt_component_borrow_graph(iterator->upstream_component));
1341
1342 if (iterator->methods.can_seek_ns_from_origin) {
f2fb1b32
SM
1343 /*
1344 * Initialize to an invalid value, so we can post-assert that
1345 * the method returned a valid value.
1346 */
1347 *can_seek = -1;
1348
c0e46a7c
SM
1349 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1350 iterator);
1351
f2fb1b32
SM
1352 status = (int) iterator->methods.can_seek_ns_from_origin(iterator,
1353 ns_from_origin, can_seek);
1354
1778c2a4
PP
1355 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1356 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME, status);
6ecdcca3 1357
c0e46a7c
SM
1358 if (status != BT_FUNC_STATUS_OK) {
1359 BT_LIB_LOGW_APPEND_CAUSE(
1360 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1361 "%![iter-]+i, status=%s",
1362 iterator, bt_common_func_status_string(status));
1363 goto end;
1364 }
1365
1778c2a4
PP
1366 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME,
1367 "valid-return-value",
1368 *can_seek == BT_TRUE || *can_seek == BT_FALSE,
f2fb1b32
SM
1369 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1370 *can_seek, iterator);
1371
c0e46a7c
SM
1372 BT_LIB_LOGD(
1373 "User's \"can seek nanoseconds from origin\" returned successfully: "
1374 "%![iter-]+i, can-seek=%d",
1375 iterator, *can_seek);
1376
1377 if (*can_seek) {
1378 goto end;
1379 }
7474e7d3
PP
1380 }
1381
1382 /*
c0e46a7c
SM
1383 * Automatic seeking fall back: if we can seek to the beginning and the
1384 * iterator supports forward seeking then we can automatically seek to
1385 * any timestamp.
7474e7d3 1386 */
9a2c8b8e 1387 status = (int) bt_message_iterator_can_seek_beginning(
f2fb1b32 1388 iterator, can_seek);
c0e46a7c
SM
1389 if (status != BT_FUNC_STATUS_OK) {
1390 goto end;
1391 }
1392
1393 *can_seek = *can_seek && iterator->config.can_seek_forward;
7474e7d3
PP
1394
1395end:
f2fb1b32 1396 return status;
7474e7d3
PP
1397}
1398
1778c2a4
PP
1399#define CAN_SEEK_BEGINNING_METHOD_NAME \
1400 "bt_message_iterator_class_can_seek_beginning"
1401
1353b066 1402BT_EXPORT
f2fb1b32 1403enum bt_message_iterator_can_seek_beginning_status
9a2c8b8e
PP
1404bt_message_iterator_can_seek_beginning(
1405 struct bt_message_iterator *iterator,
f2fb1b32 1406 bt_bool *can_seek)
7474e7d3 1407{
f2fb1b32 1408 enum bt_message_iterator_can_seek_beginning_status status;
7474e7d3 1409
17f3083a 1410 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
1411 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1412 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek);
7474e7d3 1413 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1778c2a4 1414 BT_ASSERT_PRE("graph-is-configured",
5badd463
PP
1415 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1416 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1417 "Graph is not configured: %!+g",
1418 bt_component_borrow_graph(iterator->upstream_component));
1419
1420 if (iterator->methods.can_seek_beginning) {
f2fb1b32
SM
1421 /*
1422 * Initialize to an invalid value, so we can post-assert that
1423 * the method returned a valid value.
1424 */
1425 *can_seek = -1;
1426
1427 status = (int) iterator->methods.can_seek_beginning(iterator, can_seek);
1428
1778c2a4
PP
1429 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME,
1430 "valid-return-value",
f2fb1b32
SM
1431 status != BT_FUNC_STATUS_OK ||
1432 *can_seek == BT_TRUE ||
1433 *can_seek == BT_FALSE,
1434 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1435 *can_seek, iterator);
1778c2a4
PP
1436 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1437 CAN_SEEK_BEGINNING_METHOD_NAME, status);
f2fb1b32
SM
1438 } else {
1439 *can_seek = BT_FALSE;
1440 status = BT_FUNC_STATUS_OK;
7474e7d3
PP
1441 }
1442
f2fb1b32 1443 return status;
7474e7d3
PP
1444}
1445
1446static inline
003e713f 1447void set_iterator_state_after_seeking(
9a2c8b8e 1448 struct bt_message_iterator *iterator,
d24d5663 1449 int status)
7474e7d3 1450{
9a2c8b8e 1451 enum bt_message_iterator_state new_state = 0;
7474e7d3
PP
1452
1453 /* Set iterator's state depending on seeking status */
1454 switch (status) {
d24d5663 1455 case BT_FUNC_STATUS_OK:
9a2c8b8e 1456 new_state = BT_MESSAGE_ITERATOR_STATE_ACTIVE;
7474e7d3 1457 break;
d24d5663 1458 case BT_FUNC_STATUS_AGAIN:
9a2c8b8e 1459 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
7474e7d3 1460 break;
d24d5663
PP
1461 case BT_FUNC_STATUS_ERROR:
1462 case BT_FUNC_STATUS_MEMORY_ERROR:
9a2c8b8e 1463 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
7474e7d3 1464 break;
d24d5663 1465 case BT_FUNC_STATUS_END:
9a2c8b8e 1466 new_state = BT_MESSAGE_ITERATOR_STATE_ENDED;
7474e7d3
PP
1467 break;
1468 default:
498e7994 1469 bt_common_abort();
7474e7d3
PP
1470 }
1471
9a2c8b8e 1472 set_msg_iterator_state(iterator, new_state);
7474e7d3
PP
1473}
1474
54b135a0
SM
1475static
1476void reset_iterator_expectations(
9a2c8b8e 1477 struct bt_message_iterator *iterator)
54b135a0
SM
1478{
1479 iterator->last_ns_from_origin = INT64_MIN;
1480 iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET;
1481}
1482
f2fb1b32
SM
1483static
1484bool message_iterator_can_seek_beginning(
9a2c8b8e 1485 struct bt_message_iterator *iterator)
f2fb1b32
SM
1486{
1487 enum bt_message_iterator_can_seek_beginning_status status;
1488 bt_bool can_seek;
1489
9a2c8b8e 1490 status = bt_message_iterator_can_seek_beginning(
f2fb1b32
SM
1491 iterator, &can_seek);
1492 if (status != BT_FUNC_STATUS_OK) {
1493 can_seek = BT_FALSE;
1494 }
1495
1496 return can_seek;
1497}
1498
1778c2a4
PP
1499#define SEEK_BEGINNING_METHOD_NAME \
1500 "bt_message_iterator_class_seek_beginning_method"
1501
1353b066 1502BT_EXPORT
d24d5663 1503enum bt_message_iterator_seek_beginning_status
1778c2a4 1504bt_message_iterator_seek_beginning(struct bt_message_iterator *iterator)
7474e7d3 1505{
9275bef4 1506 int status;
7474e7d3 1507
17f3083a 1508 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 1509 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
7474e7d3 1510 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1778c2a4 1511 BT_ASSERT_PRE("graph-is-configured",
5badd463
PP
1512 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1513 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1514 "Graph is not configured: %!+g",
1515 bt_component_borrow_graph(iterator->upstream_component));
1778c2a4
PP
1516 BT_ASSERT_PRE("can-seek-beginning",
1517 message_iterator_can_seek_beginning(iterator),
7474e7d3 1518 "Message iterator cannot seek beginning: %!+i", iterator);
54b135a0
SM
1519
1520 /*
1521 * We are seeking, reset our expectations about how the following
1522 * messages should look like.
1523 */
1524 reset_iterator_expectations(iterator);
1525
7474e7d3 1526 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
9a2c8b8e
PP
1527 set_msg_iterator_state(iterator,
1528 BT_MESSAGE_ITERATOR_STATE_SEEKING);
7474e7d3
PP
1529 status = iterator->methods.seek_beginning(iterator);
1530 BT_LOGD("User method returned: status=%s",
d24d5663 1531 bt_common_func_status_string(status));
1778c2a4
PP
1532 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME, "valid-status",
1533 status == BT_FUNC_STATUS_OK ||
d24d5663
PP
1534 status == BT_FUNC_STATUS_ERROR ||
1535 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1536 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 1537 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 1538 iterator, bt_common_func_status_string(status));
1778c2a4
PP
1539 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME,
1540 status);
870631a2
PP
1541 if (status < 0) {
1542 BT_LIB_LOGW_APPEND_CAUSE(
1543 "Component input port message iterator's \"seek beginning\" method failed: "
1544 "%![iter-]+i, status=%s",
1545 iterator, bt_common_func_status_string(status));
1546 }
1547
9340eff9
SM
1548 clear_per_stream_state(iterator);
1549
7474e7d3
PP
1550 set_iterator_state_after_seeking(iterator, status);
1551 return status;
1552}
1553
1353b066 1554BT_EXPORT
8d8b141d 1555bt_bool
9a2c8b8e
PP
1556bt_message_iterator_can_seek_forward(
1557 bt_message_iterator *iterator)
8d8b141d 1558{
d5b13b9b 1559 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
8d8b141d
SM
1560
1561 return iterator->config.can_seek_forward;
1562}
1563
5b7b55be
SM
1564/*
1565 * Structure used to record the state of a given stream during the fast-forward
1566 * phase of an auto-seek.
1567 */
1568struct auto_seek_stream_state {
1569 /*
1570 * Value representing which step of this timeline we are at.
1571 *
1572 * time --->
188edac1 1573 * [SB] 1 [PB] 2 [PE] 1 [SE]
5b7b55be
SM
1574 *
1575 * At each point in the timeline, the messages we need to replicate are:
1576 *
1577 * 1: Stream beginning
188edac1 1578 * 2: Stream beginning, packet beginning
5b7b55be
SM
1579 *
1580 * Before "Stream beginning" and after "Stream end", we don't need to
1581 * replicate anything as the stream doesn't exist.
1582 */
1583 enum {
1584 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN,
5b7b55be
SM
1585 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN,
1586 } state;
1587
1588 /*
1589 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1590 * in. This is a weak reference, since the packet will always be
1591 * alive by the time we use it.
1592 */
1593 struct bt_packet *packet;
188edac1
SM
1594
1595 /* Have we see a message with a clock snapshot yet? */
1596 bool seen_clock_snapshot;
5b7b55be
SM
1597};
1598
1599static
1600struct auto_seek_stream_state *create_auto_seek_stream_state(void)
1601{
1602 return g_new0(struct auto_seek_stream_state, 1);
1603}
1604
1605static
1606void destroy_auto_seek_stream_state(void *ptr)
1607{
1608 g_free(ptr);
1609}
1610
1611static
1612GHashTable *create_auto_seek_stream_states(void)
1613{
1614 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
1615 destroy_auto_seek_stream_state);
1616}
1617
1618static
1619void destroy_auto_seek_stream_states(GHashTable *stream_states)
1620{
1621 g_hash_table_destroy(stream_states);
1622}
1623
1624/*
1625 * Handle one message while we are in the fast-forward phase of an auto-seek.
1626 *
1627 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1628 * `ns_from_origin`. In other words, if this is the first message after our
1629 * seek point.
1630 *
1631 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1632 * `struct auto_seek_stream_state` used to keep the state of each stream
1633 * during the fast-forward.
1634 */
1635
7474e7d3 1636static inline
d24d5663 1637int auto_seek_handle_message(
9a2c8b8e 1638 struct bt_message_iterator *iterator,
5b9e151d 1639 int64_t ns_from_origin, const struct bt_message *msg,
5b7b55be 1640 bool *got_first, GHashTable *stream_states)
7474e7d3 1641{
d24d5663 1642 int status = BT_FUNC_STATUS_OK;
5b9e151d 1643 int64_t msg_ns_from_origin;
7474e7d3 1644 const struct bt_clock_snapshot *clk_snapshot = NULL;
5b9e151d
PP
1645 int ret;
1646
98b15851
PP
1647 BT_ASSERT_DBG(msg);
1648 BT_ASSERT_DBG(got_first);
7474e7d3
PP
1649
1650 switch (msg->type) {
1651 case BT_MESSAGE_TYPE_EVENT:
1652 {
1653 const struct bt_message_event *event_msg =
1654 (const void *) msg;
1655
2c091c04 1656 clk_snapshot = event_msg->default_cs;
1778c2a4
PP
1657 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1658 "event-message-has-default-clock-snapshot",
1659 clk_snapshot,
c7072d5a
PP
1660 "Event message has no default clock snapshot: %!+n",
1661 event_msg);
7474e7d3
PP
1662 break;
1663 }
b9fd9cbb 1664 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
7474e7d3 1665 {
b9fd9cbb 1666 const struct bt_message_message_iterator_inactivity *inactivity_msg =
7474e7d3
PP
1667 (const void *) msg;
1668
60d02328 1669 clk_snapshot = inactivity_msg->cs;
98b15851 1670 BT_ASSERT_DBG(clk_snapshot);
7474e7d3
PP
1671 break;
1672 }
16663a5e
PP
1673 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1674 case BT_MESSAGE_TYPE_PACKET_END:
c7072d5a
PP
1675 {
1676 const struct bt_message_packet *packet_msg =
1677 (const void *) msg;
1678
ed3039e4
SM
1679 if (msg->type == BT_MESSAGE_TYPE_PACKET_BEGINNING
1680 && !packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1681 goto skip_msg;
1682 }
1683
1684 if (msg->type == BT_MESSAGE_TYPE_PACKET_END
1685 && !packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1686 goto skip_msg;
1687 }
1688
c7072d5a 1689 clk_snapshot = packet_msg->default_cs;
1778c2a4
PP
1690 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1691 "packet-message-has-default-clock-snapshot",
1692 clk_snapshot,
c7072d5a
PP
1693 "Packet message has no default clock snapshot: %!+n",
1694 packet_msg);
1695 break;
1696 }
16663a5e
PP
1697 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1698 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
7474e7d3 1699 {
5b9e151d
PP
1700 struct bt_message_discarded_items *msg_disc_items =
1701 (void *) msg;
1702
ed3039e4
SM
1703 if (msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS &&
1704 !msg_disc_items->stream->class->discarded_events_have_default_clock_snapshots) {
1705 goto skip_msg;
1706 }
1707
1708 if (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS &&
1709 !msg_disc_items->stream->class->discarded_packets_have_default_clock_snapshots) {
1710 goto skip_msg;
1711 }
1712
1778c2a4
PP
1713 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1714 "discarded-events-packets-message-has-default-clock-snapshot",
1715 msg_disc_items->default_begin_cs &&
1716 msg_disc_items->default_end_cs,
5b9e151d
PP
1717 "Discarded events/packets message has no default clock snapshots: %!+n",
1718 msg_disc_items);
1719 ret = bt_clock_snapshot_get_ns_from_origin(
1720 msg_disc_items->default_begin_cs,
1721 &msg_ns_from_origin);
1722 if (ret) {
d24d5663 1723 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1724 goto end;
1725 }
7474e7d3 1726
5b9e151d
PP
1727 if (msg_ns_from_origin >= ns_from_origin) {
1728 *got_first = true;
1729 goto push_msg;
1730 }
1731
1732 ret = bt_clock_snapshot_get_ns_from_origin(
1733 msg_disc_items->default_end_cs,
1734 &msg_ns_from_origin);
1735 if (ret) {
d24d5663 1736 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1737 goto end;
1738 }
1739
1740 if (msg_ns_from_origin >= ns_from_origin) {
1741 /*
1742 * The discarded items message's beginning time
1743 * is before the requested seeking time, but its
1744 * end time is after. Modify the message so as
1745 * to set its beginning time to the requested
1746 * seeking time, and make its item count unknown
1747 * as we don't know if items were really
1748 * discarded within the new time range.
1749 */
4af85094 1750 uint64_t new_begin_raw_value = 0;
5b9e151d
PP
1751
1752 ret = bt_clock_class_clock_value_from_ns_from_origin(
1753 msg_disc_items->default_end_cs->clock_class,
1754 ns_from_origin, &new_begin_raw_value);
1755 if (ret) {
d24d5663 1756 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1757 goto end;
1758 }
1759
1760 bt_clock_snapshot_set_raw_value(
1761 msg_disc_items->default_begin_cs,
1762 new_begin_raw_value);
1763 msg_disc_items->count.base.avail =
1764 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
1765
1766 /*
1767 * It is safe to push it because its beginning
1768 * time is exactly the requested seeking time.
1769 */
1770 goto push_msg;
1771 } else {
1772 goto skip_msg;
1773 }
7474e7d3 1774 }
188edac1
SM
1775 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1776 case BT_MESSAGE_TYPE_STREAM_END:
7474e7d3 1777 {
188edac1
SM
1778 struct bt_message_stream *stream_msg =
1779 (struct bt_message_stream *) msg;
7474e7d3 1780
188edac1
SM
1781 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1782 /* Ignore */
5b9e151d 1783 goto skip_msg;
16663a5e
PP
1784 }
1785
188edac1 1786 clk_snapshot = stream_msg->default_cs;
7474e7d3
PP
1787 break;
1788 }
1789 default:
498e7994 1790 bt_common_abort();
7474e7d3
PP
1791 }
1792
98b15851 1793 BT_ASSERT_DBG(clk_snapshot);
5b9e151d
PP
1794 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1795 &msg_ns_from_origin);
1796 if (ret) {
d24d5663 1797 status = BT_FUNC_STATUS_ERROR;
7474e7d3
PP
1798 goto end;
1799 }
1800
5b9e151d
PP
1801 if (msg_ns_from_origin >= ns_from_origin) {
1802 *got_first = true;
1803 goto push_msg;
1804 }
1805
1806skip_msg:
5b7b55be
SM
1807 /* This message won't be sent downstream. */
1808 switch (msg->type) {
1809 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1810 {
1811 const struct bt_message_stream *stream_msg = (const void *) msg;
1812 struct auto_seek_stream_state *stream_state;
5b7b55be
SM
1813
1814 /* Update stream's state: stream began. */
1815 stream_state = create_auto_seek_stream_state();
1816 if (!stream_state) {
d24d5663 1817 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1818 goto end;
1819 }
1820
1821 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
e74dbb33 1822
188edac1
SM
1823 if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1824 stream_state->seen_clock_snapshot = true;
1825 }
1826
98b15851 1827 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
e74dbb33 1828 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
5b7b55be
SM
1829 break;
1830 }
188edac1 1831 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
5b7b55be 1832 {
188edac1 1833 const struct bt_message_packet *packet_msg =
5b7b55be
SM
1834 (const void *) msg;
1835 struct auto_seek_stream_state *stream_state;
1836
188edac1
SM
1837 /* Update stream's state: packet began. */
1838 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
98b15851
PP
1839 BT_ASSERT_DBG(stream_state);
1840 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
188edac1 1841 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
98b15851 1842 BT_ASSERT_DBG(!stream_state->packet);
188edac1
SM
1843 stream_state->packet = packet_msg->packet;
1844
1845 if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1846 stream_state->seen_clock_snapshot = true;
1847 }
1848
5b7b55be
SM
1849 break;
1850 }
188edac1 1851 case BT_MESSAGE_TYPE_EVENT:
5b7b55be 1852 {
188edac1 1853 const struct bt_message_event *event_msg = (const void *) msg;
5b7b55be
SM
1854 struct auto_seek_stream_state *stream_state;
1855
188edac1 1856 stream_state = g_hash_table_lookup(stream_states,
03cc4222 1857 event_msg->event->stream);
98b15851 1858 BT_ASSERT_DBG(stream_state);
5b7b55be 1859
188edac1
SM
1860 // HELPME: are we sure that event messages have clock snapshots at this point?
1861 stream_state->seen_clock_snapshot = true;
1862
5b7b55be
SM
1863 break;
1864 }
1865 case BT_MESSAGE_TYPE_PACKET_END:
1866 {
1867 const struct bt_message_packet *packet_msg =
1868 (const void *) msg;
1869 struct auto_seek_stream_state *stream_state;
1870
1871 /* Update stream's state: packet ended. */
1872 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
98b15851
PP
1873 BT_ASSERT_DBG(stream_state);
1874 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
188edac1 1875 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
98b15851 1876 BT_ASSERT_DBG(stream_state->packet);
5b7b55be 1877 stream_state->packet = NULL;
5b7b55be 1878
188edac1
SM
1879 if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1880 stream_state->seen_clock_snapshot = true;
1881 }
5b7b55be 1882
5b7b55be
SM
1883 break;
1884 }
1885 case BT_MESSAGE_TYPE_STREAM_END:
1886 {
1887 const struct bt_message_stream *stream_msg = (const void *) msg;
1888 struct auto_seek_stream_state *stream_state;
1889
1890 stream_state = g_hash_table_lookup(stream_states, stream_msg->stream);
98b15851
PP
1891 BT_ASSERT_DBG(stream_state);
1892 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1893 BT_ASSERT_DBG(!stream_state->packet);
5b7b55be
SM
1894
1895 /* Update stream's state: this stream doesn't exist anymore. */
1896 g_hash_table_remove(stream_states, stream_msg->stream);
1897 break;
1898 }
188edac1
SM
1899 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1900 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1901 {
1902 const struct bt_message_discarded_items *discarded_msg =
1903 (const void *) msg;
1904 struct auto_seek_stream_state *stream_state;
1905
1906 stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream);
98b15851 1907 BT_ASSERT_DBG(stream_state);
188edac1
SM
1908
1909 if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) ||
1910 (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) {
1911 stream_state->seen_clock_snapshot = true;
1912 }
1913
1914 break;
1915 }
5b7b55be
SM
1916 default:
1917 break;
1918 }
1919
6871026b 1920 bt_object_put_ref_no_null_check(msg);
04c0cec6 1921 msg = NULL;
5b9e151d
PP
1922 goto end;
1923
1924push_msg:
da9c4c52 1925 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
5b9e151d 1926 msg = NULL;
7474e7d3
PP
1927
1928end:
98b15851 1929 BT_ASSERT_DBG(!msg || status != BT_FUNC_STATUS_OK);
5b9e151d 1930 return status;
7474e7d3
PP
1931}
1932
1933static
d24d5663 1934int find_message_ge_ns_from_origin(
9a2c8b8e 1935 struct bt_message_iterator *iterator,
5b7b55be 1936 int64_t ns_from_origin, GHashTable *stream_states)
7474e7d3 1937{
c70a96a6 1938 int status = BT_FUNC_STATUS_OK;
9a2c8b8e 1939 enum bt_message_iterator_state init_state =
7474e7d3
PP
1940 iterator->state;
1941 const struct bt_message *messages[MSG_BATCH_SIZE];
1942 uint64_t user_count = 0;
1943 uint64_t i;
5b9e151d 1944 bool got_first = false;
7474e7d3 1945
98b15851 1946 BT_ASSERT_DBG(iterator);
7474e7d3
PP
1947 memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE);
1948
1949 /*
1950 * Make this iterator temporarily active (not seeking) to call
1951 * the "next" method.
1952 */
9a2c8b8e
PP
1953 set_msg_iterator_state(iterator,
1954 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
7474e7d3 1955
98b15851 1956 BT_ASSERT_DBG(iterator->methods.next);
7474e7d3 1957
e0dade92 1958 while (!got_first) {
7474e7d3
PP
1959 /*
1960 * Call the user's "next" method to get the next
1961 * messages and status.
1962 */
54b135a0 1963 status = call_iterator_next_method(iterator,
7474e7d3 1964 &messages[0], MSG_BATCH_SIZE, &user_count);
870631a2
PP
1965 BT_LOGD("User method returned: status=%s",
1966 bt_common_func_status_string(status));
1967 if (status < 0) {
1968 BT_LIB_LOGW_APPEND_CAUSE(
1969 "Component input port message iterator's \"next\" method failed: "
1970 "%![iter-]+i, status=%s",
1971 iterator, bt_common_func_status_string(status));
1972 }
7474e7d3 1973
7474e7d3
PP
1974 /*
1975 * The user's "next" method must not do any action which
1976 * would change the iterator's state.
1977 */
98b15851 1978 BT_ASSERT_DBG(iterator->state ==
9a2c8b8e 1979 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
7474e7d3
PP
1980
1981 switch (status) {
d24d5663 1982 case BT_FUNC_STATUS_OK:
1778c2a4
PP
1983 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1984 "count-lteq-capacity",
1985 user_count <= MSG_BATCH_SIZE,
7474e7d3
PP
1986 "Invalid returned message count: greater than "
1987 "batch size: count=%" PRIu64 ", batch-size=%u",
1988 user_count, MSG_BATCH_SIZE);
1989 break;
d24d5663
PP
1990 case BT_FUNC_STATUS_AGAIN:
1991 case BT_FUNC_STATUS_ERROR:
1992 case BT_FUNC_STATUS_MEMORY_ERROR:
1993 case BT_FUNC_STATUS_END:
7474e7d3
PP
1994 goto end;
1995 default:
498e7994 1996 bt_common_abort();
7474e7d3
PP
1997 }
1998
7474e7d3 1999 for (i = 0; i < user_count; i++) {
5b9e151d 2000 if (got_first) {
da9c4c52 2001 g_queue_push_tail(iterator->auto_seek.msgs,
5b9e151d
PP
2002 (void *) messages[i]);
2003 messages[i] = NULL;
7474e7d3
PP
2004 continue;
2005 }
2006
5b9e151d 2007 status = auto_seek_handle_message(iterator,
5b7b55be
SM
2008 ns_from_origin, messages[i], &got_first,
2009 stream_states);
d24d5663 2010 if (status == BT_FUNC_STATUS_OK) {
e0dade92 2011 /* Message was either pushed or moved */
5b9e151d
PP
2012 messages[i] = NULL;
2013 } else {
7474e7d3
PP
2014 goto end;
2015 }
7474e7d3
PP
2016 }
2017 }
2018
2019end:
2020 for (i = 0; i < user_count; i++) {
2021 if (messages[i]) {
6871026b 2022 bt_object_put_ref_no_null_check(messages[i]);
7474e7d3
PP
2023 }
2024 }
2025
9a2c8b8e 2026 set_msg_iterator_state(iterator, init_state);
7474e7d3
PP
2027 return status;
2028}
2029
5b7b55be
SM
2030/*
2031 * This function is installed as the iterator's next callback after we have
e7401568 2032 * auto-sought (sought to the beginning and fast-forwarded) to send the
5b7b55be
SM
2033 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
2034 * next callback is put back.
2035 */
2036
7474e7d3 2037static
a3f0c7db 2038enum bt_message_iterator_class_next_method_status post_auto_seek_next(
9a2c8b8e 2039 struct bt_message_iterator *iterator,
7474e7d3
PP
2040 bt_message_array_const msgs, uint64_t capacity,
2041 uint64_t *count)
2042{
da9c4c52 2043 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
5b9e151d 2044 *count = 0;
7474e7d3
PP
2045
2046 /*
2047 * Move auto-seek messages to the output array (which is this
5b9e151d 2048 * iterator's base message array).
7474e7d3 2049 */
da9c4c52
SM
2050 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
2051 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
5b9e151d
PP
2052 capacity--;
2053 (*count)++;
7474e7d3 2054 }
7474e7d3 2055
5b9e151d
PP
2056 BT_ASSERT(*count > 0);
2057
da9c4c52 2058 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
572075a8
SM
2059 /* No more auto-seek messages, restore user's next callback. */
2060 BT_ASSERT(iterator->auto_seek.original_next_callback);
2061 iterator->methods.next = iterator->auto_seek.original_next_callback;
2062 iterator->auto_seek.original_next_callback = NULL;
7474e7d3
PP
2063 }
2064
d24d5663 2065 return BT_FUNC_STATUS_OK;
7474e7d3
PP
2066}
2067
5b7b55be
SM
2068static inline
2069int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class,
2070 int64_t ns_from_origin, uint64_t *raw_value)
2071{
2072
2073 int64_t cc_offset_s = clock_class->offset_seconds;
2074 uint64_t cc_offset_cycles = clock_class->offset_cycles;
2075 uint64_t cc_freq = clock_class->frequency;
2076
2077 return bt_common_clock_value_from_ns_from_origin(cc_offset_s,
2078 cc_offset_cycles, cc_freq, ns_from_origin, raw_value);
2079}
2080
f2fb1b32
SM
2081static
2082bool message_iterator_can_seek_ns_from_origin(
9a2c8b8e 2083 struct bt_message_iterator *iterator,
f2fb1b32
SM
2084 int64_t ns_from_origin)
2085{
2086 enum bt_message_iterator_can_seek_ns_from_origin_status status;
2087 bt_bool can_seek;
2088
9a2c8b8e 2089 status = bt_message_iterator_can_seek_ns_from_origin(
f2fb1b32
SM
2090 iterator, ns_from_origin, &can_seek);
2091 if (status != BT_FUNC_STATUS_OK) {
2092 can_seek = BT_FALSE;
2093 }
2094
2095 return can_seek;
2096}
5b7b55be 2097
1778c2a4
PP
2098#define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
2099 "bt_message_iterator_class_seek_ns_from_origin_method"
2100
2101
1353b066 2102BT_EXPORT
d24d5663 2103enum bt_message_iterator_seek_ns_from_origin_status
9a2c8b8e
PP
2104bt_message_iterator_seek_ns_from_origin(
2105 struct bt_message_iterator *iterator,
7474e7d3
PP
2106 int64_t ns_from_origin)
2107{
2108 int status;
5b7b55be 2109 GHashTable *stream_states = NULL;
c0e46a7c 2110 bt_bool can_seek_by_itself;
7474e7d3 2111
17f3083a 2112 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 2113 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
7474e7d3 2114 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1778c2a4 2115 BT_ASSERT_PRE("graph-is-configured",
5badd463
PP
2116 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
2117 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
2118 "Graph is not configured: %!+g",
2119 bt_component_borrow_graph(iterator->upstream_component));
c0e46a7c 2120 /* The iterator must be able to seek ns from origin one way or another. */
1778c2a4 2121 BT_ASSERT_PRE("can-seek-ns-from-origin",
f2fb1b32 2122 message_iterator_can_seek_ns_from_origin(iterator, ns_from_origin),
7474e7d3
PP
2123 "Message iterator cannot seek nanoseconds from origin: %!+i, "
2124 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
9a2c8b8e
PP
2125 set_msg_iterator_state(iterator,
2126 BT_MESSAGE_ITERATOR_STATE_SEEKING);
7474e7d3 2127
54b135a0
SM
2128 /*
2129 * We are seeking, reset our expectations about how the following
2130 * messages should look like.
2131 */
2132 reset_iterator_expectations(iterator);
2133
c0e46a7c
SM
2134 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
2135 if (iterator->methods.can_seek_ns_from_origin) {
a3f0c7db 2136 bt_message_iterator_class_can_seek_ns_from_origin_method_status
c0e46a7c
SM
2137 can_seek_status;
2138
2139 can_seek_status =
2140 iterator->methods.can_seek_ns_from_origin(
2141 iterator, ns_from_origin, &can_seek_by_itself);
2142 if (can_seek_status != BT_FUNC_STATUS_OK) {
2143 status = can_seek_status;
2144 goto end;
2145 }
2146 } else {
2147 can_seek_by_itself = false;
2148 }
2149
2150 if (can_seek_by_itself) {
5b7b55be 2151 /* The iterator knows how to seek to a particular time, let it handle this. */
2e1b5615 2152 BT_ASSERT(iterator->methods.seek_ns_from_origin);
7474e7d3
PP
2153 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
2154 "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin);
2155 status = iterator->methods.seek_ns_from_origin(iterator,
2156 ns_from_origin);
2157 BT_LOGD("User method returned: status=%s",
d24d5663 2158 bt_common_func_status_string(status));
1778c2a4
PP
2159 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME, "valid-status",
2160 status == BT_FUNC_STATUS_OK ||
d24d5663
PP
2161 status == BT_FUNC_STATUS_ERROR ||
2162 status == BT_FUNC_STATUS_MEMORY_ERROR ||
2163 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 2164 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 2165 iterator, bt_common_func_status_string(status));
1778c2a4
PP
2166 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
2167 SEEK_NS_FROM_ORIGIN_METHOD_NAME, status);
870631a2
PP
2168 if (status < 0) {
2169 BT_LIB_LOGW_APPEND_CAUSE(
2170 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
2171 "%![iter-]+i, status=%s",
2172 iterator, bt_common_func_status_string(status));
2173 }
7474e7d3 2174 } else {
5b7b55be 2175 /*
c0e46a7c
SM
2176 * The iterator doesn't know how to seek by itself to a
2177 * particular time. We will seek to the beginning and fast
2178 * forward to the right place.
5b7b55be 2179 */
a3f0c7db 2180 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status;
f2fb1b32
SM
2181 bt_bool can_seek_beginning;
2182
2183 can_seek_status = iterator->methods.can_seek_beginning(iterator,
2184 &can_seek_beginning);
2185 BT_ASSERT(can_seek_status == BT_FUNC_STATUS_OK);
2186 BT_ASSERT(can_seek_beginning);
7474e7d3
PP
2187 BT_ASSERT(iterator->methods.seek_beginning);
2188 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
2189 iterator);
2190 status = iterator->methods.seek_beginning(iterator);
2191 BT_LOGD("User method returned: status=%s",
d24d5663 2192 bt_common_func_status_string(status));
1778c2a4
PP
2193 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME, "valid-status",
2194 status == BT_FUNC_STATUS_OK ||
d24d5663
PP
2195 status == BT_FUNC_STATUS_ERROR ||
2196 status == BT_FUNC_STATUS_MEMORY_ERROR ||
2197 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 2198 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 2199 iterator, bt_common_func_status_string(status));
870631a2
PP
2200 if (status < 0) {
2201 BT_LIB_LOGW_APPEND_CAUSE(
2202 "Component input port message iterator's \"seek beginning\" method failed: "
2203 "%![iter-]+i, status=%s",
2204 iterator, bt_common_func_status_string(status));
2205 }
2206
9340eff9
SM
2207 clear_per_stream_state(iterator);
2208
7474e7d3 2209 switch (status) {
d24d5663 2210 case BT_FUNC_STATUS_OK:
7474e7d3 2211 break;
d24d5663
PP
2212 case BT_FUNC_STATUS_ERROR:
2213 case BT_FUNC_STATUS_MEMORY_ERROR:
2214 case BT_FUNC_STATUS_AGAIN:
7474e7d3
PP
2215 goto end;
2216 default:
498e7994 2217 bt_common_abort();
7474e7d3
PP
2218 }
2219
2220 /*
2221 * Find the first message which has a default clock
2222 * snapshot greater than or equal to the requested
5b9e151d
PP
2223 * seeking time, and move the received messages from
2224 * this point in the batch to this iterator's auto-seek
2225 * message queue.
7474e7d3 2226 */
da9c4c52 2227 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
6871026b 2228 bt_object_put_ref_no_null_check(
da9c4c52 2229 g_queue_pop_tail(iterator->auto_seek.msgs));
5b9e151d
PP
2230 }
2231
5b7b55be
SM
2232 stream_states = create_auto_seek_stream_states();
2233 if (!stream_states) {
870631a2
PP
2234 BT_LIB_LOGE_APPEND_CAUSE(
2235 "Failed to allocate one GHashTable.");
d24d5663 2236 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
2237 goto end;
2238 }
2239
7474e7d3 2240 status = find_message_ge_ns_from_origin(iterator,
5b7b55be 2241 ns_from_origin, stream_states);
7474e7d3 2242 switch (status) {
d24d5663
PP
2243 case BT_FUNC_STATUS_OK:
2244 case BT_FUNC_STATUS_END:
5b7b55be
SM
2245 {
2246 GHashTableIter iter;
2247 gpointer key, value;
2248
2249 /*
2250 * If some streams exist at the seek time, prepend the
2251 * required messages to put those streams in the right
2252 * state.
2253 */
2254 g_hash_table_iter_init(&iter, stream_states);
2255 while (g_hash_table_iter_next (&iter, &key, &value)) {
2256 const bt_stream *stream = key;
2257 struct auto_seek_stream_state *stream_state =
2258 (struct auto_seek_stream_state *) value;
2259 bt_message *msg;
2260 const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const(
2261 bt_stream_borrow_class_const(stream));
188edac1
SM
2262 /* Initialize to silence maybe-uninitialized warning. */
2263 uint64_t raw_value = 0;
2264
2265 /*
2266 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2267 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2268 *
2269 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2270 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2271 * seen a message with a clock snapshot in our seeking, then we are sure that the
2272 * seek time is not below the clock range, and we know the stream was active at that
2273 * time (and that we cut it short).
2274 */
2275 if (stream_state->seen_clock_snapshot) {
2276 if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) {
2277 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K",
2278 ns_from_origin, clock_class);
2279 status = BT_FUNC_STATUS_ERROR;
2280 goto end;
2281 }
5b7b55be
SM
2282 }
2283
2284 switch (stream_state->state) {
2285 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN:
2286 BT_ASSERT(stream_state->packet);
2287 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet);
188edac1
SM
2288
2289 if (stream->class->packets_have_beginning_default_clock_snapshot) {
2290 /*
2291 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2292 * message. If "packet beginning" packets have clock snapshots, then we must have
2293 * seen a clock snapshot.
2294 */
2295 BT_ASSERT(stream_state->seen_clock_snapshot);
2296
2297 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
2298 (bt_self_message_iterator *) iterator, stream_state->packet, raw_value);
2299 } else {
2300 msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator,
2301 stream_state->packet);
5b7b55be
SM
2302 }
2303
5b7b55be 2304 if (!msg) {
d24d5663 2305 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
2306 goto end;
2307 }
2308
5b7b55be
SM
2309 g_queue_push_head(iterator->auto_seek.msgs, msg);
2310 msg = NULL;
2311 /* fall-thru */
188edac1 2312
5b7b55be
SM
2313 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN:
2314 msg = bt_message_stream_beginning_create(
2315 (bt_self_message_iterator *) iterator, stream);
2316 if (!msg) {
d24d5663 2317 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
2318 goto end;
2319 }
2320
188edac1
SM
2321 if (stream_state->seen_clock_snapshot) {
2322 bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value);
2323 }
2324
5b7b55be
SM
2325 g_queue_push_head(iterator->auto_seek.msgs, msg);
2326 msg = NULL;
2327 break;
2328 }
2329 }
2330
7474e7d3 2331 /*
5b9e151d
PP
2332 * If there are messages in the auto-seek
2333 * message queue, replace the user's "next"
2334 * method with a custom, temporary "next" method
2335 * which returns them.
7474e7d3 2336 */
da9c4c52 2337 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
572075a8
SM
2338 BT_ASSERT(!iterator->auto_seek.original_next_callback);
2339 iterator->auto_seek.original_next_callback = iterator->methods.next;
2340
5b9e151d 2341 iterator->methods.next =
9a2c8b8e 2342 (bt_message_iterator_next_method)
5b9e151d
PP
2343 post_auto_seek_next;
2344 }
2345
2346 /*
d24d5663
PP
2347 * `BT_FUNC_STATUS_END` becomes
2348 * `BT_FUNC_STATUS_OK`: the next
5b9e151d
PP
2349 * time this iterator's "next" method is called,
2350 * it will return
d24d5663 2351 * `BT_FUNC_STATUS_END`.
5b9e151d 2352 */
d24d5663 2353 status = BT_FUNC_STATUS_OK;
7474e7d3 2354 break;
5b7b55be 2355 }
d24d5663
PP
2356 case BT_FUNC_STATUS_ERROR:
2357 case BT_FUNC_STATUS_MEMORY_ERROR:
2358 case BT_FUNC_STATUS_AGAIN:
7474e7d3 2359 goto end;
7474e7d3 2360 default:
498e7994 2361 bt_common_abort();
7474e7d3
PP
2362 }
2363 }
2364
9340eff9
SM
2365 clear_per_stream_state(iterator);
2366
54b135a0
SM
2367 /*
2368 * The following messages returned by the next method (including
2369 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2370 */
2371 iterator->last_ns_from_origin = ns_from_origin;
2372
7474e7d3 2373end:
5b7b55be
SM
2374 if (stream_states) {
2375 destroy_auto_seek_stream_states(stream_states);
2376 stream_states = NULL;
2377 }
870631a2 2378
7474e7d3 2379 set_iterator_state_after_seeking(iterator, status);
7474e7d3
PP
2380 return status;
2381}
2382
1353b066 2383BT_EXPORT
9b4f9b42
PP
2384bt_bool bt_self_message_iterator_is_interrupted(
2385 const struct bt_self_message_iterator *self_msg_iter)
2386{
9a2c8b8e 2387 const struct bt_message_iterator *iterator =
9b4f9b42
PP
2388 (const void *) self_msg_iter;
2389
d5b13b9b 2390 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
9b4f9b42
PP
2391 return (bt_bool) bt_graph_is_interrupted(iterator->graph);
2392}
2393
1353b066 2394BT_EXPORT
9a2c8b8e
PP
2395void bt_message_iterator_get_ref(
2396 const struct bt_message_iterator *iterator)
c5b9b441
PP
2397{
2398 bt_object_get_ref(iterator);
2399}
2400
1353b066 2401BT_EXPORT
9a2c8b8e
PP
2402void bt_message_iterator_put_ref(
2403 const struct bt_message_iterator *iterator)
c5b9b441
PP
2404{
2405 bt_object_put_ref(iterator);
2406}
This page took 0.259169 seconds and 4 git commands to generate.