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
{
51 void bt_graph_destroy(struct bt_object
*obj
)
53 struct bt_graph
*graph
= container_of(obj
,
54 struct bt_graph
, base
);
57 * The graph's reference count is 0 if we're here. Increment
58 * it to avoid a double-destroy (possibly infinitely recursive)
61 * 1. We put and destroy a connection.
62 * 2. This connection's destructor finalizes its active
63 * notification iterators.
64 * 3. A notification iterator's finalization function gets a
65 * new reference on its component (reference count goes from
67 * 4. Since this component's reference count goes to 1, it takes
68 * a reference on its parent (this graph). This graph's
69 * reference count goes from 0 to 1.
70 * 5. The notification iterator's finalization function puts its
71 * component reference (reference count goes from 1 to 0).
72 * 6. Since this component's reference count goes from 1 to 0,
73 * it puts its parent (this graph). This graph's reference
74 * count goes from 1 to 0.
75 * 7. Since this graph's reference count goes from 1 to 0,
76 * its destructor is called (this function).
78 * With the incrementation below, the graph's reference count at
79 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
80 * ensures that this function is not called two times.
82 BT_LOGD("Destroying graph: addr=%p", graph
);
83 obj
->ref_count
.count
++;
86 * Cancel the graph to disallow some operations, like creating
87 * notification iterators and adding ports to components.
89 (void) bt_graph_cancel(graph
);
91 if (graph
->connections
) {
92 BT_LOGD_STR("Destroying connections.");
93 g_ptr_array_free(graph
->connections
, TRUE
);
95 if (graph
->components
) {
96 BT_LOGD_STR("Destroying components.");
97 g_ptr_array_free(graph
->components
, TRUE
);
99 if (graph
->sinks_to_consume
) {
100 g_queue_free(graph
->sinks_to_consume
);
103 if (graph
->listeners
.port_added
) {
104 g_array_free(graph
->listeners
.port_added
, TRUE
);
107 if (graph
->listeners
.port_removed
) {
108 g_array_free(graph
->listeners
.port_removed
, TRUE
);
111 if (graph
->listeners
.ports_connected
) {
112 g_array_free(graph
->listeners
.ports_connected
, TRUE
);
115 if (graph
->listeners
.ports_disconnected
) {
116 g_array_free(graph
->listeners
.ports_disconnected
, TRUE
);
123 int init_listeners_array(GArray
**listeners
)
128 *listeners
= g_array_new(FALSE
, TRUE
, sizeof(struct bt_graph_listener
));
130 BT_LOGE_STR("Failed to allocate one GArray.");
139 struct bt_graph
*bt_graph_create(void)
141 struct bt_graph
*graph
;
144 BT_LOGD_STR("Creating graph object.");
145 graph
= g_new0(struct bt_graph
, 1);
147 BT_LOGE_STR("Failed to allocate one graph.");
151 bt_object_init(graph
, bt_graph_destroy
);
153 graph
->connections
= g_ptr_array_new_with_free_func(bt_object_release
);
154 if (!graph
->connections
) {
155 BT_LOGE_STR("Failed to allocate one GPtrArray.");
158 graph
->components
= g_ptr_array_new_with_free_func(bt_object_release
);
159 if (!graph
->components
) {
160 BT_LOGE_STR("Failed to allocate one GPtrArray.");
163 graph
->sinks_to_consume
= g_queue_new();
164 if (!graph
->sinks_to_consume
) {
165 BT_LOGE_STR("Failed to allocate one GQueue.");
169 ret
= init_listeners_array(&graph
->listeners
.port_added
);
171 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
175 ret
= init_listeners_array(&graph
->listeners
.port_removed
);
177 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
181 ret
= init_listeners_array(&graph
->listeners
.ports_connected
);
183 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
187 ret
= init_listeners_array(&graph
->listeners
.ports_disconnected
);
189 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
193 BT_LOGD("Created graph object: addr=%p", graph
);
202 enum bt_graph_status
bt_graph_connect_ports(struct bt_graph
*graph
,
203 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
204 struct bt_connection
**user_connection
)
206 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
207 struct bt_connection
*connection
= NULL
;
208 struct bt_graph
*upstream_graph
= NULL
;
209 struct bt_graph
*downstream_graph
= NULL
;
210 struct bt_component
*upstream_component
= NULL
;
211 struct bt_component
*downstream_component
= NULL
;
212 enum bt_component_status component_status
;
215 BT_LOGW_STR("Invalid parameter: graph is NULL.");
216 status
= BT_GRAPH_STATUS_INVALID
;
220 if (!upstream_port
) {
221 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
222 status
= BT_GRAPH_STATUS_INVALID
;
226 if (!downstream_port
) {
227 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
228 status
= BT_GRAPH_STATUS_INVALID
;
232 BT_LOGD("Connecting component ports within graph: "
234 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
235 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
236 graph
, upstream_port
, bt_port_get_name(upstream_port
),
237 downstream_port
, bt_port_get_name(downstream_port
));
239 if (graph
->canceled
) {
240 BT_LOGW_STR("Invalid parameter: graph is canceled.");
241 status
= BT_GRAPH_STATUS_CANCELED
;
245 /* Ensure appropriate types for upstream and downstream ports. */
246 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
247 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
248 status
= BT_GRAPH_STATUS_INVALID
;
251 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
252 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
253 status
= BT_GRAPH_STATUS_INVALID
;
257 /* Ensure that both ports are currently unconnected. */
258 if (bt_port_is_connected(upstream_port
)) {
259 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
260 status
= BT_GRAPH_STATUS_INVALID
;
264 if (bt_port_is_connected(downstream_port
)) {
265 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
266 status
= BT_GRAPH_STATUS_INVALID
;
271 * Ensure that both ports are still attached to their creating
274 upstream_component
= bt_port_get_component(upstream_port
);
275 if (!upstream_component
) {
276 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
277 status
= BT_GRAPH_STATUS_INVALID
;
281 downstream_component
= bt_port_get_component(downstream_port
);
282 if (!downstream_component
) {
283 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
284 status
= BT_GRAPH_STATUS_INVALID
;
288 BT_LOGD("Connecting component ports: "
289 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
290 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
291 upstream_component
, bt_component_get_name(upstream_component
),
292 downstream_component
, bt_component_get_name(downstream_component
));
295 * At this point the ports are not connected yet. Both
296 * components need to accept an eventual connection to their
297 * port by the other port before we continue.
299 BT_LOGD_STR("Asking upstream component to accept the connection.");
300 component_status
= bt_component_accept_port_connection(
301 upstream_component
, upstream_port
, downstream_port
);
302 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
303 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
304 BT_LOGD_STR("Upstream component refused the connection.");
306 BT_LOGW("Cannot ask upstream component to accept the connection: "
307 "status=%s", bt_component_status_string(component_status
));
310 status
= bt_graph_status_from_component_status(
315 BT_LOGD_STR("Asking downstream component to accept the connection.");
316 component_status
= bt_component_accept_port_connection(
317 downstream_component
, downstream_port
, upstream_port
);
318 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
319 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
320 BT_LOGD_STR("Downstream component refused the connection.");
322 BT_LOGW("Cannot ask downstream component to accept the connection: "
323 "status=%s", bt_component_status_string(component_status
));
326 status
= bt_graph_status_from_component_status(
331 BT_LOGD_STR("Creating connection.");
332 connection
= bt_connection_create(graph
, upstream_port
,
335 BT_LOGW("Cannot create connection object.");
336 status
= BT_GRAPH_STATUS_NOMEM
;
340 BT_LOGD("Connection object created: conn-addr=%p", connection
);
343 * Ownership of upstream_component/downstream_component and of
344 * the connection object is transferred to the graph.
346 g_ptr_array_add(graph
->connections
, connection
);
349 * Notify both components that their port is connected.
351 BT_LOGD_STR("Notifying upstream component that its port is connected.");
352 bt_component_port_connected(upstream_component
, upstream_port
,
354 BT_LOGD_STR("Notifying downstream component that its port is connected.");
355 bt_component_port_connected(downstream_component
, downstream_port
,
359 * Notify the graph's creator that both ports are connected.
361 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
362 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
363 BT_LOGD("Connected component ports within graph: "
365 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
366 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
367 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
368 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
370 upstream_component
, bt_component_get_name(upstream_component
),
371 downstream_component
, bt_component_get_name(downstream_component
),
372 upstream_port
, bt_port_get_name(upstream_port
),
373 downstream_port
, bt_port_get_name(downstream_port
));
375 if (user_connection
) {
376 /* Move reference to user */
377 *user_connection
= connection
;
382 bt_put(upstream_graph
);
383 bt_put(downstream_graph
);
384 bt_put(upstream_component
);
385 bt_put(downstream_component
);
391 enum bt_graph_status
bt_graph_consume_no_check(struct bt_graph
*graph
)
393 struct bt_component
*sink
;
394 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
395 enum bt_component_status comp_status
;
398 BT_LOGV("Making next sink consume: addr=%p", graph
);
400 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
401 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
402 status
= BT_GRAPH_STATUS_END
;
406 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
407 sink
= current_node
->data
;
408 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
409 sink
, bt_component_get_name(sink
));
410 comp_status
= bt_component_sink_consume(sink
);
411 BT_LOGV("Consumed from sink: status=%s",
412 bt_component_status_string(comp_status
));
413 switch (comp_status
) {
414 case BT_COMPONENT_STATUS_OK
:
416 case BT_COMPONENT_STATUS_END
:
417 status
= BT_GRAPH_STATUS_END
;
419 case BT_COMPONENT_STATUS_AGAIN
:
420 status
= BT_GRAPH_STATUS_AGAIN
;
422 case BT_COMPONENT_STATUS_INVALID
:
423 status
= BT_GRAPH_STATUS_INVALID
;
426 status
= BT_GRAPH_STATUS_ERROR
;
430 if (status
!= BT_GRAPH_STATUS_END
) {
431 g_queue_push_tail_link(graph
->sinks_to_consume
, current_node
);
435 /* End reached, the node is not added back to the queue and free'd. */
436 g_queue_delete_link(graph
->sinks_to_consume
, current_node
);
438 /* Don't forward an END status if there are sinks left to consume. */
439 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
440 status
= BT_GRAPH_STATUS_OK
;
444 BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status
));
448 enum bt_graph_status
bt_graph_consume(struct bt_graph
*graph
)
450 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
453 BT_LOGW_STR("Invalid parameter: graph is NULL.");
454 status
= BT_GRAPH_STATUS_INVALID
;
458 if (graph
->canceled
) {
459 BT_LOGW("Invalid parameter: graph is canceled: "
460 "graph-addr=%p", graph
);
461 status
= BT_GRAPH_STATUS_CANCELED
;
465 status
= bt_graph_consume_no_check(graph
);
471 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
473 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
476 BT_LOGW_STR("Invalid parameter: graph is NULL.");
477 status
= BT_GRAPH_STATUS_INVALID
;
481 if (graph
->canceled
) {
482 BT_LOGW("Invalid parameter: graph is canceled: "
483 "graph-addr=%p", graph
);
484 status
= BT_GRAPH_STATUS_CANCELED
;
488 BT_LOGV("Running graph: addr=%p", graph
);
492 * Check if the graph is canceled at each iteration. If
493 * the graph was canceled by another thread or by a
494 * signal, this is not a warning nor an error, it was
495 * intentional: log with a DEBUG level only.
497 if (graph
->canceled
) {
498 BT_LOGD("Stopping the graph: graph is canceled: "
499 "graph-addr=%p", graph
);
500 status
= BT_GRAPH_STATUS_CANCELED
;
504 status
= bt_graph_consume(graph
);
505 if (status
== BT_GRAPH_STATUS_AGAIN
) {
507 * If AGAIN is received and there are multiple
508 * sinks, go ahead and consume from the next
511 * However, in the case where a single sink is
512 * left, the caller can decide to busy-wait and
513 * call bt_graph_run() continuously until the
514 * source is ready or it can decide to sleep for
515 * an arbitrary amount of time.
517 if (graph
->sinks_to_consume
->length
> 1) {
518 status
= BT_GRAPH_STATUS_OK
;
521 } while (status
== BT_GRAPH_STATUS_OK
);
523 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
524 status
= BT_GRAPH_STATUS_END
;
528 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status
));
533 int add_listener(GArray
*listeners
, void *func
, void *data
)
535 struct bt_graph_listener listener
= {
540 g_array_append_val(listeners
, listener
);
541 return listeners
->len
- 1;
544 int bt_graph_add_port_added_listener(
545 struct bt_graph
*graph
,
546 bt_graph_port_added_listener listener
, void *data
)
551 BT_LOGW_STR("Invalid parameter: graph is NULL.");
557 BT_LOGW_STR("Invalid parameter: listener is NULL.");
562 ret
= add_listener(graph
->listeners
.port_added
, listener
, data
);
563 BT_LOGV("Added \"port added\" listener to graph: "
564 "graph-addr=%p, listener-addr=%p, pos=%d",
565 graph
, listener
, ret
);
571 int bt_graph_add_port_removed_listener(
572 struct bt_graph
*graph
,
573 bt_graph_port_removed_listener listener
, void *data
)
578 BT_LOGW_STR("Invalid parameter: graph is NULL.");
584 BT_LOGW_STR("Invalid parameter: listener is NULL.");
589 ret
= add_listener(graph
->listeners
.port_removed
, listener
, data
);
590 BT_LOGV("Added \"port removed\" listener to graph: "
591 "graph-addr=%p, listener-addr=%p, pos=%d",
592 graph
, listener
, ret
);
598 int bt_graph_add_ports_connected_listener(
599 struct bt_graph
*graph
,
600 bt_graph_ports_connected_listener listener
, void *data
)
605 BT_LOGW_STR("Invalid parameter: graph is NULL.");
611 BT_LOGW_STR("Invalid parameter: listener is NULL.");
616 ret
= add_listener(graph
->listeners
.ports_connected
, listener
, data
);
617 BT_LOGV("Added \"port connected\" listener to graph: "
618 "graph-addr=%p, listener-addr=%p, pos=%d",
619 graph
, listener
, ret
);
625 int bt_graph_add_ports_disconnected_listener(
626 struct bt_graph
*graph
,
627 bt_graph_ports_disconnected_listener listener
, void *data
)
632 BT_LOGW_STR("Invalid parameter: graph is NULL.");
638 BT_LOGW_STR("Invalid parameter: listener is NULL.");
643 ret
= add_listener(graph
->listeners
.ports_disconnected
, listener
, data
);
644 BT_LOGV("Added \"port disconnected\" listener to graph: "
645 "graph-addr=%p, listener-addr=%p, pos=%d",
646 graph
, listener
, ret
);
653 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
657 BT_LOGV("Notifying graph listeners that a port was added: "
658 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
659 graph
, port
, bt_port_get_name(port
));
661 for (i
= 0; i
< graph
->listeners
.port_added
->len
; i
++) {
662 struct bt_graph_listener listener
=
663 g_array_index(graph
->listeners
.port_added
,
664 struct bt_graph_listener
, i
);
665 bt_graph_port_added_listener func
= listener
.func
;
668 func(port
, listener
.data
);
673 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
674 struct bt_component
*comp
, struct bt_port
*port
)
678 BT_LOGV("Notifying graph listeners that a port was removed: "
679 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
680 graph
, port
, bt_port_get_name(port
));
682 for (i
= 0; i
< graph
->listeners
.port_removed
->len
; i
++) {
683 struct bt_graph_listener listener
=
684 g_array_index(graph
->listeners
.port_removed
,
685 struct bt_graph_listener
, i
);
686 bt_graph_port_removed_listener func
= listener
.func
;
689 func(comp
, port
, listener
.data
);
694 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
695 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
699 BT_LOGV("Notifying graph listeners that two ports were connected: "
701 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
702 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
703 graph
, upstream_port
, bt_port_get_name(upstream_port
),
704 downstream_port
, bt_port_get_name(downstream_port
));
706 for (i
= 0; i
< graph
->listeners
.ports_connected
->len
; i
++) {
707 struct bt_graph_listener listener
=
708 g_array_index(graph
->listeners
.ports_connected
,
709 struct bt_graph_listener
, i
);
710 bt_graph_ports_connected_listener func
= listener
.func
;
713 func(upstream_port
, downstream_port
, listener
.data
);
718 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
719 struct bt_component
*upstream_comp
,
720 struct bt_component
*downstream_comp
,
721 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
725 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
727 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
728 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
729 graph
, upstream_port
, bt_port_get_name(upstream_port
),
730 downstream_port
, bt_port_get_name(downstream_port
));
732 for (i
= 0; i
< graph
->listeners
.ports_disconnected
->len
; i
++) {
733 struct bt_graph_listener listener
=
734 g_array_index(graph
->listeners
.ports_disconnected
,
735 struct bt_graph_listener
, i
);
736 bt_graph_ports_disconnected_listener func
= listener
.func
;
739 func(upstream_comp
, downstream_comp
, upstream_port
,
740 downstream_port
, listener
.data
);
744 extern enum bt_graph_status
bt_graph_cancel(struct bt_graph
*graph
)
746 enum bt_graph_status ret
= BT_GRAPH_STATUS_OK
;
749 BT_LOGW_STR("Invalid parameter: graph is NULL.");
750 ret
= BT_GRAPH_STATUS_INVALID
;
754 graph
->canceled
= BT_TRUE
;
755 BT_LOGV("Canceled graph: addr=%p", graph
);
761 extern bt_bool
bt_graph_is_canceled(struct bt_graph
*graph
)
763 return graph
? graph
->canceled
: BT_FALSE
;
767 void bt_graph_remove_connection(struct bt_graph
*graph
,
768 struct bt_connection
*connection
)
772 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
774 g_ptr_array_remove(graph
->connections
, connection
);
777 enum bt_graph_status
bt_graph_add_component_with_init_method_data(
778 struct bt_graph
*graph
,
779 struct bt_component_class
*component_class
,
780 const char *name
, struct bt_value
*params
,
781 void *init_method_data
,
782 struct bt_component
**user_component
)
784 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
785 enum bt_component_status comp_status
;
786 struct bt_component
*component
= NULL
;
787 enum bt_component_class_type type
;
793 BT_LOGW_STR("Invalid parameter: graph is NULL.");
794 graph_status
= BT_GRAPH_STATUS_INVALID
;
798 if (!component_class
) {
799 BT_LOGW_STR("Invalid parameter: component class is NULL.");
800 graph_status
= BT_GRAPH_STATUS_INVALID
;
804 type
= bt_component_class_get_type(component_class
);
805 BT_LOGD("Adding component to graph: "
806 "graph-addr=%p, comp-cls-addr=%p, "
807 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
808 "init-method-data-addr=%p",
809 graph
, component_class
, bt_component_class_type_string(type
),
810 name
, params
, init_method_data
);
813 BT_LOGW_STR("Invalid parameter: name is NULL.");
814 graph_status
= BT_GRAPH_STATUS_INVALID
;
818 if (graph
->canceled
) {
819 BT_LOGW_STR("Invalid parameter: graph is canceled.");
820 graph_status
= BT_GRAPH_STATUS_CANCELED
;
824 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
825 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
826 type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
827 BT_LOGW("Invalid parameter: unknown component class type: "
829 graph_status
= BT_GRAPH_STATUS_INVALID
;
833 for (i
= 0; i
< graph
->components
->len
; i
++) {
834 void *other_comp
= graph
->components
->pdata
[i
];
836 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
837 BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
838 "other-comp-addr=%p, name=\"%s\"",
840 graph_status
= BT_GRAPH_STATUS_INVALID
;
846 * Parameters must be a map value, but we create a convenient
847 * empty one if it's NULL.
850 if (!bt_value_is_map(params
)) {
851 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
853 bt_value_type_string(bt_value_get_type(params
)));
854 graph_status
= BT_GRAPH_STATUS_INVALID
;
858 params
= bt_value_map_create();
860 BT_LOGE_STR("Cannot create map value object.");
861 graph_status
= BT_GRAPH_STATUS_NOMEM
;
866 comp_status
= bt_component_create(component_class
, name
, &component
);
867 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
868 BT_LOGE("Cannot create empty component object: status=%s",
869 bt_component_status_string(comp_status
));
870 graph_status
= bt_graph_status_from_component_status(
876 * The user's initialization method needs to see that this
877 * component is part of the graph. If the user method fails, we
878 * immediately remove the component from the graph's components.
880 g_ptr_array_add(graph
->components
, component
);
881 bt_component_set_graph(component
, graph
);
883 if (component_class
->methods
.init
) {
884 BT_LOGD_STR("Calling user's initialization method.");
885 comp_status
= component_class
->methods
.init(
886 bt_private_component_from_component(component
), params
,
888 BT_LOGD("User method returned: status=%s",
889 bt_component_status_string(comp_status
));
890 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
891 BT_LOGW_STR("Initialization method failed.");
892 graph_status
= bt_graph_status_from_component_status(
894 bt_component_set_graph(component
, NULL
);
895 g_ptr_array_remove_fast(graph
->components
, component
);
901 * Mark the component as initialized so that its finalization
902 * method is called when it is destroyed.
904 component
->initialized
= true;
907 * If it's a sink component, it needs to be part of the graph's
908 * sink queue to be consumed by bt_graph_consume().
910 if (bt_component_is_sink(component
)) {
911 g_queue_push_tail(graph
->sinks_to_consume
, component
);
915 * Freeze the component class now that it's instantiated at
918 BT_LOGD_STR("Freezing component class.");
919 bt_component_class_freeze(component
->class);
920 BT_LOGD("Added component to graph: "
921 "graph-addr=%p, comp-cls-addr=%p, "
922 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
923 "init-method-data-addr=%p, comp-addr=%p",
924 graph
, component_class
, bt_component_class_type_string(type
),
925 name
, params
, init_method_data
, component
);
927 if (user_component
) {
928 /* Move reference to user */
929 *user_component
= component
;
939 enum bt_graph_status
bt_graph_add_component(
940 struct bt_graph
*graph
,
941 struct bt_component_class
*component_class
,
942 const char *name
, struct bt_value
*params
,
943 struct bt_component
**component
)
945 return bt_graph_add_component_with_init_method_data(graph
,
946 component_class
, name
, params
, NULL
, component
);