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