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