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> | |
3d9990ac | 38 | #include <babeltrace/compiler-internal.h> |
c55a9f58 | 39 | #include <babeltrace/types.h> |
36712f1d PP |
40 | #include <babeltrace/values.h> |
41 | #include <babeltrace/values-internal.h> | |
f60c8b34 | 42 | #include <unistd.h> |
1bf957a0 PP |
43 | #include <glib.h> |
44 | ||
45 | struct bt_graph_listener { | |
46 | void *func; | |
47 | void *data; | |
48 | }; | |
c0418dd9 | 49 | |
f60c8b34 JG |
50 | static |
51 | void bt_graph_destroy(struct bt_object *obj) | |
c0418dd9 | 52 | { |
f60c8b34 JG |
53 | struct bt_graph *graph = container_of(obj, |
54 | struct bt_graph, base); | |
c0418dd9 | 55 | |
bd14d768 PP |
56 | /* |
57 | * The graph's reference count is 0 if we're here. Increment | |
58 | * it to avoid a double-destroy (possibly infinitely recursive) | |
59 | * in this situation: | |
60 | * | |
61 | * 1. We put and destroy a connection. | |
62 | * 2. This connection's destructor finalizes its active | |
63 | * notification iterators. | |
64 | * 3. A notification iterator's finalization function gets a | |
65 | * new reference on its component (reference count goes from | |
66 | * 0 to 1). | |
67 | * 4. Since this component's reference count goes to 1, it takes | |
68 | * a reference on its parent (this graph). This graph's | |
69 | * reference count goes from 0 to 1. | |
70 | * 5. The notification iterator's finalization function puts its | |
71 | * component reference (reference count goes from 1 to 0). | |
72 | * 6. Since this component's reference count goes from 1 to 0, | |
73 | * it puts its parent (this graph). This graph's reference | |
74 | * count goes from 1 to 0. | |
75 | * 7. Since this graph's reference count goes from 1 to 0, | |
76 | * its destructor is called (this function). | |
77 | * | |
78 | * With the incrementation below, the graph's reference count at | |
79 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This | |
80 | * ensures that this function is not called two times. | |
81 | */ | |
262e5473 | 82 | BT_LOGD("Destroying graph: addr=%p", graph); |
bd14d768 PP |
83 | obj->ref_count.count++; |
84 | ||
49682acd PP |
85 | /* |
86 | * Cancel the graph to disallow some operations, like creating | |
87 | * notification iterators and adding ports to components. | |
88 | */ | |
89 | (void) bt_graph_cancel(graph); | |
90 | ||
c0418dd9 | 91 | if (graph->connections) { |
262e5473 | 92 | BT_LOGD_STR("Destroying connections."); |
c0418dd9 JG |
93 | g_ptr_array_free(graph->connections, TRUE); |
94 | } | |
bd14d768 | 95 | if (graph->components) { |
262e5473 | 96 | BT_LOGD_STR("Destroying components."); |
bd14d768 PP |
97 | g_ptr_array_free(graph->components, TRUE); |
98 | } | |
f60c8b34 JG |
99 | if (graph->sinks_to_consume) { |
100 | g_queue_free(graph->sinks_to_consume); | |
c0418dd9 | 101 | } |
1bf957a0 PP |
102 | |
103 | if (graph->listeners.port_added) { | |
104 | g_array_free(graph->listeners.port_added, TRUE); | |
105 | } | |
106 | ||
107 | if (graph->listeners.port_removed) { | |
108 | g_array_free(graph->listeners.port_removed, TRUE); | |
109 | } | |
110 | ||
f345f8bb PP |
111 | if (graph->listeners.ports_connected) { |
112 | g_array_free(graph->listeners.ports_connected, TRUE); | |
1bf957a0 PP |
113 | } |
114 | ||
f345f8bb PP |
115 | if (graph->listeners.ports_disconnected) { |
116 | g_array_free(graph->listeners.ports_disconnected, TRUE); | |
1bf957a0 PP |
117 | } |
118 | ||
c0418dd9 JG |
119 | g_free(graph); |
120 | } | |
121 | ||
1bf957a0 PP |
122 | static |
123 | int init_listeners_array(GArray **listeners) | |
124 | { | |
125 | int ret = 0; | |
126 | ||
127 | assert(listeners); | |
128 | *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener)); | |
129 | if (!*listeners) { | |
262e5473 | 130 | BT_LOGE_STR("Failed to allocate one GArray."); |
1bf957a0 PP |
131 | ret = -1; |
132 | goto end; | |
133 | } | |
134 | ||
135 | end: | |
136 | return ret; | |
137 | } | |
138 | ||
f60c8b34 | 139 | struct bt_graph *bt_graph_create(void) |
c0418dd9 | 140 | { |
f60c8b34 | 141 | struct bt_graph *graph; |
1bf957a0 | 142 | int ret; |
c0418dd9 | 143 | |
262e5473 | 144 | BT_LOGD_STR("Creating graph object."); |
f60c8b34 | 145 | graph = g_new0(struct bt_graph, 1); |
c0418dd9 | 146 | if (!graph) { |
262e5473 | 147 | BT_LOGE_STR("Failed to allocate one graph."); |
c0418dd9 JG |
148 | goto end; |
149 | } | |
150 | ||
f60c8b34 | 151 | bt_object_init(graph, bt_graph_destroy); |
c0418dd9 | 152 | |
f60c8b34 | 153 | graph->connections = g_ptr_array_new_with_free_func(bt_object_release); |
c0418dd9 | 154 | if (!graph->connections) { |
262e5473 | 155 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
c0418dd9 JG |
156 | goto error; |
157 | } | |
f60c8b34 JG |
158 | graph->components = g_ptr_array_new_with_free_func(bt_object_release); |
159 | if (!graph->components) { | |
262e5473 | 160 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
f60c8b34 JG |
161 | goto error; |
162 | } | |
163 | graph->sinks_to_consume = g_queue_new(); | |
164 | if (!graph->sinks_to_consume) { | |
262e5473 | 165 | BT_LOGE_STR("Failed to allocate one GQueue."); |
c0418dd9 JG |
166 | goto error; |
167 | } | |
1bf957a0 PP |
168 | |
169 | ret = init_listeners_array(&graph->listeners.port_added); | |
170 | if (ret) { | |
262e5473 | 171 | BT_LOGE_STR("Cannot create the \"port added\" listener array."); |
1bf957a0 PP |
172 | goto error; |
173 | } | |
174 | ||
175 | ret = init_listeners_array(&graph->listeners.port_removed); | |
176 | if (ret) { | |
262e5473 | 177 | BT_LOGE_STR("Cannot create the \"port removed\" listener array."); |
1bf957a0 PP |
178 | goto error; |
179 | } | |
180 | ||
f345f8bb | 181 | ret = init_listeners_array(&graph->listeners.ports_connected); |
1bf957a0 | 182 | if (ret) { |
262e5473 | 183 | BT_LOGE_STR("Cannot create the \"port connected\" listener array."); |
1bf957a0 PP |
184 | goto error; |
185 | } | |
186 | ||
f345f8bb | 187 | ret = init_listeners_array(&graph->listeners.ports_disconnected); |
1bf957a0 | 188 | if (ret) { |
262e5473 | 189 | BT_LOGE_STR("Cannot create the \"port disconneted\" listener array."); |
1bf957a0 PP |
190 | goto error; |
191 | } | |
192 | ||
262e5473 PP |
193 | BT_LOGD("Created graph object: addr=%p", graph); |
194 | ||
c0418dd9 JG |
195 | end: |
196 | return graph; | |
197 | error: | |
198 | BT_PUT(graph); | |
199 | goto end; | |
200 | } | |
201 | ||
a256a42d PP |
202 | enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph, |
203 | struct bt_port *upstream_port, struct bt_port *downstream_port, | |
204 | struct bt_connection **user_connection) | |
f60c8b34 | 205 | { |
a256a42d | 206 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
207 | struct bt_connection *connection = NULL; |
208 | struct bt_graph *upstream_graph = NULL; | |
209 | struct bt_graph *downstream_graph = NULL; | |
210 | struct bt_component *upstream_component = NULL; | |
211 | struct bt_component *downstream_component = NULL; | |
212 | enum bt_component_status component_status; | |
f60c8b34 | 213 | |
262e5473 PP |
214 | if (!graph) { |
215 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
a256a42d | 216 | status = BT_GRAPH_STATUS_INVALID; |
262e5473 PP |
217 | goto end; |
218 | } | |
219 | ||
220 | if (!upstream_port) { | |
221 | BT_LOGW_STR("Invalid parameter: upstream port is NULL."); | |
a256a42d | 222 | status = BT_GRAPH_STATUS_INVALID; |
262e5473 PP |
223 | goto end; |
224 | } | |
225 | ||
226 | if (!downstream_port) { | |
227 | BT_LOGW_STR("Invalid parameter: downstream port is NULL."); | |
a256a42d | 228 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
229 | goto end; |
230 | } | |
231 | ||
262e5473 PP |
232 | BT_LOGD("Connecting component ports within graph: " |
233 | "graph-addr=%p, " | |
234 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
235 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
236 | graph, upstream_port, bt_port_get_name(upstream_port), | |
237 | downstream_port, bt_port_get_name(downstream_port)); | |
238 | ||
202a3a13 | 239 | if (graph->canceled) { |
262e5473 | 240 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
a256a42d | 241 | status = BT_GRAPH_STATUS_CANCELED; |
202a3a13 PP |
242 | goto end; |
243 | } | |
244 | ||
72b913fb | 245 | /* Ensure appropriate types for upstream and downstream ports. */ |
f60c8b34 | 246 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
262e5473 | 247 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
a256a42d | 248 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
249 | goto end; |
250 | } | |
251 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { | |
262e5473 | 252 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
a256a42d | 253 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
254 | goto end; |
255 | } | |
256 | ||
72b913fb | 257 | /* Ensure that both ports are currently unconnected. */ |
0d8b4d8e | 258 | if (bt_port_is_connected(upstream_port)) { |
262e5473 | 259 | BT_LOGW_STR("Invalid parameter: upstream port is already connected."); |
a256a42d | 260 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
261 | goto end; |
262 | } | |
263 | ||
0d8b4d8e | 264 | if (bt_port_is_connected(downstream_port)) { |
262e5473 | 265 | BT_LOGW_STR("Invalid parameter: downstream port is already connected."); |
a256a42d | 266 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
267 | goto end; |
268 | } | |
269 | ||
270 | /* | |
271 | * Ensure that both ports are still attached to their creating | |
272 | * component. | |
273 | */ | |
f60c8b34 | 274 | upstream_component = bt_port_get_component(upstream_port); |
72b913fb | 275 | if (!upstream_component) { |
262e5473 | 276 | BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)"); |
a256a42d | 277 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
278 | goto end; |
279 | } | |
280 | ||
281 | downstream_component = bt_port_get_component(downstream_port); | |
282 | if (!downstream_component) { | |
262e5473 | 283 | BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)"); |
a256a42d | 284 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
285 | goto end; |
286 | } | |
287 | ||
262e5473 PP |
288 | BT_LOGD("Connecting component ports: " |
289 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
290 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\"", | |
291 | upstream_component, bt_component_get_name(upstream_component), | |
292 | downstream_component, bt_component_get_name(downstream_component)); | |
293 | ||
0d8b4d8e PP |
294 | /* |
295 | * At this point the ports are not connected yet. Both | |
296 | * components need to accept an eventual connection to their | |
297 | * port by the other port before we continue. | |
298 | */ | |
262e5473 | 299 | BT_LOGD_STR("Asking upstream component to accept the connection."); |
0d8b4d8e PP |
300 | component_status = bt_component_accept_port_connection( |
301 | upstream_component, upstream_port, downstream_port); | |
302 | if (component_status != BT_COMPONENT_STATUS_OK) { | |
262e5473 PP |
303 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
304 | BT_LOGD_STR("Upstream component refused the connection."); | |
305 | } else { | |
306 | BT_LOGW("Cannot ask upstream component to accept the connection: " | |
307 | "status=%s", bt_component_status_string(component_status)); | |
308 | } | |
309 | ||
a256a42d PP |
310 | status = bt_graph_status_from_component_status( |
311 | component_status); | |
312 | goto end; | |
0d8b4d8e | 313 | } |
262e5473 PP |
314 | |
315 | BT_LOGD_STR("Asking downstream component to accept the connection."); | |
0d8b4d8e PP |
316 | component_status = bt_component_accept_port_connection( |
317 | downstream_component, downstream_port, upstream_port); | |
318 | if (component_status != BT_COMPONENT_STATUS_OK) { | |
262e5473 PP |
319 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
320 | BT_LOGD_STR("Downstream component refused the connection."); | |
321 | } else { | |
322 | BT_LOGW("Cannot ask downstream component to accept the connection: " | |
323 | "status=%s", bt_component_status_string(component_status)); | |
324 | } | |
325 | ||
a256a42d PP |
326 | status = bt_graph_status_from_component_status( |
327 | component_status); | |
328 | goto end; | |
0d8b4d8e PP |
329 | } |
330 | ||
262e5473 | 331 | BT_LOGD_STR("Creating connection."); |
f60c8b34 JG |
332 | connection = bt_connection_create(graph, upstream_port, |
333 | downstream_port); | |
334 | if (!connection) { | |
262e5473 | 335 | BT_LOGW("Cannot create connection object."); |
a256a42d PP |
336 | status = BT_GRAPH_STATUS_NOMEM; |
337 | goto end; | |
f60c8b34 JG |
338 | } |
339 | ||
262e5473 PP |
340 | BT_LOGD("Connection object created: conn-addr=%p", connection); |
341 | ||
f60c8b34 | 342 | /* |
72b913fb PP |
343 | * Ownership of upstream_component/downstream_component and of |
344 | * the connection object is transferred to the graph. | |
f60c8b34 JG |
345 | */ |
346 | g_ptr_array_add(graph->connections, connection); | |
ffeb0eed | 347 | |
f60c8b34 | 348 | /* |
0d8b4d8e | 349 | * Notify both components that their port is connected. |
f60c8b34 | 350 | */ |
262e5473 | 351 | BT_LOGD_STR("Notifying upstream component that its port is connected."); |
0d8b4d8e PP |
352 | bt_component_port_connected(upstream_component, upstream_port, |
353 | downstream_port); | |
262e5473 | 354 | BT_LOGD_STR("Notifying downstream component that its port is connected."); |
0d8b4d8e PP |
355 | bt_component_port_connected(downstream_component, downstream_port, |
356 | upstream_port); | |
1bf957a0 PP |
357 | |
358 | /* | |
0d8b4d8e | 359 | * Notify the graph's creator that both ports are connected. |
1bf957a0 | 360 | */ |
262e5473 | 361 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
f345f8bb | 362 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
262e5473 PP |
363 | BT_LOGD("Connected component ports within graph: " |
364 | "graph-addr=%p, " | |
365 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
366 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
367 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
368 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
369 | graph, | |
370 | upstream_component, bt_component_get_name(upstream_component), | |
371 | downstream_component, bt_component_get_name(downstream_component), | |
372 | upstream_port, bt_port_get_name(upstream_port), | |
373 | downstream_port, bt_port_get_name(downstream_port)); | |
1bf957a0 | 374 | |
a256a42d | 375 | if (user_connection) { |
1a6a376a PP |
376 | /* Move reference to user */ |
377 | *user_connection = connection; | |
378 | connection = NULL; | |
a256a42d PP |
379 | } |
380 | ||
f60c8b34 JG |
381 | end: |
382 | bt_put(upstream_graph); | |
383 | bt_put(downstream_graph); | |
3eeacbb9 JG |
384 | bt_put(upstream_component); |
385 | bt_put(downstream_component); | |
a256a42d PP |
386 | bt_put(connection); |
387 | return status; | |
f60c8b34 JG |
388 | } |
389 | ||
851b70bd PP |
390 | static |
391 | enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph) | |
c0418dd9 | 392 | { |
f60c8b34 | 393 | struct bt_component *sink; |
72b913fb PP |
394 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
395 | enum bt_component_status comp_status; | |
f60c8b34 JG |
396 | GList *current_node; |
397 | ||
851b70bd | 398 | BT_LOGV("Making next sink consume: addr=%p", graph); |
202a3a13 | 399 | |
f60c8b34 | 400 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
262e5473 | 401 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
72b913fb | 402 | status = BT_GRAPH_STATUS_END; |
f60c8b34 JG |
403 | goto end; |
404 | } | |
405 | ||
406 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); | |
407 | sink = current_node->data; | |
262e5473 PP |
408 | BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"", |
409 | sink, bt_component_get_name(sink)); | |
72b913fb | 410 | comp_status = bt_component_sink_consume(sink); |
262e5473 PP |
411 | BT_LOGV("Consumed from sink: status=%s", |
412 | bt_component_status_string(comp_status)); | |
72b913fb PP |
413 | switch (comp_status) { |
414 | case BT_COMPONENT_STATUS_OK: | |
415 | break; | |
416 | case BT_COMPONENT_STATUS_END: | |
417 | status = BT_GRAPH_STATUS_END; | |
418 | break; | |
419 | case BT_COMPONENT_STATUS_AGAIN: | |
420 | status = BT_GRAPH_STATUS_AGAIN; | |
421 | break; | |
422 | case BT_COMPONENT_STATUS_INVALID: | |
423 | status = BT_GRAPH_STATUS_INVALID; | |
424 | break; | |
425 | default: | |
426 | status = BT_GRAPH_STATUS_ERROR; | |
427 | break; | |
428 | } | |
429 | ||
430 | if (status != BT_GRAPH_STATUS_END) { | |
f60c8b34 JG |
431 | g_queue_push_tail_link(graph->sinks_to_consume, current_node); |
432 | goto end; | |
433 | } | |
434 | ||
435 | /* End reached, the node is not added back to the queue and free'd. */ | |
436 | g_queue_delete_link(graph->sinks_to_consume, current_node); | |
437 | ||
438 | /* Don't forward an END status if there are sinks left to consume. */ | |
439 | if (!g_queue_is_empty(graph->sinks_to_consume)) { | |
440 | status = BT_GRAPH_STATUS_OK; | |
441 | goto end; | |
442 | } | |
443 | end: | |
262e5473 | 444 | BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status)); |
f60c8b34 | 445 | return status; |
c0418dd9 JG |
446 | } |
447 | ||
851b70bd PP |
448 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph) |
449 | { | |
450 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; | |
451 | ||
452 | if (!graph) { | |
453 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
454 | status = BT_GRAPH_STATUS_INVALID; | |
455 | goto end; | |
456 | } | |
457 | ||
458 | if (graph->canceled) { | |
459 | BT_LOGW("Invalid parameter: graph is canceled: " | |
460 | "graph-addr=%p", graph); | |
461 | status = BT_GRAPH_STATUS_CANCELED; | |
462 | goto end; | |
463 | } | |
464 | ||
465 | status = bt_graph_consume_no_check(graph); | |
466 | ||
467 | end: | |
468 | return status; | |
469 | } | |
470 | ||
72b913fb | 471 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
f60c8b34 | 472 | { |
72b913fb | 473 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
474 | |
475 | if (!graph) { | |
262e5473 | 476 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
72b913fb | 477 | status = BT_GRAPH_STATUS_INVALID; |
202a3a13 | 478 | goto end; |
f60c8b34 JG |
479 | } |
480 | ||
851b70bd PP |
481 | if (graph->canceled) { |
482 | BT_LOGW("Invalid parameter: graph is canceled: " | |
483 | "graph-addr=%p", graph); | |
484 | status = BT_GRAPH_STATUS_CANCELED; | |
485 | goto end; | |
486 | } | |
487 | ||
262e5473 PP |
488 | BT_LOGV("Running graph: addr=%p", graph); |
489 | ||
f60c8b34 | 490 | do { |
851b70bd PP |
491 | /* |
492 | * Check if the graph is canceled at each iteration. If | |
493 | * the graph was canceled by another thread or by a | |
494 | * signal, this is not a warning nor an error, it was | |
495 | * intentional: log with a DEBUG level only. | |
496 | */ | |
497 | if (graph->canceled) { | |
498 | BT_LOGD("Stopping the graph: graph is canceled: " | |
499 | "graph-addr=%p", graph); | |
500 | status = BT_GRAPH_STATUS_CANCELED; | |
501 | goto end; | |
502 | } | |
503 | ||
72b913fb PP |
504 | status = bt_graph_consume(graph); |
505 | if (status == BT_GRAPH_STATUS_AGAIN) { | |
f60c8b34 | 506 | /* |
202a3a13 PP |
507 | * If AGAIN is received and there are multiple |
508 | * sinks, go ahead and consume from the next | |
509 | * sink. | |
f60c8b34 | 510 | * |
202a3a13 PP |
511 | * However, in the case where a single sink is |
512 | * left, the caller can decide to busy-wait and | |
513 | * call bt_graph_run() continuously until the | |
514 | * source is ready or it can decide to sleep for | |
515 | * an arbitrary amount of time. | |
f60c8b34 JG |
516 | */ |
517 | if (graph->sinks_to_consume->length > 1) { | |
72b913fb | 518 | status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
519 | } |
520 | } | |
72b913fb | 521 | } while (status == BT_GRAPH_STATUS_OK); |
f60c8b34 JG |
522 | |
523 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
72b913fb | 524 | status = BT_GRAPH_STATUS_END; |
f60c8b34 | 525 | } |
262e5473 | 526 | |
202a3a13 | 527 | end: |
262e5473 | 528 | BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status)); |
72b913fb | 529 | return status; |
f60c8b34 | 530 | } |
1bf957a0 PP |
531 | |
532 | static | |
0d107cdd | 533 | int add_listener(GArray *listeners, void *func, void *data) |
1bf957a0 PP |
534 | { |
535 | struct bt_graph_listener listener = { | |
536 | .func = func, | |
537 | .data = data, | |
538 | }; | |
539 | ||
540 | g_array_append_val(listeners, listener); | |
0d107cdd | 541 | return listeners->len - 1; |
1bf957a0 PP |
542 | } |
543 | ||
0d107cdd | 544 | int bt_graph_add_port_added_listener( |
1bf957a0 PP |
545 | struct bt_graph *graph, |
546 | bt_graph_port_added_listener listener, void *data) | |
547 | { | |
0d107cdd | 548 | int ret; |
1bf957a0 | 549 | |
262e5473 PP |
550 | if (!graph) { |
551 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 552 | ret = -1; |
262e5473 PP |
553 | goto end; |
554 | } | |
555 | ||
556 | if (!listener) { | |
557 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 558 | ret = -1; |
1bf957a0 PP |
559 | goto end; |
560 | } | |
561 | ||
0d107cdd | 562 | ret = add_listener(graph->listeners.port_added, listener, data); |
262e5473 | 563 | BT_LOGV("Added \"port added\" listener to graph: " |
0d107cdd PP |
564 | "graph-addr=%p, listener-addr=%p, pos=%d", |
565 | graph, listener, ret); | |
1bf957a0 PP |
566 | |
567 | end: | |
0d107cdd | 568 | return ret; |
1bf957a0 PP |
569 | } |
570 | ||
0d107cdd | 571 | int bt_graph_add_port_removed_listener( |
1bf957a0 PP |
572 | struct bt_graph *graph, |
573 | bt_graph_port_removed_listener listener, void *data) | |
574 | { | |
0d107cdd | 575 | int ret; |
1bf957a0 | 576 | |
262e5473 PP |
577 | if (!graph) { |
578 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 579 | ret = -1; |
262e5473 PP |
580 | goto end; |
581 | } | |
582 | ||
583 | if (!listener) { | |
584 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 585 | ret = -1; |
1bf957a0 PP |
586 | goto end; |
587 | } | |
588 | ||
0d107cdd | 589 | ret = add_listener(graph->listeners.port_removed, listener, data); |
262e5473 | 590 | BT_LOGV("Added \"port removed\" listener to graph: " |
0d107cdd PP |
591 | "graph-addr=%p, listener-addr=%p, pos=%d", |
592 | graph, listener, ret); | |
1bf957a0 PP |
593 | |
594 | end: | |
0d107cdd | 595 | return ret; |
1bf957a0 PP |
596 | } |
597 | ||
0d107cdd | 598 | int bt_graph_add_ports_connected_listener( |
1bf957a0 | 599 | struct bt_graph *graph, |
f345f8bb | 600 | bt_graph_ports_connected_listener listener, void *data) |
1bf957a0 | 601 | { |
0d107cdd | 602 | int ret; |
1bf957a0 | 603 | |
262e5473 PP |
604 | if (!graph) { |
605 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 606 | ret = -1; |
262e5473 PP |
607 | goto end; |
608 | } | |
609 | ||
610 | if (!listener) { | |
611 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 612 | ret = -1; |
1bf957a0 PP |
613 | goto end; |
614 | } | |
615 | ||
0d107cdd | 616 | ret = add_listener(graph->listeners.ports_connected, listener, data); |
262e5473 | 617 | BT_LOGV("Added \"port connected\" listener to graph: " |
0d107cdd PP |
618 | "graph-addr=%p, listener-addr=%p, pos=%d", |
619 | graph, listener, ret); | |
1bf957a0 PP |
620 | |
621 | end: | |
0d107cdd | 622 | return ret; |
1bf957a0 PP |
623 | } |
624 | ||
0d107cdd | 625 | int bt_graph_add_ports_disconnected_listener( |
1bf957a0 | 626 | struct bt_graph *graph, |
f345f8bb | 627 | bt_graph_ports_disconnected_listener listener, void *data) |
1bf957a0 | 628 | { |
0d107cdd | 629 | int ret; |
1bf957a0 | 630 | |
262e5473 PP |
631 | if (!graph) { |
632 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 633 | ret = -1; |
262e5473 PP |
634 | goto end; |
635 | } | |
636 | ||
637 | if (!listener) { | |
638 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 639 | ret = -1; |
1bf957a0 PP |
640 | goto end; |
641 | } | |
642 | ||
0d107cdd | 643 | ret = add_listener(graph->listeners.ports_disconnected, listener, data); |
262e5473 | 644 | BT_LOGV("Added \"port disconnected\" listener to graph: " |
0d107cdd PP |
645 | "graph-addr=%p, listener-addr=%p, pos=%d", |
646 | graph, listener, ret); | |
1bf957a0 PP |
647 | |
648 | end: | |
0d107cdd | 649 | return ret; |
1bf957a0 PP |
650 | } |
651 | ||
652 | BT_HIDDEN | |
653 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) | |
654 | { | |
655 | size_t i; | |
656 | ||
262e5473 PP |
657 | BT_LOGV("Notifying graph listeners that a port was added: " |
658 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", | |
659 | graph, port, bt_port_get_name(port)); | |
660 | ||
1bf957a0 PP |
661 | for (i = 0; i < graph->listeners.port_added->len; i++) { |
662 | struct bt_graph_listener listener = | |
663 | g_array_index(graph->listeners.port_added, | |
664 | struct bt_graph_listener, i); | |
665 | bt_graph_port_added_listener func = listener.func; | |
666 | ||
667 | assert(func); | |
668 | func(port, listener.data); | |
669 | } | |
670 | } | |
671 | ||
672 | BT_HIDDEN | |
673 | void bt_graph_notify_port_removed(struct bt_graph *graph, | |
674 | struct bt_component *comp, struct bt_port *port) | |
675 | { | |
676 | size_t i; | |
677 | ||
262e5473 PP |
678 | BT_LOGV("Notifying graph listeners that a port was removed: " |
679 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", | |
680 | graph, port, bt_port_get_name(port)); | |
681 | ||
1bf957a0 PP |
682 | for (i = 0; i < graph->listeners.port_removed->len; i++) { |
683 | struct bt_graph_listener listener = | |
684 | g_array_index(graph->listeners.port_removed, | |
685 | struct bt_graph_listener, i); | |
686 | bt_graph_port_removed_listener func = listener.func; | |
687 | ||
688 | assert(func); | |
689 | func(comp, port, listener.data); | |
690 | } | |
691 | } | |
692 | ||
693 | BT_HIDDEN | |
f345f8bb PP |
694 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
695 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 PP |
696 | { |
697 | size_t i; | |
698 | ||
262e5473 PP |
699 | BT_LOGV("Notifying graph listeners that two ports were connected: " |
700 | "graph-addr=%p, " | |
701 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
702 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
703 | graph, upstream_port, bt_port_get_name(upstream_port), | |
704 | downstream_port, bt_port_get_name(downstream_port)); | |
705 | ||
f345f8bb | 706 | for (i = 0; i < graph->listeners.ports_connected->len; i++) { |
1bf957a0 | 707 | struct bt_graph_listener listener = |
f345f8bb | 708 | g_array_index(graph->listeners.ports_connected, |
1bf957a0 | 709 | struct bt_graph_listener, i); |
f345f8bb | 710 | bt_graph_ports_connected_listener func = listener.func; |
1bf957a0 PP |
711 | |
712 | assert(func); | |
f345f8bb | 713 | func(upstream_port, downstream_port, listener.data); |
1bf957a0 PP |
714 | } |
715 | } | |
716 | ||
717 | BT_HIDDEN | |
f345f8bb PP |
718 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
719 | struct bt_component *upstream_comp, | |
720 | struct bt_component *downstream_comp, | |
721 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 PP |
722 | { |
723 | size_t i; | |
724 | ||
262e5473 PP |
725 | BT_LOGV("Notifying graph listeners that two ports were disconnected: " |
726 | "graph-addr=%p, " | |
727 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
728 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
729 | graph, upstream_port, bt_port_get_name(upstream_port), | |
730 | downstream_port, bt_port_get_name(downstream_port)); | |
731 | ||
f345f8bb | 732 | for (i = 0; i < graph->listeners.ports_disconnected->len; i++) { |
1bf957a0 | 733 | struct bt_graph_listener listener = |
f345f8bb | 734 | g_array_index(graph->listeners.ports_disconnected, |
1bf957a0 | 735 | struct bt_graph_listener, i); |
f345f8bb | 736 | bt_graph_ports_disconnected_listener func = listener.func; |
1bf957a0 PP |
737 | |
738 | assert(func); | |
f345f8bb PP |
739 | func(upstream_comp, downstream_comp, upstream_port, |
740 | downstream_port, listener.data); | |
1bf957a0 PP |
741 | } |
742 | } | |
202a3a13 PP |
743 | |
744 | extern enum bt_graph_status bt_graph_cancel(struct bt_graph *graph) | |
745 | { | |
746 | enum bt_graph_status ret = BT_GRAPH_STATUS_OK; | |
747 | ||
748 | if (!graph) { | |
262e5473 | 749 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
202a3a13 PP |
750 | ret = BT_GRAPH_STATUS_INVALID; |
751 | goto end; | |
752 | } | |
753 | ||
754 | graph->canceled = BT_TRUE; | |
262e5473 | 755 | BT_LOGV("Canceled graph: addr=%p", graph); |
202a3a13 PP |
756 | |
757 | end: | |
758 | return ret; | |
759 | } | |
760 | ||
761 | extern bt_bool bt_graph_is_canceled(struct bt_graph *graph) | |
762 | { | |
763 | return graph ? graph->canceled : BT_FALSE; | |
764 | } | |
f167d3c0 PP |
765 | |
766 | BT_HIDDEN | |
767 | void bt_graph_remove_connection(struct bt_graph *graph, | |
768 | struct bt_connection *connection) | |
769 | { | |
770 | assert(graph); | |
771 | assert(connection); | |
262e5473 PP |
772 | BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p", |
773 | graph, connection); | |
f167d3c0 PP |
774 | g_ptr_array_remove(graph->connections, connection); |
775 | } | |
36712f1d PP |
776 | |
777 | enum bt_graph_status bt_graph_add_component_with_init_method_data( | |
778 | struct bt_graph *graph, | |
779 | struct bt_component_class *component_class, | |
780 | const char *name, struct bt_value *params, | |
781 | void *init_method_data, | |
782 | struct bt_component **user_component) | |
783 | { | |
784 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; | |
785 | enum bt_component_status comp_status; | |
786 | struct bt_component *component = NULL; | |
787 | enum bt_component_class_type type; | |
788 | size_t i; | |
789 | ||
790 | bt_get(params); | |
791 | ||
792 | if (!graph) { | |
793 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
794 | graph_status = BT_GRAPH_STATUS_INVALID; | |
795 | goto end; | |
796 | } | |
797 | ||
798 | if (!component_class) { | |
799 | BT_LOGW_STR("Invalid parameter: component class is NULL."); | |
800 | graph_status = BT_GRAPH_STATUS_INVALID; | |
801 | goto end; | |
802 | } | |
803 | ||
804 | type = bt_component_class_get_type(component_class); | |
805 | BT_LOGD("Adding component to graph: " | |
806 | "graph-addr=%p, comp-cls-addr=%p, " | |
807 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " | |
808 | "init-method-data-addr=%p", | |
809 | graph, component_class, bt_component_class_type_string(type), | |
810 | name, params, init_method_data); | |
811 | ||
812 | if (!name) { | |
813 | BT_LOGW_STR("Invalid parameter: name is NULL."); | |
814 | graph_status = BT_GRAPH_STATUS_INVALID; | |
815 | goto end; | |
816 | } | |
817 | ||
818 | if (graph->canceled) { | |
819 | BT_LOGW_STR("Invalid parameter: graph is canceled."); | |
820 | graph_status = BT_GRAPH_STATUS_CANCELED; | |
821 | goto end; | |
822 | } | |
823 | ||
824 | if (type != BT_COMPONENT_CLASS_TYPE_SOURCE && | |
825 | type != BT_COMPONENT_CLASS_TYPE_FILTER && | |
826 | type != BT_COMPONENT_CLASS_TYPE_SINK) { | |
827 | BT_LOGW("Invalid parameter: unknown component class type: " | |
828 | "type=%d", type); | |
829 | graph_status = BT_GRAPH_STATUS_INVALID; | |
830 | goto end; | |
831 | } | |
832 | ||
833 | for (i = 0; i < graph->components->len; i++) { | |
834 | void *other_comp = graph->components->pdata[i]; | |
835 | ||
836 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { | |
837 | BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: " | |
838 | "other-comp-addr=%p, name=\"%s\"", | |
839 | other_comp, name); | |
840 | graph_status = BT_GRAPH_STATUS_INVALID; | |
841 | goto end; | |
842 | } | |
843 | } | |
844 | ||
845 | /* | |
846 | * Parameters must be a map value, but we create a convenient | |
847 | * empty one if it's NULL. | |
848 | */ | |
849 | if (params) { | |
850 | if (!bt_value_is_map(params)) { | |
851 | BT_LOGW("Invalid parameter: initialization parameters must be a map value: " | |
852 | "type=%s", | |
853 | bt_value_type_string(bt_value_get_type(params))); | |
854 | graph_status = BT_GRAPH_STATUS_INVALID; | |
855 | goto end; | |
856 | } | |
857 | } else { | |
858 | params = bt_value_map_create(); | |
859 | if (!params) { | |
860 | BT_LOGE_STR("Cannot create map value object."); | |
861 | graph_status = BT_GRAPH_STATUS_NOMEM; | |
862 | goto end; | |
863 | } | |
864 | } | |
865 | ||
866 | comp_status = bt_component_create(component_class, name, &component); | |
867 | if (comp_status != BT_COMPONENT_STATUS_OK) { | |
868 | BT_LOGE("Cannot create empty component object: status=%s", | |
869 | bt_component_status_string(comp_status)); | |
870 | graph_status = bt_graph_status_from_component_status( | |
871 | comp_status); | |
872 | goto end; | |
873 | } | |
874 | ||
875 | /* | |
876 | * The user's initialization method needs to see that this | |
877 | * component is part of the graph. If the user method fails, we | |
878 | * immediately remove the component from the graph's components. | |
879 | */ | |
880 | g_ptr_array_add(graph->components, component); | |
881 | bt_component_set_graph(component, graph); | |
882 | ||
883 | if (component_class->methods.init) { | |
884 | BT_LOGD_STR("Calling user's initialization method."); | |
885 | comp_status = component_class->methods.init( | |
886 | bt_private_component_from_component(component), params, | |
887 | init_method_data); | |
888 | BT_LOGD("User method returned: status=%s", | |
889 | bt_component_status_string(comp_status)); | |
890 | if (comp_status != BT_COMPONENT_STATUS_OK) { | |
891 | BT_LOGW_STR("Initialization method failed."); | |
892 | graph_status = bt_graph_status_from_component_status( | |
893 | comp_status); | |
894 | bt_component_set_graph(component, NULL); | |
895 | g_ptr_array_remove_fast(graph->components, component); | |
896 | goto end; | |
897 | } | |
898 | } | |
899 | ||
900 | /* | |
901 | * Mark the component as initialized so that its finalization | |
902 | * method is called when it is destroyed. | |
903 | */ | |
904 | component->initialized = true; | |
905 | ||
906 | /* | |
907 | * If it's a sink component, it needs to be part of the graph's | |
908 | * sink queue to be consumed by bt_graph_consume(). | |
909 | */ | |
910 | if (bt_component_is_sink(component)) { | |
911 | g_queue_push_tail(graph->sinks_to_consume, component); | |
912 | } | |
913 | ||
914 | /* | |
915 | * Freeze the component class now that it's instantiated at | |
916 | * least once. | |
917 | */ | |
918 | BT_LOGD_STR("Freezing component class."); | |
919 | bt_component_class_freeze(component->class); | |
920 | BT_LOGD("Added component to graph: " | |
921 | "graph-addr=%p, comp-cls-addr=%p, " | |
922 | "comp-cls-type=%s, name=\"%s\", params-addr=%p, " | |
923 | "init-method-data-addr=%p, comp-addr=%p", | |
924 | graph, component_class, bt_component_class_type_string(type), | |
925 | name, params, init_method_data, component); | |
926 | ||
927 | if (user_component) { | |
928 | /* Move reference to user */ | |
929 | *user_component = component; | |
930 | component = NULL; | |
931 | } | |
932 | ||
933 | end: | |
934 | bt_put(component); | |
935 | bt_put(params); | |
936 | return graph_status; | |
937 | } | |
938 | ||
939 | enum bt_graph_status bt_graph_add_component( | |
940 | struct bt_graph *graph, | |
941 | struct bt_component_class *component_class, | |
942 | const char *name, struct bt_value *params, | |
943 | struct bt_component **component) | |
944 | { | |
945 | return bt_graph_add_component_with_init_method_data(graph, | |
946 | component_class, name, params, NULL, component); | |
947 | } |