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