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) \
88 for (i = 0; i < (_listeners)->len; i++) { \
90 &g_array_index((_listeners), _type, i); \
92 if (listener->base.removed) { \
93 listener->base.removed(listener->base.data); \
99 void destroy_graph(struct bt_object
*obj
)
101 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
104 * The graph's reference count is 0 if we're here. Increment
105 * it to avoid a double-destroy (possibly infinitely recursive)
108 * 1. We put and destroy a connection.
109 * 2. This connection's destructor finalizes its active message
111 * 3. A message iterator's finalization function gets a new
112 * reference on its component (reference count goes from 0 to
114 * 4. Since this component's reference count goes to 1, it takes
115 * a reference on its parent (this graph). This graph's
116 * reference count goes from 0 to 1.
117 * 5. The message iterator's finalization function puts its
118 * component reference (reference count goes from 1 to 0).
119 * 6. Since this component's reference count goes from 1 to 0,
120 * it puts its parent (this graph). This graph's reference
121 * count goes from 1 to 0.
122 * 7. Since this graph's reference count goes from 1 to 0, its
123 * destructor is called (this function).
125 * With the incrementation below, the graph's reference count at
126 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
127 * ensures that this function is not called two times.
129 BT_LIB_LOGD("Destroying graph: %!+g", graph
);
133 * Cancel the graph to disallow some operations, like creating
134 * message iterators and adding ports to components.
136 (void) bt_graph_cancel((void *) graph
);
138 /* Call all remove listeners */
139 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
140 graph
->listeners
.source_output_port_added
);
141 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
142 graph
->listeners
.filter_output_port_added
);
143 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
144 graph
->listeners
.filter_input_port_added
);
145 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
146 graph
->listeners
.sink_input_port_added
);
147 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
148 graph
->listeners
.source_filter_ports_connected
);
149 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
150 graph
->listeners
.filter_filter_ports_connected
);
151 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
152 graph
->listeners
.source_sink_ports_connected
);
153 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
154 graph
->listeners
.filter_sink_ports_connected
);
156 if (graph
->messages
) {
157 g_ptr_array_free(graph
->messages
, TRUE
);
158 graph
->messages
= NULL
;
161 if (graph
->connections
) {
162 BT_LOGD_STR("Destroying connections.");
163 g_ptr_array_free(graph
->connections
, TRUE
);
164 graph
->connections
= NULL
;
167 if (graph
->components
) {
168 BT_LOGD_STR("Destroying components.");
169 g_ptr_array_free(graph
->components
, TRUE
);
170 graph
->components
= NULL
;
173 if (graph
->sinks_to_consume
) {
174 g_queue_free(graph
->sinks_to_consume
);
175 graph
->sinks_to_consume
= NULL
;
178 if (graph
->listeners
.source_output_port_added
) {
179 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
180 graph
->listeners
.source_output_port_added
= NULL
;
183 if (graph
->listeners
.filter_output_port_added
) {
184 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
185 graph
->listeners
.filter_output_port_added
= NULL
;
188 if (graph
->listeners
.filter_input_port_added
) {
189 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
190 graph
->listeners
.filter_input_port_added
= NULL
;
193 if (graph
->listeners
.sink_input_port_added
) {
194 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
195 graph
->listeners
.sink_input_port_added
= NULL
;
198 if (graph
->listeners
.source_filter_ports_connected
) {
199 g_array_free(graph
->listeners
.source_filter_ports_connected
,
201 graph
->listeners
.source_filter_ports_connected
= NULL
;
204 if (graph
->listeners
.filter_filter_ports_connected
) {
205 g_array_free(graph
->listeners
.filter_filter_ports_connected
,
207 graph
->listeners
.filter_filter_ports_connected
= NULL
;
210 if (graph
->listeners
.source_sink_ports_connected
) {
211 g_array_free(graph
->listeners
.source_sink_ports_connected
,
213 graph
->listeners
.source_sink_ports_connected
= NULL
;
216 if (graph
->listeners
.filter_sink_ports_connected
) {
217 g_array_free(graph
->listeners
.filter_sink_ports_connected
,
219 graph
->listeners
.filter_sink_ports_connected
= NULL
;
222 bt_object_pool_finalize(&graph
->event_msg_pool
);
223 bt_object_pool_finalize(&graph
->packet_begin_msg_pool
);
224 bt_object_pool_finalize(&graph
->packet_end_msg_pool
);
229 void destroy_message_event(struct bt_message
*msg
,
230 struct bt_graph
*graph
)
232 bt_message_event_destroy(msg
);
236 void destroy_message_packet_begin(struct bt_message
*msg
,
237 struct bt_graph
*graph
)
239 bt_message_packet_destroy(msg
);
243 void destroy_message_packet_end(struct bt_message
*msg
,
244 struct bt_graph
*graph
)
246 bt_message_packet_destroy(msg
);
250 void notify_message_graph_is_destroyed(struct bt_message
*msg
)
252 bt_message_unlink_graph(msg
);
255 struct bt_graph
*bt_graph_create(void)
257 struct bt_graph
*graph
;
260 BT_LOGD_STR("Creating graph object.");
261 graph
= g_new0(struct bt_graph
, 1);
263 BT_LOGE_STR("Failed to allocate one graph.");
267 bt_object_init_shared(&graph
->base
, destroy_graph
);
268 graph
->connections
= g_ptr_array_new_with_free_func(
269 (GDestroyNotify
) bt_object_try_spec_release
);
270 if (!graph
->connections
) {
271 BT_LOGE_STR("Failed to allocate one GPtrArray.");
274 graph
->components
= g_ptr_array_new_with_free_func(
275 (GDestroyNotify
) bt_object_try_spec_release
);
276 if (!graph
->components
) {
277 BT_LOGE_STR("Failed to allocate one GPtrArray.");
280 graph
->sinks_to_consume
= g_queue_new();
281 if (!graph
->sinks_to_consume
) {
282 BT_LOGE_STR("Failed to allocate one GQueue.");
286 bt_graph_set_can_consume(graph
, true);
287 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
288 graph
->listeners
.source_output_port_added
);
290 if (!graph
->listeners
.source_output_port_added
) {
295 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
296 graph
->listeners
.filter_output_port_added
);
298 if (!graph
->listeners
.filter_output_port_added
) {
303 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
304 graph
->listeners
.filter_input_port_added
);
306 if (!graph
->listeners
.filter_input_port_added
) {
311 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
312 graph
->listeners
.sink_input_port_added
);
314 if (!graph
->listeners
.sink_input_port_added
) {
319 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
320 graph
->listeners
.source_filter_ports_connected
);
322 if (!graph
->listeners
.source_filter_ports_connected
) {
327 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
328 graph
->listeners
.source_sink_ports_connected
);
330 if (!graph
->listeners
.source_sink_ports_connected
) {
335 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
336 graph
->listeners
.filter_filter_ports_connected
);
338 if (!graph
->listeners
.filter_filter_ports_connected
) {
343 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
344 graph
->listeners
.filter_sink_ports_connected
);
346 if (!graph
->listeners
.filter_sink_ports_connected
) {
351 ret
= bt_object_pool_initialize(&graph
->event_msg_pool
,
352 (bt_object_pool_new_object_func
) bt_message_event_new
,
353 (bt_object_pool_destroy_object_func
) destroy_message_event
,
356 BT_LOGE("Failed to initialize event message pool: ret=%d",
361 ret
= bt_object_pool_initialize(&graph
->packet_begin_msg_pool
,
362 (bt_object_pool_new_object_func
) bt_message_packet_beginning_new
,
363 (bt_object_pool_destroy_object_func
) destroy_message_packet_begin
,
366 BT_LOGE("Failed to initialize packet beginning message pool: ret=%d",
371 ret
= bt_object_pool_initialize(&graph
->packet_end_msg_pool
,
372 (bt_object_pool_new_object_func
) bt_message_packet_end_new
,
373 (bt_object_pool_destroy_object_func
) destroy_message_packet_end
,
376 BT_LOGE("Failed to initialize packet end message pool: ret=%d",
381 graph
->messages
= g_ptr_array_new_with_free_func(
382 (GDestroyNotify
) notify_message_graph_is_destroyed
);
383 BT_LIB_LOGD("Created graph object: %!+g", graph
);
386 return (void *) graph
;
389 BT_OBJECT_PUT_REF_AND_RESET(graph
);
393 enum bt_graph_status
bt_graph_connect_ports(
394 struct bt_graph
*graph
,
395 const struct bt_port_output
*upstream_port_out
,
396 const struct bt_port_input
*downstream_port_in
,
397 const struct bt_connection
**user_connection
)
399 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
400 struct bt_connection
*connection
= NULL
;
401 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
402 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
403 struct bt_component
*upstream_component
= NULL
;
404 struct bt_component
*downstream_component
= NULL
;
405 enum bt_self_component_status component_status
;
406 bool init_can_consume
;
408 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
409 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
410 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
411 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
413 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
414 "Graph is not in the \"configuring\" state: %!+g", graph
);
415 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
416 "Upstream port is already connected: %!+p", upstream_port
);
417 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
418 "Downstream port is already connected: %!+p", downstream_port
);
419 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
420 "Upstream port does not belong to a component: %!+p",
422 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
423 "Downstream port does not belong to a component: %!+p",
425 init_can_consume
= graph
->can_consume
;
426 BT_LIB_LOGD("Connecting component ports within graph: "
427 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
428 graph
, upstream_port
, downstream_port
);
429 bt_graph_set_can_consume(graph
, false);
430 upstream_component
= bt_port_borrow_component_inline(
431 (void *) upstream_port
);
432 downstream_component
= bt_port_borrow_component_inline(
433 (void *) downstream_port
);
436 * At this point the ports are not connected yet. Both
437 * components need to accept an eventual connection to their
438 * port by the other port before we continue.
440 BT_LIB_LOGD("Asking upstream component to accept the connection: "
441 "%![comp-]+c", upstream_component
);
442 component_status
= bt_component_accept_port_connection(
443 upstream_component
, (void *) upstream_port
,
444 (void *) downstream_port
);
445 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
446 if (component_status
== BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
447 BT_LOGD_STR("Upstream component refused the connection.");
449 BT_LOGW("Cannot ask upstream component to accept the connection: "
450 "status=%s", bt_self_component_status_string(component_status
));
453 status
= (int) component_status
;
457 BT_LIB_LOGD("Asking downstream component to accept the connection: "
458 "%![comp-]+c", downstream_component
);
459 component_status
= bt_component_accept_port_connection(
460 downstream_component
, (void *) downstream_port
,
461 (void *) upstream_port
);
462 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
463 if (component_status
== BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
464 BT_LOGD_STR("Downstream component refused the connection.");
466 BT_LOGW("Cannot ask downstream component to accept the connection: "
467 "status=%s", bt_self_component_status_string(component_status
));
470 status
= (int) component_status
;
474 BT_LOGD_STR("Creating connection.");
475 connection
= bt_connection_create(graph
, (void *) upstream_port
,
476 (void *) downstream_port
);
478 BT_LOGW("Cannot create connection object.");
479 status
= BT_GRAPH_STATUS_NOMEM
;
483 BT_LIB_LOGD("Connection object created: %!+x", connection
);
486 * Ownership of upstream_component/downstream_component and of
487 * the connection object is transferred to the graph.
489 g_ptr_array_add(graph
->connections
, connection
);
492 * Notify both components that their port is connected.
494 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
495 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
496 component_status
= bt_component_port_connected(upstream_component
,
497 (void *) upstream_port
, (void *) downstream_port
);
498 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
499 BT_LIB_LOGW("Error while notifying upstream component that its port is connected: "
500 "status=%s, %![graph-]+g, %![up-comp-]+c, "
501 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
502 bt_self_component_status_string(component_status
),
503 graph
, upstream_component
, downstream_component
,
504 upstream_port
, downstream_port
);
505 bt_connection_end(connection
, true);
506 status
= (int) component_status
;
510 connection
->notified_upstream_port_connected
= true;
511 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
512 "%![comp-]+c, %![port-]+p", downstream_component
,
514 component_status
= bt_component_port_connected(downstream_component
,
515 (void *) downstream_port
, (void *) upstream_port
);
516 if (component_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
517 BT_LIB_LOGW("Error while notifying downstream component that its port is connected: "
518 "status=%s, %![graph-]+g, %![up-comp-]+c, "
519 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
520 bt_self_component_status_string(component_status
),
521 graph
, upstream_component
, downstream_component
,
522 upstream_port
, downstream_port
);
523 bt_connection_end(connection
, true);
524 status
= (int) component_status
;
528 connection
->notified_downstream_port_connected
= true;
531 * Notify the graph's creator that both ports are connected.
533 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
534 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
535 connection
->notified_graph_ports_connected
= true;
536 BT_LIB_LOGD("Connected component ports within graph: "
537 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
538 "%![up-port-]+p, %![down-port-]+p",
539 graph
, upstream_component
, downstream_component
,
540 upstream_port
, downstream_port
);
542 if (user_connection
) {
543 /* Move reference to user */
544 *user_connection
= connection
;
549 if (status
!= BT_GRAPH_STATUS_OK
) {
550 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_FAULTY
;
553 bt_object_put_ref(connection
);
554 (void) init_can_consume
;
555 bt_graph_set_can_consume(graph
, init_can_consume
);
560 enum bt_graph_status
consume_graph_sink(struct bt_component_sink
*comp
)
562 enum bt_self_component_status comp_status
;
563 struct bt_component_class_sink
*sink_class
= NULL
;
566 sink_class
= (void *) comp
->parent
.class;
567 BT_ASSERT(sink_class
->methods
.consume
);
568 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
569 comp_status
= sink_class
->methods
.consume((void *) comp
);
570 BT_LOGD("User method returned: status=%s",
571 bt_self_component_status_string(comp_status
));
572 BT_ASSERT_PRE(comp_status
== BT_SELF_COMPONENT_STATUS_OK
||
573 comp_status
== BT_SELF_COMPONENT_STATUS_END
||
574 comp_status
== BT_SELF_COMPONENT_STATUS_AGAIN
||
575 comp_status
== BT_SELF_COMPONENT_STATUS_ERROR
||
576 comp_status
== BT_SELF_COMPONENT_STATUS_NOMEM
,
577 "Invalid component status returned by consuming method: "
578 "status=%s", bt_self_component_status_string(comp_status
));
579 if (comp_status
< 0) {
580 BT_LOGW_STR("Consume method failed.");
584 BT_LIB_LOGV("Consumed from sink: %![comp-]+c, status=%s",
585 comp
, bt_self_component_status_string(comp_status
));
588 return (int) comp_status
;
592 * `node` is removed from the queue of sinks to consume when passed to
593 * this function. This function adds it back to the queue if there's
594 * still something to consume afterwards.
597 enum bt_graph_status
consume_sink_node(struct bt_graph
*graph
, GList
*node
)
599 enum bt_graph_status status
;
600 struct bt_component_sink
*sink
;
603 status
= consume_graph_sink(sink
);
604 if (unlikely(status
!= BT_GRAPH_STATUS_END
)) {
605 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
609 /* End reached, the node is not added back to the queue and free'd. */
610 g_queue_delete_link(graph
->sinks_to_consume
, node
);
612 /* Don't forward an END status if there are sinks left to consume. */
613 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
614 status
= BT_GRAPH_STATUS_OK
;
619 BT_LIB_LOGV("Consumed sink node: %![comp-]+c, status=%s",
620 sink
, bt_graph_status_string(status
));
625 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
626 struct bt_component_sink
*sink
)
628 enum bt_graph_status status
;
632 BT_LIB_LOGV("Making specific sink consume: %![comp-]+c", sink
);
633 BT_ASSERT(bt_component_borrow_graph((void *) sink
) == graph
);
635 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
636 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
637 status
= BT_GRAPH_STATUS_END
;
641 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
643 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
644 status
= BT_GRAPH_STATUS_END
;
648 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
649 BT_ASSERT(sink_node
);
650 status
= consume_sink_node(graph
, sink_node
);
657 enum bt_graph_status
consume_no_check(struct bt_graph
*graph
)
659 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
660 struct bt_component
*sink
;
663 BT_ASSERT_PRE(graph
->has_sink
,
664 "Graph has no sink component: %!+g", graph
);
665 BT_LIB_LOGV("Making next sink consume: %![graph-]+g", graph
);
667 if (unlikely(g_queue_is_empty(graph
->sinks_to_consume
))) {
668 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
669 status
= BT_GRAPH_STATUS_END
;
673 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
674 sink
= current_node
->data
;
675 BT_LIB_LOGV("Chose next sink to consume: %!+c", sink
);
676 status
= consume_sink_node(graph
, current_node
);
682 enum bt_graph_status
bt_graph_consume(struct bt_graph
*graph
)
684 enum bt_graph_status status
;
686 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
687 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
688 BT_ASSERT_PRE(graph
->can_consume
,
689 "Cannot consume graph in its current state: %!+g", graph
);
690 BT_ASSERT_PRE(graph
->config_state
!= BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
691 "Graph is in a faulty state: %!+g", graph
);
692 bt_graph_set_can_consume(graph
, false);
693 status
= bt_graph_configure(graph
);
694 if (unlikely(status
)) {
695 /* bt_graph_configure() logs errors */
699 status
= consume_no_check(graph
);
700 bt_graph_set_can_consume(graph
, true);
706 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
708 enum bt_graph_status status
;
710 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
711 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
712 BT_ASSERT_PRE(graph
->can_consume
,
713 "Cannot consume graph in its current state: %!+g", graph
);
714 BT_ASSERT_PRE(graph
->config_state
!= BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
715 "Graph is in a faulty state: %!+g", graph
);
716 bt_graph_set_can_consume(graph
, false);
717 status
= bt_graph_configure(graph
);
718 if (unlikely(status
)) {
719 /* bt_graph_configure() logs errors */
723 BT_LIB_LOGV("Running graph: %!+g", graph
);
727 * Check if the graph is canceled at each iteration. If
728 * the graph was canceled by another thread or by a
729 * signal handler, this is not a warning nor an error,
730 * it was intentional: log with a DEBUG level only.
732 if (unlikely(graph
->canceled
)) {
733 BT_LIB_LOGD("Stopping the graph: graph is canceled: "
735 status
= BT_GRAPH_STATUS_CANCELED
;
739 status
= consume_no_check(graph
);
740 if (unlikely(status
== BT_GRAPH_STATUS_AGAIN
)) {
742 * If AGAIN is received and there are multiple
743 * sinks, go ahead and consume from the next
746 * However, in the case where a single sink is
747 * left, the caller can decide to busy-wait and
748 * call bt_graph_run() continuously
749 * until the source is ready or it can decide to
750 * sleep for an arbitrary amount of time.
752 if (graph
->sinks_to_consume
->length
> 1) {
753 status
= BT_GRAPH_STATUS_OK
;
755 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
758 } while (status
== BT_GRAPH_STATUS_OK
);
760 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
761 status
= BT_GRAPH_STATUS_END
;
765 BT_LIB_LOGV("Graph ran: %![graph-]+g, status=%s", graph
,
766 bt_graph_status_string(status
));
767 bt_graph_set_can_consume(graph
, true);
772 bt_graph_add_source_component_output_port_added_listener(
773 struct bt_graph
*graph
,
774 bt_graph_source_component_output_port_added_listener_func func
,
775 bt_graph_listener_removed_func listener_removed
, void *data
,
776 int *out_listener_id
)
778 struct bt_graph_listener_port_added listener
= {
780 .removed
= listener_removed
,
783 .func
= (port_added_func_t
) func
,
787 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
788 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
789 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
790 BT_ASSERT_PRE(!graph
->in_remove_listener
,
791 "Graph currently executing a \"listener removed\" listener: "
793 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
794 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
795 BT_LIB_LOGV("Added \"source component output port added\" listener to graph: "
796 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
800 *out_listener_id
= listener_id
;
803 return BT_GRAPH_STATUS_OK
;
807 bt_graph_add_filter_component_output_port_added_listener(
808 struct bt_graph
*graph
,
809 bt_graph_filter_component_output_port_added_listener_func func
,
810 bt_graph_listener_removed_func listener_removed
, void *data
,
811 int *out_listener_id
)
813 struct bt_graph_listener_port_added listener
= {
815 .removed
= listener_removed
,
818 .func
= (port_added_func_t
) func
,
822 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
823 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
824 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
825 BT_ASSERT_PRE(!graph
->in_remove_listener
,
826 "Graph currently executing a \"listener removed\" listener: "
828 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
829 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
830 BT_LIB_LOGV("Added \"filter component output port added\" listener to graph: "
831 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
835 *out_listener_id
= listener_id
;
838 return BT_GRAPH_STATUS_OK
;
842 bt_graph_add_filter_component_input_port_added_listener(
843 struct bt_graph
*graph
,
844 bt_graph_filter_component_input_port_added_listener_func func
,
845 bt_graph_listener_removed_func listener_removed
, void *data
,
846 int *out_listener_id
)
848 struct bt_graph_listener_port_added listener
= {
850 .removed
= listener_removed
,
853 .func
= (port_added_func_t
) func
,
857 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
858 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
859 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
860 BT_ASSERT_PRE(!graph
->in_remove_listener
,
861 "Graph currently executing a \"listener removed\" listener: "
863 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
864 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
865 BT_LIB_LOGV("Added \"filter component input port added\" listener to graph: "
866 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
870 *out_listener_id
= listener_id
;
873 return BT_GRAPH_STATUS_OK
;
877 bt_graph_add_sink_component_input_port_added_listener(
878 struct bt_graph
*graph
,
879 bt_graph_sink_component_input_port_added_listener_func func
,
880 bt_graph_listener_removed_func listener_removed
, void *data
,
881 int *out_listener_id
)
883 struct bt_graph_listener_port_added listener
= {
885 .removed
= listener_removed
,
888 .func
= (port_added_func_t
) func
,
892 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
893 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
894 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
895 BT_ASSERT_PRE(!graph
->in_remove_listener
,
896 "Graph currently executing a \"listener removed\" listener: "
898 g_array_append_val(graph
->listeners
.sink_input_port_added
, listener
);
899 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
900 BT_LIB_LOGV("Added \"sink component input port added\" listener to graph: "
901 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
905 *out_listener_id
= listener_id
;
908 return BT_GRAPH_STATUS_OK
;
912 bt_graph_add_source_filter_component_ports_connected_listener(
913 struct bt_graph
*graph
,
914 bt_graph_source_filter_component_ports_connected_listener_func func
,
915 bt_graph_listener_removed_func listener_removed
, void *data
,
916 int *out_listener_id
)
918 struct bt_graph_listener_ports_connected listener
= {
920 .removed
= listener_removed
,
923 .func
= (ports_connected_func_t
) func
,
927 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
928 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
929 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
930 BT_ASSERT_PRE(!graph
->in_remove_listener
,
931 "Graph currently executing a \"listener removed\" listener: "
933 g_array_append_val(graph
->listeners
.source_filter_ports_connected
,
935 listener_id
= graph
->listeners
.source_filter_ports_connected
->len
- 1;
936 BT_LIB_LOGV("Added \"source to filter component ports connected\" listener to graph: "
937 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
941 *out_listener_id
= listener_id
;
944 return BT_GRAPH_STATUS_OK
;
948 bt_graph_add_source_sink_component_ports_connected_listener(
949 struct bt_graph
*graph
,
950 bt_graph_source_sink_component_ports_connected_listener_func func
,
951 bt_graph_listener_removed_func listener_removed
, void *data
,
952 int *out_listener_id
)
954 struct bt_graph_listener_ports_connected listener
= {
956 .removed
= listener_removed
,
959 .func
= (ports_connected_func_t
) func
,
963 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
964 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
965 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
966 BT_ASSERT_PRE(!graph
->in_remove_listener
,
967 "Graph currently executing a \"listener removed\" listener: "
969 g_array_append_val(graph
->listeners
.source_sink_ports_connected
,
971 listener_id
= graph
->listeners
.source_sink_ports_connected
->len
- 1;
972 BT_LIB_LOGV("Added \"source to sink component ports connected\" listener to graph: "
973 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
977 *out_listener_id
= listener_id
;
980 return BT_GRAPH_STATUS_OK
;
984 bt_graph_add_filter_filter_component_ports_connected_listener(
985 struct bt_graph
*graph
,
986 bt_graph_filter_filter_component_ports_connected_listener_func func
,
987 bt_graph_listener_removed_func listener_removed
, void *data
,
988 int *out_listener_id
)
990 struct bt_graph_listener_ports_connected listener
= {
992 .removed
= listener_removed
,
995 .func
= (ports_connected_func_t
) func
,
999 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1000 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1001 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1002 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1003 "Graph currently executing a \"listener removed\" listener: "
1005 g_array_append_val(graph
->listeners
.filter_filter_ports_connected
,
1007 listener_id
= graph
->listeners
.filter_filter_ports_connected
->len
- 1;
1008 BT_LIB_LOGV("Added \"filter to filter component ports connected\" listener to graph: "
1009 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1013 *out_listener_id
= listener_id
;
1016 return BT_GRAPH_STATUS_OK
;
1019 enum bt_graph_status
1020 bt_graph_add_filter_sink_component_ports_connected_listener(
1021 struct bt_graph
*graph
,
1022 bt_graph_filter_sink_component_ports_connected_listener_func func
,
1023 bt_graph_listener_removed_func listener_removed
, void *data
,
1024 int *out_listener_id
)
1026 struct bt_graph_listener_ports_connected listener
= {
1028 .removed
= listener_removed
,
1031 .func
= (ports_connected_func_t
) func
,
1035 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1036 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1037 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1038 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1039 "Graph currently executing a \"listener removed\" listener: "
1041 g_array_append_val(graph
->listeners
.filter_sink_ports_connected
,
1043 listener_id
= graph
->listeners
.filter_sink_ports_connected
->len
- 1;
1044 BT_LIB_LOGV("Added \"filter to sink component ports connected\" listener to graph: "
1045 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1049 *out_listener_id
= listener_id
;
1052 return BT_GRAPH_STATUS_OK
;
1056 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
1060 struct bt_component
*comp
;
1064 BT_LIB_LOGV("Notifying graph listeners that a port was added: "
1065 "%![graph-]+g, %![port-]+p", graph
, port
);
1066 comp
= bt_port_borrow_component_inline(port
);
1069 switch (comp
->class->type
) {
1070 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1072 switch (port
->type
) {
1073 case BT_PORT_TYPE_OUTPUT
:
1074 listeners
= graph
->listeners
.source_output_port_added
;
1082 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1084 switch (port
->type
) {
1085 case BT_PORT_TYPE_INPUT
:
1086 listeners
= graph
->listeners
.filter_input_port_added
;
1088 case BT_PORT_TYPE_OUTPUT
:
1089 listeners
= graph
->listeners
.filter_output_port_added
;
1097 case BT_COMPONENT_CLASS_TYPE_SINK
:
1099 switch (port
->type
) {
1100 case BT_PORT_TYPE_INPUT
:
1101 listeners
= graph
->listeners
.sink_input_port_added
;
1113 for (i
= 0; i
< listeners
->len
; i
++) {
1114 struct bt_graph_listener_port_added
*listener
=
1115 &g_array_index(listeners
,
1116 struct bt_graph_listener_port_added
, i
);
1118 BT_ASSERT(listener
->func
);
1119 listener
->func(comp
, port
, listener
->base
.data
);
1124 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
1125 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
1129 struct bt_component
*upstream_comp
;
1130 struct bt_component
*downstream_comp
;
1133 BT_ASSERT(upstream_port
);
1134 BT_ASSERT(downstream_port
);
1135 BT_LIB_LOGV("Notifying graph listeners that ports were connected: "
1136 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1137 graph
, upstream_port
, downstream_port
);
1138 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
1139 BT_ASSERT(upstream_comp
);
1140 downstream_comp
= bt_port_borrow_component_inline(downstream_port
);
1141 BT_ASSERT(downstream_comp
);
1143 switch (upstream_comp
->class->type
) {
1144 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1146 switch (downstream_comp
->class->type
) {
1147 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1149 graph
->listeners
.source_filter_ports_connected
;
1151 case BT_COMPONENT_CLASS_TYPE_SINK
:
1153 graph
->listeners
.source_sink_ports_connected
;
1161 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1163 switch (downstream_comp
->class->type
) {
1164 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1166 graph
->listeners
.filter_filter_ports_connected
;
1168 case BT_COMPONENT_CLASS_TYPE_SINK
:
1170 graph
->listeners
.filter_sink_ports_connected
;
1182 for (i
= 0; i
< listeners
->len
; i
++) {
1183 struct bt_graph_listener_ports_connected
*listener
=
1184 &g_array_index(listeners
,
1185 struct bt_graph_listener_ports_connected
, i
);
1187 BT_ASSERT(listener
->func
);
1188 listener
->func(upstream_comp
, downstream_comp
,
1189 upstream_port
, downstream_port
, listener
->base
.data
);
1193 enum bt_graph_status
bt_graph_cancel(struct bt_graph
*graph
)
1196 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1197 graph
->canceled
= true;
1198 BT_LIB_LOGV("Canceled graph: %!+i", graph
);
1199 return BT_GRAPH_STATUS_OK
;
1202 bt_bool
bt_graph_is_canceled(const struct bt_graph
*graph
)
1204 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1205 return graph
->canceled
? BT_TRUE
: BT_FALSE
;
1209 void bt_graph_remove_connection(struct bt_graph
*graph
,
1210 struct bt_connection
*connection
)
1213 BT_ASSERT(connection
);
1214 BT_LIB_LOGV("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1216 g_ptr_array_remove(graph
->connections
, connection
);
1221 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
1223 bool exists
= false;
1226 for (i
= 0; i
< graph
->components
->len
; i
++) {
1227 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
1229 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1230 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1231 "%![other-comp-]+c, name=\"%s\"",
1243 enum bt_graph_status
add_component_with_init_method_data(
1244 struct bt_graph
*graph
,
1245 struct bt_component_class
*comp_cls
,
1246 comp_init_method_t init_method
,
1247 const char *name
, const struct bt_value
*params
,
1248 void *init_method_data
, struct bt_component
**user_component
)
1250 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
1251 enum bt_self_component_status comp_status
;
1252 struct bt_component
*component
= NULL
;
1254 bool init_can_consume
;
1255 struct bt_value
*new_params
= NULL
;
1257 BT_ASSERT(comp_cls
);
1258 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1259 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1260 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
1262 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1263 "Graph is not in the \"configuring\" state: %!+g", graph
);
1264 BT_ASSERT_PRE(!component_name_exists(graph
, name
),
1265 "Duplicate component name: %!+g, name=\"%s\"", graph
, name
);
1266 BT_ASSERT_PRE(!params
|| bt_value_is_map(params
),
1267 "Parameter value is not a map value: %!+v", params
);
1268 init_can_consume
= graph
->can_consume
;
1269 bt_graph_set_can_consume(graph
, false);
1270 BT_LIB_LOGD("Adding component to graph: "
1271 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1272 "init-method-data-addr=%p",
1273 graph
, comp_cls
, name
, params
, init_method_data
);
1276 new_params
= bt_value_map_create();
1278 BT_LOGE_STR("Cannot create map value object.");
1279 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1283 params
= new_params
;
1286 ret
= bt_component_create(comp_cls
, name
, &component
);
1288 BT_LOGE("Cannot create empty component object: ret=%d",
1290 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1295 * The user's initialization method needs to see that this
1296 * component is part of the graph. If the user method fails, we
1297 * immediately remove the component from the graph's components.
1299 g_ptr_array_add(graph
->components
, component
);
1300 bt_component_set_graph(component
, graph
);
1301 bt_value_freeze(params
);
1304 BT_LOGD_STR("Calling user's initialization method.");
1305 comp_status
= init_method(component
, params
, init_method_data
);
1306 BT_LOGD("User method returned: status=%s",
1307 bt_self_component_status_string(comp_status
));
1308 if (comp_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
1309 BT_LOGW_STR("Initialization method failed.");
1310 graph_status
= (int) comp_status
;
1311 bt_component_set_graph(component
, NULL
);
1312 g_ptr_array_remove_fast(graph
->components
, component
);
1318 * Mark the component as initialized so that its finalization
1319 * method is called when it is destroyed.
1321 component
->initialized
= true;
1324 * If it's a sink component, it needs to be part of the graph's
1325 * sink queue to be consumed by bt_graph_consume().
1327 if (bt_component_is_sink(component
)) {
1328 graph
->has_sink
= true;
1329 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1333 * Freeze the component class now that it's instantiated at
1336 BT_LOGD_STR("Freezing component class.");
1337 bt_component_class_freeze(comp_cls
);
1338 BT_LIB_LOGD("Added component to graph: "
1339 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1340 "init-method-data-addr=%p, %![comp-]+c",
1341 graph
, comp_cls
, name
, params
, init_method_data
, component
);
1343 if (user_component
) {
1344 /* Move reference to user */
1345 *user_component
= component
;
1350 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1351 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_FAULTY
;
1354 bt_object_put_ref(component
);
1355 bt_object_put_ref(new_params
);
1356 (void) init_can_consume
;
1357 bt_graph_set_can_consume(graph
, init_can_consume
);
1358 return graph_status
;
1361 enum bt_graph_status
1362 bt_graph_add_source_component_with_init_method_data(
1363 struct bt_graph
*graph
,
1364 const struct bt_component_class_source
*comp_cls
,
1365 const char *name
, const struct bt_value
*params
,
1366 void *init_method_data
,
1367 const struct bt_component_source
**component
)
1369 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1370 return add_component_with_init_method_data(graph
,
1371 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1372 name
, params
, init_method_data
, (void *) component
);
1375 enum bt_graph_status
bt_graph_add_source_component(
1376 struct bt_graph
*graph
,
1377 const struct bt_component_class_source
*comp_cls
,
1378 const char *name
, const struct bt_value
*params
,
1379 const struct bt_component_source
**component
)
1381 return bt_graph_add_source_component_with_init_method_data(
1382 graph
, comp_cls
, name
, params
, NULL
, component
);
1385 enum bt_graph_status
1386 bt_graph_add_filter_component_with_init_method_data(
1387 struct bt_graph
*graph
,
1388 const struct bt_component_class_filter
*comp_cls
,
1389 const char *name
, const struct bt_value
*params
,
1390 void *init_method_data
,
1391 const struct bt_component_filter
**component
)
1393 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1394 return add_component_with_init_method_data(graph
,
1395 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1396 name
, params
, init_method_data
, (void *) component
);
1399 enum bt_graph_status
bt_graph_add_filter_component(
1400 struct bt_graph
*graph
,
1401 const struct bt_component_class_filter
*comp_cls
,
1402 const char *name
, const struct bt_value
*params
,
1403 const struct bt_component_filter
**component
)
1405 return bt_graph_add_filter_component_with_init_method_data(
1406 graph
, comp_cls
, name
, params
, NULL
, component
);
1409 enum bt_graph_status
1410 bt_graph_add_sink_component_with_init_method_data(
1411 struct bt_graph
*graph
,
1412 const struct bt_component_class_sink
*comp_cls
,
1413 const char *name
, const struct bt_value
*params
,
1414 void *init_method_data
,
1415 const struct bt_component_sink
**component
)
1417 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1418 return add_component_with_init_method_data(graph
,
1419 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1420 name
, params
, init_method_data
, (void *) component
);
1423 enum bt_graph_status
bt_graph_add_sink_component(
1424 struct bt_graph
*graph
,
1425 const struct bt_component_class_sink
*comp_cls
,
1426 const char *name
, const struct bt_value
*params
,
1427 const struct bt_component_sink
**component
)
1429 return bt_graph_add_sink_component_with_init_method_data(
1430 graph
, comp_cls
, name
, params
, NULL
, component
);
1434 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1435 struct bt_component
*component
)
1437 bool init_can_consume
;
1443 BT_ASSERT(component
);
1444 BT_ASSERT(component
->base
.ref_count
== 0);
1445 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1447 init_can_consume
= graph
->can_consume
;
1448 count
= bt_component_get_input_port_count(component
);
1450 for (i
= 0; i
< count
; i
++) {
1451 struct bt_port
*port
= (void *)
1452 bt_component_borrow_input_port_by_index(component
, i
);
1456 if (bt_port_is_connected(port
)) {
1457 BT_LIB_LOGW("Cannot remove component from graph: "
1458 "an input port is connected: "
1459 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1460 graph
, component
, port
);
1465 count
= bt_component_get_output_port_count(component
);
1467 for (i
= 0; i
< count
; i
++) {
1468 struct bt_port
*port
= (void *)
1469 bt_component_borrow_output_port_by_index(component
, i
);
1473 if (bt_port_is_connected(port
)) {
1474 BT_LIB_LOGW("Cannot remove component from graph: "
1475 "an output port is connected: "
1476 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1477 graph
, component
, port
);
1482 bt_graph_set_can_consume(graph
, false);
1484 /* Possibly remove from sinks to consume */
1485 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1487 if (graph
->sinks_to_consume
->length
== 0) {
1488 graph
->has_sink
= false;
1492 * This calls bt_object_try_spec_release() on the component, and
1493 * since its reference count is 0, its destructor is called. Its
1494 * destructor calls the user's finalization method (if set).
1496 g_ptr_array_remove(graph
->components
, component
);
1503 (void) init_can_consume
;
1504 bt_graph_set_can_consume(graph
, init_can_consume
);
1509 void bt_graph_add_message(struct bt_graph
*graph
,
1510 struct bt_message
*msg
)
1516 * It's okay not to take a reference because, when a
1517 * message's reference count drops to 0, either:
1519 * * It is recycled back to one of this graph's pool.
1520 * * It is destroyed because it doesn't have any link to any
1521 * graph, which means the original graph is already destroyed.
1523 g_ptr_array_add(graph
->messages
, msg
);
1526 void bt_graph_get_ref(const struct bt_graph
*graph
)
1528 bt_object_get_ref(graph
);
1531 void bt_graph_put_ref(const struct bt_graph
*graph
)
1533 bt_object_put_ref(graph
);