lib: make can_seek_ns_from_origin logic use `can_seek_forward` property of iterator
[babeltrace.git] / src / plugins / utils / muxer / muxer.c
index e68c372c0addaa43615c152abbe894a785af702b..3e4d42057b1d49e0a00814cdd6f063f9f11da634 100644 (file)
@@ -23,7 +23,7 @@
 #define BT_COMP_LOG_SELF_COMP (muxer_comp->self_comp)
 #define BT_LOG_OUTPUT_LEVEL (muxer_comp->log_level)
 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.MUXER"
-#include "plugins/comp-logging.h"
+#include "logging/comp-logging.h"
 
 #include "common/macros.h"
 #include "common/uuid.h"
@@ -36,9 +36,9 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "muxer.h"
+#include "plugins/common/muxing/muxing.h"
 
-#define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME       "assume-absolute-clock-classes"
+#include "muxer.h"
 
 struct muxer_comp {
        /* Weak refs */
@@ -48,7 +48,6 @@ struct muxer_comp {
        unsigned int next_port_num;
        size_t available_input_ports;
        bool initializing_muxer_msg_iter;
-       bool assume_absolute_clock_classes;
        bt_logging_level log_level;
 };
 
@@ -73,6 +72,9 @@ enum muxer_msg_iter_clock_class_expectation {
 struct muxer_msg_iter {
        struct muxer_comp *muxer_comp;
 
+       /* Weak */
+       bt_self_message_iterator *self_msg_iter;
+
        /*
         * Array of struct muxer_upstream_msg_iter * (owned by this).
         *
@@ -244,96 +246,14 @@ void destroy_muxer_comp(struct muxer_comp *muxer_comp)
        g_free(muxer_comp);
 }
 
-static
-bt_value *get_default_params(struct muxer_comp *muxer_comp)
-{
-       bt_value *params;
-       int ret;
-
-       params = bt_value_map_create();
-       if (!params) {
-               BT_COMP_LOGE_STR("Cannot create a map value object.");
-               goto error;
-       }
-
-       ret = bt_value_map_insert_bool_entry(params,
-               ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false);
-       if (ret) {
-               BT_COMP_LOGE_STR("Cannot add boolean value to map value object.");
-               goto error;
-       }
-
-       goto end;
-
-error:
-       BT_VALUE_PUT_REF_AND_RESET(params);
-
-end:
-       return params;
-}
-
-static
-int configure_muxer_comp(struct muxer_comp *muxer_comp,
-               const bt_value *params)
-{
-       bt_value *default_params = NULL;
-       bt_value *real_params = NULL;
-       const bt_value *assume_absolute_clock_classes = NULL;
-       int ret = 0;
-       bt_bool bool_val;
-
-       default_params = get_default_params(muxer_comp);
-       if (!default_params) {
-               BT_COMP_LOGE("Cannot get default parameters: "
-                       "muxer-comp-addr=%p", muxer_comp);
-               goto error;
-       }
-
-       ret = bt_value_map_extend(default_params, params, &real_params);
-       if (ret) {
-               BT_COMP_LOGE("Cannot extend default parameters map value: "
-                       "muxer-comp-addr=%p, def-params-addr=%p, "
-                       "params-addr=%p", muxer_comp, default_params,
-                       params);
-               goto error;
-       }
-
-       assume_absolute_clock_classes = bt_value_map_borrow_entry_value(real_params,
-                                                                       ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME);
-       if (assume_absolute_clock_classes &&
-                       !bt_value_is_bool(assume_absolute_clock_classes)) {
-               BT_COMP_LOGE("Expecting a boolean value for the `%s` parameter: "
-                       "muxer-comp-addr=%p, value-type=%s",
-                       ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp,
-                       bt_common_value_type_string(
-                               bt_value_get_type(assume_absolute_clock_classes)));
-               goto error;
-       }
-
-       bool_val = bt_value_bool_get(assume_absolute_clock_classes);
-       muxer_comp->assume_absolute_clock_classes = (bool) bool_val;
-       BT_COMP_LOGI("Configured muxer component: muxer-comp-addr=%p, "
-               "assume-absolute-clock-classes=%d",
-               muxer_comp, muxer_comp->assume_absolute_clock_classes);
-       goto end;
-
-error:
-       ret = -1;
-
-end:
-       bt_value_put_ref(default_params);
-       bt_value_put_ref(real_params);
-       return ret;
-}
-
 BT_HIDDEN
-bt_component_class_init_method_status muxer_init(
+bt_component_class_initialize_method_status muxer_init(
                bt_self_component_filter *self_comp_flt,
+               bt_self_component_filter_configuration *config,
                const bt_value *params, void *init_data)
 {
-       int ret;
-       bt_component_class_init_method_status status =
-               BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
+       bt_component_class_initialize_method_status status =
+               BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
        bt_self_component_add_port_status add_port_status;
        bt_self_component *self_comp =
                bt_self_component_filter_as_self_component(self_comp_flt);
@@ -354,13 +274,6 @@ bt_component_class_init_method_status muxer_init(
        muxer_comp->log_level = log_level;
        muxer_comp->self_comp = self_comp;
        muxer_comp->self_comp_flt = self_comp_flt;
-       ret = configure_muxer_comp(muxer_comp, params);
-       if (ret) {
-               BT_COMP_LOGE("Cannot configure muxer component: "
-                       "muxer-comp-addr=%p, params-addr=%p",
-                       muxer_comp, params);
-               goto error;
-       }
 
        bt_self_component_set_data(self_comp, muxer_comp);
        add_port_status = add_available_input_port(self_comp_flt);
@@ -371,9 +284,9 @@ bt_component_class_init_method_status muxer_init(
                        bt_common_func_status_string(add_port_status));
                if (add_port_status ==
                                BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
-                       status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
+                       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                } else {
-                       status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
+                       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
                }
 
                goto error;
@@ -387,9 +300,9 @@ bt_component_class_init_method_status muxer_init(
                        bt_common_func_status_string(add_port_status));
                if (add_port_status ==
                                BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
-                       status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
+                       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                } else {
-                       status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
+                       status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
                }
 
                goto error;
@@ -405,8 +318,8 @@ error:
        destroy_muxer_comp(muxer_comp);
        bt_self_component_set_data(self_comp, NULL);
 
-       if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
-               status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
+       if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
+               status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
        }
 
 end:
@@ -425,15 +338,17 @@ void muxer_finalize(bt_self_component_filter *self_comp)
 }
 
 static
-bt_self_component_port_input_message_iterator *
+bt_self_component_port_input_message_iterator_create_from_message_iterator_status
 create_msg_iter_on_input_port(struct muxer_comp *muxer_comp,
-               bt_self_component_port_input *self_port)
+               struct muxer_msg_iter *muxer_msg_iter,
+               bt_self_component_port_input *self_port,
+               bt_self_component_port_input_message_iterator **msg_iter)
 {
        const bt_port *port = bt_self_component_port_as_port(
                bt_self_component_port_input_as_self_component_port(
                        self_port));
-       bt_self_component_port_input_message_iterator *msg_iter =
-               NULL;
+       bt_self_component_port_input_message_iterator_create_from_message_iterator_status
+               status;
 
        BT_ASSERT(port);
        BT_ASSERT(bt_port_is_connected(port));
@@ -441,9 +356,9 @@ create_msg_iter_on_input_port(struct muxer_comp *muxer_comp,
        // TODO: Advance the iterator to >= the time of the latest
        //       returned message by the muxer message
        //       iterator which creates it.
-       msg_iter = bt_self_component_port_input_message_iterator_create(
-               self_port);
-       if (!msg_iter) {
+       status = bt_self_component_port_input_message_iterator_create_from_message_iterator(
+               muxer_msg_iter->self_msg_iter, self_port, msg_iter);
+       if (status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
                BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
                        "port-addr=%p, port-name=\"%s\"",
                        port, bt_port_get_name(port));
@@ -455,7 +370,7 @@ create_msg_iter_on_input_port(struct muxer_comp *muxer_comp,
                port, bt_port_get_name(port), msg_iter);
 
 end:
-       return msg_iter;
+       return status;
 }
 
 static
@@ -700,11 +615,9 @@ int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter,
        if (muxer_msg_iter->clock_class_expectation ==
                        MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
                /*
-                * This is the first clock class that this muxer
-                * message iterator encounters. Its properties
-                * determine what to expect for the whole lifetime of
-                * the iterator without a true
-                * `assume-absolute-clock-classes` parameter.
+                * This is the first clock class that this muxer message
+                * iterator encounters. Its properties determine what to expect
+                * for the whole lifetime of the iterator.
                 */
                if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
                        /* Expect absolute clock classes */
@@ -730,76 +643,74 @@ int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter,
                }
        }
 
-       if (!muxer_comp->assume_absolute_clock_classes) {
-               switch (muxer_msg_iter->clock_class_expectation) {
-               case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE:
-                       if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
-                               BT_COMP_LOGE("Expecting an absolute clock class, "
-                                       "but got a non-absolute one: "
-                                       "clock-class-addr=%p, clock-class-name=\"%s\"",
-                                       clock_class, cc_name);
-                               goto error;
-                       }
-                       break;
-               case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID:
-                       if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
-                               BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
-                                       "but got an absolute one: "
-                                       "clock-class-addr=%p, clock-class-name=\"%s\"",
-                                       clock_class, cc_name);
-                               goto error;
-                       }
-
-                       if (cc_uuid) {
-                               BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
-                                       "but got one with a UUID: "
-                                       "clock-class-addr=%p, clock-class-name=\"%s\", "
-                                       "uuid=\"" BT_UUID_FMT "\"",
-                                       clock_class, cc_name, BT_UUID_FMT_VALUES(cc_uuid));
-                               goto error;
-                       }
-                       break;
-               case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID:
-                       if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
-                               BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
-                                       "but got an absolute one: "
-                                       "clock-class-addr=%p, clock-class-name=\"%s\"",
-                                       clock_class, cc_name);
-                               goto error;
-                       }
+       switch (muxer_msg_iter->clock_class_expectation) {
+       case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE:
+               if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
+                       BT_COMP_LOGE("Expecting an absolute clock class, "
+                               "but got a non-absolute one: "
+                               "clock-class-addr=%p, clock-class-name=\"%s\"",
+                               clock_class, cc_name);
+                       goto error;
+               }
+               break;
+       case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID:
+               if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
+                       BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
+                               "but got an absolute one: "
+                               "clock-class-addr=%p, clock-class-name=\"%s\"",
+                               clock_class, cc_name);
+                       goto error;
+               }
 
-                       if (!cc_uuid) {
-                               BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
-                                       "but got one with no UUID: "
-                                       "clock-class-addr=%p, clock-class-name=\"%s\"",
-                                       clock_class, cc_name);
-                               goto error;
-                       }
+               if (cc_uuid) {
+                       BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
+                               "but got one with a UUID: "
+                               "clock-class-addr=%p, clock-class-name=\"%s\", "
+                               "uuid=\"" BT_UUID_FMT "\"",
+                               clock_class, cc_name, BT_UUID_FMT_VALUES(cc_uuid));
+                       goto error;
+               }
+               break;
+       case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID:
+               if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
+                       BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
+                               "but got an absolute one: "
+                               "clock-class-addr=%p, clock-class-name=\"%s\"",
+                               clock_class, cc_name);
+                       goto error;
+               }
 
-                       if (bt_uuid_compare(muxer_msg_iter->expected_clock_class_uuid, cc_uuid) != 0) {
-                               BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
-                                       "but got one with different UUID: "
-                                       "clock-class-addr=%p, clock-class-name=\"%s\", "
-                                       "expected-uuid=\"" BT_UUID_FMT "\", "
-                                       "uuid=\"" BT_UUID_FMT "\"",
-                                       clock_class, cc_name,
-                                       BT_UUID_FMT_VALUES(muxer_msg_iter->expected_clock_class_uuid),
-                                       BT_UUID_FMT_VALUES(cc_uuid));
-                               goto error;
-                       }
-                       break;
-               case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE:
-                       BT_COMP_LOGE("Expecting no clock class, but got one: "
+               if (!cc_uuid) {
+                       BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
+                               "but got one with no UUID: "
                                "clock-class-addr=%p, clock-class-name=\"%s\"",
                                clock_class, cc_name);
                        goto error;
-               default:
-                       /* Unexpected */
-                       BT_COMP_LOGF("Unexpected clock class expectation: "
-                               "expectation-code=%d",
-                               muxer_msg_iter->clock_class_expectation);
-                       abort();
                }
+
+               if (bt_uuid_compare(muxer_msg_iter->expected_clock_class_uuid, cc_uuid) != 0) {
+                       BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
+                               "but got one with different UUID: "
+                               "clock-class-addr=%p, clock-class-name=\"%s\", "
+                               "expected-uuid=\"" BT_UUID_FMT "\", "
+                               "uuid=\"" BT_UUID_FMT "\"",
+                               clock_class, cc_name,
+                               BT_UUID_FMT_VALUES(muxer_msg_iter->expected_clock_class_uuid),
+                               BT_UUID_FMT_VALUES(cc_uuid));
+                       goto error;
+               }
+               break;
+       case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE:
+               BT_COMP_LOGE("Expecting no clock class, but got one: "
+                       "clock-class-addr=%p, clock-class-name=\"%s\"",
+                       clock_class, cc_name);
+               goto error;
+       default:
+               /* Unexpected */
+               BT_COMP_LOGF("Unexpected clock class expectation: "
+                       "expectation-code=%d",
+                       muxer_msg_iter->clock_class_expectation);
+               abort();
        }
 
        goto end;
@@ -827,8 +738,9 @@ int validate_new_stream_clock_class(struct muxer_msg_iter *muxer_msg_iter,
                        /* Expect no clock class */
                        muxer_msg_iter->clock_class_expectation =
                                MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE;
-               } else {
-                       BT_COMP_LOGE("Expecting stream class with a default clock class: "
+               } else if (muxer_msg_iter->clock_class_expectation !=
+                               MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE) {
+                       BT_COMP_LOGE("Expecting stream class without a default clock class: "
                                "stream-class-addr=%p, stream-class-name=\"%s\", "
                                "stream-class-id=%" PRIu64,
                                stream_class, bt_stream_class_get_name(stream_class),
@@ -939,11 +851,50 @@ muxer_msg_iter_youngest_upstream_msg_iter(
                        goto end;
                }
 
-               if (msg_ts_ns <= youngest_ts_ns) {
+               /*
+                * Update the current message iterator if it has not been set
+                * yet, or if its current message has a timestamp smaller than
+                * the previously selected youngest message.
+                */
+               if (G_UNLIKELY(*muxer_upstream_msg_iter == NULL) ||
+                               msg_ts_ns < youngest_ts_ns) {
                        *muxer_upstream_msg_iter =
                                cur_muxer_upstream_msg_iter;
                        youngest_ts_ns = msg_ts_ns;
                        *ts_ns = youngest_ts_ns;
+               } else if (msg_ts_ns == youngest_ts_ns) {
+                       /*
+                        * The currently selected message to be sent downstream
+                        * next has the exact same timestamp that of the
+                        * current candidate message. We must break the tie
+                        * in a predictable manner.
+                        */
+                       const bt_message *selected_msg = g_queue_peek_head(
+                               (*muxer_upstream_msg_iter)->msgs);
+                       BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
+
+                       /*
+                        * Order the messages in an arbitrary but determinitic
+                        * way.
+                        */
+                       ret = common_muxing_compare_messages(msg, selected_msg);
+                       if (ret < 0) {
+                               /*
+                                * The `msg` should go first. Update the next
+                                * iterator and the current timestamp.
+                                */
+                               *muxer_upstream_msg_iter =
+                                       cur_muxer_upstream_msg_iter;
+                               youngest_ts_ns = msg_ts_ns;
+                               *ts_ns = youngest_ts_ns;
+                       } else if (ret == 0) {
+                               /* Unable to pick which one should go first. */
+                               BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
+                                       "muxer-upstream-msg-iter-wrap-addr=%p"
+                                       "cur-muxer-upstream-msg-iter-wrap-addr=%p",
+                                       *muxer_upstream_msg_iter,
+                                       cur_muxer_upstream_msg_iter);
+                       }
                }
        }
 
@@ -1195,12 +1146,15 @@ void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter)
 }
 
 static
-int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
-               struct muxer_msg_iter *muxer_msg_iter)
+bt_component_class_message_iterator_initialize_method_status
+muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
+               struct muxer_msg_iter *muxer_msg_iter,
+               struct bt_self_message_iterator_configuration *config)
 {
        int64_t count;
        int64_t i;
-       int ret = 0;
+       bt_component_class_message_iterator_initialize_method_status status;
+       bool can_seek_forward = true;
 
        count = bt_component_filter_get_input_port_count(
                bt_self_component_filter_as_component_filter(
@@ -1209,6 +1163,7 @@ int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
                BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
                        "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
                        muxer_comp, muxer_msg_iter);
+               status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK;
                goto end;
        }
 
@@ -1218,6 +1173,9 @@ int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
                        bt_self_component_filter_borrow_input_port_by_index(
                                muxer_comp->self_comp_flt, i);
                const bt_port *port;
+               bt_self_component_port_input_message_iterator_create_from_message_iterator_status
+                       msg_iter_status;
+               int int_status;
 
                BT_ASSERT(self_port);
                port = bt_self_component_port_as_port(
@@ -1230,40 +1188,52 @@ int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
                        continue;
                }
 
-               upstream_msg_iter = create_msg_iter_on_input_port(muxer_comp,
-                       self_port);
-               if (!upstream_msg_iter) {
+               msg_iter_status = create_msg_iter_on_input_port(muxer_comp,
+                       muxer_msg_iter, self_port, &upstream_msg_iter);
+               if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
                        /* create_msg_iter_on_input_port() logs errors */
-                       BT_ASSERT(!upstream_msg_iter);
-                       ret = -1;
+                       status = (int) msg_iter_status;
                        goto end;
                }
 
-               ret = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter,
+               int_status = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter,
                        upstream_msg_iter);
                bt_self_component_port_input_message_iterator_put_ref(
                        upstream_msg_iter);
-               if (ret) {
+               if (int_status) {
+                       status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR;
                        /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
                        goto end;
                }
+
+               can_seek_forward = can_seek_forward &&
+                       bt_self_component_port_input_message_iterator_can_seek_forward(
+                               upstream_msg_iter);
        }
 
+       /*
+        * This iterator can seek forward if all of its iterators can seek
+        * forward.
+        */
+       bt_self_message_iterator_configuration_set_can_seek_forward(
+               config, can_seek_forward);
+
+       status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK;
+
 end:
-       return ret;
+       return status;
 }
 
 BT_HIDDEN
-bt_component_class_message_iterator_init_method_status muxer_msg_iter_init(
+bt_component_class_message_iterator_initialize_method_status muxer_msg_iter_init(
                bt_self_message_iterator *self_msg_iter,
+               bt_self_message_iterator_configuration *config,
                bt_self_component_filter *self_comp,
                bt_self_component_port_output *port)
 {
        struct muxer_comp *muxer_comp = NULL;
        struct muxer_msg_iter *muxer_msg_iter = NULL;
-       bt_component_class_message_iterator_init_method_status status =
-               BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
-       int ret;
+       bt_component_class_message_iterator_initialize_method_status status;
 
        muxer_comp = bt_self_component_get_data(
                bt_self_component_filter_as_self_component(self_comp));
@@ -1281,6 +1251,7 @@ bt_component_class_message_iterator_init_method_status muxer_msg_iter_init(
                BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
                        "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
                        self_comp, muxer_comp, self_msg_iter);
+               status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR;
                goto error;
        }
 
@@ -1288,16 +1259,19 @@ bt_component_class_message_iterator_init_method_status muxer_msg_iter_init(
        muxer_msg_iter = g_new0(struct muxer_msg_iter, 1);
        if (!muxer_msg_iter) {
                BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
+               status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto error;
        }
 
        muxer_msg_iter->muxer_comp = muxer_comp;
+       muxer_msg_iter->self_msg_iter = self_msg_iter;
        muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
        muxer_msg_iter->active_muxer_upstream_msg_iters =
                g_ptr_array_new_with_free_func(
                        (GDestroyNotify) destroy_muxer_upstream_msg_iter);
        if (!muxer_msg_iter->active_muxer_upstream_msg_iters) {
                BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
+               status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto error;
        }
 
@@ -1306,17 +1280,18 @@ bt_component_class_message_iterator_init_method_status muxer_msg_iter_init(
                        (GDestroyNotify) destroy_muxer_upstream_msg_iter);
        if (!muxer_msg_iter->ended_muxer_upstream_msg_iters) {
                BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
+               status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
                goto error;
        }
 
-       ret = muxer_msg_iter_init_upstream_iterators(muxer_comp,
-               muxer_msg_iter);
-       if (ret) {
+       status = muxer_msg_iter_init_upstream_iterators(muxer_comp,
+               muxer_msg_iter, config);
+       if (status) {
                BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
                        "comp-addr=%p, muxer-comp-addr=%p, "
                        "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
                        self_comp, muxer_comp, muxer_msg_iter,
-                       self_msg_iter, ret);
+                       self_msg_iter, status);
                goto error;
        }
 
@@ -1330,7 +1305,6 @@ bt_component_class_message_iterator_init_method_status muxer_msg_iter_init(
 error:
        destroy_muxer_msg_iter(muxer_msg_iter);
        bt_self_message_iterator_set_data(self_msg_iter, NULL);
-       status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
 
 end:
        muxer_comp->initializing_muxer_msg_iter = false;
@@ -1432,49 +1406,58 @@ end:
 }
 
 static inline
-bt_bool muxer_upstream_msg_iters_can_all_seek_beginning(
-               GPtrArray *muxer_upstream_msg_iters)
+bt_component_class_message_iterator_can_seek_beginning_method_status
+muxer_upstream_msg_iters_can_all_seek_beginning(
+               GPtrArray *muxer_upstream_msg_iters, bt_bool *can_seek)
 {
+       bt_component_class_message_iterator_can_seek_beginning_method_status status =
+               BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
        uint64_t i;
-       bt_bool ret = BT_TRUE;
 
        for (i = 0; i < muxer_upstream_msg_iters->len; i++) {
                struct muxer_upstream_msg_iter *upstream_msg_iter =
                        muxer_upstream_msg_iters->pdata[i];
+               status = (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
+                       upstream_msg_iter->msg_iter, can_seek);
+               if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK) {
+                       goto end;
+               }
 
-               if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
-                               upstream_msg_iter->msg_iter)) {
-                       ret = BT_FALSE;
+               if (!*can_seek) {
                        goto end;
                }
        }
 
+       *can_seek = BT_TRUE;
+
 end:
-       return ret;
+       return status;
 }
 
 BT_HIDDEN
-bt_bool muxer_msg_iter_can_seek_beginning(
-               bt_self_message_iterator *self_msg_iter)
+bt_component_class_message_iterator_can_seek_beginning_method_status
+muxer_msg_iter_can_seek_beginning(
+               bt_self_message_iterator *self_msg_iter, bt_bool *can_seek)
 {
        struct muxer_msg_iter *muxer_msg_iter =
                bt_self_message_iterator_get_data(self_msg_iter);
-       bt_bool ret = BT_TRUE;
+       bt_component_class_message_iterator_can_seek_beginning_method_status status;
 
-       if (!muxer_upstream_msg_iters_can_all_seek_beginning(
-                       muxer_msg_iter->active_muxer_upstream_msg_iters)) {
-               ret = BT_FALSE;
+       status = muxer_upstream_msg_iters_can_all_seek_beginning(
+               muxer_msg_iter->active_muxer_upstream_msg_iters, can_seek);
+       if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK) {
                goto end;
        }
 
-       if (!muxer_upstream_msg_iters_can_all_seek_beginning(
-                       muxer_msg_iter->ended_muxer_upstream_msg_iters)) {
-               ret = BT_FALSE;
+       if (!*can_seek) {
                goto end;
        }
 
+       status = muxer_upstream_msg_iters_can_all_seek_beginning(
+               muxer_msg_iter->ended_muxer_upstream_msg_iters, can_seek);
+
 end:
-       return ret;
+       return status;
 }
 
 BT_HIDDEN
@@ -1531,8 +1514,14 @@ bt_component_class_message_iterator_seek_beginning_method_status muxer_msg_iter_
                muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL;
        }
 
-       g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters,
-               0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len);
+       /*
+        * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
+        * called on an empty array.
+        */
+       if (muxer_msg_iter->ended_muxer_upstream_msg_iters->len > 0) {
+               g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters,
+                       0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len);
+       }
        muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
        muxer_msg_iter->clock_class_expectation =
                MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY;
This page took 0.031913 seconds and 4 git commands to generate.