Commit | Line | Data |
---|---|---|
c0418dd9 | 1 | /* |
f2b0325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
f60c8b34 | 3 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
c0418dd9 JG |
4 | * |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
b03487ab | 24 | #define BT_LOG_TAG "LIB/GRAPH" |
1633ef46 | 25 | #include "lib/logging.h" |
262e5473 | 26 | |
57952005 MJ |
27 | #include "common/assert.h" |
28 | #include "lib/assert-pre.h" | |
3b7d3ccc | 29 | #include "lib/assert-post.h" |
71c5da58 MJ |
30 | #include <babeltrace2/graph/graph.h> |
31 | #include <babeltrace2/graph/graph-const.h> | |
71c5da58 MJ |
32 | #include <babeltrace2/graph/component-source-const.h> |
33 | #include <babeltrace2/graph/component-filter-const.h> | |
34 | #include <babeltrace2/graph/port-const.h> | |
57952005 MJ |
35 | #include "lib/graph/message/message.h" |
36 | #include "compat/compiler.h" | |
37 | #include "common/common.h" | |
71c5da58 MJ |
38 | #include <babeltrace2/types.h> |
39 | #include <babeltrace2/value.h> | |
40 | #include <babeltrace2/value-const.h> | |
57952005 | 41 | #include "lib/value.h" |
f60c8b34 | 42 | #include <unistd.h> |
1bf957a0 PP |
43 | #include <glib.h> |
44 | ||
5efafa1a | 45 | #include "component-class-sink-simple.h" |
57952005 MJ |
46 | #include "component.h" |
47 | #include "component-sink.h" | |
48 | #include "connection.h" | |
49 | #include "graph.h" | |
d73bb381 | 50 | #include "interrupter.h" |
57952005 MJ |
51 | #include "message/event.h" |
52 | #include "message/packet.h" | |
53 | ||
fb25b9e3 PP |
54 | typedef enum bt_graph_listener_func_status |
55 | (*port_added_func_t)(const void *, const void *, void *); | |
7b53201c | 56 | |
fb25b9e3 PP |
57 | typedef enum bt_graph_listener_func_status |
58 | (*ports_connected_func_t)(const void *, const void *, const void *, | |
7b53201c | 59 | const void *, void *); |
834e9996 | 60 | |
fb25b9e3 PP |
61 | typedef enum bt_component_class_init_method_status |
62 | (*comp_init_method_t)(const void *, const void *, void *); | |
63 | ||
1bf957a0 | 64 | struct bt_graph_listener { |
7b53201c | 65 | bt_graph_listener_removed_func removed; |
1bf957a0 PP |
66 | void *data; |
67 | }; | |
c0418dd9 | 68 | |
834e9996 PP |
69 | struct bt_graph_listener_port_added { |
70 | struct bt_graph_listener base; | |
71 | port_added_func_t func; | |
72 | }; | |
eb87cce7 | 73 | |
834e9996 PP |
74 | struct bt_graph_listener_ports_connected { |
75 | struct bt_graph_listener base; | |
76 | ports_connected_func_t func; | |
77 | }; | |
eb87cce7 | 78 | |
834e9996 PP |
79 | #define INIT_LISTENERS_ARRAY(_type, _listeners) \ |
80 | do { \ | |
81 | _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \ | |
82 | if (!(_listeners)) { \ | |
a8f90e5d PP |
83 | BT_LIB_LOGE_APPEND_CAUSE( \ |
84 | "Failed to allocate one GArray."); \ | |
834e9996 PP |
85 | } \ |
86 | } while (0) | |
87 | ||
88 | #define CALL_REMOVE_LISTENERS(_type, _listeners) \ | |
89 | do { \ | |
90 | size_t i; \ | |
91 | \ | |
8a62a380 FD |
92 | if (!_listeners) { \ |
93 | break; \ | |
94 | } \ | |
834e9996 PP |
95 | for (i = 0; i < (_listeners)->len; i++) { \ |
96 | _type *listener = \ | |
97 | &g_array_index((_listeners), _type, i); \ | |
98 | \ | |
99 | if (listener->base.removed) { \ | |
100 | listener->base.removed(listener->base.data); \ | |
101 | } \ | |
102 | } \ | |
103 | } while (0) | |
eb87cce7 | 104 | |
f60c8b34 | 105 | static |
834e9996 | 106 | void destroy_graph(struct bt_object *obj) |
c0418dd9 | 107 | { |
7b53201c | 108 | struct bt_graph *graph = container_of(obj, struct bt_graph, base); |
c0418dd9 | 109 | |
bd14d768 PP |
110 | /* |
111 | * The graph's reference count is 0 if we're here. Increment | |
112 | * it to avoid a double-destroy (possibly infinitely recursive) | |
113 | * in this situation: | |
114 | * | |
115 | * 1. We put and destroy a connection. | |
904860cb PP |
116 | * 2. This connection's destructor finalizes its active message |
117 | * iterators. | |
118 | * 3. A message iterator's finalization function gets a new | |
119 | * reference on its component (reference count goes from 0 to | |
120 | * 1). | |
bd14d768 PP |
121 | * 4. Since this component's reference count goes to 1, it takes |
122 | * a reference on its parent (this graph). This graph's | |
123 | * reference count goes from 0 to 1. | |
b09a5592 | 124 | * 5. The message iterator's finalization function puts its |
bd14d768 PP |
125 | * component reference (reference count goes from 1 to 0). |
126 | * 6. Since this component's reference count goes from 1 to 0, | |
127 | * it puts its parent (this graph). This graph's reference | |
128 | * count goes from 1 to 0. | |
904860cb PP |
129 | * 7. Since this graph's reference count goes from 1 to 0, its |
130 | * destructor is called (this function). | |
bd14d768 PP |
131 | * |
132 | * With the incrementation below, the graph's reference count at | |
133 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This | |
134 | * ensures that this function is not called two times. | |
135 | */ | |
a684a357 | 136 | BT_LIB_LOGI("Destroying graph: %!+g", graph); |
1d7bf349 | 137 | obj->ref_count++; |
d73bb381 | 138 | graph->config_state = BT_GRAPH_CONFIGURATION_STATE_DESTROYING; |
49682acd | 139 | |
eb87cce7 | 140 | /* Call all remove listeners */ |
834e9996 PP |
141 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added, |
142 | graph->listeners.source_output_port_added); | |
143 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added, | |
144 | graph->listeners.filter_output_port_added); | |
145 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added, | |
146 | graph->listeners.filter_input_port_added); | |
147 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added, | |
148 | graph->listeners.sink_input_port_added); | |
834e9996 PP |
149 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, |
150 | graph->listeners.source_filter_ports_connected); | |
ee976410 PP |
151 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, |
152 | graph->listeners.filter_filter_ports_connected); | |
834e9996 PP |
153 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, |
154 | graph->listeners.source_sink_ports_connected); | |
155 | CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected, | |
156 | graph->listeners.filter_sink_ports_connected); | |
eb87cce7 | 157 | |
b09a5592 PP |
158 | if (graph->messages) { |
159 | g_ptr_array_free(graph->messages, TRUE); | |
160 | graph->messages = NULL; | |
f7c3ac09 PP |
161 | } |
162 | ||
c0418dd9 | 163 | if (graph->connections) { |
262e5473 | 164 | BT_LOGD_STR("Destroying connections."); |
c0418dd9 | 165 | g_ptr_array_free(graph->connections, TRUE); |
834e9996 | 166 | graph->connections = NULL; |
c0418dd9 | 167 | } |
f7c3ac09 | 168 | |
bd14d768 | 169 | if (graph->components) { |
262e5473 | 170 | BT_LOGD_STR("Destroying components."); |
bd14d768 | 171 | g_ptr_array_free(graph->components, TRUE); |
834e9996 | 172 | graph->components = NULL; |
bd14d768 | 173 | } |
f7c3ac09 | 174 | |
d73bb381 PP |
175 | if (graph->interrupters) { |
176 | BT_LOGD_STR("Putting interrupters."); | |
177 | g_ptr_array_free(graph->interrupters, TRUE); | |
178 | graph->interrupters = NULL; | |
179 | } | |
180 | ||
181 | BT_OBJECT_PUT_REF_AND_RESET(graph->default_interrupter); | |
182 | ||
f60c8b34 JG |
183 | if (graph->sinks_to_consume) { |
184 | g_queue_free(graph->sinks_to_consume); | |
834e9996 PP |
185 | graph->sinks_to_consume = NULL; |
186 | } | |
187 | ||
188 | if (graph->listeners.source_output_port_added) { | |
189 | g_array_free(graph->listeners.source_output_port_added, TRUE); | |
190 | graph->listeners.source_output_port_added = NULL; | |
191 | } | |
192 | ||
193 | if (graph->listeners.filter_output_port_added) { | |
194 | g_array_free(graph->listeners.filter_output_port_added, TRUE); | |
195 | graph->listeners.filter_output_port_added = NULL; | |
196 | } | |
197 | ||
198 | if (graph->listeners.filter_input_port_added) { | |
199 | g_array_free(graph->listeners.filter_input_port_added, TRUE); | |
200 | graph->listeners.filter_input_port_added = NULL; | |
c0418dd9 | 201 | } |
1bf957a0 | 202 | |
834e9996 PP |
203 | if (graph->listeners.sink_input_port_added) { |
204 | g_array_free(graph->listeners.sink_input_port_added, TRUE); | |
205 | graph->listeners.sink_input_port_added = NULL; | |
1bf957a0 PP |
206 | } |
207 | ||
834e9996 PP |
208 | if (graph->listeners.source_filter_ports_connected) { |
209 | g_array_free(graph->listeners.source_filter_ports_connected, | |
210 | TRUE); | |
211 | graph->listeners.source_filter_ports_connected = NULL; | |
212 | } | |
213 | ||
ee976410 PP |
214 | if (graph->listeners.filter_filter_ports_connected) { |
215 | g_array_free(graph->listeners.filter_filter_ports_connected, | |
216 | TRUE); | |
217 | graph->listeners.filter_filter_ports_connected = NULL; | |
218 | } | |
219 | ||
834e9996 PP |
220 | if (graph->listeners.source_sink_ports_connected) { |
221 | g_array_free(graph->listeners.source_sink_ports_connected, | |
222 | TRUE); | |
223 | graph->listeners.source_sink_ports_connected = NULL; | |
224 | } | |
225 | ||
226 | if (graph->listeners.filter_sink_ports_connected) { | |
227 | g_array_free(graph->listeners.filter_sink_ports_connected, | |
228 | TRUE); | |
229 | graph->listeners.filter_sink_ports_connected = NULL; | |
230 | } | |
231 | ||
b09a5592 PP |
232 | bt_object_pool_finalize(&graph->event_msg_pool); |
233 | bt_object_pool_finalize(&graph->packet_begin_msg_pool); | |
234 | bt_object_pool_finalize(&graph->packet_end_msg_pool); | |
c0418dd9 JG |
235 | g_free(graph); |
236 | } | |
237 | ||
f7c3ac09 | 238 | static |
b09a5592 | 239 | void destroy_message_event(struct bt_message *msg, |
f7c3ac09 PP |
240 | struct bt_graph *graph) |
241 | { | |
b09a5592 | 242 | bt_message_event_destroy(msg); |
f7c3ac09 PP |
243 | } |
244 | ||
245 | static | |
b09a5592 | 246 | void destroy_message_packet_begin(struct bt_message *msg, |
f7c3ac09 PP |
247 | struct bt_graph *graph) |
248 | { | |
a1f053a9 | 249 | bt_message_packet_destroy(msg); |
f7c3ac09 PP |
250 | } |
251 | ||
252 | static | |
b09a5592 | 253 | void destroy_message_packet_end(struct bt_message *msg, |
f7c3ac09 PP |
254 | struct bt_graph *graph) |
255 | { | |
a1f053a9 | 256 | bt_message_packet_destroy(msg); |
f7c3ac09 PP |
257 | } |
258 | ||
259 | static | |
b09a5592 | 260 | void notify_message_graph_is_destroyed(struct bt_message *msg) |
f7c3ac09 | 261 | { |
b09a5592 | 262 | bt_message_unlink_graph(msg); |
f7c3ac09 PP |
263 | } |
264 | ||
7b53201c | 265 | struct bt_graph *bt_graph_create(void) |
c0418dd9 | 266 | { |
f60c8b34 | 267 | struct bt_graph *graph; |
1bf957a0 | 268 | int ret; |
c0418dd9 | 269 | |
a684a357 | 270 | BT_LOGI_STR("Creating graph object."); |
f60c8b34 | 271 | graph = g_new0(struct bt_graph, 1); |
c0418dd9 | 272 | if (!graph) { |
a8f90e5d | 273 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph."); |
c0418dd9 JG |
274 | goto end; |
275 | } | |
276 | ||
834e9996 | 277 | bt_object_init_shared(&graph->base, destroy_graph); |
1d7bf349 PP |
278 | graph->connections = g_ptr_array_new_with_free_func( |
279 | (GDestroyNotify) bt_object_try_spec_release); | |
c0418dd9 | 280 | if (!graph->connections) { |
a8f90e5d | 281 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
c0418dd9 JG |
282 | goto error; |
283 | } | |
1d7bf349 PP |
284 | graph->components = g_ptr_array_new_with_free_func( |
285 | (GDestroyNotify) bt_object_try_spec_release); | |
f60c8b34 | 286 | if (!graph->components) { |
a8f90e5d | 287 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
f60c8b34 JG |
288 | goto error; |
289 | } | |
290 | graph->sinks_to_consume = g_queue_new(); | |
291 | if (!graph->sinks_to_consume) { | |
a8f90e5d | 292 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue."); |
c0418dd9 JG |
293 | goto error; |
294 | } | |
1bf957a0 | 295 | |
834e9996 PP |
296 | bt_graph_set_can_consume(graph, true); |
297 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, | |
298 | graph->listeners.source_output_port_added); | |
299 | ||
300 | if (!graph->listeners.source_output_port_added) { | |
301 | ret = -1; | |
1bf957a0 PP |
302 | goto error; |
303 | } | |
304 | ||
834e9996 PP |
305 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
306 | graph->listeners.filter_output_port_added); | |
307 | ||
308 | if (!graph->listeners.filter_output_port_added) { | |
309 | ret = -1; | |
1bf957a0 PP |
310 | goto error; |
311 | } | |
312 | ||
834e9996 PP |
313 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
314 | graph->listeners.filter_input_port_added); | |
315 | ||
316 | if (!graph->listeners.filter_input_port_added) { | |
317 | ret = -1; | |
1bf957a0 PP |
318 | goto error; |
319 | } | |
320 | ||
834e9996 PP |
321 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
322 | graph->listeners.sink_input_port_added); | |
323 | ||
324 | if (!graph->listeners.sink_input_port_added) { | |
325 | ret = -1; | |
326 | goto error; | |
327 | } | |
328 | ||
834e9996 PP |
329 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected, |
330 | graph->listeners.source_filter_ports_connected); | |
331 | ||
332 | if (!graph->listeners.source_filter_ports_connected) { | |
333 | ret = -1; | |
334 | goto error; | |
335 | } | |
336 | ||
337 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected, | |
338 | graph->listeners.source_sink_ports_connected); | |
339 | ||
340 | if (!graph->listeners.source_sink_ports_connected) { | |
341 | ret = -1; | |
342 | goto error; | |
343 | } | |
344 | ||
ee976410 PP |
345 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected, |
346 | graph->listeners.filter_filter_ports_connected); | |
347 | ||
348 | if (!graph->listeners.filter_filter_ports_connected) { | |
349 | ret = -1; | |
350 | goto error; | |
351 | } | |
352 | ||
834e9996 PP |
353 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected, |
354 | graph->listeners.filter_sink_ports_connected); | |
355 | ||
356 | if (!graph->listeners.filter_sink_ports_connected) { | |
357 | ret = -1; | |
358 | goto error; | |
359 | } | |
360 | ||
d73bb381 PP |
361 | graph->interrupters = g_ptr_array_new_with_free_func( |
362 | (GDestroyNotify) bt_object_put_no_null_check); | |
363 | if (!graph->interrupters) { | |
364 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
365 | goto error; | |
366 | } | |
367 | ||
368 | graph->default_interrupter = bt_interrupter_create(); | |
369 | if (!graph->default_interrupter) { | |
370 | BT_LIB_LOGE_APPEND_CAUSE( | |
371 | "Failed to create one interrupter object."); | |
372 | goto error; | |
373 | } | |
374 | ||
375 | bt_graph_add_interrupter(graph, graph->default_interrupter); | |
b09a5592 PP |
376 | ret = bt_object_pool_initialize(&graph->event_msg_pool, |
377 | (bt_object_pool_new_object_func) bt_message_event_new, | |
378 | (bt_object_pool_destroy_object_func) destroy_message_event, | |
f7c3ac09 PP |
379 | graph); |
380 | if (ret) { | |
a8f90e5d PP |
381 | BT_LIB_LOGE_APPEND_CAUSE( |
382 | "Failed to initialize event message pool: ret=%d", | |
f7c3ac09 PP |
383 | ret); |
384 | goto error; | |
385 | } | |
386 | ||
b09a5592 PP |
387 | ret = bt_object_pool_initialize(&graph->packet_begin_msg_pool, |
388 | (bt_object_pool_new_object_func) bt_message_packet_beginning_new, | |
389 | (bt_object_pool_destroy_object_func) destroy_message_packet_begin, | |
f7c3ac09 PP |
390 | graph); |
391 | if (ret) { | |
a8f90e5d PP |
392 | BT_LIB_LOGE_APPEND_CAUSE( |
393 | "Failed to initialize packet beginning message pool: ret=%d", | |
f7c3ac09 PP |
394 | ret); |
395 | goto error; | |
396 | } | |
397 | ||
b09a5592 PP |
398 | ret = bt_object_pool_initialize(&graph->packet_end_msg_pool, |
399 | (bt_object_pool_new_object_func) bt_message_packet_end_new, | |
400 | (bt_object_pool_destroy_object_func) destroy_message_packet_end, | |
f7c3ac09 PP |
401 | graph); |
402 | if (ret) { | |
a8f90e5d PP |
403 | BT_LIB_LOGE_APPEND_CAUSE( |
404 | "Failed to initialize packet end message pool: ret=%d", | |
f7c3ac09 PP |
405 | ret); |
406 | goto error; | |
407 | } | |
408 | ||
b09a5592 PP |
409 | graph->messages = g_ptr_array_new_with_free_func( |
410 | (GDestroyNotify) notify_message_graph_is_destroyed); | |
a684a357 | 411 | BT_LIB_LOGI("Created graph object: %!+g", graph); |
262e5473 | 412 | |
c0418dd9 | 413 | end: |
8d750b42 | 414 | return (void *) graph; |
834e9996 | 415 | |
c0418dd9 | 416 | error: |
8138bfe1 | 417 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
c0418dd9 JG |
418 | goto end; |
419 | } | |
420 | ||
fb25b9e3 | 421 | enum bt_graph_connect_ports_status bt_graph_connect_ports( |
7b53201c PP |
422 | struct bt_graph *graph, |
423 | const struct bt_port_output *upstream_port_out, | |
424 | const struct bt_port_input *downstream_port_in, | |
425 | const struct bt_connection **user_connection) | |
f60c8b34 | 426 | { |
fb25b9e3 PP |
427 | enum bt_graph_connect_ports_status status = BT_FUNC_STATUS_OK; |
428 | enum bt_graph_listener_func_status listener_status; | |
f60c8b34 | 429 | struct bt_connection *connection = NULL; |
834e9996 PP |
430 | struct bt_port *upstream_port = (void *) upstream_port_out; |
431 | struct bt_port *downstream_port = (void *) downstream_port_in; | |
f60c8b34 JG |
432 | struct bt_component *upstream_component = NULL; |
433 | struct bt_component *downstream_component = NULL; | |
fb25b9e3 | 434 | enum bt_component_class_port_connected_method_status port_connected_status; |
834e9996 | 435 | bool init_can_consume; |
f60c8b34 | 436 | |
834e9996 PP |
437 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
438 | BT_ASSERT_PRE_NON_NULL(upstream_port, "Upstream port"); | |
439 | BT_ASSERT_PRE_NON_NULL(downstream_port, "Downstream port port"); | |
1043fdea PP |
440 | BT_ASSERT_PRE( |
441 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
9a84962b | 442 | "Graph is not in the \"configuring\" state: %!+g", graph); |
834e9996 PP |
443 | BT_ASSERT_PRE(!bt_port_is_connected(upstream_port), |
444 | "Upstream port is already connected: %!+p", upstream_port); | |
445 | BT_ASSERT_PRE(!bt_port_is_connected(downstream_port), | |
446 | "Downstream port is already connected: %!+p", downstream_port); | |
7b53201c | 447 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port), |
834e9996 PP |
448 | "Upstream port does not belong to a component: %!+p", |
449 | upstream_port); | |
7b53201c | 450 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port), |
834e9996 PP |
451 | "Downstream port does not belong to a component: %!+p", |
452 | downstream_port); | |
3afb93ee | 453 | init_can_consume = graph->can_consume; |
a684a357 | 454 | BT_LIB_LOGI("Connecting component ports within graph: " |
834e9996 PP |
455 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", |
456 | graph, upstream_port, downstream_port); | |
457 | bt_graph_set_can_consume(graph, false); | |
7b53201c | 458 | upstream_component = bt_port_borrow_component_inline( |
834e9996 | 459 | (void *) upstream_port); |
7b53201c | 460 | downstream_component = bt_port_borrow_component_inline( |
834e9996 | 461 | (void *) downstream_port); |
262e5473 | 462 | |
262e5473 | 463 | BT_LOGD_STR("Creating connection."); |
834e9996 PP |
464 | connection = bt_connection_create(graph, (void *) upstream_port, |
465 | (void *) downstream_port); | |
f60c8b34 | 466 | if (!connection) { |
a8f90e5d | 467 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object."); |
fb25b9e3 | 468 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
a256a42d | 469 | goto end; |
f60c8b34 JG |
470 | } |
471 | ||
834e9996 | 472 | BT_LIB_LOGD("Connection object created: %!+x", connection); |
262e5473 | 473 | |
f60c8b34 | 474 | /* |
72b913fb PP |
475 | * Ownership of upstream_component/downstream_component and of |
476 | * the connection object is transferred to the graph. | |
f60c8b34 JG |
477 | */ |
478 | g_ptr_array_add(graph->connections, connection); | |
ffeb0eed | 479 | |
f60c8b34 | 480 | /* |
0d8b4d8e | 481 | * Notify both components that their port is connected. |
f60c8b34 | 482 | */ |
834e9996 PP |
483 | BT_LIB_LOGD("Notifying upstream component that its port is connected: " |
484 | "%![comp-]+c, %![port-]+p", upstream_component, upstream_port); | |
fb25b9e3 | 485 | port_connected_status = bt_component_port_connected(upstream_component, |
834e9996 | 486 | (void *) upstream_port, (void *) downstream_port); |
fb25b9e3 | 487 | if (port_connected_status != BT_FUNC_STATUS_OK) { |
a8f90e5d PP |
488 | if (port_connected_status < 0) { |
489 | BT_LIB_LOGW_APPEND_CAUSE( | |
490 | "Upstream component's \"port connected\" method failed: " | |
491 | "status=%s, %![graph-]+g, %![up-comp-]+c, " | |
492 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", | |
493 | bt_common_func_status_string( | |
494 | port_connected_status), | |
495 | graph, upstream_component, downstream_component, | |
496 | upstream_port, downstream_port); | |
497 | } | |
498 | ||
634f394c | 499 | bt_connection_end(connection, true); |
fb25b9e3 | 500 | status = (int) port_connected_status; |
634f394c PP |
501 | goto end; |
502 | } | |
503 | ||
0f15970b | 504 | connection->notified_upstream_port_connected = true; |
834e9996 PP |
505 | BT_LIB_LOGD("Notifying downstream component that its port is connected: " |
506 | "%![comp-]+c, %![port-]+p", downstream_component, | |
507 | downstream_port); | |
fb25b9e3 | 508 | port_connected_status = bt_component_port_connected(downstream_component, |
834e9996 | 509 | (void *) downstream_port, (void *) upstream_port); |
fb25b9e3 | 510 | if (port_connected_status != BT_FUNC_STATUS_OK) { |
a8f90e5d PP |
511 | if (port_connected_status < 0) { |
512 | BT_LIB_LOGW_APPEND_CAUSE( | |
513 | "Downstream component's \"port connected\" method failed: " | |
514 | "status=%s, %![graph-]+g, %![up-comp-]+c, " | |
515 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", | |
516 | bt_common_func_status_string( | |
517 | port_connected_status), | |
518 | graph, upstream_component, downstream_component, | |
519 | upstream_port, downstream_port); | |
520 | } | |
521 | ||
634f394c | 522 | bt_connection_end(connection, true); |
fb25b9e3 | 523 | status = (int) port_connected_status; |
634f394c PP |
524 | goto end; |
525 | } | |
526 | ||
0f15970b | 527 | connection->notified_downstream_port_connected = true; |
1bf957a0 PP |
528 | |
529 | /* | |
0d8b4d8e | 530 | * Notify the graph's creator that both ports are connected. |
1bf957a0 | 531 | */ |
262e5473 | 532 | BT_LOGD_STR("Notifying graph's user that new component ports are connected."); |
e18f3e2b | 533 | listener_status = bt_graph_notify_ports_connected(graph, upstream_port, downstream_port); |
fb25b9e3 | 534 | if (listener_status != BT_FUNC_STATUS_OK) { |
a8f90e5d PP |
535 | if (listener_status < 0) { |
536 | BT_LIB_LOGW_APPEND_CAUSE( | |
537 | "Graph \"ports connected\" listener failed: " | |
538 | "status=%d, %![graph-]+g, %![up-comp-]+c, " | |
539 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", | |
540 | listener_status, graph, | |
541 | upstream_component, downstream_component, | |
542 | upstream_port, downstream_port); | |
543 | } | |
544 | ||
e18f3e2b SM |
545 | status = (int) listener_status; |
546 | goto end; | |
547 | } | |
548 | ||
0f15970b | 549 | connection->notified_graph_ports_connected = true; |
a684a357 | 550 | BT_LIB_LOGI("Connected component ports within graph: " |
834e9996 PP |
551 | "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, " |
552 | "%![up-port-]+p, %![down-port-]+p", | |
553 | graph, upstream_component, downstream_component, | |
554 | upstream_port, downstream_port); | |
1bf957a0 | 555 | |
a256a42d | 556 | if (user_connection) { |
1a6a376a PP |
557 | /* Move reference to user */ |
558 | *user_connection = connection; | |
559 | connection = NULL; | |
a256a42d PP |
560 | } |
561 | ||
f60c8b34 | 562 | end: |
fb25b9e3 | 563 | if (status != BT_FUNC_STATUS_OK) { |
e18f3e2b | 564 | bt_graph_make_faulty(graph); |
9a84962b PP |
565 | } |
566 | ||
8138bfe1 | 567 | bt_object_put_ref(connection); |
834e9996 PP |
568 | (void) init_can_consume; |
569 | bt_graph_set_can_consume(graph, init_can_consume); | |
a256a42d | 570 | return status; |
f60c8b34 JG |
571 | } |
572 | ||
371361a1 | 573 | static inline |
fb25b9e3 | 574 | int consume_graph_sink(struct bt_component_sink *comp) |
c0418dd9 | 575 | { |
fb25b9e3 | 576 | enum bt_component_class_sink_consume_method_status consume_status; |
834e9996 PP |
577 | struct bt_component_class_sink *sink_class = NULL; |
578 | ||
579 | BT_ASSERT(comp); | |
580 | sink_class = (void *) comp->parent.class; | |
581 | BT_ASSERT(sink_class->methods.consume); | |
582 | BT_LIB_LOGD("Calling user's consume method: %!+c", comp); | |
fb25b9e3 | 583 | consume_status = sink_class->methods.consume((void *) comp); |
834e9996 | 584 | BT_LOGD("User method returned: status=%s", |
fb25b9e3 | 585 | bt_common_func_status_string(consume_status)); |
fa6cfec3 | 586 | BT_ASSERT_POST_DEV(consume_status == BT_FUNC_STATUS_OK || |
fb25b9e3 PP |
587 | consume_status == BT_FUNC_STATUS_END || |
588 | consume_status == BT_FUNC_STATUS_AGAIN || | |
589 | consume_status == BT_FUNC_STATUS_ERROR || | |
590 | consume_status == BT_FUNC_STATUS_MEMORY_ERROR, | |
834e9996 | 591 | "Invalid component status returned by consuming method: " |
fb25b9e3 | 592 | "status=%s", bt_common_func_status_string(consume_status)); |
a8f90e5d PP |
593 | if (consume_status) { |
594 | if (consume_status < 0) { | |
595 | BT_LIB_LOGW_APPEND_CAUSE( | |
596 | "Component's \"consume\" method failed: " | |
597 | "status=%s, %![comp-]+c", | |
598 | bt_common_func_status_string(consume_status), | |
599 | comp); | |
600 | } | |
601 | ||
834e9996 PP |
602 | goto end; |
603 | } | |
604 | ||
a684a357 | 605 | BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s", |
fb25b9e3 | 606 | comp, bt_common_func_status_string(consume_status)); |
834e9996 PP |
607 | |
608 | end: | |
fb25b9e3 | 609 | return consume_status; |
c3ac0193 PP |
610 | } |
611 | ||
612 | /* | |
613 | * `node` is removed from the queue of sinks to consume when passed to | |
614 | * this function. This function adds it back to the queue if there's | |
615 | * still something to consume afterwards. | |
616 | */ | |
371361a1 | 617 | static inline |
fb25b9e3 | 618 | int consume_sink_node(struct bt_graph *graph, GList *node) |
c3ac0193 | 619 | { |
fb25b9e3 | 620 | int status; |
834e9996 | 621 | struct bt_component_sink *sink; |
c3ac0193 PP |
622 | |
623 | sink = node->data; | |
624 | status = consume_graph_sink(sink); | |
fb25b9e3 | 625 | if (G_UNLIKELY(status != BT_FUNC_STATUS_END)) { |
c3ac0193 | 626 | g_queue_push_tail_link(graph->sinks_to_consume, node); |
f60c8b34 JG |
627 | goto end; |
628 | } | |
629 | ||
630 | /* End reached, the node is not added back to the queue and free'd. */ | |
c3ac0193 | 631 | g_queue_delete_link(graph->sinks_to_consume, node); |
f60c8b34 JG |
632 | |
633 | /* Don't forward an END status if there are sinks left to consume. */ | |
634 | if (!g_queue_is_empty(graph->sinks_to_consume)) { | |
fb25b9e3 | 635 | status = BT_FUNC_STATUS_OK; |
f60c8b34 JG |
636 | goto end; |
637 | } | |
c3ac0193 PP |
638 | |
639 | end: | |
a684a357 | 640 | BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s", |
fb25b9e3 | 641 | sink, bt_common_func_status_string(status)); |
c3ac0193 PP |
642 | return status; |
643 | } | |
644 | ||
645 | BT_HIDDEN | |
fb25b9e3 | 646 | int bt_graph_consume_sink_no_check(struct bt_graph *graph, |
834e9996 | 647 | struct bt_component_sink *sink) |
c3ac0193 | 648 | { |
fb25b9e3 | 649 | int status; |
c3ac0193 PP |
650 | GList *sink_node; |
651 | int index; | |
652 | ||
a684a357 | 653 | BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink); |
834e9996 | 654 | BT_ASSERT(bt_component_borrow_graph((void *) sink) == graph); |
c3ac0193 PP |
655 | |
656 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
a684a357 | 657 | BT_LOGD_STR("Graph's sink queue is empty: end of graph."); |
fb25b9e3 | 658 | status = BT_FUNC_STATUS_END; |
c3ac0193 PP |
659 | goto end; |
660 | } | |
661 | ||
662 | index = g_queue_index(graph->sinks_to_consume, sink); | |
663 | if (index < 0) { | |
a684a357 PP |
664 | BT_LIB_LOGD("Sink component is not marked as consumable: " |
665 | "component sink is ended: %![comp-]+c", sink); | |
fb25b9e3 | 666 | status = BT_FUNC_STATUS_END; |
c3ac0193 PP |
667 | goto end; |
668 | } | |
669 | ||
670 | sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index); | |
8b45963b | 671 | BT_ASSERT(sink_node); |
c3ac0193 PP |
672 | status = consume_sink_node(graph, sink_node); |
673 | ||
674 | end: | |
675 | return status; | |
676 | } | |
677 | ||
371361a1 | 678 | static inline |
fb25b9e3 | 679 | int consume_no_check(struct bt_graph *graph) |
c3ac0193 | 680 | { |
fb25b9e3 | 681 | int status = BT_FUNC_STATUS_OK; |
c3ac0193 PP |
682 | struct bt_component *sink; |
683 | GList *current_node; | |
684 | ||
fa6cfec3 | 685 | BT_ASSERT_PRE_DEV(graph->has_sink, |
8b45963b | 686 | "Graph has no sink component: %!+g", graph); |
a684a357 | 687 | BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph); |
c3ac0193 | 688 | |
85e7137b | 689 | if (G_UNLIKELY(g_queue_is_empty(graph->sinks_to_consume))) { |
a684a357 | 690 | BT_LOGD_STR("Graph's sink queue is empty: end of graph."); |
fb25b9e3 | 691 | status = BT_FUNC_STATUS_END; |
c3ac0193 PP |
692 | goto end; |
693 | } | |
694 | ||
695 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); | |
696 | sink = current_node->data; | |
a684a357 | 697 | BT_LIB_LOGD("Chose next sink to consume: %!+c", sink); |
c3ac0193 PP |
698 | status = consume_sink_node(graph, current_node); |
699 | ||
f60c8b34 JG |
700 | end: |
701 | return status; | |
c0418dd9 JG |
702 | } |
703 | ||
e023d253 | 704 | enum bt_graph_run_once_status bt_graph_run_once(struct bt_graph *graph) |
851b70bd | 705 | { |
e023d253 | 706 | enum bt_graph_run_once_status status; |
9ef22b36 | 707 | |
fa6cfec3 | 708 | BT_ASSERT_PRE_DEV_NON_NULL(graph, "Graph"); |
fa6cfec3 | 709 | BT_ASSERT_PRE_DEV(graph->can_consume, |
8b45963b | 710 | "Cannot consume graph in its current state: %!+g", graph); |
fa6cfec3 PP |
711 | BT_ASSERT_PRE_DEV(graph->config_state != |
712 | BT_GRAPH_CONFIGURATION_STATE_FAULTY, | |
9a84962b | 713 | "Graph is in a faulty state: %!+g", graph); |
9cce94e4 | 714 | bt_graph_set_can_consume(graph, false); |
1043fdea | 715 | status = bt_graph_configure(graph); |
85e7137b | 716 | if (G_UNLIKELY(status)) { |
1043fdea PP |
717 | /* bt_graph_configure() logs errors */ |
718 | goto end; | |
719 | } | |
720 | ||
834e9996 | 721 | status = consume_no_check(graph); |
9cce94e4 | 722 | bt_graph_set_can_consume(graph, true); |
1043fdea PP |
723 | |
724 | end: | |
f8cd6131 | 725 | return status; |
851b70bd PP |
726 | } |
727 | ||
fb25b9e3 | 728 | enum bt_graph_run_status bt_graph_run(struct bt_graph *graph) |
f60c8b34 | 729 | { |
fb25b9e3 | 730 | enum bt_graph_run_status status; |
f60c8b34 | 731 | |
834e9996 | 732 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
371361a1 PP |
733 | BT_ASSERT_PRE(graph->can_consume, |
734 | "Cannot consume graph in its current state: %!+g", graph); | |
9a84962b PP |
735 | BT_ASSERT_PRE(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY, |
736 | "Graph is in a faulty state: %!+g", graph); | |
9cce94e4 | 737 | bt_graph_set_can_consume(graph, false); |
1043fdea | 738 | status = bt_graph_configure(graph); |
85e7137b | 739 | if (G_UNLIKELY(status)) { |
1043fdea PP |
740 | /* bt_graph_configure() logs errors */ |
741 | goto end; | |
742 | } | |
743 | ||
a684a357 | 744 | BT_LIB_LOGI("Running graph: %!+g", graph); |
262e5473 | 745 | |
f60c8b34 | 746 | do { |
851b70bd | 747 | /* |
d73bb381 PP |
748 | * Check if the graph is interrupted at each iteration. |
749 | * If the graph was interrupted by another thread or by | |
750 | * a signal handler, this is NOT a warning nor an error; | |
751 | * it was intentional: log with an INFO level only. | |
851b70bd | 752 | */ |
d73bb381 PP |
753 | if (G_UNLIKELY(bt_graph_is_interrupted(graph))) { |
754 | BT_LIB_LOGI("Stopping the graph: " | |
755 | "graph was interrupted: %!+g", graph); | |
756 | status = BT_FUNC_STATUS_AGAIN; | |
851b70bd PP |
757 | goto end; |
758 | } | |
759 | ||
834e9996 | 760 | status = consume_no_check(graph); |
fb25b9e3 | 761 | if (G_UNLIKELY(status == BT_FUNC_STATUS_AGAIN)) { |
f60c8b34 | 762 | /* |
202a3a13 PP |
763 | * If AGAIN is received and there are multiple |
764 | * sinks, go ahead and consume from the next | |
765 | * sink. | |
f60c8b34 | 766 | * |
202a3a13 PP |
767 | * However, in the case where a single sink is |
768 | * left, the caller can decide to busy-wait and | |
7b53201c | 769 | * call bt_graph_run() continuously |
834e9996 PP |
770 | * until the source is ready or it can decide to |
771 | * sleep for an arbitrary amount of time. | |
f60c8b34 JG |
772 | */ |
773 | if (graph->sinks_to_consume->length > 1) { | |
fb25b9e3 | 774 | status = BT_FUNC_STATUS_OK; |
f60c8b34 JG |
775 | } |
776 | } | |
fb25b9e3 | 777 | } while (status == BT_FUNC_STATUS_OK); |
f60c8b34 JG |
778 | |
779 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
fb25b9e3 | 780 | status = BT_FUNC_STATUS_END; |
f60c8b34 | 781 | } |
262e5473 | 782 | |
202a3a13 | 783 | end: |
a684a357 | 784 | BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph, |
fb25b9e3 | 785 | bt_common_func_status_string(status)); |
9cce94e4 | 786 | bt_graph_set_can_consume(graph, true); |
72b913fb | 787 | return status; |
f60c8b34 | 788 | } |
1bf957a0 | 789 | |
fb25b9e3 | 790 | enum bt_graph_add_listener_status |
7b53201c PP |
791 | bt_graph_add_source_component_output_port_added_listener( |
792 | struct bt_graph *graph, | |
793 | bt_graph_source_component_output_port_added_listener_func func, | |
794 | bt_graph_listener_removed_func listener_removed, void *data, | |
eba7ea21 | 795 | bt_listener_id *out_listener_id) |
1bf957a0 | 796 | { |
834e9996 PP |
797 | struct bt_graph_listener_port_added listener = { |
798 | .base = { | |
799 | .removed = listener_removed, | |
800 | .data = data, | |
801 | }, | |
802 | .func = (port_added_func_t) func, | |
1bf957a0 | 803 | }; |
eba7ea21 | 804 | bt_listener_id listener_id; |
1bf957a0 | 805 | |
834e9996 PP |
806 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
807 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
808 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
809 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
810 | "Graph currently executing a \"listener removed\" listener: " | |
811 | "%!+g", graph); | |
812 | g_array_append_val(graph->listeners.source_output_port_added, listener); | |
813 | listener_id = graph->listeners.source_output_port_added->len - 1; | |
a684a357 | 814 | BT_LIB_LOGD("Added \"source component output port added\" listener to graph: " |
834e9996 PP |
815 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
816 | listener_id); | |
817 | ||
818 | if (listener_id) { | |
819 | *out_listener_id = listener_id; | |
820 | } | |
821 | ||
fb25b9e3 | 822 | return BT_FUNC_STATUS_OK; |
1bf957a0 PP |
823 | } |
824 | ||
fb25b9e3 | 825 | enum bt_graph_add_listener_status |
7b53201c PP |
826 | bt_graph_add_filter_component_output_port_added_listener( |
827 | struct bt_graph *graph, | |
828 | bt_graph_filter_component_output_port_added_listener_func func, | |
829 | bt_graph_listener_removed_func listener_removed, void *data, | |
eba7ea21 | 830 | bt_listener_id *out_listener_id) |
1bf957a0 | 831 | { |
834e9996 PP |
832 | struct bt_graph_listener_port_added listener = { |
833 | .base = { | |
834 | .removed = listener_removed, | |
835 | .data = data, | |
836 | }, | |
837 | .func = (port_added_func_t) func, | |
838 | }; | |
eba7ea21 | 839 | bt_listener_id listener_id; |
1bf957a0 | 840 | |
834e9996 PP |
841 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
842 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
843 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
844 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
845 | "Graph currently executing a \"listener removed\" listener: " | |
846 | "%!+g", graph); | |
847 | g_array_append_val(graph->listeners.filter_output_port_added, listener); | |
848 | listener_id = graph->listeners.filter_output_port_added->len - 1; | |
a684a357 | 849 | BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: " |
834e9996 PP |
850 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
851 | listener_id); | |
852 | ||
853 | if (listener_id) { | |
854 | *out_listener_id = listener_id; | |
855 | } | |
856 | ||
fb25b9e3 | 857 | return BT_FUNC_STATUS_OK; |
834e9996 | 858 | } |
262e5473 | 859 | |
fb25b9e3 | 860 | enum bt_graph_add_listener_status |
7b53201c PP |
861 | bt_graph_add_filter_component_input_port_added_listener( |
862 | struct bt_graph *graph, | |
863 | bt_graph_filter_component_input_port_added_listener_func func, | |
864 | bt_graph_listener_removed_func listener_removed, void *data, | |
eba7ea21 | 865 | bt_listener_id *out_listener_id) |
834e9996 | 866 | { |
834e9996 PP |
867 | struct bt_graph_listener_port_added listener = { |
868 | .base = { | |
869 | .removed = listener_removed, | |
870 | .data = data, | |
871 | }, | |
872 | .func = (port_added_func_t) func, | |
873 | }; | |
eba7ea21 | 874 | bt_listener_id listener_id; |
eb87cce7 | 875 | |
834e9996 PP |
876 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
877 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
878 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
879 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
880 | "Graph currently executing a \"listener removed\" listener: " | |
881 | "%!+g", graph); | |
882 | g_array_append_val(graph->listeners.filter_input_port_added, listener); | |
883 | listener_id = graph->listeners.filter_input_port_added->len - 1; | |
a684a357 | 884 | BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: " |
834e9996 PP |
885 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
886 | listener_id); | |
887 | ||
888 | if (listener_id) { | |
889 | *out_listener_id = listener_id; | |
890 | } | |
891 | ||
fb25b9e3 | 892 | return BT_FUNC_STATUS_OK; |
834e9996 | 893 | } |
1bf957a0 | 894 | |
fb25b9e3 | 895 | enum bt_graph_add_listener_status |
7b53201c PP |
896 | bt_graph_add_sink_component_input_port_added_listener( |
897 | struct bt_graph *graph, | |
898 | bt_graph_sink_component_input_port_added_listener_func func, | |
899 | bt_graph_listener_removed_func listener_removed, void *data, | |
eba7ea21 | 900 | bt_listener_id *out_listener_id) |
834e9996 | 901 | { |
834e9996 PP |
902 | struct bt_graph_listener_port_added listener = { |
903 | .base = { | |
904 | .removed = listener_removed, | |
905 | .data = data, | |
906 | }, | |
907 | .func = (port_added_func_t) func, | |
908 | }; | |
eba7ea21 | 909 | bt_listener_id listener_id; |
1bf957a0 | 910 | |
834e9996 PP |
911 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
912 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
913 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
914 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
915 | "Graph currently executing a \"listener removed\" listener: " | |
916 | "%!+g", graph); | |
917 | g_array_append_val(graph->listeners.sink_input_port_added, listener); | |
918 | listener_id = graph->listeners.sink_input_port_added->len - 1; | |
a684a357 | 919 | BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: " |
834e9996 PP |
920 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
921 | listener_id); | |
922 | ||
923 | if (listener_id) { | |
924 | *out_listener_id = listener_id; | |
925 | } | |
926 | ||
fb25b9e3 | 927 | return BT_FUNC_STATUS_OK; |
1bf957a0 PP |
928 | } |
929 | ||
fb25b9e3 | 930 | enum bt_graph_add_listener_status |
7b53201c PP |
931 | bt_graph_add_source_filter_component_ports_connected_listener( |
932 | struct bt_graph *graph, | |
933 | bt_graph_source_filter_component_ports_connected_listener_func func, | |
934 | bt_graph_listener_removed_func listener_removed, void *data, | |
eba7ea21 | 935 | bt_listener_id *out_listener_id) |
834e9996 | 936 | { |
834e9996 PP |
937 | struct bt_graph_listener_ports_connected listener = { |
938 | .base = { | |
939 | .removed = listener_removed, | |
940 | .data = data, | |
941 | }, | |
942 | .func = (ports_connected_func_t) func, | |
943 | }; | |
eba7ea21 | 944 | bt_listener_id listener_id; |
eb87cce7 | 945 | |
834e9996 PP |
946 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
947 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
948 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
949 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
950 | "Graph currently executing a \"listener removed\" listener: " | |
951 | "%!+g", graph); | |
952 | g_array_append_val(graph->listeners.source_filter_ports_connected, | |
953 | listener); | |
954 | listener_id = graph->listeners.source_filter_ports_connected->len - 1; | |
a684a357 | 955 | BT_LIB_LOGD("Added \"source to filter component ports connected\" listener to graph: " |
834e9996 PP |
956 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
957 | listener_id); | |
958 | ||
959 | if (listener_id) { | |
960 | *out_listener_id = listener_id; | |
961 | } | |
962 | ||
fb25b9e3 | 963 | return BT_FUNC_STATUS_OK; |
834e9996 | 964 | } |
1bf957a0 | 965 | |
fb25b9e3 | 966 | enum bt_graph_add_listener_status |
7b53201c PP |
967 | bt_graph_add_source_sink_component_ports_connected_listener( |
968 | struct bt_graph *graph, | |
969 | bt_graph_source_sink_component_ports_connected_listener_func func, | |
970 | bt_graph_listener_removed_func listener_removed, void *data, | |
eba7ea21 | 971 | bt_listener_id *out_listener_id) |
834e9996 | 972 | { |
834e9996 PP |
973 | struct bt_graph_listener_ports_connected listener = { |
974 | .base = { | |
975 | .removed = listener_removed, | |
976 | .data = data, | |
977 | }, | |
978 | .func = (ports_connected_func_t) func, | |
979 | }; | |
eba7ea21 | 980 | bt_listener_id listener_id; |
1bf957a0 | 981 | |
834e9996 PP |
982 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
983 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
984 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
985 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
986 | "Graph currently executing a \"listener removed\" listener: " | |
987 | "%!+g", graph); | |
988 | g_array_append_val(graph->listeners.source_sink_ports_connected, | |
989 | listener); | |
990 | listener_id = graph->listeners.source_sink_ports_connected->len - 1; | |
a684a357 | 991 | BT_LIB_LOGD("Added \"source to sink component ports connected\" listener to graph: " |
834e9996 PP |
992 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
993 | listener_id); | |
994 | ||
995 | if (listener_id) { | |
996 | *out_listener_id = listener_id; | |
997 | } | |
998 | ||
fb25b9e3 | 999 | return BT_FUNC_STATUS_OK; |
1bf957a0 PP |
1000 | } |
1001 | ||
fb25b9e3 | 1002 | enum bt_graph_add_listener_status |
ee976410 PP |
1003 | bt_graph_add_filter_filter_component_ports_connected_listener( |
1004 | struct bt_graph *graph, | |
1005 | bt_graph_filter_filter_component_ports_connected_listener_func func, | |
1006 | bt_graph_listener_removed_func listener_removed, void *data, | |
eba7ea21 | 1007 | bt_listener_id *out_listener_id) |
ee976410 PP |
1008 | { |
1009 | struct bt_graph_listener_ports_connected listener = { | |
1010 | .base = { | |
1011 | .removed = listener_removed, | |
1012 | .data = data, | |
1013 | }, | |
1014 | .func = (ports_connected_func_t) func, | |
1015 | }; | |
eba7ea21 | 1016 | bt_listener_id listener_id; |
ee976410 PP |
1017 | |
1018 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); | |
1019 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
1020 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
1021 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
1022 | "Graph currently executing a \"listener removed\" listener: " | |
1023 | "%!+g", graph); | |
1024 | g_array_append_val(graph->listeners.filter_filter_ports_connected, | |
1025 | listener); | |
1026 | listener_id = graph->listeners.filter_filter_ports_connected->len - 1; | |
a684a357 | 1027 | BT_LIB_LOGD("Added \"filter to filter component ports connected\" listener to graph: " |
ee976410 PP |
1028 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
1029 | listener_id); | |
1030 | ||
1031 | if (listener_id) { | |
1032 | *out_listener_id = listener_id; | |
1033 | } | |
1034 | ||
fb25b9e3 | 1035 | return BT_FUNC_STATUS_OK; |
ee976410 PP |
1036 | } |
1037 | ||
fb25b9e3 | 1038 | enum bt_graph_add_listener_status |
7b53201c PP |
1039 | bt_graph_add_filter_sink_component_ports_connected_listener( |
1040 | struct bt_graph *graph, | |
1041 | bt_graph_filter_sink_component_ports_connected_listener_func func, | |
1042 | bt_graph_listener_removed_func listener_removed, void *data, | |
eba7ea21 | 1043 | bt_listener_id *out_listener_id) |
1bf957a0 | 1044 | { |
834e9996 PP |
1045 | struct bt_graph_listener_ports_connected listener = { |
1046 | .base = { | |
1047 | .removed = listener_removed, | |
1048 | .data = data, | |
1049 | }, | |
1050 | .func = (ports_connected_func_t) func, | |
1051 | }; | |
eba7ea21 | 1052 | bt_listener_id listener_id; |
1bf957a0 | 1053 | |
834e9996 PP |
1054 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
1055 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
1056 | BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener"); | |
1057 | BT_ASSERT_PRE(!graph->in_remove_listener, | |
1058 | "Graph currently executing a \"listener removed\" listener: " | |
1059 | "%!+g", graph); | |
1060 | g_array_append_val(graph->listeners.filter_sink_ports_connected, | |
1061 | listener); | |
1062 | listener_id = graph->listeners.filter_sink_ports_connected->len - 1; | |
a684a357 | 1063 | BT_LIB_LOGD("Added \"filter to sink component ports connected\" listener to graph: " |
834e9996 PP |
1064 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
1065 | listener_id); | |
1066 | ||
1067 | if (listener_id) { | |
1068 | *out_listener_id = listener_id; | |
1069 | } | |
1070 | ||
fb25b9e3 | 1071 | return BT_FUNC_STATUS_OK; |
834e9996 | 1072 | } |
262e5473 | 1073 | |
1bf957a0 | 1074 | BT_HIDDEN |
fb25b9e3 | 1075 | enum bt_graph_listener_func_status bt_graph_notify_port_added( |
e18f3e2b | 1076 | struct bt_graph *graph, struct bt_port *port) |
1bf957a0 | 1077 | { |
834e9996 PP |
1078 | uint64_t i; |
1079 | GArray *listeners; | |
1080 | struct bt_component *comp; | |
fb25b9e3 | 1081 | enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK; |
1bf957a0 | 1082 | |
834e9996 PP |
1083 | BT_ASSERT(graph); |
1084 | BT_ASSERT(port); | |
a684a357 | 1085 | BT_LIB_LOGD("Notifying graph listeners that a port was added: " |
834e9996 | 1086 | "%![graph-]+g, %![port-]+p", graph, port); |
7b53201c | 1087 | comp = bt_port_borrow_component_inline(port); |
834e9996 PP |
1088 | BT_ASSERT(comp); |
1089 | ||
1090 | switch (comp->class->type) { | |
1091 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
1092 | { | |
1093 | switch (port->type) { | |
1094 | case BT_PORT_TYPE_OUTPUT: | |
1095 | listeners = graph->listeners.source_output_port_added; | |
1096 | break; | |
1097 | default: | |
1098 | abort(); | |
1099 | } | |
1100 | ||
1101 | break; | |
1102 | } | |
1103 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
1104 | { | |
1105 | switch (port->type) { | |
1106 | case BT_PORT_TYPE_INPUT: | |
1107 | listeners = graph->listeners.filter_input_port_added; | |
1108 | break; | |
1109 | case BT_PORT_TYPE_OUTPUT: | |
1110 | listeners = graph->listeners.filter_output_port_added; | |
1111 | break; | |
1112 | default: | |
1113 | abort(); | |
1114 | } | |
262e5473 | 1115 | |
834e9996 PP |
1116 | break; |
1117 | } | |
1118 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
1119 | { | |
1120 | switch (port->type) { | |
1121 | case BT_PORT_TYPE_INPUT: | |
1122 | listeners = graph->listeners.sink_input_port_added; | |
1123 | break; | |
1124 | default: | |
1125 | abort(); | |
1126 | } | |
1bf957a0 | 1127 | |
834e9996 PP |
1128 | break; |
1129 | } | |
1130 | default: | |
1131 | abort(); | |
1132 | } | |
1133 | ||
1134 | for (i = 0; i < listeners->len; i++) { | |
1135 | struct bt_graph_listener_port_added *listener = | |
1136 | &g_array_index(listeners, | |
1137 | struct bt_graph_listener_port_added, i); | |
1138 | ||
e18f3e2b | 1139 | |
834e9996 | 1140 | BT_ASSERT(listener->func); |
e18f3e2b | 1141 | status = listener->func(comp, port, listener->base.data); |
fb25b9e3 | 1142 | if (status != BT_FUNC_STATUS_OK) { |
e18f3e2b SM |
1143 | goto end; |
1144 | } | |
1bf957a0 | 1145 | } |
e18f3e2b SM |
1146 | |
1147 | end: | |
1148 | return status; | |
1bf957a0 PP |
1149 | } |
1150 | ||
1bf957a0 | 1151 | BT_HIDDEN |
fb25b9e3 | 1152 | enum bt_graph_listener_func_status bt_graph_notify_ports_connected( |
e18f3e2b SM |
1153 | struct bt_graph *graph, struct bt_port *upstream_port, |
1154 | struct bt_port *downstream_port) | |
1bf957a0 | 1155 | { |
834e9996 PP |
1156 | uint64_t i; |
1157 | GArray *listeners; | |
1158 | struct bt_component *upstream_comp; | |
1159 | struct bt_component *downstream_comp; | |
fb25b9e3 | 1160 | enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK; |
1bf957a0 | 1161 | |
834e9996 PP |
1162 | BT_ASSERT(graph); |
1163 | BT_ASSERT(upstream_port); | |
1164 | BT_ASSERT(downstream_port); | |
a684a357 | 1165 | BT_LIB_LOGD("Notifying graph listeners that ports were connected: " |
834e9996 PP |
1166 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", |
1167 | graph, upstream_port, downstream_port); | |
7b53201c | 1168 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
834e9996 | 1169 | BT_ASSERT(upstream_comp); |
7b53201c | 1170 | downstream_comp = bt_port_borrow_component_inline(downstream_port); |
834e9996 PP |
1171 | BT_ASSERT(downstream_comp); |
1172 | ||
1173 | switch (upstream_comp->class->type) { | |
1174 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
1175 | { | |
1176 | switch (downstream_comp->class->type) { | |
1177 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
1178 | listeners = | |
1179 | graph->listeners.source_filter_ports_connected; | |
1180 | break; | |
1181 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
1182 | listeners = | |
1183 | graph->listeners.source_sink_ports_connected; | |
1184 | break; | |
1185 | default: | |
1186 | abort(); | |
1187 | } | |
262e5473 | 1188 | |
834e9996 PP |
1189 | break; |
1190 | } | |
1191 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
1192 | { | |
1193 | switch (downstream_comp->class->type) { | |
ee976410 PP |
1194 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
1195 | listeners = | |
1196 | graph->listeners.filter_filter_ports_connected; | |
1197 | break; | |
834e9996 PP |
1198 | case BT_COMPONENT_CLASS_TYPE_SINK: |
1199 | listeners = | |
1200 | graph->listeners.filter_sink_ports_connected; | |
1201 | break; | |
1202 | default: | |
1203 | abort(); | |
1204 | } | |
1bf957a0 | 1205 | |
834e9996 PP |
1206 | break; |
1207 | } | |
1208 | default: | |
1209 | abort(); | |
1210 | } | |
1211 | ||
1212 | for (i = 0; i < listeners->len; i++) { | |
1213 | struct bt_graph_listener_ports_connected *listener = | |
1214 | &g_array_index(listeners, | |
1215 | struct bt_graph_listener_ports_connected, i); | |
1216 | ||
1217 | BT_ASSERT(listener->func); | |
e18f3e2b | 1218 | status = listener->func(upstream_comp, downstream_comp, |
834e9996 | 1219 | upstream_port, downstream_port, listener->base.data); |
fb25b9e3 | 1220 | if (status != BT_FUNC_STATUS_OK) { |
e18f3e2b SM |
1221 | goto end; |
1222 | } | |
1bf957a0 | 1223 | } |
e18f3e2b SM |
1224 | |
1225 | end: | |
1226 | return status; | |
1bf957a0 PP |
1227 | } |
1228 | ||
f167d3c0 PP |
1229 | BT_HIDDEN |
1230 | void bt_graph_remove_connection(struct bt_graph *graph, | |
1231 | struct bt_connection *connection) | |
1232 | { | |
8b45963b PP |
1233 | BT_ASSERT(graph); |
1234 | BT_ASSERT(connection); | |
a684a357 | 1235 | BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x", |
262e5473 | 1236 | graph, connection); |
f167d3c0 PP |
1237 | g_ptr_array_remove(graph->connections, connection); |
1238 | } | |
36712f1d | 1239 | |
834e9996 PP |
1240 | static inline |
1241 | bool component_name_exists(struct bt_graph *graph, const char *name) | |
1242 | { | |
1243 | bool exists = false; | |
1244 | uint64_t i; | |
1245 | ||
1246 | for (i = 0; i < graph->components->len; i++) { | |
1247 | struct bt_component *other_comp = graph->components->pdata[i]; | |
1248 | ||
1249 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { | |
1250 | BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: " | |
1251 | "%![other-comp-]+c, name=\"%s\"", | |
1252 | other_comp, name); | |
1253 | exists = true; | |
1254 | goto end; | |
1255 | } | |
1256 | } | |
1257 | ||
1258 | end: | |
1259 | return exists; | |
1260 | } | |
1261 | ||
1262 | static | |
fb25b9e3 | 1263 | int add_component_with_init_method_data( |
7b53201c | 1264 | struct bt_graph *graph, |
834e9996 PP |
1265 | struct bt_component_class *comp_cls, |
1266 | comp_init_method_t init_method, | |
ce141536 | 1267 | const char *name, const struct bt_value *params, |
cc81b5ab | 1268 | void *init_method_data, bt_logging_level log_level, |
8345fc26 | 1269 | const struct bt_component **user_component) |
36712f1d | 1270 | { |
fb25b9e3 PP |
1271 | int status = BT_FUNC_STATUS_OK; |
1272 | enum bt_component_class_init_method_status init_status; | |
36712f1d | 1273 | struct bt_component *component = NULL; |
834e9996 PP |
1274 | int ret; |
1275 | bool init_can_consume; | |
ce141536 | 1276 | struct bt_value *new_params = NULL; |
36712f1d | 1277 | |
834e9996 PP |
1278 | BT_ASSERT(comp_cls); |
1279 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); | |
1280 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
1043fdea PP |
1281 | BT_ASSERT_PRE( |
1282 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
9a84962b | 1283 | "Graph is not in the \"configuring\" state: %!+g", graph); |
834e9996 PP |
1284 | BT_ASSERT_PRE(!component_name_exists(graph, name), |
1285 | "Duplicate component name: %!+g, name=\"%s\"", graph, name); | |
1286 | BT_ASSERT_PRE(!params || bt_value_is_map(params), | |
1287 | "Parameter value is not a map value: %!+v", params); | |
3afb93ee | 1288 | init_can_consume = graph->can_consume; |
834e9996 | 1289 | bt_graph_set_can_consume(graph, false); |
a684a357 | 1290 | BT_LIB_LOGI("Adding component to graph: " |
cc81b5ab PP |
1291 | "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, " |
1292 | "%![params-]+v, init-method-data-addr=%p", | |
1293 | graph, comp_cls, name, | |
1294 | bt_common_logging_level_string(log_level), params, | |
1295 | init_method_data); | |
36712f1d | 1296 | |
834e9996 | 1297 | if (!params) { |
ce141536 PP |
1298 | new_params = bt_value_map_create(); |
1299 | if (!new_params) { | |
a8f90e5d PP |
1300 | BT_LIB_LOGE_APPEND_CAUSE( |
1301 | "Cannot create empty map value object."); | |
fb25b9e3 | 1302 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
36712f1d PP |
1303 | goto end; |
1304 | } | |
ce141536 PP |
1305 | |
1306 | params = new_params; | |
36712f1d PP |
1307 | } |
1308 | ||
cc81b5ab | 1309 | ret = bt_component_create(comp_cls, name, log_level, &component); |
834e9996 | 1310 | if (ret) { |
a8f90e5d PP |
1311 | BT_LIB_LOGE_APPEND_CAUSE( |
1312 | "Cannot create empty component object: ret=%d", | |
834e9996 | 1313 | ret); |
fb25b9e3 | 1314 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
36712f1d PP |
1315 | goto end; |
1316 | } | |
1317 | ||
1318 | /* | |
1319 | * The user's initialization method needs to see that this | |
1320 | * component is part of the graph. If the user method fails, we | |
1321 | * immediately remove the component from the graph's components. | |
1322 | */ | |
1323 | g_ptr_array_add(graph->components, component); | |
1324 | bt_component_set_graph(component, graph); | |
f433d1bf | 1325 | bt_value_freeze(params); |
36712f1d | 1326 | |
834e9996 | 1327 | if (init_method) { |
36712f1d | 1328 | BT_LOGD_STR("Calling user's initialization method."); |
fb25b9e3 | 1329 | init_status = init_method(component, params, init_method_data); |
36712f1d | 1330 | BT_LOGD("User method returned: status=%s", |
fb25b9e3 PP |
1331 | bt_common_func_status_string(init_status)); |
1332 | if (init_status != BT_FUNC_STATUS_OK) { | |
a8f90e5d PP |
1333 | if (init_status < 0) { |
1334 | BT_LIB_LOGW_APPEND_CAUSE( | |
1335 | "Component initialization method failed: " | |
1336 | "status=%s, %![comp-]+c", | |
1337 | bt_common_func_status_string(init_status), | |
1338 | component); | |
1339 | } | |
1340 | ||
fb25b9e3 | 1341 | status = init_status; |
36712f1d PP |
1342 | bt_component_set_graph(component, NULL); |
1343 | g_ptr_array_remove_fast(graph->components, component); | |
1344 | goto end; | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | /* | |
1349 | * Mark the component as initialized so that its finalization | |
1350 | * method is called when it is destroyed. | |
1351 | */ | |
1352 | component->initialized = true; | |
1353 | ||
1354 | /* | |
1355 | * If it's a sink component, it needs to be part of the graph's | |
e023d253 PP |
1356 | * sink queue to be consumed by bt_graph_run() or |
1357 | * bt_graph_run_once(). | |
36712f1d PP |
1358 | */ |
1359 | if (bt_component_is_sink(component)) { | |
834e9996 | 1360 | graph->has_sink = true; |
36712f1d PP |
1361 | g_queue_push_tail(graph->sinks_to_consume, component); |
1362 | } | |
1363 | ||
1364 | /* | |
1365 | * Freeze the component class now that it's instantiated at | |
1366 | * least once. | |
1367 | */ | |
1368 | BT_LOGD_STR("Freezing component class."); | |
834e9996 | 1369 | bt_component_class_freeze(comp_cls); |
a684a357 | 1370 | BT_LIB_LOGI("Added component to graph: " |
cc81b5ab PP |
1371 | "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, " |
1372 | "%![params-]+v, init-method-data-addr=%p, %![comp-]+c", | |
1373 | graph, comp_cls, name, | |
1374 | bt_common_logging_level_string(log_level), params, | |
1375 | init_method_data, component); | |
36712f1d PP |
1376 | |
1377 | if (user_component) { | |
1378 | /* Move reference to user */ | |
1379 | *user_component = component; | |
1380 | component = NULL; | |
1381 | } | |
1382 | ||
1383 | end: | |
fb25b9e3 | 1384 | if (status != BT_FUNC_STATUS_OK) { |
e18f3e2b | 1385 | bt_graph_make_faulty(graph); |
9a84962b PP |
1386 | } |
1387 | ||
8138bfe1 | 1388 | bt_object_put_ref(component); |
ce141536 | 1389 | bt_object_put_ref(new_params); |
834e9996 PP |
1390 | (void) init_can_consume; |
1391 | bt_graph_set_can_consume(graph, init_can_consume); | |
fb25b9e3 | 1392 | return status; |
36712f1d PP |
1393 | } |
1394 | ||
fb25b9e3 | 1395 | enum bt_graph_add_component_status |
7b53201c PP |
1396 | bt_graph_add_source_component_with_init_method_data( |
1397 | struct bt_graph *graph, | |
1398 | const struct bt_component_class_source *comp_cls, | |
ce141536 | 1399 | const char *name, const struct bt_value *params, |
cc81b5ab | 1400 | void *init_method_data, bt_logging_level log_level, |
7b53201c | 1401 | const struct bt_component_source **component) |
834e9996 PP |
1402 | { |
1403 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); | |
1404 | return add_component_with_init_method_data(graph, | |
1405 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
cc81b5ab | 1406 | name, params, init_method_data, log_level, (void *) component); |
834e9996 PP |
1407 | } |
1408 | ||
fb25b9e3 | 1409 | enum bt_graph_add_component_status bt_graph_add_source_component( |
7b53201c PP |
1410 | struct bt_graph *graph, |
1411 | const struct bt_component_class_source *comp_cls, | |
ce141536 | 1412 | const char *name, const struct bt_value *params, |
8345fc26 | 1413 | enum bt_logging_level log_level, |
7b53201c | 1414 | const struct bt_component_source **component) |
834e9996 | 1415 | { |
7b53201c | 1416 | return bt_graph_add_source_component_with_init_method_data( |
cc81b5ab | 1417 | graph, comp_cls, name, params, NULL, log_level, component); |
834e9996 PP |
1418 | } |
1419 | ||
fb25b9e3 | 1420 | enum bt_graph_add_component_status |
7b53201c PP |
1421 | bt_graph_add_filter_component_with_init_method_data( |
1422 | struct bt_graph *graph, | |
1423 | const struct bt_component_class_filter *comp_cls, | |
ce141536 | 1424 | const char *name, const struct bt_value *params, |
8345fc26 | 1425 | void *init_method_data, enum bt_logging_level log_level, |
7b53201c | 1426 | const struct bt_component_filter **component) |
834e9996 PP |
1427 | { |
1428 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); | |
1429 | return add_component_with_init_method_data(graph, | |
1430 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
cc81b5ab | 1431 | name, params, init_method_data, log_level, (void *) component); |
834e9996 PP |
1432 | } |
1433 | ||
fb25b9e3 | 1434 | enum bt_graph_add_component_status bt_graph_add_filter_component( |
7b53201c PP |
1435 | struct bt_graph *graph, |
1436 | const struct bt_component_class_filter *comp_cls, | |
ce141536 | 1437 | const char *name, const struct bt_value *params, |
8345fc26 | 1438 | enum bt_logging_level log_level, |
7b53201c | 1439 | const struct bt_component_filter **component) |
834e9996 | 1440 | { |
7b53201c | 1441 | return bt_graph_add_filter_component_with_init_method_data( |
cc81b5ab | 1442 | graph, comp_cls, name, params, NULL, log_level, component); |
834e9996 PP |
1443 | } |
1444 | ||
fb25b9e3 | 1445 | enum bt_graph_add_component_status |
7b53201c PP |
1446 | bt_graph_add_sink_component_with_init_method_data( |
1447 | struct bt_graph *graph, | |
1448 | const struct bt_component_class_sink *comp_cls, | |
ce141536 | 1449 | const char *name, const struct bt_value *params, |
8345fc26 | 1450 | void *init_method_data, enum bt_logging_level log_level, |
7b53201c | 1451 | const struct bt_component_sink **component) |
36712f1d | 1452 | { |
834e9996 PP |
1453 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
1454 | return add_component_with_init_method_data(graph, | |
1455 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
cc81b5ab | 1456 | name, params, init_method_data, log_level, (void *) component); |
834e9996 PP |
1457 | } |
1458 | ||
fb25b9e3 | 1459 | enum bt_graph_add_component_status bt_graph_add_sink_component( |
7b53201c PP |
1460 | struct bt_graph *graph, |
1461 | const struct bt_component_class_sink *comp_cls, | |
ce141536 | 1462 | const char *name, const struct bt_value *params, |
8345fc26 | 1463 | enum bt_logging_level log_level, |
7b53201c | 1464 | const struct bt_component_sink **component) |
834e9996 | 1465 | { |
7b53201c | 1466 | return bt_graph_add_sink_component_with_init_method_data( |
cc81b5ab | 1467 | graph, comp_cls, name, params, NULL, log_level, component); |
36712f1d | 1468 | } |
c3ac0193 | 1469 | |
5efafa1a PP |
1470 | enum bt_graph_add_component_status |
1471 | bt_graph_add_simple_sink_component(struct bt_graph *graph, const char *name, | |
1472 | bt_graph_simple_sink_component_init_func init_func, | |
1473 | bt_graph_simple_sink_component_consume_func consume_func, | |
1474 | bt_graph_simple_sink_component_finalize_func finalize_func, | |
1475 | void *user_data, const bt_component_sink **component) | |
1476 | { | |
1477 | enum bt_graph_add_component_status status; | |
1478 | struct bt_component_class_sink *comp_cls; | |
1479 | struct simple_sink_init_method_data init_method_data = { | |
1480 | .init_func = init_func, | |
1481 | .consume_func = consume_func, | |
1482 | .finalize_func = finalize_func, | |
1483 | .user_data = user_data, | |
1484 | }; | |
1485 | ||
1486 | /* | |
1487 | * Other preconditions are checked by | |
1488 | * bt_graph_add_sink_component_with_init_method_data(). | |
1489 | */ | |
1490 | BT_ASSERT_PRE_NON_NULL(consume_func, "Consume function"); | |
1491 | ||
1492 | comp_cls = bt_component_class_sink_simple_borrow(); | |
1493 | if (!comp_cls) { | |
1494 | BT_LIB_LOGE_APPEND_CAUSE( | |
1495 | "Cannot borrow simple sink component class."); | |
1496 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
1497 | goto end; | |
1498 | } | |
1499 | ||
1500 | status = bt_graph_add_sink_component_with_init_method_data(graph, | |
1501 | comp_cls, name, NULL, &init_method_data, | |
1502 | BT_LOGGING_LEVEL_NONE, component); | |
1503 | ||
1504 | end: | |
1505 | return status; | |
1506 | } | |
1507 | ||
c3ac0193 PP |
1508 | BT_HIDDEN |
1509 | int bt_graph_remove_unconnected_component(struct bt_graph *graph, | |
1510 | struct bt_component *component) | |
1511 | { | |
834e9996 PP |
1512 | bool init_can_consume; |
1513 | uint64_t count; | |
c3ac0193 PP |
1514 | uint64_t i; |
1515 | int ret = 0; | |
1516 | ||
8b45963b PP |
1517 | BT_ASSERT(graph); |
1518 | BT_ASSERT(component); | |
1d7bf349 | 1519 | BT_ASSERT(component->base.ref_count == 0); |
8b45963b | 1520 | BT_ASSERT(bt_component_borrow_graph(component) == graph); |
c3ac0193 | 1521 | |
3afb93ee | 1522 | init_can_consume = graph->can_consume; |
c3ac0193 PP |
1523 | count = bt_component_get_input_port_count(component); |
1524 | ||
1525 | for (i = 0; i < count; i++) { | |
834e9996 PP |
1526 | struct bt_port *port = (void *) |
1527 | bt_component_borrow_input_port_by_index(component, i); | |
c3ac0193 | 1528 | |
8b45963b | 1529 | BT_ASSERT(port); |
c3ac0193 PP |
1530 | |
1531 | if (bt_port_is_connected(port)) { | |
a8f90e5d PP |
1532 | BT_LIB_LOGW_APPEND_CAUSE( |
1533 | "Cannot remove component from graph: " | |
c3ac0193 | 1534 | "an input port is connected: " |
834e9996 PP |
1535 | "%![graph-]+g, %![comp-]+c, %![port-]+p", |
1536 | graph, component, port); | |
c3ac0193 PP |
1537 | goto error; |
1538 | } | |
1539 | } | |
1540 | ||
1541 | count = bt_component_get_output_port_count(component); | |
1542 | ||
1543 | for (i = 0; i < count; i++) { | |
834e9996 PP |
1544 | struct bt_port *port = (void *) |
1545 | bt_component_borrow_output_port_by_index(component, i); | |
c3ac0193 | 1546 | |
8b45963b | 1547 | BT_ASSERT(port); |
c3ac0193 PP |
1548 | |
1549 | if (bt_port_is_connected(port)) { | |
a8f90e5d PP |
1550 | BT_LIB_LOGW_APPEND_CAUSE( |
1551 | "Cannot remove component from graph: " | |
c3ac0193 | 1552 | "an output port is connected: " |
834e9996 PP |
1553 | "%![graph-]+g, %![comp-]+c, %![port-]+p", |
1554 | graph, component, port); | |
c3ac0193 PP |
1555 | goto error; |
1556 | } | |
1557 | } | |
1558 | ||
834e9996 | 1559 | bt_graph_set_can_consume(graph, false); |
c3ac0193 PP |
1560 | |
1561 | /* Possibly remove from sinks to consume */ | |
1562 | (void) g_queue_remove(graph->sinks_to_consume, component); | |
1563 | ||
1564 | if (graph->sinks_to_consume->length == 0) { | |
834e9996 | 1565 | graph->has_sink = false; |
c3ac0193 PP |
1566 | } |
1567 | ||
1568 | /* | |
1d7bf349 PP |
1569 | * This calls bt_object_try_spec_release() on the component, and |
1570 | * since its reference count is 0, its destructor is called. Its | |
c3ac0193 PP |
1571 | * destructor calls the user's finalization method (if set). |
1572 | */ | |
1573 | g_ptr_array_remove(graph->components, component); | |
1574 | goto end; | |
1575 | ||
1576 | error: | |
1577 | ret = -1; | |
1578 | ||
1579 | end: | |
834e9996 PP |
1580 | (void) init_can_consume; |
1581 | bt_graph_set_can_consume(graph, init_can_consume); | |
c3ac0193 PP |
1582 | return ret; |
1583 | } | |
f7c3ac09 PP |
1584 | |
1585 | BT_HIDDEN | |
b09a5592 PP |
1586 | void bt_graph_add_message(struct bt_graph *graph, |
1587 | struct bt_message *msg) | |
f7c3ac09 PP |
1588 | { |
1589 | BT_ASSERT(graph); | |
b09a5592 | 1590 | BT_ASSERT(msg); |
f7c3ac09 PP |
1591 | |
1592 | /* | |
1593 | * It's okay not to take a reference because, when a | |
b09a5592 | 1594 | * message's reference count drops to 0, either: |
f7c3ac09 PP |
1595 | * |
1596 | * * It is recycled back to one of this graph's pool. | |
1597 | * * It is destroyed because it doesn't have any link to any | |
1598 | * graph, which means the original graph is already destroyed. | |
1599 | */ | |
b09a5592 | 1600 | g_ptr_array_add(graph->messages, msg); |
f7c3ac09 | 1601 | } |
8c6884d9 | 1602 | |
d73bb381 PP |
1603 | BT_HIDDEN |
1604 | bool bt_graph_is_interrupted(const struct bt_graph *graph) | |
1605 | { | |
1606 | BT_ASSERT(graph); | |
1607 | return bt_interrupter_array_any_is_set(graph->interrupters); | |
1608 | } | |
1609 | ||
1610 | enum bt_graph_add_interrupter_status bt_graph_add_interrupter( | |
1611 | struct bt_graph *graph, const struct bt_interrupter *intr) | |
1612 | { | |
1613 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); | |
1614 | BT_ASSERT_PRE_NON_NULL(intr, "Interrupter"); | |
1615 | g_ptr_array_add(graph->interrupters, (void *) intr); | |
1616 | bt_object_get_no_null_check(intr); | |
1617 | BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z", | |
1618 | graph, intr); | |
1619 | return BT_FUNC_STATUS_OK; | |
1620 | } | |
1621 | ||
1622 | void bt_graph_interrupt(struct bt_graph *graph) | |
1623 | { | |
1624 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); | |
1625 | bt_interrupter_set(graph->default_interrupter); | |
1626 | BT_LIB_LOGI("Interrupted graph: %!+g", graph); | |
1627 | } | |
1628 | ||
8c6884d9 PP |
1629 | void bt_graph_get_ref(const struct bt_graph *graph) |
1630 | { | |
1631 | bt_object_get_ref(graph); | |
1632 | } | |
1633 | ||
1634 | void bt_graph_put_ref(const struct bt_graph *graph) | |
1635 | { | |
1636 | bt_object_put_ref(graph); | |
1637 | } |