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