Add message compare common func/library; make flt.utils.muxer use it
[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 *
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 {
438 const bt_port *port = bt_self_component_port_as_port(
439 bt_self_component_port_input_as_self_component_port(
440 self_port));
441 bt_self_component_port_input_message_iterator *msg_iter =
442 NULL;
443
444 BT_ASSERT(port);
445 BT_ASSERT(bt_port_is_connected(port));
446
447 // TODO: Advance the iterator to >= the time of the latest
448 // returned message by the muxer message
449 // iterator which creates it.
450 msg_iter = bt_self_component_port_input_message_iterator_create_from_message_iterator(
451 muxer_msg_iter->self_msg_iter, self_port);
452 if (!msg_iter) {
453 BT_COMP_LOGE("Cannot create upstream message iterator on input port: "
454 "port-addr=%p, port-name=\"%s\"",
455 port, bt_port_get_name(port));
456 goto end;
457 }
458
459 BT_COMP_LOGI("Created upstream message iterator on input port: "
460 "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p",
461 port, bt_port_get_name(port), msg_iter);
462
463 end:
464 return msg_iter;
465 }
466
467 static
468 bt_component_class_message_iterator_next_method_status muxer_upstream_msg_iter_next(
469 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
470 bool *is_ended)
471 {
472 struct muxer_comp *muxer_comp =
473 muxer_upstream_msg_iter->muxer_comp;
474 bt_component_class_message_iterator_next_method_status status;
475 bt_message_iterator_next_status input_port_iter_status;
476 bt_message_array_const msgs;
477 uint64_t i;
478 uint64_t count;
479
480 BT_COMP_LOGD("Calling upstream message iterator's \"next\" method: "
481 "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p",
482 muxer_upstream_msg_iter,
483 muxer_upstream_msg_iter->msg_iter);
484 input_port_iter_status = bt_self_component_port_input_message_iterator_next(
485 muxer_upstream_msg_iter->msg_iter, &msgs, &count);
486 BT_COMP_LOGD("Upstream message iterator's \"next\" method returned: "
487 "status=%s",
488 bt_common_func_status_string(input_port_iter_status));
489
490 switch (input_port_iter_status) {
491 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
492 /*
493 * Message iterator's current message is
494 * valid: it must be considered for muxing operations.
495 */
496 BT_COMP_LOGD_STR("Validated upstream message iterator wrapper.");
497 BT_ASSERT(count > 0);
498
499 /* Move messages to our queue */
500 for (i = 0; i < count; i++) {
501 /*
502 * Push to tail in order; other side
503 * (muxer_msg_iter_do_next_one()) consumes
504 * from the head first.
505 */
506 g_queue_push_tail(muxer_upstream_msg_iter->msgs,
507 (void *) msgs[i]);
508 }
509 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
510 break;
511 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
512 /*
513 * Message iterator's current message is not
514 * valid anymore. Return
515 * BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN immediately.
516 */
517 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN;
518 break;
519 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: /* Fall-through. */
520 /*
521 * Message iterator reached the end: release it. It
522 * won't be considered again to find the youngest
523 * message.
524 */
525 *is_ended = true;
526 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
527 break;
528 default:
529 /* Error or unsupported status code */
530 BT_COMP_LOGE("Error or unsupported status code: "
531 "status-code=%d", input_port_iter_status);
532 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
533 break;
534 }
535
536 return status;
537 }
538
539 static
540 int get_msg_ts_ns(struct muxer_comp *muxer_comp,
541 struct muxer_msg_iter *muxer_msg_iter,
542 const bt_message *msg, int64_t last_returned_ts_ns,
543 int64_t *ts_ns)
544 {
545 const bt_clock_snapshot *clock_snapshot = NULL;
546 int ret = 0;
547 const bt_stream_class *stream_class = NULL;
548 bt_message_type msg_type;
549
550 BT_ASSERT(msg);
551 BT_ASSERT(ts_ns);
552 BT_COMP_LOGD("Getting message's timestamp: "
553 "muxer-msg-iter-addr=%p, msg-addr=%p, "
554 "last-returned-ts=%" PRId64,
555 muxer_msg_iter, msg, last_returned_ts_ns);
556
557 if (G_UNLIKELY(muxer_msg_iter->clock_class_expectation ==
558 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE)) {
559 *ts_ns = last_returned_ts_ns;
560 goto end;
561 }
562
563 msg_type = bt_message_get_type(msg);
564
565 if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_BEGINNING)) {
566 stream_class = bt_stream_borrow_class_const(
567 bt_packet_borrow_stream_const(
568 bt_message_packet_beginning_borrow_packet_const(
569 msg)));
570 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_PACKET_END)) {
571 stream_class = bt_stream_borrow_class_const(
572 bt_packet_borrow_stream_const(
573 bt_message_packet_end_borrow_packet_const(
574 msg)));
575 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS)) {
576 stream_class = bt_stream_borrow_class_const(
577 bt_message_discarded_events_borrow_stream_const(msg));
578 } else if (G_UNLIKELY(msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS)) {
579 stream_class = bt_stream_borrow_class_const(
580 bt_message_discarded_packets_borrow_stream_const(msg));
581 }
582
583 switch (msg_type) {
584 case BT_MESSAGE_TYPE_EVENT:
585 BT_ASSERT(bt_message_event_borrow_stream_class_default_clock_class_const(
586 msg));
587 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
588 msg);
589 break;
590 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
591 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(
592 stream_class)) {
593 clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
594 msg);
595 } else {
596 goto no_clock_snapshot;
597 }
598
599 break;
600 case BT_MESSAGE_TYPE_PACKET_END:
601 if (bt_stream_class_packets_have_end_default_clock_snapshot(
602 stream_class)) {
603 clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
604 msg);
605 } else {
606 goto no_clock_snapshot;
607 }
608
609 break;
610 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
611 {
612 enum bt_message_stream_clock_snapshot_state snapshot_state =
613 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
614 msg, &clock_snapshot);
615 if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) {
616 goto no_clock_snapshot;
617 }
618
619 break;
620 }
621 case BT_MESSAGE_TYPE_STREAM_END:
622 {
623 enum bt_message_stream_clock_snapshot_state snapshot_state =
624 bt_message_stream_end_borrow_default_clock_snapshot_const(
625 msg, &clock_snapshot);
626 if (snapshot_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN) {
627 goto no_clock_snapshot;
628 }
629
630 break;
631 }
632 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
633 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
634 stream_class)) {
635 clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
636 msg);
637 } else {
638 goto no_clock_snapshot;
639 }
640
641 break;
642 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
643 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
644 stream_class)) {
645 clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
646 msg);
647 } else {
648 goto no_clock_snapshot;
649 }
650
651 break;
652 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
653 clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
654 msg);
655 break;
656 default:
657 /* All the other messages have a higher priority */
658 BT_COMP_LOGD_STR("Message has no timestamp: using the last returned timestamp.");
659 *ts_ns = last_returned_ts_ns;
660 goto end;
661 }
662
663 ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns);
664 if (ret) {
665 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
666 "clock-snapshot-addr=%p", clock_snapshot);
667 goto error;
668 }
669
670 goto end;
671
672 no_clock_snapshot:
673 BT_COMP_LOGD_STR("Message's default clock snapshot is missing: "
674 "using the last returned timestamp.");
675 *ts_ns = last_returned_ts_ns;
676 goto end;
677
678 error:
679 ret = -1;
680
681 end:
682 if (ret == 0) {
683 BT_COMP_LOGD("Found message's timestamp: "
684 "muxer-msg-iter-addr=%p, msg-addr=%p, "
685 "last-returned-ts=%" PRId64 ", ts=%" PRId64,
686 muxer_msg_iter, msg, last_returned_ts_ns,
687 *ts_ns);
688 }
689
690 return ret;
691 }
692
693 static inline
694 int validate_clock_class(struct muxer_msg_iter *muxer_msg_iter,
695 struct muxer_comp *muxer_comp,
696 const bt_clock_class *clock_class)
697 {
698 int ret = 0;
699 const uint8_t *cc_uuid;
700 const char *cc_name;
701
702 BT_ASSERT(clock_class);
703 cc_uuid = bt_clock_class_get_uuid(clock_class);
704 cc_name = bt_clock_class_get_name(clock_class);
705
706 if (muxer_msg_iter->clock_class_expectation ==
707 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
708 /*
709 * This is the first clock class that this muxer
710 * message iterator encounters. Its properties
711 * determine what to expect for the whole lifetime of
712 * the iterator without a true
713 * `assume-absolute-clock-classes` parameter.
714 */
715 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
716 /* Expect absolute clock classes */
717 muxer_msg_iter->clock_class_expectation =
718 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE;
719 } else {
720 if (cc_uuid) {
721 /*
722 * Expect non-absolute clock classes
723 * with a specific UUID.
724 */
725 muxer_msg_iter->clock_class_expectation =
726 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID;
727 bt_uuid_copy(muxer_msg_iter->expected_clock_class_uuid, cc_uuid);
728 } else {
729 /*
730 * Expect non-absolute clock classes
731 * with no UUID.
732 */
733 muxer_msg_iter->clock_class_expectation =
734 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID;
735 }
736 }
737 }
738
739 if (!muxer_comp->assume_absolute_clock_classes) {
740 switch (muxer_msg_iter->clock_class_expectation) {
741 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE:
742 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
743 BT_COMP_LOGE("Expecting an absolute clock class, "
744 "but got a non-absolute one: "
745 "clock-class-addr=%p, clock-class-name=\"%s\"",
746 clock_class, cc_name);
747 goto error;
748 }
749 break;
750 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID:
751 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
752 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
753 "but got an absolute one: "
754 "clock-class-addr=%p, clock-class-name=\"%s\"",
755 clock_class, cc_name);
756 goto error;
757 }
758
759 if (cc_uuid) {
760 BT_COMP_LOGE("Expecting a non-absolute clock class with no UUID, "
761 "but got one with a UUID: "
762 "clock-class-addr=%p, clock-class-name=\"%s\", "
763 "uuid=\"" BT_UUID_FMT "\"",
764 clock_class, cc_name, BT_UUID_FMT_VALUES(cc_uuid));
765 goto error;
766 }
767 break;
768 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID:
769 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
770 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
771 "but got an absolute one: "
772 "clock-class-addr=%p, clock-class-name=\"%s\"",
773 clock_class, cc_name);
774 goto error;
775 }
776
777 if (!cc_uuid) {
778 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
779 "but got one with no UUID: "
780 "clock-class-addr=%p, clock-class-name=\"%s\"",
781 clock_class, cc_name);
782 goto error;
783 }
784
785 if (bt_uuid_compare(muxer_msg_iter->expected_clock_class_uuid, cc_uuid) != 0) {
786 BT_COMP_LOGE("Expecting a non-absolute clock class with a specific UUID, "
787 "but got one with different UUID: "
788 "clock-class-addr=%p, clock-class-name=\"%s\", "
789 "expected-uuid=\"" BT_UUID_FMT "\", "
790 "uuid=\"" BT_UUID_FMT "\"",
791 clock_class, cc_name,
792 BT_UUID_FMT_VALUES(muxer_msg_iter->expected_clock_class_uuid),
793 BT_UUID_FMT_VALUES(cc_uuid));
794 goto error;
795 }
796 break;
797 case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE:
798 BT_COMP_LOGE("Expecting no clock class, but got one: "
799 "clock-class-addr=%p, clock-class-name=\"%s\"",
800 clock_class, cc_name);
801 goto error;
802 default:
803 /* Unexpected */
804 BT_COMP_LOGF("Unexpected clock class expectation: "
805 "expectation-code=%d",
806 muxer_msg_iter->clock_class_expectation);
807 abort();
808 }
809 }
810
811 goto end;
812
813 error:
814 ret = -1;
815
816 end:
817 return ret;
818 }
819
820 static inline
821 int validate_new_stream_clock_class(struct muxer_msg_iter *muxer_msg_iter,
822 struct muxer_comp *muxer_comp, const bt_stream *stream)
823 {
824 int ret = 0;
825 const bt_stream_class *stream_class =
826 bt_stream_borrow_class_const(stream);
827 const bt_clock_class *clock_class =
828 bt_stream_class_borrow_default_clock_class_const(stream_class);
829
830 if (!clock_class) {
831 if (muxer_msg_iter->clock_class_expectation ==
832 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
833 /* Expect no clock class */
834 muxer_msg_iter->clock_class_expectation =
835 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NONE;
836 } else {
837 BT_COMP_LOGE("Expecting stream class with a default clock class: "
838 "stream-class-addr=%p, stream-class-name=\"%s\", "
839 "stream-class-id=%" PRIu64,
840 stream_class, bt_stream_class_get_name(stream_class),
841 bt_stream_class_get_id(stream_class));
842 ret = -1;
843 }
844
845 goto end;
846 }
847
848 ret = validate_clock_class(muxer_msg_iter, muxer_comp, clock_class);
849
850 end:
851 return ret;
852 }
853
854 /*
855 * This function finds the youngest available message amongst the
856 * non-ended upstream message iterators and returns the upstream
857 * message iterator which has it, or
858 * BT_MESSAGE_ITERATOR_STATUS_END if there's no available
859 * message.
860 *
861 * This function does NOT:
862 *
863 * * Update any upstream message iterator.
864 * * Check the upstream message iterators to retry.
865 *
866 * On sucess, this function sets *muxer_upstream_msg_iter to the
867 * upstream message iterator of which the current message is
868 * the youngest, and sets *ts_ns to its time.
869 */
870 static
871 bt_component_class_message_iterator_next_method_status
872 muxer_msg_iter_youngest_upstream_msg_iter(
873 struct muxer_comp *muxer_comp,
874 struct muxer_msg_iter *muxer_msg_iter,
875 struct muxer_upstream_msg_iter **muxer_upstream_msg_iter,
876 int64_t *ts_ns)
877 {
878 size_t i;
879 int ret;
880 int64_t youngest_ts_ns = INT64_MAX;
881 bt_component_class_message_iterator_next_method_status status =
882 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
883
884 BT_ASSERT(muxer_comp);
885 BT_ASSERT(muxer_msg_iter);
886 BT_ASSERT(muxer_upstream_msg_iter);
887 *muxer_upstream_msg_iter = NULL;
888
889 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
890 i++) {
891 const bt_message *msg;
892 struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter =
893 g_ptr_array_index(
894 muxer_msg_iter->active_muxer_upstream_msg_iters,
895 i);
896 int64_t msg_ts_ns;
897
898 if (!cur_muxer_upstream_msg_iter->msg_iter) {
899 /* This upstream message iterator is ended */
900 BT_COMP_LOGT("Skipping ended upstream message iterator: "
901 "muxer-upstream-msg-iter-wrap-addr=%p",
902 cur_muxer_upstream_msg_iter);
903 continue;
904 }
905
906 BT_ASSERT(cur_muxer_upstream_msg_iter->msgs->length > 0);
907 msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs);
908 BT_ASSERT(msg);
909
910 if (G_UNLIKELY(bt_message_get_type(msg) ==
911 BT_MESSAGE_TYPE_STREAM_BEGINNING)) {
912 ret = validate_new_stream_clock_class(
913 muxer_msg_iter, muxer_comp,
914 bt_message_stream_beginning_borrow_stream_const(
915 msg));
916 if (ret) {
917 /*
918 * validate_new_stream_clock_class() logs
919 * errors.
920 */
921 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
922 goto end;
923 }
924 } else if (G_UNLIKELY(bt_message_get_type(msg) ==
925 BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY)) {
926 const bt_clock_snapshot *cs;
927
928 cs = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
929 msg);
930 ret = validate_clock_class(muxer_msg_iter, muxer_comp,
931 bt_clock_snapshot_borrow_clock_class_const(cs));
932 if (ret) {
933 /* validate_clock_class() logs errors */
934 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
935 goto end;
936 }
937 }
938
939 ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg,
940 muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns);
941 if (ret) {
942 /* get_msg_ts_ns() logs errors */
943 *muxer_upstream_msg_iter = NULL;
944 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
945 goto end;
946 }
947
948 if (msg_ts_ns < youngest_ts_ns) {
949 *muxer_upstream_msg_iter =
950 cur_muxer_upstream_msg_iter;
951 youngest_ts_ns = msg_ts_ns;
952 *ts_ns = youngest_ts_ns;
953 } else if (msg_ts_ns == youngest_ts_ns) {
954 /*
955 * The currently selected message to be sent downstream
956 * next has the exact same timestamp that of the
957 * current candidate message. We must break the tie
958 * in a predictable manner.
959 */
960 const bt_message *selected_msg = g_queue_peek_head(
961 (*muxer_upstream_msg_iter)->msgs);
962 BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically.");
963
964 /*
965 * Order the messages in an arbitrary but determinitic
966 * way.
967 */
968 ret = common_muxing_compare_messages(msg, selected_msg);
969 if (ret < 0) {
970 /*
971 * The `msg` should go first. Update the next
972 * iterator and the current timestamp.
973 */
974 *muxer_upstream_msg_iter =
975 cur_muxer_upstream_msg_iter;
976 youngest_ts_ns = msg_ts_ns;
977 *ts_ns = youngest_ts_ns;
978 } else if (ret == 0) {
979 /* Unable to pick which one should go first. */
980 BT_COMP_LOGW("Cannot deterministically pick next upstream message iterator because they have identical next messages: "
981 "muxer-upstream-msg-iter-wrap-addr=%p"
982 "cur-muxer-upstream-msg-iter-wrap-addr=%p",
983 *muxer_upstream_msg_iter,
984 cur_muxer_upstream_msg_iter);
985 }
986 }
987 }
988
989 if (!*muxer_upstream_msg_iter) {
990 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
991 *ts_ns = INT64_MIN;
992 }
993
994 end:
995 return status;
996 }
997
998 static
999 bt_component_class_message_iterator_next_method_status
1000 validate_muxer_upstream_msg_iter(
1001 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter,
1002 bool *is_ended)
1003 {
1004 struct muxer_comp *muxer_comp =
1005 muxer_upstream_msg_iter->muxer_comp;
1006 bt_component_class_message_iterator_next_method_status status =
1007 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1008
1009 BT_COMP_LOGD("Validating muxer's upstream message iterator wrapper: "
1010 "muxer-upstream-msg-iter-wrap-addr=%p",
1011 muxer_upstream_msg_iter);
1012
1013 if (muxer_upstream_msg_iter->msgs->length > 0 ||
1014 !muxer_upstream_msg_iter->msg_iter) {
1015 BT_COMP_LOGD("Already valid or not considered: "
1016 "queue-len=%u, upstream-msg-iter-addr=%p",
1017 muxer_upstream_msg_iter->msgs->length,
1018 muxer_upstream_msg_iter->msg_iter);
1019 goto end;
1020 }
1021
1022 /* muxer_upstream_msg_iter_next() logs details/errors */
1023 status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter,
1024 is_ended);
1025
1026 end:
1027 return status;
1028 }
1029
1030 static
1031 bt_component_class_message_iterator_next_method_status
1032 validate_muxer_upstream_msg_iters(
1033 struct muxer_msg_iter *muxer_msg_iter)
1034 {
1035 struct muxer_comp *muxer_comp = muxer_msg_iter->muxer_comp;
1036 bt_component_class_message_iterator_next_method_status status =
1037 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1038 size_t i;
1039
1040 BT_COMP_LOGD("Validating muxer's upstream message iterator wrappers: "
1041 "muxer-msg-iter-addr=%p", muxer_msg_iter);
1042
1043 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
1044 i++) {
1045 bool is_ended = false;
1046 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter =
1047 g_ptr_array_index(
1048 muxer_msg_iter->active_muxer_upstream_msg_iters,
1049 i);
1050
1051 status = validate_muxer_upstream_msg_iter(
1052 muxer_upstream_msg_iter, &is_ended);
1053 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1054 if (status < 0) {
1055 BT_COMP_LOGE("Cannot validate muxer's upstream message iterator wrapper: "
1056 "muxer-msg-iter-addr=%p, "
1057 "muxer-upstream-msg-iter-wrap-addr=%p",
1058 muxer_msg_iter,
1059 muxer_upstream_msg_iter);
1060 } else {
1061 BT_COMP_LOGD("Cannot validate muxer's upstream message iterator wrapper: "
1062 "muxer-msg-iter-addr=%p, "
1063 "muxer-upstream-msg-iter-wrap-addr=%p",
1064 muxer_msg_iter,
1065 muxer_upstream_msg_iter);
1066 }
1067
1068 goto end;
1069 }
1070
1071 /*
1072 * Move this muxer upstream message iterator to the
1073 * array of ended iterators if it's ended.
1074 */
1075 if (G_UNLIKELY(is_ended)) {
1076 BT_COMP_LOGD("Muxer's upstream message iterator wrapper: ended or canceled: "
1077 "muxer-msg-iter-addr=%p, "
1078 "muxer-upstream-msg-iter-wrap-addr=%p",
1079 muxer_msg_iter, muxer_upstream_msg_iter);
1080 g_ptr_array_add(
1081 muxer_msg_iter->ended_muxer_upstream_msg_iters,
1082 muxer_upstream_msg_iter);
1083 muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i] = NULL;
1084
1085 /*
1086 * Use g_ptr_array_remove_fast() because the
1087 * order of those elements is not important.
1088 */
1089 g_ptr_array_remove_index_fast(
1090 muxer_msg_iter->active_muxer_upstream_msg_iters,
1091 i);
1092 i--;
1093 }
1094 }
1095
1096 end:
1097 return status;
1098 }
1099
1100 static inline
1101 bt_component_class_message_iterator_next_method_status muxer_msg_iter_do_next_one(
1102 struct muxer_comp *muxer_comp,
1103 struct muxer_msg_iter *muxer_msg_iter,
1104 const bt_message **msg)
1105 {
1106 bt_component_class_message_iterator_next_method_status status;
1107 struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL;
1108 int64_t next_return_ts;
1109
1110 status = validate_muxer_upstream_msg_iters(muxer_msg_iter);
1111 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1112 /* validate_muxer_upstream_msg_iters() logs details */
1113 goto end;
1114 }
1115
1116 /*
1117 * At this point we know that all the existing upstream
1118 * message iterators are valid. We can find the one,
1119 * amongst those, of which the current message is the
1120 * youngest.
1121 */
1122 status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp,
1123 muxer_msg_iter, &muxer_upstream_msg_iter,
1124 &next_return_ts);
1125 if (status < 0 || status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END) {
1126 if (status < 0) {
1127 BT_COMP_LOGE("Cannot find the youngest upstream message iterator wrapper: "
1128 "status=%s",
1129 bt_common_func_status_string(status));
1130 } else {
1131 BT_COMP_LOGD("Cannot find the youngest upstream message iterator wrapper: "
1132 "status=%s",
1133 bt_common_func_status_string(status));
1134 }
1135
1136 goto end;
1137 }
1138
1139 if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) {
1140 BT_COMP_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: "
1141 "muxer-msg-iter-addr=%p, ts=%" PRId64 ", "
1142 "last-returned-ts=%" PRId64,
1143 muxer_msg_iter, next_return_ts,
1144 muxer_msg_iter->last_returned_ts_ns);
1145 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
1146 goto end;
1147 }
1148
1149 BT_COMP_LOGD("Found youngest upstream message iterator wrapper: "
1150 "muxer-msg-iter-addr=%p, "
1151 "muxer-upstream-msg-iter-wrap-addr=%p, "
1152 "ts=%" PRId64,
1153 muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts);
1154 BT_ASSERT(status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK);
1155 BT_ASSERT(muxer_upstream_msg_iter);
1156
1157 /*
1158 * Consume from the queue's head: other side
1159 * (muxer_upstream_msg_iter_next()) writes to the tail.
1160 */
1161 *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs);
1162 BT_ASSERT(*msg);
1163 muxer_msg_iter->last_returned_ts_ns = next_return_ts;
1164
1165 end:
1166 return status;
1167 }
1168
1169 static
1170 bt_component_class_message_iterator_next_method_status muxer_msg_iter_do_next(
1171 struct muxer_comp *muxer_comp,
1172 struct muxer_msg_iter *muxer_msg_iter,
1173 bt_message_array_const msgs, uint64_t capacity,
1174 uint64_t *count)
1175 {
1176 bt_component_class_message_iterator_next_method_status status =
1177 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1178 uint64_t i = 0;
1179
1180 while (i < capacity && status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1181 status = muxer_msg_iter_do_next_one(muxer_comp,
1182 muxer_msg_iter, &msgs[i]);
1183 if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1184 i++;
1185 }
1186 }
1187
1188 if (i > 0) {
1189 /*
1190 * Even if muxer_msg_iter_do_next_one() returned
1191 * something else than
1192 * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated
1193 * message objects in the output message
1194 * array, so we need to return
1195 * BT_MESSAGE_ITERATOR_STATUS_OK so that they are
1196 * transfered to downstream. This other status occurs
1197 * again the next time muxer_msg_iter_do_next() is
1198 * called, possibly without any accumulated
1199 * message, in which case we'll return it.
1200 */
1201 *count = i;
1202 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1203 }
1204
1205 return status;
1206 }
1207
1208 static
1209 void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter)
1210 {
1211 struct muxer_comp *muxer_comp;
1212
1213 if (!muxer_msg_iter) {
1214 return;
1215 }
1216
1217 muxer_comp = muxer_msg_iter->muxer_comp;
1218 BT_COMP_LOGD("Destroying muxer component's message iterator: "
1219 "muxer-msg-iter-addr=%p", muxer_msg_iter);
1220
1221 if (muxer_msg_iter->active_muxer_upstream_msg_iters) {
1222 BT_COMP_LOGD_STR("Destroying muxer's active upstream message iterator wrappers.");
1223 g_ptr_array_free(
1224 muxer_msg_iter->active_muxer_upstream_msg_iters, TRUE);
1225 }
1226
1227 if (muxer_msg_iter->ended_muxer_upstream_msg_iters) {
1228 BT_COMP_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers.");
1229 g_ptr_array_free(
1230 muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE);
1231 }
1232
1233 g_free(muxer_msg_iter);
1234 }
1235
1236 static
1237 int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp,
1238 struct muxer_msg_iter *muxer_msg_iter)
1239 {
1240 int64_t count;
1241 int64_t i;
1242 int ret = 0;
1243
1244 count = bt_component_filter_get_input_port_count(
1245 bt_self_component_filter_as_component_filter(
1246 muxer_comp->self_comp_flt));
1247 if (count < 0) {
1248 BT_COMP_LOGD("No input port to initialize for muxer component's message iterator: "
1249 "muxer-comp-addr=%p, muxer-msg-iter-addr=%p",
1250 muxer_comp, muxer_msg_iter);
1251 goto end;
1252 }
1253
1254 for (i = 0; i < count; i++) {
1255 bt_self_component_port_input_message_iterator *upstream_msg_iter;
1256 bt_self_component_port_input *self_port =
1257 bt_self_component_filter_borrow_input_port_by_index(
1258 muxer_comp->self_comp_flt, i);
1259 const bt_port *port;
1260
1261 BT_ASSERT(self_port);
1262 port = bt_self_component_port_as_port(
1263 bt_self_component_port_input_as_self_component_port(
1264 self_port));
1265 BT_ASSERT(port);
1266
1267 if (!bt_port_is_connected(port)) {
1268 /* Skip non-connected port */
1269 continue;
1270 }
1271
1272 upstream_msg_iter = create_msg_iter_on_input_port(muxer_comp,
1273 muxer_msg_iter, self_port);
1274 if (!upstream_msg_iter) {
1275 /* create_msg_iter_on_input_port() logs errors */
1276 BT_ASSERT(!upstream_msg_iter);
1277 ret = -1;
1278 goto end;
1279 }
1280
1281 ret = muxer_msg_iter_add_upstream_msg_iter(muxer_msg_iter,
1282 upstream_msg_iter);
1283 bt_self_component_port_input_message_iterator_put_ref(
1284 upstream_msg_iter);
1285 if (ret) {
1286 /* muxer_msg_iter_add_upstream_msg_iter() logs errors */
1287 goto end;
1288 }
1289 }
1290
1291 end:
1292 return ret;
1293 }
1294
1295 BT_HIDDEN
1296 bt_component_class_message_iterator_init_method_status muxer_msg_iter_init(
1297 bt_self_message_iterator *self_msg_iter,
1298 bt_self_component_filter *self_comp,
1299 bt_self_component_port_output *port)
1300 {
1301 struct muxer_comp *muxer_comp = NULL;
1302 struct muxer_msg_iter *muxer_msg_iter = NULL;
1303 bt_component_class_message_iterator_init_method_status status =
1304 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
1305 int ret;
1306
1307 muxer_comp = bt_self_component_get_data(
1308 bt_self_component_filter_as_self_component(self_comp));
1309 BT_ASSERT(muxer_comp);
1310 BT_COMP_LOGD("Initializing muxer component's message iterator: "
1311 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1312 self_comp, muxer_comp, self_msg_iter);
1313
1314 if (muxer_comp->initializing_muxer_msg_iter) {
1315 /*
1316 * Weird, unhandled situation detected: downstream
1317 * creates a muxer message iterator while creating
1318 * another muxer message iterator (same component).
1319 */
1320 BT_COMP_LOGE("Recursive initialization of muxer component's message iterator: "
1321 "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p",
1322 self_comp, muxer_comp, self_msg_iter);
1323 goto error;
1324 }
1325
1326 muxer_comp->initializing_muxer_msg_iter = true;
1327 muxer_msg_iter = g_new0(struct muxer_msg_iter, 1);
1328 if (!muxer_msg_iter) {
1329 BT_COMP_LOGE_STR("Failed to allocate one muxer component's message iterator.");
1330 goto error;
1331 }
1332
1333 muxer_msg_iter->muxer_comp = muxer_comp;
1334 muxer_msg_iter->self_msg_iter = self_msg_iter;
1335 muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
1336 muxer_msg_iter->active_muxer_upstream_msg_iters =
1337 g_ptr_array_new_with_free_func(
1338 (GDestroyNotify) destroy_muxer_upstream_msg_iter);
1339 if (!muxer_msg_iter->active_muxer_upstream_msg_iters) {
1340 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1341 goto error;
1342 }
1343
1344 muxer_msg_iter->ended_muxer_upstream_msg_iters =
1345 g_ptr_array_new_with_free_func(
1346 (GDestroyNotify) destroy_muxer_upstream_msg_iter);
1347 if (!muxer_msg_iter->ended_muxer_upstream_msg_iters) {
1348 BT_COMP_LOGE_STR("Failed to allocate a GPtrArray.");
1349 goto error;
1350 }
1351
1352 ret = muxer_msg_iter_init_upstream_iterators(muxer_comp,
1353 muxer_msg_iter);
1354 if (ret) {
1355 BT_COMP_LOGE("Cannot initialize connected input ports for muxer component's message iterator: "
1356 "comp-addr=%p, muxer-comp-addr=%p, "
1357 "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d",
1358 self_comp, muxer_comp, muxer_msg_iter,
1359 self_msg_iter, ret);
1360 goto error;
1361 }
1362
1363 bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter);
1364 BT_COMP_LOGD("Initialized muxer component's message iterator: "
1365 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1366 "msg-iter-addr=%p",
1367 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
1368 goto end;
1369
1370 error:
1371 destroy_muxer_msg_iter(muxer_msg_iter);
1372 bt_self_message_iterator_set_data(self_msg_iter, NULL);
1373 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
1374
1375 end:
1376 muxer_comp->initializing_muxer_msg_iter = false;
1377 return status;
1378 }
1379
1380 BT_HIDDEN
1381 void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
1382 {
1383 struct muxer_msg_iter *muxer_msg_iter =
1384 bt_self_message_iterator_get_data(self_msg_iter);
1385 bt_self_component *self_comp = NULL;
1386 struct muxer_comp *muxer_comp = NULL;
1387
1388 self_comp = bt_self_message_iterator_borrow_component(
1389 self_msg_iter);
1390 BT_ASSERT(self_comp);
1391 muxer_comp = bt_self_component_get_data(self_comp);
1392 BT_COMP_LOGD("Finalizing muxer component's message iterator: "
1393 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1394 "msg-iter-addr=%p",
1395 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
1396
1397 if (muxer_msg_iter) {
1398 destroy_muxer_msg_iter(muxer_msg_iter);
1399 }
1400 }
1401
1402 BT_HIDDEN
1403 bt_component_class_message_iterator_next_method_status muxer_msg_iter_next(
1404 bt_self_message_iterator *self_msg_iter,
1405 bt_message_array_const msgs, uint64_t capacity,
1406 uint64_t *count)
1407 {
1408 bt_component_class_message_iterator_next_method_status status;
1409 struct muxer_msg_iter *muxer_msg_iter =
1410 bt_self_message_iterator_get_data(self_msg_iter);
1411 bt_self_component *self_comp = NULL;
1412 struct muxer_comp *muxer_comp = NULL;
1413
1414 BT_ASSERT(muxer_msg_iter);
1415 self_comp = bt_self_message_iterator_borrow_component(
1416 self_msg_iter);
1417 BT_ASSERT(self_comp);
1418 muxer_comp = bt_self_component_get_data(self_comp);
1419 BT_ASSERT(muxer_comp);
1420 BT_COMP_LOGT("Muxer component's message iterator's \"next\" method called: "
1421 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1422 "msg-iter-addr=%p",
1423 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter);
1424
1425 status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter,
1426 msgs, capacity, count);
1427 if (status < 0) {
1428 BT_COMP_LOGE("Cannot get next message: "
1429 "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, "
1430 "msg-iter-addr=%p, status=%s",
1431 self_comp, muxer_comp, muxer_msg_iter, self_msg_iter,
1432 bt_common_func_status_string(status));
1433 } else {
1434 BT_COMP_LOGT("Returning from muxer component's message iterator's \"next\" method: "
1435 "status=%s",
1436 bt_common_func_status_string(status));
1437 }
1438
1439 return status;
1440 }
1441
1442 BT_HIDDEN
1443 bt_component_class_port_connected_method_status muxer_input_port_connected(
1444 bt_self_component_filter *self_comp,
1445 bt_self_component_port_input *self_port,
1446 const bt_port_output *other_port)
1447 {
1448 bt_component_class_port_connected_method_status status =
1449 BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
1450 bt_self_component_add_port_status add_port_status;
1451 struct muxer_comp *muxer_comp = bt_self_component_get_data(
1452 bt_self_component_filter_as_self_component(self_comp));
1453
1454 add_port_status = add_available_input_port(self_comp);
1455 if (add_port_status) {
1456 BT_COMP_LOGE("Cannot add one muxer component's input port: "
1457 "status=%s",
1458 bt_common_func_status_string(status));
1459
1460 if (add_port_status ==
1461 BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR) {
1462 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_MEMORY_ERROR;
1463 } else {
1464 status = BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
1465 }
1466
1467 goto end;
1468 }
1469
1470 end:
1471 return status;
1472 }
1473
1474 static inline
1475 bt_bool muxer_upstream_msg_iters_can_all_seek_beginning(
1476 GPtrArray *muxer_upstream_msg_iters)
1477 {
1478 uint64_t i;
1479 bt_bool ret = BT_TRUE;
1480
1481 for (i = 0; i < muxer_upstream_msg_iters->len; i++) {
1482 struct muxer_upstream_msg_iter *upstream_msg_iter =
1483 muxer_upstream_msg_iters->pdata[i];
1484
1485 if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
1486 upstream_msg_iter->msg_iter)) {
1487 ret = BT_FALSE;
1488 goto end;
1489 }
1490 }
1491
1492 end:
1493 return ret;
1494 }
1495
1496 BT_HIDDEN
1497 bt_bool muxer_msg_iter_can_seek_beginning(
1498 bt_self_message_iterator *self_msg_iter)
1499 {
1500 struct muxer_msg_iter *muxer_msg_iter =
1501 bt_self_message_iterator_get_data(self_msg_iter);
1502 bt_bool ret = BT_TRUE;
1503
1504 if (!muxer_upstream_msg_iters_can_all_seek_beginning(
1505 muxer_msg_iter->active_muxer_upstream_msg_iters)) {
1506 ret = BT_FALSE;
1507 goto end;
1508 }
1509
1510 if (!muxer_upstream_msg_iters_can_all_seek_beginning(
1511 muxer_msg_iter->ended_muxer_upstream_msg_iters)) {
1512 ret = BT_FALSE;
1513 goto end;
1514 }
1515
1516 end:
1517 return ret;
1518 }
1519
1520 BT_HIDDEN
1521 bt_component_class_message_iterator_seek_beginning_method_status muxer_msg_iter_seek_beginning(
1522 bt_self_message_iterator *self_msg_iter)
1523 {
1524 struct muxer_msg_iter *muxer_msg_iter =
1525 bt_self_message_iterator_get_data(self_msg_iter);
1526 bt_component_class_message_iterator_seek_beginning_method_status status =
1527 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
1528 bt_message_iterator_seek_beginning_status seek_beg_status;
1529 uint64_t i;
1530
1531 /* Seek all ended upstream iterators first */
1532 for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
1533 i++) {
1534 struct muxer_upstream_msg_iter *upstream_msg_iter =
1535 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
1536
1537 seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning(
1538 upstream_msg_iter->msg_iter);
1539 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
1540 status = (int) seek_beg_status;
1541 goto end;
1542 }
1543
1544 empty_message_queue(upstream_msg_iter);
1545 }
1546
1547 /* Seek all previously active upstream iterators */
1548 for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len;
1549 i++) {
1550 struct muxer_upstream_msg_iter *upstream_msg_iter =
1551 muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i];
1552
1553 seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning(
1554 upstream_msg_iter->msg_iter);
1555 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
1556 status = (int) seek_beg_status;
1557 goto end;
1558 }
1559
1560 empty_message_queue(upstream_msg_iter);
1561 }
1562
1563 /* Make them all active */
1564 for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len;
1565 i++) {
1566 struct muxer_upstream_msg_iter *upstream_msg_iter =
1567 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i];
1568
1569 g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters,
1570 upstream_msg_iter);
1571 muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL;
1572 }
1573
1574 /*
1575 * GLib < 2.48.0 asserts when g_ptr_array_remove_range() is
1576 * called on an empty array.
1577 */
1578 if (muxer_msg_iter->ended_muxer_upstream_msg_iters->len > 0) {
1579 g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters,
1580 0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len);
1581 }
1582 muxer_msg_iter->last_returned_ts_ns = INT64_MIN;
1583 muxer_msg_iter->clock_class_expectation =
1584 MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY;
1585
1586 end:
1587 return status;
1588 }
This page took 0.093978 seconds and 4 git commands to generate.