2 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "GRAPH"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/graph/component-internal.h>
28 #include <babeltrace/graph/private-graph.h>
29 #include <babeltrace/graph/graph-internal.h>
30 #include <babeltrace/graph/connection-internal.h>
31 #include <babeltrace/graph/component-sink-internal.h>
32 #include <babeltrace/graph/component-source.h>
33 #include <babeltrace/graph/component-filter.h>
34 #include <babeltrace/graph/port.h>
35 #include <babeltrace/graph/notification-internal.h>
36 #include <babeltrace/graph/notification-event-internal.h>
37 #include <babeltrace/graph/notification-packet-internal.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/common-internal.h>
40 #include <babeltrace/types.h>
41 #include <babeltrace/values.h>
42 #include <babeltrace/private-values.h>
43 #include <babeltrace/values-internal.h>
44 #include <babeltrace/assert-internal.h>
45 #include <babeltrace/assert-pre-internal.h>
49 struct bt_graph_listener
{
51 bt_private_graph_listener_removed removed
;
56 int init_listeners_array(GArray
**listeners
)
61 *listeners
= g_array_new(FALSE
, TRUE
, sizeof(struct bt_graph_listener
));
63 BT_LOGE_STR("Failed to allocate one GArray.");
73 void call_remove_listeners(GArray
*listeners
)
77 for (i
= 0; i
< listeners
->len
; i
++) {
78 struct bt_graph_listener listener
=
79 g_array_index(listeners
, struct bt_graph_listener
, i
);
81 if (listener
.removed
) {
82 listener
.removed(listener
.data
);
88 void bt_graph_destroy(struct bt_object
*obj
)
90 struct bt_graph
*graph
= container_of(obj
,
91 struct bt_graph
, base
);
94 * The graph's reference count is 0 if we're here. Increment
95 * it to avoid a double-destroy (possibly infinitely recursive)
98 * 1. We put and destroy a connection.
99 * 2. This connection's destructor finalizes its active
100 * notification iterators.
101 * 3. A notification iterator's finalization function gets a
102 * new reference on its component (reference count goes from
104 * 4. Since this component's reference count goes to 1, it takes
105 * a reference on its parent (this graph). This graph's
106 * reference count goes from 0 to 1.
107 * 5. The notification iterator's finalization function puts its
108 * component reference (reference count goes from 1 to 0).
109 * 6. Since this component's reference count goes from 1 to 0,
110 * it puts its parent (this graph). This graph's reference
111 * count goes from 1 to 0.
112 * 7. Since this graph's reference count goes from 1 to 0,
113 * its destructor is called (this function).
115 * With the incrementation below, the graph's reference count at
116 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
117 * ensures that this function is not called two times.
119 BT_LOGD("Destroying graph: addr=%p", graph
);
123 * Cancel the graph to disallow some operations, like creating
124 * notification iterators and adding ports to components.
126 (void) bt_private_graph_cancel((void *) graph
);
128 /* Call all remove listeners */
129 call_remove_listeners(graph
->listeners
.port_added
);
130 call_remove_listeners(graph
->listeners
.port_removed
);
131 call_remove_listeners(graph
->listeners
.ports_connected
);
132 call_remove_listeners(graph
->listeners
.ports_disconnected
);
134 if (graph
->notifications
) {
135 g_ptr_array_free(graph
->notifications
, TRUE
);
138 if (graph
->connections
) {
139 BT_LOGD_STR("Destroying connections.");
140 g_ptr_array_free(graph
->connections
, TRUE
);
143 if (graph
->components
) {
144 BT_LOGD_STR("Destroying components.");
145 g_ptr_array_free(graph
->components
, TRUE
);
148 if (graph
->sinks_to_consume
) {
149 g_queue_free(graph
->sinks_to_consume
);
152 if (graph
->listeners
.port_added
) {
153 g_array_free(graph
->listeners
.port_added
, TRUE
);
156 if (graph
->listeners
.port_removed
) {
157 g_array_free(graph
->listeners
.port_removed
, TRUE
);
160 if (graph
->listeners
.ports_connected
) {
161 g_array_free(graph
->listeners
.ports_connected
, TRUE
);
164 if (graph
->listeners
.ports_disconnected
) {
165 g_array_free(graph
->listeners
.ports_disconnected
, TRUE
);
168 bt_object_pool_finalize(&graph
->event_notif_pool
);
169 bt_object_pool_finalize(&graph
->packet_begin_notif_pool
);
170 bt_object_pool_finalize(&graph
->packet_end_notif_pool
);
175 void destroy_notification_event(struct bt_notification
*notif
,
176 struct bt_graph
*graph
)
178 bt_notification_event_destroy(notif
);
182 void destroy_notification_packet_begin(struct bt_notification
*notif
,
183 struct bt_graph
*graph
)
185 bt_notification_packet_begin_destroy(notif
);
189 void destroy_notification_packet_end(struct bt_notification
*notif
,
190 struct bt_graph
*graph
)
192 bt_notification_packet_end_destroy(notif
);
196 void notify_notification_graph_is_destroyed(struct bt_notification
*notif
)
198 bt_notification_unlink_graph(notif
);
201 struct bt_private_graph
*bt_private_graph_create(void)
203 struct bt_graph
*graph
;
206 BT_LOGD_STR("Creating graph object.");
207 graph
= g_new0(struct bt_graph
, 1);
209 BT_LOGE_STR("Failed to allocate one graph.");
213 bt_object_init_shared(&graph
->base
, bt_graph_destroy
);
214 graph
->connections
= g_ptr_array_new_with_free_func(
215 (GDestroyNotify
) bt_object_try_spec_release
);
216 if (!graph
->connections
) {
217 BT_LOGE_STR("Failed to allocate one GPtrArray.");
220 graph
->components
= g_ptr_array_new_with_free_func(
221 (GDestroyNotify
) bt_object_try_spec_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 bt_graph_set_can_consume(graph
, 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
);
292 return (void *) graph
;
294 BT_OBJECT_PUT_REF_AND_RESET(graph
);
298 enum bt_graph_status
bt_private_graph_connect_ports(
299 struct bt_private_graph
*priv_graph
,
300 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
301 struct bt_connection
**user_connection
)
303 struct bt_graph
*graph
= (void *) priv_graph
;
304 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
305 struct bt_connection
*connection
= NULL
;
306 struct bt_graph
*upstream_graph
= NULL
;
307 struct bt_graph
*downstream_graph
= NULL
;
308 struct bt_component
*upstream_component
= NULL
;
309 struct bt_component
*downstream_component
= NULL
;
310 enum bt_component_status component_status
;
311 bt_bool init_can_consume
;
314 BT_LOGW_STR("Invalid parameter: graph is NULL.");
315 status
= BT_GRAPH_STATUS_INVALID
;
318 init_can_consume
= graph
->can_consume
;
320 if (!upstream_port
) {
321 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
322 status
= BT_GRAPH_STATUS_INVALID
;
326 if (!downstream_port
) {
327 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
328 status
= BT_GRAPH_STATUS_INVALID
;
332 BT_LOGD("Connecting component ports within graph: "
334 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
335 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
336 graph
, upstream_port
, bt_port_get_name(upstream_port
),
337 downstream_port
, bt_port_get_name(downstream_port
));
339 if (graph
->canceled
) {
340 BT_LOGW_STR("Invalid parameter: graph is canceled.");
341 status
= BT_GRAPH_STATUS_CANCELED
;
345 bt_graph_set_can_consume(graph
, BT_FALSE
);
347 /* Ensure appropriate types for upstream and downstream ports. */
348 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
349 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
350 status
= BT_GRAPH_STATUS_INVALID
;
353 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
354 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
355 status
= BT_GRAPH_STATUS_INVALID
;
359 /* Ensure that both ports are currently unconnected. */
360 if (bt_port_is_connected(upstream_port
)) {
361 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
362 status
= BT_GRAPH_STATUS_INVALID
;
366 if (bt_port_is_connected(downstream_port
)) {
367 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
368 status
= BT_GRAPH_STATUS_INVALID
;
373 * Ensure that both ports are still attached to their creating
376 upstream_component
= bt_port_get_component(upstream_port
);
377 if (!upstream_component
) {
378 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
379 status
= BT_GRAPH_STATUS_INVALID
;
383 downstream_component
= bt_port_get_component(downstream_port
);
384 if (!downstream_component
) {
385 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
386 status
= BT_GRAPH_STATUS_INVALID
;
390 BT_LOGD("Connecting component ports: "
391 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
392 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
393 upstream_component
, bt_component_get_name(upstream_component
),
394 downstream_component
, bt_component_get_name(downstream_component
));
397 * At this point the ports are not connected yet. Both
398 * components need to accept an eventual connection to their
399 * port by the other port before we continue.
401 BT_LOGD_STR("Asking upstream component to accept the connection.");
402 component_status
= bt_component_accept_port_connection(
403 upstream_component
, upstream_port
, downstream_port
);
404 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
405 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
406 BT_LOGD_STR("Upstream component refused the connection.");
408 BT_LOGW("Cannot ask upstream component to accept the connection: "
409 "status=%s", bt_component_status_string(component_status
));
412 status
= bt_graph_status_from_component_status(
417 BT_LOGD_STR("Asking downstream component to accept the connection.");
418 component_status
= bt_component_accept_port_connection(
419 downstream_component
, downstream_port
, upstream_port
);
420 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
421 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
422 BT_LOGD_STR("Downstream component refused the connection.");
424 BT_LOGW("Cannot ask downstream component to accept the connection: "
425 "status=%s", bt_component_status_string(component_status
));
428 status
= bt_graph_status_from_component_status(
433 BT_LOGD_STR("Creating connection.");
434 connection
= bt_connection_create(graph
, upstream_port
,
437 BT_LOGW("Cannot create connection object.");
438 status
= BT_GRAPH_STATUS_NOMEM
;
442 BT_LOGD("Connection object created: conn-addr=%p", connection
);
445 * Ownership of upstream_component/downstream_component and of
446 * the connection object is transferred to the graph.
448 g_ptr_array_add(graph
->connections
, connection
);
451 * Notify both components that their port is connected.
453 BT_LOGD_STR("Notifying upstream component that its port is connected.");
454 component_status
= bt_component_port_connected(upstream_component
,
455 upstream_port
, downstream_port
);
456 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
457 BT_LOGW("Error while notifying upstream component that its port is connected: "
458 "status=%s, graph-addr=%p, "
459 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
460 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
461 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
462 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
463 bt_component_status_string(component_status
), graph
,
464 upstream_component
, bt_component_get_name(upstream_component
),
465 downstream_component
, bt_component_get_name(downstream_component
),
466 upstream_port
, bt_port_get_name(upstream_port
),
467 downstream_port
, bt_port_get_name(downstream_port
));
468 bt_connection_end(connection
, true);
469 status
= bt_graph_status_from_component_status(
474 connection
->notified_upstream_port_connected
= true;
475 BT_LOGD_STR("Notifying downstream component that its port is connected.");
476 component_status
= bt_component_port_connected(downstream_component
,
477 downstream_port
, upstream_port
);
478 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
479 BT_LOGW("Error while notifying downstream component that its port is connected: "
480 "status=%s, graph-addr=%p, "
481 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
482 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
483 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
484 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
485 bt_component_status_string(component_status
), graph
,
486 upstream_component
, bt_component_get_name(upstream_component
),
487 downstream_component
, bt_component_get_name(downstream_component
),
488 upstream_port
, bt_port_get_name(upstream_port
),
489 downstream_port
, bt_port_get_name(downstream_port
));
490 bt_connection_end(connection
, true);
491 status
= bt_graph_status_from_component_status(
496 connection
->notified_downstream_port_connected
= true;
499 * Notify the graph's creator that both ports are connected.
501 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
502 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
503 connection
->notified_graph_ports_connected
= true;
504 BT_LOGD("Connected component ports within graph: "
506 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
507 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
508 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
509 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
511 upstream_component
, bt_component_get_name(upstream_component
),
512 downstream_component
, bt_component_get_name(downstream_component
),
513 upstream_port
, bt_port_get_name(upstream_port
),
514 downstream_port
, bt_port_get_name(downstream_port
));
516 if (user_connection
) {
517 /* Move reference to user */
518 *user_connection
= connection
;
523 bt_object_put_ref(upstream_graph
);
524 bt_object_put_ref(downstream_graph
);
525 bt_object_put_ref(upstream_component
);
526 bt_object_put_ref(downstream_component
);
527 bt_object_put_ref(connection
);
529 (void) init_can_consume
;
530 bt_graph_set_can_consume(graph
, init_can_consume
);
536 enum bt_graph_status
consume_graph_sink(struct bt_component
*sink
)
538 enum bt_component_status comp_status
;
541 comp_status
= bt_component_sink_consume(sink
);
542 BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s",
543 sink
, bt_component_get_name(sink
),
544 bt_component_status_string(comp_status
));
545 BT_ASSERT_PRE(comp_status
== BT_COMPONENT_STATUS_OK
||
546 comp_status
== BT_COMPONENT_STATUS_END
||
547 comp_status
== BT_COMPONENT_STATUS_AGAIN
||
548 comp_status
== BT_COMPONENT_STATUS_ERROR
||
549 comp_status
== BT_COMPONENT_STATUS_NOMEM
,
550 "Invalid component status returned by consuming function: "
551 "status=%s", bt_component_status_string(comp_status
));
552 return (enum bt_graph_status
) comp_status
;
556 * `node` is removed from the queue of sinks to consume when passed to
557 * this function. This function adds it back to the queue if there's
558 * still something to consume afterwards.
561 enum bt_graph_status
consume_sink_node(struct bt_graph
*graph
,
564 enum bt_graph_status status
;
565 struct bt_component
*sink
;
568 status
= consume_graph_sink(sink
);
569 if (unlikely(status
!= BT_GRAPH_STATUS_END
)) {
570 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
574 /* End reached, the node is not added back to the queue and free'd. */
575 g_queue_delete_link(graph
->sinks_to_consume
, node
);
577 /* Don't forward an END status if there are sinks left to consume. */
578 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
579 status
= BT_GRAPH_STATUS_OK
;
584 BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status
));
589 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
590 struct bt_component
*sink
)
592 enum bt_graph_status status
;
596 BT_LOGV("Making specific sink consume: addr=%p, "
597 "comp-addr=%p, comp-name=\"%s\"",
598 graph
, sink
, bt_component_get_name(sink
));
600 BT_ASSERT(bt_component_borrow_graph(sink
) == graph
);
602 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
603 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
604 status
= BT_GRAPH_STATUS_END
;
608 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
610 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
611 status
= BT_GRAPH_STATUS_END
;
615 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
616 BT_ASSERT(sink_node
);
617 status
= consume_sink_node(graph
, sink_node
);
624 enum bt_graph_status
bt_graph_consume_no_check(struct bt_graph
*graph
)
626 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
627 struct bt_component
*sink
;
630 BT_LOGV("Making next sink consume: addr=%p", graph
);
631 BT_ASSERT_PRE(graph
->has_sink
,
632 "Graph has no sink component: %!+g", graph
);
634 if (unlikely(g_queue_is_empty(graph
->sinks_to_consume
))) {
635 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
636 status
= BT_GRAPH_STATUS_END
;
640 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
641 sink
= current_node
->data
;
642 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
643 sink
, bt_component_get_name(sink
));
644 status
= consume_sink_node(graph
, current_node
);
650 enum bt_graph_status
bt_private_graph_consume(
651 struct bt_private_graph
*priv_graph
)
653 struct bt_graph
*graph
= (void *) priv_graph
;
654 enum bt_graph_status status
;
656 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
657 BT_ASSERT_PRE(!graph
->canceled
, "Graph is canceled: %!+g", graph
);
658 BT_ASSERT_PRE(graph
->can_consume
,
659 "Cannot consume graph in its current state: %!+g", graph
);
660 bt_graph_set_can_consume(graph
, BT_FALSE
);
661 status
= bt_graph_consume_no_check(graph
);
662 bt_graph_set_can_consume(graph
, BT_TRUE
);
666 enum bt_graph_status
bt_private_graph_run(
667 struct bt_private_graph
*priv_graph
)
669 struct bt_graph
*graph
= (void *) priv_graph
;
670 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
673 BT_LOGW_STR("Invalid parameter: graph is NULL.");
674 status
= BT_GRAPH_STATUS_INVALID
;
678 if (graph
->canceled
) {
679 BT_LOGW("Invalid parameter: graph is canceled: "
680 "graph-addr=%p", graph
);
681 status
= BT_GRAPH_STATUS_CANCELED
;
685 BT_ASSERT_PRE(graph
->can_consume
,
686 "Cannot consume graph in its current state: %!+g", graph
);
687 bt_graph_set_can_consume(graph
, BT_FALSE
);
688 BT_LOGV("Running graph: addr=%p", graph
);
692 * Check if the graph is canceled at each iteration. If
693 * the graph was canceled by another thread or by a
694 * signal, this is not a warning nor an error, it was
695 * intentional: log with a DEBUG level only.
697 if (unlikely(graph
->canceled
)) {
698 BT_LOGD("Stopping the graph: graph is canceled: "
699 "graph-addr=%p", graph
);
700 status
= BT_GRAPH_STATUS_CANCELED
;
704 status
= bt_graph_consume_no_check(graph
);
705 if (unlikely(status
== BT_GRAPH_STATUS_AGAIN
)) {
707 * If AGAIN is received and there are multiple
708 * sinks, go ahead and consume from the next
711 * However, in the case where a single sink is
712 * left, the caller can decide to busy-wait and
713 * call bt_graph_run() continuously until the
714 * source is ready or it can decide to sleep for
715 * an arbitrary amount of time.
717 if (graph
->sinks_to_consume
->length
> 1) {
718 status
= BT_GRAPH_STATUS_OK
;
720 } else if (status
== BT_GRAPH_STATUS_NO_SINK
) {
723 } while (status
== BT_GRAPH_STATUS_OK
);
725 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
726 status
= BT_GRAPH_STATUS_END
;
730 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status
));
732 bt_graph_set_can_consume(graph
, BT_TRUE
);
738 int add_listener(GArray
*listeners
, void *func
, void *removed
, void *data
)
740 struct bt_graph_listener listener
= {
746 g_array_append_val(listeners
, listener
);
747 return listeners
->len
- 1;
750 int bt_private_graph_add_port_added_listener(
751 struct bt_private_graph
*priv_graph
,
752 bt_private_graph_port_added_listener listener
,
753 bt_private_graph_listener_removed listener_removed
, void *data
)
755 struct bt_graph
*graph
= (void *) priv_graph
;
759 BT_LOGW_STR("Invalid parameter: graph is NULL.");
764 if (graph
->in_remove_listener
) {
765 BT_LOGW("Cannot call this function during the execution of a remove listener: "
772 BT_LOGW_STR("Invalid parameter: listener is NULL.");
777 ret
= add_listener(graph
->listeners
.port_added
, listener
,
778 listener_removed
, data
);
779 BT_LOGV("Added \"port added\" listener to graph: "
780 "graph-addr=%p, listener-addr=%p, pos=%d",
781 graph
, listener
, ret
);
787 int bt_private_graph_add_port_removed_listener(
788 struct bt_private_graph
*priv_graph
,
789 bt_private_graph_port_removed_listener listener
,
790 bt_private_graph_listener_removed listener_removed
, void *data
)
792 struct bt_graph
*graph
= (void *) priv_graph
;
796 BT_LOGW_STR("Invalid parameter: graph is NULL.");
801 if (graph
->in_remove_listener
) {
802 BT_LOGW("Cannot call this function during the execution of a remove listener: "
809 BT_LOGW_STR("Invalid parameter: listener is NULL.");
814 ret
= add_listener(graph
->listeners
.port_removed
, listener
,
815 listener_removed
, data
);
816 BT_LOGV("Added \"port removed\" listener to graph: "
817 "graph-addr=%p, listener-addr=%p, pos=%d",
818 graph
, listener
, ret
);
824 int bt_private_graph_add_ports_connected_listener(
825 struct bt_private_graph
*priv_graph
,
826 bt_private_graph_ports_connected_listener listener
,
827 bt_private_graph_listener_removed listener_removed
, void *data
)
829 struct bt_graph
*graph
= (void *) priv_graph
;
833 BT_LOGW_STR("Invalid parameter: graph is NULL.");
838 if (graph
->in_remove_listener
) {
839 BT_LOGW("Cannot call this function during the execution of a remove listener: "
846 BT_LOGW_STR("Invalid parameter: listener is NULL.");
851 ret
= add_listener(graph
->listeners
.ports_connected
, listener
,
852 listener_removed
, data
);
853 BT_LOGV("Added \"port connected\" listener to graph: "
854 "graph-addr=%p, listener-addr=%p, pos=%d",
855 graph
, listener
, ret
);
861 int bt_private_graph_add_ports_disconnected_listener(
862 struct bt_private_graph
*priv_graph
,
863 bt_private_graph_ports_disconnected_listener listener
,
864 bt_private_graph_listener_removed listener_removed
, void *data
)
866 struct bt_graph
*graph
= (void *) priv_graph
;
870 BT_LOGW_STR("Invalid parameter: graph is NULL.");
875 if (graph
->in_remove_listener
) {
876 BT_LOGW("Cannot call this function during the execution of a remove listener: "
883 BT_LOGW_STR("Invalid parameter: listener is NULL.");
888 ret
= add_listener(graph
->listeners
.ports_disconnected
, listener
,
889 listener_removed
, data
);
890 BT_LOGV("Added \"port disconnected\" listener to graph: "
891 "graph-addr=%p, listener-addr=%p, pos=%d",
892 graph
, listener
, ret
);
899 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
903 BT_LOGV("Notifying graph listeners that a port was added: "
904 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
905 graph
, port
, bt_port_get_name(port
));
907 for (i
= 0; i
< graph
->listeners
.port_added
->len
; i
++) {
908 struct bt_graph_listener listener
=
909 g_array_index(graph
->listeners
.port_added
,
910 struct bt_graph_listener
, i
);
911 bt_private_graph_port_added_listener func
= listener
.func
;
914 func(port
, listener
.data
);
919 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
920 struct bt_component
*comp
, struct bt_port
*port
)
924 BT_LOGV("Notifying graph listeners that a port was removed: "
925 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
926 graph
, port
, bt_port_get_name(port
));
928 for (i
= 0; i
< graph
->listeners
.port_removed
->len
; i
++) {
929 struct bt_graph_listener listener
=
930 g_array_index(graph
->listeners
.port_removed
,
931 struct bt_graph_listener
, i
);
932 bt_private_graph_port_removed_listener func
= listener
.func
;
935 func(comp
, port
, listener
.data
);
940 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
941 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
945 BT_LOGV("Notifying graph listeners that two ports were connected: "
947 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
948 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
949 graph
, upstream_port
, bt_port_get_name(upstream_port
),
950 downstream_port
, bt_port_get_name(downstream_port
));
952 for (i
= 0; i
< graph
->listeners
.ports_connected
->len
; i
++) {
953 struct bt_graph_listener listener
=
954 g_array_index(graph
->listeners
.ports_connected
,
955 struct bt_graph_listener
, i
);
956 bt_private_graph_ports_connected_listener func
= listener
.func
;
959 func(upstream_port
, downstream_port
, listener
.data
);
964 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
965 struct bt_component
*upstream_comp
,
966 struct bt_component
*downstream_comp
,
967 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
971 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
973 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
974 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
975 graph
, upstream_port
, bt_port_get_name(upstream_port
),
976 downstream_port
, bt_port_get_name(downstream_port
));
978 for (i
= 0; i
< graph
->listeners
.ports_disconnected
->len
; i
++) {
979 struct bt_graph_listener listener
=
980 g_array_index(graph
->listeners
.ports_disconnected
,
981 struct bt_graph_listener
, i
);
982 bt_private_graph_ports_disconnected_listener func
= listener
.func
;
985 func(upstream_comp
, downstream_comp
, upstream_port
,
986 downstream_port
, listener
.data
);
990 enum bt_graph_status
bt_private_graph_cancel(
991 struct bt_private_graph
*priv_graph
)
993 struct bt_graph
*graph
= (void *) priv_graph
;
994 enum bt_graph_status ret
= BT_GRAPH_STATUS_OK
;
997 BT_LOGW_STR("Invalid parameter: graph is NULL.");
998 ret
= BT_GRAPH_STATUS_INVALID
;
1002 graph
->canceled
= BT_TRUE
;
1003 BT_LOGV("Canceled graph: addr=%p", graph
);
1009 bt_bool
bt_graph_is_canceled(struct bt_graph
*graph
)
1011 bt_bool canceled
= BT_FALSE
;
1014 BT_LOGW_STR("Invalid parameter: graph is NULL.");
1018 canceled
= graph
->canceled
;
1025 void bt_graph_remove_connection(struct bt_graph
*graph
,
1026 struct bt_connection
*connection
)
1029 BT_ASSERT(connection
);
1030 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
1032 g_ptr_array_remove(graph
->connections
, connection
);
1035 enum bt_graph_status
bt_private_graph_add_component_with_init_method_data(
1036 struct bt_private_graph
*priv_graph
,
1037 struct bt_component_class
*component_class
,
1038 const char *name
, struct bt_value
*params
,
1039 void *init_method_data
,
1040 struct bt_component
**user_component
)
1042 struct bt_graph
*graph
= (void *) priv_graph
;
1043 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
1044 enum bt_component_status comp_status
;
1045 struct bt_component
*component
= NULL
;
1046 enum bt_component_class_type type
;
1048 bt_bool init_can_consume
;
1050 bt_object_get_ref(params
);
1053 BT_LOGW_STR("Invalid parameter: graph is NULL.");
1054 graph_status
= BT_GRAPH_STATUS_INVALID
;
1057 init_can_consume
= graph
->can_consume
;
1059 if (!component_class
) {
1060 BT_LOGW_STR("Invalid parameter: component class is NULL.");
1061 graph_status
= BT_GRAPH_STATUS_INVALID
;
1065 graph
->can_consume
= BT_FALSE
;
1066 type
= bt_component_class_get_type(component_class
);
1067 BT_LOGD("Adding component to graph: "
1068 "graph-addr=%p, comp-cls-addr=%p, "
1069 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
1070 "init-method-data-addr=%p",
1071 graph
, component_class
, bt_component_class_type_string(type
),
1072 name
, params
, init_method_data
);
1075 BT_LOGW_STR("Invalid parameter: name is NULL.");
1076 graph_status
= BT_GRAPH_STATUS_INVALID
;
1080 if (graph
->canceled
) {
1081 BT_LOGW_STR("Invalid parameter: graph is canceled.");
1082 graph_status
= BT_GRAPH_STATUS_CANCELED
;
1086 if (type
!= BT_COMPONENT_CLASS_TYPE_SOURCE
&&
1087 type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
1088 type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
1089 BT_LOGW("Invalid parameter: unknown component class type: "
1091 graph_status
= BT_GRAPH_STATUS_INVALID
;
1095 for (i
= 0; i
< graph
->components
->len
; i
++) {
1096 void *other_comp
= graph
->components
->pdata
[i
];
1098 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
1099 BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
1100 "other-comp-addr=%p, name=\"%s\"",
1102 graph_status
= BT_GRAPH_STATUS_INVALID
;
1108 * Parameters must be a map value, but we create a convenient
1109 * empty one if it's NULL.
1112 if (!bt_value_is_map(params
)) {
1113 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
1115 bt_common_value_type_string(
1116 bt_value_get_type(params
)));
1117 graph_status
= BT_GRAPH_STATUS_INVALID
;
1121 params
= bt_value_borrow_from_private(
1122 bt_private_value_map_create());
1124 BT_LOGE_STR("Cannot create map value object.");
1125 graph_status
= BT_GRAPH_STATUS_NOMEM
;
1130 comp_status
= bt_component_create(component_class
, name
, &component
);
1131 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
1132 BT_LOGE("Cannot create empty component object: status=%s",
1133 bt_component_status_string(comp_status
));
1134 graph_status
= bt_graph_status_from_component_status(
1140 * The user's initialization method needs to see that this
1141 * component is part of the graph. If the user method fails, we
1142 * immediately remove the component from the graph's components.
1144 g_ptr_array_add(graph
->components
, component
);
1145 bt_component_set_graph(component
, graph
);
1147 if (component_class
->methods
.init
) {
1148 BT_LOGD_STR("Calling user's initialization method.");
1149 comp_status
= component_class
->methods
.init(
1150 bt_private_component_from_component(component
), params
,
1152 BT_LOGD("User method returned: status=%s",
1153 bt_component_status_string(comp_status
));
1154 if (comp_status
!= BT_COMPONENT_STATUS_OK
) {
1155 BT_LOGW_STR("Initialization method failed.");
1156 graph_status
= bt_graph_status_from_component_status(
1158 bt_component_set_graph(component
, NULL
);
1159 g_ptr_array_remove_fast(graph
->components
, component
);
1165 * Mark the component as initialized so that its finalization
1166 * method is called when it is destroyed.
1168 component
->initialized
= true;
1171 * If it's a sink component, it needs to be part of the graph's
1172 * sink queue to be consumed by bt_graph_consume().
1174 if (bt_component_is_sink(component
)) {
1175 graph
->has_sink
= BT_TRUE
;
1176 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1180 * Freeze the component class now that it's instantiated at
1183 BT_LOGD_STR("Freezing component class.");
1184 bt_component_class_freeze(component
->class);
1185 BT_LOGD("Added component to graph: "
1186 "graph-addr=%p, comp-cls-addr=%p, "
1187 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
1188 "init-method-data-addr=%p, comp-addr=%p",
1189 graph
, component_class
, bt_component_class_type_string(type
),
1190 name
, params
, init_method_data
, component
);
1192 if (user_component
) {
1193 /* Move reference to user */
1194 *user_component
= component
;
1199 bt_object_put_ref(component
);
1200 bt_object_put_ref(params
);
1202 graph
->can_consume
= init_can_consume
;
1204 return graph_status
;
1207 enum bt_graph_status
bt_private_graph_add_component(
1208 struct bt_private_graph
*graph
,
1209 struct bt_component_class
*component_class
,
1210 const char *name
, struct bt_value
*params
,
1211 struct bt_component
**component
)
1213 return bt_private_graph_add_component_with_init_method_data(graph
,
1214 component_class
, name
, params
, NULL
, component
);
1218 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
1219 struct bt_component
*component
)
1221 bt_bool init_can_consume
;
1227 BT_ASSERT(component
);
1228 BT_ASSERT(component
->base
.ref_count
== 0);
1229 BT_ASSERT(bt_component_borrow_graph(component
) == graph
);
1231 init_can_consume
= graph
->can_consume
;
1232 count
= bt_component_get_input_port_count(component
);
1234 for (i
= 0; i
< count
; i
++) {
1235 struct bt_port
*port
=
1236 bt_component_get_input_port_by_index(component
, i
);
1239 bt_object_put_ref(port
);
1241 if (bt_port_is_connected(port
)) {
1242 BT_LOGW("Cannot remove component from graph: "
1243 "an input port is connected: "
1244 "graph-addr=%p, comp-addr=%p, "
1245 "comp-name=\"%s\", connected-port-addr=%p, "
1246 "connected-port-name=\"%s\"",
1248 bt_component_get_name(component
),
1249 port
, bt_port_get_name(port
));
1254 count
= bt_component_get_output_port_count(component
);
1256 for (i
= 0; i
< count
; i
++) {
1257 struct bt_port
*port
=
1258 bt_component_get_output_port_by_index(component
, i
);
1261 bt_object_put_ref(port
);
1263 if (bt_port_is_connected(port
)) {
1264 BT_LOGW("Cannot remove component from graph: "
1265 "an output port is connected: "
1266 "graph-addr=%p, comp-addr=%p, "
1267 "comp-name=\"%s\", connected-port-addr=%p, "
1268 "connected-port-name=\"%s\"",
1270 bt_component_get_name(component
),
1271 port
, bt_port_get_name(port
));
1276 graph
->can_consume
= BT_FALSE
;
1278 /* Possibly remove from sinks to consume */
1279 (void) g_queue_remove(graph
->sinks_to_consume
, component
);
1281 if (graph
->sinks_to_consume
->length
== 0) {
1282 graph
->has_sink
= BT_FALSE
;
1286 * This calls bt_object_try_spec_release() on the component, and
1287 * since its reference count is 0, its destructor is called. Its
1288 * destructor calls the user's finalization method (if set).
1290 g_ptr_array_remove(graph
->components
, component
);
1297 graph
->can_consume
= init_can_consume
;
1302 void bt_graph_add_notification(struct bt_graph
*graph
,
1303 struct bt_notification
*notif
)
1309 * It's okay not to take a reference because, when a
1310 * notification's reference count drops to 0, either:
1312 * * It is recycled back to one of this graph's pool.
1313 * * It is destroyed because it doesn't have any link to any
1314 * graph, which means the original graph is already destroyed.
1316 g_ptr_array_add(graph
->notifications
, notif
);
1319 struct bt_graph
*bt_graph_borrow_from_private(
1320 struct bt_private_graph
*priv_graph
)
1322 return (void *) priv_graph
;