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 ret
= init_listeners_array(&graph
->listeners
.port_added
);
193 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
197 ret
= init_listeners_array(&graph
->listeners
.port_removed
);
199 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
203 ret
= init_listeners_array(&graph
->listeners
.ports_connected
);
205 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
209 ret
= init_listeners_array(&graph
->listeners
.ports_disconnected
);
211 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
215 BT_LOGD("Created graph object: addr=%p", graph
);
224 enum bt_graph_status
bt_graph_connect_ports(struct bt_graph
*graph
,
225 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
226 struct bt_connection
**user_connection
)
228 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
229 struct bt_connection
*connection
= NULL
;
230 struct bt_graph
*upstream_graph
= NULL
;
231 struct bt_graph
*downstream_graph
= NULL
;
232 struct bt_component
*upstream_component
= NULL
;
233 struct bt_component
*downstream_component
= NULL
;
234 enum bt_component_status component_status
;
237 BT_LOGW_STR("Invalid parameter: graph is NULL.");
238 status
= BT_GRAPH_STATUS_INVALID
;
242 if (!upstream_port
) {
243 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
244 status
= BT_GRAPH_STATUS_INVALID
;
248 if (!downstream_port
) {
249 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
250 status
= BT_GRAPH_STATUS_INVALID
;
254 BT_LOGD("Connecting component ports within graph: "
256 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
257 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
258 graph
, upstream_port
, bt_port_get_name(upstream_port
),
259 downstream_port
, bt_port_get_name(downstream_port
));
261 if (graph
->canceled
) {
262 BT_LOGW_STR("Invalid parameter: graph is canceled.");
263 status
= BT_GRAPH_STATUS_CANCELED
;
267 /* Ensure appropriate types for upstream and downstream ports. */
268 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
269 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
270 status
= BT_GRAPH_STATUS_INVALID
;
273 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
274 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
275 status
= BT_GRAPH_STATUS_INVALID
;
279 /* Ensure that both ports are currently unconnected. */
280 if (bt_port_is_connected(upstream_port
)) {
281 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
282 status
= BT_GRAPH_STATUS_INVALID
;
286 if (bt_port_is_connected(downstream_port
)) {
287 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
288 status
= BT_GRAPH_STATUS_INVALID
;
293 * Ensure that both ports are still attached to their creating
296 upstream_component
= bt_port_get_component(upstream_port
);
297 if (!upstream_component
) {
298 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
299 status
= BT_GRAPH_STATUS_INVALID
;
303 downstream_component
= bt_port_get_component(downstream_port
);
304 if (!downstream_component
) {
305 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
306 status
= BT_GRAPH_STATUS_INVALID
;
310 BT_LOGD("Connecting component ports: "
311 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
312 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
313 upstream_component
, bt_component_get_name(upstream_component
),
314 downstream_component
, bt_component_get_name(downstream_component
));
317 * At this point the ports are not connected yet. Both
318 * components need to accept an eventual connection to their
319 * port by the other port before we continue.
321 BT_LOGD_STR("Asking upstream component to accept the connection.");
322 component_status
= bt_component_accept_port_connection(
323 upstream_component
, upstream_port
, downstream_port
);
324 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
325 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
326 BT_LOGD_STR("Upstream component refused the connection.");
328 BT_LOGW("Cannot ask upstream component to accept the connection: "
329 "status=%s", bt_component_status_string(component_status
));
332 status
= bt_graph_status_from_component_status(
337 BT_LOGD_STR("Asking downstream component to accept the connection.");
338 component_status
= bt_component_accept_port_connection(
339 downstream_component
, downstream_port
, upstream_port
);
340 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
341 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
342 BT_LOGD_STR("Downstream component refused the connection.");
344 BT_LOGW("Cannot ask downstream component to accept the connection: "
345 "status=%s", bt_component_status_string(component_status
));
348 status
= bt_graph_status_from_component_status(
353 BT_LOGD_STR("Creating connection.");
354 connection
= bt_connection_create(graph
, upstream_port
,
357 BT_LOGW("Cannot create connection object.");
358 status
= BT_GRAPH_STATUS_NOMEM
;
362 BT_LOGD("Connection object created: conn-addr=%p", connection
);
365 * Ownership of upstream_component/downstream_component and of
366 * the connection object is transferred to the graph.
368 g_ptr_array_add(graph
->connections
, connection
);
371 * Notify both components that their port is connected.
373 BT_LOGD_STR("Notifying upstream component that its port is connected.");
374 bt_component_port_connected(upstream_component
, upstream_port
,
376 BT_LOGD_STR("Notifying downstream component that its port is connected.");
377 bt_component_port_connected(downstream_component
, downstream_port
,
381 * Notify the graph's creator that both ports are connected.
383 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
384 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
385 BT_LOGD("Connected component ports within graph: "
387 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
388 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
389 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
390 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
392 upstream_component
, bt_component_get_name(upstream_component
),
393 downstream_component
, bt_component_get_name(downstream_component
),
394 upstream_port
, bt_port_get_name(upstream_port
),
395 downstream_port
, bt_port_get_name(downstream_port
));
397 if (user_connection
) {
398 /* Move reference to user */
399 *user_connection
= connection
;
404 bt_put(upstream_graph
);
405 bt_put(downstream_graph
);
406 bt_put(upstream_component
);
407 bt_put(downstream_component
);
413 enum bt_graph_status
bt_graph_consume_no_check(struct bt_graph
*graph
)
415 struct bt_component
*sink
;
416 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
417 enum bt_component_status comp_status
;
420 BT_LOGV("Making next sink consume: addr=%p", graph
);
422 if (!graph
->has_sink
) {
423 BT_LOGW_STR("Graph has no sink component.");
424 status
= BT_GRAPH_STATUS_NO_SINK
;
428 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
429 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
430 status
= BT_GRAPH_STATUS_END
;
434 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
435 sink
= current_node
->data
;
436 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
437 sink
, bt_component_get_name(sink
));
438 comp_status
= bt_component_sink_consume(sink
);
439 BT_LOGV("Consumed from sink: status=%s",
440 bt_component_status_string(comp_status
));
441 switch (comp_status
) {
442 case BT_COMPONENT_STATUS_OK
:
444 case BT_COMPONENT_STATUS_END
:
445 status
= BT_GRAPH_STATUS_END
;
447 case BT_COMPONENT_STATUS_AGAIN
:
448 status
= BT_GRAPH_STATUS_AGAIN
;
450 case BT_COMPONENT_STATUS_INVALID
:
451 status
= BT_GRAPH_STATUS_INVALID
;
454 status
= BT_GRAPH_STATUS_ERROR
;
458 if (status
!= BT_GRAPH_STATUS_END
) {
459 g_queue_push_tail_link(graph
->sinks_to_consume
, current_node
);
463 /* End reached, the node is not added back to the queue and free'd. */
464 g_queue_delete_link(graph
->sinks_to_consume
, current_node
);
466 /* Don't forward an END status if there are sinks left to consume. */
467 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
468 status
= BT_GRAPH_STATUS_OK
;
472 BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status
));
476 enum bt_graph_status
bt_graph_consume(struct bt_graph
*graph
)
478 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
481 BT_LOGW_STR("Invalid parameter: graph is NULL.");
482 status
= BT_GRAPH_STATUS_INVALID
;
486 if (graph
->canceled
) {
487 BT_LOGW("Invalid parameter: graph is canceled: "
488 "graph-addr=%p", graph
);
489 status
= BT_GRAPH_STATUS_CANCELED
;
493 status
= bt_graph_consume_no_check(graph
);
499 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
501 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
504 BT_LOGW_STR("Invalid parameter: graph is NULL.");
505 status
= BT_GRAPH_STATUS_INVALID
;
509 if (graph
->canceled
) {
510 BT_LOGW("Invalid parameter: graph is canceled: "
511 "graph-addr=%p", graph
);
512 status
= BT_GRAPH_STATUS_CANCELED
;
516 BT_LOGV("Running graph: addr=%p", graph
);
520 * Check if the graph is canceled at each iteration. If
521 * the graph was canceled by another thread or by a
522 * signal, this is not a warning nor an error, it was
523 * intentional: log with a DEBUG level only.
525 if (graph
->canceled
) {
526 BT_LOGD("Stopping the graph: graph is canceled: "
527 "graph-addr=%p", graph
);
528 status
= BT_GRAPH_STATUS_CANCELED
;
532 status
= bt_graph_consume(graph
);
533 if (status
== BT_GRAPH_STATUS_AGAIN
) {
535 * If AGAIN is received and there are multiple
536 * sinks, go ahead and consume from the next
539 * However, in the case where a single sink is
540 * left, the caller can decide to busy-wait and
541 * call bt_graph_run() continuously until the
542 * source is ready or it can decide to sleep for
543 * an arbitrary amount of time.
545 if (graph
->sinks_to_consume
->length
> 1) {
546 status
= BT_GRAPH_STATUS_OK
;
548 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
551 } while (status
== BT_GRAPH_STATUS_OK
);
553 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
554 status
= BT_GRAPH_STATUS_END
;
558 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status
));
563 int add_listener(GArray
*listeners
, void *func
, void *removed
, void *data
)
565 struct bt_graph_listener listener
= {
571 g_array_append_val(listeners
, listener
);
572 return listeners
->len
- 1;
575 int bt_graph_add_port_added_listener(
576 struct bt_graph
*graph
,
577 bt_graph_port_added_listener listener
,
578 bt_graph_listener_removed listener_removed
, void *data
)
583 BT_LOGW_STR("Invalid parameter: graph is NULL.");
588 if (graph
->in_remove_listener
) {
589 BT_LOGW("Cannot call this function during the execution of a remove listener: "
596 BT_LOGW_STR("Invalid parameter: listener is NULL.");
601 ret
= add_listener(graph
->listeners
.port_added
, listener
,
602 listener_removed
, data
);
603 BT_LOGV("Added \"port added\" listener to graph: "
604 "graph-addr=%p, listener-addr=%p, pos=%d",
605 graph
, listener
, ret
);
611 int bt_graph_add_port_removed_listener(
612 struct bt_graph
*graph
,
613 bt_graph_port_removed_listener listener
,
614 bt_graph_listener_removed listener_removed
, void *data
)
619 BT_LOGW_STR("Invalid parameter: graph is NULL.");
624 if (graph
->in_remove_listener
) {
625 BT_LOGW("Cannot call this function during the execution of a remove listener: "
632 BT_LOGW_STR("Invalid parameter: listener is NULL.");
637 ret
= add_listener(graph
->listeners
.port_removed
, listener
,
638 listener_removed
, data
);
639 BT_LOGV("Added \"port removed\" listener to graph: "
640 "graph-addr=%p, listener-addr=%p, pos=%d",
641 graph
, listener
, ret
);
647 int bt_graph_add_ports_connected_listener(
648 struct bt_graph
*graph
,
649 bt_graph_ports_connected_listener listener
,
650 bt_graph_listener_removed listener_removed
, void *data
)
655 BT_LOGW_STR("Invalid parameter: graph is NULL.");
660 if (graph
->in_remove_listener
) {
661 BT_LOGW("Cannot call this function during the execution of a remove listener: "
668 BT_LOGW_STR("Invalid parameter: listener is NULL.");
673 ret
= add_listener(graph
->listeners
.ports_connected
, listener
,
674 listener_removed
, data
);
675 BT_LOGV("Added \"port connected\" listener to graph: "
676 "graph-addr=%p, listener-addr=%p, pos=%d",
677 graph
, listener
, ret
);
683 int bt_graph_add_ports_disconnected_listener(
684 struct bt_graph
*graph
,
685 bt_graph_ports_disconnected_listener listener
,
686 bt_graph_listener_removed listener_removed
, void *data
)
691 BT_LOGW_STR("Invalid parameter: graph is NULL.");
696 if (graph
->in_remove_listener
) {
697 BT_LOGW("Cannot call this function during the execution of a remove listener: "
704 BT_LOGW_STR("Invalid parameter: listener is NULL.");
709 ret
= add_listener(graph
->listeners
.ports_disconnected
, listener
,
710 listener_removed
, data
);
711 BT_LOGV("Added \"port disconnected\" listener to graph: "
712 "graph-addr=%p, listener-addr=%p, pos=%d",
713 graph
, listener
, ret
);
720 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
724 BT_LOGV("Notifying graph listeners that a port was added: "
725 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
726 graph
, port
, bt_port_get_name(port
));
728 for (i
= 0; i
< graph
->listeners
.port_added
->len
; i
++) {
729 struct bt_graph_listener listener
=
730 g_array_index(graph
->listeners
.port_added
,
731 struct bt_graph_listener
, i
);
732 bt_graph_port_added_listener func
= listener
.func
;
735 func(port
, listener
.data
);
740 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
741 struct bt_component
*comp
, struct bt_port
*port
)
745 BT_LOGV("Notifying graph listeners that a port was removed: "
746 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
747 graph
, port
, bt_port_get_name(port
));
749 for (i
= 0; i
< graph
->listeners
.port_removed
->len
; i
++) {
750 struct bt_graph_listener listener
=
751 g_array_index(graph
->listeners
.port_removed
,
752 struct bt_graph_listener
, i
);
753 bt_graph_port_removed_listener func
= listener
.func
;
756 func(comp
, port
, listener
.data
);
761 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
762 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
766 BT_LOGV("Notifying graph listeners that two ports were connected: "
768 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
769 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
770 graph
, upstream_port
, bt_port_get_name(upstream_port
),
771 downstream_port
, bt_port_get_name(downstream_port
));
773 for (i
= 0; i
< graph
->listeners
.ports_connected
->len
; i
++) {
774 struct bt_graph_listener listener
=
775 g_array_index(graph
->listeners
.ports_connected
,
776 struct bt_graph_listener
, i
);
777 bt_graph_ports_connected_listener func
= listener
.func
;
780 func(upstream_port
, downstream_port
, listener
.data
);
785 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
786 struct bt_component
*upstream_comp
,
787 struct bt_component
*downstream_comp
,
788 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
792 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
794 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
795 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
796 graph
, upstream_port
, bt_port_get_name(upstream_port
),
797 downstream_port
, bt_port_get_name(downstream_port
));
799 for (i
= 0; i
< graph
->listeners
.ports_disconnected
->len
; i
++) {
800 struct bt_graph_listener listener
=
801 g_array_index(graph
->listeners
.ports_disconnected
,
802 struct bt_graph_listener
, i
);
803 bt_graph_ports_disconnected_listener func
= listener
.func
;
806 func(upstream_comp
, downstream_comp
, upstream_port
,
807 downstream_port
, listener
.data
);
811 extern enum bt_graph_status
bt_graph_cancel(struct bt_graph
*graph
)
813 enum bt_graph_status ret
= BT_GRAPH_STATUS_OK
;
816 BT_LOGW_STR("Invalid parameter: graph is NULL.");
817 ret
= BT_GRAPH_STATUS_INVALID
;
821 graph
->canceled
= BT_TRUE
;
822 BT_LOGV("Canceled graph: addr=%p", graph
);
828 extern bt_bool
bt_graph_is_canceled(struct bt_graph
*graph
)
830 return graph
? graph
->canceled
: BT_FALSE
;
834 void bt_graph_remove_connection(struct bt_graph
*graph
,
835 struct bt_connection
*connection
)
839 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
841 g_ptr_array_remove(graph
->connections
, connection
);
844 enum bt_graph_status
bt_graph_add_component_with_init_method_data(
845 struct bt_graph
*graph
,
846 struct bt_component_class
*component_class
,
847 const char *name
, struct bt_value
*params
,
848 void *init_method_data
,
849 struct bt_component
**user_component
)
851 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
852 enum bt_component_status comp_status
;
853 struct bt_component
*component
= NULL
;
854 enum bt_component_class_type type
;
860 BT_LOGW_STR("Invalid parameter: graph is NULL.");
861 graph_status
= BT_GRAPH_STATUS_INVALID
;
865 if (!component_class
) {
866 BT_LOGW_STR("Invalid parameter: component class is NULL.");
867 graph_status
= BT_GRAPH_STATUS_INVALID
;
871 type
= bt_component_class_get_type(component_class
);
872 BT_LOGD("Adding component to graph: "
873 "graph-addr=%p, comp-cls-addr=%p, "
874 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
875 "init-method-data-addr=%p",
876 graph
, component_class
, bt_component_class_type_string(type
),
877 name
, params
, init_method_data
);
880 BT_LOGW_STR("Invalid parameter: name is NULL.");
881 graph_status
= BT_GRAPH_STATUS_INVALID
;
885 if (graph
->canceled
) {
886 BT_LOGW_STR("Invalid parameter: graph is canceled.");
887 graph_status
= BT_GRAPH_STATUS_CANCELED
;
891 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
892 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
893 type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
894 BT_LOGW("Invalid parameter: unknown component class type: "
896 graph_status
= BT_GRAPH_STATUS_INVALID
;
900 for (i
= 0; i
< graph
->components
->len
; i
++) {
901 void *other_comp
= graph
->components
->pdata
[i
];
903 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
904 BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
905 "other-comp-addr=%p, name=\"%s\"",
907 graph_status
= BT_GRAPH_STATUS_INVALID
;
913 * Parameters must be a map value, but we create a convenient
914 * empty one if it's NULL.
917 if (!bt_value_is_map(params
)) {
918 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
920 bt_value_type_string(bt_value_get_type(params
)));
921 graph_status
= BT_GRAPH_STATUS_INVALID
;
925 params
= bt_value_map_create();
927 BT_LOGE_STR("Cannot create map value object.");
928 graph_status
= BT_GRAPH_STATUS_NOMEM
;
933 comp_status
= bt_component_create(component_class
, name
, &component
);
934 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
935 BT_LOGE("Cannot create empty component object: status=%s",
936 bt_component_status_string(comp_status
));
937 graph_status
= bt_graph_status_from_component_status(
943 * The user's initialization method needs to see that this
944 * component is part of the graph. If the user method fails, we
945 * immediately remove the component from the graph's components.
947 g_ptr_array_add(graph
->components
, component
);
948 bt_component_set_graph(component
, graph
);
950 if (component_class
->methods
.init
) {
951 BT_LOGD_STR("Calling user's initialization method.");
952 comp_status
= component_class
->methods
.init(
953 bt_private_component_from_component(component
), params
,
955 BT_LOGD("User method returned: status=%s",
956 bt_component_status_string(comp_status
));
957 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
958 BT_LOGW_STR("Initialization method failed.");
959 graph_status
= bt_graph_status_from_component_status(
961 bt_component_set_graph(component
, NULL
);
962 g_ptr_array_remove_fast(graph
->components
, component
);
968 * Mark the component as initialized so that its finalization
969 * method is called when it is destroyed.
971 component
->initialized
= true;
974 * If it's a sink component, it needs to be part of the graph's
975 * sink queue to be consumed by bt_graph_consume().
977 if (bt_component_is_sink(component
)) {
978 graph
->has_sink
= BT_TRUE
;
979 g_queue_push_tail(graph
->sinks_to_consume
, component
);
983 * Freeze the component class now that it's instantiated at
986 BT_LOGD_STR("Freezing component class.");
987 bt_component_class_freeze(component
->class);
988 BT_LOGD("Added component to graph: "
989 "graph-addr=%p, comp-cls-addr=%p, "
990 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
991 "init-method-data-addr=%p, comp-addr=%p",
992 graph
, component_class
, bt_component_class_type_string(type
),
993 name
, params
, init_method_data
, component
);
995 if (user_component
) {
996 /* Move reference to user */
997 *user_component
= component
;
1004 return graph_status
;
1007 enum bt_graph_status
bt_graph_add_component(
1008 struct bt_graph
*graph
,
1009 struct bt_component_class
*component_class
,
1010 const char *name
, struct bt_value
*params
,
1011 struct bt_component
**component
)
1013 return bt_graph_add_component_with_init_method_data(graph
,
1014 component_class
, name
, params
, NULL
, component
);