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_create_from_message_iterator_status
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
,
437 bt_self_component_port_input_message_iterator
**msg_iter
)
439 const bt_port
*port
= bt_self_component_port_as_port(
440 bt_self_component_port_input_as_self_component_port(
442 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
446 BT_ASSERT(bt_port_is_connected(port
));
448 // TODO: Advance the iterator to >= the time of the latest
449 // returned message by the muxer message
450 // iterator which creates it.
451 status
= bt_self_component_port_input_message_iterator_create_from_message_iterator(
452 muxer_msg_iter
->self_msg_iter
, self_port
, msg_iter
);
453 if (status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
454 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
455 "port-addr=%p, port-name=\"%s\"",
456 port
, bt_port_get_name(port
));
460 BT_COMP_LOGI("Created upstream message iterator on input port: "
461 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
462 port
, bt_port_get_name(port
), msg_iter
);
469 bt_component_class_message_iterator_next_method_status
muxer_upstream_msg_iter_next(
470 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
473 struct muxer_comp
*muxer_comp
=
474 muxer_upstream_msg_iter
->muxer_comp
;
475 bt_component_class_message_iterator_next_method_status status
;
476 bt_message_iterator_next_status input_port_iter_status
;
477 bt_message_array_const msgs
;
481 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
482 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
483 muxer_upstream_msg_iter
,
484 muxer_upstream_msg_iter
->msg_iter
);
485 input_port_iter_status
= bt_self_component_port_input_message_iterator_next(
486 muxer_upstream_msg_iter
->msg_iter
, &msgs
, &count
);
487 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
489 bt_common_func_status_string(input_port_iter_status
));
491 switch (input_port_iter_status
) {
492 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
494 * Message iterator's current message is
495 * valid: it must be considered for muxing operations.
497 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
498 BT_ASSERT(count
> 0);
500 /* Move messages to our queue */
501 for (i
= 0; i
< count
; i
++) {
503 * Push to tail in order; other side
504 * (muxer_msg_iter_do_next_one()) consumes
505 * from the head first.
507 g_queue_push_tail(muxer_upstream_msg_iter
->msgs
,
510 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
512 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
514 * Message iterator's current message is not
515 * valid anymore. Return
516 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
518 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
520 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
: /* Fall-through. */
522 * Message iterator reached the end: release it. It
523 * won't be considered again to find the youngest
527 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
530 /* Error or unsupported status code */
531 BT_COMP_LOGE("Error or unsupported status code: "
532 "status-code=%d", input_port_iter_status
);
533 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
541 int get_msg_ts_ns(struct muxer_comp
*muxer_comp
,
542 struct muxer_msg_iter
*muxer_msg_iter
,
543 const bt_message
*msg
, int64_t last_returned_ts_ns
,
546 const bt_clock_snapshot
*clock_snapshot
= NULL
;
548 const bt_stream_class
*stream_class
= NULL
;
549 bt_message_type msg_type
;
553 BT_COMP_LOGD("Getting message's timestamp: "
554 "muxer-msg-iter-addr=%p, msg-addr=%p, "
555 "last-returned-ts=%" PRId64
,
556 muxer_msg_iter
, msg
, last_returned_ts_ns
);
558 if (G_UNLIKELY(muxer_msg_iter
->clock_class_expectation
==
559 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
)) {
560 *ts_ns
= last_returned_ts_ns
;
564 msg_type
= bt_message_get_type(msg
);
566 if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
)) {
567 stream_class
= bt_stream_borrow_class_const(
568 bt_packet_borrow_stream_const(
569 bt_message_packet_beginning_borrow_packet_const(
571 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_END
)) {
572 stream_class
= bt_stream_borrow_class_const(
573 bt_packet_borrow_stream_const(
574 bt_message_packet_end_borrow_packet_const(
576 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
)) {
577 stream_class
= bt_stream_borrow_class_const(
578 bt_message_discarded_events_borrow_stream_const(msg
));
579 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
)) {
580 stream_class
= bt_stream_borrow_class_const(
581 bt_message_discarded_packets_borrow_stream_const(msg
));
585 case BT_MESSAGE_TYPE_EVENT
:
586 BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const(
588 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
591 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
592 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
594 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
597 goto no_clock_snapshot
;
601 case BT_MESSAGE_TYPE_PACKET_END
:
602 if (bt_stream_class_packets_have_end_default_clock_snapshot(
604 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
607 goto no_clock_snapshot
;
611 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
613 enum bt_message_stream_clock_snapshot_state snapshot_state
=
614 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
615 msg
, &clock_snapshot
);
616 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
617 goto no_clock_snapshot
;
622 case BT_MESSAGE_TYPE_STREAM_END
:
624 enum bt_message_stream_clock_snapshot_state snapshot_state
=
625 bt_message_stream_end_borrow_default_clock_snapshot_const(
626 msg
, &clock_snapshot
);
627 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
628 goto no_clock_snapshot
;
633 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
634 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
636 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
639 goto no_clock_snapshot
;
643 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
644 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
646 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
649 goto no_clock_snapshot
;
653 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
654 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
658 /* All the other messages have a higher priority */
659 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
660 *ts_ns
= last_returned_ts_ns
;
664 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
666 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
667 "clock-snapshot-addr=%p", clock_snapshot
);
674 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
675 "using the last returned timestamp.");
676 *ts_ns
= last_returned_ts_ns
;
684 BT_COMP_LOGD("Found message's timestamp: "
685 "muxer-msg-iter-addr=%p, msg-addr=%p, "
686 "last-returned-ts=%" PRId64
", ts=%" PRId64
,
687 muxer_msg_iter
, msg
, last_returned_ts_ns
,
695 int validate_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
696 struct muxer_comp
*muxer_comp
,
697 const bt_clock_class
*clock_class
)
700 const uint8_t *cc_uuid
;
703 BT_ASSERT(clock_class
);
704 cc_uuid
= bt_clock_class_get_uuid(clock_class
);
705 cc_name
= bt_clock_class_get_name(clock_class
);
707 if (muxer_msg_iter
->clock_class_expectation
==
708 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
710 * This is the first clock class that this muxer
711 * message iterator encounters. Its properties
712 * determine what to expect for the whole lifetime of
713 * the iterator without a true
714 * `assume-absolute-clock-classes` parameter.
716 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
717 /* Expect absolute clock classes */
718 muxer_msg_iter
->clock_class_expectation
=
719 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
;
723 * Expect non-absolute clock classes
724 * with a specific UUID.
726 muxer_msg_iter
->clock_class_expectation
=
727 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
;
728 bt_uuid_copy(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
);
731 * Expect non-absolute clock classes
734 muxer_msg_iter
->clock_class_expectation
=
735 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
;
740 if (!muxer_comp
->assume_absolute_clock_classes
) {
741 switch (muxer_msg_iter
->clock_class_expectation
) {
742 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
:
743 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
744 BT_COMP_LOGE("Expecting an absolute clock class, "
745 "but got a non-absolute one: "
746 "clock-class-addr=%p, clock-class-name=\"%s\"",
747 clock_class
, cc_name
);
751 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
:
752 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
753 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
754 "but got an absolute one: "
755 "clock-class-addr=%p, clock-class-name=\"%s\"",
756 clock_class
, cc_name
);
761 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
762 "but got one with a UUID: "
763 "clock-class-addr=%p, clock-class-name=\"%s\", "
764 "uuid=\"" BT_UUID_FMT
"\"",
765 clock_class
, cc_name
, BT_UUID_FMT_VALUES(cc_uuid
));
769 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
:
770 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
771 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
772 "but got an absolute one: "
773 "clock-class-addr=%p, clock-class-name=\"%s\"",
774 clock_class
, cc_name
);
779 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
780 "but got one with no UUID: "
781 "clock-class-addr=%p, clock-class-name=\"%s\"",
782 clock_class
, cc_name
);
786 if (bt_uuid_compare(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
) != 0) {
787 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
788 "but got one with different UUID: "
789 "clock-class-addr=%p, clock-class-name=\"%s\", "
790 "expected-uuid=\"" BT_UUID_FMT
"\", "
791 "uuid=\"" BT_UUID_FMT
"\"",
792 clock_class
, cc_name
,
793 BT_UUID_FMT_VALUES(muxer_msg_iter
->expected_clock_class_uuid
),
794 BT_UUID_FMT_VALUES(cc_uuid
));
798 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
:
799 BT_COMP_LOGE("Expecting no clock class, but got one: "
800 "clock-class-addr=%p, clock-class-name=\"%s\"",
801 clock_class
, cc_name
);
805 BT_COMP_LOGF("Unexpected clock class expectation: "
806 "expectation-code=%d",
807 muxer_msg_iter
->clock_class_expectation
);
822 int validate_new_stream_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
823 struct muxer_comp
*muxer_comp
, const bt_stream
*stream
)
826 const bt_stream_class
*stream_class
=
827 bt_stream_borrow_class_const(stream
);
828 const bt_clock_class
*clock_class
=
829 bt_stream_class_borrow_default_clock_class_const(stream_class
);
832 if (muxer_msg_iter
->clock_class_expectation
==
833 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
834 /* Expect no clock class */
835 muxer_msg_iter
->clock_class_expectation
=
836 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
;
837 } else if (muxer_msg_iter
->clock_class_expectation
!=
838 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
) {
839 BT_COMP_LOGE("Expecting stream class without a default clock class: "
840 "stream-class-addr=%p, stream-class-name=\"%s\", "
841 "stream-class-id=%" PRIu64
,
842 stream_class
, bt_stream_class_get_name(stream_class
),
843 bt_stream_class_get_id(stream_class
));
850 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
, clock_class
);
857 * This function finds the youngest available message amongst the
858 * non-ended upstream message iterators and returns the upstream
859 * message iterator which has it, or
860 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
863 * This function does NOT:
865 * * Update any upstream message iterator.
866 * * Check the upstream message iterators to retry.
868 * On sucess, this function sets *muxer_upstream_msg_iter to the
869 * upstream message iterator of which the current message is
870 * the youngest, and sets *ts_ns to its time.
873 bt_component_class_message_iterator_next_method_status
874 muxer_msg_iter_youngest_upstream_msg_iter(
875 struct muxer_comp
*muxer_comp
,
876 struct muxer_msg_iter
*muxer_msg_iter
,
877 struct muxer_upstream_msg_iter
**muxer_upstream_msg_iter
,
882 int64_t youngest_ts_ns
= INT64_MAX
;
883 bt_component_class_message_iterator_next_method_status status
=
884 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
886 BT_ASSERT(muxer_comp
);
887 BT_ASSERT(muxer_msg_iter
);
888 BT_ASSERT(muxer_upstream_msg_iter
);
889 *muxer_upstream_msg_iter
= NULL
;
891 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
893 const bt_message
*msg
;
894 struct muxer_upstream_msg_iter
*cur_muxer_upstream_msg_iter
=
896 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
900 if (!cur_muxer_upstream_msg_iter
->msg_iter
) {
901 /* This upstream message iterator is ended */
902 BT_COMP_LOGT("Skipping ended upstream message iterator: "
903 "muxer-upstream-msg-iter-wrap-addr=%p",
904 cur_muxer_upstream_msg_iter
);
908 BT_ASSERT(cur_muxer_upstream_msg_iter
->msgs
->length
> 0);
909 msg
= g_queue_peek_head(cur_muxer_upstream_msg_iter
->msgs
);
912 if (G_UNLIKELY(bt_message_get_type(msg
) ==
913 BT_MESSAGE_TYPE_STREAM_BEGINNING
)) {
914 ret
= validate_new_stream_clock_class(
915 muxer_msg_iter
, muxer_comp
,
916 bt_message_stream_beginning_borrow_stream_const(
920 * validate_new_stream_clock_class() logs
923 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
926 } else if (G_UNLIKELY(bt_message_get_type(msg
) ==
927 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
)) {
928 const bt_clock_snapshot
*cs
;
930 cs
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
932 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
,
933 bt_clock_snapshot_borrow_clock_class_const(cs
));
935 /* validate_clock_class() logs errors */
936 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
941 ret
= get_msg_ts_ns(muxer_comp
, muxer_msg_iter
, msg
,
942 muxer_msg_iter
->last_returned_ts_ns
, &msg_ts_ns
);
944 /* get_msg_ts_ns() logs errors */
945 *muxer_upstream_msg_iter
= NULL
;
946 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
951 * Update the current message iterator if it has not been set
952 * yet, or if its current message has a timestamp smaller than
953 * the previously selected youngest message.
955 if (G_UNLIKELY(*muxer_upstream_msg_iter
== NULL
) ||
956 msg_ts_ns
< youngest_ts_ns
) {
957 *muxer_upstream_msg_iter
=
958 cur_muxer_upstream_msg_iter
;
959 youngest_ts_ns
= msg_ts_ns
;
960 *ts_ns
= youngest_ts_ns
;
961 } else if (msg_ts_ns
== youngest_ts_ns
) {
963 * The currently selected message to be sent downstream
964 * next has the exact same timestamp that of the
965 * current candidate message. We must break the tie
966 * in a predictable manner.
968 const bt_message
*selected_msg
= g_queue_peek_head(
969 (*muxer_upstream_msg_iter
)->msgs
);
970 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
973 * Order the messages in an arbitrary but determinitic
976 ret
= common_muxing_compare_messages(msg
, selected_msg
);
979 * The `msg` should go first. Update the next
980 * iterator and the current timestamp.
982 *muxer_upstream_msg_iter
=
983 cur_muxer_upstream_msg_iter
;
984 youngest_ts_ns
= msg_ts_ns
;
985 *ts_ns
= youngest_ts_ns
;
986 } else if (ret
== 0) {
987 /* Unable to pick which one should go first. */
988 BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
989 "muxer-upstream-msg-iter-wrap-addr=%p"
990 "cur-muxer-upstream-msg-iter-wrap-addr=%p",
991 *muxer_upstream_msg_iter
,
992 cur_muxer_upstream_msg_iter
);
997 if (!*muxer_upstream_msg_iter
) {
998 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1007 bt_component_class_message_iterator_next_method_status
1008 validate_muxer_upstream_msg_iter(
1009 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
1012 struct muxer_comp
*muxer_comp
=
1013 muxer_upstream_msg_iter
->muxer_comp
;
1014 bt_component_class_message_iterator_next_method_status status
=
1015 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1017 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
1018 "muxer-upstream-msg-iter-wrap-addr=%p",
1019 muxer_upstream_msg_iter
);
1021 if (muxer_upstream_msg_iter
->msgs
->length
> 0 ||
1022 !muxer_upstream_msg_iter
->msg_iter
) {
1023 BT_COMP_LOGD("Already valid or not considered: "
1024 "queue-len=%u, upstream-msg-iter-addr=%p",
1025 muxer_upstream_msg_iter
->msgs
->length
,
1026 muxer_upstream_msg_iter
->msg_iter
);
1030 /* muxer_upstream_msg_iter_next() logs details/errors */
1031 status
= muxer_upstream_msg_iter_next(muxer_upstream_msg_iter
,
1039 bt_component_class_message_iterator_next_method_status
1040 validate_muxer_upstream_msg_iters(
1041 struct muxer_msg_iter
*muxer_msg_iter
)
1043 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
1044 bt_component_class_message_iterator_next_method_status status
=
1045 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1048 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
1049 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1051 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
1053 bool is_ended
= false;
1054 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
1056 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1059 status
= validate_muxer_upstream_msg_iter(
1060 muxer_upstream_msg_iter
, &is_ended
);
1061 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1063 BT_COMP_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
1064 "muxer-msg-iter-addr=%p, "
1065 "muxer-upstream-msg-iter-wrap-addr=%p",
1067 muxer_upstream_msg_iter
);
1069 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
1070 "muxer-msg-iter-addr=%p, "
1071 "muxer-upstream-msg-iter-wrap-addr=%p",
1073 muxer_upstream_msg_iter
);
1080 * Move this muxer upstream message iterator to the
1081 * array of ended iterators if it's ended.
1083 if (G_UNLIKELY(is_ended
)) {
1084 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
1085 "muxer-msg-iter-addr=%p, "
1086 "muxer-upstream-msg-iter-wrap-addr=%p",
1087 muxer_msg_iter
, muxer_upstream_msg_iter
);
1089 muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1090 muxer_upstream_msg_iter
);
1091 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1094 * Use g_ptr_array_remove_fast() because the
1095 * order of those elements is not important.
1097 g_ptr_array_remove_index_fast(
1098 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1109 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next_one(
1110 struct muxer_comp
*muxer_comp
,
1111 struct muxer_msg_iter
*muxer_msg_iter
,
1112 const bt_message
**msg
)
1114 bt_component_class_message_iterator_next_method_status status
;
1115 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
= NULL
;
1116 int64_t next_return_ts
;
1118 status
= validate_muxer_upstream_msg_iters(muxer_msg_iter
);
1119 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1120 /* validate_muxer_upstream_msg_iters() logs details */
1125 * At this point we know that all the existing upstream
1126 * message iterators are valid. We can find the one,
1127 * amongst those, of which the current message is the
1130 status
= muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp
,
1131 muxer_msg_iter
, &muxer_upstream_msg_iter
,
1133 if (status
< 0 || status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
) {
1135 BT_COMP_LOGE("Cannot find the youngest upstream message iterator wrapper: "
1137 bt_common_func_status_string(status
));
1139 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
1141 bt_common_func_status_string(status
));
1147 if (next_return_ts
< muxer_msg_iter
->last_returned_ts_ns
) {
1148 BT_COMP_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
1149 "muxer-msg-iter-addr=%p, ts=%" PRId64
", "
1150 "last-returned-ts=%" PRId64
,
1151 muxer_msg_iter
, next_return_ts
,
1152 muxer_msg_iter
->last_returned_ts_ns
);
1153 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1157 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
1158 "muxer-msg-iter-addr=%p, "
1159 "muxer-upstream-msg-iter-wrap-addr=%p, "
1161 muxer_msg_iter
, muxer_upstream_msg_iter
, next_return_ts
);
1162 BT_ASSERT(status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
);
1163 BT_ASSERT(muxer_upstream_msg_iter
);
1166 * Consume from the queue's head: other side
1167 * (muxer_upstream_msg_iter_next()) writes to the tail.
1169 *msg
= g_queue_pop_head(muxer_upstream_msg_iter
->msgs
);
1171 muxer_msg_iter
->last_returned_ts_ns
= next_return_ts
;
1178 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next(
1179 struct muxer_comp
*muxer_comp
,
1180 struct muxer_msg_iter
*muxer_msg_iter
,
1181 bt_message_array_const msgs
, uint64_t capacity
,
1184 bt_component_class_message_iterator_next_method_status status
=
1185 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1188 while (i
< capacity
&& status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1189 status
= muxer_msg_iter_do_next_one(muxer_comp
,
1190 muxer_msg_iter
, &msgs
[i
]);
1191 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1198 * Even if muxer_msg_iter_do_next_one() returned
1199 * something else than
1200 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1201 * message objects in the output message
1202 * array, so we need to return
1203 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1204 * transfered to downstream. This other status occurs
1205 * again the next time muxer_msg_iter_do_next() is
1206 * called, possibly without any accumulated
1207 * message, in which case we'll return it.
1210 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1217 void destroy_muxer_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
)
1219 struct muxer_comp
*muxer_comp
;
1221 if (!muxer_msg_iter
) {
1225 muxer_comp
= muxer_msg_iter
->muxer_comp
;
1226 BT_COMP_LOGD("Destroying muxer component's message iterator: "
1227 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1229 if (muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1230 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
1232 muxer_msg_iter
->active_muxer_upstream_msg_iters
, TRUE
);
1235 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1236 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
1238 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, TRUE
);
1241 g_free(muxer_msg_iter
);
1245 bt_component_class_message_iterator_init_method_status
1246 muxer_msg_iter_init_upstream_iterators(struct muxer_comp
*muxer_comp
,
1247 struct muxer_msg_iter
*muxer_msg_iter
)
1251 bt_component_class_message_iterator_init_method_status status
;
1253 count
= bt_component_filter_get_input_port_count(
1254 bt_self_component_filter_as_component_filter(
1255 muxer_comp
->self_comp_flt
));
1257 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
1258 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1259 muxer_comp
, muxer_msg_iter
);
1260 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1264 for (i
= 0; i
< count
; i
++) {
1265 bt_self_component_port_input_message_iterator
*upstream_msg_iter
;
1266 bt_self_component_port_input
*self_port
=
1267 bt_self_component_filter_borrow_input_port_by_index(
1268 muxer_comp
->self_comp_flt
, i
);
1269 const bt_port
*port
;
1270 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
1274 BT_ASSERT(self_port
);
1275 port
= bt_self_component_port_as_port(
1276 bt_self_component_port_input_as_self_component_port(
1280 if (!bt_port_is_connected(port
)) {
1281 /* Skip non-connected port */
1285 msg_iter_status
= create_msg_iter_on_input_port(muxer_comp
,
1286 muxer_msg_iter
, self_port
, &upstream_msg_iter
);
1287 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
1288 /* create_msg_iter_on_input_port() logs errors */
1289 status
= (int) msg_iter_status
;
1293 int_status
= muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter
,
1295 bt_self_component_port_input_message_iterator_put_ref(
1298 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1299 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1304 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1311 bt_component_class_message_iterator_init_method_status
muxer_msg_iter_init(
1312 bt_self_message_iterator
*self_msg_iter
,
1313 bt_self_component_filter
*self_comp
,
1314 bt_self_component_port_output
*port
)
1316 struct muxer_comp
*muxer_comp
= NULL
;
1317 struct muxer_msg_iter
*muxer_msg_iter
= NULL
;
1318 bt_component_class_message_iterator_init_method_status status
;
1320 muxer_comp
= bt_self_component_get_data(
1321 bt_self_component_filter_as_self_component(self_comp
));
1322 BT_ASSERT(muxer_comp
);
1323 BT_COMP_LOGD("Initializing muxer component's message iterator: "
1324 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1325 self_comp
, muxer_comp
, self_msg_iter
);
1327 if (muxer_comp
->initializing_muxer_msg_iter
) {
1329 * Weird, unhandled situation detected: downstream
1330 * creates a muxer message iterator while creating
1331 * another muxer message iterator (same component).
1333 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
1334 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1335 self_comp
, muxer_comp
, self_msg_iter
);
1336 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1340 muxer_comp
->initializing_muxer_msg_iter
= true;
1341 muxer_msg_iter
= g_new0(struct muxer_msg_iter
, 1);
1342 if (!muxer_msg_iter
) {
1343 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
1344 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
1348 muxer_msg_iter
->muxer_comp
= muxer_comp
;
1349 muxer_msg_iter
->self_msg_iter
= self_msg_iter
;
1350 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1351 muxer_msg_iter
->active_muxer_upstream_msg_iters
=
1352 g_ptr_array_new_with_free_func(
1353 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1354 if (!muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1355 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1356 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
1360 muxer_msg_iter
->ended_muxer_upstream_msg_iters
=
1361 g_ptr_array_new_with_free_func(
1362 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1363 if (!muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1364 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1365 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
1369 status
= muxer_msg_iter_init_upstream_iterators(muxer_comp
,
1372 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
1373 "comp-addr=%p, muxer-comp-addr=%p, "
1374 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1375 self_comp
, muxer_comp
, muxer_msg_iter
,
1376 self_msg_iter
, status
);
1380 bt_self_message_iterator_set_data(self_msg_iter
, muxer_msg_iter
);
1381 BT_COMP_LOGD("Initialized muxer component's message iterator: "
1382 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1384 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1388 destroy_muxer_msg_iter(muxer_msg_iter
);
1389 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
1392 muxer_comp
->initializing_muxer_msg_iter
= false;
1397 void muxer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1399 struct muxer_msg_iter
*muxer_msg_iter
=
1400 bt_self_message_iterator_get_data(self_msg_iter
);
1401 bt_self_component
*self_comp
= NULL
;
1402 struct muxer_comp
*muxer_comp
= NULL
;
1404 self_comp
= bt_self_message_iterator_borrow_component(
1406 BT_ASSERT(self_comp
);
1407 muxer_comp
= bt_self_component_get_data(self_comp
);
1408 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
1409 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1411 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1413 if (muxer_msg_iter
) {
1414 destroy_muxer_msg_iter(muxer_msg_iter
);
1419 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_next(
1420 bt_self_message_iterator
*self_msg_iter
,
1421 bt_message_array_const msgs
, uint64_t capacity
,
1424 bt_component_class_message_iterator_next_method_status status
;
1425 struct muxer_msg_iter
*muxer_msg_iter
=
1426 bt_self_message_iterator_get_data(self_msg_iter
);
1427 bt_self_component
*self_comp
= NULL
;
1428 struct muxer_comp
*muxer_comp
= NULL
;
1430 BT_ASSERT(muxer_msg_iter
);
1431 self_comp
= bt_self_message_iterator_borrow_component(
1433 BT_ASSERT(self_comp
);
1434 muxer_comp
= bt_self_component_get_data(self_comp
);
1435 BT_ASSERT(muxer_comp
);
1436 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
1437 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1439 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1441 status
= muxer_msg_iter_do_next(muxer_comp
, muxer_msg_iter
,
1442 msgs
, capacity
, count
);
1444 BT_COMP_LOGE("Cannot get next message: "
1445 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1446 "msg-iter-addr=%p, status=%s",
1447 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
,
1448 bt_common_func_status_string(status
));
1450 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
1452 bt_common_func_status_string(status
));
1459 bt_component_class_port_connected_method_status
muxer_input_port_connected(
1460 bt_self_component_filter
*self_comp
,
1461 bt_self_component_port_input
*self_port
,
1462 const bt_port_output
*other_port
)
1464 bt_component_class_port_connected_method_status status
=
1465 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK
;
1466 bt_self_component_add_port_status add_port_status
;
1467 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
1468 bt_self_component_filter_as_self_component(self_comp
));
1470 add_port_status
= add_available_input_port(self_comp
);
1471 if (add_port_status
) {
1472 BT_COMP_LOGE("Cannot add one muxer component's input port: "
1474 bt_common_func_status_string(status
));
1476 if (add_port_status
==
1477 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
1478 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR
;
1480 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR
;
1491 bt_component_class_message_iterator_can_seek_beginning_method_status
1492 muxer_upstream_msg_iters_can_all_seek_beginning(
1493 GPtrArray
*muxer_upstream_msg_iters
, bt_bool
*can_seek
)
1495 bt_component_class_message_iterator_can_seek_beginning_method_status status
;
1498 for (i
= 0; i
< muxer_upstream_msg_iters
->len
; i
++) {
1499 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1500 muxer_upstream_msg_iters
->pdata
[i
];
1501 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1502 upstream_msg_iter
->msg_iter
, can_seek
);
1503 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
) {
1512 *can_seek
= BT_TRUE
;
1519 bt_component_class_message_iterator_can_seek_beginning_method_status
1520 muxer_msg_iter_can_seek_beginning(
1521 bt_self_message_iterator
*self_msg_iter
, bt_bool
*can_seek
)
1523 struct muxer_msg_iter
*muxer_msg_iter
=
1524 bt_self_message_iterator_get_data(self_msg_iter
);
1525 bt_component_class_message_iterator_can_seek_beginning_method_status status
;
1527 status
= muxer_upstream_msg_iters_can_all_seek_beginning(
1528 muxer_msg_iter
->active_muxer_upstream_msg_iters
, can_seek
);
1529 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
) {
1537 status
= muxer_upstream_msg_iters_can_all_seek_beginning(
1538 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, can_seek
);
1545 bt_component_class_message_iterator_seek_beginning_method_status
muxer_msg_iter_seek_beginning(
1546 bt_self_message_iterator
*self_msg_iter
)
1548 struct muxer_msg_iter
*muxer_msg_iter
=
1549 bt_self_message_iterator_get_data(self_msg_iter
);
1550 bt_component_class_message_iterator_seek_beginning_method_status status
=
1551 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK
;
1552 bt_message_iterator_seek_beginning_status seek_beg_status
;
1555 /* Seek all ended upstream iterators first */
1556 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1558 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1559 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1561 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1562 upstream_msg_iter
->msg_iter
);
1563 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1564 status
= (int) seek_beg_status
;
1568 empty_message_queue(upstream_msg_iter
);
1571 /* Seek all previously active upstream iterators */
1572 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
1574 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1575 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
];
1577 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1578 upstream_msg_iter
->msg_iter
);
1579 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1580 status
= (int) seek_beg_status
;
1584 empty_message_queue(upstream_msg_iter
);
1587 /* Make them all active */
1588 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1590 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1591 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1593 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1595 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1599 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1600 * called on an empty array.
1602 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
> 0) {
1603 g_ptr_array_remove_range(muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1604 0, muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
);
1606 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1607 muxer_msg_iter
->clock_class_expectation
=
1608 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
;