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 "plugins/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"
41 #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes"
45 bt_self_component_filter
*self_comp_flt
;
46 bt_self_component
*self_comp
;
48 unsigned int next_port_num
;
49 size_t available_input_ports
;
50 bool initializing_muxer_msg_iter
;
51 bool assume_absolute_clock_classes
;
52 bt_logging_level log_level
;
55 struct muxer_upstream_msg_iter
{
56 struct muxer_comp
*muxer_comp
;
58 /* Owned by this, NULL if ended */
59 bt_self_component_port_input_message_iterator
*msg_iter
;
61 /* Contains `const bt_message *`, owned by this */
65 enum muxer_msg_iter_clock_class_expectation
{
66 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
= 0,
67 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
,
68 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
,
69 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
,
70 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
,
73 struct muxer_msg_iter
{
74 struct muxer_comp
*muxer_comp
;
77 * Array of struct muxer_upstream_msg_iter * (owned by this).
79 * NOTE: This array is searched in linearly to find the youngest
80 * current message. Keep this until benchmarks confirm that
81 * another data structure is faster than this for our typical
84 GPtrArray
*active_muxer_upstream_msg_iters
;
87 * Array of struct muxer_upstream_msg_iter * (owned by this).
89 * We move ended message iterators from
90 * `active_muxer_upstream_msg_iters` to this array so as to be
91 * able to restore them when seeking.
93 GPtrArray
*ended_muxer_upstream_msg_iters
;
95 /* Last time returned in a message */
96 int64_t last_returned_ts_ns
;
98 /* Clock class expectation state */
99 enum muxer_msg_iter_clock_class_expectation clock_class_expectation
;
102 * Expected clock class UUID, only valid when
103 * clock_class_expectation is
104 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
106 bt_uuid_t expected_clock_class_uuid
;
110 void empty_message_queue(struct muxer_upstream_msg_iter
*upstream_msg_iter
)
112 const bt_message
*msg
;
114 while ((msg
= g_queue_pop_head(upstream_msg_iter
->msgs
))) {
115 bt_message_put_ref(msg
);
120 void destroy_muxer_upstream_msg_iter(
121 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
)
123 struct muxer_comp
*muxer_comp
;
125 if (!muxer_upstream_msg_iter
) {
129 muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
130 BT_COMP_LOGD("Destroying muxer's upstream message iterator wrapper: "
131 "addr=%p, msg-iter-addr=%p, queue-len=%u",
132 muxer_upstream_msg_iter
,
133 muxer_upstream_msg_iter
->msg_iter
,
134 muxer_upstream_msg_iter
->msgs
->length
);
135 bt_self_component_port_input_message_iterator_put_ref(
136 muxer_upstream_msg_iter
->msg_iter
);
138 if (muxer_upstream_msg_iter
->msgs
) {
139 empty_message_queue(muxer_upstream_msg_iter
);
140 g_queue_free(muxer_upstream_msg_iter
->msgs
);
143 g_free(muxer_upstream_msg_iter
);
147 int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
,
148 bt_self_component_port_input_message_iterator
*self_msg_iter
)
151 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
152 g_new0(struct muxer_upstream_msg_iter
, 1);
153 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
155 if (!muxer_upstream_msg_iter
) {
156 BT_COMP_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
160 muxer_upstream_msg_iter
->muxer_comp
= muxer_comp
;
161 muxer_upstream_msg_iter
->msg_iter
= self_msg_iter
;
162 bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter
->msg_iter
);
163 muxer_upstream_msg_iter
->msgs
= g_queue_new();
164 if (!muxer_upstream_msg_iter
->msgs
) {
165 BT_COMP_LOGE_STR("Failed to allocate a GQueue.");
169 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
170 muxer_upstream_msg_iter
);
171 BT_COMP_LOGD("Added muxer's upstream message iterator wrapper: "
172 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
173 muxer_upstream_msg_iter
, muxer_msg_iter
,
179 g_free(muxer_upstream_msg_iter
);
187 bt_self_component_add_port_status
add_available_input_port(
188 bt_self_component_filter
*self_comp
)
190 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
191 bt_self_component_filter_as_self_component(self_comp
));
192 bt_self_component_add_port_status status
=
193 BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
;
194 GString
*port_name
= NULL
;
196 BT_ASSERT(muxer_comp
);
197 port_name
= g_string_new("in");
199 BT_COMP_LOGE_STR("Failed to allocate a GString.");
200 status
= BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
;
204 g_string_append_printf(port_name
, "%u", muxer_comp
->next_port_num
);
205 status
= bt_self_component_filter_add_input_port(
206 self_comp
, port_name
->str
, NULL
, NULL
);
207 if (status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
208 BT_COMP_LOGE("Cannot add input port to muxer component: "
209 "port-name=\"%s\", comp-addr=%p, status=%s",
210 port_name
->str
, self_comp
,
211 bt_common_func_status_string(status
));
215 muxer_comp
->available_input_ports
++;
216 muxer_comp
->next_port_num
++;
217 BT_COMP_LOGI("Added one input port to muxer component: "
218 "port-name=\"%s\", comp-addr=%p",
219 port_name
->str
, self_comp
);
223 g_string_free(port_name
, TRUE
);
230 bt_self_component_add_port_status
create_output_port(
231 bt_self_component_filter
*self_comp
)
233 return bt_self_component_filter_add_output_port(
234 self_comp
, "out", NULL
, NULL
);
238 void destroy_muxer_comp(struct muxer_comp
*muxer_comp
)
248 bt_value
*get_default_params(struct muxer_comp
*muxer_comp
)
253 params
= bt_value_map_create();
255 BT_COMP_LOGE_STR("Cannot create a map value object.");
259 ret
= bt_value_map_insert_bool_entry(params
,
260 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
, false);
262 BT_COMP_LOGE_STR("Cannot add boolean value to map value object.");
269 BT_VALUE_PUT_REF_AND_RESET(params
);
276 int configure_muxer_comp(struct muxer_comp
*muxer_comp
,
277 const bt_value
*params
)
279 bt_value
*default_params
= NULL
;
280 bt_value
*real_params
= NULL
;
281 const bt_value
*assume_absolute_clock_classes
= NULL
;
285 default_params
= get_default_params(muxer_comp
);
286 if (!default_params
) {
287 BT_COMP_LOGE("Cannot get default parameters: "
288 "muxer-comp-addr=%p", muxer_comp
);
292 ret
= bt_value_map_extend(default_params
, params
, &real_params
);
294 BT_COMP_LOGE("Cannot extend default parameters map value: "
295 "muxer-comp-addr=%p, def-params-addr=%p, "
296 "params-addr=%p", muxer_comp
, default_params
,
301 assume_absolute_clock_classes
= bt_value_map_borrow_entry_value(real_params
,
302 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
);
303 if (assume_absolute_clock_classes
&&
304 !bt_value_is_bool(assume_absolute_clock_classes
)) {
305 BT_COMP_LOGE("Expecting a boolean value for the `%s` parameter: "
306 "muxer-comp-addr=%p, value-type=%s",
307 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME
, muxer_comp
,
308 bt_common_value_type_string(
309 bt_value_get_type(assume_absolute_clock_classes
)));
313 bool_val
= bt_value_bool_get(assume_absolute_clock_classes
);
314 muxer_comp
->assume_absolute_clock_classes
= (bool) bool_val
;
315 BT_COMP_LOGI("Configured muxer component: muxer-comp-addr=%p, "
316 "assume-absolute-clock-classes=%d",
317 muxer_comp
, muxer_comp
->assume_absolute_clock_classes
);
324 bt_value_put_ref(default_params
);
325 bt_value_put_ref(real_params
);
330 bt_component_class_init_method_status
muxer_init(
331 bt_self_component_filter
*self_comp_flt
,
332 const bt_value
*params
, void *init_data
)
335 bt_component_class_init_method_status status
=
336 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
337 bt_self_component_add_port_status add_port_status
;
338 bt_self_component
*self_comp
=
339 bt_self_component_filter_as_self_component(self_comp_flt
);
340 struct muxer_comp
*muxer_comp
= g_new0(struct muxer_comp
, 1);
341 bt_logging_level log_level
= bt_component_get_logging_level(
342 bt_self_component_as_component(self_comp
));
344 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
345 "Initializing muxer component: "
346 "comp-addr=%p, params-addr=%p", self_comp
, params
);
349 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
350 "Failed to allocate one muxer component.");
354 muxer_comp
->log_level
= log_level
;
355 muxer_comp
->self_comp
= self_comp
;
356 muxer_comp
->self_comp_flt
= self_comp_flt
;
357 ret
= configure_muxer_comp(muxer_comp
, params
);
359 BT_COMP_LOGE("Cannot configure muxer component: "
360 "muxer-comp-addr=%p, params-addr=%p",
365 bt_self_component_set_data(self_comp
, muxer_comp
);
366 add_port_status
= add_available_input_port(self_comp_flt
);
367 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
368 BT_COMP_LOGE("Cannot ensure that at least one muxer component's input port is available: "
369 "muxer-comp-addr=%p, status=%s",
371 bt_common_func_status_string(add_port_status
));
372 if (add_port_status
==
373 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
374 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
376 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
382 add_port_status
= create_output_port(self_comp_flt
);
383 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
384 BT_COMP_LOGE("Cannot create muxer component's output port: "
385 "muxer-comp-addr=%p, status=%s",
387 bt_common_func_status_string(add_port_status
));
388 if (add_port_status
==
389 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
390 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
392 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
398 BT_COMP_LOGI("Initialized muxer component: "
399 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
400 self_comp
, params
, muxer_comp
);
405 destroy_muxer_comp(muxer_comp
);
406 bt_self_component_set_data(self_comp
, NULL
);
408 if (status
== BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
) {
409 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
417 void muxer_finalize(bt_self_component_filter
*self_comp
)
419 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
420 bt_self_component_filter_as_self_component(self_comp
));
422 BT_COMP_LOGI("Finalizing muxer component: comp-addr=%p",
424 destroy_muxer_comp(muxer_comp
);
428 bt_self_component_port_input_message_iterator
*
429 create_msg_iter_on_input_port(struct muxer_comp
*muxer_comp
,
430 bt_self_component_port_input
*self_port
)
432 const bt_port
*port
= bt_self_component_port_as_port(
433 bt_self_component_port_input_as_self_component_port(
435 bt_self_component_port_input_message_iterator
*msg_iter
=
439 BT_ASSERT(bt_port_is_connected(port
));
441 // TODO: Advance the iterator to >= the time of the latest
442 // returned message by the muxer message
443 // iterator which creates it.
444 msg_iter
= bt_self_component_port_input_message_iterator_create(
447 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
448 "port-addr=%p, port-name=\"%s\"",
449 port
, bt_port_get_name(port
));
453 BT_COMP_LOGI("Created upstream message iterator on input port: "
454 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
455 port
, bt_port_get_name(port
), msg_iter
);
462 bt_component_class_message_iterator_next_method_status
muxer_upstream_msg_iter_next(
463 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
466 struct muxer_comp
*muxer_comp
=
467 muxer_upstream_msg_iter
->muxer_comp
;
468 bt_component_class_message_iterator_next_method_status status
;
469 bt_message_iterator_next_status input_port_iter_status
;
470 bt_message_array_const msgs
;
474 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
475 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
476 muxer_upstream_msg_iter
,
477 muxer_upstream_msg_iter
->msg_iter
);
478 input_port_iter_status
= bt_self_component_port_input_message_iterator_next(
479 muxer_upstream_msg_iter
->msg_iter
, &msgs
, &count
);
480 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
482 bt_common_func_status_string(input_port_iter_status
));
484 switch (input_port_iter_status
) {
485 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
487 * Message iterator's current message is
488 * valid: it must be considered for muxing operations.
490 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
491 BT_ASSERT(count
> 0);
493 /* Move messages to our queue */
494 for (i
= 0; i
< count
; i
++) {
496 * Push to tail in order; other side
497 * (muxer_msg_iter_do_next_one()) consumes
498 * from the head first.
500 g_queue_push_tail(muxer_upstream_msg_iter
->msgs
,
503 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
505 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
507 * Message iterator's current message is not
508 * valid anymore. Return
509 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
511 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
513 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
: /* Fall-through. */
515 * Message iterator reached the end: release it. It
516 * won't be considered again to find the youngest
520 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
523 /* Error or unsupported status code */
524 BT_COMP_LOGE("Error or unsupported status code: "
525 "status-code=%d", input_port_iter_status
);
526 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
534 int get_msg_ts_ns(struct muxer_comp
*muxer_comp
,
535 struct muxer_msg_iter
*muxer_msg_iter
,
536 const bt_message
*msg
, int64_t last_returned_ts_ns
,
539 const bt_clock_snapshot
*clock_snapshot
= NULL
;
541 const bt_stream_class
*stream_class
= NULL
;
542 bt_message_type msg_type
;
546 BT_COMP_LOGD("Getting message's timestamp: "
547 "muxer-msg-iter-addr=%p, msg-addr=%p, "
548 "last-returned-ts=%" PRId64
,
549 muxer_msg_iter
, msg
, last_returned_ts_ns
);
551 if (G_UNLIKELY(muxer_msg_iter
->clock_class_expectation
==
552 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
)) {
553 *ts_ns
= last_returned_ts_ns
;
557 msg_type
= bt_message_get_type(msg
);
559 if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
)) {
560 stream_class
= bt_stream_borrow_class_const(
561 bt_packet_borrow_stream_const(
562 bt_message_packet_beginning_borrow_packet_const(
564 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_END
)) {
565 stream_class
= bt_stream_borrow_class_const(
566 bt_packet_borrow_stream_const(
567 bt_message_packet_end_borrow_packet_const(
569 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
)) {
570 stream_class
= bt_stream_borrow_class_const(
571 bt_message_discarded_events_borrow_stream_const(msg
));
572 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
)) {
573 stream_class
= bt_stream_borrow_class_const(
574 bt_message_discarded_packets_borrow_stream_const(msg
));
578 case BT_MESSAGE_TYPE_EVENT
:
579 BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const(
581 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
584 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
585 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
587 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
590 goto no_clock_snapshot
;
594 case BT_MESSAGE_TYPE_PACKET_END
:
595 if (bt_stream_class_packets_have_end_default_clock_snapshot(
597 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
600 goto no_clock_snapshot
;
604 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
605 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
607 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
610 goto no_clock_snapshot
;
614 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
615 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
617 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
620 goto no_clock_snapshot
;
624 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
625 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
629 /* All the other messages have a higher priority */
630 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
631 *ts_ns
= last_returned_ts_ns
;
635 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
637 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
638 "clock-snapshot-addr=%p", clock_snapshot
);
645 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
646 "using the last returned timestamp.");
647 *ts_ns
= last_returned_ts_ns
;
655 BT_COMP_LOGD("Found message's timestamp: "
656 "muxer-msg-iter-addr=%p, msg-addr=%p, "
657 "last-returned-ts=%" PRId64
", ts=%" PRId64
,
658 muxer_msg_iter
, msg
, last_returned_ts_ns
,
666 int validate_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
667 struct muxer_comp
*muxer_comp
,
668 const bt_clock_class
*clock_class
)
671 const uint8_t *cc_uuid
;
674 BT_ASSERT(clock_class
);
675 cc_uuid
= bt_clock_class_get_uuid(clock_class
);
676 cc_name
= bt_clock_class_get_name(clock_class
);
678 if (muxer_msg_iter
->clock_class_expectation
==
679 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
681 * This is the first clock class that this muxer
682 * message iterator encounters. Its properties
683 * determine what to expect for the whole lifetime of
684 * the iterator without a true
685 * `assume-absolute-clock-classes` parameter.
687 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
688 /* Expect absolute clock classes */
689 muxer_msg_iter
->clock_class_expectation
=
690 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
;
694 * Expect non-absolute clock classes
695 * with a specific UUID.
697 muxer_msg_iter
->clock_class_expectation
=
698 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
;
699 bt_uuid_copy(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
);
702 * Expect non-absolute clock classes
705 muxer_msg_iter
->clock_class_expectation
=
706 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
;
711 if (!muxer_comp
->assume_absolute_clock_classes
) {
712 switch (muxer_msg_iter
->clock_class_expectation
) {
713 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
:
714 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
715 BT_COMP_LOGE("Expecting an absolute clock class, "
716 "but got a non-absolute one: "
717 "clock-class-addr=%p, clock-class-name=\"%s\"",
718 clock_class
, cc_name
);
722 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
:
723 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
724 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
725 "but got an absolute one: "
726 "clock-class-addr=%p, clock-class-name=\"%s\"",
727 clock_class
, cc_name
);
732 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
733 "but got one with a UUID: "
734 "clock-class-addr=%p, clock-class-name=\"%s\", "
735 "uuid=\"" BT_UUID_FMT
"\"",
736 clock_class
, cc_name
, BT_UUID_FMT_VALUES(cc_uuid
));
740 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
:
741 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
742 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
743 "but got an absolute one: "
744 "clock-class-addr=%p, clock-class-name=\"%s\"",
745 clock_class
, cc_name
);
750 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
751 "but got one with no UUID: "
752 "clock-class-addr=%p, clock-class-name=\"%s\"",
753 clock_class
, cc_name
);
757 if (bt_uuid_compare(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
) != 0) {
758 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
759 "but got one with different UUID: "
760 "clock-class-addr=%p, clock-class-name=\"%s\", "
761 "expected-uuid=\"" BT_UUID_FMT
"\", "
762 "uuid=\"" BT_UUID_FMT
"\"",
763 clock_class
, cc_name
,
764 BT_UUID_FMT_VALUES(muxer_msg_iter
->expected_clock_class_uuid
),
765 BT_UUID_FMT_VALUES(cc_uuid
));
769 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
:
770 BT_COMP_LOGE("Expecting no clock class, but got one: "
771 "clock-class-addr=%p, clock-class-name=\"%s\"",
772 clock_class
, cc_name
);
776 BT_COMP_LOGF("Unexpected clock class expectation: "
777 "expectation-code=%d",
778 muxer_msg_iter
->clock_class_expectation
);
793 int validate_new_stream_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
794 struct muxer_comp
*muxer_comp
, const bt_stream
*stream
)
797 const bt_stream_class
*stream_class
=
798 bt_stream_borrow_class_const(stream
);
799 const bt_clock_class
*clock_class
=
800 bt_stream_class_borrow_default_clock_class_const(stream_class
);
803 if (muxer_msg_iter
->clock_class_expectation
==
804 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
805 /* Expect no clock class */
806 muxer_msg_iter
->clock_class_expectation
=
807 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
;
809 BT_COMP_LOGE("Expecting stream class with a default clock class: "
810 "stream-class-addr=%p, stream-class-name=\"%s\", "
811 "stream-class-id=%" PRIu64
,
812 stream_class
, bt_stream_class_get_name(stream_class
),
813 bt_stream_class_get_id(stream_class
));
820 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
, clock_class
);
827 * This function finds the youngest available message amongst the
828 * non-ended upstream message iterators and returns the upstream
829 * message iterator which has it, or
830 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
833 * This function does NOT:
835 * * Update any upstream message iterator.
836 * * Check the upstream message iterators to retry.
838 * On sucess, this function sets *muxer_upstream_msg_iter to the
839 * upstream message iterator of which the current message is
840 * the youngest, and sets *ts_ns to its time.
843 bt_component_class_message_iterator_next_method_status
844 muxer_msg_iter_youngest_upstream_msg_iter(
845 struct muxer_comp
*muxer_comp
,
846 struct muxer_msg_iter
*muxer_msg_iter
,
847 struct muxer_upstream_msg_iter
**muxer_upstream_msg_iter
,
852 int64_t youngest_ts_ns
= INT64_MAX
;
853 bt_component_class_message_iterator_next_method_status status
=
854 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
856 BT_ASSERT(muxer_comp
);
857 BT_ASSERT(muxer_msg_iter
);
858 BT_ASSERT(muxer_upstream_msg_iter
);
859 *muxer_upstream_msg_iter
= NULL
;
861 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
863 const bt_message
*msg
;
864 struct muxer_upstream_msg_iter
*cur_muxer_upstream_msg_iter
=
866 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
870 if (!cur_muxer_upstream_msg_iter
->msg_iter
) {
871 /* This upstream message iterator is ended */
872 BT_COMP_LOGT("Skipping ended upstream message iterator: "
873 "muxer-upstream-msg-iter-wrap-addr=%p",
874 cur_muxer_upstream_msg_iter
);
878 BT_ASSERT(cur_muxer_upstream_msg_iter
->msgs
->length
> 0);
879 msg
= g_queue_peek_head(cur_muxer_upstream_msg_iter
->msgs
);
882 if (G_UNLIKELY(bt_message_get_type(msg
) ==
883 BT_MESSAGE_TYPE_STREAM_BEGINNING
)) {
884 ret
= validate_new_stream_clock_class(
885 muxer_msg_iter
, muxer_comp
,
886 bt_message_stream_beginning_borrow_stream_const(
890 * validate_new_stream_clock_class() logs
893 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
896 } else if (G_UNLIKELY(bt_message_get_type(msg
) ==
897 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
)) {
898 const bt_clock_snapshot
*cs
;
900 cs
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
902 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
,
903 bt_clock_snapshot_borrow_clock_class_const(cs
));
905 /* validate_clock_class() logs errors */
906 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
911 ret
= get_msg_ts_ns(muxer_comp
, muxer_msg_iter
, msg
,
912 muxer_msg_iter
->last_returned_ts_ns
, &msg_ts_ns
);
914 /* get_msg_ts_ns() logs errors */
915 *muxer_upstream_msg_iter
= NULL
;
916 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
920 if (msg_ts_ns
<= youngest_ts_ns
) {
921 *muxer_upstream_msg_iter
=
922 cur_muxer_upstream_msg_iter
;
923 youngest_ts_ns
= msg_ts_ns
;
924 *ts_ns
= youngest_ts_ns
;
928 if (!*muxer_upstream_msg_iter
) {
929 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
938 bt_component_class_message_iterator_next_method_status
939 validate_muxer_upstream_msg_iter(
940 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
943 struct muxer_comp
*muxer_comp
=
944 muxer_upstream_msg_iter
->muxer_comp
;
945 bt_component_class_message_iterator_next_method_status status
=
946 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
948 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
949 "muxer-upstream-msg-iter-wrap-addr=%p",
950 muxer_upstream_msg_iter
);
952 if (muxer_upstream_msg_iter
->msgs
->length
> 0 ||
953 !muxer_upstream_msg_iter
->msg_iter
) {
954 BT_COMP_LOGD("Already valid or not considered: "
955 "queue-len=%u, upstream-msg-iter-addr=%p",
956 muxer_upstream_msg_iter
->msgs
->length
,
957 muxer_upstream_msg_iter
->msg_iter
);
961 /* muxer_upstream_msg_iter_next() logs details/errors */
962 status
= muxer_upstream_msg_iter_next(muxer_upstream_msg_iter
,
970 bt_component_class_message_iterator_next_method_status
971 validate_muxer_upstream_msg_iters(
972 struct muxer_msg_iter
*muxer_msg_iter
)
974 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
975 bt_component_class_message_iterator_next_method_status status
=
976 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
979 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
980 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
982 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
984 bool is_ended
= false;
985 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
987 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
990 status
= validate_muxer_upstream_msg_iter(
991 muxer_upstream_msg_iter
, &is_ended
);
992 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
994 BT_COMP_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
995 "muxer-msg-iter-addr=%p, "
996 "muxer-upstream-msg-iter-wrap-addr=%p",
998 muxer_upstream_msg_iter
);
1000 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
1001 "muxer-msg-iter-addr=%p, "
1002 "muxer-upstream-msg-iter-wrap-addr=%p",
1004 muxer_upstream_msg_iter
);
1011 * Move this muxer upstream message iterator to the
1012 * array of ended iterators if it's ended.
1014 if (G_UNLIKELY(is_ended
)) {
1015 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
1016 "muxer-msg-iter-addr=%p, "
1017 "muxer-upstream-msg-iter-wrap-addr=%p",
1018 muxer_msg_iter
, muxer_upstream_msg_iter
);
1020 muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1021 muxer_upstream_msg_iter
);
1022 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1025 * Use g_ptr_array_remove_fast() because the
1026 * order of those elements is not important.
1028 g_ptr_array_remove_index_fast(
1029 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1040 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next_one(
1041 struct muxer_comp
*muxer_comp
,
1042 struct muxer_msg_iter
*muxer_msg_iter
,
1043 const bt_message
**msg
)
1045 bt_component_class_message_iterator_next_method_status status
;
1046 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
= NULL
;
1047 int64_t next_return_ts
;
1049 status
= validate_muxer_upstream_msg_iters(muxer_msg_iter
);
1050 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1051 /* validate_muxer_upstream_msg_iters() logs details */
1056 * At this point we know that all the existing upstream
1057 * message iterators are valid. We can find the one,
1058 * amongst those, of which the current message is the
1061 status
= muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp
,
1062 muxer_msg_iter
, &muxer_upstream_msg_iter
,
1064 if (status
< 0 || status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
) {
1066 BT_COMP_LOGE("Cannot find the youngest upstream message iterator wrapper: "
1068 bt_common_func_status_string(status
));
1070 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
1072 bt_common_func_status_string(status
));
1078 if (next_return_ts
< muxer_msg_iter
->last_returned_ts_ns
) {
1079 BT_COMP_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
1080 "muxer-msg-iter-addr=%p, ts=%" PRId64
", "
1081 "last-returned-ts=%" PRId64
,
1082 muxer_msg_iter
, next_return_ts
,
1083 muxer_msg_iter
->last_returned_ts_ns
);
1084 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1088 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
1089 "muxer-msg-iter-addr=%p, "
1090 "muxer-upstream-msg-iter-wrap-addr=%p, "
1092 muxer_msg_iter
, muxer_upstream_msg_iter
, next_return_ts
);
1093 BT_ASSERT(status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
);
1094 BT_ASSERT(muxer_upstream_msg_iter
);
1097 * Consume from the queue's head: other side
1098 * (muxer_upstream_msg_iter_next()) writes to the tail.
1100 *msg
= g_queue_pop_head(muxer_upstream_msg_iter
->msgs
);
1102 muxer_msg_iter
->last_returned_ts_ns
= next_return_ts
;
1109 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next(
1110 struct muxer_comp
*muxer_comp
,
1111 struct muxer_msg_iter
*muxer_msg_iter
,
1112 bt_message_array_const msgs
, uint64_t capacity
,
1115 bt_component_class_message_iterator_next_method_status status
=
1116 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1119 while (i
< capacity
&& status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1120 status
= muxer_msg_iter_do_next_one(muxer_comp
,
1121 muxer_msg_iter
, &msgs
[i
]);
1122 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1129 * Even if muxer_msg_iter_do_next_one() returned
1130 * something else than
1131 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1132 * message objects in the output message
1133 * array, so we need to return
1134 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1135 * transfered to downstream. This other status occurs
1136 * again the next time muxer_msg_iter_do_next() is
1137 * called, possibly without any accumulated
1138 * message, in which case we'll return it.
1141 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1148 void destroy_muxer_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
)
1150 struct muxer_comp
*muxer_comp
;
1152 if (!muxer_msg_iter
) {
1156 muxer_comp
= muxer_msg_iter
->muxer_comp
;
1157 BT_COMP_LOGD("Destroying muxer component's message iterator: "
1158 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1160 if (muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1161 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
1163 muxer_msg_iter
->active_muxer_upstream_msg_iters
, TRUE
);
1166 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1167 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
1169 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, TRUE
);
1172 g_free(muxer_msg_iter
);
1176 int muxer_msg_iter_init_upstream_iterators(struct muxer_comp
*muxer_comp
,
1177 struct muxer_msg_iter
*muxer_msg_iter
)
1183 count
= bt_component_filter_get_input_port_count(
1184 bt_self_component_filter_as_component_filter(
1185 muxer_comp
->self_comp_flt
));
1187 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
1188 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1189 muxer_comp
, muxer_msg_iter
);
1193 for (i
= 0; i
< count
; i
++) {
1194 bt_self_component_port_input_message_iterator
*upstream_msg_iter
;
1195 bt_self_component_port_input
*self_port
=
1196 bt_self_component_filter_borrow_input_port_by_index(
1197 muxer_comp
->self_comp_flt
, i
);
1198 const bt_port
*port
;
1200 BT_ASSERT(self_port
);
1201 port
= bt_self_component_port_as_port(
1202 bt_self_component_port_input_as_self_component_port(
1206 if (!bt_port_is_connected(port
)) {
1207 /* Skip non-connected port */
1211 upstream_msg_iter
= create_msg_iter_on_input_port(muxer_comp
,
1213 if (!upstream_msg_iter
) {
1214 /* create_msg_iter_on_input_port() logs errors */
1215 BT_ASSERT(!upstream_msg_iter
);
1220 ret
= muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter
,
1222 bt_self_component_port_input_message_iterator_put_ref(
1225 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1235 bt_component_class_message_iterator_init_method_status
muxer_msg_iter_init(
1236 bt_self_message_iterator
*self_msg_iter
,
1237 bt_self_component_filter
*self_comp
,
1238 bt_self_component_port_output
*port
)
1240 struct muxer_comp
*muxer_comp
= NULL
;
1241 struct muxer_msg_iter
*muxer_msg_iter
= NULL
;
1242 bt_component_class_message_iterator_init_method_status status
=
1243 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1246 muxer_comp
= bt_self_component_get_data(
1247 bt_self_component_filter_as_self_component(self_comp
));
1248 BT_ASSERT(muxer_comp
);
1249 BT_COMP_LOGD("Initializing muxer component's message iterator: "
1250 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1251 self_comp
, muxer_comp
, self_msg_iter
);
1253 if (muxer_comp
->initializing_muxer_msg_iter
) {
1255 * Weird, unhandled situation detected: downstream
1256 * creates a muxer message iterator while creating
1257 * another muxer message iterator (same component).
1259 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
1260 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1261 self_comp
, muxer_comp
, self_msg_iter
);
1265 muxer_comp
->initializing_muxer_msg_iter
= true;
1266 muxer_msg_iter
= g_new0(struct muxer_msg_iter
, 1);
1267 if (!muxer_msg_iter
) {
1268 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
1272 muxer_msg_iter
->muxer_comp
= muxer_comp
;
1273 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1274 muxer_msg_iter
->active_muxer_upstream_msg_iters
=
1275 g_ptr_array_new_with_free_func(
1276 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1277 if (!muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1278 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1282 muxer_msg_iter
->ended_muxer_upstream_msg_iters
=
1283 g_ptr_array_new_with_free_func(
1284 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1285 if (!muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1286 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1290 ret
= muxer_msg_iter_init_upstream_iterators(muxer_comp
,
1293 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
1294 "comp-addr=%p, muxer-comp-addr=%p, "
1295 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1296 self_comp
, muxer_comp
, muxer_msg_iter
,
1297 self_msg_iter
, ret
);
1301 bt_self_message_iterator_set_data(self_msg_iter
, muxer_msg_iter
);
1302 BT_COMP_LOGD("Initialized muxer component's message iterator: "
1303 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1305 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1309 destroy_muxer_msg_iter(muxer_msg_iter
);
1310 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
1311 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1314 muxer_comp
->initializing_muxer_msg_iter
= false;
1319 void muxer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1321 struct muxer_msg_iter
*muxer_msg_iter
=
1322 bt_self_message_iterator_get_data(self_msg_iter
);
1323 bt_self_component
*self_comp
= NULL
;
1324 struct muxer_comp
*muxer_comp
= NULL
;
1326 self_comp
= bt_self_message_iterator_borrow_component(
1328 BT_ASSERT(self_comp
);
1329 muxer_comp
= bt_self_component_get_data(self_comp
);
1330 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
1331 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1333 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1335 if (muxer_msg_iter
) {
1336 destroy_muxer_msg_iter(muxer_msg_iter
);
1341 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_next(
1342 bt_self_message_iterator
*self_msg_iter
,
1343 bt_message_array_const msgs
, uint64_t capacity
,
1346 bt_component_class_message_iterator_next_method_status status
;
1347 struct muxer_msg_iter
*muxer_msg_iter
=
1348 bt_self_message_iterator_get_data(self_msg_iter
);
1349 bt_self_component
*self_comp
= NULL
;
1350 struct muxer_comp
*muxer_comp
= NULL
;
1352 BT_ASSERT(muxer_msg_iter
);
1353 self_comp
= bt_self_message_iterator_borrow_component(
1355 BT_ASSERT(self_comp
);
1356 muxer_comp
= bt_self_component_get_data(self_comp
);
1357 BT_ASSERT(muxer_comp
);
1358 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
1359 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1361 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1363 status
= muxer_msg_iter_do_next(muxer_comp
, muxer_msg_iter
,
1364 msgs
, capacity
, count
);
1366 BT_COMP_LOGE("Cannot get next message: "
1367 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1368 "msg-iter-addr=%p, status=%s",
1369 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
,
1370 bt_common_func_status_string(status
));
1372 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
1374 bt_common_func_status_string(status
));
1381 bt_component_class_port_connected_method_status
muxer_input_port_connected(
1382 bt_self_component_filter
*self_comp
,
1383 bt_self_component_port_input
*self_port
,
1384 const bt_port_output
*other_port
)
1386 bt_component_class_port_connected_method_status status
=
1387 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK
;
1388 bt_self_component_add_port_status add_port_status
;
1389 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
1390 bt_self_component_filter_as_self_component(self_comp
));
1392 add_port_status
= add_available_input_port(self_comp
);
1393 if (add_port_status
) {
1394 BT_COMP_LOGE("Cannot add one muxer component's input port: "
1396 bt_common_func_status_string(status
));
1398 if (add_port_status
==
1399 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
1400 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR
;
1402 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR
;
1413 bt_bool
muxer_upstream_msg_iters_can_all_seek_beginning(
1414 GPtrArray
*muxer_upstream_msg_iters
)
1417 bt_bool ret
= BT_TRUE
;
1419 for (i
= 0; i
< muxer_upstream_msg_iters
->len
; i
++) {
1420 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1421 muxer_upstream_msg_iters
->pdata
[i
];
1423 if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
1424 upstream_msg_iter
->msg_iter
)) {
1435 bt_bool
muxer_msg_iter_can_seek_beginning(
1436 bt_self_message_iterator
*self_msg_iter
)
1438 struct muxer_msg_iter
*muxer_msg_iter
=
1439 bt_self_message_iterator_get_data(self_msg_iter
);
1440 bt_bool ret
= BT_TRUE
;
1442 if (!muxer_upstream_msg_iters_can_all_seek_beginning(
1443 muxer_msg_iter
->active_muxer_upstream_msg_iters
)) {
1448 if (!muxer_upstream_msg_iters_can_all_seek_beginning(
1449 muxer_msg_iter
->ended_muxer_upstream_msg_iters
)) {
1459 bt_component_class_message_iterator_seek_beginning_method_status
muxer_msg_iter_seek_beginning(
1460 bt_self_message_iterator
*self_msg_iter
)
1462 struct muxer_msg_iter
*muxer_msg_iter
=
1463 bt_self_message_iterator_get_data(self_msg_iter
);
1464 bt_component_class_message_iterator_seek_beginning_method_status status
=
1465 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK
;
1466 bt_message_iterator_seek_beginning_status seek_beg_status
;
1469 /* Seek all ended upstream iterators first */
1470 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1472 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1473 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1475 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1476 upstream_msg_iter
->msg_iter
);
1477 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1478 status
= (int) seek_beg_status
;
1482 empty_message_queue(upstream_msg_iter
);
1485 /* Seek all previously active upstream iterators */
1486 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
1488 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1489 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
];
1491 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1492 upstream_msg_iter
->msg_iter
);
1493 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1494 status
= (int) seek_beg_status
;
1498 empty_message_queue(upstream_msg_iter
);
1501 /* Make them all active */
1502 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1504 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1505 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1507 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1509 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1512 g_ptr_array_remove_range(muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1513 0, muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
);
1514 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1515 muxer_msg_iter
->clock_class_expectation
=
1516 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
;