lib: pass config object to message iterator init method, add can seek forward property
[babeltrace.git] / src / plugins / utils / muxer / muxer.c
CommitLineData
958f7d11
PP
1/*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
5b6473ec 23#define BT_COMP_LOG_SELF_COMP (muxer_comp->self_comp)
87ec3926 24#define BT_LOG_OUTPUT_LEVEL (muxer_comp->log_level)
350ad6c1 25#define BT_LOG_TAG "PLUGIN/FLT.UTILS.MUXER"
d9c39b0a 26#include "logging/comp-logging.h"
fed72692 27
91d81473 28#include "common/macros.h"
6162e6b7 29#include "common/uuid.h"
3fadfbc0 30#include <babeltrace2/babeltrace.h>
958f7d11 31#include <glib.h>
c55a9f58 32#include <stdbool.h>
fed72692 33#include <inttypes.h>
578e048b
MJ
34#include "common/assert.h"
35#include "common/common.h"
0fbb9a9f 36#include <stdlib.h>
282c8cd0 37#include <string.h>
958f7d11 38
1aca5200
FD
39#include "plugins/common/muxing/muxing.h"
40
fdf0b89e
FD
41#include "muxer.h"
42
958f7d11 43struct muxer_comp {
5b6473ec
PP
44 /* Weak refs */
45 bt_self_component_filter *self_comp_flt;
46 bt_self_component *self_comp;
d94d92ac 47
958f7d11
PP
48 unsigned int next_port_num;
49 size_t available_input_ports;
d6e69534 50 bool initializing_muxer_msg_iter;
87ec3926 51 bt_logging_level log_level;
958f7d11
PP
52};
53
d6e69534 54struct muxer_upstream_msg_iter {
87ec3926
PP
55 struct muxer_comp *muxer_comp;
56
ab11110e 57 /* Owned by this, NULL if ended */
d6e69534 58 bt_self_component_port_input_message_iterator *msg_iter;
958f7d11 59
d6e69534
PP
60 /* Contains `const bt_message *`, owned by this */
61 GQueue *msgs;
958f7d11
PP
62};
63
d6e69534
PP
64enum muxer_msg_iter_clock_class_expectation {
65 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0,
60f3d027 66 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE,
d6e69534
PP
67 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE,
68 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID,
69 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID,
282c8cd0
PP
70};
71
d6e69534 72struct muxer_msg_iter {
87ec3926
PP
73 struct muxer_comp *muxer_comp;
74
ca02df0a
PP
75 /* Weak */
76 bt_self_message_iterator *self_msg_iter;
77
ab11110e 78 /*
d6e69534 79 * Array of struct muxer_upstream_msg_iter * (owned by this).
ab11110e
PP
80 *
81 * NOTE: This array is searched in linearly to find the youngest
d6e69534 82 * current message. Keep this until benchmarks confirm that
ab11110e
PP
83 * another data structure is faster than this for our typical
84 * use cases.
85 */
54bdc1f7
PP
86 GPtrArray *active_muxer_upstream_msg_iters;
87
88 /*
89 * Array of struct muxer_upstream_msg_iter * (owned by this).
90 *
91 * We move ended message iterators from
92 * `active_muxer_upstream_msg_iters` to this array so as to be
93 * able to restore them when seeking.
94 */
95 GPtrArray *ended_muxer_upstream_msg_iters;
958f7d11 96
d6e69534 97 /* Last time returned in a message */
958f7d11 98 int64_t last_returned_ts_ns;
282c8cd0
PP
99
100 /* Clock class expectation state */
d6e69534 101 enum muxer_msg_iter_clock_class_expectation clock_class_expectation;
282c8cd0
PP
102
103 /*
104 * Expected clock class UUID, only valid when
105 * clock_class_expectation is
d6e69534 106 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
282c8cd0 107 */
6162e6b7 108 bt_uuid_t expected_clock_class_uuid;
958f7d11
PP
109};
110
54bdc1f7
PP
111static
112void empty_message_queue(struct muxer_upstream_msg_iter *upstream_msg_iter)
113{
114 const bt_message *msg;
115
116 while ((msg = g_queue_pop_head(upstream_msg_iter->msgs))) {
117 bt_message_put_ref(msg);
118 }
119}
120
ab11110e 121static
d6e69534
PP
122void destroy_muxer_upstream_msg_iter(
123 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter)
ab11110e 124{
87ec3926
PP
125 struct muxer_comp *muxer_comp;
126
d6e69534 127 if (!muxer_upstream_msg_iter) {
ab11110e
PP
128 return;
129 }
130
87ec3926 131 muxer_comp = muxer_upstream_msg_iter->muxer_comp;
5b6473ec 132 BT_COMP_LOGD("Destroying muxer's upstream message iterator wrapper: "
d6e69534
PP
133 "addr=%p, msg-iter-addr=%p, queue-len=%u",
134 muxer_upstream_msg_iter,
135 muxer_upstream_msg_iter->msg_iter,
136 muxer_upstream_msg_iter->msgs->length);
54bdc1f7
PP
137 bt_self_component_port_input_message_iterator_put_ref(
138 muxer_upstream_msg_iter->msg_iter);
d4393e08 139
d6e69534 140 if (muxer_upstream_msg_iter->msgs) {
54bdc1f7 141 empty_message_queue(muxer_upstream_msg_iter);
d6e69534 142 g_queue_free(muxer_upstream_msg_iter->msgs);
d4393e08
PP
143 }
144
d6e69534 145 g_free(muxer_upstream_msg_iter);
ab11110e
PP
146}
147
958f7d11 148static
c61018b9 149int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter *muxer_msg_iter,
d6e69534 150 bt_self_component_port_input_message_iterator *self_msg_iter)
958f7d11 151{
c61018b9 152 int ret = 0;
d6e69534
PP
153 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
154 g_new0(struct muxer_upstream_msg_iter, 1);
87ec3926 155 struct muxer_comp *muxer_comp = muxer_msg_iter->muxer_comp;
958f7d11 156
d6e69534 157 if (!muxer_upstream_msg_iter) {
5b6473ec 158 BT_COMP_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
6c20f4a0 159 goto error;
958f7d11
PP
160 }
161
87ec3926 162 muxer_upstream_msg_iter->muxer_comp = muxer_comp;
d6e69534
PP
163 muxer_upstream_msg_iter->msg_iter = self_msg_iter;
164 bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter->msg_iter);
165 muxer_upstream_msg_iter->msgs = g_queue_new();
166 if (!muxer_upstream_msg_iter->msgs) {
5b6473ec 167 BT_COMP_LOGE_STR("Failed to allocate a GQueue.");
6c20f4a0 168 goto error;
d4393e08
PP
169 }
170
54bdc1f7 171 g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
d6e69534 172 muxer_upstream_msg_iter);
5b6473ec 173 BT_COMP_LOGD("Added muxer's upstream message iterator wrapper: "
d6e69534
PP
174 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
175 muxer_upstream_msg_iter, muxer_msg_iter,
176 self_msg_iter);
958f7d11 177
6c20f4a0
FD
178 goto end;
179
180error:
181 g_free(muxer_upstream_msg_iter);
c61018b9 182 ret = -1;
6c20f4a0 183
958f7d11 184end:
c61018b9 185 return ret;
958f7d11
PP
186}
187
958f7d11 188static
d24d5663 189bt_self_component_add_port_status add_available_input_port(
b19ff26f 190 bt_self_component_filter *self_comp)
958f7d11 191{
d94d92ac 192 struct muxer_comp *muxer_comp = bt_self_component_get_data(
707b7d35 193 bt_self_component_filter_as_self_component(self_comp));
d24d5663
PP
194 bt_self_component_add_port_status status =
195 BT_SELF_COMPONENT_ADD_PORT_STATUS_OK;
958f7d11 196 GString *port_name = NULL;
958f7d11 197
f6ccaed9 198 BT_ASSERT(muxer_comp);
958f7d11
PP
199 port_name = g_string_new("in");
200 if (!port_name) {
5b6473ec 201 BT_COMP_LOGE_STR("Failed to allocate a GString.");
d24d5663 202 status = BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR;
958f7d11
PP
203 goto end;
204 }
205
206 g_string_append_printf(port_name, "%u", muxer_comp->next_port_num);
d94d92ac
PP
207 status = bt_self_component_filter_add_input_port(
208 self_comp, port_name->str, NULL, NULL);
d24d5663 209 if (status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
5b6473ec 210 BT_COMP_LOGE("Cannot add input port to muxer component: "
fed72692 211 "port-name=\"%s\", comp-addr=%p, status=%s",
d94d92ac 212 port_name->str, self_comp,
d24d5663 213 bt_common_func_status_string(status));
958f7d11
PP
214 goto end;
215 }
216
217 muxer_comp->available_input_ports++;
218 muxer_comp->next_port_num++;
5b6473ec 219 BT_COMP_LOGI("Added one input port to muxer component: "
fed72692 220 "port-name=\"%s\", comp-addr=%p",
d94d92ac 221 port_name->str, self_comp);
5badd463 222
958f7d11
PP
223end:
224 if (port_name) {
225 g_string_free(port_name, TRUE);
226 }
227
147337a3 228 return status;
958f7d11
PP
229}
230
958f7d11 231static
d24d5663 232bt_self_component_add_port_status create_output_port(
b19ff26f 233 bt_self_component_filter *self_comp)
958f7d11 234{
d94d92ac
PP
235 return bt_self_component_filter_add_output_port(
236 self_comp, "out", NULL, NULL);
958f7d11
PP
237}
238
239static
240void destroy_muxer_comp(struct muxer_comp *muxer_comp)
241{
242 if (!muxer_comp) {
243 return;
244 }
245
958f7d11
PP
246 g_free(muxer_comp);
247}
248
249BT_HIDDEN
d24d5663 250bt_component_class_init_method_status muxer_init(
5b6473ec 251 bt_self_component_filter *self_comp_flt,
59225a3e 252 bt_self_component_filter_configuration *config,
fdf0b89e 253 const bt_value *params, void *init_data)
958f7d11 254{
d24d5663
PP
255 bt_component_class_init_method_status status =
256 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
257 bt_self_component_add_port_status add_port_status;
5b6473ec
PP
258 bt_self_component *self_comp =
259 bt_self_component_filter_as_self_component(self_comp_flt);
958f7d11 260 struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1);
87ec3926 261 bt_logging_level log_level = bt_component_get_logging_level(
5b6473ec 262 bt_self_component_as_component(self_comp));
958f7d11 263
5b6473ec 264 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO, log_level, self_comp,
87ec3926 265 "Initializing muxer component: "
d94d92ac 266 "comp-addr=%p, params-addr=%p", self_comp, params);
fed72692 267
958f7d11 268 if (!muxer_comp) {
5b6473ec 269 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
87ec3926 270 "Failed to allocate one muxer component.");
958f7d11
PP
271 goto error;
272 }
273
87ec3926 274 muxer_comp->log_level = log_level;
5b6473ec
PP
275 muxer_comp->self_comp = self_comp;
276 muxer_comp->self_comp_flt = self_comp_flt;
65ee897d 277
5b6473ec 278 bt_self_component_set_data(self_comp, muxer_comp);
d24d5663
PP
279 add_port_status = add_available_input_port(self_comp_flt);
280 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
5b6473ec 281 BT_COMP_LOGE("Cannot ensure that at least one muxer component's input port is available: "
fed72692
PP
282 "muxer-comp-addr=%p, status=%s",
283 muxer_comp,
d24d5663
PP
284 bt_common_func_status_string(add_port_status));
285 if (add_port_status ==
286 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
287 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
288 } else {
289 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
290 }
291
958f7d11
PP
292 goto error;
293 }
294
d24d5663
PP
295 add_port_status = create_output_port(self_comp_flt);
296 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
5b6473ec 297 BT_COMP_LOGE("Cannot create muxer component's output port: "
fed72692
PP
298 "muxer-comp-addr=%p, status=%s",
299 muxer_comp,
d24d5663
PP
300 bt_common_func_status_string(add_port_status));
301 if (add_port_status ==
302 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
303 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
304 } else {
305 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
306 }
307
958f7d11
PP
308 goto error;
309 }
310
5b6473ec 311 BT_COMP_LOGI("Initialized muxer component: "
fed72692 312 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
d94d92ac 313 self_comp, params, muxer_comp);
fed72692 314
958f7d11
PP
315 goto end;
316
317error:
318 destroy_muxer_comp(muxer_comp);
5b6473ec 319 bt_self_component_set_data(self_comp, NULL);
147337a3 320
d24d5663
PP
321 if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
322 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
147337a3 323 }
958f7d11
PP
324
325end:
326 return status;
327}
328
329BT_HIDDEN
b19ff26f 330void muxer_finalize(bt_self_component_filter *self_comp)
958f7d11 331{
d94d92ac 332 struct muxer_comp *muxer_comp = bt_self_component_get_data(
707b7d35 333 bt_self_component_filter_as_self_component(self_comp));
958f7d11 334
5b6473ec 335 BT_COMP_LOGI("Finalizing muxer component: comp-addr=%p",
d94d92ac 336 self_comp);
958f7d11
PP
337 destroy_muxer_comp(muxer_comp);
338}
339
340static
e803df70 341bt_self_component_port_input_message_iterator_create_from_message_iterator_status
87ec3926 342create_msg_iter_on_input_port(struct muxer_comp *muxer_comp,
ca02df0a 343 struct muxer_msg_iter *muxer_msg_iter,
e803df70
SM
344 bt_self_component_port_input *self_port,
345 bt_self_component_port_input_message_iterator **msg_iter)
958f7d11 346{
b19ff26f 347 const bt_port *port = bt_self_component_port_as_port(
707b7d35 348 bt_self_component_port_input_as_self_component_port(
d94d92ac 349 self_port));
e803df70
SM
350 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
351 status;
958f7d11 352
f6ccaed9
PP
353 BT_ASSERT(port);
354 BT_ASSERT(bt_port_is_connected(port));
958f7d11 355
ab11110e 356 // TODO: Advance the iterator to >= the time of the latest
d6e69534 357 // returned message by the muxer message
ab11110e 358 // iterator which creates it.
e803df70
SM
359 status = bt_self_component_port_input_message_iterator_create_from_message_iterator(
360 muxer_msg_iter->self_msg_iter, self_port, msg_iter);
361 if (status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
5b6473ec 362 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
d94d92ac
PP
363 "port-addr=%p, port-name=\"%s\"",
364 port, bt_port_get_name(port));
958f7d11
PP
365 goto end;
366 }
367
5b6473ec 368 BT_COMP_LOGI("Created upstream message iterator on input port: "
d6e69534
PP
369 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
370 port, bt_port_get_name(port), msg_iter);
fed72692 371
958f7d11 372end:
e803df70 373 return status;
958f7d11
PP
374}
375
ab11110e 376static
d24d5663 377bt_component_class_message_iterator_next_method_status muxer_upstream_msg_iter_next(
54bdc1f7
PP
378 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
379 bool *is_ended)
ab11110e 380{
87ec3926
PP
381 struct muxer_comp *muxer_comp =
382 muxer_upstream_msg_iter->muxer_comp;
d24d5663
PP
383 bt_component_class_message_iterator_next_method_status status;
384 bt_message_iterator_next_status input_port_iter_status;
d6e69534 385 bt_message_array_const msgs;
d4393e08
PP
386 uint64_t i;
387 uint64_t count;
ab11110e 388
5b6473ec 389 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
d6e69534
PP
390 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
391 muxer_upstream_msg_iter,
392 muxer_upstream_msg_iter->msg_iter);
fdf0b89e 393 input_port_iter_status = bt_self_component_port_input_message_iterator_next(
d6e69534 394 muxer_upstream_msg_iter->msg_iter, &msgs, &count);
5b6473ec 395 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
d24d5663
PP
396 "status=%s",
397 bt_common_func_status_string(input_port_iter_status));
ab11110e 398
fdf0b89e 399 switch (input_port_iter_status) {
d24d5663 400 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
089717de 401 /*
d6e69534 402 * Message iterator's current message is
d4393e08 403 * valid: it must be considered for muxing operations.
089717de 404 */
5b6473ec 405 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
d4393e08
PP
406 BT_ASSERT(count > 0);
407
d6e69534 408 /* Move messages to our queue */
d4393e08
PP
409 for (i = 0; i < count; i++) {
410 /*
411 * Push to tail in order; other side
d6e69534 412 * (muxer_msg_iter_do_next_one()) consumes
d4393e08
PP
413 * from the head first.
414 */
d6e69534
PP
415 g_queue_push_tail(muxer_upstream_msg_iter->msgs,
416 (void *) msgs[i]);
d4393e08 417 }
d24d5663 418 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
ab11110e 419 break;
d24d5663 420 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
089717de 421 /*
d6e69534 422 * Message iterator's current message is not
089717de 423 * valid anymore. Return
d24d5663 424 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
089717de 425 */
d24d5663 426 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN;
ab11110e 427 break;
d24d5663 428 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: /* Fall-through. */
ab11110e 429 /*
d6e69534 430 * Message iterator reached the end: release it. It
ab11110e 431 * won't be considered again to find the youngest
d6e69534 432 * message.
ab11110e 433 */
54bdc1f7 434 *is_ended = true;
d24d5663 435 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
089717de 436 break;
ab11110e
PP
437 default:
438 /* Error or unsupported status code */
5b6473ec 439 BT_COMP_LOGE("Error or unsupported status code: "
fdf0b89e 440 "status-code=%d", input_port_iter_status);
d24d5663 441 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
089717de 442 break;
ab11110e
PP
443 }
444
089717de 445 return status;
ab11110e
PP
446}
447
958f7d11 448static
d6e69534
PP
449int get_msg_ts_ns(struct muxer_comp *muxer_comp,
450 struct muxer_msg_iter *muxer_msg_iter,
451 const bt_message *msg, int64_t last_returned_ts_ns,
958f7d11
PP
452 int64_t *ts_ns)
453{
605e1019 454 const bt_clock_snapshot *clock_snapshot = NULL;
958f7d11 455 int ret = 0;
649934d2
PP
456 const bt_stream_class *stream_class = NULL;
457 bt_message_type msg_type;
958f7d11 458
d6e69534 459 BT_ASSERT(msg);
f6ccaed9 460 BT_ASSERT(ts_ns);
5b6473ec 461 BT_COMP_LOGD("Getting message's timestamp: "
d6e69534 462 "muxer-msg-iter-addr=%p, msg-addr=%p, "
fed72692 463 "last-returned-ts=%" PRId64,
d6e69534 464 muxer_msg_iter, msg, last_returned_ts_ns);
fed72692 465
91d81473 466 if (G_UNLIKELY(muxer_msg_iter->clock_class_expectation ==
60f3d027
PP
467 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE)) {
468 *ts_ns = last_returned_ts_ns;
469 goto end;
470 }
471
649934d2
PP
472 msg_type = bt_message_get_type(msg);
473
91d81473 474 if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_BEGINNING)) {
649934d2
PP
475 stream_class = bt_stream_borrow_class_const(
476 bt_packet_borrow_stream_const(
477 bt_message_packet_beginning_borrow_packet_const(
478 msg)));
91d81473 479 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_END)) {
649934d2
PP
480 stream_class = bt_stream_borrow_class_const(
481 bt_packet_borrow_stream_const(
482 bt_message_packet_end_borrow_packet_const(
483 msg)));
91d81473 484 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS)) {
2e90378a
PP
485 stream_class = bt_stream_borrow_class_const(
486 bt_message_discarded_events_borrow_stream_const(msg));
91d81473 487 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS)) {
2e90378a
PP
488 stream_class = bt_stream_borrow_class_const(
489 bt_message_discarded_packets_borrow_stream_const(msg));
649934d2
PP
490 }
491
492 switch (msg_type) {
d6e69534 493 case BT_MESSAGE_TYPE_EVENT:
60f3d027
PP
494 BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const(
495 msg));
0cbc2c33
PP
496 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
497 msg);
958f7d11 498 break;
5366eb53 499 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
9b24b6aa 500 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
649934d2
PP
501 stream_class)) {
502 clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
503 msg);
504 } else {
505 goto no_clock_snapshot;
506 }
507
5366eb53
PP
508 break;
509 case BT_MESSAGE_TYPE_PACKET_END:
9b24b6aa 510 if (bt_stream_class_packets_have_end_default_clock_snapshot(
649934d2
PP
511 stream_class)) {
512 clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
513 msg);
514 } else {
515 goto no_clock_snapshot;
516 }
517
5366eb53 518 break;
c47138bf
FD
519 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
520 {
521 enum bt_message_stream_clock_snapshot_state snapshot_state =
522 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
523 msg, &clock_snapshot);
524 if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) {
525 goto no_clock_snapshot;
526 }
527
528 break;
529 }
530 case BT_MESSAGE_TYPE_STREAM_END:
531 {
532 enum bt_message_stream_clock_snapshot_state snapshot_state =
533 bt_message_stream_end_borrow_default_clock_snapshot_const(
534 msg, &clock_snapshot);
535 if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) {
536 goto no_clock_snapshot;
537 }
538
539 break;
540 }
5366eb53 541 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
2e90378a
PP
542 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
543 stream_class)) {
9b24b6aa 544 clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
2e90378a
PP
545 msg);
546 } else {
547 goto no_clock_snapshot;
548 }
549
5366eb53
PP
550 break;
551 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
2e90378a
PP
552 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
553 stream_class)) {
9b24b6aa 554 clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
2e90378a
PP
555 msg);
556 } else {
557 goto no_clock_snapshot;
558 }
559
5366eb53 560 break;
b9fd9cbb 561 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
0cbc2c33
PP
562 clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
563 msg);
958f7d11
PP
564 break;
565 default:
d6e69534 566 /* All the other messages have a higher priority */
5b6473ec 567 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
958f7d11
PP
568 *ts_ns = last_returned_ts_ns;
569 goto end;
570 }
571
60f3d027
PP
572 ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns);
573 if (ret) {
5b6473ec 574 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
60f3d027
PP
575 "clock-snapshot-addr=%p", clock_snapshot);
576 goto error;
577 }
578
579 goto end;
580
581no_clock_snapshot:
5b6473ec 582 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
60f3d027
PP
583 "using the last returned timestamp.");
584 *ts_ns = last_returned_ts_ns;
585 goto end;
586
587error:
588 ret = -1;
589
590end:
591 if (ret == 0) {
5b6473ec 592 BT_COMP_LOGD("Found message's timestamp: "
60f3d027
PP
593 "muxer-msg-iter-addr=%p, msg-addr=%p, "
594 "last-returned-ts=%" PRId64 ", ts=%" PRId64,
595 muxer_msg_iter, msg, last_returned_ts_ns,
596 *ts_ns);
44c440bc
PP
597 }
598
60f3d027
PP
599 return ret;
600}
601
602static inline
603int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter,
604 struct muxer_comp *muxer_comp,
605 const bt_clock_class *clock_class)
606{
607 int ret = 0;
6162e6b7 608 const uint8_t *cc_uuid;
60f3d027
PP
609 const char *cc_name;
610
e22b45d0 611 BT_ASSERT(clock_class);
50842bdc
PP
612 cc_uuid = bt_clock_class_get_uuid(clock_class);
613 cc_name = bt_clock_class_get_name(clock_class);
282c8cd0 614
d6e69534
PP
615 if (muxer_msg_iter->clock_class_expectation ==
616 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
282c8cd0 617 /*
74f4949e
FD
618 * This is the first clock class that this muxer message
619 * iterator encounters. Its properties determine what to expect
620 * for the whole lifetime of the iterator.
282c8cd0 621 */
5552377a 622 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
282c8cd0 623 /* Expect absolute clock classes */
d6e69534
PP
624 muxer_msg_iter->clock_class_expectation =
625 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE;
282c8cd0
PP
626 } else {
627 if (cc_uuid) {
628 /*
629 * Expect non-absolute clock classes
630 * with a specific UUID.
631 */
d6e69534
PP
632 muxer_msg_iter->clock_class_expectation =
633 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID;
6162e6b7 634 bt_uuid_copy(muxer_msg_iter->expected_clock_class_uuid, cc_uuid);
282c8cd0
PP
635 } else {
636 /*
637 * Expect non-absolute clock classes
638 * with no UUID.
639 */
d6e69534
PP
640 muxer_msg_iter->clock_class_expectation =
641 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID;
282c8cd0
PP
642 }
643 }
644 }
645
74f4949e
FD
646 switch (muxer_msg_iter->clock_class_expectation) {
647 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE:
648 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
649 BT_COMP_LOGE("Expecting an absolute clock class, "
650 "but got a non-absolute one: "
651 "clock-class-addr=%p, clock-class-name=\"%s\"",
652 clock_class, cc_name);
653 goto error;
654 }
655 break;
656 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID:
657 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
658 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
659 "but got an absolute one: "
660 "clock-class-addr=%p, clock-class-name=\"%s\"",
661 clock_class, cc_name);
662 goto error;
663 }
282c8cd0 664
74f4949e
FD
665 if (cc_uuid) {
666 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
667 "but got one with a UUID: "
668 "clock-class-addr=%p, clock-class-name=\"%s\", "
669 "uuid=\"" BT_UUID_FMT "\"",
670 clock_class, cc_name, BT_UUID_FMT_VALUES(cc_uuid));
671 goto error;
672 }
673 break;
674 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID:
675 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
676 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
677 "but got an absolute one: "
678 "clock-class-addr=%p, clock-class-name=\"%s\"",
679 clock_class, cc_name);
680 goto error;
681 }
282c8cd0 682
74f4949e
FD
683 if (!cc_uuid) {
684 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
685 "but got one with no UUID: "
60f3d027
PP
686 "clock-class-addr=%p, clock-class-name=\"%s\"",
687 clock_class, cc_name);
688 goto error;
282c8cd0 689 }
74f4949e
FD
690
691 if (bt_uuid_compare(muxer_msg_iter->expected_clock_class_uuid, cc_uuid) != 0) {
692 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
693 "but got one with different UUID: "
694 "clock-class-addr=%p, clock-class-name=\"%s\", "
695 "expected-uuid=\"" BT_UUID_FMT "\", "
696 "uuid=\"" BT_UUID_FMT "\"",
697 clock_class, cc_name,
698 BT_UUID_FMT_VALUES(muxer_msg_iter->expected_clock_class_uuid),
699 BT_UUID_FMT_VALUES(cc_uuid));
700 goto error;
701 }
702 break;
703 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE:
704 BT_COMP_LOGE("Expecting no clock class, but got one: "
705 "clock-class-addr=%p, clock-class-name=\"%s\"",
706 clock_class, cc_name);
707 goto error;
708 default:
709 /* Unexpected */
710 BT_COMP_LOGF("Unexpected clock class expectation: "
711 "expectation-code=%d",
712 muxer_msg_iter->clock_class_expectation);
713 abort();
958f7d11
PP
714 }
715
4c0f1c8c
PP
716 goto end;
717
958f7d11
PP
718error:
719 ret = -1;
720
721end:
60f3d027
PP
722 return ret;
723}
724
725static inline
726int validate_new_stream_clock_class(struct muxer_msg_iter *muxer_msg_iter,
727 struct muxer_comp *muxer_comp, const bt_stream *stream)
728{
729 int ret = 0;
730 const bt_stream_class *stream_class =
731 bt_stream_borrow_class_const(stream);
732 const bt_clock_class *clock_class =
733 bt_stream_class_borrow_default_clock_class_const(stream_class);
734
735 if (!clock_class) {
736 if (muxer_msg_iter->clock_class_expectation ==
737 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
738 /* Expect no clock class */
739 muxer_msg_iter->clock_class_expectation =
740 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE;
9244b07a
SM
741 } else if (muxer_msg_iter->clock_class_expectation !=
742 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE) {
743 BT_COMP_LOGE("Expecting stream class without a default clock class: "
60f3d027
PP
744 "stream-class-addr=%p, stream-class-name=\"%s\", "
745 "stream-class-id=%" PRIu64,
746 stream_class, bt_stream_class_get_name(stream_class),
747 bt_stream_class_get_id(stream_class));
748 ret = -1;
749 }
750
751 goto end;
fed72692
PP
752 }
753
60f3d027
PP
754 ret = validate_clock_class(muxer_msg_iter, muxer_comp, clock_class);
755
756end:
958f7d11
PP
757 return ret;
758}
759
ab11110e 760/*
d6e69534
PP
761 * This function finds the youngest available message amongst the
762 * non-ended upstream message iterators and returns the upstream
763 * message iterator which has it, or
764 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
765 * message.
ab11110e
PP
766 *
767 * This function does NOT:
768 *
d6e69534 769 * * Update any upstream message iterator.
d6e69534 770 * * Check the upstream message iterators to retry.
ab11110e 771 *
d6e69534
PP
772 * On sucess, this function sets *muxer_upstream_msg_iter to the
773 * upstream message iterator of which the current message is
ab11110e
PP
774 * the youngest, and sets *ts_ns to its time.
775 */
958f7d11 776static
d24d5663 777bt_component_class_message_iterator_next_method_status
d6e69534 778muxer_msg_iter_youngest_upstream_msg_iter(
958f7d11 779 struct muxer_comp *muxer_comp,
d6e69534
PP
780 struct muxer_msg_iter *muxer_msg_iter,
781 struct muxer_upstream_msg_iter **muxer_upstream_msg_iter,
958f7d11
PP
782 int64_t *ts_ns)
783{
784 size_t i;
785 int ret;
786 int64_t youngest_ts_ns = INT64_MAX;
d24d5663
PP
787 bt_component_class_message_iterator_next_method_status status =
788 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
958f7d11 789
f6ccaed9 790 BT_ASSERT(muxer_comp);
d6e69534
PP
791 BT_ASSERT(muxer_msg_iter);
792 BT_ASSERT(muxer_upstream_msg_iter);
793 *muxer_upstream_msg_iter = NULL;
794
54bdc1f7
PP
795 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
796 i++) {
d6e69534
PP
797 const bt_message *msg;
798 struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter =
54bdc1f7
PP
799 g_ptr_array_index(
800 muxer_msg_iter->active_muxer_upstream_msg_iters,
801 i);
d6e69534
PP
802 int64_t msg_ts_ns;
803
804 if (!cur_muxer_upstream_msg_iter->msg_iter) {
805 /* This upstream message iterator is ended */
ef267d12 806 BT_COMP_LOGT("Skipping ended upstream message iterator: "
d6e69534
PP
807 "muxer-upstream-msg-iter-wrap-addr=%p",
808 cur_muxer_upstream_msg_iter);
958f7d11
PP
809 continue;
810 }
811
d6e69534
PP
812 BT_ASSERT(cur_muxer_upstream_msg_iter->msgs->length > 0);
813 msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs);
814 BT_ASSERT(msg);
60f3d027 815
91d81473 816 if (G_UNLIKELY(bt_message_get_type(msg) ==
60f3d027
PP
817 BT_MESSAGE_TYPE_STREAM_BEGINNING)) {
818 ret = validate_new_stream_clock_class(
819 muxer_msg_iter, muxer_comp,
820 bt_message_stream_beginning_borrow_stream_const(
821 msg));
822 if (ret) {
823 /*
824 * validate_new_stream_clock_class() logs
825 * errors.
826 */
d24d5663 827 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
60f3d027
PP
828 goto end;
829 }
91d81473 830 } else if (G_UNLIKELY(bt_message_get_type(msg) ==
60f3d027
PP
831 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY)) {
832 const bt_clock_snapshot *cs;
60f3d027 833
0cbc2c33
PP
834 cs = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
835 msg);
60f3d027
PP
836 ret = validate_clock_class(muxer_msg_iter, muxer_comp,
837 bt_clock_snapshot_borrow_clock_class_const(cs));
838 if (ret) {
839 /* validate_clock_class() logs errors */
d24d5663 840 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
60f3d027
PP
841 goto end;
842 }
843 }
844
d6e69534
PP
845 ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg,
846 muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns);
958f7d11 847 if (ret) {
d6e69534
PP
848 /* get_msg_ts_ns() logs errors */
849 *muxer_upstream_msg_iter = NULL;
d24d5663 850 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
958f7d11
PP
851 goto end;
852 }
853
e5b2784a
FD
854 /*
855 * Update the current message iterator if it has not been set
856 * yet, or if its current message has a timestamp smaller than
857 * the previously selected youngest message.
858 */
859 if (G_UNLIKELY(*muxer_upstream_msg_iter == NULL) ||
860 msg_ts_ns < youngest_ts_ns) {
d6e69534
PP
861 *muxer_upstream_msg_iter =
862 cur_muxer_upstream_msg_iter;
863 youngest_ts_ns = msg_ts_ns;
958f7d11 864 *ts_ns = youngest_ts_ns;
6915f47a
FD
865 } else if (msg_ts_ns == youngest_ts_ns) {
866 /*
867 * The currently selected message to be sent downstream
868 * next has the exact same timestamp that of the
869 * current candidate message. We must break the tie
870 * in a predictable manner.
871 */
872 const bt_message *selected_msg = g_queue_peek_head(
873 (*muxer_upstream_msg_iter)->msgs);
874 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
875
876 /*
877 * Order the messages in an arbitrary but determinitic
878 * way.
879 */
1aca5200 880 ret = common_muxing_compare_messages(msg, selected_msg);
6915f47a
FD
881 if (ret < 0) {
882 /*
883 * The `msg` should go first. Update the next
884 * iterator and the current timestamp.
885 */
886 *muxer_upstream_msg_iter =
887 cur_muxer_upstream_msg_iter;
888 youngest_ts_ns = msg_ts_ns;
889 *ts_ns = youngest_ts_ns;
890 } else if (ret == 0) {
891 /* Unable to pick which one should go first. */
892 BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
893 "muxer-upstream-msg-iter-wrap-addr=%p"
894 "cur-muxer-upstream-msg-iter-wrap-addr=%p",
895 *muxer_upstream_msg_iter,
896 cur_muxer_upstream_msg_iter);
897 }
958f7d11
PP
898 }
899 }
900
d6e69534 901 if (!*muxer_upstream_msg_iter) {
d24d5663 902 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
958f7d11
PP
903 *ts_ns = INT64_MIN;
904 }
905
906end:
907 return status;
908}
909
910static
d24d5663
PP
911bt_component_class_message_iterator_next_method_status
912validate_muxer_upstream_msg_iter(
54bdc1f7
PP
913 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
914 bool *is_ended)
958f7d11 915{
87ec3926
PP
916 struct muxer_comp *muxer_comp =
917 muxer_upstream_msg_iter->muxer_comp;
d24d5663
PP
918 bt_component_class_message_iterator_next_method_status status =
919 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
958f7d11 920
5b6473ec 921 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
d6e69534
PP
922 "muxer-upstream-msg-iter-wrap-addr=%p",
923 muxer_upstream_msg_iter);
fed72692 924
d6e69534
PP
925 if (muxer_upstream_msg_iter->msgs->length > 0 ||
926 !muxer_upstream_msg_iter->msg_iter) {
5b6473ec 927 BT_COMP_LOGD("Already valid or not considered: "
d6e69534
PP
928 "queue-len=%u, upstream-msg-iter-addr=%p",
929 muxer_upstream_msg_iter->msgs->length,
930 muxer_upstream_msg_iter->msg_iter);
ab11110e
PP
931 goto end;
932 }
933
d6e69534 934 /* muxer_upstream_msg_iter_next() logs details/errors */
54bdc1f7
PP
935 status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter,
936 is_ended);
089717de
PP
937
938end:
939 return status;
940}
941
942static
d24d5663
PP
943bt_component_class_message_iterator_next_method_status
944validate_muxer_upstream_msg_iters(
d6e69534 945 struct muxer_msg_iter *muxer_msg_iter)
089717de 946{
87ec3926 947 struct muxer_comp *muxer_comp = muxer_msg_iter->muxer_comp;
d24d5663
PP
948 bt_component_class_message_iterator_next_method_status status =
949 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
089717de
PP
950 size_t i;
951
5b6473ec 952 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
d6e69534 953 "muxer-msg-iter-addr=%p", muxer_msg_iter);
fed72692 954
54bdc1f7
PP
955 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
956 i++) {
6bf2abbd 957 bool is_ended = false;
d6e69534 958 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
089717de 959 g_ptr_array_index(
54bdc1f7 960 muxer_msg_iter->active_muxer_upstream_msg_iters,
089717de
PP
961 i);
962
d6e69534 963 status = validate_muxer_upstream_msg_iter(
54bdc1f7 964 muxer_upstream_msg_iter, &is_ended);
d24d5663 965 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
fed72692 966 if (status < 0) {
5b6473ec 967 BT_COMP_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
d6e69534
PP
968 "muxer-msg-iter-addr=%p, "
969 "muxer-upstream-msg-iter-wrap-addr=%p",
970 muxer_msg_iter,
971 muxer_upstream_msg_iter);
fed72692 972 } else {
5b6473ec 973 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
d6e69534
PP
974 "muxer-msg-iter-addr=%p, "
975 "muxer-upstream-msg-iter-wrap-addr=%p",
976 muxer_msg_iter,
977 muxer_upstream_msg_iter);
fed72692
PP
978 }
979
089717de
PP
980 goto end;
981 }
744ba28b
PP
982
983 /*
54bdc1f7
PP
984 * Move this muxer upstream message iterator to the
985 * array of ended iterators if it's ended.
744ba28b 986 */
91d81473 987 if (G_UNLIKELY(is_ended)) {
5b6473ec 988 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
54bdc1f7
PP
989 "muxer-msg-iter-addr=%p, "
990 "muxer-upstream-msg-iter-wrap-addr=%p",
991 muxer_msg_iter, muxer_upstream_msg_iter);
992 g_ptr_array_add(
993 muxer_msg_iter->ended_muxer_upstream_msg_iters,
994 muxer_upstream_msg_iter);
995 muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i] = NULL;
996
744ba28b
PP
997 /*
998 * Use g_ptr_array_remove_fast() because the
999 * order of those elements is not important.
1000 */
1001 g_ptr_array_remove_index_fast(
54bdc1f7 1002 muxer_msg_iter->active_muxer_upstream_msg_iters,
744ba28b
PP
1003 i);
1004 i--;
1005 }
089717de
PP
1006 }
1007
1008end:
1009 return status;
1010}
1011
d4393e08 1012static inline
d24d5663 1013bt_component_class_message_iterator_next_method_status muxer_msg_iter_do_next_one(
089717de 1014 struct muxer_comp *muxer_comp,
d6e69534
PP
1015 struct muxer_msg_iter *muxer_msg_iter,
1016 const bt_message **msg)
089717de 1017{
d24d5663 1018 bt_component_class_message_iterator_next_method_status status;
d6e69534 1019 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL;
089717de
PP
1020 int64_t next_return_ts;
1021
5badd463 1022 status = validate_muxer_upstream_msg_iters(muxer_msg_iter);
d24d5663 1023 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
5badd463
PP
1024 /* validate_muxer_upstream_msg_iters() logs details */
1025 goto end;
958f7d11
PP
1026 }
1027
1028 /*
089717de 1029 * At this point we know that all the existing upstream
d6e69534
PP
1030 * message iterators are valid. We can find the one,
1031 * amongst those, of which the current message is the
089717de 1032 * youngest.
958f7d11 1033 */
d6e69534
PP
1034 status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp,
1035 muxer_msg_iter, &muxer_upstream_msg_iter,
089717de 1036 &next_return_ts);
d24d5663 1037 if (status < 0 || status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END) {
d4393e08 1038 if (status < 0) {
5b6473ec 1039 BT_COMP_LOGE("Cannot find the youngest upstream message iterator wrapper: "
fed72692 1040 "status=%s",
d24d5663 1041 bt_common_func_status_string(status));
fed72692 1042 } else {
5b6473ec 1043 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
fed72692 1044 "status=%s",
d24d5663 1045 bt_common_func_status_string(status));
fed72692
PP
1046 }
1047
958f7d11
PP
1048 goto end;
1049 }
1050
d6e69534 1051 if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) {
5b6473ec 1052 BT_COMP_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
d6e69534 1053 "muxer-msg-iter-addr=%p, ts=%" PRId64 ", "
fed72692 1054 "last-returned-ts=%" PRId64,
d6e69534
PP
1055 muxer_msg_iter, next_return_ts,
1056 muxer_msg_iter->last_returned_ts_ns);
d24d5663 1057 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
958f7d11
PP
1058 goto end;
1059 }
1060
5b6473ec 1061 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
d6e69534
PP
1062 "muxer-msg-iter-addr=%p, "
1063 "muxer-upstream-msg-iter-wrap-addr=%p, "
fed72692 1064 "ts=%" PRId64,
d6e69534 1065 muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts);
d24d5663 1066 BT_ASSERT(status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK);
d6e69534 1067 BT_ASSERT(muxer_upstream_msg_iter);
958f7d11
PP
1068
1069 /*
d4393e08 1070 * Consume from the queue's head: other side
d6e69534 1071 * (muxer_upstream_msg_iter_next()) writes to the tail.
958f7d11 1072 */
d6e69534
PP
1073 *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs);
1074 BT_ASSERT(*msg);
1075 muxer_msg_iter->last_returned_ts_ns = next_return_ts;
958f7d11
PP
1076
1077end:
d4393e08
PP
1078 return status;
1079}
1080
1081static
d24d5663 1082bt_component_class_message_iterator_next_method_status muxer_msg_iter_do_next(
d4393e08 1083 struct muxer_comp *muxer_comp,
d6e69534
PP
1084 struct muxer_msg_iter *muxer_msg_iter,
1085 bt_message_array_const msgs, uint64_t capacity,
d4393e08
PP
1086 uint64_t *count)
1087{
d24d5663
PP
1088 bt_component_class_message_iterator_next_method_status status =
1089 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
d4393e08
PP
1090 uint64_t i = 0;
1091
d24d5663 1092 while (i < capacity && status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
d6e69534
PP
1093 status = muxer_msg_iter_do_next_one(muxer_comp,
1094 muxer_msg_iter, &msgs[i]);
d24d5663 1095 if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
d4393e08
PP
1096 i++;
1097 }
1098 }
1099
1100 if (i > 0) {
1101 /*
d6e69534 1102 * Even if muxer_msg_iter_do_next_one() returned
d4393e08 1103 * something else than
d6e69534
PP
1104 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1105 * message objects in the output message
d4393e08 1106 * array, so we need to return
d6e69534 1107 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
d4393e08 1108 * transfered to downstream. This other status occurs
d6e69534 1109 * again the next time muxer_msg_iter_do_next() is
d4393e08 1110 * called, possibly without any accumulated
d6e69534 1111 * message, in which case we'll return it.
d4393e08
PP
1112 */
1113 *count = i;
d24d5663 1114 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
d4393e08
PP
1115 }
1116
1117 return status;
958f7d11
PP
1118}
1119
1120static
d6e69534 1121void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter)
ab11110e 1122{
87ec3926
PP
1123 struct muxer_comp *muxer_comp;
1124
d6e69534 1125 if (!muxer_msg_iter) {
ab11110e
PP
1126 return;
1127 }
1128
87ec3926 1129 muxer_comp = muxer_msg_iter->muxer_comp;
5b6473ec 1130 BT_COMP_LOGD("Destroying muxer component's message iterator: "
d6e69534 1131 "muxer-msg-iter-addr=%p", muxer_msg_iter);
fed72692 1132
54bdc1f7 1133 if (muxer_msg_iter->active_muxer_upstream_msg_iters) {
5b6473ec 1134 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
54bdc1f7
PP
1135 g_ptr_array_free(
1136 muxer_msg_iter->active_muxer_upstream_msg_iters, TRUE);
1137 }
1138
1139 if (muxer_msg_iter->ended_muxer_upstream_msg_iters) {
5b6473ec 1140 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
ab11110e 1141 g_ptr_array_free(
54bdc1f7 1142 muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE);
ab11110e
PP
1143 }
1144
d6e69534 1145 g_free(muxer_msg_iter);
ab11110e
PP
1146}
1147
1148static
e803df70
SM
1149bt_component_class_message_iterator_init_method_status
1150muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
d6e69534 1151 struct muxer_msg_iter *muxer_msg_iter)
958f7d11 1152{
544d0515
PP
1153 int64_t count;
1154 int64_t i;
e803df70 1155 bt_component_class_message_iterator_init_method_status status;
958f7d11 1156
d94d92ac 1157 count = bt_component_filter_get_input_port_count(
707b7d35 1158 bt_self_component_filter_as_component_filter(
5b6473ec 1159 muxer_comp->self_comp_flt));
544d0515 1160 if (count < 0) {
5b6473ec 1161 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
d6e69534
PP
1162 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1163 muxer_comp, muxer_msg_iter);
e803df70 1164 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
ab11110e
PP
1165 goto end;
1166 }
958f7d11
PP
1167
1168 for (i = 0; i < count; i++) {
5badd463 1169 bt_self_component_port_input_message_iterator *upstream_msg_iter;
b19ff26f 1170 bt_self_component_port_input *self_port =
d94d92ac 1171 bt_self_component_filter_borrow_input_port_by_index(
5b6473ec 1172 muxer_comp->self_comp_flt, i);
b19ff26f 1173 const bt_port *port;
e803df70
SM
1174 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
1175 msg_iter_status;
1176 int int_status;
958f7d11 1177
d94d92ac 1178 BT_ASSERT(self_port);
707b7d35
PP
1179 port = bt_self_component_port_as_port(
1180 bt_self_component_port_input_as_self_component_port(
d94d92ac 1181 self_port));
f6ccaed9 1182 BT_ASSERT(port);
958f7d11
PP
1183
1184 if (!bt_port_is_connected(port)) {
5badd463 1185 /* Skip non-connected port */
958f7d11
PP
1186 continue;
1187 }
1188
e803df70
SM
1189 msg_iter_status = create_msg_iter_on_input_port(muxer_comp,
1190 muxer_msg_iter, self_port, &upstream_msg_iter);
1191 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
5badd463 1192 /* create_msg_iter_on_input_port() logs errors */
e803df70 1193 status = (int) msg_iter_status;
ab11110e 1194 goto end;
958f7d11 1195 }
fed72692 1196
e803df70 1197 int_status = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter,
c61018b9 1198 upstream_msg_iter);
5badd463
PP
1199 bt_self_component_port_input_message_iterator_put_ref(
1200 upstream_msg_iter);
e803df70
SM
1201 if (int_status) {
1202 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
5badd463 1203 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
5badd463
PP
1204 goto end;
1205 }
958f7d11
PP
1206 }
1207
e803df70
SM
1208 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
1209
958f7d11 1210end:
e803df70 1211 return status;
958f7d11
PP
1212}
1213
958f7d11 1214BT_HIDDEN
d24d5663 1215bt_component_class_message_iterator_init_method_status muxer_msg_iter_init(
d6e69534 1216 bt_self_message_iterator *self_msg_iter,
8d8b141d 1217 bt_self_message_iterator_configuration *config,
b19ff26f
PP
1218 bt_self_component_filter *self_comp,
1219 bt_self_component_port_output *port)
958f7d11
PP
1220{
1221 struct muxer_comp *muxer_comp = NULL;
d6e69534 1222 struct muxer_msg_iter *muxer_msg_iter = NULL;
e803df70 1223 bt_component_class_message_iterator_init_method_status status;
958f7d11 1224
d94d92ac 1225 muxer_comp = bt_self_component_get_data(
707b7d35 1226 bt_self_component_filter_as_self_component(self_comp));
f6ccaed9 1227 BT_ASSERT(muxer_comp);
5b6473ec 1228 BT_COMP_LOGD("Initializing muxer component's message iterator: "
d6e69534
PP
1229 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1230 self_comp, muxer_comp, self_msg_iter);
a09c6b95 1231
d6e69534 1232 if (muxer_comp->initializing_muxer_msg_iter) {
a09c6b95 1233 /*
089717de 1234 * Weird, unhandled situation detected: downstream
d6e69534
PP
1235 * creates a muxer message iterator while creating
1236 * another muxer message iterator (same component).
a09c6b95 1237 */
5b6473ec 1238 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
d6e69534
PP
1239 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1240 self_comp, muxer_comp, self_msg_iter);
e803df70 1241 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
958f7d11
PP
1242 goto error;
1243 }
1244
d6e69534
PP
1245 muxer_comp->initializing_muxer_msg_iter = true;
1246 muxer_msg_iter = g_new0(struct muxer_msg_iter, 1);
1247 if (!muxer_msg_iter) {
5b6473ec 1248 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
e803df70 1249 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
ab11110e
PP
1250 goto error;
1251 }
1252
87ec3926 1253 muxer_msg_iter->muxer_comp = muxer_comp;
ca02df0a 1254 muxer_msg_iter->self_msg_iter = self_msg_iter;
d6e69534 1255 muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
54bdc1f7 1256 muxer_msg_iter->active_muxer_upstream_msg_iters =
958f7d11 1257 g_ptr_array_new_with_free_func(
d6e69534 1258 (GDestroyNotify) destroy_muxer_upstream_msg_iter);
54bdc1f7 1259 if (!muxer_msg_iter->active_muxer_upstream_msg_iters) {
5b6473ec 1260 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
e803df70 1261 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
54bdc1f7
PP
1262 goto error;
1263 }
1264
1265 muxer_msg_iter->ended_muxer_upstream_msg_iters =
1266 g_ptr_array_new_with_free_func(
1267 (GDestroyNotify) destroy_muxer_upstream_msg_iter);
1268 if (!muxer_msg_iter->ended_muxer_upstream_msg_iters) {
5b6473ec 1269 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
e803df70 1270 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
958f7d11
PP
1271 goto error;
1272 }
1273
e803df70 1274 status = muxer_msg_iter_init_upstream_iterators(muxer_comp,
d6e69534 1275 muxer_msg_iter);
e803df70 1276 if (status) {
5b6473ec 1277 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
fed72692 1278 "comp-addr=%p, muxer-comp-addr=%p, "
d6e69534
PP
1279 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1280 self_comp, muxer_comp, muxer_msg_iter,
e803df70 1281 self_msg_iter, status);
a09c6b95
PP
1282 goto error;
1283 }
1284
5badd463 1285 bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter);
5b6473ec 1286 BT_COMP_LOGD("Initialized muxer component's message iterator: "
d6e69534
PP
1287 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1288 "msg-iter-addr=%p",
1289 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
958f7d11
PP
1290 goto end;
1291
1292error:
d6e69534 1293 destroy_muxer_msg_iter(muxer_msg_iter);
5badd463 1294 bt_self_message_iterator_set_data(self_msg_iter, NULL);
958f7d11
PP
1295
1296end:
d6e69534 1297 muxer_comp->initializing_muxer_msg_iter = false;
958f7d11
PP
1298 return status;
1299}
1300
1301BT_HIDDEN
54bdc1f7 1302void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
958f7d11 1303{
d6e69534
PP
1304 struct muxer_msg_iter *muxer_msg_iter =
1305 bt_self_message_iterator_get_data(self_msg_iter);
b19ff26f 1306 bt_self_component *self_comp = NULL;
958f7d11
PP
1307 struct muxer_comp *muxer_comp = NULL;
1308
d6e69534
PP
1309 self_comp = bt_self_message_iterator_borrow_component(
1310 self_msg_iter);
d94d92ac
PP
1311 BT_ASSERT(self_comp);
1312 muxer_comp = bt_self_component_get_data(self_comp);
5b6473ec 1313 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
d6e69534
PP
1314 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1315 "msg-iter-addr=%p",
1316 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
958f7d11 1317
5badd463 1318 if (muxer_msg_iter) {
d6e69534 1319 destroy_muxer_msg_iter(muxer_msg_iter);
958f7d11 1320 }
958f7d11
PP
1321}
1322
1323BT_HIDDEN
d24d5663 1324bt_component_class_message_iterator_next_method_status muxer_msg_iter_next(
d6e69534
PP
1325 bt_self_message_iterator *self_msg_iter,
1326 bt_message_array_const msgs, uint64_t capacity,
d4393e08 1327 uint64_t *count)
958f7d11 1328{
d24d5663 1329 bt_component_class_message_iterator_next_method_status status;
d6e69534
PP
1330 struct muxer_msg_iter *muxer_msg_iter =
1331 bt_self_message_iterator_get_data(self_msg_iter);
b19ff26f 1332 bt_self_component *self_comp = NULL;
958f7d11 1333 struct muxer_comp *muxer_comp = NULL;
958f7d11 1334
d6e69534
PP
1335 BT_ASSERT(muxer_msg_iter);
1336 self_comp = bt_self_message_iterator_borrow_component(
1337 self_msg_iter);
d94d92ac
PP
1338 BT_ASSERT(self_comp);
1339 muxer_comp = bt_self_component_get_data(self_comp);
f6ccaed9 1340 BT_ASSERT(muxer_comp);
ef267d12 1341 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
d6e69534
PP
1342 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1343 "msg-iter-addr=%p",
1344 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
fed72692 1345
d6e69534
PP
1346 status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter,
1347 msgs, capacity, count);
d4393e08 1348 if (status < 0) {
5b6473ec 1349 BT_COMP_LOGE("Cannot get next message: "
d6e69534
PP
1350 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1351 "msg-iter-addr=%p, status=%s",
1352 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter,
d24d5663 1353 bt_common_func_status_string(status));
fed72692 1354 } else {
ef267d12 1355 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
d4393e08 1356 "status=%s",
d24d5663 1357 bt_common_func_status_string(status));
fed72692 1358 }
958f7d11 1359
d4393e08 1360 return status;
958f7d11
PP
1361}
1362
1363BT_HIDDEN
d24d5663 1364bt_component_class_port_connected_method_status muxer_input_port_connected(
b19ff26f
PP
1365 bt_self_component_filter *self_comp,
1366 bt_self_component_port_input *self_port,
1367 const bt_port_output *other_port)
958f7d11 1368{
d24d5663
PP
1369 bt_component_class_port_connected_method_status status =
1370 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
1371 bt_self_component_add_port_status add_port_status;
87ec3926
PP
1372 struct muxer_comp *muxer_comp = bt_self_component_get_data(
1373 bt_self_component_filter_as_self_component(self_comp));
958f7d11 1374
d24d5663
PP
1375 add_port_status = add_available_input_port(self_comp);
1376 if (add_port_status) {
5b6473ec 1377 BT_COMP_LOGE("Cannot add one muxer component's input port: "
5badd463 1378 "status=%s",
d24d5663
PP
1379 bt_common_func_status_string(status));
1380
1381 if (add_port_status ==
1382 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
1383 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR;
1384 } else {
1385 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
1386 }
1387
06a2cb0d
PP
1388 goto end;
1389 }
1390
958f7d11 1391end:
bf55043c 1392 return status;
958f7d11 1393}
b5443165 1394
54bdc1f7 1395static inline
f2fb1b32
SM
1396bt_component_class_message_iterator_can_seek_beginning_method_status
1397muxer_upstream_msg_iters_can_all_seek_beginning(
1398 GPtrArray *muxer_upstream_msg_iters, bt_bool *can_seek)
b5443165 1399{
c4b56970
PP
1400 bt_component_class_message_iterator_can_seek_beginning_method_status status =
1401 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
b5443165 1402 uint64_t i;
b5443165 1403
54bdc1f7 1404 for (i = 0; i < muxer_upstream_msg_iters->len; i++) {
b5443165 1405 struct muxer_upstream_msg_iter *upstream_msg_iter =
54bdc1f7 1406 muxer_upstream_msg_iters->pdata[i];
f2fb1b32
SM
1407 status = (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1408 upstream_msg_iter->msg_iter, can_seek);
1409 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK) {
1410 goto end;
1411 }
b5443165 1412
f2fb1b32 1413 if (!*can_seek) {
b5443165
PP
1414 goto end;
1415 }
1416 }
1417
f2fb1b32
SM
1418 *can_seek = BT_TRUE;
1419
b5443165 1420end:
f2fb1b32 1421 return status;
b5443165
PP
1422}
1423
54bdc1f7 1424BT_HIDDEN
f2fb1b32
SM
1425bt_component_class_message_iterator_can_seek_beginning_method_status
1426muxer_msg_iter_can_seek_beginning(
1427 bt_self_message_iterator *self_msg_iter, bt_bool *can_seek)
54bdc1f7
PP
1428{
1429 struct muxer_msg_iter *muxer_msg_iter =
1430 bt_self_message_iterator_get_data(self_msg_iter);
f2fb1b32 1431 bt_component_class_message_iterator_can_seek_beginning_method_status status;
54bdc1f7 1432
f2fb1b32
SM
1433 status = muxer_upstream_msg_iters_can_all_seek_beginning(
1434 muxer_msg_iter->active_muxer_upstream_msg_iters, can_seek);
1435 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK) {
54bdc1f7
PP
1436 goto end;
1437 }
1438
f2fb1b32 1439 if (!*can_seek) {
54bdc1f7
PP
1440 goto end;
1441 }
1442
f2fb1b32
SM
1443 status = muxer_upstream_msg_iters_can_all_seek_beginning(
1444 muxer_msg_iter->ended_muxer_upstream_msg_iters, can_seek);
1445
54bdc1f7 1446end:
f2fb1b32 1447 return status;
54bdc1f7
PP
1448}
1449
b5443165 1450BT_HIDDEN
d24d5663 1451bt_component_class_message_iterator_seek_beginning_method_status muxer_msg_iter_seek_beginning(
b5443165
PP
1452 bt_self_message_iterator *self_msg_iter)
1453{
1454 struct muxer_msg_iter *muxer_msg_iter =
1455 bt_self_message_iterator_get_data(self_msg_iter);
d24d5663
PP
1456 bt_component_class_message_iterator_seek_beginning_method_status status =
1457 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
1458 bt_message_iterator_seek_beginning_status seek_beg_status;
b5443165
PP
1459 uint64_t i;
1460
54bdc1f7
PP
1461 /* Seek all ended upstream iterators first */
1462 for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
1463 i++) {
b5443165 1464 struct muxer_upstream_msg_iter *upstream_msg_iter =
54bdc1f7 1465 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
b5443165 1466
d24d5663 1467 seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning(
b5443165 1468 upstream_msg_iter->msg_iter);
d24d5663
PP
1469 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
1470 status = (int) seek_beg_status;
b5443165
PP
1471 goto end;
1472 }
54bdc1f7
PP
1473
1474 empty_message_queue(upstream_msg_iter);
1475 }
1476
1477 /* Seek all previously active upstream iterators */
1478 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
1479 i++) {
1480 struct muxer_upstream_msg_iter *upstream_msg_iter =
1481 muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i];
1482
d24d5663 1483 seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning(
54bdc1f7 1484 upstream_msg_iter->msg_iter);
d24d5663
PP
1485 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
1486 status = (int) seek_beg_status;
54bdc1f7
PP
1487 goto end;
1488 }
1489
1490 empty_message_queue(upstream_msg_iter);
1491 }
1492
1493 /* Make them all active */
1494 for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
1495 i++) {
1496 struct muxer_upstream_msg_iter *upstream_msg_iter =
1497 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
1498
1499 g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
1500 upstream_msg_iter);
1501 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL;
b5443165
PP
1502 }
1503
3625612b
MJ
1504 /*
1505 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1506 * called on an empty array.
1507 */
1508 if (muxer_msg_iter->ended_muxer_upstream_msg_iters->len > 0) {
1509 g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters,
1510 0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len);
1511 }
b5443165 1512 muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
54bdc1f7
PP
1513 muxer_msg_iter->clock_class_expectation =
1514 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY;
b5443165
PP
1515
1516end:
d24d5663 1517 return status;
b5443165 1518}
This page took 0.138846 seconds and 4 git commands to generate.