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