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-class-sink-simple.h"
46 #include "component.h"
47 #include "component-sink.h"
48 #include "connection.h"
50 #include "interrupter.h"
51 #include "message/event.h"
52 #include "message/packet.h"
54 typedef enum bt_graph_listener_func_status
55 (*port_added_func_t
)(const void *, const void *, void *);
57 typedef enum bt_graph_listener_func_status
58 (*ports_connected_func_t
)(const void *, const void *, const void *,
59 const void *, void *);
61 typedef enum bt_component_class_init_method_status
62 (*comp_init_method_t
)(const void *, const void *, void *);
64 struct bt_graph_listener
{
65 bt_graph_listener_removed_func removed
;
69 struct bt_graph_listener_port_added
{
70 struct bt_graph_listener base
;
71 port_added_func_t func
;
74 struct bt_graph_listener_ports_connected
{
75 struct bt_graph_listener base
;
76 ports_connected_func_t func
;
79 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
81 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
82 if (!(_listeners)) { \
83 BT_LIB_LOGE_APPEND_CAUSE( \
84 "Failed to allocate one GArray."); \
88 #define CALL_REMOVE_LISTENERS(_type, _listeners) \
95 for (i = 0; i < (_listeners)->len; i++) { \
97 &g_array_index((_listeners), _type, i); \
99 if (listener->base.removed) { \
100 listener->base.removed(listener->base.data); \
106 void destroy_graph(struct bt_object
*obj
)
108 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
111 * The graph's reference count is 0 if we're here. Increment
112 * it to avoid a double-destroy (possibly infinitely recursive)
115 * 1. We put and destroy a connection.
116 * 2. This connection's destructor finalizes its active message
118 * 3. A message iterator's finalization function gets a new
119 * reference on its component (reference count goes from 0 to
121 * 4. Since this component's reference count goes to 1, it takes
122 * a reference on its parent (this graph). This graph's
123 * reference count goes from 0 to 1.
124 * 5. The message iterator's finalization function puts its
125 * component reference (reference count goes from 1 to 0).
126 * 6. Since this component's reference count goes from 1 to 0,
127 * it puts its parent (this graph). This graph's reference
128 * count goes from 1 to 0.
129 * 7. Since this graph's reference count goes from 1 to 0, its
130 * destructor is called (this function).
132 * With the incrementation below, the graph's reference count at
133 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
134 * ensures that this function is not called two times.
136 BT_LIB_LOGI("Destroying graph: %!+g", graph
);
138 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_DESTROYING
;
140 /* Call all remove listeners */
141 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
142 graph
->listeners
.source_output_port_added
);
143 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
144 graph
->listeners
.filter_output_port_added
);
145 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
146 graph
->listeners
.filter_input_port_added
);
147 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
148 graph
->listeners
.sink_input_port_added
);
149 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
150 graph
->listeners
.source_filter_ports_connected
);
151 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
152 graph
->listeners
.filter_filter_ports_connected
);
153 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
154 graph
->listeners
.source_sink_ports_connected
);
155 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
156 graph
->listeners
.filter_sink_ports_connected
);
158 if (graph
->messages
) {
159 g_ptr_array_free(graph
->messages
, TRUE
);
160 graph
->messages
= NULL
;
163 if (graph
->connections
) {
164 BT_LOGD_STR("Destroying connections.");
165 g_ptr_array_free(graph
->connections
, TRUE
);
166 graph
->connections
= NULL
;
169 if (graph
->components
) {
170 BT_LOGD_STR("Destroying components.");
171 g_ptr_array_free(graph
->components
, TRUE
);
172 graph
->components
= NULL
;
175 if (graph
->interrupters
) {
176 BT_LOGD_STR("Putting interrupters.");
177 g_ptr_array_free(graph
->interrupters
, TRUE
);
178 graph
->interrupters
= NULL
;
181 BT_OBJECT_PUT_REF_AND_RESET(graph
->default_interrupter
);
183 if (graph
->sinks_to_consume
) {
184 g_queue_free(graph
->sinks_to_consume
);
185 graph
->sinks_to_consume
= NULL
;
188 if (graph
->listeners
.source_output_port_added
) {
189 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
190 graph
->listeners
.source_output_port_added
= NULL
;
193 if (graph
->listeners
.filter_output_port_added
) {
194 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
195 graph
->listeners
.filter_output_port_added
= NULL
;
198 if (graph
->listeners
.filter_input_port_added
) {
199 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
200 graph
->listeners
.filter_input_port_added
= NULL
;
203 if (graph
->listeners
.sink_input_port_added
) {
204 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
205 graph
->listeners
.sink_input_port_added
= NULL
;
208 if (graph
->listeners
.source_filter_ports_connected
) {
209 g_array_free(graph
->listeners
.source_filter_ports_connected
,
211 graph
->listeners
.source_filter_ports_connected
= NULL
;
214 if (graph
->listeners
.filter_filter_ports_connected
) {
215 g_array_free(graph
->listeners
.filter_filter_ports_connected
,
217 graph
->listeners
.filter_filter_ports_connected
= NULL
;
220 if (graph
->listeners
.source_sink_ports_connected
) {
221 g_array_free(graph
->listeners
.source_sink_ports_connected
,
223 graph
->listeners
.source_sink_ports_connected
= NULL
;
226 if (graph
->listeners
.filter_sink_ports_connected
) {
227 g_array_free(graph
->listeners
.filter_sink_ports_connected
,
229 graph
->listeners
.filter_sink_ports_connected
= NULL
;
232 bt_object_pool_finalize(&graph
->event_msg_pool
);
233 bt_object_pool_finalize(&graph
->packet_begin_msg_pool
);
234 bt_object_pool_finalize(&graph
->packet_end_msg_pool
);
239 void destroy_message_event(struct bt_message
*msg
,
240 struct bt_graph
*graph
)
242 bt_message_event_destroy(msg
);
246 void destroy_message_packet_begin(struct bt_message
*msg
,
247 struct bt_graph
*graph
)
249 bt_message_packet_destroy(msg
);
253 void destroy_message_packet_end(struct bt_message
*msg
,
254 struct bt_graph
*graph
)
256 bt_message_packet_destroy(msg
);
260 void notify_message_graph_is_destroyed(struct bt_message
*msg
)
262 bt_message_unlink_graph(msg
);
265 struct bt_graph
*bt_graph_create(void)
267 struct bt_graph
*graph
;
270 BT_LOGI_STR("Creating graph object.");
271 graph
= g_new0(struct bt_graph
, 1);
273 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph.");
277 bt_object_init_shared(&graph
->base
, destroy_graph
);
278 graph
->connections
= g_ptr_array_new_with_free_func(
279 (GDestroyNotify
) bt_object_try_spec_release
);
280 if (!graph
->connections
) {
281 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
284 graph
->components
= g_ptr_array_new_with_free_func(
285 (GDestroyNotify
) bt_object_try_spec_release
);
286 if (!graph
->components
) {
287 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
290 graph
->sinks_to_consume
= g_queue_new();
291 if (!graph
->sinks_to_consume
) {
292 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue.");
296 bt_graph_set_can_consume(graph
, true);
297 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
298 graph
->listeners
.source_output_port_added
);
300 if (!graph
->listeners
.source_output_port_added
) {
305 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
306 graph
->listeners
.filter_output_port_added
);
308 if (!graph
->listeners
.filter_output_port_added
) {
313 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
314 graph
->listeners
.filter_input_port_added
);
316 if (!graph
->listeners
.filter_input_port_added
) {
321 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
322 graph
->listeners
.sink_input_port_added
);
324 if (!graph
->listeners
.sink_input_port_added
) {
329 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
330 graph
->listeners
.source_filter_ports_connected
);
332 if (!graph
->listeners
.source_filter_ports_connected
) {
337 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
338 graph
->listeners
.source_sink_ports_connected
);
340 if (!graph
->listeners
.source_sink_ports_connected
) {
345 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
346 graph
->listeners
.filter_filter_ports_connected
);
348 if (!graph
->listeners
.filter_filter_ports_connected
) {
353 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
354 graph
->listeners
.filter_sink_ports_connected
);
356 if (!graph
->listeners
.filter_sink_ports_connected
) {
361 graph
->interrupters
= g_ptr_array_new_with_free_func(
362 (GDestroyNotify
) bt_object_put_no_null_check
);
363 if (!graph
->interrupters
) {
364 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
368 graph
->default_interrupter
= bt_interrupter_create();
369 if (!graph
->default_interrupter
) {
370 BT_LIB_LOGE_APPEND_CAUSE(
371 "Failed to create one interrupter object.");
375 bt_graph_add_interrupter(graph
, graph
->default_interrupter
);
376 ret
= bt_object_pool_initialize(&graph
->event_msg_pool
,
377 (bt_object_pool_new_object_func
) bt_message_event_new
,
378 (bt_object_pool_destroy_object_func
) destroy_message_event
,
381 BT_LIB_LOGE_APPEND_CAUSE(
382 "Failed to initialize event message pool: ret=%d",
387 ret
= bt_object_pool_initialize(&graph
->packet_begin_msg_pool
,
388 (bt_object_pool_new_object_func
) bt_message_packet_beginning_new
,
389 (bt_object_pool_destroy_object_func
) destroy_message_packet_begin
,
392 BT_LIB_LOGE_APPEND_CAUSE(
393 "Failed to initialize packet beginning message pool: ret=%d",
398 ret
= bt_object_pool_initialize(&graph
->packet_end_msg_pool
,
399 (bt_object_pool_new_object_func
) bt_message_packet_end_new
,
400 (bt_object_pool_destroy_object_func
) destroy_message_packet_end
,
403 BT_LIB_LOGE_APPEND_CAUSE(
404 "Failed to initialize packet end message pool: ret=%d",
409 graph
->messages
= g_ptr_array_new_with_free_func(
410 (GDestroyNotify
) notify_message_graph_is_destroyed
);
411 BT_LIB_LOGI("Created graph object: %!+g", graph
);
414 return (void *) graph
;
417 BT_OBJECT_PUT_REF_AND_RESET(graph
);
421 enum bt_graph_connect_ports_status
bt_graph_connect_ports(
422 struct bt_graph
*graph
,
423 const struct bt_port_output
*upstream_port_out
,
424 const struct bt_port_input
*downstream_port_in
,
425 const struct bt_connection
**user_connection
)
427 enum bt_graph_connect_ports_status status
= BT_FUNC_STATUS_OK
;
428 enum bt_graph_listener_func_status listener_status
;
429 struct bt_connection
*connection
= NULL
;
430 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
431 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
432 struct bt_component
*upstream_component
= NULL
;
433 struct bt_component
*downstream_component
= NULL
;
434 enum bt_component_class_port_connected_method_status port_connected_status
;
435 bool init_can_consume
;
437 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
438 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
439 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
441 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
442 "Graph is not in the \"configuring\" state: %!+g", graph
);
443 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
444 "Upstream port is already connected: %!+p", upstream_port
);
445 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
446 "Downstream port is already connected: %!+p", downstream_port
);
447 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
448 "Upstream port does not belong to a component: %!+p",
450 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
451 "Downstream port does not belong to a component: %!+p",
453 init_can_consume
= graph
->can_consume
;
454 BT_LIB_LOGI("Connecting component ports within graph: "
455 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
456 graph
, upstream_port
, downstream_port
);
457 bt_graph_set_can_consume(graph
, false);
458 upstream_component
= bt_port_borrow_component_inline(
459 (void *) upstream_port
);
460 downstream_component
= bt_port_borrow_component_inline(
461 (void *) downstream_port
);
463 BT_LOGD_STR("Creating connection.");
464 connection
= bt_connection_create(graph
, (void *) upstream_port
,
465 (void *) downstream_port
);
467 BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object.");
468 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
472 BT_LIB_LOGD("Connection object created: %!+x", connection
);
475 * Ownership of upstream_component/downstream_component and of
476 * the connection object is transferred to the graph.
478 g_ptr_array_add(graph
->connections
, connection
);
481 * Notify both components that their port is connected.
483 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
484 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
485 port_connected_status
= bt_component_port_connected(upstream_component
,
486 (void *) upstream_port
, (void *) downstream_port
);
487 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
488 if (port_connected_status
< 0) {
489 BT_LIB_LOGW_APPEND_CAUSE(
490 "Upstream component's \"port connected\" method failed: "
491 "status=%s, %![graph-]+g, %![up-comp-]+c, "
492 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
493 bt_common_func_status_string(
494 port_connected_status
),
495 graph
, upstream_component
, downstream_component
,
496 upstream_port
, downstream_port
);
499 bt_connection_end(connection
, true);
500 status
= (int) port_connected_status
;
504 connection
->notified_upstream_port_connected
= true;
505 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
506 "%![comp-]+c, %![port-]+p", downstream_component
,
508 port_connected_status
= bt_component_port_connected(downstream_component
,
509 (void *) downstream_port
, (void *) upstream_port
);
510 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
511 if (port_connected_status
< 0) {
512 BT_LIB_LOGW_APPEND_CAUSE(
513 "Downstream component's \"port connected\" method failed: "
514 "status=%s, %![graph-]+g, %![up-comp-]+c, "
515 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
516 bt_common_func_status_string(
517 port_connected_status
),
518 graph
, upstream_component
, downstream_component
,
519 upstream_port
, downstream_port
);
522 bt_connection_end(connection
, true);
523 status
= (int) port_connected_status
;
527 connection
->notified_downstream_port_connected
= true;
530 * Notify the graph's creator that both ports are connected.
532 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
533 listener_status
= bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
534 if (listener_status
!= BT_FUNC_STATUS_OK
) {
535 if (listener_status
< 0) {
536 BT_LIB_LOGW_APPEND_CAUSE(
537 "Graph \"ports connected\" listener failed: "
538 "status=%d, %![graph-]+g, %![up-comp-]+c, "
539 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
540 listener_status
, graph
,
541 upstream_component
, downstream_component
,
542 upstream_port
, downstream_port
);
545 status
= (int) listener_status
;
549 connection
->notified_graph_ports_connected
= true;
550 BT_LIB_LOGI("Connected component ports within graph: "
551 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
552 "%![up-port-]+p, %![down-port-]+p",
553 graph
, upstream_component
, downstream_component
,
554 upstream_port
, downstream_port
);
556 if (user_connection
) {
557 /* Move reference to user */
558 *user_connection
= connection
;
563 if (status
!= BT_FUNC_STATUS_OK
) {
564 bt_graph_make_faulty(graph
);
567 bt_object_put_ref(connection
);
568 (void) init_can_consume
;
569 bt_graph_set_can_consume(graph
, init_can_consume
);
574 int consume_graph_sink(struct bt_component_sink
*comp
)
576 enum bt_component_class_sink_consume_method_status consume_status
;
577 struct bt_component_class_sink
*sink_class
= NULL
;
580 sink_class
= (void *) comp
->parent
.class;
581 BT_ASSERT(sink_class
->methods
.consume
);
582 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
583 consume_status
= sink_class
->methods
.consume((void *) comp
);
584 BT_LOGD("User method returned: status=%s",
585 bt_common_func_status_string(consume_status
));
586 BT_ASSERT_POST_DEV(consume_status
== BT_FUNC_STATUS_OK
||
587 consume_status
== BT_FUNC_STATUS_END
||
588 consume_status
== BT_FUNC_STATUS_AGAIN
||
589 consume_status
== BT_FUNC_STATUS_ERROR
||
590 consume_status
== BT_FUNC_STATUS_MEMORY_ERROR
,
591 "Invalid component status returned by consuming method: "
592 "status=%s", bt_common_func_status_string(consume_status
));
593 if (consume_status
) {
594 if (consume_status
< 0) {
595 BT_LIB_LOGW_APPEND_CAUSE(
596 "Component's \"consume\" method failed: "
597 "status=%s, %![comp-]+c",
598 bt_common_func_status_string(consume_status
),
605 BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s",
606 comp
, bt_common_func_status_string(consume_status
));
609 return consume_status
;
613 * `node` is removed from the queue of sinks to consume when passed to
614 * this function. This function adds it back to the queue if there's
615 * still something to consume afterwards.
618 int consume_sink_node(struct bt_graph
*graph
, GList
*node
)
621 struct bt_component_sink
*sink
;
624 status
= consume_graph_sink(sink
);
625 if (G_UNLIKELY(status
!= BT_FUNC_STATUS_END
)) {
626 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
630 /* End reached, the node is not added back to the queue and free'd. */
631 g_queue_delete_link(graph
->sinks_to_consume
, node
);
633 /* Don't forward an END status if there are sinks left to consume. */
634 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
635 status
= BT_FUNC_STATUS_OK
;
640 BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s",
641 sink
, bt_common_func_status_string(status
));
646 int bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
647 struct bt_component_sink
*sink
)
653 BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink
);
654 BT_ASSERT(bt_component_borrow_graph((void *) sink
) == graph
);
656 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
657 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
658 status
= BT_FUNC_STATUS_END
;
662 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
664 BT_LIB_LOGD("Sink component is not marked as consumable: "
665 "component sink is ended: %![comp-]+c", sink
);
666 status
= BT_FUNC_STATUS_END
;
670 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
671 BT_ASSERT(sink_node
);
672 status
= consume_sink_node(graph
, sink_node
);
679 int consume_no_check(struct bt_graph
*graph
)
681 int status
= BT_FUNC_STATUS_OK
;
682 struct bt_component
*sink
;
685 BT_ASSERT_PRE_DEV(graph
->has_sink
,
686 "Graph has no sink component: %!+g", graph
);
687 BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph
);
689 if (G_UNLIKELY(g_queue_is_empty(graph
->sinks_to_consume
))) {
690 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
691 status
= BT_FUNC_STATUS_END
;
695 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
696 sink
= current_node
->data
;
697 BT_LIB_LOGD("Chose next sink to consume: %!+c", sink
);
698 status
= consume_sink_node(graph
, current_node
);
704 enum bt_graph_run_once_status
bt_graph_run_once(struct bt_graph
*graph
)
706 enum bt_graph_run_once_status status
;
708 BT_ASSERT_PRE_DEV_NON_NULL(graph
, "Graph");
709 BT_ASSERT_PRE_DEV(graph
->can_consume
,
710 "Cannot consume graph in its current state: %!+g", graph
);
711 BT_ASSERT_PRE_DEV(graph
->config_state
!=
712 BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
713 "Graph is in a faulty state: %!+g", graph
);
714 bt_graph_set_can_consume(graph
, false);
715 status
= bt_graph_configure(graph
);
716 if (G_UNLIKELY(status
)) {
717 /* bt_graph_configure() logs errors */
721 status
= consume_no_check(graph
);
722 bt_graph_set_can_consume(graph
, true);
728 enum bt_graph_run_status
bt_graph_run(struct bt_graph
*graph
)
730 enum bt_graph_run_status status
;
732 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
733 BT_ASSERT_PRE(graph
->can_consume
,
734 "Cannot consume graph in its current state: %!+g", graph
);
735 BT_ASSERT_PRE(graph
->config_state
!= BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
736 "Graph is in a faulty state: %!+g", graph
);
737 bt_graph_set_can_consume(graph
, false);
738 status
= bt_graph_configure(graph
);
739 if (G_UNLIKELY(status
)) {
740 /* bt_graph_configure() logs errors */
744 BT_LIB_LOGI("Running graph: %!+g", graph
);
748 * Check if the graph is interrupted at each iteration.
749 * If the graph was interrupted by another thread or by
750 * a signal handler, this is NOT a warning nor an error;
751 * it was intentional: log with an INFO level only.
753 if (G_UNLIKELY(bt_graph_is_interrupted(graph
))) {
754 BT_LIB_LOGI("Stopping the graph: "
755 "graph was interrupted: %!+g", graph
);
756 status
= BT_FUNC_STATUS_AGAIN
;
760 status
= consume_no_check(graph
);
761 if (G_UNLIKELY(status
== BT_FUNC_STATUS_AGAIN
)) {
763 * If AGAIN is received and there are multiple
764 * sinks, go ahead and consume from the next
767 * However, in the case where a single sink is
768 * left, the caller can decide to busy-wait and
769 * call bt_graph_run() continuously
770 * until the source is ready or it can decide to
771 * sleep for an arbitrary amount of time.
773 if (graph
->sinks_to_consume
->length
> 1) {
774 status
= BT_FUNC_STATUS_OK
;
777 } while (status
== BT_FUNC_STATUS_OK
);
779 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
780 status
= BT_FUNC_STATUS_END
;
784 BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph
,
785 bt_common_func_status_string(status
));
786 bt_graph_set_can_consume(graph
, true);
790 enum bt_graph_add_listener_status
791 bt_graph_add_source_component_output_port_added_listener(
792 struct bt_graph
*graph
,
793 bt_graph_source_component_output_port_added_listener_func func
,
794 bt_graph_listener_removed_func listener_removed
, void *data
,
795 bt_listener_id
*out_listener_id
)
797 struct bt_graph_listener_port_added listener
= {
799 .removed
= listener_removed
,
802 .func
= (port_added_func_t
) func
,
804 bt_listener_id listener_id
;
806 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
807 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
808 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
809 BT_ASSERT_PRE(!graph
->in_remove_listener
,
810 "Graph currently executing a \"listener removed\" listener: "
812 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
813 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
814 BT_LIB_LOGD("Added \"source component output port added\" listener to graph: "
815 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
819 *out_listener_id
= listener_id
;
822 return BT_FUNC_STATUS_OK
;
825 enum bt_graph_add_listener_status
826 bt_graph_add_filter_component_output_port_added_listener(
827 struct bt_graph
*graph
,
828 bt_graph_filter_component_output_port_added_listener_func func
,
829 bt_graph_listener_removed_func listener_removed
, void *data
,
830 bt_listener_id
*out_listener_id
)
832 struct bt_graph_listener_port_added listener
= {
834 .removed
= listener_removed
,
837 .func
= (port_added_func_t
) func
,
839 bt_listener_id listener_id
;
841 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
842 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
843 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
844 BT_ASSERT_PRE(!graph
->in_remove_listener
,
845 "Graph currently executing a \"listener removed\" listener: "
847 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
848 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
849 BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: "
850 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
854 *out_listener_id
= listener_id
;
857 return BT_FUNC_STATUS_OK
;
860 enum bt_graph_add_listener_status
861 bt_graph_add_filter_component_input_port_added_listener(
862 struct bt_graph
*graph
,
863 bt_graph_filter_component_input_port_added_listener_func func
,
864 bt_graph_listener_removed_func listener_removed
, void *data
,
865 bt_listener_id
*out_listener_id
)
867 struct bt_graph_listener_port_added listener
= {
869 .removed
= listener_removed
,
872 .func
= (port_added_func_t
) func
,
874 bt_listener_id listener_id
;
876 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
877 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
878 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
879 BT_ASSERT_PRE(!graph
->in_remove_listener
,
880 "Graph currently executing a \"listener removed\" listener: "
882 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
883 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
884 BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: "
885 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
889 *out_listener_id
= listener_id
;
892 return BT_FUNC_STATUS_OK
;
895 enum bt_graph_add_listener_status
896 bt_graph_add_sink_component_input_port_added_listener(
897 struct bt_graph
*graph
,
898 bt_graph_sink_component_input_port_added_listener_func func
,
899 bt_graph_listener_removed_func listener_removed
, void *data
,
900 bt_listener_id
*out_listener_id
)
902 struct bt_graph_listener_port_added listener
= {
904 .removed
= listener_removed
,
907 .func
= (port_added_func_t
) func
,
909 bt_listener_id listener_id
;
911 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
912 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
913 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
914 BT_ASSERT_PRE(!graph
->in_remove_listener
,
915 "Graph currently executing a \"listener removed\" listener: "
917 g_array_append_val(graph
->listeners
.sink_input_port_added
, listener
);
918 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
919 BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: "
920 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
924 *out_listener_id
= listener_id
;
927 return BT_FUNC_STATUS_OK
;
930 enum bt_graph_add_listener_status
931 bt_graph_add_source_filter_component_ports_connected_listener(
932 struct bt_graph
*graph
,
933 bt_graph_source_filter_component_ports_connected_listener_func func
,
934 bt_graph_listener_removed_func listener_removed
, void *data
,
935 bt_listener_id
*out_listener_id
)
937 struct bt_graph_listener_ports_connected listener
= {
939 .removed
= listener_removed
,
942 .func
= (ports_connected_func_t
) func
,
944 bt_listener_id listener_id
;
946 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
947 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
948 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
949 BT_ASSERT_PRE(!graph
->in_remove_listener
,
950 "Graph currently executing a \"listener removed\" listener: "
952 g_array_append_val(graph
->listeners
.source_filter_ports_connected
,
954 listener_id
= graph
->listeners
.source_filter_ports_connected
->len
- 1;
955 BT_LIB_LOGD("Added \"source to filter component ports connected\" listener to graph: "
956 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
960 *out_listener_id
= listener_id
;
963 return BT_FUNC_STATUS_OK
;
966 enum bt_graph_add_listener_status
967 bt_graph_add_source_sink_component_ports_connected_listener(
968 struct bt_graph
*graph
,
969 bt_graph_source_sink_component_ports_connected_listener_func func
,
970 bt_graph_listener_removed_func listener_removed
, void *data
,
971 bt_listener_id
*out_listener_id
)
973 struct bt_graph_listener_ports_connected listener
= {
975 .removed
= listener_removed
,
978 .func
= (ports_connected_func_t
) func
,
980 bt_listener_id listener_id
;
982 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
983 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
984 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
985 BT_ASSERT_PRE(!graph
->in_remove_listener
,
986 "Graph currently executing a \"listener removed\" listener: "
988 g_array_append_val(graph
->listeners
.source_sink_ports_connected
,
990 listener_id
= graph
->listeners
.source_sink_ports_connected
->len
- 1;
991 BT_LIB_LOGD("Added \"source to sink component ports connected\" listener to graph: "
992 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
996 *out_listener_id
= listener_id
;
999 return BT_FUNC_STATUS_OK
;
1002 enum bt_graph_add_listener_status
1003 bt_graph_add_filter_filter_component_ports_connected_listener(
1004 struct bt_graph
*graph
,
1005 bt_graph_filter_filter_component_ports_connected_listener_func func
,
1006 bt_graph_listener_removed_func listener_removed
, void *data
,
1007 bt_listener_id
*out_listener_id
)
1009 struct bt_graph_listener_ports_connected listener
= {
1011 .removed
= listener_removed
,
1014 .func
= (ports_connected_func_t
) func
,
1016 bt_listener_id listener_id
;
1018 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1019 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1020 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1021 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1022 "Graph currently executing a \"listener removed\" listener: "
1024 g_array_append_val(graph
->listeners
.filter_filter_ports_connected
,
1026 listener_id
= graph
->listeners
.filter_filter_ports_connected
->len
- 1;
1027 BT_LIB_LOGD("Added \"filter to filter component ports connected\" listener to graph: "
1028 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1032 *out_listener_id
= listener_id
;
1035 return BT_FUNC_STATUS_OK
;
1038 enum bt_graph_add_listener_status
1039 bt_graph_add_filter_sink_component_ports_connected_listener(
1040 struct bt_graph
*graph
,
1041 bt_graph_filter_sink_component_ports_connected_listener_func func
,
1042 bt_graph_listener_removed_func listener_removed
, void *data
,
1043 bt_listener_id
*out_listener_id
)
1045 struct bt_graph_listener_ports_connected listener
= {
1047 .removed
= listener_removed
,
1050 .func
= (ports_connected_func_t
) func
,
1052 bt_listener_id listener_id
;
1054 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1055 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1056 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1057 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1058 "Graph currently executing a \"listener removed\" listener: "
1060 g_array_append_val(graph
->listeners
.filter_sink_ports_connected
,
1062 listener_id
= graph
->listeners
.filter_sink_ports_connected
->len
- 1;
1063 BT_LIB_LOGD("Added \"filter to sink component ports connected\" listener to graph: "
1064 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1068 *out_listener_id
= listener_id
;
1071 return BT_FUNC_STATUS_OK
;
1075 enum bt_graph_listener_func_status
bt_graph_notify_port_added(
1076 struct bt_graph
*graph
, struct bt_port
*port
)
1080 struct bt_component
*comp
;
1081 enum bt_graph_listener_func_status status
= BT_FUNC_STATUS_OK
;
1085 BT_LIB_LOGD("Notifying graph listeners that a port was added: "
1086 "%![graph-]+g, %![port-]+p", graph
, port
);
1087 comp
= bt_port_borrow_component_inline(port
);
1090 switch (comp
->class->type
) {
1091 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1093 switch (port
->type
) {
1094 case BT_PORT_TYPE_OUTPUT
:
1095 listeners
= graph
->listeners
.source_output_port_added
;
1103 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1105 switch (port
->type
) {
1106 case BT_PORT_TYPE_INPUT
:
1107 listeners
= graph
->listeners
.filter_input_port_added
;
1109 case BT_PORT_TYPE_OUTPUT
:
1110 listeners
= graph
->listeners
.filter_output_port_added
;
1118 case BT_COMPONENT_CLASS_TYPE_SINK
:
1120 switch (port
->type
) {
1121 case BT_PORT_TYPE_INPUT
:
1122 listeners
= graph
->listeners
.sink_input_port_added
;
1134 for (i
= 0; i
< listeners
->len
; i
++) {
1135 struct bt_graph_listener_port_added
*listener
=
1136 &g_array_index(listeners
,
1137 struct bt_graph_listener_port_added
, i
);
1140 BT_ASSERT(listener
->func
);
1141 status
= listener
->func(comp
, port
, listener
->base
.data
);
1142 if (status
!= BT_FUNC_STATUS_OK
) {
1152 enum bt_graph_listener_func_status
bt_graph_notify_ports_connected(
1153 struct bt_graph
*graph
, struct bt_port
*upstream_port
,
1154 struct bt_port
*downstream_port
)
1158 struct bt_component
*upstream_comp
;
1159 struct bt_component
*downstream_comp
;
1160 enum bt_graph_listener_func_status status
= BT_FUNC_STATUS_OK
;
1163 BT_ASSERT(upstream_port
);
1164 BT_ASSERT(downstream_port
);
1165 BT_LIB_LOGD("Notifying graph listeners that ports were connected: "
1166 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1167 graph
, upstream_port
, downstream_port
);
1168 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
1169 BT_ASSERT(upstream_comp
);
1170 downstream_comp
= bt_port_borrow_component_inline(downstream_port
);
1171 BT_ASSERT(downstream_comp
);
1173 switch (upstream_comp
->class->type
) {
1174 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1176 switch (downstream_comp
->class->type
) {
1177 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1179 graph
->listeners
.source_filter_ports_connected
;
1181 case BT_COMPONENT_CLASS_TYPE_SINK
:
1183 graph
->listeners
.source_sink_ports_connected
;
1191 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1193 switch (downstream_comp
->class->type
) {
1194 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1196 graph
->listeners
.filter_filter_ports_connected
;
1198 case BT_COMPONENT_CLASS_TYPE_SINK
:
1200 graph
->listeners
.filter_sink_ports_connected
;
1212 for (i
= 0; i
< listeners
->len
; i
++) {
1213 struct bt_graph_listener_ports_connected
*listener
=
1214 &g_array_index(listeners
,
1215 struct bt_graph_listener_ports_connected
, i
);
1217 BT_ASSERT(listener
->func
);
1218 status
= listener
->func(upstream_comp
, downstream_comp
,
1219 upstream_port
, downstream_port
, listener
->base
.data
);
1220 if (status
!= BT_FUNC_STATUS_OK
) {
1230 void bt_graph_remove_connection(struct bt_graph
*graph
,
1231 struct bt_connection
*connection
)
1234 BT_ASSERT(connection
);
1235 BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1237 g_ptr_array_remove(graph
->connections
, connection
);
1241 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
1243 bool exists
= false;
1246 for (i
= 0; i
< graph
->components
->len
; i
++) {
1247 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
1249 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1250 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1251 "%![other-comp-]+c, name=\"%s\"",
1263 int add_component_with_init_method_data(
1264 struct bt_graph
*graph
,
1265 struct bt_component_class
*comp_cls
,
1266 comp_init_method_t init_method
,
1267 const char *name
, const struct bt_value
*params
,
1268 void *init_method_data
, bt_logging_level log_level
,
1269 const struct bt_component
**user_component
)
1271 int status
= BT_FUNC_STATUS_OK
;
1272 enum bt_component_class_init_method_status init_status
;
1273 struct bt_component
*component
= NULL
;
1275 bool init_can_consume
;
1276 struct bt_value
*new_params
= NULL
;
1278 BT_ASSERT(comp_cls
);
1279 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1280 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1282 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
1283 "Graph is not in the \"configuring\" state: %!+g", graph
);
1284 BT_ASSERT_PRE(!component_name_exists(graph
, name
),
1285 "Duplicate component name: %!+g, name=\"%s\"", graph
, name
);
1286 BT_ASSERT_PRE(!params
|| bt_value_is_map(params
),
1287 "Parameter value is not a map value: %!+v", params
);
1288 init_can_consume
= graph
->can_consume
;
1289 bt_graph_set_can_consume(graph
, false);
1290 BT_LIB_LOGI("Adding component to graph: "
1291 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1292 "%![params-]+v, init-method-data-addr=%p",
1293 graph
, comp_cls
, name
,
1294 bt_common_logging_level_string(log_level
), params
,
1298 new_params
= bt_value_map_create();
1300 BT_LIB_LOGE_APPEND_CAUSE(
1301 "Cannot create empty map value object.");
1302 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1306 params
= new_params
;
1309 ret
= bt_component_create(comp_cls
, name
, log_level
, &component
);
1311 BT_LIB_LOGE_APPEND_CAUSE(
1312 "Cannot create empty component object: ret=%d",
1314 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1319 * The user's initialization method needs to see that this
1320 * component is part of the graph. If the user method fails, we
1321 * immediately remove the component from the graph's components.
1323 g_ptr_array_add(graph
->components
, component
);
1324 bt_component_set_graph(component
, graph
);
1325 bt_value_freeze(params
);
1328 BT_LOGD_STR("Calling user's initialization method.");
1329 init_status
= init_method(component
, params
, init_method_data
);
1330 BT_LOGD("User method returned: status=%s",
1331 bt_common_func_status_string(init_status
));
1332 if (init_status
!= BT_FUNC_STATUS_OK
) {
1333 if (init_status
< 0) {
1334 BT_LIB_LOGW_APPEND_CAUSE(
1335 "Component initialization method failed: "
1336 "status=%s, %![comp-]+c",
1337 bt_common_func_status_string(init_status
),
1341 status
= init_status
;
1342 bt_component_set_graph(component
, NULL
);
1343 g_ptr_array_remove_fast(graph
->components
, component
);
1349 * Mark the component as initialized so that its finalization
1350 * method is called when it is destroyed.
1352 component
->initialized
= true;
1355 * If it's a sink component, it needs to be part of the graph's
1356 * sink queue to be consumed by bt_graph_run() or
1357 * bt_graph_run_once().
1359 if (bt_component_is_sink(component
)) {
1360 graph
->has_sink
= true;
1361 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1365 * Freeze the component class now that it's instantiated at
1368 BT_LOGD_STR("Freezing component class.");
1369 bt_component_class_freeze(comp_cls
);
1370 BT_LIB_LOGI("Added component to graph: "
1371 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1372 "%![params-]+v, init-method-data-addr=%p, %![comp-]+c",
1373 graph
, comp_cls
, name
,
1374 bt_common_logging_level_string(log_level
), params
,
1375 init_method_data
, component
);
1377 if (user_component
) {
1378 /* Move reference to user */
1379 *user_component
= component
;
1384 if (status
!= BT_FUNC_STATUS_OK
) {
1385 bt_graph_make_faulty(graph
);
1388 bt_object_put_ref(component
);
1389 bt_object_put_ref(new_params
);
1390 (void) init_can_consume
;
1391 bt_graph_set_can_consume(graph
, init_can_consume
);
1395 enum bt_graph_add_component_status
1396 bt_graph_add_source_component_with_init_method_data(
1397 struct bt_graph
*graph
,
1398 const struct bt_component_class_source
*comp_cls
,
1399 const char *name
, const struct bt_value
*params
,
1400 void *init_method_data
, bt_logging_level log_level
,
1401 const struct bt_component_source
**component
)
1403 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1404 return add_component_with_init_method_data(graph
,
1405 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1406 name
, params
, init_method_data
, log_level
, (void *) component
);
1409 enum bt_graph_add_component_status
bt_graph_add_source_component(
1410 struct bt_graph
*graph
,
1411 const struct bt_component_class_source
*comp_cls
,
1412 const char *name
, const struct bt_value
*params
,
1413 enum bt_logging_level log_level
,
1414 const struct bt_component_source
**component
)
1416 return bt_graph_add_source_component_with_init_method_data(
1417 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1420 enum bt_graph_add_component_status
1421 bt_graph_add_filter_component_with_init_method_data(
1422 struct bt_graph
*graph
,
1423 const struct bt_component_class_filter
*comp_cls
,
1424 const char *name
, const struct bt_value
*params
,
1425 void *init_method_data
, enum bt_logging_level log_level
,
1426 const struct bt_component_filter
**component
)
1428 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1429 return add_component_with_init_method_data(graph
,
1430 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1431 name
, params
, init_method_data
, log_level
, (void *) component
);
1434 enum bt_graph_add_component_status
bt_graph_add_filter_component(
1435 struct bt_graph
*graph
,
1436 const struct bt_component_class_filter
*comp_cls
,
1437 const char *name
, const struct bt_value
*params
,
1438 enum bt_logging_level log_level
,
1439 const struct bt_component_filter
**component
)
1441 return bt_graph_add_filter_component_with_init_method_data(
1442 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1445 enum bt_graph_add_component_status
1446 bt_graph_add_sink_component_with_init_method_data(
1447 struct bt_graph
*graph
,
1448 const struct bt_component_class_sink
*comp_cls
,
1449 const char *name
, const struct bt_value
*params
,
1450 void *init_method_data
, enum bt_logging_level log_level
,
1451 const struct bt_component_sink
**component
)
1453 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1454 return add_component_with_init_method_data(graph
,
1455 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1456 name
, params
, init_method_data
, log_level
, (void *) component
);
1459 enum bt_graph_add_component_status
bt_graph_add_sink_component(
1460 struct bt_graph
*graph
,
1461 const struct bt_component_class_sink
*comp_cls
,
1462 const char *name
, const struct bt_value
*params
,
1463 enum bt_logging_level log_level
,
1464 const struct bt_component_sink
**component
)
1466 return bt_graph_add_sink_component_with_init_method_data(
1467 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1470 enum bt_graph_add_component_status
1471 bt_graph_add_simple_sink_component(struct bt_graph
*graph
, const char *name
,
1472 bt_graph_simple_sink_component_init_func init_func
,
1473 bt_graph_simple_sink_component_consume_func consume_func
,
1474 bt_graph_simple_sink_component_finalize_func finalize_func
,
1475 void *user_data
, const bt_component_sink
**component
)
1477 enum bt_graph_add_component_status status
;
1478 struct bt_component_class_sink
*comp_cls
;
1479 struct simple_sink_init_method_data init_method_data
= {
1480 .init_func
= init_func
,
1481 .consume_func
= consume_func
,
1482 .finalize_func
= finalize_func
,
1483 .user_data
= user_data
,
1487 * Other preconditions are checked by
1488 * bt_graph_add_sink_component_with_init_method_data().
1490 BT_ASSERT_PRE_NON_NULL(consume_func
, "Consume function");
1492 comp_cls
= bt_component_class_sink_simple_borrow();
1494 BT_LIB_LOGE_APPEND_CAUSE(
1495 "Cannot borrow simple sink component class.");
1496 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1500 status
= bt_graph_add_sink_component_with_init_method_data(graph
,
1501 comp_cls
, name
, NULL
, &init_method_data
,
1502 BT_LOGGING_LEVEL_NONE
, component
);
1509 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1510 struct bt_component
*component
)
1512 bool init_can_consume
;
1518 BT_ASSERT(component
);
1519 BT_ASSERT(component
->base
.ref_count
== 0);
1520 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1522 init_can_consume
= graph
->can_consume
;
1523 count
= bt_component_get_input_port_count(component
);
1525 for (i
= 0; i
< count
; i
++) {
1526 struct bt_port
*port
= (void *)
1527 bt_component_borrow_input_port_by_index(component
, i
);
1531 if (bt_port_is_connected(port
)) {
1532 BT_LIB_LOGW_APPEND_CAUSE(
1533 "Cannot remove component from graph: "
1534 "an input port is connected: "
1535 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1536 graph
, component
, port
);
1541 count
= bt_component_get_output_port_count(component
);
1543 for (i
= 0; i
< count
; i
++) {
1544 struct bt_port
*port
= (void *)
1545 bt_component_borrow_output_port_by_index(component
, i
);
1549 if (bt_port_is_connected(port
)) {
1550 BT_LIB_LOGW_APPEND_CAUSE(
1551 "Cannot remove component from graph: "
1552 "an output port is connected: "
1553 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1554 graph
, component
, port
);
1559 bt_graph_set_can_consume(graph
, false);
1561 /* Possibly remove from sinks to consume */
1562 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1564 if (graph
->sinks_to_consume
->length
== 0) {
1565 graph
->has_sink
= false;
1569 * This calls bt_object_try_spec_release() on the component, and
1570 * since its reference count is 0, its destructor is called. Its
1571 * destructor calls the user's finalization method (if set).
1573 g_ptr_array_remove(graph
->components
, component
);
1580 (void) init_can_consume
;
1581 bt_graph_set_can_consume(graph
, init_can_consume
);
1586 void bt_graph_add_message(struct bt_graph
*graph
,
1587 struct bt_message
*msg
)
1593 * It's okay not to take a reference because, when a
1594 * message's reference count drops to 0, either:
1596 * * It is recycled back to one of this graph's pool.
1597 * * It is destroyed because it doesn't have any link to any
1598 * graph, which means the original graph is already destroyed.
1600 g_ptr_array_add(graph
->messages
, msg
);
1604 bool bt_graph_is_interrupted(const struct bt_graph
*graph
)
1607 return bt_interrupter_array_any_is_set(graph
->interrupters
);
1610 enum bt_graph_add_interrupter_status
bt_graph_add_interrupter(
1611 struct bt_graph
*graph
, const struct bt_interrupter
*intr
)
1613 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1614 BT_ASSERT_PRE_NON_NULL(intr
, "Interrupter");
1615 g_ptr_array_add(graph
->interrupters
, (void *) intr
);
1616 bt_object_get_no_null_check(intr
);
1617 BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z",
1619 return BT_FUNC_STATUS_OK
;
1622 void bt_graph_interrupt(struct bt_graph
*graph
)
1624 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1625 bt_interrupter_set(graph
->default_interrupter
);
1626 BT_LIB_LOGI("Interrupted graph: %!+g", graph
);
1629 void bt_graph_get_ref(const struct bt_graph
*graph
)
1631 bt_object_get_ref(graph
);
1634 void bt_graph_put_ref(const struct bt_graph
*graph
)
1636 bt_object_put_ref(graph
);