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