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