Commit | Line | Data |
---|---|---|
c0418dd9 | 1 | /* |
f2b0325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
f60c8b34 | 3 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
c0418dd9 JG |
4 | * |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
262e5473 PP |
24 | #define BT_LOG_TAG "GRAPH" |
25 | #include <babeltrace/lib-logging-internal.h> | |
26 | ||
8c6884d9 PP |
27 | #include <babeltrace/assert-internal.h> |
28 | #include <babeltrace/assert-pre-internal.h> | |
b2e0c907 | 29 | #include <babeltrace/graph/component-internal.h> |
7b53201c 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> | |
7b53201c PP |
35 | #include <babeltrace/graph/component-source-const.h> |
36 | #include <babeltrace/graph/component-filter-const.h> | |
37 | #include <babeltrace/graph/port-const.h> | |
b09a5592 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> |
17582c6d | 42 | #include <babeltrace/common-internal.h> |
c55a9f58 | 43 | #include <babeltrace/types.h> |
0f15f666 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 | ||
7b53201c PP |
50 | typedef void (*port_added_func_t)(const void *, const void *, void *); |
51 | ||
7b53201c PP |
52 | typedef void (*ports_connected_func_t)(const void *, const void *, const void *, |
53 | const void *, void *); | |
54 | ||
7b53201c PP |
55 | typedef enum bt_self_component_status (*comp_init_method_t)(const void *, |
56 | const void *, void *); | |
834e9996 | 57 | |
1bf957a0 | 58 | struct bt_graph_listener { |
7b53201c | 59 | bt_graph_listener_removed_func removed; |
1bf957a0 PP |
60 | void *data; |
61 | }; | |
c0418dd9 | 62 | |
834e9996 PP |
63 | struct bt_graph_listener_port_added { |
64 | struct bt_graph_listener base; | |
65 | port_added_func_t func; | |
66 | }; | |
eb87cce7 | 67 | |
834e9996 PP |
68 | struct bt_graph_listener_ports_connected { |
69 | struct bt_graph_listener base; | |
70 | ports_connected_func_t func; | |
71 | }; | |
eb87cce7 | 72 | |
834e9996 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) | |
eb87cce7 | 94 | |
f60c8b34 | 95 | static |
834e9996 | 96 | void destroy_graph(struct bt_object *obj) |
c0418dd9 | 97 | { |
7b53201c | 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. | |
904860cb 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. | |
b09a5592 | 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. | |
904860cb 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 | */ | |
834e9996 | 126 | BT_LIB_LOGD("Destroying graph: %!+g", graph); |
1d7bf349 | 127 | obj->ref_count++; |
bd14d768 | 128 | |
49682acd PP |
129 | /* |
130 | * Cancel the graph to disallow some operations, like creating | |
b09a5592 | 131 | * message iterators and adding ports to components. |
49682acd | 132 | */ |
7b53201c | 133 | (void) bt_graph_cancel((void *) graph); |
49682acd | 134 | |
eb87cce7 | 135 | /* Call all remove listeners */ |
834e9996 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); | |
834e9996 PP |
144 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, |
145 | graph->listeners.source_filter_ports_connected); | |
ee976410 PP |
146 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, |
147 | graph->listeners.filter_filter_ports_connected); | |
834e9996 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); | |
eb87cce7 | 152 | |
b09a5592 PP |
153 | if (graph->messages) { |
154 | g_ptr_array_free(graph->messages, TRUE); | |
155 | graph->messages = NULL; | |
f7c3ac09 PP |
156 | } |
157 | ||
c0418dd9 | 158 | if (graph->connections) { |
262e5473 | 159 | BT_LOGD_STR("Destroying connections."); |
c0418dd9 | 160 | g_ptr_array_free(graph->connections, TRUE); |
834e9996 | 161 | graph->connections = NULL; |
c0418dd9 | 162 | } |
f7c3ac09 | 163 | |
bd14d768 | 164 | if (graph->components) { |
262e5473 | 165 | BT_LOGD_STR("Destroying components."); |
bd14d768 | 166 | g_ptr_array_free(graph->components, TRUE); |
834e9996 | 167 | graph->components = NULL; |
bd14d768 | 168 | } |
f7c3ac09 | 169 | |
f60c8b34 JG |
170 | if (graph->sinks_to_consume) { |
171 | g_queue_free(graph->sinks_to_consume); | |
834e9996 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 | |
834e9996 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 | ||
834e9996 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 | ||
ee976410 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 | ||
834e9996 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 | ||
b09a5592 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 | ||
f7c3ac09 | 225 | static |
b09a5592 | 226 | void destroy_message_event(struct bt_message *msg, |
f7c3ac09 PP |
227 | struct bt_graph *graph) |
228 | { | |
b09a5592 | 229 | bt_message_event_destroy(msg); |
f7c3ac09 PP |
230 | } |
231 | ||
232 | static | |
b09a5592 | 233 | void destroy_message_packet_begin(struct bt_message *msg, |
f7c3ac09 PP |
234 | struct bt_graph *graph) |
235 | { | |
a1f053a9 | 236 | bt_message_packet_destroy(msg); |
f7c3ac09 PP |
237 | } |
238 | ||
239 | static | |
b09a5592 | 240 | void destroy_message_packet_end(struct bt_message *msg, |
f7c3ac09 PP |
241 | struct bt_graph *graph) |
242 | { | |
a1f053a9 | 243 | bt_message_packet_destroy(msg); |
f7c3ac09 PP |
244 | } |
245 | ||
246 | static | |
b09a5592 | 247 | void notify_message_graph_is_destroyed(struct bt_message *msg) |
f7c3ac09 | 248 | { |
b09a5592 | 249 | bt_message_unlink_graph(msg); |
f7c3ac09 PP |
250 | } |
251 | ||
7b53201c | 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 | ||
834e9996 | 264 | bt_object_init_shared(&graph->base, destroy_graph); |
1d7bf349 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 | } | |
1d7bf349 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 | |
834e9996 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 | ||
834e9996 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 | ||
834e9996 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 | ||
834e9996 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 | ||
834e9996 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 | ||
ee976410 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 | ||
834e9996 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 | ||
b09a5592 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, | |
f7c3ac09 PP |
351 | graph); |
352 | if (ret) { | |
b09a5592 | 353 | BT_LOGE("Failed to initialize event message pool: ret=%d", |
f7c3ac09 PP |
354 | ret); |
355 | goto error; | |
356 | } | |
357 | ||
b09a5592 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, | |
f7c3ac09 PP |
361 | graph); |
362 | if (ret) { | |
b09a5592 | 363 | BT_LOGE("Failed to initialize packet beginning message pool: ret=%d", |
f7c3ac09 PP |
364 | ret); |
365 | goto error; | |
366 | } | |
367 | ||
b09a5592 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, | |
f7c3ac09 PP |
371 | graph); |
372 | if (ret) { | |
b09a5592 | 373 | BT_LOGE("Failed to initialize packet end message pool: ret=%d", |
f7c3ac09 PP |
374 | ret); |
375 | goto error; | |
376 | } | |
377 | ||
b09a5592 PP |
378 | graph->messages = g_ptr_array_new_with_free_func( |
379 | (GDestroyNotify) notify_message_graph_is_destroyed); | |
834e9996 | 380 | BT_LIB_LOGD("Created graph object: %!+g", graph); |
262e5473 | 381 | |
c0418dd9 | 382 | end: |
8d750b42 | 383 | return (void *) graph; |
834e9996 | 384 | |
c0418dd9 | 385 | error: |
8138bfe1 | 386 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
c0418dd9 JG |
387 | goto end; |
388 | } | |
389 | ||
7b53201c 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; |
834e9996 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; | |
834e9996 PP |
402 | enum bt_self_component_status component_status; |
403 | bool init_can_consume; | |
f60c8b34 | 404 | |
834e9996 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); | |
1043fdea PP |
409 | BT_ASSERT_PRE( |
410 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
9cce94e4 | 411 | "Graph is already configured: %!+g", graph); |
834e9996 PP |
412 | BT_ASSERT_PRE(!bt_port_is_connected(upstream_port), |
413 | "Upstream port is already connected: %!+p", upstream_port); | |
414 | BT_ASSERT_PRE(!bt_port_is_connected(downstream_port), | |
415 | "Downstream port is already connected: %!+p", downstream_port); | |
7b53201c | 416 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port), |
834e9996 PP |
417 | "Upstream port does not belong to a component: %!+p", |
418 | upstream_port); | |
7b53201c | 419 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port), |
834e9996 PP |
420 | "Downstream port does not belong to a component: %!+p", |
421 | downstream_port); | |
3afb93ee | 422 | init_can_consume = graph->can_consume; |
834e9996 PP |
423 | BT_LIB_LOGD("Connecting component ports within graph: " |
424 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", | |
425 | graph, upstream_port, downstream_port); | |
426 | bt_graph_set_can_consume(graph, false); | |
7b53201c | 427 | upstream_component = bt_port_borrow_component_inline( |
834e9996 | 428 | (void *) upstream_port); |
7b53201c | 429 | downstream_component = bt_port_borrow_component_inline( |
834e9996 | 430 | (void *) downstream_port); |
262e5473 | 431 | |
0d8b4d8e PP |
432 | /* |
433 | * At this point the ports are not connected yet. Both | |
434 | * components need to accept an eventual connection to their | |
435 | * port by the other port before we continue. | |
436 | */ | |
834e9996 PP |
437 | BT_LIB_LOGD("Asking upstream component to accept the connection: " |
438 | "%![comp-]+c", upstream_component); | |
0d8b4d8e | 439 | component_status = bt_component_accept_port_connection( |
834e9996 PP |
440 | upstream_component, (void *) upstream_port, |
441 | (void *) downstream_port); | |
442 | if (component_status != BT_SELF_COMPONENT_STATUS_OK) { | |
443 | if (component_status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { | |
262e5473 PP |
444 | BT_LOGD_STR("Upstream component refused the connection."); |
445 | } else { | |
446 | BT_LOGW("Cannot ask upstream component to accept the connection: " | |
834e9996 | 447 | "status=%s", bt_self_component_status_string(component_status)); |
262e5473 PP |
448 | } |
449 | ||
834e9996 | 450 | status = (int) component_status; |
a256a42d | 451 | goto end; |
0d8b4d8e | 452 | } |
262e5473 | 453 | |
834e9996 PP |
454 | BT_LIB_LOGD("Asking downstream component to accept the connection: " |
455 | "%![comp-]+c", downstream_component); | |
0d8b4d8e | 456 | component_status = bt_component_accept_port_connection( |
834e9996 PP |
457 | downstream_component, (void *) downstream_port, |
458 | (void *) upstream_port); | |
459 | if (component_status != BT_SELF_COMPONENT_STATUS_OK) { | |
460 | if (component_status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { | |
262e5473 PP |
461 | BT_LOGD_STR("Downstream component refused the connection."); |
462 | } else { | |
463 | BT_LOGW("Cannot ask downstream component to accept the connection: " | |
834e9996 | 464 | "status=%s", bt_self_component_status_string(component_status)); |
262e5473 PP |
465 | } |
466 | ||
834e9996 | 467 | status = (int) component_status; |
a256a42d | 468 | goto end; |
0d8b4d8e PP |
469 | } |
470 | ||
262e5473 | 471 | BT_LOGD_STR("Creating connection."); |
834e9996 PP |
472 | connection = bt_connection_create(graph, (void *) upstream_port, |
473 | (void *) downstream_port); | |
f60c8b34 | 474 | if (!connection) { |
262e5473 | 475 | BT_LOGW("Cannot create connection object."); |
a256a42d PP |
476 | status = BT_GRAPH_STATUS_NOMEM; |
477 | goto end; | |
f60c8b34 JG |
478 | } |
479 | ||
834e9996 | 480 | BT_LIB_LOGD("Connection object created: %!+x", connection); |
262e5473 | 481 | |
f60c8b34 | 482 | /* |
72b913fb PP |
483 | * Ownership of upstream_component/downstream_component and of |
484 | * the connection object is transferred to the graph. | |
f60c8b34 JG |
485 | */ |
486 | g_ptr_array_add(graph->connections, connection); | |
ffeb0eed | 487 | |
f60c8b34 | 488 | /* |
0d8b4d8e | 489 | * Notify both components that their port is connected. |
f60c8b34 | 490 | */ |
834e9996 PP |
491 | BT_LIB_LOGD("Notifying upstream component that its port is connected: " |
492 | "%![comp-]+c, %![port-]+p", upstream_component, upstream_port); | |
634f394c | 493 | component_status = bt_component_port_connected(upstream_component, |
834e9996 PP |
494 | (void *) upstream_port, (void *) downstream_port); |
495 | if (component_status != BT_SELF_COMPONENT_STATUS_OK) { | |
496 | BT_LIB_LOGW("Error while notifying upstream component that its port is connected: " | |
497 | "status=%s, %![graph-]+g, %![up-comp-]+c, " | |
498 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", | |
499 | bt_self_component_status_string(component_status), | |
500 | graph, upstream_component, downstream_component, | |
501 | upstream_port, downstream_port); | |
634f394c | 502 | bt_connection_end(connection, true); |
834e9996 | 503 | status = (int) component_status; |
634f394c PP |
504 | goto end; |
505 | } | |
506 | ||
0f15970b | 507 | connection->notified_upstream_port_connected = true; |
834e9996 PP |
508 | BT_LIB_LOGD("Notifying downstream component that its port is connected: " |
509 | "%![comp-]+c, %![port-]+p", downstream_component, | |
510 | downstream_port); | |
634f394c | 511 | component_status = bt_component_port_connected(downstream_component, |
834e9996 PP |
512 | (void *) downstream_port, (void *) upstream_port); |
513 | if (component_status != BT_SELF_COMPONENT_STATUS_OK) { | |
514 | BT_LIB_LOGW("Error while notifying downstream component that its port is connected: " | |
515 | "status=%s, %![graph-]+g, %![up-comp-]+c, " | |
516 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", | |
517 | bt_self_component_status_string(component_status), | |
518 | graph, upstream_component, downstream_component, | |
519 | upstream_port, downstream_port); | |
634f394c | 520 | bt_connection_end(connection, true); |
834e9996 | 521 | status = (int) component_status; |
634f394c PP |
522 | goto end; |
523 | } | |
524 | ||
0f15970b | 525 | connection->notified_downstream_port_connected = true; |
1bf957a0 PP |
526 | |
527 | /* | |
0d8b4d8e | 528 | * Notify the graph's creator that both ports are connected. |
1bf957a0 | 529 | */ |
262e5473 | 530 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
f345f8bb | 531 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
0f15970b | 532 | connection->notified_graph_ports_connected = true; |
834e9996 PP |
533 | BT_LIB_LOGD("Connected component ports within graph: " |
534 | "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, " | |
535 | "%![up-port-]+p, %![down-port-]+p", | |
536 | graph, upstream_component, downstream_component, | |
537 | upstream_port, downstream_port); | |
1bf957a0 | 538 | |
a256a42d | 539 | if (user_connection) { |
1a6a376a PP |
540 | /* Move reference to user */ |
541 | *user_connection = connection; | |
542 | connection = NULL; | |
a256a42d PP |
543 | } |
544 | ||
f60c8b34 | 545 | end: |
8138bfe1 | 546 | bt_object_put_ref(connection); |
834e9996 PP |
547 | (void) init_can_consume; |
548 | bt_graph_set_can_consume(graph, init_can_consume); | |
a256a42d | 549 | return status; |
f60c8b34 JG |
550 | } |
551 | ||
371361a1 | 552 | static inline |
834e9996 | 553 | enum bt_graph_status consume_graph_sink(struct bt_component_sink *comp) |
c0418dd9 | 554 | { |
834e9996 PP |
555 | enum bt_self_component_status comp_status; |
556 | struct bt_component_class_sink *sink_class = NULL; | |
557 | ||
558 | BT_ASSERT(comp); | |
559 | sink_class = (void *) comp->parent.class; | |
560 | BT_ASSERT(sink_class->methods.consume); | |
561 | BT_LIB_LOGD("Calling user's consume method: %!+c", comp); | |
562 | comp_status = sink_class->methods.consume((void *) comp); | |
563 | BT_LOGD("User method returned: status=%s", | |
564 | bt_self_component_status_string(comp_status)); | |
565 | BT_ASSERT_PRE(comp_status == BT_SELF_COMPONENT_STATUS_OK || | |
566 | comp_status == BT_SELF_COMPONENT_STATUS_END || | |
567 | comp_status == BT_SELF_COMPONENT_STATUS_AGAIN || | |
568 | comp_status == BT_SELF_COMPONENT_STATUS_ERROR || | |
569 | comp_status == BT_SELF_COMPONENT_STATUS_NOMEM, | |
570 | "Invalid component status returned by consuming method: " | |
571 | "status=%s", bt_self_component_status_string(comp_status)); | |
572 | if (comp_status < 0) { | |
573 | BT_LOGW_STR("Consume method failed."); | |
574 | goto end; | |
575 | } | |
576 | ||
577 | BT_LIB_LOGV("Consumed from sink: %![comp-]+c, status=%s", | |
578 | comp, bt_self_component_status_string(comp_status)); | |
579 | ||
580 | end: | |
581 | return (int) comp_status; | |
c3ac0193 PP |
582 | } |
583 | ||
584 | /* | |
585 | * `node` is removed from the queue of sinks to consume when passed to | |
586 | * this function. This function adds it back to the queue if there's | |
587 | * still something to consume afterwards. | |
588 | */ | |
371361a1 | 589 | static inline |
834e9996 | 590 | enum bt_graph_status consume_sink_node(struct bt_graph *graph, GList *node) |
c3ac0193 PP |
591 | { |
592 | enum bt_graph_status status; | |
834e9996 | 593 | struct bt_component_sink *sink; |
c3ac0193 PP |
594 | |
595 | sink = node->data; | |
596 | status = consume_graph_sink(sink); | |
371361a1 | 597 | if (unlikely(status != BT_GRAPH_STATUS_END)) { |
c3ac0193 | 598 | g_queue_push_tail_link(graph->sinks_to_consume, node); |
f60c8b34 JG |
599 | goto end; |
600 | } | |
601 | ||
602 | /* End reached, the node is not added back to the queue and free'd. */ | |
c3ac0193 | 603 | g_queue_delete_link(graph->sinks_to_consume, node); |
f60c8b34 JG |
604 | |
605 | /* Don't forward an END status if there are sinks left to consume. */ | |
606 | if (!g_queue_is_empty(graph->sinks_to_consume)) { | |
607 | status = BT_GRAPH_STATUS_OK; | |
608 | goto end; | |
609 | } | |
c3ac0193 PP |
610 | |
611 | end: | |
834e9996 PP |
612 | BT_LIB_LOGV("Consumed sink node: %![comp-]+c, status=%s", |
613 | sink, bt_graph_status_string(status)); | |
c3ac0193 PP |
614 | return status; |
615 | } | |
616 | ||
617 | BT_HIDDEN | |
618 | enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph, | |
834e9996 | 619 | struct bt_component_sink *sink) |
c3ac0193 PP |
620 | { |
621 | enum bt_graph_status status; | |
622 | GList *sink_node; | |
623 | int index; | |
624 | ||
834e9996 PP |
625 | BT_LIB_LOGV("Making specific sink consume: %![comp-]+c", sink); |
626 | BT_ASSERT(bt_component_borrow_graph((void *) sink) == graph); | |
c3ac0193 PP |
627 | |
628 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
629 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); | |
630 | status = BT_GRAPH_STATUS_END; | |
631 | goto end; | |
632 | } | |
633 | ||
634 | index = g_queue_index(graph->sinks_to_consume, sink); | |
635 | if (index < 0) { | |
636 | BT_LOGV_STR("Sink is not marked as consumable: sink is ended."); | |
637 | status = BT_GRAPH_STATUS_END; | |
638 | goto end; | |
639 | } | |
640 | ||
641 | sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index); | |
8b45963b | 642 | BT_ASSERT(sink_node); |
c3ac0193 PP |
643 | status = consume_sink_node(graph, sink_node); |
644 | ||
645 | end: | |
646 | return status; | |
647 | } | |
648 | ||
371361a1 | 649 | static inline |
834e9996 | 650 | enum bt_graph_status consume_no_check(struct bt_graph *graph) |
c3ac0193 PP |
651 | { |
652 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; | |
653 | struct bt_component *sink; | |
654 | GList *current_node; | |
655 | ||
8b45963b PP |
656 | BT_ASSERT_PRE(graph->has_sink, |
657 | "Graph has no sink component: %!+g", graph); | |
834e9996 | 658 | BT_LIB_LOGV("Making next sink consume: %![graph-]+g", graph); |
c3ac0193 | 659 | |
371361a1 | 660 | if (unlikely(g_queue_is_empty(graph->sinks_to_consume))) { |
c3ac0193 PP |
661 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
662 | status = BT_GRAPH_STATUS_END; | |
663 | goto end; | |
664 | } | |
665 | ||
666 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); | |
667 | sink = current_node->data; | |
834e9996 | 668 | BT_LIB_LOGV("Chose next sink to consume: %!+c", sink); |
c3ac0193 PP |
669 | status = consume_sink_node(graph, current_node); |
670 | ||
f60c8b34 JG |
671 | end: |
672 | return status; | |
c0418dd9 JG |
673 | } |
674 | ||
9cce94e4 | 675 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph) |
851b70bd | 676 | { |
8b45963b | 677 | enum bt_graph_status status; |
9ef22b36 | 678 | |
8b45963b PP |
679 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
680 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); | |
681 | BT_ASSERT_PRE(graph->can_consume, | |
682 | "Cannot consume graph in its current state: %!+g", graph); | |
9cce94e4 | 683 | bt_graph_set_can_consume(graph, false); |
1043fdea PP |
684 | status = bt_graph_configure(graph); |
685 | if (status) { | |
686 | /* bt_graph_configure() logs errors */ | |
687 | goto end; | |
688 | } | |
689 | ||
834e9996 | 690 | status = consume_no_check(graph); |
9cce94e4 | 691 | bt_graph_set_can_consume(graph, true); |
1043fdea PP |
692 | |
693 | end: | |
f8cd6131 | 694 | return status; |
851b70bd PP |
695 | } |
696 | ||
7b53201c | 697 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
f60c8b34 | 698 | { |
1043fdea | 699 | enum bt_graph_status status; |
f60c8b34 | 700 | |
834e9996 PP |
701 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
702 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); | |
371361a1 PP |
703 | BT_ASSERT_PRE(graph->can_consume, |
704 | "Cannot consume graph in its current state: %!+g", graph); | |
9cce94e4 | 705 | bt_graph_set_can_consume(graph, false); |
1043fdea PP |
706 | status = bt_graph_configure(graph); |
707 | if (status) { | |
708 | /* bt_graph_configure() logs errors */ | |
709 | goto end; | |
710 | } | |
711 | ||
834e9996 | 712 | BT_LIB_LOGV("Running graph: %!+g", graph); |
262e5473 | 713 | |
f60c8b34 | 714 | do { |
851b70bd PP |
715 | /* |
716 | * Check if the graph is canceled at each iteration. If | |
717 | * the graph was canceled by another thread or by a | |
834e9996 PP |
718 | * signal handler, this is not a warning nor an error, |
719 | * it was intentional: log with a DEBUG level only. | |
851b70bd | 720 | */ |
371361a1 | 721 | if (unlikely(graph->canceled)) { |
834e9996 PP |
722 | BT_LIB_LOGD("Stopping the graph: graph is canceled: " |
723 | "%!+g", graph); | |
851b70bd PP |
724 | status = BT_GRAPH_STATUS_CANCELED; |
725 | goto end; | |
726 | } | |
727 | ||
834e9996 | 728 | status = consume_no_check(graph); |
371361a1 | 729 | if (unlikely(status == BT_GRAPH_STATUS_AGAIN)) { |
f60c8b34 | 730 | /* |
202a3a13 PP |
731 | * If AGAIN is received and there are multiple |
732 | * sinks, go ahead and consume from the next | |
733 | * sink. | |
f60c8b34 | 734 | * |
202a3a13 PP |
735 | * However, in the case where a single sink is |
736 | * left, the caller can decide to busy-wait and | |
7b53201c | 737 | * call bt_graph_run() continuously |
834e9996 PP |
738 | * until the source is ready or it can decide to |
739 | * sleep for an arbitrary amount of time. | |
f60c8b34 JG |
740 | */ |
741 | if (graph->sinks_to_consume->length > 1) { | |
72b913fb | 742 | status = BT_GRAPH_STATUS_OK; |
f60c8b34 | 743 | } |
aebb2750 PP |
744 | } else if (status == BT_GRAPH_STATUS_NO_SINK) { |
745 | goto end; | |
f60c8b34 | 746 | } |
72b913fb | 747 | } while (status == BT_GRAPH_STATUS_OK); |
f60c8b34 JG |
748 | |
749 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
72b913fb | 750 | status = BT_GRAPH_STATUS_END; |
f60c8b34 | 751 | } |
262e5473 | 752 | |
202a3a13 | 753 | end: |
834e9996 PP |
754 | BT_LIB_LOGV("Graph ran: %![graph-]+g, status=%s", graph, |
755 | bt_graph_status_string(status)); | |
9cce94e4 | 756 | bt_graph_set_can_consume(graph, true); |
72b913fb | 757 | return status; |
f60c8b34 | 758 | } |
1bf957a0 | 759 | |
834e9996 | 760 | enum bt_graph_status |
7b53201c PP |
761 | bt_graph_add_source_component_output_port_added_listener( |
762 | struct bt_graph *graph, | |
763 | bt_graph_source_component_output_port_added_listener_func func, | |
764 | bt_graph_listener_removed_func listener_removed, void *data, | |
834e9996 | 765 | int *out_listener_id) |
1bf957a0 | 766 | { |
834e9996 PP |
767 | struct bt_graph_listener_port_added listener = { |
768 | .base = { | |
769 | .removed = listener_removed, | |
770 | .data = data, | |
771 | }, | |
772 | .func = (port_added_func_t) func, | |
1bf957a0 | 773 | }; |
834e9996 | 774 | int listener_id; |
1bf957a0 | 775 | |
834e9996 PP |
776 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
777 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
778 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
779 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
780 | "Graph currently executing a \"listener removed\" listener: " | |
781 | "%!+g", graph); | |
782 | g_array_append_val(graph->listeners.source_output_port_added, listener); | |
783 | listener_id = graph->listeners.source_output_port_added->len - 1; | |
784 | BT_LIB_LOGV("Added \"source component output port added\" listener to graph: " | |
785 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, | |
786 | listener_id); | |
787 | ||
788 | if (listener_id) { | |
789 | *out_listener_id = listener_id; | |
790 | } | |
791 | ||
792 | return BT_GRAPH_STATUS_OK; | |
1bf957a0 PP |
793 | } |
794 | ||
834e9996 | 795 | enum bt_graph_status |
7b53201c PP |
796 | bt_graph_add_filter_component_output_port_added_listener( |
797 | struct bt_graph *graph, | |
798 | bt_graph_filter_component_output_port_added_listener_func func, | |
799 | bt_graph_listener_removed_func listener_removed, void *data, | |
834e9996 | 800 | int *out_listener_id) |
1bf957a0 | 801 | { |
834e9996 PP |
802 | struct bt_graph_listener_port_added listener = { |
803 | .base = { | |
804 | .removed = listener_removed, | |
805 | .data = data, | |
806 | }, | |
807 | .func = (port_added_func_t) func, | |
808 | }; | |
809 | int listener_id; | |
1bf957a0 | 810 | |
834e9996 PP |
811 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
812 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
813 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
814 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
815 | "Graph currently executing a \"listener removed\" listener: " | |
816 | "%!+g", graph); | |
817 | g_array_append_val(graph->listeners.filter_output_port_added, listener); | |
818 | listener_id = graph->listeners.filter_output_port_added->len - 1; | |
819 | BT_LIB_LOGV("Added \"filter component output port added\" listener to graph: " | |
820 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, | |
821 | listener_id); | |
822 | ||
823 | if (listener_id) { | |
824 | *out_listener_id = listener_id; | |
825 | } | |
826 | ||
827 | return BT_GRAPH_STATUS_OK; | |
828 | } | |
262e5473 | 829 | |
834e9996 | 830 | enum bt_graph_status |
7b53201c PP |
831 | bt_graph_add_filter_component_input_port_added_listener( |
832 | struct bt_graph *graph, | |
833 | bt_graph_filter_component_input_port_added_listener_func func, | |
834 | bt_graph_listener_removed_func listener_removed, void *data, | |
834e9996 PP |
835 | int *out_listener_id) |
836 | { | |
834e9996 PP |
837 | struct bt_graph_listener_port_added listener = { |
838 | .base = { | |
839 | .removed = listener_removed, | |
840 | .data = data, | |
841 | }, | |
842 | .func = (port_added_func_t) func, | |
843 | }; | |
844 | int listener_id; | |
eb87cce7 | 845 | |
834e9996 PP |
846 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
847 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
848 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
849 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
850 | "Graph currently executing a \"listener removed\" listener: " | |
851 | "%!+g", graph); | |
852 | g_array_append_val(graph->listeners.filter_input_port_added, listener); | |
853 | listener_id = graph->listeners.filter_input_port_added->len - 1; | |
854 | BT_LIB_LOGV("Added \"filter component input port added\" listener to graph: " | |
855 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, | |
856 | listener_id); | |
857 | ||
858 | if (listener_id) { | |
859 | *out_listener_id = listener_id; | |
860 | } | |
861 | ||
862 | return BT_GRAPH_STATUS_OK; | |
863 | } | |
1bf957a0 | 864 | |
834e9996 | 865 | enum bt_graph_status |
7b53201c PP |
866 | bt_graph_add_sink_component_input_port_added_listener( |
867 | struct bt_graph *graph, | |
868 | bt_graph_sink_component_input_port_added_listener_func func, | |
869 | bt_graph_listener_removed_func listener_removed, void *data, | |
834e9996 PP |
870 | int *out_listener_id) |
871 | { | |
834e9996 PP |
872 | struct bt_graph_listener_port_added listener = { |
873 | .base = { | |
874 | .removed = listener_removed, | |
875 | .data = data, | |
876 | }, | |
877 | .func = (port_added_func_t) func, | |
878 | }; | |
879 | int listener_id; | |
1bf957a0 | 880 | |
834e9996 PP |
881 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
882 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
883 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
884 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
885 | "Graph currently executing a \"listener removed\" listener: " | |
886 | "%!+g", graph); | |
887 | g_array_append_val(graph->listeners.sink_input_port_added, listener); | |
888 | listener_id = graph->listeners.sink_input_port_added->len - 1; | |
889 | BT_LIB_LOGV("Added \"sink component input port added\" listener to graph: " | |
890 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, | |
891 | listener_id); | |
892 | ||
893 | if (listener_id) { | |
894 | *out_listener_id = listener_id; | |
895 | } | |
896 | ||
897 | return BT_GRAPH_STATUS_OK; | |
1bf957a0 PP |
898 | } |
899 | ||
834e9996 | 900 | enum bt_graph_status |
7b53201c PP |
901 | bt_graph_add_source_filter_component_ports_connected_listener( |
902 | struct bt_graph *graph, | |
903 | bt_graph_source_filter_component_ports_connected_listener_func func, | |
904 | bt_graph_listener_removed_func listener_removed, void *data, | |
834e9996 PP |
905 | int *out_listener_id) |
906 | { | |
834e9996 PP |
907 | struct bt_graph_listener_ports_connected listener = { |
908 | .base = { | |
909 | .removed = listener_removed, | |
910 | .data = data, | |
911 | }, | |
912 | .func = (ports_connected_func_t) func, | |
913 | }; | |
914 | int listener_id; | |
eb87cce7 | 915 | |
834e9996 PP |
916 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
917 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
918 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
919 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
920 | "Graph currently executing a \"listener removed\" listener: " | |
921 | "%!+g", graph); | |
922 | g_array_append_val(graph->listeners.source_filter_ports_connected, | |
923 | listener); | |
924 | listener_id = graph->listeners.source_filter_ports_connected->len - 1; | |
925 | BT_LIB_LOGV("Added \"source to filter component ports connected\" listener to graph: " | |
926 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, | |
927 | listener_id); | |
928 | ||
929 | if (listener_id) { | |
930 | *out_listener_id = listener_id; | |
931 | } | |
932 | ||
933 | return BT_GRAPH_STATUS_OK; | |
934 | } | |
1bf957a0 | 935 | |
834e9996 | 936 | enum bt_graph_status |
7b53201c PP |
937 | bt_graph_add_source_sink_component_ports_connected_listener( |
938 | struct bt_graph *graph, | |
939 | bt_graph_source_sink_component_ports_connected_listener_func func, | |
940 | bt_graph_listener_removed_func listener_removed, void *data, | |
834e9996 PP |
941 | int *out_listener_id) |
942 | { | |
834e9996 PP |
943 | struct bt_graph_listener_ports_connected listener = { |
944 | .base = { | |
945 | .removed = listener_removed, | |
946 | .data = data, | |
947 | }, | |
948 | .func = (ports_connected_func_t) func, | |
949 | }; | |
950 | int listener_id; | |
1bf957a0 | 951 | |
834e9996 PP |
952 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
953 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
954 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
955 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
956 | "Graph currently executing a \"listener removed\" listener: " | |
957 | "%!+g", graph); | |
958 | g_array_append_val(graph->listeners.source_sink_ports_connected, | |
959 | listener); | |
960 | listener_id = graph->listeners.source_sink_ports_connected->len - 1; | |
961 | BT_LIB_LOGV("Added \"source to sink component ports connected\" listener to graph: " | |
962 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, | |
963 | listener_id); | |
964 | ||
965 | if (listener_id) { | |
966 | *out_listener_id = listener_id; | |
967 | } | |
968 | ||
969 | return BT_GRAPH_STATUS_OK; | |
1bf957a0 PP |
970 | } |
971 | ||
ee976410 PP |
972 | enum bt_graph_status |
973 | bt_graph_add_filter_filter_component_ports_connected_listener( | |
974 | struct bt_graph *graph, | |
975 | bt_graph_filter_filter_component_ports_connected_listener_func func, | |
976 | bt_graph_listener_removed_func listener_removed, void *data, | |
977 | int *out_listener_id) | |
978 | { | |
979 | struct bt_graph_listener_ports_connected listener = { | |
980 | .base = { | |
981 | .removed = listener_removed, | |
982 | .data = data, | |
983 | }, | |
984 | .func = (ports_connected_func_t) func, | |
985 | }; | |
986 | int listener_id; | |
987 | ||
988 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); | |
989 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
990 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
991 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
992 | "Graph currently executing a \"listener removed\" listener: " | |
993 | "%!+g", graph); | |
994 | g_array_append_val(graph->listeners.filter_filter_ports_connected, | |
995 | listener); | |
996 | listener_id = graph->listeners.filter_filter_ports_connected->len - 1; | |
997 | BT_LIB_LOGV("Added \"filter to filter component ports connected\" listener to graph: " | |
998 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, | |
999 | listener_id); | |
1000 | ||
1001 | if (listener_id) { | |
1002 | *out_listener_id = listener_id; | |
1003 | } | |
1004 | ||
1005 | return BT_GRAPH_STATUS_OK; | |
1006 | } | |
1007 | ||
834e9996 | 1008 | enum bt_graph_status |
7b53201c PP |
1009 | bt_graph_add_filter_sink_component_ports_connected_listener( |
1010 | struct bt_graph *graph, | |
1011 | bt_graph_filter_sink_component_ports_connected_listener_func func, | |
1012 | bt_graph_listener_removed_func listener_removed, void *data, | |
834e9996 | 1013 | int *out_listener_id) |
1bf957a0 | 1014 | { |
834e9996 PP |
1015 | struct bt_graph_listener_ports_connected listener = { |
1016 | .base = { | |
1017 | .removed = listener_removed, | |
1018 | .data = data, | |
1019 | }, | |
1020 | .func = (ports_connected_func_t) func, | |
1021 | }; | |
1022 | int listener_id; | |
1bf957a0 | 1023 | |
834e9996 PP |
1024 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
1025 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
1026 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
1027 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
1028 | "Graph currently executing a \"listener removed\" listener: " | |
1029 | "%!+g", graph); | |
1030 | g_array_append_val(graph->listeners.filter_sink_ports_connected, | |
1031 | listener); | |
1032 | listener_id = graph->listeners.filter_sink_ports_connected->len - 1; | |
1033 | BT_LIB_LOGV("Added \"filter to sink component ports connected\" listener to graph: " | |
1034 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, | |
1035 | listener_id); | |
1036 | ||
1037 | if (listener_id) { | |
1038 | *out_listener_id = listener_id; | |
1039 | } | |
1040 | ||
1041 | return BT_GRAPH_STATUS_OK; | |
1042 | } | |
262e5473 | 1043 | |
1bf957a0 PP |
1044 | BT_HIDDEN |
1045 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) | |
1046 | { | |
834e9996 PP |
1047 | uint64_t i; |
1048 | GArray *listeners; | |
1049 | struct bt_component *comp; | |
1bf957a0 | 1050 | |
834e9996 PP |
1051 | BT_ASSERT(graph); |
1052 | BT_ASSERT(port); | |
1053 | BT_LIB_LOGV("Notifying graph listeners that a port was added: " | |
1054 | "%![graph-]+g, %![port-]+p", graph, port); | |
7b53201c | 1055 | comp = bt_port_borrow_component_inline(port); |
834e9996 PP |
1056 | BT_ASSERT(comp); |
1057 | ||
1058 | switch (comp->class->type) { | |
1059 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
1060 | { | |
1061 | switch (port->type) { | |
1062 | case BT_PORT_TYPE_OUTPUT: | |
1063 | listeners = graph->listeners.source_output_port_added; | |
1064 | break; | |
1065 | default: | |
1066 | abort(); | |
1067 | } | |
1068 | ||
1069 | break; | |
1070 | } | |
1071 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
1072 | { | |
1073 | switch (port->type) { | |
1074 | case BT_PORT_TYPE_INPUT: | |
1075 | listeners = graph->listeners.filter_input_port_added; | |
1076 | break; | |
1077 | case BT_PORT_TYPE_OUTPUT: | |
1078 | listeners = graph->listeners.filter_output_port_added; | |
1079 | break; | |
1080 | default: | |
1081 | abort(); | |
1082 | } | |
262e5473 | 1083 | |
834e9996 PP |
1084 | break; |
1085 | } | |
1086 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
1087 | { | |
1088 | switch (port->type) { | |
1089 | case BT_PORT_TYPE_INPUT: | |
1090 | listeners = graph->listeners.sink_input_port_added; | |
1091 | break; | |
1092 | default: | |
1093 | abort(); | |
1094 | } | |
1bf957a0 | 1095 | |
834e9996 PP |
1096 | break; |
1097 | } | |
1098 | default: | |
1099 | abort(); | |
1100 | } | |
1101 | ||
1102 | for (i = 0; i < listeners->len; i++) { | |
1103 | struct bt_graph_listener_port_added *listener = | |
1104 | &g_array_index(listeners, | |
1105 | struct bt_graph_listener_port_added, i); | |
1106 | ||
1107 | BT_ASSERT(listener->func); | |
1108 | listener->func(comp, port, listener->base.data); | |
1bf957a0 PP |
1109 | } |
1110 | } | |
1111 | ||
1bf957a0 | 1112 | BT_HIDDEN |
f345f8bb PP |
1113 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
1114 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 | 1115 | { |
834e9996 PP |
1116 | uint64_t i; |
1117 | GArray *listeners; | |
1118 | struct bt_component *upstream_comp; | |
1119 | struct bt_component *downstream_comp; | |
1bf957a0 | 1120 | |
834e9996 PP |
1121 | BT_ASSERT(graph); |
1122 | BT_ASSERT(upstream_port); | |
1123 | BT_ASSERT(downstream_port); | |
1124 | BT_LIB_LOGV("Notifying graph listeners that ports were connected: " | |
1125 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", | |
1126 | graph, upstream_port, downstream_port); | |
7b53201c | 1127 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
834e9996 | 1128 | BT_ASSERT(upstream_comp); |
7b53201c | 1129 | downstream_comp = bt_port_borrow_component_inline(downstream_port); |
834e9996 PP |
1130 | BT_ASSERT(downstream_comp); |
1131 | ||
1132 | switch (upstream_comp->class->type) { | |
1133 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
1134 | { | |
1135 | switch (downstream_comp->class->type) { | |
1136 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
1137 | listeners = | |
1138 | graph->listeners.source_filter_ports_connected; | |
1139 | break; | |
1140 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
1141 | listeners = | |
1142 | graph->listeners.source_sink_ports_connected; | |
1143 | break; | |
1144 | default: | |
1145 | abort(); | |
1146 | } | |
262e5473 | 1147 | |
834e9996 PP |
1148 | break; |
1149 | } | |
1150 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
1151 | { | |
1152 | switch (downstream_comp->class->type) { | |
ee976410 PP |
1153 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
1154 | listeners = | |
1155 | graph->listeners.filter_filter_ports_connected; | |
1156 | break; | |
834e9996 PP |
1157 | case BT_COMPONENT_CLASS_TYPE_SINK: |
1158 | listeners = | |
1159 | graph->listeners.filter_sink_ports_connected; | |
1160 | break; | |
1161 | default: | |
1162 | abort(); | |
1163 | } | |
1bf957a0 | 1164 | |
834e9996 PP |
1165 | break; |
1166 | } | |
1167 | default: | |
1168 | abort(); | |
1169 | } | |
1170 | ||
1171 | for (i = 0; i < listeners->len; i++) { | |
1172 | struct bt_graph_listener_ports_connected *listener = | |
1173 | &g_array_index(listeners, | |
1174 | struct bt_graph_listener_ports_connected, i); | |
1175 | ||
1176 | BT_ASSERT(listener->func); | |
1177 | listener->func(upstream_comp, downstream_comp, | |
1178 | upstream_port, downstream_port, listener->base.data); | |
1bf957a0 PP |
1179 | } |
1180 | } | |
1181 | ||
9cce94e4 | 1182 | enum bt_graph_status bt_graph_cancel(struct bt_graph *graph) |
202a3a13 | 1183 | { |
202a3a13 | 1184 | |
834e9996 PP |
1185 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
1186 | graph->canceled = true; | |
1187 | BT_LIB_LOGV("Canceled graph: %!+i", graph); | |
1188 | return BT_GRAPH_STATUS_OK; | |
202a3a13 PP |
1189 | } |
1190 | ||
7b53201c | 1191 | bt_bool bt_graph_is_canceled(const struct bt_graph *graph) |
202a3a13 | 1192 | { |
834e9996 PP |
1193 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
1194 | return graph->canceled ? BT_TRUE : BT_FALSE; | |
202a3a13 | 1195 | } |
f167d3c0 PP |
1196 | |
1197 | BT_HIDDEN | |
1198 | void bt_graph_remove_connection(struct bt_graph *graph, | |
1199 | struct bt_connection *connection) | |
1200 | { | |
8b45963b PP |
1201 | BT_ASSERT(graph); |
1202 | BT_ASSERT(connection); | |
834e9996 | 1203 | BT_LIB_LOGV("Removing graph's connection: %![graph-]+g, %![conn-]+x", |
262e5473 | 1204 | graph, connection); |
f167d3c0 PP |
1205 | g_ptr_array_remove(graph->connections, connection); |
1206 | } | |
36712f1d | 1207 | |
834e9996 PP |
1208 | BT_ASSERT_PRE_FUNC |
1209 | static inline | |
1210 | bool component_name_exists(struct bt_graph *graph, const char *name) | |
1211 | { | |
1212 | bool exists = false; | |
1213 | uint64_t i; | |
1214 | ||
1215 | for (i = 0; i < graph->components->len; i++) { | |
1216 | struct bt_component *other_comp = graph->components->pdata[i]; | |
1217 | ||
1218 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { | |
1219 | BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: " | |
1220 | "%![other-comp-]+c, name=\"%s\"", | |
1221 | other_comp, name); | |
1222 | exists = true; | |
1223 | goto end; | |
1224 | } | |
1225 | } | |
1226 | ||
1227 | end: | |
1228 | return exists; | |
1229 | } | |
1230 | ||
1231 | static | |
1232 | enum bt_graph_status add_component_with_init_method_data( | |
7b53201c | 1233 | struct bt_graph *graph, |
834e9996 PP |
1234 | struct bt_component_class *comp_cls, |
1235 | comp_init_method_t init_method, | |
ce141536 | 1236 | const char *name, const struct bt_value *params, |
834e9996 | 1237 | void *init_method_data, struct bt_component **user_component) |
36712f1d PP |
1238 | { |
1239 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; | |
834e9996 | 1240 | enum bt_self_component_status comp_status; |
36712f1d | 1241 | struct bt_component *component = NULL; |
834e9996 PP |
1242 | int ret; |
1243 | bool init_can_consume; | |
ce141536 | 1244 | struct bt_value *new_params = NULL; |
36712f1d | 1245 | |
834e9996 PP |
1246 | BT_ASSERT(comp_cls); |
1247 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); | |
1248 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
1249 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); | |
1043fdea PP |
1250 | BT_ASSERT_PRE( |
1251 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
9cce94e4 | 1252 | "Graph is already configured: %!+g", graph); |
834e9996 PP |
1253 | BT_ASSERT_PRE(!component_name_exists(graph, name), |
1254 | "Duplicate component name: %!+g, name=\"%s\"", graph, name); | |
1255 | BT_ASSERT_PRE(!params || bt_value_is_map(params), | |
1256 | "Parameter value is not a map value: %!+v", params); | |
3afb93ee | 1257 | init_can_consume = graph->can_consume; |
834e9996 PP |
1258 | bt_graph_set_can_consume(graph, false); |
1259 | BT_LIB_LOGD("Adding component to graph: " | |
1260 | "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, " | |
36712f1d | 1261 | "init-method-data-addr=%p", |
834e9996 | 1262 | graph, comp_cls, name, params, init_method_data); |
36712f1d | 1263 | |
834e9996 | 1264 | if (!params) { |
ce141536 PP |
1265 | new_params = bt_value_map_create(); |
1266 | if (!new_params) { | |
36712f1d PP |
1267 | BT_LOGE_STR("Cannot create map value object."); |
1268 | graph_status = BT_GRAPH_STATUS_NOMEM; | |
1269 | goto end; | |
1270 | } | |
ce141536 PP |
1271 | |
1272 | params = new_params; | |
36712f1d PP |
1273 | } |
1274 | ||
834e9996 PP |
1275 | ret = bt_component_create(comp_cls, name, &component); |
1276 | if (ret) { | |
1277 | BT_LOGE("Cannot create empty component object: ret=%d", | |
1278 | ret); | |
1279 | graph_status = BT_GRAPH_STATUS_NOMEM; | |
36712f1d PP |
1280 | goto end; |
1281 | } | |
1282 | ||
1283 | /* | |
1284 | * The user's initialization method needs to see that this | |
1285 | * component is part of the graph. If the user method fails, we | |
1286 | * immediately remove the component from the graph's components. | |
1287 | */ | |
1288 | g_ptr_array_add(graph->components, component); | |
1289 | bt_component_set_graph(component, graph); | |
1290 | ||
834e9996 | 1291 | if (init_method) { |
36712f1d | 1292 | BT_LOGD_STR("Calling user's initialization method."); |
834e9996 | 1293 | comp_status = init_method(component, params, init_method_data); |
36712f1d | 1294 | BT_LOGD("User method returned: status=%s", |
834e9996 PP |
1295 | bt_self_component_status_string(comp_status)); |
1296 | if (comp_status != BT_SELF_COMPONENT_STATUS_OK) { | |
36712f1d | 1297 | BT_LOGW_STR("Initialization method failed."); |
834e9996 | 1298 | graph_status = (int) comp_status; |
36712f1d PP |
1299 | bt_component_set_graph(component, NULL); |
1300 | g_ptr_array_remove_fast(graph->components, component); | |
1301 | goto end; | |
1302 | } | |
1303 | } | |
1304 | ||
1305 | /* | |
1306 | * Mark the component as initialized so that its finalization | |
1307 | * method is called when it is destroyed. | |
1308 | */ | |
1309 | component->initialized = true; | |
1310 | ||
1311 | /* | |
1312 | * If it's a sink component, it needs to be part of the graph's | |
1313 | * sink queue to be consumed by bt_graph_consume(). | |
1314 | */ | |
1315 | if (bt_component_is_sink(component)) { | |
834e9996 | 1316 | graph->has_sink = true; |
36712f1d PP |
1317 | g_queue_push_tail(graph->sinks_to_consume, component); |
1318 | } | |
1319 | ||
1320 | /* | |
1321 | * Freeze the component class now that it's instantiated at | |
1322 | * least once. | |
1323 | */ | |
1324 | BT_LOGD_STR("Freezing component class."); | |
834e9996 PP |
1325 | bt_component_class_freeze(comp_cls); |
1326 | BT_LIB_LOGD("Added component to graph: " | |
1327 | "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, " | |
1328 | "init-method-data-addr=%p, %![comp-]+c", | |
1329 | graph, comp_cls, name, params, init_method_data, component); | |
36712f1d PP |
1330 | |
1331 | if (user_component) { | |
1332 | /* Move reference to user */ | |
1333 | *user_component = component; | |
1334 | component = NULL; | |
1335 | } | |
1336 | ||
1337 | end: | |
8138bfe1 | 1338 | bt_object_put_ref(component); |
ce141536 | 1339 | bt_object_put_ref(new_params); |
834e9996 PP |
1340 | (void) init_can_consume; |
1341 | bt_graph_set_can_consume(graph, init_can_consume); | |
36712f1d PP |
1342 | return graph_status; |
1343 | } | |
1344 | ||
834e9996 | 1345 | enum bt_graph_status |
7b53201c PP |
1346 | bt_graph_add_source_component_with_init_method_data( |
1347 | struct bt_graph *graph, | |
1348 | const struct bt_component_class_source *comp_cls, | |
ce141536 | 1349 | const char *name, const struct bt_value *params, |
7b53201c PP |
1350 | void *init_method_data, |
1351 | const struct bt_component_source **component) | |
834e9996 PP |
1352 | { |
1353 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); | |
1354 | return add_component_with_init_method_data(graph, | |
1355 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
1356 | name, params, init_method_data, (void *) component); | |
1357 | } | |
1358 | ||
7b53201c PP |
1359 | enum bt_graph_status bt_graph_add_source_component( |
1360 | struct bt_graph *graph, | |
1361 | const struct bt_component_class_source *comp_cls, | |
ce141536 | 1362 | const char *name, const struct bt_value *params, |
7b53201c | 1363 | const struct bt_component_source **component) |
834e9996 | 1364 | { |
7b53201c | 1365 | return bt_graph_add_source_component_with_init_method_data( |
834e9996 PP |
1366 | graph, comp_cls, name, params, NULL, component); |
1367 | } | |
1368 | ||
1369 | enum bt_graph_status | |
7b53201c PP |
1370 | bt_graph_add_filter_component_with_init_method_data( |
1371 | struct bt_graph *graph, | |
1372 | const struct bt_component_class_filter *comp_cls, | |
ce141536 | 1373 | const char *name, const struct bt_value *params, |
7b53201c PP |
1374 | void *init_method_data, |
1375 | const struct bt_component_filter **component) | |
834e9996 PP |
1376 | { |
1377 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); | |
1378 | return add_component_with_init_method_data(graph, | |
1379 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
1380 | name, params, init_method_data, (void *) component); | |
1381 | } | |
1382 | ||
7b53201c PP |
1383 | enum bt_graph_status bt_graph_add_filter_component( |
1384 | struct bt_graph *graph, | |
1385 | const struct bt_component_class_filter *comp_cls, | |
ce141536 | 1386 | const char *name, const struct bt_value *params, |
7b53201c | 1387 | const struct bt_component_filter **component) |
834e9996 | 1388 | { |
7b53201c | 1389 | return bt_graph_add_filter_component_with_init_method_data( |
834e9996 PP |
1390 | graph, comp_cls, name, params, NULL, component); |
1391 | } | |
1392 | ||
1393 | enum bt_graph_status | |
7b53201c PP |
1394 | bt_graph_add_sink_component_with_init_method_data( |
1395 | struct bt_graph *graph, | |
1396 | const struct bt_component_class_sink *comp_cls, | |
ce141536 | 1397 | const char *name, const struct bt_value *params, |
7b53201c PP |
1398 | void *init_method_data, |
1399 | const struct bt_component_sink **component) | |
36712f1d | 1400 | { |
834e9996 PP |
1401 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
1402 | return add_component_with_init_method_data(graph, | |
1403 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
1404 | name, params, init_method_data, (void *) component); | |
1405 | } | |
1406 | ||
7b53201c PP |
1407 | enum bt_graph_status bt_graph_add_sink_component( |
1408 | struct bt_graph *graph, | |
1409 | const struct bt_component_class_sink *comp_cls, | |
ce141536 | 1410 | const char *name, const struct bt_value *params, |
7b53201c | 1411 | const struct bt_component_sink **component) |
834e9996 | 1412 | { |
7b53201c | 1413 | return bt_graph_add_sink_component_with_init_method_data( |
834e9996 | 1414 | graph, comp_cls, name, params, NULL, component); |
36712f1d | 1415 | } |
c3ac0193 PP |
1416 | |
1417 | BT_HIDDEN | |
1418 | int bt_graph_remove_unconnected_component(struct bt_graph *graph, | |
1419 | struct bt_component *component) | |
1420 | { | |
834e9996 PP |
1421 | bool init_can_consume; |
1422 | uint64_t count; | |
c3ac0193 PP |
1423 | uint64_t i; |
1424 | int ret = 0; | |
1425 | ||
8b45963b PP |
1426 | BT_ASSERT(graph); |
1427 | BT_ASSERT(component); | |
1d7bf349 | 1428 | BT_ASSERT(component->base.ref_count == 0); |
8b45963b | 1429 | BT_ASSERT(bt_component_borrow_graph(component) == graph); |
c3ac0193 | 1430 | |
3afb93ee | 1431 | init_can_consume = graph->can_consume; |
c3ac0193 PP |
1432 | count = bt_component_get_input_port_count(component); |
1433 | ||
1434 | for (i = 0; i < count; i++) { | |
834e9996 PP |
1435 | struct bt_port *port = (void *) |
1436 | bt_component_borrow_input_port_by_index(component, i); | |
c3ac0193 | 1437 | |
8b45963b | 1438 | BT_ASSERT(port); |
c3ac0193 PP |
1439 | |
1440 | if (bt_port_is_connected(port)) { | |
834e9996 | 1441 | BT_LIB_LOGW("Cannot remove component from graph: " |
c3ac0193 | 1442 | "an input port is connected: " |
834e9996 PP |
1443 | "%![graph-]+g, %![comp-]+c, %![port-]+p", |
1444 | graph, component, port); | |
c3ac0193 PP |
1445 | goto error; |
1446 | } | |
1447 | } | |
1448 | ||
1449 | count = bt_component_get_output_port_count(component); | |
1450 | ||
1451 | for (i = 0; i < count; i++) { | |
834e9996 PP |
1452 | struct bt_port *port = (void *) |
1453 | bt_component_borrow_output_port_by_index(component, i); | |
c3ac0193 | 1454 | |
8b45963b | 1455 | BT_ASSERT(port); |
c3ac0193 PP |
1456 | |
1457 | if (bt_port_is_connected(port)) { | |
834e9996 | 1458 | BT_LIB_LOGW("Cannot remove component from graph: " |
c3ac0193 | 1459 | "an output port is connected: " |
834e9996 PP |
1460 | "%![graph-]+g, %![comp-]+c, %![port-]+p", |
1461 | graph, component, port); | |
c3ac0193 PP |
1462 | goto error; |
1463 | } | |
1464 | } | |
1465 | ||
834e9996 | 1466 | bt_graph_set_can_consume(graph, false); |
c3ac0193 PP |
1467 | |
1468 | /* Possibly remove from sinks to consume */ | |
1469 | (void) g_queue_remove(graph->sinks_to_consume, component); | |
1470 | ||
1471 | if (graph->sinks_to_consume->length == 0) { | |
834e9996 | 1472 | graph->has_sink = false; |
c3ac0193 PP |
1473 | } |
1474 | ||
1475 | /* | |
1d7bf349 PP |
1476 | * This calls bt_object_try_spec_release() on the component, and |
1477 | * since its reference count is 0, its destructor is called. Its | |
c3ac0193 PP |
1478 | * destructor calls the user's finalization method (if set). |
1479 | */ | |
1480 | g_ptr_array_remove(graph->components, component); | |
1481 | goto end; | |
1482 | ||
1483 | error: | |
1484 | ret = -1; | |
1485 | ||
1486 | end: | |
834e9996 PP |
1487 | (void) init_can_consume; |
1488 | bt_graph_set_can_consume(graph, init_can_consume); | |
c3ac0193 PP |
1489 | return ret; |
1490 | } | |
f7c3ac09 PP |
1491 | |
1492 | BT_HIDDEN | |
b09a5592 PP |
1493 | void bt_graph_add_message(struct bt_graph *graph, |
1494 | struct bt_message *msg) | |
f7c3ac09 PP |
1495 | { |
1496 | BT_ASSERT(graph); | |
b09a5592 | 1497 | BT_ASSERT(msg); |
f7c3ac09 PP |
1498 | |
1499 | /* | |
1500 | * It's okay not to take a reference because, when a | |
b09a5592 | 1501 | * message's reference count drops to 0, either: |
f7c3ac09 PP |
1502 | * |
1503 | * * It is recycled back to one of this graph's pool. | |
1504 | * * It is destroyed because it doesn't have any link to any | |
1505 | * graph, which means the original graph is already destroyed. | |
1506 | */ | |
b09a5592 | 1507 | g_ptr_array_add(graph->messages, msg); |
f7c3ac09 | 1508 | } |
8c6884d9 PP |
1509 | |
1510 | void bt_graph_get_ref(const struct bt_graph *graph) | |
1511 | { | |
1512 | bt_object_get_ref(graph); | |
1513 | } | |
1514 | ||
1515 | void bt_graph_put_ref(const struct bt_graph *graph) | |
1516 | { | |
1517 | bt_object_put_ref(graph); | |
1518 | } |