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