lib: add stream activity beginning/end messages
[babeltrace.git] / lib / graph / graph.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
24 #define BT_LOG_TAG "GRAPH"
25 #include <babeltrace/lib-logging-internal.h>
26
27 #include <babeltrace/assert-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/graph/component-internal.h>
30 #include <babeltrace/graph/graph.h>
31 #include <babeltrace/graph/graph-const.h>
32 #include <babeltrace/graph/graph-internal.h>
33 #include <babeltrace/graph/connection-internal.h>
34 #include <babeltrace/graph/component-sink-internal.h>
35 #include <babeltrace/graph/component-source-const.h>
36 #include <babeltrace/graph/component-filter-const.h>
37 #include <babeltrace/graph/port-const.h>
38 #include <babeltrace/graph/message-internal.h>
39 #include <babeltrace/graph/message-event-internal.h>
40 #include <babeltrace/graph/message-packet-internal.h>
41 #include <babeltrace/compiler-internal.h>
42 #include <babeltrace/common-internal.h>
43 #include <babeltrace/types.h>
44 #include <babeltrace/value.h>
45 #include <babeltrace/value-const.h>
46 #include <babeltrace/value-internal.h>
47 #include <unistd.h>
48 #include <glib.h>
49
50 typedef void (*port_added_func_t)(const void *, const void *, void *);
51
52 typedef void (*ports_connected_func_t)(const void *, const void *, const void *,
53 const void *, void *);
54
55 typedef enum bt_self_component_status (*comp_init_method_t)(const void *,
56 const void *, void *);
57
58 struct bt_graph_listener {
59 bt_graph_listener_removed_func removed;
60 void *data;
61 };
62
63 struct bt_graph_listener_port_added {
64 struct bt_graph_listener base;
65 port_added_func_t func;
66 };
67
68 struct bt_graph_listener_ports_connected {
69 struct bt_graph_listener base;
70 ports_connected_func_t func;
71 };
72
73 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
74 do { \
75 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
76 if (!(_listeners)) { \
77 BT_LOGE_STR("Failed to allocate one GArray."); \
78 } \
79 } while (0)
80
81 #define CALL_REMOVE_LISTENERS(_type, _listeners) \
82 do { \
83 size_t i; \
84 \
85 for (i = 0; i < (_listeners)->len; i++) { \
86 _type *listener = \
87 &g_array_index((_listeners), _type, i); \
88 \
89 if (listener->base.removed) { \
90 listener->base.removed(listener->base.data); \
91 } \
92 } \
93 } while (0)
94
95 static
96 void destroy_graph(struct bt_object *obj)
97 {
98 struct bt_graph *graph = container_of(obj, struct bt_graph, base);
99
100 /*
101 * The graph's reference count is 0 if we're here. Increment
102 * it to avoid a double-destroy (possibly infinitely recursive)
103 * in this situation:
104 *
105 * 1. We put and destroy a connection.
106 * 2. This connection's destructor finalizes its active message
107 * iterators.
108 * 3. A message iterator's finalization function gets a new
109 * reference on its component (reference count goes from 0 to
110 * 1).
111 * 4. Since this component's reference count goes to 1, it takes
112 * a reference on its parent (this graph). This graph's
113 * reference count goes from 0 to 1.
114 * 5. The message iterator's finalization function puts its
115 * component reference (reference count goes from 1 to 0).
116 * 6. Since this component's reference count goes from 1 to 0,
117 * it puts its parent (this graph). This graph's reference
118 * count goes from 1 to 0.
119 * 7. Since this graph's reference count goes from 1 to 0, its
120 * destructor is called (this function).
121 *
122 * With the incrementation below, the graph's reference count at
123 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
124 * ensures that this function is not called two times.
125 */
126 BT_LIB_LOGD("Destroying graph: %!+g", graph);
127 obj->ref_count++;
128
129 /*
130 * Cancel the graph to disallow some operations, like creating
131 * message iterators and adding ports to components.
132 */
133 (void) bt_graph_cancel((void *) graph);
134
135 /* Call all remove listeners */
136 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
137 graph->listeners.source_output_port_added);
138 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
139 graph->listeners.filter_output_port_added);
140 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
141 graph->listeners.filter_input_port_added);
142 CALL_REMOVE_LISTENERS(struct bt_graph_listener_port_added,
143 graph->listeners.sink_input_port_added);
144 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
145 graph->listeners.source_filter_ports_connected);
146 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
147 graph->listeners.filter_filter_ports_connected);
148 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
149 graph->listeners.source_sink_ports_connected);
150 CALL_REMOVE_LISTENERS(struct bt_graph_listener_ports_connected,
151 graph->listeners.filter_sink_ports_connected);
152
153 if (graph->messages) {
154 g_ptr_array_free(graph->messages, TRUE);
155 graph->messages = NULL;
156 }
157
158 if (graph->connections) {
159 BT_LOGD_STR("Destroying connections.");
160 g_ptr_array_free(graph->connections, TRUE);
161 graph->connections = NULL;
162 }
163
164 if (graph->components) {
165 BT_LOGD_STR("Destroying components.");
166 g_ptr_array_free(graph->components, TRUE);
167 graph->components = NULL;
168 }
169
170 if (graph->sinks_to_consume) {
171 g_queue_free(graph->sinks_to_consume);
172 graph->sinks_to_consume = NULL;
173 }
174
175 if (graph->listeners.source_output_port_added) {
176 g_array_free(graph->listeners.source_output_port_added, TRUE);
177 graph->listeners.source_output_port_added = NULL;
178 }
179
180 if (graph->listeners.filter_output_port_added) {
181 g_array_free(graph->listeners.filter_output_port_added, TRUE);
182 graph->listeners.filter_output_port_added = NULL;
183 }
184
185 if (graph->listeners.filter_input_port_added) {
186 g_array_free(graph->listeners.filter_input_port_added, TRUE);
187 graph->listeners.filter_input_port_added = NULL;
188 }
189
190 if (graph->listeners.sink_input_port_added) {
191 g_array_free(graph->listeners.sink_input_port_added, TRUE);
192 graph->listeners.sink_input_port_added = NULL;
193 }
194
195 if (graph->listeners.source_filter_ports_connected) {
196 g_array_free(graph->listeners.source_filter_ports_connected,
197 TRUE);
198 graph->listeners.source_filter_ports_connected = NULL;
199 }
200
201 if (graph->listeners.filter_filter_ports_connected) {
202 g_array_free(graph->listeners.filter_filter_ports_connected,
203 TRUE);
204 graph->listeners.filter_filter_ports_connected = NULL;
205 }
206
207 if (graph->listeners.source_sink_ports_connected) {
208 g_array_free(graph->listeners.source_sink_ports_connected,
209 TRUE);
210 graph->listeners.source_sink_ports_connected = NULL;
211 }
212
213 if (graph->listeners.filter_sink_ports_connected) {
214 g_array_free(graph->listeners.filter_sink_ports_connected,
215 TRUE);
216 graph->listeners.filter_sink_ports_connected = NULL;
217 }
218
219 bt_object_pool_finalize(&graph->event_msg_pool);
220 bt_object_pool_finalize(&graph->packet_begin_msg_pool);
221 bt_object_pool_finalize(&graph->packet_end_msg_pool);
222 g_free(graph);
223 }
224
225 static
226 void destroy_message_event(struct bt_message *msg,
227 struct bt_graph *graph)
228 {
229 bt_message_event_destroy(msg);
230 }
231
232 static
233 void destroy_message_packet_begin(struct bt_message *msg,
234 struct bt_graph *graph)
235 {
236 bt_message_packet_destroy(msg);
237 }
238
239 static
240 void destroy_message_packet_end(struct bt_message *msg,
241 struct bt_graph *graph)
242 {
243 bt_message_packet_destroy(msg);
244 }
245
246 static
247 void notify_message_graph_is_destroyed(struct bt_message *msg)
248 {
249 bt_message_unlink_graph(msg);
250 }
251
252 struct bt_graph *bt_graph_create(void)
253 {
254 struct bt_graph *graph;
255 int ret;
256
257 BT_LOGD_STR("Creating graph object.");
258 graph = g_new0(struct bt_graph, 1);
259 if (!graph) {
260 BT_LOGE_STR("Failed to allocate one graph.");
261 goto end;
262 }
263
264 bt_object_init_shared(&graph->base, destroy_graph);
265 graph->connections = g_ptr_array_new_with_free_func(
266 (GDestroyNotify) bt_object_try_spec_release);
267 if (!graph->connections) {
268 BT_LOGE_STR("Failed to allocate one GPtrArray.");
269 goto error;
270 }
271 graph->components = g_ptr_array_new_with_free_func(
272 (GDestroyNotify) bt_object_try_spec_release);
273 if (!graph->components) {
274 BT_LOGE_STR("Failed to allocate one GPtrArray.");
275 goto error;
276 }
277 graph->sinks_to_consume = g_queue_new();
278 if (!graph->sinks_to_consume) {
279 BT_LOGE_STR("Failed to allocate one GQueue.");
280 goto error;
281 }
282
283 bt_graph_set_can_consume(graph, true);
284 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
285 graph->listeners.source_output_port_added);
286
287 if (!graph->listeners.source_output_port_added) {
288 ret = -1;
289 goto error;
290 }
291
292 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
293 graph->listeners.filter_output_port_added);
294
295 if (!graph->listeners.filter_output_port_added) {
296 ret = -1;
297 goto error;
298 }
299
300 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
301 graph->listeners.filter_input_port_added);
302
303 if (!graph->listeners.filter_input_port_added) {
304 ret = -1;
305 goto error;
306 }
307
308 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added,
309 graph->listeners.sink_input_port_added);
310
311 if (!graph->listeners.sink_input_port_added) {
312 ret = -1;
313 goto error;
314 }
315
316 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
317 graph->listeners.source_filter_ports_connected);
318
319 if (!graph->listeners.source_filter_ports_connected) {
320 ret = -1;
321 goto error;
322 }
323
324 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
325 graph->listeners.source_sink_ports_connected);
326
327 if (!graph->listeners.source_sink_ports_connected) {
328 ret = -1;
329 goto error;
330 }
331
332 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
333 graph->listeners.filter_filter_ports_connected);
334
335 if (!graph->listeners.filter_filter_ports_connected) {
336 ret = -1;
337 goto error;
338 }
339
340 INIT_LISTENERS_ARRAY(struct bt_graph_listener_ports_connected,
341 graph->listeners.filter_sink_ports_connected);
342
343 if (!graph->listeners.filter_sink_ports_connected) {
344 ret = -1;
345 goto error;
346 }
347
348 ret = bt_object_pool_initialize(&graph->event_msg_pool,
349 (bt_object_pool_new_object_func) bt_message_event_new,
350 (bt_object_pool_destroy_object_func) destroy_message_event,
351 graph);
352 if (ret) {
353 BT_LOGE("Failed to initialize event message pool: ret=%d",
354 ret);
355 goto error;
356 }
357
358 ret = bt_object_pool_initialize(&graph->packet_begin_msg_pool,
359 (bt_object_pool_new_object_func) bt_message_packet_beginning_new,
360 (bt_object_pool_destroy_object_func) destroy_message_packet_begin,
361 graph);
362 if (ret) {
363 BT_LOGE("Failed to initialize packet beginning message pool: ret=%d",
364 ret);
365 goto error;
366 }
367
368 ret = bt_object_pool_initialize(&graph->packet_end_msg_pool,
369 (bt_object_pool_new_object_func) bt_message_packet_end_new,
370 (bt_object_pool_destroy_object_func) destroy_message_packet_end,
371 graph);
372 if (ret) {
373 BT_LOGE("Failed to initialize packet end message pool: ret=%d",
374 ret);
375 goto error;
376 }
377
378 graph->messages = g_ptr_array_new_with_free_func(
379 (GDestroyNotify) notify_message_graph_is_destroyed);
380 BT_LIB_LOGD("Created graph object: %!+g", graph);
381
382 end:
383 return (void *) graph;
384
385 error:
386 BT_OBJECT_PUT_REF_AND_RESET(graph);
387 goto end;
388 }
389
390 enum bt_graph_status bt_graph_connect_ports(
391 struct bt_graph *graph,
392 const struct bt_port_output *upstream_port_out,
393 const struct bt_port_input *downstream_port_in,
394 const struct bt_connection **user_connection)
395 {
396 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
397 struct bt_connection *connection = NULL;
398 struct bt_port *upstream_port = (void *) upstream_port_out;
399 struct bt_port *downstream_port = (void *) downstream_port_in;
400 struct bt_component *upstream_component = NULL;
401 struct bt_component *downstream_component = NULL;
402 enum bt_self_component_status component_status;
403 bool init_can_consume;
404
405 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
406 BT_ASSERT_PRE_NON_NULL(upstream_port, "Upstream port");
407 BT_ASSERT_PRE_NON_NULL(downstream_port, "Downstream port port");
408 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
409 BT_ASSERT_PRE(!graph->is_configured,
410 "Graph is already configured: %!+g", graph);
411 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port),
412 "Upstream port is already connected: %!+p", upstream_port);
413 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port),
414 "Downstream port is already connected: %!+p", downstream_port);
415 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port),
416 "Upstream port does not belong to a component: %!+p",
417 upstream_port);
418 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port),
419 "Downstream port does not belong to a component: %!+p",
420 downstream_port);
421 init_can_consume = graph->can_consume;
422 BT_LIB_LOGD("Connecting component ports within graph: "
423 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
424 graph, upstream_port, downstream_port);
425 bt_graph_set_can_consume(graph, false);
426 upstream_component = bt_port_borrow_component_inline(
427 (void *) upstream_port);
428 downstream_component = bt_port_borrow_component_inline(
429 (void *) downstream_port);
430
431 /*
432 * At this point the ports are not connected yet. Both
433 * components need to accept an eventual connection to their
434 * port by the other port before we continue.
435 */
436 BT_LIB_LOGD("Asking upstream component to accept the connection: "
437 "%![comp-]+c", upstream_component);
438 component_status = bt_component_accept_port_connection(
439 upstream_component, (void *) upstream_port,
440 (void *) downstream_port);
441 if (component_status != BT_SELF_COMPONENT_STATUS_OK) {
442 if (component_status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
443 BT_LOGD_STR("Upstream component refused the connection.");
444 } else {
445 BT_LOGW("Cannot ask upstream component to accept the connection: "
446 "status=%s", bt_self_component_status_string(component_status));
447 }
448
449 status = (int) component_status;
450 goto end;
451 }
452
453 BT_LIB_LOGD("Asking downstream component to accept the connection: "
454 "%![comp-]+c", downstream_component);
455 component_status = bt_component_accept_port_connection(
456 downstream_component, (void *) downstream_port,
457 (void *) upstream_port);
458 if (component_status != BT_SELF_COMPONENT_STATUS_OK) {
459 if (component_status == BT_SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
460 BT_LOGD_STR("Downstream component refused the connection.");
461 } else {
462 BT_LOGW("Cannot ask downstream component to accept the connection: "
463 "status=%s", bt_self_component_status_string(component_status));
464 }
465
466 status = (int) component_status;
467 goto end;
468 }
469
470 BT_LOGD_STR("Creating connection.");
471 connection = bt_connection_create(graph, (void *) upstream_port,
472 (void *) downstream_port);
473 if (!connection) {
474 BT_LOGW("Cannot create connection object.");
475 status = BT_GRAPH_STATUS_NOMEM;
476 goto end;
477 }
478
479 BT_LIB_LOGD("Connection object created: %!+x", connection);
480
481 /*
482 * Ownership of upstream_component/downstream_component and of
483 * the connection object is transferred to the graph.
484 */
485 g_ptr_array_add(graph->connections, connection);
486
487 /*
488 * Notify both components that their port is connected.
489 */
490 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
491 "%![comp-]+c, %![port-]+p", upstream_component, upstream_port);
492 component_status = bt_component_port_connected(upstream_component,
493 (void *) upstream_port, (void *) downstream_port);
494 if (component_status != BT_SELF_COMPONENT_STATUS_OK) {
495 BT_LIB_LOGW("Error while notifying upstream component that its port is connected: "
496 "status=%s, %![graph-]+g, %![up-comp-]+c, "
497 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
498 bt_self_component_status_string(component_status),
499 graph, upstream_component, downstream_component,
500 upstream_port, downstream_port);
501 bt_connection_end(connection, true);
502 status = (int) component_status;
503 goto end;
504 }
505
506 connection->notified_upstream_port_connected = true;
507 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
508 "%![comp-]+c, %![port-]+p", downstream_component,
509 downstream_port);
510 component_status = bt_component_port_connected(downstream_component,
511 (void *) downstream_port, (void *) upstream_port);
512 if (component_status != BT_SELF_COMPONENT_STATUS_OK) {
513 BT_LIB_LOGW("Error while notifying downstream component that its port is connected: "
514 "status=%s, %![graph-]+g, %![up-comp-]+c, "
515 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
516 bt_self_component_status_string(component_status),
517 graph, upstream_component, downstream_component,
518 upstream_port, downstream_port);
519 bt_connection_end(connection, true);
520 status = (int) component_status;
521 goto end;
522 }
523
524 connection->notified_downstream_port_connected = true;
525
526 /*
527 * Notify the graph's creator that both ports are connected.
528 */
529 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
530 bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
531 connection->notified_graph_ports_connected = true;
532 BT_LIB_LOGD("Connected component ports within graph: "
533 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
534 "%![up-port-]+p, %![down-port-]+p",
535 graph, upstream_component, downstream_component,
536 upstream_port, downstream_port);
537
538 if (user_connection) {
539 /* Move reference to user */
540 *user_connection = connection;
541 connection = NULL;
542 }
543
544 end:
545 bt_object_put_ref(connection);
546 (void) init_can_consume;
547 bt_graph_set_can_consume(graph, init_can_consume);
548 return status;
549 }
550
551 static inline
552 enum bt_graph_status consume_graph_sink(struct bt_component_sink *comp)
553 {
554 enum bt_self_component_status comp_status;
555 struct bt_component_class_sink *sink_class = NULL;
556
557 BT_ASSERT(comp);
558 sink_class = (void *) comp->parent.class;
559 BT_ASSERT(sink_class->methods.consume);
560 BT_LIB_LOGD("Calling user's consume method: %!+c", comp);
561 comp_status = sink_class->methods.consume((void *) comp);
562 BT_LOGD("User method returned: status=%s",
563 bt_self_component_status_string(comp_status));
564 BT_ASSERT_PRE(comp_status == BT_SELF_COMPONENT_STATUS_OK ||
565 comp_status == BT_SELF_COMPONENT_STATUS_END ||
566 comp_status == BT_SELF_COMPONENT_STATUS_AGAIN ||
567 comp_status == BT_SELF_COMPONENT_STATUS_ERROR ||
568 comp_status == BT_SELF_COMPONENT_STATUS_NOMEM,
569 "Invalid component status returned by consuming method: "
570 "status=%s", bt_self_component_status_string(comp_status));
571 if (comp_status < 0) {
572 BT_LOGW_STR("Consume method failed.");
573 goto end;
574 }
575
576 BT_LIB_LOGV("Consumed from sink: %![comp-]+c, status=%s",
577 comp, bt_self_component_status_string(comp_status));
578
579 end:
580 return (int) comp_status;
581 }
582
583 /*
584 * `node` is removed from the queue of sinks to consume when passed to
585 * this function. This function adds it back to the queue if there's
586 * still something to consume afterwards.
587 */
588 static inline
589 enum bt_graph_status consume_sink_node(struct bt_graph *graph, GList *node)
590 {
591 enum bt_graph_status status;
592 struct bt_component_sink *sink;
593
594 sink = node->data;
595 status = consume_graph_sink(sink);
596 if (unlikely(status != BT_GRAPH_STATUS_END)) {
597 g_queue_push_tail_link(graph->sinks_to_consume, node);
598 goto end;
599 }
600
601 /* End reached, the node is not added back to the queue and free'd. */
602 g_queue_delete_link(graph->sinks_to_consume, node);
603
604 /* Don't forward an END status if there are sinks left to consume. */
605 if (!g_queue_is_empty(graph->sinks_to_consume)) {
606 status = BT_GRAPH_STATUS_OK;
607 goto end;
608 }
609
610 end:
611 BT_LIB_LOGV("Consumed sink node: %![comp-]+c, status=%s",
612 sink, bt_graph_status_string(status));
613 return status;
614 }
615
616 BT_HIDDEN
617 enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
618 struct bt_component_sink *sink)
619 {
620 enum bt_graph_status status;
621 GList *sink_node;
622 int index;
623
624 BT_LIB_LOGV("Making specific sink consume: %![comp-]+c", sink);
625 BT_ASSERT(bt_component_borrow_graph((void *) sink) == graph);
626
627 if (g_queue_is_empty(graph->sinks_to_consume)) {
628 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
629 status = BT_GRAPH_STATUS_END;
630 goto end;
631 }
632
633 index = g_queue_index(graph->sinks_to_consume, sink);
634 if (index < 0) {
635 BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
636 status = BT_GRAPH_STATUS_END;
637 goto end;
638 }
639
640 sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index);
641 BT_ASSERT(sink_node);
642 status = consume_sink_node(graph, sink_node);
643
644 end:
645 return status;
646 }
647
648 static inline
649 enum bt_graph_status consume_no_check(struct bt_graph *graph)
650 {
651 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
652 struct bt_component *sink;
653 GList *current_node;
654
655 BT_ASSERT_PRE(graph->has_sink,
656 "Graph has no sink component: %!+g", graph);
657 BT_LIB_LOGV("Making next sink consume: %![graph-]+g", graph);
658
659 if (unlikely(g_queue_is_empty(graph->sinks_to_consume))) {
660 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
661 status = BT_GRAPH_STATUS_END;
662 goto end;
663 }
664
665 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
666 sink = current_node->data;
667 BT_LIB_LOGV("Chose next sink to consume: %!+c", sink);
668 status = consume_sink_node(graph, current_node);
669
670 end:
671 return status;
672 }
673
674 enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
675 {
676 enum bt_graph_status status;
677
678 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
679 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
680 BT_ASSERT_PRE(graph->can_consume,
681 "Cannot consume graph in its current state: %!+g", graph);
682 bt_graph_set_can_consume(graph, false);
683 bt_graph_set_is_configured(graph, true);
684 status = consume_no_check(graph);
685 bt_graph_set_can_consume(graph, true);
686 return status;
687 }
688
689 enum bt_graph_status bt_graph_run(struct bt_graph *graph)
690 {
691 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
692
693 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
694 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
695 BT_ASSERT_PRE(graph->can_consume,
696 "Cannot consume graph in its current state: %!+g", graph);
697 bt_graph_set_can_consume(graph, false);
698 bt_graph_set_is_configured(graph, true);
699 BT_LIB_LOGV("Running graph: %!+g", graph);
700
701 do {
702 /*
703 * Check if the graph is canceled at each iteration. If
704 * the graph was canceled by another thread or by a
705 * signal handler, this is not a warning nor an error,
706 * it was intentional: log with a DEBUG level only.
707 */
708 if (unlikely(graph->canceled)) {
709 BT_LIB_LOGD("Stopping the graph: graph is canceled: "
710 "%!+g", graph);
711 status = BT_GRAPH_STATUS_CANCELED;
712 goto end;
713 }
714
715 status = consume_no_check(graph);
716 if (unlikely(status == BT_GRAPH_STATUS_AGAIN)) {
717 /*
718 * If AGAIN is received and there are multiple
719 * sinks, go ahead and consume from the next
720 * sink.
721 *
722 * However, in the case where a single sink is
723 * left, the caller can decide to busy-wait and
724 * call bt_graph_run() continuously
725 * until the source is ready or it can decide to
726 * sleep for an arbitrary amount of time.
727 */
728 if (graph->sinks_to_consume->length > 1) {
729 status = BT_GRAPH_STATUS_OK;
730 }
731 } else if (status == BT_GRAPH_STATUS_NO_SINK) {
732 goto end;
733 }
734 } while (status == BT_GRAPH_STATUS_OK);
735
736 if (g_queue_is_empty(graph->sinks_to_consume)) {
737 status = BT_GRAPH_STATUS_END;
738 }
739
740 end:
741 BT_LIB_LOGV("Graph ran: %![graph-]+g, status=%s", graph,
742 bt_graph_status_string(status));
743 bt_graph_set_can_consume(graph, true);
744 return status;
745 }
746
747 enum bt_graph_status
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,
751 bt_graph_listener_removed_func listener_removed, void *data,
752 int *out_listener_id)
753 {
754 struct bt_graph_listener_port_added listener = {
755 .base = {
756 .removed = listener_removed,
757 .data = data,
758 },
759 .func = (port_added_func_t) func,
760 };
761 int listener_id;
762
763 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
764 BT_ASSERT_PRE_NON_NULL(func, "Listener");
765 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
766 BT_ASSERT_PRE(!graph->in_remove_listener,
767 "Graph currently executing a \"listener removed\" listener: "
768 "%!+g", graph);
769 g_array_append_val(graph->listeners.source_output_port_added, listener);
770 listener_id = graph->listeners.source_output_port_added->len - 1;
771 BT_LIB_LOGV("Added \"source component output port added\" listener to graph: "
772 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
773 listener_id);
774
775 if (listener_id) {
776 *out_listener_id = listener_id;
777 }
778
779 return BT_GRAPH_STATUS_OK;
780 }
781
782 enum bt_graph_status
783 bt_graph_add_filter_component_output_port_added_listener(
784 struct bt_graph *graph,
785 bt_graph_filter_component_output_port_added_listener_func func,
786 bt_graph_listener_removed_func listener_removed, void *data,
787 int *out_listener_id)
788 {
789 struct bt_graph_listener_port_added listener = {
790 .base = {
791 .removed = listener_removed,
792 .data = data,
793 },
794 .func = (port_added_func_t) func,
795 };
796 int listener_id;
797
798 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
799 BT_ASSERT_PRE_NON_NULL(func, "Listener");
800 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
801 BT_ASSERT_PRE(!graph->in_remove_listener,
802 "Graph currently executing a \"listener removed\" listener: "
803 "%!+g", graph);
804 g_array_append_val(graph->listeners.filter_output_port_added, listener);
805 listener_id = graph->listeners.filter_output_port_added->len - 1;
806 BT_LIB_LOGV("Added \"filter component output port added\" listener to graph: "
807 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
808 listener_id);
809
810 if (listener_id) {
811 *out_listener_id = listener_id;
812 }
813
814 return BT_GRAPH_STATUS_OK;
815 }
816
817 enum bt_graph_status
818 bt_graph_add_filter_component_input_port_added_listener(
819 struct bt_graph *graph,
820 bt_graph_filter_component_input_port_added_listener_func func,
821 bt_graph_listener_removed_func listener_removed, void *data,
822 int *out_listener_id)
823 {
824 struct bt_graph_listener_port_added listener = {
825 .base = {
826 .removed = listener_removed,
827 .data = data,
828 },
829 .func = (port_added_func_t) func,
830 };
831 int listener_id;
832
833 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
834 BT_ASSERT_PRE_NON_NULL(func, "Listener");
835 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
836 BT_ASSERT_PRE(!graph->in_remove_listener,
837 "Graph currently executing a \"listener removed\" listener: "
838 "%!+g", graph);
839 g_array_append_val(graph->listeners.filter_input_port_added, listener);
840 listener_id = graph->listeners.filter_input_port_added->len - 1;
841 BT_LIB_LOGV("Added \"filter component input port added\" listener to graph: "
842 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
843 listener_id);
844
845 if (listener_id) {
846 *out_listener_id = listener_id;
847 }
848
849 return BT_GRAPH_STATUS_OK;
850 }
851
852 enum bt_graph_status
853 bt_graph_add_sink_component_input_port_added_listener(
854 struct bt_graph *graph,
855 bt_graph_sink_component_input_port_added_listener_func func,
856 bt_graph_listener_removed_func listener_removed, void *data,
857 int *out_listener_id)
858 {
859 struct bt_graph_listener_port_added listener = {
860 .base = {
861 .removed = listener_removed,
862 .data = data,
863 },
864 .func = (port_added_func_t) func,
865 };
866 int listener_id;
867
868 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
869 BT_ASSERT_PRE_NON_NULL(func, "Listener");
870 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
871 BT_ASSERT_PRE(!graph->in_remove_listener,
872 "Graph currently executing a \"listener removed\" listener: "
873 "%!+g", graph);
874 g_array_append_val(graph->listeners.sink_input_port_added, listener);
875 listener_id = graph->listeners.sink_input_port_added->len - 1;
876 BT_LIB_LOGV("Added \"sink component input port added\" listener to graph: "
877 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
878 listener_id);
879
880 if (listener_id) {
881 *out_listener_id = listener_id;
882 }
883
884 return BT_GRAPH_STATUS_OK;
885 }
886
887 enum bt_graph_status
888 bt_graph_add_source_filter_component_ports_connected_listener(
889 struct bt_graph *graph,
890 bt_graph_source_filter_component_ports_connected_listener_func func,
891 bt_graph_listener_removed_func listener_removed, void *data,
892 int *out_listener_id)
893 {
894 struct bt_graph_listener_ports_connected listener = {
895 .base = {
896 .removed = listener_removed,
897 .data = data,
898 },
899 .func = (ports_connected_func_t) func,
900 };
901 int listener_id;
902
903 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
904 BT_ASSERT_PRE_NON_NULL(func, "Listener");
905 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
906 BT_ASSERT_PRE(!graph->in_remove_listener,
907 "Graph currently executing a \"listener removed\" listener: "
908 "%!+g", graph);
909 g_array_append_val(graph->listeners.source_filter_ports_connected,
910 listener);
911 listener_id = graph->listeners.source_filter_ports_connected->len - 1;
912 BT_LIB_LOGV("Added \"source to filter component ports connected\" listener to graph: "
913 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
914 listener_id);
915
916 if (listener_id) {
917 *out_listener_id = listener_id;
918 }
919
920 return BT_GRAPH_STATUS_OK;
921 }
922
923 enum bt_graph_status
924 bt_graph_add_source_sink_component_ports_connected_listener(
925 struct bt_graph *graph,
926 bt_graph_source_sink_component_ports_connected_listener_func func,
927 bt_graph_listener_removed_func listener_removed, void *data,
928 int *out_listener_id)
929 {
930 struct bt_graph_listener_ports_connected listener = {
931 .base = {
932 .removed = listener_removed,
933 .data = data,
934 },
935 .func = (ports_connected_func_t) func,
936 };
937 int listener_id;
938
939 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
940 BT_ASSERT_PRE_NON_NULL(func, "Listener");
941 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
942 BT_ASSERT_PRE(!graph->in_remove_listener,
943 "Graph currently executing a \"listener removed\" listener: "
944 "%!+g", graph);
945 g_array_append_val(graph->listeners.source_sink_ports_connected,
946 listener);
947 listener_id = graph->listeners.source_sink_ports_connected->len - 1;
948 BT_LIB_LOGV("Added \"source to sink component ports connected\" listener to graph: "
949 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
950 listener_id);
951
952 if (listener_id) {
953 *out_listener_id = listener_id;
954 }
955
956 return BT_GRAPH_STATUS_OK;
957 }
958
959 enum bt_graph_status
960 bt_graph_add_filter_filter_component_ports_connected_listener(
961 struct bt_graph *graph,
962 bt_graph_filter_filter_component_ports_connected_listener_func func,
963 bt_graph_listener_removed_func listener_removed, void *data,
964 int *out_listener_id)
965 {
966 struct bt_graph_listener_ports_connected listener = {
967 .base = {
968 .removed = listener_removed,
969 .data = data,
970 },
971 .func = (ports_connected_func_t) func,
972 };
973 int listener_id;
974
975 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
976 BT_ASSERT_PRE_NON_NULL(func, "Listener");
977 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
978 BT_ASSERT_PRE(!graph->in_remove_listener,
979 "Graph currently executing a \"listener removed\" listener: "
980 "%!+g", graph);
981 g_array_append_val(graph->listeners.filter_filter_ports_connected,
982 listener);
983 listener_id = graph->listeners.filter_filter_ports_connected->len - 1;
984 BT_LIB_LOGV("Added \"filter to filter component ports connected\" listener to graph: "
985 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
986 listener_id);
987
988 if (listener_id) {
989 *out_listener_id = listener_id;
990 }
991
992 return BT_GRAPH_STATUS_OK;
993 }
994
995 enum bt_graph_status
996 bt_graph_add_filter_sink_component_ports_connected_listener(
997 struct bt_graph *graph,
998 bt_graph_filter_sink_component_ports_connected_listener_func func,
999 bt_graph_listener_removed_func listener_removed, void *data,
1000 int *out_listener_id)
1001 {
1002 struct bt_graph_listener_ports_connected listener = {
1003 .base = {
1004 .removed = listener_removed,
1005 .data = data,
1006 },
1007 .func = (ports_connected_func_t) func,
1008 };
1009 int listener_id;
1010
1011 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1012 BT_ASSERT_PRE_NON_NULL(func, "Listener");
1013 BT_ASSERT_PRE_NON_NULL(func, "\"Listener removed\" listener");
1014 BT_ASSERT_PRE(!graph->in_remove_listener,
1015 "Graph currently executing a \"listener removed\" listener: "
1016 "%!+g", graph);
1017 g_array_append_val(graph->listeners.filter_sink_ports_connected,
1018 listener);
1019 listener_id = graph->listeners.filter_sink_ports_connected->len - 1;
1020 BT_LIB_LOGV("Added \"filter to sink component ports connected\" listener to graph: "
1021 "%![graph-]+g, listener-addr=%p, id=%d", graph, listener,
1022 listener_id);
1023
1024 if (listener_id) {
1025 *out_listener_id = listener_id;
1026 }
1027
1028 return BT_GRAPH_STATUS_OK;
1029 }
1030
1031 BT_HIDDEN
1032 void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port)
1033 {
1034 uint64_t i;
1035 GArray *listeners;
1036 struct bt_component *comp;
1037
1038 BT_ASSERT(graph);
1039 BT_ASSERT(port);
1040 BT_LIB_LOGV("Notifying graph listeners that a port was added: "
1041 "%![graph-]+g, %![port-]+p", graph, port);
1042 comp = bt_port_borrow_component_inline(port);
1043 BT_ASSERT(comp);
1044
1045 switch (comp->class->type) {
1046 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1047 {
1048 switch (port->type) {
1049 case BT_PORT_TYPE_OUTPUT:
1050 listeners = graph->listeners.source_output_port_added;
1051 break;
1052 default:
1053 abort();
1054 }
1055
1056 break;
1057 }
1058 case BT_COMPONENT_CLASS_TYPE_FILTER:
1059 {
1060 switch (port->type) {
1061 case BT_PORT_TYPE_INPUT:
1062 listeners = graph->listeners.filter_input_port_added;
1063 break;
1064 case BT_PORT_TYPE_OUTPUT:
1065 listeners = graph->listeners.filter_output_port_added;
1066 break;
1067 default:
1068 abort();
1069 }
1070
1071 break;
1072 }
1073 case BT_COMPONENT_CLASS_TYPE_SINK:
1074 {
1075 switch (port->type) {
1076 case BT_PORT_TYPE_INPUT:
1077 listeners = graph->listeners.sink_input_port_added;
1078 break;
1079 default:
1080 abort();
1081 }
1082
1083 break;
1084 }
1085 default:
1086 abort();
1087 }
1088
1089 for (i = 0; i < listeners->len; i++) {
1090 struct bt_graph_listener_port_added *listener =
1091 &g_array_index(listeners,
1092 struct bt_graph_listener_port_added, i);
1093
1094 BT_ASSERT(listener->func);
1095 listener->func(comp, port, listener->base.data);
1096 }
1097 }
1098
1099 BT_HIDDEN
1100 void bt_graph_notify_ports_connected(struct bt_graph *graph,
1101 struct bt_port *upstream_port, struct bt_port *downstream_port)
1102 {
1103 uint64_t i;
1104 GArray *listeners;
1105 struct bt_component *upstream_comp;
1106 struct bt_component *downstream_comp;
1107
1108 BT_ASSERT(graph);
1109 BT_ASSERT(upstream_port);
1110 BT_ASSERT(downstream_port);
1111 BT_LIB_LOGV("Notifying graph listeners that ports were connected: "
1112 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
1113 graph, upstream_port, downstream_port);
1114 upstream_comp = bt_port_borrow_component_inline(upstream_port);
1115 BT_ASSERT(upstream_comp);
1116 downstream_comp = bt_port_borrow_component_inline(downstream_port);
1117 BT_ASSERT(downstream_comp);
1118
1119 switch (upstream_comp->class->type) {
1120 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1121 {
1122 switch (downstream_comp->class->type) {
1123 case BT_COMPONENT_CLASS_TYPE_FILTER:
1124 listeners =
1125 graph->listeners.source_filter_ports_connected;
1126 break;
1127 case BT_COMPONENT_CLASS_TYPE_SINK:
1128 listeners =
1129 graph->listeners.source_sink_ports_connected;
1130 break;
1131 default:
1132 abort();
1133 }
1134
1135 break;
1136 }
1137 case BT_COMPONENT_CLASS_TYPE_FILTER:
1138 {
1139 switch (downstream_comp->class->type) {
1140 case BT_COMPONENT_CLASS_TYPE_FILTER:
1141 listeners =
1142 graph->listeners.filter_filter_ports_connected;
1143 break;
1144 case BT_COMPONENT_CLASS_TYPE_SINK:
1145 listeners =
1146 graph->listeners.filter_sink_ports_connected;
1147 break;
1148 default:
1149 abort();
1150 }
1151
1152 break;
1153 }
1154 default:
1155 abort();
1156 }
1157
1158 for (i = 0; i < listeners->len; i++) {
1159 struct bt_graph_listener_ports_connected *listener =
1160 &g_array_index(listeners,
1161 struct bt_graph_listener_ports_connected, i);
1162
1163 BT_ASSERT(listener->func);
1164 listener->func(upstream_comp, downstream_comp,
1165 upstream_port, downstream_port, listener->base.data);
1166 }
1167 }
1168
1169 enum bt_graph_status bt_graph_cancel(struct bt_graph *graph)
1170 {
1171
1172 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1173 graph->canceled = true;
1174 BT_LIB_LOGV("Canceled graph: %!+i", graph);
1175 return BT_GRAPH_STATUS_OK;
1176 }
1177
1178 bt_bool bt_graph_is_canceled(const struct bt_graph *graph)
1179 {
1180 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1181 return graph->canceled ? BT_TRUE : BT_FALSE;
1182 }
1183
1184 BT_HIDDEN
1185 void bt_graph_remove_connection(struct bt_graph *graph,
1186 struct bt_connection *connection)
1187 {
1188 BT_ASSERT(graph);
1189 BT_ASSERT(connection);
1190 BT_LIB_LOGV("Removing graph's connection: %![graph-]+g, %![conn-]+x",
1191 graph, connection);
1192 g_ptr_array_remove(graph->connections, connection);
1193 }
1194
1195 BT_ASSERT_PRE_FUNC
1196 static inline
1197 bool component_name_exists(struct bt_graph *graph, const char *name)
1198 {
1199 bool exists = false;
1200 uint64_t i;
1201
1202 for (i = 0; i < graph->components->len; i++) {
1203 struct bt_component *other_comp = graph->components->pdata[i];
1204
1205 if (strcmp(name, bt_component_get_name(other_comp)) == 0) {
1206 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
1207 "%![other-comp-]+c, name=\"%s\"",
1208 other_comp, name);
1209 exists = true;
1210 goto end;
1211 }
1212 }
1213
1214 end:
1215 return exists;
1216 }
1217
1218 static
1219 enum bt_graph_status add_component_with_init_method_data(
1220 struct bt_graph *graph,
1221 struct bt_component_class *comp_cls,
1222 comp_init_method_t init_method,
1223 const char *name, const struct bt_value *params,
1224 void *init_method_data, struct bt_component **user_component)
1225 {
1226 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
1227 enum bt_self_component_status comp_status;
1228 struct bt_component *component = NULL;
1229 int ret;
1230 bool init_can_consume;
1231 struct bt_value *new_params = NULL;
1232
1233 BT_ASSERT(comp_cls);
1234 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1235 BT_ASSERT_PRE_NON_NULL(name, "Name");
1236 BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
1237 BT_ASSERT_PRE(!graph->is_configured,
1238 "Graph is already configured: %!+g", graph);
1239 BT_ASSERT_PRE(!component_name_exists(graph, name),
1240 "Duplicate component name: %!+g, name=\"%s\"", graph, name);
1241 BT_ASSERT_PRE(!params || bt_value_is_map(params),
1242 "Parameter value is not a map value: %!+v", params);
1243 init_can_consume = graph->can_consume;
1244 bt_graph_set_can_consume(graph, false);
1245 BT_LIB_LOGD("Adding component to graph: "
1246 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1247 "init-method-data-addr=%p",
1248 graph, comp_cls, name, params, init_method_data);
1249
1250 if (!params) {
1251 new_params = bt_value_map_create();
1252 if (!new_params) {
1253 BT_LOGE_STR("Cannot create map value object.");
1254 graph_status = BT_GRAPH_STATUS_NOMEM;
1255 goto end;
1256 }
1257
1258 params = new_params;
1259 }
1260
1261 ret = bt_component_create(comp_cls, name, &component);
1262 if (ret) {
1263 BT_LOGE("Cannot create empty component object: ret=%d",
1264 ret);
1265 graph_status = BT_GRAPH_STATUS_NOMEM;
1266 goto end;
1267 }
1268
1269 /*
1270 * The user's initialization method needs to see that this
1271 * component is part of the graph. If the user method fails, we
1272 * immediately remove the component from the graph's components.
1273 */
1274 g_ptr_array_add(graph->components, component);
1275 bt_component_set_graph(component, graph);
1276
1277 if (init_method) {
1278 BT_LOGD_STR("Calling user's initialization method.");
1279 comp_status = init_method(component, params, init_method_data);
1280 BT_LOGD("User method returned: status=%s",
1281 bt_self_component_status_string(comp_status));
1282 if (comp_status != BT_SELF_COMPONENT_STATUS_OK) {
1283 BT_LOGW_STR("Initialization method failed.");
1284 graph_status = (int) comp_status;
1285 bt_component_set_graph(component, NULL);
1286 g_ptr_array_remove_fast(graph->components, component);
1287 goto end;
1288 }
1289 }
1290
1291 /*
1292 * Mark the component as initialized so that its finalization
1293 * method is called when it is destroyed.
1294 */
1295 component->initialized = true;
1296
1297 /*
1298 * If it's a sink component, it needs to be part of the graph's
1299 * sink queue to be consumed by bt_graph_consume().
1300 */
1301 if (bt_component_is_sink(component)) {
1302 graph->has_sink = true;
1303 g_queue_push_tail(graph->sinks_to_consume, component);
1304 }
1305
1306 /*
1307 * Freeze the component class now that it's instantiated at
1308 * least once.
1309 */
1310 BT_LOGD_STR("Freezing component class.");
1311 bt_component_class_freeze(comp_cls);
1312 BT_LIB_LOGD("Added component to graph: "
1313 "%![graph-]+g, %![cc-]+C, name=\"%s\", %![params-]+v, "
1314 "init-method-data-addr=%p, %![comp-]+c",
1315 graph, comp_cls, name, params, init_method_data, component);
1316
1317 if (user_component) {
1318 /* Move reference to user */
1319 *user_component = component;
1320 component = NULL;
1321 }
1322
1323 end:
1324 bt_object_put_ref(component);
1325 bt_object_put_ref(new_params);
1326 (void) init_can_consume;
1327 bt_graph_set_can_consume(graph, init_can_consume);
1328 return graph_status;
1329 }
1330
1331 enum bt_graph_status
1332 bt_graph_add_source_component_with_init_method_data(
1333 struct bt_graph *graph,
1334 const struct bt_component_class_source *comp_cls,
1335 const char *name, const struct bt_value *params,
1336 void *init_method_data,
1337 const struct bt_component_source **component)
1338 {
1339 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1340 return add_component_with_init_method_data(graph,
1341 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1342 name, params, init_method_data, (void *) component);
1343 }
1344
1345 enum bt_graph_status bt_graph_add_source_component(
1346 struct bt_graph *graph,
1347 const struct bt_component_class_source *comp_cls,
1348 const char *name, const struct bt_value *params,
1349 const struct bt_component_source **component)
1350 {
1351 return bt_graph_add_source_component_with_init_method_data(
1352 graph, comp_cls, name, params, NULL, component);
1353 }
1354
1355 enum bt_graph_status
1356 bt_graph_add_filter_component_with_init_method_data(
1357 struct bt_graph *graph,
1358 const struct bt_component_class_filter *comp_cls,
1359 const char *name, const struct bt_value *params,
1360 void *init_method_data,
1361 const struct bt_component_filter **component)
1362 {
1363 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1364 return add_component_with_init_method_data(graph,
1365 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1366 name, params, init_method_data, (void *) component);
1367 }
1368
1369 enum bt_graph_status bt_graph_add_filter_component(
1370 struct bt_graph *graph,
1371 const struct bt_component_class_filter *comp_cls,
1372 const char *name, const struct bt_value *params,
1373 const struct bt_component_filter **component)
1374 {
1375 return bt_graph_add_filter_component_with_init_method_data(
1376 graph, comp_cls, name, params, NULL, component);
1377 }
1378
1379 enum bt_graph_status
1380 bt_graph_add_sink_component_with_init_method_data(
1381 struct bt_graph *graph,
1382 const struct bt_component_class_sink *comp_cls,
1383 const char *name, const struct bt_value *params,
1384 void *init_method_data,
1385 const struct bt_component_sink **component)
1386 {
1387 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
1388 return add_component_with_init_method_data(graph,
1389 (void *) comp_cls, (comp_init_method_t) comp_cls->methods.init,
1390 name, params, init_method_data, (void *) component);
1391 }
1392
1393 enum bt_graph_status bt_graph_add_sink_component(
1394 struct bt_graph *graph,
1395 const struct bt_component_class_sink *comp_cls,
1396 const char *name, const struct bt_value *params,
1397 const struct bt_component_sink **component)
1398 {
1399 return bt_graph_add_sink_component_with_init_method_data(
1400 graph, comp_cls, name, params, NULL, component);
1401 }
1402
1403 BT_HIDDEN
1404 int bt_graph_remove_unconnected_component(struct bt_graph *graph,
1405 struct bt_component *component)
1406 {
1407 bool init_can_consume;
1408 uint64_t count;
1409 uint64_t i;
1410 int ret = 0;
1411
1412 BT_ASSERT(graph);
1413 BT_ASSERT(component);
1414 BT_ASSERT(component->base.ref_count == 0);
1415 BT_ASSERT(bt_component_borrow_graph(component) == graph);
1416
1417 init_can_consume = graph->can_consume;
1418 count = bt_component_get_input_port_count(component);
1419
1420 for (i = 0; i < count; i++) {
1421 struct bt_port *port = (void *)
1422 bt_component_borrow_input_port_by_index(component, i);
1423
1424 BT_ASSERT(port);
1425
1426 if (bt_port_is_connected(port)) {
1427 BT_LIB_LOGW("Cannot remove component from graph: "
1428 "an input port is connected: "
1429 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1430 graph, component, port);
1431 goto error;
1432 }
1433 }
1434
1435 count = bt_component_get_output_port_count(component);
1436
1437 for (i = 0; i < count; i++) {
1438 struct bt_port *port = (void *)
1439 bt_component_borrow_output_port_by_index(component, i);
1440
1441 BT_ASSERT(port);
1442
1443 if (bt_port_is_connected(port)) {
1444 BT_LIB_LOGW("Cannot remove component from graph: "
1445 "an output port is connected: "
1446 "%![graph-]+g, %![comp-]+c, %![port-]+p",
1447 graph, component, port);
1448 goto error;
1449 }
1450 }
1451
1452 bt_graph_set_can_consume(graph, false);
1453
1454 /* Possibly remove from sinks to consume */
1455 (void) g_queue_remove(graph->sinks_to_consume, component);
1456
1457 if (graph->sinks_to_consume->length == 0) {
1458 graph->has_sink = false;
1459 }
1460
1461 /*
1462 * This calls bt_object_try_spec_release() on the component, and
1463 * since its reference count is 0, its destructor is called. Its
1464 * destructor calls the user's finalization method (if set).
1465 */
1466 g_ptr_array_remove(graph->components, component);
1467 goto end;
1468
1469 error:
1470 ret = -1;
1471
1472 end:
1473 (void) init_can_consume;
1474 bt_graph_set_can_consume(graph, init_can_consume);
1475 return ret;
1476 }
1477
1478 BT_HIDDEN
1479 void bt_graph_add_message(struct bt_graph *graph,
1480 struct bt_message *msg)
1481 {
1482 BT_ASSERT(graph);
1483 BT_ASSERT(msg);
1484
1485 /*
1486 * It's okay not to take a reference because, when a
1487 * message's reference count drops to 0, either:
1488 *
1489 * * It is recycled back to one of this graph's pool.
1490 * * It is destroyed because it doesn't have any link to any
1491 * graph, which means the original graph is already destroyed.
1492 */
1493 g_ptr_array_add(graph->messages, msg);
1494 }
1495
1496 void bt_graph_get_ref(const struct bt_graph *graph)
1497 {
1498 bt_object_get_ref(graph);
1499 }
1500
1501 void bt_graph_put_ref(const struct bt_graph *graph)
1502 {
1503 bt_object_put_ref(graph);
1504 }
This page took 0.089132 seconds and 4 git commands to generate.