2 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "GRAPH"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/graph/component-internal.h>
28 #include <babeltrace/graph/graph.h>
29 #include <babeltrace/graph/graph-const.h>
30 #include <babeltrace/graph/graph-internal.h>
31 #include <babeltrace/graph/connection-internal.h>
32 #include <babeltrace/graph/component-sink-internal.h>
33 #include <babeltrace/graph/component-source-const.h>
34 #include <babeltrace/graph/component-filter-const.h>
35 #include <babeltrace/graph/port-const.h>
36 #include <babeltrace/graph/notification-internal.h>
37 #include <babeltrace/graph/notification-event-internal.h>
38 #include <babeltrace/graph/notification-packet-internal.h>
39 #include <babeltrace/compiler-internal.h>
40 #include <babeltrace/common-internal.h>
41 #include <babeltrace/types.h>
42 #include <babeltrace/values.h>
43 #include <babeltrace/values-const.h>
44 #include <babeltrace/values-internal.h>
45 #include <babeltrace/object.h>
46 #include <babeltrace/assert-internal.h>
47 #include <babeltrace/assert-pre-internal.h>
51 typedef void (*port_added_func_t
)(const void *, const void *, void *);
53 typedef void (*port_removed_func_t
)(const void *, const void *, void *);
55 typedef void (*ports_connected_func_t
)(const void *, const void *, const void *,
56 const void *, void *);
58 typedef void (*ports_disconnected_func_t
)(const void *, const void *,
59 const void *, const void *, void *);
61 typedef enum bt_self_component_status (*comp_init_method_t
)(const void *,
62 const void *, void *);
64 struct bt_graph_listener
{
65 bt_graph_listener_removed_func removed
;
69 struct bt_graph_listener_port_added
{
70 struct bt_graph_listener base
;
71 port_added_func_t func
;
74 struct bt_graph_listener_port_removed
{
75 struct bt_graph_listener base
;
76 port_removed_func_t func
;
79 struct bt_graph_listener_ports_connected
{
80 struct bt_graph_listener base
;
81 ports_connected_func_t func
;
84 struct bt_graph_listener_ports_disconnected
{
85 struct bt_graph_listener base
;
86 ports_disconnected_func_t func
;
89 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
91 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
92 if (!(_listeners)) { \
93 BT_LOGE_STR("Failed to allocate one GArray."); \
97 #define CALL_REMOVE_LISTENERS(_type, _listeners) \
101 for (i = 0; i < (_listeners)->len; i++) { \
103 &g_array_index((_listeners), _type, i); \
105 if (listener->base.removed) { \
106 listener->base.removed(listener->base.data); \
112 void destroy_graph(struct bt_object
*obj
)
114 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
117 * The graph's reference count is 0 if we're here. Increment
118 * it to avoid a double-destroy (possibly infinitely recursive)
121 * 1. We put and destroy a connection.
122 * 2. This connection's destructor finalizes its active
123 * notification iterators.
124 * 3. A notification iterator's finalization function gets a
125 * new reference on its component (reference count goes from
127 * 4. Since this component's reference count goes to 1, it takes
128 * a reference on its parent (this graph). This graph's
129 * reference count goes from 0 to 1.
130 * 5. The notification iterator's finalization function puts its
131 * component reference (reference count goes from 1 to 0).
132 * 6. Since this component's reference count goes from 1 to 0,
133 * it puts its parent (this graph). This graph's reference
134 * count goes from 1 to 0.
135 * 7. Since this graph's reference count goes from 1 to 0,
136 * its destructor is called (this function).
138 * With the incrementation below, the graph's reference count at
139 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
140 * ensures that this function is not called two times.
142 BT_LIB_LOGD("Destroying graph: %!+g", graph
);
146 * Cancel the graph to disallow some operations, like creating
147 * notification iterators and adding ports to components.
149 (void) bt_graph_cancel((void *) graph
);
151 /* Call all remove listeners */
152 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
153 graph
->listeners
.source_output_port_added
);
154 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
155 graph
->listeners
.filter_output_port_added
);
156 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
157 graph
->listeners
.filter_input_port_added
);
158 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
159 graph
->listeners
.sink_input_port_added
);
160 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed
,
161 graph
->listeners
.source_output_port_removed
);
162 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed
,
163 graph
->listeners
.filter_output_port_removed
);
164 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed
,
165 graph
->listeners
.filter_input_port_removed
);
166 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed
,
167 graph
->listeners
.sink_input_port_removed
);
168 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
169 graph
->listeners
.source_filter_ports_connected
);
170 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
171 graph
->listeners
.source_sink_ports_connected
);
172 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
173 graph
->listeners
.filter_sink_ports_connected
);
174 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected
,
175 graph
->listeners
.source_filter_ports_disconnected
);
176 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected
,
177 graph
->listeners
.source_sink_ports_disconnected
);
178 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected
,
179 graph
->listeners
.filter_sink_ports_disconnected
);
181 if (graph
->notifications
) {
182 g_ptr_array_free(graph
->notifications
, TRUE
);
183 graph
->notifications
= NULL
;
186 if (graph
->connections
) {
187 BT_LOGD_STR("Destroying connections.");
188 g_ptr_array_free(graph
->connections
, TRUE
);
189 graph
->connections
= NULL
;
192 if (graph
->components
) {
193 BT_LOGD_STR("Destroying components.");
194 g_ptr_array_free(graph
->components
, TRUE
);
195 graph
->components
= NULL
;
198 if (graph
->sinks_to_consume
) {
199 g_queue_free(graph
->sinks_to_consume
);
200 graph
->sinks_to_consume
= NULL
;
203 if (graph
->listeners
.source_output_port_added
) {
204 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
205 graph
->listeners
.source_output_port_added
= NULL
;
208 if (graph
->listeners
.filter_output_port_added
) {
209 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
210 graph
->listeners
.filter_output_port_added
= NULL
;
213 if (graph
->listeners
.filter_input_port_added
) {
214 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
215 graph
->listeners
.filter_input_port_added
= NULL
;
218 if (graph
->listeners
.sink_input_port_added
) {
219 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
220 graph
->listeners
.sink_input_port_added
= NULL
;
223 if (graph
->listeners
.source_output_port_removed
) {
224 g_array_free(graph
->listeners
.source_output_port_removed
, TRUE
);
225 graph
->listeners
.source_output_port_removed
= NULL
;
228 if (graph
->listeners
.filter_output_port_removed
) {
229 g_array_free(graph
->listeners
.filter_output_port_removed
, TRUE
);
230 graph
->listeners
.filter_output_port_removed
= NULL
;
233 if (graph
->listeners
.filter_input_port_removed
) {
234 g_array_free(graph
->listeners
.filter_input_port_removed
, TRUE
);
235 graph
->listeners
.filter_input_port_removed
= NULL
;
238 if (graph
->listeners
.sink_input_port_removed
) {
239 g_array_free(graph
->listeners
.sink_input_port_removed
, TRUE
);
240 graph
->listeners
.sink_input_port_removed
= NULL
;
243 if (graph
->listeners
.source_filter_ports_connected
) {
244 g_array_free(graph
->listeners
.source_filter_ports_connected
,
246 graph
->listeners
.source_filter_ports_connected
= NULL
;
249 if (graph
->listeners
.source_sink_ports_connected
) {
250 g_array_free(graph
->listeners
.source_sink_ports_connected
,
252 graph
->listeners
.source_sink_ports_connected
= NULL
;
255 if (graph
->listeners
.filter_sink_ports_connected
) {
256 g_array_free(graph
->listeners
.filter_sink_ports_connected
,
258 graph
->listeners
.filter_sink_ports_connected
= NULL
;
261 if (graph
->listeners
.source_filter_ports_disconnected
) {
262 g_array_free(graph
->listeners
.source_filter_ports_disconnected
,
264 graph
->listeners
.source_filter_ports_disconnected
= NULL
;
267 if (graph
->listeners
.source_sink_ports_disconnected
) {
268 g_array_free(graph
->listeners
.source_sink_ports_disconnected
,
270 graph
->listeners
.source_sink_ports_disconnected
= NULL
;
273 if (graph
->listeners
.filter_sink_ports_disconnected
) {
274 g_array_free(graph
->listeners
.filter_sink_ports_disconnected
,
276 graph
->listeners
.filter_sink_ports_disconnected
= NULL
;
279 bt_object_pool_finalize(&graph
->event_notif_pool
);
280 bt_object_pool_finalize(&graph
->packet_begin_notif_pool
);
281 bt_object_pool_finalize(&graph
->packet_end_notif_pool
);
286 void destroy_notification_event(struct bt_notification
*notif
,
287 struct bt_graph
*graph
)
289 bt_notification_event_destroy(notif
);
293 void destroy_notification_packet_begin(struct bt_notification
*notif
,
294 struct bt_graph
*graph
)
296 bt_notification_packet_begin_destroy(notif
);
300 void destroy_notification_packet_end(struct bt_notification
*notif
,
301 struct bt_graph
*graph
)
303 bt_notification_packet_end_destroy(notif
);
307 void notify_notification_graph_is_destroyed(struct bt_notification
*notif
)
309 bt_notification_unlink_graph(notif
);
312 struct bt_graph
*bt_graph_create(void)
314 struct bt_graph
*graph
;
317 BT_LOGD_STR("Creating graph object.");
318 graph
= g_new0(struct bt_graph
, 1);
320 BT_LOGE_STR("Failed to allocate one graph.");
324 bt_object_init_shared(&graph
->base
, destroy_graph
);
325 graph
->connections
= g_ptr_array_new_with_free_func(
326 (GDestroyNotify
) bt_object_try_spec_release
);
327 if (!graph
->connections
) {
328 BT_LOGE_STR("Failed to allocate one GPtrArray.");
331 graph
->components
= g_ptr_array_new_with_free_func(
332 (GDestroyNotify
) bt_object_try_spec_release
);
333 if (!graph
->components
) {
334 BT_LOGE_STR("Failed to allocate one GPtrArray.");
337 graph
->sinks_to_consume
= g_queue_new();
338 if (!graph
->sinks_to_consume
) {
339 BT_LOGE_STR("Failed to allocate one GQueue.");
343 bt_graph_set_can_consume(graph
, true);
344 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
345 graph
->listeners
.source_output_port_added
);
347 if (!graph
->listeners
.source_output_port_added
) {
352 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
353 graph
->listeners
.filter_output_port_added
);
355 if (!graph
->listeners
.filter_output_port_added
) {
360 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
361 graph
->listeners
.filter_input_port_added
);
363 if (!graph
->listeners
.filter_input_port_added
) {
368 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
369 graph
->listeners
.sink_input_port_added
);
371 if (!graph
->listeners
.sink_input_port_added
) {
376 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed
,
377 graph
->listeners
.source_output_port_removed
);
379 if (!graph
->listeners
.source_output_port_removed
) {
384 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed
,
385 graph
->listeners
.filter_output_port_removed
);
387 if (!graph
->listeners
.filter_output_port_removed
) {
392 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed
,
393 graph
->listeners
.filter_input_port_removed
);
395 if (!graph
->listeners
.filter_input_port_removed
) {
400 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed
,
401 graph
->listeners
.sink_input_port_removed
);
403 if (!graph
->listeners
.sink_input_port_removed
) {
408 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
409 graph
->listeners
.source_filter_ports_connected
);
411 if (!graph
->listeners
.source_filter_ports_connected
) {
416 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
417 graph
->listeners
.source_sink_ports_connected
);
419 if (!graph
->listeners
.source_sink_ports_connected
) {
424 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
425 graph
->listeners
.filter_sink_ports_connected
);
427 if (!graph
->listeners
.filter_sink_ports_connected
) {
432 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected
,
433 graph
->listeners
.source_filter_ports_disconnected
);
435 if (!graph
->listeners
.source_filter_ports_disconnected
) {
440 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected
,
441 graph
->listeners
.source_sink_ports_disconnected
);
443 if (!graph
->listeners
.source_sink_ports_disconnected
) {
448 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected
,
449 graph
->listeners
.filter_sink_ports_disconnected
);
451 if (!graph
->listeners
.filter_sink_ports_disconnected
) {
456 ret
= bt_object_pool_initialize(&graph
->event_notif_pool
,
457 (bt_object_pool_new_object_func
) bt_notification_event_new
,
458 (bt_object_pool_destroy_object_func
) destroy_notification_event
,
461 BT_LOGE("Failed to initialize event notification pool: ret=%d",
466 ret
= bt_object_pool_initialize(&graph
->packet_begin_notif_pool
,
467 (bt_object_pool_new_object_func
) bt_notification_packet_begin_new
,
468 (bt_object_pool_destroy_object_func
) destroy_notification_packet_begin
,
471 BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d",
476 ret
= bt_object_pool_initialize(&graph
->packet_end_notif_pool
,
477 (bt_object_pool_new_object_func
) bt_notification_packet_end_new
,
478 (bt_object_pool_destroy_object_func
) destroy_notification_packet_end
,
481 BT_LOGE("Failed to initialize packet end notification pool: ret=%d",
486 graph
->notifications
= g_ptr_array_new_with_free_func(
487 (GDestroyNotify
) notify_notification_graph_is_destroyed
);
488 BT_LIB_LOGD("Created graph object: %!+g", graph
);
491 return (void *) graph
;
494 BT_OBJECT_PUT_REF_AND_RESET(graph
);
498 enum bt_graph_status
bt_graph_connect_ports(
499 struct bt_graph
*graph
,
500 const struct bt_port_output
*upstream_port_out
,
501 const struct bt_port_input
*downstream_port_in
,
502 const struct bt_connection
**user_connection
)
504 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
505 struct bt_connection
*connection
= NULL
;
506 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
507 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
508 struct bt_component
*upstream_component
= NULL
;
509 struct bt_component
*downstream_component
= NULL
;
510 enum bt_self_component_status component_status
;
511 bool init_can_consume
;
513 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
514 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
515 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
516 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
517 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
518 "Upstream port is already connected: %!+p", upstream_port
);
519 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
520 "Downstream port is already connected: %!+p", downstream_port
);
521 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
522 "Upstream port does not belong to a component: %!+p",
524 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
525 "Downstream port does not belong to a component: %!+p",
527 init_can_consume
= graph
->can_consume
;
528 BT_LIB_LOGD("Connecting component ports within graph: "
529 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
530 graph
, upstream_port
, downstream_port
);
531 bt_graph_set_can_consume(graph
, false);
532 upstream_component
= bt_port_borrow_component_inline(
533 (void *) upstream_port
);
534 downstream_component
= bt_port_borrow_component_inline(
535 (void *) downstream_port
);
538 * At this point the ports are not connected yet. Both
539 * components need to accept an eventual connection to their
540 * port by the other port before we continue.
542 BT_LIB_LOGD("Asking upstream component to accept the connection: "
543 "%![comp-]+c", upstream_component
);
544 component_status
= bt_component_accept_port_connection(
545 upstream_component
, (void *) upstream_port
,
546 (void *) downstream_port
);
547 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
548 if (component_status
== BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
549 BT_LOGD_STR("Upstream component refused the connection.");
551 BT_LOGW("Cannot ask upstream component to accept the connection: "
552 "status=%s", bt_self_component_status_string(component_status
));
555 status
= (int) component_status
;
559 BT_LIB_LOGD("Asking downstream component to accept the connection: "
560 "%![comp-]+c", downstream_component
);
561 component_status
= bt_component_accept_port_connection(
562 downstream_component
, (void *) downstream_port
,
563 (void *) upstream_port
);
564 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
565 if (component_status
== BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
566 BT_LOGD_STR("Downstream component refused the connection.");
568 BT_LOGW("Cannot ask downstream component to accept the connection: "
569 "status=%s", bt_self_component_status_string(component_status
));
572 status
= (int) component_status
;
576 BT_LOGD_STR("Creating connection.");
577 connection
= bt_connection_create(graph
, (void *) upstream_port
,
578 (void *) downstream_port
);
580 BT_LOGW("Cannot create connection object.");
581 status
= BT_GRAPH_STATUS_NOMEM
;
585 BT_LIB_LOGD("Connection object created: %!+x", connection
);
588 * Ownership of upstream_component/downstream_component and of
589 * the connection object is transferred to the graph.
591 g_ptr_array_add(graph
->connections
, connection
);
594 * Notify both components that their port is connected.
596 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
597 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
598 component_status
= bt_component_port_connected(upstream_component
,
599 (void *) upstream_port
, (void *) downstream_port
);
600 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
601 BT_LIB_LOGW("Error while notifying upstream component that its port is connected: "
602 "status=%s, %![graph-]+g, %![up-comp-]+c, "
603 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
604 bt_self_component_status_string(component_status
),
605 graph
, upstream_component
, downstream_component
,
606 upstream_port
, downstream_port
);
607 bt_connection_end(connection
, true);
608 status
= (int) component_status
;
612 connection
->notified_upstream_port_connected
= true;
613 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
614 "%![comp-]+c, %![port-]+p", downstream_component
,
616 component_status
= bt_component_port_connected(downstream_component
,
617 (void *) downstream_port
, (void *) upstream_port
);
618 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
619 BT_LIB_LOGW("Error while notifying downstream component that its port is connected: "
620 "status=%s, %![graph-]+g, %![up-comp-]+c, "
621 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
622 bt_self_component_status_string(component_status
),
623 graph
, upstream_component
, downstream_component
,
624 upstream_port
, downstream_port
);
625 bt_connection_end(connection
, true);
626 status
= (int) component_status
;
630 connection
->notified_downstream_port_connected
= true;
633 * Notify the graph's creator that both ports are connected.
635 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
636 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
637 connection
->notified_graph_ports_connected
= true;
638 BT_LIB_LOGD("Connected component ports within graph: "
639 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
640 "%![up-port-]+p, %![down-port-]+p",
641 graph
, upstream_component
, downstream_component
,
642 upstream_port
, downstream_port
);
644 if (user_connection
) {
645 /* Move reference to user */
646 *user_connection
= connection
;
651 bt_object_put_ref(connection
);
652 (void) init_can_consume
;
653 bt_graph_set_can_consume(graph
, init_can_consume
);
658 enum bt_graph_status
consume_graph_sink(struct bt_component_sink
*comp
)
660 enum bt_self_component_status comp_status
;
661 struct bt_component_class_sink
*sink_class
= NULL
;
664 sink_class
= (void *) comp
->parent
.class;
665 BT_ASSERT(sink_class
->methods
.consume
);
666 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
667 comp_status
= sink_class
->methods
.consume((void *) comp
);
668 BT_LOGD("User method returned: status=%s",
669 bt_self_component_status_string(comp_status
));
670 BT_ASSERT_PRE(comp_status
== BT_SELF_COMPONENT_STATUS_OK
||
671 comp_status
== BT_SELF_COMPONENT_STATUS_END
||
672 comp_status
== BT_SELF_COMPONENT_STATUS_AGAIN
||
673 comp_status
== BT_SELF_COMPONENT_STATUS_ERROR
||
674 comp_status
== BT_SELF_COMPONENT_STATUS_NOMEM
,
675 "Invalid component status returned by consuming method: "
676 "status=%s", bt_self_component_status_string(comp_status
));
677 if (comp_status
< 0) {
678 BT_LOGW_STR("Consume method failed.");
682 BT_LIB_LOGV("Consumed from sink: %![comp-]+c, status=%s",
683 comp
, bt_self_component_status_string(comp_status
));
686 return (int) comp_status
;
690 * `node` is removed from the queue of sinks to consume when passed to
691 * this function. This function adds it back to the queue if there's
692 * still something to consume afterwards.
695 enum bt_graph_status
consume_sink_node(struct bt_graph
*graph
, GList
*node
)
697 enum bt_graph_status status
;
698 struct bt_component_sink
*sink
;
701 status
= consume_graph_sink(sink
);
702 if (unlikely(status
!= BT_GRAPH_STATUS_END
)) {
703 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
707 /* End reached, the node is not added back to the queue and free'd. */
708 g_queue_delete_link(graph
->sinks_to_consume
, node
);
710 /* Don't forward an END status if there are sinks left to consume. */
711 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
712 status
= BT_GRAPH_STATUS_OK
;
717 BT_LIB_LOGV("Consumed sink node: %![comp-]+c, status=%s",
718 sink
, bt_graph_status_string(status
));
723 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
724 struct bt_component_sink
*sink
)
726 enum bt_graph_status status
;
730 BT_LIB_LOGV("Making specific sink consume: %![comp-]+c", sink
);
731 BT_ASSERT(bt_component_borrow_graph((void *) sink
) == graph
);
733 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
734 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
735 status
= BT_GRAPH_STATUS_END
;
739 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
741 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
742 status
= BT_GRAPH_STATUS_END
;
746 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
747 BT_ASSERT(sink_node
);
748 status
= consume_sink_node(graph
, sink_node
);
755 enum bt_graph_status
consume_no_check(struct bt_graph
*graph
)
757 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
758 struct bt_component
*sink
;
761 BT_ASSERT_PRE(graph
->has_sink
,
762 "Graph has no sink component: %!+g", graph
);
763 BT_LIB_LOGV("Making next sink consume: %![graph-]+g", graph
);
765 if (unlikely(g_queue_is_empty(graph
->sinks_to_consume
))) {
766 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
767 status
= BT_GRAPH_STATUS_END
;
771 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
772 sink
= current_node
->data
;
773 BT_LIB_LOGV("Chose next sink to consume: %!+c", sink
);
774 status
= consume_sink_node(graph
, current_node
);
780 enum bt_graph_status
bt_graph_consume(
781 struct bt_graph
*graph
)
783 enum bt_graph_status status
;
785 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
786 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
787 BT_ASSERT_PRE(graph
->can_consume
,
788 "Cannot consume graph in its current state: %!+g", graph
);
789 bt_graph_set_can_consume(graph
, BT_FALSE
);
790 status
= consume_no_check(graph
);
791 bt_graph_set_can_consume(graph
, BT_TRUE
);
795 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
797 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
799 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
800 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
801 BT_ASSERT_PRE(graph
->can_consume
,
802 "Cannot consume graph in its current state: %!+g", graph
);
803 bt_graph_set_can_consume(graph
, BT_FALSE
);
804 BT_LIB_LOGV("Running graph: %!+g", graph
);
808 * Check if the graph is canceled at each iteration. If
809 * the graph was canceled by another thread or by a
810 * signal handler, this is not a warning nor an error,
811 * it was intentional: log with a DEBUG level only.
813 if (unlikely(graph
->canceled
)) {
814 BT_LIB_LOGD("Stopping the graph: graph is canceled: "
816 status
= BT_GRAPH_STATUS_CANCELED
;
820 status
= consume_no_check(graph
);
821 if (unlikely(status
== BT_GRAPH_STATUS_AGAIN
)) {
823 * If AGAIN is received and there are multiple
824 * sinks, go ahead and consume from the next
827 * However, in the case where a single sink is
828 * left, the caller can decide to busy-wait and
829 * call bt_graph_run() continuously
830 * until the source is ready or it can decide to
831 * sleep for an arbitrary amount of time.
833 if (graph
->sinks_to_consume
->length
> 1) {
834 status
= BT_GRAPH_STATUS_OK
;
836 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
839 } while (status
== BT_GRAPH_STATUS_OK
);
841 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
842 status
= BT_GRAPH_STATUS_END
;
846 BT_LIB_LOGV("Graph ran: %![graph-]+g, status=%s", graph
,
847 bt_graph_status_string(status
));
848 bt_graph_set_can_consume(graph
, BT_TRUE
);
853 bt_graph_add_source_component_output_port_added_listener(
854 struct bt_graph
*graph
,
855 bt_graph_source_component_output_port_added_listener_func func
,
856 bt_graph_listener_removed_func listener_removed
, void *data
,
857 int *out_listener_id
)
859 struct bt_graph_listener_port_added listener
= {
861 .removed
= listener_removed
,
864 .func
= (port_added_func_t
) func
,
868 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
869 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
870 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
871 BT_ASSERT_PRE(!graph
->in_remove_listener
,
872 "Graph currently executing a \"listener removed\" listener: "
874 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
875 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
876 BT_LIB_LOGV("Added \"source component output port added\" listener to graph: "
877 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
881 *out_listener_id
= listener_id
;
884 return BT_GRAPH_STATUS_OK
;
888 bt_graph_add_filter_component_output_port_added_listener(
889 struct bt_graph
*graph
,
890 bt_graph_filter_component_output_port_added_listener_func func
,
891 bt_graph_listener_removed_func listener_removed
, void *data
,
892 int *out_listener_id
)
894 struct bt_graph_listener_port_added listener
= {
896 .removed
= listener_removed
,
899 .func
= (port_added_func_t
) func
,
903 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
904 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
905 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
906 BT_ASSERT_PRE(!graph
->in_remove_listener
,
907 "Graph currently executing a \"listener removed\" listener: "
909 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
910 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
911 BT_LIB_LOGV("Added \"filter component output port added\" listener to graph: "
912 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
916 *out_listener_id
= listener_id
;
919 return BT_GRAPH_STATUS_OK
;
923 bt_graph_add_filter_component_input_port_added_listener(
924 struct bt_graph
*graph
,
925 bt_graph_filter_component_input_port_added_listener_func func
,
926 bt_graph_listener_removed_func listener_removed
, void *data
,
927 int *out_listener_id
)
929 struct bt_graph_listener_port_added listener
= {
931 .removed
= listener_removed
,
934 .func
= (port_added_func_t
) func
,
938 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
939 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
940 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
941 BT_ASSERT_PRE(!graph
->in_remove_listener
,
942 "Graph currently executing a \"listener removed\" listener: "
944 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
945 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
946 BT_LIB_LOGV("Added \"filter component input port added\" listener to graph: "
947 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
951 *out_listener_id
= listener_id
;
954 return BT_GRAPH_STATUS_OK
;
958 bt_graph_add_sink_component_input_port_added_listener(
959 struct bt_graph
*graph
,
960 bt_graph_sink_component_input_port_added_listener_func func
,
961 bt_graph_listener_removed_func listener_removed
, void *data
,
962 int *out_listener_id
)
964 struct bt_graph_listener_port_added listener
= {
966 .removed
= listener_removed
,
969 .func
= (port_added_func_t
) func
,
973 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
974 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
975 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
976 BT_ASSERT_PRE(!graph
->in_remove_listener
,
977 "Graph currently executing a \"listener removed\" listener: "
979 g_array_append_val(graph
->listeners
.sink_input_port_added
, listener
);
980 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
981 BT_LIB_LOGV("Added \"sink component input port added\" listener to graph: "
982 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
986 *out_listener_id
= listener_id
;
989 return BT_GRAPH_STATUS_OK
;
993 bt_graph_add_source_component_output_port_removed_listener(
994 struct bt_graph
*graph
,
995 bt_graph_source_component_output_port_removed_listener_func func
,
996 bt_graph_listener_removed_func listener_removed
, void *data
,
997 int *out_listener_id
)
999 struct bt_graph_listener_port_removed listener
= {
1001 .removed
= listener_removed
,
1004 .func
= (port_removed_func_t
) func
,
1008 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1009 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1010 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1011 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1012 "Graph currently executing a \"listener removed\" listener: "
1014 g_array_append_val(graph
->listeners
.source_output_port_removed
, listener
);
1015 listener_id
= graph
->listeners
.source_output_port_removed
->len
- 1;
1016 BT_LIB_LOGV("Added \"source component output port removed\" listener to graph: "
1017 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1021 *out_listener_id
= listener_id
;
1024 return BT_GRAPH_STATUS_OK
;
1027 enum bt_graph_status
1028 bt_graph_add_filter_component_output_port_removed_listener(
1029 struct bt_graph
*graph
,
1030 bt_graph_filter_component_output_port_removed_listener_func func
,
1031 bt_graph_listener_removed_func listener_removed
, void *data
,
1032 int *out_listener_id
)
1034 struct bt_graph_listener_port_removed listener
= {
1036 .removed
= listener_removed
,
1039 .func
= (port_removed_func_t
) func
,
1043 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1044 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1045 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1046 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1047 "Graph currently executing a \"listener removed\" listener: "
1049 g_array_append_val(graph
->listeners
.filter_output_port_removed
, listener
);
1050 listener_id
= graph
->listeners
.filter_output_port_removed
->len
- 1;
1051 BT_LIB_LOGV("Added \"filter component output port removed\" listener to graph: "
1052 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1056 *out_listener_id
= listener_id
;
1059 return BT_GRAPH_STATUS_OK
;
1062 enum bt_graph_status
1063 bt_graph_add_filter_component_input_port_removed_listener(
1064 struct bt_graph
*graph
,
1065 bt_graph_filter_component_input_port_removed_listener_func func
,
1066 bt_graph_listener_removed_func listener_removed
, void *data
,
1067 int *out_listener_id
)
1069 struct bt_graph_listener_port_removed listener
= {
1071 .removed
= listener_removed
,
1074 .func
= (port_removed_func_t
) func
,
1078 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1079 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1080 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1081 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1082 "Graph currently executing a \"listener removed\" listener: "
1084 g_array_append_val(graph
->listeners
.filter_input_port_removed
, listener
);
1085 listener_id
= graph
->listeners
.filter_input_port_removed
->len
- 1;
1086 BT_LIB_LOGV("Added \"filter component input port removed\" listener to graph: "
1087 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1091 *out_listener_id
= listener_id
;
1094 return BT_GRAPH_STATUS_OK
;
1097 enum bt_graph_status
1098 bt_graph_add_sink_component_input_port_removed_listener(
1099 struct bt_graph
*graph
,
1100 bt_graph_sink_component_input_port_removed_listener_func func
,
1101 bt_graph_listener_removed_func listener_removed
, void *data
,
1102 int *out_listener_id
)
1104 struct bt_graph_listener_port_removed listener
= {
1106 .removed
= listener_removed
,
1109 .func
= (port_removed_func_t
) func
,
1113 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1114 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1115 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1116 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1117 "Graph currently executing a \"listener removed\" listener: "
1119 g_array_append_val(graph
->listeners
.sink_input_port_removed
, listener
);
1120 listener_id
= graph
->listeners
.sink_input_port_removed
->len
- 1;
1121 BT_LIB_LOGV("Added \"sink component input port removed\" listener to graph: "
1122 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1126 *out_listener_id
= listener_id
;
1129 return BT_GRAPH_STATUS_OK
;
1132 enum bt_graph_status
1133 bt_graph_add_source_filter_component_ports_connected_listener(
1134 struct bt_graph
*graph
,
1135 bt_graph_source_filter_component_ports_connected_listener_func func
,
1136 bt_graph_listener_removed_func listener_removed
, void *data
,
1137 int *out_listener_id
)
1139 struct bt_graph_listener_ports_connected listener
= {
1141 .removed
= listener_removed
,
1144 .func
= (ports_connected_func_t
) func
,
1148 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1149 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1150 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1151 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1152 "Graph currently executing a \"listener removed\" listener: "
1154 g_array_append_val(graph
->listeners
.source_filter_ports_connected
,
1156 listener_id
= graph
->listeners
.source_filter_ports_connected
->len
- 1;
1157 BT_LIB_LOGV("Added \"source to filter component ports connected\" listener to graph: "
1158 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1162 *out_listener_id
= listener_id
;
1165 return BT_GRAPH_STATUS_OK
;
1168 enum bt_graph_status
1169 bt_graph_add_source_sink_component_ports_connected_listener(
1170 struct bt_graph
*graph
,
1171 bt_graph_source_sink_component_ports_connected_listener_func func
,
1172 bt_graph_listener_removed_func listener_removed
, void *data
,
1173 int *out_listener_id
)
1175 struct bt_graph_listener_ports_connected listener
= {
1177 .removed
= listener_removed
,
1180 .func
= (ports_connected_func_t
) func
,
1184 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1185 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1186 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1187 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1188 "Graph currently executing a \"listener removed\" listener: "
1190 g_array_append_val(graph
->listeners
.source_sink_ports_connected
,
1192 listener_id
= graph
->listeners
.source_sink_ports_connected
->len
- 1;
1193 BT_LIB_LOGV("Added \"source to sink component ports connected\" listener to graph: "
1194 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1198 *out_listener_id
= listener_id
;
1201 return BT_GRAPH_STATUS_OK
;
1204 enum bt_graph_status
1205 bt_graph_add_filter_sink_component_ports_connected_listener(
1206 struct bt_graph
*graph
,
1207 bt_graph_filter_sink_component_ports_connected_listener_func func
,
1208 bt_graph_listener_removed_func listener_removed
, void *data
,
1209 int *out_listener_id
)
1211 struct bt_graph_listener_ports_connected listener
= {
1213 .removed
= listener_removed
,
1216 .func
= (ports_connected_func_t
) func
,
1220 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1221 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1222 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1223 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1224 "Graph currently executing a \"listener removed\" listener: "
1226 g_array_append_val(graph
->listeners
.filter_sink_ports_connected
,
1228 listener_id
= graph
->listeners
.filter_sink_ports_connected
->len
- 1;
1229 BT_LIB_LOGV("Added \"filter to sink component ports connected\" listener to graph: "
1230 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1234 *out_listener_id
= listener_id
;
1237 return BT_GRAPH_STATUS_OK
;
1240 enum bt_graph_status
1241 bt_graph_add_source_filter_component_ports_disconnected_listener(
1242 struct bt_graph
*graph
,
1243 bt_graph_source_filter_component_ports_disconnected_listener_func func
,
1244 bt_graph_listener_removed_func listener_removed
, void *data
,
1245 int *out_listener_id
)
1247 struct bt_graph_listener_ports_disconnected listener
= {
1249 .removed
= listener_removed
,
1252 .func
= (ports_disconnected_func_t
) func
,
1256 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1257 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1258 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1259 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1260 "Graph currently executing a \"listener removed\" listener: "
1262 g_array_append_val(graph
->listeners
.source_filter_ports_disconnected
,
1264 listener_id
= graph
->listeners
.source_filter_ports_disconnected
->len
- 1;
1265 BT_LIB_LOGV("Added \"source to filter component ports disconnected\" listener to graph: "
1266 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1270 *out_listener_id
= listener_id
;
1273 return BT_GRAPH_STATUS_OK
;
1276 enum bt_graph_status
1277 bt_graph_add_source_sink_component_ports_disconnected_listener(
1278 struct bt_graph
*graph
,
1279 bt_graph_source_sink_component_ports_disconnected_listener_func func
,
1280 bt_graph_listener_removed_func listener_removed
, void *data
,
1281 int *out_listener_id
)
1283 struct bt_graph_listener_ports_disconnected listener
= {
1285 .removed
= listener_removed
,
1288 .func
= (ports_disconnected_func_t
) func
,
1292 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1293 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1294 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1295 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1296 "Graph currently executing a \"listener removed\" listener: "
1298 g_array_append_val(graph
->listeners
.source_sink_ports_disconnected
,
1300 listener_id
= graph
->listeners
.source_sink_ports_disconnected
->len
- 1;
1301 BT_LIB_LOGV("Added \"source to sink component ports disconnected\" listener to graph: "
1302 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1306 *out_listener_id
= listener_id
;
1309 return BT_GRAPH_STATUS_OK
;
1312 enum bt_graph_status
1313 bt_graph_add_filter_sink_component_ports_disconnected_listener(
1314 struct bt_graph
*graph
,
1315 bt_graph_filter_sink_component_ports_disconnected_listener_func func
,
1316 bt_graph_listener_removed_func listener_removed
, void *data
,
1317 int *out_listener_id
)
1319 struct bt_graph_listener_ports_disconnected listener
= {
1321 .removed
= listener_removed
,
1324 .func
= (ports_disconnected_func_t
) func
,
1328 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1329 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1330 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1331 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1332 "Graph currently executing a \"listener removed\" listener: "
1334 g_array_append_val(graph
->listeners
.filter_sink_ports_disconnected
,
1336 listener_id
= graph
->listeners
.filter_sink_ports_disconnected
->len
- 1;
1337 BT_LIB_LOGV("Added \"filter to sink component ports disconnected\" listener to graph: "
1338 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1342 *out_listener_id
= listener_id
;
1345 return BT_GRAPH_STATUS_OK
;
1349 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
1353 struct bt_component
*comp
;
1357 BT_LIB_LOGV("Notifying graph listeners that a port was added: "
1358 "%![graph-]+g, %![port-]+p", graph
, port
);
1359 comp
= bt_port_borrow_component_inline(port
);
1362 switch (comp
->class->type
) {
1363 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1365 switch (port
->type
) {
1366 case BT_PORT_TYPE_OUTPUT
:
1367 listeners
= graph
->listeners
.source_output_port_added
;
1375 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1377 switch (port
->type
) {
1378 case BT_PORT_TYPE_INPUT
:
1379 listeners
= graph
->listeners
.filter_input_port_added
;
1381 case BT_PORT_TYPE_OUTPUT
:
1382 listeners
= graph
->listeners
.filter_output_port_added
;
1390 case BT_COMPONENT_CLASS_TYPE_SINK
:
1392 switch (port
->type
) {
1393 case BT_PORT_TYPE_INPUT
:
1394 listeners
= graph
->listeners
.sink_input_port_added
;
1406 for (i
= 0; i
< listeners
->len
; i
++) {
1407 struct bt_graph_listener_port_added
*listener
=
1408 &g_array_index(listeners
,
1409 struct bt_graph_listener_port_added
, i
);
1411 BT_ASSERT(listener
->func
);
1412 listener
->func(comp
, port
, listener
->base
.data
);
1417 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
1418 struct bt_component
*comp
, struct bt_port
*port
)
1425 BT_LIB_LOGV("Notifying graph listeners that a port was removed: "
1426 "%![graph-]+g, %![comp-]+c, %![port-]+p", graph
, comp
, port
);
1428 switch (comp
->class->type
) {
1429 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1431 switch (port
->type
) {
1432 case BT_PORT_TYPE_OUTPUT
:
1433 listeners
= graph
->listeners
.source_output_port_removed
;
1441 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1443 switch (port
->type
) {
1444 case BT_PORT_TYPE_INPUT
:
1445 listeners
= graph
->listeners
.filter_input_port_removed
;
1447 case BT_PORT_TYPE_OUTPUT
:
1448 listeners
= graph
->listeners
.filter_output_port_removed
;
1456 case BT_COMPONENT_CLASS_TYPE_SINK
:
1458 switch (port
->type
) {
1459 case BT_PORT_TYPE_INPUT
:
1460 listeners
= graph
->listeners
.sink_input_port_removed
;
1472 for (i
= 0; i
< listeners
->len
; i
++) {
1473 struct bt_graph_listener_port_removed
*listener
=
1474 &g_array_index(listeners
,
1475 struct bt_graph_listener_port_removed
, i
);
1477 BT_ASSERT(listener
->func
);
1478 listener
->func(comp
, port
, listener
->base
.data
);
1483 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
1484 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
1488 struct bt_component
*upstream_comp
;
1489 struct bt_component
*downstream_comp
;
1492 BT_ASSERT(upstream_port
);
1493 BT_ASSERT(downstream_port
);
1494 BT_LIB_LOGV("Notifying graph listeners that ports were connected: "
1495 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1496 graph
, upstream_port
, downstream_port
);
1497 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
1498 BT_ASSERT(upstream_comp
);
1499 downstream_comp
= bt_port_borrow_component_inline(downstream_port
);
1500 BT_ASSERT(downstream_comp
);
1502 switch (upstream_comp
->class->type
) {
1503 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1505 switch (downstream_comp
->class->type
) {
1506 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1508 graph
->listeners
.source_filter_ports_connected
;
1510 case BT_COMPONENT_CLASS_TYPE_SINK
:
1512 graph
->listeners
.source_sink_ports_connected
;
1520 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1522 switch (downstream_comp
->class->type
) {
1523 case BT_COMPONENT_CLASS_TYPE_SINK
:
1525 graph
->listeners
.filter_sink_ports_connected
;
1537 for (i
= 0; i
< listeners
->len
; i
++) {
1538 struct bt_graph_listener_ports_connected
*listener
=
1539 &g_array_index(listeners
,
1540 struct bt_graph_listener_ports_connected
, i
);
1542 BT_ASSERT(listener
->func
);
1543 listener
->func(upstream_comp
, downstream_comp
,
1544 upstream_port
, downstream_port
, listener
->base
.data
);
1549 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
1550 struct bt_component
*upstream_comp
,
1551 struct bt_component
*downstream_comp
,
1552 struct bt_port
*upstream_port
,
1553 struct bt_port
*downstream_port
)
1559 BT_ASSERT(upstream_comp
);
1560 BT_ASSERT(downstream_comp
);
1561 BT_ASSERT(upstream_port
);
1562 BT_ASSERT(downstream_port
);
1563 BT_LIB_LOGV("Notifying graph listeners that ports were disconnected: "
1564 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p, "
1565 "%![up-comp-]+c, %![down-comp-]+c",
1566 graph
, upstream_port
, downstream_port
, upstream_comp
,
1569 switch (upstream_comp
->class->type
) {
1570 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1572 switch (downstream_comp
->class->type
) {
1573 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1575 graph
->listeners
.source_filter_ports_disconnected
;
1577 case BT_COMPONENT_CLASS_TYPE_SINK
:
1579 graph
->listeners
.source_sink_ports_disconnected
;
1587 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1589 switch (downstream_comp
->class->type
) {
1590 case BT_COMPONENT_CLASS_TYPE_SINK
:
1592 graph
->listeners
.filter_sink_ports_disconnected
;
1604 for (i
= 0; i
< listeners
->len
; i
++) {
1605 struct bt_graph_listener_ports_disconnected
*listener
=
1606 &g_array_index(listeners
,
1607 struct bt_graph_listener_ports_disconnected
, i
);
1609 BT_ASSERT(listener
->func
);
1610 listener
->func(upstream_comp
, downstream_comp
,
1611 upstream_port
, downstream_port
, listener
->base
.data
);
1615 enum bt_graph_status
bt_graph_cancel(
1616 struct bt_graph
*graph
)
1619 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1620 graph
->canceled
= true;
1621 BT_LIB_LOGV("Canceled graph: %!+i", graph
);
1622 return BT_GRAPH_STATUS_OK
;
1625 bt_bool
bt_graph_is_canceled(const struct bt_graph
*graph
)
1627 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1628 return graph
->canceled
? BT_TRUE
: BT_FALSE
;
1632 void bt_graph_remove_connection(struct bt_graph
*graph
,
1633 struct bt_connection
*connection
)
1636 BT_ASSERT(connection
);
1637 BT_LIB_LOGV("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1639 g_ptr_array_remove(graph
->connections
, connection
);
1644 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
1646 bool exists
= false;
1649 for (i
= 0; i
< graph
->components
->len
; i
++) {
1650 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
1652 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1653 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1654 "%![other-comp-]+c, name=\"%s\"",
1666 enum bt_graph_status
add_component_with_init_method_data(
1667 struct bt_graph
*graph
,
1668 struct bt_component_class
*comp_cls
,
1669 comp_init_method_t init_method
,
1670 const char *name
, const struct bt_value
*params
,
1671 void *init_method_data
, struct bt_component
**user_component
)
1673 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
1674 enum bt_self_component_status comp_status
;
1675 struct bt_component
*component
= NULL
;
1677 bool init_can_consume
;
1678 struct bt_value
*new_params
= NULL
;
1680 BT_ASSERT(comp_cls
);
1681 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1682 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1683 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
1684 BT_ASSERT_PRE(!component_name_exists(graph
, name
),
1685 "Duplicate component name: %!+g, name=\"%s\"", graph
, name
);
1686 BT_ASSERT_PRE(!params
|| bt_value_is_map(params
),
1687 "Parameter value is not a map value: %!+v", params
);
1688 init_can_consume
= graph
->can_consume
;
1689 bt_graph_set_can_consume(graph
, false);
1690 BT_LIB_LOGD("Adding component to graph: "
1691 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1692 "init-method-data-addr=%p",
1693 graph
, comp_cls
, name
, params
, init_method_data
);
1696 new_params
= bt_value_map_create();
1698 BT_LOGE_STR("Cannot create map value object.");
1699 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1703 params
= new_params
;
1706 ret
= bt_component_create(comp_cls
, name
, &component
);
1708 BT_LOGE("Cannot create empty component object: ret=%d",
1710 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1715 * The user's initialization method needs to see that this
1716 * component is part of the graph. If the user method fails, we
1717 * immediately remove the component from the graph's components.
1719 g_ptr_array_add(graph
->components
, component
);
1720 bt_component_set_graph(component
, graph
);
1723 BT_LOGD_STR("Calling user's initialization method.");
1724 comp_status
= init_method(component
, params
, init_method_data
);
1725 BT_LOGD("User method returned: status=%s",
1726 bt_self_component_status_string(comp_status
));
1727 if (comp_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
1728 BT_LOGW_STR("Initialization method failed.");
1729 graph_status
= (int) comp_status
;
1730 bt_component_set_graph(component
, NULL
);
1731 g_ptr_array_remove_fast(graph
->components
, component
);
1737 * Mark the component as initialized so that its finalization
1738 * method is called when it is destroyed.
1740 component
->initialized
= true;
1743 * If it's a sink component, it needs to be part of the graph's
1744 * sink queue to be consumed by bt_graph_consume().
1746 if (bt_component_is_sink(component
)) {
1747 graph
->has_sink
= true;
1748 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1752 * Freeze the component class now that it's instantiated at
1755 BT_LOGD_STR("Freezing component class.");
1756 bt_component_class_freeze(comp_cls
);
1757 BT_LIB_LOGD("Added component to graph: "
1758 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1759 "init-method-data-addr=%p, %![comp-]+c",
1760 graph
, comp_cls
, name
, params
, init_method_data
, component
);
1762 if (user_component
) {
1763 /* Move reference to user */
1764 *user_component
= component
;
1769 bt_object_put_ref(component
);
1770 bt_object_put_ref(new_params
);
1771 (void) init_can_consume
;
1772 bt_graph_set_can_consume(graph
, init_can_consume
);
1773 return graph_status
;
1776 enum bt_graph_status
1777 bt_graph_add_source_component_with_init_method_data(
1778 struct bt_graph
*graph
,
1779 const struct bt_component_class_source
*comp_cls
,
1780 const char *name
, const struct bt_value
*params
,
1781 void *init_method_data
,
1782 const struct bt_component_source
**component
)
1784 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1785 return add_component_with_init_method_data(graph
,
1786 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1787 name
, params
, init_method_data
, (void *) component
);
1790 enum bt_graph_status
bt_graph_add_source_component(
1791 struct bt_graph
*graph
,
1792 const struct bt_component_class_source
*comp_cls
,
1793 const char *name
, const struct bt_value
*params
,
1794 const struct bt_component_source
**component
)
1796 return bt_graph_add_source_component_with_init_method_data(
1797 graph
, comp_cls
, name
, params
, NULL
, component
);
1800 enum bt_graph_status
1801 bt_graph_add_filter_component_with_init_method_data(
1802 struct bt_graph
*graph
,
1803 const struct bt_component_class_filter
*comp_cls
,
1804 const char *name
, const struct bt_value
*params
,
1805 void *init_method_data
,
1806 const struct bt_component_filter
**component
)
1808 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1809 return add_component_with_init_method_data(graph
,
1810 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1811 name
, params
, init_method_data
, (void *) component
);
1814 enum bt_graph_status
bt_graph_add_filter_component(
1815 struct bt_graph
*graph
,
1816 const struct bt_component_class_filter
*comp_cls
,
1817 const char *name
, const struct bt_value
*params
,
1818 const struct bt_component_filter
**component
)
1820 return bt_graph_add_filter_component_with_init_method_data(
1821 graph
, comp_cls
, name
, params
, NULL
, component
);
1824 enum bt_graph_status
1825 bt_graph_add_sink_component_with_init_method_data(
1826 struct bt_graph
*graph
,
1827 const struct bt_component_class_sink
*comp_cls
,
1828 const char *name
, const struct bt_value
*params
,
1829 void *init_method_data
,
1830 const struct bt_component_sink
**component
)
1832 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1833 return add_component_with_init_method_data(graph
,
1834 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1835 name
, params
, init_method_data
, (void *) component
);
1838 enum bt_graph_status
bt_graph_add_sink_component(
1839 struct bt_graph
*graph
,
1840 const struct bt_component_class_sink
*comp_cls
,
1841 const char *name
, const struct bt_value
*params
,
1842 const struct bt_component_sink
**component
)
1844 return bt_graph_add_sink_component_with_init_method_data(
1845 graph
, comp_cls
, name
, params
, NULL
, component
);
1849 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1850 struct bt_component
*component
)
1852 bool init_can_consume
;
1858 BT_ASSERT(component
);
1859 BT_ASSERT(component
->base
.ref_count
== 0);
1860 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1862 init_can_consume
= graph
->can_consume
;
1863 count
= bt_component_get_input_port_count(component
);
1865 for (i
= 0; i
< count
; i
++) {
1866 struct bt_port
*port
= (void *)
1867 bt_component_borrow_input_port_by_index(component
, i
);
1871 if (bt_port_is_connected(port
)) {
1872 BT_LIB_LOGW("Cannot remove component from graph: "
1873 "an input port is connected: "
1874 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1875 graph
, component
, port
);
1880 count
= bt_component_get_output_port_count(component
);
1882 for (i
= 0; i
< count
; i
++) {
1883 struct bt_port
*port
= (void *)
1884 bt_component_borrow_output_port_by_index(component
, i
);
1888 if (bt_port_is_connected(port
)) {
1889 BT_LIB_LOGW("Cannot remove component from graph: "
1890 "an output port is connected: "
1891 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1892 graph
, component
, port
);
1897 bt_graph_set_can_consume(graph
, false);
1899 /* Possibly remove from sinks to consume */
1900 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1902 if (graph
->sinks_to_consume
->length
== 0) {
1903 graph
->has_sink
= false;
1907 * This calls bt_object_try_spec_release() on the component, and
1908 * since its reference count is 0, its destructor is called. Its
1909 * destructor calls the user's finalization method (if set).
1911 g_ptr_array_remove(graph
->components
, component
);
1918 (void) init_can_consume
;
1919 bt_graph_set_can_consume(graph
, init_can_consume
);
1924 void bt_graph_add_notification(struct bt_graph
*graph
,
1925 struct bt_notification
*notif
)
1931 * It's okay not to take a reference because, when a
1932 * notification's reference count drops to 0, either:
1934 * * It is recycled back to one of this graph's pool.
1935 * * It is destroyed because it doesn't have any link to any
1936 * graph, which means the original graph is already destroyed.
1938 g_ptr_array_add(graph
->notifications
, notif
);