2 * SPDX-License-Identifier: MIT
4 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
7 #define BT_COMP_LOG_SELF_COMP (muxer_comp->self_comp)
8 #define BT_LOG_OUTPUT_LEVEL (muxer_comp->log_level)
9 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.MUXER"
10 #include "logging/comp-logging.h"
12 #include "common/macros.h"
13 #include "common/uuid.h"
14 #include <babeltrace2/babeltrace.h>
18 #include "common/assert.h"
19 #include "common/common.h"
23 #include "plugins/common/muxing/muxing.h"
24 #include "plugins/common/param-validation/param-validation.h"
30 bt_self_component_filter
*self_comp_flt
;
31 bt_self_component
*self_comp
;
33 unsigned int next_port_num
;
34 size_t available_input_ports
;
35 bool initializing_muxer_msg_iter
;
36 bt_logging_level log_level
;
39 struct muxer_upstream_msg_iter
{
40 struct muxer_comp
*muxer_comp
;
42 /* Owned by this, NULL if ended */
43 bt_message_iterator
*msg_iter
;
45 /* Contains `const bt_message *`, owned by this */
48 /* Index of the next message in `msgs` to return */
52 enum muxer_msg_iter_clock_class_expectation
{
53 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
= 0,
54 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
,
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
,
60 struct muxer_msg_iter
{
61 struct muxer_comp
*muxer_comp
;
64 bt_self_message_iterator
*self_msg_iter
;
67 * Array of struct muxer_upstream_msg_iter * (owned by this).
69 * NOTE: This array is searched in linearly to find the youngest
70 * current message. Keep this until benchmarks confirm that
71 * another data structure is faster than this for our typical
74 GPtrArray
*active_muxer_upstream_msg_iters
;
77 * Array of struct muxer_upstream_msg_iter * (owned by this).
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.
83 GPtrArray
*ended_muxer_upstream_msg_iters
;
85 /* Last time returned in a message */
86 int64_t last_returned_ts_ns
;
88 /* Clock class expectation state */
89 enum muxer_msg_iter_clock_class_expectation clock_class_expectation
;
92 * Expected clock class UUID, only valid when
93 * clock_class_expectation is
94 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
96 bt_uuid_t expected_clock_class_uuid
;
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.
103 bt_message_iterator_class_next_method_status next_saved_status
;
104 const struct bt_error
*next_saved_error
;
108 void empty_message_queue(struct muxer_upstream_msg_iter
*upstream_msg_iter
)
110 g_ptr_array_set_size(upstream_msg_iter
->msgs
, 0);
114 void destroy_muxer_upstream_msg_iter(
115 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
)
117 struct muxer_comp
*muxer_comp
;
119 if (!muxer_upstream_msg_iter
) {
123 muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
124 BT_COMP_LOGD("Destroying muxer's upstream message iterator wrapper: "
125 "addr=%p, msg-iter-addr=%p, queue-len=%u, next-msg=%u",
126 muxer_upstream_msg_iter
,
127 muxer_upstream_msg_iter
->msg_iter
,
128 muxer_upstream_msg_iter
->msgs
->len
,
129 muxer_upstream_msg_iter
->next_msg
);
131 bt_message_iterator_put_ref(
132 muxer_upstream_msg_iter
->msg_iter
);
134 if (muxer_upstream_msg_iter
->msgs
) {
135 g_ptr_array_free(muxer_upstream_msg_iter
->msgs
, TRUE
);
138 g_free(muxer_upstream_msg_iter
);
142 int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
,
143 bt_message_iterator
*self_msg_iter
)
146 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
147 g_new0(struct muxer_upstream_msg_iter
, 1);
148 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
150 if (!muxer_upstream_msg_iter
) {
151 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
152 "Failed to allocate one muxer's upstream message iterator wrapper.");
156 muxer_upstream_msg_iter
->muxer_comp
= muxer_comp
;
157 muxer_upstream_msg_iter
->msg_iter
= self_msg_iter
;
158 bt_message_iterator_get_ref(muxer_upstream_msg_iter
->msg_iter
);
159 muxer_upstream_msg_iter
->msgs
=
160 g_ptr_array_new_with_free_func((GDestroyNotify
) bt_message_put_ref
);
161 if (!muxer_upstream_msg_iter
->msgs
) {
162 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
163 "Failed to allocate a GPtrArray.");
167 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
168 muxer_upstream_msg_iter
);
169 BT_COMP_LOGD("Added muxer's upstream message iterator wrapper: "
170 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
171 muxer_upstream_msg_iter
, muxer_msg_iter
,
177 destroy_muxer_upstream_msg_iter(muxer_upstream_msg_iter
);
185 bt_self_component_add_port_status
add_available_input_port(
186 bt_self_component_filter
*self_comp
)
188 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
189 bt_self_component_filter_as_self_component(self_comp
));
190 bt_self_component_add_port_status status
=
191 BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
;
192 GString
*port_name
= NULL
;
194 BT_ASSERT(muxer_comp
);
195 port_name
= g_string_new("in");
197 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
, "Failed to allocate a GString.");
198 status
= BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
;
202 g_string_append_printf(port_name
, "%u", muxer_comp
->next_port_num
);
203 status
= bt_self_component_filter_add_input_port(
204 self_comp
, port_name
->str
, NULL
, NULL
);
205 if (status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
206 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
207 "Cannot add input port to muxer component: "
208 "port-name=\"%s\", comp-addr=%p, status=%s",
209 port_name
->str
, self_comp
,
210 bt_common_func_status_string(status
));
214 muxer_comp
->available_input_ports
++;
215 muxer_comp
->next_port_num
++;
216 BT_COMP_LOGI("Added one input port to muxer component: "
217 "port-name=\"%s\", comp-addr=%p",
218 port_name
->str
, self_comp
);
222 g_string_free(port_name
, TRUE
);
229 bt_self_component_add_port_status
create_output_port(
230 bt_self_component_filter
*self_comp
)
232 return bt_self_component_filter_add_output_port(
233 self_comp
, "out", NULL
, NULL
);
237 void destroy_muxer_comp(struct muxer_comp
*muxer_comp
)
247 struct bt_param_validation_map_value_entry_descr muxer_params
[] = {
248 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
251 bt_component_class_initialize_method_status
muxer_init(
252 bt_self_component_filter
*self_comp_flt
,
253 bt_self_component_filter_configuration
*config
__attribute__((unused
)),
254 const bt_value
*params
,
255 void *init_data
__attribute__((unused
)))
257 bt_component_class_initialize_method_status status
;
258 bt_self_component_add_port_status add_port_status
;
259 bt_self_component
*self_comp
=
260 bt_self_component_filter_as_self_component(self_comp_flt
);
261 struct muxer_comp
*muxer_comp
= g_new0(struct muxer_comp
, 1);
262 bt_logging_level log_level
= bt_component_get_logging_level(
263 bt_self_component_as_component(self_comp
));
264 enum bt_param_validation_status validation_status
;
265 gchar
*validate_error
= NULL
;
267 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
268 "Initializing muxer component: "
269 "comp-addr=%p, params-addr=%p", self_comp
, params
);
273 * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `muxer_comp` is not
276 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
277 "Failed to allocate one muxer component.");
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
;
284 muxer_comp
->log_level
= log_level
;
285 muxer_comp
->self_comp
= self_comp
;
286 muxer_comp
->self_comp_flt
= self_comp_flt
;
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
;
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
);
299 bt_self_component_set_data(self_comp
, muxer_comp
);
300 add_port_status
= add_available_input_port(self_comp_flt
);
301 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
302 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
303 "Cannot ensure that at least one muxer component's input port is available: "
304 "muxer-comp-addr=%p, status=%s",
305 muxer_comp
, bt_common_func_status_string(add_port_status
));
306 status
= (int) add_port_status
;
310 add_port_status
= create_output_port(self_comp_flt
);
311 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
312 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
313 "Cannot create muxer component's output port: "
314 "muxer-comp-addr=%p, status=%s",
315 muxer_comp
, bt_common_func_status_string(add_port_status
));
316 status
= (int) add_port_status
;
320 BT_COMP_LOGI("Initialized muxer component: "
321 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
322 self_comp
, params
, muxer_comp
);
324 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
328 destroy_muxer_comp(muxer_comp
);
329 bt_self_component_set_data(self_comp
, NULL
);
332 g_free(validate_error
);
336 void muxer_finalize(bt_self_component_filter
*self_comp
)
338 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
339 bt_self_component_filter_as_self_component(self_comp
));
341 BT_COMP_LOGI("Finalizing muxer component: comp-addr=%p",
343 destroy_muxer_comp(muxer_comp
);
347 bt_message_iterator_create_from_message_iterator_status
348 create_msg_iter_on_input_port(struct muxer_comp
*muxer_comp
,
349 struct muxer_msg_iter
*muxer_msg_iter
,
350 bt_self_component_port_input
*self_port
,
351 bt_message_iterator
**msg_iter
)
353 const bt_port
*port
= bt_self_component_port_as_port(
354 bt_self_component_port_input_as_self_component_port(
356 bt_message_iterator_create_from_message_iterator_status
360 BT_ASSERT(bt_port_is_connected(port
));
362 // TODO: Advance the iterator to >= the time of the latest
363 // returned message by the muxer message
364 // iterator which creates it.
365 status
= bt_message_iterator_create_from_message_iterator(
366 muxer_msg_iter
->self_msg_iter
, self_port
, msg_iter
);
367 if (status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
368 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
369 "Cannot create upstream message iterator on input port: "
370 "port-addr=%p, port-name=\"%s\"",
371 port
, bt_port_get_name(port
));
375 BT_COMP_LOGI("Created upstream message iterator on input port: "
376 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
377 port
, bt_port_get_name(port
), msg_iter
);
384 bt_message_iterator_class_next_method_status
muxer_upstream_msg_iter_next(
385 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
388 struct muxer_comp
*muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
389 bt_message_iterator_class_next_method_status status
;
390 bt_message_iterator_next_status input_port_iter_status
;
391 bt_message_array_const msgs
;
395 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
396 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
397 muxer_upstream_msg_iter
,
398 muxer_upstream_msg_iter
->msg_iter
);
399 input_port_iter_status
= bt_message_iterator_next(
400 muxer_upstream_msg_iter
->msg_iter
, &msgs
, &count
);
401 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
403 bt_common_func_status_string(input_port_iter_status
));
405 switch (input_port_iter_status
) {
406 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
408 * Message iterator's current message is
409 * valid: it must be considered for muxing operations.
411 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
412 BT_ASSERT_DBG(count
> 0);
414 g_ptr_array_set_size(muxer_upstream_msg_iter
->msgs
, count
);
415 muxer_upstream_msg_iter
->next_msg
= 0;
417 /* Move messages to our queue */
418 for (i
= 0; i
< count
; i
++) {
420 * Push to tail in order; other side
421 * (muxer_msg_iter_do_next_one()) consumes
422 * from the head first.
424 g_ptr_array_index(muxer_upstream_msg_iter
->msgs
, i
)
425 = (gpointer
*) msgs
[i
];
427 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
429 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
431 * Message iterator's current message is not
432 * valid anymore. Return
433 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
435 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN
;
437 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
: /* Fall-through. */
439 * Message iterator reached the end: release it. It
440 * won't be considered again to find the youngest
444 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
446 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
:
447 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
448 /* Error status code */
449 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
450 "Upstream iterator's next method returned an error: status=%s",
451 bt_common_func_status_string(input_port_iter_status
));
452 status
= (int) input_port_iter_status
;
455 /* Unsupported status code */
456 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
457 "Unsupported status code: status=%s",
458 bt_common_func_status_string(input_port_iter_status
));
459 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
467 int get_msg_ts_ns(struct muxer_comp
*muxer_comp
,
468 struct muxer_msg_iter
*muxer_msg_iter
,
469 const bt_message
*msg
, int64_t last_returned_ts_ns
,
472 const bt_clock_snapshot
*clock_snapshot
= NULL
;
474 const bt_stream_class
*stream_class
= NULL
;
475 bt_message_type msg_type
;
478 BT_ASSERT_DBG(ts_ns
);
479 BT_COMP_LOGD("Getting message's timestamp: "
480 "muxer-msg-iter-addr=%p, msg-addr=%p, "
481 "last-returned-ts=%" PRId64
,
482 muxer_msg_iter
, msg
, last_returned_ts_ns
);
484 if (G_UNLIKELY(muxer_msg_iter
->clock_class_expectation
==
485 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
)) {
486 *ts_ns
= last_returned_ts_ns
;
490 msg_type
= bt_message_get_type(msg
);
492 if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
)) {
493 stream_class
= bt_stream_borrow_class_const(
494 bt_packet_borrow_stream_const(
495 bt_message_packet_beginning_borrow_packet_const(
497 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_END
)) {
498 stream_class
= bt_stream_borrow_class_const(
499 bt_packet_borrow_stream_const(
500 bt_message_packet_end_borrow_packet_const(
502 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
)) {
503 stream_class
= bt_stream_borrow_class_const(
504 bt_message_discarded_events_borrow_stream_const(msg
));
505 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
)) {
506 stream_class
= bt_stream_borrow_class_const(
507 bt_message_discarded_packets_borrow_stream_const(msg
));
511 case BT_MESSAGE_TYPE_EVENT
:
512 BT_ASSERT_DBG(bt_message_event_borrow_stream_class_default_clock_class_const(
514 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
517 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
518 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
520 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
523 goto no_clock_snapshot
;
527 case BT_MESSAGE_TYPE_PACKET_END
:
528 if (bt_stream_class_packets_have_end_default_clock_snapshot(
530 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
533 goto no_clock_snapshot
;
537 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
539 enum bt_message_stream_clock_snapshot_state snapshot_state
=
540 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
541 msg
, &clock_snapshot
);
542 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
543 goto no_clock_snapshot
;
548 case BT_MESSAGE_TYPE_STREAM_END
:
550 enum bt_message_stream_clock_snapshot_state snapshot_state
=
551 bt_message_stream_end_borrow_default_clock_snapshot_const(
552 msg
, &clock_snapshot
);
553 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
554 goto no_clock_snapshot
;
559 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
560 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
562 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
565 goto no_clock_snapshot
;
569 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
570 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
572 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
575 goto no_clock_snapshot
;
579 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
580 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
584 /* All the other messages have a higher priority */
585 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
586 *ts_ns
= last_returned_ts_ns
;
590 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
592 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
593 "Cannot get nanoseconds from Epoch of clock snapshot: "
594 "clock-snapshot-addr=%p", clock_snapshot
);
601 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
602 "using the last returned timestamp.");
603 *ts_ns
= last_returned_ts_ns
;
611 BT_COMP_LOGD("Found message's timestamp: "
612 "muxer-msg-iter-addr=%p, msg-addr=%p, "
613 "last-returned-ts=%" PRId64
", ts=%" PRId64
,
614 muxer_msg_iter
, msg
, last_returned_ts_ns
,
622 int validate_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
623 struct muxer_comp
*muxer_comp
,
624 const bt_clock_class
*clock_class
)
627 const uint8_t *cc_uuid
;
630 BT_ASSERT_DBG(clock_class
);
631 cc_uuid
= bt_clock_class_get_uuid(clock_class
);
632 cc_name
= bt_clock_class_get_name(clock_class
);
634 if (muxer_msg_iter
->clock_class_expectation
==
635 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
637 * This is the first clock class that this muxer message
638 * iterator encounters. Its properties determine what to expect
639 * for the whole lifetime of the iterator.
641 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
642 /* Expect absolute clock classes */
643 muxer_msg_iter
->clock_class_expectation
=
644 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
;
648 * Expect non-absolute clock classes
649 * with a specific UUID.
651 muxer_msg_iter
->clock_class_expectation
=
652 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
;
653 bt_uuid_copy(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
);
656 * Expect non-absolute clock classes
659 muxer_msg_iter
->clock_class_expectation
=
660 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
;
665 switch (muxer_msg_iter
->clock_class_expectation
) {
666 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
:
667 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
668 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
669 "Expecting an absolute clock class, "
670 "but got a non-absolute one: "
671 "clock-class-addr=%p, clock-class-name=\"%s\"",
672 clock_class
, cc_name
);
676 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
:
677 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
678 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
679 "Expecting a non-absolute clock class with no UUID, "
680 "but got an absolute one: "
681 "clock-class-addr=%p, clock-class-name=\"%s\"",
682 clock_class
, cc_name
);
687 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
688 "Expecting a non-absolute clock class with no UUID, "
689 "but got one with a UUID: "
690 "clock-class-addr=%p, clock-class-name=\"%s\", "
691 "uuid=\"" BT_UUID_FMT
"\"",
692 clock_class
, cc_name
, BT_UUID_FMT_VALUES(cc_uuid
));
696 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
:
697 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
698 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
699 "Expecting a non-absolute clock class with a specific UUID, "
700 "but got an absolute one: "
701 "clock-class-addr=%p, clock-class-name=\"%s\"",
702 clock_class
, cc_name
);
707 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
708 "Expecting a non-absolute clock class with a specific UUID, "
709 "but got one with no UUID: "
710 "clock-class-addr=%p, clock-class-name=\"%s\"",
711 clock_class
, cc_name
);
715 if (bt_uuid_compare(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
) != 0) {
716 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
717 "Expecting a non-absolute clock class with a specific UUID, "
718 "but got one with different UUID: "
719 "clock-class-addr=%p, clock-class-name=\"%s\", "
720 "expected-uuid=\"" BT_UUID_FMT
"\", "
721 "uuid=\"" BT_UUID_FMT
"\"",
722 clock_class
, cc_name
,
723 BT_UUID_FMT_VALUES(muxer_msg_iter
->expected_clock_class_uuid
),
724 BT_UUID_FMT_VALUES(cc_uuid
));
728 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
:
729 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
730 "Expecting no clock class, but got one: "
731 "clock-class-addr=%p, clock-class-name=\"%s\"",
732 clock_class
, cc_name
);
736 BT_COMP_LOGF("Unexpected clock class expectation: "
737 "expectation-code=%d",
738 muxer_msg_iter
->clock_class_expectation
);
752 int validate_new_stream_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
753 struct muxer_comp
*muxer_comp
, const bt_stream
*stream
)
756 const bt_stream_class
*stream_class
=
757 bt_stream_borrow_class_const(stream
);
758 const bt_clock_class
*clock_class
=
759 bt_stream_class_borrow_default_clock_class_const(stream_class
);
762 if (muxer_msg_iter
->clock_class_expectation
==
763 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
764 /* Expect no clock class */
765 muxer_msg_iter
->clock_class_expectation
=
766 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
;
767 } else if (muxer_msg_iter
->clock_class_expectation
!=
768 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
) {
769 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
770 "Expecting stream class without a default clock class: "
771 "stream-class-addr=%p, stream-class-name=\"%s\", "
772 "stream-class-id=%" PRIu64
,
773 stream_class
, bt_stream_class_get_name(stream_class
),
774 bt_stream_class_get_id(stream_class
));
781 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
, clock_class
);
788 * This function finds the youngest available message amongst the
789 * non-ended upstream message iterators and returns the upstream
790 * message iterator which has it, or
791 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
794 * This function does NOT:
796 * * Update any upstream message iterator.
797 * * Check the upstream message iterators to retry.
799 * On sucess, this function sets *muxer_upstream_msg_iter to the
800 * upstream message iterator of which the current message is
801 * the youngest, and sets *ts_ns to its time.
804 bt_message_iterator_class_next_method_status
805 muxer_msg_iter_youngest_upstream_msg_iter(
806 struct muxer_comp
*muxer_comp
,
807 struct muxer_msg_iter
*muxer_msg_iter
,
808 struct muxer_upstream_msg_iter
**muxer_upstream_msg_iter
,
813 int64_t youngest_ts_ns
= INT64_MAX
;
814 bt_message_iterator_class_next_method_status status
=
815 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
817 BT_ASSERT_DBG(muxer_comp
);
818 BT_ASSERT_DBG(muxer_msg_iter
);
819 BT_ASSERT_DBG(muxer_upstream_msg_iter
);
820 *muxer_upstream_msg_iter
= NULL
;
822 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
824 const bt_message
*msg
;
825 struct muxer_upstream_msg_iter
*cur_muxer_upstream_msg_iter
=
827 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
831 if (!cur_muxer_upstream_msg_iter
->msg_iter
) {
832 /* This upstream message iterator is ended */
833 BT_COMP_LOGT("Skipping ended upstream message iterator: "
834 "muxer-upstream-msg-iter-wrap-addr=%p",
835 cur_muxer_upstream_msg_iter
);
839 BT_ASSERT_DBG(cur_muxer_upstream_msg_iter
->next_msg
<
840 cur_muxer_upstream_msg_iter
->msgs
->len
);
841 msg
= g_ptr_array_index(cur_muxer_upstream_msg_iter
->msgs
,
842 cur_muxer_upstream_msg_iter
->next_msg
);
845 if (G_UNLIKELY(bt_message_get_type(msg
) ==
846 BT_MESSAGE_TYPE_STREAM_BEGINNING
)) {
847 ret
= validate_new_stream_clock_class(
848 muxer_msg_iter
, muxer_comp
,
849 bt_message_stream_beginning_borrow_stream_const(
853 * validate_new_stream_clock_class() logs
856 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
859 } else if (G_UNLIKELY(bt_message_get_type(msg
) ==
860 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
)) {
861 const bt_clock_snapshot
*cs
;
863 cs
= bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
865 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
,
866 bt_clock_snapshot_borrow_clock_class_const(cs
));
868 /* validate_clock_class() logs errors */
869 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
874 ret
= get_msg_ts_ns(muxer_comp
, muxer_msg_iter
, msg
,
875 muxer_msg_iter
->last_returned_ts_ns
, &msg_ts_ns
);
877 /* get_msg_ts_ns() logs errors */
878 *muxer_upstream_msg_iter
= NULL
;
879 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
884 * Update the current message iterator if it has not been set
885 * yet, or if its current message has a timestamp smaller than
886 * the previously selected youngest message.
888 if (G_UNLIKELY(*muxer_upstream_msg_iter
== NULL
) ||
889 msg_ts_ns
< youngest_ts_ns
) {
890 *muxer_upstream_msg_iter
=
891 cur_muxer_upstream_msg_iter
;
892 youngest_ts_ns
= msg_ts_ns
;
893 *ts_ns
= youngest_ts_ns
;
894 } else if (msg_ts_ns
== youngest_ts_ns
) {
896 * The currently selected message to be sent downstream
897 * next has the exact same timestamp that of the
898 * current candidate message. We must break the tie
899 * in a predictable manner.
901 BT_ASSERT_DBG((*muxer_upstream_msg_iter
)->next_msg
<
902 (*muxer_upstream_msg_iter
)->msgs
->len
);
903 const bt_message
*selected_msg
=
904 g_ptr_array_index((*muxer_upstream_msg_iter
)->msgs
,
905 (*muxer_upstream_msg_iter
)->next_msg
);
906 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
909 * Order the messages in an arbitrary but determinitic
912 ret
= common_muxing_compare_messages(msg
, selected_msg
);
915 * The `msg` should go first. Update the next
916 * iterator and the current timestamp.
918 *muxer_upstream_msg_iter
=
919 cur_muxer_upstream_msg_iter
;
920 youngest_ts_ns
= msg_ts_ns
;
921 *ts_ns
= youngest_ts_ns
;
922 } else if (ret
== 0) {
923 /* Unable to pick which one should go first. */
924 BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
925 "muxer-upstream-msg-iter-wrap-addr=%p"
926 "cur-muxer-upstream-msg-iter-wrap-addr=%p",
927 *muxer_upstream_msg_iter
,
928 cur_muxer_upstream_msg_iter
);
933 if (!*muxer_upstream_msg_iter
) {
934 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
943 bt_message_iterator_class_next_method_status
944 validate_muxer_upstream_msg_iter(
945 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
948 struct muxer_comp
*muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
949 bt_message_iterator_class_next_method_status status
;
951 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
952 "muxer-upstream-msg-iter-wrap-addr=%p",
953 muxer_upstream_msg_iter
);
955 if (muxer_upstream_msg_iter
->next_msg
< muxer_upstream_msg_iter
->msgs
->len
||
956 !muxer_upstream_msg_iter
->msg_iter
) {
957 BT_COMP_LOGD("Already valid or not considered: "
958 "queue-len=%u, next-msg=%u, upstream-msg-iter-addr=%p",
959 muxer_upstream_msg_iter
->msgs
->len
,
960 muxer_upstream_msg_iter
->next_msg
,
961 muxer_upstream_msg_iter
->msg_iter
);
962 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
966 /* muxer_upstream_msg_iter_next() logs details/errors */
967 status
= muxer_upstream_msg_iter_next(muxer_upstream_msg_iter
,
975 bt_message_iterator_class_next_method_status
976 validate_muxer_upstream_msg_iters(
977 struct muxer_msg_iter
*muxer_msg_iter
)
979 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
980 bt_message_iterator_class_next_method_status status
;
983 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
984 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
986 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
988 bool is_ended
= false;
989 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
991 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
994 status
= validate_muxer_upstream_msg_iter(
995 muxer_upstream_msg_iter
, &is_ended
);
996 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
998 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
999 "Cannot validate muxer's upstream message iterator wrapper: "
1000 "muxer-msg-iter-addr=%p, "
1001 "muxer-upstream-msg-iter-wrap-addr=%p",
1003 muxer_upstream_msg_iter
);
1005 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
1006 "muxer-msg-iter-addr=%p, "
1007 "muxer-upstream-msg-iter-wrap-addr=%p",
1009 muxer_upstream_msg_iter
);
1016 * Move this muxer upstream message iterator to the
1017 * array of ended iterators if it's ended.
1019 if (G_UNLIKELY(is_ended
)) {
1020 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
1021 "muxer-msg-iter-addr=%p, "
1022 "muxer-upstream-msg-iter-wrap-addr=%p",
1023 muxer_msg_iter
, muxer_upstream_msg_iter
);
1025 muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1026 muxer_upstream_msg_iter
);
1027 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1030 * Use g_ptr_array_remove_fast() because the
1031 * order of those elements is not important.
1033 g_ptr_array_remove_index_fast(
1034 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1040 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1047 bt_message_iterator_class_next_method_status
muxer_msg_iter_do_next_one(
1048 struct muxer_comp
*muxer_comp
,
1049 struct muxer_msg_iter
*muxer_msg_iter
,
1050 const bt_message
**msg
)
1052 bt_message_iterator_class_next_method_status status
;
1053 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
= NULL
;
1054 /* Initialize to avoid -Wmaybe-uninitialized warning with gcc 4.8. */
1055 int64_t next_return_ts
= 0;
1057 status
= validate_muxer_upstream_msg_iters(muxer_msg_iter
);
1058 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1059 /* validate_muxer_upstream_msg_iters() logs details */
1064 * At this point we know that all the existing upstream
1065 * message iterators are valid. We can find the one,
1066 * amongst those, of which the current message is the
1069 status
= muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp
,
1070 muxer_msg_iter
, &muxer_upstream_msg_iter
,
1072 if (status
< 0 || status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1074 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1075 "Cannot find the youngest upstream message iterator wrapper: "
1077 bt_common_func_status_string(status
));
1079 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
1081 bt_common_func_status_string(status
));
1087 if (next_return_ts
< muxer_msg_iter
->last_returned_ts_ns
) {
1088 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1089 "Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
1090 "muxer-msg-iter-addr=%p, ts=%" PRId64
", "
1091 "last-returned-ts=%" PRId64
,
1092 muxer_msg_iter
, next_return_ts
,
1093 muxer_msg_iter
->last_returned_ts_ns
);
1094 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1098 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
1099 "muxer-msg-iter-addr=%p, "
1100 "muxer-upstream-msg-iter-wrap-addr=%p, "
1102 muxer_msg_iter
, muxer_upstream_msg_iter
, next_return_ts
);
1103 BT_ASSERT_DBG(status
==
1104 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
);
1105 BT_ASSERT_DBG(muxer_upstream_msg_iter
);
1108 * Consume from the queue's head: other side
1109 * (muxer_upstream_msg_iter_next()) writes to the tail.
1111 *msg
= g_ptr_array_index(muxer_upstream_msg_iter
->msgs
,
1112 muxer_upstream_msg_iter
->next_msg
);
1113 g_ptr_array_index(muxer_upstream_msg_iter
->msgs
,
1114 muxer_upstream_msg_iter
->next_msg
) = NULL
;
1115 ++muxer_upstream_msg_iter
->next_msg
;
1116 BT_ASSERT_DBG(*msg
);
1117 muxer_msg_iter
->last_returned_ts_ns
= next_return_ts
;
1124 bt_message_iterator_class_next_method_status
muxer_msg_iter_do_next(
1125 struct muxer_comp
*muxer_comp
,
1126 struct muxer_msg_iter
*muxer_msg_iter
,
1127 bt_message_array_const msgs
, uint64_t capacity
,
1130 bt_message_iterator_class_next_method_status status
;
1133 if (G_UNLIKELY(muxer_msg_iter
->next_saved_error
)) {
1135 * Last time we were called, we hit an error but had some
1136 * messages to deliver, so we stashed the error here. Return
1139 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(muxer_msg_iter
->next_saved_error
);
1140 status
= muxer_msg_iter
->next_saved_status
;
1145 status
= muxer_msg_iter_do_next_one(muxer_comp
,
1146 muxer_msg_iter
, &msgs
[i
]);
1147 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1150 } while (i
< capacity
&& status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
);
1154 * Even if muxer_msg_iter_do_next_one() returned
1155 * something else than
1156 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1157 * message objects in the output message
1158 * array, so we need to return
1159 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1160 * transfered to downstream. This other status occurs
1161 * again the next time muxer_msg_iter_do_next() is
1162 * called, possibly without any accumulated
1163 * message, in which case we'll return it.
1167 * Save this error for the next _next call. Assume that
1168 * this component always appends error causes when
1169 * returning an error status code, which will cause the
1170 * current thread error to be non-NULL.
1172 muxer_msg_iter
->next_saved_error
= bt_current_thread_take_error();
1173 BT_ASSERT(muxer_msg_iter
->next_saved_error
);
1174 muxer_msg_iter
->next_saved_status
= status
;
1178 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1186 void destroy_muxer_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
)
1188 struct muxer_comp
*muxer_comp
;
1190 if (!muxer_msg_iter
) {
1194 muxer_comp
= muxer_msg_iter
->muxer_comp
;
1195 BT_COMP_LOGD("Destroying muxer component's message iterator: "
1196 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1198 if (muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1199 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
1201 muxer_msg_iter
->active_muxer_upstream_msg_iters
, TRUE
);
1204 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1205 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
1207 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, TRUE
);
1210 g_free(muxer_msg_iter
);
1214 bt_message_iterator_class_initialize_method_status
1215 muxer_msg_iter_init_upstream_iterators(struct muxer_comp
*muxer_comp
,
1216 struct muxer_msg_iter
*muxer_msg_iter
,
1217 struct bt_self_message_iterator_configuration
*config
)
1221 bt_message_iterator_class_initialize_method_status status
;
1222 bool can_seek_forward
= true;
1224 count
= bt_component_filter_get_input_port_count(
1225 bt_self_component_filter_as_component_filter(
1226 muxer_comp
->self_comp_flt
));
1228 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
1229 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1230 muxer_comp
, muxer_msg_iter
);
1231 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1235 for (i
= 0; i
< count
; i
++) {
1236 bt_message_iterator
*upstream_msg_iter
;
1237 bt_self_component_port_input
*self_port
=
1238 bt_self_component_filter_borrow_input_port_by_index(
1239 muxer_comp
->self_comp_flt
, i
);
1240 const bt_port
*port
;
1241 bt_message_iterator_create_from_message_iterator_status
1245 BT_ASSERT(self_port
);
1246 port
= bt_self_component_port_as_port(
1247 bt_self_component_port_input_as_self_component_port(
1251 if (!bt_port_is_connected(port
)) {
1252 /* Skip non-connected port */
1256 msg_iter_status
= create_msg_iter_on_input_port(muxer_comp
,
1257 muxer_msg_iter
, self_port
, &upstream_msg_iter
);
1258 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
1259 /* create_msg_iter_on_input_port() logs errors */
1260 status
= (int) msg_iter_status
;
1264 int_status
= muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter
,
1266 bt_message_iterator_put_ref(
1269 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1270 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1274 can_seek_forward
= can_seek_forward
&&
1275 bt_message_iterator_can_seek_forward(
1280 * This iterator can seek forward if all of its iterators can seek
1283 bt_self_message_iterator_configuration_set_can_seek_forward(
1284 config
, can_seek_forward
);
1286 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
1292 bt_message_iterator_class_initialize_method_status
muxer_msg_iter_init(
1293 bt_self_message_iterator
*self_msg_iter
,
1294 bt_self_message_iterator_configuration
*config
,
1295 bt_self_component_port_output
*port
__attribute__((unused
)))
1297 struct muxer_comp
*muxer_comp
= NULL
;
1298 struct muxer_msg_iter
*muxer_msg_iter
= NULL
;
1299 bt_message_iterator_class_initialize_method_status status
;
1300 bt_self_component
*self_comp
=
1301 bt_self_message_iterator_borrow_component(self_msg_iter
);
1303 muxer_comp
= bt_self_component_get_data(self_comp
);
1304 BT_ASSERT(muxer_comp
);
1305 BT_COMP_LOGD("Initializing muxer component's message iterator: "
1306 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1307 self_comp
, muxer_comp
, self_msg_iter
);
1309 if (muxer_comp
->initializing_muxer_msg_iter
) {
1311 * Weird, unhandled situation detected: downstream
1312 * creates a muxer message iterator while creating
1313 * another muxer message iterator (same component).
1315 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1316 "Recursive initialization of muxer component's message iterator: "
1317 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1318 self_comp
, muxer_comp
, self_msg_iter
);
1319 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
1323 muxer_comp
->initializing_muxer_msg_iter
= true;
1324 muxer_msg_iter
= g_new0(struct muxer_msg_iter
, 1);
1325 if (!muxer_msg_iter
) {
1326 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1327 "Failed to allocate one muxer component's message iterator.");
1328 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1332 muxer_msg_iter
->muxer_comp
= muxer_comp
;
1333 muxer_msg_iter
->self_msg_iter
= self_msg_iter
;
1334 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1335 muxer_msg_iter
->active_muxer_upstream_msg_iters
=
1336 g_ptr_array_new_with_free_func(
1337 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1338 if (!muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1339 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
, "Failed to allocate a GPtrArray.");
1340 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1344 muxer_msg_iter
->ended_muxer_upstream_msg_iters
=
1345 g_ptr_array_new_with_free_func(
1346 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1347 if (!muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1348 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
, "Failed to allocate a GPtrArray.");
1349 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1353 status
= muxer_msg_iter_init_upstream_iterators(muxer_comp
,
1354 muxer_msg_iter
, config
);
1356 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1357 "Cannot initialize connected input ports for muxer component's message iterator: "
1358 "comp-addr=%p, muxer-comp-addr=%p, "
1359 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1360 self_comp
, muxer_comp
, muxer_msg_iter
,
1361 self_msg_iter
, status
);
1365 bt_self_message_iterator_set_data(self_msg_iter
, muxer_msg_iter
);
1366 BT_COMP_LOGD("Initialized muxer component's message iterator: "
1367 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1369 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1373 destroy_muxer_msg_iter(muxer_msg_iter
);
1374 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
1377 muxer_comp
->initializing_muxer_msg_iter
= false;
1381 void muxer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1383 struct muxer_msg_iter
*muxer_msg_iter
=
1384 bt_self_message_iterator_get_data(self_msg_iter
);
1385 bt_self_component
*self_comp
= NULL
;
1386 struct muxer_comp
*muxer_comp
= NULL
;
1388 self_comp
= bt_self_message_iterator_borrow_component(
1390 BT_ASSERT(self_comp
);
1391 muxer_comp
= bt_self_component_get_data(self_comp
);
1392 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
1393 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1395 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1397 if (muxer_msg_iter
) {
1398 destroy_muxer_msg_iter(muxer_msg_iter
);
1402 bt_message_iterator_class_next_method_status
muxer_msg_iter_next(
1403 bt_self_message_iterator
*self_msg_iter
,
1404 bt_message_array_const msgs
, uint64_t capacity
,
1407 bt_message_iterator_class_next_method_status status
;
1408 struct muxer_msg_iter
*muxer_msg_iter
=
1409 bt_self_message_iterator_get_data(self_msg_iter
);
1410 bt_self_component
*self_comp
= NULL
;
1411 struct muxer_comp
*muxer_comp
= NULL
;
1413 BT_ASSERT_DBG(muxer_msg_iter
);
1414 self_comp
= bt_self_message_iterator_borrow_component(
1416 BT_ASSERT_DBG(self_comp
);
1417 muxer_comp
= bt_self_component_get_data(self_comp
);
1418 BT_ASSERT_DBG(muxer_comp
);
1419 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
1420 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1422 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1424 status
= muxer_msg_iter_do_next(muxer_comp
, muxer_msg_iter
,
1425 msgs
, capacity
, count
);
1427 BT_COMP_LOGE_APPEND_CAUSE(self_comp
,
1428 "Cannot get next message: "
1429 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1430 "msg-iter-addr=%p, status=%s",
1431 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
,
1432 bt_common_func_status_string(status
));
1434 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
1436 bt_common_func_status_string(status
));
1442 bt_component_class_port_connected_method_status
muxer_input_port_connected(
1443 bt_self_component_filter
*self_comp
,
1444 bt_self_component_port_input
*self_port
__attribute__((unused
)),
1445 const bt_port_output
*other_port
__attribute__((unused
)))
1447 bt_component_class_port_connected_method_status status
=
1448 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK
;
1449 bt_self_component_add_port_status add_port_status
;
1450 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
1451 bt_self_component_filter_as_self_component(self_comp
));
1453 add_port_status
= add_available_input_port(self_comp
);
1454 if (add_port_status
) {
1455 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1456 "Cannot add one muxer component's input port: status=%s",
1457 bt_common_func_status_string(add_port_status
));
1459 if (add_port_status
==
1460 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
1461 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR
;
1463 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR
;
1474 bt_message_iterator_class_can_seek_beginning_method_status
1475 muxer_upstream_msg_iters_can_all_seek_beginning(
1476 struct muxer_comp
*muxer_comp
,
1477 GPtrArray
*muxer_upstream_msg_iters
, bt_bool
*can_seek
)
1479 bt_message_iterator_class_can_seek_beginning_method_status status
=
1480 BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
;
1483 for (i
= 0; i
< muxer_upstream_msg_iters
->len
; i
++) {
1484 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1485 muxer_upstream_msg_iters
->pdata
[i
];
1486 status
= (int) bt_message_iterator_can_seek_beginning(
1487 upstream_msg_iter
->msg_iter
, can_seek
);
1488 if (status
!= BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
) {
1489 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1490 "Failed to determine whether upstream message iterator can seek beginning: "
1491 "msg-iter-addr=%p", upstream_msg_iter
->msg_iter
);
1500 *can_seek
= BT_TRUE
;
1506 bt_message_iterator_class_can_seek_beginning_method_status
1507 muxer_msg_iter_can_seek_beginning(
1508 bt_self_message_iterator
*self_msg_iter
, bt_bool
*can_seek
)
1510 struct muxer_msg_iter
*muxer_msg_iter
=
1511 bt_self_message_iterator_get_data(self_msg_iter
);
1512 bt_message_iterator_class_can_seek_beginning_method_status status
;
1514 status
= muxer_upstream_msg_iters_can_all_seek_beginning(
1515 muxer_msg_iter
->muxer_comp
,
1516 muxer_msg_iter
->active_muxer_upstream_msg_iters
, can_seek
);
1517 if (status
!= BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
) {
1525 status
= muxer_upstream_msg_iters_can_all_seek_beginning(
1526 muxer_msg_iter
->muxer_comp
,
1527 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, can_seek
);
1533 bt_message_iterator_class_seek_beginning_method_status
muxer_msg_iter_seek_beginning(
1534 bt_self_message_iterator
*self_msg_iter
)
1536 struct muxer_msg_iter
*muxer_msg_iter
=
1537 bt_self_message_iterator_get_data(self_msg_iter
);
1538 bt_message_iterator_class_seek_beginning_method_status status
=
1539 BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK
;
1540 bt_message_iterator_seek_beginning_status seek_beg_status
;
1543 /* Seek all ended upstream iterators first */
1544 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1546 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1547 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1549 seek_beg_status
= bt_message_iterator_seek_beginning(
1550 upstream_msg_iter
->msg_iter
);
1551 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1552 status
= (int) seek_beg_status
;
1556 empty_message_queue(upstream_msg_iter
);
1559 /* Seek all previously active upstream iterators */
1560 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
1562 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1563 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
];
1565 seek_beg_status
= bt_message_iterator_seek_beginning(
1566 upstream_msg_iter
->msg_iter
);
1567 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1568 status
= (int) seek_beg_status
;
1572 empty_message_queue(upstream_msg_iter
);
1575 /* Make them all active */
1576 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1578 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1579 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1581 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1583 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1587 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1588 * called on an empty array.
1590 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
> 0) {
1591 g_ptr_array_remove_range(muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1592 0, muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
);
1594 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1595 muxer_msg_iter
->clock_class_expectation
=
1596 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
;