lib: make trace IR API const-correct
[babeltrace.git] / 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_LOG_TAG "PLUGIN-UTILS-MUXER-FLT"
24 #include "logging.h"
25
26 #include <babeltrace/babeltrace-internal.h>
27 #include <babeltrace/compat/uuid-internal.h>
28 #include <babeltrace/babeltrace.h>
29 #include <babeltrace/values-internal.h>
30 #include <babeltrace/graph/component-internal.h>
31 #include <babeltrace/graph/notification-iterator-internal.h>
32 #include <babeltrace/graph/connection-internal.h>
33 #include <plugins-common.h>
34 #include <glib.h>
35 #include <stdbool.h>
36 #include <inttypes.h>
37 #include <babeltrace/assert-internal.h>
38 #include <babeltrace/common-internal.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes"
43
44 struct muxer_comp {
45 /*
46 * Array of struct
47 * bt_self_notification_iterator *
48 * (weak refs)
49 */
50 GPtrArray *muxer_notif_iters;
51
52 /* Weak ref */
53 struct bt_self_component_filter *self_comp;
54
55 unsigned int next_port_num;
56 size_t available_input_ports;
57 bool initializing_muxer_notif_iter;
58 bool assume_absolute_clock_classes;
59 };
60
61 struct muxer_upstream_notif_iter {
62 /* Owned by this, NULL if ended */
63 struct bt_self_component_port_input_notification_iterator *notif_iter;
64
65 /* Contains `struct bt_notification *`, owned by this */
66 GQueue *notifs;
67 };
68
69 enum muxer_notif_iter_clock_class_expectation {
70 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0,
71 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE,
72 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID,
73 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID,
74 };
75
76 struct muxer_notif_iter {
77 /*
78 * Array of struct muxer_upstream_notif_iter * (owned by this).
79 *
80 * NOTE: This array is searched in linearly to find the youngest
81 * current notification. Keep this until benchmarks confirm that
82 * another data structure is faster than this for our typical
83 * use cases.
84 */
85 GPtrArray *muxer_upstream_notif_iters;
86
87 /*
88 * List of "recently" connected input ports (weak) to
89 * handle by this muxer notification iterator.
90 * muxer_port_connected() adds entries to this list, and the
91 * entries are removed when a notification iterator is created
92 * on the port's connection and put into
93 * muxer_upstream_notif_iters above by
94 * muxer_notif_iter_handle_newly_connected_ports().
95 */
96 GList *newly_connected_self_ports;
97
98 /* Last time returned in a notification */
99 int64_t last_returned_ts_ns;
100
101 /* Clock class expectation state */
102 enum muxer_notif_iter_clock_class_expectation clock_class_expectation;
103
104 /*
105 * Expected clock class UUID, only valid when
106 * clock_class_expectation is
107 * MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
108 */
109 unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN];
110 };
111
112 static
113 void destroy_muxer_upstream_notif_iter(
114 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
115 {
116 if (!muxer_upstream_notif_iter) {
117 return;
118 }
119
120 BT_LOGD("Destroying muxer's upstream notification iterator wrapper: "
121 "addr=%p, notif-iter-addr=%p, queue-len=%u",
122 muxer_upstream_notif_iter,
123 muxer_upstream_notif_iter->notif_iter,
124 muxer_upstream_notif_iter->notifs->length);
125 bt_object_put_ref(muxer_upstream_notif_iter->notif_iter);
126
127 if (muxer_upstream_notif_iter->notifs) {
128 struct bt_notification *notif;
129
130 while ((notif = g_queue_pop_head(
131 muxer_upstream_notif_iter->notifs))) {
132 bt_object_put_ref(notif);
133 }
134
135 g_queue_free(muxer_upstream_notif_iter->notifs);
136 }
137
138 g_free(muxer_upstream_notif_iter);
139 }
140
141 static
142 struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter(
143 struct muxer_notif_iter *muxer_notif_iter,
144 struct bt_self_component_port_input_notification_iterator *self_notif_iter)
145 {
146 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
147 g_new0(struct muxer_upstream_notif_iter, 1);
148
149 if (!muxer_upstream_notif_iter) {
150 BT_LOGE_STR("Failed to allocate one muxer's upstream notification iterator wrapper.");
151 goto end;
152 }
153
154 muxer_upstream_notif_iter->notif_iter = self_notif_iter;
155 bt_object_get_ref(muxer_upstream_notif_iter->notif_iter);
156 muxer_upstream_notif_iter->notifs = g_queue_new();
157 if (!muxer_upstream_notif_iter->notifs) {
158 BT_LOGE_STR("Failed to allocate a GQueue.");
159 goto end;
160 }
161
162 g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters,
163 muxer_upstream_notif_iter);
164 BT_LOGD("Added muxer's upstream notification iterator wrapper: "
165 "addr=%p, muxer-notif-iter-addr=%p, notif-iter-addr=%p",
166 muxer_upstream_notif_iter, muxer_notif_iter,
167 self_notif_iter);
168
169 end:
170 return muxer_upstream_notif_iter;
171 }
172
173 static
174 enum bt_self_component_status ensure_available_input_port(
175 struct bt_self_component_filter *self_comp)
176 {
177 struct muxer_comp *muxer_comp = bt_self_component_get_data(
178 bt_self_component_filter_as_self_component(self_comp));
179 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
180 GString *port_name = NULL;
181
182 BT_ASSERT(muxer_comp);
183
184 if (muxer_comp->available_input_ports >= 1) {
185 goto end;
186 }
187
188 port_name = g_string_new("in");
189 if (!port_name) {
190 BT_LOGE_STR("Failed to allocate a GString.");
191 status = BT_SELF_COMPONENT_STATUS_NOMEM;
192 goto end;
193 }
194
195 g_string_append_printf(port_name, "%u", muxer_comp->next_port_num);
196 status = bt_self_component_filter_add_input_port(
197 self_comp, port_name->str, NULL, NULL);
198 if (status != BT_SELF_COMPONENT_STATUS_OK) {
199 BT_LOGE("Cannot add input port to muxer component: "
200 "port-name=\"%s\", comp-addr=%p, status=%s",
201 port_name->str, self_comp,
202 bt_self_component_status_string(status));
203 goto end;
204 }
205
206 muxer_comp->available_input_ports++;
207 muxer_comp->next_port_num++;
208 BT_LOGD("Added one input port to muxer component: "
209 "port-name=\"%s\", comp-addr=%p",
210 port_name->str, self_comp);
211 end:
212 if (port_name) {
213 g_string_free(port_name, TRUE);
214 }
215
216 return status;
217 }
218
219 static
220 enum bt_self_component_status create_output_port(
221 struct bt_self_component_filter *self_comp)
222 {
223 return bt_self_component_filter_add_output_port(
224 self_comp, "out", NULL, NULL);
225 }
226
227 static
228 void destroy_muxer_comp(struct muxer_comp *muxer_comp)
229 {
230 if (!muxer_comp) {
231 return;
232 }
233
234 BT_LOGD("Destroying muxer component: muxer-comp-addr=%p, "
235 "muxer-notif-iter-count=%u", muxer_comp,
236 muxer_comp->muxer_notif_iters ?
237 muxer_comp->muxer_notif_iters->len : 0);
238
239 if (muxer_comp->muxer_notif_iters) {
240 g_ptr_array_free(muxer_comp->muxer_notif_iters, TRUE);
241 }
242
243 g_free(muxer_comp);
244 }
245
246 static
247 struct bt_value *get_default_params(void)
248 {
249 struct bt_value *params;
250 int ret;
251
252 params = bt_value_map_create();
253 if (!params) {
254 BT_LOGE_STR("Cannot create a map value object.");
255 goto error;
256 }
257
258 ret = bt_value_map_insert_bool_entry(params,
259 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false);
260 if (ret) {
261 BT_LOGE_STR("Cannot add boolean value to map value object.");
262 goto error;
263 }
264
265 goto end;
266
267 error:
268 BT_OBJECT_PUT_REF_AND_RESET(params);
269
270 end:
271 return params;
272 }
273
274 static
275 int configure_muxer_comp(struct muxer_comp *muxer_comp,
276 const struct bt_value *params)
277 {
278 struct bt_value *default_params = NULL;
279 struct bt_value *real_params = NULL;
280 const struct bt_value *assume_absolute_clock_classes = NULL;
281 int ret = 0;
282 bt_bool bool_val;
283
284 default_params = get_default_params();
285 if (!default_params) {
286 BT_LOGE("Cannot get default parameters: "
287 "muxer-comp-addr=%p", muxer_comp);
288 goto error;
289 }
290
291 ret = bt_value_map_extend(&real_params,
292 default_params, params);
293 if (ret) {
294 BT_LOGE("Cannot extend default parameters map value: "
295 "muxer-comp-addr=%p, def-params-addr=%p, "
296 "params-addr=%p", muxer_comp, default_params,
297 params);
298 goto error;
299 }
300
301 assume_absolute_clock_classes = bt_value_map_borrow_entry_value(real_params,
302 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME);
303 if (assume_absolute_clock_classes &&
304 !bt_value_is_bool(assume_absolute_clock_classes)) {
305 BT_LOGE("Expecting a boolean value for the `%s` parameter: "
306 "muxer-comp-addr=%p, value-type=%s",
307 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp,
308 bt_common_value_type_string(
309 bt_value_get_type(assume_absolute_clock_classes)));
310 goto error;
311 }
312
313 bool_val = bt_value_bool_get(assume_absolute_clock_classes);
314 muxer_comp->assume_absolute_clock_classes = (bool) bool_val;
315 BT_LOGD("Configured muxer component: muxer-comp-addr=%p, "
316 "assume-absolute-clock-classes=%d",
317 muxer_comp, muxer_comp->assume_absolute_clock_classes);
318 goto end;
319
320 error:
321 ret = -1;
322
323 end:
324 bt_object_put_ref(default_params);
325 bt_object_put_ref(real_params);
326 return ret;
327 }
328
329 BT_HIDDEN
330 enum bt_self_component_status muxer_init(
331 struct bt_self_component_filter *self_comp,
332 struct bt_value *params, void *init_data)
333 {
334 int ret;
335 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
336 struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1);
337
338 BT_LOGD("Initializing muxer component: "
339 "comp-addr=%p, params-addr=%p", self_comp, params);
340
341 if (!muxer_comp) {
342 BT_LOGE_STR("Failed to allocate one muxer component.");
343 goto error;
344 }
345
346 ret = configure_muxer_comp(muxer_comp, params);
347 if (ret) {
348 BT_LOGE("Cannot configure muxer component: "
349 "muxer-comp-addr=%p, params-addr=%p",
350 muxer_comp, params);
351 goto error;
352 }
353
354 muxer_comp->muxer_notif_iters = g_ptr_array_new();
355 if (!muxer_comp->muxer_notif_iters) {
356 BT_LOGE_STR("Failed to allocate a GPtrArray.");
357 goto error;
358 }
359
360 muxer_comp->self_comp = self_comp;
361 bt_self_component_set_data(
362 bt_self_component_filter_as_self_component(self_comp),
363 muxer_comp);
364 status = ensure_available_input_port(self_comp);
365 if (status != BT_SELF_COMPONENT_STATUS_OK) {
366 BT_LOGE("Cannot ensure that at least one muxer component's input port is available: "
367 "muxer-comp-addr=%p, status=%s",
368 muxer_comp,
369 bt_self_component_status_string(status));
370 goto error;
371 }
372
373 status = create_output_port(self_comp);
374 if (status) {
375 BT_LOGE("Cannot create muxer component's output port: "
376 "muxer-comp-addr=%p, status=%s",
377 muxer_comp,
378 bt_self_component_status_string(status));
379 goto error;
380 }
381
382 BT_LOGD("Initialized muxer component: "
383 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
384 self_comp, params, muxer_comp);
385
386 goto end;
387
388 error:
389 destroy_muxer_comp(muxer_comp);
390 bt_self_component_set_data(
391 bt_self_component_filter_as_self_component(self_comp),
392 NULL);
393
394 if (status == BT_SELF_COMPONENT_STATUS_OK) {
395 status = BT_SELF_COMPONENT_STATUS_ERROR;
396 }
397
398 end:
399 return status;
400 }
401
402 BT_HIDDEN
403 void muxer_finalize(struct bt_self_component_filter *self_comp)
404 {
405 struct muxer_comp *muxer_comp = bt_self_component_get_data(
406 bt_self_component_filter_as_self_component(self_comp));
407
408 BT_LOGD("Finalizing muxer component: comp-addr=%p",
409 self_comp);
410 destroy_muxer_comp(muxer_comp);
411 }
412
413 static
414 struct bt_self_component_port_input_notification_iterator *
415 create_notif_iter_on_input_port(
416 struct bt_self_component_port_input *self_port, int *ret)
417 {
418 struct bt_port *port = bt_self_component_port_as_port(
419 bt_self_component_port_input_as_self_component_port(
420 self_port));
421 struct bt_self_component_port_input_notification_iterator *notif_iter =
422 NULL;
423
424 BT_ASSERT(ret);
425 *ret = 0;
426 BT_ASSERT(port);
427 BT_ASSERT(bt_port_is_connected(port));
428
429 // TODO: Advance the iterator to >= the time of the latest
430 // returned notification by the muxer notification
431 // iterator which creates it.
432 notif_iter = bt_self_component_port_input_notification_iterator_create(
433 self_port);
434 if (!notif_iter) {
435 BT_LOGE("Cannot create upstream notification iterator on input port: "
436 "port-addr=%p, port-name=\"%s\"",
437 port, bt_port_get_name(port));
438 *ret = -1;
439 goto end;
440 }
441
442 BT_LOGD("Created upstream notification iterator on input port: "
443 "port-addr=%p, port-name=\"%s\", notif-iter-addr=%p",
444 port, bt_port_get_name(port), notif_iter);
445
446 end:
447 return notif_iter;
448 }
449
450 static
451 enum bt_notification_iterator_status muxer_upstream_notif_iter_next(
452 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
453 {
454 enum bt_notification_iterator_status status;
455 bt_notification_array notifs;
456 uint64_t i;
457 uint64_t count;
458
459 BT_LOGV("Calling upstream notification iterator's \"next\" method: "
460 "muxer-upstream-notif-iter-wrap-addr=%p, notif-iter-addr=%p",
461 muxer_upstream_notif_iter,
462 muxer_upstream_notif_iter->notif_iter);
463 status = bt_self_component_port_input_notification_iterator_next(
464 muxer_upstream_notif_iter->notif_iter, &notifs, &count);
465 BT_LOGV("Upstream notification iterator's \"next\" method returned: "
466 "status=%s", bt_notification_iterator_status_string(status));
467
468 switch (status) {
469 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
470 /*
471 * Notification iterator's current notification is
472 * valid: it must be considered for muxing operations.
473 */
474 BT_LOGV_STR("Validated upstream notification iterator wrapper.");
475 BT_ASSERT(count > 0);
476
477 /* Move notifications to our queue */
478 for (i = 0; i < count; i++) {
479 /*
480 * Push to tail in order; other side
481 * (muxer_notif_iter_do_next_one()) consumes
482 * from the head first.
483 */
484 g_queue_push_tail(muxer_upstream_notif_iter->notifs,
485 notifs[i]);
486 }
487 break;
488 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
489 /*
490 * Notification iterator's current notification is not
491 * valid anymore. Return
492 * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN immediately.
493 */
494 break;
495 case BT_NOTIFICATION_ITERATOR_STATUS_END: /* Fall-through. */
496 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
497 /*
498 * Notification iterator reached the end: release it. It
499 * won't be considered again to find the youngest
500 * notification.
501 */
502 BT_OBJECT_PUT_REF_AND_RESET(muxer_upstream_notif_iter->notif_iter);
503 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
504 break;
505 default:
506 /* Error or unsupported status code */
507 BT_LOGE("Error or unsupported status code: "
508 "status-code=%d", status);
509 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
510 break;
511 }
512
513 return status;
514 }
515
516 static
517 int muxer_notif_iter_handle_newly_connected_ports(
518 struct muxer_notif_iter *muxer_notif_iter)
519 {
520 int ret = 0;
521
522 BT_LOGV("Handling newly connected ports: "
523 "muxer-notif-iter-addr=%p", muxer_notif_iter);
524
525 /*
526 * Here we create one upstream notification iterator for each
527 * newly connected port. We do NOT perform an initial "next" on
528 * those new upstream notification iterators: they are
529 * invalidated, to be validated later. The list of newly
530 * connected ports to handle here is updated by
531 * muxer_port_connected().
532 */
533 while (true) {
534 GList *node = muxer_notif_iter->newly_connected_self_ports;
535 struct bt_self_component_port_input *self_port;
536 struct bt_port *port;
537 struct bt_self_component_port_input_notification_iterator *
538 upstream_notif_iter = NULL;
539 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter;
540
541 if (!node) {
542 break;
543 }
544
545 self_port = node->data;
546 port = bt_self_component_port_as_port(
547 bt_self_component_port_input_as_self_component_port(
548 (self_port)));
549 BT_ASSERT(port);
550
551 if (!bt_port_is_connected(port)) {
552 /*
553 * Looks like this port is not connected
554 * anymore: we can't create an upstream
555 * notification iterator on its (non-existing)
556 * connection in this case.
557 */
558 goto remove_node;
559 }
560
561 upstream_notif_iter = create_notif_iter_on_input_port(
562 self_port, &ret);
563 if (ret) {
564 /* create_notif_iter_on_input_port() logs errors */
565 BT_ASSERT(!upstream_notif_iter);
566 goto error;
567 }
568
569 muxer_upstream_notif_iter =
570 muxer_notif_iter_add_upstream_notif_iter(
571 muxer_notif_iter, upstream_notif_iter);
572 BT_OBJECT_PUT_REF_AND_RESET(upstream_notif_iter);
573 if (!muxer_upstream_notif_iter) {
574 /*
575 * muxer_notif_iter_add_upstream_notif_iter()
576 * logs errors.
577 */
578 goto error;
579 }
580
581 remove_node:
582 bt_object_put_ref(upstream_notif_iter);
583 muxer_notif_iter->newly_connected_self_ports =
584 g_list_delete_link(
585 muxer_notif_iter->newly_connected_self_ports,
586 node);
587 }
588
589 goto end;
590
591 error:
592 if (ret >= 0) {
593 ret = -1;
594 }
595
596 end:
597 return ret;
598 }
599
600 static
601 int get_notif_ts_ns(struct muxer_comp *muxer_comp,
602 struct muxer_notif_iter *muxer_notif_iter,
603 struct bt_notification *notif, int64_t last_returned_ts_ns,
604 int64_t *ts_ns)
605 {
606 const struct bt_clock_class *clock_class = NULL;
607 const struct bt_clock_value *clock_value = NULL;
608 const struct bt_event *event = NULL;
609 int ret = 0;
610 const unsigned char *cc_uuid;
611 const char *cc_name;
612 enum bt_clock_value_status cv_status = BT_CLOCK_VALUE_STATUS_KNOWN;
613
614 BT_ASSERT(notif);
615 BT_ASSERT(ts_ns);
616
617 BT_LOGV("Getting notification's timestamp: "
618 "muxer-notif-iter-addr=%p, notif-addr=%p, "
619 "last-returned-ts=%" PRId64,
620 muxer_notif_iter, notif, last_returned_ts_ns);
621
622 switch (bt_notification_get_type(notif)) {
623 case BT_NOTIFICATION_TYPE_EVENT:
624 event = bt_notification_event_borrow_event(notif);
625 BT_ASSERT(event);
626 cv_status = bt_event_borrow_default_clock_value_const(event,
627 &clock_value);
628 break;
629
630 case BT_NOTIFICATION_TYPE_INACTIVITY:
631 clock_value =
632 bt_notification_inactivity_borrow_default_clock_value(
633 notif);
634 break;
635 default:
636 /* All the other notifications have a higher priority */
637 BT_LOGV_STR("Notification has no timestamp: using the last returned timestamp.");
638 *ts_ns = last_returned_ts_ns;
639 goto end;
640 }
641
642 if (cv_status != BT_CLOCK_VALUE_STATUS_KNOWN) {
643 BT_LOGE_STR("Unsupported unknown clock value.");
644 ret = -1;
645 goto end;
646 }
647
648 /*
649 * If the clock value is missing, then we consider that this
650 * notification has no time. In this case it's always the
651 * youngest.
652 */
653 if (!clock_value) {
654 BT_LOGV_STR("Notification's default clock value is missing: "
655 "using the last returned timestamp.");
656 *ts_ns = last_returned_ts_ns;
657 goto end;
658 }
659
660 clock_class = bt_clock_value_borrow_clock_class_const(clock_value);
661 BT_ASSERT(clock_class);
662 cc_uuid = bt_clock_class_get_uuid(clock_class);
663 cc_name = bt_clock_class_get_name(clock_class);
664
665 if (muxer_notif_iter->clock_class_expectation ==
666 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
667 /*
668 * This is the first clock class that this muxer
669 * notification iterator encounters. Its properties
670 * determine what to expect for the whole lifetime of
671 * the iterator without a true
672 * `assume-absolute-clock-classes` parameter.
673 */
674 if (bt_clock_class_is_absolute(clock_class)) {
675 /* Expect absolute clock classes */
676 muxer_notif_iter->clock_class_expectation =
677 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE;
678 } else {
679 if (cc_uuid) {
680 /*
681 * Expect non-absolute clock classes
682 * with a specific UUID.
683 */
684 muxer_notif_iter->clock_class_expectation =
685 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID;
686 memcpy(muxer_notif_iter->expected_clock_class_uuid,
687 cc_uuid, BABELTRACE_UUID_LEN);
688 } else {
689 /*
690 * Expect non-absolute clock classes
691 * with no UUID.
692 */
693 muxer_notif_iter->clock_class_expectation =
694 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID;
695 }
696 }
697 }
698
699 if (!muxer_comp->assume_absolute_clock_classes) {
700 switch (muxer_notif_iter->clock_class_expectation) {
701 case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE:
702 if (!bt_clock_class_is_absolute(clock_class)) {
703 BT_LOGE("Expecting an absolute clock class, "
704 "but got a non-absolute one: "
705 "clock-class-addr=%p, clock-class-name=\"%s\"",
706 clock_class, cc_name);
707 goto error;
708 }
709 break;
710 case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID:
711 if (bt_clock_class_is_absolute(clock_class)) {
712 BT_LOGE("Expecting a non-absolute clock class with no UUID, "
713 "but got an absolute one: "
714 "clock-class-addr=%p, clock-class-name=\"%s\"",
715 clock_class, cc_name);
716 goto error;
717 }
718
719 if (cc_uuid) {
720 BT_LOGE("Expecting a non-absolute clock class with no UUID, "
721 "but got one with a UUID: "
722 "clock-class-addr=%p, clock-class-name=\"%s\", "
723 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
724 clock_class, cc_name,
725 (unsigned int) cc_uuid[0],
726 (unsigned int) cc_uuid[1],
727 (unsigned int) cc_uuid[2],
728 (unsigned int) cc_uuid[3],
729 (unsigned int) cc_uuid[4],
730 (unsigned int) cc_uuid[5],
731 (unsigned int) cc_uuid[6],
732 (unsigned int) cc_uuid[7],
733 (unsigned int) cc_uuid[8],
734 (unsigned int) cc_uuid[9],
735 (unsigned int) cc_uuid[10],
736 (unsigned int) cc_uuid[11],
737 (unsigned int) cc_uuid[12],
738 (unsigned int) cc_uuid[13],
739 (unsigned int) cc_uuid[14],
740 (unsigned int) cc_uuid[15]);
741 goto error;
742 }
743 break;
744 case MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID:
745 if (bt_clock_class_is_absolute(clock_class)) {
746 BT_LOGE("Expecting a non-absolute clock class with a specific UUID, "
747 "but got an absolute one: "
748 "clock-class-addr=%p, clock-class-name=\"%s\"",
749 clock_class, cc_name);
750 goto error;
751 }
752
753 if (!cc_uuid) {
754 BT_LOGE("Expecting a non-absolute clock class with a specific UUID, "
755 "but got one with no UUID: "
756 "clock-class-addr=%p, clock-class-name=\"%s\"",
757 clock_class, cc_name);
758 goto error;
759 }
760
761 if (memcmp(muxer_notif_iter->expected_clock_class_uuid,
762 cc_uuid, BABELTRACE_UUID_LEN) != 0) {
763 BT_LOGE("Expecting a non-absolute clock class with a specific UUID, "
764 "but got one with different UUID: "
765 "clock-class-addr=%p, clock-class-name=\"%s\", "
766 "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
767 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
768 clock_class, cc_name,
769 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[0],
770 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[1],
771 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[2],
772 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[3],
773 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[4],
774 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[5],
775 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[6],
776 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[7],
777 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[8],
778 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[9],
779 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[10],
780 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[11],
781 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[12],
782 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[13],
783 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[14],
784 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[15],
785 (unsigned int) cc_uuid[0],
786 (unsigned int) cc_uuid[1],
787 (unsigned int) cc_uuid[2],
788 (unsigned int) cc_uuid[3],
789 (unsigned int) cc_uuid[4],
790 (unsigned int) cc_uuid[5],
791 (unsigned int) cc_uuid[6],
792 (unsigned int) cc_uuid[7],
793 (unsigned int) cc_uuid[8],
794 (unsigned int) cc_uuid[9],
795 (unsigned int) cc_uuid[10],
796 (unsigned int) cc_uuid[11],
797 (unsigned int) cc_uuid[12],
798 (unsigned int) cc_uuid[13],
799 (unsigned int) cc_uuid[14],
800 (unsigned int) cc_uuid[15]);
801 goto error;
802 }
803 break;
804 default:
805 /* Unexpected */
806 BT_LOGF("Unexpected clock class expectation: "
807 "expectation-code=%d",
808 muxer_notif_iter->clock_class_expectation);
809 abort();
810 }
811 }
812
813 ret = bt_clock_value_get_ns_from_origin(clock_value, ts_ns);
814 if (ret) {
815 BT_LOGE("Cannot get nanoseconds from Epoch of clock value: "
816 "clock-value-addr=%p", clock_value);
817 goto error;
818 }
819
820 goto end;
821
822 error:
823 ret = -1;
824
825 end:
826 if (ret == 0) {
827 BT_LOGV("Found notification's timestamp: "
828 "muxer-notif-iter-addr=%p, notif-addr=%p, "
829 "last-returned-ts=%" PRId64 ", ts=%" PRId64,
830 muxer_notif_iter, notif, last_returned_ts_ns,
831 *ts_ns);
832 }
833
834 return ret;
835 }
836
837 /*
838 * This function finds the youngest available notification amongst the
839 * non-ended upstream notification iterators and returns the upstream
840 * notification iterator which has it, or
841 * BT_NOTIFICATION_ITERATOR_STATUS_END if there's no available
842 * notification.
843 *
844 * This function does NOT:
845 *
846 * * Update any upstream notification iterator.
847 * * Check for newly connected ports.
848 * * Check the upstream notification iterators to retry.
849 *
850 * On sucess, this function sets *muxer_upstream_notif_iter to the
851 * upstream notification iterator of which the current notification is
852 * the youngest, and sets *ts_ns to its time.
853 */
854 static
855 enum bt_notification_iterator_status
856 muxer_notif_iter_youngest_upstream_notif_iter(
857 struct muxer_comp *muxer_comp,
858 struct muxer_notif_iter *muxer_notif_iter,
859 struct muxer_upstream_notif_iter **muxer_upstream_notif_iter,
860 int64_t *ts_ns)
861 {
862 size_t i;
863 int ret;
864 int64_t youngest_ts_ns = INT64_MAX;
865 enum bt_notification_iterator_status status =
866 BT_NOTIFICATION_ITERATOR_STATUS_OK;
867
868 BT_ASSERT(muxer_comp);
869 BT_ASSERT(muxer_notif_iter);
870 BT_ASSERT(muxer_upstream_notif_iter);
871 *muxer_upstream_notif_iter = NULL;
872
873 for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
874 struct bt_notification *notif;
875 struct muxer_upstream_notif_iter *cur_muxer_upstream_notif_iter =
876 g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters, i);
877 int64_t notif_ts_ns;
878
879 if (!cur_muxer_upstream_notif_iter->notif_iter) {
880 /* This upstream notification iterator is ended */
881 BT_LOGV("Skipping ended upstream notification iterator: "
882 "muxer-upstream-notif-iter-wrap-addr=%p",
883 cur_muxer_upstream_notif_iter);
884 continue;
885 }
886
887 BT_ASSERT(cur_muxer_upstream_notif_iter->notifs->length > 0);
888 notif = g_queue_peek_head(cur_muxer_upstream_notif_iter->notifs);
889 BT_ASSERT(notif);
890 ret = get_notif_ts_ns(muxer_comp, muxer_notif_iter, notif,
891 muxer_notif_iter->last_returned_ts_ns, &notif_ts_ns);
892 if (ret) {
893 /* get_notif_ts_ns() logs errors */
894 *muxer_upstream_notif_iter = NULL;
895 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
896 goto end;
897 }
898
899 if (notif_ts_ns <= youngest_ts_ns) {
900 *muxer_upstream_notif_iter =
901 cur_muxer_upstream_notif_iter;
902 youngest_ts_ns = notif_ts_ns;
903 *ts_ns = youngest_ts_ns;
904 }
905 }
906
907 if (!*muxer_upstream_notif_iter) {
908 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
909 *ts_ns = INT64_MIN;
910 }
911
912 end:
913 return status;
914 }
915
916 static
917 enum bt_notification_iterator_status validate_muxer_upstream_notif_iter(
918 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
919 {
920 enum bt_notification_iterator_status status =
921 BT_NOTIFICATION_ITERATOR_STATUS_OK;
922
923 BT_LOGV("Validating muxer's upstream notification iterator wrapper: "
924 "muxer-upstream-notif-iter-wrap-addr=%p",
925 muxer_upstream_notif_iter);
926
927 if (muxer_upstream_notif_iter->notifs->length > 0 ||
928 !muxer_upstream_notif_iter->notif_iter) {
929 BT_LOGV("Already valid or not considered: "
930 "queue-len=%u, upstream-notif-iter-addr=%p",
931 muxer_upstream_notif_iter->notifs->length,
932 muxer_upstream_notif_iter->notif_iter);
933 goto end;
934 }
935
936 /* muxer_upstream_notif_iter_next() logs details/errors */
937 status = muxer_upstream_notif_iter_next(muxer_upstream_notif_iter);
938
939 end:
940 return status;
941 }
942
943 static
944 enum bt_notification_iterator_status validate_muxer_upstream_notif_iters(
945 struct muxer_notif_iter *muxer_notif_iter)
946 {
947 enum bt_notification_iterator_status status =
948 BT_NOTIFICATION_ITERATOR_STATUS_OK;
949 size_t i;
950
951 BT_LOGV("Validating muxer's upstream notification iterator wrappers: "
952 "muxer-notif-iter-addr=%p", muxer_notif_iter);
953
954 for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
955 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
956 g_ptr_array_index(
957 muxer_notif_iter->muxer_upstream_notif_iters,
958 i);
959
960 status = validate_muxer_upstream_notif_iter(
961 muxer_upstream_notif_iter);
962 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
963 if (status < 0) {
964 BT_LOGE("Cannot validate muxer's upstream notification iterator wrapper: "
965 "muxer-notif-iter-addr=%p, "
966 "muxer-upstream-notif-iter-wrap-addr=%p",
967 muxer_notif_iter,
968 muxer_upstream_notif_iter);
969 } else {
970 BT_LOGV("Cannot validate muxer's upstream notification iterator wrapper: "
971 "muxer-notif-iter-addr=%p, "
972 "muxer-upstream-notif-iter-wrap-addr=%p",
973 muxer_notif_iter,
974 muxer_upstream_notif_iter);
975 }
976
977 goto end;
978 }
979
980 /*
981 * Remove this muxer upstream notification iterator
982 * if it's ended or canceled.
983 */
984 if (!muxer_upstream_notif_iter->notif_iter) {
985 /*
986 * Use g_ptr_array_remove_fast() because the
987 * order of those elements is not important.
988 */
989 BT_LOGV("Removing muxer's upstream notification iterator wrapper: ended or canceled: "
990 "muxer-notif-iter-addr=%p, "
991 "muxer-upstream-notif-iter-wrap-addr=%p",
992 muxer_notif_iter, muxer_upstream_notif_iter);
993 g_ptr_array_remove_index_fast(
994 muxer_notif_iter->muxer_upstream_notif_iters,
995 i);
996 i--;
997 }
998 }
999
1000 end:
1001 return status;
1002 }
1003
1004 static inline
1005 enum bt_notification_iterator_status muxer_notif_iter_do_next_one(
1006 struct muxer_comp *muxer_comp,
1007 struct muxer_notif_iter *muxer_notif_iter,
1008 struct bt_notification **notif)
1009 {
1010 enum bt_notification_iterator_status status =
1011 BT_NOTIFICATION_ITERATOR_STATUS_OK;
1012 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = NULL;
1013 int64_t next_return_ts;
1014
1015 while (true) {
1016 int ret = muxer_notif_iter_handle_newly_connected_ports(
1017 muxer_notif_iter);
1018
1019 if (ret) {
1020 BT_LOGE("Cannot handle newly connected input ports for muxer's notification iterator: "
1021 "muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1022 "ret=%d",
1023 muxer_comp, muxer_notif_iter, ret);
1024 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1025 goto end;
1026 }
1027
1028 status = validate_muxer_upstream_notif_iters(muxer_notif_iter);
1029 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
1030 /* validate_muxer_upstream_notif_iters() logs details */
1031 goto end;
1032 }
1033
1034 /*
1035 * At this point, we know that all the existing upstream
1036 * notification iterators are valid. However the
1037 * operations to validate them (during
1038 * validate_muxer_upstream_notif_iters()) may have
1039 * connected new ports. If no ports were connected
1040 * during this operation, exit the loop.
1041 */
1042 if (!muxer_notif_iter->newly_connected_self_ports) {
1043 BT_LOGV("Not breaking this loop: muxer's notification iterator still has newly connected input ports to handle: "
1044 "muxer-comp-addr=%p", muxer_comp);
1045 break;
1046 }
1047 }
1048
1049 BT_ASSERT(!muxer_notif_iter->newly_connected_self_ports);
1050
1051 /*
1052 * At this point we know that all the existing upstream
1053 * notification iterators are valid. We can find the one,
1054 * amongst those, of which the current notification is the
1055 * youngest.
1056 */
1057 status = muxer_notif_iter_youngest_upstream_notif_iter(muxer_comp,
1058 muxer_notif_iter, &muxer_upstream_notif_iter,
1059 &next_return_ts);
1060 if (status < 0 || status == BT_NOTIFICATION_ITERATOR_STATUS_END ||
1061 status == BT_NOTIFICATION_ITERATOR_STATUS_CANCELED) {
1062 if (status < 0) {
1063 BT_LOGE("Cannot find the youngest upstream notification iterator wrapper: "
1064 "status=%s",
1065 bt_notification_iterator_status_string(status));
1066 } else {
1067 BT_LOGV("Cannot find the youngest upstream notification iterator wrapper: "
1068 "status=%s",
1069 bt_notification_iterator_status_string(status));
1070 }
1071
1072 goto end;
1073 }
1074
1075 if (next_return_ts < muxer_notif_iter->last_returned_ts_ns) {
1076 BT_LOGE("Youngest upstream notification iterator wrapper's timestamp is less than muxer's notification iterator's last returned timestamp: "
1077 "muxer-notif-iter-addr=%p, ts=%" PRId64 ", "
1078 "last-returned-ts=%" PRId64,
1079 muxer_notif_iter, next_return_ts,
1080 muxer_notif_iter->last_returned_ts_ns);
1081 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1082 goto end;
1083 }
1084
1085 BT_LOGV("Found youngest upstream notification iterator wrapper: "
1086 "muxer-notif-iter-addr=%p, "
1087 "muxer-upstream-notif-iter-wrap-addr=%p, "
1088 "ts=%" PRId64,
1089 muxer_notif_iter, muxer_upstream_notif_iter, next_return_ts);
1090 BT_ASSERT(status == BT_NOTIFICATION_ITERATOR_STATUS_OK);
1091 BT_ASSERT(muxer_upstream_notif_iter);
1092
1093 /*
1094 * Consume from the queue's head: other side
1095 * (muxer_upstream_notif_iter_next()) writes to the tail.
1096 */
1097 *notif = g_queue_pop_head(muxer_upstream_notif_iter->notifs);
1098 BT_ASSERT(*notif);
1099 muxer_notif_iter->last_returned_ts_ns = next_return_ts;
1100
1101 end:
1102 return status;
1103 }
1104
1105 static
1106 enum bt_notification_iterator_status muxer_notif_iter_do_next(
1107 struct muxer_comp *muxer_comp,
1108 struct muxer_notif_iter *muxer_notif_iter,
1109 bt_notification_array notifs, uint64_t capacity,
1110 uint64_t *count)
1111 {
1112 enum bt_notification_iterator_status status =
1113 BT_NOTIFICATION_ITERATOR_STATUS_OK;
1114 uint64_t i = 0;
1115
1116 while (i < capacity && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
1117 status = muxer_notif_iter_do_next_one(muxer_comp,
1118 muxer_notif_iter, &notifs[i]);
1119 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
1120 i++;
1121 }
1122 }
1123
1124 if (i > 0) {
1125 /*
1126 * Even if muxer_notif_iter_do_next_one() returned
1127 * something else than
1128 * BT_NOTIFICATION_ITERATOR_STATUS_OK, we accumulated
1129 * notification objects in the output notification
1130 * array, so we need to return
1131 * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are
1132 * transfered to downstream. This other status occurs
1133 * again the next time muxer_notif_iter_do_next() is
1134 * called, possibly without any accumulated
1135 * notification, in which case we'll return it.
1136 */
1137 *count = i;
1138 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
1139 }
1140
1141 return status;
1142 }
1143
1144 static
1145 void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter)
1146 {
1147 if (!muxer_notif_iter) {
1148 return;
1149 }
1150
1151 BT_LOGD("Destroying muxer component's notification iterator: "
1152 "muxer-notif-iter-addr=%p", muxer_notif_iter);
1153
1154 if (muxer_notif_iter->muxer_upstream_notif_iters) {
1155 BT_LOGD_STR("Destroying muxer's upstream notification iterator wrappers.");
1156 g_ptr_array_free(
1157 muxer_notif_iter->muxer_upstream_notif_iters, TRUE);
1158 }
1159
1160 g_list_free(muxer_notif_iter->newly_connected_self_ports);
1161 g_free(muxer_notif_iter);
1162 }
1163
1164 static
1165 int muxer_notif_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp,
1166 struct muxer_notif_iter *muxer_notif_iter)
1167 {
1168 int64_t count;
1169 int64_t i;
1170 int ret = 0;
1171
1172 /*
1173 * Add the connected input ports to this muxer notification
1174 * iterator's list of newly connected ports. They will be
1175 * handled by muxer_notif_iter_handle_newly_connected_ports().
1176 */
1177 count = bt_component_filter_get_input_port_count(
1178 bt_self_component_filter_as_component_filter(
1179 muxer_comp->self_comp));
1180 if (count < 0) {
1181 BT_LOGD("No input port to initialize for muxer component's notification iterator: "
1182 "muxer-comp-addr=%p, muxer-notif-iter-addr=%p",
1183 muxer_comp, muxer_notif_iter);
1184 goto end;
1185 }
1186
1187 for (i = 0; i < count; i++) {
1188 struct bt_self_component_port_input *self_port =
1189 bt_self_component_filter_borrow_input_port_by_index(
1190 muxer_comp->self_comp, i);
1191 struct bt_port *port;
1192
1193 BT_ASSERT(self_port);
1194 port = bt_self_component_port_as_port(
1195 bt_self_component_port_input_as_self_component_port(
1196 self_port));
1197 BT_ASSERT(port);
1198
1199 if (!bt_port_is_connected(port)) {
1200 BT_LOGD("Skipping input port: not connected: "
1201 "muxer-comp-addr=%p, port-addr=%p, port-name\"%s\"",
1202 muxer_comp, port, bt_port_get_name(port));
1203 continue;
1204 }
1205
1206 muxer_notif_iter->newly_connected_self_ports =
1207 g_list_append(
1208 muxer_notif_iter->newly_connected_self_ports,
1209 self_port);
1210 if (!muxer_notif_iter->newly_connected_self_ports) {
1211 BT_LOGE("Cannot append port to muxer's notification iterator list of newly connected input ports: "
1212 "port-addr=%p, port-name=\"%s\", "
1213 "muxer-notif-iter-addr=%p", port,
1214 bt_port_get_name(port), muxer_notif_iter);
1215 ret = -1;
1216 goto end;
1217 }
1218
1219 BT_LOGD("Appended port to muxer's notification iterator list of newly connected input ports: "
1220 "port-addr=%p, port-name=\"%s\", "
1221 "muxer-notif-iter-addr=%p", port,
1222 bt_port_get_name(port), muxer_notif_iter);
1223 }
1224
1225 end:
1226 return ret;
1227 }
1228
1229 BT_HIDDEN
1230 enum bt_self_notification_iterator_status muxer_notif_iter_init(
1231 struct bt_self_notification_iterator *self_notif_iter,
1232 struct bt_self_component_filter *self_comp,
1233 struct bt_self_component_port_output *port)
1234 {
1235 struct muxer_comp *muxer_comp = NULL;
1236 struct muxer_notif_iter *muxer_notif_iter = NULL;
1237 enum bt_self_notification_iterator_status status =
1238 BT_NOTIFICATION_ITERATOR_STATUS_OK;
1239 int ret;
1240
1241 muxer_comp = bt_self_component_get_data(
1242 bt_self_component_filter_as_self_component(self_comp));
1243 BT_ASSERT(muxer_comp);
1244 BT_LOGD("Initializing muxer component's notification iterator: "
1245 "comp-addr=%p, muxer-comp-addr=%p, notif-iter-addr=%p",
1246 self_comp, muxer_comp, self_notif_iter);
1247
1248 if (muxer_comp->initializing_muxer_notif_iter) {
1249 /*
1250 * Weird, unhandled situation detected: downstream
1251 * creates a muxer notification iterator while creating
1252 * another muxer notification iterator (same component).
1253 */
1254 BT_LOGE("Recursive initialization of muxer component's notification iterator: "
1255 "comp-addr=%p, muxer-comp-addr=%p, notif-iter-addr=%p",
1256 self_comp, muxer_comp, self_notif_iter);
1257 goto error;
1258 }
1259
1260 muxer_comp->initializing_muxer_notif_iter = true;
1261 muxer_notif_iter = g_new0(struct muxer_notif_iter, 1);
1262 if (!muxer_notif_iter) {
1263 BT_LOGE_STR("Failed to allocate one muxer component's notification iterator.");
1264 goto error;
1265 }
1266
1267 muxer_notif_iter->last_returned_ts_ns = INT64_MIN;
1268 muxer_notif_iter->muxer_upstream_notif_iters =
1269 g_ptr_array_new_with_free_func(
1270 (GDestroyNotify) destroy_muxer_upstream_notif_iter);
1271 if (!muxer_notif_iter->muxer_upstream_notif_iters) {
1272 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1273 goto error;
1274 }
1275
1276 /*
1277 * Add the muxer notification iterator to the component's array
1278 * of muxer notification iterators here because
1279 * muxer_notif_iter_init_newly_connected_ports() can cause
1280 * muxer_port_connected() to be called, which adds the newly
1281 * connected port to each muxer notification iterator's list of
1282 * newly connected ports.
1283 */
1284 g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter);
1285 ret = muxer_notif_iter_init_newly_connected_ports(muxer_comp,
1286 muxer_notif_iter);
1287 if (ret) {
1288 BT_LOGE("Cannot initialize newly connected input ports for muxer component's notification iterator: "
1289 "comp-addr=%p, muxer-comp-addr=%p, "
1290 "muxer-notif-iter-addr=%p, notif-iter-addr=%p, ret=%d",
1291 self_comp, muxer_comp, muxer_notif_iter,
1292 self_notif_iter, ret);
1293 goto error;
1294 }
1295
1296 bt_self_notification_iterator_set_data(self_notif_iter,
1297 muxer_notif_iter);
1298 BT_LOGD("Initialized muxer component's notification iterator: "
1299 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1300 "notif-iter-addr=%p",
1301 self_comp, muxer_comp, muxer_notif_iter, self_notif_iter);
1302 goto end;
1303
1304 error:
1305 if (g_ptr_array_index(muxer_comp->muxer_notif_iters,
1306 muxer_comp->muxer_notif_iters->len - 1) == muxer_notif_iter) {
1307 g_ptr_array_remove_index(muxer_comp->muxer_notif_iters,
1308 muxer_comp->muxer_notif_iters->len - 1);
1309 }
1310
1311 destroy_muxer_notif_iter(muxer_notif_iter);
1312 bt_self_notification_iterator_set_data(self_notif_iter,
1313 NULL);
1314 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1315
1316 end:
1317 muxer_comp->initializing_muxer_notif_iter = false;
1318 return status;
1319 }
1320
1321 BT_HIDDEN
1322 void muxer_notif_iter_finalize(
1323 struct bt_self_notification_iterator *self_notif_iter)
1324 {
1325 struct muxer_notif_iter *muxer_notif_iter =
1326 bt_self_notification_iterator_get_data(self_notif_iter);
1327 struct bt_self_component *self_comp = NULL;
1328 struct muxer_comp *muxer_comp = NULL;
1329
1330 self_comp = bt_self_notification_iterator_borrow_component(
1331 self_notif_iter);
1332 BT_ASSERT(self_comp);
1333 muxer_comp = bt_self_component_get_data(self_comp);
1334 BT_LOGD("Finalizing muxer component's notification iterator: "
1335 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1336 "notif-iter-addr=%p",
1337 self_comp, muxer_comp, muxer_notif_iter, self_notif_iter);
1338
1339 if (muxer_comp) {
1340 (void) g_ptr_array_remove_fast(muxer_comp->muxer_notif_iters,
1341 muxer_notif_iter);
1342 destroy_muxer_notif_iter(muxer_notif_iter);
1343 }
1344 }
1345
1346 BT_HIDDEN
1347 enum bt_notification_iterator_status muxer_notif_iter_next(
1348 struct bt_self_notification_iterator *self_notif_iter,
1349 bt_notification_array notifs, uint64_t capacity,
1350 uint64_t *count)
1351 {
1352 enum bt_notification_iterator_status status;
1353 struct muxer_notif_iter *muxer_notif_iter =
1354 bt_self_notification_iterator_get_data(self_notif_iter);
1355 struct bt_self_component *self_comp = NULL;
1356 struct muxer_comp *muxer_comp = NULL;
1357
1358 BT_ASSERT(muxer_notif_iter);
1359 self_comp = bt_self_notification_iterator_borrow_component(
1360 self_notif_iter);
1361 BT_ASSERT(self_comp);
1362 muxer_comp = bt_self_component_get_data(self_comp);
1363 BT_ASSERT(muxer_comp);
1364 BT_LOGV("Muxer component's notification iterator's \"next\" method called: "
1365 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1366 "notif-iter-addr=%p",
1367 self_comp, muxer_comp, muxer_notif_iter, self_notif_iter);
1368
1369 status = muxer_notif_iter_do_next(muxer_comp, muxer_notif_iter,
1370 notifs, capacity, count);
1371 if (status < 0) {
1372 BT_LOGE("Cannot get next notification: "
1373 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1374 "notif-iter-addr=%p, status=%s",
1375 self_comp, muxer_comp, muxer_notif_iter, self_notif_iter,
1376 bt_notification_iterator_status_string(status));
1377 } else {
1378 BT_LOGV("Returning from muxer component's notification iterator's \"next\" method: "
1379 "status=%s",
1380 bt_notification_iterator_status_string(status));
1381 }
1382
1383 return status;
1384 }
1385
1386 BT_HIDDEN
1387 enum bt_self_component_status muxer_input_port_connected(
1388 struct bt_self_component_filter *self_comp,
1389 struct bt_self_component_port_input *self_port,
1390 struct bt_port_output *other_port)
1391 {
1392 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
1393 struct bt_port *port = bt_self_component_port_as_port(
1394 bt_self_component_port_input_as_self_component_port(
1395 self_port));
1396 struct muxer_comp *muxer_comp =
1397 bt_self_component_get_data(
1398 bt_self_component_filter_as_self_component(
1399 self_comp));
1400 size_t i;
1401 int ret;
1402
1403 BT_ASSERT(port);
1404 BT_ASSERT(muxer_comp);
1405 BT_LOGD("Port connected: "
1406 "comp-addr=%p, muxer-comp-addr=%p, "
1407 "port-addr=%p, port-name=\"%s\", "
1408 "other-port-addr=%p, other-port-name=\"%s\"",
1409 self_comp, muxer_comp, self_port, bt_port_get_name(port),
1410 other_port,
1411 bt_port_get_name(bt_port_output_as_port(other_port)));
1412
1413 for (i = 0; i < muxer_comp->muxer_notif_iters->len; i++) {
1414 struct muxer_notif_iter *muxer_notif_iter =
1415 g_ptr_array_index(muxer_comp->muxer_notif_iters, i);
1416
1417 /*
1418 * Add this port to the list of newly connected ports
1419 * for this muxer notification iterator. We append at
1420 * the end of this list while
1421 * muxer_notif_iter_handle_newly_connected_ports()
1422 * removes the nodes from the beginning.
1423 */
1424 muxer_notif_iter->newly_connected_self_ports =
1425 g_list_append(
1426 muxer_notif_iter->newly_connected_self_ports,
1427 self_port);
1428 if (!muxer_notif_iter->newly_connected_self_ports) {
1429 BT_LOGE("Cannot append port to muxer's notification iterator list of newly connected input ports: "
1430 "port-addr=%p, port-name=\"%s\", "
1431 "muxer-notif-iter-addr=%p", self_port,
1432 bt_port_get_name(port), muxer_notif_iter);
1433 status = BT_SELF_COMPONENT_STATUS_ERROR;
1434 goto end;
1435 }
1436
1437 BT_LOGD("Appended port to muxer's notification iterator list of newly connected input ports: "
1438 "port-addr=%p, port-name=\"%s\", "
1439 "muxer-notif-iter-addr=%p", self_port,
1440 bt_port_get_name(port), muxer_notif_iter);
1441 }
1442
1443 /* One less available input port */
1444 muxer_comp->available_input_ports--;
1445 ret = ensure_available_input_port(self_comp);
1446 if (ret) {
1447 /*
1448 * Only way to report an error later since this
1449 * method does not return anything.
1450 */
1451 BT_LOGE("Cannot ensure that at least one muxer component's input port is available: "
1452 "muxer-comp-addr=%p, status=%s",
1453 muxer_comp, bt_self_component_status_string(ret));
1454 status = BT_SELF_COMPONENT_STATUS_ERROR;
1455 goto end;
1456 }
1457
1458 end:
1459 return status;
1460 }
1461
1462 BT_HIDDEN
1463 void muxer_input_port_disconnected(
1464 struct bt_self_component_filter *self_component,
1465 struct bt_self_component_port_input *self_port)
1466 {
1467 struct muxer_comp *muxer_comp =
1468 bt_self_component_get_data(
1469 bt_self_component_filter_as_self_component(
1470 self_component));
1471 struct bt_port *port =
1472 bt_self_component_port_as_port(
1473 bt_self_component_port_input_as_self_component_port(
1474 self_port));
1475
1476 BT_ASSERT(port);
1477 BT_ASSERT(muxer_comp);
1478
1479 /* One more available input port */
1480 muxer_comp->available_input_ports++;
1481 BT_LOGD("Leaving disconnected input port available for future connections: "
1482 "comp-addr=%p, muxer-comp-addr=%p, port-addr=%p, "
1483 "port-name=\"%s\", avail-input-port-count=%zu",
1484 self_component, muxer_comp, port, bt_port_get_name(port),
1485 muxer_comp->available_input_ports);
1486 }
This page took 0.09835 seconds and 4 git commands to generate.