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/message-internal.h>
39 #include <babeltrace/graph/message-event-internal.h>
40 #include <babeltrace/graph/message-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 (*ports_connected_func_t
)(const void *, const void *, const void *,
53 const void *, void *);
55 typedef enum bt_self_component_status (*comp_init_method_t
)(const void *,
56 const void *, void *);
58 struct bt_graph_listener
{
59 bt_graph_listener_removed_func removed
;
63 struct bt_graph_listener_port_added
{
64 struct bt_graph_listener base
;
65 port_added_func_t func
;
68 struct bt_graph_listener_ports_connected
{
69 struct bt_graph_listener base
;
70 ports_connected_func_t func
;
73 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
75 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
76 if (!(_listeners)) { \
77 BT_LOGE_STR("Failed to allocate one GArray."); \
81 #define CALL_REMOVE_LISTENERS(_type, _listeners) \
85 for (i = 0; i < (_listeners)->len; i++) { \
87 &g_array_index((_listeners), _type, i); \
89 if (listener->base.removed) { \
90 listener->base.removed(listener->base.data); \
96 void destroy_graph(struct bt_object
*obj
)
98 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
101 * The graph's reference count is 0 if we're here. Increment
102 * it to avoid a double-destroy (possibly infinitely recursive)
105 * 1. We put and destroy a connection.
106 * 2. This connection's destructor finalizes its active message
108 * 3. A message iterator's finalization function gets a new
109 * reference on its component (reference count goes from 0 to
111 * 4. Since this component's reference count goes to 1, it takes
112 * a reference on its parent (this graph). This graph's
113 * reference count goes from 0 to 1.
114 * 5. The message iterator's finalization function puts its
115 * component reference (reference count goes from 1 to 0).
116 * 6. Since this component's reference count goes from 1 to 0,
117 * it puts its parent (this graph). This graph's reference
118 * count goes from 1 to 0.
119 * 7. Since this graph's reference count goes from 1 to 0, its
120 * destructor is called (this function).
122 * With the incrementation below, the graph's reference count at
123 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
124 * ensures that this function is not called two times.
126 BT_LIB_LOGD("Destroying graph: %!+g", graph
);
130 * Cancel the graph to disallow some operations, like creating
131 * message iterators and adding ports to components.
133 (void) bt_graph_cancel((void *) graph
);
135 /* Call all remove listeners */
136 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
137 graph
->listeners
.source_output_port_added
);
138 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
139 graph
->listeners
.filter_output_port_added
);
140 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
141 graph
->listeners
.filter_input_port_added
);
142 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
143 graph
->listeners
.sink_input_port_added
);
144 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
145 graph
->listeners
.source_filter_ports_connected
);
146 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
147 graph
->listeners
.filter_filter_ports_connected
);
148 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
149 graph
->listeners
.source_sink_ports_connected
);
150 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
151 graph
->listeners
.filter_sink_ports_connected
);
153 if (graph
->messages
) {
154 g_ptr_array_free(graph
->messages
, TRUE
);
155 graph
->messages
= NULL
;
158 if (graph
->connections
) {
159 BT_LOGD_STR("Destroying connections.");
160 g_ptr_array_free(graph
->connections
, TRUE
);
161 graph
->connections
= NULL
;
164 if (graph
->components
) {
165 BT_LOGD_STR("Destroying components.");
166 g_ptr_array_free(graph
->components
, TRUE
);
167 graph
->components
= NULL
;
170 if (graph
->sinks_to_consume
) {
171 g_queue_free(graph
->sinks_to_consume
);
172 graph
->sinks_to_consume
= NULL
;
175 if (graph
->listeners
.source_output_port_added
) {
176 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
177 graph
->listeners
.source_output_port_added
= NULL
;
180 if (graph
->listeners
.filter_output_port_added
) {
181 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
182 graph
->listeners
.filter_output_port_added
= NULL
;
185 if (graph
->listeners
.filter_input_port_added
) {
186 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
187 graph
->listeners
.filter_input_port_added
= NULL
;
190 if (graph
->listeners
.sink_input_port_added
) {
191 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
192 graph
->listeners
.sink_input_port_added
= NULL
;
195 if (graph
->listeners
.source_filter_ports_connected
) {
196 g_array_free(graph
->listeners
.source_filter_ports_connected
,
198 graph
->listeners
.source_filter_ports_connected
= NULL
;
201 if (graph
->listeners
.filter_filter_ports_connected
) {
202 g_array_free(graph
->listeners
.filter_filter_ports_connected
,
204 graph
->listeners
.filter_filter_ports_connected
= NULL
;
207 if (graph
->listeners
.source_sink_ports_connected
) {
208 g_array_free(graph
->listeners
.source_sink_ports_connected
,
210 graph
->listeners
.source_sink_ports_connected
= NULL
;
213 if (graph
->listeners
.filter_sink_ports_connected
) {
214 g_array_free(graph
->listeners
.filter_sink_ports_connected
,
216 graph
->listeners
.filter_sink_ports_connected
= NULL
;
219 bt_object_pool_finalize(&graph
->event_msg_pool
);
220 bt_object_pool_finalize(&graph
->packet_begin_msg_pool
);
221 bt_object_pool_finalize(&graph
->packet_end_msg_pool
);
226 void destroy_message_event(struct bt_message
*msg
,
227 struct bt_graph
*graph
)
229 bt_message_event_destroy(msg
);
233 void destroy_message_packet_begin(struct bt_message
*msg
,
234 struct bt_graph
*graph
)
236 bt_message_packet_destroy(msg
);
240 void destroy_message_packet_end(struct bt_message
*msg
,
241 struct bt_graph
*graph
)
243 bt_message_packet_destroy(msg
);
247 void notify_message_graph_is_destroyed(struct bt_message
*msg
)
249 bt_message_unlink_graph(msg
);
252 struct bt_graph
*bt_graph_create(void)
254 struct bt_graph
*graph
;
257 BT_LOGD_STR("Creating graph object.");
258 graph
= g_new0(struct bt_graph
, 1);
260 BT_LOGE_STR("Failed to allocate one graph.");
264 bt_object_init_shared(&graph
->base
, destroy_graph
);
265 graph
->connections
= g_ptr_array_new_with_free_func(
266 (GDestroyNotify
) bt_object_try_spec_release
);
267 if (!graph
->connections
) {
268 BT_LOGE_STR("Failed to allocate one GPtrArray.");
271 graph
->components
= g_ptr_array_new_with_free_func(
272 (GDestroyNotify
) bt_object_try_spec_release
);
273 if (!graph
->components
) {
274 BT_LOGE_STR("Failed to allocate one GPtrArray.");
277 graph
->sinks_to_consume
= g_queue_new();
278 if (!graph
->sinks_to_consume
) {
279 BT_LOGE_STR("Failed to allocate one GQueue.");
283 bt_graph_set_can_consume(graph
, true);
284 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
285 graph
->listeners
.source_output_port_added
);
287 if (!graph
->listeners
.source_output_port_added
) {
292 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
293 graph
->listeners
.filter_output_port_added
);
295 if (!graph
->listeners
.filter_output_port_added
) {
300 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
301 graph
->listeners
.filter_input_port_added
);
303 if (!graph
->listeners
.filter_input_port_added
) {
308 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
309 graph
->listeners
.sink_input_port_added
);
311 if (!graph
->listeners
.sink_input_port_added
) {
316 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
317 graph
->listeners
.source_filter_ports_connected
);
319 if (!graph
->listeners
.source_filter_ports_connected
) {
324 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
325 graph
->listeners
.source_sink_ports_connected
);
327 if (!graph
->listeners
.source_sink_ports_connected
) {
332 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
333 graph
->listeners
.filter_filter_ports_connected
);
335 if (!graph
->listeners
.filter_filter_ports_connected
) {
340 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
341 graph
->listeners
.filter_sink_ports_connected
);
343 if (!graph
->listeners
.filter_sink_ports_connected
) {
348 ret
= bt_object_pool_initialize(&graph
->event_msg_pool
,
349 (bt_object_pool_new_object_func
) bt_message_event_new
,
350 (bt_object_pool_destroy_object_func
) destroy_message_event
,
353 BT_LOGE("Failed to initialize event message pool: ret=%d",
358 ret
= bt_object_pool_initialize(&graph
->packet_begin_msg_pool
,
359 (bt_object_pool_new_object_func
) bt_message_packet_beginning_new
,
360 (bt_object_pool_destroy_object_func
) destroy_message_packet_begin
,
363 BT_LOGE("Failed to initialize packet beginning message pool: ret=%d",
368 ret
= bt_object_pool_initialize(&graph
->packet_end_msg_pool
,
369 (bt_object_pool_new_object_func
) bt_message_packet_end_new
,
370 (bt_object_pool_destroy_object_func
) destroy_message_packet_end
,
373 BT_LOGE("Failed to initialize packet end message pool: ret=%d",
378 graph
->messages
= g_ptr_array_new_with_free_func(
379 (GDestroyNotify
) notify_message_graph_is_destroyed
);
380 BT_LIB_LOGD("Created graph object: %!+g", graph
);
383 return (void *) graph
;
386 BT_OBJECT_PUT_REF_AND_RESET(graph
);
390 enum bt_graph_status
bt_graph_connect_ports(
391 struct bt_graph
*graph
,
392 const struct bt_port_output
*upstream_port_out
,
393 const struct bt_port_input
*downstream_port_in
,
394 const struct bt_connection
**user_connection
)
396 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
397 struct bt_connection
*connection
= NULL
;
398 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
399 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
400 struct bt_component
*upstream_component
= NULL
;
401 struct bt_component
*downstream_component
= NULL
;
402 enum bt_self_component_status component_status
;
403 bool init_can_consume
;
405 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
406 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
407 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
408 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
409 BT_ASSERT_PRE(!graph
->is_configured
,
410 "Graph is already configured: %!+g", graph
);
411 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
412 "Upstream port is already connected: %!+p", upstream_port
);
413 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
414 "Downstream port is already connected: %!+p", downstream_port
);
415 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
416 "Upstream port does not belong to a component: %!+p",
418 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
419 "Downstream port does not belong to a component: %!+p",
421 init_can_consume
= graph
->can_consume
;
422 BT_LIB_LOGD("Connecting component ports within graph: "
423 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
424 graph
, upstream_port
, downstream_port
);
425 bt_graph_set_can_consume(graph
, false);
426 upstream_component
= bt_port_borrow_component_inline(
427 (void *) upstream_port
);
428 downstream_component
= bt_port_borrow_component_inline(
429 (void *) downstream_port
);
432 * At this point the ports are not connected yet. Both
433 * components need to accept an eventual connection to their
434 * port by the other port before we continue.
436 BT_LIB_LOGD("Asking upstream component to accept the connection: "
437 "%![comp-]+c", upstream_component
);
438 component_status
= bt_component_accept_port_connection(
439 upstream_component
, (void *) upstream_port
,
440 (void *) downstream_port
);
441 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
442 if (component_status
== BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
443 BT_LOGD_STR("Upstream component refused the connection.");
445 BT_LOGW("Cannot ask upstream component to accept the connection: "
446 "status=%s", bt_self_component_status_string(component_status
));
449 status
= (int) component_status
;
453 BT_LIB_LOGD("Asking downstream component to accept the connection: "
454 "%![comp-]+c", downstream_component
);
455 component_status
= bt_component_accept_port_connection(
456 downstream_component
, (void *) downstream_port
,
457 (void *) upstream_port
);
458 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
459 if (component_status
== BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
460 BT_LOGD_STR("Downstream component refused the connection.");
462 BT_LOGW("Cannot ask downstream component to accept the connection: "
463 "status=%s", bt_self_component_status_string(component_status
));
466 status
= (int) component_status
;
470 BT_LOGD_STR("Creating connection.");
471 connection
= bt_connection_create(graph
, (void *) upstream_port
,
472 (void *) downstream_port
);
474 BT_LOGW("Cannot create connection object.");
475 status
= BT_GRAPH_STATUS_NOMEM
;
479 BT_LIB_LOGD("Connection object created: %!+x", connection
);
482 * Ownership of upstream_component/downstream_component and of
483 * the connection object is transferred to the graph.
485 g_ptr_array_add(graph
->connections
, connection
);
488 * Notify both components that their port is connected.
490 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
491 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
492 component_status
= bt_component_port_connected(upstream_component
,
493 (void *) upstream_port
, (void *) downstream_port
);
494 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
495 BT_LIB_LOGW("Error while notifying upstream component that its port is connected: "
496 "status=%s, %![graph-]+g, %![up-comp-]+c, "
497 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
498 bt_self_component_status_string(component_status
),
499 graph
, upstream_component
, downstream_component
,
500 upstream_port
, downstream_port
);
501 bt_connection_end(connection
, true);
502 status
= (int) component_status
;
506 connection
->notified_upstream_port_connected
= true;
507 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
508 "%![comp-]+c, %![port-]+p", downstream_component
,
510 component_status
= bt_component_port_connected(downstream_component
,
511 (void *) downstream_port
, (void *) upstream_port
);
512 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
513 BT_LIB_LOGW("Error while notifying downstream component that its port is connected: "
514 "status=%s, %![graph-]+g, %![up-comp-]+c, "
515 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
516 bt_self_component_status_string(component_status
),
517 graph
, upstream_component
, downstream_component
,
518 upstream_port
, downstream_port
);
519 bt_connection_end(connection
, true);
520 status
= (int) component_status
;
524 connection
->notified_downstream_port_connected
= true;
527 * Notify the graph's creator that both ports are connected.
529 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
530 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
531 connection
->notified_graph_ports_connected
= true;
532 BT_LIB_LOGD("Connected component ports within graph: "
533 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
534 "%![up-port-]+p, %![down-port-]+p",
535 graph
, upstream_component
, downstream_component
,
536 upstream_port
, downstream_port
);
538 if (user_connection
) {
539 /* Move reference to user */
540 *user_connection
= connection
;
545 bt_object_put_ref(connection
);
546 (void) init_can_consume
;
547 bt_graph_set_can_consume(graph
, init_can_consume
);
552 enum bt_graph_status
consume_graph_sink(struct bt_component_sink
*comp
)
554 enum bt_self_component_status comp_status
;
555 struct bt_component_class_sink
*sink_class
= NULL
;
558 sink_class
= (void *) comp
->parent
.class;
559 BT_ASSERT(sink_class
->methods
.consume
);
560 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
561 comp_status
= sink_class
->methods
.consume((void *) comp
);
562 BT_LOGD("User method returned: status=%s",
563 bt_self_component_status_string(comp_status
));
564 BT_ASSERT_PRE(comp_status
== BT_SELF_COMPONENT_STATUS_OK
||
565 comp_status
== BT_SELF_COMPONENT_STATUS_END
||
566 comp_status
== BT_SELF_COMPONENT_STATUS_AGAIN
||
567 comp_status
== BT_SELF_COMPONENT_STATUS_ERROR
||
568 comp_status
== BT_SELF_COMPONENT_STATUS_NOMEM
,
569 "Invalid component status returned by consuming method: "
570 "status=%s", bt_self_component_status_string(comp_status
));
571 if (comp_status
< 0) {
572 BT_LOGW_STR("Consume method failed.");
576 BT_LIB_LOGV("Consumed from sink: %![comp-]+c, status=%s",
577 comp
, bt_self_component_status_string(comp_status
));
580 return (int) comp_status
;
584 * `node` is removed from the queue of sinks to consume when passed to
585 * this function. This function adds it back to the queue if there's
586 * still something to consume afterwards.
589 enum bt_graph_status
consume_sink_node(struct bt_graph
*graph
, GList
*node
)
591 enum bt_graph_status status
;
592 struct bt_component_sink
*sink
;
595 status
= consume_graph_sink(sink
);
596 if (unlikely(status
!= BT_GRAPH_STATUS_END
)) {
597 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
601 /* End reached, the node is not added back to the queue and free'd. */
602 g_queue_delete_link(graph
->sinks_to_consume
, node
);
604 /* Don't forward an END status if there are sinks left to consume. */
605 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
606 status
= BT_GRAPH_STATUS_OK
;
611 BT_LIB_LOGV("Consumed sink node: %![comp-]+c, status=%s",
612 sink
, bt_graph_status_string(status
));
617 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
618 struct bt_component_sink
*sink
)
620 enum bt_graph_status status
;
624 BT_LIB_LOGV("Making specific sink consume: %![comp-]+c", sink
);
625 BT_ASSERT(bt_component_borrow_graph((void *) sink
) == graph
);
627 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
628 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
629 status
= BT_GRAPH_STATUS_END
;
633 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
635 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
636 status
= BT_GRAPH_STATUS_END
;
640 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
641 BT_ASSERT(sink_node
);
642 status
= consume_sink_node(graph
, sink_node
);
649 enum bt_graph_status
consume_no_check(struct bt_graph
*graph
)
651 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
652 struct bt_component
*sink
;
655 BT_ASSERT_PRE(graph
->has_sink
,
656 "Graph has no sink component: %!+g", graph
);
657 BT_LIB_LOGV("Making next sink consume: %![graph-]+g", graph
);
659 if (unlikely(g_queue_is_empty(graph
->sinks_to_consume
))) {
660 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
661 status
= BT_GRAPH_STATUS_END
;
665 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
666 sink
= current_node
->data
;
667 BT_LIB_LOGV("Chose next sink to consume: %!+c", sink
);
668 status
= consume_sink_node(graph
, current_node
);
674 enum bt_graph_status
bt_graph_consume(struct bt_graph
*graph
)
676 enum bt_graph_status status
;
678 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
679 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
680 BT_ASSERT_PRE(graph
->can_consume
,
681 "Cannot consume graph in its current state: %!+g", graph
);
682 bt_graph_set_can_consume(graph
, false);
683 bt_graph_set_is_configured(graph
, true);
684 status
= consume_no_check(graph
);
685 bt_graph_set_can_consume(graph
, true);
689 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
691 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
693 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
694 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
695 BT_ASSERT_PRE(graph
->can_consume
,
696 "Cannot consume graph in its current state: %!+g", graph
);
697 bt_graph_set_can_consume(graph
, false);
698 bt_graph_set_is_configured(graph
, true);
699 BT_LIB_LOGV("Running graph: %!+g", graph
);
703 * Check if the graph is canceled at each iteration. If
704 * the graph was canceled by another thread or by a
705 * signal handler, this is not a warning nor an error,
706 * it was intentional: log with a DEBUG level only.
708 if (unlikely(graph
->canceled
)) {
709 BT_LIB_LOGD("Stopping the graph: graph is canceled: "
711 status
= BT_GRAPH_STATUS_CANCELED
;
715 status
= consume_no_check(graph
);
716 if (unlikely(status
== BT_GRAPH_STATUS_AGAIN
)) {
718 * If AGAIN is received and there are multiple
719 * sinks, go ahead and consume from the next
722 * However, in the case where a single sink is
723 * left, the caller can decide to busy-wait and
724 * call bt_graph_run() continuously
725 * until the source is ready or it can decide to
726 * sleep for an arbitrary amount of time.
728 if (graph
->sinks_to_consume
->length
> 1) {
729 status
= BT_GRAPH_STATUS_OK
;
731 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
734 } while (status
== BT_GRAPH_STATUS_OK
);
736 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
737 status
= BT_GRAPH_STATUS_END
;
741 BT_LIB_LOGV("Graph ran: %![graph-]+g, status=%s", graph
,
742 bt_graph_status_string(status
));
743 bt_graph_set_can_consume(graph
, true);
748 bt_graph_add_source_component_output_port_added_listener(
749 struct bt_graph
*graph
,
750 bt_graph_source_component_output_port_added_listener_func func
,
751 bt_graph_listener_removed_func listener_removed
, void *data
,
752 int *out_listener_id
)
754 struct bt_graph_listener_port_added listener
= {
756 .removed
= listener_removed
,
759 .func
= (port_added_func_t
) func
,
763 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
764 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
765 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
766 BT_ASSERT_PRE(!graph
->in_remove_listener
,
767 "Graph currently executing a \"listener removed\" listener: "
769 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
770 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
771 BT_LIB_LOGV("Added \"source component output port added\" listener to graph: "
772 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
776 *out_listener_id
= listener_id
;
779 return BT_GRAPH_STATUS_OK
;
783 bt_graph_add_filter_component_output_port_added_listener(
784 struct bt_graph
*graph
,
785 bt_graph_filter_component_output_port_added_listener_func func
,
786 bt_graph_listener_removed_func listener_removed
, void *data
,
787 int *out_listener_id
)
789 struct bt_graph_listener_port_added listener
= {
791 .removed
= listener_removed
,
794 .func
= (port_added_func_t
) func
,
798 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
799 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
800 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
801 BT_ASSERT_PRE(!graph
->in_remove_listener
,
802 "Graph currently executing a \"listener removed\" listener: "
804 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
805 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
806 BT_LIB_LOGV("Added \"filter component output port added\" listener to graph: "
807 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
811 *out_listener_id
= listener_id
;
814 return BT_GRAPH_STATUS_OK
;
818 bt_graph_add_filter_component_input_port_added_listener(
819 struct bt_graph
*graph
,
820 bt_graph_filter_component_input_port_added_listener_func func
,
821 bt_graph_listener_removed_func listener_removed
, void *data
,
822 int *out_listener_id
)
824 struct bt_graph_listener_port_added listener
= {
826 .removed
= listener_removed
,
829 .func
= (port_added_func_t
) func
,
833 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
834 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
835 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
836 BT_ASSERT_PRE(!graph
->in_remove_listener
,
837 "Graph currently executing a \"listener removed\" listener: "
839 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
840 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
841 BT_LIB_LOGV("Added \"filter component input port added\" listener to graph: "
842 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
846 *out_listener_id
= listener_id
;
849 return BT_GRAPH_STATUS_OK
;
853 bt_graph_add_sink_component_input_port_added_listener(
854 struct bt_graph
*graph
,
855 bt_graph_sink_component_input_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
.sink_input_port_added
, listener
);
875 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
876 BT_LIB_LOGV("Added \"sink component input 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_source_filter_component_ports_connected_listener(
889 struct bt_graph
*graph
,
890 bt_graph_source_filter_component_ports_connected_listener_func func
,
891 bt_graph_listener_removed_func listener_removed
, void *data
,
892 int *out_listener_id
)
894 struct bt_graph_listener_ports_connected listener
= {
896 .removed
= listener_removed
,
899 .func
= (ports_connected_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
.source_filter_ports_connected
,
911 listener_id
= graph
->listeners
.source_filter_ports_connected
->len
- 1;
912 BT_LIB_LOGV("Added \"source to filter component ports connected\" listener to graph: "
913 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
917 *out_listener_id
= listener_id
;
920 return BT_GRAPH_STATUS_OK
;
924 bt_graph_add_source_sink_component_ports_connected_listener(
925 struct bt_graph
*graph
,
926 bt_graph_source_sink_component_ports_connected_listener_func func
,
927 bt_graph_listener_removed_func listener_removed
, void *data
,
928 int *out_listener_id
)
930 struct bt_graph_listener_ports_connected listener
= {
932 .removed
= listener_removed
,
935 .func
= (ports_connected_func_t
) func
,
939 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
940 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
941 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
942 BT_ASSERT_PRE(!graph
->in_remove_listener
,
943 "Graph currently executing a \"listener removed\" listener: "
945 g_array_append_val(graph
->listeners
.source_sink_ports_connected
,
947 listener_id
= graph
->listeners
.source_sink_ports_connected
->len
- 1;
948 BT_LIB_LOGV("Added \"source to sink component ports connected\" listener to graph: "
949 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
953 *out_listener_id
= listener_id
;
956 return BT_GRAPH_STATUS_OK
;
960 bt_graph_add_filter_filter_component_ports_connected_listener(
961 struct bt_graph
*graph
,
962 bt_graph_filter_filter_component_ports_connected_listener_func func
,
963 bt_graph_listener_removed_func listener_removed
, void *data
,
964 int *out_listener_id
)
966 struct bt_graph_listener_ports_connected listener
= {
968 .removed
= listener_removed
,
971 .func
= (ports_connected_func_t
) func
,
975 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
976 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
977 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
978 BT_ASSERT_PRE(!graph
->in_remove_listener
,
979 "Graph currently executing a \"listener removed\" listener: "
981 g_array_append_val(graph
->listeners
.filter_filter_ports_connected
,
983 listener_id
= graph
->listeners
.filter_filter_ports_connected
->len
- 1;
984 BT_LIB_LOGV("Added \"filter to filter component ports connected\" listener to graph: "
985 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
989 *out_listener_id
= listener_id
;
992 return BT_GRAPH_STATUS_OK
;
996 bt_graph_add_filter_sink_component_ports_connected_listener(
997 struct bt_graph
*graph
,
998 bt_graph_filter_sink_component_ports_connected_listener_func func
,
999 bt_graph_listener_removed_func listener_removed
, void *data
,
1000 int *out_listener_id
)
1002 struct bt_graph_listener_ports_connected listener
= {
1004 .removed
= listener_removed
,
1007 .func
= (ports_connected_func_t
) func
,
1011 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1012 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1013 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1014 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1015 "Graph currently executing a \"listener removed\" listener: "
1017 g_array_append_val(graph
->listeners
.filter_sink_ports_connected
,
1019 listener_id
= graph
->listeners
.filter_sink_ports_connected
->len
- 1;
1020 BT_LIB_LOGV("Added \"filter to sink component ports connected\" listener to graph: "
1021 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1025 *out_listener_id
= listener_id
;
1028 return BT_GRAPH_STATUS_OK
;
1032 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
1036 struct bt_component
*comp
;
1040 BT_LIB_LOGV("Notifying graph listeners that a port was added: "
1041 "%![graph-]+g, %![port-]+p", graph
, port
);
1042 comp
= bt_port_borrow_component_inline(port
);
1045 switch (comp
->class->type
) {
1046 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1048 switch (port
->type
) {
1049 case BT_PORT_TYPE_OUTPUT
:
1050 listeners
= graph
->listeners
.source_output_port_added
;
1058 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1060 switch (port
->type
) {
1061 case BT_PORT_TYPE_INPUT
:
1062 listeners
= graph
->listeners
.filter_input_port_added
;
1064 case BT_PORT_TYPE_OUTPUT
:
1065 listeners
= graph
->listeners
.filter_output_port_added
;
1073 case BT_COMPONENT_CLASS_TYPE_SINK
:
1075 switch (port
->type
) {
1076 case BT_PORT_TYPE_INPUT
:
1077 listeners
= graph
->listeners
.sink_input_port_added
;
1089 for (i
= 0; i
< listeners
->len
; i
++) {
1090 struct bt_graph_listener_port_added
*listener
=
1091 &g_array_index(listeners
,
1092 struct bt_graph_listener_port_added
, i
);
1094 BT_ASSERT(listener
->func
);
1095 listener
->func(comp
, port
, listener
->base
.data
);
1100 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
1101 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
1105 struct bt_component
*upstream_comp
;
1106 struct bt_component
*downstream_comp
;
1109 BT_ASSERT(upstream_port
);
1110 BT_ASSERT(downstream_port
);
1111 BT_LIB_LOGV("Notifying graph listeners that ports were connected: "
1112 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1113 graph
, upstream_port
, downstream_port
);
1114 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
1115 BT_ASSERT(upstream_comp
);
1116 downstream_comp
= bt_port_borrow_component_inline(downstream_port
);
1117 BT_ASSERT(downstream_comp
);
1119 switch (upstream_comp
->class->type
) {
1120 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1122 switch (downstream_comp
->class->type
) {
1123 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1125 graph
->listeners
.source_filter_ports_connected
;
1127 case BT_COMPONENT_CLASS_TYPE_SINK
:
1129 graph
->listeners
.source_sink_ports_connected
;
1137 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1139 switch (downstream_comp
->class->type
) {
1140 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1142 graph
->listeners
.filter_filter_ports_connected
;
1144 case BT_COMPONENT_CLASS_TYPE_SINK
:
1146 graph
->listeners
.filter_sink_ports_connected
;
1158 for (i
= 0; i
< listeners
->len
; i
++) {
1159 struct bt_graph_listener_ports_connected
*listener
=
1160 &g_array_index(listeners
,
1161 struct bt_graph_listener_ports_connected
, i
);
1163 BT_ASSERT(listener
->func
);
1164 listener
->func(upstream_comp
, downstream_comp
,
1165 upstream_port
, downstream_port
, listener
->base
.data
);
1169 enum bt_graph_status
bt_graph_cancel(struct bt_graph
*graph
)
1172 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1173 graph
->canceled
= true;
1174 BT_LIB_LOGV("Canceled graph: %!+i", graph
);
1175 return BT_GRAPH_STATUS_OK
;
1178 bt_bool
bt_graph_is_canceled(const struct bt_graph
*graph
)
1180 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1181 return graph
->canceled
? BT_TRUE
: BT_FALSE
;
1185 void bt_graph_remove_connection(struct bt_graph
*graph
,
1186 struct bt_connection
*connection
)
1189 BT_ASSERT(connection
);
1190 BT_LIB_LOGV("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1192 g_ptr_array_remove(graph
->connections
, connection
);
1197 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
1199 bool exists
= false;
1202 for (i
= 0; i
< graph
->components
->len
; i
++) {
1203 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
1205 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1206 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1207 "%![other-comp-]+c, name=\"%s\"",
1219 enum bt_graph_status
add_component_with_init_method_data(
1220 struct bt_graph
*graph
,
1221 struct bt_component_class
*comp_cls
,
1222 comp_init_method_t init_method
,
1223 const char *name
, const struct bt_value
*params
,
1224 void *init_method_data
, struct bt_component
**user_component
)
1226 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
1227 enum bt_self_component_status comp_status
;
1228 struct bt_component
*component
= NULL
;
1230 bool init_can_consume
;
1231 struct bt_value
*new_params
= NULL
;
1233 BT_ASSERT(comp_cls
);
1234 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1235 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1236 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
1237 BT_ASSERT_PRE(!graph
->is_configured
,
1238 "Graph is already configured: %!+g", graph
);
1239 BT_ASSERT_PRE(!component_name_exists(graph
, name
),
1240 "Duplicate component name: %!+g, name=\"%s\"", graph
, name
);
1241 BT_ASSERT_PRE(!params
|| bt_value_is_map(params
),
1242 "Parameter value is not a map value: %!+v", params
);
1243 init_can_consume
= graph
->can_consume
;
1244 bt_graph_set_can_consume(graph
, false);
1245 BT_LIB_LOGD("Adding component to graph: "
1246 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1247 "init-method-data-addr=%p",
1248 graph
, comp_cls
, name
, params
, init_method_data
);
1251 new_params
= bt_value_map_create();
1253 BT_LOGE_STR("Cannot create map value object.");
1254 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1258 params
= new_params
;
1261 ret
= bt_component_create(comp_cls
, name
, &component
);
1263 BT_LOGE("Cannot create empty component object: ret=%d",
1265 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1270 * The user's initialization method needs to see that this
1271 * component is part of the graph. If the user method fails, we
1272 * immediately remove the component from the graph's components.
1274 g_ptr_array_add(graph
->components
, component
);
1275 bt_component_set_graph(component
, graph
);
1278 BT_LOGD_STR("Calling user's initialization method.");
1279 comp_status
= init_method(component
, params
, init_method_data
);
1280 BT_LOGD("User method returned: status=%s",
1281 bt_self_component_status_string(comp_status
));
1282 if (comp_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
1283 BT_LOGW_STR("Initialization method failed.");
1284 graph_status
= (int) comp_status
;
1285 bt_component_set_graph(component
, NULL
);
1286 g_ptr_array_remove_fast(graph
->components
, component
);
1292 * Mark the component as initialized so that its finalization
1293 * method is called when it is destroyed.
1295 component
->initialized
= true;
1298 * If it's a sink component, it needs to be part of the graph's
1299 * sink queue to be consumed by bt_graph_consume().
1301 if (bt_component_is_sink(component
)) {
1302 graph
->has_sink
= true;
1303 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1307 * Freeze the component class now that it's instantiated at
1310 BT_LOGD_STR("Freezing component class.");
1311 bt_component_class_freeze(comp_cls
);
1312 BT_LIB_LOGD("Added component to graph: "
1313 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1314 "init-method-data-addr=%p, %![comp-]+c",
1315 graph
, comp_cls
, name
, params
, init_method_data
, component
);
1317 if (user_component
) {
1318 /* Move reference to user */
1319 *user_component
= component
;
1324 bt_object_put_ref(component
);
1325 bt_object_put_ref(new_params
);
1326 (void) init_can_consume
;
1327 bt_graph_set_can_consume(graph
, init_can_consume
);
1328 return graph_status
;
1331 enum bt_graph_status
1332 bt_graph_add_source_component_with_init_method_data(
1333 struct bt_graph
*graph
,
1334 const struct bt_component_class_source
*comp_cls
,
1335 const char *name
, const struct bt_value
*params
,
1336 void *init_method_data
,
1337 const struct bt_component_source
**component
)
1339 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1340 return add_component_with_init_method_data(graph
,
1341 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1342 name
, params
, init_method_data
, (void *) component
);
1345 enum bt_graph_status
bt_graph_add_source_component(
1346 struct bt_graph
*graph
,
1347 const struct bt_component_class_source
*comp_cls
,
1348 const char *name
, const struct bt_value
*params
,
1349 const struct bt_component_source
**component
)
1351 return bt_graph_add_source_component_with_init_method_data(
1352 graph
, comp_cls
, name
, params
, NULL
, component
);
1355 enum bt_graph_status
1356 bt_graph_add_filter_component_with_init_method_data(
1357 struct bt_graph
*graph
,
1358 const struct bt_component_class_filter
*comp_cls
,
1359 const char *name
, const struct bt_value
*params
,
1360 void *init_method_data
,
1361 const struct bt_component_filter
**component
)
1363 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1364 return add_component_with_init_method_data(graph
,
1365 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1366 name
, params
, init_method_data
, (void *) component
);
1369 enum bt_graph_status
bt_graph_add_filter_component(
1370 struct bt_graph
*graph
,
1371 const struct bt_component_class_filter
*comp_cls
,
1372 const char *name
, const struct bt_value
*params
,
1373 const struct bt_component_filter
**component
)
1375 return bt_graph_add_filter_component_with_init_method_data(
1376 graph
, comp_cls
, name
, params
, NULL
, component
);
1379 enum bt_graph_status
1380 bt_graph_add_sink_component_with_init_method_data(
1381 struct bt_graph
*graph
,
1382 const struct bt_component_class_sink
*comp_cls
,
1383 const char *name
, const struct bt_value
*params
,
1384 void *init_method_data
,
1385 const struct bt_component_sink
**component
)
1387 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1388 return add_component_with_init_method_data(graph
,
1389 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1390 name
, params
, init_method_data
, (void *) component
);
1393 enum bt_graph_status
bt_graph_add_sink_component(
1394 struct bt_graph
*graph
,
1395 const struct bt_component_class_sink
*comp_cls
,
1396 const char *name
, const struct bt_value
*params
,
1397 const struct bt_component_sink
**component
)
1399 return bt_graph_add_sink_component_with_init_method_data(
1400 graph
, comp_cls
, name
, params
, NULL
, component
);
1404 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1405 struct bt_component
*component
)
1407 bool init_can_consume
;
1413 BT_ASSERT(component
);
1414 BT_ASSERT(component
->base
.ref_count
== 0);
1415 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1417 init_can_consume
= graph
->can_consume
;
1418 count
= bt_component_get_input_port_count(component
);
1420 for (i
= 0; i
< count
; i
++) {
1421 struct bt_port
*port
= (void *)
1422 bt_component_borrow_input_port_by_index(component
, i
);
1426 if (bt_port_is_connected(port
)) {
1427 BT_LIB_LOGW("Cannot remove component from graph: "
1428 "an input port is connected: "
1429 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1430 graph
, component
, port
);
1435 count
= bt_component_get_output_port_count(component
);
1437 for (i
= 0; i
< count
; i
++) {
1438 struct bt_port
*port
= (void *)
1439 bt_component_borrow_output_port_by_index(component
, i
);
1443 if (bt_port_is_connected(port
)) {
1444 BT_LIB_LOGW("Cannot remove component from graph: "
1445 "an output port is connected: "
1446 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1447 graph
, component
, port
);
1452 bt_graph_set_can_consume(graph
, false);
1454 /* Possibly remove from sinks to consume */
1455 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1457 if (graph
->sinks_to_consume
->length
== 0) {
1458 graph
->has_sink
= false;
1462 * This calls bt_object_try_spec_release() on the component, and
1463 * since its reference count is 0, its destructor is called. Its
1464 * destructor calls the user's finalization method (if set).
1466 g_ptr_array_remove(graph
->components
, component
);
1473 (void) init_can_consume
;
1474 bt_graph_set_can_consume(graph
, init_can_consume
);
1479 void bt_graph_add_message(struct bt_graph
*graph
,
1480 struct bt_message
*msg
)
1486 * It's okay not to take a reference because, when a
1487 * message's reference count drops to 0, either:
1489 * * It is recycled back to one of this graph's pool.
1490 * * It is destroyed because it doesn't have any link to any
1491 * graph, which means the original graph is already destroyed.
1493 g_ptr_array_add(graph
->messages
, msg
);
1496 void bt_graph_get_ref(const struct bt_graph
*graph
)
1498 bt_object_get_ref(graph
);
1501 void bt_graph_put_ref(const struct bt_graph
*graph
)
1503 bt_object_put_ref(graph
);