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