4 * Babeltrace Plugin Component Graph
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #define BT_LOG_TAG "GRAPH"
29 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/graph/component-internal.h>
32 #include <babeltrace/graph/graph-internal.h>
33 #include <babeltrace/graph/connection-internal.h>
34 #include <babeltrace/graph/component-sink-internal.h>
35 #include <babeltrace/graph/component-source.h>
36 #include <babeltrace/graph/component-filter.h>
37 #include <babeltrace/graph/port.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/types.h>
40 #include <babeltrace/values.h>
41 #include <babeltrace/values-internal.h>
42 #include <babeltrace/assert-internal.h>
43 #include <babeltrace/assert-pre-internal.h>
47 struct bt_graph_listener
{
49 bt_graph_listener_removed removed
;
54 int init_listeners_array(GArray
**listeners
)
59 *listeners
= g_array_new(FALSE
, TRUE
, sizeof(struct bt_graph_listener
));
61 BT_LOGE_STR("Failed to allocate one GArray.");
71 void call_remove_listeners(GArray
*listeners
)
75 for (i
= 0; i
< listeners
->len
; i
++) {
76 struct bt_graph_listener listener
=
77 g_array_index(listeners
, struct bt_graph_listener
, i
);
79 if (listener
.removed
) {
80 listener
.removed(listener
.data
);
86 void bt_graph_destroy(struct bt_object
*obj
)
88 struct bt_graph
*graph
= container_of(obj
,
89 struct bt_graph
, base
);
92 * The graph's reference count is 0 if we're here. Increment
93 * it to avoid a double-destroy (possibly infinitely recursive)
96 * 1. We put and destroy a connection.
97 * 2. This connection's destructor finalizes its active
98 * notification iterators.
99 * 3. A notification iterator's finalization function gets a
100 * new reference on its component (reference count goes from
102 * 4. Since this component's reference count goes to 1, it takes
103 * a reference on its parent (this graph). This graph's
104 * reference count goes from 0 to 1.
105 * 5. The notification iterator's finalization function puts its
106 * component reference (reference count goes from 1 to 0).
107 * 6. Since this component's reference count goes from 1 to 0,
108 * it puts its parent (this graph). This graph's reference
109 * count goes from 1 to 0.
110 * 7. Since this graph's reference count goes from 1 to 0,
111 * its destructor is called (this function).
113 * With the incrementation below, the graph's reference count at
114 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
115 * ensures that this function is not called two times.
117 BT_LOGD("Destroying graph: addr=%p", graph
);
118 obj
->ref_count
.count
++;
121 * Cancel the graph to disallow some operations, like creating
122 * notification iterators and adding ports to components.
124 (void) bt_graph_cancel(graph
);
126 /* Call all remove listeners */
127 call_remove_listeners(graph
->listeners
.port_added
);
128 call_remove_listeners(graph
->listeners
.port_removed
);
129 call_remove_listeners(graph
->listeners
.ports_connected
);
130 call_remove_listeners(graph
->listeners
.ports_disconnected
);
132 if (graph
->connections
) {
133 BT_LOGD_STR("Destroying connections.");
134 g_ptr_array_free(graph
->connections
, TRUE
);
136 if (graph
->components
) {
137 BT_LOGD_STR("Destroying components.");
138 g_ptr_array_free(graph
->components
, TRUE
);
140 if (graph
->sinks_to_consume
) {
141 g_queue_free(graph
->sinks_to_consume
);
144 if (graph
->listeners
.port_added
) {
145 g_array_free(graph
->listeners
.port_added
, TRUE
);
148 if (graph
->listeners
.port_removed
) {
149 g_array_free(graph
->listeners
.port_removed
, TRUE
);
152 if (graph
->listeners
.ports_connected
) {
153 g_array_free(graph
->listeners
.ports_connected
, TRUE
);
156 if (graph
->listeners
.ports_disconnected
) {
157 g_array_free(graph
->listeners
.ports_disconnected
, TRUE
);
163 struct bt_graph
*bt_graph_create(void)
165 struct bt_graph
*graph
;
168 BT_LOGD_STR("Creating graph object.");
169 graph
= g_new0(struct bt_graph
, 1);
171 BT_LOGE_STR("Failed to allocate one graph.");
175 bt_object_init(graph
, bt_graph_destroy
);
177 graph
->connections
= g_ptr_array_new_with_free_func(bt_object_release
);
178 if (!graph
->connections
) {
179 BT_LOGE_STR("Failed to allocate one GPtrArray.");
182 graph
->components
= g_ptr_array_new_with_free_func(bt_object_release
);
183 if (!graph
->components
) {
184 BT_LOGE_STR("Failed to allocate one GPtrArray.");
187 graph
->sinks_to_consume
= g_queue_new();
188 if (!graph
->sinks_to_consume
) {
189 BT_LOGE_STR("Failed to allocate one GQueue.");
193 graph
->can_consume
= BT_TRUE
;
194 ret
= init_listeners_array(&graph
->listeners
.port_added
);
196 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
200 ret
= init_listeners_array(&graph
->listeners
.port_removed
);
202 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
206 ret
= init_listeners_array(&graph
->listeners
.ports_connected
);
208 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
212 ret
= init_listeners_array(&graph
->listeners
.ports_disconnected
);
214 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
218 BT_LOGD("Created graph object: addr=%p", graph
);
227 enum bt_graph_status
bt_graph_connect_ports(struct bt_graph
*graph
,
228 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
229 struct bt_connection
**user_connection
)
231 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
232 struct bt_connection
*connection
= NULL
;
233 struct bt_graph
*upstream_graph
= NULL
;
234 struct bt_graph
*downstream_graph
= NULL
;
235 struct bt_component
*upstream_component
= NULL
;
236 struct bt_component
*downstream_component
= NULL
;
237 enum bt_component_status component_status
;
238 bt_bool init_can_consume
;
241 BT_LOGW_STR("Invalid parameter: graph is NULL.");
242 status
= BT_GRAPH_STATUS_INVALID
;
245 init_can_consume
= graph
->can_consume
;
247 if (!upstream_port
) {
248 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
249 status
= BT_GRAPH_STATUS_INVALID
;
253 if (!downstream_port
) {
254 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
255 status
= BT_GRAPH_STATUS_INVALID
;
259 BT_LOGD("Connecting component ports within graph: "
261 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
262 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
263 graph
, upstream_port
, bt_port_get_name(upstream_port
),
264 downstream_port
, bt_port_get_name(downstream_port
));
266 if (graph
->canceled
) {
267 BT_LOGW_STR("Invalid parameter: graph is canceled.");
268 status
= BT_GRAPH_STATUS_CANCELED
;
272 graph
->can_consume
= BT_FALSE
;
274 /* Ensure appropriate types for upstream and downstream ports. */
275 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
276 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
277 status
= BT_GRAPH_STATUS_INVALID
;
280 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
281 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
282 status
= BT_GRAPH_STATUS_INVALID
;
286 /* Ensure that both ports are currently unconnected. */
287 if (bt_port_is_connected(upstream_port
)) {
288 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
289 status
= BT_GRAPH_STATUS_INVALID
;
293 if (bt_port_is_connected(downstream_port
)) {
294 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
295 status
= BT_GRAPH_STATUS_INVALID
;
300 * Ensure that both ports are still attached to their creating
303 upstream_component
= bt_port_get_component(upstream_port
);
304 if (!upstream_component
) {
305 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
306 status
= BT_GRAPH_STATUS_INVALID
;
310 downstream_component
= bt_port_get_component(downstream_port
);
311 if (!downstream_component
) {
312 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
313 status
= BT_GRAPH_STATUS_INVALID
;
317 BT_LOGD("Connecting component ports: "
318 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
319 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
320 upstream_component
, bt_component_get_name(upstream_component
),
321 downstream_component
, bt_component_get_name(downstream_component
));
324 * At this point the ports are not connected yet. Both
325 * components need to accept an eventual connection to their
326 * port by the other port before we continue.
328 BT_LOGD_STR("Asking upstream component to accept the connection.");
329 component_status
= bt_component_accept_port_connection(
330 upstream_component
, upstream_port
, downstream_port
);
331 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
332 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
333 BT_LOGD_STR("Upstream component refused the connection.");
335 BT_LOGW("Cannot ask upstream component to accept the connection: "
336 "status=%s", bt_component_status_string(component_status
));
339 status
= bt_graph_status_from_component_status(
344 BT_LOGD_STR("Asking downstream component to accept the connection.");
345 component_status
= bt_component_accept_port_connection(
346 downstream_component
, downstream_port
, upstream_port
);
347 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
348 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
349 BT_LOGD_STR("Downstream component refused the connection.");
351 BT_LOGW("Cannot ask downstream component to accept the connection: "
352 "status=%s", bt_component_status_string(component_status
));
355 status
= bt_graph_status_from_component_status(
360 BT_LOGD_STR("Creating connection.");
361 connection
= bt_connection_create(graph
, upstream_port
,
364 BT_LOGW("Cannot create connection object.");
365 status
= BT_GRAPH_STATUS_NOMEM
;
369 BT_LOGD("Connection object created: conn-addr=%p", connection
);
372 * Ownership of upstream_component/downstream_component and of
373 * the connection object is transferred to the graph.
375 g_ptr_array_add(graph
->connections
, connection
);
378 * Notify both components that their port is connected.
380 BT_LOGD_STR("Notifying upstream component that its port is connected.");
381 bt_component_port_connected(upstream_component
, upstream_port
,
383 BT_LOGD_STR("Notifying downstream component that its port is connected.");
384 bt_component_port_connected(downstream_component
, downstream_port
,
388 * Notify the graph's creator that both ports are connected.
390 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
391 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
392 BT_LOGD("Connected component ports within graph: "
394 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
395 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
396 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
397 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
399 upstream_component
, bt_component_get_name(upstream_component
),
400 downstream_component
, bt_component_get_name(downstream_component
),
401 upstream_port
, bt_port_get_name(upstream_port
),
402 downstream_port
, bt_port_get_name(downstream_port
));
404 if (user_connection
) {
405 /* Move reference to user */
406 *user_connection
= connection
;
411 bt_put(upstream_graph
);
412 bt_put(downstream_graph
);
413 bt_put(upstream_component
);
414 bt_put(downstream_component
);
417 graph
->can_consume
= init_can_consume
;
423 enum bt_graph_status
consume_graph_sink(struct bt_component
*sink
)
425 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
426 enum bt_component_status comp_status
;
429 comp_status
= bt_component_sink_consume(sink
);
430 BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s",
431 sink
, bt_component_get_name(sink
),
432 bt_component_status_string(comp_status
));
434 switch (comp_status
) {
435 case BT_COMPONENT_STATUS_OK
:
437 case BT_COMPONENT_STATUS_END
:
438 status
= BT_GRAPH_STATUS_END
;
440 case BT_COMPONENT_STATUS_AGAIN
:
441 status
= BT_GRAPH_STATUS_AGAIN
;
443 case BT_COMPONENT_STATUS_INVALID
:
444 status
= BT_GRAPH_STATUS_INVALID
;
447 status
= BT_GRAPH_STATUS_ERROR
;
455 * `node` is removed from the queue of sinks to consume when passed to
456 * this function. This function adds it back to the queue if there's
457 * still something to consume afterwards.
460 enum bt_graph_status
consume_sink_node(struct bt_graph
*graph
,
463 enum bt_graph_status status
;
464 struct bt_component
*sink
;
467 status
= consume_graph_sink(sink
);
468 if (status
!= BT_GRAPH_STATUS_END
) {
469 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
473 /* End reached, the node is not added back to the queue and free'd. */
474 g_queue_delete_link(graph
->sinks_to_consume
, node
);
476 /* Don't forward an END status if there are sinks left to consume. */
477 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
478 status
= BT_GRAPH_STATUS_OK
;
483 BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status
));
488 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
489 struct bt_component
*sink
)
491 enum bt_graph_status status
;
495 BT_LOGV("Making specific sink consume: addr=%p, "
496 "comp-addr=%p, comp-name=\"%s\"",
497 graph
, sink
, bt_component_get_name(sink
));
499 BT_ASSERT(bt_component_borrow_graph(sink
) == graph
);
501 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
502 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
503 status
= BT_GRAPH_STATUS_END
;
507 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
509 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
510 status
= BT_GRAPH_STATUS_END
;
514 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
515 BT_ASSERT(sink_node
);
516 status
= consume_sink_node(graph
, sink_node
);
523 enum bt_graph_status
bt_graph_consume_no_check(struct bt_graph
*graph
)
525 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
526 struct bt_component
*sink
;
529 BT_LOGV("Making next sink consume: addr=%p", graph
);
530 BT_ASSERT_PRE(graph
->has_sink
,
531 "Graph has no sink component: %!+g", graph
);
533 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
534 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
535 status
= BT_GRAPH_STATUS_END
;
539 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
540 sink
= current_node
->data
;
541 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
542 sink
, bt_component_get_name(sink
));
543 status
= consume_sink_node(graph
, current_node
);
549 enum bt_graph_status
bt_graph_consume(struct bt_graph
*graph
)
551 enum bt_graph_status status
;
553 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
554 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
555 BT_ASSERT_PRE(graph
->can_consume
,
556 "Cannot consume graph in its current state: %!+g", graph
);
557 graph
->can_consume
= BT_FALSE
;
558 status
= bt_graph_consume_no_check(graph
);
559 graph
->can_consume
= BT_TRUE
;
560 return BT_GRAPH_STATUS_OK
;
563 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
565 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
568 BT_LOGW_STR("Invalid parameter: graph is NULL.");
569 status
= BT_GRAPH_STATUS_INVALID
;
573 if (graph
->canceled
) {
574 BT_LOGW("Invalid parameter: graph is canceled: "
575 "graph-addr=%p", graph
);
576 status
= BT_GRAPH_STATUS_CANCELED
;
580 if (!graph
->can_consume
) {
581 BT_LOGW_STR("Cannot run graph in its current state.");
582 status
= BT_GRAPH_STATUS_CANNOT_CONSUME
;
586 graph
->can_consume
= BT_FALSE
;
587 BT_LOGV("Running graph: addr=%p", graph
);
591 * Check if the graph is canceled at each iteration. If
592 * the graph was canceled by another thread or by a
593 * signal, this is not a warning nor an error, it was
594 * intentional: log with a DEBUG level only.
596 if (graph
->canceled
) {
597 BT_LOGD("Stopping the graph: graph is canceled: "
598 "graph-addr=%p", graph
);
599 status
= BT_GRAPH_STATUS_CANCELED
;
603 status
= bt_graph_consume_no_check(graph
);
604 if (status
== BT_GRAPH_STATUS_AGAIN
) {
606 * If AGAIN is received and there are multiple
607 * sinks, go ahead and consume from the next
610 * However, in the case where a single sink is
611 * left, the caller can decide to busy-wait and
612 * call bt_graph_run() continuously until the
613 * source is ready or it can decide to sleep for
614 * an arbitrary amount of time.
616 if (graph
->sinks_to_consume
->length
> 1) {
617 status
= BT_GRAPH_STATUS_OK
;
619 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
622 } while (status
== BT_GRAPH_STATUS_OK
);
624 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
625 status
= BT_GRAPH_STATUS_END
;
629 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status
));
631 graph
->can_consume
= BT_TRUE
;
637 int add_listener(GArray
*listeners
, void *func
, void *removed
, void *data
)
639 struct bt_graph_listener listener
= {
645 g_array_append_val(listeners
, listener
);
646 return listeners
->len
- 1;
649 int bt_graph_add_port_added_listener(
650 struct bt_graph
*graph
,
651 bt_graph_port_added_listener listener
,
652 bt_graph_listener_removed listener_removed
, void *data
)
657 BT_LOGW_STR("Invalid parameter: graph is NULL.");
662 if (graph
->in_remove_listener
) {
663 BT_LOGW("Cannot call this function during the execution of a remove listener: "
670 BT_LOGW_STR("Invalid parameter: listener is NULL.");
675 ret
= add_listener(graph
->listeners
.port_added
, listener
,
676 listener_removed
, data
);
677 BT_LOGV("Added \"port added\" listener to graph: "
678 "graph-addr=%p, listener-addr=%p, pos=%d",
679 graph
, listener
, ret
);
685 int bt_graph_add_port_removed_listener(
686 struct bt_graph
*graph
,
687 bt_graph_port_removed_listener listener
,
688 bt_graph_listener_removed listener_removed
, void *data
)
693 BT_LOGW_STR("Invalid parameter: graph is NULL.");
698 if (graph
->in_remove_listener
) {
699 BT_LOGW("Cannot call this function during the execution of a remove listener: "
706 BT_LOGW_STR("Invalid parameter: listener is NULL.");
711 ret
= add_listener(graph
->listeners
.port_removed
, listener
,
712 listener_removed
, data
);
713 BT_LOGV("Added \"port removed\" listener to graph: "
714 "graph-addr=%p, listener-addr=%p, pos=%d",
715 graph
, listener
, ret
);
721 int bt_graph_add_ports_connected_listener(
722 struct bt_graph
*graph
,
723 bt_graph_ports_connected_listener listener
,
724 bt_graph_listener_removed listener_removed
, void *data
)
729 BT_LOGW_STR("Invalid parameter: graph is NULL.");
734 if (graph
->in_remove_listener
) {
735 BT_LOGW("Cannot call this function during the execution of a remove listener: "
742 BT_LOGW_STR("Invalid parameter: listener is NULL.");
747 ret
= add_listener(graph
->listeners
.ports_connected
, listener
,
748 listener_removed
, data
);
749 BT_LOGV("Added \"port connected\" listener to graph: "
750 "graph-addr=%p, listener-addr=%p, pos=%d",
751 graph
, listener
, ret
);
757 int bt_graph_add_ports_disconnected_listener(
758 struct bt_graph
*graph
,
759 bt_graph_ports_disconnected_listener listener
,
760 bt_graph_listener_removed listener_removed
, void *data
)
765 BT_LOGW_STR("Invalid parameter: graph is NULL.");
770 if (graph
->in_remove_listener
) {
771 BT_LOGW("Cannot call this function during the execution of a remove listener: "
778 BT_LOGW_STR("Invalid parameter: listener is NULL.");
783 ret
= add_listener(graph
->listeners
.ports_disconnected
, listener
,
784 listener_removed
, data
);
785 BT_LOGV("Added \"port disconnected\" listener to graph: "
786 "graph-addr=%p, listener-addr=%p, pos=%d",
787 graph
, listener
, ret
);
794 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
798 BT_LOGV("Notifying graph listeners that a port was added: "
799 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
800 graph
, port
, bt_port_get_name(port
));
802 for (i
= 0; i
< graph
->listeners
.port_added
->len
; i
++) {
803 struct bt_graph_listener listener
=
804 g_array_index(graph
->listeners
.port_added
,
805 struct bt_graph_listener
, i
);
806 bt_graph_port_added_listener func
= listener
.func
;
809 func(port
, listener
.data
);
814 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
815 struct bt_component
*comp
, struct bt_port
*port
)
819 BT_LOGV("Notifying graph listeners that a port was removed: "
820 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
821 graph
, port
, bt_port_get_name(port
));
823 for (i
= 0; i
< graph
->listeners
.port_removed
->len
; i
++) {
824 struct bt_graph_listener listener
=
825 g_array_index(graph
->listeners
.port_removed
,
826 struct bt_graph_listener
, i
);
827 bt_graph_port_removed_listener func
= listener
.func
;
830 func(comp
, port
, listener
.data
);
835 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
836 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
840 BT_LOGV("Notifying graph listeners that two ports were connected: "
842 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
843 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
844 graph
, upstream_port
, bt_port_get_name(upstream_port
),
845 downstream_port
, bt_port_get_name(downstream_port
));
847 for (i
= 0; i
< graph
->listeners
.ports_connected
->len
; i
++) {
848 struct bt_graph_listener listener
=
849 g_array_index(graph
->listeners
.ports_connected
,
850 struct bt_graph_listener
, i
);
851 bt_graph_ports_connected_listener func
= listener
.func
;
854 func(upstream_port
, downstream_port
, listener
.data
);
859 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
860 struct bt_component
*upstream_comp
,
861 struct bt_component
*downstream_comp
,
862 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
866 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
868 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
869 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
870 graph
, upstream_port
, bt_port_get_name(upstream_port
),
871 downstream_port
, bt_port_get_name(downstream_port
));
873 for (i
= 0; i
< graph
->listeners
.ports_disconnected
->len
; i
++) {
874 struct bt_graph_listener listener
=
875 g_array_index(graph
->listeners
.ports_disconnected
,
876 struct bt_graph_listener
, i
);
877 bt_graph_ports_disconnected_listener func
= listener
.func
;
880 func(upstream_comp
, downstream_comp
, upstream_port
,
881 downstream_port
, listener
.data
);
885 enum bt_graph_status
bt_graph_cancel(struct bt_graph
*graph
)
887 enum bt_graph_status ret
= BT_GRAPH_STATUS_OK
;
890 BT_LOGW_STR("Invalid parameter: graph is NULL.");
891 ret
= BT_GRAPH_STATUS_INVALID
;
895 graph
->canceled
= BT_TRUE
;
896 BT_LOGV("Canceled graph: addr=%p", graph
);
902 bt_bool
bt_graph_is_canceled(struct bt_graph
*graph
)
904 bt_bool canceled
= BT_FALSE
;
907 BT_LOGW_STR("Invalid parameter: graph is NULL.");
911 canceled
= graph
->canceled
;
918 void bt_graph_remove_connection(struct bt_graph
*graph
,
919 struct bt_connection
*connection
)
922 BT_ASSERT(connection
);
923 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
925 g_ptr_array_remove(graph
->connections
, connection
);
928 enum bt_graph_status
bt_graph_add_component_with_init_method_data(
929 struct bt_graph
*graph
,
930 struct bt_component_class
*component_class
,
931 const char *name
, struct bt_value
*params
,
932 void *init_method_data
,
933 struct bt_component
**user_component
)
935 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
936 enum bt_component_status comp_status
;
937 struct bt_component
*component
= NULL
;
938 enum bt_component_class_type type
;
940 bt_bool init_can_consume
;
945 BT_LOGW_STR("Invalid parameter: graph is NULL.");
946 graph_status
= BT_GRAPH_STATUS_INVALID
;
949 init_can_consume
= graph
->can_consume
;
951 if (!component_class
) {
952 BT_LOGW_STR("Invalid parameter: component class is NULL.");
953 graph_status
= BT_GRAPH_STATUS_INVALID
;
957 graph
->can_consume
= BT_FALSE
;
958 type
= bt_component_class_get_type(component_class
);
959 BT_LOGD("Adding component to graph: "
960 "graph-addr=%p, comp-cls-addr=%p, "
961 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
962 "init-method-data-addr=%p",
963 graph
, component_class
, bt_component_class_type_string(type
),
964 name
, params
, init_method_data
);
967 BT_LOGW_STR("Invalid parameter: name is NULL.");
968 graph_status
= BT_GRAPH_STATUS_INVALID
;
972 if (graph
->canceled
) {
973 BT_LOGW_STR("Invalid parameter: graph is canceled.");
974 graph_status
= BT_GRAPH_STATUS_CANCELED
;
978 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
979 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
980 type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
981 BT_LOGW("Invalid parameter: unknown component class type: "
983 graph_status
= BT_GRAPH_STATUS_INVALID
;
987 for (i
= 0; i
< graph
->components
->len
; i
++) {
988 void *other_comp
= graph
->components
->pdata
[i
];
990 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
991 BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
992 "other-comp-addr=%p, name=\"%s\"",
994 graph_status
= BT_GRAPH_STATUS_INVALID
;
1000 * Parameters must be a map value, but we create a convenient
1001 * empty one if it's NULL.
1004 if (!bt_value_is_map(params
)) {
1005 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
1007 bt_value_type_string(bt_value_get_type(params
)));
1008 graph_status
= BT_GRAPH_STATUS_INVALID
;
1012 params
= bt_value_map_create();
1014 BT_LOGE_STR("Cannot create map value object.");
1015 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1020 comp_status
= bt_component_create(component_class
, name
, &component
);
1021 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
1022 BT_LOGE("Cannot create empty component object: status=%s",
1023 bt_component_status_string(comp_status
));
1024 graph_status
= bt_graph_status_from_component_status(
1030 * The user's initialization method needs to see that this
1031 * component is part of the graph. If the user method fails, we
1032 * immediately remove the component from the graph's components.
1034 g_ptr_array_add(graph
->components
, component
);
1035 bt_component_set_graph(component
, graph
);
1037 if (component_class
->methods
.init
) {
1038 BT_LOGD_STR("Calling user's initialization method.");
1039 comp_status
= component_class
->methods
.init(
1040 bt_private_component_from_component(component
), params
,
1042 BT_LOGD("User method returned: status=%s",
1043 bt_component_status_string(comp_status
));
1044 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
1045 BT_LOGW_STR("Initialization method failed.");
1046 graph_status
= bt_graph_status_from_component_status(
1048 bt_component_set_graph(component
, NULL
);
1049 g_ptr_array_remove_fast(graph
->components
, component
);
1055 * Mark the component as initialized so that its finalization
1056 * method is called when it is destroyed.
1058 component
->initialized
= true;
1061 * If it's a sink component, it needs to be part of the graph's
1062 * sink queue to be consumed by bt_graph_consume().
1064 if (bt_component_is_sink(component
)) {
1065 graph
->has_sink
= BT_TRUE
;
1066 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1070 * Freeze the component class now that it's instantiated at
1073 BT_LOGD_STR("Freezing component class.");
1074 bt_component_class_freeze(component
->class);
1075 BT_LOGD("Added component to graph: "
1076 "graph-addr=%p, comp-cls-addr=%p, "
1077 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
1078 "init-method-data-addr=%p, comp-addr=%p",
1079 graph
, component_class
, bt_component_class_type_string(type
),
1080 name
, params
, init_method_data
, component
);
1082 if (user_component
) {
1083 /* Move reference to user */
1084 *user_component
= component
;
1092 graph
->can_consume
= init_can_consume
;
1094 return graph_status
;
1097 enum bt_graph_status
bt_graph_add_component(
1098 struct bt_graph
*graph
,
1099 struct bt_component_class
*component_class
,
1100 const char *name
, struct bt_value
*params
,
1101 struct bt_component
**component
)
1103 return bt_graph_add_component_with_init_method_data(graph
,
1104 component_class
, name
, params
, NULL
, component
);
1108 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1109 struct bt_component
*component
)
1111 bt_bool init_can_consume
;
1117 BT_ASSERT(component
);
1118 BT_ASSERT(component
->base
.ref_count
.count
== 0);
1119 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1121 init_can_consume
= graph
->can_consume
;
1122 count
= bt_component_get_input_port_count(component
);
1124 for (i
= 0; i
< count
; i
++) {
1125 struct bt_port
*port
=
1126 bt_component_get_input_port_by_index(component
, i
);
1131 if (bt_port_is_connected(port
)) {
1132 BT_LOGW("Cannot remove component from graph: "
1133 "an input port is connected: "
1134 "graph-addr=%p, comp-addr=%p, "
1135 "comp-name=\"%s\", connected-port-addr=%p, "
1136 "connected-port-name=\"%s\"",
1138 bt_component_get_name(component
),
1139 port
, bt_port_get_name(port
));
1144 count
= bt_component_get_output_port_count(component
);
1146 for (i
= 0; i
< count
; i
++) {
1147 struct bt_port
*port
=
1148 bt_component_get_output_port_by_index(component
, i
);
1153 if (bt_port_is_connected(port
)) {
1154 BT_LOGW("Cannot remove component from graph: "
1155 "an output port is connected: "
1156 "graph-addr=%p, comp-addr=%p, "
1157 "comp-name=\"%s\", connected-port-addr=%p, "
1158 "connected-port-name=\"%s\"",
1160 bt_component_get_name(component
),
1161 port
, bt_port_get_name(port
));
1166 graph
->can_consume
= BT_FALSE
;
1168 /* Possibly remove from sinks to consume */
1169 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1171 if (graph
->sinks_to_consume
->length
== 0) {
1172 graph
->has_sink
= BT_FALSE
;
1176 * This calls bt_object_release() on the component, and since
1177 * its reference count is 0, its destructor is called. Its
1178 * destructor calls the user's finalization method (if set).
1180 g_ptr_array_remove(graph
->components
, component
);
1187 graph
->can_consume
= init_can_consume
;