Commit | Line | Data |
---|---|---|
c0418dd9 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
f60c8b34 | 3 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
c0418dd9 JG |
4 | * |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/GRAPH" |
c2d9d9cf | 25 | #include "lib/logging.h" |
262e5473 | 26 | |
578e048b MJ |
27 | #include "common/assert.h" |
28 | #include "lib/assert-pre.h" | |
f6f301d7 | 29 | #include "lib/assert-post.h" |
3fadfbc0 MJ |
30 | #include <babeltrace2/graph/graph.h> |
31 | #include <babeltrace2/graph/graph-const.h> | |
3fadfbc0 MJ |
32 | #include <babeltrace2/graph/component-source-const.h> |
33 | #include <babeltrace2/graph/component-filter-const.h> | |
34 | #include <babeltrace2/graph/port-const.h> | |
578e048b MJ |
35 | #include "lib/graph/message/message.h" |
36 | #include "compat/compiler.h" | |
37 | #include "common/common.h" | |
3fadfbc0 MJ |
38 | #include <babeltrace2/types.h> |
39 | #include <babeltrace2/value.h> | |
40 | #include <babeltrace2/value-const.h> | |
578e048b | 41 | #include "lib/value.h" |
f60c8b34 | 42 | #include <unistd.h> |
c4f23e30 | 43 | #include <stdbool.h> |
1bf957a0 PP |
44 | #include <glib.h> |
45 | ||
078033ed | 46 | #include "component-class-sink-simple.h" |
578e048b MJ |
47 | #include "component.h" |
48 | #include "component-sink.h" | |
49 | #include "connection.h" | |
50 | #include "graph.h" | |
9b4f9b42 | 51 | #include "interrupter.h" |
578e048b MJ |
52 | #include "message/event.h" |
53 | #include "message/packet.h" | |
54 | ||
d24d5663 PP |
55 | typedef enum bt_graph_listener_func_status |
56 | (*port_added_func_t)(const void *, const void *, void *); | |
0d72b8c3 | 57 | |
21a9f056 | 58 | typedef enum bt_component_class_initialize_method_status |
59225a3e | 59 | (*comp_init_method_t)(const void *, void *, const void *, void *); |
d24d5663 | 60 | |
d94d92ac | 61 | struct bt_graph_listener_port_added { |
d94d92ac | 62 | port_added_func_t func; |
f3d6b4c2 | 63 | void *data; |
d94d92ac | 64 | }; |
8cc092c9 | 65 | |
d94d92ac PP |
66 | #define INIT_LISTENERS_ARRAY(_type, _listeners) \ |
67 | do { \ | |
68 | _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \ | |
69 | if (!(_listeners)) { \ | |
870631a2 PP |
70 | BT_LIB_LOGE_APPEND_CAUSE( \ |
71 | "Failed to allocate one GArray."); \ | |
d94d92ac PP |
72 | } \ |
73 | } while (0) | |
74 | ||
f60c8b34 | 75 | static |
d94d92ac | 76 | void destroy_graph(struct bt_object *obj) |
c0418dd9 | 77 | { |
0d72b8c3 | 78 | struct bt_graph *graph = container_of(obj, struct bt_graph, base); |
c0418dd9 | 79 | |
bd14d768 PP |
80 | /* |
81 | * The graph's reference count is 0 if we're here. Increment | |
82 | * it to avoid a double-destroy (possibly infinitely recursive) | |
83 | * in this situation: | |
84 | * | |
85 | * 1. We put and destroy a connection. | |
d0fea130 PP |
86 | * 2. This connection's destructor finalizes its active message |
87 | * iterators. | |
88 | * 3. A message iterator's finalization function gets a new | |
89 | * reference on its component (reference count goes from 0 to | |
90 | * 1). | |
bd14d768 PP |
91 | * 4. Since this component's reference count goes to 1, it takes |
92 | * a reference on its parent (this graph). This graph's | |
93 | * reference count goes from 0 to 1. | |
d6e69534 | 94 | * 5. The message iterator's finalization function puts its |
bd14d768 PP |
95 | * component reference (reference count goes from 1 to 0). |
96 | * 6. Since this component's reference count goes from 1 to 0, | |
97 | * it puts its parent (this graph). This graph's reference | |
98 | * count goes from 1 to 0. | |
d0fea130 PP |
99 | * 7. Since this graph's reference count goes from 1 to 0, its |
100 | * destructor is called (this function). | |
bd14d768 PP |
101 | * |
102 | * With the incrementation below, the graph's reference count at | |
103 | * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This | |
104 | * ensures that this function is not called two times. | |
105 | */ | |
3f7d4d90 | 106 | BT_LIB_LOGI("Destroying graph: %!+g", graph); |
3fea54f6 | 107 | obj->ref_count++; |
9b4f9b42 | 108 | graph->config_state = BT_GRAPH_CONFIGURATION_STATE_DESTROYING; |
49682acd | 109 | |
d6e69534 PP |
110 | if (graph->messages) { |
111 | g_ptr_array_free(graph->messages, TRUE); | |
112 | graph->messages = NULL; | |
5c563278 PP |
113 | } |
114 | ||
c0418dd9 | 115 | if (graph->connections) { |
262e5473 | 116 | BT_LOGD_STR("Destroying connections."); |
c0418dd9 | 117 | g_ptr_array_free(graph->connections, TRUE); |
d94d92ac | 118 | graph->connections = NULL; |
c0418dd9 | 119 | } |
5c563278 | 120 | |
bd14d768 | 121 | if (graph->components) { |
262e5473 | 122 | BT_LOGD_STR("Destroying components."); |
bd14d768 | 123 | g_ptr_array_free(graph->components, TRUE); |
d94d92ac | 124 | graph->components = NULL; |
bd14d768 | 125 | } |
5c563278 | 126 | |
9b4f9b42 PP |
127 | if (graph->interrupters) { |
128 | BT_LOGD_STR("Putting interrupters."); | |
129 | g_ptr_array_free(graph->interrupters, TRUE); | |
130 | graph->interrupters = NULL; | |
131 | } | |
132 | ||
133 | BT_OBJECT_PUT_REF_AND_RESET(graph->default_interrupter); | |
134 | ||
f60c8b34 JG |
135 | if (graph->sinks_to_consume) { |
136 | g_queue_free(graph->sinks_to_consume); | |
d94d92ac PP |
137 | graph->sinks_to_consume = NULL; |
138 | } | |
139 | ||
140 | if (graph->listeners.source_output_port_added) { | |
141 | g_array_free(graph->listeners.source_output_port_added, TRUE); | |
142 | graph->listeners.source_output_port_added = NULL; | |
143 | } | |
144 | ||
145 | if (graph->listeners.filter_output_port_added) { | |
146 | g_array_free(graph->listeners.filter_output_port_added, TRUE); | |
147 | graph->listeners.filter_output_port_added = NULL; | |
148 | } | |
149 | ||
150 | if (graph->listeners.filter_input_port_added) { | |
151 | g_array_free(graph->listeners.filter_input_port_added, TRUE); | |
152 | graph->listeners.filter_input_port_added = NULL; | |
c0418dd9 | 153 | } |
1bf957a0 | 154 | |
d94d92ac PP |
155 | if (graph->listeners.sink_input_port_added) { |
156 | g_array_free(graph->listeners.sink_input_port_added, TRUE); | |
157 | graph->listeners.sink_input_port_added = NULL; | |
1bf957a0 PP |
158 | } |
159 | ||
d6e69534 PP |
160 | bt_object_pool_finalize(&graph->event_msg_pool); |
161 | bt_object_pool_finalize(&graph->packet_begin_msg_pool); | |
162 | bt_object_pool_finalize(&graph->packet_end_msg_pool); | |
c0418dd9 JG |
163 | g_free(graph); |
164 | } | |
165 | ||
5c563278 | 166 | static |
d6e69534 | 167 | void destroy_message_event(struct bt_message *msg, |
5c563278 PP |
168 | struct bt_graph *graph) |
169 | { | |
d6e69534 | 170 | bt_message_event_destroy(msg); |
5c563278 PP |
171 | } |
172 | ||
173 | static | |
d6e69534 | 174 | void destroy_message_packet_begin(struct bt_message *msg, |
5c563278 PP |
175 | struct bt_graph *graph) |
176 | { | |
5df26c89 | 177 | bt_message_packet_destroy(msg); |
5c563278 PP |
178 | } |
179 | ||
180 | static | |
d6e69534 | 181 | void destroy_message_packet_end(struct bt_message *msg, |
5c563278 PP |
182 | struct bt_graph *graph) |
183 | { | |
5df26c89 | 184 | bt_message_packet_destroy(msg); |
5c563278 PP |
185 | } |
186 | ||
187 | static | |
d6e69534 | 188 | void notify_message_graph_is_destroyed(struct bt_message *msg) |
5c563278 | 189 | { |
d6e69534 | 190 | bt_message_unlink_graph(msg); |
5c563278 PP |
191 | } |
192 | ||
056deb59 | 193 | struct bt_graph *bt_graph_create(uint64_t mip_version) |
c0418dd9 | 194 | { |
f60c8b34 | 195 | struct bt_graph *graph; |
1bf957a0 | 196 | int ret; |
c0418dd9 | 197 | |
17f3083a | 198 | BT_ASSERT_PRE_NO_ERROR(); |
056deb59 PP |
199 | BT_ASSERT_PRE(mip_version <= bt_get_maximal_mip_version(), |
200 | "Unknown MIP version: mip-version=%" PRIu64 ", " | |
201 | "max-mip-version=%" PRIu64, | |
202 | mip_version, bt_get_maximal_mip_version()); | |
3f7d4d90 | 203 | BT_LOGI_STR("Creating graph object."); |
f60c8b34 | 204 | graph = g_new0(struct bt_graph, 1); |
c0418dd9 | 205 | if (!graph) { |
870631a2 | 206 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph."); |
c0418dd9 JG |
207 | goto end; |
208 | } | |
209 | ||
d94d92ac | 210 | bt_object_init_shared(&graph->base, destroy_graph); |
056deb59 | 211 | graph->mip_version = mip_version; |
3fea54f6 PP |
212 | graph->connections = g_ptr_array_new_with_free_func( |
213 | (GDestroyNotify) bt_object_try_spec_release); | |
c0418dd9 | 214 | if (!graph->connections) { |
870631a2 | 215 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
c0418dd9 JG |
216 | goto error; |
217 | } | |
3fea54f6 PP |
218 | graph->components = g_ptr_array_new_with_free_func( |
219 | (GDestroyNotify) bt_object_try_spec_release); | |
f60c8b34 | 220 | if (!graph->components) { |
870631a2 | 221 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
f60c8b34 JG |
222 | goto error; |
223 | } | |
224 | graph->sinks_to_consume = g_queue_new(); | |
225 | if (!graph->sinks_to_consume) { | |
870631a2 | 226 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue."); |
c0418dd9 JG |
227 | goto error; |
228 | } | |
1bf957a0 | 229 | |
d94d92ac PP |
230 | bt_graph_set_can_consume(graph, true); |
231 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, | |
232 | graph->listeners.source_output_port_added); | |
233 | ||
234 | if (!graph->listeners.source_output_port_added) { | |
1bf957a0 PP |
235 | goto error; |
236 | } | |
237 | ||
d94d92ac PP |
238 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
239 | graph->listeners.filter_output_port_added); | |
240 | ||
241 | if (!graph->listeners.filter_output_port_added) { | |
1bf957a0 PP |
242 | goto error; |
243 | } | |
244 | ||
d94d92ac PP |
245 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
246 | graph->listeners.filter_input_port_added); | |
247 | ||
248 | if (!graph->listeners.filter_input_port_added) { | |
1bf957a0 PP |
249 | goto error; |
250 | } | |
251 | ||
d94d92ac PP |
252 | INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added, |
253 | graph->listeners.sink_input_port_added); | |
254 | ||
255 | if (!graph->listeners.sink_input_port_added) { | |
d94d92ac PP |
256 | goto error; |
257 | } | |
258 | ||
9b4f9b42 | 259 | graph->interrupters = g_ptr_array_new_with_free_func( |
6871026b | 260 | (GDestroyNotify) bt_object_put_ref_no_null_check); |
9b4f9b42 PP |
261 | if (!graph->interrupters) { |
262 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
263 | goto error; | |
264 | } | |
265 | ||
266 | graph->default_interrupter = bt_interrupter_create(); | |
267 | if (!graph->default_interrupter) { | |
268 | BT_LIB_LOGE_APPEND_CAUSE( | |
269 | "Failed to create one interrupter object."); | |
270 | goto error; | |
271 | } | |
272 | ||
273 | bt_graph_add_interrupter(graph, graph->default_interrupter); | |
d6e69534 PP |
274 | ret = bt_object_pool_initialize(&graph->event_msg_pool, |
275 | (bt_object_pool_new_object_func) bt_message_event_new, | |
276 | (bt_object_pool_destroy_object_func) destroy_message_event, | |
5c563278 PP |
277 | graph); |
278 | if (ret) { | |
870631a2 PP |
279 | BT_LIB_LOGE_APPEND_CAUSE( |
280 | "Failed to initialize event message pool: ret=%d", | |
5c563278 PP |
281 | ret); |
282 | goto error; | |
283 | } | |
284 | ||
d6e69534 PP |
285 | ret = bt_object_pool_initialize(&graph->packet_begin_msg_pool, |
286 | (bt_object_pool_new_object_func) bt_message_packet_beginning_new, | |
287 | (bt_object_pool_destroy_object_func) destroy_message_packet_begin, | |
5c563278 PP |
288 | graph); |
289 | if (ret) { | |
870631a2 PP |
290 | BT_LIB_LOGE_APPEND_CAUSE( |
291 | "Failed to initialize packet beginning message pool: ret=%d", | |
5c563278 PP |
292 | ret); |
293 | goto error; | |
294 | } | |
295 | ||
d6e69534 PP |
296 | ret = bt_object_pool_initialize(&graph->packet_end_msg_pool, |
297 | (bt_object_pool_new_object_func) bt_message_packet_end_new, | |
298 | (bt_object_pool_destroy_object_func) destroy_message_packet_end, | |
5c563278 PP |
299 | graph); |
300 | if (ret) { | |
870631a2 PP |
301 | BT_LIB_LOGE_APPEND_CAUSE( |
302 | "Failed to initialize packet end message pool: ret=%d", | |
5c563278 PP |
303 | ret); |
304 | goto error; | |
305 | } | |
306 | ||
d6e69534 PP |
307 | graph->messages = g_ptr_array_new_with_free_func( |
308 | (GDestroyNotify) notify_message_graph_is_destroyed); | |
3f7d4d90 | 309 | BT_LIB_LOGI("Created graph object: %!+g", graph); |
262e5473 | 310 | |
c0418dd9 | 311 | end: |
a2d06fd5 | 312 | return (void *) graph; |
d94d92ac | 313 | |
c0418dd9 | 314 | error: |
65300d60 | 315 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
c0418dd9 JG |
316 | goto end; |
317 | } | |
318 | ||
d24d5663 | 319 | enum bt_graph_connect_ports_status bt_graph_connect_ports( |
0d72b8c3 PP |
320 | struct bt_graph *graph, |
321 | const struct bt_port_output *upstream_port_out, | |
322 | const struct bt_port_input *downstream_port_in, | |
323 | const struct bt_connection **user_connection) | |
f60c8b34 | 324 | { |
d24d5663 | 325 | enum bt_graph_connect_ports_status status = BT_FUNC_STATUS_OK; |
f60c8b34 | 326 | struct bt_connection *connection = NULL; |
d94d92ac PP |
327 | struct bt_port *upstream_port = (void *) upstream_port_out; |
328 | struct bt_port *downstream_port = (void *) downstream_port_in; | |
f60c8b34 JG |
329 | struct bt_component *upstream_component = NULL; |
330 | struct bt_component *downstream_component = NULL; | |
d24d5663 | 331 | enum bt_component_class_port_connected_method_status port_connected_status; |
d94d92ac | 332 | bool init_can_consume; |
f60c8b34 | 333 | |
17f3083a | 334 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac PP |
335 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
336 | BT_ASSERT_PRE_NON_NULL(upstream_port, "Upstream port"); | |
337 | BT_ASSERT_PRE_NON_NULL(downstream_port, "Downstream port port"); | |
5badd463 PP |
338 | BT_ASSERT_PRE( |
339 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
38cda5da | 340 | "Graph is not in the \"configuring\" state: %!+g", graph); |
d94d92ac PP |
341 | BT_ASSERT_PRE(!bt_port_is_connected(upstream_port), |
342 | "Upstream port is already connected: %!+p", upstream_port); | |
343 | BT_ASSERT_PRE(!bt_port_is_connected(downstream_port), | |
344 | "Downstream port is already connected: %!+p", downstream_port); | |
0d72b8c3 | 345 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port), |
d94d92ac PP |
346 | "Upstream port does not belong to a component: %!+p", |
347 | upstream_port); | |
0d72b8c3 | 348 | BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port), |
d94d92ac PP |
349 | "Downstream port does not belong to a component: %!+p", |
350 | downstream_port); | |
4aa7981f | 351 | init_can_consume = graph->can_consume; |
3f7d4d90 | 352 | BT_LIB_LOGI("Connecting component ports within graph: " |
d94d92ac PP |
353 | "%![graph-]+g, %![up-port-]+p, %![down-port-]+p", |
354 | graph, upstream_port, downstream_port); | |
355 | bt_graph_set_can_consume(graph, false); | |
0d72b8c3 | 356 | upstream_component = bt_port_borrow_component_inline( |
d94d92ac | 357 | (void *) upstream_port); |
0d72b8c3 | 358 | downstream_component = bt_port_borrow_component_inline( |
d94d92ac | 359 | (void *) downstream_port); |
262e5473 | 360 | |
262e5473 | 361 | BT_LOGD_STR("Creating connection."); |
d94d92ac PP |
362 | connection = bt_connection_create(graph, (void *) upstream_port, |
363 | (void *) downstream_port); | |
f60c8b34 | 364 | if (!connection) { |
870631a2 | 365 | BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object."); |
d24d5663 | 366 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
a256a42d | 367 | goto end; |
f60c8b34 JG |
368 | } |
369 | ||
d94d92ac | 370 | BT_LIB_LOGD("Connection object created: %!+x", connection); |
262e5473 | 371 | |
f60c8b34 | 372 | /* |
72b913fb PP |
373 | * Ownership of upstream_component/downstream_component and of |
374 | * the connection object is transferred to the graph. | |
f60c8b34 JG |
375 | */ |
376 | g_ptr_array_add(graph->connections, connection); | |
ffeb0eed | 377 | |
f60c8b34 | 378 | /* |
0d8b4d8e | 379 | * Notify both components that their port is connected. |
f60c8b34 | 380 | */ |
d94d92ac PP |
381 | BT_LIB_LOGD("Notifying upstream component that its port is connected: " |
382 | "%![comp-]+c, %![port-]+p", upstream_component, upstream_port); | |
d24d5663 | 383 | port_connected_status = bt_component_port_connected(upstream_component, |
d94d92ac | 384 | (void *) upstream_port, (void *) downstream_port); |
d24d5663 | 385 | if (port_connected_status != BT_FUNC_STATUS_OK) { |
870631a2 PP |
386 | if (port_connected_status < 0) { |
387 | BT_LIB_LOGW_APPEND_CAUSE( | |
388 | "Upstream component's \"port connected\" method failed: " | |
389 | "status=%s, %![graph-]+g, %![up-comp-]+c, " | |
390 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", | |
391 | bt_common_func_status_string( | |
392 | port_connected_status), | |
393 | graph, upstream_component, downstream_component, | |
394 | upstream_port, downstream_port); | |
395 | } | |
396 | ||
bf55043c | 397 | bt_connection_end(connection, true); |
d24d5663 | 398 | status = (int) port_connected_status; |
bf55043c PP |
399 | goto end; |
400 | } | |
401 | ||
85031ceb | 402 | connection->notified_upstream_port_connected = true; |
d94d92ac PP |
403 | BT_LIB_LOGD("Notifying downstream component that its port is connected: " |
404 | "%![comp-]+c, %![port-]+p", downstream_component, | |
405 | downstream_port); | |
d24d5663 | 406 | port_connected_status = bt_component_port_connected(downstream_component, |
d94d92ac | 407 | (void *) downstream_port, (void *) upstream_port); |
d24d5663 | 408 | if (port_connected_status != BT_FUNC_STATUS_OK) { |
870631a2 PP |
409 | if (port_connected_status < 0) { |
410 | BT_LIB_LOGW_APPEND_CAUSE( | |
411 | "Downstream component's \"port connected\" method failed: " | |
412 | "status=%s, %![graph-]+g, %![up-comp-]+c, " | |
413 | "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p", | |
414 | bt_common_func_status_string( | |
415 | port_connected_status), | |
416 | graph, upstream_component, downstream_component, | |
417 | upstream_port, downstream_port); | |
418 | } | |
419 | ||
bf55043c | 420 | bt_connection_end(connection, true); |
d24d5663 | 421 | status = (int) port_connected_status; |
bf55043c PP |
422 | goto end; |
423 | } | |
424 | ||
85031ceb | 425 | connection->notified_downstream_port_connected = true; |
1bf957a0 | 426 | |
3f7d4d90 | 427 | BT_LIB_LOGI("Connected component ports within graph: " |
d94d92ac PP |
428 | "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, " |
429 | "%![up-port-]+p, %![down-port-]+p", | |
430 | graph, upstream_component, downstream_component, | |
431 | upstream_port, downstream_port); | |
1bf957a0 | 432 | |
a256a42d | 433 | if (user_connection) { |
1a6a376a PP |
434 | /* Move reference to user */ |
435 | *user_connection = connection; | |
a256a42d PP |
436 | } |
437 | ||
f60c8b34 | 438 | end: |
d24d5663 | 439 | if (status != BT_FUNC_STATUS_OK) { |
8cc56726 | 440 | bt_graph_make_faulty(graph); |
38cda5da PP |
441 | } |
442 | ||
65300d60 | 443 | bt_object_put_ref(connection); |
d94d92ac PP |
444 | (void) init_can_consume; |
445 | bt_graph_set_can_consume(graph, init_can_consume); | |
a256a42d | 446 | return status; |
f60c8b34 JG |
447 | } |
448 | ||
ad847455 | 449 | static inline |
d24d5663 | 450 | int consume_graph_sink(struct bt_component_sink *comp) |
c0418dd9 | 451 | { |
d24d5663 | 452 | enum bt_component_class_sink_consume_method_status consume_status; |
d94d92ac PP |
453 | struct bt_component_class_sink *sink_class = NULL; |
454 | ||
98b15851 | 455 | BT_ASSERT_DBG(comp); |
d94d92ac | 456 | sink_class = (void *) comp->parent.class; |
98b15851 | 457 | BT_ASSERT_DBG(sink_class->methods.consume); |
d94d92ac | 458 | BT_LIB_LOGD("Calling user's consume method: %!+c", comp); |
d24d5663 | 459 | consume_status = sink_class->methods.consume((void *) comp); |
d94d92ac | 460 | BT_LOGD("User method returned: status=%s", |
d24d5663 | 461 | bt_common_func_status_string(consume_status)); |
bdb288b3 | 462 | BT_ASSERT_POST_DEV(consume_status == BT_FUNC_STATUS_OK || |
d24d5663 PP |
463 | consume_status == BT_FUNC_STATUS_END || |
464 | consume_status == BT_FUNC_STATUS_AGAIN || | |
465 | consume_status == BT_FUNC_STATUS_ERROR || | |
466 | consume_status == BT_FUNC_STATUS_MEMORY_ERROR, | |
d94d92ac | 467 | "Invalid component status returned by consuming method: " |
d24d5663 | 468 | "status=%s", bt_common_func_status_string(consume_status)); |
6ecdcca3 | 469 | BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(consume_status); |
870631a2 PP |
470 | if (consume_status) { |
471 | if (consume_status < 0) { | |
472 | BT_LIB_LOGW_APPEND_CAUSE( | |
473 | "Component's \"consume\" method failed: " | |
474 | "status=%s, %![comp-]+c", | |
475 | bt_common_func_status_string(consume_status), | |
476 | comp); | |
477 | } | |
478 | ||
d94d92ac PP |
479 | goto end; |
480 | } | |
481 | ||
3f7d4d90 | 482 | BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s", |
d24d5663 | 483 | comp, bt_common_func_status_string(consume_status)); |
d94d92ac PP |
484 | |
485 | end: | |
d24d5663 | 486 | return consume_status; |
8ed535b5 PP |
487 | } |
488 | ||
489 | /* | |
490 | * `node` is removed from the queue of sinks to consume when passed to | |
491 | * this function. This function adds it back to the queue if there's | |
492 | * still something to consume afterwards. | |
493 | */ | |
ad847455 | 494 | static inline |
d24d5663 | 495 | int consume_sink_node(struct bt_graph *graph, GList *node) |
8ed535b5 | 496 | { |
d24d5663 | 497 | int status; |
d94d92ac | 498 | struct bt_component_sink *sink; |
8ed535b5 PP |
499 | |
500 | sink = node->data; | |
501 | status = consume_graph_sink(sink); | |
d24d5663 | 502 | if (G_UNLIKELY(status != BT_FUNC_STATUS_END)) { |
8ed535b5 | 503 | g_queue_push_tail_link(graph->sinks_to_consume, node); |
f60c8b34 JG |
504 | goto end; |
505 | } | |
506 | ||
507 | /* End reached, the node is not added back to the queue and free'd. */ | |
8ed535b5 | 508 | g_queue_delete_link(graph->sinks_to_consume, node); |
f60c8b34 JG |
509 | |
510 | /* Don't forward an END status if there are sinks left to consume. */ | |
511 | if (!g_queue_is_empty(graph->sinks_to_consume)) { | |
d24d5663 | 512 | status = BT_FUNC_STATUS_OK; |
f60c8b34 JG |
513 | goto end; |
514 | } | |
8ed535b5 PP |
515 | |
516 | end: | |
3f7d4d90 | 517 | BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s", |
d24d5663 | 518 | sink, bt_common_func_status_string(status)); |
8ed535b5 PP |
519 | return status; |
520 | } | |
521 | ||
522 | BT_HIDDEN | |
d24d5663 | 523 | int bt_graph_consume_sink_no_check(struct bt_graph *graph, |
d94d92ac | 524 | struct bt_component_sink *sink) |
8ed535b5 | 525 | { |
d24d5663 | 526 | int status; |
8ed535b5 PP |
527 | GList *sink_node; |
528 | int index; | |
529 | ||
3f7d4d90 | 530 | BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink); |
98b15851 | 531 | BT_ASSERT_DBG(bt_component_borrow_graph((void *) sink) == graph); |
8ed535b5 PP |
532 | |
533 | if (g_queue_is_empty(graph->sinks_to_consume)) { | |
3f7d4d90 | 534 | BT_LOGD_STR("Graph's sink queue is empty: end of graph."); |
d24d5663 | 535 | status = BT_FUNC_STATUS_END; |
8ed535b5 PP |
536 | goto end; |
537 | } | |
538 | ||
539 | index = g_queue_index(graph->sinks_to_consume, sink); | |
540 | if (index < 0) { | |
3f7d4d90 PP |
541 | BT_LIB_LOGD("Sink component is not marked as consumable: " |
542 | "component sink is ended: %![comp-]+c", sink); | |
d24d5663 | 543 | status = BT_FUNC_STATUS_END; |
8ed535b5 PP |
544 | goto end; |
545 | } | |
546 | ||
547 | sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index); | |
98b15851 | 548 | BT_ASSERT_DBG(sink_node); |
8ed535b5 PP |
549 | status = consume_sink_node(graph, sink_node); |
550 | ||
551 | end: | |
552 | return status; | |
553 | } | |
554 | ||
ad847455 | 555 | static inline |
d24d5663 | 556 | int consume_no_check(struct bt_graph *graph) |
8ed535b5 | 557 | { |
d24d5663 | 558 | int status = BT_FUNC_STATUS_OK; |
8ed535b5 PP |
559 | struct bt_component *sink; |
560 | GList *current_node; | |
561 | ||
bdb288b3 | 562 | BT_ASSERT_PRE_DEV(graph->has_sink, |
f6ccaed9 | 563 | "Graph has no sink component: %!+g", graph); |
3f7d4d90 | 564 | BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph); |
8ed535b5 | 565 | |
91d81473 | 566 | if (G_UNLIKELY(g_queue_is_empty(graph->sinks_to_consume))) { |
3f7d4d90 | 567 | BT_LOGD_STR("Graph's sink queue is empty: end of graph."); |
d24d5663 | 568 | status = BT_FUNC_STATUS_END; |
8ed535b5 PP |
569 | goto end; |
570 | } | |
571 | ||
572 | current_node = g_queue_pop_head_link(graph->sinks_to_consume); | |
573 | sink = current_node->data; | |
3f7d4d90 | 574 | BT_LIB_LOGD("Chose next sink to consume: %!+c", sink); |
8ed535b5 PP |
575 | status = consume_sink_node(graph, current_node); |
576 | ||
f60c8b34 JG |
577 | end: |
578 | return status; | |
c0418dd9 JG |
579 | } |
580 | ||
648dab91 | 581 | enum bt_graph_run_once_status bt_graph_run_once(struct bt_graph *graph) |
851b70bd | 582 | { |
648dab91 | 583 | enum bt_graph_run_once_status status; |
1d915789 | 584 | |
17f3083a | 585 | BT_ASSERT_PRE_NO_ERROR(); |
bdb288b3 | 586 | BT_ASSERT_PRE_DEV_NON_NULL(graph, "Graph"); |
bdb288b3 | 587 | BT_ASSERT_PRE_DEV(graph->can_consume, |
f6ccaed9 | 588 | "Cannot consume graph in its current state: %!+g", graph); |
bdb288b3 PP |
589 | BT_ASSERT_PRE_DEV(graph->config_state != |
590 | BT_GRAPH_CONFIGURATION_STATE_FAULTY, | |
38cda5da | 591 | "Graph is in a faulty state: %!+g", graph); |
4725a201 | 592 | bt_graph_set_can_consume(graph, false); |
5badd463 | 593 | status = bt_graph_configure(graph); |
91d81473 | 594 | if (G_UNLIKELY(status)) { |
5badd463 PP |
595 | /* bt_graph_configure() logs errors */ |
596 | goto end; | |
597 | } | |
598 | ||
d94d92ac | 599 | status = consume_no_check(graph); |
4725a201 | 600 | bt_graph_set_can_consume(graph, true); |
5badd463 PP |
601 | |
602 | end: | |
26a15756 | 603 | return status; |
851b70bd PP |
604 | } |
605 | ||
d24d5663 | 606 | enum bt_graph_run_status bt_graph_run(struct bt_graph *graph) |
f60c8b34 | 607 | { |
d24d5663 | 608 | enum bt_graph_run_status status; |
f60c8b34 | 609 | |
17f3083a | 610 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac | 611 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
ad847455 PP |
612 | BT_ASSERT_PRE(graph->can_consume, |
613 | "Cannot consume graph in its current state: %!+g", graph); | |
38cda5da PP |
614 | BT_ASSERT_PRE(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY, |
615 | "Graph is in a faulty state: %!+g", graph); | |
4725a201 | 616 | bt_graph_set_can_consume(graph, false); |
5badd463 | 617 | status = bt_graph_configure(graph); |
91d81473 | 618 | if (G_UNLIKELY(status)) { |
5badd463 PP |
619 | /* bt_graph_configure() logs errors */ |
620 | goto end; | |
621 | } | |
622 | ||
3f7d4d90 | 623 | BT_LIB_LOGI("Running graph: %!+g", graph); |
262e5473 | 624 | |
f60c8b34 | 625 | do { |
851b70bd | 626 | /* |
9b4f9b42 PP |
627 | * Check if the graph is interrupted at each iteration. |
628 | * If the graph was interrupted by another thread or by | |
629 | * a signal handler, this is NOT a warning nor an error; | |
630 | * it was intentional: log with an INFO level only. | |
851b70bd | 631 | */ |
9b4f9b42 PP |
632 | if (G_UNLIKELY(bt_graph_is_interrupted(graph))) { |
633 | BT_LIB_LOGI("Stopping the graph: " | |
634 | "graph was interrupted: %!+g", graph); | |
635 | status = BT_FUNC_STATUS_AGAIN; | |
851b70bd PP |
636 | goto end; |
637 | } | |
638 | ||
d94d92ac | 639 | status = consume_no_check(graph); |
d24d5663 | 640 | if (G_UNLIKELY(status == BT_FUNC_STATUS_AGAIN)) { |
f60c8b34 | 641 | /* |
202a3a13 PP |
642 | * If AGAIN is received and there are multiple |
643 | * sinks, go ahead and consume from the next | |
644 | * sink. | |
f60c8b34 | 645 | * |
202a3a13 PP |
646 | * However, in the case where a single sink is |
647 | * left, the caller can decide to busy-wait and | |
0d72b8c3 | 648 | * call bt_graph_run() continuously |
d94d92ac PP |
649 | * until the source is ready or it can decide to |
650 | * sleep for an arbitrary amount of time. | |
f60c8b34 JG |
651 | */ |
652 | if (graph->sinks_to_consume->length > 1) { | |
d24d5663 | 653 | status = BT_FUNC_STATUS_OK; |
f60c8b34 JG |
654 | } |
655 | } | |
d24d5663 | 656 | } while (status == BT_FUNC_STATUS_OK); |
f60c8b34 | 657 | |
9669d693 PP |
658 | if (status == BT_FUNC_STATUS_END) { |
659 | /* | |
660 | * The last call to consume_no_check() returned | |
661 | * `BT_FUNC_STATUS_END`, but bt_graph_run() has no | |
662 | * `BT_GRAPH_RUN_STATUS_END` status: replace with | |
663 | * `BT_GRAPH_RUN_STATUS_OK` (success: graph ran | |
664 | * completely). | |
665 | */ | |
666 | status = BT_FUNC_STATUS_OK; | |
f60c8b34 | 667 | } |
262e5473 | 668 | |
202a3a13 | 669 | end: |
3f7d4d90 | 670 | BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph, |
d24d5663 | 671 | bt_common_func_status_string(status)); |
4725a201 | 672 | bt_graph_set_can_consume(graph, true); |
72b913fb | 673 | return status; |
f60c8b34 | 674 | } |
1bf957a0 | 675 | |
d24d5663 | 676 | enum bt_graph_add_listener_status |
0d72b8c3 PP |
677 | bt_graph_add_source_component_output_port_added_listener( |
678 | struct bt_graph *graph, | |
679 | bt_graph_source_component_output_port_added_listener_func func, | |
f3d6b4c2 | 680 | void *data, bt_listener_id *out_listener_id) |
1bf957a0 | 681 | { |
d94d92ac | 682 | struct bt_graph_listener_port_added listener = { |
d94d92ac | 683 | .func = (port_added_func_t) func, |
f3d6b4c2 | 684 | .data = data, |
1bf957a0 | 685 | }; |
2054a0d1 | 686 | bt_listener_id listener_id; |
1bf957a0 | 687 | |
17f3083a | 688 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac PP |
689 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
690 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
d94d92ac PP |
691 | g_array_append_val(graph->listeners.source_output_port_added, listener); |
692 | listener_id = graph->listeners.source_output_port_added->len - 1; | |
3f7d4d90 | 693 | BT_LIB_LOGD("Added \"source component output port added\" listener to graph: " |
d94d92ac PP |
694 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
695 | listener_id); | |
696 | ||
697 | if (listener_id) { | |
698 | *out_listener_id = listener_id; | |
699 | } | |
700 | ||
d24d5663 | 701 | return BT_FUNC_STATUS_OK; |
1bf957a0 PP |
702 | } |
703 | ||
d24d5663 | 704 | enum bt_graph_add_listener_status |
0d72b8c3 PP |
705 | bt_graph_add_filter_component_output_port_added_listener( |
706 | struct bt_graph *graph, | |
707 | bt_graph_filter_component_output_port_added_listener_func func, | |
f3d6b4c2 | 708 | void *data, bt_listener_id *out_listener_id) |
1bf957a0 | 709 | { |
d94d92ac | 710 | struct bt_graph_listener_port_added listener = { |
d94d92ac | 711 | .func = (port_added_func_t) func, |
f3d6b4c2 | 712 | .data = data, |
d94d92ac | 713 | }; |
2054a0d1 | 714 | bt_listener_id listener_id; |
1bf957a0 | 715 | |
17f3083a | 716 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac PP |
717 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
718 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
d94d92ac PP |
719 | g_array_append_val(graph->listeners.filter_output_port_added, listener); |
720 | listener_id = graph->listeners.filter_output_port_added->len - 1; | |
3f7d4d90 | 721 | BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: " |
d94d92ac PP |
722 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
723 | listener_id); | |
724 | ||
725 | if (listener_id) { | |
726 | *out_listener_id = listener_id; | |
727 | } | |
728 | ||
d24d5663 | 729 | return BT_FUNC_STATUS_OK; |
d94d92ac | 730 | } |
262e5473 | 731 | |
d24d5663 | 732 | enum bt_graph_add_listener_status |
0d72b8c3 PP |
733 | bt_graph_add_filter_component_input_port_added_listener( |
734 | struct bt_graph *graph, | |
735 | bt_graph_filter_component_input_port_added_listener_func func, | |
f3d6b4c2 | 736 | void *data, bt_listener_id *out_listener_id) |
d94d92ac | 737 | { |
d94d92ac | 738 | struct bt_graph_listener_port_added listener = { |
d94d92ac | 739 | .func = (port_added_func_t) func, |
f3d6b4c2 | 740 | .data = data, |
d94d92ac | 741 | }; |
2054a0d1 | 742 | bt_listener_id listener_id; |
8cc092c9 | 743 | |
17f3083a | 744 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac PP |
745 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
746 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
d94d92ac PP |
747 | g_array_append_val(graph->listeners.filter_input_port_added, listener); |
748 | listener_id = graph->listeners.filter_input_port_added->len - 1; | |
3f7d4d90 | 749 | BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: " |
d94d92ac PP |
750 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
751 | listener_id); | |
752 | ||
753 | if (listener_id) { | |
754 | *out_listener_id = listener_id; | |
755 | } | |
756 | ||
d24d5663 | 757 | return BT_FUNC_STATUS_OK; |
d94d92ac | 758 | } |
1bf957a0 | 759 | |
d24d5663 | 760 | enum bt_graph_add_listener_status |
0d72b8c3 PP |
761 | bt_graph_add_sink_component_input_port_added_listener( |
762 | struct bt_graph *graph, | |
763 | bt_graph_sink_component_input_port_added_listener_func func, | |
f3d6b4c2 | 764 | void *data, bt_listener_id *out_listener_id) |
d94d92ac | 765 | { |
d94d92ac | 766 | struct bt_graph_listener_port_added listener = { |
d94d92ac | 767 | .func = (port_added_func_t) func, |
f3d6b4c2 | 768 | .data = data, |
d94d92ac | 769 | }; |
2054a0d1 | 770 | bt_listener_id listener_id; |
1bf957a0 | 771 | |
17f3083a | 772 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac PP |
773 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
774 | BT_ASSERT_PRE_NON_NULL(func, "Listener"); | |
d94d92ac PP |
775 | g_array_append_val(graph->listeners.sink_input_port_added, listener); |
776 | listener_id = graph->listeners.sink_input_port_added->len - 1; | |
3f7d4d90 | 777 | BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: " |
d94d92ac PP |
778 | "%![graph-]+g, listener-addr=%p, id=%d", graph, listener, |
779 | listener_id); | |
780 | ||
781 | if (listener_id) { | |
782 | *out_listener_id = listener_id; | |
783 | } | |
784 | ||
d24d5663 | 785 | return BT_FUNC_STATUS_OK; |
1bf957a0 PP |
786 | } |
787 | ||
1bf957a0 | 788 | BT_HIDDEN |
d24d5663 | 789 | enum bt_graph_listener_func_status bt_graph_notify_port_added( |
8cc56726 | 790 | struct bt_graph *graph, struct bt_port *port) |
1bf957a0 | 791 | { |
d94d92ac PP |
792 | uint64_t i; |
793 | GArray *listeners; | |
794 | struct bt_component *comp; | |
d24d5663 | 795 | enum bt_graph_listener_func_status status = BT_FUNC_STATUS_OK; |
1bf957a0 | 796 | |
d94d92ac PP |
797 | BT_ASSERT(graph); |
798 | BT_ASSERT(port); | |
3f7d4d90 | 799 | BT_LIB_LOGD("Notifying graph listeners that a port was added: " |
d94d92ac | 800 | "%![graph-]+g, %![port-]+p", graph, port); |
0d72b8c3 | 801 | comp = bt_port_borrow_component_inline(port); |
d94d92ac PP |
802 | BT_ASSERT(comp); |
803 | ||
804 | switch (comp->class->type) { | |
805 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
806 | { | |
807 | switch (port->type) { | |
808 | case BT_PORT_TYPE_OUTPUT: | |
809 | listeners = graph->listeners.source_output_port_added; | |
810 | break; | |
811 | default: | |
498e7994 | 812 | bt_common_abort(); |
d94d92ac PP |
813 | } |
814 | ||
815 | break; | |
816 | } | |
817 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
818 | { | |
819 | switch (port->type) { | |
820 | case BT_PORT_TYPE_INPUT: | |
821 | listeners = graph->listeners.filter_input_port_added; | |
822 | break; | |
823 | case BT_PORT_TYPE_OUTPUT: | |
824 | listeners = graph->listeners.filter_output_port_added; | |
825 | break; | |
826 | default: | |
498e7994 | 827 | bt_common_abort(); |
d94d92ac | 828 | } |
262e5473 | 829 | |
d94d92ac PP |
830 | break; |
831 | } | |
832 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
833 | { | |
834 | switch (port->type) { | |
835 | case BT_PORT_TYPE_INPUT: | |
836 | listeners = graph->listeners.sink_input_port_added; | |
837 | break; | |
838 | default: | |
498e7994 | 839 | bt_common_abort(); |
d94d92ac | 840 | } |
1bf957a0 | 841 | |
d94d92ac PP |
842 | break; |
843 | } | |
844 | default: | |
498e7994 | 845 | bt_common_abort(); |
d94d92ac PP |
846 | } |
847 | ||
848 | for (i = 0; i < listeners->len; i++) { | |
849 | struct bt_graph_listener_port_added *listener = | |
850 | &g_array_index(listeners, | |
851 | struct bt_graph_listener_port_added, i); | |
852 | ||
8cc56726 | 853 | |
d94d92ac | 854 | BT_ASSERT(listener->func); |
f3d6b4c2 | 855 | status = listener->func(comp, port, listener->data); |
6ecdcca3 | 856 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status); |
d24d5663 | 857 | if (status != BT_FUNC_STATUS_OK) { |
8cc56726 SM |
858 | goto end; |
859 | } | |
1bf957a0 | 860 | } |
8cc56726 SM |
861 | |
862 | end: | |
863 | return status; | |
1bf957a0 PP |
864 | } |
865 | ||
f167d3c0 PP |
866 | BT_HIDDEN |
867 | void bt_graph_remove_connection(struct bt_graph *graph, | |
868 | struct bt_connection *connection) | |
869 | { | |
f6ccaed9 PP |
870 | BT_ASSERT(graph); |
871 | BT_ASSERT(connection); | |
3f7d4d90 | 872 | BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x", |
262e5473 | 873 | graph, connection); |
f167d3c0 PP |
874 | g_ptr_array_remove(graph->connections, connection); |
875 | } | |
36712f1d | 876 | |
d94d92ac PP |
877 | static inline |
878 | bool component_name_exists(struct bt_graph *graph, const char *name) | |
879 | { | |
880 | bool exists = false; | |
881 | uint64_t i; | |
882 | ||
883 | for (i = 0; i < graph->components->len; i++) { | |
884 | struct bt_component *other_comp = graph->components->pdata[i]; | |
885 | ||
886 | if (strcmp(name, bt_component_get_name(other_comp)) == 0) { | |
887 | BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: " | |
888 | "%![other-comp-]+c, name=\"%s\"", | |
889 | other_comp, name); | |
890 | exists = true; | |
891 | goto end; | |
892 | } | |
893 | } | |
894 | ||
895 | end: | |
896 | return exists; | |
897 | } | |
898 | ||
899 | static | |
d24d5663 | 900 | int add_component_with_init_method_data( |
0d72b8c3 | 901 | struct bt_graph *graph, |
d94d92ac PP |
902 | struct bt_component_class *comp_cls, |
903 | comp_init_method_t init_method, | |
05e21286 | 904 | const char *name, const struct bt_value *params, |
e874da19 | 905 | void *init_method_data, bt_logging_level log_level, |
9628043c | 906 | const struct bt_component **user_component) |
36712f1d | 907 | { |
d24d5663 | 908 | int status = BT_FUNC_STATUS_OK; |
21a9f056 | 909 | enum bt_component_class_initialize_method_status init_status; |
36712f1d | 910 | struct bt_component *component = NULL; |
d94d92ac PP |
911 | int ret; |
912 | bool init_can_consume; | |
05e21286 | 913 | struct bt_value *new_params = NULL; |
36712f1d | 914 | |
d94d92ac PP |
915 | BT_ASSERT(comp_cls); |
916 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); | |
917 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
5badd463 PP |
918 | BT_ASSERT_PRE( |
919 | graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
38cda5da | 920 | "Graph is not in the \"configuring\" state: %!+g", graph); |
d94d92ac PP |
921 | BT_ASSERT_PRE(!component_name_exists(graph, name), |
922 | "Duplicate component name: %!+g, name=\"%s\"", graph, name); | |
923 | BT_ASSERT_PRE(!params || bt_value_is_map(params), | |
924 | "Parameter value is not a map value: %!+v", params); | |
4aa7981f | 925 | init_can_consume = graph->can_consume; |
d94d92ac | 926 | bt_graph_set_can_consume(graph, false); |
3f7d4d90 | 927 | BT_LIB_LOGI("Adding component to graph: " |
e874da19 PP |
928 | "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, " |
929 | "%![params-]+v, init-method-data-addr=%p", | |
930 | graph, comp_cls, name, | |
931 | bt_common_logging_level_string(log_level), params, | |
932 | init_method_data); | |
36712f1d | 933 | |
d94d92ac | 934 | if (!params) { |
05e21286 PP |
935 | new_params = bt_value_map_create(); |
936 | if (!new_params) { | |
870631a2 PP |
937 | BT_LIB_LOGE_APPEND_CAUSE( |
938 | "Cannot create empty map value object."); | |
d24d5663 | 939 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
36712f1d PP |
940 | goto end; |
941 | } | |
05e21286 PP |
942 | |
943 | params = new_params; | |
36712f1d PP |
944 | } |
945 | ||
e874da19 | 946 | ret = bt_component_create(comp_cls, name, log_level, &component); |
d94d92ac | 947 | if (ret) { |
870631a2 PP |
948 | BT_LIB_LOGE_APPEND_CAUSE( |
949 | "Cannot create empty component object: ret=%d", | |
d94d92ac | 950 | ret); |
d24d5663 | 951 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
36712f1d PP |
952 | goto end; |
953 | } | |
954 | ||
955 | /* | |
956 | * The user's initialization method needs to see that this | |
957 | * component is part of the graph. If the user method fails, we | |
958 | * immediately remove the component from the graph's components. | |
959 | */ | |
960 | g_ptr_array_add(graph->components, component); | |
961 | bt_component_set_graph(component, graph); | |
0d47d31b | 962 | bt_value_freeze(params); |
36712f1d | 963 | |
d94d92ac | 964 | if (init_method) { |
59225a3e SM |
965 | /* |
966 | * There is no use for config objects right now, so just pass | |
967 | * NULL. | |
968 | */ | |
36712f1d | 969 | BT_LOGD_STR("Calling user's initialization method."); |
59225a3e | 970 | init_status = init_method(component, NULL, params, init_method_data); |
36712f1d | 971 | BT_LOGD("User method returned: status=%s", |
d24d5663 | 972 | bt_common_func_status_string(init_status)); |
6ecdcca3 | 973 | BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(init_status); |
d24d5663 | 974 | if (init_status != BT_FUNC_STATUS_OK) { |
870631a2 PP |
975 | if (init_status < 0) { |
976 | BT_LIB_LOGW_APPEND_CAUSE( | |
977 | "Component initialization method failed: " | |
978 | "status=%s, %![comp-]+c", | |
979 | bt_common_func_status_string(init_status), | |
980 | component); | |
981 | } | |
982 | ||
d24d5663 | 983 | status = init_status; |
36712f1d PP |
984 | bt_component_set_graph(component, NULL); |
985 | g_ptr_array_remove_fast(graph->components, component); | |
986 | goto end; | |
987 | } | |
988 | } | |
989 | ||
990 | /* | |
991 | * Mark the component as initialized so that its finalization | |
992 | * method is called when it is destroyed. | |
993 | */ | |
994 | component->initialized = true; | |
995 | ||
996 | /* | |
997 | * If it's a sink component, it needs to be part of the graph's | |
648dab91 PP |
998 | * sink queue to be consumed by bt_graph_run() or |
999 | * bt_graph_run_once(). | |
36712f1d PP |
1000 | */ |
1001 | if (bt_component_is_sink(component)) { | |
d94d92ac | 1002 | graph->has_sink = true; |
36712f1d PP |
1003 | g_queue_push_tail(graph->sinks_to_consume, component); |
1004 | } | |
1005 | ||
1006 | /* | |
1007 | * Freeze the component class now that it's instantiated at | |
1008 | * least once. | |
1009 | */ | |
1010 | BT_LOGD_STR("Freezing component class."); | |
d94d92ac | 1011 | bt_component_class_freeze(comp_cls); |
3f7d4d90 | 1012 | BT_LIB_LOGI("Added component to graph: " |
e874da19 PP |
1013 | "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, " |
1014 | "%![params-]+v, init-method-data-addr=%p, %![comp-]+c", | |
1015 | graph, comp_cls, name, | |
1016 | bt_common_logging_level_string(log_level), params, | |
1017 | init_method_data, component); | |
36712f1d PP |
1018 | |
1019 | if (user_component) { | |
1020 | /* Move reference to user */ | |
1021 | *user_component = component; | |
36712f1d PP |
1022 | } |
1023 | ||
1024 | end: | |
d24d5663 | 1025 | if (status != BT_FUNC_STATUS_OK) { |
8cc56726 | 1026 | bt_graph_make_faulty(graph); |
38cda5da PP |
1027 | } |
1028 | ||
65300d60 | 1029 | bt_object_put_ref(component); |
05e21286 | 1030 | bt_object_put_ref(new_params); |
d94d92ac PP |
1031 | (void) init_can_consume; |
1032 | bt_graph_set_can_consume(graph, init_can_consume); | |
d24d5663 | 1033 | return status; |
36712f1d PP |
1034 | } |
1035 | ||
d24d5663 | 1036 | enum bt_graph_add_component_status |
21a9f056 | 1037 | bt_graph_add_source_component_with_initialize_method_data( |
0d72b8c3 PP |
1038 | struct bt_graph *graph, |
1039 | const struct bt_component_class_source *comp_cls, | |
05e21286 | 1040 | const char *name, const struct bt_value *params, |
e874da19 | 1041 | void *init_method_data, bt_logging_level log_level, |
0d72b8c3 | 1042 | const struct bt_component_source **component) |
d94d92ac | 1043 | { |
17f3083a | 1044 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac PP |
1045 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
1046 | return add_component_with_init_method_data(graph, | |
1047 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
e874da19 | 1048 | name, params, init_method_data, log_level, (void *) component); |
d94d92ac PP |
1049 | } |
1050 | ||
d24d5663 | 1051 | enum bt_graph_add_component_status bt_graph_add_source_component( |
0d72b8c3 PP |
1052 | struct bt_graph *graph, |
1053 | const struct bt_component_class_source *comp_cls, | |
05e21286 | 1054 | const char *name, const struct bt_value *params, |
9628043c | 1055 | enum bt_logging_level log_level, |
0d72b8c3 | 1056 | const struct bt_component_source **component) |
d94d92ac | 1057 | { |
17f3083a | 1058 | BT_ASSERT_PRE_NO_ERROR(); |
21a9f056 | 1059 | return bt_graph_add_source_component_with_initialize_method_data( |
e874da19 | 1060 | graph, comp_cls, name, params, NULL, log_level, component); |
d94d92ac PP |
1061 | } |
1062 | ||
d24d5663 | 1063 | enum bt_graph_add_component_status |
21a9f056 | 1064 | bt_graph_add_filter_component_with_initialize_method_data( |
0d72b8c3 PP |
1065 | struct bt_graph *graph, |
1066 | const struct bt_component_class_filter *comp_cls, | |
05e21286 | 1067 | const char *name, const struct bt_value *params, |
9628043c | 1068 | void *init_method_data, enum bt_logging_level log_level, |
0d72b8c3 | 1069 | const struct bt_component_filter **component) |
d94d92ac | 1070 | { |
17f3083a | 1071 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac PP |
1072 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
1073 | return add_component_with_init_method_data(graph, | |
1074 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
e874da19 | 1075 | name, params, init_method_data, log_level, (void *) component); |
d94d92ac PP |
1076 | } |
1077 | ||
d24d5663 | 1078 | enum bt_graph_add_component_status bt_graph_add_filter_component( |
0d72b8c3 PP |
1079 | struct bt_graph *graph, |
1080 | const struct bt_component_class_filter *comp_cls, | |
05e21286 | 1081 | const char *name, const struct bt_value *params, |
9628043c | 1082 | enum bt_logging_level log_level, |
0d72b8c3 | 1083 | const struct bt_component_filter **component) |
d94d92ac | 1084 | { |
17f3083a | 1085 | BT_ASSERT_PRE_NO_ERROR(); |
21a9f056 | 1086 | return bt_graph_add_filter_component_with_initialize_method_data( |
e874da19 | 1087 | graph, comp_cls, name, params, NULL, log_level, component); |
d94d92ac PP |
1088 | } |
1089 | ||
d24d5663 | 1090 | enum bt_graph_add_component_status |
21a9f056 | 1091 | bt_graph_add_sink_component_with_initialize_method_data( |
0d72b8c3 PP |
1092 | struct bt_graph *graph, |
1093 | const struct bt_component_class_sink *comp_cls, | |
05e21286 | 1094 | const char *name, const struct bt_value *params, |
9628043c | 1095 | void *init_method_data, enum bt_logging_level log_level, |
0d72b8c3 | 1096 | const struct bt_component_sink **component) |
36712f1d | 1097 | { |
17f3083a | 1098 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac PP |
1099 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
1100 | return add_component_with_init_method_data(graph, | |
1101 | (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init, | |
e874da19 | 1102 | name, params, init_method_data, log_level, (void *) component); |
d94d92ac PP |
1103 | } |
1104 | ||
d24d5663 | 1105 | enum bt_graph_add_component_status bt_graph_add_sink_component( |
0d72b8c3 PP |
1106 | struct bt_graph *graph, |
1107 | const struct bt_component_class_sink *comp_cls, | |
05e21286 | 1108 | const char *name, const struct bt_value *params, |
9628043c | 1109 | enum bt_logging_level log_level, |
0d72b8c3 | 1110 | const struct bt_component_sink **component) |
d94d92ac | 1111 | { |
17f3083a | 1112 | BT_ASSERT_PRE_NO_ERROR(); |
21a9f056 | 1113 | return bt_graph_add_sink_component_with_initialize_method_data( |
e874da19 | 1114 | graph, comp_cls, name, params, NULL, log_level, component); |
36712f1d | 1115 | } |
8ed535b5 | 1116 | |
078033ed PP |
1117 | enum bt_graph_add_component_status |
1118 | bt_graph_add_simple_sink_component(struct bt_graph *graph, const char *name, | |
21a9f056 | 1119 | bt_graph_simple_sink_component_initialize_func init_func, |
078033ed PP |
1120 | bt_graph_simple_sink_component_consume_func consume_func, |
1121 | bt_graph_simple_sink_component_finalize_func finalize_func, | |
1122 | void *user_data, const bt_component_sink **component) | |
1123 | { | |
1124 | enum bt_graph_add_component_status status; | |
1125 | struct bt_component_class_sink *comp_cls; | |
1126 | struct simple_sink_init_method_data init_method_data = { | |
1127 | .init_func = init_func, | |
1128 | .consume_func = consume_func, | |
1129 | .finalize_func = finalize_func, | |
1130 | .user_data = user_data, | |
1131 | }; | |
1132 | ||
17f3083a SM |
1133 | BT_ASSERT_PRE_NO_ERROR(); |
1134 | ||
078033ed PP |
1135 | /* |
1136 | * Other preconditions are checked by | |
1137 | * bt_graph_add_sink_component_with_init_method_data(). | |
1138 | */ | |
1139 | BT_ASSERT_PRE_NON_NULL(consume_func, "Consume function"); | |
1140 | ||
1141 | comp_cls = bt_component_class_sink_simple_borrow(); | |
1142 | if (!comp_cls) { | |
1143 | BT_LIB_LOGE_APPEND_CAUSE( | |
1144 | "Cannot borrow simple sink component class."); | |
1145 | status = BT_FUNC_STATUS_MEMORY_ERROR; | |
1146 | goto end; | |
1147 | } | |
1148 | ||
21a9f056 | 1149 | status = bt_graph_add_sink_component_with_initialize_method_data(graph, |
078033ed PP |
1150 | comp_cls, name, NULL, &init_method_data, |
1151 | BT_LOGGING_LEVEL_NONE, component); | |
1152 | ||
1153 | end: | |
1154 | return status; | |
1155 | } | |
1156 | ||
5c563278 | 1157 | BT_HIDDEN |
d6e69534 PP |
1158 | void bt_graph_add_message(struct bt_graph *graph, |
1159 | struct bt_message *msg) | |
5c563278 PP |
1160 | { |
1161 | BT_ASSERT(graph); | |
d6e69534 | 1162 | BT_ASSERT(msg); |
5c563278 PP |
1163 | |
1164 | /* | |
1165 | * It's okay not to take a reference because, when a | |
d6e69534 | 1166 | * message's reference count drops to 0, either: |
5c563278 PP |
1167 | * |
1168 | * * It is recycled back to one of this graph's pool. | |
1169 | * * It is destroyed because it doesn't have any link to any | |
1170 | * graph, which means the original graph is already destroyed. | |
1171 | */ | |
d6e69534 | 1172 | g_ptr_array_add(graph->messages, msg); |
5c563278 | 1173 | } |
c5b9b441 | 1174 | |
9b4f9b42 PP |
1175 | BT_HIDDEN |
1176 | bool bt_graph_is_interrupted(const struct bt_graph *graph) | |
1177 | { | |
98b15851 | 1178 | BT_ASSERT_DBG(graph); |
9b4f9b42 PP |
1179 | return bt_interrupter_array_any_is_set(graph->interrupters); |
1180 | } | |
1181 | ||
1182 | enum bt_graph_add_interrupter_status bt_graph_add_interrupter( | |
1183 | struct bt_graph *graph, const struct bt_interrupter *intr) | |
1184 | { | |
17f3083a | 1185 | BT_ASSERT_PRE_NO_ERROR(); |
9b4f9b42 PP |
1186 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
1187 | BT_ASSERT_PRE_NON_NULL(intr, "Interrupter"); | |
1188 | g_ptr_array_add(graph->interrupters, (void *) intr); | |
6871026b | 1189 | bt_object_get_ref_no_null_check(intr); |
9b4f9b42 PP |
1190 | BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z", |
1191 | graph, intr); | |
1192 | return BT_FUNC_STATUS_OK; | |
1193 | } | |
1194 | ||
c513d240 | 1195 | struct bt_interrupter *bt_graph_borrow_default_interrupter(bt_graph *graph) |
9b4f9b42 PP |
1196 | { |
1197 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); | |
c513d240 | 1198 | return graph->default_interrupter; |
9b4f9b42 PP |
1199 | } |
1200 | ||
c5b9b441 PP |
1201 | void bt_graph_get_ref(const struct bt_graph *graph) |
1202 | { | |
1203 | bt_object_get_ref(graph); | |
1204 | } | |
1205 | ||
1206 | void bt_graph_put_ref(const struct bt_graph *graph) | |
1207 | { | |
1208 | bt_object_put_ref(graph); | |
1209 | } |