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 "LIB/GRAPH"
25 #include "lib/logging.h"
27 #include "common/assert.h"
28 #include "lib/assert-pre.h"
29 #include "lib/assert-post.h"
30 #include <babeltrace2/graph/graph.h>
31 #include <babeltrace2/graph/graph-const.h>
32 #include <babeltrace2/graph/component-source-const.h>
33 #include <babeltrace2/graph/component-filter-const.h>
34 #include <babeltrace2/graph/port-const.h>
35 #include "lib/graph/message/message.h"
36 #include "compat/compiler.h"
37 #include "common/common.h"
38 #include <babeltrace2/types.h>
39 #include <babeltrace2/value.h>
40 #include <babeltrace2/value-const.h>
41 #include "lib/value.h"
45 #include "component.h"
46 #include "component-sink.h"
47 #include "connection.h"
49 #include "message/event.h"
50 #include "message/packet.h"
52 typedef enum bt_graph_listener_func_status
53 (*port_added_func_t
)(const void *, const void *, void *);
55 typedef enum bt_graph_listener_func_status
56 (*ports_connected_func_t
)(const void *, const void *, const void *,
57 const void *, void *);
59 typedef enum bt_component_class_init_method_status
60 (*comp_init_method_t
)(const void *, const void *, void *);
62 struct bt_graph_listener
{
63 bt_graph_listener_removed_func removed
;
67 struct bt_graph_listener_port_added
{
68 struct bt_graph_listener base
;
69 port_added_func_t func
;
72 struct bt_graph_listener_ports_connected
{
73 struct bt_graph_listener base
;
74 ports_connected_func_t func
;
77 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
79 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
80 if (!(_listeners)) { \
81 BT_LIB_LOGE_APPEND_CAUSE( \
82 "Failed to allocate one GArray."); \
86 #define CALL_REMOVE_LISTENERS(_type, _listeners) \
93 for (i = 0; i < (_listeners)->len; i++) { \
95 &g_array_index((_listeners), _type, i); \
97 if (listener->base.removed) { \
98 listener->base.removed(listener->base.data); \
104 void destroy_graph(struct bt_object
*obj
)
106 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
109 * The graph's reference count is 0 if we're here. Increment
110 * it to avoid a double-destroy (possibly infinitely recursive)
113 * 1. We put and destroy a connection.
114 * 2. This connection's destructor finalizes its active message
116 * 3. A message iterator's finalization function gets a new
117 * reference on its component (reference count goes from 0 to
119 * 4. Since this component's reference count goes to 1, it takes
120 * a reference on its parent (this graph). This graph's
121 * reference count goes from 0 to 1.
122 * 5. The message iterator's finalization function puts its
123 * component reference (reference count goes from 1 to 0).
124 * 6. Since this component's reference count goes from 1 to 0,
125 * it puts its parent (this graph). This graph's reference
126 * count goes from 1 to 0.
127 * 7. Since this graph's reference count goes from 1 to 0, its
128 * destructor is called (this function).
130 * With the incrementation below, the graph's reference count at
131 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
132 * ensures that this function is not called two times.
134 BT_LIB_LOGI("Destroying graph: %!+g", graph
);
138 * Cancel the graph to disallow some operations, like creating
139 * message iterators and adding ports to components.
141 (void) bt_graph_cancel((void *) graph
);
143 /* Call all remove listeners */
144 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
145 graph
->listeners
.source_output_port_added
);
146 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
147 graph
->listeners
.filter_output_port_added
);
148 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
149 graph
->listeners
.filter_input_port_added
);
150 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
151 graph
->listeners
.sink_input_port_added
);
152 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
153 graph
->listeners
.source_filter_ports_connected
);
154 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
155 graph
->listeners
.filter_filter_ports_connected
);
156 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
157 graph
->listeners
.source_sink_ports_connected
);
158 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
159 graph
->listeners
.filter_sink_ports_connected
);
161 if (graph
->messages
) {
162 g_ptr_array_free(graph
->messages
, TRUE
);
163 graph
->messages
= NULL
;
166 if (graph
->connections
) {
167 BT_LOGD_STR("Destroying connections.");
168 g_ptr_array_free(graph
->connections
, TRUE
);
169 graph
->connections
= NULL
;
172 if (graph
->components
) {
173 BT_LOGD_STR("Destroying components.");
174 g_ptr_array_free(graph
->components
, TRUE
);
175 graph
->components
= NULL
;
178 if (graph
->sinks_to_consume
) {
179 g_queue_free(graph
->sinks_to_consume
);
180 graph
->sinks_to_consume
= NULL
;
183 if (graph
->listeners
.source_output_port_added
) {
184 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
185 graph
->listeners
.source_output_port_added
= NULL
;
188 if (graph
->listeners
.filter_output_port_added
) {
189 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
190 graph
->listeners
.filter_output_port_added
= NULL
;
193 if (graph
->listeners
.filter_input_port_added
) {
194 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
195 graph
->listeners
.filter_input_port_added
= NULL
;
198 if (graph
->listeners
.sink_input_port_added
) {
199 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
200 graph
->listeners
.sink_input_port_added
= NULL
;
203 if (graph
->listeners
.source_filter_ports_connected
) {
204 g_array_free(graph
->listeners
.source_filter_ports_connected
,
206 graph
->listeners
.source_filter_ports_connected
= NULL
;
209 if (graph
->listeners
.filter_filter_ports_connected
) {
210 g_array_free(graph
->listeners
.filter_filter_ports_connected
,
212 graph
->listeners
.filter_filter_ports_connected
= NULL
;
215 if (graph
->listeners
.source_sink_ports_connected
) {
216 g_array_free(graph
->listeners
.source_sink_ports_connected
,
218 graph
->listeners
.source_sink_ports_connected
= NULL
;
221 if (graph
->listeners
.filter_sink_ports_connected
) {
222 g_array_free(graph
->listeners
.filter_sink_ports_connected
,
224 graph
->listeners
.filter_sink_ports_connected
= NULL
;
227 bt_object_pool_finalize(&graph
->event_msg_pool
);
228 bt_object_pool_finalize(&graph
->packet_begin_msg_pool
);
229 bt_object_pool_finalize(&graph
->packet_end_msg_pool
);
234 void destroy_message_event(struct bt_message
*msg
,
235 struct bt_graph
*graph
)
237 bt_message_event_destroy(msg
);
241 void destroy_message_packet_begin(struct bt_message
*msg
,
242 struct bt_graph
*graph
)
244 bt_message_packet_destroy(msg
);
248 void destroy_message_packet_end(struct bt_message
*msg
,
249 struct bt_graph
*graph
)
251 bt_message_packet_destroy(msg
);
255 void notify_message_graph_is_destroyed(struct bt_message
*msg
)
257 bt_message_unlink_graph(msg
);
260 struct bt_graph
*bt_graph_create(void)
262 struct bt_graph
*graph
;
265 BT_LOGI_STR("Creating graph object.");
266 graph
= g_new0(struct bt_graph
, 1);
268 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph.");
272 bt_object_init_shared(&graph
->base
, destroy_graph
);
273 graph
->connections
= g_ptr_array_new_with_free_func(
274 (GDestroyNotify
) bt_object_try_spec_release
);
275 if (!graph
->connections
) {
276 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
279 graph
->components
= g_ptr_array_new_with_free_func(
280 (GDestroyNotify
) bt_object_try_spec_release
);
281 if (!graph
->components
) {
282 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
285 graph
->sinks_to_consume
= g_queue_new();
286 if (!graph
->sinks_to_consume
) {
287 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue.");
291 bt_graph_set_can_consume(graph
, true);
292 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
293 graph
->listeners
.source_output_port_added
);
295 if (!graph
->listeners
.source_output_port_added
) {
300 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
301 graph
->listeners
.filter_output_port_added
);
303 if (!graph
->listeners
.filter_output_port_added
) {
308 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
309 graph
->listeners
.filter_input_port_added
);
311 if (!graph
->listeners
.filter_input_port_added
) {
316 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
317 graph
->listeners
.sink_input_port_added
);
319 if (!graph
->listeners
.sink_input_port_added
) {
324 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
325 graph
->listeners
.source_filter_ports_connected
);
327 if (!graph
->listeners
.source_filter_ports_connected
) {
332 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
333 graph
->listeners
.source_sink_ports_connected
);
335 if (!graph
->listeners
.source_sink_ports_connected
) {
340 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
341 graph
->listeners
.filter_filter_ports_connected
);
343 if (!graph
->listeners
.filter_filter_ports_connected
) {
348 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
349 graph
->listeners
.filter_sink_ports_connected
);
351 if (!graph
->listeners
.filter_sink_ports_connected
) {
356 ret
= bt_object_pool_initialize(&graph
->event_msg_pool
,
357 (bt_object_pool_new_object_func
) bt_message_event_new
,
358 (bt_object_pool_destroy_object_func
) destroy_message_event
,
361 BT_LIB_LOGE_APPEND_CAUSE(
362 "Failed to initialize event message pool: ret=%d",
367 ret
= bt_object_pool_initialize(&graph
->packet_begin_msg_pool
,
368 (bt_object_pool_new_object_func
) bt_message_packet_beginning_new
,
369 (bt_object_pool_destroy_object_func
) destroy_message_packet_begin
,
372 BT_LIB_LOGE_APPEND_CAUSE(
373 "Failed to initialize packet beginning message pool: ret=%d",
378 ret
= bt_object_pool_initialize(&graph
->packet_end_msg_pool
,
379 (bt_object_pool_new_object_func
) bt_message_packet_end_new
,
380 (bt_object_pool_destroy_object_func
) destroy_message_packet_end
,
383 BT_LIB_LOGE_APPEND_CAUSE(
384 "Failed to initialize packet end message pool: ret=%d",
389 graph
->messages
= g_ptr_array_new_with_free_func(
390 (GDestroyNotify
) notify_message_graph_is_destroyed
);
391 BT_LIB_LOGI("Created graph object: %!+g", graph
);
394 return (void *) graph
;
397 BT_OBJECT_PUT_REF_AND_RESET(graph
);
401 enum bt_graph_connect_ports_status
bt_graph_connect_ports(
402 struct bt_graph
*graph
,
403 const struct bt_port_output
*upstream_port_out
,
404 const struct bt_port_input
*downstream_port_in
,
405 const struct bt_connection
**user_connection
)
407 enum bt_graph_connect_ports_status status
= BT_FUNC_STATUS_OK
;
408 enum bt_graph_listener_func_status listener_status
;
409 struct bt_connection
*connection
= NULL
;
410 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
411 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
412 struct bt_component
*upstream_component
= NULL
;
413 struct bt_component
*downstream_component
= NULL
;
414 enum bt_component_class_port_connected_method_status port_connected_status
;
415 bool init_can_consume
;
417 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
418 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
419 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
420 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
422 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
423 "Graph is not in the \"configuring\" state: %!+g", graph
);
424 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
425 "Upstream port is already connected: %!+p", upstream_port
);
426 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
427 "Downstream port is already connected: %!+p", downstream_port
);
428 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
429 "Upstream port does not belong to a component: %!+p",
431 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
432 "Downstream port does not belong to a component: %!+p",
434 init_can_consume
= graph
->can_consume
;
435 BT_LIB_LOGI("Connecting component ports within graph: "
436 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
437 graph
, upstream_port
, downstream_port
);
438 bt_graph_set_can_consume(graph
, false);
439 upstream_component
= bt_port_borrow_component_inline(
440 (void *) upstream_port
);
441 downstream_component
= bt_port_borrow_component_inline(
442 (void *) downstream_port
);
444 BT_LOGD_STR("Creating connection.");
445 connection
= bt_connection_create(graph
, (void *) upstream_port
,
446 (void *) downstream_port
);
448 BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object.");
449 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
453 BT_LIB_LOGD("Connection object created: %!+x", connection
);
456 * Ownership of upstream_component/downstream_component and of
457 * the connection object is transferred to the graph.
459 g_ptr_array_add(graph
->connections
, connection
);
462 * Notify both components that their port is connected.
464 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
465 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
466 port_connected_status
= bt_component_port_connected(upstream_component
,
467 (void *) upstream_port
, (void *) downstream_port
);
468 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
469 if (port_connected_status
< 0) {
470 BT_LIB_LOGW_APPEND_CAUSE(
471 "Upstream component's \"port connected\" method failed: "
472 "status=%s, %![graph-]+g, %![up-comp-]+c, "
473 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
474 bt_common_func_status_string(
475 port_connected_status
),
476 graph
, upstream_component
, downstream_component
,
477 upstream_port
, downstream_port
);
480 bt_connection_end(connection
, true);
481 status
= (int) port_connected_status
;
485 connection
->notified_upstream_port_connected
= true;
486 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
487 "%![comp-]+c, %![port-]+p", downstream_component
,
489 port_connected_status
= bt_component_port_connected(downstream_component
,
490 (void *) downstream_port
, (void *) upstream_port
);
491 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
492 if (port_connected_status
< 0) {
493 BT_LIB_LOGW_APPEND_CAUSE(
494 "Downstream component's \"port connected\" method failed: "
495 "status=%s, %![graph-]+g, %![up-comp-]+c, "
496 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
497 bt_common_func_status_string(
498 port_connected_status
),
499 graph
, upstream_component
, downstream_component
,
500 upstream_port
, downstream_port
);
503 bt_connection_end(connection
, true);
504 status
= (int) port_connected_status
;
508 connection
->notified_downstream_port_connected
= true;
511 * Notify the graph's creator that both ports are connected.
513 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
514 listener_status
= bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
515 if (listener_status
!= BT_FUNC_STATUS_OK
) {
516 if (listener_status
< 0) {
517 BT_LIB_LOGW_APPEND_CAUSE(
518 "Graph \"ports connected\" listener failed: "
519 "status=%d, %![graph-]+g, %![up-comp-]+c, "
520 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
521 listener_status
, graph
,
522 upstream_component
, downstream_component
,
523 upstream_port
, downstream_port
);
526 status
= (int) listener_status
;
530 connection
->notified_graph_ports_connected
= true;
531 BT_LIB_LOGI("Connected component ports within graph: "
532 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
533 "%![up-port-]+p, %![down-port-]+p",
534 graph
, upstream_component
, downstream_component
,
535 upstream_port
, downstream_port
);
537 if (user_connection
) {
538 /* Move reference to user */
539 *user_connection
= connection
;
544 if (status
!= BT_FUNC_STATUS_OK
) {
545 bt_graph_make_faulty(graph
);
548 bt_object_put_ref(connection
);
549 (void) init_can_consume
;
550 bt_graph_set_can_consume(graph
, init_can_consume
);
555 int consume_graph_sink(struct bt_component_sink
*comp
)
557 enum bt_component_class_sink_consume_method_status consume_status
;
558 struct bt_component_class_sink
*sink_class
= NULL
;
561 sink_class
= (void *) comp
->parent
.class;
562 BT_ASSERT(sink_class
->methods
.consume
);
563 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
564 consume_status
= sink_class
->methods
.consume((void *) comp
);
565 BT_LOGD("User method returned: status=%s",
566 bt_common_func_status_string(consume_status
));
567 BT_ASSERT_POST_DEV(consume_status
== BT_FUNC_STATUS_OK
||
568 consume_status
== BT_FUNC_STATUS_END
||
569 consume_status
== BT_FUNC_STATUS_AGAIN
||
570 consume_status
== BT_FUNC_STATUS_ERROR
||
571 consume_status
== BT_FUNC_STATUS_MEMORY_ERROR
,
572 "Invalid component status returned by consuming method: "
573 "status=%s", bt_common_func_status_string(consume_status
));
574 if (consume_status
) {
575 if (consume_status
< 0) {
576 BT_LIB_LOGW_APPEND_CAUSE(
577 "Component's \"consume\" method failed: "
578 "status=%s, %![comp-]+c",
579 bt_common_func_status_string(consume_status
),
586 BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s",
587 comp
, bt_common_func_status_string(consume_status
));
590 return consume_status
;
594 * `node` is removed from the queue of sinks to consume when passed to
595 * this function. This function adds it back to the queue if there's
596 * still something to consume afterwards.
599 int consume_sink_node(struct bt_graph
*graph
, GList
*node
)
602 struct bt_component_sink
*sink
;
605 status
= consume_graph_sink(sink
);
606 if (G_UNLIKELY(status
!= BT_FUNC_STATUS_END
)) {
607 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
611 /* End reached, the node is not added back to the queue and free'd. */
612 g_queue_delete_link(graph
->sinks_to_consume
, node
);
614 /* Don't forward an END status if there are sinks left to consume. */
615 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
616 status
= BT_FUNC_STATUS_OK
;
621 BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s",
622 sink
, bt_common_func_status_string(status
));
627 int bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
628 struct bt_component_sink
*sink
)
634 BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink
);
635 BT_ASSERT(bt_component_borrow_graph((void *) sink
) == graph
);
637 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
638 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
639 status
= BT_FUNC_STATUS_END
;
643 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
645 BT_LIB_LOGD("Sink component is not marked as consumable: "
646 "component sink is ended: %![comp-]+c", sink
);
647 status
= BT_FUNC_STATUS_END
;
651 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
652 BT_ASSERT(sink_node
);
653 status
= consume_sink_node(graph
, sink_node
);
660 int consume_no_check(struct bt_graph
*graph
)
662 int status
= BT_FUNC_STATUS_OK
;
663 struct bt_component
*sink
;
666 BT_ASSERT_PRE_DEV(graph
->has_sink
,
667 "Graph has no sink component: %!+g", graph
);
668 BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph
);
670 if (G_UNLIKELY(g_queue_is_empty(graph
->sinks_to_consume
))) {
671 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
672 status
= BT_FUNC_STATUS_END
;
676 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
677 sink
= current_node
->data
;
678 BT_LIB_LOGD("Chose next sink to consume: %!+c", sink
);
679 status
= consume_sink_node(graph
, current_node
);
685 enum bt_graph_consume_status
bt_graph_consume(struct bt_graph
*graph
)
687 enum bt_graph_consume_status status
;
689 BT_ASSERT_PRE_DEV_NON_NULL(graph
, "Graph");
690 BT_ASSERT_PRE_DEV(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
691 BT_ASSERT_PRE_DEV(graph
->can_consume
,
692 "Cannot consume graph in its current state: %!+g", graph
);
693 BT_ASSERT_PRE_DEV(graph
->config_state
!=
694 BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
695 "Graph is in a faulty state: %!+g", graph
);
696 bt_graph_set_can_consume(graph
, false);
697 status
= bt_graph_configure(graph
);
698 if (G_UNLIKELY(status
)) {
699 /* bt_graph_configure() logs errors */
703 status
= consume_no_check(graph
);
704 bt_graph_set_can_consume(graph
, true);
710 enum bt_graph_run_status
bt_graph_run(struct bt_graph
*graph
)
712 enum bt_graph_run_status status
;
714 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
715 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
716 BT_ASSERT_PRE(graph
->can_consume
,
717 "Cannot consume graph in its current state: %!+g", graph
);
718 BT_ASSERT_PRE(graph
->config_state
!= BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
719 "Graph is in a faulty state: %!+g", graph
);
720 bt_graph_set_can_consume(graph
, false);
721 status
= bt_graph_configure(graph
);
722 if (G_UNLIKELY(status
)) {
723 /* bt_graph_configure() logs errors */
727 BT_LIB_LOGI("Running graph: %!+g", graph
);
731 * Check if the graph is canceled at each iteration. If
732 * the graph was canceled by another thread or by a
733 * signal handler, this is not a warning nor an error,
734 * it was intentional: log with a DEBUG level only.
736 if (G_UNLIKELY(graph
->canceled
)) {
737 BT_LIB_LOGI("Stopping the graph: graph is canceled: "
739 status
= BT_FUNC_STATUS_CANCELED
;
743 status
= consume_no_check(graph
);
744 if (G_UNLIKELY(status
== BT_FUNC_STATUS_AGAIN
)) {
746 * If AGAIN is received and there are multiple
747 * sinks, go ahead and consume from the next
750 * However, in the case where a single sink is
751 * left, the caller can decide to busy-wait and
752 * call bt_graph_run() continuously
753 * until the source is ready or it can decide to
754 * sleep for an arbitrary amount of time.
756 if (graph
->sinks_to_consume
->length
> 1) {
757 status
= BT_FUNC_STATUS_OK
;
760 } while (status
== BT_FUNC_STATUS_OK
);
762 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
763 status
= BT_FUNC_STATUS_END
;
767 BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph
,
768 bt_common_func_status_string(status
));
769 bt_graph_set_can_consume(graph
, true);
773 enum bt_graph_add_listener_status
774 bt_graph_add_source_component_output_port_added_listener(
775 struct bt_graph
*graph
,
776 bt_graph_source_component_output_port_added_listener_func func
,
777 bt_graph_listener_removed_func listener_removed
, void *data
,
778 int *out_listener_id
)
780 struct bt_graph_listener_port_added listener
= {
782 .removed
= listener_removed
,
785 .func
= (port_added_func_t
) func
,
789 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
790 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
791 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
792 BT_ASSERT_PRE(!graph
->in_remove_listener
,
793 "Graph currently executing a \"listener removed\" listener: "
795 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
796 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
797 BT_LIB_LOGD("Added \"source component output port added\" listener to graph: "
798 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
802 *out_listener_id
= listener_id
;
805 return BT_FUNC_STATUS_OK
;
808 enum bt_graph_add_listener_status
809 bt_graph_add_filter_component_output_port_added_listener(
810 struct bt_graph
*graph
,
811 bt_graph_filter_component_output_port_added_listener_func func
,
812 bt_graph_listener_removed_func listener_removed
, void *data
,
813 int *out_listener_id
)
815 struct bt_graph_listener_port_added listener
= {
817 .removed
= listener_removed
,
820 .func
= (port_added_func_t
) func
,
824 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
825 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
826 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
827 BT_ASSERT_PRE(!graph
->in_remove_listener
,
828 "Graph currently executing a \"listener removed\" listener: "
830 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
831 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
832 BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: "
833 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
837 *out_listener_id
= listener_id
;
840 return BT_FUNC_STATUS_OK
;
843 enum bt_graph_add_listener_status
844 bt_graph_add_filter_component_input_port_added_listener(
845 struct bt_graph
*graph
,
846 bt_graph_filter_component_input_port_added_listener_func func
,
847 bt_graph_listener_removed_func listener_removed
, void *data
,
848 int *out_listener_id
)
850 struct bt_graph_listener_port_added listener
= {
852 .removed
= listener_removed
,
855 .func
= (port_added_func_t
) func
,
859 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
860 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
861 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
862 BT_ASSERT_PRE(!graph
->in_remove_listener
,
863 "Graph currently executing a \"listener removed\" listener: "
865 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
866 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
867 BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: "
868 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
872 *out_listener_id
= listener_id
;
875 return BT_FUNC_STATUS_OK
;
878 enum bt_graph_add_listener_status
879 bt_graph_add_sink_component_input_port_added_listener(
880 struct bt_graph
*graph
,
881 bt_graph_sink_component_input_port_added_listener_func func
,
882 bt_graph_listener_removed_func listener_removed
, void *data
,
883 int *out_listener_id
)
885 struct bt_graph_listener_port_added listener
= {
887 .removed
= listener_removed
,
890 .func
= (port_added_func_t
) func
,
894 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
895 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
896 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
897 BT_ASSERT_PRE(!graph
->in_remove_listener
,
898 "Graph currently executing a \"listener removed\" listener: "
900 g_array_append_val(graph
->listeners
.sink_input_port_added
, listener
);
901 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
902 BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: "
903 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
907 *out_listener_id
= listener_id
;
910 return BT_FUNC_STATUS_OK
;
913 enum bt_graph_add_listener_status
914 bt_graph_add_source_filter_component_ports_connected_listener(
915 struct bt_graph
*graph
,
916 bt_graph_source_filter_component_ports_connected_listener_func func
,
917 bt_graph_listener_removed_func listener_removed
, void *data
,
918 int *out_listener_id
)
920 struct bt_graph_listener_ports_connected listener
= {
922 .removed
= listener_removed
,
925 .func
= (ports_connected_func_t
) func
,
929 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
930 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
931 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
932 BT_ASSERT_PRE(!graph
->in_remove_listener
,
933 "Graph currently executing a \"listener removed\" listener: "
935 g_array_append_val(graph
->listeners
.source_filter_ports_connected
,
937 listener_id
= graph
->listeners
.source_filter_ports_connected
->len
- 1;
938 BT_LIB_LOGD("Added \"source to filter component ports connected\" listener to graph: "
939 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
943 *out_listener_id
= listener_id
;
946 return BT_FUNC_STATUS_OK
;
949 enum bt_graph_add_listener_status
950 bt_graph_add_source_sink_component_ports_connected_listener(
951 struct bt_graph
*graph
,
952 bt_graph_source_sink_component_ports_connected_listener_func func
,
953 bt_graph_listener_removed_func listener_removed
, void *data
,
954 int *out_listener_id
)
956 struct bt_graph_listener_ports_connected listener
= {
958 .removed
= listener_removed
,
961 .func
= (ports_connected_func_t
) func
,
965 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
966 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
967 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
968 BT_ASSERT_PRE(!graph
->in_remove_listener
,
969 "Graph currently executing a \"listener removed\" listener: "
971 g_array_append_val(graph
->listeners
.source_sink_ports_connected
,
973 listener_id
= graph
->listeners
.source_sink_ports_connected
->len
- 1;
974 BT_LIB_LOGD("Added \"source to sink component ports connected\" listener to graph: "
975 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
979 *out_listener_id
= listener_id
;
982 return BT_FUNC_STATUS_OK
;
985 enum bt_graph_add_listener_status
986 bt_graph_add_filter_filter_component_ports_connected_listener(
987 struct bt_graph
*graph
,
988 bt_graph_filter_filter_component_ports_connected_listener_func func
,
989 bt_graph_listener_removed_func listener_removed
, void *data
,
990 int *out_listener_id
)
992 struct bt_graph_listener_ports_connected listener
= {
994 .removed
= listener_removed
,
997 .func
= (ports_connected_func_t
) func
,
1001 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1002 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1003 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1004 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1005 "Graph currently executing a \"listener removed\" listener: "
1007 g_array_append_val(graph
->listeners
.filter_filter_ports_connected
,
1009 listener_id
= graph
->listeners
.filter_filter_ports_connected
->len
- 1;
1010 BT_LIB_LOGD("Added \"filter to filter component ports connected\" listener to graph: "
1011 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1015 *out_listener_id
= listener_id
;
1018 return BT_FUNC_STATUS_OK
;
1021 enum bt_graph_add_listener_status
1022 bt_graph_add_filter_sink_component_ports_connected_listener(
1023 struct bt_graph
*graph
,
1024 bt_graph_filter_sink_component_ports_connected_listener_func func
,
1025 bt_graph_listener_removed_func listener_removed
, void *data
,
1026 int *out_listener_id
)
1028 struct bt_graph_listener_ports_connected listener
= {
1030 .removed
= listener_removed
,
1033 .func
= (ports_connected_func_t
) func
,
1037 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1038 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1039 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1040 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1041 "Graph currently executing a \"listener removed\" listener: "
1043 g_array_append_val(graph
->listeners
.filter_sink_ports_connected
,
1045 listener_id
= graph
->listeners
.filter_sink_ports_connected
->len
- 1;
1046 BT_LIB_LOGD("Added \"filter to sink component ports connected\" listener to graph: "
1047 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1051 *out_listener_id
= listener_id
;
1054 return BT_FUNC_STATUS_OK
;
1058 enum bt_graph_listener_func_status
bt_graph_notify_port_added(
1059 struct bt_graph
*graph
, struct bt_port
*port
)
1063 struct bt_component
*comp
;
1064 enum bt_graph_listener_func_status status
= BT_FUNC_STATUS_OK
;
1068 BT_LIB_LOGD("Notifying graph listeners that a port was added: "
1069 "%![graph-]+g, %![port-]+p", graph
, port
);
1070 comp
= bt_port_borrow_component_inline(port
);
1073 switch (comp
->class->type
) {
1074 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1076 switch (port
->type
) {
1077 case BT_PORT_TYPE_OUTPUT
:
1078 listeners
= graph
->listeners
.source_output_port_added
;
1086 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1088 switch (port
->type
) {
1089 case BT_PORT_TYPE_INPUT
:
1090 listeners
= graph
->listeners
.filter_input_port_added
;
1092 case BT_PORT_TYPE_OUTPUT
:
1093 listeners
= graph
->listeners
.filter_output_port_added
;
1101 case BT_COMPONENT_CLASS_TYPE_SINK
:
1103 switch (port
->type
) {
1104 case BT_PORT_TYPE_INPUT
:
1105 listeners
= graph
->listeners
.sink_input_port_added
;
1117 for (i
= 0; i
< listeners
->len
; i
++) {
1118 struct bt_graph_listener_port_added
*listener
=
1119 &g_array_index(listeners
,
1120 struct bt_graph_listener_port_added
, i
);
1123 BT_ASSERT(listener
->func
);
1124 status
= listener
->func(comp
, port
, listener
->base
.data
);
1125 if (status
!= BT_FUNC_STATUS_OK
) {
1135 enum bt_graph_listener_func_status
bt_graph_notify_ports_connected(
1136 struct bt_graph
*graph
, struct bt_port
*upstream_port
,
1137 struct bt_port
*downstream_port
)
1141 struct bt_component
*upstream_comp
;
1142 struct bt_component
*downstream_comp
;
1143 enum bt_graph_listener_func_status status
= BT_FUNC_STATUS_OK
;
1146 BT_ASSERT(upstream_port
);
1147 BT_ASSERT(downstream_port
);
1148 BT_LIB_LOGD("Notifying graph listeners that ports were connected: "
1149 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1150 graph
, upstream_port
, downstream_port
);
1151 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
1152 BT_ASSERT(upstream_comp
);
1153 downstream_comp
= bt_port_borrow_component_inline(downstream_port
);
1154 BT_ASSERT(downstream_comp
);
1156 switch (upstream_comp
->class->type
) {
1157 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1159 switch (downstream_comp
->class->type
) {
1160 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1162 graph
->listeners
.source_filter_ports_connected
;
1164 case BT_COMPONENT_CLASS_TYPE_SINK
:
1166 graph
->listeners
.source_sink_ports_connected
;
1174 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1176 switch (downstream_comp
->class->type
) {
1177 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1179 graph
->listeners
.filter_filter_ports_connected
;
1181 case BT_COMPONENT_CLASS_TYPE_SINK
:
1183 graph
->listeners
.filter_sink_ports_connected
;
1195 for (i
= 0; i
< listeners
->len
; i
++) {
1196 struct bt_graph_listener_ports_connected
*listener
=
1197 &g_array_index(listeners
,
1198 struct bt_graph_listener_ports_connected
, i
);
1200 BT_ASSERT(listener
->func
);
1201 status
= listener
->func(upstream_comp
, downstream_comp
,
1202 upstream_port
, downstream_port
, listener
->base
.data
);
1203 if (status
!= BT_FUNC_STATUS_OK
) {
1212 enum bt_graph_cancel_status
bt_graph_cancel(struct bt_graph
*graph
)
1214 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1215 graph
->canceled
= true;
1216 BT_LIB_LOGI("Canceled graph: %!+i", graph
);
1217 return BT_FUNC_STATUS_OK
;
1220 bt_bool
bt_graph_is_canceled(const struct bt_graph
*graph
)
1222 BT_ASSERT_PRE_DEV_NON_NULL(graph
, "Graph");
1223 return graph
->canceled
? BT_TRUE
: BT_FALSE
;
1227 void bt_graph_remove_connection(struct bt_graph
*graph
,
1228 struct bt_connection
*connection
)
1231 BT_ASSERT(connection
);
1232 BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1234 g_ptr_array_remove(graph
->connections
, connection
);
1239 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
1241 bool exists
= false;
1244 for (i
= 0; i
< graph
->components
->len
; i
++) {
1245 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
1247 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1248 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1249 "%![other-comp-]+c, name=\"%s\"",
1261 int add_component_with_init_method_data(
1262 struct bt_graph
*graph
,
1263 struct bt_component_class
*comp_cls
,
1264 comp_init_method_t init_method
,
1265 const char *name
, const struct bt_value
*params
,
1266 void *init_method_data
, bt_logging_level log_level
,
1267 struct bt_component
**user_component
)
1269 int status
= BT_FUNC_STATUS_OK
;
1270 enum bt_component_class_init_method_status init_status
;
1271 struct bt_component
*component
= NULL
;
1273 bool init_can_consume
;
1274 struct bt_value
*new_params
= NULL
;
1276 BT_ASSERT(comp_cls
);
1277 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1278 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1279 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
1281 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1282 "Graph is not in the \"configuring\" state: %!+g", graph
);
1283 BT_ASSERT_PRE(!component_name_exists(graph
, name
),
1284 "Duplicate component name: %!+g, name=\"%s\"", graph
, name
);
1285 BT_ASSERT_PRE(!params
|| bt_value_is_map(params
),
1286 "Parameter value is not a map value: %!+v", params
);
1287 init_can_consume
= graph
->can_consume
;
1288 bt_graph_set_can_consume(graph
, false);
1289 BT_LIB_LOGI("Adding component to graph: "
1290 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1291 "%![params-]+v, init-method-data-addr=%p",
1292 graph
, comp_cls
, name
,
1293 bt_common_logging_level_string(log_level
), params
,
1297 new_params
= bt_value_map_create();
1299 BT_LIB_LOGE_APPEND_CAUSE(
1300 "Cannot create empty map value object.");
1301 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1305 params
= new_params
;
1308 ret
= bt_component_create(comp_cls
, name
, log_level
, &component
);
1310 BT_LIB_LOGE_APPEND_CAUSE(
1311 "Cannot create empty component object: ret=%d",
1313 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1318 * The user's initialization method needs to see that this
1319 * component is part of the graph. If the user method fails, we
1320 * immediately remove the component from the graph's components.
1322 g_ptr_array_add(graph
->components
, component
);
1323 bt_component_set_graph(component
, graph
);
1324 bt_value_freeze(params
);
1327 BT_LOGD_STR("Calling user's initialization method.");
1328 init_status
= init_method(component
, params
, init_method_data
);
1329 BT_LOGD("User method returned: status=%s",
1330 bt_common_func_status_string(init_status
));
1331 if (init_status
!= BT_FUNC_STATUS_OK
) {
1332 if (init_status
< 0) {
1333 BT_LIB_LOGW_APPEND_CAUSE(
1334 "Component initialization method failed: "
1335 "status=%s, %![comp-]+c",
1336 bt_common_func_status_string(init_status
),
1340 status
= init_status
;
1341 bt_component_set_graph(component
, NULL
);
1342 g_ptr_array_remove_fast(graph
->components
, component
);
1348 * Mark the component as initialized so that its finalization
1349 * method is called when it is destroyed.
1351 component
->initialized
= true;
1354 * If it's a sink component, it needs to be part of the graph's
1355 * sink queue to be consumed by bt_graph_consume().
1357 if (bt_component_is_sink(component
)) {
1358 graph
->has_sink
= true;
1359 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1363 * Freeze the component class now that it's instantiated at
1366 BT_LOGD_STR("Freezing component class.");
1367 bt_component_class_freeze(comp_cls
);
1368 BT_LIB_LOGI("Added component to graph: "
1369 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1370 "%![params-]+v, init-method-data-addr=%p, %![comp-]+c",
1371 graph
, comp_cls
, name
,
1372 bt_common_logging_level_string(log_level
), params
,
1373 init_method_data
, component
);
1375 if (user_component
) {
1376 /* Move reference to user */
1377 *user_component
= component
;
1382 if (status
!= BT_FUNC_STATUS_OK
) {
1383 bt_graph_make_faulty(graph
);
1386 bt_object_put_ref(component
);
1387 bt_object_put_ref(new_params
);
1388 (void) init_can_consume
;
1389 bt_graph_set_can_consume(graph
, init_can_consume
);
1393 enum bt_graph_add_component_status
1394 bt_graph_add_source_component_with_init_method_data(
1395 struct bt_graph
*graph
,
1396 const struct bt_component_class_source
*comp_cls
,
1397 const char *name
, const struct bt_value
*params
,
1398 void *init_method_data
, bt_logging_level log_level
,
1399 const struct bt_component_source
**component
)
1401 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1402 return add_component_with_init_method_data(graph
,
1403 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1404 name
, params
, init_method_data
, log_level
, (void *) component
);
1407 enum bt_graph_add_component_status
bt_graph_add_source_component(
1408 struct bt_graph
*graph
,
1409 const struct bt_component_class_source
*comp_cls
,
1410 const char *name
, const struct bt_value
*params
,
1411 bt_logging_level log_level
,
1412 const struct bt_component_source
**component
)
1414 return bt_graph_add_source_component_with_init_method_data(
1415 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1418 enum bt_graph_add_component_status
1419 bt_graph_add_filter_component_with_init_method_data(
1420 struct bt_graph
*graph
,
1421 const struct bt_component_class_filter
*comp_cls
,
1422 const char *name
, const struct bt_value
*params
,
1423 void *init_method_data
, bt_logging_level log_level
,
1424 const struct bt_component_filter
**component
)
1426 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1427 return add_component_with_init_method_data(graph
,
1428 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1429 name
, params
, init_method_data
, log_level
, (void *) component
);
1432 enum bt_graph_add_component_status
bt_graph_add_filter_component(
1433 struct bt_graph
*graph
,
1434 const struct bt_component_class_filter
*comp_cls
,
1435 const char *name
, const struct bt_value
*params
,
1436 bt_logging_level log_level
,
1437 const struct bt_component_filter
**component
)
1439 return bt_graph_add_filter_component_with_init_method_data(
1440 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1443 enum bt_graph_add_component_status
1444 bt_graph_add_sink_component_with_init_method_data(
1445 struct bt_graph
*graph
,
1446 const struct bt_component_class_sink
*comp_cls
,
1447 const char *name
, const struct bt_value
*params
,
1448 void *init_method_data
, bt_logging_level log_level
,
1449 const struct bt_component_sink
**component
)
1451 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1452 return add_component_with_init_method_data(graph
,
1453 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1454 name
, params
, init_method_data
, log_level
, (void *) component
);
1457 enum bt_graph_add_component_status
bt_graph_add_sink_component(
1458 struct bt_graph
*graph
,
1459 const struct bt_component_class_sink
*comp_cls
,
1460 const char *name
, const struct bt_value
*params
,
1461 bt_logging_level log_level
,
1462 const struct bt_component_sink
**component
)
1464 return bt_graph_add_sink_component_with_init_method_data(
1465 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1469 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1470 struct bt_component
*component
)
1472 bool init_can_consume
;
1478 BT_ASSERT(component
);
1479 BT_ASSERT(component
->base
.ref_count
== 0);
1480 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1482 init_can_consume
= graph
->can_consume
;
1483 count
= bt_component_get_input_port_count(component
);
1485 for (i
= 0; i
< count
; i
++) {
1486 struct bt_port
*port
= (void *)
1487 bt_component_borrow_input_port_by_index(component
, i
);
1491 if (bt_port_is_connected(port
)) {
1492 BT_LIB_LOGW_APPEND_CAUSE(
1493 "Cannot remove component from graph: "
1494 "an input port is connected: "
1495 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1496 graph
, component
, port
);
1501 count
= bt_component_get_output_port_count(component
);
1503 for (i
= 0; i
< count
; i
++) {
1504 struct bt_port
*port
= (void *)
1505 bt_component_borrow_output_port_by_index(component
, i
);
1509 if (bt_port_is_connected(port
)) {
1510 BT_LIB_LOGW_APPEND_CAUSE(
1511 "Cannot remove component from graph: "
1512 "an output port is connected: "
1513 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1514 graph
, component
, port
);
1519 bt_graph_set_can_consume(graph
, false);
1521 /* Possibly remove from sinks to consume */
1522 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1524 if (graph
->sinks_to_consume
->length
== 0) {
1525 graph
->has_sink
= false;
1529 * This calls bt_object_try_spec_release() on the component, and
1530 * since its reference count is 0, its destructor is called. Its
1531 * destructor calls the user's finalization method (if set).
1533 g_ptr_array_remove(graph
->components
, component
);
1540 (void) init_can_consume
;
1541 bt_graph_set_can_consume(graph
, init_can_consume
);
1546 void bt_graph_add_message(struct bt_graph
*graph
,
1547 struct bt_message
*msg
)
1553 * It's okay not to take a reference because, when a
1554 * message's reference count drops to 0, either:
1556 * * It is recycled back to one of this graph's pool.
1557 * * It is destroyed because it doesn't have any link to any
1558 * graph, which means the original graph is already destroyed.
1560 g_ptr_array_add(graph
->messages
, msg
);
1563 void bt_graph_get_ref(const struct bt_graph
*graph
)
1565 bt_object_get_ref(graph
);
1568 void bt_graph_put_ref(const struct bt_graph
*graph
)
1570 bt_object_put_ref(graph
);