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