lib: rename "begin" to "beginning" when used as a noun
[babeltrace.git] / lib / graph / graph.c
CommitLineData
c0418dd9 1/*
f60c8b34 2 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
262e5473 3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
c0418dd9
JG
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
262e5473
PP
24#define BT_LOG_TAG "GRAPH"
25#include <babeltrace/lib-logging-internal.h>
26
b2e0c907 27#include <babeltrace/graph/component-internal.h>
0d72b8c3
PP
28#include <babeltrace/graph/graph.h>
29#include <babeltrace/graph/graph-const.h>
b2e0c907
PP
30#include <babeltrace/graph/graph-internal.h>
31#include <babeltrace/graph/connection-internal.h>
32#include <babeltrace/graph/component-sink-internal.h>
0d72b8c3
PP
33#include <babeltrace/graph/component-source-const.h>
34#include <babeltrace/graph/component-filter-const.h>
35#include <babeltrace/graph/port-const.h>
5c563278
PP
36#include <babeltrace/graph/notification-internal.h>
37#include <babeltrace/graph/notification-event-internal.h>
38#include <babeltrace/graph/notification-packet-internal.h>
3d9990ac 39#include <babeltrace/compiler-internal.h>
da91b29a 40#include <babeltrace/common-internal.h>
c55a9f58 41#include <babeltrace/types.h>
36712f1d 42#include <babeltrace/values.h>
05e21286 43#include <babeltrace/values-const.h>
36712f1d 44#include <babeltrace/values-internal.h>
d94d92ac 45#include <babeltrace/object.h>
f6ccaed9
PP
46#include <babeltrace/assert-internal.h>
47#include <babeltrace/assert-pre-internal.h>
f60c8b34 48#include <unistd.h>
1bf957a0
PP
49#include <glib.h>
50
0d72b8c3
PP
51typedef void (*port_added_func_t)(const void *, const void *, void *);
52
53typedef void (*port_removed_func_t)(const void *, const void *, void *);
54
55typedef void (*ports_connected_func_t)(const void *, const void *, const void *,
56 const void *, void *);
57
58typedef void (*ports_disconnected_func_t)(const void *, const void *,
59 const void *, const void *, void *);
60
61typedef enum bt_self_component_status (*comp_init_method_t)(const void *,
62 const void *, void *);
d94d92ac 63
1bf957a0 64struct bt_graph_listener {
0d72b8c3 65 bt_graph_listener_removed_func removed;
1bf957a0
PP
66 void *data;
67};
c0418dd9 68
d94d92ac
PP
69struct bt_graph_listener_port_added {
70 struct bt_graph_listener base;
71 port_added_func_t func;
72};
8cc092c9 73
d94d92ac
PP
74struct bt_graph_listener_port_removed {
75 struct bt_graph_listener base;
76 port_removed_func_t func;
77};
8cc092c9 78
d94d92ac
PP
79struct bt_graph_listener_ports_connected {
80 struct bt_graph_listener base;
81 ports_connected_func_t func;
82};
8cc092c9 83
d94d92ac
PP
84struct bt_graph_listener_ports_disconnected {
85 struct bt_graph_listener base;
86 ports_disconnected_func_t func;
87};
8cc092c9 88
d94d92ac
PP
89#define INIT_LISTENERS_ARRAY(_type, _listeners) \
90 do { \
91 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
92 if (!(_listeners)) { \
93 BT_LOGE_STR("Failed to allocate one GArray."); \
94 } \
95 } while (0)
96
97#define CALL_REMOVE_LISTENERS(_type, _listeners) \
98 do { \
99 size_t i; \
100 \
101 for (i = 0; i < (_listeners)->len; i++) { \
102 _type *listener = \
103 &g_array_index((_listeners), _type, i); \
104 \
105 if (listener->base.removed) { \
106 listener->base.removed(listener->base.data); \
107 } \
108 } \
109 } while (0)
8cc092c9 110
f60c8b34 111static
d94d92ac 112void destroy_graph(struct bt_object *obj)
c0418dd9 113{
0d72b8c3 114 struct bt_graph *graph = container_of(obj, struct bt_graph, base);
c0418dd9 115
bd14d768
PP
116 /*
117 * The graph's reference count is 0 if we're here. Increment
118 * it to avoid a double-destroy (possibly infinitely recursive)
119 * in this situation:
120 *
121 * 1. We put and destroy a connection.
122 * 2. This connection's destructor finalizes its active
123 * notification iterators.
124 * 3. A notification iterator's finalization function gets a
125 * new reference on its component (reference count goes from
126 * 0 to 1).
127 * 4. Since this component's reference count goes to 1, it takes
128 * a reference on its parent (this graph). This graph's
129 * reference count goes from 0 to 1.
130 * 5. The notification iterator's finalization function puts its
131 * component reference (reference count goes from 1 to 0).
132 * 6. Since this component's reference count goes from 1 to 0,
133 * it puts its parent (this graph). This graph's reference
134 * count goes from 1 to 0.
135 * 7. Since this graph's reference count goes from 1 to 0,
136 * its destructor is called (this function).
137 *
138 * With the incrementation below, the graph's reference count at
139 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
140 * ensures that this function is not called two times.
141 */
d94d92ac 142 BT_LIB_LOGD("Destroying graph: %!+g", graph);
3fea54f6 143 obj->ref_count++;
bd14d768 144
49682acd
PP
145 /*
146 * Cancel the graph to disallow some operations, like creating
147 * notification iterators and adding ports to components.
148 */
0d72b8c3 149 (void) bt_graph_cancel((void *) graph);
49682acd 150
8cc092c9 151 /* Call all remove listeners */
d94d92ac
PP
152 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
153 graph->listeners.source_output_port_added);
154 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
155 graph->listeners.filter_output_port_added);
156 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
157 graph->listeners.filter_input_port_added);
158 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
159 graph->listeners.sink_input_port_added);
160 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed,
161 graph->listeners.source_output_port_removed);
162 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed,
163 graph->listeners.filter_output_port_removed);
164 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed,
165 graph->listeners.filter_input_port_removed);
166 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_removed,
167 graph->listeners.sink_input_port_removed);
168 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
169 graph->listeners.source_filter_ports_connected);
170 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
171 graph->listeners.source_sink_ports_connected);
172 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
173 graph->listeners.filter_sink_ports_connected);
174 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected,
175 graph->listeners.source_filter_ports_disconnected);
176 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected,
177 graph->listeners.source_sink_ports_disconnected);
178 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_disconnected,
179 graph->listeners.filter_sink_ports_disconnected);
8cc092c9 180
5c563278
PP
181 if (graph->notifications) {
182 g_ptr_array_free(graph->notifications, TRUE);
d94d92ac 183 graph->notifications = NULL;
5c563278
PP
184 }
185
c0418dd9 186 if (graph->connections) {
262e5473 187 BT_LOGD_STR("Destroying connections.");
c0418dd9 188 g_ptr_array_free(graph->connections, TRUE);
d94d92ac 189 graph->connections = NULL;
c0418dd9 190 }
5c563278 191
bd14d768 192 if (graph->components) {
262e5473 193 BT_LOGD_STR("Destroying components.");
bd14d768 194 g_ptr_array_free(graph->components, TRUE);
d94d92ac 195 graph->components = NULL;
bd14d768 196 }
5c563278 197
f60c8b34
JG
198 if (graph->sinks_to_consume) {
199 g_queue_free(graph->sinks_to_consume);
d94d92ac
PP
200 graph->sinks_to_consume = NULL;
201 }
202
203 if (graph->listeners.source_output_port_added) {
204 g_array_free(graph->listeners.source_output_port_added, TRUE);
205 graph->listeners.source_output_port_added = NULL;
206 }
207
208 if (graph->listeners.filter_output_port_added) {
209 g_array_free(graph->listeners.filter_output_port_added, TRUE);
210 graph->listeners.filter_output_port_added = NULL;
211 }
212
213 if (graph->listeners.filter_input_port_added) {
214 g_array_free(graph->listeners.filter_input_port_added, TRUE);
215 graph->listeners.filter_input_port_added = NULL;
c0418dd9 216 }
1bf957a0 217
d94d92ac
PP
218 if (graph->listeners.sink_input_port_added) {
219 g_array_free(graph->listeners.sink_input_port_added, TRUE);
220 graph->listeners.sink_input_port_added = NULL;
1bf957a0
PP
221 }
222
d94d92ac
PP
223 if (graph->listeners.source_output_port_removed) {
224 g_array_free(graph->listeners.source_output_port_removed, TRUE);
225 graph->listeners.source_output_port_removed = NULL;
1bf957a0
PP
226 }
227
d94d92ac
PP
228 if (graph->listeners.filter_output_port_removed) {
229 g_array_free(graph->listeners.filter_output_port_removed, TRUE);
230 graph->listeners.filter_output_port_removed = NULL;
1bf957a0
PP
231 }
232
d94d92ac
PP
233 if (graph->listeners.filter_input_port_removed) {
234 g_array_free(graph->listeners.filter_input_port_removed, TRUE);
235 graph->listeners.filter_input_port_removed = NULL;
236 }
237
238 if (graph->listeners.sink_input_port_removed) {
239 g_array_free(graph->listeners.sink_input_port_removed, TRUE);
240 graph->listeners.sink_input_port_removed = NULL;
241 }
242
243 if (graph->listeners.source_filter_ports_connected) {
244 g_array_free(graph->listeners.source_filter_ports_connected,
245 TRUE);
246 graph->listeners.source_filter_ports_connected = NULL;
247 }
248
249 if (graph->listeners.source_sink_ports_connected) {
250 g_array_free(graph->listeners.source_sink_ports_connected,
251 TRUE);
252 graph->listeners.source_sink_ports_connected = NULL;
253 }
254
255 if (graph->listeners.filter_sink_ports_connected) {
256 g_array_free(graph->listeners.filter_sink_ports_connected,
257 TRUE);
258 graph->listeners.filter_sink_ports_connected = NULL;
259 }
260
261 if (graph->listeners.source_filter_ports_disconnected) {
262 g_array_free(graph->listeners.source_filter_ports_disconnected,
263 TRUE);
264 graph->listeners.source_filter_ports_disconnected = NULL;
265 }
266
267 if (graph->listeners.source_sink_ports_disconnected) {
268 g_array_free(graph->listeners.source_sink_ports_disconnected,
269 TRUE);
270 graph->listeners.source_sink_ports_disconnected = NULL;
271 }
272
273 if (graph->listeners.filter_sink_ports_disconnected) {
274 g_array_free(graph->listeners.filter_sink_ports_disconnected,
275 TRUE);
276 graph->listeners.filter_sink_ports_disconnected = NULL;
1bf957a0
PP
277 }
278
5c563278
PP
279 bt_object_pool_finalize(&graph->event_notif_pool);
280 bt_object_pool_finalize(&graph->packet_begin_notif_pool);
281 bt_object_pool_finalize(&graph->packet_end_notif_pool);
c0418dd9
JG
282 g_free(graph);
283}
284
5c563278
PP
285static
286void destroy_notification_event(struct bt_notification *notif,
287 struct bt_graph *graph)
288{
289 bt_notification_event_destroy(notif);
290}
291
292static
293void destroy_notification_packet_begin(struct bt_notification *notif,
294 struct bt_graph *graph)
295{
bb5bb160 296 bt_notification_packet_beginning_destroy(notif);
5c563278
PP
297}
298
299static
300void destroy_notification_packet_end(struct bt_notification *notif,
301 struct bt_graph *graph)
302{
303 bt_notification_packet_end_destroy(notif);
304}
305
306static
307void notify_notification_graph_is_destroyed(struct bt_notification *notif)
308{
309 bt_notification_unlink_graph(notif);
310}
311
0d72b8c3 312struct bt_graph *bt_graph_create(void)
c0418dd9 313{
f60c8b34 314 struct bt_graph *graph;
1bf957a0 315 int ret;
c0418dd9 316
262e5473 317 BT_LOGD_STR("Creating graph object.");
f60c8b34 318 graph = g_new0(struct bt_graph, 1);
c0418dd9 319 if (!graph) {
262e5473 320 BT_LOGE_STR("Failed to allocate one graph.");
c0418dd9
JG
321 goto end;
322 }
323
d94d92ac 324 bt_object_init_shared(&graph->base, destroy_graph);
3fea54f6
PP
325 graph->connections = g_ptr_array_new_with_free_func(
326 (GDestroyNotify) bt_object_try_spec_release);
c0418dd9 327 if (!graph->connections) {
262e5473 328 BT_LOGE_STR("Failed to allocate one GPtrArray.");
c0418dd9
JG
329 goto error;
330 }
3fea54f6
PP
331 graph->components = g_ptr_array_new_with_free_func(
332 (GDestroyNotify) bt_object_try_spec_release);
f60c8b34 333 if (!graph->components) {
262e5473 334 BT_LOGE_STR("Failed to allocate one GPtrArray.");
f60c8b34
JG
335 goto error;
336 }
337 graph->sinks_to_consume = g_queue_new();
338 if (!graph->sinks_to_consume) {
262e5473 339 BT_LOGE_STR("Failed to allocate one GQueue.");
c0418dd9
JG
340 goto error;
341 }
1bf957a0 342
d94d92ac
PP
343 bt_graph_set_can_consume(graph, true);
344 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
345 graph->listeners.source_output_port_added);
346
347 if (!graph->listeners.source_output_port_added) {
348 ret = -1;
1bf957a0
PP
349 goto error;
350 }
351
d94d92ac
PP
352 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
353 graph->listeners.filter_output_port_added);
354
355 if (!graph->listeners.filter_output_port_added) {
356 ret = -1;
1bf957a0
PP
357 goto error;
358 }
359
d94d92ac
PP
360 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
361 graph->listeners.filter_input_port_added);
362
363 if (!graph->listeners.filter_input_port_added) {
364 ret = -1;
1bf957a0
PP
365 goto error;
366 }
367
d94d92ac
PP
368 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
369 graph->listeners.sink_input_port_added);
370
371 if (!graph->listeners.sink_input_port_added) {
372 ret = -1;
373 goto error;
374 }
375
376 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed,
377 graph->listeners.source_output_port_removed);
378
379 if (!graph->listeners.source_output_port_removed) {
380 ret = -1;
381 goto error;
382 }
383
384 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed,
385 graph->listeners.filter_output_port_removed);
386
387 if (!graph->listeners.filter_output_port_removed) {
388 ret = -1;
389 goto error;
390 }
391
392 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed,
393 graph->listeners.filter_input_port_removed);
394
395 if (!graph->listeners.filter_input_port_removed) {
396 ret = -1;
397 goto error;
398 }
399
400 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_removed,
401 graph->listeners.sink_input_port_removed);
402
403 if (!graph->listeners.sink_input_port_removed) {
404 ret = -1;
405 goto error;
406 }
407
408 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
409 graph->listeners.source_filter_ports_connected);
410
411 if (!graph->listeners.source_filter_ports_connected) {
412 ret = -1;
413 goto error;
414 }
415
416 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
417 graph->listeners.source_sink_ports_connected);
418
419 if (!graph->listeners.source_sink_ports_connected) {
420 ret = -1;
421 goto error;
422 }
423
424 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
425 graph->listeners.filter_sink_ports_connected);
426
427 if (!graph->listeners.filter_sink_ports_connected) {
428 ret = -1;
429 goto error;
430 }
431
432 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected,
433 graph->listeners.source_filter_ports_disconnected);
434
435 if (!graph->listeners.source_filter_ports_disconnected) {
436 ret = -1;
437 goto error;
438 }
439
440 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected,
441 graph->listeners.source_sink_ports_disconnected);
442
443 if (!graph->listeners.source_sink_ports_disconnected) {
444 ret = -1;
445 goto error;
446 }
447
448 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_disconnected,
449 graph->listeners.filter_sink_ports_disconnected);
450
451 if (!graph->listeners.filter_sink_ports_disconnected) {
452 ret = -1;
1bf957a0
PP
453 goto error;
454 }
455
5c563278
PP
456 ret = bt_object_pool_initialize(&graph->event_notif_pool,
457 (bt_object_pool_new_object_func) bt_notification_event_new,
458 (bt_object_pool_destroy_object_func) destroy_notification_event,
459 graph);
460 if (ret) {
461 BT_LOGE("Failed to initialize event notification pool: ret=%d",
462 ret);
463 goto error;
464 }
465
466 ret = bt_object_pool_initialize(&graph->packet_begin_notif_pool,
bb5bb160 467 (bt_object_pool_new_object_func) bt_notification_packet_beginning_new,
5c563278
PP
468 (bt_object_pool_destroy_object_func) destroy_notification_packet_begin,
469 graph);
470 if (ret) {
471 BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d",
472 ret);
473 goto error;
474 }
475
476 ret = bt_object_pool_initialize(&graph->packet_end_notif_pool,
477 (bt_object_pool_new_object_func) bt_notification_packet_end_new,
478 (bt_object_pool_destroy_object_func) destroy_notification_packet_end,
479 graph);
480 if (ret) {
481 BT_LOGE("Failed to initialize packet end notification pool: ret=%d",
482 ret);
483 goto error;
484 }
485
486 graph->notifications = g_ptr_array_new_with_free_func(
487 (GDestroyNotify) notify_notification_graph_is_destroyed);
d94d92ac 488 BT_LIB_LOGD("Created graph object: %!+g", graph);
262e5473 489
c0418dd9 490end:
a2d06fd5 491 return (void *) graph;
d94d92ac 492
c0418dd9 493error:
65300d60 494 BT_OBJECT_PUT_REF_AND_RESET(graph);
c0418dd9
JG
495 goto end;
496}
497
0d72b8c3
PP
498enum bt_graph_status bt_graph_connect_ports(
499 struct bt_graph *graph,
500 const struct bt_port_output *upstream_port_out,
501 const struct bt_port_input *downstream_port_in,
502 const struct bt_connection **user_connection)
f60c8b34 503{
a256a42d 504 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
f60c8b34 505 struct bt_connection *connection = NULL;
d94d92ac
PP
506 struct bt_port *upstream_port = (void *) upstream_port_out;
507 struct bt_port *downstream_port = (void *) downstream_port_in;
f60c8b34
JG
508 struct bt_component *upstream_component = NULL;
509 struct bt_component *downstream_component = NULL;
d94d92ac
PP
510 enum bt_self_component_status component_status;
511 bool init_can_consume;
f60c8b34 512
d94d92ac
PP
513 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
514 BT_ASSERT_PRE_NON_NULL(upstream_port, "Upstream port");
515 BT_ASSERT_PRE_NON_NULL(downstream_port, "Downstream port port");
516 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
517 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port),
518 "Upstream port is already connected: %!+p", upstream_port);
519 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port),
520 "Downstream port is already connected: %!+p", downstream_port);
0d72b8c3 521 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port),
d94d92ac
PP
522 "Upstream port does not belong to a component: %!+p",
523 upstream_port);
0d72b8c3 524 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port),
d94d92ac
PP
525 "Downstream port does not belong to a component: %!+p",
526 downstream_port);
4aa7981f 527 init_can_consume = graph->can_consume;
d94d92ac
PP
528 BT_LIB_LOGD("Connecting component ports within graph: "
529 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
530 graph, upstream_port, downstream_port);
531 bt_graph_set_can_consume(graph, false);
0d72b8c3 532 upstream_component = bt_port_borrow_component_inline(
d94d92ac 533 (void *) upstream_port);
0d72b8c3 534 downstream_component = bt_port_borrow_component_inline(
d94d92ac 535 (void *) downstream_port);
262e5473 536
0d8b4d8e
PP
537 /*
538 * At this point the ports are not connected yet. Both
539 * components need to accept an eventual connection to their
540 * port by the other port before we continue.
541 */
d94d92ac
PP
542 BT_LIB_LOGD("Asking upstream component to accept the connection: "
543 "%![comp-]+c", upstream_component);
0d8b4d8e 544 component_status = bt_component_accept_port_connection(
d94d92ac
PP
545 upstream_component, (void *) upstream_port,
546 (void *) downstream_port);
547 if (component_status != BT_SELF_COMPONENT_STATUS_OK) {
548 if (component_status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
262e5473
PP
549 BT_LOGD_STR("Upstream component refused the connection.");
550 } else {
551 BT_LOGW("Cannot ask upstream component to accept the connection: "
d94d92ac 552 "status=%s", bt_self_component_status_string(component_status));
262e5473
PP
553 }
554
d94d92ac 555 status = (int) component_status;
a256a42d 556 goto end;
0d8b4d8e 557 }
262e5473 558
d94d92ac
PP
559 BT_LIB_LOGD("Asking downstream component to accept the connection: "
560 "%![comp-]+c", downstream_component);
0d8b4d8e 561 component_status = bt_component_accept_port_connection(
d94d92ac
PP
562 downstream_component, (void *) downstream_port,
563 (void *) upstream_port);
564 if (component_status != BT_SELF_COMPONENT_STATUS_OK) {
565 if (component_status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
262e5473
PP
566 BT_LOGD_STR("Downstream component refused the connection.");
567 } else {
568 BT_LOGW("Cannot ask downstream component to accept the connection: "
d94d92ac 569 "status=%s", bt_self_component_status_string(component_status));
262e5473
PP
570 }
571
d94d92ac 572 status = (int) component_status;
a256a42d 573 goto end;
0d8b4d8e
PP
574 }
575
262e5473 576 BT_LOGD_STR("Creating connection.");
d94d92ac
PP
577 connection = bt_connection_create(graph, (void *) upstream_port,
578 (void *) downstream_port);
f60c8b34 579 if (!connection) {
262e5473 580 BT_LOGW("Cannot create connection object.");
a256a42d
PP
581 status = BT_GRAPH_STATUS_NOMEM;
582 goto end;
f60c8b34
JG
583 }
584
d94d92ac 585 BT_LIB_LOGD("Connection object created: %!+x", connection);
262e5473 586
f60c8b34 587 /*
72b913fb
PP
588 * Ownership of upstream_component/downstream_component and of
589 * the connection object is transferred to the graph.
f60c8b34
JG
590 */
591 g_ptr_array_add(graph->connections, connection);
ffeb0eed 592
f60c8b34 593 /*
0d8b4d8e 594 * Notify both components that their port is connected.
f60c8b34 595 */
d94d92ac
PP
596 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
597 "%![comp-]+c, %![port-]+p", upstream_component, upstream_port);
bf55043c 598 component_status = bt_component_port_connected(upstream_component,
d94d92ac
PP
599 (void *) upstream_port, (void *) downstream_port);
600 if (component_status != BT_SELF_COMPONENT_STATUS_OK) {
601 BT_LIB_LOGW("Error while notifying upstream component that its port is connected: "
602 "status=%s, %![graph-]+g, %![up-comp-]+c, "
603 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
604 bt_self_component_status_string(component_status),
605 graph, upstream_component, downstream_component,
606 upstream_port, downstream_port);
bf55043c 607 bt_connection_end(connection, true);
d94d92ac 608 status = (int) component_status;
bf55043c
PP
609 goto end;
610 }
611
612 connection->notified_upstream_port_connected = true;
d94d92ac
PP
613 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
614 "%![comp-]+c, %![port-]+p", downstream_component,
615 downstream_port);
bf55043c 616 component_status = bt_component_port_connected(downstream_component,
d94d92ac
PP
617 (void *) downstream_port, (void *) upstream_port);
618 if (component_status != BT_SELF_COMPONENT_STATUS_OK) {
619 BT_LIB_LOGW("Error while notifying downstream component that its port is connected: "
620 "status=%s, %![graph-]+g, %![up-comp-]+c, "
621 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
622 bt_self_component_status_string(component_status),
623 graph, upstream_component, downstream_component,
624 upstream_port, downstream_port);
bf55043c 625 bt_connection_end(connection, true);
d94d92ac 626 status = (int) component_status;
bf55043c
PP
627 goto end;
628 }
629
630 connection->notified_downstream_port_connected = true;
1bf957a0
PP
631
632 /*
0d8b4d8e 633 * Notify the graph's creator that both ports are connected.
1bf957a0 634 */
262e5473 635 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
f345f8bb 636 bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
bf55043c 637 connection->notified_graph_ports_connected = true;
d94d92ac
PP
638 BT_LIB_LOGD("Connected component ports within graph: "
639 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
640 "%![up-port-]+p, %![down-port-]+p",
641 graph, upstream_component, downstream_component,
642 upstream_port, downstream_port);
1bf957a0 643
a256a42d 644 if (user_connection) {
1a6a376a
PP
645 /* Move reference to user */
646 *user_connection = connection;
647 connection = NULL;
a256a42d
PP
648 }
649
f60c8b34 650end:
65300d60 651 bt_object_put_ref(connection);
d94d92ac
PP
652 (void) init_can_consume;
653 bt_graph_set_can_consume(graph, init_can_consume);
a256a42d 654 return status;
f60c8b34
JG
655}
656
ad847455 657static inline
d94d92ac 658enum bt_graph_status consume_graph_sink(struct bt_component_sink *comp)
c0418dd9 659{
d94d92ac
PP
660 enum bt_self_component_status comp_status;
661 struct bt_component_class_sink *sink_class = NULL;
662
663 BT_ASSERT(comp);
664 sink_class = (void *) comp->parent.class;
665 BT_ASSERT(sink_class->methods.consume);
666 BT_LIB_LOGD("Calling user's consume method: %!+c", comp);
667 comp_status = sink_class->methods.consume((void *) comp);
668 BT_LOGD("User method returned: status=%s",
669 bt_self_component_status_string(comp_status));
670 BT_ASSERT_PRE(comp_status == BT_SELF_COMPONENT_STATUS_OK ||
671 comp_status == BT_SELF_COMPONENT_STATUS_END ||
672 comp_status == BT_SELF_COMPONENT_STATUS_AGAIN ||
673 comp_status == BT_SELF_COMPONENT_STATUS_ERROR ||
674 comp_status == BT_SELF_COMPONENT_STATUS_NOMEM,
675 "Invalid component status returned by consuming method: "
676 "status=%s", bt_self_component_status_string(comp_status));
677 if (comp_status < 0) {
678 BT_LOGW_STR("Consume method failed.");
679 goto end;
680 }
681
682 BT_LIB_LOGV("Consumed from sink: %![comp-]+c, status=%s",
683 comp, bt_self_component_status_string(comp_status));
684
685end:
686 return (int) comp_status;
8ed535b5
PP
687}
688
689/*
690 * `node` is removed from the queue of sinks to consume when passed to
691 * this function. This function adds it back to the queue if there's
692 * still something to consume afterwards.
693 */
ad847455 694static inline
d94d92ac 695enum bt_graph_status consume_sink_node(struct bt_graph *graph, GList *node)
8ed535b5
PP
696{
697 enum bt_graph_status status;
d94d92ac 698 struct bt_component_sink *sink;
8ed535b5
PP
699
700 sink = node->data;
701 status = consume_graph_sink(sink);
ad847455 702 if (unlikely(status != BT_GRAPH_STATUS_END)) {
8ed535b5 703 g_queue_push_tail_link(graph->sinks_to_consume, node);
f60c8b34
JG
704 goto end;
705 }
706
707 /* End reached, the node is not added back to the queue and free'd. */
8ed535b5 708 g_queue_delete_link(graph->sinks_to_consume, node);
f60c8b34
JG
709
710 /* Don't forward an END status if there are sinks left to consume. */
711 if (!g_queue_is_empty(graph->sinks_to_consume)) {
712 status = BT_GRAPH_STATUS_OK;
713 goto end;
714 }
8ed535b5
PP
715
716end:
d94d92ac
PP
717 BT_LIB_LOGV("Consumed sink node: %![comp-]+c, status=%s",
718 sink, bt_graph_status_string(status));
8ed535b5
PP
719 return status;
720}
721
722BT_HIDDEN
723enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
d94d92ac 724 struct bt_component_sink *sink)
8ed535b5
PP
725{
726 enum bt_graph_status status;
727 GList *sink_node;
728 int index;
729
d94d92ac
PP
730 BT_LIB_LOGV("Making specific sink consume: %![comp-]+c", sink);
731 BT_ASSERT(bt_component_borrow_graph((void *) sink) == graph);
8ed535b5
PP
732
733 if (g_queue_is_empty(graph->sinks_to_consume)) {
734 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
735 status = BT_GRAPH_STATUS_END;
736 goto end;
737 }
738
739 index = g_queue_index(graph->sinks_to_consume, sink);
740 if (index < 0) {
741 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
742 status = BT_GRAPH_STATUS_END;
743 goto end;
744 }
745
746 sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index);
f6ccaed9 747 BT_ASSERT(sink_node);
8ed535b5
PP
748 status = consume_sink_node(graph, sink_node);
749
750end:
751 return status;
752}
753
ad847455 754static inline
d94d92ac 755enum bt_graph_status consume_no_check(struct bt_graph *graph)
8ed535b5
PP
756{
757 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
758 struct bt_component *sink;
759 GList *current_node;
760
f6ccaed9
PP
761 BT_ASSERT_PRE(graph->has_sink,
762 "Graph has no sink component: %!+g", graph);
d94d92ac 763 BT_LIB_LOGV("Making next sink consume: %![graph-]+g", graph);
8ed535b5 764
ad847455 765 if (unlikely(g_queue_is_empty(graph->sinks_to_consume))) {
8ed535b5
PP
766 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
767 status = BT_GRAPH_STATUS_END;
768 goto end;
769 }
770
771 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
772 sink = current_node->data;
d94d92ac 773 BT_LIB_LOGV("Chose next sink to consume: %!+c", sink);
8ed535b5
PP
774 status = consume_sink_node(graph, current_node);
775
f60c8b34
JG
776end:
777 return status;
c0418dd9
JG
778}
779
0d72b8c3
PP
780enum bt_graph_status bt_graph_consume(
781 struct bt_graph *graph)
851b70bd 782{
f6ccaed9 783 enum bt_graph_status status;
1d915789 784
f6ccaed9
PP
785 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
786 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
787 BT_ASSERT_PRE(graph->can_consume,
788 "Cannot consume graph in its current state: %!+g", graph);
ad847455 789 bt_graph_set_can_consume(graph, BT_FALSE);
d94d92ac 790 status = consume_no_check(graph);
ad847455 791 bt_graph_set_can_consume(graph, BT_TRUE);
26a15756 792 return status;
851b70bd
PP
793}
794
0d72b8c3 795enum bt_graph_status bt_graph_run(struct bt_graph *graph)
f60c8b34 796{
72b913fb 797 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
f60c8b34 798
d94d92ac
PP
799 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
800 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
ad847455
PP
801 BT_ASSERT_PRE(graph->can_consume,
802 "Cannot consume graph in its current state: %!+g", graph);
803 bt_graph_set_can_consume(graph, BT_FALSE);
d94d92ac 804 BT_LIB_LOGV("Running graph: %!+g", graph);
262e5473 805
f60c8b34 806 do {
851b70bd
PP
807 /*
808 * Check if the graph is canceled at each iteration. If
809 * the graph was canceled by another thread or by a
d94d92ac
PP
810 * signal handler, this is not a warning nor an error,
811 * it was intentional: log with a DEBUG level only.
851b70bd 812 */
ad847455 813 if (unlikely(graph->canceled)) {
d94d92ac
PP
814 BT_LIB_LOGD("Stopping the graph: graph is canceled: "
815 "%!+g", graph);
851b70bd
PP
816 status = BT_GRAPH_STATUS_CANCELED;
817 goto end;
818 }
819
d94d92ac 820 status = consume_no_check(graph);
ad847455 821 if (unlikely(status == BT_GRAPH_STATUS_AGAIN)) {
f60c8b34 822 /*
202a3a13
PP
823 * If AGAIN is received and there are multiple
824 * sinks, go ahead and consume from the next
825 * sink.
f60c8b34 826 *
202a3a13
PP
827 * However, in the case where a single sink is
828 * left, the caller can decide to busy-wait and
0d72b8c3 829 * call bt_graph_run() continuously
d94d92ac
PP
830 * until the source is ready or it can decide to
831 * sleep for an arbitrary amount of time.
f60c8b34
JG
832 */
833 if (graph->sinks_to_consume->length > 1) {
72b913fb 834 status = BT_GRAPH_STATUS_OK;
f60c8b34 835 }
2de524b3
PP
836 } else if (status == BT_GRAPH_STATUS_NO_SINK) {
837 goto end;
f60c8b34 838 }
72b913fb 839 } while (status == BT_GRAPH_STATUS_OK);
f60c8b34
JG
840
841 if (g_queue_is_empty(graph->sinks_to_consume)) {
72b913fb 842 status = BT_GRAPH_STATUS_END;
f60c8b34 843 }
262e5473 844
202a3a13 845end:
d94d92ac
PP
846 BT_LIB_LOGV("Graph ran: %![graph-]+g, status=%s", graph,
847 bt_graph_status_string(status));
848 bt_graph_set_can_consume(graph, BT_TRUE);
72b913fb 849 return status;
f60c8b34 850}
1bf957a0 851
d94d92ac 852enum bt_graph_status
0d72b8c3
PP
853bt_graph_add_source_component_output_port_added_listener(
854 struct bt_graph *graph,
855 bt_graph_source_component_output_port_added_listener_func func,
856 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac 857 int *out_listener_id)
1bf957a0 858{
d94d92ac
PP
859 struct bt_graph_listener_port_added listener = {
860 .base = {
861 .removed = listener_removed,
862 .data = data,
863 },
864 .func = (port_added_func_t) func,
1bf957a0 865 };
d94d92ac 866 int listener_id;
1bf957a0 867
d94d92ac
PP
868 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
869 BT_ASSERT_PRE_NON_NULL(func, "Listener");
870 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
871 BT_ASSERT_PRE(!graph->in_remove_listener,
872 "Graph currently executing a \"listener removed\" listener: "
873 "%!+g", graph);
874 g_array_append_val(graph->listeners.source_output_port_added, listener);
875 listener_id = graph->listeners.source_output_port_added->len - 1;
876 BT_LIB_LOGV("Added \"source component output port added\" listener to graph: "
877 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
878 listener_id);
879
880 if (listener_id) {
881 *out_listener_id = listener_id;
882 }
883
884 return BT_GRAPH_STATUS_OK;
1bf957a0
PP
885}
886
d94d92ac 887enum bt_graph_status
0d72b8c3
PP
888bt_graph_add_filter_component_output_port_added_listener(
889 struct bt_graph *graph,
890 bt_graph_filter_component_output_port_added_listener_func func,
891 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac 892 int *out_listener_id)
1bf957a0 893{
d94d92ac
PP
894 struct bt_graph_listener_port_added listener = {
895 .base = {
896 .removed = listener_removed,
897 .data = data,
898 },
899 .func = (port_added_func_t) func,
900 };
901 int listener_id;
1bf957a0 902
d94d92ac
PP
903 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
904 BT_ASSERT_PRE_NON_NULL(func, "Listener");
905 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
906 BT_ASSERT_PRE(!graph->in_remove_listener,
907 "Graph currently executing a \"listener removed\" listener: "
908 "%!+g", graph);
909 g_array_append_val(graph->listeners.filter_output_port_added, listener);
910 listener_id = graph->listeners.filter_output_port_added->len - 1;
911 BT_LIB_LOGV("Added \"filter component output port added\" listener to graph: "
912 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
913 listener_id);
914
915 if (listener_id) {
916 *out_listener_id = listener_id;
917 }
918
919 return BT_GRAPH_STATUS_OK;
920}
262e5473 921
d94d92ac 922enum bt_graph_status
0d72b8c3
PP
923bt_graph_add_filter_component_input_port_added_listener(
924 struct bt_graph *graph,
925 bt_graph_filter_component_input_port_added_listener_func func,
926 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
927 int *out_listener_id)
928{
d94d92ac
PP
929 struct bt_graph_listener_port_added listener = {
930 .base = {
931 .removed = listener_removed,
932 .data = data,
933 },
934 .func = (port_added_func_t) func,
935 };
936 int listener_id;
8cc092c9 937
d94d92ac
PP
938 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
939 BT_ASSERT_PRE_NON_NULL(func, "Listener");
940 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
941 BT_ASSERT_PRE(!graph->in_remove_listener,
942 "Graph currently executing a \"listener removed\" listener: "
943 "%!+g", graph);
944 g_array_append_val(graph->listeners.filter_input_port_added, listener);
945 listener_id = graph->listeners.filter_input_port_added->len - 1;
946 BT_LIB_LOGV("Added \"filter component input port added\" listener to graph: "
947 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
948 listener_id);
949
950 if (listener_id) {
951 *out_listener_id = listener_id;
952 }
953
954 return BT_GRAPH_STATUS_OK;
955}
1bf957a0 956
d94d92ac 957enum bt_graph_status
0d72b8c3
PP
958bt_graph_add_sink_component_input_port_added_listener(
959 struct bt_graph *graph,
960 bt_graph_sink_component_input_port_added_listener_func func,
961 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
962 int *out_listener_id)
963{
d94d92ac
PP
964 struct bt_graph_listener_port_added listener = {
965 .base = {
966 .removed = listener_removed,
967 .data = data,
968 },
969 .func = (port_added_func_t) func,
970 };
971 int listener_id;
1bf957a0 972
d94d92ac
PP
973 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
974 BT_ASSERT_PRE_NON_NULL(func, "Listener");
975 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
976 BT_ASSERT_PRE(!graph->in_remove_listener,
977 "Graph currently executing a \"listener removed\" listener: "
978 "%!+g", graph);
979 g_array_append_val(graph->listeners.sink_input_port_added, listener);
980 listener_id = graph->listeners.sink_input_port_added->len - 1;
981 BT_LIB_LOGV("Added \"sink component input port added\" listener to graph: "
982 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
983 listener_id);
984
985 if (listener_id) {
986 *out_listener_id = listener_id;
987 }
988
989 return BT_GRAPH_STATUS_OK;
1bf957a0
PP
990}
991
d94d92ac 992enum bt_graph_status
0d72b8c3
PP
993bt_graph_add_source_component_output_port_removed_listener(
994 struct bt_graph *graph,
995 bt_graph_source_component_output_port_removed_listener_func func,
996 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac 997 int *out_listener_id)
1bf957a0 998{
d94d92ac
PP
999 struct bt_graph_listener_port_removed listener = {
1000 .base = {
1001 .removed = listener_removed,
1002 .data = data,
1003 },
1004 .func = (port_removed_func_t) func,
1005 };
1006 int listener_id;
1bf957a0 1007
d94d92ac
PP
1008 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1009 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1010 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1011 BT_ASSERT_PRE(!graph->in_remove_listener,
1012 "Graph currently executing a \"listener removed\" listener: "
1013 "%!+g", graph);
1014 g_array_append_val(graph->listeners.source_output_port_removed, listener);
1015 listener_id = graph->listeners.source_output_port_removed->len - 1;
1016 BT_LIB_LOGV("Added \"source component output port removed\" listener to graph: "
1017 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1018 listener_id);
1019
1020 if (listener_id) {
1021 *out_listener_id = listener_id;
1022 }
1023
1024 return BT_GRAPH_STATUS_OK;
1025}
262e5473 1026
d94d92ac 1027enum bt_graph_status
0d72b8c3
PP
1028bt_graph_add_filter_component_output_port_removed_listener(
1029 struct bt_graph *graph,
1030 bt_graph_filter_component_output_port_removed_listener_func func,
1031 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
1032 int *out_listener_id)
1033{
d94d92ac
PP
1034 struct bt_graph_listener_port_removed listener = {
1035 .base = {
1036 .removed = listener_removed,
1037 .data = data,
1038 },
1039 .func = (port_removed_func_t) func,
1040 };
1041 int listener_id;
8cc092c9 1042
d94d92ac
PP
1043 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1044 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1045 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1046 BT_ASSERT_PRE(!graph->in_remove_listener,
1047 "Graph currently executing a \"listener removed\" listener: "
1048 "%!+g", graph);
1049 g_array_append_val(graph->listeners.filter_output_port_removed, listener);
1050 listener_id = graph->listeners.filter_output_port_removed->len - 1;
1051 BT_LIB_LOGV("Added \"filter component output port removed\" listener to graph: "
1052 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1053 listener_id);
1054
1055 if (listener_id) {
1056 *out_listener_id = listener_id;
1057 }
1058
1059 return BT_GRAPH_STATUS_OK;
1060}
1bf957a0 1061
d94d92ac 1062enum bt_graph_status
0d72b8c3
PP
1063bt_graph_add_filter_component_input_port_removed_listener(
1064 struct bt_graph *graph,
1065 bt_graph_filter_component_input_port_removed_listener_func func,
1066 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
1067 int *out_listener_id)
1068{
d94d92ac
PP
1069 struct bt_graph_listener_port_removed listener = {
1070 .base = {
1071 .removed = listener_removed,
1072 .data = data,
1073 },
1074 .func = (port_removed_func_t) func,
1075 };
1076 int listener_id;
1bf957a0 1077
d94d92ac
PP
1078 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1079 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1080 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1081 BT_ASSERT_PRE(!graph->in_remove_listener,
1082 "Graph currently executing a \"listener removed\" listener: "
1083 "%!+g", graph);
1084 g_array_append_val(graph->listeners.filter_input_port_removed, listener);
1085 listener_id = graph->listeners.filter_input_port_removed->len - 1;
1086 BT_LIB_LOGV("Added \"filter component input port removed\" listener to graph: "
1087 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1088 listener_id);
1089
1090 if (listener_id) {
1091 *out_listener_id = listener_id;
1092 }
1093
1094 return BT_GRAPH_STATUS_OK;
1bf957a0
PP
1095}
1096
d94d92ac 1097enum bt_graph_status
0d72b8c3
PP
1098bt_graph_add_sink_component_input_port_removed_listener(
1099 struct bt_graph *graph,
1100 bt_graph_sink_component_input_port_removed_listener_func func,
1101 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac 1102 int *out_listener_id)
1bf957a0 1103{
d94d92ac
PP
1104 struct bt_graph_listener_port_removed listener = {
1105 .base = {
1106 .removed = listener_removed,
1107 .data = data,
1108 },
1109 .func = (port_removed_func_t) func,
1110 };
1111 int listener_id;
1bf957a0 1112
d94d92ac
PP
1113 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1114 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1115 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1116 BT_ASSERT_PRE(!graph->in_remove_listener,
1117 "Graph currently executing a \"listener removed\" listener: "
1118 "%!+g", graph);
1119 g_array_append_val(graph->listeners.sink_input_port_removed, listener);
1120 listener_id = graph->listeners.sink_input_port_removed->len - 1;
1121 BT_LIB_LOGV("Added \"sink component input port removed\" listener to graph: "
1122 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1123 listener_id);
1124
1125 if (listener_id) {
1126 *out_listener_id = listener_id;
1127 }
1128
1129 return BT_GRAPH_STATUS_OK;
1130}
262e5473 1131
d94d92ac 1132enum bt_graph_status
0d72b8c3
PP
1133bt_graph_add_source_filter_component_ports_connected_listener(
1134 struct bt_graph *graph,
1135 bt_graph_source_filter_component_ports_connected_listener_func func,
1136 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
1137 int *out_listener_id)
1138{
d94d92ac
PP
1139 struct bt_graph_listener_ports_connected listener = {
1140 .base = {
1141 .removed = listener_removed,
1142 .data = data,
1143 },
1144 .func = (ports_connected_func_t) func,
1145 };
1146 int listener_id;
8cc092c9 1147
d94d92ac
PP
1148 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1149 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1150 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1151 BT_ASSERT_PRE(!graph->in_remove_listener,
1152 "Graph currently executing a \"listener removed\" listener: "
1153 "%!+g", graph);
1154 g_array_append_val(graph->listeners.source_filter_ports_connected,
1155 listener);
1156 listener_id = graph->listeners.source_filter_ports_connected->len - 1;
1157 BT_LIB_LOGV("Added \"source to filter component ports connected\" listener to graph: "
1158 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1159 listener_id);
1160
1161 if (listener_id) {
1162 *out_listener_id = listener_id;
1163 }
1164
1165 return BT_GRAPH_STATUS_OK;
1166}
1bf957a0 1167
d94d92ac 1168enum bt_graph_status
0d72b8c3
PP
1169bt_graph_add_source_sink_component_ports_connected_listener(
1170 struct bt_graph *graph,
1171 bt_graph_source_sink_component_ports_connected_listener_func func,
1172 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
1173 int *out_listener_id)
1174{
d94d92ac
PP
1175 struct bt_graph_listener_ports_connected listener = {
1176 .base = {
1177 .removed = listener_removed,
1178 .data = data,
1179 },
1180 .func = (ports_connected_func_t) func,
1181 };
1182 int listener_id;
1bf957a0 1183
d94d92ac
PP
1184 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1185 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1186 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1187 BT_ASSERT_PRE(!graph->in_remove_listener,
1188 "Graph currently executing a \"listener removed\" listener: "
1189 "%!+g", graph);
1190 g_array_append_val(graph->listeners.source_sink_ports_connected,
1191 listener);
1192 listener_id = graph->listeners.source_sink_ports_connected->len - 1;
1193 BT_LIB_LOGV("Added \"source to sink component ports connected\" listener to graph: "
1194 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1195 listener_id);
1196
1197 if (listener_id) {
1198 *out_listener_id = listener_id;
1199 }
1200
1201 return BT_GRAPH_STATUS_OK;
1bf957a0
PP
1202}
1203
d94d92ac 1204enum bt_graph_status
0d72b8c3
PP
1205bt_graph_add_filter_sink_component_ports_connected_listener(
1206 struct bt_graph *graph,
1207 bt_graph_filter_sink_component_ports_connected_listener_func func,
1208 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac 1209 int *out_listener_id)
1bf957a0 1210{
d94d92ac
PP
1211 struct bt_graph_listener_ports_connected listener = {
1212 .base = {
1213 .removed = listener_removed,
1214 .data = data,
1215 },
1216 .func = (ports_connected_func_t) func,
1217 };
1218 int listener_id;
1bf957a0 1219
d94d92ac
PP
1220 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1221 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1222 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1223 BT_ASSERT_PRE(!graph->in_remove_listener,
1224 "Graph currently executing a \"listener removed\" listener: "
1225 "%!+g", graph);
1226 g_array_append_val(graph->listeners.filter_sink_ports_connected,
1227 listener);
1228 listener_id = graph->listeners.filter_sink_ports_connected->len - 1;
1229 BT_LIB_LOGV("Added \"filter to sink component ports connected\" listener to graph: "
1230 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1231 listener_id);
1232
1233 if (listener_id) {
1234 *out_listener_id = listener_id;
1235 }
1236
1237 return BT_GRAPH_STATUS_OK;
1238}
262e5473 1239
d94d92ac 1240enum bt_graph_status
0d72b8c3
PP
1241bt_graph_add_source_filter_component_ports_disconnected_listener(
1242 struct bt_graph *graph,
1243 bt_graph_source_filter_component_ports_disconnected_listener_func func,
1244 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
1245 int *out_listener_id)
1246{
d94d92ac
PP
1247 struct bt_graph_listener_ports_disconnected listener = {
1248 .base = {
1249 .removed = listener_removed,
1250 .data = data,
1251 },
1252 .func = (ports_disconnected_func_t) func,
1253 };
1254 int listener_id;
8cc092c9 1255
d94d92ac
PP
1256 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1257 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1258 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1259 BT_ASSERT_PRE(!graph->in_remove_listener,
1260 "Graph currently executing a \"listener removed\" listener: "
1261 "%!+g", graph);
1262 g_array_append_val(graph->listeners.source_filter_ports_disconnected,
1263 listener);
1264 listener_id = graph->listeners.source_filter_ports_disconnected->len - 1;
1265 BT_LIB_LOGV("Added \"source to filter component ports disconnected\" listener to graph: "
1266 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1267 listener_id);
1268
1269 if (listener_id) {
1270 *out_listener_id = listener_id;
1271 }
1272
1273 return BT_GRAPH_STATUS_OK;
1274}
1bf957a0 1275
d94d92ac 1276enum bt_graph_status
0d72b8c3
PP
1277bt_graph_add_source_sink_component_ports_disconnected_listener(
1278 struct bt_graph *graph,
1279 bt_graph_source_sink_component_ports_disconnected_listener_func func,
1280 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
1281 int *out_listener_id)
1282{
d94d92ac
PP
1283 struct bt_graph_listener_ports_disconnected listener = {
1284 .base = {
1285 .removed = listener_removed,
1286 .data = data,
1287 },
1288 .func = (ports_disconnected_func_t) func,
1289 };
1290 int listener_id;
1bf957a0 1291
d94d92ac
PP
1292 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1293 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1294 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1295 BT_ASSERT_PRE(!graph->in_remove_listener,
1296 "Graph currently executing a \"listener removed\" listener: "
1297 "%!+g", graph);
1298 g_array_append_val(graph->listeners.source_sink_ports_disconnected,
1299 listener);
1300 listener_id = graph->listeners.source_sink_ports_disconnected->len - 1;
1301 BT_LIB_LOGV("Added \"source to sink component ports disconnected\" listener to graph: "
1302 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1303 listener_id);
1304
1305 if (listener_id) {
1306 *out_listener_id = listener_id;
1307 }
1308
1309 return BT_GRAPH_STATUS_OK;
1310}
1311
1312enum bt_graph_status
0d72b8c3
PP
1313bt_graph_add_filter_sink_component_ports_disconnected_listener(
1314 struct bt_graph *graph,
1315 bt_graph_filter_sink_component_ports_disconnected_listener_func func,
1316 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
1317 int *out_listener_id)
1318{
d94d92ac
PP
1319 struct bt_graph_listener_ports_disconnected listener = {
1320 .base = {
1321 .removed = listener_removed,
1322 .data = data,
1323 },
1324 .func = (ports_disconnected_func_t) func,
1325 };
1326 int listener_id;
1327
1328 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1329 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1330 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1331 BT_ASSERT_PRE(!graph->in_remove_listener,
1332 "Graph currently executing a \"listener removed\" listener: "
1333 "%!+g", graph);
1334 g_array_append_val(graph->listeners.filter_sink_ports_disconnected,
1335 listener);
1336 listener_id = graph->listeners.filter_sink_ports_disconnected->len - 1;
1337 BT_LIB_LOGV("Added \"filter to sink component ports disconnected\" listener to graph: "
1338 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1339 listener_id);
1340
1341 if (listener_id) {
1342 *out_listener_id = listener_id;
1343 }
1344
1345 return BT_GRAPH_STATUS_OK;
1bf957a0
PP
1346}
1347
1348BT_HIDDEN
1349void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port)
1350{
d94d92ac
PP
1351 uint64_t i;
1352 GArray *listeners;
1353 struct bt_component *comp;
1bf957a0 1354
d94d92ac
PP
1355 BT_ASSERT(graph);
1356 BT_ASSERT(port);
1357 BT_LIB_LOGV("Notifying graph listeners that a port was added: "
1358 "%![graph-]+g, %![port-]+p", graph, port);
0d72b8c3 1359 comp = bt_port_borrow_component_inline(port);
d94d92ac
PP
1360 BT_ASSERT(comp);
1361
1362 switch (comp->class->type) {
1363 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1364 {
1365 switch (port->type) {
1366 case BT_PORT_TYPE_OUTPUT:
1367 listeners = graph->listeners.source_output_port_added;
1368 break;
1369 default:
1370 abort();
1371 }
1372
1373 break;
1374 }
1375 case BT_COMPONENT_CLASS_TYPE_FILTER:
1376 {
1377 switch (port->type) {
1378 case BT_PORT_TYPE_INPUT:
1379 listeners = graph->listeners.filter_input_port_added;
1380 break;
1381 case BT_PORT_TYPE_OUTPUT:
1382 listeners = graph->listeners.filter_output_port_added;
1383 break;
1384 default:
1385 abort();
1386 }
262e5473 1387
d94d92ac
PP
1388 break;
1389 }
1390 case BT_COMPONENT_CLASS_TYPE_SINK:
1391 {
1392 switch (port->type) {
1393 case BT_PORT_TYPE_INPUT:
1394 listeners = graph->listeners.sink_input_port_added;
1395 break;
1396 default:
1397 abort();
1398 }
1bf957a0 1399
d94d92ac
PP
1400 break;
1401 }
1402 default:
1403 abort();
1404 }
1405
1406 for (i = 0; i < listeners->len; i++) {
1407 struct bt_graph_listener_port_added *listener =
1408 &g_array_index(listeners,
1409 struct bt_graph_listener_port_added, i);
1410
1411 BT_ASSERT(listener->func);
1412 listener->func(comp, port, listener->base.data);
1bf957a0
PP
1413 }
1414}
1415
1416BT_HIDDEN
1417void bt_graph_notify_port_removed(struct bt_graph *graph,
1418 struct bt_component *comp, struct bt_port *port)
1419{
d94d92ac
PP
1420 uint64_t i;
1421 GArray *listeners;
1422
1423 BT_ASSERT(graph);
1424 BT_ASSERT(port);
1425 BT_LIB_LOGV("Notifying graph listeners that a port was removed: "
1426 "%![graph-]+g, %![comp-]+c, %![port-]+p", graph, comp, port);
1427
1428 switch (comp->class->type) {
1429 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1430 {
1431 switch (port->type) {
1432 case BT_PORT_TYPE_OUTPUT:
1433 listeners = graph->listeners.source_output_port_removed;
1434 break;
1435 default:
1436 abort();
1437 }
1438
1439 break;
1440 }
1441 case BT_COMPONENT_CLASS_TYPE_FILTER:
1442 {
1443 switch (port->type) {
1444 case BT_PORT_TYPE_INPUT:
1445 listeners = graph->listeners.filter_input_port_removed;
1446 break;
1447 case BT_PORT_TYPE_OUTPUT:
1448 listeners = graph->listeners.filter_output_port_removed;
1449 break;
1450 default:
1451 abort();
1452 }
1453
1454 break;
1455 }
1456 case BT_COMPONENT_CLASS_TYPE_SINK:
1457 {
1458 switch (port->type) {
1459 case BT_PORT_TYPE_INPUT:
1460 listeners = graph->listeners.sink_input_port_removed;
1461 break;
1462 default:
1463 abort();
1464 }
1bf957a0 1465
d94d92ac
PP
1466 break;
1467 }
1468 default:
1469 abort();
1470 }
262e5473 1471
d94d92ac
PP
1472 for (i = 0; i < listeners->len; i++) {
1473 struct bt_graph_listener_port_removed *listener =
1474 &g_array_index(listeners,
1475 struct bt_graph_listener_port_removed, i);
1bf957a0 1476
d94d92ac
PP
1477 BT_ASSERT(listener->func);
1478 listener->func(comp, port, listener->base.data);
1bf957a0
PP
1479 }
1480}
1481
1482BT_HIDDEN
f345f8bb
PP
1483void bt_graph_notify_ports_connected(struct bt_graph *graph,
1484 struct bt_port *upstream_port, struct bt_port *downstream_port)
1bf957a0 1485{
d94d92ac
PP
1486 uint64_t i;
1487 GArray *listeners;
1488 struct bt_component *upstream_comp;
1489 struct bt_component *downstream_comp;
1bf957a0 1490
d94d92ac
PP
1491 BT_ASSERT(graph);
1492 BT_ASSERT(upstream_port);
1493 BT_ASSERT(downstream_port);
1494 BT_LIB_LOGV("Notifying graph listeners that ports were connected: "
1495 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1496 graph, upstream_port, downstream_port);
0d72b8c3 1497 upstream_comp = bt_port_borrow_component_inline(upstream_port);
d94d92ac 1498 BT_ASSERT(upstream_comp);
0d72b8c3 1499 downstream_comp = bt_port_borrow_component_inline(downstream_port);
d94d92ac
PP
1500 BT_ASSERT(downstream_comp);
1501
1502 switch (upstream_comp->class->type) {
1503 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1504 {
1505 switch (downstream_comp->class->type) {
1506 case BT_COMPONENT_CLASS_TYPE_FILTER:
1507 listeners =
1508 graph->listeners.source_filter_ports_connected;
1509 break;
1510 case BT_COMPONENT_CLASS_TYPE_SINK:
1511 listeners =
1512 graph->listeners.source_sink_ports_connected;
1513 break;
1514 default:
1515 abort();
1516 }
262e5473 1517
d94d92ac
PP
1518 break;
1519 }
1520 case BT_COMPONENT_CLASS_TYPE_FILTER:
1521 {
1522 switch (downstream_comp->class->type) {
1523 case BT_COMPONENT_CLASS_TYPE_SINK:
1524 listeners =
1525 graph->listeners.filter_sink_ports_connected;
1526 break;
1527 default:
1528 abort();
1529 }
1bf957a0 1530
d94d92ac
PP
1531 break;
1532 }
1533 default:
1534 abort();
1535 }
1536
1537 for (i = 0; i < listeners->len; i++) {
1538 struct bt_graph_listener_ports_connected *listener =
1539 &g_array_index(listeners,
1540 struct bt_graph_listener_ports_connected, i);
1541
1542 BT_ASSERT(listener->func);
1543 listener->func(upstream_comp, downstream_comp,
1544 upstream_port, downstream_port, listener->base.data);
1bf957a0
PP
1545 }
1546}
1547
1548BT_HIDDEN
f345f8bb
PP
1549void bt_graph_notify_ports_disconnected(struct bt_graph *graph,
1550 struct bt_component *upstream_comp,
1551 struct bt_component *downstream_comp,
d94d92ac
PP
1552 struct bt_port *upstream_port,
1553 struct bt_port *downstream_port)
1bf957a0 1554{
d94d92ac
PP
1555 uint64_t i;
1556 GArray *listeners;
1557
1558 BT_ASSERT(graph);
1559 BT_ASSERT(upstream_comp);
1560 BT_ASSERT(downstream_comp);
1561 BT_ASSERT(upstream_port);
1562 BT_ASSERT(downstream_port);
1563 BT_LIB_LOGV("Notifying graph listeners that ports were disconnected: "
1564 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p, "
1565 "%![up-comp-]+c, %![down-comp-]+c",
1566 graph, upstream_port, downstream_port, upstream_comp,
1567 downstream_comp);
1568
1569 switch (upstream_comp->class->type) {
1570 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1571 {
1572 switch (downstream_comp->class->type) {
1573 case BT_COMPONENT_CLASS_TYPE_FILTER:
1574 listeners =
1575 graph->listeners.source_filter_ports_disconnected;
1576 break;
1577 case BT_COMPONENT_CLASS_TYPE_SINK:
1578 listeners =
1579 graph->listeners.source_sink_ports_disconnected;
1580 break;
1581 default:
1582 abort();
1583 }
1bf957a0 1584
d94d92ac
PP
1585 break;
1586 }
1587 case BT_COMPONENT_CLASS_TYPE_FILTER:
1588 {
1589 switch (downstream_comp->class->type) {
1590 case BT_COMPONENT_CLASS_TYPE_SINK:
1591 listeners =
1592 graph->listeners.filter_sink_ports_disconnected;
1593 break;
1594 default:
1595 abort();
1596 }
1597
1598 break;
1599 }
1600 default:
1601 abort();
1602 }
262e5473 1603
d94d92ac
PP
1604 for (i = 0; i < listeners->len; i++) {
1605 struct bt_graph_listener_ports_disconnected *listener =
1606 &g_array_index(listeners,
1607 struct bt_graph_listener_ports_disconnected, i);
1bf957a0 1608
d94d92ac
PP
1609 BT_ASSERT(listener->func);
1610 listener->func(upstream_comp, downstream_comp,
1611 upstream_port, downstream_port, listener->base.data);
1bf957a0
PP
1612 }
1613}
202a3a13 1614
0d72b8c3
PP
1615enum bt_graph_status bt_graph_cancel(
1616 struct bt_graph *graph)
202a3a13 1617{
202a3a13 1618
d94d92ac
PP
1619 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1620 graph->canceled = true;
1621 BT_LIB_LOGV("Canceled graph: %!+i", graph);
1622 return BT_GRAPH_STATUS_OK;
202a3a13
PP
1623}
1624
0d72b8c3 1625bt_bool bt_graph_is_canceled(const struct bt_graph *graph)
202a3a13 1626{
d94d92ac
PP
1627 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1628 return graph->canceled ? BT_TRUE : BT_FALSE;
202a3a13 1629}
f167d3c0
PP
1630
1631BT_HIDDEN
1632void bt_graph_remove_connection(struct bt_graph *graph,
1633 struct bt_connection *connection)
1634{
f6ccaed9
PP
1635 BT_ASSERT(graph);
1636 BT_ASSERT(connection);
d94d92ac 1637 BT_LIB_LOGV("Removing graph's connection: %![graph-]+g, %![conn-]+x",
262e5473 1638 graph, connection);
f167d3c0
PP
1639 g_ptr_array_remove(graph->connections, connection);
1640}
36712f1d 1641
d94d92ac
PP
1642BT_ASSERT_PRE_FUNC
1643static inline
1644bool component_name_exists(struct bt_graph *graph, const char *name)
1645{
1646 bool exists = false;
1647 uint64_t i;
1648
1649 for (i = 0; i < graph->components->len; i++) {
1650 struct bt_component *other_comp = graph->components->pdata[i];
1651
1652 if (strcmp(name, bt_component_get_name(other_comp)) == 0) {
1653 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1654 "%![other-comp-]+c, name=\"%s\"",
1655 other_comp, name);
1656 exists = true;
1657 goto end;
1658 }
1659 }
1660
1661end:
1662 return exists;
1663}
1664
1665static
1666enum bt_graph_status add_component_with_init_method_data(
0d72b8c3 1667 struct bt_graph *graph,
d94d92ac
PP
1668 struct bt_component_class *comp_cls,
1669 comp_init_method_t init_method,
05e21286 1670 const char *name, const struct bt_value *params,
d94d92ac 1671 void *init_method_data, struct bt_component **user_component)
36712f1d
PP
1672{
1673 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
d94d92ac 1674 enum bt_self_component_status comp_status;
36712f1d 1675 struct bt_component *component = NULL;
d94d92ac
PP
1676 int ret;
1677 bool init_can_consume;
05e21286 1678 struct bt_value *new_params = NULL;
36712f1d 1679
d94d92ac
PP
1680 BT_ASSERT(comp_cls);
1681 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1682 BT_ASSERT_PRE_NON_NULL(name, "Name");
1683 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
1684 BT_ASSERT_PRE(!component_name_exists(graph, name),
1685 "Duplicate component name: %!+g, name=\"%s\"", graph, name);
1686 BT_ASSERT_PRE(!params || bt_value_is_map(params),
1687 "Parameter value is not a map value: %!+v", params);
4aa7981f 1688 init_can_consume = graph->can_consume;
d94d92ac
PP
1689 bt_graph_set_can_consume(graph, false);
1690 BT_LIB_LOGD("Adding component to graph: "
1691 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
36712f1d 1692 "init-method-data-addr=%p",
d94d92ac 1693 graph, comp_cls, name, params, init_method_data);
36712f1d 1694
d94d92ac 1695 if (!params) {
05e21286
PP
1696 new_params = bt_value_map_create();
1697 if (!new_params) {
36712f1d
PP
1698 BT_LOGE_STR("Cannot create map value object.");
1699 graph_status = BT_GRAPH_STATUS_NOMEM;
1700 goto end;
1701 }
05e21286
PP
1702
1703 params = new_params;
36712f1d
PP
1704 }
1705
d94d92ac
PP
1706 ret = bt_component_create(comp_cls, name, &component);
1707 if (ret) {
1708 BT_LOGE("Cannot create empty component object: ret=%d",
1709 ret);
1710 graph_status = BT_GRAPH_STATUS_NOMEM;
36712f1d
PP
1711 goto end;
1712 }
1713
1714 /*
1715 * The user's initialization method needs to see that this
1716 * component is part of the graph. If the user method fails, we
1717 * immediately remove the component from the graph's components.
1718 */
1719 g_ptr_array_add(graph->components, component);
1720 bt_component_set_graph(component, graph);
1721
d94d92ac 1722 if (init_method) {
36712f1d 1723 BT_LOGD_STR("Calling user's initialization method.");
d94d92ac 1724 comp_status = init_method(component, params, init_method_data);
36712f1d 1725 BT_LOGD("User method returned: status=%s",
d94d92ac
PP
1726 bt_self_component_status_string(comp_status));
1727 if (comp_status != BT_SELF_COMPONENT_STATUS_OK) {
36712f1d 1728 BT_LOGW_STR("Initialization method failed.");
d94d92ac 1729 graph_status = (int) comp_status;
36712f1d
PP
1730 bt_component_set_graph(component, NULL);
1731 g_ptr_array_remove_fast(graph->components, component);
1732 goto end;
1733 }
1734 }
1735
1736 /*
1737 * Mark the component as initialized so that its finalization
1738 * method is called when it is destroyed.
1739 */
1740 component->initialized = true;
1741
1742 /*
1743 * If it's a sink component, it needs to be part of the graph's
1744 * sink queue to be consumed by bt_graph_consume().
1745 */
1746 if (bt_component_is_sink(component)) {
d94d92ac 1747 graph->has_sink = true;
36712f1d
PP
1748 g_queue_push_tail(graph->sinks_to_consume, component);
1749 }
1750
1751 /*
1752 * Freeze the component class now that it's instantiated at
1753 * least once.
1754 */
1755 BT_LOGD_STR("Freezing component class.");
d94d92ac
PP
1756 bt_component_class_freeze(comp_cls);
1757 BT_LIB_LOGD("Added component to graph: "
1758 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1759 "init-method-data-addr=%p, %![comp-]+c",
1760 graph, comp_cls, name, params, init_method_data, component);
36712f1d
PP
1761
1762 if (user_component) {
1763 /* Move reference to user */
1764 *user_component = component;
1765 component = NULL;
1766 }
1767
1768end:
65300d60 1769 bt_object_put_ref(component);
05e21286 1770 bt_object_put_ref(new_params);
d94d92ac
PP
1771 (void) init_can_consume;
1772 bt_graph_set_can_consume(graph, init_can_consume);
36712f1d
PP
1773 return graph_status;
1774}
1775
d94d92ac 1776enum bt_graph_status
0d72b8c3
PP
1777bt_graph_add_source_component_with_init_method_data(
1778 struct bt_graph *graph,
1779 const struct bt_component_class_source *comp_cls,
05e21286 1780 const char *name, const struct bt_value *params,
0d72b8c3
PP
1781 void *init_method_data,
1782 const struct bt_component_source **component)
d94d92ac
PP
1783{
1784 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1785 return add_component_with_init_method_data(graph,
1786 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1787 name, params, init_method_data, (void *) component);
1788}
1789
0d72b8c3
PP
1790enum bt_graph_status bt_graph_add_source_component(
1791 struct bt_graph *graph,
1792 const struct bt_component_class_source *comp_cls,
05e21286 1793 const char *name, const struct bt_value *params,
0d72b8c3 1794 const struct bt_component_source **component)
d94d92ac 1795{
0d72b8c3 1796 return bt_graph_add_source_component_with_init_method_data(
d94d92ac
PP
1797 graph, comp_cls, name, params, NULL, component);
1798}
1799
1800enum bt_graph_status
0d72b8c3
PP
1801bt_graph_add_filter_component_with_init_method_data(
1802 struct bt_graph *graph,
1803 const struct bt_component_class_filter *comp_cls,
05e21286 1804 const char *name, const struct bt_value *params,
0d72b8c3
PP
1805 void *init_method_data,
1806 const struct bt_component_filter **component)
d94d92ac
PP
1807{
1808 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1809 return add_component_with_init_method_data(graph,
1810 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1811 name, params, init_method_data, (void *) component);
1812}
1813
0d72b8c3
PP
1814enum bt_graph_status bt_graph_add_filter_component(
1815 struct bt_graph *graph,
1816 const struct bt_component_class_filter *comp_cls,
05e21286 1817 const char *name, const struct bt_value *params,
0d72b8c3 1818 const struct bt_component_filter **component)
d94d92ac 1819{
0d72b8c3 1820 return bt_graph_add_filter_component_with_init_method_data(
d94d92ac
PP
1821 graph, comp_cls, name, params, NULL, component);
1822}
1823
1824enum bt_graph_status
0d72b8c3
PP
1825bt_graph_add_sink_component_with_init_method_data(
1826 struct bt_graph *graph,
1827 const struct bt_component_class_sink *comp_cls,
05e21286 1828 const char *name, const struct bt_value *params,
0d72b8c3
PP
1829 void *init_method_data,
1830 const struct bt_component_sink **component)
36712f1d 1831{
d94d92ac
PP
1832 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1833 return add_component_with_init_method_data(graph,
1834 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1835 name, params, init_method_data, (void *) component);
1836}
1837
0d72b8c3
PP
1838enum bt_graph_status bt_graph_add_sink_component(
1839 struct bt_graph *graph,
1840 const struct bt_component_class_sink *comp_cls,
05e21286 1841 const char *name, const struct bt_value *params,
0d72b8c3 1842 const struct bt_component_sink **component)
d94d92ac 1843{
0d72b8c3 1844 return bt_graph_add_sink_component_with_init_method_data(
d94d92ac 1845 graph, comp_cls, name, params, NULL, component);
36712f1d 1846}
8ed535b5
PP
1847
1848BT_HIDDEN
1849int bt_graph_remove_unconnected_component(struct bt_graph *graph,
1850 struct bt_component *component)
1851{
d94d92ac
PP
1852 bool init_can_consume;
1853 uint64_t count;
8ed535b5
PP
1854 uint64_t i;
1855 int ret = 0;
1856
f6ccaed9
PP
1857 BT_ASSERT(graph);
1858 BT_ASSERT(component);
3fea54f6 1859 BT_ASSERT(component->base.ref_count == 0);
f6ccaed9 1860 BT_ASSERT(bt_component_borrow_graph(component) == graph);
8ed535b5 1861
4aa7981f 1862 init_can_consume = graph->can_consume;
8ed535b5
PP
1863 count = bt_component_get_input_port_count(component);
1864
1865 for (i = 0; i < count; i++) {
d94d92ac
PP
1866 struct bt_port *port = (void *)
1867 bt_component_borrow_input_port_by_index(component, i);
8ed535b5 1868
f6ccaed9 1869 BT_ASSERT(port);
8ed535b5
PP
1870
1871 if (bt_port_is_connected(port)) {
d94d92ac 1872 BT_LIB_LOGW("Cannot remove component from graph: "
8ed535b5 1873 "an input port is connected: "
d94d92ac
PP
1874 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1875 graph, component, port);
8ed535b5
PP
1876 goto error;
1877 }
1878 }
1879
1880 count = bt_component_get_output_port_count(component);
1881
1882 for (i = 0; i < count; i++) {
d94d92ac
PP
1883 struct bt_port *port = (void *)
1884 bt_component_borrow_output_port_by_index(component, i);
8ed535b5 1885
f6ccaed9 1886 BT_ASSERT(port);
8ed535b5
PP
1887
1888 if (bt_port_is_connected(port)) {
d94d92ac 1889 BT_LIB_LOGW("Cannot remove component from graph: "
8ed535b5 1890 "an output port is connected: "
d94d92ac
PP
1891 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1892 graph, component, port);
8ed535b5
PP
1893 goto error;
1894 }
1895 }
1896
d94d92ac 1897 bt_graph_set_can_consume(graph, false);
8ed535b5
PP
1898
1899 /* Possibly remove from sinks to consume */
1900 (void) g_queue_remove(graph->sinks_to_consume, component);
1901
1902 if (graph->sinks_to_consume->length == 0) {
d94d92ac 1903 graph->has_sink = false;
8ed535b5
PP
1904 }
1905
1906 /*
3fea54f6
PP
1907 * This calls bt_object_try_spec_release() on the component, and
1908 * since its reference count is 0, its destructor is called. Its
8ed535b5
PP
1909 * destructor calls the user's finalization method (if set).
1910 */
1911 g_ptr_array_remove(graph->components, component);
1912 goto end;
1913
1914error:
1915 ret = -1;
1916
1917end:
d94d92ac
PP
1918 (void) init_can_consume;
1919 bt_graph_set_can_consume(graph, init_can_consume);
8ed535b5
PP
1920 return ret;
1921}
5c563278
PP
1922
1923BT_HIDDEN
1924void bt_graph_add_notification(struct bt_graph *graph,
1925 struct bt_notification *notif)
1926{
1927 BT_ASSERT(graph);
1928 BT_ASSERT(notif);
1929
1930 /*
1931 * It's okay not to take a reference because, when a
1932 * notification's reference count drops to 0, either:
1933 *
1934 * * It is recycled back to one of this graph's pool.
1935 * * It is destroyed because it doesn't have any link to any
1936 * graph, which means the original graph is already destroyed.
1937 */
1938 g_ptr_array_add(graph->notifications, notif);
1939}
This page took 0.130497 seconds and 4 git commands to generate.