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