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 "interrupter.h"
50 #include "message/event.h"
51 #include "message/packet.h"
53 typedef enum bt_graph_listener_func_status
54 (*port_added_func_t
)(const void *, const void *, void *);
56 typedef enum bt_graph_listener_func_status
57 (*ports_connected_func_t
)(const void *, const void *, const void *,
58 const void *, void *);
60 typedef enum bt_component_class_init_method_status
61 (*comp_init_method_t
)(const void *, const void *, void *);
63 struct bt_graph_listener
{
64 bt_graph_listener_removed_func removed
;
68 struct bt_graph_listener_port_added
{
69 struct bt_graph_listener base
;
70 port_added_func_t func
;
73 struct bt_graph_listener_ports_connected
{
74 struct bt_graph_listener base
;
75 ports_connected_func_t func
;
78 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
80 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
81 if (!(_listeners)) { \
82 BT_LIB_LOGE_APPEND_CAUSE( \
83 "Failed to allocate one GArray."); \
87 #define CALL_REMOVE_LISTENERS(_type, _listeners) \
94 for (i = 0; i < (_listeners)->len; i++) { \
96 &g_array_index((_listeners), _type, i); \
98 if (listener->base.removed) { \
99 listener->base.removed(listener->base.data); \
105 void destroy_graph(struct bt_object
*obj
)
107 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
110 * The graph's reference count is 0 if we're here. Increment
111 * it to avoid a double-destroy (possibly infinitely recursive)
114 * 1. We put and destroy a connection.
115 * 2. This connection's destructor finalizes its active message
117 * 3. A message iterator's finalization function gets a new
118 * reference on its component (reference count goes from 0 to
120 * 4. Since this component's reference count goes to 1, it takes
121 * a reference on its parent (this graph). This graph's
122 * reference count goes from 0 to 1.
123 * 5. The message iterator's finalization function puts its
124 * component reference (reference count goes from 1 to 0).
125 * 6. Since this component's reference count goes from 1 to 0,
126 * it puts its parent (this graph). This graph's reference
127 * count goes from 1 to 0.
128 * 7. Since this graph's reference count goes from 1 to 0, its
129 * destructor is called (this function).
131 * With the incrementation below, the graph's reference count at
132 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
133 * ensures that this function is not called two times.
135 BT_LIB_LOGI("Destroying graph: %!+g", graph
);
137 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_DESTROYING
;
139 /* Call all remove listeners */
140 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
141 graph
->listeners
.source_output_port_added
);
142 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
143 graph
->listeners
.filter_output_port_added
);
144 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
145 graph
->listeners
.filter_input_port_added
);
146 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added
,
147 graph
->listeners
.sink_input_port_added
);
148 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
149 graph
->listeners
.source_filter_ports_connected
);
150 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
151 graph
->listeners
.filter_filter_ports_connected
);
152 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
153 graph
->listeners
.source_sink_ports_connected
);
154 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected
,
155 graph
->listeners
.filter_sink_ports_connected
);
157 if (graph
->messages
) {
158 g_ptr_array_free(graph
->messages
, TRUE
);
159 graph
->messages
= NULL
;
162 if (graph
->connections
) {
163 BT_LOGD_STR("Destroying connections.");
164 g_ptr_array_free(graph
->connections
, TRUE
);
165 graph
->connections
= NULL
;
168 if (graph
->components
) {
169 BT_LOGD_STR("Destroying components.");
170 g_ptr_array_free(graph
->components
, TRUE
);
171 graph
->components
= NULL
;
174 if (graph
->interrupters
) {
175 BT_LOGD_STR("Putting interrupters.");
176 g_ptr_array_free(graph
->interrupters
, TRUE
);
177 graph
->interrupters
= NULL
;
180 BT_OBJECT_PUT_REF_AND_RESET(graph
->default_interrupter
);
182 if (graph
->sinks_to_consume
) {
183 g_queue_free(graph
->sinks_to_consume
);
184 graph
->sinks_to_consume
= NULL
;
187 if (graph
->listeners
.source_output_port_added
) {
188 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
189 graph
->listeners
.source_output_port_added
= NULL
;
192 if (graph
->listeners
.filter_output_port_added
) {
193 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
194 graph
->listeners
.filter_output_port_added
= NULL
;
197 if (graph
->listeners
.filter_input_port_added
) {
198 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
199 graph
->listeners
.filter_input_port_added
= NULL
;
202 if (graph
->listeners
.sink_input_port_added
) {
203 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
204 graph
->listeners
.sink_input_port_added
= NULL
;
207 if (graph
->listeners
.source_filter_ports_connected
) {
208 g_array_free(graph
->listeners
.source_filter_ports_connected
,
210 graph
->listeners
.source_filter_ports_connected
= NULL
;
213 if (graph
->listeners
.filter_filter_ports_connected
) {
214 g_array_free(graph
->listeners
.filter_filter_ports_connected
,
216 graph
->listeners
.filter_filter_ports_connected
= NULL
;
219 if (graph
->listeners
.source_sink_ports_connected
) {
220 g_array_free(graph
->listeners
.source_sink_ports_connected
,
222 graph
->listeners
.source_sink_ports_connected
= NULL
;
225 if (graph
->listeners
.filter_sink_ports_connected
) {
226 g_array_free(graph
->listeners
.filter_sink_ports_connected
,
228 graph
->listeners
.filter_sink_ports_connected
= NULL
;
231 bt_object_pool_finalize(&graph
->event_msg_pool
);
232 bt_object_pool_finalize(&graph
->packet_begin_msg_pool
);
233 bt_object_pool_finalize(&graph
->packet_end_msg_pool
);
238 void destroy_message_event(struct bt_message
*msg
,
239 struct bt_graph
*graph
)
241 bt_message_event_destroy(msg
);
245 void destroy_message_packet_begin(struct bt_message
*msg
,
246 struct bt_graph
*graph
)
248 bt_message_packet_destroy(msg
);
252 void destroy_message_packet_end(struct bt_message
*msg
,
253 struct bt_graph
*graph
)
255 bt_message_packet_destroy(msg
);
259 void notify_message_graph_is_destroyed(struct bt_message
*msg
)
261 bt_message_unlink_graph(msg
);
264 struct bt_graph
*bt_graph_create(void)
266 struct bt_graph
*graph
;
269 BT_LOGI_STR("Creating graph object.");
270 graph
= g_new0(struct bt_graph
, 1);
272 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph.");
276 bt_object_init_shared(&graph
->base
, destroy_graph
);
277 graph
->connections
= g_ptr_array_new_with_free_func(
278 (GDestroyNotify
) bt_object_try_spec_release
);
279 if (!graph
->connections
) {
280 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
283 graph
->components
= g_ptr_array_new_with_free_func(
284 (GDestroyNotify
) bt_object_try_spec_release
);
285 if (!graph
->components
) {
286 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
289 graph
->sinks_to_consume
= g_queue_new();
290 if (!graph
->sinks_to_consume
) {
291 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue.");
295 bt_graph_set_can_consume(graph
, true);
296 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
297 graph
->listeners
.source_output_port_added
);
299 if (!graph
->listeners
.source_output_port_added
) {
304 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
305 graph
->listeners
.filter_output_port_added
);
307 if (!graph
->listeners
.filter_output_port_added
) {
312 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
313 graph
->listeners
.filter_input_port_added
);
315 if (!graph
->listeners
.filter_input_port_added
) {
320 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
321 graph
->listeners
.sink_input_port_added
);
323 if (!graph
->listeners
.sink_input_port_added
) {
328 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
329 graph
->listeners
.source_filter_ports_connected
);
331 if (!graph
->listeners
.source_filter_ports_connected
) {
336 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
337 graph
->listeners
.source_sink_ports_connected
);
339 if (!graph
->listeners
.source_sink_ports_connected
) {
344 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
345 graph
->listeners
.filter_filter_ports_connected
);
347 if (!graph
->listeners
.filter_filter_ports_connected
) {
352 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected
,
353 graph
->listeners
.filter_sink_ports_connected
);
355 if (!graph
->listeners
.filter_sink_ports_connected
) {
360 graph
->interrupters
= g_ptr_array_new_with_free_func(
361 (GDestroyNotify
) bt_object_put_no_null_check
);
362 if (!graph
->interrupters
) {
363 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
367 graph
->default_interrupter
= bt_interrupter_create();
368 if (!graph
->default_interrupter
) {
369 BT_LIB_LOGE_APPEND_CAUSE(
370 "Failed to create one interrupter object.");
374 bt_graph_add_interrupter(graph
, graph
->default_interrupter
);
375 ret
= bt_object_pool_initialize(&graph
->event_msg_pool
,
376 (bt_object_pool_new_object_func
) bt_message_event_new
,
377 (bt_object_pool_destroy_object_func
) destroy_message_event
,
380 BT_LIB_LOGE_APPEND_CAUSE(
381 "Failed to initialize event message pool: ret=%d",
386 ret
= bt_object_pool_initialize(&graph
->packet_begin_msg_pool
,
387 (bt_object_pool_new_object_func
) bt_message_packet_beginning_new
,
388 (bt_object_pool_destroy_object_func
) destroy_message_packet_begin
,
391 BT_LIB_LOGE_APPEND_CAUSE(
392 "Failed to initialize packet beginning message pool: ret=%d",
397 ret
= bt_object_pool_initialize(&graph
->packet_end_msg_pool
,
398 (bt_object_pool_new_object_func
) bt_message_packet_end_new
,
399 (bt_object_pool_destroy_object_func
) destroy_message_packet_end
,
402 BT_LIB_LOGE_APPEND_CAUSE(
403 "Failed to initialize packet end message pool: ret=%d",
408 graph
->messages
= g_ptr_array_new_with_free_func(
409 (GDestroyNotify
) notify_message_graph_is_destroyed
);
410 BT_LIB_LOGI("Created graph object: %!+g", graph
);
413 return (void *) graph
;
416 BT_OBJECT_PUT_REF_AND_RESET(graph
);
420 enum bt_graph_connect_ports_status
bt_graph_connect_ports(
421 struct bt_graph
*graph
,
422 const struct bt_port_output
*upstream_port_out
,
423 const struct bt_port_input
*downstream_port_in
,
424 const struct bt_connection
**user_connection
)
426 enum bt_graph_connect_ports_status status
= BT_FUNC_STATUS_OK
;
427 enum bt_graph_listener_func_status listener_status
;
428 struct bt_connection
*connection
= NULL
;
429 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
430 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
431 struct bt_component
*upstream_component
= NULL
;
432 struct bt_component
*downstream_component
= NULL
;
433 enum bt_component_class_port_connected_method_status port_connected_status
;
434 bool init_can_consume
;
436 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
437 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
438 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
440 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
441 "Graph is not in the \"configuring\" state: %!+g", graph
);
442 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
443 "Upstream port is already connected: %!+p", upstream_port
);
444 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
445 "Downstream port is already connected: %!+p", downstream_port
);
446 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
447 "Upstream port does not belong to a component: %!+p",
449 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
450 "Downstream port does not belong to a component: %!+p",
452 init_can_consume
= graph
->can_consume
;
453 BT_LIB_LOGI("Connecting component ports within graph: "
454 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
455 graph
, upstream_port
, downstream_port
);
456 bt_graph_set_can_consume(graph
, false);
457 upstream_component
= bt_port_borrow_component_inline(
458 (void *) upstream_port
);
459 downstream_component
= bt_port_borrow_component_inline(
460 (void *) downstream_port
);
462 BT_LOGD_STR("Creating connection.");
463 connection
= bt_connection_create(graph
, (void *) upstream_port
,
464 (void *) downstream_port
);
466 BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object.");
467 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
471 BT_LIB_LOGD("Connection object created: %!+x", connection
);
474 * Ownership of upstream_component/downstream_component and of
475 * the connection object is transferred to the graph.
477 g_ptr_array_add(graph
->connections
, connection
);
480 * Notify both components that their port is connected.
482 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
483 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
484 port_connected_status
= bt_component_port_connected(upstream_component
,
485 (void *) upstream_port
, (void *) downstream_port
);
486 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
487 if (port_connected_status
< 0) {
488 BT_LIB_LOGW_APPEND_CAUSE(
489 "Upstream component's \"port connected\" method failed: "
490 "status=%s, %![graph-]+g, %![up-comp-]+c, "
491 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
492 bt_common_func_status_string(
493 port_connected_status
),
494 graph
, upstream_component
, downstream_component
,
495 upstream_port
, downstream_port
);
498 bt_connection_end(connection
, true);
499 status
= (int) port_connected_status
;
503 connection
->notified_upstream_port_connected
= true;
504 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
505 "%![comp-]+c, %![port-]+p", downstream_component
,
507 port_connected_status
= bt_component_port_connected(downstream_component
,
508 (void *) downstream_port
, (void *) upstream_port
);
509 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
510 if (port_connected_status
< 0) {
511 BT_LIB_LOGW_APPEND_CAUSE(
512 "Downstream component's \"port connected\" method failed: "
513 "status=%s, %![graph-]+g, %![up-comp-]+c, "
514 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
515 bt_common_func_status_string(
516 port_connected_status
),
517 graph
, upstream_component
, downstream_component
,
518 upstream_port
, downstream_port
);
521 bt_connection_end(connection
, true);
522 status
= (int) port_connected_status
;
526 connection
->notified_downstream_port_connected
= true;
529 * Notify the graph's creator that both ports are connected.
531 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
532 listener_status
= bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
533 if (listener_status
!= BT_FUNC_STATUS_OK
) {
534 if (listener_status
< 0) {
535 BT_LIB_LOGW_APPEND_CAUSE(
536 "Graph \"ports connected\" listener failed: "
537 "status=%d, %![graph-]+g, %![up-comp-]+c, "
538 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
539 listener_status
, graph
,
540 upstream_component
, downstream_component
,
541 upstream_port
, downstream_port
);
544 status
= (int) listener_status
;
548 connection
->notified_graph_ports_connected
= true;
549 BT_LIB_LOGI("Connected component ports within graph: "
550 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
551 "%![up-port-]+p, %![down-port-]+p",
552 graph
, upstream_component
, downstream_component
,
553 upstream_port
, downstream_port
);
555 if (user_connection
) {
556 /* Move reference to user */
557 *user_connection
= connection
;
562 if (status
!= BT_FUNC_STATUS_OK
) {
563 bt_graph_make_faulty(graph
);
566 bt_object_put_ref(connection
);
567 (void) init_can_consume
;
568 bt_graph_set_can_consume(graph
, init_can_consume
);
573 int consume_graph_sink(struct bt_component_sink
*comp
)
575 enum bt_component_class_sink_consume_method_status consume_status
;
576 struct bt_component_class_sink
*sink_class
= NULL
;
579 sink_class
= (void *) comp
->parent
.class;
580 BT_ASSERT(sink_class
->methods
.consume
);
581 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
582 consume_status
= sink_class
->methods
.consume((void *) comp
);
583 BT_LOGD("User method returned: status=%s",
584 bt_common_func_status_string(consume_status
));
585 BT_ASSERT_POST_DEV(consume_status
== BT_FUNC_STATUS_OK
||
586 consume_status
== BT_FUNC_STATUS_END
||
587 consume_status
== BT_FUNC_STATUS_AGAIN
||
588 consume_status
== BT_FUNC_STATUS_ERROR
||
589 consume_status
== BT_FUNC_STATUS_MEMORY_ERROR
,
590 "Invalid component status returned by consuming method: "
591 "status=%s", bt_common_func_status_string(consume_status
));
592 if (consume_status
) {
593 if (consume_status
< 0) {
594 BT_LIB_LOGW_APPEND_CAUSE(
595 "Component's \"consume\" method failed: "
596 "status=%s, %![comp-]+c",
597 bt_common_func_status_string(consume_status
),
604 BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s",
605 comp
, bt_common_func_status_string(consume_status
));
608 return consume_status
;
612 * `node` is removed from the queue of sinks to consume when passed to
613 * this function. This function adds it back to the queue if there's
614 * still something to consume afterwards.
617 int consume_sink_node(struct bt_graph
*graph
, GList
*node
)
620 struct bt_component_sink
*sink
;
623 status
= consume_graph_sink(sink
);
624 if (G_UNLIKELY(status
!= BT_FUNC_STATUS_END
)) {
625 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
629 /* End reached, the node is not added back to the queue and free'd. */
630 g_queue_delete_link(graph
->sinks_to_consume
, node
);
632 /* Don't forward an END status if there are sinks left to consume. */
633 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
634 status
= BT_FUNC_STATUS_OK
;
639 BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s",
640 sink
, bt_common_func_status_string(status
));
645 int bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
646 struct bt_component_sink
*sink
)
652 BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink
);
653 BT_ASSERT(bt_component_borrow_graph((void *) sink
) == graph
);
655 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
656 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
657 status
= BT_FUNC_STATUS_END
;
661 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
663 BT_LIB_LOGD("Sink component is not marked as consumable: "
664 "component sink is ended: %![comp-]+c", sink
);
665 status
= BT_FUNC_STATUS_END
;
669 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
670 BT_ASSERT(sink_node
);
671 status
= consume_sink_node(graph
, sink_node
);
678 int consume_no_check(struct bt_graph
*graph
)
680 int status
= BT_FUNC_STATUS_OK
;
681 struct bt_component
*sink
;
684 BT_ASSERT_PRE_DEV(graph
->has_sink
,
685 "Graph has no sink component: %!+g", graph
);
686 BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph
);
688 if (G_UNLIKELY(g_queue_is_empty(graph
->sinks_to_consume
))) {
689 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
690 status
= BT_FUNC_STATUS_END
;
694 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
695 sink
= current_node
->data
;
696 BT_LIB_LOGD("Chose next sink to consume: %!+c", sink
);
697 status
= consume_sink_node(graph
, current_node
);
703 enum bt_graph_consume_status
bt_graph_consume(struct bt_graph
*graph
)
705 enum bt_graph_consume_status status
;
707 BT_ASSERT_PRE_DEV_NON_NULL(graph
, "Graph");
708 BT_ASSERT_PRE_DEV(graph
->can_consume
,
709 "Cannot consume graph in its current state: %!+g", graph
);
710 BT_ASSERT_PRE_DEV(graph
->config_state
!=
711 BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
712 "Graph is in a faulty state: %!+g", graph
);
713 bt_graph_set_can_consume(graph
, false);
714 status
= bt_graph_configure(graph
);
715 if (G_UNLIKELY(status
)) {
716 /* bt_graph_configure() logs errors */
720 status
= consume_no_check(graph
);
721 bt_graph_set_can_consume(graph
, true);
727 enum bt_graph_run_status
bt_graph_run(struct bt_graph
*graph
)
729 enum bt_graph_run_status status
;
731 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
732 BT_ASSERT_PRE(graph
->can_consume
,
733 "Cannot consume graph in its current state: %!+g", graph
);
734 BT_ASSERT_PRE(graph
->config_state
!= BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
735 "Graph is in a faulty state: %!+g", graph
);
736 bt_graph_set_can_consume(graph
, false);
737 status
= bt_graph_configure(graph
);
738 if (G_UNLIKELY(status
)) {
739 /* bt_graph_configure() logs errors */
743 BT_LIB_LOGI("Running graph: %!+g", graph
);
747 * Check if the graph is interrupted at each iteration.
748 * If the graph was interrupted by another thread or by
749 * a signal handler, this is NOT a warning nor an error;
750 * it was intentional: log with an INFO level only.
752 if (G_UNLIKELY(bt_graph_is_interrupted(graph
))) {
753 BT_LIB_LOGI("Stopping the graph: "
754 "graph was interrupted: %!+g", graph
);
755 status
= BT_FUNC_STATUS_AGAIN
;
759 status
= consume_no_check(graph
);
760 if (G_UNLIKELY(status
== BT_FUNC_STATUS_AGAIN
)) {
762 * If AGAIN is received and there are multiple
763 * sinks, go ahead and consume from the next
766 * However, in the case where a single sink is
767 * left, the caller can decide to busy-wait and
768 * call bt_graph_run() continuously
769 * until the source is ready or it can decide to
770 * sleep for an arbitrary amount of time.
772 if (graph
->sinks_to_consume
->length
> 1) {
773 status
= BT_FUNC_STATUS_OK
;
776 } while (status
== BT_FUNC_STATUS_OK
);
778 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
779 status
= BT_FUNC_STATUS_END
;
783 BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph
,
784 bt_common_func_status_string(status
));
785 bt_graph_set_can_consume(graph
, true);
789 enum bt_graph_add_listener_status
790 bt_graph_add_source_component_output_port_added_listener(
791 struct bt_graph
*graph
,
792 bt_graph_source_component_output_port_added_listener_func func
,
793 bt_graph_listener_removed_func listener_removed
, void *data
,
794 bt_listener_id
*out_listener_id
)
796 struct bt_graph_listener_port_added listener
= {
798 .removed
= listener_removed
,
801 .func
= (port_added_func_t
) func
,
803 bt_listener_id listener_id
;
805 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
806 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
807 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
808 BT_ASSERT_PRE(!graph
->in_remove_listener
,
809 "Graph currently executing a \"listener removed\" listener: "
811 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
812 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
813 BT_LIB_LOGD("Added \"source component output port added\" listener to graph: "
814 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
818 *out_listener_id
= listener_id
;
821 return BT_FUNC_STATUS_OK
;
824 enum bt_graph_add_listener_status
825 bt_graph_add_filter_component_output_port_added_listener(
826 struct bt_graph
*graph
,
827 bt_graph_filter_component_output_port_added_listener_func func
,
828 bt_graph_listener_removed_func listener_removed
, void *data
,
829 bt_listener_id
*out_listener_id
)
831 struct bt_graph_listener_port_added listener
= {
833 .removed
= listener_removed
,
836 .func
= (port_added_func_t
) func
,
838 bt_listener_id listener_id
;
840 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
841 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
842 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
843 BT_ASSERT_PRE(!graph
->in_remove_listener
,
844 "Graph currently executing a \"listener removed\" listener: "
846 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
847 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
848 BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: "
849 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
853 *out_listener_id
= listener_id
;
856 return BT_FUNC_STATUS_OK
;
859 enum bt_graph_add_listener_status
860 bt_graph_add_filter_component_input_port_added_listener(
861 struct bt_graph
*graph
,
862 bt_graph_filter_component_input_port_added_listener_func func
,
863 bt_graph_listener_removed_func listener_removed
, void *data
,
864 bt_listener_id
*out_listener_id
)
866 struct bt_graph_listener_port_added listener
= {
868 .removed
= listener_removed
,
871 .func
= (port_added_func_t
) func
,
873 bt_listener_id listener_id
;
875 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
876 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
877 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
878 BT_ASSERT_PRE(!graph
->in_remove_listener
,
879 "Graph currently executing a \"listener removed\" listener: "
881 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
882 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
883 BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: "
884 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
888 *out_listener_id
= listener_id
;
891 return BT_FUNC_STATUS_OK
;
894 enum bt_graph_add_listener_status
895 bt_graph_add_sink_component_input_port_added_listener(
896 struct bt_graph
*graph
,
897 bt_graph_sink_component_input_port_added_listener_func func
,
898 bt_graph_listener_removed_func listener_removed
, void *data
,
899 bt_listener_id
*out_listener_id
)
901 struct bt_graph_listener_port_added listener
= {
903 .removed
= listener_removed
,
906 .func
= (port_added_func_t
) func
,
908 bt_listener_id listener_id
;
910 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
911 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
912 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
913 BT_ASSERT_PRE(!graph
->in_remove_listener
,
914 "Graph currently executing a \"listener removed\" listener: "
916 g_array_append_val(graph
->listeners
.sink_input_port_added
, listener
);
917 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
918 BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: "
919 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
923 *out_listener_id
= listener_id
;
926 return BT_FUNC_STATUS_OK
;
929 enum bt_graph_add_listener_status
930 bt_graph_add_source_filter_component_ports_connected_listener(
931 struct bt_graph
*graph
,
932 bt_graph_source_filter_component_ports_connected_listener_func func
,
933 bt_graph_listener_removed_func listener_removed
, void *data
,
934 bt_listener_id
*out_listener_id
)
936 struct bt_graph_listener_ports_connected listener
= {
938 .removed
= listener_removed
,
941 .func
= (ports_connected_func_t
) func
,
943 bt_listener_id listener_id
;
945 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
946 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
947 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
948 BT_ASSERT_PRE(!graph
->in_remove_listener
,
949 "Graph currently executing a \"listener removed\" listener: "
951 g_array_append_val(graph
->listeners
.source_filter_ports_connected
,
953 listener_id
= graph
->listeners
.source_filter_ports_connected
->len
- 1;
954 BT_LIB_LOGD("Added \"source to filter component ports connected\" listener to graph: "
955 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
959 *out_listener_id
= listener_id
;
962 return BT_FUNC_STATUS_OK
;
965 enum bt_graph_add_listener_status
966 bt_graph_add_source_sink_component_ports_connected_listener(
967 struct bt_graph
*graph
,
968 bt_graph_source_sink_component_ports_connected_listener_func func
,
969 bt_graph_listener_removed_func listener_removed
, void *data
,
970 bt_listener_id
*out_listener_id
)
972 struct bt_graph_listener_ports_connected listener
= {
974 .removed
= listener_removed
,
977 .func
= (ports_connected_func_t
) func
,
979 bt_listener_id listener_id
;
981 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
982 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
983 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
984 BT_ASSERT_PRE(!graph
->in_remove_listener
,
985 "Graph currently executing a \"listener removed\" listener: "
987 g_array_append_val(graph
->listeners
.source_sink_ports_connected
,
989 listener_id
= graph
->listeners
.source_sink_ports_connected
->len
- 1;
990 BT_LIB_LOGD("Added \"source to sink component ports connected\" listener to graph: "
991 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
995 *out_listener_id
= listener_id
;
998 return BT_FUNC_STATUS_OK
;
1001 enum bt_graph_add_listener_status
1002 bt_graph_add_filter_filter_component_ports_connected_listener(
1003 struct bt_graph
*graph
,
1004 bt_graph_filter_filter_component_ports_connected_listener_func func
,
1005 bt_graph_listener_removed_func listener_removed
, void *data
,
1006 bt_listener_id
*out_listener_id
)
1008 struct bt_graph_listener_ports_connected listener
= {
1010 .removed
= listener_removed
,
1013 .func
= (ports_connected_func_t
) func
,
1015 bt_listener_id listener_id
;
1017 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1018 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1019 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1020 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1021 "Graph currently executing a \"listener removed\" listener: "
1023 g_array_append_val(graph
->listeners
.filter_filter_ports_connected
,
1025 listener_id
= graph
->listeners
.filter_filter_ports_connected
->len
- 1;
1026 BT_LIB_LOGD("Added \"filter to filter component ports connected\" listener to graph: "
1027 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1031 *out_listener_id
= listener_id
;
1034 return BT_FUNC_STATUS_OK
;
1037 enum bt_graph_add_listener_status
1038 bt_graph_add_filter_sink_component_ports_connected_listener(
1039 struct bt_graph
*graph
,
1040 bt_graph_filter_sink_component_ports_connected_listener_func func
,
1041 bt_graph_listener_removed_func listener_removed
, void *data
,
1042 bt_listener_id
*out_listener_id
)
1044 struct bt_graph_listener_ports_connected listener
= {
1046 .removed
= listener_removed
,
1049 .func
= (ports_connected_func_t
) func
,
1051 bt_listener_id listener_id
;
1053 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1054 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
1055 BT_ASSERT_PRE_NON_NULL(func
, "\"Listener removed\" listener");
1056 BT_ASSERT_PRE(!graph
->in_remove_listener
,
1057 "Graph currently executing a \"listener removed\" listener: "
1059 g_array_append_val(graph
->listeners
.filter_sink_ports_connected
,
1061 listener_id
= graph
->listeners
.filter_sink_ports_connected
->len
- 1;
1062 BT_LIB_LOGD("Added \"filter to sink component ports connected\" listener to graph: "
1063 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
1067 *out_listener_id
= listener_id
;
1070 return BT_FUNC_STATUS_OK
;
1074 enum bt_graph_listener_func_status
bt_graph_notify_port_added(
1075 struct bt_graph
*graph
, struct bt_port
*port
)
1079 struct bt_component
*comp
;
1080 enum bt_graph_listener_func_status status
= BT_FUNC_STATUS_OK
;
1084 BT_LIB_LOGD("Notifying graph listeners that a port was added: "
1085 "%![graph-]+g, %![port-]+p", graph
, port
);
1086 comp
= bt_port_borrow_component_inline(port
);
1089 switch (comp
->class->type
) {
1090 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1092 switch (port
->type
) {
1093 case BT_PORT_TYPE_OUTPUT
:
1094 listeners
= graph
->listeners
.source_output_port_added
;
1102 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1104 switch (port
->type
) {
1105 case BT_PORT_TYPE_INPUT
:
1106 listeners
= graph
->listeners
.filter_input_port_added
;
1108 case BT_PORT_TYPE_OUTPUT
:
1109 listeners
= graph
->listeners
.filter_output_port_added
;
1117 case BT_COMPONENT_CLASS_TYPE_SINK
:
1119 switch (port
->type
) {
1120 case BT_PORT_TYPE_INPUT
:
1121 listeners
= graph
->listeners
.sink_input_port_added
;
1133 for (i
= 0; i
< listeners
->len
; i
++) {
1134 struct bt_graph_listener_port_added
*listener
=
1135 &g_array_index(listeners
,
1136 struct bt_graph_listener_port_added
, i
);
1139 BT_ASSERT(listener
->func
);
1140 status
= listener
->func(comp
, port
, listener
->base
.data
);
1141 if (status
!= BT_FUNC_STATUS_OK
) {
1151 enum bt_graph_listener_func_status
bt_graph_notify_ports_connected(
1152 struct bt_graph
*graph
, struct bt_port
*upstream_port
,
1153 struct bt_port
*downstream_port
)
1157 struct bt_component
*upstream_comp
;
1158 struct bt_component
*downstream_comp
;
1159 enum bt_graph_listener_func_status status
= BT_FUNC_STATUS_OK
;
1162 BT_ASSERT(upstream_port
);
1163 BT_ASSERT(downstream_port
);
1164 BT_LIB_LOGD("Notifying graph listeners that ports were connected: "
1165 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1166 graph
, upstream_port
, downstream_port
);
1167 upstream_comp
= bt_port_borrow_component_inline(upstream_port
);
1168 BT_ASSERT(upstream_comp
);
1169 downstream_comp
= bt_port_borrow_component_inline(downstream_port
);
1170 BT_ASSERT(downstream_comp
);
1172 switch (upstream_comp
->class->type
) {
1173 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1175 switch (downstream_comp
->class->type
) {
1176 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1178 graph
->listeners
.source_filter_ports_connected
;
1180 case BT_COMPONENT_CLASS_TYPE_SINK
:
1182 graph
->listeners
.source_sink_ports_connected
;
1190 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1192 switch (downstream_comp
->class->type
) {
1193 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1195 graph
->listeners
.filter_filter_ports_connected
;
1197 case BT_COMPONENT_CLASS_TYPE_SINK
:
1199 graph
->listeners
.filter_sink_ports_connected
;
1211 for (i
= 0; i
< listeners
->len
; i
++) {
1212 struct bt_graph_listener_ports_connected
*listener
=
1213 &g_array_index(listeners
,
1214 struct bt_graph_listener_ports_connected
, i
);
1216 BT_ASSERT(listener
->func
);
1217 status
= listener
->func(upstream_comp
, downstream_comp
,
1218 upstream_port
, downstream_port
, listener
->base
.data
);
1219 if (status
!= BT_FUNC_STATUS_OK
) {
1229 void bt_graph_remove_connection(struct bt_graph
*graph
,
1230 struct bt_connection
*connection
)
1233 BT_ASSERT(connection
);
1234 BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1236 g_ptr_array_remove(graph
->connections
, connection
);
1240 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
1242 bool exists
= false;
1245 for (i
= 0; i
< graph
->components
->len
; i
++) {
1246 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
1248 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1249 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1250 "%![other-comp-]+c, name=\"%s\"",
1262 int add_component_with_init_method_data(
1263 struct bt_graph
*graph
,
1264 struct bt_component_class
*comp_cls
,
1265 comp_init_method_t init_method
,
1266 const char *name
, const struct bt_value
*params
,
1267 void *init_method_data
, bt_logging_level log_level
,
1268 struct bt_component
**user_component
)
1270 int status
= BT_FUNC_STATUS_OK
;
1271 enum bt_component_class_init_method_status init_status
;
1272 struct bt_component
*component
= NULL
;
1274 bool init_can_consume
;
1275 struct bt_value
*new_params
= NULL
;
1277 BT_ASSERT(comp_cls
);
1278 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1279 BT_ASSERT_PRE_NON_NULL(name
, "Name");
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
);
1564 bool bt_graph_is_interrupted(const struct bt_graph
*graph
)
1567 return bt_interrupter_array_any_is_set(graph
->interrupters
);
1570 enum bt_graph_add_interrupter_status
bt_graph_add_interrupter(
1571 struct bt_graph
*graph
, const struct bt_interrupter
*intr
)
1573 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1574 BT_ASSERT_PRE_NON_NULL(intr
, "Interrupter");
1575 g_ptr_array_add(graph
->interrupters
, (void *) intr
);
1576 bt_object_get_no_null_check(intr
);
1577 BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z",
1579 return BT_FUNC_STATUS_OK
;
1582 void bt_graph_interrupt(struct bt_graph
*graph
)
1584 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1585 bt_interrupter_set(graph
->default_interrupter
);
1586 BT_LIB_LOGI("Interrupted graph: %!+g", graph
);
1589 void bt_graph_get_ref(const struct bt_graph
*graph
)
1591 bt_object_get_ref(graph
);
1594 void bt_graph_put_ref(const struct bt_graph
*graph
)
1596 bt_object_put_ref(graph
);