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