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"
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 bt_logging_level log_level
;
54 struct muxer_upstream_msg_iter
{
55 struct muxer_comp
*muxer_comp
;
57 /* Owned by this, NULL if ended */
58 bt_self_component_port_input_message_iterator
*msg_iter
;
60 /* Contains `const bt_message *`, owned by this */
64 enum muxer_msg_iter_clock_class_expectation
{
65 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
= 0,
66 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
,
67 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
,
68 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
,
69 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
,
72 struct muxer_msg_iter
{
73 struct muxer_comp
*muxer_comp
;
76 bt_self_message_iterator
*self_msg_iter
;
79 * Array of struct muxer_upstream_msg_iter * (owned by this).
81 * NOTE: This array is searched in linearly to find the youngest
82 * current message. Keep this until benchmarks confirm that
83 * another data structure is faster than this for our typical
86 GPtrArray
*active_muxer_upstream_msg_iters
;
89 * Array of struct muxer_upstream_msg_iter * (owned by this).
91 * We move ended message iterators from
92 * `active_muxer_upstream_msg_iters` to this array so as to be
93 * able to restore them when seeking.
95 GPtrArray
*ended_muxer_upstream_msg_iters
;
97 /* Last time returned in a message */
98 int64_t last_returned_ts_ns
;
100 /* Clock class expectation state */
101 enum muxer_msg_iter_clock_class_expectation clock_class_expectation
;
104 * Expected clock class UUID, only valid when
105 * clock_class_expectation is
106 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
108 bt_uuid_t expected_clock_class_uuid
;
112 void empty_message_queue(struct muxer_upstream_msg_iter
*upstream_msg_iter
)
114 const bt_message
*msg
;
116 while ((msg
= g_queue_pop_head(upstream_msg_iter
->msgs
))) {
117 bt_message_put_ref(msg
);
122 void destroy_muxer_upstream_msg_iter(
123 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
)
125 struct muxer_comp
*muxer_comp
;
127 if (!muxer_upstream_msg_iter
) {
131 muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
132 BT_COMP_LOGD("Destroying muxer's upstream message iterator wrapper: "
133 "addr=%p, msg-iter-addr=%p, queue-len=%u",
134 muxer_upstream_msg_iter
,
135 muxer_upstream_msg_iter
->msg_iter
,
136 muxer_upstream_msg_iter
->msgs
->length
);
137 bt_self_component_port_input_message_iterator_put_ref(
138 muxer_upstream_msg_iter
->msg_iter
);
140 if (muxer_upstream_msg_iter
->msgs
) {
141 empty_message_queue(muxer_upstream_msg_iter
);
142 g_queue_free(muxer_upstream_msg_iter
->msgs
);
145 g_free(muxer_upstream_msg_iter
);
149 int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
,
150 bt_self_component_port_input_message_iterator
*self_msg_iter
)
153 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
154 g_new0(struct muxer_upstream_msg_iter
, 1);
155 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
157 if (!muxer_upstream_msg_iter
) {
158 BT_COMP_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
162 muxer_upstream_msg_iter
->muxer_comp
= muxer_comp
;
163 muxer_upstream_msg_iter
->msg_iter
= self_msg_iter
;
164 bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter
->msg_iter
);
165 muxer_upstream_msg_iter
->msgs
= g_queue_new();
166 if (!muxer_upstream_msg_iter
->msgs
) {
167 BT_COMP_LOGE_STR("Failed to allocate a GQueue.");
171 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
172 muxer_upstream_msg_iter
);
173 BT_COMP_LOGD("Added muxer's upstream message iterator wrapper: "
174 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
175 muxer_upstream_msg_iter
, muxer_msg_iter
,
181 g_free(muxer_upstream_msg_iter
);
189 bt_self_component_add_port_status
add_available_input_port(
190 bt_self_component_filter
*self_comp
)
192 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
193 bt_self_component_filter_as_self_component(self_comp
));
194 bt_self_component_add_port_status status
=
195 BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
;
196 GString
*port_name
= NULL
;
198 BT_ASSERT(muxer_comp
);
199 port_name
= g_string_new("in");
201 BT_COMP_LOGE_STR("Failed to allocate a GString.");
202 status
= BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
;
206 g_string_append_printf(port_name
, "%u", muxer_comp
->next_port_num
);
207 status
= bt_self_component_filter_add_input_port(
208 self_comp
, port_name
->str
, NULL
, NULL
);
209 if (status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
210 BT_COMP_LOGE("Cannot add input port to muxer component: "
211 "port-name=\"%s\", comp-addr=%p, status=%s",
212 port_name
->str
, self_comp
,
213 bt_common_func_status_string(status
));
217 muxer_comp
->available_input_ports
++;
218 muxer_comp
->next_port_num
++;
219 BT_COMP_LOGI("Added one input port to muxer component: "
220 "port-name=\"%s\", comp-addr=%p",
221 port_name
->str
, self_comp
);
225 g_string_free(port_name
, TRUE
);
232 bt_self_component_add_port_status
create_output_port(
233 bt_self_component_filter
*self_comp
)
235 return bt_self_component_filter_add_output_port(
236 self_comp
, "out", NULL
, NULL
);
240 void destroy_muxer_comp(struct muxer_comp
*muxer_comp
)
250 bt_component_class_init_method_status
muxer_init(
251 bt_self_component_filter
*self_comp_flt
,
252 const bt_value
*params
, void *init_data
)
254 bt_component_class_init_method_status status
=
255 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
256 bt_self_component_add_port_status add_port_status
;
257 bt_self_component
*self_comp
=
258 bt_self_component_filter_as_self_component(self_comp_flt
);
259 struct muxer_comp
*muxer_comp
= g_new0(struct muxer_comp
, 1);
260 bt_logging_level log_level
= bt_component_get_logging_level(
261 bt_self_component_as_component(self_comp
));
263 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
264 "Initializing muxer component: "
265 "comp-addr=%p, params-addr=%p", self_comp
, params
);
268 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
269 "Failed to allocate one muxer component.");
273 muxer_comp
->log_level
= log_level
;
274 muxer_comp
->self_comp
= self_comp
;
275 muxer_comp
->self_comp_flt
= self_comp_flt
;
277 bt_self_component_set_data(self_comp
, muxer_comp
);
278 add_port_status
= add_available_input_port(self_comp_flt
);
279 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
280 BT_COMP_LOGE("Cannot ensure that at least one muxer component's input port is available: "
281 "muxer-comp-addr=%p, status=%s",
283 bt_common_func_status_string(add_port_status
));
284 if (add_port_status
==
285 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
286 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
288 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
294 add_port_status
= create_output_port(self_comp_flt
);
295 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
296 BT_COMP_LOGE("Cannot create muxer component's output port: "
297 "muxer-comp-addr=%p, status=%s",
299 bt_common_func_status_string(add_port_status
));
300 if (add_port_status
==
301 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
302 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
304 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
310 BT_COMP_LOGI("Initialized muxer component: "
311 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
312 self_comp
, params
, muxer_comp
);
317 destroy_muxer_comp(muxer_comp
);
318 bt_self_component_set_data(self_comp
, NULL
);
320 if (status
== BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
) {
321 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
329 void muxer_finalize(bt_self_component_filter
*self_comp
)
331 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
332 bt_self_component_filter_as_self_component(self_comp
));
334 BT_COMP_LOGI("Finalizing muxer component: comp-addr=%p",
336 destroy_muxer_comp(muxer_comp
);
340 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
341 create_msg_iter_on_input_port(struct muxer_comp
*muxer_comp
,
342 struct muxer_msg_iter
*muxer_msg_iter
,
343 bt_self_component_port_input
*self_port
,
344 bt_self_component_port_input_message_iterator
**msg_iter
)
346 const bt_port
*port
= bt_self_component_port_as_port(
347 bt_self_component_port_input_as_self_component_port(
349 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
353 BT_ASSERT(bt_port_is_connected(port
));
355 // TODO: Advance the iterator to >= the time of the latest
356 // returned message by the muxer message
357 // iterator which creates it.
358 status
= bt_self_component_port_input_message_iterator_create_from_message_iterator(
359 muxer_msg_iter
->self_msg_iter
, self_port
, msg_iter
);
360 if (status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
361 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
362 "port-addr=%p, port-name=\"%s\"",
363 port
, bt_port_get_name(port
));
367 BT_COMP_LOGI("Created upstream message iterator on input port: "
368 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
369 port
, bt_port_get_name(port
), msg_iter
);
376 bt_component_class_message_iterator_next_method_status
muxer_upstream_msg_iter_next(
377 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
380 struct muxer_comp
*muxer_comp
=
381 muxer_upstream_msg_iter
->muxer_comp
;
382 bt_component_class_message_iterator_next_method_status status
;
383 bt_message_iterator_next_status input_port_iter_status
;
384 bt_message_array_const msgs
;
388 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
389 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
390 muxer_upstream_msg_iter
,
391 muxer_upstream_msg_iter
->msg_iter
);
392 input_port_iter_status
= bt_self_component_port_input_message_iterator_next(
393 muxer_upstream_msg_iter
->msg_iter
, &msgs
, &count
);
394 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
396 bt_common_func_status_string(input_port_iter_status
));
398 switch (input_port_iter_status
) {
399 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
401 * Message iterator's current message is
402 * valid: it must be considered for muxing operations.
404 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
405 BT_ASSERT(count
> 0);
407 /* Move messages to our queue */
408 for (i
= 0; i
< count
; i
++) {
410 * Push to tail in order; other side
411 * (muxer_msg_iter_do_next_one()) consumes
412 * from the head first.
414 g_queue_push_tail(muxer_upstream_msg_iter
->msgs
,
417 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
419 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
421 * Message iterator's current message is not
422 * valid anymore. Return
423 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
425 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
427 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
: /* Fall-through. */
429 * Message iterator reached the end: release it. It
430 * won't be considered again to find the youngest
434 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
437 /* Error or unsupported status code */
438 BT_COMP_LOGE("Error or unsupported status code: "
439 "status-code=%d", input_port_iter_status
);
440 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
448 int get_msg_ts_ns(struct muxer_comp
*muxer_comp
,
449 struct muxer_msg_iter
*muxer_msg_iter
,
450 const bt_message
*msg
, int64_t last_returned_ts_ns
,
453 const bt_clock_snapshot
*clock_snapshot
= NULL
;
455 const bt_stream_class
*stream_class
= NULL
;
456 bt_message_type msg_type
;
460 BT_COMP_LOGD("Getting message's timestamp: "
461 "muxer-msg-iter-addr=%p, msg-addr=%p, "
462 "last-returned-ts=%" PRId64
,
463 muxer_msg_iter
, msg
, last_returned_ts_ns
);
465 if (G_UNLIKELY(muxer_msg_iter
->clock_class_expectation
==
466 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
)) {
467 *ts_ns
= last_returned_ts_ns
;
471 msg_type
= bt_message_get_type(msg
);
473 if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
)) {
474 stream_class
= bt_stream_borrow_class_const(
475 bt_packet_borrow_stream_const(
476 bt_message_packet_beginning_borrow_packet_const(
478 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_END
)) {
479 stream_class
= bt_stream_borrow_class_const(
480 bt_packet_borrow_stream_const(
481 bt_message_packet_end_borrow_packet_const(
483 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
)) {
484 stream_class
= bt_stream_borrow_class_const(
485 bt_message_discarded_events_borrow_stream_const(msg
));
486 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
)) {
487 stream_class
= bt_stream_borrow_class_const(
488 bt_message_discarded_packets_borrow_stream_const(msg
));
492 case BT_MESSAGE_TYPE_EVENT
:
493 BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const(
495 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
498 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
499 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
501 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
504 goto no_clock_snapshot
;
508 case BT_MESSAGE_TYPE_PACKET_END
:
509 if (bt_stream_class_packets_have_end_default_clock_snapshot(
511 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
514 goto no_clock_snapshot
;
518 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
520 enum bt_message_stream_clock_snapshot_state snapshot_state
=
521 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
522 msg
, &clock_snapshot
);
523 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
524 goto no_clock_snapshot
;
529 case BT_MESSAGE_TYPE_STREAM_END
:
531 enum bt_message_stream_clock_snapshot_state snapshot_state
=
532 bt_message_stream_end_borrow_default_clock_snapshot_const(
533 msg
, &clock_snapshot
);
534 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
535 goto no_clock_snapshot
;
540 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
541 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
543 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
546 goto no_clock_snapshot
;
550 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
551 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
553 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
556 goto no_clock_snapshot
;
560 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
561 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
565 /* All the other messages have a higher priority */
566 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
567 *ts_ns
= last_returned_ts_ns
;
571 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
573 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
574 "clock-snapshot-addr=%p", clock_snapshot
);
581 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
582 "using the last returned timestamp.");
583 *ts_ns
= last_returned_ts_ns
;
591 BT_COMP_LOGD("Found message's timestamp: "
592 "muxer-msg-iter-addr=%p, msg-addr=%p, "
593 "last-returned-ts=%" PRId64
", ts=%" PRId64
,
594 muxer_msg_iter
, msg
, last_returned_ts_ns
,
602 int validate_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
603 struct muxer_comp
*muxer_comp
,
604 const bt_clock_class
*clock_class
)
607 const uint8_t *cc_uuid
;
610 BT_ASSERT(clock_class
);
611 cc_uuid
= bt_clock_class_get_uuid(clock_class
);
612 cc_name
= bt_clock_class_get_name(clock_class
);
614 if (muxer_msg_iter
->clock_class_expectation
==
615 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
617 * This is the first clock class that this muxer message
618 * iterator encounters. Its properties determine what to expect
619 * for the whole lifetime of the iterator.
621 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
622 /* Expect absolute clock classes */
623 muxer_msg_iter
->clock_class_expectation
=
624 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
;
628 * Expect non-absolute clock classes
629 * with a specific UUID.
631 muxer_msg_iter
->clock_class_expectation
=
632 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
;
633 bt_uuid_copy(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
);
636 * Expect non-absolute clock classes
639 muxer_msg_iter
->clock_class_expectation
=
640 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
;
645 switch (muxer_msg_iter
->clock_class_expectation
) {
646 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
:
647 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
648 BT_COMP_LOGE("Expecting an absolute clock class, "
649 "but got a non-absolute one: "
650 "clock-class-addr=%p, clock-class-name=\"%s\"",
651 clock_class
, cc_name
);
655 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
:
656 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
657 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
658 "but got an absolute one: "
659 "clock-class-addr=%p, clock-class-name=\"%s\"",
660 clock_class
, cc_name
);
665 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
666 "but got one with a UUID: "
667 "clock-class-addr=%p, clock-class-name=\"%s\", "
668 "uuid=\"" BT_UUID_FMT
"\"",
669 clock_class
, cc_name
, BT_UUID_FMT_VALUES(cc_uuid
));
673 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
:
674 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
675 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
676 "but got an absolute one: "
677 "clock-class-addr=%p, clock-class-name=\"%s\"",
678 clock_class
, cc_name
);
683 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
684 "but got one with no UUID: "
685 "clock-class-addr=%p, clock-class-name=\"%s\"",
686 clock_class
, cc_name
);
690 if (bt_uuid_compare(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
) != 0) {
691 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
692 "but got one with different UUID: "
693 "clock-class-addr=%p, clock-class-name=\"%s\", "
694 "expected-uuid=\"" BT_UUID_FMT
"\", "
695 "uuid=\"" BT_UUID_FMT
"\"",
696 clock_class
, cc_name
,
697 BT_UUID_FMT_VALUES(muxer_msg_iter
->expected_clock_class_uuid
),
698 BT_UUID_FMT_VALUES(cc_uuid
));
702 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
:
703 BT_COMP_LOGE("Expecting no clock class, but got one: "
704 "clock-class-addr=%p, clock-class-name=\"%s\"",
705 clock_class
, cc_name
);
709 BT_COMP_LOGF("Unexpected clock class expectation: "
710 "expectation-code=%d",
711 muxer_msg_iter
->clock_class_expectation
);
725 int validate_new_stream_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
726 struct muxer_comp
*muxer_comp
, const bt_stream
*stream
)
729 const bt_stream_class
*stream_class
=
730 bt_stream_borrow_class_const(stream
);
731 const bt_clock_class
*clock_class
=
732 bt_stream_class_borrow_default_clock_class_const(stream_class
);
735 if (muxer_msg_iter
->clock_class_expectation
==
736 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
737 /* Expect no clock class */
738 muxer_msg_iter
->clock_class_expectation
=
739 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
;
740 } else if (muxer_msg_iter
->clock_class_expectation
!=
741 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
) {
742 BT_COMP_LOGE("Expecting stream class without a default clock class: "
743 "stream-class-addr=%p, stream-class-name=\"%s\", "
744 "stream-class-id=%" PRIu64
,
745 stream_class
, bt_stream_class_get_name(stream_class
),
746 bt_stream_class_get_id(stream_class
));
753 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
, clock_class
);
760 * This function finds the youngest available message amongst the
761 * non-ended upstream message iterators and returns the upstream
762 * message iterator which has it, or
763 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
766 * This function does NOT:
768 * * Update any upstream message iterator.
769 * * Check the upstream message iterators to retry.
771 * On sucess, this function sets *muxer_upstream_msg_iter to the
772 * upstream message iterator of which the current message is
773 * the youngest, and sets *ts_ns to its time.
776 bt_component_class_message_iterator_next_method_status
777 muxer_msg_iter_youngest_upstream_msg_iter(
778 struct muxer_comp
*muxer_comp
,
779 struct muxer_msg_iter
*muxer_msg_iter
,
780 struct muxer_upstream_msg_iter
**muxer_upstream_msg_iter
,
785 int64_t youngest_ts_ns
= INT64_MAX
;
786 bt_component_class_message_iterator_next_method_status status
=
787 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
789 BT_ASSERT(muxer_comp
);
790 BT_ASSERT(muxer_msg_iter
);
791 BT_ASSERT(muxer_upstream_msg_iter
);
792 *muxer_upstream_msg_iter
= NULL
;
794 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
796 const bt_message
*msg
;
797 struct muxer_upstream_msg_iter
*cur_muxer_upstream_msg_iter
=
799 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
803 if (!cur_muxer_upstream_msg_iter
->msg_iter
) {
804 /* This upstream message iterator is ended */
805 BT_COMP_LOGT("Skipping ended upstream message iterator: "
806 "muxer-upstream-msg-iter-wrap-addr=%p",
807 cur_muxer_upstream_msg_iter
);
811 BT_ASSERT(cur_muxer_upstream_msg_iter
->msgs
->length
> 0);
812 msg
= g_queue_peek_head(cur_muxer_upstream_msg_iter
->msgs
);
815 if (G_UNLIKELY(bt_message_get_type(msg
) ==
816 BT_MESSAGE_TYPE_STREAM_BEGINNING
)) {
817 ret
= validate_new_stream_clock_class(
818 muxer_msg_iter
, muxer_comp
,
819 bt_message_stream_beginning_borrow_stream_const(
823 * validate_new_stream_clock_class() logs
826 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
829 } else if (G_UNLIKELY(bt_message_get_type(msg
) ==
830 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
)) {
831 const bt_clock_snapshot
*cs
;
833 cs
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
835 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
,
836 bt_clock_snapshot_borrow_clock_class_const(cs
));
838 /* validate_clock_class() logs errors */
839 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
844 ret
= get_msg_ts_ns(muxer_comp
, muxer_msg_iter
, msg
,
845 muxer_msg_iter
->last_returned_ts_ns
, &msg_ts_ns
);
847 /* get_msg_ts_ns() logs errors */
848 *muxer_upstream_msg_iter
= NULL
;
849 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
854 * Update the current message iterator if it has not been set
855 * yet, or if its current message has a timestamp smaller than
856 * the previously selected youngest message.
858 if (G_UNLIKELY(*muxer_upstream_msg_iter
== NULL
) ||
859 msg_ts_ns
< youngest_ts_ns
) {
860 *muxer_upstream_msg_iter
=
861 cur_muxer_upstream_msg_iter
;
862 youngest_ts_ns
= msg_ts_ns
;
863 *ts_ns
= youngest_ts_ns
;
864 } else if (msg_ts_ns
== youngest_ts_ns
) {
866 * The currently selected message to be sent downstream
867 * next has the exact same timestamp that of the
868 * current candidate message. We must break the tie
869 * in a predictable manner.
871 const bt_message
*selected_msg
= g_queue_peek_head(
872 (*muxer_upstream_msg_iter
)->msgs
);
873 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
876 * Order the messages in an arbitrary but determinitic
879 ret
= common_muxing_compare_messages(msg
, selected_msg
);
882 * The `msg` should go first. Update the next
883 * iterator and the current timestamp.
885 *muxer_upstream_msg_iter
=
886 cur_muxer_upstream_msg_iter
;
887 youngest_ts_ns
= msg_ts_ns
;
888 *ts_ns
= youngest_ts_ns
;
889 } else if (ret
== 0) {
890 /* Unable to pick which one should go first. */
891 BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
892 "muxer-upstream-msg-iter-wrap-addr=%p"
893 "cur-muxer-upstream-msg-iter-wrap-addr=%p",
894 *muxer_upstream_msg_iter
,
895 cur_muxer_upstream_msg_iter
);
900 if (!*muxer_upstream_msg_iter
) {
901 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
910 bt_component_class_message_iterator_next_method_status
911 validate_muxer_upstream_msg_iter(
912 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
915 struct muxer_comp
*muxer_comp
=
916 muxer_upstream_msg_iter
->muxer_comp
;
917 bt_component_class_message_iterator_next_method_status status
=
918 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
920 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
921 "muxer-upstream-msg-iter-wrap-addr=%p",
922 muxer_upstream_msg_iter
);
924 if (muxer_upstream_msg_iter
->msgs
->length
> 0 ||
925 !muxer_upstream_msg_iter
->msg_iter
) {
926 BT_COMP_LOGD("Already valid or not considered: "
927 "queue-len=%u, upstream-msg-iter-addr=%p",
928 muxer_upstream_msg_iter
->msgs
->length
,
929 muxer_upstream_msg_iter
->msg_iter
);
933 /* muxer_upstream_msg_iter_next() logs details/errors */
934 status
= muxer_upstream_msg_iter_next(muxer_upstream_msg_iter
,
942 bt_component_class_message_iterator_next_method_status
943 validate_muxer_upstream_msg_iters(
944 struct muxer_msg_iter
*muxer_msg_iter
)
946 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
947 bt_component_class_message_iterator_next_method_status status
=
948 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
951 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
952 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
954 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
956 bool is_ended
= false;
957 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
959 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
962 status
= validate_muxer_upstream_msg_iter(
963 muxer_upstream_msg_iter
, &is_ended
);
964 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
966 BT_COMP_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
967 "muxer-msg-iter-addr=%p, "
968 "muxer-upstream-msg-iter-wrap-addr=%p",
970 muxer_upstream_msg_iter
);
972 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
973 "muxer-msg-iter-addr=%p, "
974 "muxer-upstream-msg-iter-wrap-addr=%p",
976 muxer_upstream_msg_iter
);
983 * Move this muxer upstream message iterator to the
984 * array of ended iterators if it's ended.
986 if (G_UNLIKELY(is_ended
)) {
987 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
988 "muxer-msg-iter-addr=%p, "
989 "muxer-upstream-msg-iter-wrap-addr=%p",
990 muxer_msg_iter
, muxer_upstream_msg_iter
);
992 muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
993 muxer_upstream_msg_iter
);
994 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
997 * Use g_ptr_array_remove_fast() because the
998 * order of those elements is not important.
1000 g_ptr_array_remove_index_fast(
1001 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1012 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next_one(
1013 struct muxer_comp
*muxer_comp
,
1014 struct muxer_msg_iter
*muxer_msg_iter
,
1015 const bt_message
**msg
)
1017 bt_component_class_message_iterator_next_method_status status
;
1018 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
= NULL
;
1019 int64_t next_return_ts
;
1021 status
= validate_muxer_upstream_msg_iters(muxer_msg_iter
);
1022 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1023 /* validate_muxer_upstream_msg_iters() logs details */
1028 * At this point we know that all the existing upstream
1029 * message iterators are valid. We can find the one,
1030 * amongst those, of which the current message is the
1033 status
= muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp
,
1034 muxer_msg_iter
, &muxer_upstream_msg_iter
,
1036 if (status
< 0 || status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
) {
1038 BT_COMP_LOGE("Cannot find the youngest upstream message iterator wrapper: "
1040 bt_common_func_status_string(status
));
1042 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
1044 bt_common_func_status_string(status
));
1050 if (next_return_ts
< muxer_msg_iter
->last_returned_ts_ns
) {
1051 BT_COMP_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
1052 "muxer-msg-iter-addr=%p, ts=%" PRId64
", "
1053 "last-returned-ts=%" PRId64
,
1054 muxer_msg_iter
, next_return_ts
,
1055 muxer_msg_iter
->last_returned_ts_ns
);
1056 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1060 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
1061 "muxer-msg-iter-addr=%p, "
1062 "muxer-upstream-msg-iter-wrap-addr=%p, "
1064 muxer_msg_iter
, muxer_upstream_msg_iter
, next_return_ts
);
1065 BT_ASSERT(status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
);
1066 BT_ASSERT(muxer_upstream_msg_iter
);
1069 * Consume from the queue's head: other side
1070 * (muxer_upstream_msg_iter_next()) writes to the tail.
1072 *msg
= g_queue_pop_head(muxer_upstream_msg_iter
->msgs
);
1074 muxer_msg_iter
->last_returned_ts_ns
= next_return_ts
;
1081 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next(
1082 struct muxer_comp
*muxer_comp
,
1083 struct muxer_msg_iter
*muxer_msg_iter
,
1084 bt_message_array_const msgs
, uint64_t capacity
,
1087 bt_component_class_message_iterator_next_method_status status
=
1088 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1091 while (i
< capacity
&& status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1092 status
= muxer_msg_iter_do_next_one(muxer_comp
,
1093 muxer_msg_iter
, &msgs
[i
]);
1094 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1101 * Even if muxer_msg_iter_do_next_one() returned
1102 * something else than
1103 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1104 * message objects in the output message
1105 * array, so we need to return
1106 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1107 * transfered to downstream. This other status occurs
1108 * again the next time muxer_msg_iter_do_next() is
1109 * called, possibly without any accumulated
1110 * message, in which case we'll return it.
1113 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1120 void destroy_muxer_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
)
1122 struct muxer_comp
*muxer_comp
;
1124 if (!muxer_msg_iter
) {
1128 muxer_comp
= muxer_msg_iter
->muxer_comp
;
1129 BT_COMP_LOGD("Destroying muxer component's message iterator: "
1130 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1132 if (muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1133 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
1135 muxer_msg_iter
->active_muxer_upstream_msg_iters
, TRUE
);
1138 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1139 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
1141 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, TRUE
);
1144 g_free(muxer_msg_iter
);
1148 bt_component_class_message_iterator_init_method_status
1149 muxer_msg_iter_init_upstream_iterators(struct muxer_comp
*muxer_comp
,
1150 struct muxer_msg_iter
*muxer_msg_iter
)
1154 bt_component_class_message_iterator_init_method_status status
;
1156 count
= bt_component_filter_get_input_port_count(
1157 bt_self_component_filter_as_component_filter(
1158 muxer_comp
->self_comp_flt
));
1160 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
1161 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1162 muxer_comp
, muxer_msg_iter
);
1163 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1167 for (i
= 0; i
< count
; i
++) {
1168 bt_self_component_port_input_message_iterator
*upstream_msg_iter
;
1169 bt_self_component_port_input
*self_port
=
1170 bt_self_component_filter_borrow_input_port_by_index(
1171 muxer_comp
->self_comp_flt
, i
);
1172 const bt_port
*port
;
1173 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
1177 BT_ASSERT(self_port
);
1178 port
= bt_self_component_port_as_port(
1179 bt_self_component_port_input_as_self_component_port(
1183 if (!bt_port_is_connected(port
)) {
1184 /* Skip non-connected port */
1188 msg_iter_status
= create_msg_iter_on_input_port(muxer_comp
,
1189 muxer_msg_iter
, self_port
, &upstream_msg_iter
);
1190 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
1191 /* create_msg_iter_on_input_port() logs errors */
1192 status
= (int) msg_iter_status
;
1196 int_status
= muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter
,
1198 bt_self_component_port_input_message_iterator_put_ref(
1201 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1202 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1207 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
1214 bt_component_class_message_iterator_init_method_status
muxer_msg_iter_init(
1215 bt_self_message_iterator
*self_msg_iter
,
1216 bt_self_component_filter
*self_comp
,
1217 bt_self_component_port_output
*port
)
1219 struct muxer_comp
*muxer_comp
= NULL
;
1220 struct muxer_msg_iter
*muxer_msg_iter
= NULL
;
1221 bt_component_class_message_iterator_init_method_status status
;
1223 muxer_comp
= bt_self_component_get_data(
1224 bt_self_component_filter_as_self_component(self_comp
));
1225 BT_ASSERT(muxer_comp
);
1226 BT_COMP_LOGD("Initializing muxer component's message iterator: "
1227 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1228 self_comp
, muxer_comp
, self_msg_iter
);
1230 if (muxer_comp
->initializing_muxer_msg_iter
) {
1232 * Weird, unhandled situation detected: downstream
1233 * creates a muxer message iterator while creating
1234 * another muxer message iterator (same component).
1236 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
1237 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1238 self_comp
, muxer_comp
, self_msg_iter
);
1239 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
1243 muxer_comp
->initializing_muxer_msg_iter
= true;
1244 muxer_msg_iter
= g_new0(struct muxer_msg_iter
, 1);
1245 if (!muxer_msg_iter
) {
1246 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
1247 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
1251 muxer_msg_iter
->muxer_comp
= muxer_comp
;
1252 muxer_msg_iter
->self_msg_iter
= self_msg_iter
;
1253 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1254 muxer_msg_iter
->active_muxer_upstream_msg_iters
=
1255 g_ptr_array_new_with_free_func(
1256 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1257 if (!muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1258 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1259 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
1263 muxer_msg_iter
->ended_muxer_upstream_msg_iters
=
1264 g_ptr_array_new_with_free_func(
1265 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1266 if (!muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1267 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1268 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
1272 status
= muxer_msg_iter_init_upstream_iterators(muxer_comp
,
1275 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
1276 "comp-addr=%p, muxer-comp-addr=%p, "
1277 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1278 self_comp
, muxer_comp
, muxer_msg_iter
,
1279 self_msg_iter
, status
);
1283 bt_self_message_iterator_set_data(self_msg_iter
, muxer_msg_iter
);
1284 BT_COMP_LOGD("Initialized muxer component's message iterator: "
1285 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1287 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1291 destroy_muxer_msg_iter(muxer_msg_iter
);
1292 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
1295 muxer_comp
->initializing_muxer_msg_iter
= false;
1300 void muxer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1302 struct muxer_msg_iter
*muxer_msg_iter
=
1303 bt_self_message_iterator_get_data(self_msg_iter
);
1304 bt_self_component
*self_comp
= NULL
;
1305 struct muxer_comp
*muxer_comp
= NULL
;
1307 self_comp
= bt_self_message_iterator_borrow_component(
1309 BT_ASSERT(self_comp
);
1310 muxer_comp
= bt_self_component_get_data(self_comp
);
1311 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
1312 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1314 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1316 if (muxer_msg_iter
) {
1317 destroy_muxer_msg_iter(muxer_msg_iter
);
1322 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_next(
1323 bt_self_message_iterator
*self_msg_iter
,
1324 bt_message_array_const msgs
, uint64_t capacity
,
1327 bt_component_class_message_iterator_next_method_status status
;
1328 struct muxer_msg_iter
*muxer_msg_iter
=
1329 bt_self_message_iterator_get_data(self_msg_iter
);
1330 bt_self_component
*self_comp
= NULL
;
1331 struct muxer_comp
*muxer_comp
= NULL
;
1333 BT_ASSERT(muxer_msg_iter
);
1334 self_comp
= bt_self_message_iterator_borrow_component(
1336 BT_ASSERT(self_comp
);
1337 muxer_comp
= bt_self_component_get_data(self_comp
);
1338 BT_ASSERT(muxer_comp
);
1339 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
1340 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1342 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1344 status
= muxer_msg_iter_do_next(muxer_comp
, muxer_msg_iter
,
1345 msgs
, capacity
, count
);
1347 BT_COMP_LOGE("Cannot get next message: "
1348 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1349 "msg-iter-addr=%p, status=%s",
1350 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
,
1351 bt_common_func_status_string(status
));
1353 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
1355 bt_common_func_status_string(status
));
1362 bt_component_class_port_connected_method_status
muxer_input_port_connected(
1363 bt_self_component_filter
*self_comp
,
1364 bt_self_component_port_input
*self_port
,
1365 const bt_port_output
*other_port
)
1367 bt_component_class_port_connected_method_status status
=
1368 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK
;
1369 bt_self_component_add_port_status add_port_status
;
1370 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
1371 bt_self_component_filter_as_self_component(self_comp
));
1373 add_port_status
= add_available_input_port(self_comp
);
1374 if (add_port_status
) {
1375 BT_COMP_LOGE("Cannot add one muxer component's input port: "
1377 bt_common_func_status_string(status
));
1379 if (add_port_status
==
1380 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
1381 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR
;
1383 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR
;
1394 bt_component_class_message_iterator_can_seek_beginning_method_status
1395 muxer_upstream_msg_iters_can_all_seek_beginning(
1396 GPtrArray
*muxer_upstream_msg_iters
, bt_bool
*can_seek
)
1398 bt_component_class_message_iterator_can_seek_beginning_method_status status
=
1399 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
;
1402 for (i
= 0; i
< muxer_upstream_msg_iters
->len
; i
++) {
1403 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1404 muxer_upstream_msg_iters
->pdata
[i
];
1405 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1406 upstream_msg_iter
->msg_iter
, can_seek
);
1407 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
) {
1416 *can_seek
= BT_TRUE
;
1423 bt_component_class_message_iterator_can_seek_beginning_method_status
1424 muxer_msg_iter_can_seek_beginning(
1425 bt_self_message_iterator
*self_msg_iter
, bt_bool
*can_seek
)
1427 struct muxer_msg_iter
*muxer_msg_iter
=
1428 bt_self_message_iterator_get_data(self_msg_iter
);
1429 bt_component_class_message_iterator_can_seek_beginning_method_status status
;
1431 status
= muxer_upstream_msg_iters_can_all_seek_beginning(
1432 muxer_msg_iter
->active_muxer_upstream_msg_iters
, can_seek
);
1433 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
) {
1441 status
= muxer_upstream_msg_iters_can_all_seek_beginning(
1442 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, can_seek
);
1449 bt_component_class_message_iterator_seek_beginning_method_status
muxer_msg_iter_seek_beginning(
1450 bt_self_message_iterator
*self_msg_iter
)
1452 struct muxer_msg_iter
*muxer_msg_iter
=
1453 bt_self_message_iterator_get_data(self_msg_iter
);
1454 bt_component_class_message_iterator_seek_beginning_method_status status
=
1455 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK
;
1456 bt_message_iterator_seek_beginning_status seek_beg_status
;
1459 /* Seek all ended upstream iterators first */
1460 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1462 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1463 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1465 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1466 upstream_msg_iter
->msg_iter
);
1467 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1468 status
= (int) seek_beg_status
;
1472 empty_message_queue(upstream_msg_iter
);
1475 /* Seek all previously active upstream iterators */
1476 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
1478 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1479 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
];
1481 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1482 upstream_msg_iter
->msg_iter
);
1483 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1484 status
= (int) seek_beg_status
;
1488 empty_message_queue(upstream_msg_iter
);
1491 /* Make them all active */
1492 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1494 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1495 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1497 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1499 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1503 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1504 * called on an empty array.
1506 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
> 0) {
1507 g_ptr_array_remove_range(muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1508 0, muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
);
1510 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1511 muxer_msg_iter
->clock_class_expectation
=
1512 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
;