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