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