lib: strictly type function return status enumerations
[babeltrace.git] / src / lib / graph / graph.c
CommitLineData
c0418dd9 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
f60c8b34 3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@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
350ad6c1 24#define BT_LOG_TAG "LIB/GRAPH"
c2d9d9cf 25#include "lib/logging.h"
262e5473 26
578e048b
MJ
27#include "common/assert.h"
28#include "lib/assert-pre.h"
f6f301d7 29#include "lib/assert-post.h"
3fadfbc0
MJ
30#include <babeltrace2/graph/graph.h>
31#include <babeltrace2/graph/graph-const.h>
3fadfbc0
MJ
32#include <babeltrace2/graph/component-source-const.h>
33#include <babeltrace2/graph/component-filter-const.h>
34#include <babeltrace2/graph/port-const.h>
578e048b
MJ
35#include "lib/graph/message/message.h"
36#include "compat/compiler.h"
37#include "common/common.h"
3fadfbc0
MJ
38#include <babeltrace2/types.h>
39#include <babeltrace2/value.h>
40#include <babeltrace2/value-const.h>
578e048b 41#include "lib/value.h"
f60c8b34 42#include <unistd.h>
1bf957a0
PP
43#include <glib.h>
44
578e048b
MJ
45#include "component.h"
46#include "component-sink.h"
47#include "connection.h"
48#include "graph.h"
49#include "message/event.h"
50#include "message/packet.h"
51
d24d5663
PP
52typedef enum bt_graph_listener_func_status
53(*port_added_func_t)(const void *, const void *, void *);
0d72b8c3 54
d24d5663
PP
55typedef enum bt_graph_listener_func_status
56(*ports_connected_func_t)(const void *, const void *, const void *,
0d72b8c3 57 const void *, void *);
d94d92ac 58
d24d5663
PP
59typedef enum bt_component_class_init_method_status
60(*comp_init_method_t)(const void *, const void *, void *);
61
1bf957a0 62struct bt_graph_listener {
0d72b8c3 63 bt_graph_listener_removed_func removed;
1bf957a0
PP
64 void *data;
65};
c0418dd9 66
d94d92ac
PP
67struct bt_graph_listener_port_added {
68 struct bt_graph_listener base;
69 port_added_func_t func;
70};
8cc092c9 71
d94d92ac
PP
72struct bt_graph_listener_ports_connected {
73 struct bt_graph_listener base;
74 ports_connected_func_t func;
75};
8cc092c9 76
d94d92ac
PP
77#define INIT_LISTENERS_ARRAY(_type, _listeners) \
78 do { \
79 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
80 if (!(_listeners)) { \
81 BT_LOGE_STR("Failed to allocate one GArray."); \
82 } \
83 } while (0)
84
85#define CALL_REMOVE_LISTENERS(_type, _listeners) \
86 do { \
87 size_t i; \
88 \
9fde7203
FD
89 if (!_listeners) { \
90 break; \
91 } \
d94d92ac
PP
92 for (i = 0; i < (_listeners)->len; i++) { \
93 _type *listener = \
94 &g_array_index((_listeners), _type, i); \
95 \
96 if (listener->base.removed) { \
97 listener->base.removed(listener->base.data); \
98 } \
99 } \
100 } while (0)
8cc092c9 101
f60c8b34 102static
d94d92ac 103void destroy_graph(struct bt_object *obj)
c0418dd9 104{
0d72b8c3 105 struct bt_graph *graph = container_of(obj, struct bt_graph, base);
c0418dd9 106
bd14d768
PP
107 /*
108 * The graph's reference count is 0 if we're here. Increment
109 * it to avoid a double-destroy (possibly infinitely recursive)
110 * in this situation:
111 *
112 * 1. We put and destroy a connection.
d0fea130
PP
113 * 2. This connection's destructor finalizes its active message
114 * iterators.
115 * 3. A message iterator's finalization function gets a new
116 * reference on its component (reference count goes from 0 to
117 * 1).
bd14d768
PP
118 * 4. Since this component's reference count goes to 1, it takes
119 * a reference on its parent (this graph). This graph's
120 * reference count goes from 0 to 1.
d6e69534 121 * 5. The message iterator's finalization function puts its
bd14d768
PP
122 * component reference (reference count goes from 1 to 0).
123 * 6. Since this component's reference count goes from 1 to 0,
124 * it puts its parent (this graph). This graph's reference
125 * count goes from 1 to 0.
d0fea130
PP
126 * 7. Since this graph's reference count goes from 1 to 0, its
127 * destructor is called (this function).
bd14d768
PP
128 *
129 * With the incrementation below, the graph's reference count at
130 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
131 * ensures that this function is not called two times.
132 */
3f7d4d90 133 BT_LIB_LOGI("Destroying graph: %!+g", graph);
3fea54f6 134 obj->ref_count++;
bd14d768 135
49682acd
PP
136 /*
137 * Cancel the graph to disallow some operations, like creating
d6e69534 138 * message iterators and adding ports to components.
49682acd 139 */
0d72b8c3 140 (void) bt_graph_cancel((void *) graph);
49682acd 141
8cc092c9 142 /* Call all remove listeners */
d94d92ac
PP
143 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
144 graph->listeners.source_output_port_added);
145 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
146 graph->listeners.filter_output_port_added);
147 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
148 graph->listeners.filter_input_port_added);
149 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
150 graph->listeners.sink_input_port_added);
d94d92ac
PP
151 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
152 graph->listeners.source_filter_ports_connected);
9c0a126a
PP
153 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
154 graph->listeners.filter_filter_ports_connected);
d94d92ac
PP
155 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
156 graph->listeners.source_sink_ports_connected);
157 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
158 graph->listeners.filter_sink_ports_connected);
8cc092c9 159
d6e69534
PP
160 if (graph->messages) {
161 g_ptr_array_free(graph->messages, TRUE);
162 graph->messages = NULL;
5c563278
PP
163 }
164
c0418dd9 165 if (graph->connections) {
262e5473 166 BT_LOGD_STR("Destroying connections.");
c0418dd9 167 g_ptr_array_free(graph->connections, TRUE);
d94d92ac 168 graph->connections = NULL;
c0418dd9 169 }
5c563278 170
bd14d768 171 if (graph->components) {
262e5473 172 BT_LOGD_STR("Destroying components.");
bd14d768 173 g_ptr_array_free(graph->components, TRUE);
d94d92ac 174 graph->components = NULL;
bd14d768 175 }
5c563278 176
f60c8b34
JG
177 if (graph->sinks_to_consume) {
178 g_queue_free(graph->sinks_to_consume);
d94d92ac
PP
179 graph->sinks_to_consume = NULL;
180 }
181
182 if (graph->listeners.source_output_port_added) {
183 g_array_free(graph->listeners.source_output_port_added, TRUE);
184 graph->listeners.source_output_port_added = NULL;
185 }
186
187 if (graph->listeners.filter_output_port_added) {
188 g_array_free(graph->listeners.filter_output_port_added, TRUE);
189 graph->listeners.filter_output_port_added = NULL;
190 }
191
192 if (graph->listeners.filter_input_port_added) {
193 g_array_free(graph->listeners.filter_input_port_added, TRUE);
194 graph->listeners.filter_input_port_added = NULL;
c0418dd9 195 }
1bf957a0 196
d94d92ac
PP
197 if (graph->listeners.sink_input_port_added) {
198 g_array_free(graph->listeners.sink_input_port_added, TRUE);
199 graph->listeners.sink_input_port_added = NULL;
1bf957a0
PP
200 }
201
d94d92ac
PP
202 if (graph->listeners.source_filter_ports_connected) {
203 g_array_free(graph->listeners.source_filter_ports_connected,
204 TRUE);
205 graph->listeners.source_filter_ports_connected = NULL;
206 }
207
9c0a126a
PP
208 if (graph->listeners.filter_filter_ports_connected) {
209 g_array_free(graph->listeners.filter_filter_ports_connected,
210 TRUE);
211 graph->listeners.filter_filter_ports_connected = NULL;
212 }
213
d94d92ac
PP
214 if (graph->listeners.source_sink_ports_connected) {
215 g_array_free(graph->listeners.source_sink_ports_connected,
216 TRUE);
217 graph->listeners.source_sink_ports_connected = NULL;
218 }
219
220 if (graph->listeners.filter_sink_ports_connected) {
221 g_array_free(graph->listeners.filter_sink_ports_connected,
222 TRUE);
223 graph->listeners.filter_sink_ports_connected = NULL;
224 }
225
d6e69534
PP
226 bt_object_pool_finalize(&graph->event_msg_pool);
227 bt_object_pool_finalize(&graph->packet_begin_msg_pool);
228 bt_object_pool_finalize(&graph->packet_end_msg_pool);
c0418dd9
JG
229 g_free(graph);
230}
231
5c563278 232static
d6e69534 233void destroy_message_event(struct bt_message *msg,
5c563278
PP
234 struct bt_graph *graph)
235{
d6e69534 236 bt_message_event_destroy(msg);
5c563278
PP
237}
238
239static
d6e69534 240void destroy_message_packet_begin(struct bt_message *msg,
5c563278
PP
241 struct bt_graph *graph)
242{
5df26c89 243 bt_message_packet_destroy(msg);
5c563278
PP
244}
245
246static
d6e69534 247void destroy_message_packet_end(struct bt_message *msg,
5c563278
PP
248 struct bt_graph *graph)
249{
5df26c89 250 bt_message_packet_destroy(msg);
5c563278
PP
251}
252
253static
d6e69534 254void notify_message_graph_is_destroyed(struct bt_message *msg)
5c563278 255{
d6e69534 256 bt_message_unlink_graph(msg);
5c563278
PP
257}
258
0d72b8c3 259struct bt_graph *bt_graph_create(void)
c0418dd9 260{
f60c8b34 261 struct bt_graph *graph;
1bf957a0 262 int ret;
c0418dd9 263
3f7d4d90 264 BT_LOGI_STR("Creating graph object.");
f60c8b34 265 graph = g_new0(struct bt_graph, 1);
c0418dd9 266 if (!graph) {
262e5473 267 BT_LOGE_STR("Failed to allocate one graph.");
c0418dd9
JG
268 goto end;
269 }
270
d94d92ac 271 bt_object_init_shared(&graph->base, destroy_graph);
3fea54f6
PP
272 graph->connections = g_ptr_array_new_with_free_func(
273 (GDestroyNotify) bt_object_try_spec_release);
c0418dd9 274 if (!graph->connections) {
262e5473 275 BT_LOGE_STR("Failed to allocate one GPtrArray.");
c0418dd9
JG
276 goto error;
277 }
3fea54f6
PP
278 graph->components = g_ptr_array_new_with_free_func(
279 (GDestroyNotify) bt_object_try_spec_release);
f60c8b34 280 if (!graph->components) {
262e5473 281 BT_LOGE_STR("Failed to allocate one GPtrArray.");
f60c8b34
JG
282 goto error;
283 }
284 graph->sinks_to_consume = g_queue_new();
285 if (!graph->sinks_to_consume) {
262e5473 286 BT_LOGE_STR("Failed to allocate one GQueue.");
c0418dd9
JG
287 goto error;
288 }
1bf957a0 289
d94d92ac
PP
290 bt_graph_set_can_consume(graph, true);
291 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
292 graph->listeners.source_output_port_added);
293
294 if (!graph->listeners.source_output_port_added) {
295 ret = -1;
1bf957a0
PP
296 goto error;
297 }
298
d94d92ac
PP
299 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
300 graph->listeners.filter_output_port_added);
301
302 if (!graph->listeners.filter_output_port_added) {
303 ret = -1;
1bf957a0
PP
304 goto error;
305 }
306
d94d92ac
PP
307 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
308 graph->listeners.filter_input_port_added);
309
310 if (!graph->listeners.filter_input_port_added) {
311 ret = -1;
1bf957a0
PP
312 goto error;
313 }
314
d94d92ac
PP
315 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
316 graph->listeners.sink_input_port_added);
317
318 if (!graph->listeners.sink_input_port_added) {
319 ret = -1;
320 goto error;
321 }
322
d94d92ac
PP
323 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
324 graph->listeners.source_filter_ports_connected);
325
326 if (!graph->listeners.source_filter_ports_connected) {
327 ret = -1;
328 goto error;
329 }
330
331 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
332 graph->listeners.source_sink_ports_connected);
333
334 if (!graph->listeners.source_sink_ports_connected) {
335 ret = -1;
336 goto error;
337 }
338
9c0a126a
PP
339 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
340 graph->listeners.filter_filter_ports_connected);
341
342 if (!graph->listeners.filter_filter_ports_connected) {
343 ret = -1;
344 goto error;
345 }
346
d94d92ac
PP
347 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
348 graph->listeners.filter_sink_ports_connected);
349
350 if (!graph->listeners.filter_sink_ports_connected) {
351 ret = -1;
352 goto error;
353 }
354
d6e69534
PP
355 ret = bt_object_pool_initialize(&graph->event_msg_pool,
356 (bt_object_pool_new_object_func) bt_message_event_new,
357 (bt_object_pool_destroy_object_func) destroy_message_event,
5c563278
PP
358 graph);
359 if (ret) {
d6e69534 360 BT_LOGE("Failed to initialize event message pool: ret=%d",
5c563278
PP
361 ret);
362 goto error;
363 }
364
d6e69534
PP
365 ret = bt_object_pool_initialize(&graph->packet_begin_msg_pool,
366 (bt_object_pool_new_object_func) bt_message_packet_beginning_new,
367 (bt_object_pool_destroy_object_func) destroy_message_packet_begin,
5c563278
PP
368 graph);
369 if (ret) {
d6e69534 370 BT_LOGE("Failed to initialize packet beginning message pool: ret=%d",
5c563278
PP
371 ret);
372 goto error;
373 }
374
d6e69534
PP
375 ret = bt_object_pool_initialize(&graph->packet_end_msg_pool,
376 (bt_object_pool_new_object_func) bt_message_packet_end_new,
377 (bt_object_pool_destroy_object_func) destroy_message_packet_end,
5c563278
PP
378 graph);
379 if (ret) {
d6e69534 380 BT_LOGE("Failed to initialize packet end message pool: ret=%d",
5c563278
PP
381 ret);
382 goto error;
383 }
384
d6e69534
PP
385 graph->messages = g_ptr_array_new_with_free_func(
386 (GDestroyNotify) notify_message_graph_is_destroyed);
3f7d4d90 387 BT_LIB_LOGI("Created graph object: %!+g", graph);
262e5473 388
c0418dd9 389end:
a2d06fd5 390 return (void *) graph;
d94d92ac 391
c0418dd9 392error:
65300d60 393 BT_OBJECT_PUT_REF_AND_RESET(graph);
c0418dd9
JG
394 goto end;
395}
396
d24d5663 397enum bt_graph_connect_ports_status bt_graph_connect_ports(
0d72b8c3
PP
398 struct bt_graph *graph,
399 const struct bt_port_output *upstream_port_out,
400 const struct bt_port_input *downstream_port_in,
401 const struct bt_connection **user_connection)
f60c8b34 402{
d24d5663
PP
403 enum bt_graph_connect_ports_status status = BT_FUNC_STATUS_OK;
404 enum bt_graph_listener_func_status listener_status;
f60c8b34 405 struct bt_connection *connection = NULL;
d94d92ac
PP
406 struct bt_port *upstream_port = (void *) upstream_port_out;
407 struct bt_port *downstream_port = (void *) downstream_port_in;
f60c8b34
JG
408 struct bt_component *upstream_component = NULL;
409 struct bt_component *downstream_component = NULL;
d24d5663 410 enum bt_component_class_port_connected_method_status port_connected_status;
d94d92ac 411 bool init_can_consume;
f60c8b34 412
d94d92ac
PP
413 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
414 BT_ASSERT_PRE_NON_NULL(upstream_port, "Upstream port");
415 BT_ASSERT_PRE_NON_NULL(downstream_port, "Downstream port port");
416 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
5badd463
PP
417 BT_ASSERT_PRE(
418 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
38cda5da 419 "Graph is not in the \"configuring\" state: %!+g", graph);
d94d92ac
PP
420 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port),
421 "Upstream port is already connected: %!+p", upstream_port);
422 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port),
423 "Downstream port is already connected: %!+p", downstream_port);
0d72b8c3 424 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port),
d94d92ac
PP
425 "Upstream port does not belong to a component: %!+p",
426 upstream_port);
0d72b8c3 427 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port),
d94d92ac
PP
428 "Downstream port does not belong to a component: %!+p",
429 downstream_port);
4aa7981f 430 init_can_consume = graph->can_consume;
3f7d4d90 431 BT_LIB_LOGI("Connecting component ports within graph: "
d94d92ac
PP
432 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
433 graph, upstream_port, downstream_port);
434 bt_graph_set_can_consume(graph, false);
0d72b8c3 435 upstream_component = bt_port_borrow_component_inline(
d94d92ac 436 (void *) upstream_port);
0d72b8c3 437 downstream_component = bt_port_borrow_component_inline(
d94d92ac 438 (void *) downstream_port);
262e5473 439
262e5473 440 BT_LOGD_STR("Creating connection.");
d94d92ac
PP
441 connection = bt_connection_create(graph, (void *) upstream_port,
442 (void *) downstream_port);
f60c8b34 443 if (!connection) {
262e5473 444 BT_LOGW("Cannot create connection object.");
d24d5663 445 status = BT_FUNC_STATUS_MEMORY_ERROR;
a256a42d 446 goto end;
f60c8b34
JG
447 }
448
d94d92ac 449 BT_LIB_LOGD("Connection object created: %!+x", connection);
262e5473 450
f60c8b34 451 /*
72b913fb
PP
452 * Ownership of upstream_component/downstream_component and of
453 * the connection object is transferred to the graph.
f60c8b34
JG
454 */
455 g_ptr_array_add(graph->connections, connection);
ffeb0eed 456
f60c8b34 457 /*
0d8b4d8e 458 * Notify both components that their port is connected.
f60c8b34 459 */
d94d92ac
PP
460 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
461 "%![comp-]+c, %![port-]+p", upstream_component, upstream_port);
d24d5663 462 port_connected_status = bt_component_port_connected(upstream_component,
d94d92ac 463 (void *) upstream_port, (void *) downstream_port);
d24d5663 464 if (port_connected_status != BT_FUNC_STATUS_OK) {
d94d92ac
PP
465 BT_LIB_LOGW("Error while notifying upstream component that its port is connected: "
466 "status=%s, %![graph-]+g, %![up-comp-]+c, "
467 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
d24d5663 468 bt_common_func_status_string(port_connected_status),
d94d92ac
PP
469 graph, upstream_component, downstream_component,
470 upstream_port, downstream_port);
bf55043c 471 bt_connection_end(connection, true);
d24d5663 472 status = (int) port_connected_status;
bf55043c
PP
473 goto end;
474 }
475
85031ceb 476 connection->notified_upstream_port_connected = true;
d94d92ac
PP
477 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
478 "%![comp-]+c, %![port-]+p", downstream_component,
479 downstream_port);
d24d5663 480 port_connected_status = bt_component_port_connected(downstream_component,
d94d92ac 481 (void *) downstream_port, (void *) upstream_port);
d24d5663 482 if (port_connected_status != BT_FUNC_STATUS_OK) {
d94d92ac
PP
483 BT_LIB_LOGW("Error while notifying downstream component that its port is connected: "
484 "status=%s, %![graph-]+g, %![up-comp-]+c, "
485 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
d24d5663 486 bt_common_func_status_string(port_connected_status),
d94d92ac
PP
487 graph, upstream_component, downstream_component,
488 upstream_port, downstream_port);
bf55043c 489 bt_connection_end(connection, true);
d24d5663 490 status = (int) port_connected_status;
bf55043c
PP
491 goto end;
492 }
493
85031ceb 494 connection->notified_downstream_port_connected = true;
1bf957a0
PP
495
496 /*
0d8b4d8e 497 * Notify the graph's creator that both ports are connected.
1bf957a0 498 */
262e5473 499 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
8cc56726 500 listener_status = bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
d24d5663 501 if (listener_status != BT_FUNC_STATUS_OK) {
8cc56726
SM
502 status = (int) listener_status;
503 goto end;
504 }
505
85031ceb 506 connection->notified_graph_ports_connected = true;
3f7d4d90 507 BT_LIB_LOGI("Connected component ports within graph: "
d94d92ac
PP
508 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
509 "%![up-port-]+p, %![down-port-]+p",
510 graph, upstream_component, downstream_component,
511 upstream_port, downstream_port);
1bf957a0 512
a256a42d 513 if (user_connection) {
1a6a376a
PP
514 /* Move reference to user */
515 *user_connection = connection;
516 connection = NULL;
a256a42d
PP
517 }
518
f60c8b34 519end:
d24d5663 520 if (status != BT_FUNC_STATUS_OK) {
8cc56726 521 bt_graph_make_faulty(graph);
38cda5da
PP
522 }
523
65300d60 524 bt_object_put_ref(connection);
d94d92ac
PP
525 (void) init_can_consume;
526 bt_graph_set_can_consume(graph, init_can_consume);
a256a42d 527 return status;
f60c8b34
JG
528}
529
ad847455 530static inline
d24d5663 531int consume_graph_sink(struct bt_component_sink *comp)
c0418dd9 532{
d24d5663 533 enum bt_component_class_sink_consume_method_status consume_status;
d94d92ac
PP
534 struct bt_component_class_sink *sink_class = NULL;
535
536 BT_ASSERT(comp);
537 sink_class = (void *) comp->parent.class;
538 BT_ASSERT(sink_class->methods.consume);
539 BT_LIB_LOGD("Calling user's consume method: %!+c", comp);
d24d5663 540 consume_status = sink_class->methods.consume((void *) comp);
d94d92ac 541 BT_LOGD("User method returned: status=%s",
d24d5663
PP
542 bt_common_func_status_string(consume_status));
543 BT_ASSERT_POST(consume_status == BT_FUNC_STATUS_OK ||
544 consume_status == BT_FUNC_STATUS_END ||
545 consume_status == BT_FUNC_STATUS_AGAIN ||
546 consume_status == BT_FUNC_STATUS_ERROR ||
547 consume_status == BT_FUNC_STATUS_MEMORY_ERROR,
d94d92ac 548 "Invalid component status returned by consuming method: "
d24d5663
PP
549 "status=%s", bt_common_func_status_string(consume_status));
550 if (consume_status < 0) {
d94d92ac
PP
551 BT_LOGW_STR("Consume method failed.");
552 goto end;
553 }
554
3f7d4d90 555 BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s",
d24d5663 556 comp, bt_common_func_status_string(consume_status));
d94d92ac
PP
557
558end:
d24d5663 559 return consume_status;
8ed535b5
PP
560}
561
562/*
563 * `node` is removed from the queue of sinks to consume when passed to
564 * this function. This function adds it back to the queue if there's
565 * still something to consume afterwards.
566 */
ad847455 567static inline
d24d5663 568int consume_sink_node(struct bt_graph *graph, GList *node)
8ed535b5 569{
d24d5663 570 int status;
d94d92ac 571 struct bt_component_sink *sink;
8ed535b5
PP
572
573 sink = node->data;
574 status = consume_graph_sink(sink);
d24d5663 575 if (G_UNLIKELY(status != BT_FUNC_STATUS_END)) {
8ed535b5 576 g_queue_push_tail_link(graph->sinks_to_consume, node);
f60c8b34
JG
577 goto end;
578 }
579
580 /* End reached, the node is not added back to the queue and free'd. */
8ed535b5 581 g_queue_delete_link(graph->sinks_to_consume, node);
f60c8b34
JG
582
583 /* Don't forward an END status if there are sinks left to consume. */
584 if (!g_queue_is_empty(graph->sinks_to_consume)) {
d24d5663 585 status = BT_FUNC_STATUS_OK;
f60c8b34
JG
586 goto end;
587 }
8ed535b5
PP
588
589end:
3f7d4d90 590 BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s",
d24d5663 591 sink, bt_common_func_status_string(status));
8ed535b5
PP
592 return status;
593}
594
595BT_HIDDEN
d24d5663 596int bt_graph_consume_sink_no_check(struct bt_graph *graph,
d94d92ac 597 struct bt_component_sink *sink)
8ed535b5 598{
d24d5663 599 int status;
8ed535b5
PP
600 GList *sink_node;
601 int index;
602
3f7d4d90 603 BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink);
d94d92ac 604 BT_ASSERT(bt_component_borrow_graph((void *) sink) == graph);
8ed535b5
PP
605
606 if (g_queue_is_empty(graph->sinks_to_consume)) {
3f7d4d90 607 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
d24d5663 608 status = BT_FUNC_STATUS_END;
8ed535b5
PP
609 goto end;
610 }
611
612 index = g_queue_index(graph->sinks_to_consume, sink);
613 if (index < 0) {
3f7d4d90
PP
614 BT_LIB_LOGD("Sink component is not marked as consumable: "
615 "component sink is ended: %![comp-]+c", sink);
d24d5663 616 status = BT_FUNC_STATUS_END;
8ed535b5
PP
617 goto end;
618 }
619
620 sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index);
f6ccaed9 621 BT_ASSERT(sink_node);
8ed535b5
PP
622 status = consume_sink_node(graph, sink_node);
623
624end:
625 return status;
626}
627
ad847455 628static inline
d24d5663 629int consume_no_check(struct bt_graph *graph)
8ed535b5 630{
d24d5663 631 int status = BT_FUNC_STATUS_OK;
8ed535b5
PP
632 struct bt_component *sink;
633 GList *current_node;
634
f6ccaed9
PP
635 BT_ASSERT_PRE(graph->has_sink,
636 "Graph has no sink component: %!+g", graph);
3f7d4d90 637 BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph);
8ed535b5 638
91d81473 639 if (G_UNLIKELY(g_queue_is_empty(graph->sinks_to_consume))) {
3f7d4d90 640 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
d24d5663 641 status = BT_FUNC_STATUS_END;
8ed535b5
PP
642 goto end;
643 }
644
645 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
646 sink = current_node->data;
3f7d4d90 647 BT_LIB_LOGD("Chose next sink to consume: %!+c", sink);
8ed535b5
PP
648 status = consume_sink_node(graph, current_node);
649
f60c8b34
JG
650end:
651 return status;
c0418dd9
JG
652}
653
d24d5663 654enum bt_graph_consume_status bt_graph_consume(struct bt_graph *graph)
851b70bd 655{
d24d5663 656 enum bt_graph_consume_status status;
1d915789 657
f6ccaed9
PP
658 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
659 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
660 BT_ASSERT_PRE(graph->can_consume,
661 "Cannot consume graph in its current state: %!+g", graph);
38cda5da
PP
662 BT_ASSERT_PRE(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY,
663 "Graph is in a faulty state: %!+g", graph);
4725a201 664 bt_graph_set_can_consume(graph, false);
5badd463 665 status = bt_graph_configure(graph);
91d81473 666 if (G_UNLIKELY(status)) {
5badd463
PP
667 /* bt_graph_configure() logs errors */
668 goto end;
669 }
670
d94d92ac 671 status = consume_no_check(graph);
4725a201 672 bt_graph_set_can_consume(graph, true);
5badd463
PP
673
674end:
26a15756 675 return status;
851b70bd
PP
676}
677
d24d5663 678enum bt_graph_run_status bt_graph_run(struct bt_graph *graph)
f60c8b34 679{
d24d5663 680 enum bt_graph_run_status status;
f60c8b34 681
d94d92ac
PP
682 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
683 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
ad847455
PP
684 BT_ASSERT_PRE(graph->can_consume,
685 "Cannot consume graph in its current state: %!+g", graph);
38cda5da
PP
686 BT_ASSERT_PRE(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY,
687 "Graph is in a faulty state: %!+g", graph);
4725a201 688 bt_graph_set_can_consume(graph, false);
5badd463 689 status = bt_graph_configure(graph);
91d81473 690 if (G_UNLIKELY(status)) {
5badd463
PP
691 /* bt_graph_configure() logs errors */
692 goto end;
693 }
694
3f7d4d90 695 BT_LIB_LOGI("Running graph: %!+g", graph);
262e5473 696
f60c8b34 697 do {
851b70bd
PP
698 /*
699 * Check if the graph is canceled at each iteration. If
700 * the graph was canceled by another thread or by a
d94d92ac
PP
701 * signal handler, this is not a warning nor an error,
702 * it was intentional: log with a DEBUG level only.
851b70bd 703 */
91d81473 704 if (G_UNLIKELY(graph->canceled)) {
3f7d4d90 705 BT_LIB_LOGI("Stopping the graph: graph is canceled: "
d94d92ac 706 "%!+g", graph);
d24d5663 707 status = BT_FUNC_STATUS_CANCELED;
851b70bd
PP
708 goto end;
709 }
710
d94d92ac 711 status = consume_no_check(graph);
d24d5663 712 if (G_UNLIKELY(status == BT_FUNC_STATUS_AGAIN)) {
f60c8b34 713 /*
202a3a13
PP
714 * If AGAIN is received and there are multiple
715 * sinks, go ahead and consume from the next
716 * sink.
f60c8b34 717 *
202a3a13
PP
718 * However, in the case where a single sink is
719 * left, the caller can decide to busy-wait and
0d72b8c3 720 * call bt_graph_run() continuously
d94d92ac
PP
721 * until the source is ready or it can decide to
722 * sleep for an arbitrary amount of time.
f60c8b34
JG
723 */
724 if (graph->sinks_to_consume->length > 1) {
d24d5663 725 status = BT_FUNC_STATUS_OK;
f60c8b34
JG
726 }
727 }
d24d5663 728 } while (status == BT_FUNC_STATUS_OK);
f60c8b34
JG
729
730 if (g_queue_is_empty(graph->sinks_to_consume)) {
d24d5663 731 status = BT_FUNC_STATUS_END;
f60c8b34 732 }
262e5473 733
202a3a13 734end:
3f7d4d90 735 BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph,
d24d5663 736 bt_common_func_status_string(status));
4725a201 737 bt_graph_set_can_consume(graph, true);
72b913fb 738 return status;
f60c8b34 739}
1bf957a0 740
d24d5663 741enum bt_graph_add_listener_status
0d72b8c3
PP
742bt_graph_add_source_component_output_port_added_listener(
743 struct bt_graph *graph,
744 bt_graph_source_component_output_port_added_listener_func func,
745 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac 746 int *out_listener_id)
1bf957a0 747{
d94d92ac
PP
748 struct bt_graph_listener_port_added listener = {
749 .base = {
750 .removed = listener_removed,
751 .data = data,
752 },
753 .func = (port_added_func_t) func,
1bf957a0 754 };
d94d92ac 755 int listener_id;
1bf957a0 756
d94d92ac
PP
757 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
758 BT_ASSERT_PRE_NON_NULL(func, "Listener");
759 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
760 BT_ASSERT_PRE(!graph->in_remove_listener,
761 "Graph currently executing a \"listener removed\" listener: "
762 "%!+g", graph);
763 g_array_append_val(graph->listeners.source_output_port_added, listener);
764 listener_id = graph->listeners.source_output_port_added->len - 1;
3f7d4d90 765 BT_LIB_LOGD("Added \"source component output port added\" listener to graph: "
d94d92ac
PP
766 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
767 listener_id);
768
769 if (listener_id) {
770 *out_listener_id = listener_id;
771 }
772
d24d5663 773 return BT_FUNC_STATUS_OK;
1bf957a0
PP
774}
775
d24d5663 776enum bt_graph_add_listener_status
0d72b8c3
PP
777bt_graph_add_filter_component_output_port_added_listener(
778 struct bt_graph *graph,
779 bt_graph_filter_component_output_port_added_listener_func func,
780 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac 781 int *out_listener_id)
1bf957a0 782{
d94d92ac
PP
783 struct bt_graph_listener_port_added listener = {
784 .base = {
785 .removed = listener_removed,
786 .data = data,
787 },
788 .func = (port_added_func_t) func,
789 };
790 int listener_id;
1bf957a0 791
d94d92ac
PP
792 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
793 BT_ASSERT_PRE_NON_NULL(func, "Listener");
794 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
795 BT_ASSERT_PRE(!graph->in_remove_listener,
796 "Graph currently executing a \"listener removed\" listener: "
797 "%!+g", graph);
798 g_array_append_val(graph->listeners.filter_output_port_added, listener);
799 listener_id = graph->listeners.filter_output_port_added->len - 1;
3f7d4d90 800 BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: "
d94d92ac
PP
801 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
802 listener_id);
803
804 if (listener_id) {
805 *out_listener_id = listener_id;
806 }
807
d24d5663 808 return BT_FUNC_STATUS_OK;
d94d92ac 809}
262e5473 810
d24d5663 811enum bt_graph_add_listener_status
0d72b8c3
PP
812bt_graph_add_filter_component_input_port_added_listener(
813 struct bt_graph *graph,
814 bt_graph_filter_component_input_port_added_listener_func func,
815 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
816 int *out_listener_id)
817{
d94d92ac
PP
818 struct bt_graph_listener_port_added listener = {
819 .base = {
820 .removed = listener_removed,
821 .data = data,
822 },
823 .func = (port_added_func_t) func,
824 };
825 int listener_id;
8cc092c9 826
d94d92ac
PP
827 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
828 BT_ASSERT_PRE_NON_NULL(func, "Listener");
829 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
830 BT_ASSERT_PRE(!graph->in_remove_listener,
831 "Graph currently executing a \"listener removed\" listener: "
832 "%!+g", graph);
833 g_array_append_val(graph->listeners.filter_input_port_added, listener);
834 listener_id = graph->listeners.filter_input_port_added->len - 1;
3f7d4d90 835 BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: "
d94d92ac
PP
836 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
837 listener_id);
838
839 if (listener_id) {
840 *out_listener_id = listener_id;
841 }
842
d24d5663 843 return BT_FUNC_STATUS_OK;
d94d92ac 844}
1bf957a0 845
d24d5663 846enum bt_graph_add_listener_status
0d72b8c3
PP
847bt_graph_add_sink_component_input_port_added_listener(
848 struct bt_graph *graph,
849 bt_graph_sink_component_input_port_added_listener_func func,
850 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
851 int *out_listener_id)
852{
d94d92ac
PP
853 struct bt_graph_listener_port_added listener = {
854 .base = {
855 .removed = listener_removed,
856 .data = data,
857 },
858 .func = (port_added_func_t) func,
859 };
860 int listener_id;
1bf957a0 861
d94d92ac
PP
862 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
863 BT_ASSERT_PRE_NON_NULL(func, "Listener");
864 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
865 BT_ASSERT_PRE(!graph->in_remove_listener,
866 "Graph currently executing a \"listener removed\" listener: "
867 "%!+g", graph);
868 g_array_append_val(graph->listeners.sink_input_port_added, listener);
869 listener_id = graph->listeners.sink_input_port_added->len - 1;
3f7d4d90 870 BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: "
d94d92ac
PP
871 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
872 listener_id);
873
874 if (listener_id) {
875 *out_listener_id = listener_id;
876 }
877
d24d5663 878 return BT_FUNC_STATUS_OK;
1bf957a0
PP
879}
880
d24d5663 881enum bt_graph_add_listener_status
0d72b8c3
PP
882bt_graph_add_source_filter_component_ports_connected_listener(
883 struct bt_graph *graph,
884 bt_graph_source_filter_component_ports_connected_listener_func func,
885 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
886 int *out_listener_id)
887{
d94d92ac
PP
888 struct bt_graph_listener_ports_connected listener = {
889 .base = {
890 .removed = listener_removed,
891 .data = data,
892 },
893 .func = (ports_connected_func_t) func,
894 };
895 int listener_id;
8cc092c9 896
d94d92ac
PP
897 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
898 BT_ASSERT_PRE_NON_NULL(func, "Listener");
899 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
900 BT_ASSERT_PRE(!graph->in_remove_listener,
901 "Graph currently executing a \"listener removed\" listener: "
902 "%!+g", graph);
903 g_array_append_val(graph->listeners.source_filter_ports_connected,
904 listener);
905 listener_id = graph->listeners.source_filter_ports_connected->len - 1;
3f7d4d90 906 BT_LIB_LOGD("Added \"source to filter component ports connected\" listener to graph: "
d94d92ac
PP
907 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
908 listener_id);
909
910 if (listener_id) {
911 *out_listener_id = listener_id;
912 }
913
d24d5663 914 return BT_FUNC_STATUS_OK;
d94d92ac 915}
1bf957a0 916
d24d5663 917enum bt_graph_add_listener_status
0d72b8c3
PP
918bt_graph_add_source_sink_component_ports_connected_listener(
919 struct bt_graph *graph,
920 bt_graph_source_sink_component_ports_connected_listener_func func,
921 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac
PP
922 int *out_listener_id)
923{
d94d92ac
PP
924 struct bt_graph_listener_ports_connected listener = {
925 .base = {
926 .removed = listener_removed,
927 .data = data,
928 },
929 .func = (ports_connected_func_t) func,
930 };
931 int listener_id;
1bf957a0 932
d94d92ac
PP
933 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
934 BT_ASSERT_PRE_NON_NULL(func, "Listener");
935 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
936 BT_ASSERT_PRE(!graph->in_remove_listener,
937 "Graph currently executing a \"listener removed\" listener: "
938 "%!+g", graph);
939 g_array_append_val(graph->listeners.source_sink_ports_connected,
940 listener);
941 listener_id = graph->listeners.source_sink_ports_connected->len - 1;
3f7d4d90 942 BT_LIB_LOGD("Added \"source to sink component ports connected\" listener to graph: "
d94d92ac
PP
943 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
944 listener_id);
945
946 if (listener_id) {
947 *out_listener_id = listener_id;
948 }
949
d24d5663 950 return BT_FUNC_STATUS_OK;
1bf957a0
PP
951}
952
d24d5663 953enum bt_graph_add_listener_status
9c0a126a
PP
954bt_graph_add_filter_filter_component_ports_connected_listener(
955 struct bt_graph *graph,
956 bt_graph_filter_filter_component_ports_connected_listener_func func,
957 bt_graph_listener_removed_func listener_removed, void *data,
958 int *out_listener_id)
959{
960 struct bt_graph_listener_ports_connected listener = {
961 .base = {
962 .removed = listener_removed,
963 .data = data,
964 },
965 .func = (ports_connected_func_t) func,
966 };
967 int listener_id;
968
969 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
970 BT_ASSERT_PRE_NON_NULL(func, "Listener");
971 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
972 BT_ASSERT_PRE(!graph->in_remove_listener,
973 "Graph currently executing a \"listener removed\" listener: "
974 "%!+g", graph);
975 g_array_append_val(graph->listeners.filter_filter_ports_connected,
976 listener);
977 listener_id = graph->listeners.filter_filter_ports_connected->len - 1;
3f7d4d90 978 BT_LIB_LOGD("Added \"filter to filter component ports connected\" listener to graph: "
9c0a126a
PP
979 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
980 listener_id);
981
982 if (listener_id) {
983 *out_listener_id = listener_id;
984 }
985
d24d5663 986 return BT_FUNC_STATUS_OK;
9c0a126a
PP
987}
988
d24d5663 989enum bt_graph_add_listener_status
0d72b8c3
PP
990bt_graph_add_filter_sink_component_ports_connected_listener(
991 struct bt_graph *graph,
992 bt_graph_filter_sink_component_ports_connected_listener_func func,
993 bt_graph_listener_removed_func listener_removed, void *data,
d94d92ac 994 int *out_listener_id)
1bf957a0 995{
d94d92ac
PP
996 struct bt_graph_listener_ports_connected listener = {
997 .base = {
998 .removed = listener_removed,
999 .data = data,
1000 },
1001 .func = (ports_connected_func_t) func,
1002 };
1003 int listener_id;
1bf957a0 1004
d94d92ac
PP
1005 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1006 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1007 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1008 BT_ASSERT_PRE(!graph->in_remove_listener,
1009 "Graph currently executing a \"listener removed\" listener: "
1010 "%!+g", graph);
1011 g_array_append_val(graph->listeners.filter_sink_ports_connected,
1012 listener);
1013 listener_id = graph->listeners.filter_sink_ports_connected->len - 1;
3f7d4d90 1014 BT_LIB_LOGD("Added \"filter to sink component ports connected\" listener to graph: "
d94d92ac
PP
1015 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1016 listener_id);
1017
1018 if (listener_id) {
1019 *out_listener_id = listener_id;
1020 }
1021
d24d5663 1022 return BT_FUNC_STATUS_OK;
d94d92ac 1023}
262e5473 1024
1bf957a0 1025BT_HIDDEN
d24d5663 1026enum bt_graph_listener_func_status bt_graph_notify_port_added(
8cc56726 1027 struct bt_graph *graph, struct bt_port *port)
1bf957a0 1028{
d94d92ac
PP
1029 uint64_t i;
1030 GArray *listeners;
1031 struct bt_component *comp;
d24d5663 1032 enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK;
1bf957a0 1033
d94d92ac
PP
1034 BT_ASSERT(graph);
1035 BT_ASSERT(port);
3f7d4d90 1036 BT_LIB_LOGD("Notifying graph listeners that a port was added: "
d94d92ac 1037 "%![graph-]+g, %![port-]+p", graph, port);
0d72b8c3 1038 comp = bt_port_borrow_component_inline(port);
d94d92ac
PP
1039 BT_ASSERT(comp);
1040
1041 switch (comp->class->type) {
1042 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1043 {
1044 switch (port->type) {
1045 case BT_PORT_TYPE_OUTPUT:
1046 listeners = graph->listeners.source_output_port_added;
1047 break;
1048 default:
1049 abort();
1050 }
1051
1052 break;
1053 }
1054 case BT_COMPONENT_CLASS_TYPE_FILTER:
1055 {
1056 switch (port->type) {
1057 case BT_PORT_TYPE_INPUT:
1058 listeners = graph->listeners.filter_input_port_added;
1059 break;
1060 case BT_PORT_TYPE_OUTPUT:
1061 listeners = graph->listeners.filter_output_port_added;
1062 break;
1063 default:
1064 abort();
1065 }
262e5473 1066
d94d92ac
PP
1067 break;
1068 }
1069 case BT_COMPONENT_CLASS_TYPE_SINK:
1070 {
1071 switch (port->type) {
1072 case BT_PORT_TYPE_INPUT:
1073 listeners = graph->listeners.sink_input_port_added;
1074 break;
1075 default:
1076 abort();
1077 }
1bf957a0 1078
d94d92ac
PP
1079 break;
1080 }
1081 default:
1082 abort();
1083 }
1084
1085 for (i = 0; i < listeners->len; i++) {
1086 struct bt_graph_listener_port_added *listener =
1087 &g_array_index(listeners,
1088 struct bt_graph_listener_port_added, i);
1089
8cc56726 1090
d94d92ac 1091 BT_ASSERT(listener->func);
8cc56726 1092 status = listener->func(comp, port, listener->base.data);
d24d5663 1093 if (status != BT_FUNC_STATUS_OK) {
8cc56726
SM
1094 goto end;
1095 }
1bf957a0 1096 }
8cc56726
SM
1097
1098end:
1099 return status;
1bf957a0
PP
1100}
1101
1bf957a0 1102BT_HIDDEN
d24d5663 1103enum bt_graph_listener_func_status bt_graph_notify_ports_connected(
8cc56726
SM
1104 struct bt_graph *graph, struct bt_port *upstream_port,
1105 struct bt_port *downstream_port)
1bf957a0 1106{
d94d92ac
PP
1107 uint64_t i;
1108 GArray *listeners;
1109 struct bt_component *upstream_comp;
1110 struct bt_component *downstream_comp;
d24d5663 1111 enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK;
1bf957a0 1112
d94d92ac
PP
1113 BT_ASSERT(graph);
1114 BT_ASSERT(upstream_port);
1115 BT_ASSERT(downstream_port);
3f7d4d90 1116 BT_LIB_LOGD("Notifying graph listeners that ports were connected: "
d94d92ac
PP
1117 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1118 graph, upstream_port, downstream_port);
0d72b8c3 1119 upstream_comp = bt_port_borrow_component_inline(upstream_port);
d94d92ac 1120 BT_ASSERT(upstream_comp);
0d72b8c3 1121 downstream_comp = bt_port_borrow_component_inline(downstream_port);
d94d92ac
PP
1122 BT_ASSERT(downstream_comp);
1123
1124 switch (upstream_comp->class->type) {
1125 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1126 {
1127 switch (downstream_comp->class->type) {
1128 case BT_COMPONENT_CLASS_TYPE_FILTER:
1129 listeners =
1130 graph->listeners.source_filter_ports_connected;
1131 break;
1132 case BT_COMPONENT_CLASS_TYPE_SINK:
1133 listeners =
1134 graph->listeners.source_sink_ports_connected;
1135 break;
1136 default:
1137 abort();
1138 }
262e5473 1139
d94d92ac
PP
1140 break;
1141 }
1142 case BT_COMPONENT_CLASS_TYPE_FILTER:
1143 {
1144 switch (downstream_comp->class->type) {
9c0a126a
PP
1145 case BT_COMPONENT_CLASS_TYPE_FILTER:
1146 listeners =
1147 graph->listeners.filter_filter_ports_connected;
1148 break;
d94d92ac
PP
1149 case BT_COMPONENT_CLASS_TYPE_SINK:
1150 listeners =
1151 graph->listeners.filter_sink_ports_connected;
1152 break;
1153 default:
1154 abort();
1155 }
1bf957a0 1156
d94d92ac
PP
1157 break;
1158 }
1159 default:
1160 abort();
1161 }
1162
1163 for (i = 0; i < listeners->len; i++) {
1164 struct bt_graph_listener_ports_connected *listener =
1165 &g_array_index(listeners,
1166 struct bt_graph_listener_ports_connected, i);
1167
1168 BT_ASSERT(listener->func);
8cc56726 1169 status = listener->func(upstream_comp, downstream_comp,
d94d92ac 1170 upstream_port, downstream_port, listener->base.data);
d24d5663 1171 if (status != BT_FUNC_STATUS_OK) {
8cc56726
SM
1172 goto end;
1173 }
1bf957a0 1174 }
8cc56726
SM
1175
1176end:
1177 return status;
1bf957a0
PP
1178}
1179
d24d5663 1180enum bt_graph_cancel_status bt_graph_cancel(struct bt_graph *graph)
202a3a13 1181{
d94d92ac
PP
1182 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1183 graph->canceled = true;
3f7d4d90 1184 BT_LIB_LOGI("Canceled graph: %!+i", graph);
d24d5663 1185 return BT_FUNC_STATUS_OK;
202a3a13
PP
1186}
1187
0d72b8c3 1188bt_bool bt_graph_is_canceled(const struct bt_graph *graph)
202a3a13 1189{
d94d92ac
PP
1190 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1191 return graph->canceled ? BT_TRUE : BT_FALSE;
202a3a13 1192}
f167d3c0
PP
1193
1194BT_HIDDEN
1195void bt_graph_remove_connection(struct bt_graph *graph,
1196 struct bt_connection *connection)
1197{
f6ccaed9
PP
1198 BT_ASSERT(graph);
1199 BT_ASSERT(connection);
3f7d4d90 1200 BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x",
262e5473 1201 graph, connection);
f167d3c0
PP
1202 g_ptr_array_remove(graph->connections, connection);
1203}
36712f1d 1204
d94d92ac
PP
1205BT_ASSERT_PRE_FUNC
1206static inline
1207bool component_name_exists(struct bt_graph *graph, const char *name)
1208{
1209 bool exists = false;
1210 uint64_t i;
1211
1212 for (i = 0; i < graph->components->len; i++) {
1213 struct bt_component *other_comp = graph->components->pdata[i];
1214
1215 if (strcmp(name, bt_component_get_name(other_comp)) == 0) {
1216 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1217 "%![other-comp-]+c, name=\"%s\"",
1218 other_comp, name);
1219 exists = true;
1220 goto end;
1221 }
1222 }
1223
1224end:
1225 return exists;
1226}
1227
1228static
d24d5663 1229int add_component_with_init_method_data(
0d72b8c3 1230 struct bt_graph *graph,
d94d92ac
PP
1231 struct bt_component_class *comp_cls,
1232 comp_init_method_t init_method,
05e21286 1233 const char *name, const struct bt_value *params,
e874da19
PP
1234 void *init_method_data, bt_logging_level log_level,
1235 struct bt_component **user_component)
36712f1d 1236{
d24d5663
PP
1237 int status = BT_FUNC_STATUS_OK;
1238 enum bt_component_class_init_method_status init_status;
36712f1d 1239 struct bt_component *component = NULL;
d94d92ac
PP
1240 int ret;
1241 bool init_can_consume;
05e21286 1242 struct bt_value *new_params = NULL;
36712f1d 1243
d94d92ac
PP
1244 BT_ASSERT(comp_cls);
1245 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1246 BT_ASSERT_PRE_NON_NULL(name, "Name");
1247 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
5badd463
PP
1248 BT_ASSERT_PRE(
1249 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
38cda5da 1250 "Graph is not in the \"configuring\" state: %!+g", graph);
d94d92ac
PP
1251 BT_ASSERT_PRE(!component_name_exists(graph, name),
1252 "Duplicate component name: %!+g, name=\"%s\"", graph, name);
1253 BT_ASSERT_PRE(!params || bt_value_is_map(params),
1254 "Parameter value is not a map value: %!+v", params);
4aa7981f 1255 init_can_consume = graph->can_consume;
d94d92ac 1256 bt_graph_set_can_consume(graph, false);
3f7d4d90 1257 BT_LIB_LOGI("Adding component to graph: "
e874da19
PP
1258 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1259 "%![params-]+v, init-method-data-addr=%p",
1260 graph, comp_cls, name,
1261 bt_common_logging_level_string(log_level), params,
1262 init_method_data);
36712f1d 1263
d94d92ac 1264 if (!params) {
05e21286
PP
1265 new_params = bt_value_map_create();
1266 if (!new_params) {
3f7d4d90 1267 BT_LOGE_STR("Cannot create empty map value object.");
d24d5663 1268 status = BT_FUNC_STATUS_MEMORY_ERROR;
36712f1d
PP
1269 goto end;
1270 }
05e21286
PP
1271
1272 params = new_params;
36712f1d
PP
1273 }
1274
e874da19 1275 ret = bt_component_create(comp_cls, name, log_level, &component);
d94d92ac
PP
1276 if (ret) {
1277 BT_LOGE("Cannot create empty component object: ret=%d",
1278 ret);
d24d5663 1279 status = BT_FUNC_STATUS_MEMORY_ERROR;
36712f1d
PP
1280 goto end;
1281 }
1282
1283 /*
1284 * The user's initialization method needs to see that this
1285 * component is part of the graph. If the user method fails, we
1286 * immediately remove the component from the graph's components.
1287 */
1288 g_ptr_array_add(graph->components, component);
1289 bt_component_set_graph(component, graph);
0d47d31b 1290 bt_value_freeze(params);
36712f1d 1291
d94d92ac 1292 if (init_method) {
36712f1d 1293 BT_LOGD_STR("Calling user's initialization method.");
d24d5663 1294 init_status = init_method(component, params, init_method_data);
36712f1d 1295 BT_LOGD("User method returned: status=%s",
d24d5663
PP
1296 bt_common_func_status_string(init_status));
1297 if (init_status != BT_FUNC_STATUS_OK) {
1298 BT_LIB_LOGW("Component initialization method failed: "
1299 "%!+c", component);
1300 status = init_status;
36712f1d
PP
1301 bt_component_set_graph(component, NULL);
1302 g_ptr_array_remove_fast(graph->components, component);
1303 goto end;
1304 }
1305 }
1306
1307 /*
1308 * Mark the component as initialized so that its finalization
1309 * method is called when it is destroyed.
1310 */
1311 component->initialized = true;
1312
1313 /*
1314 * If it's a sink component, it needs to be part of the graph's
1315 * sink queue to be consumed by bt_graph_consume().
1316 */
1317 if (bt_component_is_sink(component)) {
d94d92ac 1318 graph->has_sink = true;
36712f1d
PP
1319 g_queue_push_tail(graph->sinks_to_consume, component);
1320 }
1321
1322 /*
1323 * Freeze the component class now that it's instantiated at
1324 * least once.
1325 */
1326 BT_LOGD_STR("Freezing component class.");
d94d92ac 1327 bt_component_class_freeze(comp_cls);
3f7d4d90 1328 BT_LIB_LOGI("Added component to graph: "
e874da19
PP
1329 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1330 "%![params-]+v, init-method-data-addr=%p, %![comp-]+c",
1331 graph, comp_cls, name,
1332 bt_common_logging_level_string(log_level), params,
1333 init_method_data, component);
36712f1d
PP
1334
1335 if (user_component) {
1336 /* Move reference to user */
1337 *user_component = component;
1338 component = NULL;
1339 }
1340
1341end:
d24d5663 1342 if (status != BT_FUNC_STATUS_OK) {
8cc56726 1343 bt_graph_make_faulty(graph);
38cda5da
PP
1344 }
1345
65300d60 1346 bt_object_put_ref(component);
05e21286 1347 bt_object_put_ref(new_params);
d94d92ac
PP
1348 (void) init_can_consume;
1349 bt_graph_set_can_consume(graph, init_can_consume);
d24d5663 1350 return status;
36712f1d
PP
1351}
1352
d24d5663 1353enum bt_graph_add_component_status
0d72b8c3
PP
1354bt_graph_add_source_component_with_init_method_data(
1355 struct bt_graph *graph,
1356 const struct bt_component_class_source *comp_cls,
05e21286 1357 const char *name, const struct bt_value *params,
e874da19 1358 void *init_method_data, bt_logging_level log_level,
0d72b8c3 1359 const struct bt_component_source **component)
d94d92ac
PP
1360{
1361 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1362 return add_component_with_init_method_data(graph,
1363 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
e874da19 1364 name, params, init_method_data, log_level, (void *) component);
d94d92ac
PP
1365}
1366
d24d5663 1367enum bt_graph_add_component_status bt_graph_add_source_component(
0d72b8c3
PP
1368 struct bt_graph *graph,
1369 const struct bt_component_class_source *comp_cls,
05e21286 1370 const char *name, const struct bt_value *params,
e874da19 1371 bt_logging_level log_level,
0d72b8c3 1372 const struct bt_component_source **component)
d94d92ac 1373{
0d72b8c3 1374 return bt_graph_add_source_component_with_init_method_data(
e874da19 1375 graph, comp_cls, name, params, NULL, log_level, component);
d94d92ac
PP
1376}
1377
d24d5663 1378enum bt_graph_add_component_status
0d72b8c3
PP
1379bt_graph_add_filter_component_with_init_method_data(
1380 struct bt_graph *graph,
1381 const struct bt_component_class_filter *comp_cls,
05e21286 1382 const char *name, const struct bt_value *params,
e874da19 1383 void *init_method_data, bt_logging_level log_level,
0d72b8c3 1384 const struct bt_component_filter **component)
d94d92ac
PP
1385{
1386 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1387 return add_component_with_init_method_data(graph,
1388 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
e874da19 1389 name, params, init_method_data, log_level, (void *) component);
d94d92ac
PP
1390}
1391
d24d5663 1392enum bt_graph_add_component_status bt_graph_add_filter_component(
0d72b8c3
PP
1393 struct bt_graph *graph,
1394 const struct bt_component_class_filter *comp_cls,
05e21286 1395 const char *name, const struct bt_value *params,
e874da19 1396 bt_logging_level log_level,
0d72b8c3 1397 const struct bt_component_filter **component)
d94d92ac 1398{
0d72b8c3 1399 return bt_graph_add_filter_component_with_init_method_data(
e874da19 1400 graph, comp_cls, name, params, NULL, log_level, component);
d94d92ac
PP
1401}
1402
d24d5663 1403enum bt_graph_add_component_status
0d72b8c3
PP
1404bt_graph_add_sink_component_with_init_method_data(
1405 struct bt_graph *graph,
1406 const struct bt_component_class_sink *comp_cls,
05e21286 1407 const char *name, const struct bt_value *params,
e874da19 1408 void *init_method_data, bt_logging_level log_level,
0d72b8c3 1409 const struct bt_component_sink **component)
36712f1d 1410{
d94d92ac
PP
1411 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1412 return add_component_with_init_method_data(graph,
1413 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
e874da19 1414 name, params, init_method_data, log_level, (void *) component);
d94d92ac
PP
1415}
1416
d24d5663 1417enum bt_graph_add_component_status bt_graph_add_sink_component(
0d72b8c3
PP
1418 struct bt_graph *graph,
1419 const struct bt_component_class_sink *comp_cls,
05e21286 1420 const char *name, const struct bt_value *params,
e874da19 1421 bt_logging_level log_level,
0d72b8c3 1422 const struct bt_component_sink **component)
d94d92ac 1423{
0d72b8c3 1424 return bt_graph_add_sink_component_with_init_method_data(
e874da19 1425 graph, comp_cls, name, params, NULL, log_level, component);
36712f1d 1426}
8ed535b5
PP
1427
1428BT_HIDDEN
1429int bt_graph_remove_unconnected_component(struct bt_graph *graph,
1430 struct bt_component *component)
1431{
d94d92ac
PP
1432 bool init_can_consume;
1433 uint64_t count;
8ed535b5
PP
1434 uint64_t i;
1435 int ret = 0;
1436
f6ccaed9
PP
1437 BT_ASSERT(graph);
1438 BT_ASSERT(component);
3fea54f6 1439 BT_ASSERT(component->base.ref_count == 0);
f6ccaed9 1440 BT_ASSERT(bt_component_borrow_graph(component) == graph);
8ed535b5 1441
4aa7981f 1442 init_can_consume = graph->can_consume;
8ed535b5
PP
1443 count = bt_component_get_input_port_count(component);
1444
1445 for (i = 0; i < count; i++) {
d94d92ac
PP
1446 struct bt_port *port = (void *)
1447 bt_component_borrow_input_port_by_index(component, i);
8ed535b5 1448
f6ccaed9 1449 BT_ASSERT(port);
8ed535b5
PP
1450
1451 if (bt_port_is_connected(port)) {
d94d92ac 1452 BT_LIB_LOGW("Cannot remove component from graph: "
8ed535b5 1453 "an input port is connected: "
d94d92ac
PP
1454 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1455 graph, component, port);
8ed535b5
PP
1456 goto error;
1457 }
1458 }
1459
1460 count = bt_component_get_output_port_count(component);
1461
1462 for (i = 0; i < count; i++) {
d94d92ac
PP
1463 struct bt_port *port = (void *)
1464 bt_component_borrow_output_port_by_index(component, i);
8ed535b5 1465
f6ccaed9 1466 BT_ASSERT(port);
8ed535b5
PP
1467
1468 if (bt_port_is_connected(port)) {
d94d92ac 1469 BT_LIB_LOGW("Cannot remove component from graph: "
8ed535b5 1470 "an output port is connected: "
d94d92ac
PP
1471 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1472 graph, component, port);
8ed535b5
PP
1473 goto error;
1474 }
1475 }
1476
d94d92ac 1477 bt_graph_set_can_consume(graph, false);
8ed535b5
PP
1478
1479 /* Possibly remove from sinks to consume */
1480 (void) g_queue_remove(graph->sinks_to_consume, component);
1481
1482 if (graph->sinks_to_consume->length == 0) {
d94d92ac 1483 graph->has_sink = false;
8ed535b5
PP
1484 }
1485
1486 /*
3fea54f6
PP
1487 * This calls bt_object_try_spec_release() on the component, and
1488 * since its reference count is 0, its destructor is called. Its
8ed535b5
PP
1489 * destructor calls the user's finalization method (if set).
1490 */
1491 g_ptr_array_remove(graph->components, component);
1492 goto end;
1493
1494error:
1495 ret = -1;
1496
1497end:
d94d92ac
PP
1498 (void) init_can_consume;
1499 bt_graph_set_can_consume(graph, init_can_consume);
8ed535b5
PP
1500 return ret;
1501}
5c563278
PP
1502
1503BT_HIDDEN
d6e69534
PP
1504void bt_graph_add_message(struct bt_graph *graph,
1505 struct bt_message *msg)
5c563278
PP
1506{
1507 BT_ASSERT(graph);
d6e69534 1508 BT_ASSERT(msg);
5c563278
PP
1509
1510 /*
1511 * It's okay not to take a reference because, when a
d6e69534 1512 * message's reference count drops to 0, either:
5c563278
PP
1513 *
1514 * * It is recycled back to one of this graph's pool.
1515 * * It is destroyed because it doesn't have any link to any
1516 * graph, which means the original graph is already destroyed.
1517 */
d6e69534 1518 g_ptr_array_add(graph->messages, msg);
5c563278 1519}
c5b9b441
PP
1520
1521void bt_graph_get_ref(const struct bt_graph *graph)
1522{
1523 bt_object_get_ref(graph);
1524}
1525
1526void bt_graph_put_ref(const struct bt_graph *graph)
1527{
1528 bt_object_put_ref(graph);
1529}
This page took 0.125965 seconds and 4 git commands to generate.