Commit | Line | Data |
---|---|---|
c0418dd9 | 1 | /* |
7d55361f | 2 | * graph.c |
c0418dd9 JG |
3 | * |
4 | * Babeltrace Plugin Component Graph | |
5 | * | |
f60c8b34 | 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
262e5473 | 7 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
c0418dd9 JG |
8 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
262e5473 PP |
28 | #define BT_LOG_TAG "GRAPH" |
29 | #include <babeltrace/lib-logging-internal.h> | |
30 | ||
b2e0c907 PP |
31 | #include <babeltrace/graph/component-internal.h> |
32 | #include <babeltrace/graph/graph-internal.h> | |
33 | #include <babeltrace/graph/connection-internal.h> | |
34 | #include <babeltrace/graph/component-sink-internal.h> | |
35 | #include <babeltrace/graph/component-source.h> | |
36 | #include <babeltrace/graph/component-filter.h> | |
37 | #include <babeltrace/graph/port.h> | |
5c563278 PP |
38 | #include <babeltrace/graph/notification-internal.h> |
39 | #include <babeltrace/graph/notification-event-internal.h> | |
40 | #include <babeltrace/graph/notification-packet-internal.h> | |
3d9990ac | 41 | #include <babeltrace/compiler-internal.h> |
c55a9f58 | 42 | #include <babeltrace/types.h> |
36712f1d PP |
43 | #include <babeltrace/values.h> |
44 | #include <babeltrace/values-internal.h> | |
f6ccaed9 PP |
45 | #include <babeltrace/assert-internal.h> |
46 | #include <babeltrace/assert-pre-internal.h> | |
f60c8b34 | 47 | #include <unistd.h> |
1bf957a0 PP |
48 | #include <glib.h> |
49 | ||
50 | struct bt_graph_listener { | |
51 | void *func; | |
8cc092c9 | 52 | bt_graph_listener_removed removed; |
1bf957a0 PP |
53 | void *data; |
54 | }; | |
c0418dd9 | 55 | |
8cc092c9 PP |
56 | static |
57 | int init_listeners_array(GArray **listeners) | |
58 | { | |
59 | int ret = 0; | |
60 | ||
f6ccaed9 | 61 | BT_ASSERT(listeners); |
8cc092c9 PP |
62 | *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener)); |
63 | if (!*listeners) { | |
64 | BT_LOGE_STR("Failed to allocate one GArray."); | |
65 | ret = -1; | |
66 | goto end; | |
67 | } | |
68 | ||
69 | end: | |
70 | return ret; | |
71 | } | |
72 | ||
73 | static | |
74 | void call_remove_listeners(GArray *listeners) | |
75 | { | |
76 | size_t i; | |
77 | ||
78 | for (i = 0; i < listeners->len; i++) { | |
79 | struct bt_graph_listener listener = | |
80 | g_array_index(listeners, struct bt_graph_listener, i); | |
81 | ||
82 | if (listener.removed) { | |
83 | listener.removed(listener.data); | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
f60c8b34 JG |
88 | static |
89 | void bt_graph_destroy(struct bt_object *obj) | |
c0418dd9 | 90 | { |
f60c8b34 JG |
91 | struct bt_graph *graph = container_of(obj, |
92 | struct bt_graph, base); | |
c0418dd9 | 93 | |
bd14d768 PP |
94 | /* |
95 | * The graph's reference count is 0 if we're here. Increment | |
96 | * it to avoid a double-destroy (possibly infinitely recursive) | |
97 | * in this situation: | |
98 | * | |
99 | * 1. We put and destroy a connection. | |
100 | * 2. This connection's destructor finalizes its active | |
101 | * notification iterators. | |
102 | * 3. A notification iterator's finalization function gets a | |
103 | * new reference on its component (reference count goes from | |
104 | * 0 to 1). | |
105 | * 4. Since this component's reference count goes to 1, it takes | |
106 | * a reference on its parent (this graph). This graph's | |
107 | * reference count goes from 0 to 1. | |
108 | * 5. The notification iterator's finalization function puts its | |
109 | * component reference (reference count goes from 1 to 0). | |
110 | * 6. Since this component's reference count goes from 1 to 0, | |
111 | * it puts its parent (this graph). This graph's reference | |
112 | * count goes from 1 to 0. | |
113 | * 7. Since this graph's reference count goes from 1 to 0, | |
114 | * its destructor is called (this function). | |
115 | * | |
116 | * With the incrementation below, the graph's reference count at | |
117 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This | |
118 | * ensures that this function is not called two times. | |
119 | */ | |
262e5473 | 120 | BT_LOGD("Destroying graph: addr=%p", graph); |
3fea54f6 | 121 | obj->ref_count++; |
bd14d768 | 122 | |
49682acd PP |
123 | /* |
124 | * Cancel the graph to disallow some operations, like creating | |
125 | * notification iterators and adding ports to components. | |
126 | */ | |
127 | (void) bt_graph_cancel(graph); | |
128 | ||
8cc092c9 PP |
129 | /* Call all remove listeners */ |
130 | call_remove_listeners(graph->listeners.port_added); | |
131 | call_remove_listeners(graph->listeners.port_removed); | |
132 | call_remove_listeners(graph->listeners.ports_connected); | |
133 | call_remove_listeners(graph->listeners.ports_disconnected); | |
134 | ||
5c563278 PP |
135 | if (graph->notifications) { |
136 | g_ptr_array_free(graph->notifications, TRUE); | |
137 | } | |
138 | ||
c0418dd9 | 139 | if (graph->connections) { |
262e5473 | 140 | BT_LOGD_STR("Destroying connections."); |
c0418dd9 JG |
141 | g_ptr_array_free(graph->connections, TRUE); |
142 | } | |
5c563278 | 143 | |
bd14d768 | 144 | if (graph->components) { |
262e5473 | 145 | BT_LOGD_STR("Destroying components."); |
bd14d768 PP |
146 | g_ptr_array_free(graph->components, TRUE); |
147 | } | |
5c563278 | 148 | |
f60c8b34 JG |
149 | if (graph->sinks_to_consume) { |
150 | g_queue_free(graph->sinks_to_consume); | |
c0418dd9 | 151 | } |
1bf957a0 PP |
152 | |
153 | if (graph->listeners.port_added) { | |
154 | g_array_free(graph->listeners.port_added, TRUE); | |
155 | } | |
156 | ||
157 | if (graph->listeners.port_removed) { | |
158 | g_array_free(graph->listeners.port_removed, TRUE); | |
159 | } | |
160 | ||
f345f8bb PP |
161 | if (graph->listeners.ports_connected) { |
162 | g_array_free(graph->listeners.ports_connected, TRUE); | |
1bf957a0 PP |
163 | } |
164 | ||
f345f8bb PP |
165 | if (graph->listeners.ports_disconnected) { |
166 | g_array_free(graph->listeners.ports_disconnected, TRUE); | |
1bf957a0 PP |
167 | } |
168 | ||
5c563278 PP |
169 | bt_object_pool_finalize(&graph->event_notif_pool); |
170 | bt_object_pool_finalize(&graph->packet_begin_notif_pool); | |
171 | bt_object_pool_finalize(&graph->packet_end_notif_pool); | |
c0418dd9 JG |
172 | g_free(graph); |
173 | } | |
174 | ||
5c563278 PP |
175 | static |
176 | void destroy_notification_event(struct bt_notification *notif, | |
177 | struct bt_graph *graph) | |
178 | { | |
179 | bt_notification_event_destroy(notif); | |
180 | } | |
181 | ||
182 | static | |
183 | void destroy_notification_packet_begin(struct bt_notification *notif, | |
184 | struct bt_graph *graph) | |
185 | { | |
186 | bt_notification_packet_begin_destroy(notif); | |
187 | } | |
188 | ||
189 | static | |
190 | void destroy_notification_packet_end(struct bt_notification *notif, | |
191 | struct bt_graph *graph) | |
192 | { | |
193 | bt_notification_packet_end_destroy(notif); | |
194 | } | |
195 | ||
196 | static | |
197 | void notify_notification_graph_is_destroyed(struct bt_notification *notif) | |
198 | { | |
199 | bt_notification_unlink_graph(notif); | |
200 | } | |
201 | ||
f60c8b34 | 202 | struct bt_graph *bt_graph_create(void) |
c0418dd9 | 203 | { |
f60c8b34 | 204 | struct bt_graph *graph; |
1bf957a0 | 205 | int ret; |
c0418dd9 | 206 | |
262e5473 | 207 | BT_LOGD_STR("Creating graph object."); |
f60c8b34 | 208 | graph = g_new0(struct bt_graph, 1); |
c0418dd9 | 209 | if (!graph) { |
262e5473 | 210 | BT_LOGE_STR("Failed to allocate one graph."); |
c0418dd9 JG |
211 | goto end; |
212 | } | |
213 | ||
3fea54f6 PP |
214 | bt_object_init_shared(&graph->base, bt_graph_destroy); |
215 | graph->connections = g_ptr_array_new_with_free_func( | |
216 | (GDestroyNotify) bt_object_try_spec_release); | |
c0418dd9 | 217 | if (!graph->connections) { |
262e5473 | 218 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
c0418dd9 JG |
219 | goto error; |
220 | } | |
3fea54f6 PP |
221 | graph->components = g_ptr_array_new_with_free_func( |
222 | (GDestroyNotify) bt_object_try_spec_release); | |
f60c8b34 | 223 | if (!graph->components) { |
262e5473 | 224 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
f60c8b34 JG |
225 | goto error; |
226 | } | |
227 | graph->sinks_to_consume = g_queue_new(); | |
228 | if (!graph->sinks_to_consume) { | |
262e5473 | 229 | BT_LOGE_STR("Failed to allocate one GQueue."); |
c0418dd9 JG |
230 | goto error; |
231 | } | |
1bf957a0 | 232 | |
ad847455 | 233 | bt_graph_set_can_consume(graph, BT_TRUE); |
1bf957a0 PP |
234 | ret = init_listeners_array(&graph->listeners.port_added); |
235 | if (ret) { | |
262e5473 | 236 | BT_LOGE_STR("Cannot create the \"port added\" listener array."); |
1bf957a0 PP |
237 | goto error; |
238 | } | |
239 | ||
240 | ret = init_listeners_array(&graph->listeners.port_removed); | |
241 | if (ret) { | |
262e5473 | 242 | BT_LOGE_STR("Cannot create the \"port removed\" listener array."); |
1bf957a0 PP |
243 | goto error; |
244 | } | |
245 | ||
f345f8bb | 246 | ret = init_listeners_array(&graph->listeners.ports_connected); |
1bf957a0 | 247 | if (ret) { |
262e5473 | 248 | BT_LOGE_STR("Cannot create the \"port connected\" listener array."); |
1bf957a0 PP |
249 | goto error; |
250 | } | |
251 | ||
f345f8bb | 252 | ret = init_listeners_array(&graph->listeners.ports_disconnected); |
1bf957a0 | 253 | if (ret) { |
262e5473 | 254 | BT_LOGE_STR("Cannot create the \"port disconneted\" listener array."); |
1bf957a0 PP |
255 | goto error; |
256 | } | |
257 | ||
5c563278 PP |
258 | ret = bt_object_pool_initialize(&graph->event_notif_pool, |
259 | (bt_object_pool_new_object_func) bt_notification_event_new, | |
260 | (bt_object_pool_destroy_object_func) destroy_notification_event, | |
261 | graph); | |
262 | if (ret) { | |
263 | BT_LOGE("Failed to initialize event notification pool: ret=%d", | |
264 | ret); | |
265 | goto error; | |
266 | } | |
267 | ||
268 | ret = bt_object_pool_initialize(&graph->packet_begin_notif_pool, | |
269 | (bt_object_pool_new_object_func) bt_notification_packet_begin_new, | |
270 | (bt_object_pool_destroy_object_func) destroy_notification_packet_begin, | |
271 | graph); | |
272 | if (ret) { | |
273 | BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d", | |
274 | ret); | |
275 | goto error; | |
276 | } | |
277 | ||
278 | ret = bt_object_pool_initialize(&graph->packet_end_notif_pool, | |
279 | (bt_object_pool_new_object_func) bt_notification_packet_end_new, | |
280 | (bt_object_pool_destroy_object_func) destroy_notification_packet_end, | |
281 | graph); | |
282 | if (ret) { | |
283 | BT_LOGE("Failed to initialize packet end notification pool: ret=%d", | |
284 | ret); | |
285 | goto error; | |
286 | } | |
287 | ||
288 | graph->notifications = g_ptr_array_new_with_free_func( | |
289 | (GDestroyNotify) notify_notification_graph_is_destroyed); | |
262e5473 PP |
290 | BT_LOGD("Created graph object: addr=%p", graph); |
291 | ||
c0418dd9 JG |
292 | end: |
293 | return graph; | |
294 | error: | |
295 | BT_PUT(graph); | |
296 | goto end; | |
297 | } | |
298 | ||
a256a42d PP |
299 | enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph, |
300 | struct bt_port *upstream_port, struct bt_port *downstream_port, | |
301 | struct bt_connection **user_connection) | |
f60c8b34 | 302 | { |
a256a42d | 303 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
304 | struct bt_connection *connection = NULL; |
305 | struct bt_graph *upstream_graph = NULL; | |
306 | struct bt_graph *downstream_graph = NULL; | |
307 | struct bt_component *upstream_component = NULL; | |
308 | struct bt_component *downstream_component = NULL; | |
309 | enum bt_component_status component_status; | |
ecbe9f49 | 310 | bt_bool init_can_consume; |
f60c8b34 | 311 | |
262e5473 PP |
312 | if (!graph) { |
313 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
a256a42d | 314 | status = BT_GRAPH_STATUS_INVALID; |
262e5473 PP |
315 | goto end; |
316 | } | |
4aa7981f | 317 | init_can_consume = graph->can_consume; |
262e5473 PP |
318 | |
319 | if (!upstream_port) { | |
320 | BT_LOGW_STR("Invalid parameter: upstream port is NULL."); | |
a256a42d | 321 | status = BT_GRAPH_STATUS_INVALID; |
262e5473 PP |
322 | goto end; |
323 | } | |
324 | ||
325 | if (!downstream_port) { | |
326 | BT_LOGW_STR("Invalid parameter: downstream port is NULL."); | |
a256a42d | 327 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
328 | goto end; |
329 | } | |
330 | ||
262e5473 PP |
331 | BT_LOGD("Connecting component ports within graph: " |
332 | "graph-addr=%p, " | |
333 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
334 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
335 | graph, upstream_port, bt_port_get_name(upstream_port), | |
336 | downstream_port, bt_port_get_name(downstream_port)); | |
337 | ||
202a3a13 | 338 | if (graph->canceled) { |
262e5473 | 339 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
a256a42d | 340 | status = BT_GRAPH_STATUS_CANCELED; |
202a3a13 PP |
341 | goto end; |
342 | } | |
343 | ||
ad847455 | 344 | bt_graph_set_can_consume(graph, BT_FALSE); |
1d915789 | 345 | |
72b913fb | 346 | /* Ensure appropriate types for upstream and downstream ports. */ |
f60c8b34 | 347 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
262e5473 | 348 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
a256a42d | 349 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
350 | goto end; |
351 | } | |
352 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { | |
262e5473 | 353 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
a256a42d | 354 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
355 | goto end; |
356 | } | |
357 | ||
72b913fb | 358 | /* Ensure that both ports are currently unconnected. */ |
0d8b4d8e | 359 | if (bt_port_is_connected(upstream_port)) { |
262e5473 | 360 | BT_LOGW_STR("Invalid parameter: upstream port is already connected."); |
a256a42d | 361 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
362 | goto end; |
363 | } | |
364 | ||
0d8b4d8e | 365 | if (bt_port_is_connected(downstream_port)) { |
262e5473 | 366 | BT_LOGW_STR("Invalid parameter: downstream port is already connected."); |
a256a42d | 367 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
368 | goto end; |
369 | } | |
370 | ||
371 | /* | |
372 | * Ensure that both ports are still attached to their creating | |
373 | * component. | |
374 | */ | |
f60c8b34 | 375 | upstream_component = bt_port_get_component(upstream_port); |
72b913fb | 376 | if (!upstream_component) { |
262e5473 | 377 | BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)"); |
a256a42d | 378 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
379 | goto end; |
380 | } | |
381 | ||
382 | downstream_component = bt_port_get_component(downstream_port); | |
383 | if (!downstream_component) { | |
262e5473 | 384 | BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)"); |
a256a42d | 385 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
386 | goto end; |
387 | } | |
388 | ||
262e5473 PP |
389 | BT_LOGD("Connecting component ports: " |
390 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
391 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\"", | |
392 | upstream_component, bt_component_get_name(upstream_component), | |
393 | downstream_component, bt_component_get_name(downstream_component)); | |
394 | ||
0d8b4d8e PP |
395 | /* |
396 | * At this point the ports are not connected yet. Both | |
397 | * components need to accept an eventual connection to their | |
398 | * port by the other port before we continue. | |
399 | */ | |
262e5473 | 400 | BT_LOGD_STR("Asking upstream component to accept the connection."); |
0d8b4d8e PP |
401 | component_status = bt_component_accept_port_connection( |
402 | upstream_component, upstream_port, downstream_port); | |
403 | if (component_status != BT_COMPONENT_STATUS_OK) { | |
262e5473 PP |
404 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
405 | BT_LOGD_STR("Upstream component refused the connection."); | |
406 | } else { | |
407 | BT_LOGW("Cannot ask upstream component to accept the connection: " | |
408 | "status=%s", bt_component_status_string(component_status)); | |
409 | } | |
410 | ||
a256a42d PP |
411 | status = bt_graph_status_from_component_status( |
412 | component_status); | |
413 | goto end; | |
0d8b4d8e | 414 | } |
262e5473 PP |
415 | |
416 | BT_LOGD_STR("Asking downstream component to accept the connection."); | |
0d8b4d8e PP |
417 | component_status = bt_component_accept_port_connection( |
418 | downstream_component, downstream_port, upstream_port); | |
419 | if (component_status != BT_COMPONENT_STATUS_OK) { | |
262e5473 PP |
420 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
421 | BT_LOGD_STR("Downstream component refused the connection."); | |
422 | } else { | |
423 | BT_LOGW("Cannot ask downstream component to accept the connection: " | |
424 | "status=%s", bt_component_status_string(component_status)); | |
425 | } | |
426 | ||
a256a42d PP |
427 | status = bt_graph_status_from_component_status( |
428 | component_status); | |
429 | goto end; | |
0d8b4d8e PP |
430 | } |
431 | ||
262e5473 | 432 | BT_LOGD_STR("Creating connection."); |
f60c8b34 JG |
433 | connection = bt_connection_create(graph, upstream_port, |
434 | downstream_port); | |
435 | if (!connection) { | |
262e5473 | 436 | BT_LOGW("Cannot create connection object."); |
a256a42d PP |
437 | status = BT_GRAPH_STATUS_NOMEM; |
438 | goto end; | |
f60c8b34 JG |
439 | } |
440 | ||
262e5473 PP |
441 | BT_LOGD("Connection object created: conn-addr=%p", connection); |
442 | ||
f60c8b34 | 443 | /* |
72b913fb PP |
444 | * Ownership of upstream_component/downstream_component and of |
445 | * the connection object is transferred to the graph. | |
f60c8b34 JG |
446 | */ |
447 | g_ptr_array_add(graph->connections, connection); | |
ffeb0eed | 448 | |
f60c8b34 | 449 | /* |
0d8b4d8e | 450 | * Notify both components that their port is connected. |
f60c8b34 | 451 | */ |
262e5473 | 452 | BT_LOGD_STR("Notifying upstream component that its port is connected."); |
0d8b4d8e PP |
453 | bt_component_port_connected(upstream_component, upstream_port, |
454 | downstream_port); | |
262e5473 | 455 | BT_LOGD_STR("Notifying downstream component that its port is connected."); |
0d8b4d8e PP |
456 | bt_component_port_connected(downstream_component, downstream_port, |
457 | upstream_port); | |
1bf957a0 PP |
458 | |
459 | /* | |
0d8b4d8e | 460 | * Notify the graph's creator that both ports are connected. |
1bf957a0 | 461 | */ |
262e5473 | 462 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
f345f8bb | 463 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
262e5473 PP |
464 | BT_LOGD("Connected component ports within graph: " |
465 | "graph-addr=%p, " | |
466 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
467 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
468 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
469 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
470 | graph, | |
471 | upstream_component, bt_component_get_name(upstream_component), | |
472 | downstream_component, bt_component_get_name(downstream_component), | |
473 | upstream_port, bt_port_get_name(upstream_port), | |
474 | downstream_port, bt_port_get_name(downstream_port)); | |
1bf957a0 | 475 | |
a256a42d | 476 | if (user_connection) { |
1a6a376a PP |
477 | /* Move reference to user */ |
478 | *user_connection = connection; | |
479 | connection = NULL; | |
a256a42d PP |
480 | } |
481 | ||
f60c8b34 JG |
482 | end: |
483 | bt_put(upstream_graph); | |
484 | bt_put(downstream_graph); | |
3eeacbb9 JG |
485 | bt_put(upstream_component); |
486 | bt_put(downstream_component); | |
a256a42d | 487 | bt_put(connection); |
4aa7981f | 488 | if (graph) { |
ad847455 PP |
489 | (void) init_can_consume; |
490 | bt_graph_set_can_consume(graph, init_can_consume); | |
4aa7981f | 491 | } |
a256a42d | 492 | return status; |
f60c8b34 JG |
493 | } |
494 | ||
ad847455 | 495 | static inline |
8ed535b5 | 496 | enum bt_graph_status consume_graph_sink(struct bt_component *sink) |
c0418dd9 | 497 | { |
72b913fb | 498 | enum bt_component_status comp_status; |
202a3a13 | 499 | |
f6ccaed9 | 500 | BT_ASSERT(sink); |
72b913fb | 501 | comp_status = bt_component_sink_consume(sink); |
8ed535b5 PP |
502 | BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s", |
503 | sink, bt_component_get_name(sink), | |
262e5473 | 504 | bt_component_status_string(comp_status)); |
ad847455 PP |
505 | BT_ASSERT_PRE(comp_status == BT_COMPONENT_STATUS_OK || |
506 | comp_status == BT_COMPONENT_STATUS_END || | |
507 | comp_status == BT_COMPONENT_STATUS_AGAIN || | |
508 | comp_status == BT_COMPONENT_STATUS_ERROR || | |
509 | comp_status == BT_COMPONENT_STATUS_NOMEM, | |
510 | "Invalid component status returned by consuming function: " | |
511 | "status=%s", bt_component_status_string(comp_status)); | |
512 | return (enum bt_graph_status) comp_status; | |
8ed535b5 PP |
513 | } |
514 | ||
515 | /* | |
516 | * `node` is removed from the queue of sinks to consume when passed to | |
517 | * this function. This function adds it back to the queue if there's | |
518 | * still something to consume afterwards. | |
519 | */ | |
ad847455 | 520 | static inline |
8ed535b5 PP |
521 | enum bt_graph_status consume_sink_node(struct bt_graph *graph, |
522 | GList *node) | |
523 | { | |
524 | enum bt_graph_status status; | |
525 | struct bt_component *sink; | |
526 | ||
527 | sink = node->data; | |
528 | status = consume_graph_sink(sink); | |
ad847455 | 529 | if (unlikely(status != BT_GRAPH_STATUS_END)) { |
8ed535b5 | 530 | g_queue_push_tail_link(graph->sinks_to_consume, node); |
f60c8b34 JG |
531 | goto end; |
532 | } | |
533 | ||
534 | /* End reached, the node is not added back to the queue and free'd. */ | |
8ed535b5 | 535 | g_queue_delete_link(graph->sinks_to_consume, node); |
f60c8b34 JG |
536 | |
537 | /* Don't forward an END status if there are sinks left to consume. */ | |
538 | if (!g_queue_is_empty(graph->sinks_to_consume)) { | |
539 | status = BT_GRAPH_STATUS_OK; | |
540 | goto end; | |
541 | } | |
8ed535b5 PP |
542 | |
543 | end: | |
544 | BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status)); | |
545 | return status; | |
546 | } | |
547 | ||
548 | BT_HIDDEN | |
549 | enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph, | |
550 | struct bt_component *sink) | |
551 | { | |
552 | enum bt_graph_status status; | |
553 | GList *sink_node; | |
554 | int index; | |
555 | ||
556 | BT_LOGV("Making specific sink consume: addr=%p, " | |
557 | "comp-addr=%p, comp-name=\"%s\"", | |
558 | graph, sink, bt_component_get_name(sink)); | |
559 | ||
f6ccaed9 | 560 | BT_ASSERT(bt_component_borrow_graph(sink) == graph); |
8ed535b5 PP |
561 | |
562 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
563 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); | |
564 | status = BT_GRAPH_STATUS_END; | |
565 | goto end; | |
566 | } | |
567 | ||
568 | index = g_queue_index(graph->sinks_to_consume, sink); | |
569 | if (index < 0) { | |
570 | BT_LOGV_STR("Sink is not marked as consumable: sink is ended."); | |
571 | status = BT_GRAPH_STATUS_END; | |
572 | goto end; | |
573 | } | |
574 | ||
575 | sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index); | |
f6ccaed9 | 576 | BT_ASSERT(sink_node); |
8ed535b5 PP |
577 | status = consume_sink_node(graph, sink_node); |
578 | ||
579 | end: | |
580 | return status; | |
581 | } | |
582 | ||
ad847455 | 583 | static inline |
8ed535b5 PP |
584 | enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph) |
585 | { | |
586 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; | |
587 | struct bt_component *sink; | |
588 | GList *current_node; | |
589 | ||
590 | BT_LOGV("Making next sink consume: addr=%p", graph); | |
f6ccaed9 PP |
591 | BT_ASSERT_PRE(graph->has_sink, |
592 | "Graph has no sink component: %!+g", graph); | |
8ed535b5 | 593 | |
ad847455 | 594 | if (unlikely(g_queue_is_empty(graph->sinks_to_consume))) { |
8ed535b5 PP |
595 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
596 | status = BT_GRAPH_STATUS_END; | |
597 | goto end; | |
598 | } | |
599 | ||
600 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); | |
601 | sink = current_node->data; | |
602 | BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"", | |
603 | sink, bt_component_get_name(sink)); | |
604 | status = consume_sink_node(graph, current_node); | |
605 | ||
f60c8b34 JG |
606 | end: |
607 | return status; | |
c0418dd9 JG |
608 | } |
609 | ||
851b70bd PP |
610 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph) |
611 | { | |
f6ccaed9 | 612 | enum bt_graph_status status; |
1d915789 | 613 | |
f6ccaed9 PP |
614 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
615 | BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph); | |
616 | BT_ASSERT_PRE(graph->can_consume, | |
617 | "Cannot consume graph in its current state: %!+g", graph); | |
ad847455 | 618 | bt_graph_set_can_consume(graph, BT_FALSE); |
851b70bd | 619 | status = bt_graph_consume_no_check(graph); |
ad847455 | 620 | bt_graph_set_can_consume(graph, BT_TRUE); |
26a15756 | 621 | return status; |
851b70bd PP |
622 | } |
623 | ||
72b913fb | 624 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
f60c8b34 | 625 | { |
72b913fb | 626 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
627 | |
628 | if (!graph) { | |
262e5473 | 629 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
72b913fb | 630 | status = BT_GRAPH_STATUS_INVALID; |
202a3a13 | 631 | goto end; |
f60c8b34 JG |
632 | } |
633 | ||
851b70bd PP |
634 | if (graph->canceled) { |
635 | BT_LOGW("Invalid parameter: graph is canceled: " | |
636 | "graph-addr=%p", graph); | |
637 | status = BT_GRAPH_STATUS_CANCELED; | |
638 | goto end; | |
639 | } | |
640 | ||
ad847455 PP |
641 | BT_ASSERT_PRE(graph->can_consume, |
642 | "Cannot consume graph in its current state: %!+g", graph); | |
643 | bt_graph_set_can_consume(graph, BT_FALSE); | |
262e5473 PP |
644 | BT_LOGV("Running graph: addr=%p", graph); |
645 | ||
f60c8b34 | 646 | do { |
851b70bd PP |
647 | /* |
648 | * Check if the graph is canceled at each iteration. If | |
649 | * the graph was canceled by another thread or by a | |
650 | * signal, this is not a warning nor an error, it was | |
651 | * intentional: log with a DEBUG level only. | |
652 | */ | |
ad847455 | 653 | if (unlikely(graph->canceled)) { |
851b70bd PP |
654 | BT_LOGD("Stopping the graph: graph is canceled: " |
655 | "graph-addr=%p", graph); | |
656 | status = BT_GRAPH_STATUS_CANCELED; | |
657 | goto end; | |
658 | } | |
659 | ||
4fef099a | 660 | status = bt_graph_consume_no_check(graph); |
ad847455 | 661 | if (unlikely(status == BT_GRAPH_STATUS_AGAIN)) { |
f60c8b34 | 662 | /* |
202a3a13 PP |
663 | * If AGAIN is received and there are multiple |
664 | * sinks, go ahead and consume from the next | |
665 | * sink. | |
f60c8b34 | 666 | * |
202a3a13 PP |
667 | * However, in the case where a single sink is |
668 | * left, the caller can decide to busy-wait and | |
669 | * call bt_graph_run() continuously until the | |
670 | * source is ready or it can decide to sleep for | |
671 | * an arbitrary amount of time. | |
f60c8b34 JG |
672 | */ |
673 | if (graph->sinks_to_consume->length > 1) { | |
72b913fb | 674 | status = BT_GRAPH_STATUS_OK; |
f60c8b34 | 675 | } |
2de524b3 PP |
676 | } else if (status == BT_GRAPH_STATUS_NO_SINK) { |
677 | goto end; | |
f60c8b34 | 678 | } |
72b913fb | 679 | } while (status == BT_GRAPH_STATUS_OK); |
f60c8b34 JG |
680 | |
681 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
72b913fb | 682 | status = BT_GRAPH_STATUS_END; |
f60c8b34 | 683 | } |
262e5473 | 684 | |
202a3a13 | 685 | end: |
262e5473 | 686 | BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status)); |
4aa7981f | 687 | if (graph) { |
ad847455 | 688 | bt_graph_set_can_consume(graph, BT_TRUE); |
4aa7981f | 689 | } |
72b913fb | 690 | return status; |
f60c8b34 | 691 | } |
1bf957a0 PP |
692 | |
693 | static | |
8cc092c9 | 694 | int add_listener(GArray *listeners, void *func, void *removed, void *data) |
1bf957a0 PP |
695 | { |
696 | struct bt_graph_listener listener = { | |
697 | .func = func, | |
8cc092c9 | 698 | .removed = removed, |
1bf957a0 PP |
699 | .data = data, |
700 | }; | |
701 | ||
702 | g_array_append_val(listeners, listener); | |
0d107cdd | 703 | return listeners->len - 1; |
1bf957a0 PP |
704 | } |
705 | ||
0d107cdd | 706 | int bt_graph_add_port_added_listener( |
1bf957a0 | 707 | struct bt_graph *graph, |
8cc092c9 PP |
708 | bt_graph_port_added_listener listener, |
709 | bt_graph_listener_removed listener_removed, void *data) | |
1bf957a0 | 710 | { |
0d107cdd | 711 | int ret; |
1bf957a0 | 712 | |
262e5473 PP |
713 | if (!graph) { |
714 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 715 | ret = -1; |
262e5473 PP |
716 | goto end; |
717 | } | |
718 | ||
8cc092c9 PP |
719 | if (graph->in_remove_listener) { |
720 | BT_LOGW("Cannot call this function during the execution of a remove listener: " | |
721 | "addr=%p", graph); | |
722 | ret = -1; | |
723 | goto end; | |
724 | } | |
725 | ||
262e5473 PP |
726 | if (!listener) { |
727 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 728 | ret = -1; |
1bf957a0 PP |
729 | goto end; |
730 | } | |
731 | ||
8cc092c9 PP |
732 | ret = add_listener(graph->listeners.port_added, listener, |
733 | listener_removed, data); | |
262e5473 | 734 | BT_LOGV("Added \"port added\" listener to graph: " |
0d107cdd PP |
735 | "graph-addr=%p, listener-addr=%p, pos=%d", |
736 | graph, listener, ret); | |
1bf957a0 PP |
737 | |
738 | end: | |
0d107cdd | 739 | return ret; |
1bf957a0 PP |
740 | } |
741 | ||
0d107cdd | 742 | int bt_graph_add_port_removed_listener( |
1bf957a0 | 743 | struct bt_graph *graph, |
8cc092c9 PP |
744 | bt_graph_port_removed_listener listener, |
745 | bt_graph_listener_removed listener_removed, void *data) | |
1bf957a0 | 746 | { |
0d107cdd | 747 | int ret; |
1bf957a0 | 748 | |
262e5473 PP |
749 | if (!graph) { |
750 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 751 | ret = -1; |
262e5473 PP |
752 | goto end; |
753 | } | |
754 | ||
8cc092c9 PP |
755 | if (graph->in_remove_listener) { |
756 | BT_LOGW("Cannot call this function during the execution of a remove listener: " | |
757 | "addr=%p", graph); | |
758 | ret = -1; | |
759 | goto end; | |
760 | } | |
761 | ||
262e5473 PP |
762 | if (!listener) { |
763 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 764 | ret = -1; |
1bf957a0 PP |
765 | goto end; |
766 | } | |
767 | ||
8cc092c9 PP |
768 | ret = add_listener(graph->listeners.port_removed, listener, |
769 | listener_removed, data); | |
262e5473 | 770 | BT_LOGV("Added \"port removed\" listener to graph: " |
0d107cdd PP |
771 | "graph-addr=%p, listener-addr=%p, pos=%d", |
772 | graph, listener, ret); | |
1bf957a0 PP |
773 | |
774 | end: | |
0d107cdd | 775 | return ret; |
1bf957a0 PP |
776 | } |
777 | ||
0d107cdd | 778 | int bt_graph_add_ports_connected_listener( |
1bf957a0 | 779 | struct bt_graph *graph, |
8cc092c9 PP |
780 | bt_graph_ports_connected_listener listener, |
781 | bt_graph_listener_removed listener_removed, void *data) | |
1bf957a0 | 782 | { |
0d107cdd | 783 | int ret; |
1bf957a0 | 784 | |
262e5473 PP |
785 | if (!graph) { |
786 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 787 | ret = -1; |
262e5473 PP |
788 | goto end; |
789 | } | |
790 | ||
8cc092c9 PP |
791 | if (graph->in_remove_listener) { |
792 | BT_LOGW("Cannot call this function during the execution of a remove listener: " | |
793 | "addr=%p", graph); | |
794 | ret = -1; | |
795 | goto end; | |
796 | } | |
797 | ||
262e5473 PP |
798 | if (!listener) { |
799 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 800 | ret = -1; |
1bf957a0 PP |
801 | goto end; |
802 | } | |
803 | ||
8cc092c9 PP |
804 | ret = add_listener(graph->listeners.ports_connected, listener, |
805 | listener_removed, data); | |
262e5473 | 806 | BT_LOGV("Added \"port connected\" listener to graph: " |
0d107cdd PP |
807 | "graph-addr=%p, listener-addr=%p, pos=%d", |
808 | graph, listener, ret); | |
1bf957a0 PP |
809 | |
810 | end: | |
0d107cdd | 811 | return ret; |
1bf957a0 PP |
812 | } |
813 | ||
0d107cdd | 814 | int bt_graph_add_ports_disconnected_listener( |
1bf957a0 | 815 | struct bt_graph *graph, |
8cc092c9 PP |
816 | bt_graph_ports_disconnected_listener listener, |
817 | bt_graph_listener_removed listener_removed, void *data) | |
1bf957a0 | 818 | { |
0d107cdd | 819 | int ret; |
1bf957a0 | 820 | |
262e5473 PP |
821 | if (!graph) { |
822 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 823 | ret = -1; |
262e5473 PP |
824 | goto end; |
825 | } | |
826 | ||
8cc092c9 PP |
827 | if (graph->in_remove_listener) { |
828 | BT_LOGW("Cannot call this function during the execution of a remove listener: " | |
829 | "addr=%p", graph); | |
830 | ret = -1; | |
831 | goto end; | |
832 | } | |
833 | ||
262e5473 PP |
834 | if (!listener) { |
835 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 836 | ret = -1; |
1bf957a0 PP |
837 | goto end; |
838 | } | |
839 | ||
8cc092c9 PP |
840 | ret = add_listener(graph->listeners.ports_disconnected, listener, |
841 | listener_removed, data); | |
262e5473 | 842 | BT_LOGV("Added \"port disconnected\" listener to graph: " |
0d107cdd PP |
843 | "graph-addr=%p, listener-addr=%p, pos=%d", |
844 | graph, listener, ret); | |
1bf957a0 PP |
845 | |
846 | end: | |
0d107cdd | 847 | return ret; |
1bf957a0 PP |
848 | } |
849 | ||
850 | BT_HIDDEN | |
851 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) | |
852 | { | |
853 | size_t i; | |
854 | ||
262e5473 PP |
855 | BT_LOGV("Notifying graph listeners that a port was added: " |
856 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", | |
857 | graph, port, bt_port_get_name(port)); | |
858 | ||
1bf957a0 PP |
859 | for (i = 0; i < graph->listeners.port_added->len; i++) { |
860 | struct bt_graph_listener listener = | |
861 | g_array_index(graph->listeners.port_added, | |
862 | struct bt_graph_listener, i); | |
863 | bt_graph_port_added_listener func = listener.func; | |
864 | ||
f6ccaed9 | 865 | BT_ASSERT(func); |
1bf957a0 PP |
866 | func(port, listener.data); |
867 | } | |
868 | } | |
869 | ||
870 | BT_HIDDEN | |
871 | void bt_graph_notify_port_removed(struct bt_graph *graph, | |
872 | struct bt_component *comp, struct bt_port *port) | |
873 | { | |
874 | size_t i; | |
875 | ||
262e5473 PP |
876 | BT_LOGV("Notifying graph listeners that a port was removed: " |
877 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", | |
878 | graph, port, bt_port_get_name(port)); | |
879 | ||
1bf957a0 PP |
880 | for (i = 0; i < graph->listeners.port_removed->len; i++) { |
881 | struct bt_graph_listener listener = | |
882 | g_array_index(graph->listeners.port_removed, | |
883 | struct bt_graph_listener, i); | |
884 | bt_graph_port_removed_listener func = listener.func; | |
885 | ||
f6ccaed9 | 886 | BT_ASSERT(func); |
1bf957a0 PP |
887 | func(comp, port, listener.data); |
888 | } | |
889 | } | |
890 | ||
891 | BT_HIDDEN | |
f345f8bb PP |
892 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
893 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 PP |
894 | { |
895 | size_t i; | |
896 | ||
262e5473 PP |
897 | BT_LOGV("Notifying graph listeners that two ports were connected: " |
898 | "graph-addr=%p, " | |
899 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
900 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
901 | graph, upstream_port, bt_port_get_name(upstream_port), | |
902 | downstream_port, bt_port_get_name(downstream_port)); | |
903 | ||
f345f8bb | 904 | for (i = 0; i < graph->listeners.ports_connected->len; i++) { |
1bf957a0 | 905 | struct bt_graph_listener listener = |
f345f8bb | 906 | g_array_index(graph->listeners.ports_connected, |
1bf957a0 | 907 | struct bt_graph_listener, i); |
f345f8bb | 908 | bt_graph_ports_connected_listener func = listener.func; |
1bf957a0 | 909 | |
f6ccaed9 | 910 | BT_ASSERT(func); |
f345f8bb | 911 | func(upstream_port, downstream_port, listener.data); |
1bf957a0 PP |
912 | } |
913 | } | |
914 | ||
915 | BT_HIDDEN | |
f345f8bb PP |
916 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
917 | struct bt_component *upstream_comp, | |
918 | struct bt_component *downstream_comp, | |
919 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 PP |
920 | { |
921 | size_t i; | |
922 | ||
262e5473 PP |
923 | BT_LOGV("Notifying graph listeners that two ports were disconnected: " |
924 | "graph-addr=%p, " | |
925 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
926 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
927 | graph, upstream_port, bt_port_get_name(upstream_port), | |
928 | downstream_port, bt_port_get_name(downstream_port)); | |
929 | ||
f345f8bb | 930 | for (i = 0; i < graph->listeners.ports_disconnected->len; i++) { |
1bf957a0 | 931 | struct bt_graph_listener listener = |
f345f8bb | 932 | g_array_index(graph->listeners.ports_disconnected, |
1bf957a0 | 933 | struct bt_graph_listener, i); |
f345f8bb | 934 | bt_graph_ports_disconnected_listener func = listener.func; |
1bf957a0 | 935 | |
f6ccaed9 | 936 | BT_ASSERT(func); |
f345f8bb PP |
937 | func(upstream_comp, downstream_comp, upstream_port, |
938 | downstream_port, listener.data); | |
1bf957a0 PP |
939 | } |
940 | } | |
202a3a13 | 941 | |
c7eee084 | 942 | enum bt_graph_status bt_graph_cancel(struct bt_graph *graph) |
202a3a13 PP |
943 | { |
944 | enum bt_graph_status ret = BT_GRAPH_STATUS_OK; | |
945 | ||
946 | if (!graph) { | |
262e5473 | 947 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
202a3a13 PP |
948 | ret = BT_GRAPH_STATUS_INVALID; |
949 | goto end; | |
950 | } | |
951 | ||
952 | graph->canceled = BT_TRUE; | |
262e5473 | 953 | BT_LOGV("Canceled graph: addr=%p", graph); |
202a3a13 PP |
954 | |
955 | end: | |
956 | return ret; | |
957 | } | |
958 | ||
c7eee084 | 959 | bt_bool bt_graph_is_canceled(struct bt_graph *graph) |
202a3a13 | 960 | { |
c7eee084 PP |
961 | bt_bool canceled = BT_FALSE; |
962 | ||
963 | if (!graph) { | |
964 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
965 | goto end; | |
966 | } | |
967 | ||
968 | canceled = graph->canceled; | |
969 | ||
970 | end: | |
971 | return canceled; | |
202a3a13 | 972 | } |
f167d3c0 PP |
973 | |
974 | BT_HIDDEN | |
975 | void bt_graph_remove_connection(struct bt_graph *graph, | |
976 | struct bt_connection *connection) | |
977 | { | |
f6ccaed9 PP |
978 | BT_ASSERT(graph); |
979 | BT_ASSERT(connection); | |
262e5473 PP |
980 | BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p", |
981 | graph, connection); | |
f167d3c0 PP |
982 | g_ptr_array_remove(graph->connections, connection); |
983 | } | |
36712f1d PP |
984 | |
985 | enum bt_graph_status bt_graph_add_component_with_init_method_data( | |
986 | struct bt_graph *graph, | |
987 | struct bt_component_class *component_class, | |
988 | const char *name, struct bt_value *params, | |
989 | void *init_method_data, | |
990 | struct bt_component **user_component) | |
991 | { | |
992 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; | |
993 | enum bt_component_status comp_status; | |
994 | struct bt_component *component = NULL; | |
995 | enum bt_component_class_type type; | |
996 | size_t i; | |
4aa7981f | 997 | bt_bool init_can_consume; |
36712f1d PP |
998 | |
999 | bt_get(params); | |
1000 | ||
1001 | if (!graph) { | |
1002 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
1003 | graph_status = BT_GRAPH_STATUS_INVALID; | |
1004 | goto end; | |
1005 | } | |
4aa7981f | 1006 | init_can_consume = graph->can_consume; |
36712f1d PP |
1007 | |
1008 | if (!component_class) { | |
1009 | BT_LOGW_STR("Invalid parameter: component class is NULL."); | |
1010 | graph_status = BT_GRAPH_STATUS_INVALID; | |
1011 | goto end; | |
1012 | } | |
1013 | ||
1d915789 | 1014 | graph->can_consume = BT_FALSE; |
36712f1d PP |
1015 | type = bt_component_class_get_type(component_class); |
1016 | BT_LOGD("Adding component to graph: " | |
1017 | "graph-addr=%p, comp-cls-addr=%p, " | |
1018 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " | |
1019 | "init-method-data-addr=%p", | |
1020 | graph, component_class, bt_component_class_type_string(type), | |
1021 | name, params, init_method_data); | |
1022 | ||
1023 | if (!name) { | |
1024 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
1025 | graph_status = BT_GRAPH_STATUS_INVALID; | |
1026 | goto end; | |
1027 | } | |
1028 | ||
1029 | if (graph->canceled) { | |
1030 | BT_LOGW_STR("Invalid parameter: graph is canceled."); | |
1031 | graph_status = BT_GRAPH_STATUS_CANCELED; | |
1032 | goto end; | |
1033 | } | |
1034 | ||
1035 | if (type != BT_COMPONENT_CLASS_TYPE_SOURCE && | |
1036 | type != BT_COMPONENT_CLASS_TYPE_FILTER && | |
1037 | type != BT_COMPONENT_CLASS_TYPE_SINK) { | |
1038 | BT_LOGW("Invalid parameter: unknown component class type: " | |
1039 | "type=%d", type); | |
1040 | graph_status = BT_GRAPH_STATUS_INVALID; | |
1041 | goto end; | |
1042 | } | |
1043 | ||
1044 | for (i = 0; i < graph->components->len; i++) { | |
1045 | void *other_comp = graph->components->pdata[i]; | |
1046 | ||
1047 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { | |
1048 | BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: " | |
1049 | "other-comp-addr=%p, name=\"%s\"", | |
1050 | other_comp, name); | |
1051 | graph_status = BT_GRAPH_STATUS_INVALID; | |
1052 | goto end; | |
1053 | } | |
1054 | } | |
1055 | ||
1056 | /* | |
1057 | * Parameters must be a map value, but we create a convenient | |
1058 | * empty one if it's NULL. | |
1059 | */ | |
1060 | if (params) { | |
1061 | if (!bt_value_is_map(params)) { | |
1062 | BT_LOGW("Invalid parameter: initialization parameters must be a map value: " | |
1063 | "type=%s", | |
1064 | bt_value_type_string(bt_value_get_type(params))); | |
1065 | graph_status = BT_GRAPH_STATUS_INVALID; | |
1066 | goto end; | |
1067 | } | |
1068 | } else { | |
1069 | params = bt_value_map_create(); | |
1070 | if (!params) { | |
1071 | BT_LOGE_STR("Cannot create map value object."); | |
1072 | graph_status = BT_GRAPH_STATUS_NOMEM; | |
1073 | goto end; | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | comp_status = bt_component_create(component_class, name, &component); | |
1078 | if (comp_status != BT_COMPONENT_STATUS_OK) { | |
1079 | BT_LOGE("Cannot create empty component object: status=%s", | |
1080 | bt_component_status_string(comp_status)); | |
1081 | graph_status = bt_graph_status_from_component_status( | |
1082 | comp_status); | |
1083 | goto end; | |
1084 | } | |
1085 | ||
1086 | /* | |
1087 | * The user's initialization method needs to see that this | |
1088 | * component is part of the graph. If the user method fails, we | |
1089 | * immediately remove the component from the graph's components. | |
1090 | */ | |
1091 | g_ptr_array_add(graph->components, component); | |
1092 | bt_component_set_graph(component, graph); | |
1093 | ||
1094 | if (component_class->methods.init) { | |
1095 | BT_LOGD_STR("Calling user's initialization method."); | |
1096 | comp_status = component_class->methods.init( | |
1097 | bt_private_component_from_component(component), params, | |
1098 | init_method_data); | |
1099 | BT_LOGD("User method returned: status=%s", | |
1100 | bt_component_status_string(comp_status)); | |
1101 | if (comp_status != BT_COMPONENT_STATUS_OK) { | |
1102 | BT_LOGW_STR("Initialization method failed."); | |
1103 | graph_status = bt_graph_status_from_component_status( | |
1104 | comp_status); | |
1105 | bt_component_set_graph(component, NULL); | |
1106 | g_ptr_array_remove_fast(graph->components, component); | |
1107 | goto end; | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | /* | |
1112 | * Mark the component as initialized so that its finalization | |
1113 | * method is called when it is destroyed. | |
1114 | */ | |
1115 | component->initialized = true; | |
1116 | ||
1117 | /* | |
1118 | * If it's a sink component, it needs to be part of the graph's | |
1119 | * sink queue to be consumed by bt_graph_consume(). | |
1120 | */ | |
1121 | if (bt_component_is_sink(component)) { | |
2de524b3 | 1122 | graph->has_sink = BT_TRUE; |
36712f1d PP |
1123 | g_queue_push_tail(graph->sinks_to_consume, component); |
1124 | } | |
1125 | ||
1126 | /* | |
1127 | * Freeze the component class now that it's instantiated at | |
1128 | * least once. | |
1129 | */ | |
1130 | BT_LOGD_STR("Freezing component class."); | |
1131 | bt_component_class_freeze(component->class); | |
1132 | BT_LOGD("Added component to graph: " | |
1133 | "graph-addr=%p, comp-cls-addr=%p, " | |
1134 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " | |
1135 | "init-method-data-addr=%p, comp-addr=%p", | |
1136 | graph, component_class, bt_component_class_type_string(type), | |
1137 | name, params, init_method_data, component); | |
1138 | ||
1139 | if (user_component) { | |
1140 | /* Move reference to user */ | |
1141 | *user_component = component; | |
1142 | component = NULL; | |
1143 | } | |
1144 | ||
1145 | end: | |
1146 | bt_put(component); | |
1147 | bt_put(params); | |
4aa7981f JG |
1148 | if (graph) { |
1149 | graph->can_consume = init_can_consume; | |
1150 | } | |
36712f1d PP |
1151 | return graph_status; |
1152 | } | |
1153 | ||
1154 | enum bt_graph_status bt_graph_add_component( | |
1155 | struct bt_graph *graph, | |
1156 | struct bt_component_class *component_class, | |
1157 | const char *name, struct bt_value *params, | |
1158 | struct bt_component **component) | |
1159 | { | |
1160 | return bt_graph_add_component_with_init_method_data(graph, | |
1161 | component_class, name, params, NULL, component); | |
1162 | } | |
8ed535b5 PP |
1163 | |
1164 | BT_HIDDEN | |
1165 | int bt_graph_remove_unconnected_component(struct bt_graph *graph, | |
1166 | struct bt_component *component) | |
1167 | { | |
4aa7981f | 1168 | bt_bool init_can_consume; |
8ed535b5 PP |
1169 | int64_t count; |
1170 | uint64_t i; | |
1171 | int ret = 0; | |
1172 | ||
f6ccaed9 PP |
1173 | BT_ASSERT(graph); |
1174 | BT_ASSERT(component); | |
3fea54f6 | 1175 | BT_ASSERT(component->base.ref_count == 0); |
f6ccaed9 | 1176 | BT_ASSERT(bt_component_borrow_graph(component) == graph); |
8ed535b5 | 1177 | |
4aa7981f | 1178 | init_can_consume = graph->can_consume; |
8ed535b5 PP |
1179 | count = bt_component_get_input_port_count(component); |
1180 | ||
1181 | for (i = 0; i < count; i++) { | |
1182 | struct bt_port *port = | |
1183 | bt_component_get_input_port_by_index(component, i); | |
1184 | ||
f6ccaed9 | 1185 | BT_ASSERT(port); |
8ed535b5 PP |
1186 | bt_put(port); |
1187 | ||
1188 | if (bt_port_is_connected(port)) { | |
1189 | BT_LOGW("Cannot remove component from graph: " | |
1190 | "an input port is connected: " | |
1191 | "graph-addr=%p, comp-addr=%p, " | |
1192 | "comp-name=\"%s\", connected-port-addr=%p, " | |
1193 | "connected-port-name=\"%s\"", | |
1194 | graph, component, | |
1195 | bt_component_get_name(component), | |
1196 | port, bt_port_get_name(port)); | |
1197 | goto error; | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | count = bt_component_get_output_port_count(component); | |
1202 | ||
1203 | for (i = 0; i < count; i++) { | |
1204 | struct bt_port *port = | |
1205 | bt_component_get_output_port_by_index(component, i); | |
1206 | ||
f6ccaed9 | 1207 | BT_ASSERT(port); |
8ed535b5 PP |
1208 | bt_put(port); |
1209 | ||
1210 | if (bt_port_is_connected(port)) { | |
1211 | BT_LOGW("Cannot remove component from graph: " | |
1212 | "an output port is connected: " | |
1213 | "graph-addr=%p, comp-addr=%p, " | |
1214 | "comp-name=\"%s\", connected-port-addr=%p, " | |
1215 | "connected-port-name=\"%s\"", | |
1216 | graph, component, | |
1217 | bt_component_get_name(component), | |
1218 | port, bt_port_get_name(port)); | |
1219 | goto error; | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | graph->can_consume = BT_FALSE; | |
1224 | ||
1225 | /* Possibly remove from sinks to consume */ | |
1226 | (void) g_queue_remove(graph->sinks_to_consume, component); | |
1227 | ||
1228 | if (graph->sinks_to_consume->length == 0) { | |
1229 | graph->has_sink = BT_FALSE; | |
1230 | } | |
1231 | ||
1232 | /* | |
3fea54f6 PP |
1233 | * This calls bt_object_try_spec_release() on the component, and |
1234 | * since its reference count is 0, its destructor is called. Its | |
8ed535b5 PP |
1235 | * destructor calls the user's finalization method (if set). |
1236 | */ | |
1237 | g_ptr_array_remove(graph->components, component); | |
1238 | goto end; | |
1239 | ||
1240 | error: | |
1241 | ret = -1; | |
1242 | ||
1243 | end: | |
1244 | graph->can_consume = init_can_consume; | |
1245 | return ret; | |
1246 | } | |
5c563278 PP |
1247 | |
1248 | BT_HIDDEN | |
1249 | void bt_graph_add_notification(struct bt_graph *graph, | |
1250 | struct bt_notification *notif) | |
1251 | { | |
1252 | BT_ASSERT(graph); | |
1253 | BT_ASSERT(notif); | |
1254 | ||
1255 | /* | |
1256 | * It's okay not to take a reference because, when a | |
1257 | * notification's reference count drops to 0, either: | |
1258 | * | |
1259 | * * It is recycled back to one of this graph's pool. | |
1260 | * * It is destroyed because it doesn't have any link to any | |
1261 | * graph, which means the original graph is already destroyed. | |
1262 | */ | |
1263 | g_ptr_array_add(graph->notifications, notif); | |
1264 | } |