lib: make can_seek_beginning and can_seek_ns_from_origin methods return a status
[babeltrace.git] / src / plugins / utils / muxer / muxer.c
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
23 #define BT_COMP_LOG_SELF_COMP (muxer_comp->self_comp)
24 #define BT_LOG_OUTPUT_LEVEL (muxer_comp->log_level)
25 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.MUXER"
26 #include "logging/comp-logging.h"
27
28 #include "common/macros.h"
29 #include "common/uuid.h"
30 #include <babeltrace2/babeltrace.h>
31 #include <glib.h>
32 #include <stdbool.h>
33 #include <inttypes.h>
34 #include "common/assert.h"
35 #include "common/common.h"
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "plugins/common/muxing/muxing.h"
40
41 #include "muxer.h"
42
43 #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes"
44
45 struct muxer_comp {
46 /* Weak refs */
47 bt_self_component_filter *self_comp_flt;
48 bt_self_component *self_comp;
49
50 unsigned int next_port_num;
51 size_t available_input_ports;
52 bool initializing_muxer_msg_iter;
53 bool assume_absolute_clock_classes;
54 bt_logging_level log_level;
55 };
56
57 struct muxer_upstream_msg_iter {
58 struct muxer_comp *muxer_comp;
59
60 /* Owned by this, NULL if ended */
61 bt_self_component_port_input_message_iterator *msg_iter;
62
63 /* Contains `const bt_message *`, owned by this */
64 GQueue *msgs;
65 };
66
67 enum muxer_msg_iter_clock_class_expectation {
68 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0,
69 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE,
70 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE,
71 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID,
72 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID,
73 };
74
75 struct muxer_msg_iter {
76 struct muxer_comp *muxer_comp;
77
78 /* Weak */
79 bt_self_message_iterator *self_msg_iter;
80
81 /*
82 * Array of struct muxer_upstream_msg_iter * (owned by this).
83 *
84 * NOTE: This array is searched in linearly to find the youngest
85 * current message. Keep this until benchmarks confirm that
86 * another data structure is faster than this for our typical
87 * use cases.
88 */
89 GPtrArray *active_muxer_upstream_msg_iters;
90
91 /*
92 * Array of struct muxer_upstream_msg_iter * (owned by this).
93 *
94 * We move ended message iterators from
95 * `active_muxer_upstream_msg_iters` to this array so as to be
96 * able to restore them when seeking.
97 */
98 GPtrArray *ended_muxer_upstream_msg_iters;
99
100 /* Last time returned in a message */
101 int64_t last_returned_ts_ns;
102
103 /* Clock class expectation state */
104 enum muxer_msg_iter_clock_class_expectation clock_class_expectation;
105
106 /*
107 * Expected clock class UUID, only valid when
108 * clock_class_expectation is
109 * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
110 */
111 bt_uuid_t expected_clock_class_uuid;
112 };
113
114 static
115 void empty_message_queue(struct muxer_upstream_msg_iter *upstream_msg_iter)
116 {
117 const bt_message *msg;
118
119 while ((msg = g_queue_pop_head(upstream_msg_iter->msgs))) {
120 bt_message_put_ref(msg);
121 }
122 }
123
124 static
125 void destroy_muxer_upstream_msg_iter(
126 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter)
127 {
128 struct muxer_comp *muxer_comp;
129
130 if (!muxer_upstream_msg_iter) {
131 return;
132 }
133
134 muxer_comp = muxer_upstream_msg_iter->muxer_comp;
135 BT_COMP_LOGD("Destroying muxer's upstream message iterator wrapper: "
136 "addr=%p, msg-iter-addr=%p, queue-len=%u",
137 muxer_upstream_msg_iter,
138 muxer_upstream_msg_iter->msg_iter,
139 muxer_upstream_msg_iter->msgs->length);
140 bt_self_component_port_input_message_iterator_put_ref(
141 muxer_upstream_msg_iter->msg_iter);
142
143 if (muxer_upstream_msg_iter->msgs) {
144 empty_message_queue(muxer_upstream_msg_iter);
145 g_queue_free(muxer_upstream_msg_iter->msgs);
146 }
147
148 g_free(muxer_upstream_msg_iter);
149 }
150
151 static
152 int muxer_msg_iter_add_upstream_msg_iter(struct muxer_msg_iter *muxer_msg_iter,
153 bt_self_component_port_input_message_iterator *self_msg_iter)
154 {
155 int ret = 0;
156 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
157 g_new0(struct muxer_upstream_msg_iter, 1);
158 struct muxer_comp *muxer_comp = muxer_msg_iter->muxer_comp;
159
160 if (!muxer_upstream_msg_iter) {
161 BT_COMP_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper.");
162 goto error;
163 }
164
165 muxer_upstream_msg_iter->muxer_comp = muxer_comp;
166 muxer_upstream_msg_iter->msg_iter = self_msg_iter;
167 bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter->msg_iter);
168 muxer_upstream_msg_iter->msgs = g_queue_new();
169 if (!muxer_upstream_msg_iter->msgs) {
170 BT_COMP_LOGE_STR("Failed to allocate a GQueue.");
171 goto error;
172 }
173
174 g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
175 muxer_upstream_msg_iter);
176 BT_COMP_LOGD("Added muxer's upstream message iterator wrapper: "
177 "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p",
178 muxer_upstream_msg_iter, muxer_msg_iter,
179 self_msg_iter);
180
181 goto end;
182
183 error:
184 g_free(muxer_upstream_msg_iter);
185 ret = -1;
186
187 end:
188 return ret;
189 }
190
191 static
192 bt_self_component_add_port_status add_available_input_port(
193 bt_self_component_filter *self_comp)
194 {
195 struct muxer_comp *muxer_comp = bt_self_component_get_data(
196 bt_self_component_filter_as_self_component(self_comp));
197 bt_self_component_add_port_status status =
198 BT_SELF_COMPONENT_ADD_PORT_STATUS_OK;
199 GString *port_name = NULL;
200
201 BT_ASSERT(muxer_comp);
202 port_name = g_string_new("in");
203 if (!port_name) {
204 BT_COMP_LOGE_STR("Failed to allocate a GString.");
205 status = BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR;
206 goto end;
207 }
208
209 g_string_append_printf(port_name, "%u", muxer_comp->next_port_num);
210 status = bt_self_component_filter_add_input_port(
211 self_comp, port_name->str, NULL, NULL);
212 if (status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
213 BT_COMP_LOGE("Cannot add input port to muxer component: "
214 "port-name=\"%s\", comp-addr=%p, status=%s",
215 port_name->str, self_comp,
216 bt_common_func_status_string(status));
217 goto end;
218 }
219
220 muxer_comp->available_input_ports++;
221 muxer_comp->next_port_num++;
222 BT_COMP_LOGI("Added one input port to muxer component: "
223 "port-name=\"%s\", comp-addr=%p",
224 port_name->str, self_comp);
225
226 end:
227 if (port_name) {
228 g_string_free(port_name, TRUE);
229 }
230
231 return status;
232 }
233
234 static
235 bt_self_component_add_port_status create_output_port(
236 bt_self_component_filter *self_comp)
237 {
238 return bt_self_component_filter_add_output_port(
239 self_comp, "out", NULL, NULL);
240 }
241
242 static
243 void destroy_muxer_comp(struct muxer_comp *muxer_comp)
244 {
245 if (!muxer_comp) {
246 return;
247 }
248
249 g_free(muxer_comp);
250 }
251
252 static
253 bt_value *get_default_params(struct muxer_comp *muxer_comp)
254 {
255 bt_value *params;
256 int ret;
257
258 params = bt_value_map_create();
259 if (!params) {
260 BT_COMP_LOGE_STR("Cannot create a map value object.");
261 goto error;
262 }
263
264 ret = bt_value_map_insert_bool_entry(params,
265 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false);
266 if (ret) {
267 BT_COMP_LOGE_STR("Cannot add boolean value to map value object.");
268 goto error;
269 }
270
271 goto end;
272
273 error:
274 BT_VALUE_PUT_REF_AND_RESET(params);
275
276 end:
277 return params;
278 }
279
280 static
281 int configure_muxer_comp(struct muxer_comp *muxer_comp,
282 const bt_value *params)
283 {
284 bt_value *default_params = NULL;
285 bt_value *real_params = NULL;
286 const bt_value *assume_absolute_clock_classes = NULL;
287 int ret = 0;
288 bt_bool bool_val;
289
290 default_params = get_default_params(muxer_comp);
291 if (!default_params) {
292 BT_COMP_LOGE("Cannot get default parameters: "
293 "muxer-comp-addr=%p", muxer_comp);
294 goto error;
295 }
296
297 ret = bt_value_map_extend(default_params, params, &real_params);
298 if (ret) {
299 BT_COMP_LOGE("Cannot extend default parameters map value: "
300 "muxer-comp-addr=%p, def-params-addr=%p, "
301 "params-addr=%p", muxer_comp, default_params,
302 params);
303 goto error;
304 }
305
306 assume_absolute_clock_classes = bt_value_map_borrow_entry_value(real_params,
307 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME);
308 if (assume_absolute_clock_classes &&
309 !bt_value_is_bool(assume_absolute_clock_classes)) {
310 BT_COMP_LOGE("Expecting a boolean value for the `%s` parameter: "
311 "muxer-comp-addr=%p, value-type=%s",
312 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp,
313 bt_common_value_type_string(
314 bt_value_get_type(assume_absolute_clock_classes)));
315 goto error;
316 }
317
318 bool_val = bt_value_bool_get(assume_absolute_clock_classes);
319 muxer_comp->assume_absolute_clock_classes = (bool) bool_val;
320 BT_COMP_LOGI("Configured muxer component: muxer-comp-addr=%p, "
321 "assume-absolute-clock-classes=%d",
322 muxer_comp, muxer_comp->assume_absolute_clock_classes);
323 goto end;
324
325 error:
326 ret = -1;
327
328 end:
329 bt_value_put_ref(default_params);
330 bt_value_put_ref(real_params);
331 return ret;
332 }
333
334 BT_HIDDEN
335 bt_component_class_init_method_status muxer_init(
336 bt_self_component_filter *self_comp_flt,
337 const bt_value *params, void *init_data)
338 {
339 int ret;
340 bt_component_class_init_method_status status =
341 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
342 bt_self_component_add_port_status add_port_status;
343 bt_self_component *self_comp =
344 bt_self_component_filter_as_self_component(self_comp_flt);
345 struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1);
346 bt_logging_level log_level = bt_component_get_logging_level(
347 bt_self_component_as_component(self_comp));
348
349 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO, log_level, self_comp,
350 "Initializing muxer component: "
351 "comp-addr=%p, params-addr=%p", self_comp, params);
352
353 if (!muxer_comp) {
354 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
355 "Failed to allocate one muxer component.");
356 goto error;
357 }
358
359 muxer_comp->log_level = log_level;
360 muxer_comp->self_comp = self_comp;
361 muxer_comp->self_comp_flt = self_comp_flt;
362 ret = configure_muxer_comp(muxer_comp, params);
363 if (ret) {
364 BT_COMP_LOGE("Cannot configure muxer component: "
365 "muxer-comp-addr=%p, params-addr=%p",
366 muxer_comp, params);
367 goto error;
368 }
369
370 bt_self_component_set_data(self_comp, muxer_comp);
371 add_port_status = add_available_input_port(self_comp_flt);
372 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
373 BT_COMP_LOGE("Cannot ensure that at least one muxer component's input port is available: "
374 "muxer-comp-addr=%p, status=%s",
375 muxer_comp,
376 bt_common_func_status_string(add_port_status));
377 if (add_port_status ==
378 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
379 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
380 } else {
381 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
382 }
383
384 goto error;
385 }
386
387 add_port_status = create_output_port(self_comp_flt);
388 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
389 BT_COMP_LOGE("Cannot create muxer component's output port: "
390 "muxer-comp-addr=%p, status=%s",
391 muxer_comp,
392 bt_common_func_status_string(add_port_status));
393 if (add_port_status ==
394 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
395 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
396 } else {
397 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
398 }
399
400 goto error;
401 }
402
403 BT_COMP_LOGI("Initialized muxer component: "
404 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
405 self_comp, params, muxer_comp);
406
407 goto end;
408
409 error:
410 destroy_muxer_comp(muxer_comp);
411 bt_self_component_set_data(self_comp, NULL);
412
413 if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
414 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
415 }
416
417 end:
418 return status;
419 }
420
421 BT_HIDDEN
422 void muxer_finalize(bt_self_component_filter *self_comp)
423 {
424 struct muxer_comp *muxer_comp = bt_self_component_get_data(
425 bt_self_component_filter_as_self_component(self_comp));
426
427 BT_COMP_LOGI("Finalizing muxer component: comp-addr=%p",
428 self_comp);
429 destroy_muxer_comp(muxer_comp);
430 }
431
432 static
433 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
434 create_msg_iter_on_input_port(struct muxer_comp *muxer_comp,
435 struct muxer_msg_iter *muxer_msg_iter,
436 bt_self_component_port_input *self_port,
437 bt_self_component_port_input_message_iterator **msg_iter)
438 {
439 const bt_port *port = bt_self_component_port_as_port(
440 bt_self_component_port_input_as_self_component_port(
441 self_port));
442 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
443 status;
444
445 BT_ASSERT(port);
446 BT_ASSERT(bt_port_is_connected(port));
447
448 // TODO: Advance the iterator to >= the time of the latest
449 // returned message by the muxer message
450 // iterator which creates it.
451 status = bt_self_component_port_input_message_iterator_create_from_message_iterator(
452 muxer_msg_iter->self_msg_iter, self_port, msg_iter);
453 if (status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
454 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
455 "port-addr=%p, port-name=\"%s\"",
456 port, bt_port_get_name(port));
457 goto end;
458 }
459
460 BT_COMP_LOGI("Created upstream message iterator on input port: "
461 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
462 port, bt_port_get_name(port), msg_iter);
463
464 end:
465 return status;
466 }
467
468 static
469 bt_component_class_message_iterator_next_method_status muxer_upstream_msg_iter_next(
470 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
471 bool *is_ended)
472 {
473 struct muxer_comp *muxer_comp =
474 muxer_upstream_msg_iter->muxer_comp;
475 bt_component_class_message_iterator_next_method_status status;
476 bt_message_iterator_next_status input_port_iter_status;
477 bt_message_array_const msgs;
478 uint64_t i;
479 uint64_t count;
480
481 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
482 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
483 muxer_upstream_msg_iter,
484 muxer_upstream_msg_iter->msg_iter);
485 input_port_iter_status = bt_self_component_port_input_message_iterator_next(
486 muxer_upstream_msg_iter->msg_iter, &msgs, &count);
487 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
488 "status=%s",
489 bt_common_func_status_string(input_port_iter_status));
490
491 switch (input_port_iter_status) {
492 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
493 /*
494 * Message iterator's current message is
495 * valid: it must be considered for muxing operations.
496 */
497 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
498 BT_ASSERT(count > 0);
499
500 /* Move messages to our queue */
501 for (i = 0; i < count; i++) {
502 /*
503 * Push to tail in order; other side
504 * (muxer_msg_iter_do_next_one()) consumes
505 * from the head first.
506 */
507 g_queue_push_tail(muxer_upstream_msg_iter->msgs,
508 (void *) msgs[i]);
509 }
510 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
511 break;
512 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
513 /*
514 * Message iterator's current message is not
515 * valid anymore. Return
516 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
517 */
518 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN;
519 break;
520 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: /* Fall-through. */
521 /*
522 * Message iterator reached the end: release it. It
523 * won't be considered again to find the youngest
524 * message.
525 */
526 *is_ended = true;
527 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
528 break;
529 default:
530 /* Error or unsupported status code */
531 BT_COMP_LOGE("Error or unsupported status code: "
532 "status-code=%d", input_port_iter_status);
533 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
534 break;
535 }
536
537 return status;
538 }
539
540 static
541 int get_msg_ts_ns(struct muxer_comp *muxer_comp,
542 struct muxer_msg_iter *muxer_msg_iter,
543 const bt_message *msg, int64_t last_returned_ts_ns,
544 int64_t *ts_ns)
545 {
546 const bt_clock_snapshot *clock_snapshot = NULL;
547 int ret = 0;
548 const bt_stream_class *stream_class = NULL;
549 bt_message_type msg_type;
550
551 BT_ASSERT(msg);
552 BT_ASSERT(ts_ns);
553 BT_COMP_LOGD("Getting message's timestamp: "
554 "muxer-msg-iter-addr=%p, msg-addr=%p, "
555 "last-returned-ts=%" PRId64,
556 muxer_msg_iter, msg, last_returned_ts_ns);
557
558 if (G_UNLIKELY(muxer_msg_iter->clock_class_expectation ==
559 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE)) {
560 *ts_ns = last_returned_ts_ns;
561 goto end;
562 }
563
564 msg_type = bt_message_get_type(msg);
565
566 if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_BEGINNING)) {
567 stream_class = bt_stream_borrow_class_const(
568 bt_packet_borrow_stream_const(
569 bt_message_packet_beginning_borrow_packet_const(
570 msg)));
571 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_END)) {
572 stream_class = bt_stream_borrow_class_const(
573 bt_packet_borrow_stream_const(
574 bt_message_packet_end_borrow_packet_const(
575 msg)));
576 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS)) {
577 stream_class = bt_stream_borrow_class_const(
578 bt_message_discarded_events_borrow_stream_const(msg));
579 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS)) {
580 stream_class = bt_stream_borrow_class_const(
581 bt_message_discarded_packets_borrow_stream_const(msg));
582 }
583
584 switch (msg_type) {
585 case BT_MESSAGE_TYPE_EVENT:
586 BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const(
587 msg));
588 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
589 msg);
590 break;
591 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
592 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
593 stream_class)) {
594 clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
595 msg);
596 } else {
597 goto no_clock_snapshot;
598 }
599
600 break;
601 case BT_MESSAGE_TYPE_PACKET_END:
602 if (bt_stream_class_packets_have_end_default_clock_snapshot(
603 stream_class)) {
604 clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
605 msg);
606 } else {
607 goto no_clock_snapshot;
608 }
609
610 break;
611 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
612 {
613 enum bt_message_stream_clock_snapshot_state snapshot_state =
614 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
615 msg, &clock_snapshot);
616 if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) {
617 goto no_clock_snapshot;
618 }
619
620 break;
621 }
622 case BT_MESSAGE_TYPE_STREAM_END:
623 {
624 enum bt_message_stream_clock_snapshot_state snapshot_state =
625 bt_message_stream_end_borrow_default_clock_snapshot_const(
626 msg, &clock_snapshot);
627 if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) {
628 goto no_clock_snapshot;
629 }
630
631 break;
632 }
633 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
634 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
635 stream_class)) {
636 clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
637 msg);
638 } else {
639 goto no_clock_snapshot;
640 }
641
642 break;
643 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
644 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
645 stream_class)) {
646 clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
647 msg);
648 } else {
649 goto no_clock_snapshot;
650 }
651
652 break;
653 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
654 clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
655 msg);
656 break;
657 default:
658 /* All the other messages have a higher priority */
659 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
660 *ts_ns = last_returned_ts_ns;
661 goto end;
662 }
663
664 ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns);
665 if (ret) {
666 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
667 "clock-snapshot-addr=%p", clock_snapshot);
668 goto error;
669 }
670
671 goto end;
672
673 no_clock_snapshot:
674 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
675 "using the last returned timestamp.");
676 *ts_ns = last_returned_ts_ns;
677 goto end;
678
679 error:
680 ret = -1;
681
682 end:
683 if (ret == 0) {
684 BT_COMP_LOGD("Found message's timestamp: "
685 "muxer-msg-iter-addr=%p, msg-addr=%p, "
686 "last-returned-ts=%" PRId64 ", ts=%" PRId64,
687 muxer_msg_iter, msg, last_returned_ts_ns,
688 *ts_ns);
689 }
690
691 return ret;
692 }
693
694 static inline
695 int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter,
696 struct muxer_comp *muxer_comp,
697 const bt_clock_class *clock_class)
698 {
699 int ret = 0;
700 const uint8_t *cc_uuid;
701 const char *cc_name;
702
703 BT_ASSERT(clock_class);
704 cc_uuid = bt_clock_class_get_uuid(clock_class);
705 cc_name = bt_clock_class_get_name(clock_class);
706
707 if (muxer_msg_iter->clock_class_expectation ==
708 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
709 /*
710 * This is the first clock class that this muxer
711 * message iterator encounters. Its properties
712 * determine what to expect for the whole lifetime of
713 * the iterator without a true
714 * `assume-absolute-clock-classes` parameter.
715 */
716 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
717 /* Expect absolute clock classes */
718 muxer_msg_iter->clock_class_expectation =
719 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE;
720 } else {
721 if (cc_uuid) {
722 /*
723 * Expect non-absolute clock classes
724 * with a specific UUID.
725 */
726 muxer_msg_iter->clock_class_expectation =
727 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID;
728 bt_uuid_copy(muxer_msg_iter->expected_clock_class_uuid, cc_uuid);
729 } else {
730 /*
731 * Expect non-absolute clock classes
732 * with no UUID.
733 */
734 muxer_msg_iter->clock_class_expectation =
735 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID;
736 }
737 }
738 }
739
740 if (!muxer_comp->assume_absolute_clock_classes) {
741 switch (muxer_msg_iter->clock_class_expectation) {
742 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE:
743 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
744 BT_COMP_LOGE("Expecting an absolute clock class, "
745 "but got a non-absolute one: "
746 "clock-class-addr=%p, clock-class-name=\"%s\"",
747 clock_class, cc_name);
748 goto error;
749 }
750 break;
751 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID:
752 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
753 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
754 "but got an absolute one: "
755 "clock-class-addr=%p, clock-class-name=\"%s\"",
756 clock_class, cc_name);
757 goto error;
758 }
759
760 if (cc_uuid) {
761 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
762 "but got one with a UUID: "
763 "clock-class-addr=%p, clock-class-name=\"%s\", "
764 "uuid=\"" BT_UUID_FMT "\"",
765 clock_class, cc_name, BT_UUID_FMT_VALUES(cc_uuid));
766 goto error;
767 }
768 break;
769 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID:
770 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
771 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
772 "but got an absolute one: "
773 "clock-class-addr=%p, clock-class-name=\"%s\"",
774 clock_class, cc_name);
775 goto error;
776 }
777
778 if (!cc_uuid) {
779 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
780 "but got one with no UUID: "
781 "clock-class-addr=%p, clock-class-name=\"%s\"",
782 clock_class, cc_name);
783 goto error;
784 }
785
786 if (bt_uuid_compare(muxer_msg_iter->expected_clock_class_uuid, cc_uuid) != 0) {
787 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
788 "but got one with different UUID: "
789 "clock-class-addr=%p, clock-class-name=\"%s\", "
790 "expected-uuid=\"" BT_UUID_FMT "\", "
791 "uuid=\"" BT_UUID_FMT "\"",
792 clock_class, cc_name,
793 BT_UUID_FMT_VALUES(muxer_msg_iter->expected_clock_class_uuid),
794 BT_UUID_FMT_VALUES(cc_uuid));
795 goto error;
796 }
797 break;
798 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE:
799 BT_COMP_LOGE("Expecting no clock class, but got one: "
800 "clock-class-addr=%p, clock-class-name=\"%s\"",
801 clock_class, cc_name);
802 goto error;
803 default:
804 /* Unexpected */
805 BT_COMP_LOGF("Unexpected clock class expectation: "
806 "expectation-code=%d",
807 muxer_msg_iter->clock_class_expectation);
808 abort();
809 }
810 }
811
812 goto end;
813
814 error:
815 ret = -1;
816
817 end:
818 return ret;
819 }
820
821 static inline
822 int validate_new_stream_clock_class(struct muxer_msg_iter *muxer_msg_iter,
823 struct muxer_comp *muxer_comp, const bt_stream *stream)
824 {
825 int ret = 0;
826 const bt_stream_class *stream_class =
827 bt_stream_borrow_class_const(stream);
828 const bt_clock_class *clock_class =
829 bt_stream_class_borrow_default_clock_class_const(stream_class);
830
831 if (!clock_class) {
832 if (muxer_msg_iter->clock_class_expectation ==
833 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
834 /* Expect no clock class */
835 muxer_msg_iter->clock_class_expectation =
836 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE;
837 } else if (muxer_msg_iter->clock_class_expectation !=
838 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE) {
839 BT_COMP_LOGE("Expecting stream class without a default clock class: "
840 "stream-class-addr=%p, stream-class-name=\"%s\", "
841 "stream-class-id=%" PRIu64,
842 stream_class, bt_stream_class_get_name(stream_class),
843 bt_stream_class_get_id(stream_class));
844 ret = -1;
845 }
846
847 goto end;
848 }
849
850 ret = validate_clock_class(muxer_msg_iter, muxer_comp, clock_class);
851
852 end:
853 return ret;
854 }
855
856 /*
857 * This function finds the youngest available message amongst the
858 * non-ended upstream message iterators and returns the upstream
859 * message iterator which has it, or
860 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
861 * message.
862 *
863 * This function does NOT:
864 *
865 * * Update any upstream message iterator.
866 * * Check the upstream message iterators to retry.
867 *
868 * On sucess, this function sets *muxer_upstream_msg_iter to the
869 * upstream message iterator of which the current message is
870 * the youngest, and sets *ts_ns to its time.
871 */
872 static
873 bt_component_class_message_iterator_next_method_status
874 muxer_msg_iter_youngest_upstream_msg_iter(
875 struct muxer_comp *muxer_comp,
876 struct muxer_msg_iter *muxer_msg_iter,
877 struct muxer_upstream_msg_iter **muxer_upstream_msg_iter,
878 int64_t *ts_ns)
879 {
880 size_t i;
881 int ret;
882 int64_t youngest_ts_ns = INT64_MAX;
883 bt_component_class_message_iterator_next_method_status status =
884 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
885
886 BT_ASSERT(muxer_comp);
887 BT_ASSERT(muxer_msg_iter);
888 BT_ASSERT(muxer_upstream_msg_iter);
889 *muxer_upstream_msg_iter = NULL;
890
891 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
892 i++) {
893 const bt_message *msg;
894 struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter =
895 g_ptr_array_index(
896 muxer_msg_iter->active_muxer_upstream_msg_iters,
897 i);
898 int64_t msg_ts_ns;
899
900 if (!cur_muxer_upstream_msg_iter->msg_iter) {
901 /* This upstream message iterator is ended */
902 BT_COMP_LOGT("Skipping ended upstream message iterator: "
903 "muxer-upstream-msg-iter-wrap-addr=%p",
904 cur_muxer_upstream_msg_iter);
905 continue;
906 }
907
908 BT_ASSERT(cur_muxer_upstream_msg_iter->msgs->length > 0);
909 msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs);
910 BT_ASSERT(msg);
911
912 if (G_UNLIKELY(bt_message_get_type(msg) ==
913 BT_MESSAGE_TYPE_STREAM_BEGINNING)) {
914 ret = validate_new_stream_clock_class(
915 muxer_msg_iter, muxer_comp,
916 bt_message_stream_beginning_borrow_stream_const(
917 msg));
918 if (ret) {
919 /*
920 * validate_new_stream_clock_class() logs
921 * errors.
922 */
923 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
924 goto end;
925 }
926 } else if (G_UNLIKELY(bt_message_get_type(msg) ==
927 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY)) {
928 const bt_clock_snapshot *cs;
929
930 cs = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
931 msg);
932 ret = validate_clock_class(muxer_msg_iter, muxer_comp,
933 bt_clock_snapshot_borrow_clock_class_const(cs));
934 if (ret) {
935 /* validate_clock_class() logs errors */
936 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
937 goto end;
938 }
939 }
940
941 ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg,
942 muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns);
943 if (ret) {
944 /* get_msg_ts_ns() logs errors */
945 *muxer_upstream_msg_iter = NULL;
946 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
947 goto end;
948 }
949
950 /*
951 * Update the current message iterator if it has not been set
952 * yet, or if its current message has a timestamp smaller than
953 * the previously selected youngest message.
954 */
955 if (G_UNLIKELY(*muxer_upstream_msg_iter == NULL) ||
956 msg_ts_ns < youngest_ts_ns) {
957 *muxer_upstream_msg_iter =
958 cur_muxer_upstream_msg_iter;
959 youngest_ts_ns = msg_ts_ns;
960 *ts_ns = youngest_ts_ns;
961 } else if (msg_ts_ns == youngest_ts_ns) {
962 /*
963 * The currently selected message to be sent downstream
964 * next has the exact same timestamp that of the
965 * current candidate message. We must break the tie
966 * in a predictable manner.
967 */
968 const bt_message *selected_msg = g_queue_peek_head(
969 (*muxer_upstream_msg_iter)->msgs);
970 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
971
972 /*
973 * Order the messages in an arbitrary but determinitic
974 * way.
975 */
976 ret = common_muxing_compare_messages(msg, selected_msg);
977 if (ret < 0) {
978 /*
979 * The `msg` should go first. Update the next
980 * iterator and the current timestamp.
981 */
982 *muxer_upstream_msg_iter =
983 cur_muxer_upstream_msg_iter;
984 youngest_ts_ns = msg_ts_ns;
985 *ts_ns = youngest_ts_ns;
986 } else if (ret == 0) {
987 /* Unable to pick which one should go first. */
988 BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
989 "muxer-upstream-msg-iter-wrap-addr=%p"
990 "cur-muxer-upstream-msg-iter-wrap-addr=%p",
991 *muxer_upstream_msg_iter,
992 cur_muxer_upstream_msg_iter);
993 }
994 }
995 }
996
997 if (!*muxer_upstream_msg_iter) {
998 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
999 *ts_ns = INT64_MIN;
1000 }
1001
1002 end:
1003 return status;
1004 }
1005
1006 static
1007 bt_component_class_message_iterator_next_method_status
1008 validate_muxer_upstream_msg_iter(
1009 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
1010 bool *is_ended)
1011 {
1012 struct muxer_comp *muxer_comp =
1013 muxer_upstream_msg_iter->muxer_comp;
1014 bt_component_class_message_iterator_next_method_status status =
1015 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1016
1017 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
1018 "muxer-upstream-msg-iter-wrap-addr=%p",
1019 muxer_upstream_msg_iter);
1020
1021 if (muxer_upstream_msg_iter->msgs->length > 0 ||
1022 !muxer_upstream_msg_iter->msg_iter) {
1023 BT_COMP_LOGD("Already valid or not considered: "
1024 "queue-len=%u, upstream-msg-iter-addr=%p",
1025 muxer_upstream_msg_iter->msgs->length,
1026 muxer_upstream_msg_iter->msg_iter);
1027 goto end;
1028 }
1029
1030 /* muxer_upstream_msg_iter_next() logs details/errors */
1031 status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter,
1032 is_ended);
1033
1034 end:
1035 return status;
1036 }
1037
1038 static
1039 bt_component_class_message_iterator_next_method_status
1040 validate_muxer_upstream_msg_iters(
1041 struct muxer_msg_iter *muxer_msg_iter)
1042 {
1043 struct muxer_comp *muxer_comp = muxer_msg_iter->muxer_comp;
1044 bt_component_class_message_iterator_next_method_status status =
1045 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1046 size_t i;
1047
1048 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
1049 "muxer-msg-iter-addr=%p", muxer_msg_iter);
1050
1051 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
1052 i++) {
1053 bool is_ended = false;
1054 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
1055 g_ptr_array_index(
1056 muxer_msg_iter->active_muxer_upstream_msg_iters,
1057 i);
1058
1059 status = validate_muxer_upstream_msg_iter(
1060 muxer_upstream_msg_iter, &is_ended);
1061 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1062 if (status < 0) {
1063 BT_COMP_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
1064 "muxer-msg-iter-addr=%p, "
1065 "muxer-upstream-msg-iter-wrap-addr=%p",
1066 muxer_msg_iter,
1067 muxer_upstream_msg_iter);
1068 } else {
1069 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
1070 "muxer-msg-iter-addr=%p, "
1071 "muxer-upstream-msg-iter-wrap-addr=%p",
1072 muxer_msg_iter,
1073 muxer_upstream_msg_iter);
1074 }
1075
1076 goto end;
1077 }
1078
1079 /*
1080 * Move this muxer upstream message iterator to the
1081 * array of ended iterators if it's ended.
1082 */
1083 if (G_UNLIKELY(is_ended)) {
1084 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
1085 "muxer-msg-iter-addr=%p, "
1086 "muxer-upstream-msg-iter-wrap-addr=%p",
1087 muxer_msg_iter, muxer_upstream_msg_iter);
1088 g_ptr_array_add(
1089 muxer_msg_iter->ended_muxer_upstream_msg_iters,
1090 muxer_upstream_msg_iter);
1091 muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i] = NULL;
1092
1093 /*
1094 * Use g_ptr_array_remove_fast() because the
1095 * order of those elements is not important.
1096 */
1097 g_ptr_array_remove_index_fast(
1098 muxer_msg_iter->active_muxer_upstream_msg_iters,
1099 i);
1100 i--;
1101 }
1102 }
1103
1104 end:
1105 return status;
1106 }
1107
1108 static inline
1109 bt_component_class_message_iterator_next_method_status muxer_msg_iter_do_next_one(
1110 struct muxer_comp *muxer_comp,
1111 struct muxer_msg_iter *muxer_msg_iter,
1112 const bt_message **msg)
1113 {
1114 bt_component_class_message_iterator_next_method_status status;
1115 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL;
1116 int64_t next_return_ts;
1117
1118 status = validate_muxer_upstream_msg_iters(muxer_msg_iter);
1119 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1120 /* validate_muxer_upstream_msg_iters() logs details */
1121 goto end;
1122 }
1123
1124 /*
1125 * At this point we know that all the existing upstream
1126 * message iterators are valid. We can find the one,
1127 * amongst those, of which the current message is the
1128 * youngest.
1129 */
1130 status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp,
1131 muxer_msg_iter, &muxer_upstream_msg_iter,
1132 &next_return_ts);
1133 if (status < 0 || status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END) {
1134 if (status < 0) {
1135 BT_COMP_LOGE("Cannot find the youngest upstream message iterator wrapper: "
1136 "status=%s",
1137 bt_common_func_status_string(status));
1138 } else {
1139 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
1140 "status=%s",
1141 bt_common_func_status_string(status));
1142 }
1143
1144 goto end;
1145 }
1146
1147 if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) {
1148 BT_COMP_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
1149 "muxer-msg-iter-addr=%p, ts=%" PRId64 ", "
1150 "last-returned-ts=%" PRId64,
1151 muxer_msg_iter, next_return_ts,
1152 muxer_msg_iter->last_returned_ts_ns);
1153 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
1154 goto end;
1155 }
1156
1157 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
1158 "muxer-msg-iter-addr=%p, "
1159 "muxer-upstream-msg-iter-wrap-addr=%p, "
1160 "ts=%" PRId64,
1161 muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts);
1162 BT_ASSERT(status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK);
1163 BT_ASSERT(muxer_upstream_msg_iter);
1164
1165 /*
1166 * Consume from the queue's head: other side
1167 * (muxer_upstream_msg_iter_next()) writes to the tail.
1168 */
1169 *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs);
1170 BT_ASSERT(*msg);
1171 muxer_msg_iter->last_returned_ts_ns = next_return_ts;
1172
1173 end:
1174 return status;
1175 }
1176
1177 static
1178 bt_component_class_message_iterator_next_method_status muxer_msg_iter_do_next(
1179 struct muxer_comp *muxer_comp,
1180 struct muxer_msg_iter *muxer_msg_iter,
1181 bt_message_array_const msgs, uint64_t capacity,
1182 uint64_t *count)
1183 {
1184 bt_component_class_message_iterator_next_method_status status =
1185 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1186 uint64_t i = 0;
1187
1188 while (i < capacity && status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1189 status = muxer_msg_iter_do_next_one(muxer_comp,
1190 muxer_msg_iter, &msgs[i]);
1191 if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1192 i++;
1193 }
1194 }
1195
1196 if (i > 0) {
1197 /*
1198 * Even if muxer_msg_iter_do_next_one() returned
1199 * something else than
1200 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1201 * message objects in the output message
1202 * array, so we need to return
1203 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1204 * transfered to downstream. This other status occurs
1205 * again the next time muxer_msg_iter_do_next() is
1206 * called, possibly without any accumulated
1207 * message, in which case we'll return it.
1208 */
1209 *count = i;
1210 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1211 }
1212
1213 return status;
1214 }
1215
1216 static
1217 void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter)
1218 {
1219 struct muxer_comp *muxer_comp;
1220
1221 if (!muxer_msg_iter) {
1222 return;
1223 }
1224
1225 muxer_comp = muxer_msg_iter->muxer_comp;
1226 BT_COMP_LOGD("Destroying muxer component's message iterator: "
1227 "muxer-msg-iter-addr=%p", muxer_msg_iter);
1228
1229 if (muxer_msg_iter->active_muxer_upstream_msg_iters) {
1230 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
1231 g_ptr_array_free(
1232 muxer_msg_iter->active_muxer_upstream_msg_iters, TRUE);
1233 }
1234
1235 if (muxer_msg_iter->ended_muxer_upstream_msg_iters) {
1236 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
1237 g_ptr_array_free(
1238 muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE);
1239 }
1240
1241 g_free(muxer_msg_iter);
1242 }
1243
1244 static
1245 bt_component_class_message_iterator_init_method_status
1246 muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
1247 struct muxer_msg_iter *muxer_msg_iter)
1248 {
1249 int64_t count;
1250 int64_t i;
1251 bt_component_class_message_iterator_init_method_status status;
1252
1253 count = bt_component_filter_get_input_port_count(
1254 bt_self_component_filter_as_component_filter(
1255 muxer_comp->self_comp_flt));
1256 if (count < 0) {
1257 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
1258 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1259 muxer_comp, muxer_msg_iter);
1260 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
1261 goto end;
1262 }
1263
1264 for (i = 0; i < count; i++) {
1265 bt_self_component_port_input_message_iterator *upstream_msg_iter;
1266 bt_self_component_port_input *self_port =
1267 bt_self_component_filter_borrow_input_port_by_index(
1268 muxer_comp->self_comp_flt, i);
1269 const bt_port *port;
1270 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
1271 msg_iter_status;
1272 int int_status;
1273
1274 BT_ASSERT(self_port);
1275 port = bt_self_component_port_as_port(
1276 bt_self_component_port_input_as_self_component_port(
1277 self_port));
1278 BT_ASSERT(port);
1279
1280 if (!bt_port_is_connected(port)) {
1281 /* Skip non-connected port */
1282 continue;
1283 }
1284
1285 msg_iter_status = create_msg_iter_on_input_port(muxer_comp,
1286 muxer_msg_iter, self_port, &upstream_msg_iter);
1287 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
1288 /* create_msg_iter_on_input_port() logs errors */
1289 status = (int) msg_iter_status;
1290 goto end;
1291 }
1292
1293 int_status = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter,
1294 upstream_msg_iter);
1295 bt_self_component_port_input_message_iterator_put_ref(
1296 upstream_msg_iter);
1297 if (int_status) {
1298 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
1299 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1300 goto end;
1301 }
1302 }
1303
1304 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
1305
1306 end:
1307 return status;
1308 }
1309
1310 BT_HIDDEN
1311 bt_component_class_message_iterator_init_method_status muxer_msg_iter_init(
1312 bt_self_message_iterator *self_msg_iter,
1313 bt_self_component_filter *self_comp,
1314 bt_self_component_port_output *port)
1315 {
1316 struct muxer_comp *muxer_comp = NULL;
1317 struct muxer_msg_iter *muxer_msg_iter = NULL;
1318 bt_component_class_message_iterator_init_method_status status;
1319
1320 muxer_comp = bt_self_component_get_data(
1321 bt_self_component_filter_as_self_component(self_comp));
1322 BT_ASSERT(muxer_comp);
1323 BT_COMP_LOGD("Initializing muxer component's message iterator: "
1324 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1325 self_comp, muxer_comp, self_msg_iter);
1326
1327 if (muxer_comp->initializing_muxer_msg_iter) {
1328 /*
1329 * Weird, unhandled situation detected: downstream
1330 * creates a muxer message iterator while creating
1331 * another muxer message iterator (same component).
1332 */
1333 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
1334 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1335 self_comp, muxer_comp, self_msg_iter);
1336 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
1337 goto error;
1338 }
1339
1340 muxer_comp->initializing_muxer_msg_iter = true;
1341 muxer_msg_iter = g_new0(struct muxer_msg_iter, 1);
1342 if (!muxer_msg_iter) {
1343 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
1344 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
1345 goto error;
1346 }
1347
1348 muxer_msg_iter->muxer_comp = muxer_comp;
1349 muxer_msg_iter->self_msg_iter = self_msg_iter;
1350 muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
1351 muxer_msg_iter->active_muxer_upstream_msg_iters =
1352 g_ptr_array_new_with_free_func(
1353 (GDestroyNotify) destroy_muxer_upstream_msg_iter);
1354 if (!muxer_msg_iter->active_muxer_upstream_msg_iters) {
1355 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1356 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
1357 goto error;
1358 }
1359
1360 muxer_msg_iter->ended_muxer_upstream_msg_iters =
1361 g_ptr_array_new_with_free_func(
1362 (GDestroyNotify) destroy_muxer_upstream_msg_iter);
1363 if (!muxer_msg_iter->ended_muxer_upstream_msg_iters) {
1364 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1365 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
1366 goto error;
1367 }
1368
1369 status = muxer_msg_iter_init_upstream_iterators(muxer_comp,
1370 muxer_msg_iter);
1371 if (status) {
1372 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
1373 "comp-addr=%p, muxer-comp-addr=%p, "
1374 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1375 self_comp, muxer_comp, muxer_msg_iter,
1376 self_msg_iter, status);
1377 goto error;
1378 }
1379
1380 bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter);
1381 BT_COMP_LOGD("Initialized muxer component's message iterator: "
1382 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1383 "msg-iter-addr=%p",
1384 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
1385 goto end;
1386
1387 error:
1388 destroy_muxer_msg_iter(muxer_msg_iter);
1389 bt_self_message_iterator_set_data(self_msg_iter, NULL);
1390
1391 end:
1392 muxer_comp->initializing_muxer_msg_iter = false;
1393 return status;
1394 }
1395
1396 BT_HIDDEN
1397 void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
1398 {
1399 struct muxer_msg_iter *muxer_msg_iter =
1400 bt_self_message_iterator_get_data(self_msg_iter);
1401 bt_self_component *self_comp = NULL;
1402 struct muxer_comp *muxer_comp = NULL;
1403
1404 self_comp = bt_self_message_iterator_borrow_component(
1405 self_msg_iter);
1406 BT_ASSERT(self_comp);
1407 muxer_comp = bt_self_component_get_data(self_comp);
1408 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
1409 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1410 "msg-iter-addr=%p",
1411 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
1412
1413 if (muxer_msg_iter) {
1414 destroy_muxer_msg_iter(muxer_msg_iter);
1415 }
1416 }
1417
1418 BT_HIDDEN
1419 bt_component_class_message_iterator_next_method_status muxer_msg_iter_next(
1420 bt_self_message_iterator *self_msg_iter,
1421 bt_message_array_const msgs, uint64_t capacity,
1422 uint64_t *count)
1423 {
1424 bt_component_class_message_iterator_next_method_status status;
1425 struct muxer_msg_iter *muxer_msg_iter =
1426 bt_self_message_iterator_get_data(self_msg_iter);
1427 bt_self_component *self_comp = NULL;
1428 struct muxer_comp *muxer_comp = NULL;
1429
1430 BT_ASSERT(muxer_msg_iter);
1431 self_comp = bt_self_message_iterator_borrow_component(
1432 self_msg_iter);
1433 BT_ASSERT(self_comp);
1434 muxer_comp = bt_self_component_get_data(self_comp);
1435 BT_ASSERT(muxer_comp);
1436 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
1437 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1438 "msg-iter-addr=%p",
1439 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
1440
1441 status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter,
1442 msgs, capacity, count);
1443 if (status < 0) {
1444 BT_COMP_LOGE("Cannot get next message: "
1445 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1446 "msg-iter-addr=%p, status=%s",
1447 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter,
1448 bt_common_func_status_string(status));
1449 } else {
1450 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
1451 "status=%s",
1452 bt_common_func_status_string(status));
1453 }
1454
1455 return status;
1456 }
1457
1458 BT_HIDDEN
1459 bt_component_class_port_connected_method_status muxer_input_port_connected(
1460 bt_self_component_filter *self_comp,
1461 bt_self_component_port_input *self_port,
1462 const bt_port_output *other_port)
1463 {
1464 bt_component_class_port_connected_method_status status =
1465 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
1466 bt_self_component_add_port_status add_port_status;
1467 struct muxer_comp *muxer_comp = bt_self_component_get_data(
1468 bt_self_component_filter_as_self_component(self_comp));
1469
1470 add_port_status = add_available_input_port(self_comp);
1471 if (add_port_status) {
1472 BT_COMP_LOGE("Cannot add one muxer component's input port: "
1473 "status=%s",
1474 bt_common_func_status_string(status));
1475
1476 if (add_port_status ==
1477 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
1478 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR;
1479 } else {
1480 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
1481 }
1482
1483 goto end;
1484 }
1485
1486 end:
1487 return status;
1488 }
1489
1490 static inline
1491 bt_component_class_message_iterator_can_seek_beginning_method_status
1492 muxer_upstream_msg_iters_can_all_seek_beginning(
1493 GPtrArray *muxer_upstream_msg_iters, bt_bool *can_seek)
1494 {
1495 bt_component_class_message_iterator_can_seek_beginning_method_status status;
1496 uint64_t i;
1497
1498 for (i = 0; i < muxer_upstream_msg_iters->len; i++) {
1499 struct muxer_upstream_msg_iter *upstream_msg_iter =
1500 muxer_upstream_msg_iters->pdata[i];
1501 status = (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1502 upstream_msg_iter->msg_iter, can_seek);
1503 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK) {
1504 goto end;
1505 }
1506
1507 if (!*can_seek) {
1508 goto end;
1509 }
1510 }
1511
1512 *can_seek = BT_TRUE;
1513
1514 end:
1515 return status;
1516 }
1517
1518 BT_HIDDEN
1519 bt_component_class_message_iterator_can_seek_beginning_method_status
1520 muxer_msg_iter_can_seek_beginning(
1521 bt_self_message_iterator *self_msg_iter, bt_bool *can_seek)
1522 {
1523 struct muxer_msg_iter *muxer_msg_iter =
1524 bt_self_message_iterator_get_data(self_msg_iter);
1525 bt_component_class_message_iterator_can_seek_beginning_method_status status;
1526
1527 status = muxer_upstream_msg_iters_can_all_seek_beginning(
1528 muxer_msg_iter->active_muxer_upstream_msg_iters, can_seek);
1529 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK) {
1530 goto end;
1531 }
1532
1533 if (!*can_seek) {
1534 goto end;
1535 }
1536
1537 status = muxer_upstream_msg_iters_can_all_seek_beginning(
1538 muxer_msg_iter->ended_muxer_upstream_msg_iters, can_seek);
1539
1540 end:
1541 return status;
1542 }
1543
1544 BT_HIDDEN
1545 bt_component_class_message_iterator_seek_beginning_method_status muxer_msg_iter_seek_beginning(
1546 bt_self_message_iterator *self_msg_iter)
1547 {
1548 struct muxer_msg_iter *muxer_msg_iter =
1549 bt_self_message_iterator_get_data(self_msg_iter);
1550 bt_component_class_message_iterator_seek_beginning_method_status status =
1551 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
1552 bt_message_iterator_seek_beginning_status seek_beg_status;
1553 uint64_t i;
1554
1555 /* Seek all ended upstream iterators first */
1556 for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
1557 i++) {
1558 struct muxer_upstream_msg_iter *upstream_msg_iter =
1559 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
1560
1561 seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning(
1562 upstream_msg_iter->msg_iter);
1563 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
1564 status = (int) seek_beg_status;
1565 goto end;
1566 }
1567
1568 empty_message_queue(upstream_msg_iter);
1569 }
1570
1571 /* Seek all previously active upstream iterators */
1572 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
1573 i++) {
1574 struct muxer_upstream_msg_iter *upstream_msg_iter =
1575 muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i];
1576
1577 seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning(
1578 upstream_msg_iter->msg_iter);
1579 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
1580 status = (int) seek_beg_status;
1581 goto end;
1582 }
1583
1584 empty_message_queue(upstream_msg_iter);
1585 }
1586
1587 /* Make them all active */
1588 for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
1589 i++) {
1590 struct muxer_upstream_msg_iter *upstream_msg_iter =
1591 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
1592
1593 g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
1594 upstream_msg_iter);
1595 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL;
1596 }
1597
1598 /*
1599 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1600 * called on an empty array.
1601 */
1602 if (muxer_msg_iter->ended_muxer_upstream_msg_iters->len > 0) {
1603 g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters,
1604 0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len);
1605 }
1606 muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
1607 muxer_msg_iter->clock_class_expectation =
1608 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY;
1609
1610 end:
1611 return status;
1612 }
This page took 0.06443 seconds and 4 git commands to generate.