lib: remove self component param. from msg. iterator init. method
[babeltrace.git] / src / plugins / utils / muxer / muxer.c
CommitLineData
958f7d11
PP
1/*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
d9693c64 23#define BT_COMP_LOG_SELF_COMP (muxer_comp->self_comp)
f5abbab4 24#define BT_LOG_OUTPUT_LEVEL (muxer_comp->log_level)
b03487ab 25#define BT_LOG_TAG "PLUGIN/FLT.UTILS.MUXER"
3fa1b6a3 26#include "logging/comp-logging.h"
318fe670 27
85e7137b 28#include "common/macros.h"
d126826c 29#include "common/uuid.h"
71c5da58 30#include <babeltrace2/babeltrace.h>
958f7d11 31#include <glib.h>
c55a9f58 32#include <stdbool.h>
318fe670 33#include <inttypes.h>
57952005
MJ
34#include "common/assert.h"
35#include "common/common.h"
0fbb9a9f 36#include <stdlib.h>
282c8cd0 37#include <string.h>
958f7d11 38
9240924b 39#include "plugins/common/muxing/muxing.h"
ec9a9794 40#include "plugins/common/param-validation/param-validation.h"
9240924b 41
3e3ea13e
FD
42#include "muxer.h"
43
958f7d11 44struct muxer_comp {
d9693c64
PP
45 /* Weak refs */
46 bt_self_component_filter *self_comp_flt;
47 bt_self_component *self_comp;
834e9996 48
958f7d11
PP
49 unsigned int next_port_num;
50 size_t available_input_ports;
b09a5592 51 bool initializing_muxer_msg_iter;
f5abbab4 52 bt_logging_level log_level;
958f7d11
PP
53};
54
b09a5592 55struct muxer_upstream_msg_iter {
f5abbab4
PP
56 struct muxer_comp *muxer_comp;
57
ab11110e 58 /* Owned by this, NULL if ended */
fbd8a4e0 59 bt_message_iterator *msg_iter;
958f7d11 60
b09a5592
PP
61 /* Contains `const bt_message *`, owned by this */
62 GQueue *msgs;
958f7d11
PP
63};
64
b09a5592
PP
65enum muxer_msg_iter_clock_class_expectation {
66 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0,
37911c11 67 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE,
b09a5592
PP
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,
282c8cd0
PP
71};
72
b09a5592 73struct muxer_msg_iter {
f5abbab4
PP
74 struct muxer_comp *muxer_comp;
75
692f1a01
PP
76 /* Weak */
77 bt_self_message_iterator *self_msg_iter;
78
ab11110e 79 /*
b09a5592 80 * Array of struct muxer_upstream_msg_iter * (owned by this).
ab11110e
PP
81 *
82 * NOTE: This array is searched in linearly to find the youngest
b09a5592 83 * current message. Keep this until benchmarks confirm that
ab11110e
PP
84 * another data structure is faster than this for our typical
85 * use cases.
86 */
a71ed05c
PP
87 GPtrArray *active_muxer_upstream_msg_iters;
88
89 /*
90 * Array of struct muxer_upstream_msg_iter * (owned by this).
91 *
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.
95 */
96 GPtrArray *ended_muxer_upstream_msg_iters;
958f7d11 97
b09a5592 98 /* Last time returned in a message */
958f7d11 99 int64_t last_returned_ts_ns;
282c8cd0
PP
100
101 /* Clock class expectation state */
b09a5592 102 enum muxer_msg_iter_clock_class_expectation clock_class_expectation;
282c8cd0
PP
103
104 /*
105 * Expected clock class UUID, only valid when
106 * clock_class_expectation is
b09a5592 107 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
282c8cd0 108 */
d126826c 109 bt_uuid_t expected_clock_class_uuid;
44381fae
SM
110
111 /*
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.
115 */
68e2deed 116 bt_message_iterator_class_next_method_status next_saved_status;
44381fae 117 const struct bt_error *next_saved_error;
958f7d11
PP
118};
119
a71ed05c
PP
120static
121void empty_message_queue(struct muxer_upstream_msg_iter *upstream_msg_iter)
122{
123 const bt_message *msg;
124
125 while ((msg = g_queue_pop_head(upstream_msg_iter->msgs))) {
126 bt_message_put_ref(msg);
127 }
128}
129
ab11110e 130static
b09a5592
PP
131void destroy_muxer_upstream_msg_iter(
132 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter)
ab11110e 133{
f5abbab4
PP
134 struct muxer_comp *muxer_comp;
135
b09a5592 136 if (!muxer_upstream_msg_iter) {
ab11110e
PP
137 return;
138 }
139
f5abbab4 140 muxer_comp = muxer_upstream_msg_iter->muxer_comp;
d9693c64 141 BT_COMP_LOGD("Destroying muxer's upstream message iterator wrapper: "
b09a5592
PP
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);
fbd8a4e0 146 bt_message_iterator_put_ref(
a71ed05c 147 muxer_upstream_msg_iter->msg_iter);
3fd7b79d 148
b09a5592 149 if (muxer_upstream_msg_iter->msgs) {
a71ed05c 150 empty_message_queue(muxer_upstream_msg_iter);
b09a5592 151 g_queue_free(muxer_upstream_msg_iter->msgs);
3fd7b79d
PP
152 }
153
b09a5592 154 g_free(muxer_upstream_msg_iter);
ab11110e
PP
155}
156
958f7d11 157static
ae644e59 158int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter *muxer_msg_iter,
fbd8a4e0 159 bt_message_iterator *self_msg_iter)
958f7d11 160{
ae644e59 161 int ret = 0;
b09a5592
PP
162 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
163 g_new0(struct muxer_upstream_msg_iter, 1);
f5abbab4 164 struct muxer_comp *muxer_comp = muxer_msg_iter->muxer_comp;
958f7d11 165
b09a5592 166 if (!muxer_upstream_msg_iter) {
d9693c64 167 BT_COMP_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
11603bce 168 goto error;
958f7d11
PP
169 }
170
f5abbab4 171 muxer_upstream_msg_iter->muxer_comp = muxer_comp;
b09a5592 172 muxer_upstream_msg_iter->msg_iter = self_msg_iter;
fbd8a4e0 173 bt_message_iterator_get_ref(muxer_upstream_msg_iter->msg_iter);
b09a5592
PP
174 muxer_upstream_msg_iter->msgs = g_queue_new();
175 if (!muxer_upstream_msg_iter->msgs) {
d9693c64 176 BT_COMP_LOGE_STR("Failed to allocate a GQueue.");
11603bce 177 goto error;
3fd7b79d
PP
178 }
179
a71ed05c 180 g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
b09a5592 181 muxer_upstream_msg_iter);
d9693c64 182 BT_COMP_LOGD("Added muxer's upstream message iterator wrapper: "
b09a5592
PP
183 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
184 muxer_upstream_msg_iter, muxer_msg_iter,
185 self_msg_iter);
958f7d11 186
11603bce
FD
187 goto end;
188
189error:
190 g_free(muxer_upstream_msg_iter);
ae644e59 191 ret = -1;
11603bce 192
958f7d11 193end:
ae644e59 194 return ret;
958f7d11
PP
195}
196
958f7d11 197static
fb25b9e3 198bt_self_component_add_port_status add_available_input_port(
8eee8ea2 199 bt_self_component_filter *self_comp)
958f7d11 200{
834e9996 201 struct muxer_comp *muxer_comp = bt_self_component_get_data(
bb61965b 202 bt_self_component_filter_as_self_component(self_comp));
fb25b9e3
PP
203 bt_self_component_add_port_status status =
204 BT_SELF_COMPONENT_ADD_PORT_STATUS_OK;
958f7d11 205 GString *port_name = NULL;
958f7d11 206
8b45963b 207 BT_ASSERT(muxer_comp);
958f7d11
PP
208 port_name = g_string_new("in");
209 if (!port_name) {
d9693c64 210 BT_COMP_LOGE_STR("Failed to allocate a GString.");
fb25b9e3 211 status = BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR;
958f7d11
PP
212 goto end;
213 }
214
215 g_string_append_printf(port_name, "%u", muxer_comp->next_port_num);
834e9996
PP
216 status = bt_self_component_filter_add_input_port(
217 self_comp, port_name->str, NULL, NULL);
fb25b9e3 218 if (status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
d9693c64 219 BT_COMP_LOGE("Cannot add input port to muxer component: "
318fe670 220 "port-name=\"%s\", comp-addr=%p, status=%s",
834e9996 221 port_name->str, self_comp,
fb25b9e3 222 bt_common_func_status_string(status));
958f7d11
PP
223 goto end;
224 }
225
226 muxer_comp->available_input_ports++;
227 muxer_comp->next_port_num++;
d9693c64 228 BT_COMP_LOGI("Added one input port to muxer component: "
318fe670 229 "port-name=\"%s\", comp-addr=%p",
834e9996 230 port_name->str, self_comp);
1043fdea 231
958f7d11
PP
232end:
233 if (port_name) {
234 g_string_free(port_name, TRUE);
235 }
236
147337a3 237 return status;
958f7d11
PP
238}
239
958f7d11 240static
fb25b9e3 241bt_self_component_add_port_status create_output_port(
8eee8ea2 242 bt_self_component_filter *self_comp)
958f7d11 243{
834e9996
PP
244 return bt_self_component_filter_add_output_port(
245 self_comp, "out", NULL, NULL);
958f7d11
PP
246}
247
248static
249void destroy_muxer_comp(struct muxer_comp *muxer_comp)
250{
251 if (!muxer_comp) {
252 return;
253 }
254
958f7d11
PP
255 g_free(muxer_comp);
256}
257
5ca9cc5a 258static
ec9a9794
SM
259struct bt_param_validation_map_value_entry_descr muxer_params[] = {
260 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
261};
262
958f7d11 263BT_HIDDEN
4175c1d5 264bt_component_class_initialize_method_status muxer_init(
d9693c64 265 bt_self_component_filter *self_comp_flt,
e3250e61 266 bt_self_component_filter_configuration *config,
3e3ea13e 267 const bt_value *params, void *init_data)
958f7d11 268{
ec9a9794 269 bt_component_class_initialize_method_status status;
fb25b9e3 270 bt_self_component_add_port_status add_port_status;
d9693c64
PP
271 bt_self_component *self_comp =
272 bt_self_component_filter_as_self_component(self_comp_flt);
958f7d11 273 struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1);
f5abbab4 274 bt_logging_level log_level = bt_component_get_logging_level(
d9693c64 275 bt_self_component_as_component(self_comp));
ec9a9794
SM
276 enum bt_param_validation_status validation_status;
277 gchar *validate_error = NULL;
958f7d11 278
d9693c64 279 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO, log_level, self_comp,
f5abbab4 280 "Initializing muxer component: "
834e9996 281 "comp-addr=%p, params-addr=%p", self_comp, params);
318fe670 282
958f7d11 283 if (!muxer_comp) {
d9693c64 284 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
f5abbab4 285 "Failed to allocate one muxer component.");
ec9a9794 286 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
958f7d11
PP
287 goto error;
288 }
289
f5abbab4 290 muxer_comp->log_level = log_level;
d9693c64
PP
291 muxer_comp->self_comp = self_comp;
292 muxer_comp->self_comp_flt = self_comp_flt;
65ee897d 293
ec9a9794
SM
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;
298 goto 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);
302 goto error;
303 }
304
d9693c64 305 bt_self_component_set_data(self_comp, muxer_comp);
fb25b9e3
PP
306 add_port_status = add_available_input_port(self_comp_flt);
307 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
d9693c64 308 BT_COMP_LOGE("Cannot ensure that at least one muxer component's input port is available: "
318fe670
PP
309 "muxer-comp-addr=%p, status=%s",
310 muxer_comp,
fb25b9e3 311 bt_common_func_status_string(add_port_status));
ec9a9794 312 status = (int) add_port_status;
958f7d11
PP
313 goto error;
314 }
315
fb25b9e3
PP
316 add_port_status = create_output_port(self_comp_flt);
317 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
d9693c64 318 BT_COMP_LOGE("Cannot create muxer component's output port: "
318fe670
PP
319 "muxer-comp-addr=%p, status=%s",
320 muxer_comp,
fb25b9e3 321 bt_common_func_status_string(add_port_status));
ec9a9794 322 status = (int) add_port_status;
958f7d11
PP
323 goto error;
324 }
325
d9693c64 326 BT_COMP_LOGI("Initialized muxer component: "
318fe670 327 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
834e9996 328 self_comp, params, muxer_comp);
318fe670 329
ec9a9794 330 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
958f7d11
PP
331 goto end;
332
333error:
334 destroy_muxer_comp(muxer_comp);
d9693c64 335 bt_self_component_set_data(self_comp, NULL);
147337a3 336
958f7d11 337end:
ec9a9794 338 g_free(validate_error);
958f7d11
PP
339 return status;
340}
341
342BT_HIDDEN
8eee8ea2 343void muxer_finalize(bt_self_component_filter *self_comp)
958f7d11 344{
834e9996 345 struct muxer_comp *muxer_comp = bt_self_component_get_data(
bb61965b 346 bt_self_component_filter_as_self_component(self_comp));
958f7d11 347
d9693c64 348 BT_COMP_LOGI("Finalizing muxer component: comp-addr=%p",
834e9996 349 self_comp);
958f7d11
PP
350 destroy_muxer_comp(muxer_comp);
351}
352
353static
fbd8a4e0 354bt_message_iterator_create_from_message_iterator_status
f5abbab4 355create_msg_iter_on_input_port(struct muxer_comp *muxer_comp,
692f1a01 356 struct muxer_msg_iter *muxer_msg_iter,
ab8b2b1b 357 bt_self_component_port_input *self_port,
fbd8a4e0 358 bt_message_iterator **msg_iter)
958f7d11 359{
8eee8ea2 360 const bt_port *port = bt_self_component_port_as_port(
bb61965b 361 bt_self_component_port_input_as_self_component_port(
834e9996 362 self_port));
fbd8a4e0 363 bt_message_iterator_create_from_message_iterator_status
ab8b2b1b 364 status;
958f7d11 365
8b45963b
PP
366 BT_ASSERT(port);
367 BT_ASSERT(bt_port_is_connected(port));
958f7d11 368
ab11110e 369 // TODO: Advance the iterator to >= the time of the latest
b09a5592 370 // returned message by the muxer message
ab11110e 371 // iterator which creates it.
fbd8a4e0 372 status = bt_message_iterator_create_from_message_iterator(
ab8b2b1b 373 muxer_msg_iter->self_msg_iter, self_port, msg_iter);
fbd8a4e0 374 if (status != BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
d9693c64 375 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
834e9996
PP
376 "port-addr=%p, port-name=\"%s\"",
377 port, bt_port_get_name(port));
958f7d11
PP
378 goto end;
379 }
380
d9693c64 381 BT_COMP_LOGI("Created upstream message iterator on input port: "
b09a5592
PP
382 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
383 port, bt_port_get_name(port), msg_iter);
318fe670 384
958f7d11 385end:
ab8b2b1b 386 return status;
958f7d11
PP
387}
388
ab11110e 389static
68e2deed 390bt_message_iterator_class_next_method_status muxer_upstream_msg_iter_next(
a71ed05c
PP
391 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
392 bool *is_ended)
ab11110e 393{
f331468c 394 struct muxer_comp *muxer_comp = muxer_upstream_msg_iter->muxer_comp;
68e2deed 395 bt_message_iterator_class_next_method_status status;
fb25b9e3 396 bt_message_iterator_next_status input_port_iter_status;
b09a5592 397 bt_message_array_const msgs;
3fd7b79d
PP
398 uint64_t i;
399 uint64_t count;
ab11110e 400
d9693c64 401 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
b09a5592
PP
402 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
403 muxer_upstream_msg_iter,
404 muxer_upstream_msg_iter->msg_iter);
fbd8a4e0 405 input_port_iter_status = bt_message_iterator_next(
b09a5592 406 muxer_upstream_msg_iter->msg_iter, &msgs, &count);
d9693c64 407 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
fb25b9e3
PP
408 "status=%s",
409 bt_common_func_status_string(input_port_iter_status));
ab11110e 410
3e3ea13e 411 switch (input_port_iter_status) {
fb25b9e3 412 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
089717de 413 /*
b09a5592 414 * Message iterator's current message is
3fd7b79d 415 * valid: it must be considered for muxing operations.
089717de 416 */
d9693c64 417 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
ec4a3354 418 BT_ASSERT_DBG(count > 0);
3fd7b79d 419
b09a5592 420 /* Move messages to our queue */
3fd7b79d
PP
421 for (i = 0; i < count; i++) {
422 /*
423 * Push to tail in order; other side
b09a5592 424 * (muxer_msg_iter_do_next_one()) consumes
3fd7b79d
PP
425 * from the head first.
426 */
b09a5592
PP
427 g_queue_push_tail(muxer_upstream_msg_iter->msgs,
428 (void *) msgs[i]);
3fd7b79d 429 }
68e2deed 430 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
ab11110e 431 break;
fb25b9e3 432 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
089717de 433 /*
b09a5592 434 * Message iterator's current message is not
089717de 435 * valid anymore. Return
fb25b9e3 436 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
089717de 437 */
68e2deed 438 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN;
ab11110e 439 break;
fb25b9e3 440 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: /* Fall-through. */
ab11110e 441 /*
b09a5592 442 * Message iterator reached the end: release it. It
ab11110e 443 * won't be considered again to find the youngest
b09a5592 444 * message.
ab11110e 445 */
a71ed05c 446 *is_ended = true;
68e2deed 447 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
089717de 448 break;
f331468c
SM
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;
456 break;
ab11110e 457 default:
f331468c
SM
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));
68e2deed 462 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
089717de 463 break;
ab11110e
PP
464 }
465
089717de 466 return status;
ab11110e
PP
467}
468
958f7d11 469static
b09a5592
PP
470int 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,
958f7d11
PP
473 int64_t *ts_ns)
474{
ecbb78c0 475 const bt_clock_snapshot *clock_snapshot = NULL;
958f7d11 476 int ret = 0;
34e13c22
PP
477 const bt_stream_class *stream_class = NULL;
478 bt_message_type msg_type;
958f7d11 479
ec4a3354
PP
480 BT_ASSERT_DBG(msg);
481 BT_ASSERT_DBG(ts_ns);
d9693c64 482 BT_COMP_LOGD("Getting message's timestamp: "
b09a5592 483 "muxer-msg-iter-addr=%p, msg-addr=%p, "
318fe670 484 "last-returned-ts=%" PRId64,
b09a5592 485 muxer_msg_iter, msg, last_returned_ts_ns);
318fe670 486
85e7137b 487 if (G_UNLIKELY(muxer_msg_iter->clock_class_expectation ==
37911c11
PP
488 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE)) {
489 *ts_ns = last_returned_ts_ns;
490 goto end;
491 }
492
34e13c22
PP
493 msg_type = bt_message_get_type(msg);
494
85e7137b 495 if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_BEGINNING)) {
34e13c22
PP
496 stream_class = bt_stream_borrow_class_const(
497 bt_packet_borrow_stream_const(
498 bt_message_packet_beginning_borrow_packet_const(
499 msg)));
85e7137b 500 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_END)) {
34e13c22
PP
501 stream_class = bt_stream_borrow_class_const(
502 bt_packet_borrow_stream_const(
503 bt_message_packet_end_borrow_packet_const(
504 msg)));
85e7137b 505 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS)) {
77037b2b
PP
506 stream_class = bt_stream_borrow_class_const(
507 bt_message_discarded_events_borrow_stream_const(msg));
85e7137b 508 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS)) {
77037b2b
PP
509 stream_class = bt_stream_borrow_class_const(
510 bt_message_discarded_packets_borrow_stream_const(msg));
34e13c22
PP
511 }
512
513 switch (msg_type) {
b09a5592 514 case BT_MESSAGE_TYPE_EVENT:
ec4a3354 515 BT_ASSERT_DBG(bt_message_event_borrow_stream_class_default_clock_class_const(
37911c11 516 msg));
11ddb3ef
PP
517 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
518 msg);
958f7d11 519 break;
4a4dd64f 520 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
5ef34326 521 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
34e13c22
PP
522 stream_class)) {
523 clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
524 msg);
525 } else {
526 goto no_clock_snapshot;
527 }
528
4a4dd64f
PP
529 break;
530 case BT_MESSAGE_TYPE_PACKET_END:
5ef34326 531 if (bt_stream_class_packets_have_end_default_clock_snapshot(
34e13c22
PP
532 stream_class)) {
533 clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
534 msg);
535 } else {
536 goto no_clock_snapshot;
537 }
538
4a4dd64f 539 break;
3ce73327
FD
540 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
541 {
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;
547 }
548
549 break;
550 }
551 case BT_MESSAGE_TYPE_STREAM_END:
552 {
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;
558 }
559
560 break;
561 }
4a4dd64f 562 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
77037b2b
PP
563 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
564 stream_class)) {
5ef34326 565 clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
77037b2b
PP
566 msg);
567 } else {
568 goto no_clock_snapshot;
569 }
570
4a4dd64f
PP
571 break;
572 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
77037b2b
PP
573 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
574 stream_class)) {
5ef34326 575 clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
77037b2b
PP
576 msg);
577 } else {
578 goto no_clock_snapshot;
579 }
580
4a4dd64f 581 break;
40bf6fd0 582 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
11ddb3ef
PP
583 clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
584 msg);
958f7d11
PP
585 break;
586 default:
b09a5592 587 /* All the other messages have a higher priority */
d9693c64 588 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
958f7d11
PP
589 *ts_ns = last_returned_ts_ns;
590 goto end;
591 }
592
37911c11
PP
593 ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns);
594 if (ret) {
d9693c64 595 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
37911c11
PP
596 "clock-snapshot-addr=%p", clock_snapshot);
597 goto error;
598 }
599
600 goto end;
601
602no_clock_snapshot:
d9693c64 603 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
37911c11
PP
604 "using the last returned timestamp.");
605 *ts_ns = last_returned_ts_ns;
606 goto end;
607
608error:
609 ret = -1;
610
611end:
612 if (ret == 0) {
d9693c64 613 BT_COMP_LOGD("Found message's timestamp: "
37911c11
PP
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,
617 *ts_ns);
7b33a0e0
PP
618 }
619
37911c11
PP
620 return ret;
621}
622
623static inline
624int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter,
625 struct muxer_comp *muxer_comp,
626 const bt_clock_class *clock_class)
627{
628 int ret = 0;
d126826c 629 const uint8_t *cc_uuid;
37911c11
PP
630 const char *cc_name;
631
ec4a3354 632 BT_ASSERT_DBG(clock_class);
839d52a5
PP
633 cc_uuid = bt_clock_class_get_uuid(clock_class);
634 cc_name = bt_clock_class_get_name(clock_class);
282c8cd0 635
b09a5592
PP
636 if (muxer_msg_iter->clock_class_expectation ==
637 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
282c8cd0 638 /*
9635b8a6
FD
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.
282c8cd0 642 */
d608d675 643 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
282c8cd0 644 /* Expect absolute clock classes */
b09a5592
PP
645 muxer_msg_iter->clock_class_expectation =
646 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE;
282c8cd0
PP
647 } else {
648 if (cc_uuid) {
649 /*
650 * Expect non-absolute clock classes
651 * with a specific UUID.
652 */
b09a5592
PP
653 muxer_msg_iter->clock_class_expectation =
654 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID;
d126826c 655 bt_uuid_copy(muxer_msg_iter->expected_clock_class_uuid, cc_uuid);
282c8cd0
PP
656 } else {
657 /*
658 * Expect non-absolute clock classes
659 * with no UUID.
660 */
b09a5592
PP
661 muxer_msg_iter->clock_class_expectation =
662 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID;
282c8cd0
PP
663 }
664 }
665 }
666
9635b8a6
FD
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);
674 goto error;
675 }
676 break;
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);
683 goto error;
684 }
282c8cd0 685
9635b8a6
FD
686 if (cc_uuid) {
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));
692 goto error;
693 }
694 break;
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);
701 goto error;
702 }
282c8cd0 703
9635b8a6
FD
704 if (!cc_uuid) {
705 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
706 "but got one with no UUID: "
37911c11
PP
707 "clock-class-addr=%p, clock-class-name=\"%s\"",
708 clock_class, cc_name);
709 goto error;
282c8cd0 710 }
9635b8a6
FD
711
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));
721 goto error;
722 }
723 break;
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);
728 goto error;
729 default:
730 /* Unexpected */
731 BT_COMP_LOGF("Unexpected clock class expectation: "
732 "expectation-code=%d",
733 muxer_msg_iter->clock_class_expectation);
24847fc7 734 bt_common_abort();
958f7d11
PP
735 }
736
2d42927b
PP
737 goto end;
738
958f7d11
PP
739error:
740 ret = -1;
741
742end:
37911c11
PP
743 return ret;
744}
745
746static inline
747int validate_new_stream_clock_class(struct muxer_msg_iter *muxer_msg_iter,
748 struct muxer_comp *muxer_comp, const bt_stream *stream)
749{
750 int ret = 0;
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);
755
756 if (!clock_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;
150a034f
SM
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: "
37911c11
PP
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));
769 ret = -1;
770 }
771
772 goto end;
318fe670
PP
773 }
774
37911c11
PP
775 ret = validate_clock_class(muxer_msg_iter, muxer_comp, clock_class);
776
777end:
958f7d11
PP
778 return ret;
779}
780
ab11110e 781/*
b09a5592
PP
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
786 * message.
ab11110e
PP
787 *
788 * This function does NOT:
789 *
b09a5592 790 * * Update any upstream message iterator.
b09a5592 791 * * Check the upstream message iterators to retry.
ab11110e 792 *
b09a5592
PP
793 * On sucess, this function sets *muxer_upstream_msg_iter to the
794 * upstream message iterator of which the current message is
ab11110e
PP
795 * the youngest, and sets *ts_ns to its time.
796 */
958f7d11 797static
68e2deed 798bt_message_iterator_class_next_method_status
b09a5592 799muxer_msg_iter_youngest_upstream_msg_iter(
958f7d11 800 struct muxer_comp *muxer_comp,
b09a5592
PP
801 struct muxer_msg_iter *muxer_msg_iter,
802 struct muxer_upstream_msg_iter **muxer_upstream_msg_iter,
958f7d11
PP
803 int64_t *ts_ns)
804{
805 size_t i;
806 int ret;
807 int64_t youngest_ts_ns = INT64_MAX;
68e2deed
SM
808 bt_message_iterator_class_next_method_status status =
809 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
958f7d11 810
ec4a3354
PP
811 BT_ASSERT_DBG(muxer_comp);
812 BT_ASSERT_DBG(muxer_msg_iter);
813 BT_ASSERT_DBG(muxer_upstream_msg_iter);
b09a5592
PP
814 *muxer_upstream_msg_iter = NULL;
815
a71ed05c
PP
816 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
817 i++) {
b09a5592
PP
818 const bt_message *msg;
819 struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter =
a71ed05c
PP
820 g_ptr_array_index(
821 muxer_msg_iter->active_muxer_upstream_msg_iters,
822 i);
b09a5592
PP
823 int64_t msg_ts_ns;
824
825 if (!cur_muxer_upstream_msg_iter->msg_iter) {
826 /* This upstream message iterator is ended */
c9ecaa78 827 BT_COMP_LOGT("Skipping ended upstream message iterator: "
b09a5592
PP
828 "muxer-upstream-msg-iter-wrap-addr=%p",
829 cur_muxer_upstream_msg_iter);
958f7d11
PP
830 continue;
831 }
832
ec4a3354 833 BT_ASSERT_DBG(cur_muxer_upstream_msg_iter->msgs->length > 0);
b09a5592 834 msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs);
ec4a3354 835 BT_ASSERT_DBG(msg);
37911c11 836
85e7137b 837 if (G_UNLIKELY(bt_message_get_type(msg) ==
37911c11
PP
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(
842 msg));
843 if (ret) {
844 /*
845 * validate_new_stream_clock_class() logs
846 * errors.
847 */
68e2deed 848 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
37911c11
PP
849 goto end;
850 }
85e7137b 851 } else if (G_UNLIKELY(bt_message_get_type(msg) ==
37911c11
PP
852 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY)) {
853 const bt_clock_snapshot *cs;
37911c11 854
11ddb3ef
PP
855 cs = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
856 msg);
37911c11
PP
857 ret = validate_clock_class(muxer_msg_iter, muxer_comp,
858 bt_clock_snapshot_borrow_clock_class_const(cs));
859 if (ret) {
860 /* validate_clock_class() logs errors */
68e2deed 861 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
37911c11
PP
862 goto end;
863 }
864 }
865
b09a5592
PP
866 ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg,
867 muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns);
958f7d11 868 if (ret) {
b09a5592
PP
869 /* get_msg_ts_ns() logs errors */
870 *muxer_upstream_msg_iter = NULL;
68e2deed 871 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
958f7d11
PP
872 goto end;
873 }
874
759bd1bf
FD
875 /*
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.
879 */
880 if (G_UNLIKELY(*muxer_upstream_msg_iter == NULL) ||
881 msg_ts_ns < youngest_ts_ns) {
b09a5592
PP
882 *muxer_upstream_msg_iter =
883 cur_muxer_upstream_msg_iter;
884 youngest_ts_ns = msg_ts_ns;
958f7d11 885 *ts_ns = youngest_ts_ns;
c3923e1f
FD
886 } else if (msg_ts_ns == youngest_ts_ns) {
887 /*
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.
892 */
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.");
896
897 /*
898 * Order the messages in an arbitrary but determinitic
899 * way.
900 */
9240924b 901 ret = common_muxing_compare_messages(msg, selected_msg);
c3923e1f
FD
902 if (ret < 0) {
903 /*
904 * The `msg` should go first. Update the next
905 * iterator and the current timestamp.
906 */
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);
918 }
958f7d11
PP
919 }
920 }
921
b09a5592 922 if (!*muxer_upstream_msg_iter) {
68e2deed 923 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
958f7d11
PP
924 *ts_ns = INT64_MIN;
925 }
926
927end:
928 return status;
929}
930
931static
68e2deed 932bt_message_iterator_class_next_method_status
fb25b9e3 933validate_muxer_upstream_msg_iter(
a71ed05c
PP
934 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
935 bool *is_ended)
958f7d11 936{
f331468c 937 struct muxer_comp *muxer_comp = muxer_upstream_msg_iter->muxer_comp;
68e2deed 938 bt_message_iterator_class_next_method_status status;
958f7d11 939
d9693c64 940 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
b09a5592
PP
941 "muxer-upstream-msg-iter-wrap-addr=%p",
942 muxer_upstream_msg_iter);
318fe670 943
b09a5592
PP
944 if (muxer_upstream_msg_iter->msgs->length > 0 ||
945 !muxer_upstream_msg_iter->msg_iter) {
d9693c64 946 BT_COMP_LOGD("Already valid or not considered: "
b09a5592
PP
947 "queue-len=%u, upstream-msg-iter-addr=%p",
948 muxer_upstream_msg_iter->msgs->length,
949 muxer_upstream_msg_iter->msg_iter);
68e2deed 950 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
ab11110e
PP
951 goto end;
952 }
953
b09a5592 954 /* muxer_upstream_msg_iter_next() logs details/errors */
a71ed05c
PP
955 status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter,
956 is_ended);
089717de
PP
957
958end:
959 return status;
960}
961
962static
68e2deed 963bt_message_iterator_class_next_method_status
fb25b9e3 964validate_muxer_upstream_msg_iters(
b09a5592 965 struct muxer_msg_iter *muxer_msg_iter)
089717de 966{
f5abbab4 967 struct muxer_comp *muxer_comp = muxer_msg_iter->muxer_comp;
68e2deed 968 bt_message_iterator_class_next_method_status status;
089717de
PP
969 size_t i;
970
d9693c64 971 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
b09a5592 972 "muxer-msg-iter-addr=%p", muxer_msg_iter);
318fe670 973
a71ed05c
PP
974 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
975 i++) {
90aed4f2 976 bool is_ended = false;
b09a5592 977 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
089717de 978 g_ptr_array_index(
a71ed05c 979 muxer_msg_iter->active_muxer_upstream_msg_iters,
089717de
PP
980 i);
981
b09a5592 982 status = validate_muxer_upstream_msg_iter(
a71ed05c 983 muxer_upstream_msg_iter, &is_ended);
68e2deed 984 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
318fe670 985 if (status < 0) {
f331468c
SM
986 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp->self_comp,
987 "Cannot validate muxer's upstream message iterator wrapper: "
b09a5592
PP
988 "muxer-msg-iter-addr=%p, "
989 "muxer-upstream-msg-iter-wrap-addr=%p",
990 muxer_msg_iter,
991 muxer_upstream_msg_iter);
318fe670 992 } else {
d9693c64 993 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
b09a5592
PP
994 "muxer-msg-iter-addr=%p, "
995 "muxer-upstream-msg-iter-wrap-addr=%p",
996 muxer_msg_iter,
997 muxer_upstream_msg_iter);
318fe670
PP
998 }
999
089717de
PP
1000 goto end;
1001 }
744ba28b
PP
1002
1003 /*
a71ed05c
PP
1004 * Move this muxer upstream message iterator to the
1005 * array of ended iterators if it's ended.
744ba28b 1006 */
85e7137b 1007 if (G_UNLIKELY(is_ended)) {
d9693c64 1008 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
a71ed05c
PP
1009 "muxer-msg-iter-addr=%p, "
1010 "muxer-upstream-msg-iter-wrap-addr=%p",
1011 muxer_msg_iter, muxer_upstream_msg_iter);
1012 g_ptr_array_add(
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;
1016
744ba28b
PP
1017 /*
1018 * Use g_ptr_array_remove_fast() because the
1019 * order of those elements is not important.
1020 */
1021 g_ptr_array_remove_index_fast(
a71ed05c 1022 muxer_msg_iter->active_muxer_upstream_msg_iters,
744ba28b
PP
1023 i);
1024 i--;
1025 }
089717de
PP
1026 }
1027
68e2deed 1028 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
f331468c 1029
089717de
PP
1030end:
1031 return status;
1032}
1033
3fd7b79d 1034static inline
68e2deed 1035bt_message_iterator_class_next_method_status muxer_msg_iter_do_next_one(
089717de 1036 struct muxer_comp *muxer_comp,
b09a5592
PP
1037 struct muxer_msg_iter *muxer_msg_iter,
1038 const bt_message **msg)
089717de 1039{
68e2deed 1040 bt_message_iterator_class_next_method_status status;
b09a5592 1041 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL;
089717de
PP
1042 int64_t next_return_ts;
1043
1043fdea 1044 status = validate_muxer_upstream_msg_iters(muxer_msg_iter);
68e2deed 1045 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1043fdea
PP
1046 /* validate_muxer_upstream_msg_iters() logs details */
1047 goto end;
958f7d11
PP
1048 }
1049
1050 /*
089717de 1051 * At this point we know that all the existing upstream
b09a5592
PP
1052 * message iterators are valid. We can find the one,
1053 * amongst those, of which the current message is the
089717de 1054 * youngest.
958f7d11 1055 */
b09a5592
PP
1056 status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp,
1057 muxer_msg_iter, &muxer_upstream_msg_iter,
089717de 1058 &next_return_ts);
68e2deed 1059 if (status < 0 || status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END) {
3fd7b79d 1060 if (status < 0) {
f331468c
SM
1061 BT_COMP_LOGE_APPEND_CAUSE(muxer_comp->self_comp,
1062 "Cannot find the youngest upstream message iterator wrapper: "
318fe670 1063 "status=%s",
fb25b9e3 1064 bt_common_func_status_string(status));
318fe670 1065 } else {
d9693c64 1066 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
318fe670 1067 "status=%s",
fb25b9e3 1068 bt_common_func_status_string(status));
318fe670
PP
1069 }
1070
958f7d11
PP
1071 goto end;
1072 }
1073
b09a5592 1074 if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) {
f331468c
SM
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: "
b09a5592 1077 "muxer-msg-iter-addr=%p, ts=%" PRId64 ", "
318fe670 1078 "last-returned-ts=%" PRId64,
b09a5592
PP
1079 muxer_msg_iter, next_return_ts,
1080 muxer_msg_iter->last_returned_ts_ns);
68e2deed 1081 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
958f7d11
PP
1082 goto end;
1083 }
1084
d9693c64 1085 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
b09a5592
PP
1086 "muxer-msg-iter-addr=%p, "
1087 "muxer-upstream-msg-iter-wrap-addr=%p, "
318fe670 1088 "ts=%" PRId64,
b09a5592 1089 muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts);
ec4a3354 1090 BT_ASSERT_DBG(status ==
68e2deed 1091 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK);
ec4a3354 1092 BT_ASSERT_DBG(muxer_upstream_msg_iter);
958f7d11
PP
1093
1094 /*
3fd7b79d 1095 * Consume from the queue's head: other side
b09a5592 1096 * (muxer_upstream_msg_iter_next()) writes to the tail.
958f7d11 1097 */
b09a5592 1098 *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs);
ec4a3354 1099 BT_ASSERT_DBG(*msg);
b09a5592 1100 muxer_msg_iter->last_returned_ts_ns = next_return_ts;
958f7d11
PP
1101
1102end:
3fd7b79d
PP
1103 return status;
1104}
1105
1106static
68e2deed 1107bt_message_iterator_class_next_method_status muxer_msg_iter_do_next(
3fd7b79d 1108 struct muxer_comp *muxer_comp,
b09a5592
PP
1109 struct muxer_msg_iter *muxer_msg_iter,
1110 bt_message_array_const msgs, uint64_t capacity,
3fd7b79d
PP
1111 uint64_t *count)
1112{
68e2deed 1113 bt_message_iterator_class_next_method_status status;
3fd7b79d
PP
1114 uint64_t i = 0;
1115
44381fae
SM
1116 if (G_UNLIKELY(muxer_msg_iter->next_saved_error)) {
1117 /*
1118 * Last time we were called, we hit an error but had some
1119 * messages to deliver, so we stashed the error here. Return
1120 * it now.
1121 */
1122 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(muxer_msg_iter->next_saved_error);
1123 status = muxer_msg_iter->next_saved_status;
1124 goto end;
1125 }
1126
1127 do {
b09a5592
PP
1128 status = muxer_msg_iter_do_next_one(muxer_comp,
1129 muxer_msg_iter, &msgs[i]);
68e2deed 1130 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
3fd7b79d
PP
1131 i++;
1132 }
68e2deed 1133 } while (i < capacity && status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK);
3fd7b79d
PP
1134
1135 if (i > 0) {
1136 /*
b09a5592 1137 * Even if muxer_msg_iter_do_next_one() returned
3fd7b79d 1138 * something else than
b09a5592
PP
1139 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1140 * message objects in the output message
3fd7b79d 1141 * array, so we need to return
b09a5592 1142 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
3fd7b79d 1143 * transfered to downstream. This other status occurs
b09a5592 1144 * again the next time muxer_msg_iter_do_next() is
3fd7b79d 1145 * called, possibly without any accumulated
b09a5592 1146 * message, in which case we'll return it.
3fd7b79d 1147 */
44381fae
SM
1148 if (status < 0) {
1149 /*
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.
1154 */
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;
1158 }
1159
3fd7b79d 1160 *count = i;
68e2deed 1161 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
3fd7b79d
PP
1162 }
1163
44381fae 1164end:
3fd7b79d 1165 return status;
958f7d11
PP
1166}
1167
1168static
b09a5592 1169void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter)
ab11110e 1170{
f5abbab4
PP
1171 struct muxer_comp *muxer_comp;
1172
b09a5592 1173 if (!muxer_msg_iter) {
ab11110e
PP
1174 return;
1175 }
1176
f5abbab4 1177 muxer_comp = muxer_msg_iter->muxer_comp;
d9693c64 1178 BT_COMP_LOGD("Destroying muxer component's message iterator: "
b09a5592 1179 "muxer-msg-iter-addr=%p", muxer_msg_iter);
318fe670 1180
a71ed05c 1181 if (muxer_msg_iter->active_muxer_upstream_msg_iters) {
d9693c64 1182 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
a71ed05c
PP
1183 g_ptr_array_free(
1184 muxer_msg_iter->active_muxer_upstream_msg_iters, TRUE);
1185 }
1186
1187 if (muxer_msg_iter->ended_muxer_upstream_msg_iters) {
d9693c64 1188 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
ab11110e 1189 g_ptr_array_free(
a71ed05c 1190 muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE);
ab11110e
PP
1191 }
1192
b09a5592 1193 g_free(muxer_msg_iter);
ab11110e
PP
1194}
1195
1196static
68e2deed 1197bt_message_iterator_class_initialize_method_status
ab8b2b1b 1198muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
c49bf79b
SM
1199 struct muxer_msg_iter *muxer_msg_iter,
1200 struct bt_self_message_iterator_configuration *config)
958f7d11 1201{
544d0515
PP
1202 int64_t count;
1203 int64_t i;
68e2deed 1204 bt_message_iterator_class_initialize_method_status status;
c49bf79b 1205 bool can_seek_forward = true;
958f7d11 1206
834e9996 1207 count = bt_component_filter_get_input_port_count(
bb61965b 1208 bt_self_component_filter_as_component_filter(
d9693c64 1209 muxer_comp->self_comp_flt));
544d0515 1210 if (count < 0) {
d9693c64 1211 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
b09a5592
PP
1212 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1213 muxer_comp, muxer_msg_iter);
68e2deed 1214 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
ab11110e
PP
1215 goto end;
1216 }
958f7d11
PP
1217
1218 for (i = 0; i < count; i++) {
fbd8a4e0 1219 bt_message_iterator *upstream_msg_iter;
8eee8ea2 1220 bt_self_component_port_input *self_port =
834e9996 1221 bt_self_component_filter_borrow_input_port_by_index(
d9693c64 1222 muxer_comp->self_comp_flt, i);
8eee8ea2 1223 const bt_port *port;
fbd8a4e0 1224 bt_message_iterator_create_from_message_iterator_status
ab8b2b1b
SM
1225 msg_iter_status;
1226 int int_status;
958f7d11 1227
834e9996 1228 BT_ASSERT(self_port);
bb61965b
PP
1229 port = bt_self_component_port_as_port(
1230 bt_self_component_port_input_as_self_component_port(
834e9996 1231 self_port));
8b45963b 1232 BT_ASSERT(port);
958f7d11
PP
1233
1234 if (!bt_port_is_connected(port)) {
1043fdea 1235 /* Skip non-connected port */
958f7d11
PP
1236 continue;
1237 }
1238
ab8b2b1b
SM
1239 msg_iter_status = create_msg_iter_on_input_port(muxer_comp,
1240 muxer_msg_iter, self_port, &upstream_msg_iter);
fbd8a4e0 1241 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
1043fdea 1242 /* create_msg_iter_on_input_port() logs errors */
ab8b2b1b 1243 status = (int) msg_iter_status;
ab11110e 1244 goto end;
958f7d11 1245 }
318fe670 1246
ab8b2b1b 1247 int_status = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter,
ae644e59 1248 upstream_msg_iter);
fbd8a4e0 1249 bt_message_iterator_put_ref(
1043fdea 1250 upstream_msg_iter);
ab8b2b1b 1251 if (int_status) {
68e2deed 1252 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
1043fdea 1253 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1043fdea
PP
1254 goto end;
1255 }
c49bf79b
SM
1256
1257 can_seek_forward = can_seek_forward &&
fbd8a4e0 1258 bt_message_iterator_can_seek_forward(
c49bf79b 1259 upstream_msg_iter);
958f7d11
PP
1260 }
1261
c49bf79b
SM
1262 /*
1263 * This iterator can seek forward if all of its iterators can seek
1264 * forward.
1265 */
1266 bt_self_message_iterator_configuration_set_can_seek_forward(
1267 config, can_seek_forward);
1268
68e2deed 1269 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
ab8b2b1b 1270
958f7d11 1271end:
ab8b2b1b 1272 return status;
958f7d11
PP
1273}
1274
958f7d11 1275BT_HIDDEN
68e2deed 1276bt_message_iterator_class_initialize_method_status muxer_msg_iter_init(
b09a5592 1277 bt_self_message_iterator *self_msg_iter,
9415de1c 1278 bt_self_message_iterator_configuration *config,
8eee8ea2 1279 bt_self_component_port_output *port)
958f7d11
PP
1280{
1281 struct muxer_comp *muxer_comp = NULL;
b09a5592 1282 struct muxer_msg_iter *muxer_msg_iter = NULL;
68e2deed 1283 bt_message_iterator_class_initialize_method_status status;
e1b15e55
PP
1284 bt_self_component *self_comp =
1285 bt_self_message_iterator_borrow_component(self_msg_iter);
958f7d11 1286
68e2deed 1287 muxer_comp = bt_self_component_get_data(self_comp);
8b45963b 1288 BT_ASSERT(muxer_comp);
d9693c64 1289 BT_COMP_LOGD("Initializing muxer component's message iterator: "
b09a5592
PP
1290 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1291 self_comp, muxer_comp, self_msg_iter);
a09c6b95 1292
b09a5592 1293 if (muxer_comp->initializing_muxer_msg_iter) {
a09c6b95 1294 /*
089717de 1295 * Weird, unhandled situation detected: downstream
b09a5592
PP
1296 * creates a muxer message iterator while creating
1297 * another muxer message iterator (same component).
a09c6b95 1298 */
d9693c64 1299 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
b09a5592
PP
1300 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1301 self_comp, muxer_comp, self_msg_iter);
68e2deed 1302 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
958f7d11
PP
1303 goto error;
1304 }
1305
b09a5592
PP
1306 muxer_comp->initializing_muxer_msg_iter = true;
1307 muxer_msg_iter = g_new0(struct muxer_msg_iter, 1);
1308 if (!muxer_msg_iter) {
d9693c64 1309 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
68e2deed 1310 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
ab11110e
PP
1311 goto error;
1312 }
1313
f5abbab4 1314 muxer_msg_iter->muxer_comp = muxer_comp;
692f1a01 1315 muxer_msg_iter->self_msg_iter = self_msg_iter;
b09a5592 1316 muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
a71ed05c 1317 muxer_msg_iter->active_muxer_upstream_msg_iters =
958f7d11 1318 g_ptr_array_new_with_free_func(
b09a5592 1319 (GDestroyNotify) destroy_muxer_upstream_msg_iter);
a71ed05c 1320 if (!muxer_msg_iter->active_muxer_upstream_msg_iters) {
d9693c64 1321 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
68e2deed 1322 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
a71ed05c
PP
1323 goto error;
1324 }
1325
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) {
d9693c64 1330 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
68e2deed 1331 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
958f7d11
PP
1332 goto error;
1333 }
1334
ab8b2b1b 1335 status = muxer_msg_iter_init_upstream_iterators(muxer_comp,
c49bf79b 1336 muxer_msg_iter, config);
ab8b2b1b 1337 if (status) {
d9693c64 1338 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
318fe670 1339 "comp-addr=%p, muxer-comp-addr=%p, "
b09a5592
PP
1340 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1341 self_comp, muxer_comp, muxer_msg_iter,
ab8b2b1b 1342 self_msg_iter, status);
a09c6b95
PP
1343 goto error;
1344 }
1345
1043fdea 1346 bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter);
d9693c64 1347 BT_COMP_LOGD("Initialized muxer component's message iterator: "
b09a5592
PP
1348 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1349 "msg-iter-addr=%p",
1350 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
958f7d11
PP
1351 goto end;
1352
1353error:
b09a5592 1354 destroy_muxer_msg_iter(muxer_msg_iter);
1043fdea 1355 bt_self_message_iterator_set_data(self_msg_iter, NULL);
958f7d11
PP
1356
1357end:
b09a5592 1358 muxer_comp->initializing_muxer_msg_iter = false;
958f7d11
PP
1359 return status;
1360}
1361
1362BT_HIDDEN
a71ed05c 1363void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
958f7d11 1364{
b09a5592
PP
1365 struct muxer_msg_iter *muxer_msg_iter =
1366 bt_self_message_iterator_get_data(self_msg_iter);
8eee8ea2 1367 bt_self_component *self_comp = NULL;
958f7d11
PP
1368 struct muxer_comp *muxer_comp = NULL;
1369
b09a5592
PP
1370 self_comp = bt_self_message_iterator_borrow_component(
1371 self_msg_iter);
834e9996
PP
1372 BT_ASSERT(self_comp);
1373 muxer_comp = bt_self_component_get_data(self_comp);
d9693c64 1374 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
b09a5592
PP
1375 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1376 "msg-iter-addr=%p",
1377 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
958f7d11 1378
1043fdea 1379 if (muxer_msg_iter) {
b09a5592 1380 destroy_muxer_msg_iter(muxer_msg_iter);
958f7d11 1381 }
958f7d11
PP
1382}
1383
1384BT_HIDDEN
68e2deed 1385bt_message_iterator_class_next_method_status muxer_msg_iter_next(
b09a5592
PP
1386 bt_self_message_iterator *self_msg_iter,
1387 bt_message_array_const msgs, uint64_t capacity,
3fd7b79d 1388 uint64_t *count)
958f7d11 1389{
68e2deed 1390 bt_message_iterator_class_next_method_status status;
b09a5592
PP
1391 struct muxer_msg_iter *muxer_msg_iter =
1392 bt_self_message_iterator_get_data(self_msg_iter);
8eee8ea2 1393 bt_self_component *self_comp = NULL;
958f7d11 1394 struct muxer_comp *muxer_comp = NULL;
958f7d11 1395
ec4a3354 1396 BT_ASSERT_DBG(muxer_msg_iter);
b09a5592
PP
1397 self_comp = bt_self_message_iterator_borrow_component(
1398 self_msg_iter);
ec4a3354 1399 BT_ASSERT_DBG(self_comp);
834e9996 1400 muxer_comp = bt_self_component_get_data(self_comp);
ec4a3354 1401 BT_ASSERT_DBG(muxer_comp);
c9ecaa78 1402 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
b09a5592
PP
1403 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1404 "msg-iter-addr=%p",
1405 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
318fe670 1406
b09a5592
PP
1407 status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter,
1408 msgs, capacity, count);
3fd7b79d 1409 if (status < 0) {
d9693c64 1410 BT_COMP_LOGE("Cannot get next message: "
b09a5592
PP
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,
fb25b9e3 1414 bt_common_func_status_string(status));
318fe670 1415 } else {
c9ecaa78 1416 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
3fd7b79d 1417 "status=%s",
fb25b9e3 1418 bt_common_func_status_string(status));
318fe670 1419 }
958f7d11 1420
3fd7b79d 1421 return status;
958f7d11
PP
1422}
1423
1424BT_HIDDEN
fb25b9e3 1425bt_component_class_port_connected_method_status muxer_input_port_connected(
8eee8ea2
PP
1426 bt_self_component_filter *self_comp,
1427 bt_self_component_port_input *self_port,
1428 const bt_port_output *other_port)
958f7d11 1429{
fb25b9e3
PP
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;
f5abbab4
PP
1433 struct muxer_comp *muxer_comp = bt_self_component_get_data(
1434 bt_self_component_filter_as_self_component(self_comp));
958f7d11 1435
fb25b9e3
PP
1436 add_port_status = add_available_input_port(self_comp);
1437 if (add_port_status) {
d9693c64 1438 BT_COMP_LOGE("Cannot add one muxer component's input port: "
1043fdea 1439 "status=%s",
fb25b9e3
PP
1440 bt_common_func_status_string(status));
1441
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;
1445 } else {
1446 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
1447 }
1448
06a2cb0d
PP
1449 goto end;
1450 }
1451
958f7d11 1452end:
634f394c 1453 return status;
958f7d11 1454}
c9d3ff42 1455
a71ed05c 1456static inline
68e2deed 1457bt_message_iterator_class_can_seek_beginning_method_status
9e8e8b43
SM
1458muxer_upstream_msg_iters_can_all_seek_beginning(
1459 GPtrArray *muxer_upstream_msg_iters, bt_bool *can_seek)
c9d3ff42 1460{
68e2deed
SM
1461 bt_message_iterator_class_can_seek_beginning_method_status status =
1462 BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
c9d3ff42 1463 uint64_t i;
c9d3ff42 1464
a71ed05c 1465 for (i = 0; i < muxer_upstream_msg_iters->len; i++) {
c9d3ff42 1466 struct muxer_upstream_msg_iter *upstream_msg_iter =
a71ed05c 1467 muxer_upstream_msg_iters->pdata[i];
fbd8a4e0 1468 status = (int) bt_message_iterator_can_seek_beginning(
9e8e8b43 1469 upstream_msg_iter->msg_iter, can_seek);
68e2deed 1470 if (status != BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK) {
9e8e8b43
SM
1471 goto end;
1472 }
c9d3ff42 1473
9e8e8b43 1474 if (!*can_seek) {
c9d3ff42
PP
1475 goto end;
1476 }
1477 }
1478
9e8e8b43
SM
1479 *can_seek = BT_TRUE;
1480
c9d3ff42 1481end:
9e8e8b43 1482 return status;
c9d3ff42
PP
1483}
1484
a71ed05c 1485BT_HIDDEN
68e2deed 1486bt_message_iterator_class_can_seek_beginning_method_status
9e8e8b43
SM
1487muxer_msg_iter_can_seek_beginning(
1488 bt_self_message_iterator *self_msg_iter, bt_bool *can_seek)
a71ed05c
PP
1489{
1490 struct muxer_msg_iter *muxer_msg_iter =
1491 bt_self_message_iterator_get_data(self_msg_iter);
68e2deed 1492 bt_message_iterator_class_can_seek_beginning_method_status status;
a71ed05c 1493
9e8e8b43
SM
1494 status = muxer_upstream_msg_iters_can_all_seek_beginning(
1495 muxer_msg_iter->active_muxer_upstream_msg_iters, can_seek);
68e2deed 1496 if (status != BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK) {
a71ed05c
PP
1497 goto end;
1498 }
1499
9e8e8b43 1500 if (!*can_seek) {
a71ed05c
PP
1501 goto end;
1502 }
1503
9e8e8b43
SM
1504 status = muxer_upstream_msg_iters_can_all_seek_beginning(
1505 muxer_msg_iter->ended_muxer_upstream_msg_iters, can_seek);
1506
a71ed05c 1507end:
9e8e8b43 1508 return status;
a71ed05c
PP
1509}
1510
c9d3ff42 1511BT_HIDDEN
68e2deed 1512bt_message_iterator_class_seek_beginning_method_status muxer_msg_iter_seek_beginning(
c9d3ff42
PP
1513 bt_self_message_iterator *self_msg_iter)
1514{
1515 struct muxer_msg_iter *muxer_msg_iter =
1516 bt_self_message_iterator_get_data(self_msg_iter);
68e2deed
SM
1517 bt_message_iterator_class_seek_beginning_method_status status =
1518 BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
fb25b9e3 1519 bt_message_iterator_seek_beginning_status seek_beg_status;
c9d3ff42
PP
1520 uint64_t i;
1521
a71ed05c
PP
1522 /* Seek all ended upstream iterators first */
1523 for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
1524 i++) {
c9d3ff42 1525 struct muxer_upstream_msg_iter *upstream_msg_iter =
a71ed05c 1526 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
c9d3ff42 1527
fbd8a4e0 1528 seek_beg_status = bt_message_iterator_seek_beginning(
c9d3ff42 1529 upstream_msg_iter->msg_iter);
fb25b9e3
PP
1530 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
1531 status = (int) seek_beg_status;
c9d3ff42
PP
1532 goto end;
1533 }
a71ed05c
PP
1534
1535 empty_message_queue(upstream_msg_iter);
1536 }
1537
1538 /* Seek all previously active upstream iterators */
1539 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
1540 i++) {
1541 struct muxer_upstream_msg_iter *upstream_msg_iter =
1542 muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i];
1543
fbd8a4e0 1544 seek_beg_status = bt_message_iterator_seek_beginning(
a71ed05c 1545 upstream_msg_iter->msg_iter);
fb25b9e3
PP
1546 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
1547 status = (int) seek_beg_status;
a71ed05c
PP
1548 goto end;
1549 }
1550
1551 empty_message_queue(upstream_msg_iter);
1552 }
1553
1554 /* Make them all active */
1555 for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
1556 i++) {
1557 struct muxer_upstream_msg_iter *upstream_msg_iter =
1558 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
1559
1560 g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
1561 upstream_msg_iter);
1562 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL;
c9d3ff42
PP
1563 }
1564
0e4ccfb9
MJ
1565 /*
1566 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1567 * called on an empty array.
1568 */
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);
1572 }
c9d3ff42 1573 muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
a71ed05c
PP
1574 muxer_msg_iter->clock_class_expectation =
1575 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY;
c9d3ff42
PP
1576
1577end:
fb25b9e3 1578 return status;
c9d3ff42 1579}
This page took 0.160004 seconds and 4 git commands to generate.