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