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