2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@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/assert-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/graph/component-internal.h>
30 #include <babeltrace/graph/graph.h>
31 #include <babeltrace/graph/graph-const.h>
32 #include <babeltrace/graph/graph-internal.h>
33 #include <babeltrace/graph/connection-internal.h>
34 #include <babeltrace/graph/component-sink-internal.h>
35 #include <babeltrace/graph/component-source-const.h>
36 #include <babeltrace/graph/component-filter-const.h>
37 #include <babeltrace/graph/port-const.h>
38 #include <babeltrace/graph/notification-internal.h>
39 #include <babeltrace/graph/notification-event-internal.h>
40 #include <babeltrace/graph/notification-packet-internal.h>
41 #include <babeltrace/compiler-internal.h>
42 #include <babeltrace/common-internal.h>
43 #include <babeltrace/types.h>
44 #include <babeltrace/value.h>
45 #include <babeltrace/value-const.h>
46 #include <babeltrace/value-internal.h>
50 typedef void (*port_added_func_t
)(const void *, const void *, void *);
52 typedef void (*port_removed_func_t
)(const void *, const void *, void *);
54 typedef void (*ports_connected_func_t
)(const void *, const void *, const void *,
55 const void *, void *);
57 typedef void (*ports_disconnected_func_t
)(const void *, const void *,
58 const void *, const void *, void *);
60 typedef enum bt_self_component_status (*comp_init_method_t
)(const void *,
61 const void *, void *);
63 struct bt_graph_listener
{
64 bt_graph_listener_removed_func removed
;
68 struct bt_graph_listener_port_added
{
69 struct bt_graph_listener base
;
70 port_added_func_t func
;
73 struct bt_graph_listener_port_removed
{
74 struct bt_graph_listener base
;
75 port_removed_func_t func
;
78 struct bt_graph_listener_ports_connected
{
79 struct bt_graph_listener base
;
80 ports_connected_func_t func
;
83 struct bt_graph_listener_ports_disconnected
{
84 struct bt_graph_listener base
;
85 ports_disconnected_func_t func
;
88 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
90 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
91 if (!(_listeners)) { \
92 BT_LOGE_STR("Failed to allocate one GArray."); \
96 #define CALL_REMOVE_LISTENERS(_type, _listeners) \
100 for (i = 0; i < (_listeners)->len; i++) { \
102 &g_array_index((_listeners), _type, i); \
104 if (listener->base.removed) { \
105 listener->base.removed(listener->base.data); \
111 void destroy_graph(struct bt_object
*obj
)
113 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
116 * The graph's reference count is 0 if we're here. Increment
117 * it to avoid a double-destroy (possibly infinitely recursive)
120 * 1. We put and destroy a connection.
121 * 2. This connection's destructor finalizes its active
122 * notification iterators.
123 * 3. A notification iterator's finalization function gets a
124 * new reference on its component (reference count goes from
126 * 4. Since this component's reference count goes to 1, it takes
127 * a reference on its parent (this graph). This graph's
128 * reference count goes from 0 to 1.
129 * 5. The notification iterator's finalization function puts its
130 * component reference (reference count goes from 1 to 0).
131 * 6. Since this component's reference count goes from 1 to 0,
132 * it puts its parent (this graph). This graph's reference
133 * count goes from 1 to 0.
134 * 7. Since this graph's reference count goes from 1 to 0,
135 * its destructor is called (this function).
137 * With the incrementation below, the graph's reference count at
138 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
139 * ensures that this function is not called two times.
141 BT_LIB_LOGD("Destroying graph: %!+g", graph
);
145 * Cancel the graph to disallow some operations, like creating
146 * notification iterators and adding ports to components.
148 (void) bt_graph_cancel((void *) graph
);
150 /* Call all remove listeners */
151 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
152 graph
->listeners
.source_output_port_added
);
153 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
154 graph
->listeners
.filter_output_port_added
);
155 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
156 graph
->listeners
.filter_input_port_added
);
157 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
158 graph
->listeners
.sink_input_port_added
);
159 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed
,
160 graph
->listeners
.source_output_port_removed
);
161 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed
,
162 graph
->listeners
.filter_output_port_removed
);
163 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed
,
164 graph
->listeners
.filter_input_port_removed
);
165 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed
,
166 graph
->listeners
.sink_input_port_removed
);
167 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
168 graph
->listeners
.source_filter_ports_connected
);
169 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
170 graph
->listeners
.source_sink_ports_connected
);
171 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
172 graph
->listeners
.filter_sink_ports_connected
);
173 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected
,
174 graph
->listeners
.source_filter_ports_disconnected
);
175 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected
,
176 graph
->listeners
.source_sink_ports_disconnected
);
177 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected
,
178 graph
->listeners
.filter_sink_ports_disconnected
);
180 if (graph
->notifications
) {
181 g_ptr_array_free(graph
->notifications
, TRUE
);
182 graph
->notifications
= NULL
;
185 if (graph
->connections
) {
186 BT_LOGD_STR("Destroying connections.");
187 g_ptr_array_free(graph
->connections
, TRUE
);
188 graph
->connections
= NULL
;
191 if (graph
->components
) {
192 BT_LOGD_STR("Destroying components.");
193 g_ptr_array_free(graph
->components
, TRUE
);
194 graph
->components
= NULL
;
197 if (graph
->sinks_to_consume
) {
198 g_queue_free(graph
->sinks_to_consume
);
199 graph
->sinks_to_consume
= NULL
;
202 if (graph
->listeners
.source_output_port_added
) {
203 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
204 graph
->listeners
.source_output_port_added
= NULL
;
207 if (graph
->listeners
.filter_output_port_added
) {
208 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
209 graph
->listeners
.filter_output_port_added
= NULL
;
212 if (graph
->listeners
.filter_input_port_added
) {
213 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
214 graph
->listeners
.filter_input_port_added
= NULL
;
217 if (graph
->listeners
.sink_input_port_added
) {
218 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
219 graph
->listeners
.sink_input_port_added
= NULL
;
222 if (graph
->listeners
.source_output_port_removed
) {
223 g_array_free(graph
->listeners
.source_output_port_removed
, TRUE
);
224 graph
->listeners
.source_output_port_removed
= NULL
;
227 if (graph
->listeners
.filter_output_port_removed
) {
228 g_array_free(graph
->listeners
.filter_output_port_removed
, TRUE
);
229 graph
->listeners
.filter_output_port_removed
= NULL
;
232 if (graph
->listeners
.filter_input_port_removed
) {
233 g_array_free(graph
->listeners
.filter_input_port_removed
, TRUE
);
234 graph
->listeners
.filter_input_port_removed
= NULL
;
237 if (graph
->listeners
.sink_input_port_removed
) {
238 g_array_free(graph
->listeners
.sink_input_port_removed
, TRUE
);
239 graph
->listeners
.sink_input_port_removed
= NULL
;
242 if (graph
->listeners
.source_filter_ports_connected
) {
243 g_array_free(graph
->listeners
.source_filter_ports_connected
,
245 graph
->listeners
.source_filter_ports_connected
= NULL
;
248 if (graph
->listeners
.source_sink_ports_connected
) {
249 g_array_free(graph
->listeners
.source_sink_ports_connected
,
251 graph
->listeners
.source_sink_ports_connected
= NULL
;
254 if (graph
->listeners
.filter_sink_ports_connected
) {
255 g_array_free(graph
->listeners
.filter_sink_ports_connected
,
257 graph
->listeners
.filter_sink_ports_connected
= NULL
;
260 if (graph
->listeners
.source_filter_ports_disconnected
) {
261 g_array_free(graph
->listeners
.source_filter_ports_disconnected
,
263 graph
->listeners
.source_filter_ports_disconnected
= NULL
;
266 if (graph
->listeners
.source_sink_ports_disconnected
) {
267 g_array_free(graph
->listeners
.source_sink_ports_disconnected
,
269 graph
->listeners
.source_sink_ports_disconnected
= NULL
;
272 if (graph
->listeners
.filter_sink_ports_disconnected
) {
273 g_array_free(graph
->listeners
.filter_sink_ports_disconnected
,
275 graph
->listeners
.filter_sink_ports_disconnected
= NULL
;
278 bt_object_pool_finalize(&graph
->event_notif_pool
);
279 bt_object_pool_finalize(&graph
->packet_begin_notif_pool
);
280 bt_object_pool_finalize(&graph
->packet_end_notif_pool
);
285 void destroy_notification_event(struct bt_notification
*notif
,
286 struct bt_graph
*graph
)
288 bt_notification_event_destroy(notif
);
292 void destroy_notification_packet_begin(struct bt_notification
*notif
,
293 struct bt_graph
*graph
)
295 bt_notification_packet_beginning_destroy(notif
);
299 void destroy_notification_packet_end(struct bt_notification
*notif
,
300 struct bt_graph
*graph
)
302 bt_notification_packet_end_destroy(notif
);
306 void notify_notification_graph_is_destroyed(struct bt_notification
*notif
)
308 bt_notification_unlink_graph(notif
);
311 struct bt_graph
*bt_graph_create(void)
313 struct bt_graph
*graph
;
316 BT_LOGD_STR("Creating graph object.");
317 graph
= g_new0(struct bt_graph
, 1);
319 BT_LOGE_STR("Failed to allocate one graph.");
323 bt_object_init_shared(&graph
->base
, destroy_graph
);
324 graph
->connections
= g_ptr_array_new_with_free_func(
325 (GDestroyNotify
) bt_object_try_spec_release
);
326 if (!graph
->connections
) {
327 BT_LOGE_STR("Failed to allocate one GPtrArray.");
330 graph
->components
= g_ptr_array_new_with_free_func(
331 (GDestroyNotify
) bt_object_try_spec_release
);
332 if (!graph
->components
) {
333 BT_LOGE_STR("Failed to allocate one GPtrArray.");
336 graph
->sinks_to_consume
= g_queue_new();
337 if (!graph
->sinks_to_consume
) {
338 BT_LOGE_STR("Failed to allocate one GQueue.");
342 bt_graph_set_can_consume(graph
, true);
343 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
344 graph
->listeners
.source_output_port_added
);
346 if (!graph
->listeners
.source_output_port_added
) {
351 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
352 graph
->listeners
.filter_output_port_added
);
354 if (!graph
->listeners
.filter_output_port_added
) {
359 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
360 graph
->listeners
.filter_input_port_added
);
362 if (!graph
->listeners
.filter_input_port_added
) {
367 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
368 graph
->listeners
.sink_input_port_added
);
370 if (!graph
->listeners
.sink_input_port_added
) {
375 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed
,
376 graph
->listeners
.source_output_port_removed
);
378 if (!graph
->listeners
.source_output_port_removed
) {
383 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed
,
384 graph
->listeners
.filter_output_port_removed
);
386 if (!graph
->listeners
.filter_output_port_removed
) {
391 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed
,
392 graph
->listeners
.filter_input_port_removed
);
394 if (!graph
->listeners
.filter_input_port_removed
) {
399 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed
,
400 graph
->listeners
.sink_input_port_removed
);
402 if (!graph
->listeners
.sink_input_port_removed
) {
407 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
408 graph
->listeners
.source_filter_ports_connected
);
410 if (!graph
->listeners
.source_filter_ports_connected
) {
415 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
416 graph
->listeners
.source_sink_ports_connected
);
418 if (!graph
->listeners
.source_sink_ports_connected
) {
423 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
424 graph
->listeners
.filter_sink_ports_connected
);
426 if (!graph
->listeners
.filter_sink_ports_connected
) {
431 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected
,
432 graph
->listeners
.source_filter_ports_disconnected
);
434 if (!graph
->listeners
.source_filter_ports_disconnected
) {
439 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected
,
440 graph
->listeners
.source_sink_ports_disconnected
);
442 if (!graph
->listeners
.source_sink_ports_disconnected
) {
447 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected
,
448 graph
->listeners
.filter_sink_ports_disconnected
);
450 if (!graph
->listeners
.filter_sink_ports_disconnected
) {
455 ret
= bt_object_pool_initialize(&graph
->event_notif_pool
,
456 (bt_object_pool_new_object_func
) bt_notification_event_new
,
457 (bt_object_pool_destroy_object_func
) destroy_notification_event
,
460 BT_LOGE("Failed to initialize event notification pool: ret=%d",
465 ret
= bt_object_pool_initialize(&graph
->packet_begin_notif_pool
,
466 (bt_object_pool_new_object_func
) bt_notification_packet_beginning_new
,
467 (bt_object_pool_destroy_object_func
) destroy_notification_packet_begin
,
470 BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d",
475 ret
= bt_object_pool_initialize(&graph
->packet_end_notif_pool
,
476 (bt_object_pool_new_object_func
) bt_notification_packet_end_new
,
477 (bt_object_pool_destroy_object_func
) destroy_notification_packet_end
,
480 BT_LOGE("Failed to initialize packet end notification pool: ret=%d",
485 graph
->notifications
= g_ptr_array_new_with_free_func(
486 (GDestroyNotify
) notify_notification_graph_is_destroyed
);
487 BT_LIB_LOGD("Created graph object: %!+g", graph
);
490 return (void *) graph
;
493 BT_OBJECT_PUT_REF_AND_RESET(graph
);
497 enum bt_graph_status
bt_graph_connect_ports(
498 struct bt_graph
*graph
,
499 const struct bt_port_output
*upstream_port_out
,
500 const struct bt_port_input
*downstream_port_in
,
501 const struct bt_connection
**user_connection
)
503 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
504 struct bt_connection
*connection
= NULL
;
505 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
506 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
507 struct bt_component
*upstream_component
= NULL
;
508 struct bt_component
*downstream_component
= NULL
;
509 enum bt_self_component_status component_status
;
510 bool init_can_consume
;
512 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
513 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
514 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
515 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
516 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
517 "Upstream port is already connected: %!+p", upstream_port
);
518 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
519 "Downstream port is already connected: %!+p", downstream_port
);
520 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
521 "Upstream port does not belong to a component: %!+p",
523 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
524 "Downstream port does not belong to a component: %!+p",
526 init_can_consume
= graph
->can_consume
;
527 BT_LIB_LOGD("Connecting component ports within graph: "
528 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
529 graph
, upstream_port
, downstream_port
);
530 bt_graph_set_can_consume(graph
, false);
531 upstream_component
= bt_port_borrow_component_inline(
532 (void *) upstream_port
);
533 downstream_component
= bt_port_borrow_component_inline(
534 (void *) downstream_port
);
537 * At this point the ports are not connected yet. Both
538 * components need to accept an eventual connection to their
539 * port by the other port before we continue.
541 BT_LIB_LOGD("Asking upstream component to accept the connection: "
542 "%![comp-]+c", upstream_component
);
543 component_status
= bt_component_accept_port_connection(
544 upstream_component
, (void *) upstream_port
,
545 (void *) downstream_port
);
546 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
547 if (component_status
== BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
548 BT_LOGD_STR("Upstream component refused the connection.");
550 BT_LOGW("Cannot ask upstream component to accept the connection: "
551 "status=%s", bt_self_component_status_string(component_status
));
554 status
= (int) component_status
;
558 BT_LIB_LOGD("Asking downstream component to accept the connection: "
559 "%![comp-]+c", downstream_component
);
560 component_status
= bt_component_accept_port_connection(
561 downstream_component
, (void *) downstream_port
,
562 (void *) upstream_port
);
563 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
564 if (component_status
== BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
565 BT_LOGD_STR("Downstream component refused the connection.");
567 BT_LOGW("Cannot ask downstream component to accept the connection: "
568 "status=%s", bt_self_component_status_string(component_status
));
571 status
= (int) component_status
;
575 BT_LOGD_STR("Creating connection.");
576 connection
= bt_connection_create(graph
, (void *) upstream_port
,
577 (void *) downstream_port
);
579 BT_LOGW("Cannot create connection object.");
580 status
= BT_GRAPH_STATUS_NOMEM
;
584 BT_LIB_LOGD("Connection object created: %!+x", connection
);
587 * Ownership of upstream_component/downstream_component and of
588 * the connection object is transferred to the graph.
590 g_ptr_array_add(graph
->connections
, connection
);
593 * Notify both components that their port is connected.
595 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
596 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
597 component_status
= bt_component_port_connected(upstream_component
,
598 (void *) upstream_port
, (void *) downstream_port
);
599 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
600 BT_LIB_LOGW("Error while notifying upstream component that its port is connected: "
601 "status=%s, %![graph-]+g, %![up-comp-]+c, "
602 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
603 bt_self_component_status_string(component_status
),
604 graph
, upstream_component
, downstream_component
,
605 upstream_port
, downstream_port
);
606 bt_connection_end(connection
, true);
607 status
= (int) component_status
;
611 connection
->notified_upstream_port_connected
= true;
612 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
613 "%![comp-]+c, %![port-]+p", downstream_component
,
615 component_status
= bt_component_port_connected(downstream_component
,
616 (void *) downstream_port
, (void *) upstream_port
);
617 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
618 BT_LIB_LOGW("Error while notifying downstream component that its port is connected: "
619 "status=%s, %![graph-]+g, %![up-comp-]+c, "
620 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
621 bt_self_component_status_string(component_status
),
622 graph
, upstream_component
, downstream_component
,
623 upstream_port
, downstream_port
);
624 bt_connection_end(connection
, true);
625 status
= (int) component_status
;
629 connection
->notified_downstream_port_connected
= true;
632 * Notify the graph's creator that both ports are connected.
634 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
635 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
636 connection
->notified_graph_ports_connected
= true;
637 BT_LIB_LOGD("Connected component ports within graph: "
638 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
639 "%![up-port-]+p, %![down-port-]+p",
640 graph
, upstream_component
, downstream_component
,
641 upstream_port
, downstream_port
);
643 if (user_connection
) {
644 /* Move reference to user */
645 *user_connection
= connection
;
650 bt_object_put_ref(connection
);
651 (void) init_can_consume
;
652 bt_graph_set_can_consume(graph
, init_can_consume
);
657 enum bt_graph_status
consume_graph_sink(struct bt_component_sink
*comp
)
659 enum bt_self_component_status comp_status
;
660 struct bt_component_class_sink
*sink_class
= NULL
;
663 sink_class
= (void *) comp
->parent
.class;
664 BT_ASSERT(sink_class
->methods
.consume
);
665 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
666 comp_status
= sink_class
->methods
.consume((void *) comp
);
667 BT_LOGD("User method returned: status=%s",
668 bt_self_component_status_string(comp_status
));
669 BT_ASSERT_PRE(comp_status
== BT_SELF_COMPONENT_STATUS_OK
||
670 comp_status
== BT_SELF_COMPONENT_STATUS_END
||
671 comp_status
== BT_SELF_COMPONENT_STATUS_AGAIN
||
672 comp_status
== BT_SELF_COMPONENT_STATUS_ERROR
||
673 comp_status
== BT_SELF_COMPONENT_STATUS_NOMEM
,
674 "Invalid component status returned by consuming method: "
675 "status=%s", bt_self_component_status_string(comp_status
));
676 if (comp_status
< 0) {
677 BT_LOGW_STR("Consume method failed.");
681 BT_LIB_LOGV("Consumed from sink: %![comp-]+c, status=%s",
682 comp
, bt_self_component_status_string(comp_status
));
685 return (int) comp_status
;
689 * `node` is removed from the queue of sinks to consume when passed to
690 * this function. This function adds it back to the queue if there's
691 * still something to consume afterwards.
694 enum bt_graph_status
consume_sink_node(struct bt_graph
*graph
, GList
*node
)
696 enum bt_graph_status status
;
697 struct bt_component_sink
*sink
;
700 status
= consume_graph_sink(sink
);
701 if (unlikely(status
!= BT_GRAPH_STATUS_END
)) {
702 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
706 /* End reached, the node is not added back to the queue and free'd. */
707 g_queue_delete_link(graph
->sinks_to_consume
, node
);
709 /* Don't forward an END status if there are sinks left to consume. */
710 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
711 status
= BT_GRAPH_STATUS_OK
;
716 BT_LIB_LOGV("Consumed sink node: %![comp-]+c, status=%s",
717 sink
, bt_graph_status_string(status
));
722 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
723 struct bt_component_sink
*sink
)
725 enum bt_graph_status status
;
729 BT_LIB_LOGV("Making specific sink consume: %![comp-]+c", sink
);
730 BT_ASSERT(bt_component_borrow_graph((void *) sink
) == graph
);
732 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
733 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
734 status
= BT_GRAPH_STATUS_END
;
738 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
740 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
741 status
= BT_GRAPH_STATUS_END
;
745 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
746 BT_ASSERT(sink_node
);
747 status
= consume_sink_node(graph
, sink_node
);
754 enum bt_graph_status
consume_no_check(struct bt_graph
*graph
)
756 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
757 struct bt_component
*sink
;
760 BT_ASSERT_PRE(graph
->has_sink
,
761 "Graph has no sink component: %!+g", graph
);
762 BT_LIB_LOGV("Making next sink consume: %![graph-]+g", graph
);
764 if (unlikely(g_queue_is_empty(graph
->sinks_to_consume
))) {
765 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
766 status
= BT_GRAPH_STATUS_END
;
770 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
771 sink
= current_node
->data
;
772 BT_LIB_LOGV("Chose next sink to consume: %!+c", sink
);
773 status
= consume_sink_node(graph
, current_node
);
779 enum bt_graph_status
bt_graph_consume(
780 struct bt_graph
*graph
)
782 enum bt_graph_status status
;
784 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
785 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
786 BT_ASSERT_PRE(graph
->can_consume
,
787 "Cannot consume graph in its current state: %!+g", graph
);
788 bt_graph_set_can_consume(graph
, BT_FALSE
);
789 status
= consume_no_check(graph
);
790 bt_graph_set_can_consume(graph
, BT_TRUE
);
794 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
796 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
798 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
799 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
800 BT_ASSERT_PRE(graph
->can_consume
,
801 "Cannot consume graph in its current state: %!+g", graph
);
802 bt_graph_set_can_consume(graph
, BT_FALSE
);
803 BT_LIB_LOGV("Running graph: %!+g", graph
);
807 * Check if the graph is canceled at each iteration. If
808 * the graph was canceled by another thread or by a
809 * signal handler, this is not a warning nor an error,
810 * it was intentional: log with a DEBUG level only.
812 if (unlikely(graph
->canceled
)) {
813 BT_LIB_LOGD("Stopping the graph: graph is canceled: "
815 status
= BT_GRAPH_STATUS_CANCELED
;
819 status
= consume_no_check(graph
);
820 if (unlikely(status
== BT_GRAPH_STATUS_AGAIN
)) {
822 * If AGAIN is received and there are multiple
823 * sinks, go ahead and consume from the next
826 * However, in the case where a single sink is
827 * left, the caller can decide to busy-wait and
828 * call bt_graph_run() continuously
829 * until the source is ready or it can decide to
830 * sleep for an arbitrary amount of time.
832 if (graph
->sinks_to_consume
->length
> 1) {
833 status
= BT_GRAPH_STATUS_OK
;
835 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
838 } while (status
== BT_GRAPH_STATUS_OK
);
840 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
841 status
= BT_GRAPH_STATUS_END
;
845 BT_LIB_LOGV("Graph ran: %![graph-]+g, status=%s", graph
,
846 bt_graph_status_string(status
));
847 bt_graph_set_can_consume(graph
, BT_TRUE
);
852 bt_graph_add_source_component_output_port_added_listener(
853 struct bt_graph
*graph
,
854 bt_graph_source_component_output_port_added_listener_func func
,
855 bt_graph_listener_removed_func listener_removed
, void *data
,
856 int *out_listener_id
)
858 struct bt_graph_listener_port_added listener
= {
860 .removed
= listener_removed
,
863 .func
= (port_added_func_t
) func
,
867 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
868 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
869 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
870 BT_ASSERT_PRE(!graph
->in_remove_listener
,
871 "Graph currently executing a \"listener removed\" listener: "
873 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
874 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
875 BT_LIB_LOGV("Added \"source component output port added\" listener to graph: "
876 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
880 *out_listener_id
= listener_id
;
883 return BT_GRAPH_STATUS_OK
;
887 bt_graph_add_filter_component_output_port_added_listener(
888 struct bt_graph
*graph
,
889 bt_graph_filter_component_output_port_added_listener_func func
,
890 bt_graph_listener_removed_func listener_removed
, void *data
,
891 int *out_listener_id
)
893 struct bt_graph_listener_port_added listener
= {
895 .removed
= listener_removed
,
898 .func
= (port_added_func_t
) func
,
902 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
903 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
904 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
905 BT_ASSERT_PRE(!graph
->in_remove_listener
,
906 "Graph currently executing a \"listener removed\" listener: "
908 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
909 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
910 BT_LIB_LOGV("Added \"filter component output port added\" listener to graph: "
911 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
915 *out_listener_id
= listener_id
;
918 return BT_GRAPH_STATUS_OK
;
922 bt_graph_add_filter_component_input_port_added_listener(
923 struct bt_graph
*graph
,
924 bt_graph_filter_component_input_port_added_listener_func func
,
925 bt_graph_listener_removed_func listener_removed
, void *data
,
926 int *out_listener_id
)
928 struct bt_graph_listener_port_added listener
= {
930 .removed
= listener_removed
,
933 .func
= (port_added_func_t
) func
,
937 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
938 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
939 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
940 BT_ASSERT_PRE(!graph
->in_remove_listener
,
941 "Graph currently executing a \"listener removed\" listener: "
943 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
944 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
945 BT_LIB_LOGV("Added \"filter component input port added\" listener to graph: "
946 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
950 *out_listener_id
= listener_id
;
953 return BT_GRAPH_STATUS_OK
;
957 bt_graph_add_sink_component_input_port_added_listener(
958 struct bt_graph
*graph
,
959 bt_graph_sink_component_input_port_added_listener_func func
,
960 bt_graph_listener_removed_func listener_removed
, void *data
,
961 int *out_listener_id
)
963 struct bt_graph_listener_port_added listener
= {
965 .removed
= listener_removed
,
968 .func
= (port_added_func_t
) func
,
972 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
973 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
974 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
975 BT_ASSERT_PRE(!graph
->in_remove_listener
,
976 "Graph currently executing a \"listener removed\" listener: "
978 g_array_append_val(graph
->listeners
.sink_input_port_added
, listener
);
979 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
980 BT_LIB_LOGV("Added \"sink component input port added\" listener to graph: "
981 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
985 *out_listener_id
= listener_id
;
988 return BT_GRAPH_STATUS_OK
;
992 bt_graph_add_source_component_output_port_removed_listener(
993 struct bt_graph
*graph
,
994 bt_graph_source_component_output_port_removed_listener_func func
,
995 bt_graph_listener_removed_func listener_removed
, void *data
,
996 int *out_listener_id
)
998 struct bt_graph_listener_port_removed listener
= {
1000 .removed
= listener_removed
,
1003 .func
= (port_removed_func_t
) func
,
1007 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1008 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1009 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1010 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1011 "Graph currently executing a \"listener removed\" listener: "
1013 g_array_append_val(graph
->listeners
.source_output_port_removed
, listener
);
1014 listener_id
= graph
->listeners
.source_output_port_removed
->len
- 1;
1015 BT_LIB_LOGV("Added \"source component output port removed\" listener to graph: "
1016 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1020 *out_listener_id
= listener_id
;
1023 return BT_GRAPH_STATUS_OK
;
1026 enum bt_graph_status
1027 bt_graph_add_filter_component_output_port_removed_listener(
1028 struct bt_graph
*graph
,
1029 bt_graph_filter_component_output_port_removed_listener_func func
,
1030 bt_graph_listener_removed_func listener_removed
, void *data
,
1031 int *out_listener_id
)
1033 struct bt_graph_listener_port_removed listener
= {
1035 .removed
= listener_removed
,
1038 .func
= (port_removed_func_t
) func
,
1042 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1043 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1044 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1045 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1046 "Graph currently executing a \"listener removed\" listener: "
1048 g_array_append_val(graph
->listeners
.filter_output_port_removed
, listener
);
1049 listener_id
= graph
->listeners
.filter_output_port_removed
->len
- 1;
1050 BT_LIB_LOGV("Added \"filter component output port removed\" listener to graph: "
1051 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1055 *out_listener_id
= listener_id
;
1058 return BT_GRAPH_STATUS_OK
;
1061 enum bt_graph_status
1062 bt_graph_add_filter_component_input_port_removed_listener(
1063 struct bt_graph
*graph
,
1064 bt_graph_filter_component_input_port_removed_listener_func func
,
1065 bt_graph_listener_removed_func listener_removed
, void *data
,
1066 int *out_listener_id
)
1068 struct bt_graph_listener_port_removed listener
= {
1070 .removed
= listener_removed
,
1073 .func
= (port_removed_func_t
) func
,
1077 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1078 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1079 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1080 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1081 "Graph currently executing a \"listener removed\" listener: "
1083 g_array_append_val(graph
->listeners
.filter_input_port_removed
, listener
);
1084 listener_id
= graph
->listeners
.filter_input_port_removed
->len
- 1;
1085 BT_LIB_LOGV("Added \"filter component input port removed\" listener to graph: "
1086 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1090 *out_listener_id
= listener_id
;
1093 return BT_GRAPH_STATUS_OK
;
1096 enum bt_graph_status
1097 bt_graph_add_sink_component_input_port_removed_listener(
1098 struct bt_graph
*graph
,
1099 bt_graph_sink_component_input_port_removed_listener_func func
,
1100 bt_graph_listener_removed_func listener_removed
, void *data
,
1101 int *out_listener_id
)
1103 struct bt_graph_listener_port_removed listener
= {
1105 .removed
= listener_removed
,
1108 .func
= (port_removed_func_t
) func
,
1112 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1113 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1114 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1115 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1116 "Graph currently executing a \"listener removed\" listener: "
1118 g_array_append_val(graph
->listeners
.sink_input_port_removed
, listener
);
1119 listener_id
= graph
->listeners
.sink_input_port_removed
->len
- 1;
1120 BT_LIB_LOGV("Added \"sink component input port removed\" listener to graph: "
1121 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1125 *out_listener_id
= listener_id
;
1128 return BT_GRAPH_STATUS_OK
;
1131 enum bt_graph_status
1132 bt_graph_add_source_filter_component_ports_connected_listener(
1133 struct bt_graph
*graph
,
1134 bt_graph_source_filter_component_ports_connected_listener_func func
,
1135 bt_graph_listener_removed_func listener_removed
, void *data
,
1136 int *out_listener_id
)
1138 struct bt_graph_listener_ports_connected listener
= {
1140 .removed
= listener_removed
,
1143 .func
= (ports_connected_func_t
) func
,
1147 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1148 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1149 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1150 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1151 "Graph currently executing a \"listener removed\" listener: "
1153 g_array_append_val(graph
->listeners
.source_filter_ports_connected
,
1155 listener_id
= graph
->listeners
.source_filter_ports_connected
->len
- 1;
1156 BT_LIB_LOGV("Added \"source to filter component ports connected\" listener to graph: "
1157 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1161 *out_listener_id
= listener_id
;
1164 return BT_GRAPH_STATUS_OK
;
1167 enum bt_graph_status
1168 bt_graph_add_source_sink_component_ports_connected_listener(
1169 struct bt_graph
*graph
,
1170 bt_graph_source_sink_component_ports_connected_listener_func func
,
1171 bt_graph_listener_removed_func listener_removed
, void *data
,
1172 int *out_listener_id
)
1174 struct bt_graph_listener_ports_connected listener
= {
1176 .removed
= listener_removed
,
1179 .func
= (ports_connected_func_t
) func
,
1183 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1184 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1185 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1186 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1187 "Graph currently executing a \"listener removed\" listener: "
1189 g_array_append_val(graph
->listeners
.source_sink_ports_connected
,
1191 listener_id
= graph
->listeners
.source_sink_ports_connected
->len
- 1;
1192 BT_LIB_LOGV("Added \"source to sink component ports connected\" listener to graph: "
1193 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1197 *out_listener_id
= listener_id
;
1200 return BT_GRAPH_STATUS_OK
;
1203 enum bt_graph_status
1204 bt_graph_add_filter_sink_component_ports_connected_listener(
1205 struct bt_graph
*graph
,
1206 bt_graph_filter_sink_component_ports_connected_listener_func func
,
1207 bt_graph_listener_removed_func listener_removed
, void *data
,
1208 int *out_listener_id
)
1210 struct bt_graph_listener_ports_connected listener
= {
1212 .removed
= listener_removed
,
1215 .func
= (ports_connected_func_t
) func
,
1219 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1220 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1221 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1222 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1223 "Graph currently executing a \"listener removed\" listener: "
1225 g_array_append_val(graph
->listeners
.filter_sink_ports_connected
,
1227 listener_id
= graph
->listeners
.filter_sink_ports_connected
->len
- 1;
1228 BT_LIB_LOGV("Added \"filter to sink component ports connected\" listener to graph: "
1229 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1233 *out_listener_id
= listener_id
;
1236 return BT_GRAPH_STATUS_OK
;
1239 enum bt_graph_status
1240 bt_graph_add_source_filter_component_ports_disconnected_listener(
1241 struct bt_graph
*graph
,
1242 bt_graph_source_filter_component_ports_disconnected_listener_func func
,
1243 bt_graph_listener_removed_func listener_removed
, void *data
,
1244 int *out_listener_id
)
1246 struct bt_graph_listener_ports_disconnected listener
= {
1248 .removed
= listener_removed
,
1251 .func
= (ports_disconnected_func_t
) func
,
1255 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1256 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1257 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1258 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1259 "Graph currently executing a \"listener removed\" listener: "
1261 g_array_append_val(graph
->listeners
.source_filter_ports_disconnected
,
1263 listener_id
= graph
->listeners
.source_filter_ports_disconnected
->len
- 1;
1264 BT_LIB_LOGV("Added \"source to filter component ports disconnected\" listener to graph: "
1265 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1269 *out_listener_id
= listener_id
;
1272 return BT_GRAPH_STATUS_OK
;
1275 enum bt_graph_status
1276 bt_graph_add_source_sink_component_ports_disconnected_listener(
1277 struct bt_graph
*graph
,
1278 bt_graph_source_sink_component_ports_disconnected_listener_func func
,
1279 bt_graph_listener_removed_func listener_removed
, void *data
,
1280 int *out_listener_id
)
1282 struct bt_graph_listener_ports_disconnected listener
= {
1284 .removed
= listener_removed
,
1287 .func
= (ports_disconnected_func_t
) func
,
1291 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1292 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1293 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1294 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1295 "Graph currently executing a \"listener removed\" listener: "
1297 g_array_append_val(graph
->listeners
.source_sink_ports_disconnected
,
1299 listener_id
= graph
->listeners
.source_sink_ports_disconnected
->len
- 1;
1300 BT_LIB_LOGV("Added \"source to sink component ports disconnected\" listener to graph: "
1301 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1305 *out_listener_id
= listener_id
;
1308 return BT_GRAPH_STATUS_OK
;
1311 enum bt_graph_status
1312 bt_graph_add_filter_sink_component_ports_disconnected_listener(
1313 struct bt_graph
*graph
,
1314 bt_graph_filter_sink_component_ports_disconnected_listener_func func
,
1315 bt_graph_listener_removed_func listener_removed
, void *data
,
1316 int *out_listener_id
)
1318 struct bt_graph_listener_ports_disconnected listener
= {
1320 .removed
= listener_removed
,
1323 .func
= (ports_disconnected_func_t
) func
,
1327 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1328 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1329 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1330 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1331 "Graph currently executing a \"listener removed\" listener: "
1333 g_array_append_val(graph
->listeners
.filter_sink_ports_disconnected
,
1335 listener_id
= graph
->listeners
.filter_sink_ports_disconnected
->len
- 1;
1336 BT_LIB_LOGV("Added \"filter to sink component ports disconnected\" listener to graph: "
1337 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1341 *out_listener_id
= listener_id
;
1344 return BT_GRAPH_STATUS_OK
;
1348 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
1352 struct bt_component
*comp
;
1356 BT_LIB_LOGV("Notifying graph listeners that a port was added: "
1357 "%![graph-]+g, %![port-]+p", graph
, port
);
1358 comp
= bt_port_borrow_component_inline(port
);
1361 switch (comp
->class->type
) {
1362 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1364 switch (port
->type
) {
1365 case BT_PORT_TYPE_OUTPUT
:
1366 listeners
= graph
->listeners
.source_output_port_added
;
1374 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1376 switch (port
->type
) {
1377 case BT_PORT_TYPE_INPUT
:
1378 listeners
= graph
->listeners
.filter_input_port_added
;
1380 case BT_PORT_TYPE_OUTPUT
:
1381 listeners
= graph
->listeners
.filter_output_port_added
;
1389 case BT_COMPONENT_CLASS_TYPE_SINK
:
1391 switch (port
->type
) {
1392 case BT_PORT_TYPE_INPUT
:
1393 listeners
= graph
->listeners
.sink_input_port_added
;
1405 for (i
= 0; i
< listeners
->len
; i
++) {
1406 struct bt_graph_listener_port_added
*listener
=
1407 &g_array_index(listeners
,
1408 struct bt_graph_listener_port_added
, i
);
1410 BT_ASSERT(listener
->func
);
1411 listener
->func(comp
, port
, listener
->base
.data
);
1416 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
1417 struct bt_component
*comp
, struct bt_port
*port
)
1424 BT_LIB_LOGV("Notifying graph listeners that a port was removed: "
1425 "%![graph-]+g, %![comp-]+c, %![port-]+p", graph
, comp
, port
);
1427 switch (comp
->class->type
) {
1428 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1430 switch (port
->type
) {
1431 case BT_PORT_TYPE_OUTPUT
:
1432 listeners
= graph
->listeners
.source_output_port_removed
;
1440 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1442 switch (port
->type
) {
1443 case BT_PORT_TYPE_INPUT
:
1444 listeners
= graph
->listeners
.filter_input_port_removed
;
1446 case BT_PORT_TYPE_OUTPUT
:
1447 listeners
= graph
->listeners
.filter_output_port_removed
;
1455 case BT_COMPONENT_CLASS_TYPE_SINK
:
1457 switch (port
->type
) {
1458 case BT_PORT_TYPE_INPUT
:
1459 listeners
= graph
->listeners
.sink_input_port_removed
;
1471 for (i
= 0; i
< listeners
->len
; i
++) {
1472 struct bt_graph_listener_port_removed
*listener
=
1473 &g_array_index(listeners
,
1474 struct bt_graph_listener_port_removed
, i
);
1476 BT_ASSERT(listener
->func
);
1477 listener
->func(comp
, port
, listener
->base
.data
);
1482 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
1483 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
1487 struct bt_component
*upstream_comp
;
1488 struct bt_component
*downstream_comp
;
1491 BT_ASSERT(upstream_port
);
1492 BT_ASSERT(downstream_port
);
1493 BT_LIB_LOGV("Notifying graph listeners that ports were connected: "
1494 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1495 graph
, upstream_port
, downstream_port
);
1496 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
1497 BT_ASSERT(upstream_comp
);
1498 downstream_comp
= bt_port_borrow_component_inline(downstream_port
);
1499 BT_ASSERT(downstream_comp
);
1501 switch (upstream_comp
->class->type
) {
1502 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1504 switch (downstream_comp
->class->type
) {
1505 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1507 graph
->listeners
.source_filter_ports_connected
;
1509 case BT_COMPONENT_CLASS_TYPE_SINK
:
1511 graph
->listeners
.source_sink_ports_connected
;
1519 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1521 switch (downstream_comp
->class->type
) {
1522 case BT_COMPONENT_CLASS_TYPE_SINK
:
1524 graph
->listeners
.filter_sink_ports_connected
;
1536 for (i
= 0; i
< listeners
->len
; i
++) {
1537 struct bt_graph_listener_ports_connected
*listener
=
1538 &g_array_index(listeners
,
1539 struct bt_graph_listener_ports_connected
, i
);
1541 BT_ASSERT(listener
->func
);
1542 listener
->func(upstream_comp
, downstream_comp
,
1543 upstream_port
, downstream_port
, listener
->base
.data
);
1548 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
1549 struct bt_component
*upstream_comp
,
1550 struct bt_component
*downstream_comp
,
1551 struct bt_port
*upstream_port
,
1552 struct bt_port
*downstream_port
)
1558 BT_ASSERT(upstream_comp
);
1559 BT_ASSERT(downstream_comp
);
1560 BT_ASSERT(upstream_port
);
1561 BT_ASSERT(downstream_port
);
1562 BT_LIB_LOGV("Notifying graph listeners that ports were disconnected: "
1563 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p, "
1564 "%![up-comp-]+c, %![down-comp-]+c",
1565 graph
, upstream_port
, downstream_port
, upstream_comp
,
1568 switch (upstream_comp
->class->type
) {
1569 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1571 switch (downstream_comp
->class->type
) {
1572 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1574 graph
->listeners
.source_filter_ports_disconnected
;
1576 case BT_COMPONENT_CLASS_TYPE_SINK
:
1578 graph
->listeners
.source_sink_ports_disconnected
;
1586 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1588 switch (downstream_comp
->class->type
) {
1589 case BT_COMPONENT_CLASS_TYPE_SINK
:
1591 graph
->listeners
.filter_sink_ports_disconnected
;
1603 for (i
= 0; i
< listeners
->len
; i
++) {
1604 struct bt_graph_listener_ports_disconnected
*listener
=
1605 &g_array_index(listeners
,
1606 struct bt_graph_listener_ports_disconnected
, i
);
1608 BT_ASSERT(listener
->func
);
1609 listener
->func(upstream_comp
, downstream_comp
,
1610 upstream_port
, downstream_port
, listener
->base
.data
);
1614 enum bt_graph_status
bt_graph_cancel(
1615 struct bt_graph
*graph
)
1618 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1619 graph
->canceled
= true;
1620 BT_LIB_LOGV("Canceled graph: %!+i", graph
);
1621 return BT_GRAPH_STATUS_OK
;
1624 bt_bool
bt_graph_is_canceled(const struct bt_graph
*graph
)
1626 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1627 return graph
->canceled
? BT_TRUE
: BT_FALSE
;
1631 void bt_graph_remove_connection(struct bt_graph
*graph
,
1632 struct bt_connection
*connection
)
1635 BT_ASSERT(connection
);
1636 BT_LIB_LOGV("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1638 g_ptr_array_remove(graph
->connections
, connection
);
1643 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
1645 bool exists
= false;
1648 for (i
= 0; i
< graph
->components
->len
; i
++) {
1649 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
1651 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1652 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1653 "%![other-comp-]+c, name=\"%s\"",
1665 enum bt_graph_status
add_component_with_init_method_data(
1666 struct bt_graph
*graph
,
1667 struct bt_component_class
*comp_cls
,
1668 comp_init_method_t init_method
,
1669 const char *name
, const struct bt_value
*params
,
1670 void *init_method_data
, struct bt_component
**user_component
)
1672 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
1673 enum bt_self_component_status comp_status
;
1674 struct bt_component
*component
= NULL
;
1676 bool init_can_consume
;
1677 struct bt_value
*new_params
= NULL
;
1679 BT_ASSERT(comp_cls
);
1680 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1681 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1682 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
1683 BT_ASSERT_PRE(!component_name_exists(graph
, name
),
1684 "Duplicate component name: %!+g, name=\"%s\"", graph
, name
);
1685 BT_ASSERT_PRE(!params
|| bt_value_is_map(params
),
1686 "Parameter value is not a map value: %!+v", params
);
1687 init_can_consume
= graph
->can_consume
;
1688 bt_graph_set_can_consume(graph
, false);
1689 BT_LIB_LOGD("Adding component to graph: "
1690 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1691 "init-method-data-addr=%p",
1692 graph
, comp_cls
, name
, params
, init_method_data
);
1695 new_params
= bt_value_map_create();
1697 BT_LOGE_STR("Cannot create map value object.");
1698 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1702 params
= new_params
;
1705 ret
= bt_component_create(comp_cls
, name
, &component
);
1707 BT_LOGE("Cannot create empty component object: ret=%d",
1709 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1714 * The user's initialization method needs to see that this
1715 * component is part of the graph. If the user method fails, we
1716 * immediately remove the component from the graph's components.
1718 g_ptr_array_add(graph
->components
, component
);
1719 bt_component_set_graph(component
, graph
);
1722 BT_LOGD_STR("Calling user's initialization method.");
1723 comp_status
= init_method(component
, params
, init_method_data
);
1724 BT_LOGD("User method returned: status=%s",
1725 bt_self_component_status_string(comp_status
));
1726 if (comp_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
1727 BT_LOGW_STR("Initialization method failed.");
1728 graph_status
= (int) comp_status
;
1729 bt_component_set_graph(component
, NULL
);
1730 g_ptr_array_remove_fast(graph
->components
, component
);
1736 * Mark the component as initialized so that its finalization
1737 * method is called when it is destroyed.
1739 component
->initialized
= true;
1742 * If it's a sink component, it needs to be part of the graph's
1743 * sink queue to be consumed by bt_graph_consume().
1745 if (bt_component_is_sink(component
)) {
1746 graph
->has_sink
= true;
1747 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1751 * Freeze the component class now that it's instantiated at
1754 BT_LOGD_STR("Freezing component class.");
1755 bt_component_class_freeze(comp_cls
);
1756 BT_LIB_LOGD("Added component to graph: "
1757 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1758 "init-method-data-addr=%p, %![comp-]+c",
1759 graph
, comp_cls
, name
, params
, init_method_data
, component
);
1761 if (user_component
) {
1762 /* Move reference to user */
1763 *user_component
= component
;
1768 bt_object_put_ref(component
);
1769 bt_object_put_ref(new_params
);
1770 (void) init_can_consume
;
1771 bt_graph_set_can_consume(graph
, init_can_consume
);
1772 return graph_status
;
1775 enum bt_graph_status
1776 bt_graph_add_source_component_with_init_method_data(
1777 struct bt_graph
*graph
,
1778 const struct bt_component_class_source
*comp_cls
,
1779 const char *name
, const struct bt_value
*params
,
1780 void *init_method_data
,
1781 const struct bt_component_source
**component
)
1783 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1784 return add_component_with_init_method_data(graph
,
1785 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1786 name
, params
, init_method_data
, (void *) component
);
1789 enum bt_graph_status
bt_graph_add_source_component(
1790 struct bt_graph
*graph
,
1791 const struct bt_component_class_source
*comp_cls
,
1792 const char *name
, const struct bt_value
*params
,
1793 const struct bt_component_source
**component
)
1795 return bt_graph_add_source_component_with_init_method_data(
1796 graph
, comp_cls
, name
, params
, NULL
, component
);
1799 enum bt_graph_status
1800 bt_graph_add_filter_component_with_init_method_data(
1801 struct bt_graph
*graph
,
1802 const struct bt_component_class_filter
*comp_cls
,
1803 const char *name
, const struct bt_value
*params
,
1804 void *init_method_data
,
1805 const struct bt_component_filter
**component
)
1807 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1808 return add_component_with_init_method_data(graph
,
1809 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1810 name
, params
, init_method_data
, (void *) component
);
1813 enum bt_graph_status
bt_graph_add_filter_component(
1814 struct bt_graph
*graph
,
1815 const struct bt_component_class_filter
*comp_cls
,
1816 const char *name
, const struct bt_value
*params
,
1817 const struct bt_component_filter
**component
)
1819 return bt_graph_add_filter_component_with_init_method_data(
1820 graph
, comp_cls
, name
, params
, NULL
, component
);
1823 enum bt_graph_status
1824 bt_graph_add_sink_component_with_init_method_data(
1825 struct bt_graph
*graph
,
1826 const struct bt_component_class_sink
*comp_cls
,
1827 const char *name
, const struct bt_value
*params
,
1828 void *init_method_data
,
1829 const struct bt_component_sink
**component
)
1831 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1832 return add_component_with_init_method_data(graph
,
1833 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1834 name
, params
, init_method_data
, (void *) component
);
1837 enum bt_graph_status
bt_graph_add_sink_component(
1838 struct bt_graph
*graph
,
1839 const struct bt_component_class_sink
*comp_cls
,
1840 const char *name
, const struct bt_value
*params
,
1841 const struct bt_component_sink
**component
)
1843 return bt_graph_add_sink_component_with_init_method_data(
1844 graph
, comp_cls
, name
, params
, NULL
, component
);
1848 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1849 struct bt_component
*component
)
1851 bool init_can_consume
;
1857 BT_ASSERT(component
);
1858 BT_ASSERT(component
->base
.ref_count
== 0);
1859 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1861 init_can_consume
= graph
->can_consume
;
1862 count
= bt_component_get_input_port_count(component
);
1864 for (i
= 0; i
< count
; i
++) {
1865 struct bt_port
*port
= (void *)
1866 bt_component_borrow_input_port_by_index(component
, i
);
1870 if (bt_port_is_connected(port
)) {
1871 BT_LIB_LOGW("Cannot remove component from graph: "
1872 "an input port is connected: "
1873 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1874 graph
, component
, port
);
1879 count
= bt_component_get_output_port_count(component
);
1881 for (i
= 0; i
< count
; i
++) {
1882 struct bt_port
*port
= (void *)
1883 bt_component_borrow_output_port_by_index(component
, i
);
1887 if (bt_port_is_connected(port
)) {
1888 BT_LIB_LOGW("Cannot remove component from graph: "
1889 "an output port is connected: "
1890 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1891 graph
, component
, port
);
1896 bt_graph_set_can_consume(graph
, false);
1898 /* Possibly remove from sinks to consume */
1899 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1901 if (graph
->sinks_to_consume
->length
== 0) {
1902 graph
->has_sink
= false;
1906 * This calls bt_object_try_spec_release() on the component, and
1907 * since its reference count is 0, its destructor is called. Its
1908 * destructor calls the user's finalization method (if set).
1910 g_ptr_array_remove(graph
->components
, component
);
1917 (void) init_can_consume
;
1918 bt_graph_set_can_consume(graph
, init_can_consume
);
1923 void bt_graph_add_notification(struct bt_graph
*graph
,
1924 struct bt_notification
*notif
)
1930 * It's okay not to take a reference because, when a
1931 * notification's reference count drops to 0, either:
1933 * * It is recycled back to one of this graph's pool.
1934 * * It is destroyed because it doesn't have any link to any
1935 * graph, which means the original graph is already destroyed.
1937 g_ptr_array_add(graph
->notifications
, notif
);
1940 void bt_graph_get_ref(const struct bt_graph
*graph
)
1942 bt_object_get_ref(graph
);
1945 void bt_graph_put_ref(const struct bt_graph
*graph
)
1947 bt_object_put_ref(graph
);