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