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