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/graph/notification-internal.h>
39 #include <babeltrace/graph/notification-event-internal.h>
40 #include <babeltrace/graph/notification-packet-internal.h>
41 #include <babeltrace/compiler-internal.h>
42 #include <babeltrace/types.h>
43 #include <babeltrace/values.h>
44 #include <babeltrace/values-internal.h>
45 #include <babeltrace/assert-internal.h>
46 #include <babeltrace/assert-pre-internal.h>
50 struct bt_graph_listener
{
52 bt_graph_listener_removed removed
;
57 int init_listeners_array(GArray
**listeners
)
62 *listeners
= g_array_new(FALSE
, TRUE
, sizeof(struct bt_graph_listener
));
64 BT_LOGE_STR("Failed to allocate one GArray.");
74 void call_remove_listeners(GArray
*listeners
)
78 for (i
= 0; i
< listeners
->len
; i
++) {
79 struct bt_graph_listener listener
=
80 g_array_index(listeners
, struct bt_graph_listener
, i
);
82 if (listener
.removed
) {
83 listener
.removed(listener
.data
);
89 void bt_graph_destroy(struct bt_object
*obj
)
91 struct bt_graph
*graph
= container_of(obj
,
92 struct bt_graph
, base
);
95 * The graph's reference count is 0 if we're here. Increment
96 * it to avoid a double-destroy (possibly infinitely recursive)
99 * 1. We put and destroy a connection.
100 * 2. This connection's destructor finalizes its active
101 * notification iterators.
102 * 3. A notification iterator's finalization function gets a
103 * new reference on its component (reference count goes from
105 * 4. Since this component's reference count goes to 1, it takes
106 * a reference on its parent (this graph). This graph's
107 * reference count goes from 0 to 1.
108 * 5. The notification iterator's finalization function puts its
109 * component reference (reference count goes from 1 to 0).
110 * 6. Since this component's reference count goes from 1 to 0,
111 * it puts its parent (this graph). This graph's reference
112 * count goes from 1 to 0.
113 * 7. Since this graph's reference count goes from 1 to 0,
114 * its destructor is called (this function).
116 * With the incrementation below, the graph's reference count at
117 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
118 * ensures that this function is not called two times.
120 BT_LOGD("Destroying graph: addr=%p", graph
);
121 obj
->ref_count
.count
++;
124 * Cancel the graph to disallow some operations, like creating
125 * notification iterators and adding ports to components.
127 (void) bt_graph_cancel(graph
);
129 /* Call all remove listeners */
130 call_remove_listeners(graph
->listeners
.port_added
);
131 call_remove_listeners(graph
->listeners
.port_removed
);
132 call_remove_listeners(graph
->listeners
.ports_connected
);
133 call_remove_listeners(graph
->listeners
.ports_disconnected
);
135 if (graph
->notifications
) {
136 g_ptr_array_free(graph
->notifications
, TRUE
);
139 if (graph
->connections
) {
140 BT_LOGD_STR("Destroying connections.");
141 g_ptr_array_free(graph
->connections
, TRUE
);
144 if (graph
->components
) {
145 BT_LOGD_STR("Destroying components.");
146 g_ptr_array_free(graph
->components
, TRUE
);
149 if (graph
->sinks_to_consume
) {
150 g_queue_free(graph
->sinks_to_consume
);
153 if (graph
->listeners
.port_added
) {
154 g_array_free(graph
->listeners
.port_added
, TRUE
);
157 if (graph
->listeners
.port_removed
) {
158 g_array_free(graph
->listeners
.port_removed
, TRUE
);
161 if (graph
->listeners
.ports_connected
) {
162 g_array_free(graph
->listeners
.ports_connected
, TRUE
);
165 if (graph
->listeners
.ports_disconnected
) {
166 g_array_free(graph
->listeners
.ports_disconnected
, TRUE
);
169 bt_object_pool_finalize(&graph
->event_notif_pool
);
170 bt_object_pool_finalize(&graph
->packet_begin_notif_pool
);
171 bt_object_pool_finalize(&graph
->packet_end_notif_pool
);
176 void destroy_notification_event(struct bt_notification
*notif
,
177 struct bt_graph
*graph
)
179 bt_notification_event_destroy(notif
);
183 void destroy_notification_packet_begin(struct bt_notification
*notif
,
184 struct bt_graph
*graph
)
186 bt_notification_packet_begin_destroy(notif
);
190 void destroy_notification_packet_end(struct bt_notification
*notif
,
191 struct bt_graph
*graph
)
193 bt_notification_packet_end_destroy(notif
);
197 void notify_notification_graph_is_destroyed(struct bt_notification
*notif
)
199 bt_notification_unlink_graph(notif
);
202 struct bt_graph
*bt_graph_create(void)
204 struct bt_graph
*graph
;
207 BT_LOGD_STR("Creating graph object.");
208 graph
= g_new0(struct bt_graph
, 1);
210 BT_LOGE_STR("Failed to allocate one graph.");
214 bt_object_init(graph
, bt_graph_destroy
);
216 graph
->connections
= g_ptr_array_new_with_free_func(bt_object_release
);
217 if (!graph
->connections
) {
218 BT_LOGE_STR("Failed to allocate one GPtrArray.");
221 graph
->components
= g_ptr_array_new_with_free_func(bt_object_release
);
222 if (!graph
->components
) {
223 BT_LOGE_STR("Failed to allocate one GPtrArray.");
226 graph
->sinks_to_consume
= g_queue_new();
227 if (!graph
->sinks_to_consume
) {
228 BT_LOGE_STR("Failed to allocate one GQueue.");
232 graph
->can_consume
= BT_TRUE
;
233 ret
= init_listeners_array(&graph
->listeners
.port_added
);
235 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
239 ret
= init_listeners_array(&graph
->listeners
.port_removed
);
241 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
245 ret
= init_listeners_array(&graph
->listeners
.ports_connected
);
247 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
251 ret
= init_listeners_array(&graph
->listeners
.ports_disconnected
);
253 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
257 ret
= bt_object_pool_initialize(&graph
->event_notif_pool
,
258 (bt_object_pool_new_object_func
) bt_notification_event_new
,
259 (bt_object_pool_destroy_object_func
) destroy_notification_event
,
262 BT_LOGE("Failed to initialize event notification pool: ret=%d",
267 ret
= bt_object_pool_initialize(&graph
->packet_begin_notif_pool
,
268 (bt_object_pool_new_object_func
) bt_notification_packet_begin_new
,
269 (bt_object_pool_destroy_object_func
) destroy_notification_packet_begin
,
272 BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d",
277 ret
= bt_object_pool_initialize(&graph
->packet_end_notif_pool
,
278 (bt_object_pool_new_object_func
) bt_notification_packet_end_new
,
279 (bt_object_pool_destroy_object_func
) destroy_notification_packet_end
,
282 BT_LOGE("Failed to initialize packet end notification pool: ret=%d",
287 graph
->notifications
= g_ptr_array_new_with_free_func(
288 (GDestroyNotify
) notify_notification_graph_is_destroyed
);
289 BT_LOGD("Created graph object: addr=%p", graph
);
298 enum bt_graph_status
bt_graph_connect_ports(struct bt_graph
*graph
,
299 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
300 struct bt_connection
**user_connection
)
302 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
303 struct bt_connection
*connection
= NULL
;
304 struct bt_graph
*upstream_graph
= NULL
;
305 struct bt_graph
*downstream_graph
= NULL
;
306 struct bt_component
*upstream_component
= NULL
;
307 struct bt_component
*downstream_component
= NULL
;
308 enum bt_component_status component_status
;
309 bt_bool init_can_consume
;
312 BT_LOGW_STR("Invalid parameter: graph is NULL.");
313 status
= BT_GRAPH_STATUS_INVALID
;
316 init_can_consume
= graph
->can_consume
;
318 if (!upstream_port
) {
319 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
320 status
= BT_GRAPH_STATUS_INVALID
;
324 if (!downstream_port
) {
325 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
326 status
= BT_GRAPH_STATUS_INVALID
;
330 BT_LOGD("Connecting component ports within graph: "
332 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
333 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
334 graph
, upstream_port
, bt_port_get_name(upstream_port
),
335 downstream_port
, bt_port_get_name(downstream_port
));
337 if (graph
->canceled
) {
338 BT_LOGW_STR("Invalid parameter: graph is canceled.");
339 status
= BT_GRAPH_STATUS_CANCELED
;
343 graph
->can_consume
= BT_FALSE
;
345 /* Ensure appropriate types for upstream and downstream ports. */
346 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
347 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
348 status
= BT_GRAPH_STATUS_INVALID
;
351 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
352 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
353 status
= BT_GRAPH_STATUS_INVALID
;
357 /* Ensure that both ports are currently unconnected. */
358 if (bt_port_is_connected(upstream_port
)) {
359 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
360 status
= BT_GRAPH_STATUS_INVALID
;
364 if (bt_port_is_connected(downstream_port
)) {
365 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
366 status
= BT_GRAPH_STATUS_INVALID
;
371 * Ensure that both ports are still attached to their creating
374 upstream_component
= bt_port_get_component(upstream_port
);
375 if (!upstream_component
) {
376 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
377 status
= BT_GRAPH_STATUS_INVALID
;
381 downstream_component
= bt_port_get_component(downstream_port
);
382 if (!downstream_component
) {
383 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
384 status
= BT_GRAPH_STATUS_INVALID
;
388 BT_LOGD("Connecting component ports: "
389 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
390 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
391 upstream_component
, bt_component_get_name(upstream_component
),
392 downstream_component
, bt_component_get_name(downstream_component
));
395 * At this point the ports are not connected yet. Both
396 * components need to accept an eventual connection to their
397 * port by the other port before we continue.
399 BT_LOGD_STR("Asking upstream component to accept the connection.");
400 component_status
= bt_component_accept_port_connection(
401 upstream_component
, upstream_port
, downstream_port
);
402 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
403 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
404 BT_LOGD_STR("Upstream component refused the connection.");
406 BT_LOGW("Cannot ask upstream component to accept the connection: "
407 "status=%s", bt_component_status_string(component_status
));
410 status
= bt_graph_status_from_component_status(
415 BT_LOGD_STR("Asking downstream component to accept the connection.");
416 component_status
= bt_component_accept_port_connection(
417 downstream_component
, downstream_port
, upstream_port
);
418 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
419 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
420 BT_LOGD_STR("Downstream component refused the connection.");
422 BT_LOGW("Cannot ask downstream component to accept the connection: "
423 "status=%s", bt_component_status_string(component_status
));
426 status
= bt_graph_status_from_component_status(
431 BT_LOGD_STR("Creating connection.");
432 connection
= bt_connection_create(graph
, upstream_port
,
435 BT_LOGW("Cannot create connection object.");
436 status
= BT_GRAPH_STATUS_NOMEM
;
440 BT_LOGD("Connection object created: conn-addr=%p", connection
);
443 * Ownership of upstream_component/downstream_component and of
444 * the connection object is transferred to the graph.
446 g_ptr_array_add(graph
->connections
, connection
);
449 * Notify both components that their port is connected.
451 BT_LOGD_STR("Notifying upstream component that its port is connected.");
452 bt_component_port_connected(upstream_component
, upstream_port
,
454 BT_LOGD_STR("Notifying downstream component that its port is connected.");
455 bt_component_port_connected(downstream_component
, downstream_port
,
459 * Notify the graph's creator that both ports are connected.
461 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
462 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
463 BT_LOGD("Connected component ports within graph: "
465 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
466 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
467 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
468 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
470 upstream_component
, bt_component_get_name(upstream_component
),
471 downstream_component
, bt_component_get_name(downstream_component
),
472 upstream_port
, bt_port_get_name(upstream_port
),
473 downstream_port
, bt_port_get_name(downstream_port
));
475 if (user_connection
) {
476 /* Move reference to user */
477 *user_connection
= connection
;
482 bt_put(upstream_graph
);
483 bt_put(downstream_graph
);
484 bt_put(upstream_component
);
485 bt_put(downstream_component
);
488 graph
->can_consume
= init_can_consume
;
494 enum bt_graph_status
consume_graph_sink(struct bt_component
*sink
)
496 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
497 enum bt_component_status comp_status
;
500 comp_status
= bt_component_sink_consume(sink
);
501 BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s",
502 sink
, bt_component_get_name(sink
),
503 bt_component_status_string(comp_status
));
505 switch (comp_status
) {
506 case BT_COMPONENT_STATUS_OK
:
508 case BT_COMPONENT_STATUS_END
:
509 status
= BT_GRAPH_STATUS_END
;
511 case BT_COMPONENT_STATUS_AGAIN
:
512 status
= BT_GRAPH_STATUS_AGAIN
;
514 case BT_COMPONENT_STATUS_INVALID
:
515 status
= BT_GRAPH_STATUS_INVALID
;
518 status
= BT_GRAPH_STATUS_ERROR
;
526 * `node` is removed from the queue of sinks to consume when passed to
527 * this function. This function adds it back to the queue if there's
528 * still something to consume afterwards.
531 enum bt_graph_status
consume_sink_node(struct bt_graph
*graph
,
534 enum bt_graph_status status
;
535 struct bt_component
*sink
;
538 status
= consume_graph_sink(sink
);
539 if (status
!= BT_GRAPH_STATUS_END
) {
540 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
544 /* End reached, the node is not added back to the queue and free'd. */
545 g_queue_delete_link(graph
->sinks_to_consume
, node
);
547 /* Don't forward an END status if there are sinks left to consume. */
548 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
549 status
= BT_GRAPH_STATUS_OK
;
554 BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status
));
559 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
560 struct bt_component
*sink
)
562 enum bt_graph_status status
;
566 BT_LOGV("Making specific sink consume: addr=%p, "
567 "comp-addr=%p, comp-name=\"%s\"",
568 graph
, sink
, bt_component_get_name(sink
));
570 BT_ASSERT(bt_component_borrow_graph(sink
) == graph
);
572 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
573 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
574 status
= BT_GRAPH_STATUS_END
;
578 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
580 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
581 status
= BT_GRAPH_STATUS_END
;
585 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
586 BT_ASSERT(sink_node
);
587 status
= consume_sink_node(graph
, sink_node
);
594 enum bt_graph_status
bt_graph_consume_no_check(struct bt_graph
*graph
)
596 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
597 struct bt_component
*sink
;
600 BT_LOGV("Making next sink consume: addr=%p", graph
);
601 BT_ASSERT_PRE(graph
->has_sink
,
602 "Graph has no sink component: %!+g", graph
);
604 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
605 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
606 status
= BT_GRAPH_STATUS_END
;
610 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
611 sink
= current_node
->data
;
612 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
613 sink
, bt_component_get_name(sink
));
614 status
= consume_sink_node(graph
, current_node
);
620 enum bt_graph_status
bt_graph_consume(struct bt_graph
*graph
)
622 enum bt_graph_status status
;
624 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
625 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
626 BT_ASSERT_PRE(graph
->can_consume
,
627 "Cannot consume graph in its current state: %!+g", graph
);
628 graph
->can_consume
= BT_FALSE
;
629 status
= bt_graph_consume_no_check(graph
);
630 graph
->can_consume
= BT_TRUE
;
634 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
636 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
639 BT_LOGW_STR("Invalid parameter: graph is NULL.");
640 status
= BT_GRAPH_STATUS_INVALID
;
644 if (graph
->canceled
) {
645 BT_LOGW("Invalid parameter: graph is canceled: "
646 "graph-addr=%p", graph
);
647 status
= BT_GRAPH_STATUS_CANCELED
;
651 if (!graph
->can_consume
) {
652 BT_LOGW_STR("Cannot run graph in its current state.");
653 status
= BT_GRAPH_STATUS_CANNOT_CONSUME
;
657 graph
->can_consume
= BT_FALSE
;
658 BT_LOGV("Running graph: addr=%p", graph
);
662 * Check if the graph is canceled at each iteration. If
663 * the graph was canceled by another thread or by a
664 * signal, this is not a warning nor an error, it was
665 * intentional: log with a DEBUG level only.
667 if (graph
->canceled
) {
668 BT_LOGD("Stopping the graph: graph is canceled: "
669 "graph-addr=%p", graph
);
670 status
= BT_GRAPH_STATUS_CANCELED
;
674 status
= bt_graph_consume_no_check(graph
);
675 if (status
== BT_GRAPH_STATUS_AGAIN
) {
677 * If AGAIN is received and there are multiple
678 * sinks, go ahead and consume from the next
681 * However, in the case where a single sink is
682 * left, the caller can decide to busy-wait and
683 * call bt_graph_run() continuously until the
684 * source is ready or it can decide to sleep for
685 * an arbitrary amount of time.
687 if (graph
->sinks_to_consume
->length
> 1) {
688 status
= BT_GRAPH_STATUS_OK
;
690 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
693 } while (status
== BT_GRAPH_STATUS_OK
);
695 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
696 status
= BT_GRAPH_STATUS_END
;
700 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status
));
702 graph
->can_consume
= BT_TRUE
;
708 int add_listener(GArray
*listeners
, void *func
, void *removed
, void *data
)
710 struct bt_graph_listener listener
= {
716 g_array_append_val(listeners
, listener
);
717 return listeners
->len
- 1;
720 int bt_graph_add_port_added_listener(
721 struct bt_graph
*graph
,
722 bt_graph_port_added_listener listener
,
723 bt_graph_listener_removed listener_removed
, void *data
)
728 BT_LOGW_STR("Invalid parameter: graph is NULL.");
733 if (graph
->in_remove_listener
) {
734 BT_LOGW("Cannot call this function during the execution of a remove listener: "
741 BT_LOGW_STR("Invalid parameter: listener is NULL.");
746 ret
= add_listener(graph
->listeners
.port_added
, listener
,
747 listener_removed
, data
);
748 BT_LOGV("Added \"port added\" listener to graph: "
749 "graph-addr=%p, listener-addr=%p, pos=%d",
750 graph
, listener
, ret
);
756 int bt_graph_add_port_removed_listener(
757 struct bt_graph
*graph
,
758 bt_graph_port_removed_listener listener
,
759 bt_graph_listener_removed listener_removed
, void *data
)
764 BT_LOGW_STR("Invalid parameter: graph is NULL.");
769 if (graph
->in_remove_listener
) {
770 BT_LOGW("Cannot call this function during the execution of a remove listener: "
777 BT_LOGW_STR("Invalid parameter: listener is NULL.");
782 ret
= add_listener(graph
->listeners
.port_removed
, listener
,
783 listener_removed
, data
);
784 BT_LOGV("Added \"port removed\" listener to graph: "
785 "graph-addr=%p, listener-addr=%p, pos=%d",
786 graph
, listener
, ret
);
792 int bt_graph_add_ports_connected_listener(
793 struct bt_graph
*graph
,
794 bt_graph_ports_connected_listener listener
,
795 bt_graph_listener_removed listener_removed
, void *data
)
800 BT_LOGW_STR("Invalid parameter: graph is NULL.");
805 if (graph
->in_remove_listener
) {
806 BT_LOGW("Cannot call this function during the execution of a remove listener: "
813 BT_LOGW_STR("Invalid parameter: listener is NULL.");
818 ret
= add_listener(graph
->listeners
.ports_connected
, listener
,
819 listener_removed
, data
);
820 BT_LOGV("Added \"port connected\" listener to graph: "
821 "graph-addr=%p, listener-addr=%p, pos=%d",
822 graph
, listener
, ret
);
828 int bt_graph_add_ports_disconnected_listener(
829 struct bt_graph
*graph
,
830 bt_graph_ports_disconnected_listener listener
,
831 bt_graph_listener_removed listener_removed
, void *data
)
836 BT_LOGW_STR("Invalid parameter: graph is NULL.");
841 if (graph
->in_remove_listener
) {
842 BT_LOGW("Cannot call this function during the execution of a remove listener: "
849 BT_LOGW_STR("Invalid parameter: listener is NULL.");
854 ret
= add_listener(graph
->listeners
.ports_disconnected
, listener
,
855 listener_removed
, data
);
856 BT_LOGV("Added \"port disconnected\" listener to graph: "
857 "graph-addr=%p, listener-addr=%p, pos=%d",
858 graph
, listener
, ret
);
865 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
869 BT_LOGV("Notifying graph listeners that a port was added: "
870 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
871 graph
, port
, bt_port_get_name(port
));
873 for (i
= 0; i
< graph
->listeners
.port_added
->len
; i
++) {
874 struct bt_graph_listener listener
=
875 g_array_index(graph
->listeners
.port_added
,
876 struct bt_graph_listener
, i
);
877 bt_graph_port_added_listener func
= listener
.func
;
880 func(port
, listener
.data
);
885 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
886 struct bt_component
*comp
, struct bt_port
*port
)
890 BT_LOGV("Notifying graph listeners that a port was removed: "
891 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
892 graph
, port
, bt_port_get_name(port
));
894 for (i
= 0; i
< graph
->listeners
.port_removed
->len
; i
++) {
895 struct bt_graph_listener listener
=
896 g_array_index(graph
->listeners
.port_removed
,
897 struct bt_graph_listener
, i
);
898 bt_graph_port_removed_listener func
= listener
.func
;
901 func(comp
, port
, listener
.data
);
906 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
907 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
911 BT_LOGV("Notifying graph listeners that two ports were connected: "
913 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
914 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
915 graph
, upstream_port
, bt_port_get_name(upstream_port
),
916 downstream_port
, bt_port_get_name(downstream_port
));
918 for (i
= 0; i
< graph
->listeners
.ports_connected
->len
; i
++) {
919 struct bt_graph_listener listener
=
920 g_array_index(graph
->listeners
.ports_connected
,
921 struct bt_graph_listener
, i
);
922 bt_graph_ports_connected_listener func
= listener
.func
;
925 func(upstream_port
, downstream_port
, listener
.data
);
930 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
931 struct bt_component
*upstream_comp
,
932 struct bt_component
*downstream_comp
,
933 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
937 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
939 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
940 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
941 graph
, upstream_port
, bt_port_get_name(upstream_port
),
942 downstream_port
, bt_port_get_name(downstream_port
));
944 for (i
= 0; i
< graph
->listeners
.ports_disconnected
->len
; i
++) {
945 struct bt_graph_listener listener
=
946 g_array_index(graph
->listeners
.ports_disconnected
,
947 struct bt_graph_listener
, i
);
948 bt_graph_ports_disconnected_listener func
= listener
.func
;
951 func(upstream_comp
, downstream_comp
, upstream_port
,
952 downstream_port
, listener
.data
);
956 enum bt_graph_status
bt_graph_cancel(struct bt_graph
*graph
)
958 enum bt_graph_status ret
= BT_GRAPH_STATUS_OK
;
961 BT_LOGW_STR("Invalid parameter: graph is NULL.");
962 ret
= BT_GRAPH_STATUS_INVALID
;
966 graph
->canceled
= BT_TRUE
;
967 BT_LOGV("Canceled graph: addr=%p", graph
);
973 bt_bool
bt_graph_is_canceled(struct bt_graph
*graph
)
975 bt_bool canceled
= BT_FALSE
;
978 BT_LOGW_STR("Invalid parameter: graph is NULL.");
982 canceled
= graph
->canceled
;
989 void bt_graph_remove_connection(struct bt_graph
*graph
,
990 struct bt_connection
*connection
)
993 BT_ASSERT(connection
);
994 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
996 g_ptr_array_remove(graph
->connections
, connection
);
999 enum bt_graph_status
bt_graph_add_component_with_init_method_data(
1000 struct bt_graph
*graph
,
1001 struct bt_component_class
*component_class
,
1002 const char *name
, struct bt_value
*params
,
1003 void *init_method_data
,
1004 struct bt_component
**user_component
)
1006 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
1007 enum bt_component_status comp_status
;
1008 struct bt_component
*component
= NULL
;
1009 enum bt_component_class_type type
;
1011 bt_bool init_can_consume
;
1016 BT_LOGW_STR("Invalid parameter: graph is NULL.");
1017 graph_status
= BT_GRAPH_STATUS_INVALID
;
1020 init_can_consume
= graph
->can_consume
;
1022 if (!component_class
) {
1023 BT_LOGW_STR("Invalid parameter: component class is NULL.");
1024 graph_status
= BT_GRAPH_STATUS_INVALID
;
1028 graph
->can_consume
= BT_FALSE
;
1029 type
= bt_component_class_get_type(component_class
);
1030 BT_LOGD("Adding component to graph: "
1031 "graph-addr=%p, comp-cls-addr=%p, "
1032 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
1033 "init-method-data-addr=%p",
1034 graph
, component_class
, bt_component_class_type_string(type
),
1035 name
, params
, init_method_data
);
1038 BT_LOGW_STR("Invalid parameter: name is NULL.");
1039 graph_status
= BT_GRAPH_STATUS_INVALID
;
1043 if (graph
->canceled
) {
1044 BT_LOGW_STR("Invalid parameter: graph is canceled.");
1045 graph_status
= BT_GRAPH_STATUS_CANCELED
;
1049 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
1050 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
1051 type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
1052 BT_LOGW("Invalid parameter: unknown component class type: "
1054 graph_status
= BT_GRAPH_STATUS_INVALID
;
1058 for (i
= 0; i
< graph
->components
->len
; i
++) {
1059 void *other_comp
= graph
->components
->pdata
[i
];
1061 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1062 BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
1063 "other-comp-addr=%p, name=\"%s\"",
1065 graph_status
= BT_GRAPH_STATUS_INVALID
;
1071 * Parameters must be a map value, but we create a convenient
1072 * empty one if it's NULL.
1075 if (!bt_value_is_map(params
)) {
1076 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
1078 bt_value_type_string(bt_value_get_type(params
)));
1079 graph_status
= BT_GRAPH_STATUS_INVALID
;
1083 params
= bt_value_map_create();
1085 BT_LOGE_STR("Cannot create map value object.");
1086 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1091 comp_status
= bt_component_create(component_class
, name
, &component
);
1092 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
1093 BT_LOGE("Cannot create empty component object: status=%s",
1094 bt_component_status_string(comp_status
));
1095 graph_status
= bt_graph_status_from_component_status(
1101 * The user's initialization method needs to see that this
1102 * component is part of the graph. If the user method fails, we
1103 * immediately remove the component from the graph's components.
1105 g_ptr_array_add(graph
->components
, component
);
1106 bt_component_set_graph(component
, graph
);
1108 if (component_class
->methods
.init
) {
1109 BT_LOGD_STR("Calling user's initialization method.");
1110 comp_status
= component_class
->methods
.init(
1111 bt_private_component_from_component(component
), params
,
1113 BT_LOGD("User method returned: status=%s",
1114 bt_component_status_string(comp_status
));
1115 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
1116 BT_LOGW_STR("Initialization method failed.");
1117 graph_status
= bt_graph_status_from_component_status(
1119 bt_component_set_graph(component
, NULL
);
1120 g_ptr_array_remove_fast(graph
->components
, component
);
1126 * Mark the component as initialized so that its finalization
1127 * method is called when it is destroyed.
1129 component
->initialized
= true;
1132 * If it's a sink component, it needs to be part of the graph's
1133 * sink queue to be consumed by bt_graph_consume().
1135 if (bt_component_is_sink(component
)) {
1136 graph
->has_sink
= BT_TRUE
;
1137 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1141 * Freeze the component class now that it's instantiated at
1144 BT_LOGD_STR("Freezing component class.");
1145 bt_component_class_freeze(component
->class);
1146 BT_LOGD("Added component to graph: "
1147 "graph-addr=%p, comp-cls-addr=%p, "
1148 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
1149 "init-method-data-addr=%p, comp-addr=%p",
1150 graph
, component_class
, bt_component_class_type_string(type
),
1151 name
, params
, init_method_data
, component
);
1153 if (user_component
) {
1154 /* Move reference to user */
1155 *user_component
= component
;
1163 graph
->can_consume
= init_can_consume
;
1165 return graph_status
;
1168 enum bt_graph_status
bt_graph_add_component(
1169 struct bt_graph
*graph
,
1170 struct bt_component_class
*component_class
,
1171 const char *name
, struct bt_value
*params
,
1172 struct bt_component
**component
)
1174 return bt_graph_add_component_with_init_method_data(graph
,
1175 component_class
, name
, params
, NULL
, component
);
1179 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1180 struct bt_component
*component
)
1182 bt_bool init_can_consume
;
1188 BT_ASSERT(component
);
1189 BT_ASSERT(component
->base
.ref_count
.count
== 0);
1190 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1192 init_can_consume
= graph
->can_consume
;
1193 count
= bt_component_get_input_port_count(component
);
1195 for (i
= 0; i
< count
; i
++) {
1196 struct bt_port
*port
=
1197 bt_component_get_input_port_by_index(component
, i
);
1202 if (bt_port_is_connected(port
)) {
1203 BT_LOGW("Cannot remove component from graph: "
1204 "an input port is connected: "
1205 "graph-addr=%p, comp-addr=%p, "
1206 "comp-name=\"%s\", connected-port-addr=%p, "
1207 "connected-port-name=\"%s\"",
1209 bt_component_get_name(component
),
1210 port
, bt_port_get_name(port
));
1215 count
= bt_component_get_output_port_count(component
);
1217 for (i
= 0; i
< count
; i
++) {
1218 struct bt_port
*port
=
1219 bt_component_get_output_port_by_index(component
, i
);
1224 if (bt_port_is_connected(port
)) {
1225 BT_LOGW("Cannot remove component from graph: "
1226 "an output port is connected: "
1227 "graph-addr=%p, comp-addr=%p, "
1228 "comp-name=\"%s\", connected-port-addr=%p, "
1229 "connected-port-name=\"%s\"",
1231 bt_component_get_name(component
),
1232 port
, bt_port_get_name(port
));
1237 graph
->can_consume
= BT_FALSE
;
1239 /* Possibly remove from sinks to consume */
1240 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1242 if (graph
->sinks_to_consume
->length
== 0) {
1243 graph
->has_sink
= BT_FALSE
;
1247 * This calls bt_object_release() on the component, and since
1248 * its reference count is 0, its destructor is called. Its
1249 * destructor calls the user's finalization method (if set).
1251 g_ptr_array_remove(graph
->components
, component
);
1258 graph
->can_consume
= init_can_consume
;
1263 void bt_graph_add_notification(struct bt_graph
*graph
,
1264 struct bt_notification
*notif
)
1270 * It's okay not to take a reference because, when a
1271 * notification's reference count drops to 0, either:
1273 * * It is recycled back to one of this graph's pool.
1274 * * It is destroyed because it doesn't have any link to any
1275 * graph, which means the original graph is already destroyed.
1277 g_ptr_array_add(graph
->notifications
, notif
);