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