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