2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_COMP_LOG_SELF_COMP (muxer_comp->self_comp)
24 #define BT_LOG_OUTPUT_LEVEL (muxer_comp->log_level)
25 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.MUXER"
26 #include "logging/comp-logging.h"
28 #include "common/macros.h"
29 #include "common/uuid.h"
30 #include <babeltrace2/babeltrace.h>
34 #include "common/assert.h"
35 #include "common/common.h"
39 #include "plugins/common/muxing/muxing.h"
43 #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes"
47 bt_self_component_filter
*self_comp_flt
;
48 bt_self_component
*self_comp
;
50 unsigned int next_port_num
;
51 size_t available_input_ports
;
52 bool initializing_muxer_msg_iter
;
53 bool assume_absolute_clock_classes
;
54 bt_logging_level log_level
;
57 struct muxer_upstream_msg_iter
{
58 struct muxer_comp
*muxer_comp
;
60 /* Owned by this, NULL if ended */
61 bt_self_component_port_input_message_iterator
*msg_iter
;
63 /* Contains `const bt_message *`, owned by this */
67 enum muxer_msg_iter_clock_class_expectation
{
68 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
= 0,
69 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
,
70 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
,
71 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
,
72 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
,
75 struct muxer_msg_iter
{
76 struct muxer_comp
*muxer_comp
;
79 bt_self_message_iterator
*self_msg_iter
;
82 * Array of struct muxer_upstream_msg_iter * (owned by this).
84 * NOTE: This array is searched in linearly to find the youngest
85 * current message. Keep this until benchmarks confirm that
86 * another data structure is faster than this for our typical
89 GPtrArray
*active_muxer_upstream_msg_iters
;
92 * Array of struct muxer_upstream_msg_iter * (owned by this).
94 * We move ended message iterators from
95 * `active_muxer_upstream_msg_iters` to this array so as to be
96 * able to restore them when seeking.
98 GPtrArray
*ended_muxer_upstream_msg_iters
;
100 /* Last time returned in a message */
101 int64_t last_returned_ts_ns
;
103 /* Clock class expectation state */
104 enum muxer_msg_iter_clock_class_expectation clock_class_expectation
;
107 * Expected clock class UUID, only valid when
108 * clock_class_expectation is
109 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
111 bt_uuid_t expected_clock_class_uuid
;
115 void empty_message_queue(struct muxer_upstream_msg_iter
*upstream_msg_iter
)
117 const bt_message
*msg
;
119 while ((msg
= g_queue_pop_head(upstream_msg_iter
->msgs
))) {
120 bt_message_put_ref(msg
);
125 void destroy_muxer_upstream_msg_iter(
126 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
)
128 struct muxer_comp
*muxer_comp
;
130 if (!muxer_upstream_msg_iter
) {
134 muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
135 BT_COMP_LOGD("Destroying muxer's upstream message iterator wrapper: "
136 "addr=%p, msg-iter-addr=%p, queue-len=%u",
137 muxer_upstream_msg_iter
,
138 muxer_upstream_msg_iter
->msg_iter
,
139 muxer_upstream_msg_iter
->msgs
->length
);
140 bt_self_component_port_input_message_iterator_put_ref(
141 muxer_upstream_msg_iter
->msg_iter
);
143 if (muxer_upstream_msg_iter
->msgs
) {
144 empty_message_queue(muxer_upstream_msg_iter
);
145 g_queue_free(muxer_upstream_msg_iter
->msgs
);
148 g_free(muxer_upstream_msg_iter
);
152 int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
,
153 bt_self_component_port_input_message_iterator
*self_msg_iter
)
156 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
157 g_new0(struct muxer_upstream_msg_iter
, 1);
158 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
160 if (!muxer_upstream_msg_iter
) {
161 BT_COMP_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
165 muxer_upstream_msg_iter
->muxer_comp
= muxer_comp
;
166 muxer_upstream_msg_iter
->msg_iter
= self_msg_iter
;
167 bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter
->msg_iter
);
168 muxer_upstream_msg_iter
->msgs
= g_queue_new();
169 if (!muxer_upstream_msg_iter
->msgs
) {
170 BT_COMP_LOGE_STR("Failed to allocate a GQueue.");
174 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
175 muxer_upstream_msg_iter
);
176 BT_COMP_LOGD("Added muxer's upstream message iterator wrapper: "
177 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
178 muxer_upstream_msg_iter
, muxer_msg_iter
,
184 g_free(muxer_upstream_msg_iter
);
192 bt_self_component_add_port_status
add_available_input_port(
193 bt_self_component_filter
*self_comp
)
195 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
196 bt_self_component_filter_as_self_component(self_comp
));
197 bt_self_component_add_port_status status
=
198 BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
;
199 GString
*port_name
= NULL
;
201 BT_ASSERT(muxer_comp
);
202 port_name
= g_string_new("in");
204 BT_COMP_LOGE_STR("Failed to allocate a GString.");
205 status
= BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
;
209 g_string_append_printf(port_name
, "%u", muxer_comp
->next_port_num
);
210 status
= bt_self_component_filter_add_input_port(
211 self_comp
, port_name
->str
, NULL
, NULL
);
212 if (status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
213 BT_COMP_LOGE("Cannot add input port to muxer component: "
214 "port-name=\"%s\", comp-addr=%p, status=%s",
215 port_name
->str
, self_comp
,
216 bt_common_func_status_string(status
));
220 muxer_comp
->available_input_ports
++;
221 muxer_comp
->next_port_num
++;
222 BT_COMP_LOGI("Added one input port to muxer component: "
223 "port-name=\"%s\", comp-addr=%p",
224 port_name
->str
, self_comp
);
228 g_string_free(port_name
, TRUE
);
235 bt_self_component_add_port_status
create_output_port(
236 bt_self_component_filter
*self_comp
)
238 return bt_self_component_filter_add_output_port(
239 self_comp
, "out", NULL
, NULL
);
243 void destroy_muxer_comp(struct muxer_comp
*muxer_comp
)
253 bt_value
*get_default_params(struct muxer_comp
*muxer_comp
)
258 params
= bt_value_map_create();
260 BT_COMP_LOGE_STR("Cannot create a map value object.");
264 ret
= bt_value_map_insert_bool_entry(params
,
265 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
, false);
267 BT_COMP_LOGE_STR("Cannot add boolean value to map value object.");
274 BT_VALUE_PUT_REF_AND_RESET(params
);
281 int configure_muxer_comp(struct muxer_comp
*muxer_comp
,
282 const bt_value
*params
)
284 bt_value
*default_params
= NULL
;
285 bt_value
*real_params
= NULL
;
286 const bt_value
*assume_absolute_clock_classes
= NULL
;
290 default_params
= get_default_params(muxer_comp
);
291 if (!default_params
) {
292 BT_COMP_LOGE("Cannot get default parameters: "
293 "muxer-comp-addr=%p", muxer_comp
);
297 ret
= bt_value_map_extend(default_params
, params
, &real_params
);
299 BT_COMP_LOGE("Cannot extend default parameters map value: "
300 "muxer-comp-addr=%p, def-params-addr=%p, "
301 "params-addr=%p", muxer_comp
, default_params
,
306 assume_absolute_clock_classes
= bt_value_map_borrow_entry_value(real_params
,
307 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
);
308 if (assume_absolute_clock_classes
&&
309 !bt_value_is_bool(assume_absolute_clock_classes
)) {
310 BT_COMP_LOGE("Expecting a boolean value for the `%s` parameter: "
311 "muxer-comp-addr=%p, value-type=%s",
312 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
, muxer_comp
,
313 bt_common_value_type_string(
314 bt_value_get_type(assume_absolute_clock_classes
)));
318 bool_val
= bt_value_bool_get(assume_absolute_clock_classes
);
319 muxer_comp
->assume_absolute_clock_classes
= (bool) bool_val
;
320 BT_COMP_LOGI("Configured muxer component: muxer-comp-addr=%p, "
321 "assume-absolute-clock-classes=%d",
322 muxer_comp
, muxer_comp
->assume_absolute_clock_classes
);
329 bt_value_put_ref(default_params
);
330 bt_value_put_ref(real_params
);
335 bt_component_class_init_method_status
muxer_init(
336 bt_self_component_filter
*self_comp_flt
,
337 const bt_value
*params
, void *init_data
)
340 bt_component_class_init_method_status status
=
341 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
342 bt_self_component_add_port_status add_port_status
;
343 bt_self_component
*self_comp
=
344 bt_self_component_filter_as_self_component(self_comp_flt
);
345 struct muxer_comp
*muxer_comp
= g_new0(struct muxer_comp
, 1);
346 bt_logging_level log_level
= bt_component_get_logging_level(
347 bt_self_component_as_component(self_comp
));
349 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
350 "Initializing muxer component: "
351 "comp-addr=%p, params-addr=%p", self_comp
, params
);
354 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
355 "Failed to allocate one muxer component.");
359 muxer_comp
->log_level
= log_level
;
360 muxer_comp
->self_comp
= self_comp
;
361 muxer_comp
->self_comp_flt
= self_comp_flt
;
362 ret
= configure_muxer_comp(muxer_comp
, params
);
364 BT_COMP_LOGE("Cannot configure muxer component: "
365 "muxer-comp-addr=%p, params-addr=%p",
370 bt_self_component_set_data(self_comp
, muxer_comp
);
371 add_port_status
= add_available_input_port(self_comp_flt
);
372 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
373 BT_COMP_LOGE("Cannot ensure that at least one muxer component's input port is available: "
374 "muxer-comp-addr=%p, status=%s",
376 bt_common_func_status_string(add_port_status
));
377 if (add_port_status
==
378 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
379 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
381 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
387 add_port_status
= create_output_port(self_comp_flt
);
388 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
389 BT_COMP_LOGE("Cannot create muxer component's output port: "
390 "muxer-comp-addr=%p, status=%s",
392 bt_common_func_status_string(add_port_status
));
393 if (add_port_status
==
394 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
395 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
397 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
403 BT_COMP_LOGI("Initialized muxer component: "
404 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
405 self_comp
, params
, muxer_comp
);
410 destroy_muxer_comp(muxer_comp
);
411 bt_self_component_set_data(self_comp
, NULL
);
413 if (status
== BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
) {
414 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
422 void muxer_finalize(bt_self_component_filter
*self_comp
)
424 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
425 bt_self_component_filter_as_self_component(self_comp
));
427 BT_COMP_LOGI("Finalizing muxer component: comp-addr=%p",
429 destroy_muxer_comp(muxer_comp
);
433 bt_self_component_port_input_message_iterator
*
434 create_msg_iter_on_input_port(struct muxer_comp
*muxer_comp
,
435 struct muxer_msg_iter
*muxer_msg_iter
,
436 bt_self_component_port_input
*self_port
)
438 const bt_port
*port
= bt_self_component_port_as_port(
439 bt_self_component_port_input_as_self_component_port(
441 bt_self_component_port_input_message_iterator
*msg_iter
=
445 BT_ASSERT(bt_port_is_connected(port
));
447 // TODO: Advance the iterator to >= the time of the latest
448 // returned message by the muxer message
449 // iterator which creates it.
450 msg_iter
= bt_self_component_port_input_message_iterator_create_from_message_iterator(
451 muxer_msg_iter
->self_msg_iter
, self_port
);
453 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
454 "port-addr=%p, port-name=\"%s\"",
455 port
, bt_port_get_name(port
));
459 BT_COMP_LOGI("Created upstream message iterator on input port: "
460 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
461 port
, bt_port_get_name(port
), msg_iter
);
468 bt_component_class_message_iterator_next_method_status
muxer_upstream_msg_iter_next(
469 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
472 struct muxer_comp
*muxer_comp
=
473 muxer_upstream_msg_iter
->muxer_comp
;
474 bt_component_class_message_iterator_next_method_status status
;
475 bt_message_iterator_next_status input_port_iter_status
;
476 bt_message_array_const msgs
;
480 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
481 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
482 muxer_upstream_msg_iter
,
483 muxer_upstream_msg_iter
->msg_iter
);
484 input_port_iter_status
= bt_self_component_port_input_message_iterator_next(
485 muxer_upstream_msg_iter
->msg_iter
, &msgs
, &count
);
486 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
488 bt_common_func_status_string(input_port_iter_status
));
490 switch (input_port_iter_status
) {
491 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
493 * Message iterator's current message is
494 * valid: it must be considered for muxing operations.
496 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
497 BT_ASSERT(count
> 0);
499 /* Move messages to our queue */
500 for (i
= 0; i
< count
; i
++) {
502 * Push to tail in order; other side
503 * (muxer_msg_iter_do_next_one()) consumes
504 * from the head first.
506 g_queue_push_tail(muxer_upstream_msg_iter
->msgs
,
509 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
511 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
513 * Message iterator's current message is not
514 * valid anymore. Return
515 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
517 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
519 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
: /* Fall-through. */
521 * Message iterator reached the end: release it. It
522 * won't be considered again to find the youngest
526 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
529 /* Error or unsupported status code */
530 BT_COMP_LOGE("Error or unsupported status code: "
531 "status-code=%d", input_port_iter_status
);
532 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
540 int get_msg_ts_ns(struct muxer_comp
*muxer_comp
,
541 struct muxer_msg_iter
*muxer_msg_iter
,
542 const bt_message
*msg
, int64_t last_returned_ts_ns
,
545 const bt_clock_snapshot
*clock_snapshot
= NULL
;
547 const bt_stream_class
*stream_class
= NULL
;
548 bt_message_type msg_type
;
552 BT_COMP_LOGD("Getting message's timestamp: "
553 "muxer-msg-iter-addr=%p, msg-addr=%p, "
554 "last-returned-ts=%" PRId64
,
555 muxer_msg_iter
, msg
, last_returned_ts_ns
);
557 if (G_UNLIKELY(muxer_msg_iter
->clock_class_expectation
==
558 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
)) {
559 *ts_ns
= last_returned_ts_ns
;
563 msg_type
= bt_message_get_type(msg
);
565 if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
)) {
566 stream_class
= bt_stream_borrow_class_const(
567 bt_packet_borrow_stream_const(
568 bt_message_packet_beginning_borrow_packet_const(
570 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_END
)) {
571 stream_class
= bt_stream_borrow_class_const(
572 bt_packet_borrow_stream_const(
573 bt_message_packet_end_borrow_packet_const(
575 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
)) {
576 stream_class
= bt_stream_borrow_class_const(
577 bt_message_discarded_events_borrow_stream_const(msg
));
578 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
)) {
579 stream_class
= bt_stream_borrow_class_const(
580 bt_message_discarded_packets_borrow_stream_const(msg
));
584 case BT_MESSAGE_TYPE_EVENT
:
585 BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const(
587 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
590 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
591 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
593 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
596 goto no_clock_snapshot
;
600 case BT_MESSAGE_TYPE_PACKET_END
:
601 if (bt_stream_class_packets_have_end_default_clock_snapshot(
603 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
606 goto no_clock_snapshot
;
610 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
612 enum bt_message_stream_clock_snapshot_state snapshot_state
=
613 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
614 msg
, &clock_snapshot
);
615 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
616 goto no_clock_snapshot
;
621 case BT_MESSAGE_TYPE_STREAM_END
:
623 enum bt_message_stream_clock_snapshot_state snapshot_state
=
624 bt_message_stream_end_borrow_default_clock_snapshot_const(
625 msg
, &clock_snapshot
);
626 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
627 goto no_clock_snapshot
;
632 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
633 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
635 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
638 goto no_clock_snapshot
;
642 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
643 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
645 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
648 goto no_clock_snapshot
;
652 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
653 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
657 /* All the other messages have a higher priority */
658 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
659 *ts_ns
= last_returned_ts_ns
;
663 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
665 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
666 "clock-snapshot-addr=%p", clock_snapshot
);
673 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
674 "using the last returned timestamp.");
675 *ts_ns
= last_returned_ts_ns
;
683 BT_COMP_LOGD("Found message's timestamp: "
684 "muxer-msg-iter-addr=%p, msg-addr=%p, "
685 "last-returned-ts=%" PRId64
", ts=%" PRId64
,
686 muxer_msg_iter
, msg
, last_returned_ts_ns
,
694 int validate_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
695 struct muxer_comp
*muxer_comp
,
696 const bt_clock_class
*clock_class
)
699 const uint8_t *cc_uuid
;
702 BT_ASSERT(clock_class
);
703 cc_uuid
= bt_clock_class_get_uuid(clock_class
);
704 cc_name
= bt_clock_class_get_name(clock_class
);
706 if (muxer_msg_iter
->clock_class_expectation
==
707 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
709 * This is the first clock class that this muxer
710 * message iterator encounters. Its properties
711 * determine what to expect for the whole lifetime of
712 * the iterator without a true
713 * `assume-absolute-clock-classes` parameter.
715 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
716 /* Expect absolute clock classes */
717 muxer_msg_iter
->clock_class_expectation
=
718 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
;
722 * Expect non-absolute clock classes
723 * with a specific UUID.
725 muxer_msg_iter
->clock_class_expectation
=
726 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
;
727 bt_uuid_copy(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
);
730 * Expect non-absolute clock classes
733 muxer_msg_iter
->clock_class_expectation
=
734 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
;
739 if (!muxer_comp
->assume_absolute_clock_classes
) {
740 switch (muxer_msg_iter
->clock_class_expectation
) {
741 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
:
742 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
743 BT_COMP_LOGE("Expecting an absolute clock class, "
744 "but got a non-absolute one: "
745 "clock-class-addr=%p, clock-class-name=\"%s\"",
746 clock_class
, cc_name
);
750 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
:
751 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
752 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
753 "but got an absolute one: "
754 "clock-class-addr=%p, clock-class-name=\"%s\"",
755 clock_class
, cc_name
);
760 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
761 "but got one with a UUID: "
762 "clock-class-addr=%p, clock-class-name=\"%s\", "
763 "uuid=\"" BT_UUID_FMT
"\"",
764 clock_class
, cc_name
, BT_UUID_FMT_VALUES(cc_uuid
));
768 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
:
769 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
770 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
771 "but got an absolute one: "
772 "clock-class-addr=%p, clock-class-name=\"%s\"",
773 clock_class
, cc_name
);
778 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
779 "but got one with no UUID: "
780 "clock-class-addr=%p, clock-class-name=\"%s\"",
781 clock_class
, cc_name
);
785 if (bt_uuid_compare(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
) != 0) {
786 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
787 "but got one with different UUID: "
788 "clock-class-addr=%p, clock-class-name=\"%s\", "
789 "expected-uuid=\"" BT_UUID_FMT
"\", "
790 "uuid=\"" BT_UUID_FMT
"\"",
791 clock_class
, cc_name
,
792 BT_UUID_FMT_VALUES(muxer_msg_iter
->expected_clock_class_uuid
),
793 BT_UUID_FMT_VALUES(cc_uuid
));
797 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
:
798 BT_COMP_LOGE("Expecting no clock class, but got one: "
799 "clock-class-addr=%p, clock-class-name=\"%s\"",
800 clock_class
, cc_name
);
804 BT_COMP_LOGF("Unexpected clock class expectation: "
805 "expectation-code=%d",
806 muxer_msg_iter
->clock_class_expectation
);
821 int validate_new_stream_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
822 struct muxer_comp
*muxer_comp
, const bt_stream
*stream
)
825 const bt_stream_class
*stream_class
=
826 bt_stream_borrow_class_const(stream
);
827 const bt_clock_class
*clock_class
=
828 bt_stream_class_borrow_default_clock_class_const(stream_class
);
831 if (muxer_msg_iter
->clock_class_expectation
==
832 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
833 /* Expect no clock class */
834 muxer_msg_iter
->clock_class_expectation
=
835 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
;
836 } else if (muxer_msg_iter
->clock_class_expectation
!=
837 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
) {
838 BT_COMP_LOGE("Expecting stream class without a default clock class: "
839 "stream-class-addr=%p, stream-class-name=\"%s\", "
840 "stream-class-id=%" PRIu64
,
841 stream_class
, bt_stream_class_get_name(stream_class
),
842 bt_stream_class_get_id(stream_class
));
849 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
, clock_class
);
856 * This function finds the youngest available message amongst the
857 * non-ended upstream message iterators and returns the upstream
858 * message iterator which has it, or
859 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
862 * This function does NOT:
864 * * Update any upstream message iterator.
865 * * Check the upstream message iterators to retry.
867 * On sucess, this function sets *muxer_upstream_msg_iter to the
868 * upstream message iterator of which the current message is
869 * the youngest, and sets *ts_ns to its time.
872 bt_component_class_message_iterator_next_method_status
873 muxer_msg_iter_youngest_upstream_msg_iter(
874 struct muxer_comp
*muxer_comp
,
875 struct muxer_msg_iter
*muxer_msg_iter
,
876 struct muxer_upstream_msg_iter
**muxer_upstream_msg_iter
,
881 int64_t youngest_ts_ns
= INT64_MAX
;
882 bt_component_class_message_iterator_next_method_status status
=
883 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
885 BT_ASSERT(muxer_comp
);
886 BT_ASSERT(muxer_msg_iter
);
887 BT_ASSERT(muxer_upstream_msg_iter
);
888 *muxer_upstream_msg_iter
= NULL
;
890 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
892 const bt_message
*msg
;
893 struct muxer_upstream_msg_iter
*cur_muxer_upstream_msg_iter
=
895 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
899 if (!cur_muxer_upstream_msg_iter
->msg_iter
) {
900 /* This upstream message iterator is ended */
901 BT_COMP_LOGT("Skipping ended upstream message iterator: "
902 "muxer-upstream-msg-iter-wrap-addr=%p",
903 cur_muxer_upstream_msg_iter
);
907 BT_ASSERT(cur_muxer_upstream_msg_iter
->msgs
->length
> 0);
908 msg
= g_queue_peek_head(cur_muxer_upstream_msg_iter
->msgs
);
911 if (G_UNLIKELY(bt_message_get_type(msg
) ==
912 BT_MESSAGE_TYPE_STREAM_BEGINNING
)) {
913 ret
= validate_new_stream_clock_class(
914 muxer_msg_iter
, muxer_comp
,
915 bt_message_stream_beginning_borrow_stream_const(
919 * validate_new_stream_clock_class() logs
922 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
925 } else if (G_UNLIKELY(bt_message_get_type(msg
) ==
926 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
)) {
927 const bt_clock_snapshot
*cs
;
929 cs
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
931 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
,
932 bt_clock_snapshot_borrow_clock_class_const(cs
));
934 /* validate_clock_class() logs errors */
935 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
940 ret
= get_msg_ts_ns(muxer_comp
, muxer_msg_iter
, msg
,
941 muxer_msg_iter
->last_returned_ts_ns
, &msg_ts_ns
);
943 /* get_msg_ts_ns() logs errors */
944 *muxer_upstream_msg_iter
= NULL
;
945 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
950 * Update the current message iterator if it has not been set
951 * yet, or if its current message has a timestamp smaller than
952 * the previously selected youngest message.
954 if (G_UNLIKELY(*muxer_upstream_msg_iter
== NULL
) ||
955 msg_ts_ns
< youngest_ts_ns
) {
956 *muxer_upstream_msg_iter
=
957 cur_muxer_upstream_msg_iter
;
958 youngest_ts_ns
= msg_ts_ns
;
959 *ts_ns
= youngest_ts_ns
;
960 } else if (msg_ts_ns
== youngest_ts_ns
) {
962 * The currently selected message to be sent downstream
963 * next has the exact same timestamp that of the
964 * current candidate message. We must break the tie
965 * in a predictable manner.
967 const bt_message
*selected_msg
= g_queue_peek_head(
968 (*muxer_upstream_msg_iter
)->msgs
);
969 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
972 * Order the messages in an arbitrary but determinitic
975 ret
= common_muxing_compare_messages(msg
, selected_msg
);
978 * The `msg` should go first. Update the next
979 * iterator and the current timestamp.
981 *muxer_upstream_msg_iter
=
982 cur_muxer_upstream_msg_iter
;
983 youngest_ts_ns
= msg_ts_ns
;
984 *ts_ns
= youngest_ts_ns
;
985 } else if (ret
== 0) {
986 /* Unable to pick which one should go first. */
987 BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
988 "muxer-upstream-msg-iter-wrap-addr=%p"
989 "cur-muxer-upstream-msg-iter-wrap-addr=%p",
990 *muxer_upstream_msg_iter
,
991 cur_muxer_upstream_msg_iter
);
996 if (!*muxer_upstream_msg_iter
) {
997 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1006 bt_component_class_message_iterator_next_method_status
1007 validate_muxer_upstream_msg_iter(
1008 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
1011 struct muxer_comp
*muxer_comp
=
1012 muxer_upstream_msg_iter
->muxer_comp
;
1013 bt_component_class_message_iterator_next_method_status status
=
1014 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1016 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
1017 "muxer-upstream-msg-iter-wrap-addr=%p",
1018 muxer_upstream_msg_iter
);
1020 if (muxer_upstream_msg_iter
->msgs
->length
> 0 ||
1021 !muxer_upstream_msg_iter
->msg_iter
) {
1022 BT_COMP_LOGD("Already valid or not considered: "
1023 "queue-len=%u, upstream-msg-iter-addr=%p",
1024 muxer_upstream_msg_iter
->msgs
->length
,
1025 muxer_upstream_msg_iter
->msg_iter
);
1029 /* muxer_upstream_msg_iter_next() logs details/errors */
1030 status
= muxer_upstream_msg_iter_next(muxer_upstream_msg_iter
,
1038 bt_component_class_message_iterator_next_method_status
1039 validate_muxer_upstream_msg_iters(
1040 struct muxer_msg_iter
*muxer_msg_iter
)
1042 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
1043 bt_component_class_message_iterator_next_method_status status
=
1044 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1047 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
1048 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1050 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
1052 bool is_ended
= false;
1053 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
1055 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1058 status
= validate_muxer_upstream_msg_iter(
1059 muxer_upstream_msg_iter
, &is_ended
);
1060 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1062 BT_COMP_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
1063 "muxer-msg-iter-addr=%p, "
1064 "muxer-upstream-msg-iter-wrap-addr=%p",
1066 muxer_upstream_msg_iter
);
1068 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
1069 "muxer-msg-iter-addr=%p, "
1070 "muxer-upstream-msg-iter-wrap-addr=%p",
1072 muxer_upstream_msg_iter
);
1079 * Move this muxer upstream message iterator to the
1080 * array of ended iterators if it's ended.
1082 if (G_UNLIKELY(is_ended
)) {
1083 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
1084 "muxer-msg-iter-addr=%p, "
1085 "muxer-upstream-msg-iter-wrap-addr=%p",
1086 muxer_msg_iter
, muxer_upstream_msg_iter
);
1088 muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1089 muxer_upstream_msg_iter
);
1090 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1093 * Use g_ptr_array_remove_fast() because the
1094 * order of those elements is not important.
1096 g_ptr_array_remove_index_fast(
1097 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1108 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next_one(
1109 struct muxer_comp
*muxer_comp
,
1110 struct muxer_msg_iter
*muxer_msg_iter
,
1111 const bt_message
**msg
)
1113 bt_component_class_message_iterator_next_method_status status
;
1114 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
= NULL
;
1115 int64_t next_return_ts
;
1117 status
= validate_muxer_upstream_msg_iters(muxer_msg_iter
);
1118 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1119 /* validate_muxer_upstream_msg_iters() logs details */
1124 * At this point we know that all the existing upstream
1125 * message iterators are valid. We can find the one,
1126 * amongst those, of which the current message is the
1129 status
= muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp
,
1130 muxer_msg_iter
, &muxer_upstream_msg_iter
,
1132 if (status
< 0 || status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
) {
1134 BT_COMP_LOGE("Cannot find the youngest upstream message iterator wrapper: "
1136 bt_common_func_status_string(status
));
1138 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
1140 bt_common_func_status_string(status
));
1146 if (next_return_ts
< muxer_msg_iter
->last_returned_ts_ns
) {
1147 BT_COMP_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
1148 "muxer-msg-iter-addr=%p, ts=%" PRId64
", "
1149 "last-returned-ts=%" PRId64
,
1150 muxer_msg_iter
, next_return_ts
,
1151 muxer_msg_iter
->last_returned_ts_ns
);
1152 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1156 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
1157 "muxer-msg-iter-addr=%p, "
1158 "muxer-upstream-msg-iter-wrap-addr=%p, "
1160 muxer_msg_iter
, muxer_upstream_msg_iter
, next_return_ts
);
1161 BT_ASSERT(status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
);
1162 BT_ASSERT(muxer_upstream_msg_iter
);
1165 * Consume from the queue's head: other side
1166 * (muxer_upstream_msg_iter_next()) writes to the tail.
1168 *msg
= g_queue_pop_head(muxer_upstream_msg_iter
->msgs
);
1170 muxer_msg_iter
->last_returned_ts_ns
= next_return_ts
;
1177 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next(
1178 struct muxer_comp
*muxer_comp
,
1179 struct muxer_msg_iter
*muxer_msg_iter
,
1180 bt_message_array_const msgs
, uint64_t capacity
,
1183 bt_component_class_message_iterator_next_method_status status
=
1184 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1187 while (i
< capacity
&& status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1188 status
= muxer_msg_iter_do_next_one(muxer_comp
,
1189 muxer_msg_iter
, &msgs
[i
]);
1190 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1197 * Even if muxer_msg_iter_do_next_one() returned
1198 * something else than
1199 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1200 * message objects in the output message
1201 * array, so we need to return
1202 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1203 * transfered to downstream. This other status occurs
1204 * again the next time muxer_msg_iter_do_next() is
1205 * called, possibly without any accumulated
1206 * message, in which case we'll return it.
1209 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1216 void destroy_muxer_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
)
1218 struct muxer_comp
*muxer_comp
;
1220 if (!muxer_msg_iter
) {
1224 muxer_comp
= muxer_msg_iter
->muxer_comp
;
1225 BT_COMP_LOGD("Destroying muxer component's message iterator: "
1226 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1228 if (muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1229 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
1231 muxer_msg_iter
->active_muxer_upstream_msg_iters
, TRUE
);
1234 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1235 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
1237 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, TRUE
);
1240 g_free(muxer_msg_iter
);
1244 int muxer_msg_iter_init_upstream_iterators(struct muxer_comp
*muxer_comp
,
1245 struct muxer_msg_iter
*muxer_msg_iter
)
1251 count
= bt_component_filter_get_input_port_count(
1252 bt_self_component_filter_as_component_filter(
1253 muxer_comp
->self_comp_flt
));
1255 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
1256 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1257 muxer_comp
, muxer_msg_iter
);
1261 for (i
= 0; i
< count
; i
++) {
1262 bt_self_component_port_input_message_iterator
*upstream_msg_iter
;
1263 bt_self_component_port_input
*self_port
=
1264 bt_self_component_filter_borrow_input_port_by_index(
1265 muxer_comp
->self_comp_flt
, i
);
1266 const bt_port
*port
;
1268 BT_ASSERT(self_port
);
1269 port
= bt_self_component_port_as_port(
1270 bt_self_component_port_input_as_self_component_port(
1274 if (!bt_port_is_connected(port
)) {
1275 /* Skip non-connected port */
1279 upstream_msg_iter
= create_msg_iter_on_input_port(muxer_comp
,
1280 muxer_msg_iter
, self_port
);
1281 if (!upstream_msg_iter
) {
1282 /* create_msg_iter_on_input_port() logs errors */
1283 BT_ASSERT(!upstream_msg_iter
);
1288 ret
= muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter
,
1290 bt_self_component_port_input_message_iterator_put_ref(
1293 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1303 bt_component_class_message_iterator_init_method_status
muxer_msg_iter_init(
1304 bt_self_message_iterator
*self_msg_iter
,
1305 bt_self_component_filter
*self_comp
,
1306 bt_self_component_port_output
*port
)
1308 struct muxer_comp
*muxer_comp
= NULL
;
1309 struct muxer_msg_iter
*muxer_msg_iter
= NULL
;
1310 bt_component_class_message_iterator_init_method_status status
=
1311 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1314 muxer_comp
= bt_self_component_get_data(
1315 bt_self_component_filter_as_self_component(self_comp
));
1316 BT_ASSERT(muxer_comp
);
1317 BT_COMP_LOGD("Initializing muxer component's message iterator: "
1318 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1319 self_comp
, muxer_comp
, self_msg_iter
);
1321 if (muxer_comp
->initializing_muxer_msg_iter
) {
1323 * Weird, unhandled situation detected: downstream
1324 * creates a muxer message iterator while creating
1325 * another muxer message iterator (same component).
1327 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
1328 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1329 self_comp
, muxer_comp
, self_msg_iter
);
1333 muxer_comp
->initializing_muxer_msg_iter
= true;
1334 muxer_msg_iter
= g_new0(struct muxer_msg_iter
, 1);
1335 if (!muxer_msg_iter
) {
1336 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
1340 muxer_msg_iter
->muxer_comp
= muxer_comp
;
1341 muxer_msg_iter
->self_msg_iter
= self_msg_iter
;
1342 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1343 muxer_msg_iter
->active_muxer_upstream_msg_iters
=
1344 g_ptr_array_new_with_free_func(
1345 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1346 if (!muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1347 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1351 muxer_msg_iter
->ended_muxer_upstream_msg_iters
=
1352 g_ptr_array_new_with_free_func(
1353 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1354 if (!muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1355 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1359 ret
= muxer_msg_iter_init_upstream_iterators(muxer_comp
,
1362 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
1363 "comp-addr=%p, muxer-comp-addr=%p, "
1364 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1365 self_comp
, muxer_comp
, muxer_msg_iter
,
1366 self_msg_iter
, ret
);
1370 bt_self_message_iterator_set_data(self_msg_iter
, muxer_msg_iter
);
1371 BT_COMP_LOGD("Initialized muxer component's message iterator: "
1372 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1374 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1378 destroy_muxer_msg_iter(muxer_msg_iter
);
1379 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
1380 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1383 muxer_comp
->initializing_muxer_msg_iter
= false;
1388 void muxer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1390 struct muxer_msg_iter
*muxer_msg_iter
=
1391 bt_self_message_iterator_get_data(self_msg_iter
);
1392 bt_self_component
*self_comp
= NULL
;
1393 struct muxer_comp
*muxer_comp
= NULL
;
1395 self_comp
= bt_self_message_iterator_borrow_component(
1397 BT_ASSERT(self_comp
);
1398 muxer_comp
= bt_self_component_get_data(self_comp
);
1399 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
1400 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1402 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1404 if (muxer_msg_iter
) {
1405 destroy_muxer_msg_iter(muxer_msg_iter
);
1410 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_next(
1411 bt_self_message_iterator
*self_msg_iter
,
1412 bt_message_array_const msgs
, uint64_t capacity
,
1415 bt_component_class_message_iterator_next_method_status status
;
1416 struct muxer_msg_iter
*muxer_msg_iter
=
1417 bt_self_message_iterator_get_data(self_msg_iter
);
1418 bt_self_component
*self_comp
= NULL
;
1419 struct muxer_comp
*muxer_comp
= NULL
;
1421 BT_ASSERT(muxer_msg_iter
);
1422 self_comp
= bt_self_message_iterator_borrow_component(
1424 BT_ASSERT(self_comp
);
1425 muxer_comp
= bt_self_component_get_data(self_comp
);
1426 BT_ASSERT(muxer_comp
);
1427 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
1428 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1430 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1432 status
= muxer_msg_iter_do_next(muxer_comp
, muxer_msg_iter
,
1433 msgs
, capacity
, count
);
1435 BT_COMP_LOGE("Cannot get next message: "
1436 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1437 "msg-iter-addr=%p, status=%s",
1438 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
,
1439 bt_common_func_status_string(status
));
1441 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
1443 bt_common_func_status_string(status
));
1450 bt_component_class_port_connected_method_status
muxer_input_port_connected(
1451 bt_self_component_filter
*self_comp
,
1452 bt_self_component_port_input
*self_port
,
1453 const bt_port_output
*other_port
)
1455 bt_component_class_port_connected_method_status status
=
1456 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK
;
1457 bt_self_component_add_port_status add_port_status
;
1458 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
1459 bt_self_component_filter_as_self_component(self_comp
));
1461 add_port_status
= add_available_input_port(self_comp
);
1462 if (add_port_status
) {
1463 BT_COMP_LOGE("Cannot add one muxer component's input port: "
1465 bt_common_func_status_string(status
));
1467 if (add_port_status
==
1468 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
1469 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR
;
1471 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR
;
1482 bt_bool
muxer_upstream_msg_iters_can_all_seek_beginning(
1483 GPtrArray
*muxer_upstream_msg_iters
)
1486 bt_bool ret
= BT_TRUE
;
1488 for (i
= 0; i
< muxer_upstream_msg_iters
->len
; i
++) {
1489 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1490 muxer_upstream_msg_iters
->pdata
[i
];
1492 if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
1493 upstream_msg_iter
->msg_iter
)) {
1504 bt_bool
muxer_msg_iter_can_seek_beginning(
1505 bt_self_message_iterator
*self_msg_iter
)
1507 struct muxer_msg_iter
*muxer_msg_iter
=
1508 bt_self_message_iterator_get_data(self_msg_iter
);
1509 bt_bool ret
= BT_TRUE
;
1511 if (!muxer_upstream_msg_iters_can_all_seek_beginning(
1512 muxer_msg_iter
->active_muxer_upstream_msg_iters
)) {
1517 if (!muxer_upstream_msg_iters_can_all_seek_beginning(
1518 muxer_msg_iter
->ended_muxer_upstream_msg_iters
)) {
1528 bt_component_class_message_iterator_seek_beginning_method_status
muxer_msg_iter_seek_beginning(
1529 bt_self_message_iterator
*self_msg_iter
)
1531 struct muxer_msg_iter
*muxer_msg_iter
=
1532 bt_self_message_iterator_get_data(self_msg_iter
);
1533 bt_component_class_message_iterator_seek_beginning_method_status status
=
1534 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK
;
1535 bt_message_iterator_seek_beginning_status seek_beg_status
;
1538 /* Seek all ended upstream iterators first */
1539 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1541 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1542 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1544 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1545 upstream_msg_iter
->msg_iter
);
1546 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1547 status
= (int) seek_beg_status
;
1551 empty_message_queue(upstream_msg_iter
);
1554 /* Seek all previously active upstream iterators */
1555 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
1557 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1558 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
];
1560 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1561 upstream_msg_iter
->msg_iter
);
1562 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1563 status
= (int) seek_beg_status
;
1567 empty_message_queue(upstream_msg_iter
);
1570 /* Make them all active */
1571 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1573 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1574 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1576 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1578 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1582 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1583 * called on an empty array.
1585 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
> 0) {
1586 g_ptr_array_remove_range(muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1587 0, muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
);
1589 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1590 muxer_msg_iter
->clock_class_expectation
=
1591 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
;