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"
40 #include "plugins/common/param-validation/param-validation.h"
46 bt_self_component_filter
*self_comp_flt
;
47 bt_self_component
*self_comp
;
49 unsigned int next_port_num
;
50 size_t available_input_ports
;
51 bool initializing_muxer_msg_iter
;
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 bt_self_message_iterator
*self_msg_iter
;
80 * Array of struct muxer_upstream_msg_iter * (owned by this).
82 * NOTE: This array is searched in linearly to find the youngest
83 * current message. Keep this until benchmarks confirm that
84 * another data structure is faster than this for our typical
87 GPtrArray
*active_muxer_upstream_msg_iters
;
90 * Array of struct muxer_upstream_msg_iter * (owned by this).
92 * We move ended message iterators from
93 * `active_muxer_upstream_msg_iters` to this array so as to be
94 * able to restore them when seeking.
96 GPtrArray
*ended_muxer_upstream_msg_iters
;
98 /* Last time returned in a message */
99 int64_t last_returned_ts_ns
;
101 /* Clock class expectation state */
102 enum muxer_msg_iter_clock_class_expectation clock_class_expectation
;
105 * Expected clock class UUID, only valid when
106 * clock_class_expectation is
107 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
109 bt_uuid_t expected_clock_class_uuid
;
112 * Saved error. If we hit an error in the _next method, but have some
113 * messages ready to return, we save the error here and return it on
114 * the next _next call.
116 bt_component_class_message_iterator_next_method_status next_saved_status
;
117 const struct bt_error
*next_saved_error
;
121 void empty_message_queue(struct muxer_upstream_msg_iter
*upstream_msg_iter
)
123 const bt_message
*msg
;
125 while ((msg
= g_queue_pop_head(upstream_msg_iter
->msgs
))) {
126 bt_message_put_ref(msg
);
131 void destroy_muxer_upstream_msg_iter(
132 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
)
134 struct muxer_comp
*muxer_comp
;
136 if (!muxer_upstream_msg_iter
) {
140 muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
141 BT_COMP_LOGD("Destroying muxer's upstream message iterator wrapper: "
142 "addr=%p, msg-iter-addr=%p, queue-len=%u",
143 muxer_upstream_msg_iter
,
144 muxer_upstream_msg_iter
->msg_iter
,
145 muxer_upstream_msg_iter
->msgs
->length
);
146 bt_self_component_port_input_message_iterator_put_ref(
147 muxer_upstream_msg_iter
->msg_iter
);
149 if (muxer_upstream_msg_iter
->msgs
) {
150 empty_message_queue(muxer_upstream_msg_iter
);
151 g_queue_free(muxer_upstream_msg_iter
->msgs
);
154 g_free(muxer_upstream_msg_iter
);
158 int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
,
159 bt_self_component_port_input_message_iterator
*self_msg_iter
)
162 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
163 g_new0(struct muxer_upstream_msg_iter
, 1);
164 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
166 if (!muxer_upstream_msg_iter
) {
167 BT_COMP_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
171 muxer_upstream_msg_iter
->muxer_comp
= muxer_comp
;
172 muxer_upstream_msg_iter
->msg_iter
= self_msg_iter
;
173 bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter
->msg_iter
);
174 muxer_upstream_msg_iter
->msgs
= g_queue_new();
175 if (!muxer_upstream_msg_iter
->msgs
) {
176 BT_COMP_LOGE_STR("Failed to allocate a GQueue.");
180 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
181 muxer_upstream_msg_iter
);
182 BT_COMP_LOGD("Added muxer's upstream message iterator wrapper: "
183 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
184 muxer_upstream_msg_iter
, muxer_msg_iter
,
190 g_free(muxer_upstream_msg_iter
);
198 bt_self_component_add_port_status
add_available_input_port(
199 bt_self_component_filter
*self_comp
)
201 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
202 bt_self_component_filter_as_self_component(self_comp
));
203 bt_self_component_add_port_status status
=
204 BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
;
205 GString
*port_name
= NULL
;
207 BT_ASSERT(muxer_comp
);
208 port_name
= g_string_new("in");
210 BT_COMP_LOGE_STR("Failed to allocate a GString.");
211 status
= BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
;
215 g_string_append_printf(port_name
, "%u", muxer_comp
->next_port_num
);
216 status
= bt_self_component_filter_add_input_port(
217 self_comp
, port_name
->str
, NULL
, NULL
);
218 if (status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
219 BT_COMP_LOGE("Cannot add input port to muxer component: "
220 "port-name=\"%s\", comp-addr=%p, status=%s",
221 port_name
->str
, self_comp
,
222 bt_common_func_status_string(status
));
226 muxer_comp
->available_input_ports
++;
227 muxer_comp
->next_port_num
++;
228 BT_COMP_LOGI("Added one input port to muxer component: "
229 "port-name=\"%s\", comp-addr=%p",
230 port_name
->str
, self_comp
);
234 g_string_free(port_name
, TRUE
);
241 bt_self_component_add_port_status
create_output_port(
242 bt_self_component_filter
*self_comp
)
244 return bt_self_component_filter_add_output_port(
245 self_comp
, "out", NULL
, NULL
);
249 void destroy_muxer_comp(struct muxer_comp
*muxer_comp
)
259 struct bt_param_validation_map_value_entry_descr muxer_params
[] = {
260 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
264 bt_component_class_initialize_method_status
muxer_init(
265 bt_self_component_filter
*self_comp_flt
,
266 bt_self_component_filter_configuration
*config
,
267 const bt_value
*params
, void *init_data
)
269 bt_component_class_initialize_method_status status
;
270 bt_self_component_add_port_status add_port_status
;
271 bt_self_component
*self_comp
=
272 bt_self_component_filter_as_self_component(self_comp_flt
);
273 struct muxer_comp
*muxer_comp
= g_new0(struct muxer_comp
, 1);
274 bt_logging_level log_level
= bt_component_get_logging_level(
275 bt_self_component_as_component(self_comp
));
276 enum bt_param_validation_status validation_status
;
277 gchar
*validate_error
= NULL
;
279 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
280 "Initializing muxer component: "
281 "comp-addr=%p, params-addr=%p", self_comp
, params
);
284 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
285 "Failed to allocate one muxer component.");
286 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
290 muxer_comp
->log_level
= log_level
;
291 muxer_comp
->self_comp
= self_comp
;
292 muxer_comp
->self_comp_flt
= self_comp_flt
;
294 validation_status
= bt_param_validation_validate(params
,
295 muxer_params
, &validate_error
);
296 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
297 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
299 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
300 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
301 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "%s", validate_error
);
305 bt_self_component_set_data(self_comp
, muxer_comp
);
306 add_port_status
= add_available_input_port(self_comp_flt
);
307 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
308 BT_COMP_LOGE("Cannot ensure that at least one muxer component's input port is available: "
309 "muxer-comp-addr=%p, status=%s",
311 bt_common_func_status_string(add_port_status
));
312 status
= (int) add_port_status
;
316 add_port_status
= create_output_port(self_comp_flt
);
317 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
318 BT_COMP_LOGE("Cannot create muxer component's output port: "
319 "muxer-comp-addr=%p, status=%s",
321 bt_common_func_status_string(add_port_status
));
322 status
= (int) add_port_status
;
326 BT_COMP_LOGI("Initialized muxer component: "
327 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
328 self_comp
, params
, muxer_comp
);
330 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
334 destroy_muxer_comp(muxer_comp
);
335 bt_self_component_set_data(self_comp
, NULL
);
338 g_free(validate_error
);
343 void muxer_finalize(bt_self_component_filter
*self_comp
)
345 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
346 bt_self_component_filter_as_self_component(self_comp
));
348 BT_COMP_LOGI("Finalizing muxer component: comp-addr=%p",
350 destroy_muxer_comp(muxer_comp
);
354 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
355 create_msg_iter_on_input_port(struct muxer_comp
*muxer_comp
,
356 struct muxer_msg_iter
*muxer_msg_iter
,
357 bt_self_component_port_input
*self_port
,
358 bt_self_component_port_input_message_iterator
**msg_iter
)
360 const bt_port
*port
= bt_self_component_port_as_port(
361 bt_self_component_port_input_as_self_component_port(
363 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
367 BT_ASSERT(bt_port_is_connected(port
));
369 // TODO: Advance the iterator to >= the time of the latest
370 // returned message by the muxer message
371 // iterator which creates it.
372 status
= bt_self_component_port_input_message_iterator_create_from_message_iterator(
373 muxer_msg_iter
->self_msg_iter
, self_port
, msg_iter
);
374 if (status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
375 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
376 "port-addr=%p, port-name=\"%s\"",
377 port
, bt_port_get_name(port
));
381 BT_COMP_LOGI("Created upstream message iterator on input port: "
382 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
383 port
, bt_port_get_name(port
), msg_iter
);
390 bt_component_class_message_iterator_next_method_status
muxer_upstream_msg_iter_next(
391 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
394 struct muxer_comp
*muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
395 bt_component_class_message_iterator_next_method_status status
;
396 bt_message_iterator_next_status input_port_iter_status
;
397 bt_message_array_const msgs
;
401 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
402 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
403 muxer_upstream_msg_iter
,
404 muxer_upstream_msg_iter
->msg_iter
);
405 input_port_iter_status
= bt_self_component_port_input_message_iterator_next(
406 muxer_upstream_msg_iter
->msg_iter
, &msgs
, &count
);
407 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
409 bt_common_func_status_string(input_port_iter_status
));
411 switch (input_port_iter_status
) {
412 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
:
414 * Message iterator's current message is
415 * valid: it must be considered for muxing operations.
417 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
418 BT_ASSERT_DBG(count
> 0);
420 /* Move messages to our queue */
421 for (i
= 0; i
< count
; i
++) {
423 * Push to tail in order; other side
424 * (muxer_msg_iter_do_next_one()) consumes
425 * from the head first.
427 g_queue_push_tail(muxer_upstream_msg_iter
->msgs
,
430 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
432 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN
:
434 * Message iterator's current message is not
435 * valid anymore. Return
436 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
438 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN
;
440 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END
: /* Fall-through. */
442 * Message iterator reached the end: release it. It
443 * won't be considered again to find the youngest
447 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
449 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
:
450 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR
:
451 /* Error status code */
452 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
453 "Upstream iterator's next method returned an error: status=%s",
454 bt_common_func_status_string(input_port_iter_status
));
455 status
= (int) input_port_iter_status
;
458 /* Unsupported status code */
459 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
460 "Unsupported status code: status=%s",
461 bt_common_func_status_string(input_port_iter_status
));
462 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
470 int get_msg_ts_ns(struct muxer_comp
*muxer_comp
,
471 struct muxer_msg_iter
*muxer_msg_iter
,
472 const bt_message
*msg
, int64_t last_returned_ts_ns
,
475 const bt_clock_snapshot
*clock_snapshot
= NULL
;
477 const bt_stream_class
*stream_class
= NULL
;
478 bt_message_type msg_type
;
481 BT_ASSERT_DBG(ts_ns
);
482 BT_COMP_LOGD("Getting message's timestamp: "
483 "muxer-msg-iter-addr=%p, msg-addr=%p, "
484 "last-returned-ts=%" PRId64
,
485 muxer_msg_iter
, msg
, last_returned_ts_ns
);
487 if (G_UNLIKELY(muxer_msg_iter
->clock_class_expectation
==
488 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
)) {
489 *ts_ns
= last_returned_ts_ns
;
493 msg_type
= bt_message_get_type(msg
);
495 if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_BEGINNING
)) {
496 stream_class
= bt_stream_borrow_class_const(
497 bt_packet_borrow_stream_const(
498 bt_message_packet_beginning_borrow_packet_const(
500 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_PACKET_END
)) {
501 stream_class
= bt_stream_borrow_class_const(
502 bt_packet_borrow_stream_const(
503 bt_message_packet_end_borrow_packet_const(
505 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
)) {
506 stream_class
= bt_stream_borrow_class_const(
507 bt_message_discarded_events_borrow_stream_const(msg
));
508 } else if (G_UNLIKELY(msg_type
== BT_MESSAGE_TYPE_DISCARDED_PACKETS
)) {
509 stream_class
= bt_stream_borrow_class_const(
510 bt_message_discarded_packets_borrow_stream_const(msg
));
514 case BT_MESSAGE_TYPE_EVENT
:
515 BT_ASSERT_DBG(bt_message_event_borrow_stream_class_default_clock_class_const(
517 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
520 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
521 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
523 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
526 goto no_clock_snapshot
;
530 case BT_MESSAGE_TYPE_PACKET_END
:
531 if (bt_stream_class_packets_have_end_default_clock_snapshot(
533 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
536 goto no_clock_snapshot
;
540 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
542 enum bt_message_stream_clock_snapshot_state snapshot_state
=
543 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
544 msg
, &clock_snapshot
);
545 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
546 goto no_clock_snapshot
;
551 case BT_MESSAGE_TYPE_STREAM_END
:
553 enum bt_message_stream_clock_snapshot_state snapshot_state
=
554 bt_message_stream_end_borrow_default_clock_snapshot_const(
555 msg
, &clock_snapshot
);
556 if (snapshot_state
== BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
557 goto no_clock_snapshot
;
562 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
563 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
565 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
568 goto no_clock_snapshot
;
572 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
573 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
575 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
578 goto no_clock_snapshot
;
582 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
583 clock_snapshot
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
587 /* All the other messages have a higher priority */
588 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
589 *ts_ns
= last_returned_ts_ns
;
593 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, ts_ns
);
595 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
596 "clock-snapshot-addr=%p", clock_snapshot
);
603 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
604 "using the last returned timestamp.");
605 *ts_ns
= last_returned_ts_ns
;
613 BT_COMP_LOGD("Found message's timestamp: "
614 "muxer-msg-iter-addr=%p, msg-addr=%p, "
615 "last-returned-ts=%" PRId64
", ts=%" PRId64
,
616 muxer_msg_iter
, msg
, last_returned_ts_ns
,
624 int validate_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
625 struct muxer_comp
*muxer_comp
,
626 const bt_clock_class
*clock_class
)
629 const uint8_t *cc_uuid
;
632 BT_ASSERT_DBG(clock_class
);
633 cc_uuid
= bt_clock_class_get_uuid(clock_class
);
634 cc_name
= bt_clock_class_get_name(clock_class
);
636 if (muxer_msg_iter
->clock_class_expectation
==
637 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
639 * This is the first clock class that this muxer message
640 * iterator encounters. Its properties determine what to expect
641 * for the whole lifetime of the iterator.
643 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
644 /* Expect absolute clock classes */
645 muxer_msg_iter
->clock_class_expectation
=
646 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
;
650 * Expect non-absolute clock classes
651 * with a specific UUID.
653 muxer_msg_iter
->clock_class_expectation
=
654 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
;
655 bt_uuid_copy(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
);
658 * Expect non-absolute clock classes
661 muxer_msg_iter
->clock_class_expectation
=
662 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
;
667 switch (muxer_msg_iter
->clock_class_expectation
) {
668 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE
:
669 if (!bt_clock_class_origin_is_unix_epoch(clock_class
)) {
670 BT_COMP_LOGE("Expecting an absolute clock class, "
671 "but got a non-absolute one: "
672 "clock-class-addr=%p, clock-class-name=\"%s\"",
673 clock_class
, cc_name
);
677 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID
:
678 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
679 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
680 "but got an absolute one: "
681 "clock-class-addr=%p, clock-class-name=\"%s\"",
682 clock_class
, cc_name
);
687 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
688 "but got one with a UUID: "
689 "clock-class-addr=%p, clock-class-name=\"%s\", "
690 "uuid=\"" BT_UUID_FMT
"\"",
691 clock_class
, cc_name
, BT_UUID_FMT_VALUES(cc_uuid
));
695 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID
:
696 if (bt_clock_class_origin_is_unix_epoch(clock_class
)) {
697 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
698 "but got an absolute one: "
699 "clock-class-addr=%p, clock-class-name=\"%s\"",
700 clock_class
, cc_name
);
705 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
706 "but got one with no UUID: "
707 "clock-class-addr=%p, clock-class-name=\"%s\"",
708 clock_class
, cc_name
);
712 if (bt_uuid_compare(muxer_msg_iter
->expected_clock_class_uuid
, cc_uuid
) != 0) {
713 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
714 "but got one with different UUID: "
715 "clock-class-addr=%p, clock-class-name=\"%s\", "
716 "expected-uuid=\"" BT_UUID_FMT
"\", "
717 "uuid=\"" BT_UUID_FMT
"\"",
718 clock_class
, cc_name
,
719 BT_UUID_FMT_VALUES(muxer_msg_iter
->expected_clock_class_uuid
),
720 BT_UUID_FMT_VALUES(cc_uuid
));
724 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
:
725 BT_COMP_LOGE("Expecting no clock class, but got one: "
726 "clock-class-addr=%p, clock-class-name=\"%s\"",
727 clock_class
, cc_name
);
731 BT_COMP_LOGF("Unexpected clock class expectation: "
732 "expectation-code=%d",
733 muxer_msg_iter
->clock_class_expectation
);
747 int validate_new_stream_clock_class(struct muxer_msg_iter
*muxer_msg_iter
,
748 struct muxer_comp
*muxer_comp
, const bt_stream
*stream
)
751 const bt_stream_class
*stream_class
=
752 bt_stream_borrow_class_const(stream
);
753 const bt_clock_class
*clock_class
=
754 bt_stream_class_borrow_default_clock_class_const(stream_class
);
757 if (muxer_msg_iter
->clock_class_expectation
==
758 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
) {
759 /* Expect no clock class */
760 muxer_msg_iter
->clock_class_expectation
=
761 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
;
762 } else if (muxer_msg_iter
->clock_class_expectation
!=
763 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE
) {
764 BT_COMP_LOGE("Expecting stream class without a default clock class: "
765 "stream-class-addr=%p, stream-class-name=\"%s\", "
766 "stream-class-id=%" PRIu64
,
767 stream_class
, bt_stream_class_get_name(stream_class
),
768 bt_stream_class_get_id(stream_class
));
775 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
, clock_class
);
782 * This function finds the youngest available message amongst the
783 * non-ended upstream message iterators and returns the upstream
784 * message iterator which has it, or
785 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
788 * This function does NOT:
790 * * Update any upstream message iterator.
791 * * Check the upstream message iterators to retry.
793 * On sucess, this function sets *muxer_upstream_msg_iter to the
794 * upstream message iterator of which the current message is
795 * the youngest, and sets *ts_ns to its time.
798 bt_component_class_message_iterator_next_method_status
799 muxer_msg_iter_youngest_upstream_msg_iter(
800 struct muxer_comp
*muxer_comp
,
801 struct muxer_msg_iter
*muxer_msg_iter
,
802 struct muxer_upstream_msg_iter
**muxer_upstream_msg_iter
,
807 int64_t youngest_ts_ns
= INT64_MAX
;
808 bt_component_class_message_iterator_next_method_status status
=
809 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
811 BT_ASSERT_DBG(muxer_comp
);
812 BT_ASSERT_DBG(muxer_msg_iter
);
813 BT_ASSERT_DBG(muxer_upstream_msg_iter
);
814 *muxer_upstream_msg_iter
= NULL
;
816 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
818 const bt_message
*msg
;
819 struct muxer_upstream_msg_iter
*cur_muxer_upstream_msg_iter
=
821 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
825 if (!cur_muxer_upstream_msg_iter
->msg_iter
) {
826 /* This upstream message iterator is ended */
827 BT_COMP_LOGT("Skipping ended upstream message iterator: "
828 "muxer-upstream-msg-iter-wrap-addr=%p",
829 cur_muxer_upstream_msg_iter
);
833 BT_ASSERT_DBG(cur_muxer_upstream_msg_iter
->msgs
->length
> 0);
834 msg
= g_queue_peek_head(cur_muxer_upstream_msg_iter
->msgs
);
837 if (G_UNLIKELY(bt_message_get_type(msg
) ==
838 BT_MESSAGE_TYPE_STREAM_BEGINNING
)) {
839 ret
= validate_new_stream_clock_class(
840 muxer_msg_iter
, muxer_comp
,
841 bt_message_stream_beginning_borrow_stream_const(
845 * validate_new_stream_clock_class() logs
848 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
851 } else if (G_UNLIKELY(bt_message_get_type(msg
) ==
852 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
)) {
853 const bt_clock_snapshot
*cs
;
855 cs
= bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
857 ret
= validate_clock_class(muxer_msg_iter
, muxer_comp
,
858 bt_clock_snapshot_borrow_clock_class_const(cs
));
860 /* validate_clock_class() logs errors */
861 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
866 ret
= get_msg_ts_ns(muxer_comp
, muxer_msg_iter
, msg
,
867 muxer_msg_iter
->last_returned_ts_ns
, &msg_ts_ns
);
869 /* get_msg_ts_ns() logs errors */
870 *muxer_upstream_msg_iter
= NULL
;
871 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
876 * Update the current message iterator if it has not been set
877 * yet, or if its current message has a timestamp smaller than
878 * the previously selected youngest message.
880 if (G_UNLIKELY(*muxer_upstream_msg_iter
== NULL
) ||
881 msg_ts_ns
< youngest_ts_ns
) {
882 *muxer_upstream_msg_iter
=
883 cur_muxer_upstream_msg_iter
;
884 youngest_ts_ns
= msg_ts_ns
;
885 *ts_ns
= youngest_ts_ns
;
886 } else if (msg_ts_ns
== youngest_ts_ns
) {
888 * The currently selected message to be sent downstream
889 * next has the exact same timestamp that of the
890 * current candidate message. We must break the tie
891 * in a predictable manner.
893 const bt_message
*selected_msg
= g_queue_peek_head(
894 (*muxer_upstream_msg_iter
)->msgs
);
895 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
898 * Order the messages in an arbitrary but determinitic
901 ret
= common_muxing_compare_messages(msg
, selected_msg
);
904 * The `msg` should go first. Update the next
905 * iterator and the current timestamp.
907 *muxer_upstream_msg_iter
=
908 cur_muxer_upstream_msg_iter
;
909 youngest_ts_ns
= msg_ts_ns
;
910 *ts_ns
= youngest_ts_ns
;
911 } else if (ret
== 0) {
912 /* Unable to pick which one should go first. */
913 BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
914 "muxer-upstream-msg-iter-wrap-addr=%p"
915 "cur-muxer-upstream-msg-iter-wrap-addr=%p",
916 *muxer_upstream_msg_iter
,
917 cur_muxer_upstream_msg_iter
);
922 if (!*muxer_upstream_msg_iter
) {
923 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
932 bt_component_class_message_iterator_next_method_status
933 validate_muxer_upstream_msg_iter(
934 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
,
937 struct muxer_comp
*muxer_comp
= muxer_upstream_msg_iter
->muxer_comp
;
938 bt_component_class_message_iterator_next_method_status status
;
940 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
941 "muxer-upstream-msg-iter-wrap-addr=%p",
942 muxer_upstream_msg_iter
);
944 if (muxer_upstream_msg_iter
->msgs
->length
> 0 ||
945 !muxer_upstream_msg_iter
->msg_iter
) {
946 BT_COMP_LOGD("Already valid or not considered: "
947 "queue-len=%u, upstream-msg-iter-addr=%p",
948 muxer_upstream_msg_iter
->msgs
->length
,
949 muxer_upstream_msg_iter
->msg_iter
);
950 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
954 /* muxer_upstream_msg_iter_next() logs details/errors */
955 status
= muxer_upstream_msg_iter_next(muxer_upstream_msg_iter
,
963 bt_component_class_message_iterator_next_method_status
964 validate_muxer_upstream_msg_iters(
965 struct muxer_msg_iter
*muxer_msg_iter
)
967 struct muxer_comp
*muxer_comp
= muxer_msg_iter
->muxer_comp
;
968 bt_component_class_message_iterator_next_method_status status
;
971 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
972 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
974 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
976 bool is_ended
= false;
977 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
=
979 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
982 status
= validate_muxer_upstream_msg_iter(
983 muxer_upstream_msg_iter
, &is_ended
);
984 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
986 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
987 "Cannot validate muxer's upstream message iterator wrapper: "
988 "muxer-msg-iter-addr=%p, "
989 "muxer-upstream-msg-iter-wrap-addr=%p",
991 muxer_upstream_msg_iter
);
993 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
994 "muxer-msg-iter-addr=%p, "
995 "muxer-upstream-msg-iter-wrap-addr=%p",
997 muxer_upstream_msg_iter
);
1004 * Move this muxer upstream message iterator to the
1005 * array of ended iterators if it's ended.
1007 if (G_UNLIKELY(is_ended
)) {
1008 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
1009 "muxer-msg-iter-addr=%p, "
1010 "muxer-upstream-msg-iter-wrap-addr=%p",
1011 muxer_msg_iter
, muxer_upstream_msg_iter
);
1013 muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1014 muxer_upstream_msg_iter
);
1015 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1018 * Use g_ptr_array_remove_fast() because the
1019 * order of those elements is not important.
1021 g_ptr_array_remove_index_fast(
1022 muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1028 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1035 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next_one(
1036 struct muxer_comp
*muxer_comp
,
1037 struct muxer_msg_iter
*muxer_msg_iter
,
1038 const bt_message
**msg
)
1040 bt_component_class_message_iterator_next_method_status status
;
1041 struct muxer_upstream_msg_iter
*muxer_upstream_msg_iter
= NULL
;
1042 int64_t next_return_ts
;
1044 status
= validate_muxer_upstream_msg_iters(muxer_msg_iter
);
1045 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1046 /* validate_muxer_upstream_msg_iters() logs details */
1051 * At this point we know that all the existing upstream
1052 * message iterators are valid. We can find the one,
1053 * amongst those, of which the current message is the
1056 status
= muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp
,
1057 muxer_msg_iter
, &muxer_upstream_msg_iter
,
1059 if (status
< 0 || status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
) {
1061 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1062 "Cannot find the youngest upstream message iterator wrapper: "
1064 bt_common_func_status_string(status
));
1066 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
1068 bt_common_func_status_string(status
));
1074 if (next_return_ts
< muxer_msg_iter
->last_returned_ts_ns
) {
1075 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp
->self_comp
,
1076 "Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
1077 "muxer-msg-iter-addr=%p, ts=%" PRId64
", "
1078 "last-returned-ts=%" PRId64
,
1079 muxer_msg_iter
, next_return_ts
,
1080 muxer_msg_iter
->last_returned_ts_ns
);
1081 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1085 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
1086 "muxer-msg-iter-addr=%p, "
1087 "muxer-upstream-msg-iter-wrap-addr=%p, "
1089 muxer_msg_iter
, muxer_upstream_msg_iter
, next_return_ts
);
1090 BT_ASSERT_DBG(status
==
1091 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
);
1092 BT_ASSERT_DBG(muxer_upstream_msg_iter
);
1095 * Consume from the queue's head: other side
1096 * (muxer_upstream_msg_iter_next()) writes to the tail.
1098 *msg
= g_queue_pop_head(muxer_upstream_msg_iter
->msgs
);
1099 BT_ASSERT_DBG(*msg
);
1100 muxer_msg_iter
->last_returned_ts_ns
= next_return_ts
;
1107 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_do_next(
1108 struct muxer_comp
*muxer_comp
,
1109 struct muxer_msg_iter
*muxer_msg_iter
,
1110 bt_message_array_const msgs
, uint64_t capacity
,
1113 bt_component_class_message_iterator_next_method_status status
;
1116 if (G_UNLIKELY(muxer_msg_iter
->next_saved_error
)) {
1118 * Last time we were called, we hit an error but had some
1119 * messages to deliver, so we stashed the error here. Return
1122 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(muxer_msg_iter
->next_saved_error
);
1123 status
= muxer_msg_iter
->next_saved_status
;
1128 status
= muxer_msg_iter_do_next_one(muxer_comp
,
1129 muxer_msg_iter
, &msgs
[i
]);
1130 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1133 } while (i
< capacity
&& status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
);
1137 * Even if muxer_msg_iter_do_next_one() returned
1138 * something else than
1139 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1140 * message objects in the output message
1141 * array, so we need to return
1142 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1143 * transfered to downstream. This other status occurs
1144 * again the next time muxer_msg_iter_do_next() is
1145 * called, possibly without any accumulated
1146 * message, in which case we'll return it.
1150 * Save this error for the next _next call. Assume that
1151 * this component always appends error causes when
1152 * returning an error status code, which will cause the
1153 * current thread error to be non-NULL.
1155 muxer_msg_iter
->next_saved_error
= bt_current_thread_take_error();
1156 BT_ASSERT(muxer_msg_iter
->next_saved_error
);
1157 muxer_msg_iter
->next_saved_status
= status
;
1161 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1169 void destroy_muxer_msg_iter(struct muxer_msg_iter
*muxer_msg_iter
)
1171 struct muxer_comp
*muxer_comp
;
1173 if (!muxer_msg_iter
) {
1177 muxer_comp
= muxer_msg_iter
->muxer_comp
;
1178 BT_COMP_LOGD("Destroying muxer component's message iterator: "
1179 "muxer-msg-iter-addr=%p", muxer_msg_iter
);
1181 if (muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1182 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
1184 muxer_msg_iter
->active_muxer_upstream_msg_iters
, TRUE
);
1187 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1188 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
1190 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, TRUE
);
1193 g_free(muxer_msg_iter
);
1197 bt_component_class_message_iterator_initialize_method_status
1198 muxer_msg_iter_init_upstream_iterators(struct muxer_comp
*muxer_comp
,
1199 struct muxer_msg_iter
*muxer_msg_iter
,
1200 struct bt_self_message_iterator_configuration
*config
)
1204 bt_component_class_message_iterator_initialize_method_status status
;
1205 bool can_seek_forward
= true;
1207 count
= bt_component_filter_get_input_port_count(
1208 bt_self_component_filter_as_component_filter(
1209 muxer_comp
->self_comp_flt
));
1211 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
1212 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1213 muxer_comp
, muxer_msg_iter
);
1214 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK
;
1218 for (i
= 0; i
< count
; i
++) {
1219 bt_self_component_port_input_message_iterator
*upstream_msg_iter
;
1220 bt_self_component_port_input
*self_port
=
1221 bt_self_component_filter_borrow_input_port_by_index(
1222 muxer_comp
->self_comp_flt
, i
);
1223 const bt_port
*port
;
1224 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
1228 BT_ASSERT(self_port
);
1229 port
= bt_self_component_port_as_port(
1230 bt_self_component_port_input_as_self_component_port(
1234 if (!bt_port_is_connected(port
)) {
1235 /* Skip non-connected port */
1239 msg_iter_status
= create_msg_iter_on_input_port(muxer_comp
,
1240 muxer_msg_iter
, self_port
, &upstream_msg_iter
);
1241 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
1242 /* create_msg_iter_on_input_port() logs errors */
1243 status
= (int) msg_iter_status
;
1247 int_status
= muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter
,
1249 bt_self_component_port_input_message_iterator_put_ref(
1252 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1253 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1257 can_seek_forward
= can_seek_forward
&&
1258 bt_self_component_port_input_message_iterator_can_seek_forward(
1263 * This iterator can seek forward if all of its iterators can seek
1266 bt_self_message_iterator_configuration_set_can_seek_forward(
1267 config
, can_seek_forward
);
1269 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK
;
1276 bt_component_class_message_iterator_initialize_method_status
muxer_msg_iter_init(
1277 bt_self_message_iterator
*self_msg_iter
,
1278 bt_self_message_iterator_configuration
*config
,
1279 bt_self_component_filter
*self_comp
,
1280 bt_self_component_port_output
*port
)
1282 struct muxer_comp
*muxer_comp
= NULL
;
1283 struct muxer_msg_iter
*muxer_msg_iter
= NULL
;
1284 bt_component_class_message_iterator_initialize_method_status status
;
1286 muxer_comp
= bt_self_component_get_data(
1287 bt_self_component_filter_as_self_component(self_comp
));
1288 BT_ASSERT(muxer_comp
);
1289 BT_COMP_LOGD("Initializing muxer component's message iterator: "
1290 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1291 self_comp
, muxer_comp
, self_msg_iter
);
1293 if (muxer_comp
->initializing_muxer_msg_iter
) {
1295 * Weird, unhandled situation detected: downstream
1296 * creates a muxer message iterator while creating
1297 * another muxer message iterator (same component).
1299 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
1300 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1301 self_comp
, muxer_comp
, self_msg_iter
);
1302 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR
;
1306 muxer_comp
->initializing_muxer_msg_iter
= true;
1307 muxer_msg_iter
= g_new0(struct muxer_msg_iter
, 1);
1308 if (!muxer_msg_iter
) {
1309 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
1310 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1314 muxer_msg_iter
->muxer_comp
= muxer_comp
;
1315 muxer_msg_iter
->self_msg_iter
= self_msg_iter
;
1316 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1317 muxer_msg_iter
->active_muxer_upstream_msg_iters
=
1318 g_ptr_array_new_with_free_func(
1319 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1320 if (!muxer_msg_iter
->active_muxer_upstream_msg_iters
) {
1321 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1322 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1326 muxer_msg_iter
->ended_muxer_upstream_msg_iters
=
1327 g_ptr_array_new_with_free_func(
1328 (GDestroyNotify
) destroy_muxer_upstream_msg_iter
);
1329 if (!muxer_msg_iter
->ended_muxer_upstream_msg_iters
) {
1330 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1331 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
1335 status
= muxer_msg_iter_init_upstream_iterators(muxer_comp
,
1336 muxer_msg_iter
, config
);
1338 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
1339 "comp-addr=%p, muxer-comp-addr=%p, "
1340 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1341 self_comp
, muxer_comp
, muxer_msg_iter
,
1342 self_msg_iter
, status
);
1346 bt_self_message_iterator_set_data(self_msg_iter
, muxer_msg_iter
);
1347 BT_COMP_LOGD("Initialized muxer component's message iterator: "
1348 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1350 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1354 destroy_muxer_msg_iter(muxer_msg_iter
);
1355 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
1358 muxer_comp
->initializing_muxer_msg_iter
= false;
1363 void muxer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1365 struct muxer_msg_iter
*muxer_msg_iter
=
1366 bt_self_message_iterator_get_data(self_msg_iter
);
1367 bt_self_component
*self_comp
= NULL
;
1368 struct muxer_comp
*muxer_comp
= NULL
;
1370 self_comp
= bt_self_message_iterator_borrow_component(
1372 BT_ASSERT(self_comp
);
1373 muxer_comp
= bt_self_component_get_data(self_comp
);
1374 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
1375 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1377 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1379 if (muxer_msg_iter
) {
1380 destroy_muxer_msg_iter(muxer_msg_iter
);
1385 bt_component_class_message_iterator_next_method_status
muxer_msg_iter_next(
1386 bt_self_message_iterator
*self_msg_iter
,
1387 bt_message_array_const msgs
, uint64_t capacity
,
1390 bt_component_class_message_iterator_next_method_status status
;
1391 struct muxer_msg_iter
*muxer_msg_iter
=
1392 bt_self_message_iterator_get_data(self_msg_iter
);
1393 bt_self_component
*self_comp
= NULL
;
1394 struct muxer_comp
*muxer_comp
= NULL
;
1396 BT_ASSERT_DBG(muxer_msg_iter
);
1397 self_comp
= bt_self_message_iterator_borrow_component(
1399 BT_ASSERT_DBG(self_comp
);
1400 muxer_comp
= bt_self_component_get_data(self_comp
);
1401 BT_ASSERT_DBG(muxer_comp
);
1402 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
1403 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1405 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
);
1407 status
= muxer_msg_iter_do_next(muxer_comp
, muxer_msg_iter
,
1408 msgs
, capacity
, count
);
1410 BT_COMP_LOGE("Cannot get next message: "
1411 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1412 "msg-iter-addr=%p, status=%s",
1413 self_comp
, muxer_comp
, muxer_msg_iter
, self_msg_iter
,
1414 bt_common_func_status_string(status
));
1416 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
1418 bt_common_func_status_string(status
));
1425 bt_component_class_port_connected_method_status
muxer_input_port_connected(
1426 bt_self_component_filter
*self_comp
,
1427 bt_self_component_port_input
*self_port
,
1428 const bt_port_output
*other_port
)
1430 bt_component_class_port_connected_method_status status
=
1431 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK
;
1432 bt_self_component_add_port_status add_port_status
;
1433 struct muxer_comp
*muxer_comp
= bt_self_component_get_data(
1434 bt_self_component_filter_as_self_component(self_comp
));
1436 add_port_status
= add_available_input_port(self_comp
);
1437 if (add_port_status
) {
1438 BT_COMP_LOGE("Cannot add one muxer component's input port: "
1440 bt_common_func_status_string(status
));
1442 if (add_port_status
==
1443 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
) {
1444 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR
;
1446 status
= BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR
;
1457 bt_component_class_message_iterator_can_seek_beginning_method_status
1458 muxer_upstream_msg_iters_can_all_seek_beginning(
1459 GPtrArray
*muxer_upstream_msg_iters
, bt_bool
*can_seek
)
1461 bt_component_class_message_iterator_can_seek_beginning_method_status status
=
1462 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
;
1465 for (i
= 0; i
< muxer_upstream_msg_iters
->len
; i
++) {
1466 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1467 muxer_upstream_msg_iters
->pdata
[i
];
1468 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1469 upstream_msg_iter
->msg_iter
, can_seek
);
1470 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
) {
1479 *can_seek
= BT_TRUE
;
1486 bt_component_class_message_iterator_can_seek_beginning_method_status
1487 muxer_msg_iter_can_seek_beginning(
1488 bt_self_message_iterator
*self_msg_iter
, bt_bool
*can_seek
)
1490 struct muxer_msg_iter
*muxer_msg_iter
=
1491 bt_self_message_iterator_get_data(self_msg_iter
);
1492 bt_component_class_message_iterator_can_seek_beginning_method_status status
;
1494 status
= muxer_upstream_msg_iters_can_all_seek_beginning(
1495 muxer_msg_iter
->active_muxer_upstream_msg_iters
, can_seek
);
1496 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK
) {
1504 status
= muxer_upstream_msg_iters_can_all_seek_beginning(
1505 muxer_msg_iter
->ended_muxer_upstream_msg_iters
, can_seek
);
1512 bt_component_class_message_iterator_seek_beginning_method_status
muxer_msg_iter_seek_beginning(
1513 bt_self_message_iterator
*self_msg_iter
)
1515 struct muxer_msg_iter
*muxer_msg_iter
=
1516 bt_self_message_iterator_get_data(self_msg_iter
);
1517 bt_component_class_message_iterator_seek_beginning_method_status status
=
1518 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK
;
1519 bt_message_iterator_seek_beginning_status seek_beg_status
;
1522 /* Seek all ended upstream iterators first */
1523 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1525 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1526 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1528 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1529 upstream_msg_iter
->msg_iter
);
1530 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1531 status
= (int) seek_beg_status
;
1535 empty_message_queue(upstream_msg_iter
);
1538 /* Seek all previously active upstream iterators */
1539 for (i
= 0; i
< muxer_msg_iter
->active_muxer_upstream_msg_iters
->len
;
1541 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1542 muxer_msg_iter
->active_muxer_upstream_msg_iters
->pdata
[i
];
1544 seek_beg_status
= bt_self_component_port_input_message_iterator_seek_beginning(
1545 upstream_msg_iter
->msg_iter
);
1546 if (seek_beg_status
!= BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK
) {
1547 status
= (int) seek_beg_status
;
1551 empty_message_queue(upstream_msg_iter
);
1554 /* Make them all active */
1555 for (i
= 0; i
< muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
;
1557 struct muxer_upstream_msg_iter
*upstream_msg_iter
=
1558 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
];
1560 g_ptr_array_add(muxer_msg_iter
->active_muxer_upstream_msg_iters
,
1562 muxer_msg_iter
->ended_muxer_upstream_msg_iters
->pdata
[i
] = NULL
;
1566 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1567 * called on an empty array.
1569 if (muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
> 0) {
1570 g_ptr_array_remove_range(muxer_msg_iter
->ended_muxer_upstream_msg_iters
,
1571 0, muxer_msg_iter
->ended_muxer_upstream_msg_iters
->len
);
1573 muxer_msg_iter
->last_returned_ts_ns
= INT64_MIN
;
1574 muxer_msg_iter
->clock_class_expectation
=
1575 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY
;