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