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