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