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