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>
45 struct bt_graph_listener
{
47 bt_graph_listener_removed removed
;
52 int init_listeners_array(GArray
**listeners
)
57 *listeners
= g_array_new(FALSE
, TRUE
, sizeof(struct bt_graph_listener
));
59 BT_LOGE_STR("Failed to allocate one GArray.");
69 void call_remove_listeners(GArray
*listeners
)
73 for (i
= 0; i
< listeners
->len
; i
++) {
74 struct bt_graph_listener listener
=
75 g_array_index(listeners
, struct bt_graph_listener
, i
);
77 if (listener
.removed
) {
78 listener
.removed(listener
.data
);
84 void bt_graph_destroy(struct bt_object
*obj
)
86 struct bt_graph
*graph
= container_of(obj
,
87 struct bt_graph
, base
);
90 * The graph's reference count is 0 if we're here. Increment
91 * it to avoid a double-destroy (possibly infinitely recursive)
94 * 1. We put and destroy a connection.
95 * 2. This connection's destructor finalizes its active
96 * notification iterators.
97 * 3. A notification iterator's finalization function gets a
98 * new reference on its component (reference count goes from
100 * 4. Since this component's reference count goes to 1, it takes
101 * a reference on its parent (this graph). This graph's
102 * reference count goes from 0 to 1.
103 * 5. The notification iterator's finalization function puts its
104 * component reference (reference count goes from 1 to 0).
105 * 6. Since this component's reference count goes from 1 to 0,
106 * it puts its parent (this graph). This graph's reference
107 * count goes from 1 to 0.
108 * 7. Since this graph's reference count goes from 1 to 0,
109 * its destructor is called (this function).
111 * With the incrementation below, the graph's reference count at
112 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
113 * ensures that this function is not called two times.
115 BT_LOGD("Destroying graph: addr=%p", graph
);
116 obj
->ref_count
.count
++;
119 * Cancel the graph to disallow some operations, like creating
120 * notification iterators and adding ports to components.
122 (void) bt_graph_cancel(graph
);
124 /* Call all remove listeners */
125 call_remove_listeners(graph
->listeners
.port_added
);
126 call_remove_listeners(graph
->listeners
.port_removed
);
127 call_remove_listeners(graph
->listeners
.ports_connected
);
128 call_remove_listeners(graph
->listeners
.ports_disconnected
);
130 if (graph
->connections
) {
131 BT_LOGD_STR("Destroying connections.");
132 g_ptr_array_free(graph
->connections
, TRUE
);
134 if (graph
->components
) {
135 BT_LOGD_STR("Destroying components.");
136 g_ptr_array_free(graph
->components
, TRUE
);
138 if (graph
->sinks_to_consume
) {
139 g_queue_free(graph
->sinks_to_consume
);
142 if (graph
->listeners
.port_added
) {
143 g_array_free(graph
->listeners
.port_added
, TRUE
);
146 if (graph
->listeners
.port_removed
) {
147 g_array_free(graph
->listeners
.port_removed
, TRUE
);
150 if (graph
->listeners
.ports_connected
) {
151 g_array_free(graph
->listeners
.ports_connected
, TRUE
);
154 if (graph
->listeners
.ports_disconnected
) {
155 g_array_free(graph
->listeners
.ports_disconnected
, TRUE
);
161 struct bt_graph
*bt_graph_create(void)
163 struct bt_graph
*graph
;
166 BT_LOGD_STR("Creating graph object.");
167 graph
= g_new0(struct bt_graph
, 1);
169 BT_LOGE_STR("Failed to allocate one graph.");
173 bt_object_init(graph
, bt_graph_destroy
);
175 graph
->connections
= g_ptr_array_new_with_free_func(bt_object_release
);
176 if (!graph
->connections
) {
177 BT_LOGE_STR("Failed to allocate one GPtrArray.");
180 graph
->components
= g_ptr_array_new_with_free_func(bt_object_release
);
181 if (!graph
->components
) {
182 BT_LOGE_STR("Failed to allocate one GPtrArray.");
185 graph
->sinks_to_consume
= g_queue_new();
186 if (!graph
->sinks_to_consume
) {
187 BT_LOGE_STR("Failed to allocate one GQueue.");
191 graph
->can_consume
= BT_TRUE
;
192 ret
= init_listeners_array(&graph
->listeners
.port_added
);
194 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
198 ret
= init_listeners_array(&graph
->listeners
.port_removed
);
200 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
204 ret
= init_listeners_array(&graph
->listeners
.ports_connected
);
206 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
210 ret
= init_listeners_array(&graph
->listeners
.ports_disconnected
);
212 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
216 BT_LOGD("Created graph object: addr=%p", graph
);
225 enum bt_graph_status
bt_graph_connect_ports(struct bt_graph
*graph
,
226 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
227 struct bt_connection
**user_connection
)
229 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
230 struct bt_connection
*connection
= NULL
;
231 struct bt_graph
*upstream_graph
= NULL
;
232 struct bt_graph
*downstream_graph
= NULL
;
233 struct bt_component
*upstream_component
= NULL
;
234 struct bt_component
*downstream_component
= NULL
;
235 enum bt_component_status component_status
;
236 bt_bool init_can_consume
;
239 BT_LOGW_STR("Invalid parameter: graph is NULL.");
240 status
= BT_GRAPH_STATUS_INVALID
;
243 init_can_consume
= graph
->can_consume
;
245 if (!upstream_port
) {
246 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
247 status
= BT_GRAPH_STATUS_INVALID
;
251 if (!downstream_port
) {
252 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
253 status
= BT_GRAPH_STATUS_INVALID
;
257 BT_LOGD("Connecting component ports within graph: "
259 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
260 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
261 graph
, upstream_port
, bt_port_get_name(upstream_port
),
262 downstream_port
, bt_port_get_name(downstream_port
));
264 if (graph
->canceled
) {
265 BT_LOGW_STR("Invalid parameter: graph is canceled.");
266 status
= BT_GRAPH_STATUS_CANCELED
;
270 graph
->can_consume
= BT_FALSE
;
272 /* Ensure appropriate types for upstream and downstream ports. */
273 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
274 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
275 status
= BT_GRAPH_STATUS_INVALID
;
278 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
279 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
280 status
= BT_GRAPH_STATUS_INVALID
;
284 /* Ensure that both ports are currently unconnected. */
285 if (bt_port_is_connected(upstream_port
)) {
286 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
287 status
= BT_GRAPH_STATUS_INVALID
;
291 if (bt_port_is_connected(downstream_port
)) {
292 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
293 status
= BT_GRAPH_STATUS_INVALID
;
298 * Ensure that both ports are still attached to their creating
301 upstream_component
= bt_port_get_component(upstream_port
);
302 if (!upstream_component
) {
303 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
304 status
= BT_GRAPH_STATUS_INVALID
;
308 downstream_component
= bt_port_get_component(downstream_port
);
309 if (!downstream_component
) {
310 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
311 status
= BT_GRAPH_STATUS_INVALID
;
315 BT_LOGD("Connecting component ports: "
316 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
317 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
318 upstream_component
, bt_component_get_name(upstream_component
),
319 downstream_component
, bt_component_get_name(downstream_component
));
322 * At this point the ports are not connected yet. Both
323 * components need to accept an eventual connection to their
324 * port by the other port before we continue.
326 BT_LOGD_STR("Asking upstream component to accept the connection.");
327 component_status
= bt_component_accept_port_connection(
328 upstream_component
, upstream_port
, downstream_port
);
329 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
330 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
331 BT_LOGD_STR("Upstream component refused the connection.");
333 BT_LOGW("Cannot ask upstream component to accept the connection: "
334 "status=%s", bt_component_status_string(component_status
));
337 status
= bt_graph_status_from_component_status(
342 BT_LOGD_STR("Asking downstream component to accept the connection.");
343 component_status
= bt_component_accept_port_connection(
344 downstream_component
, downstream_port
, upstream_port
);
345 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
346 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
347 BT_LOGD_STR("Downstream component refused the connection.");
349 BT_LOGW("Cannot ask downstream component to accept the connection: "
350 "status=%s", bt_component_status_string(component_status
));
353 status
= bt_graph_status_from_component_status(
358 BT_LOGD_STR("Creating connection.");
359 connection
= bt_connection_create(graph
, upstream_port
,
362 BT_LOGW("Cannot create connection object.");
363 status
= BT_GRAPH_STATUS_NOMEM
;
367 BT_LOGD("Connection object created: conn-addr=%p", connection
);
370 * Ownership of upstream_component/downstream_component and of
371 * the connection object is transferred to the graph.
373 g_ptr_array_add(graph
->connections
, connection
);
376 * Notify both components that their port is connected.
378 BT_LOGD_STR("Notifying upstream component that its port is connected.");
379 bt_component_port_connected(upstream_component
, upstream_port
,
381 BT_LOGD_STR("Notifying downstream component that its port is connected.");
382 bt_component_port_connected(downstream_component
, downstream_port
,
386 * Notify the graph's creator that both ports are connected.
388 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
389 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
390 BT_LOGD("Connected component ports within graph: "
392 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
393 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
394 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
395 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
397 upstream_component
, bt_component_get_name(upstream_component
),
398 downstream_component
, bt_component_get_name(downstream_component
),
399 upstream_port
, bt_port_get_name(upstream_port
),
400 downstream_port
, bt_port_get_name(downstream_port
));
402 if (user_connection
) {
403 /* Move reference to user */
404 *user_connection
= connection
;
409 bt_put(upstream_graph
);
410 bt_put(downstream_graph
);
411 bt_put(upstream_component
);
412 bt_put(downstream_component
);
415 graph
->can_consume
= init_can_consume
;
421 enum bt_graph_status
consume_graph_sink(struct bt_component
*sink
)
423 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
424 enum bt_component_status comp_status
;
427 comp_status
= bt_component_sink_consume(sink
);
428 BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s",
429 sink
, bt_component_get_name(sink
),
430 bt_component_status_string(comp_status
));
432 switch (comp_status
) {
433 case BT_COMPONENT_STATUS_OK
:
435 case BT_COMPONENT_STATUS_END
:
436 status
= BT_GRAPH_STATUS_END
;
438 case BT_COMPONENT_STATUS_AGAIN
:
439 status
= BT_GRAPH_STATUS_AGAIN
;
441 case BT_COMPONENT_STATUS_INVALID
:
442 status
= BT_GRAPH_STATUS_INVALID
;
445 status
= BT_GRAPH_STATUS_ERROR
;
453 * `node` is removed from the queue of sinks to consume when passed to
454 * this function. This function adds it back to the queue if there's
455 * still something to consume afterwards.
458 enum bt_graph_status
consume_sink_node(struct bt_graph
*graph
,
461 enum bt_graph_status status
;
462 struct bt_component
*sink
;
465 status
= consume_graph_sink(sink
);
466 if (status
!= BT_GRAPH_STATUS_END
) {
467 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
471 /* End reached, the node is not added back to the queue and free'd. */
472 g_queue_delete_link(graph
->sinks_to_consume
, node
);
474 /* Don't forward an END status if there are sinks left to consume. */
475 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
476 status
= BT_GRAPH_STATUS_OK
;
481 BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status
));
486 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
487 struct bt_component
*sink
)
489 enum bt_graph_status status
;
493 BT_LOGV("Making specific sink consume: addr=%p, "
494 "comp-addr=%p, comp-name=\"%s\"",
495 graph
, sink
, bt_component_get_name(sink
));
497 assert(bt_component_borrow_graph(sink
) == graph
);
499 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
500 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
501 status
= BT_GRAPH_STATUS_END
;
505 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
507 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
508 status
= BT_GRAPH_STATUS_END
;
512 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
514 status
= consume_sink_node(graph
, sink_node
);
521 enum bt_graph_status
bt_graph_consume_no_check(struct bt_graph
*graph
)
523 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
524 struct bt_component
*sink
;
527 BT_LOGV("Making next sink consume: addr=%p", graph
);
529 if (!graph
->has_sink
) {
530 BT_LOGW_STR("Graph has no sink component.");
531 status
= BT_GRAPH_STATUS_NO_SINK
;
535 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
536 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
537 status
= BT_GRAPH_STATUS_END
;
541 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
542 sink
= current_node
->data
;
543 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
544 sink
, bt_component_get_name(sink
));
545 status
= consume_sink_node(graph
, current_node
);
551 enum bt_graph_status
bt_graph_consume(struct bt_graph
*graph
)
553 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
556 BT_LOGW_STR("Invalid parameter: graph is NULL.");
557 status
= BT_GRAPH_STATUS_INVALID
;
561 if (graph
->canceled
) {
562 BT_LOGW("Invalid parameter: graph is canceled: "
563 "graph-addr=%p", graph
);
564 status
= BT_GRAPH_STATUS_CANCELED
;
568 if (!graph
->can_consume
) {
569 BT_LOGW_STR("Cannot consume graph in its current state.");
570 status
= BT_GRAPH_STATUS_CANNOT_CONSUME
;
574 graph
->can_consume
= BT_FALSE
;
575 status
= bt_graph_consume_no_check(graph
);
576 graph
->can_consume
= BT_TRUE
;
582 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
584 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
587 BT_LOGW_STR("Invalid parameter: graph is NULL.");
588 status
= BT_GRAPH_STATUS_INVALID
;
592 if (graph
->canceled
) {
593 BT_LOGW("Invalid parameter: graph is canceled: "
594 "graph-addr=%p", graph
);
595 status
= BT_GRAPH_STATUS_CANCELED
;
599 if (!graph
->can_consume
) {
600 BT_LOGW_STR("Cannot run graph in its current state.");
601 status
= BT_GRAPH_STATUS_CANNOT_CONSUME
;
605 graph
->can_consume
= BT_FALSE
;
606 BT_LOGV("Running graph: addr=%p", graph
);
610 * Check if the graph is canceled at each iteration. If
611 * the graph was canceled by another thread or by a
612 * signal, this is not a warning nor an error, it was
613 * intentional: log with a DEBUG level only.
615 if (graph
->canceled
) {
616 BT_LOGD("Stopping the graph: graph is canceled: "
617 "graph-addr=%p", graph
);
618 status
= BT_GRAPH_STATUS_CANCELED
;
622 status
= bt_graph_consume_no_check(graph
);
623 if (status
== BT_GRAPH_STATUS_AGAIN
) {
625 * If AGAIN is received and there are multiple
626 * sinks, go ahead and consume from the next
629 * However, in the case where a single sink is
630 * left, the caller can decide to busy-wait and
631 * call bt_graph_run() continuously until the
632 * source is ready or it can decide to sleep for
633 * an arbitrary amount of time.
635 if (graph
->sinks_to_consume
->length
> 1) {
636 status
= BT_GRAPH_STATUS_OK
;
638 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
641 } while (status
== BT_GRAPH_STATUS_OK
);
643 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
644 status
= BT_GRAPH_STATUS_END
;
648 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status
));
650 graph
->can_consume
= BT_TRUE
;
656 int add_listener(GArray
*listeners
, void *func
, void *removed
, void *data
)
658 struct bt_graph_listener listener
= {
664 g_array_append_val(listeners
, listener
);
665 return listeners
->len
- 1;
668 int bt_graph_add_port_added_listener(
669 struct bt_graph
*graph
,
670 bt_graph_port_added_listener listener
,
671 bt_graph_listener_removed listener_removed
, void *data
)
676 BT_LOGW_STR("Invalid parameter: graph is NULL.");
681 if (graph
->in_remove_listener
) {
682 BT_LOGW("Cannot call this function during the execution of a remove listener: "
689 BT_LOGW_STR("Invalid parameter: listener is NULL.");
694 ret
= add_listener(graph
->listeners
.port_added
, listener
,
695 listener_removed
, data
);
696 BT_LOGV("Added \"port added\" listener to graph: "
697 "graph-addr=%p, listener-addr=%p, pos=%d",
698 graph
, listener
, ret
);
704 int bt_graph_add_port_removed_listener(
705 struct bt_graph
*graph
,
706 bt_graph_port_removed_listener listener
,
707 bt_graph_listener_removed listener_removed
, void *data
)
712 BT_LOGW_STR("Invalid parameter: graph is NULL.");
717 if (graph
->in_remove_listener
) {
718 BT_LOGW("Cannot call this function during the execution of a remove listener: "
725 BT_LOGW_STR("Invalid parameter: listener is NULL.");
730 ret
= add_listener(graph
->listeners
.port_removed
, listener
,
731 listener_removed
, data
);
732 BT_LOGV("Added \"port removed\" listener to graph: "
733 "graph-addr=%p, listener-addr=%p, pos=%d",
734 graph
, listener
, ret
);
740 int bt_graph_add_ports_connected_listener(
741 struct bt_graph
*graph
,
742 bt_graph_ports_connected_listener listener
,
743 bt_graph_listener_removed listener_removed
, void *data
)
748 BT_LOGW_STR("Invalid parameter: graph is NULL.");
753 if (graph
->in_remove_listener
) {
754 BT_LOGW("Cannot call this function during the execution of a remove listener: "
761 BT_LOGW_STR("Invalid parameter: listener is NULL.");
766 ret
= add_listener(graph
->listeners
.ports_connected
, listener
,
767 listener_removed
, data
);
768 BT_LOGV("Added \"port connected\" listener to graph: "
769 "graph-addr=%p, listener-addr=%p, pos=%d",
770 graph
, listener
, ret
);
776 int bt_graph_add_ports_disconnected_listener(
777 struct bt_graph
*graph
,
778 bt_graph_ports_disconnected_listener listener
,
779 bt_graph_listener_removed listener_removed
, void *data
)
784 BT_LOGW_STR("Invalid parameter: graph is NULL.");
789 if (graph
->in_remove_listener
) {
790 BT_LOGW("Cannot call this function during the execution of a remove listener: "
797 BT_LOGW_STR("Invalid parameter: listener is NULL.");
802 ret
= add_listener(graph
->listeners
.ports_disconnected
, listener
,
803 listener_removed
, data
);
804 BT_LOGV("Added \"port disconnected\" listener to graph: "
805 "graph-addr=%p, listener-addr=%p, pos=%d",
806 graph
, listener
, ret
);
813 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
817 BT_LOGV("Notifying graph listeners that a port was added: "
818 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
819 graph
, port
, bt_port_get_name(port
));
821 for (i
= 0; i
< graph
->listeners
.port_added
->len
; i
++) {
822 struct bt_graph_listener listener
=
823 g_array_index(graph
->listeners
.port_added
,
824 struct bt_graph_listener
, i
);
825 bt_graph_port_added_listener func
= listener
.func
;
828 func(port
, listener
.data
);
833 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
834 struct bt_component
*comp
, struct bt_port
*port
)
838 BT_LOGV("Notifying graph listeners that a port was removed: "
839 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
840 graph
, port
, bt_port_get_name(port
));
842 for (i
= 0; i
< graph
->listeners
.port_removed
->len
; i
++) {
843 struct bt_graph_listener listener
=
844 g_array_index(graph
->listeners
.port_removed
,
845 struct bt_graph_listener
, i
);
846 bt_graph_port_removed_listener func
= listener
.func
;
849 func(comp
, port
, listener
.data
);
854 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
855 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
859 BT_LOGV("Notifying graph listeners that two ports were connected: "
861 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
862 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
863 graph
, upstream_port
, bt_port_get_name(upstream_port
),
864 downstream_port
, bt_port_get_name(downstream_port
));
866 for (i
= 0; i
< graph
->listeners
.ports_connected
->len
; i
++) {
867 struct bt_graph_listener listener
=
868 g_array_index(graph
->listeners
.ports_connected
,
869 struct bt_graph_listener
, i
);
870 bt_graph_ports_connected_listener func
= listener
.func
;
873 func(upstream_port
, downstream_port
, listener
.data
);
878 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
879 struct bt_component
*upstream_comp
,
880 struct bt_component
*downstream_comp
,
881 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
885 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
887 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
888 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
889 graph
, upstream_port
, bt_port_get_name(upstream_port
),
890 downstream_port
, bt_port_get_name(downstream_port
));
892 for (i
= 0; i
< graph
->listeners
.ports_disconnected
->len
; i
++) {
893 struct bt_graph_listener listener
=
894 g_array_index(graph
->listeners
.ports_disconnected
,
895 struct bt_graph_listener
, i
);
896 bt_graph_ports_disconnected_listener func
= listener
.func
;
899 func(upstream_comp
, downstream_comp
, upstream_port
,
900 downstream_port
, listener
.data
);
904 enum bt_graph_status
bt_graph_cancel(struct bt_graph
*graph
)
906 enum bt_graph_status ret
= BT_GRAPH_STATUS_OK
;
909 BT_LOGW_STR("Invalid parameter: graph is NULL.");
910 ret
= BT_GRAPH_STATUS_INVALID
;
914 graph
->canceled
= BT_TRUE
;
915 BT_LOGV("Canceled graph: addr=%p", graph
);
921 bt_bool
bt_graph_is_canceled(struct bt_graph
*graph
)
923 bt_bool canceled
= BT_FALSE
;
926 BT_LOGW_STR("Invalid parameter: graph is NULL.");
930 canceled
= graph
->canceled
;
937 void bt_graph_remove_connection(struct bt_graph
*graph
,
938 struct bt_connection
*connection
)
942 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
944 g_ptr_array_remove(graph
->connections
, connection
);
947 enum bt_graph_status
bt_graph_add_component_with_init_method_data(
948 struct bt_graph
*graph
,
949 struct bt_component_class
*component_class
,
950 const char *name
, struct bt_value
*params
,
951 void *init_method_data
,
952 struct bt_component
**user_component
)
954 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
955 enum bt_component_status comp_status
;
956 struct bt_component
*component
= NULL
;
957 enum bt_component_class_type type
;
959 bt_bool init_can_consume
;
964 BT_LOGW_STR("Invalid parameter: graph is NULL.");
965 graph_status
= BT_GRAPH_STATUS_INVALID
;
968 init_can_consume
= graph
->can_consume
;
970 if (!component_class
) {
971 BT_LOGW_STR("Invalid parameter: component class is NULL.");
972 graph_status
= BT_GRAPH_STATUS_INVALID
;
976 graph
->can_consume
= BT_FALSE
;
977 type
= bt_component_class_get_type(component_class
);
978 BT_LOGD("Adding component to graph: "
979 "graph-addr=%p, comp-cls-addr=%p, "
980 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
981 "init-method-data-addr=%p",
982 graph
, component_class
, bt_component_class_type_string(type
),
983 name
, params
, init_method_data
);
986 BT_LOGW_STR("Invalid parameter: name is NULL.");
987 graph_status
= BT_GRAPH_STATUS_INVALID
;
991 if (graph
->canceled
) {
992 BT_LOGW_STR("Invalid parameter: graph is canceled.");
993 graph_status
= BT_GRAPH_STATUS_CANCELED
;
997 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
998 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
999 type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
1000 BT_LOGW("Invalid parameter: unknown component class type: "
1002 graph_status
= BT_GRAPH_STATUS_INVALID
;
1006 for (i
= 0; i
< graph
->components
->len
; i
++) {
1007 void *other_comp
= graph
->components
->pdata
[i
];
1009 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1010 BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
1011 "other-comp-addr=%p, name=\"%s\"",
1013 graph_status
= BT_GRAPH_STATUS_INVALID
;
1019 * Parameters must be a map value, but we create a convenient
1020 * empty one if it's NULL.
1023 if (!bt_value_is_map(params
)) {
1024 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
1026 bt_value_type_string(bt_value_get_type(params
)));
1027 graph_status
= BT_GRAPH_STATUS_INVALID
;
1031 params
= bt_value_map_create();
1033 BT_LOGE_STR("Cannot create map value object.");
1034 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1039 comp_status
= bt_component_create(component_class
, name
, &component
);
1040 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
1041 BT_LOGE("Cannot create empty component object: status=%s",
1042 bt_component_status_string(comp_status
));
1043 graph_status
= bt_graph_status_from_component_status(
1049 * The user's initialization method needs to see that this
1050 * component is part of the graph. If the user method fails, we
1051 * immediately remove the component from the graph's components.
1053 g_ptr_array_add(graph
->components
, component
);
1054 bt_component_set_graph(component
, graph
);
1056 if (component_class
->methods
.init
) {
1057 BT_LOGD_STR("Calling user's initialization method.");
1058 comp_status
= component_class
->methods
.init(
1059 bt_private_component_from_component(component
), params
,
1061 BT_LOGD("User method returned: status=%s",
1062 bt_component_status_string(comp_status
));
1063 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
1064 BT_LOGW_STR("Initialization method failed.");
1065 graph_status
= bt_graph_status_from_component_status(
1067 bt_component_set_graph(component
, NULL
);
1068 g_ptr_array_remove_fast(graph
->components
, component
);
1074 * Mark the component as initialized so that its finalization
1075 * method is called when it is destroyed.
1077 component
->initialized
= true;
1080 * If it's a sink component, it needs to be part of the graph's
1081 * sink queue to be consumed by bt_graph_consume().
1083 if (bt_component_is_sink(component
)) {
1084 graph
->has_sink
= BT_TRUE
;
1085 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1089 * Freeze the component class now that it's instantiated at
1092 BT_LOGD_STR("Freezing component class.");
1093 bt_component_class_freeze(component
->class);
1094 BT_LOGD("Added component to graph: "
1095 "graph-addr=%p, comp-cls-addr=%p, "
1096 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
1097 "init-method-data-addr=%p, comp-addr=%p",
1098 graph
, component_class
, bt_component_class_type_string(type
),
1099 name
, params
, init_method_data
, component
);
1101 if (user_component
) {
1102 /* Move reference to user */
1103 *user_component
= component
;
1111 graph
->can_consume
= init_can_consume
;
1113 return graph_status
;
1116 enum bt_graph_status
bt_graph_add_component(
1117 struct bt_graph
*graph
,
1118 struct bt_component_class
*component_class
,
1119 const char *name
, struct bt_value
*params
,
1120 struct bt_component
**component
)
1122 return bt_graph_add_component_with_init_method_data(graph
,
1123 component_class
, name
, params
, NULL
, component
);
1127 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1128 struct bt_component
*component
)
1130 bt_bool init_can_consume
;
1137 assert(component
->base
.ref_count
.count
== 0);
1138 assert(bt_component_borrow_graph(component
) == graph
);
1140 init_can_consume
= graph
->can_consume
;
1141 count
= bt_component_get_input_port_count(component
);
1143 for (i
= 0; i
< count
; i
++) {
1144 struct bt_port
*port
=
1145 bt_component_get_input_port_by_index(component
, i
);
1150 if (bt_port_is_connected(port
)) {
1151 BT_LOGW("Cannot remove component from graph: "
1152 "an input port is connected: "
1153 "graph-addr=%p, comp-addr=%p, "
1154 "comp-name=\"%s\", connected-port-addr=%p, "
1155 "connected-port-name=\"%s\"",
1157 bt_component_get_name(component
),
1158 port
, bt_port_get_name(port
));
1163 count
= bt_component_get_output_port_count(component
);
1165 for (i
= 0; i
< count
; i
++) {
1166 struct bt_port
*port
=
1167 bt_component_get_output_port_by_index(component
, i
);
1172 if (bt_port_is_connected(port
)) {
1173 BT_LOGW("Cannot remove component from graph: "
1174 "an output port is connected: "
1175 "graph-addr=%p, comp-addr=%p, "
1176 "comp-name=\"%s\", connected-port-addr=%p, "
1177 "connected-port-name=\"%s\"",
1179 bt_component_get_name(component
),
1180 port
, bt_port_get_name(port
));
1185 graph
->can_consume
= BT_FALSE
;
1187 /* Possibly remove from sinks to consume */
1188 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1190 if (graph
->sinks_to_consume
->length
== 0) {
1191 graph
->has_sink
= BT_FALSE
;
1195 * This calls bt_object_release() on the component, and since
1196 * its reference count is 0, its destructor is called. Its
1197 * destructor calls the user's finalization method (if set).
1199 g_ptr_array_remove(graph
->components
, component
);
1206 graph
->can_consume
= init_can_consume
;