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