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