Document libbabeltrace2's C API
[babeltrace.git] / src / lib / graph / graph.c
CommitLineData
c0418dd9 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
f60c8b34 3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
c0418dd9
JG
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "LIB/GRAPH"
c2d9d9cf 25#include "lib/logging.h"
262e5473 26
578e048b
MJ
27#include "common/assert.h"
28#include "lib/assert-pre.h"
f6f301d7 29#include "lib/assert-post.h"
3fadfbc0 30#include <babeltrace2/graph/graph.h>
43c59509
PP
31#include <babeltrace2/graph/component.h>
32#include <babeltrace2/graph/port.h>
578e048b
MJ
33#include "lib/graph/message/message.h"
34#include "compat/compiler.h"
35#include "common/common.h"
3fadfbc0
MJ
36#include <babeltrace2/types.h>
37#include <babeltrace2/value.h>
578e048b 38#include "lib/value.h"
f60c8b34 39#include <unistd.h>
c4f23e30 40#include <stdbool.h>
1bf957a0
PP
41#include <glib.h>
42
078033ed 43#include "component-class-sink-simple.h"
578e048b
MJ
44#include "component.h"
45#include "component-sink.h"
46#include "connection.h"
47#include "graph.h"
9b4f9b42 48#include "interrupter.h"
578e048b
MJ
49#include "message/event.h"
50#include "message/packet.h"
51
d24d5663
PP
52typedef enum bt_graph_listener_func_status
53(*port_added_func_t)(const void *, const void *, void *);
0d72b8c3 54
21a9f056 55typedef enum bt_component_class_initialize_method_status
59225a3e 56(*comp_init_method_t)(const void *, void *, const void *, void *);
d24d5663 57
d94d92ac 58struct bt_graph_listener_port_added {
d94d92ac 59 port_added_func_t func;
f3d6b4c2 60 void *data;
d94d92ac 61};
8cc092c9 62
d94d92ac
PP
63#define INIT_LISTENERS_ARRAY(_type, _listeners) \
64 do { \
65 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
66 if (!(_listeners)) { \
870631a2
PP
67 BT_LIB_LOGE_APPEND_CAUSE( \
68 "Failed to allocate one GArray."); \
d94d92ac
PP
69 } \
70 } while (0)
71
f60c8b34 72static
d94d92ac 73void destroy_graph(struct bt_object *obj)
c0418dd9 74{
0d72b8c3 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.
d0fea130
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.
d6e69534 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.
d0fea130
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 */
3f7d4d90 103 BT_LIB_LOGI("Destroying graph: %!+g", graph);
3fea54f6 104 obj->ref_count++;
9b4f9b42 105 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_DESTROYING;
49682acd 106
d6e69534
PP
107 if (graph->messages) {
108 g_ptr_array_free(graph->messages, TRUE);
109 graph->messages = NULL;
5c563278
PP
110 }
111
c0418dd9 112 if (graph->connections) {
262e5473 113 BT_LOGD_STR("Destroying connections.");
c0418dd9 114 g_ptr_array_free(graph->connections, TRUE);
d94d92ac 115 graph->connections = NULL;
c0418dd9 116 }
5c563278 117
bd14d768 118 if (graph->components) {
262e5473 119 BT_LOGD_STR("Destroying components.");
bd14d768 120 g_ptr_array_free(graph->components, TRUE);
d94d92ac 121 graph->components = NULL;
bd14d768 122 }
5c563278 123
9b4f9b42
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);
d94d92ac
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
d94d92ac
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
d6e69534
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
5c563278 163static
d6e69534 164void destroy_message_event(struct bt_message *msg,
5c563278
PP
165 struct bt_graph *graph)
166{
d6e69534 167 bt_message_event_destroy(msg);
5c563278
PP
168}
169
170static
d6e69534 171void destroy_message_packet_begin(struct bt_message *msg,
5c563278
PP
172 struct bt_graph *graph)
173{
5df26c89 174 bt_message_packet_destroy(msg);
5c563278
PP
175}
176
177static
d6e69534 178void destroy_message_packet_end(struct bt_message *msg,
5c563278
PP
179 struct bt_graph *graph)
180{
5df26c89 181 bt_message_packet_destroy(msg);
5c563278
PP
182}
183
184static
d6e69534 185void notify_message_graph_is_destroyed(struct bt_message *msg)
5c563278 186{
d6e69534 187 bt_message_unlink_graph(msg);
5c563278
PP
188}
189
056deb59 190struct bt_graph *bt_graph_create(uint64_t mip_version)
c0418dd9 191{
f60c8b34 192 struct bt_graph *graph;
1bf957a0 193 int ret;
c0418dd9 194
17f3083a 195 BT_ASSERT_PRE_NO_ERROR();
056deb59
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());
3f7d4d90 200 BT_LOGI_STR("Creating graph object.");
f60c8b34 201 graph = g_new0(struct bt_graph, 1);
c0418dd9 202 if (!graph) {
870631a2 203 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph.");
c0418dd9
JG
204 goto end;
205 }
206
d94d92ac 207 bt_object_init_shared(&graph->base, destroy_graph);
056deb59 208 graph->mip_version = mip_version;
3fea54f6
PP
209 graph->connections = g_ptr_array_new_with_free_func(
210 (GDestroyNotify) bt_object_try_spec_release);
c0418dd9 211 if (!graph->connections) {
870631a2 212 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
c0418dd9
JG
213 goto error;
214 }
3fea54f6
PP
215 graph->components = g_ptr_array_new_with_free_func(
216 (GDestroyNotify) bt_object_try_spec_release);
f60c8b34 217 if (!graph->components) {
870631a2 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) {
870631a2 223 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue.");
c0418dd9
JG
224 goto error;
225 }
1bf957a0 226
d94d92ac
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
d94d92ac
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
d94d92ac
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
d94d92ac
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) {
d94d92ac
PP
253 goto error;
254 }
255
9b4f9b42 256 graph->interrupters = g_ptr_array_new_with_free_func(
6871026b 257 (GDestroyNotify) bt_object_put_ref_no_null_check);
9b4f9b42
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);
d6e69534
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,
5c563278
PP
274 graph);
275 if (ret) {
870631a2
PP
276 BT_LIB_LOGE_APPEND_CAUSE(
277 "Failed to initialize event message pool: ret=%d",
5c563278
PP
278 ret);
279 goto error;
280 }
281
d6e69534
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,
5c563278
PP
285 graph);
286 if (ret) {
870631a2
PP
287 BT_LIB_LOGE_APPEND_CAUSE(
288 "Failed to initialize packet beginning message pool: ret=%d",
5c563278
PP
289 ret);
290 goto error;
291 }
292
d6e69534
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,
5c563278
PP
296 graph);
297 if (ret) {
870631a2
PP
298 BT_LIB_LOGE_APPEND_CAUSE(
299 "Failed to initialize packet end message pool: ret=%d",
5c563278
PP
300 ret);
301 goto error;
302 }
303
d6e69534
PP
304 graph->messages = g_ptr_array_new_with_free_func(
305 (GDestroyNotify) notify_message_graph_is_destroyed);
3f7d4d90 306 BT_LIB_LOGI("Created graph object: %!+g", graph);
262e5473 307
c0418dd9 308end:
a2d06fd5 309 return (void *) graph;
d94d92ac 310
c0418dd9 311error:
65300d60 312 BT_OBJECT_PUT_REF_AND_RESET(graph);
c0418dd9
JG
313 goto end;
314}
315
d24d5663 316enum bt_graph_connect_ports_status bt_graph_connect_ports(
0d72b8c3
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{
d24d5663 322 enum bt_graph_connect_ports_status status = BT_FUNC_STATUS_OK;
f60c8b34 323 struct bt_connection *connection = NULL;
d94d92ac
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;
d24d5663 328 enum bt_component_class_port_connected_method_status port_connected_status;
d94d92ac 329 bool init_can_consume;
f60c8b34 330
17f3083a 331 BT_ASSERT_PRE_NO_ERROR();
d94d92ac
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");
5badd463
PP
335 BT_ASSERT_PRE(
336 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
38cda5da 337 "Graph is not in the \"configuring\" state: %!+g", graph);
d94d92ac
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);
0d72b8c3 342 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port),
d94d92ac
PP
343 "Upstream port does not belong to a component: %!+p",
344 upstream_port);
0d72b8c3 345 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port),
d94d92ac
PP
346 "Downstream port does not belong to a component: %!+p",
347 downstream_port);
4aa7981f 348 init_can_consume = graph->can_consume;
3f7d4d90 349 BT_LIB_LOGI("Connecting component ports within graph: "
d94d92ac
PP
350 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
351 graph, upstream_port, downstream_port);
352 bt_graph_set_can_consume(graph, false);
0d72b8c3 353 upstream_component = bt_port_borrow_component_inline(
d94d92ac 354 (void *) upstream_port);
0d72b8c3 355 downstream_component = bt_port_borrow_component_inline(
d94d92ac 356 (void *) downstream_port);
262e5473 357
262e5473 358 BT_LOGD_STR("Creating connection.");
d94d92ac
PP
359 connection = bt_connection_create(graph, (void *) upstream_port,
360 (void *) downstream_port);
f60c8b34 361 if (!connection) {
870631a2 362 BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object.");
d24d5663 363 status = BT_FUNC_STATUS_MEMORY_ERROR;
a256a42d 364 goto end;
f60c8b34
JG
365 }
366
d94d92ac 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 */
d94d92ac
PP
378 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
379 "%![comp-]+c, %![port-]+p", upstream_component, upstream_port);
d24d5663 380 port_connected_status = bt_component_port_connected(upstream_component,
d94d92ac 381 (void *) upstream_port, (void *) downstream_port);
d24d5663 382 if (port_connected_status != BT_FUNC_STATUS_OK) {
870631a2
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
bf55043c 394 bt_connection_end(connection, true);
d24d5663 395 status = (int) port_connected_status;
bf55043c
PP
396 goto end;
397 }
398
85031ceb 399 connection->notified_upstream_port_connected = true;
d94d92ac
PP
400 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
401 "%![comp-]+c, %![port-]+p", downstream_component,
402 downstream_port);
d24d5663 403 port_connected_status = bt_component_port_connected(downstream_component,
d94d92ac 404 (void *) downstream_port, (void *) upstream_port);
d24d5663 405 if (port_connected_status != BT_FUNC_STATUS_OK) {
870631a2
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
bf55043c 417 bt_connection_end(connection, true);
d24d5663 418 status = (int) port_connected_status;
bf55043c
PP
419 goto end;
420 }
421
85031ceb 422 connection->notified_downstream_port_connected = true;
1bf957a0 423
3f7d4d90 424 BT_LIB_LOGI("Connected component ports within graph: "
d94d92ac
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:
d24d5663 436 if (status != BT_FUNC_STATUS_OK) {
8cc56726 437 bt_graph_make_faulty(graph);
38cda5da
PP
438 }
439
65300d60 440 bt_object_put_ref(connection);
d94d92ac
PP
441 (void) init_can_consume;
442 bt_graph_set_can_consume(graph, init_can_consume);
a256a42d 443 return status;
f60c8b34
JG
444}
445
ad847455 446static inline
d24d5663 447int consume_graph_sink(struct bt_component_sink *comp)
c0418dd9 448{
d24d5663 449 enum bt_component_class_sink_consume_method_status consume_status;
d94d92ac
PP
450 struct bt_component_class_sink *sink_class = NULL;
451
98b15851 452 BT_ASSERT_DBG(comp);
d94d92ac 453 sink_class = (void *) comp->parent.class;
98b15851 454 BT_ASSERT_DBG(sink_class->methods.consume);
d94d92ac 455 BT_LIB_LOGD("Calling user's consume method: %!+c", comp);
d24d5663 456 consume_status = sink_class->methods.consume((void *) comp);
d94d92ac 457 BT_LOGD("User method returned: status=%s",
d24d5663 458 bt_common_func_status_string(consume_status));
bdb288b3 459 BT_ASSERT_POST_DEV(consume_status == BT_FUNC_STATUS_OK ||
d24d5663
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,
d94d92ac 464 "Invalid component status returned by consuming method: "
d24d5663 465 "status=%s", bt_common_func_status_string(consume_status));
6ecdcca3 466 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(consume_status);
870631a2
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
d94d92ac
PP
476 goto end;
477 }
478
3f7d4d90 479 BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s",
d24d5663 480 comp, bt_common_func_status_string(consume_status));
d94d92ac
PP
481
482end:
d24d5663 483 return consume_status;
8ed535b5
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 */
ad847455 491static inline
d24d5663 492int consume_sink_node(struct bt_graph *graph, GList *node)
8ed535b5 493{
d24d5663 494 int status;
d94d92ac 495 struct bt_component_sink *sink;
8ed535b5
PP
496
497 sink = node->data;
498 status = consume_graph_sink(sink);
d24d5663 499 if (G_UNLIKELY(status != BT_FUNC_STATUS_END)) {
8ed535b5 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. */
8ed535b5 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)) {
d24d5663 509 status = BT_FUNC_STATUS_OK;
f60c8b34
JG
510 goto end;
511 }
8ed535b5
PP
512
513end:
3f7d4d90 514 BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s",
d24d5663 515 sink, bt_common_func_status_string(status));
8ed535b5
PP
516 return status;
517}
518
519BT_HIDDEN
d24d5663 520int bt_graph_consume_sink_no_check(struct bt_graph *graph,
d94d92ac 521 struct bt_component_sink *sink)
8ed535b5 522{
d24d5663 523 int status;
8ed535b5
PP
524 GList *sink_node;
525 int index;
526
3f7d4d90 527 BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink);
98b15851 528 BT_ASSERT_DBG(bt_component_borrow_graph((void *) sink) == graph);
8ed535b5
PP
529
530 if (g_queue_is_empty(graph->sinks_to_consume)) {
3f7d4d90 531 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
d24d5663 532 status = BT_FUNC_STATUS_END;
8ed535b5
PP
533 goto end;
534 }
535
536 index = g_queue_index(graph->sinks_to_consume, sink);
537 if (index < 0) {
3f7d4d90
PP
538 BT_LIB_LOGD("Sink component is not marked as consumable: "
539 "component sink is ended: %![comp-]+c", sink);
d24d5663 540 status = BT_FUNC_STATUS_END;
8ed535b5
PP
541 goto end;
542 }
543
544 sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index);
98b15851 545 BT_ASSERT_DBG(sink_node);
8ed535b5
PP
546 status = consume_sink_node(graph, sink_node);
547
548end:
549 return status;
550}
551
ad847455 552static inline
d24d5663 553int consume_no_check(struct bt_graph *graph)
8ed535b5 554{
d24d5663 555 int status = BT_FUNC_STATUS_OK;
8ed535b5
PP
556 struct bt_component *sink;
557 GList *current_node;
558
bdb288b3 559 BT_ASSERT_PRE_DEV(graph->has_sink,
f6ccaed9 560 "Graph has no sink component: %!+g", graph);
3f7d4d90 561 BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph);
8ed535b5 562
91d81473 563 if (G_UNLIKELY(g_queue_is_empty(graph->sinks_to_consume))) {
3f7d4d90 564 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
d24d5663 565 status = BT_FUNC_STATUS_END;
8ed535b5
PP
566 goto end;
567 }
568
569 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
570 sink = current_node->data;
3f7d4d90 571 BT_LIB_LOGD("Chose next sink to consume: %!+c", sink);
8ed535b5
PP
572 status = consume_sink_node(graph, current_node);
573
f60c8b34
JG
574end:
575 return status;
c0418dd9
JG
576}
577
648dab91 578enum bt_graph_run_once_status bt_graph_run_once(struct bt_graph *graph)
851b70bd 579{
648dab91 580 enum bt_graph_run_once_status status;
1d915789 581
17f3083a 582 BT_ASSERT_PRE_NO_ERROR();
bdb288b3 583 BT_ASSERT_PRE_DEV_NON_NULL(graph, "Graph");
bdb288b3 584 BT_ASSERT_PRE_DEV(graph->can_consume,
f6ccaed9 585 "Cannot consume graph in its current state: %!+g", graph);
bdb288b3
PP
586 BT_ASSERT_PRE_DEV(graph->config_state !=
587 BT_GRAPH_CONFIGURATION_STATE_FAULTY,
38cda5da 588 "Graph is in a faulty state: %!+g", graph);
4725a201 589 bt_graph_set_can_consume(graph, false);
5badd463 590 status = bt_graph_configure(graph);
91d81473 591 if (G_UNLIKELY(status)) {
5badd463
PP
592 /* bt_graph_configure() logs errors */
593 goto end;
594 }
595
d94d92ac 596 status = consume_no_check(graph);
4725a201 597 bt_graph_set_can_consume(graph, true);
5badd463
PP
598
599end:
26a15756 600 return status;
851b70bd
PP
601}
602
d24d5663 603enum bt_graph_run_status bt_graph_run(struct bt_graph *graph)
f60c8b34 604{
d24d5663 605 enum bt_graph_run_status status;
f60c8b34 606
17f3083a 607 BT_ASSERT_PRE_NO_ERROR();
d94d92ac 608 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
ad847455
PP
609 BT_ASSERT_PRE(graph->can_consume,
610 "Cannot consume graph in its current state: %!+g", graph);
38cda5da
PP
611 BT_ASSERT_PRE(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY,
612 "Graph is in a faulty state: %!+g", graph);
4725a201 613 bt_graph_set_can_consume(graph, false);
5badd463 614 status = bt_graph_configure(graph);
91d81473 615 if (G_UNLIKELY(status)) {
5badd463
PP
616 /* bt_graph_configure() logs errors */
617 goto end;
618 }
619
3f7d4d90 620 BT_LIB_LOGI("Running graph: %!+g", graph);
262e5473 621
f60c8b34 622 do {
851b70bd 623 /*
9b4f9b42
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 */
9b4f9b42
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
d94d92ac 636 status = consume_no_check(graph);
d24d5663 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
0d72b8c3 645 * call bt_graph_run() continuously
d94d92ac
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) {
d24d5663 650 status = BT_FUNC_STATUS_OK;
f60c8b34
JG
651 }
652 }
d24d5663 653 } while (status == BT_FUNC_STATUS_OK);
f60c8b34 654
9669d693
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:
3f7d4d90 667 BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph,
d24d5663 668 bt_common_func_status_string(status));
4725a201 669 bt_graph_set_can_consume(graph, true);
72b913fb 670 return status;
f60c8b34 671}
1bf957a0 672
d24d5663 673enum bt_graph_add_listener_status
0d72b8c3
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,
f3d6b4c2 677 void *data, bt_listener_id *out_listener_id)
1bf957a0 678{
d94d92ac 679 struct bt_graph_listener_port_added listener = {
d94d92ac 680 .func = (port_added_func_t) func,
f3d6b4c2 681 .data = data,
1bf957a0 682 };
2054a0d1 683 bt_listener_id listener_id;
1bf957a0 684
17f3083a 685 BT_ASSERT_PRE_NO_ERROR();
d94d92ac
PP
686 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
687 BT_ASSERT_PRE_NON_NULL(func, "Listener");
d94d92ac
PP
688 g_array_append_val(graph->listeners.source_output_port_added, listener);
689 listener_id = graph->listeners.source_output_port_added->len - 1;
3f7d4d90 690 BT_LIB_LOGD("Added \"source component output port added\" listener to graph: "
d94d92ac
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
d24d5663 698 return BT_FUNC_STATUS_OK;
1bf957a0
PP
699}
700
d24d5663 701enum bt_graph_add_listener_status
0d72b8c3
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,
f3d6b4c2 705 void *data, bt_listener_id *out_listener_id)
1bf957a0 706{
d94d92ac 707 struct bt_graph_listener_port_added listener = {
d94d92ac 708 .func = (port_added_func_t) func,
f3d6b4c2 709 .data = data,
d94d92ac 710 };
2054a0d1 711 bt_listener_id listener_id;
1bf957a0 712
17f3083a 713 BT_ASSERT_PRE_NO_ERROR();
d94d92ac
PP
714 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
715 BT_ASSERT_PRE_NON_NULL(func, "Listener");
d94d92ac
PP
716 g_array_append_val(graph->listeners.filter_output_port_added, listener);
717 listener_id = graph->listeners.filter_output_port_added->len - 1;
3f7d4d90 718 BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: "
d94d92ac
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
d24d5663 726 return BT_FUNC_STATUS_OK;
d94d92ac 727}
262e5473 728
d24d5663 729enum bt_graph_add_listener_status
0d72b8c3
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,
f3d6b4c2 733 void *data, bt_listener_id *out_listener_id)
d94d92ac 734{
d94d92ac 735 struct bt_graph_listener_port_added listener = {
d94d92ac 736 .func = (port_added_func_t) func,
f3d6b4c2 737 .data = data,
d94d92ac 738 };
2054a0d1 739 bt_listener_id listener_id;
8cc092c9 740
17f3083a 741 BT_ASSERT_PRE_NO_ERROR();
d94d92ac
PP
742 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
743 BT_ASSERT_PRE_NON_NULL(func, "Listener");
d94d92ac
PP
744 g_array_append_val(graph->listeners.filter_input_port_added, listener);
745 listener_id = graph->listeners.filter_input_port_added->len - 1;
3f7d4d90 746 BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: "
d94d92ac
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
d24d5663 754 return BT_FUNC_STATUS_OK;
d94d92ac 755}
1bf957a0 756
d24d5663 757enum bt_graph_add_listener_status
0d72b8c3
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,
f3d6b4c2 761 void *data, bt_listener_id *out_listener_id)
d94d92ac 762{
d94d92ac 763 struct bt_graph_listener_port_added listener = {
d94d92ac 764 .func = (port_added_func_t) func,
f3d6b4c2 765 .data = data,
d94d92ac 766 };
2054a0d1 767 bt_listener_id listener_id;
1bf957a0 768
17f3083a 769 BT_ASSERT_PRE_NO_ERROR();
d94d92ac
PP
770 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
771 BT_ASSERT_PRE_NON_NULL(func, "Listener");
d94d92ac
PP
772 g_array_append_val(graph->listeners.sink_input_port_added, listener);
773 listener_id = graph->listeners.sink_input_port_added->len - 1;
3f7d4d90 774 BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: "
d94d92ac
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
d24d5663 782 return BT_FUNC_STATUS_OK;
1bf957a0
PP
783}
784
1bf957a0 785BT_HIDDEN
d24d5663 786enum bt_graph_listener_func_status bt_graph_notify_port_added(
8cc56726 787 struct bt_graph *graph, struct bt_port *port)
1bf957a0 788{
d94d92ac
PP
789 uint64_t i;
790 GArray *listeners;
791 struct bt_component *comp;
d24d5663 792 enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK;
1bf957a0 793
d94d92ac
PP
794 BT_ASSERT(graph);
795 BT_ASSERT(port);
3f7d4d90 796 BT_LIB_LOGD("Notifying graph listeners that a port was added: "
d94d92ac 797 "%![graph-]+g, %![port-]+p", graph, port);
0d72b8c3 798 comp = bt_port_borrow_component_inline(port);
d94d92ac
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:
498e7994 809 bt_common_abort();
d94d92ac
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:
498e7994 824 bt_common_abort();
d94d92ac 825 }
262e5473 826
d94d92ac
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:
498e7994 836 bt_common_abort();
d94d92ac 837 }
1bf957a0 838
d94d92ac
PP
839 break;
840 }
841 default:
498e7994 842 bt_common_abort();
d94d92ac
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
8cc56726 850
d94d92ac 851 BT_ASSERT(listener->func);
f3d6b4c2 852 status = listener->func(comp, port, listener->data);
6ecdcca3 853 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
d24d5663 854 if (status != BT_FUNC_STATUS_OK) {
8cc56726
SM
855 goto end;
856 }
1bf957a0 857 }
8cc56726
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{
f6ccaed9
PP
867 BT_ASSERT(graph);
868 BT_ASSERT(connection);
3f7d4d90 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
d94d92ac
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
d24d5663 897int add_component_with_init_method_data(
0d72b8c3 898 struct bt_graph *graph,
d94d92ac
PP
899 struct bt_component_class *comp_cls,
900 comp_init_method_t init_method,
05e21286 901 const char *name, const struct bt_value *params,
e874da19 902 void *init_method_data, bt_logging_level log_level,
9628043c 903 const struct bt_component **user_component)
36712f1d 904{
d24d5663 905 int status = BT_FUNC_STATUS_OK;
21a9f056 906 enum bt_component_class_initialize_method_status init_status;
36712f1d 907 struct bt_component *component = NULL;
d94d92ac
PP
908 int ret;
909 bool init_can_consume;
05e21286 910 struct bt_value *new_params = NULL;
36712f1d 911
d94d92ac
PP
912 BT_ASSERT(comp_cls);
913 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
914 BT_ASSERT_PRE_NON_NULL(name, "Name");
5badd463
PP
915 BT_ASSERT_PRE(
916 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
38cda5da 917 "Graph is not in the \"configuring\" state: %!+g", graph);
d94d92ac
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);
4aa7981f 922 init_can_consume = graph->can_consume;
d94d92ac 923 bt_graph_set_can_consume(graph, false);
3f7d4d90 924 BT_LIB_LOGI("Adding component to graph: "
e874da19
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
d94d92ac 931 if (!params) {
05e21286
PP
932 new_params = bt_value_map_create();
933 if (!new_params) {
870631a2
PP
934 BT_LIB_LOGE_APPEND_CAUSE(
935 "Cannot create empty map value object.");
d24d5663 936 status = BT_FUNC_STATUS_MEMORY_ERROR;
36712f1d
PP
937 goto end;
938 }
05e21286
PP
939
940 params = new_params;
36712f1d
PP
941 }
942
e874da19 943 ret = bt_component_create(comp_cls, name, log_level, &component);
d94d92ac 944 if (ret) {
870631a2
PP
945 BT_LIB_LOGE_APPEND_CAUSE(
946 "Cannot create empty component object: ret=%d",
d94d92ac 947 ret);
d24d5663 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);
0d47d31b 959 bt_value_freeze(params);
36712f1d 960
d94d92ac 961 if (init_method) {
59225a3e
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.");
59225a3e 967 init_status = init_method(component, NULL, params, init_method_data);
36712f1d 968 BT_LOGD("User method returned: status=%s",
d24d5663 969 bt_common_func_status_string(init_status));
6ecdcca3 970 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(init_status);
d24d5663 971 if (init_status != BT_FUNC_STATUS_OK) {
870631a2
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
d24d5663 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
648dab91
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)) {
d94d92ac 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.");
d94d92ac 1008 bt_component_class_freeze(comp_cls);
3f7d4d90 1009 BT_LIB_LOGI("Added component to graph: "
e874da19
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:
d24d5663 1022 if (status != BT_FUNC_STATUS_OK) {
8cc56726 1023 bt_graph_make_faulty(graph);
38cda5da
PP
1024 }
1025
65300d60 1026 bt_object_put_ref(component);
05e21286 1027 bt_object_put_ref(new_params);
d94d92ac
PP
1028 (void) init_can_consume;
1029 bt_graph_set_can_consume(graph, init_can_consume);
d24d5663 1030 return status;
36712f1d
PP
1031}
1032
d24d5663 1033enum bt_graph_add_component_status
21a9f056 1034bt_graph_add_source_component_with_initialize_method_data(
0d72b8c3
PP
1035 struct bt_graph *graph,
1036 const struct bt_component_class_source *comp_cls,
05e21286 1037 const char *name, const struct bt_value *params,
e874da19 1038 void *init_method_data, bt_logging_level log_level,
0d72b8c3 1039 const struct bt_component_source **component)
d94d92ac 1040{
17f3083a 1041 BT_ASSERT_PRE_NO_ERROR();
d94d92ac
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,
e874da19 1045 name, params, init_method_data, log_level, (void *) component);
d94d92ac
PP
1046}
1047
d24d5663 1048enum bt_graph_add_component_status bt_graph_add_source_component(
0d72b8c3
PP
1049 struct bt_graph *graph,
1050 const struct bt_component_class_source *comp_cls,
05e21286 1051 const char *name, const struct bt_value *params,
9628043c 1052 enum bt_logging_level log_level,
0d72b8c3 1053 const struct bt_component_source **component)
d94d92ac 1054{
17f3083a 1055 BT_ASSERT_PRE_NO_ERROR();
21a9f056 1056 return bt_graph_add_source_component_with_initialize_method_data(
e874da19 1057 graph, comp_cls, name, params, NULL, log_level, component);
d94d92ac
PP
1058}
1059
d24d5663 1060enum bt_graph_add_component_status
21a9f056 1061bt_graph_add_filter_component_with_initialize_method_data(
0d72b8c3
PP
1062 struct bt_graph *graph,
1063 const struct bt_component_class_filter *comp_cls,
05e21286 1064 const char *name, const struct bt_value *params,
9628043c 1065 void *init_method_data, enum bt_logging_level log_level,
0d72b8c3 1066 const struct bt_component_filter **component)
d94d92ac 1067{
17f3083a 1068 BT_ASSERT_PRE_NO_ERROR();
d94d92ac
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,
e874da19 1072 name, params, init_method_data, log_level, (void *) component);
d94d92ac
PP
1073}
1074
d24d5663 1075enum bt_graph_add_component_status bt_graph_add_filter_component(
0d72b8c3
PP
1076 struct bt_graph *graph,
1077 const struct bt_component_class_filter *comp_cls,
05e21286 1078 const char *name, const struct bt_value *params,
9628043c 1079 enum bt_logging_level log_level,
0d72b8c3 1080 const struct bt_component_filter **component)
d94d92ac 1081{
17f3083a 1082 BT_ASSERT_PRE_NO_ERROR();
21a9f056 1083 return bt_graph_add_filter_component_with_initialize_method_data(
e874da19 1084 graph, comp_cls, name, params, NULL, log_level, component);
d94d92ac
PP
1085}
1086
d24d5663 1087enum bt_graph_add_component_status
21a9f056 1088bt_graph_add_sink_component_with_initialize_method_data(
0d72b8c3
PP
1089 struct bt_graph *graph,
1090 const struct bt_component_class_sink *comp_cls,
05e21286 1091 const char *name, const struct bt_value *params,
9628043c 1092 void *init_method_data, enum bt_logging_level log_level,
0d72b8c3 1093 const struct bt_component_sink **component)
36712f1d 1094{
17f3083a 1095 BT_ASSERT_PRE_NO_ERROR();
d94d92ac
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,
e874da19 1099 name, params, init_method_data, log_level, (void *) component);
d94d92ac
PP
1100}
1101
d24d5663 1102enum bt_graph_add_component_status bt_graph_add_sink_component(
0d72b8c3
PP
1103 struct bt_graph *graph,
1104 const struct bt_component_class_sink *comp_cls,
05e21286 1105 const char *name, const struct bt_value *params,
9628043c 1106 enum bt_logging_level log_level,
0d72b8c3 1107 const struct bt_component_sink **component)
d94d92ac 1108{
17f3083a 1109 BT_ASSERT_PRE_NO_ERROR();
21a9f056 1110 return bt_graph_add_sink_component_with_initialize_method_data(
e874da19 1111 graph, comp_cls, name, params, NULL, log_level, component);
36712f1d 1112}
8ed535b5 1113
078033ed
PP
1114enum bt_graph_add_component_status
1115bt_graph_add_simple_sink_component(struct bt_graph *graph, const char *name,
21a9f056 1116 bt_graph_simple_sink_component_initialize_func init_func,
078033ed
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
17f3083a
SM
1130 BT_ASSERT_PRE_NO_ERROR();
1131
078033ed
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
21a9f056 1146 status = bt_graph_add_sink_component_with_initialize_method_data(graph,
078033ed
PP
1147 comp_cls, name, NULL, &init_method_data,
1148 BT_LOGGING_LEVEL_NONE, component);
1149
1150end:
1151 return status;
1152}
1153
5c563278 1154BT_HIDDEN
d6e69534
PP
1155void bt_graph_add_message(struct bt_graph *graph,
1156 struct bt_message *msg)
5c563278
PP
1157{
1158 BT_ASSERT(graph);
d6e69534 1159 BT_ASSERT(msg);
5c563278
PP
1160
1161 /*
1162 * It's okay not to take a reference because, when a
d6e69534 1163 * message's reference count drops to 0, either:
5c563278
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 */
d6e69534 1169 g_ptr_array_add(graph->messages, msg);
5c563278 1170}
c5b9b441 1171
9b4f9b42
PP
1172BT_HIDDEN
1173bool bt_graph_is_interrupted(const struct bt_graph *graph)
1174{
98b15851 1175 BT_ASSERT_DBG(graph);
9b4f9b42
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{
17f3083a 1182 BT_ASSERT_PRE_NO_ERROR();
9b4f9b42
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);
6871026b 1186 bt_object_get_ref_no_null_check(intr);
9b4f9b42
PP
1187 BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z",
1188 graph, intr);
1189 return BT_FUNC_STATUS_OK;
1190}
1191
c513d240 1192struct bt_interrupter *bt_graph_borrow_default_interrupter(bt_graph *graph)
9b4f9b42
PP
1193{
1194 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
c513d240 1195 return graph->default_interrupter;
9b4f9b42
PP
1196}
1197
c5b9b441
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.134392 seconds and 4 git commands to generate.