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