test_ctf_writer.c: put statements outside BT_ASSERT()
[babeltrace.git] / lib / graph / graph.c
CommitLineData
c0418dd9 1/*
7d55361f 2 * graph.c
c0418dd9
JG
3 *
4 * Babeltrace Plugin Component Graph
5 *
f60c8b34 6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
262e5473 7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
c0418dd9
JG
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
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
25 * SOFTWARE.
26 */
27
262e5473
PP
28#define BT_LOG_TAG "GRAPH"
29#include <babeltrace/lib-logging-internal.h>
30
b2e0c907
PP
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>
5c563278
PP
38#include <babeltrace/graph/notification-internal.h>
39#include <babeltrace/graph/notification-event-internal.h>
40#include <babeltrace/graph/notification-packet-internal.h>
3d9990ac 41#include <babeltrace/compiler-internal.h>
c55a9f58 42#include <babeltrace/types.h>
36712f1d
PP
43#include <babeltrace/values.h>
44#include <babeltrace/values-internal.h>
f6ccaed9
PP
45#include <babeltrace/assert-internal.h>
46#include <babeltrace/assert-pre-internal.h>
f60c8b34 47#include <unistd.h>
1bf957a0
PP
48#include <glib.h>
49
50struct bt_graph_listener {
51 void *func;
8cc092c9 52 bt_graph_listener_removed removed;
1bf957a0
PP
53 void *data;
54};
c0418dd9 55
8cc092c9
PP
56static
57int init_listeners_array(GArray **listeners)
58{
59 int ret = 0;
60
f6ccaed9 61 BT_ASSERT(listeners);
8cc092c9
PP
62 *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener));
63 if (!*listeners) {
64 BT_LOGE_STR("Failed to allocate one GArray.");
65 ret = -1;
66 goto end;
67 }
68
69end:
70 return ret;
71}
72
73static
74void call_remove_listeners(GArray *listeners)
75{
76 size_t i;
77
78 for (i = 0; i < listeners->len; i++) {
79 struct bt_graph_listener listener =
80 g_array_index(listeners, struct bt_graph_listener, i);
81
82 if (listener.removed) {
83 listener.removed(listener.data);
84 }
85 }
86}
87
f60c8b34
JG
88static
89void bt_graph_destroy(struct bt_object *obj)
c0418dd9 90{
f60c8b34
JG
91 struct bt_graph *graph = container_of(obj,
92 struct bt_graph, base);
c0418dd9 93
bd14d768
PP
94 /*
95 * The graph's reference count is 0 if we're here. Increment
96 * it to avoid a double-destroy (possibly infinitely recursive)
97 * in this situation:
98 *
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
104 * 0 to 1).
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).
115 *
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.
119 */
262e5473 120 BT_LOGD("Destroying graph: addr=%p", graph);
bd14d768
PP
121 obj->ref_count.count++;
122
49682acd
PP
123 /*
124 * Cancel the graph to disallow some operations, like creating
125 * notification iterators and adding ports to components.
126 */
127 (void) bt_graph_cancel(graph);
128
8cc092c9
PP
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);
134
5c563278
PP
135 if (graph->notifications) {
136 g_ptr_array_free(graph->notifications, TRUE);
137 }
138
c0418dd9 139 if (graph->connections) {
262e5473 140 BT_LOGD_STR("Destroying connections.");
c0418dd9
JG
141 g_ptr_array_free(graph->connections, TRUE);
142 }
5c563278 143
bd14d768 144 if (graph->components) {
262e5473 145 BT_LOGD_STR("Destroying components.");
bd14d768
PP
146 g_ptr_array_free(graph->components, TRUE);
147 }
5c563278 148
f60c8b34
JG
149 if (graph->sinks_to_consume) {
150 g_queue_free(graph->sinks_to_consume);
c0418dd9 151 }
1bf957a0
PP
152
153 if (graph->listeners.port_added) {
154 g_array_free(graph->listeners.port_added, TRUE);
155 }
156
157 if (graph->listeners.port_removed) {
158 g_array_free(graph->listeners.port_removed, TRUE);
159 }
160
f345f8bb
PP
161 if (graph->listeners.ports_connected) {
162 g_array_free(graph->listeners.ports_connected, TRUE);
1bf957a0
PP
163 }
164
f345f8bb
PP
165 if (graph->listeners.ports_disconnected) {
166 g_array_free(graph->listeners.ports_disconnected, TRUE);
1bf957a0
PP
167 }
168
5c563278
PP
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);
c0418dd9
JG
172 g_free(graph);
173}
174
5c563278
PP
175static
176void destroy_notification_event(struct bt_notification *notif,
177 struct bt_graph *graph)
178{
179 bt_notification_event_destroy(notif);
180}
181
182static
183void destroy_notification_packet_begin(struct bt_notification *notif,
184 struct bt_graph *graph)
185{
186 bt_notification_packet_begin_destroy(notif);
187}
188
189static
190void destroy_notification_packet_end(struct bt_notification *notif,
191 struct bt_graph *graph)
192{
193 bt_notification_packet_end_destroy(notif);
194}
195
196static
197void notify_notification_graph_is_destroyed(struct bt_notification *notif)
198{
199 bt_notification_unlink_graph(notif);
200}
201
f60c8b34 202struct bt_graph *bt_graph_create(void)
c0418dd9 203{
f60c8b34 204 struct bt_graph *graph;
1bf957a0 205 int ret;
c0418dd9 206
262e5473 207 BT_LOGD_STR("Creating graph object.");
f60c8b34 208 graph = g_new0(struct bt_graph, 1);
c0418dd9 209 if (!graph) {
262e5473 210 BT_LOGE_STR("Failed to allocate one graph.");
c0418dd9
JG
211 goto end;
212 }
213
f60c8b34 214 bt_object_init(graph, bt_graph_destroy);
c0418dd9 215
f60c8b34 216 graph->connections = g_ptr_array_new_with_free_func(bt_object_release);
c0418dd9 217 if (!graph->connections) {
262e5473 218 BT_LOGE_STR("Failed to allocate one GPtrArray.");
c0418dd9
JG
219 goto error;
220 }
f60c8b34
JG
221 graph->components = g_ptr_array_new_with_free_func(bt_object_release);
222 if (!graph->components) {
262e5473 223 BT_LOGE_STR("Failed to allocate one GPtrArray.");
f60c8b34
JG
224 goto error;
225 }
226 graph->sinks_to_consume = g_queue_new();
227 if (!graph->sinks_to_consume) {
262e5473 228 BT_LOGE_STR("Failed to allocate one GQueue.");
c0418dd9
JG
229 goto error;
230 }
1bf957a0 231
1d915789 232 graph->can_consume = BT_TRUE;
1bf957a0
PP
233 ret = init_listeners_array(&graph->listeners.port_added);
234 if (ret) {
262e5473 235 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
1bf957a0
PP
236 goto error;
237 }
238
239 ret = init_listeners_array(&graph->listeners.port_removed);
240 if (ret) {
262e5473 241 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
1bf957a0
PP
242 goto error;
243 }
244
f345f8bb 245 ret = init_listeners_array(&graph->listeners.ports_connected);
1bf957a0 246 if (ret) {
262e5473 247 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
1bf957a0
PP
248 goto error;
249 }
250
f345f8bb 251 ret = init_listeners_array(&graph->listeners.ports_disconnected);
1bf957a0 252 if (ret) {
262e5473 253 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
1bf957a0
PP
254 goto error;
255 }
256
5c563278
PP
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,
260 graph);
261 if (ret) {
262 BT_LOGE("Failed to initialize event notification pool: ret=%d",
263 ret);
264 goto error;
265 }
266
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,
270 graph);
271 if (ret) {
272 BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d",
273 ret);
274 goto error;
275 }
276
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,
280 graph);
281 if (ret) {
282 BT_LOGE("Failed to initialize packet end notification pool: ret=%d",
283 ret);
284 goto error;
285 }
286
287 graph->notifications = g_ptr_array_new_with_free_func(
288 (GDestroyNotify) notify_notification_graph_is_destroyed);
262e5473
PP
289 BT_LOGD("Created graph object: addr=%p", graph);
290
c0418dd9
JG
291end:
292 return graph;
293error:
294 BT_PUT(graph);
295 goto end;
296}
297
a256a42d
PP
298enum 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)
f60c8b34 301{
a256a42d 302 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
f60c8b34
JG
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;
ecbe9f49 309 bt_bool init_can_consume;
f60c8b34 310
262e5473
PP
311 if (!graph) {
312 BT_LOGW_STR("Invalid parameter: graph is NULL.");
a256a42d 313 status = BT_GRAPH_STATUS_INVALID;
262e5473
PP
314 goto end;
315 }
4aa7981f 316 init_can_consume = graph->can_consume;
262e5473
PP
317
318 if (!upstream_port) {
319 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
a256a42d 320 status = BT_GRAPH_STATUS_INVALID;
262e5473
PP
321 goto end;
322 }
323
324 if (!downstream_port) {
325 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
a256a42d 326 status = BT_GRAPH_STATUS_INVALID;
f60c8b34
JG
327 goto end;
328 }
329
262e5473
PP
330 BT_LOGD("Connecting component ports within graph: "
331 "graph-addr=%p, "
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));
336
202a3a13 337 if (graph->canceled) {
262e5473 338 BT_LOGW_STR("Invalid parameter: graph is canceled.");
a256a42d 339 status = BT_GRAPH_STATUS_CANCELED;
202a3a13
PP
340 goto end;
341 }
342
1d915789
PP
343 graph->can_consume = BT_FALSE;
344
72b913fb 345 /* Ensure appropriate types for upstream and downstream ports. */
f60c8b34 346 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
262e5473 347 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
a256a42d 348 status = BT_GRAPH_STATUS_INVALID;
f60c8b34
JG
349 goto end;
350 }
351 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
262e5473 352 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
a256a42d 353 status = BT_GRAPH_STATUS_INVALID;
f60c8b34
JG
354 goto end;
355 }
356
72b913fb 357 /* Ensure that both ports are currently unconnected. */
0d8b4d8e 358 if (bt_port_is_connected(upstream_port)) {
262e5473 359 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
a256a42d 360 status = BT_GRAPH_STATUS_INVALID;
72b913fb
PP
361 goto end;
362 }
363
0d8b4d8e 364 if (bt_port_is_connected(downstream_port)) {
262e5473 365 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
a256a42d 366 status = BT_GRAPH_STATUS_INVALID;
72b913fb
PP
367 goto end;
368 }
369
370 /*
371 * Ensure that both ports are still attached to their creating
372 * component.
373 */
f60c8b34 374 upstream_component = bt_port_get_component(upstream_port);
72b913fb 375 if (!upstream_component) {
262e5473 376 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
a256a42d 377 status = BT_GRAPH_STATUS_INVALID;
72b913fb
PP
378 goto end;
379 }
380
381 downstream_component = bt_port_get_component(downstream_port);
382 if (!downstream_component) {
262e5473 383 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
a256a42d 384 status = BT_GRAPH_STATUS_INVALID;
72b913fb
PP
385 goto end;
386 }
387
262e5473
PP
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));
393
0d8b4d8e
PP
394 /*
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.
398 */
262e5473 399 BT_LOGD_STR("Asking upstream component to accept the connection.");
0d8b4d8e
PP
400 component_status = bt_component_accept_port_connection(
401 upstream_component, upstream_port, downstream_port);
402 if (component_status != BT_COMPONENT_STATUS_OK) {
262e5473
PP
403 if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
404 BT_LOGD_STR("Upstream component refused the connection.");
405 } else {
406 BT_LOGW("Cannot ask upstream component to accept the connection: "
407 "status=%s", bt_component_status_string(component_status));
408 }
409
a256a42d
PP
410 status = bt_graph_status_from_component_status(
411 component_status);
412 goto end;
0d8b4d8e 413 }
262e5473
PP
414
415 BT_LOGD_STR("Asking downstream component to accept the connection.");
0d8b4d8e
PP
416 component_status = bt_component_accept_port_connection(
417 downstream_component, downstream_port, upstream_port);
418 if (component_status != BT_COMPONENT_STATUS_OK) {
262e5473
PP
419 if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
420 BT_LOGD_STR("Downstream component refused the connection.");
421 } else {
422 BT_LOGW("Cannot ask downstream component to accept the connection: "
423 "status=%s", bt_component_status_string(component_status));
424 }
425
a256a42d
PP
426 status = bt_graph_status_from_component_status(
427 component_status);
428 goto end;
0d8b4d8e
PP
429 }
430
262e5473 431 BT_LOGD_STR("Creating connection.");
f60c8b34
JG
432 connection = bt_connection_create(graph, upstream_port,
433 downstream_port);
434 if (!connection) {
262e5473 435 BT_LOGW("Cannot create connection object.");
a256a42d
PP
436 status = BT_GRAPH_STATUS_NOMEM;
437 goto end;
f60c8b34
JG
438 }
439
262e5473
PP
440 BT_LOGD("Connection object created: conn-addr=%p", connection);
441
f60c8b34 442 /*
72b913fb
PP
443 * Ownership of upstream_component/downstream_component and of
444 * the connection object is transferred to the graph.
f60c8b34
JG
445 */
446 g_ptr_array_add(graph->connections, connection);
ffeb0eed 447
f60c8b34 448 /*
0d8b4d8e 449 * Notify both components that their port is connected.
f60c8b34 450 */
262e5473 451 BT_LOGD_STR("Notifying upstream component that its port is connected.");
0d8b4d8e
PP
452 bt_component_port_connected(upstream_component, upstream_port,
453 downstream_port);
262e5473 454 BT_LOGD_STR("Notifying downstream component that its port is connected.");
0d8b4d8e
PP
455 bt_component_port_connected(downstream_component, downstream_port,
456 upstream_port);
1bf957a0
PP
457
458 /*
0d8b4d8e 459 * Notify the graph's creator that both ports are connected.
1bf957a0 460 */
262e5473 461 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
f345f8bb 462 bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
262e5473
PP
463 BT_LOGD("Connected component ports within graph: "
464 "graph-addr=%p, "
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\"",
469 graph,
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));
1bf957a0 474
a256a42d 475 if (user_connection) {
1a6a376a
PP
476 /* Move reference to user */
477 *user_connection = connection;
478 connection = NULL;
a256a42d
PP
479 }
480
f60c8b34
JG
481end:
482 bt_put(upstream_graph);
483 bt_put(downstream_graph);
3eeacbb9
JG
484 bt_put(upstream_component);
485 bt_put(downstream_component);
a256a42d 486 bt_put(connection);
4aa7981f
JG
487 if (graph) {
488 graph->can_consume = init_can_consume;
489 }
a256a42d 490 return status;
f60c8b34
JG
491}
492
851b70bd 493static
8ed535b5 494enum bt_graph_status consume_graph_sink(struct bt_component *sink)
c0418dd9 495{
72b913fb
PP
496 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
497 enum bt_component_status comp_status;
202a3a13 498
f6ccaed9 499 BT_ASSERT(sink);
72b913fb 500 comp_status = bt_component_sink_consume(sink);
8ed535b5
PP
501 BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s",
502 sink, bt_component_get_name(sink),
262e5473 503 bt_component_status_string(comp_status));
8ed535b5 504
72b913fb
PP
505 switch (comp_status) {
506 case BT_COMPONENT_STATUS_OK:
507 break;
508 case BT_COMPONENT_STATUS_END:
509 status = BT_GRAPH_STATUS_END;
510 break;
511 case BT_COMPONENT_STATUS_AGAIN:
512 status = BT_GRAPH_STATUS_AGAIN;
513 break;
514 case BT_COMPONENT_STATUS_INVALID:
515 status = BT_GRAPH_STATUS_INVALID;
516 break;
517 default:
518 status = BT_GRAPH_STATUS_ERROR;
519 break;
520 }
521
8ed535b5
PP
522 return status;
523}
524
525/*
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.
529 */
530static
531enum bt_graph_status consume_sink_node(struct bt_graph *graph,
532 GList *node)
533{
534 enum bt_graph_status status;
535 struct bt_component *sink;
536
537 sink = node->data;
538 status = consume_graph_sink(sink);
72b913fb 539 if (status != BT_GRAPH_STATUS_END) {
8ed535b5 540 g_queue_push_tail_link(graph->sinks_to_consume, node);
f60c8b34
JG
541 goto end;
542 }
543
544 /* End reached, the node is not added back to the queue and free'd. */
8ed535b5 545 g_queue_delete_link(graph->sinks_to_consume, node);
f60c8b34
JG
546
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;
550 goto end;
551 }
8ed535b5
PP
552
553end:
554 BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status));
555 return status;
556}
557
558BT_HIDDEN
559enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
560 struct bt_component *sink)
561{
562 enum bt_graph_status status;
563 GList *sink_node;
564 int index;
565
566 BT_LOGV("Making specific sink consume: addr=%p, "
567 "comp-addr=%p, comp-name=\"%s\"",
568 graph, sink, bt_component_get_name(sink));
569
f6ccaed9 570 BT_ASSERT(bt_component_borrow_graph(sink) == graph);
8ed535b5
PP
571
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;
575 goto end;
576 }
577
578 index = g_queue_index(graph->sinks_to_consume, sink);
579 if (index < 0) {
580 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
581 status = BT_GRAPH_STATUS_END;
582 goto end;
583 }
584
585 sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index);
f6ccaed9 586 BT_ASSERT(sink_node);
8ed535b5
PP
587 status = consume_sink_node(graph, sink_node);
588
589end:
590 return status;
591}
592
593BT_HIDDEN
594enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
595{
596 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
597 struct bt_component *sink;
598 GList *current_node;
599
600 BT_LOGV("Making next sink consume: addr=%p", graph);
f6ccaed9
PP
601 BT_ASSERT_PRE(graph->has_sink,
602 "Graph has no sink component: %!+g", graph);
8ed535b5
PP
603
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;
607 goto end;
608 }
609
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);
615
f60c8b34
JG
616end:
617 return status;
c0418dd9
JG
618}
619
851b70bd
PP
620enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
621{
f6ccaed9 622 enum bt_graph_status status;
1d915789 623
f6ccaed9
PP
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);
1d915789 628 graph->can_consume = BT_FALSE;
851b70bd 629 status = bt_graph_consume_no_check(graph);
1d915789 630 graph->can_consume = BT_TRUE;
26a15756 631 return status;
851b70bd
PP
632}
633
72b913fb 634enum bt_graph_status bt_graph_run(struct bt_graph *graph)
f60c8b34 635{
72b913fb 636 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
f60c8b34
JG
637
638 if (!graph) {
262e5473 639 BT_LOGW_STR("Invalid parameter: graph is NULL.");
72b913fb 640 status = BT_GRAPH_STATUS_INVALID;
202a3a13 641 goto end;
f60c8b34
JG
642 }
643
851b70bd
PP
644 if (graph->canceled) {
645 BT_LOGW("Invalid parameter: graph is canceled: "
646 "graph-addr=%p", graph);
647 status = BT_GRAPH_STATUS_CANCELED;
648 goto end;
649 }
650
1d915789
PP
651 if (!graph->can_consume) {
652 BT_LOGW_STR("Cannot run graph in its current state.");
653 status = BT_GRAPH_STATUS_CANNOT_CONSUME;
654 goto end;
655 }
656
657 graph->can_consume = BT_FALSE;
262e5473
PP
658 BT_LOGV("Running graph: addr=%p", graph);
659
f60c8b34 660 do {
851b70bd
PP
661 /*
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.
666 */
667 if (graph->canceled) {
668 BT_LOGD("Stopping the graph: graph is canceled: "
669 "graph-addr=%p", graph);
670 status = BT_GRAPH_STATUS_CANCELED;
671 goto end;
672 }
673
4fef099a 674 status = bt_graph_consume_no_check(graph);
72b913fb 675 if (status == BT_GRAPH_STATUS_AGAIN) {
f60c8b34 676 /*
202a3a13
PP
677 * If AGAIN is received and there are multiple
678 * sinks, go ahead and consume from the next
679 * sink.
f60c8b34 680 *
202a3a13
PP
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.
f60c8b34
JG
686 */
687 if (graph->sinks_to_consume->length > 1) {
72b913fb 688 status = BT_GRAPH_STATUS_OK;
f60c8b34 689 }
2de524b3
PP
690 } else if (status == BT_GRAPH_STATUS_NO_SINK) {
691 goto end;
f60c8b34 692 }
72b913fb 693 } while (status == BT_GRAPH_STATUS_OK);
f60c8b34
JG
694
695 if (g_queue_is_empty(graph->sinks_to_consume)) {
72b913fb 696 status = BT_GRAPH_STATUS_END;
f60c8b34 697 }
262e5473 698
202a3a13 699end:
262e5473 700 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status));
4aa7981f
JG
701 if (graph) {
702 graph->can_consume = BT_TRUE;
703 }
72b913fb 704 return status;
f60c8b34 705}
1bf957a0
PP
706
707static
8cc092c9 708int add_listener(GArray *listeners, void *func, void *removed, void *data)
1bf957a0
PP
709{
710 struct bt_graph_listener listener = {
711 .func = func,
8cc092c9 712 .removed = removed,
1bf957a0
PP
713 .data = data,
714 };
715
716 g_array_append_val(listeners, listener);
0d107cdd 717 return listeners->len - 1;
1bf957a0
PP
718}
719
0d107cdd 720int bt_graph_add_port_added_listener(
1bf957a0 721 struct bt_graph *graph,
8cc092c9
PP
722 bt_graph_port_added_listener listener,
723 bt_graph_listener_removed listener_removed, void *data)
1bf957a0 724{
0d107cdd 725 int ret;
1bf957a0 726
262e5473
PP
727 if (!graph) {
728 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 729 ret = -1;
262e5473
PP
730 goto end;
731 }
732
8cc092c9
PP
733 if (graph->in_remove_listener) {
734 BT_LOGW("Cannot call this function during the execution of a remove listener: "
735 "addr=%p", graph);
736 ret = -1;
737 goto end;
738 }
739
262e5473
PP
740 if (!listener) {
741 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 742 ret = -1;
1bf957a0
PP
743 goto end;
744 }
745
8cc092c9
PP
746 ret = add_listener(graph->listeners.port_added, listener,
747 listener_removed, data);
262e5473 748 BT_LOGV("Added \"port added\" listener to graph: "
0d107cdd
PP
749 "graph-addr=%p, listener-addr=%p, pos=%d",
750 graph, listener, ret);
1bf957a0
PP
751
752end:
0d107cdd 753 return ret;
1bf957a0
PP
754}
755
0d107cdd 756int bt_graph_add_port_removed_listener(
1bf957a0 757 struct bt_graph *graph,
8cc092c9
PP
758 bt_graph_port_removed_listener listener,
759 bt_graph_listener_removed listener_removed, void *data)
1bf957a0 760{
0d107cdd 761 int ret;
1bf957a0 762
262e5473
PP
763 if (!graph) {
764 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 765 ret = -1;
262e5473
PP
766 goto end;
767 }
768
8cc092c9
PP
769 if (graph->in_remove_listener) {
770 BT_LOGW("Cannot call this function during the execution of a remove listener: "
771 "addr=%p", graph);
772 ret = -1;
773 goto end;
774 }
775
262e5473
PP
776 if (!listener) {
777 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 778 ret = -1;
1bf957a0
PP
779 goto end;
780 }
781
8cc092c9
PP
782 ret = add_listener(graph->listeners.port_removed, listener,
783 listener_removed, data);
262e5473 784 BT_LOGV("Added \"port removed\" listener to graph: "
0d107cdd
PP
785 "graph-addr=%p, listener-addr=%p, pos=%d",
786 graph, listener, ret);
1bf957a0
PP
787
788end:
0d107cdd 789 return ret;
1bf957a0
PP
790}
791
0d107cdd 792int bt_graph_add_ports_connected_listener(
1bf957a0 793 struct bt_graph *graph,
8cc092c9
PP
794 bt_graph_ports_connected_listener listener,
795 bt_graph_listener_removed listener_removed, void *data)
1bf957a0 796{
0d107cdd 797 int ret;
1bf957a0 798
262e5473
PP
799 if (!graph) {
800 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 801 ret = -1;
262e5473
PP
802 goto end;
803 }
804
8cc092c9
PP
805 if (graph->in_remove_listener) {
806 BT_LOGW("Cannot call this function during the execution of a remove listener: "
807 "addr=%p", graph);
808 ret = -1;
809 goto end;
810 }
811
262e5473
PP
812 if (!listener) {
813 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 814 ret = -1;
1bf957a0
PP
815 goto end;
816 }
817
8cc092c9
PP
818 ret = add_listener(graph->listeners.ports_connected, listener,
819 listener_removed, data);
262e5473 820 BT_LOGV("Added \"port connected\" listener to graph: "
0d107cdd
PP
821 "graph-addr=%p, listener-addr=%p, pos=%d",
822 graph, listener, ret);
1bf957a0
PP
823
824end:
0d107cdd 825 return ret;
1bf957a0
PP
826}
827
0d107cdd 828int bt_graph_add_ports_disconnected_listener(
1bf957a0 829 struct bt_graph *graph,
8cc092c9
PP
830 bt_graph_ports_disconnected_listener listener,
831 bt_graph_listener_removed listener_removed, void *data)
1bf957a0 832{
0d107cdd 833 int ret;
1bf957a0 834
262e5473
PP
835 if (!graph) {
836 BT_LOGW_STR("Invalid parameter: graph is NULL.");
0d107cdd 837 ret = -1;
262e5473
PP
838 goto end;
839 }
840
8cc092c9
PP
841 if (graph->in_remove_listener) {
842 BT_LOGW("Cannot call this function during the execution of a remove listener: "
843 "addr=%p", graph);
844 ret = -1;
845 goto end;
846 }
847
262e5473
PP
848 if (!listener) {
849 BT_LOGW_STR("Invalid parameter: listener is NULL.");
0d107cdd 850 ret = -1;
1bf957a0
PP
851 goto end;
852 }
853
8cc092c9
PP
854 ret = add_listener(graph->listeners.ports_disconnected, listener,
855 listener_removed, data);
262e5473 856 BT_LOGV("Added \"port disconnected\" listener to graph: "
0d107cdd
PP
857 "graph-addr=%p, listener-addr=%p, pos=%d",
858 graph, listener, ret);
1bf957a0
PP
859
860end:
0d107cdd 861 return ret;
1bf957a0
PP
862}
863
864BT_HIDDEN
865void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port)
866{
867 size_t i;
868
262e5473
PP
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));
872
1bf957a0
PP
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;
878
f6ccaed9 879 BT_ASSERT(func);
1bf957a0
PP
880 func(port, listener.data);
881 }
882}
883
884BT_HIDDEN
885void bt_graph_notify_port_removed(struct bt_graph *graph,
886 struct bt_component *comp, struct bt_port *port)
887{
888 size_t i;
889
262e5473
PP
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));
893
1bf957a0
PP
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;
899
f6ccaed9 900 BT_ASSERT(func);
1bf957a0
PP
901 func(comp, port, listener.data);
902 }
903}
904
905BT_HIDDEN
f345f8bb
PP
906void bt_graph_notify_ports_connected(struct bt_graph *graph,
907 struct bt_port *upstream_port, struct bt_port *downstream_port)
1bf957a0
PP
908{
909 size_t i;
910
262e5473
PP
911 BT_LOGV("Notifying graph listeners that two ports were connected: "
912 "graph-addr=%p, "
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));
917
f345f8bb 918 for (i = 0; i < graph->listeners.ports_connected->len; i++) {
1bf957a0 919 struct bt_graph_listener listener =
f345f8bb 920 g_array_index(graph->listeners.ports_connected,
1bf957a0 921 struct bt_graph_listener, i);
f345f8bb 922 bt_graph_ports_connected_listener func = listener.func;
1bf957a0 923
f6ccaed9 924 BT_ASSERT(func);
f345f8bb 925 func(upstream_port, downstream_port, listener.data);
1bf957a0
PP
926 }
927}
928
929BT_HIDDEN
f345f8bb
PP
930void 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)
1bf957a0
PP
934{
935 size_t i;
936
262e5473
PP
937 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
938 "graph-addr=%p, "
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));
943
f345f8bb 944 for (i = 0; i < graph->listeners.ports_disconnected->len; i++) {
1bf957a0 945 struct bt_graph_listener listener =
f345f8bb 946 g_array_index(graph->listeners.ports_disconnected,
1bf957a0 947 struct bt_graph_listener, i);
f345f8bb 948 bt_graph_ports_disconnected_listener func = listener.func;
1bf957a0 949
f6ccaed9 950 BT_ASSERT(func);
f345f8bb
PP
951 func(upstream_comp, downstream_comp, upstream_port,
952 downstream_port, listener.data);
1bf957a0
PP
953 }
954}
202a3a13 955
c7eee084 956enum bt_graph_status bt_graph_cancel(struct bt_graph *graph)
202a3a13
PP
957{
958 enum bt_graph_status ret = BT_GRAPH_STATUS_OK;
959
960 if (!graph) {
262e5473 961 BT_LOGW_STR("Invalid parameter: graph is NULL.");
202a3a13
PP
962 ret = BT_GRAPH_STATUS_INVALID;
963 goto end;
964 }
965
966 graph->canceled = BT_TRUE;
262e5473 967 BT_LOGV("Canceled graph: addr=%p", graph);
202a3a13
PP
968
969end:
970 return ret;
971}
972
c7eee084 973bt_bool bt_graph_is_canceled(struct bt_graph *graph)
202a3a13 974{
c7eee084
PP
975 bt_bool canceled = BT_FALSE;
976
977 if (!graph) {
978 BT_LOGW_STR("Invalid parameter: graph is NULL.");
979 goto end;
980 }
981
982 canceled = graph->canceled;
983
984end:
985 return canceled;
202a3a13 986}
f167d3c0
PP
987
988BT_HIDDEN
989void bt_graph_remove_connection(struct bt_graph *graph,
990 struct bt_connection *connection)
991{
f6ccaed9
PP
992 BT_ASSERT(graph);
993 BT_ASSERT(connection);
262e5473
PP
994 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
995 graph, connection);
f167d3c0
PP
996 g_ptr_array_remove(graph->connections, connection);
997}
36712f1d
PP
998
999enum 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)
1005{
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;
1010 size_t i;
4aa7981f 1011 bt_bool init_can_consume;
36712f1d
PP
1012
1013 bt_get(params);
1014
1015 if (!graph) {
1016 BT_LOGW_STR("Invalid parameter: graph is NULL.");
1017 graph_status = BT_GRAPH_STATUS_INVALID;
1018 goto end;
1019 }
4aa7981f 1020 init_can_consume = graph->can_consume;
36712f1d
PP
1021
1022 if (!component_class) {
1023 BT_LOGW_STR("Invalid parameter: component class is NULL.");
1024 graph_status = BT_GRAPH_STATUS_INVALID;
1025 goto end;
1026 }
1027
1d915789 1028 graph->can_consume = BT_FALSE;
36712f1d
PP
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);
1036
1037 if (!name) {
1038 BT_LOGW_STR("Invalid parameter: name is NULL.");
1039 graph_status = BT_GRAPH_STATUS_INVALID;
1040 goto end;
1041 }
1042
1043 if (graph->canceled) {
1044 BT_LOGW_STR("Invalid parameter: graph is canceled.");
1045 graph_status = BT_GRAPH_STATUS_CANCELED;
1046 goto end;
1047 }
1048
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: "
1053 "type=%d", type);
1054 graph_status = BT_GRAPH_STATUS_INVALID;
1055 goto end;
1056 }
1057
1058 for (i = 0; i < graph->components->len; i++) {
1059 void *other_comp = graph->components->pdata[i];
1060
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\"",
1064 other_comp, name);
1065 graph_status = BT_GRAPH_STATUS_INVALID;
1066 goto end;
1067 }
1068 }
1069
1070 /*
1071 * Parameters must be a map value, but we create a convenient
1072 * empty one if it's NULL.
1073 */
1074 if (params) {
1075 if (!bt_value_is_map(params)) {
1076 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
1077 "type=%s",
1078 bt_value_type_string(bt_value_get_type(params)));
1079 graph_status = BT_GRAPH_STATUS_INVALID;
1080 goto end;
1081 }
1082 } else {
1083 params = bt_value_map_create();
1084 if (!params) {
1085 BT_LOGE_STR("Cannot create map value object.");
1086 graph_status = BT_GRAPH_STATUS_NOMEM;
1087 goto end;
1088 }
1089 }
1090
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(
1096 comp_status);
1097 goto end;
1098 }
1099
1100 /*
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.
1104 */
1105 g_ptr_array_add(graph->components, component);
1106 bt_component_set_graph(component, graph);
1107
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,
1112 init_method_data);
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(
1118 comp_status);
1119 bt_component_set_graph(component, NULL);
1120 g_ptr_array_remove_fast(graph->components, component);
1121 goto end;
1122 }
1123 }
1124
1125 /*
1126 * Mark the component as initialized so that its finalization
1127 * method is called when it is destroyed.
1128 */
1129 component->initialized = true;
1130
1131 /*
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().
1134 */
1135 if (bt_component_is_sink(component)) {
2de524b3 1136 graph->has_sink = BT_TRUE;
36712f1d
PP
1137 g_queue_push_tail(graph->sinks_to_consume, component);
1138 }
1139
1140 /*
1141 * Freeze the component class now that it's instantiated at
1142 * least once.
1143 */
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);
1152
1153 if (user_component) {
1154 /* Move reference to user */
1155 *user_component = component;
1156 component = NULL;
1157 }
1158
1159end:
1160 bt_put(component);
1161 bt_put(params);
4aa7981f
JG
1162 if (graph) {
1163 graph->can_consume = init_can_consume;
1164 }
36712f1d
PP
1165 return graph_status;
1166}
1167
1168enum 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)
1173{
1174 return bt_graph_add_component_with_init_method_data(graph,
1175 component_class, name, params, NULL, component);
1176}
8ed535b5
PP
1177
1178BT_HIDDEN
1179int bt_graph_remove_unconnected_component(struct bt_graph *graph,
1180 struct bt_component *component)
1181{
4aa7981f 1182 bt_bool init_can_consume;
8ed535b5
PP
1183 int64_t count;
1184 uint64_t i;
1185 int ret = 0;
1186
f6ccaed9
PP
1187 BT_ASSERT(graph);
1188 BT_ASSERT(component);
1189 BT_ASSERT(component->base.ref_count.count == 0);
1190 BT_ASSERT(bt_component_borrow_graph(component) == graph);
8ed535b5 1191
4aa7981f 1192 init_can_consume = graph->can_consume;
8ed535b5
PP
1193 count = bt_component_get_input_port_count(component);
1194
1195 for (i = 0; i < count; i++) {
1196 struct bt_port *port =
1197 bt_component_get_input_port_by_index(component, i);
1198
f6ccaed9 1199 BT_ASSERT(port);
8ed535b5
PP
1200 bt_put(port);
1201
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\"",
1208 graph, component,
1209 bt_component_get_name(component),
1210 port, bt_port_get_name(port));
1211 goto error;
1212 }
1213 }
1214
1215 count = bt_component_get_output_port_count(component);
1216
1217 for (i = 0; i < count; i++) {
1218 struct bt_port *port =
1219 bt_component_get_output_port_by_index(component, i);
1220
f6ccaed9 1221 BT_ASSERT(port);
8ed535b5
PP
1222 bt_put(port);
1223
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\"",
1230 graph, component,
1231 bt_component_get_name(component),
1232 port, bt_port_get_name(port));
1233 goto error;
1234 }
1235 }
1236
1237 graph->can_consume = BT_FALSE;
1238
1239 /* Possibly remove from sinks to consume */
1240 (void) g_queue_remove(graph->sinks_to_consume, component);
1241
1242 if (graph->sinks_to_consume->length == 0) {
1243 graph->has_sink = BT_FALSE;
1244 }
1245
1246 /*
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).
1250 */
1251 g_ptr_array_remove(graph->components, component);
1252 goto end;
1253
1254error:
1255 ret = -1;
1256
1257end:
1258 graph->can_consume = init_can_consume;
1259 return ret;
1260}
5c563278
PP
1261
1262BT_HIDDEN
1263void bt_graph_add_notification(struct bt_graph *graph,
1264 struct bt_notification *notif)
1265{
1266 BT_ASSERT(graph);
1267 BT_ASSERT(notif);
1268
1269 /*
1270 * It's okay not to take a reference because, when a
1271 * notification's reference count drops to 0, either:
1272 *
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.
1276 */
1277 g_ptr_array_add(graph->notifications, notif);
1278}
This page took 0.095459 seconds and 4 git commands to generate.