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> |
f60c8b34 | 40 | #include <unistd.h> |
1bf957a0 PP |
41 | #include <glib.h> |
42 | ||
43 | struct bt_graph_listener { | |
44 | void *func; | |
45 | void *data; | |
46 | }; | |
c0418dd9 | 47 | |
f60c8b34 JG |
48 | static |
49 | void bt_graph_destroy(struct bt_object *obj) | |
c0418dd9 | 50 | { |
f60c8b34 JG |
51 | struct bt_graph *graph = container_of(obj, |
52 | struct bt_graph, base); | |
c0418dd9 | 53 | |
bd14d768 PP |
54 | /* |
55 | * The graph's reference count is 0 if we're here. Increment | |
56 | * it to avoid a double-destroy (possibly infinitely recursive) | |
57 | * in this situation: | |
58 | * | |
59 | * 1. We put and destroy a connection. | |
60 | * 2. This connection's destructor finalizes its active | |
61 | * notification iterators. | |
62 | * 3. A notification iterator's finalization function gets a | |
63 | * new reference on its component (reference count goes from | |
64 | * 0 to 1). | |
65 | * 4. Since this component's reference count goes to 1, it takes | |
66 | * a reference on its parent (this graph). This graph's | |
67 | * reference count goes from 0 to 1. | |
68 | * 5. The notification iterator's finalization function puts its | |
69 | * component reference (reference count goes from 1 to 0). | |
70 | * 6. Since this component's reference count goes from 1 to 0, | |
71 | * it puts its parent (this graph). This graph's reference | |
72 | * count goes from 1 to 0. | |
73 | * 7. Since this graph's reference count goes from 1 to 0, | |
74 | * its destructor is called (this function). | |
75 | * | |
76 | * With the incrementation below, the graph's reference count at | |
77 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This | |
78 | * ensures that this function is not called two times. | |
79 | */ | |
262e5473 | 80 | BT_LOGD("Destroying graph: addr=%p", graph); |
bd14d768 PP |
81 | obj->ref_count.count++; |
82 | ||
c0418dd9 | 83 | if (graph->connections) { |
262e5473 | 84 | BT_LOGD_STR("Destroying connections."); |
c0418dd9 JG |
85 | g_ptr_array_free(graph->connections, TRUE); |
86 | } | |
bd14d768 | 87 | if (graph->components) { |
262e5473 | 88 | BT_LOGD_STR("Destroying components."); |
bd14d768 PP |
89 | g_ptr_array_free(graph->components, TRUE); |
90 | } | |
f60c8b34 JG |
91 | if (graph->sinks_to_consume) { |
92 | g_queue_free(graph->sinks_to_consume); | |
c0418dd9 | 93 | } |
1bf957a0 PP |
94 | |
95 | if (graph->listeners.port_added) { | |
96 | g_array_free(graph->listeners.port_added, TRUE); | |
97 | } | |
98 | ||
99 | if (graph->listeners.port_removed) { | |
100 | g_array_free(graph->listeners.port_removed, TRUE); | |
101 | } | |
102 | ||
f345f8bb PP |
103 | if (graph->listeners.ports_connected) { |
104 | g_array_free(graph->listeners.ports_connected, TRUE); | |
1bf957a0 PP |
105 | } |
106 | ||
f345f8bb PP |
107 | if (graph->listeners.ports_disconnected) { |
108 | g_array_free(graph->listeners.ports_disconnected, TRUE); | |
1bf957a0 PP |
109 | } |
110 | ||
c0418dd9 JG |
111 | g_free(graph); |
112 | } | |
113 | ||
1bf957a0 PP |
114 | static |
115 | int init_listeners_array(GArray **listeners) | |
116 | { | |
117 | int ret = 0; | |
118 | ||
119 | assert(listeners); | |
120 | *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener)); | |
121 | if (!*listeners) { | |
262e5473 | 122 | BT_LOGE_STR("Failed to allocate one GArray."); |
1bf957a0 PP |
123 | ret = -1; |
124 | goto end; | |
125 | } | |
126 | ||
127 | end: | |
128 | return ret; | |
129 | } | |
130 | ||
f60c8b34 | 131 | struct bt_graph *bt_graph_create(void) |
c0418dd9 | 132 | { |
f60c8b34 | 133 | struct bt_graph *graph; |
1bf957a0 | 134 | int ret; |
c0418dd9 | 135 | |
262e5473 | 136 | BT_LOGD_STR("Creating graph object."); |
f60c8b34 | 137 | graph = g_new0(struct bt_graph, 1); |
c0418dd9 | 138 | if (!graph) { |
262e5473 | 139 | BT_LOGE_STR("Failed to allocate one graph."); |
c0418dd9 JG |
140 | goto end; |
141 | } | |
142 | ||
f60c8b34 | 143 | bt_object_init(graph, bt_graph_destroy); |
c0418dd9 | 144 | |
f60c8b34 | 145 | graph->connections = g_ptr_array_new_with_free_func(bt_object_release); |
c0418dd9 | 146 | if (!graph->connections) { |
262e5473 | 147 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
c0418dd9 JG |
148 | goto error; |
149 | } | |
f60c8b34 JG |
150 | graph->components = g_ptr_array_new_with_free_func(bt_object_release); |
151 | if (!graph->components) { | |
262e5473 | 152 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
f60c8b34 JG |
153 | goto error; |
154 | } | |
155 | graph->sinks_to_consume = g_queue_new(); | |
156 | if (!graph->sinks_to_consume) { | |
262e5473 | 157 | BT_LOGE_STR("Failed to allocate one GQueue."); |
c0418dd9 JG |
158 | goto error; |
159 | } | |
1bf957a0 PP |
160 | |
161 | ret = init_listeners_array(&graph->listeners.port_added); | |
162 | if (ret) { | |
262e5473 | 163 | BT_LOGE_STR("Cannot create the \"port added\" listener array."); |
1bf957a0 PP |
164 | goto error; |
165 | } | |
166 | ||
167 | ret = init_listeners_array(&graph->listeners.port_removed); | |
168 | if (ret) { | |
262e5473 | 169 | BT_LOGE_STR("Cannot create the \"port removed\" listener array."); |
1bf957a0 PP |
170 | goto error; |
171 | } | |
172 | ||
f345f8bb | 173 | ret = init_listeners_array(&graph->listeners.ports_connected); |
1bf957a0 | 174 | if (ret) { |
262e5473 | 175 | BT_LOGE_STR("Cannot create the \"port connected\" listener array."); |
1bf957a0 PP |
176 | goto error; |
177 | } | |
178 | ||
f345f8bb | 179 | ret = init_listeners_array(&graph->listeners.ports_disconnected); |
1bf957a0 | 180 | if (ret) { |
262e5473 | 181 | BT_LOGE_STR("Cannot create the \"port disconneted\" listener array."); |
1bf957a0 PP |
182 | goto error; |
183 | } | |
184 | ||
262e5473 PP |
185 | BT_LOGD("Created graph object: addr=%p", graph); |
186 | ||
c0418dd9 JG |
187 | end: |
188 | return graph; | |
189 | error: | |
190 | BT_PUT(graph); | |
191 | goto end; | |
192 | } | |
193 | ||
a256a42d PP |
194 | enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph, |
195 | struct bt_port *upstream_port, struct bt_port *downstream_port, | |
196 | struct bt_connection **user_connection) | |
f60c8b34 | 197 | { |
a256a42d | 198 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
199 | struct bt_connection *connection = NULL; |
200 | struct bt_graph *upstream_graph = NULL; | |
201 | struct bt_graph *downstream_graph = NULL; | |
202 | struct bt_component *upstream_component = NULL; | |
203 | struct bt_component *downstream_component = NULL; | |
204 | enum bt_component_status component_status; | |
c55a9f58 PP |
205 | bt_bool upstream_was_already_in_graph; |
206 | bt_bool downstream_was_already_in_graph; | |
f60c8b34 | 207 | |
262e5473 PP |
208 | if (!graph) { |
209 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
a256a42d | 210 | status = BT_GRAPH_STATUS_INVALID; |
262e5473 PP |
211 | goto end; |
212 | } | |
213 | ||
214 | if (!upstream_port) { | |
215 | BT_LOGW_STR("Invalid parameter: upstream port is NULL."); | |
a256a42d | 216 | status = BT_GRAPH_STATUS_INVALID; |
262e5473 PP |
217 | goto end; |
218 | } | |
219 | ||
220 | if (!downstream_port) { | |
221 | BT_LOGW_STR("Invalid parameter: downstream port is NULL."); | |
a256a42d | 222 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
223 | goto end; |
224 | } | |
225 | ||
262e5473 PP |
226 | BT_LOGD("Connecting component ports within graph: " |
227 | "graph-addr=%p, " | |
228 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
229 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
230 | graph, upstream_port, bt_port_get_name(upstream_port), | |
231 | downstream_port, bt_port_get_name(downstream_port)); | |
232 | ||
202a3a13 | 233 | if (graph->canceled) { |
262e5473 | 234 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
a256a42d | 235 | status = BT_GRAPH_STATUS_CANCELED; |
202a3a13 PP |
236 | goto end; |
237 | } | |
238 | ||
72b913fb | 239 | /* Ensure appropriate types for upstream and downstream ports. */ |
f60c8b34 | 240 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
262e5473 | 241 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
a256a42d | 242 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
243 | goto end; |
244 | } | |
245 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { | |
262e5473 | 246 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
a256a42d | 247 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
248 | goto end; |
249 | } | |
250 | ||
72b913fb | 251 | /* Ensure that both ports are currently unconnected. */ |
0d8b4d8e | 252 | if (bt_port_is_connected(upstream_port)) { |
262e5473 | 253 | BT_LOGW_STR("Invalid parameter: upstream port is already connected."); |
a256a42d | 254 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
255 | goto end; |
256 | } | |
257 | ||
0d8b4d8e | 258 | if (bt_port_is_connected(downstream_port)) { |
262e5473 | 259 | BT_LOGW_STR("Invalid parameter: downstream port is already connected."); |
a256a42d | 260 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
261 | goto end; |
262 | } | |
263 | ||
264 | /* | |
265 | * Ensure that both ports are still attached to their creating | |
266 | * component. | |
267 | */ | |
f60c8b34 | 268 | upstream_component = bt_port_get_component(upstream_port); |
72b913fb | 269 | if (!upstream_component) { |
262e5473 | 270 | BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)"); |
a256a42d | 271 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
272 | goto end; |
273 | } | |
274 | ||
275 | downstream_component = bt_port_get_component(downstream_port); | |
276 | if (!downstream_component) { | |
262e5473 | 277 | BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)"); |
a256a42d | 278 | status = BT_GRAPH_STATUS_INVALID; |
72b913fb PP |
279 | goto end; |
280 | } | |
281 | ||
262e5473 PP |
282 | BT_LOGD("Connecting component ports: " |
283 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
284 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\"", | |
285 | upstream_component, bt_component_get_name(upstream_component), | |
286 | downstream_component, bt_component_get_name(downstream_component)); | |
287 | ||
72b913fb | 288 | /* Ensure the components are not already part of another graph. */ |
f60c8b34 JG |
289 | upstream_graph = bt_component_get_graph(upstream_component); |
290 | if (upstream_graph && (graph != upstream_graph)) { | |
262e5473 PP |
291 | BT_LOGW("Invalid parameter: upstream port's component is already part of another graph: " |
292 | "other-graph-addr=%p", upstream_graph); | |
a256a42d PP |
293 | status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH; |
294 | goto end; | |
f60c8b34 | 295 | } |
ffeb0eed | 296 | upstream_was_already_in_graph = (graph == upstream_graph); |
f60c8b34 JG |
297 | downstream_graph = bt_component_get_graph(downstream_component); |
298 | if (downstream_graph && (graph != downstream_graph)) { | |
262e5473 PP |
299 | BT_LOGW("Invalid parameter: downstream port's component is already part of another graph: " |
300 | "other-graph-addr=%p", downstream_graph); | |
a256a42d PP |
301 | status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH; |
302 | goto end; | |
f60c8b34 | 303 | } |
ffeb0eed | 304 | downstream_was_already_in_graph = (graph == downstream_graph); |
f60c8b34 | 305 | |
0d8b4d8e PP |
306 | /* |
307 | * At this point the ports are not connected yet. Both | |
308 | * components need to accept an eventual connection to their | |
309 | * port by the other port before we continue. | |
310 | */ | |
262e5473 | 311 | BT_LOGD_STR("Asking upstream component to accept the connection."); |
0d8b4d8e PP |
312 | component_status = bt_component_accept_port_connection( |
313 | upstream_component, upstream_port, downstream_port); | |
314 | if (component_status != BT_COMPONENT_STATUS_OK) { | |
262e5473 PP |
315 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
316 | BT_LOGD_STR("Upstream component refused the connection."); | |
317 | } else { | |
318 | BT_LOGW("Cannot ask upstream component to accept the connection: " | |
319 | "status=%s", bt_component_status_string(component_status)); | |
320 | } | |
321 | ||
a256a42d PP |
322 | status = bt_graph_status_from_component_status( |
323 | component_status); | |
324 | goto end; | |
0d8b4d8e | 325 | } |
262e5473 PP |
326 | |
327 | BT_LOGD_STR("Asking downstream component to accept the connection."); | |
0d8b4d8e PP |
328 | component_status = bt_component_accept_port_connection( |
329 | downstream_component, downstream_port, upstream_port); | |
330 | if (component_status != BT_COMPONENT_STATUS_OK) { | |
262e5473 PP |
331 | if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) { |
332 | BT_LOGD_STR("Downstream component refused the connection."); | |
333 | } else { | |
334 | BT_LOGW("Cannot ask downstream component to accept the connection: " | |
335 | "status=%s", bt_component_status_string(component_status)); | |
336 | } | |
337 | ||
a256a42d PP |
338 | status = bt_graph_status_from_component_status( |
339 | component_status); | |
340 | goto end; | |
0d8b4d8e PP |
341 | } |
342 | ||
262e5473 | 343 | BT_LOGD_STR("Creating connection."); |
f60c8b34 JG |
344 | connection = bt_connection_create(graph, upstream_port, |
345 | downstream_port); | |
346 | if (!connection) { | |
262e5473 | 347 | BT_LOGW("Cannot create connection object."); |
a256a42d PP |
348 | status = BT_GRAPH_STATUS_NOMEM; |
349 | goto end; | |
f60c8b34 JG |
350 | } |
351 | ||
262e5473 PP |
352 | BT_LOGD("Connection object created: conn-addr=%p", connection); |
353 | ||
f60c8b34 | 354 | /* |
72b913fb PP |
355 | * Ownership of upstream_component/downstream_component and of |
356 | * the connection object is transferred to the graph. | |
f60c8b34 JG |
357 | */ |
358 | g_ptr_array_add(graph->connections, connection); | |
ffeb0eed JG |
359 | |
360 | if (!upstream_was_already_in_graph) { | |
361 | g_ptr_array_add(graph->components, upstream_component); | |
362 | bt_component_set_graph(upstream_component, graph); | |
363 | } | |
364 | if (!downstream_was_already_in_graph) { | |
365 | g_ptr_array_add(graph->components, downstream_component); | |
366 | bt_component_set_graph(downstream_component, graph); | |
367 | if (bt_component_get_class_type(downstream_component) == | |
368 | BT_COMPONENT_CLASS_TYPE_SINK) { | |
369 | g_queue_push_tail(graph->sinks_to_consume, | |
370 | downstream_component); | |
371 | } | |
f60c8b34 JG |
372 | } |
373 | ||
374 | /* | |
0d8b4d8e PP |
375 | * The graph is now the parent of these components which |
376 | * garantees their existence for the duration of the graph's | |
377 | * lifetime. | |
f60c8b34 | 378 | */ |
f60c8b34 JG |
379 | |
380 | /* | |
0d8b4d8e | 381 | * Notify both components that their port is connected. |
f60c8b34 | 382 | */ |
262e5473 | 383 | BT_LOGD_STR("Notifying upstream component that its port is connected."); |
0d8b4d8e PP |
384 | bt_component_port_connected(upstream_component, upstream_port, |
385 | downstream_port); | |
262e5473 | 386 | BT_LOGD_STR("Notifying downstream component that its port is connected."); |
0d8b4d8e PP |
387 | bt_component_port_connected(downstream_component, downstream_port, |
388 | upstream_port); | |
1bf957a0 PP |
389 | |
390 | /* | |
0d8b4d8e | 391 | * Notify the graph's creator that both ports are connected. |
1bf957a0 | 392 | */ |
262e5473 | 393 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
f345f8bb | 394 | bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
262e5473 PP |
395 | BT_LOGD("Connected component ports within graph: " |
396 | "graph-addr=%p, " | |
397 | "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " | |
398 | "downstream-comp-addr=%p, downstream-comp-name=\"%s\", " | |
399 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
400 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
401 | graph, | |
402 | upstream_component, bt_component_get_name(upstream_component), | |
403 | downstream_component, bt_component_get_name(downstream_component), | |
404 | upstream_port, bt_port_get_name(upstream_port), | |
405 | downstream_port, bt_port_get_name(downstream_port)); | |
1bf957a0 | 406 | |
a256a42d PP |
407 | if (user_connection) { |
408 | BT_MOVE(*user_connection, connection); | |
409 | } | |
410 | ||
f60c8b34 JG |
411 | end: |
412 | bt_put(upstream_graph); | |
413 | bt_put(downstream_graph); | |
3eeacbb9 JG |
414 | bt_put(upstream_component); |
415 | bt_put(downstream_component); | |
a256a42d PP |
416 | bt_put(connection); |
417 | return status; | |
f60c8b34 JG |
418 | } |
419 | ||
72b913fb | 420 | enum bt_graph_status bt_graph_consume(struct bt_graph *graph) |
c0418dd9 | 421 | { |
f60c8b34 | 422 | struct bt_component *sink; |
72b913fb PP |
423 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
424 | enum bt_component_status comp_status; | |
f60c8b34 JG |
425 | GList *current_node; |
426 | ||
427 | if (!graph) { | |
262e5473 | 428 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
72b913fb | 429 | status = BT_GRAPH_STATUS_INVALID; |
f60c8b34 JG |
430 | goto end; |
431 | } | |
432 | ||
262e5473 PP |
433 | BT_LOGV("Making sink consume: addr=%p", graph); |
434 | ||
202a3a13 | 435 | if (graph->canceled) { |
262e5473 | 436 | BT_LOGW_STR("Invalid parameter: graph is canceled."); |
202a3a13 PP |
437 | status = BT_GRAPH_STATUS_CANCELED; |
438 | goto end; | |
439 | } | |
440 | ||
f60c8b34 | 441 | if (g_queue_is_empty(graph->sinks_to_consume)) { |
262e5473 | 442 | BT_LOGV_STR("Graph's sink queue is empty: end of graph."); |
72b913fb | 443 | status = BT_GRAPH_STATUS_END; |
f60c8b34 JG |
444 | goto end; |
445 | } | |
446 | ||
447 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); | |
448 | sink = current_node->data; | |
262e5473 PP |
449 | BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"", |
450 | sink, bt_component_get_name(sink)); | |
72b913fb | 451 | comp_status = bt_component_sink_consume(sink); |
262e5473 PP |
452 | BT_LOGV("Consumed from sink: status=%s", |
453 | bt_component_status_string(comp_status)); | |
72b913fb PP |
454 | switch (comp_status) { |
455 | case BT_COMPONENT_STATUS_OK: | |
456 | break; | |
457 | case BT_COMPONENT_STATUS_END: | |
458 | status = BT_GRAPH_STATUS_END; | |
459 | break; | |
460 | case BT_COMPONENT_STATUS_AGAIN: | |
461 | status = BT_GRAPH_STATUS_AGAIN; | |
462 | break; | |
463 | case BT_COMPONENT_STATUS_INVALID: | |
464 | status = BT_GRAPH_STATUS_INVALID; | |
465 | break; | |
466 | default: | |
467 | status = BT_GRAPH_STATUS_ERROR; | |
468 | break; | |
469 | } | |
470 | ||
471 | if (status != BT_GRAPH_STATUS_END) { | |
f60c8b34 JG |
472 | g_queue_push_tail_link(graph->sinks_to_consume, current_node); |
473 | goto end; | |
474 | } | |
475 | ||
476 | /* End reached, the node is not added back to the queue and free'd. */ | |
477 | g_queue_delete_link(graph->sinks_to_consume, current_node); | |
478 | ||
479 | /* Don't forward an END status if there are sinks left to consume. */ | |
480 | if (!g_queue_is_empty(graph->sinks_to_consume)) { | |
481 | status = BT_GRAPH_STATUS_OK; | |
482 | goto end; | |
483 | } | |
484 | end: | |
262e5473 | 485 | BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status)); |
f60c8b34 | 486 | return status; |
c0418dd9 JG |
487 | } |
488 | ||
72b913fb | 489 | enum bt_graph_status bt_graph_run(struct bt_graph *graph) |
f60c8b34 | 490 | { |
72b913fb | 491 | enum bt_graph_status status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
492 | |
493 | if (!graph) { | |
262e5473 | 494 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
72b913fb | 495 | status = BT_GRAPH_STATUS_INVALID; |
202a3a13 | 496 | goto end; |
f60c8b34 JG |
497 | } |
498 | ||
262e5473 PP |
499 | BT_LOGV("Running graph: addr=%p", graph); |
500 | ||
f60c8b34 | 501 | do { |
72b913fb PP |
502 | status = bt_graph_consume(graph); |
503 | if (status == BT_GRAPH_STATUS_AGAIN) { | |
f60c8b34 | 504 | /* |
202a3a13 PP |
505 | * If AGAIN is received and there are multiple |
506 | * sinks, go ahead and consume from the next | |
507 | * sink. | |
f60c8b34 | 508 | * |
202a3a13 PP |
509 | * However, in the case where a single sink is |
510 | * left, the caller can decide to busy-wait and | |
511 | * call bt_graph_run() continuously until the | |
512 | * source is ready or it can decide to sleep for | |
513 | * an arbitrary amount of time. | |
f60c8b34 JG |
514 | */ |
515 | if (graph->sinks_to_consume->length > 1) { | |
72b913fb | 516 | status = BT_GRAPH_STATUS_OK; |
f60c8b34 JG |
517 | } |
518 | } | |
72b913fb | 519 | } while (status == BT_GRAPH_STATUS_OK); |
f60c8b34 JG |
520 | |
521 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
72b913fb | 522 | status = BT_GRAPH_STATUS_END; |
f60c8b34 | 523 | } |
262e5473 | 524 | |
202a3a13 | 525 | end: |
262e5473 | 526 | BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status)); |
72b913fb | 527 | return status; |
f60c8b34 | 528 | } |
1bf957a0 PP |
529 | |
530 | static | |
0d107cdd | 531 | int add_listener(GArray *listeners, void *func, void *data) |
1bf957a0 PP |
532 | { |
533 | struct bt_graph_listener listener = { | |
534 | .func = func, | |
535 | .data = data, | |
536 | }; | |
537 | ||
538 | g_array_append_val(listeners, listener); | |
0d107cdd | 539 | return listeners->len - 1; |
1bf957a0 PP |
540 | } |
541 | ||
0d107cdd | 542 | int bt_graph_add_port_added_listener( |
1bf957a0 PP |
543 | struct bt_graph *graph, |
544 | bt_graph_port_added_listener listener, void *data) | |
545 | { | |
0d107cdd | 546 | int ret; |
1bf957a0 | 547 | |
262e5473 PP |
548 | if (!graph) { |
549 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 550 | ret = -1; |
262e5473 PP |
551 | goto end; |
552 | } | |
553 | ||
554 | if (!listener) { | |
555 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 556 | ret = -1; |
1bf957a0 PP |
557 | goto end; |
558 | } | |
559 | ||
0d107cdd | 560 | ret = add_listener(graph->listeners.port_added, listener, data); |
262e5473 | 561 | BT_LOGV("Added \"port added\" listener to graph: " |
0d107cdd PP |
562 | "graph-addr=%p, listener-addr=%p, pos=%d", |
563 | graph, listener, ret); | |
1bf957a0 PP |
564 | |
565 | end: | |
0d107cdd | 566 | return ret; |
1bf957a0 PP |
567 | } |
568 | ||
0d107cdd | 569 | int bt_graph_add_port_removed_listener( |
1bf957a0 PP |
570 | struct bt_graph *graph, |
571 | bt_graph_port_removed_listener listener, void *data) | |
572 | { | |
0d107cdd | 573 | int ret; |
1bf957a0 | 574 | |
262e5473 PP |
575 | if (!graph) { |
576 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 577 | ret = -1; |
262e5473 PP |
578 | goto end; |
579 | } | |
580 | ||
581 | if (!listener) { | |
582 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 583 | ret = -1; |
1bf957a0 PP |
584 | goto end; |
585 | } | |
586 | ||
0d107cdd | 587 | ret = add_listener(graph->listeners.port_removed, listener, data); |
262e5473 | 588 | BT_LOGV("Added \"port removed\" listener to graph: " |
0d107cdd PP |
589 | "graph-addr=%p, listener-addr=%p, pos=%d", |
590 | graph, listener, ret); | |
1bf957a0 PP |
591 | |
592 | end: | |
0d107cdd | 593 | return ret; |
1bf957a0 PP |
594 | } |
595 | ||
0d107cdd | 596 | int bt_graph_add_ports_connected_listener( |
1bf957a0 | 597 | struct bt_graph *graph, |
f345f8bb | 598 | bt_graph_ports_connected_listener listener, void *data) |
1bf957a0 | 599 | { |
0d107cdd | 600 | int ret; |
1bf957a0 | 601 | |
262e5473 PP |
602 | if (!graph) { |
603 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 604 | ret = -1; |
262e5473 PP |
605 | goto end; |
606 | } | |
607 | ||
608 | if (!listener) { | |
609 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 610 | ret = -1; |
1bf957a0 PP |
611 | goto end; |
612 | } | |
613 | ||
0d107cdd | 614 | ret = add_listener(graph->listeners.ports_connected, listener, data); |
262e5473 | 615 | BT_LOGV("Added \"port connected\" listener to graph: " |
0d107cdd PP |
616 | "graph-addr=%p, listener-addr=%p, pos=%d", |
617 | graph, listener, ret); | |
1bf957a0 PP |
618 | |
619 | end: | |
0d107cdd | 620 | return ret; |
1bf957a0 PP |
621 | } |
622 | ||
0d107cdd | 623 | int bt_graph_add_ports_disconnected_listener( |
1bf957a0 | 624 | struct bt_graph *graph, |
f345f8bb | 625 | bt_graph_ports_disconnected_listener listener, void *data) |
1bf957a0 | 626 | { |
0d107cdd | 627 | int ret; |
1bf957a0 | 628 | |
262e5473 PP |
629 | if (!graph) { |
630 | BT_LOGW_STR("Invalid parameter: graph is NULL."); | |
0d107cdd | 631 | ret = -1; |
262e5473 PP |
632 | goto end; |
633 | } | |
634 | ||
635 | if (!listener) { | |
636 | BT_LOGW_STR("Invalid parameter: listener is NULL."); | |
0d107cdd | 637 | ret = -1; |
1bf957a0 PP |
638 | goto end; |
639 | } | |
640 | ||
0d107cdd | 641 | ret = add_listener(graph->listeners.ports_disconnected, listener, data); |
262e5473 | 642 | BT_LOGV("Added \"port disconnected\" listener to graph: " |
0d107cdd PP |
643 | "graph-addr=%p, listener-addr=%p, pos=%d", |
644 | graph, listener, ret); | |
1bf957a0 PP |
645 | |
646 | end: | |
0d107cdd | 647 | return ret; |
1bf957a0 PP |
648 | } |
649 | ||
650 | BT_HIDDEN | |
651 | void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port) | |
652 | { | |
653 | size_t i; | |
654 | ||
262e5473 PP |
655 | BT_LOGV("Notifying graph listeners that a port was added: " |
656 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", | |
657 | graph, port, bt_port_get_name(port)); | |
658 | ||
1bf957a0 PP |
659 | for (i = 0; i < graph->listeners.port_added->len; i++) { |
660 | struct bt_graph_listener listener = | |
661 | g_array_index(graph->listeners.port_added, | |
662 | struct bt_graph_listener, i); | |
663 | bt_graph_port_added_listener func = listener.func; | |
664 | ||
665 | assert(func); | |
666 | func(port, listener.data); | |
667 | } | |
668 | } | |
669 | ||
670 | BT_HIDDEN | |
671 | void bt_graph_notify_port_removed(struct bt_graph *graph, | |
672 | struct bt_component *comp, struct bt_port *port) | |
673 | { | |
674 | size_t i; | |
675 | ||
262e5473 PP |
676 | BT_LOGV("Notifying graph listeners that a port was removed: " |
677 | "graph-addr=%p, port-addr=%p, port-name=\"%s\"", | |
678 | graph, port, bt_port_get_name(port)); | |
679 | ||
1bf957a0 PP |
680 | for (i = 0; i < graph->listeners.port_removed->len; i++) { |
681 | struct bt_graph_listener listener = | |
682 | g_array_index(graph->listeners.port_removed, | |
683 | struct bt_graph_listener, i); | |
684 | bt_graph_port_removed_listener func = listener.func; | |
685 | ||
686 | assert(func); | |
687 | func(comp, port, listener.data); | |
688 | } | |
689 | } | |
690 | ||
691 | BT_HIDDEN | |
f345f8bb PP |
692 | void bt_graph_notify_ports_connected(struct bt_graph *graph, |
693 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 PP |
694 | { |
695 | size_t i; | |
696 | ||
262e5473 PP |
697 | BT_LOGV("Notifying graph listeners that two ports were connected: " |
698 | "graph-addr=%p, " | |
699 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
700 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
701 | graph, upstream_port, bt_port_get_name(upstream_port), | |
702 | downstream_port, bt_port_get_name(downstream_port)); | |
703 | ||
f345f8bb | 704 | for (i = 0; i < graph->listeners.ports_connected->len; i++) { |
1bf957a0 | 705 | struct bt_graph_listener listener = |
f345f8bb | 706 | g_array_index(graph->listeners.ports_connected, |
1bf957a0 | 707 | struct bt_graph_listener, i); |
f345f8bb | 708 | bt_graph_ports_connected_listener func = listener.func; |
1bf957a0 PP |
709 | |
710 | assert(func); | |
f345f8bb | 711 | func(upstream_port, downstream_port, listener.data); |
1bf957a0 PP |
712 | } |
713 | } | |
714 | ||
715 | BT_HIDDEN | |
f345f8bb PP |
716 | void bt_graph_notify_ports_disconnected(struct bt_graph *graph, |
717 | struct bt_component *upstream_comp, | |
718 | struct bt_component *downstream_comp, | |
719 | struct bt_port *upstream_port, struct bt_port *downstream_port) | |
1bf957a0 PP |
720 | { |
721 | size_t i; | |
722 | ||
262e5473 PP |
723 | BT_LOGV("Notifying graph listeners that two ports were disconnected: " |
724 | "graph-addr=%p, " | |
725 | "upstream-port-addr=%p, upstream-port-name=\"%s\", " | |
726 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
727 | graph, upstream_port, bt_port_get_name(upstream_port), | |
728 | downstream_port, bt_port_get_name(downstream_port)); | |
729 | ||
f345f8bb | 730 | for (i = 0; i < graph->listeners.ports_disconnected->len; i++) { |
1bf957a0 | 731 | struct bt_graph_listener listener = |
f345f8bb | 732 | g_array_index(graph->listeners.ports_disconnected, |
1bf957a0 | 733 | struct bt_graph_listener, i); |
f345f8bb | 734 | bt_graph_ports_disconnected_listener func = listener.func; |
1bf957a0 PP |
735 | |
736 | assert(func); | |
f345f8bb PP |
737 | func(upstream_comp, downstream_comp, upstream_port, |
738 | downstream_port, listener.data); | |
1bf957a0 PP |
739 | } |
740 | } | |
202a3a13 PP |
741 | |
742 | extern enum bt_graph_status bt_graph_cancel(struct bt_graph *graph) | |
743 | { | |
744 | enum bt_graph_status ret = BT_GRAPH_STATUS_OK; | |
745 | ||
746 | if (!graph) { | |
262e5473 | 747 | BT_LOGW_STR("Invalid parameter: graph is NULL."); |
202a3a13 PP |
748 | ret = BT_GRAPH_STATUS_INVALID; |
749 | goto end; | |
750 | } | |
751 | ||
752 | graph->canceled = BT_TRUE; | |
262e5473 | 753 | BT_LOGV("Canceled graph: addr=%p", graph); |
202a3a13 PP |
754 | |
755 | end: | |
756 | return ret; | |
757 | } | |
758 | ||
759 | extern bt_bool bt_graph_is_canceled(struct bt_graph *graph) | |
760 | { | |
761 | return graph ? graph->canceled : BT_FALSE; | |
762 | } | |
f167d3c0 PP |
763 | |
764 | BT_HIDDEN | |
765 | void bt_graph_remove_connection(struct bt_graph *graph, | |
766 | struct bt_connection *connection) | |
767 | { | |
768 | assert(graph); | |
769 | assert(connection); | |
262e5473 PP |
770 | BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p", |
771 | graph, connection); | |
f167d3c0 PP |
772 | g_ptr_array_remove(graph->connections, connection); |
773 | } |