Add bt_graph_add_component(), make bt_component_create() internal
[babeltrace.git] / lib / graph / graph.c
1 /*
2 * graph.c
3 *
4 * Babeltrace Plugin Component Graph
5 *
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #define BT_LOG_TAG "GRAPH"
29 #include <babeltrace/lib-logging-internal.h>
30
31 #include <babeltrace/graph/component-internal.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.h>
36 #include <babeltrace/graph/component-filter.h>
37 #include <babeltrace/graph/port.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/types.h>
40 #include <babeltrace/values.h>
41 #include <babeltrace/values-internal.h>
42 #include <unistd.h>
43 #include <glib.h>
44
45 struct bt_graph_listener {
46 void *func;
47 void *data;
48 };
49
50 static
51 void bt_graph_destroy(struct bt_object *obj)
52 {
53 struct bt_graph *graph = container_of(obj,
54 struct bt_graph, base);
55
56 /*
57 * The graph's reference count is 0 if we're here. Increment
58 * it to avoid a double-destroy (possibly infinitely recursive)
59 * in this situation:
60 *
61 * 1. We put and destroy a connection.
62 * 2. This connection's destructor finalizes its active
63 * notification iterators.
64 * 3. A notification iterator's finalization function gets a
65 * new reference on its component (reference count goes from
66 * 0 to 1).
67 * 4. Since this component's reference count goes to 1, it takes
68 * a reference on its parent (this graph). This graph's
69 * reference count goes from 0 to 1.
70 * 5. The notification iterator's finalization function puts its
71 * component reference (reference count goes from 1 to 0).
72 * 6. Since this component's reference count goes from 1 to 0,
73 * it puts its parent (this graph). This graph's reference
74 * count goes from 1 to 0.
75 * 7. Since this graph's reference count goes from 1 to 0,
76 * its destructor is called (this function).
77 *
78 * With the incrementation below, the graph's reference count at
79 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
80 * ensures that this function is not called two times.
81 */
82 BT_LOGD("Destroying graph: addr=%p", graph);
83 obj->ref_count.count++;
84
85 /*
86 * Cancel the graph to disallow some operations, like creating
87 * notification iterators and adding ports to components.
88 */
89 (void) bt_graph_cancel(graph);
90
91 if (graph->connections) {
92 BT_LOGD_STR("Destroying connections.");
93 g_ptr_array_free(graph->connections, TRUE);
94 }
95 if (graph->components) {
96 BT_LOGD_STR("Destroying components.");
97 g_ptr_array_free(graph->components, TRUE);
98 }
99 if (graph->sinks_to_consume) {
100 g_queue_free(graph->sinks_to_consume);
101 }
102
103 if (graph->listeners.port_added) {
104 g_array_free(graph->listeners.port_added, TRUE);
105 }
106
107 if (graph->listeners.port_removed) {
108 g_array_free(graph->listeners.port_removed, TRUE);
109 }
110
111 if (graph->listeners.ports_connected) {
112 g_array_free(graph->listeners.ports_connected, TRUE);
113 }
114
115 if (graph->listeners.ports_disconnected) {
116 g_array_free(graph->listeners.ports_disconnected, TRUE);
117 }
118
119 g_free(graph);
120 }
121
122 static
123 int init_listeners_array(GArray **listeners)
124 {
125 int ret = 0;
126
127 assert(listeners);
128 *listeners = g_array_new(FALSE, TRUE, sizeof(struct bt_graph_listener));
129 if (!*listeners) {
130 BT_LOGE_STR("Failed to allocate one GArray.");
131 ret = -1;
132 goto end;
133 }
134
135 end:
136 return ret;
137 }
138
139 struct bt_graph *bt_graph_create(void)
140 {
141 struct bt_graph *graph;
142 int ret;
143
144 BT_LOGD_STR("Creating graph object.");
145 graph = g_new0(struct bt_graph, 1);
146 if (!graph) {
147 BT_LOGE_STR("Failed to allocate one graph.");
148 goto end;
149 }
150
151 bt_object_init(graph, bt_graph_destroy);
152
153 graph->connections = g_ptr_array_new_with_free_func(bt_object_release);
154 if (!graph->connections) {
155 BT_LOGE_STR("Failed to allocate one GPtrArray.");
156 goto error;
157 }
158 graph->components = g_ptr_array_new_with_free_func(bt_object_release);
159 if (!graph->components) {
160 BT_LOGE_STR("Failed to allocate one GPtrArray.");
161 goto error;
162 }
163 graph->sinks_to_consume = g_queue_new();
164 if (!graph->sinks_to_consume) {
165 BT_LOGE_STR("Failed to allocate one GQueue.");
166 goto error;
167 }
168
169 ret = init_listeners_array(&graph->listeners.port_added);
170 if (ret) {
171 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
172 goto error;
173 }
174
175 ret = init_listeners_array(&graph->listeners.port_removed);
176 if (ret) {
177 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
178 goto error;
179 }
180
181 ret = init_listeners_array(&graph->listeners.ports_connected);
182 if (ret) {
183 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
184 goto error;
185 }
186
187 ret = init_listeners_array(&graph->listeners.ports_disconnected);
188 if (ret) {
189 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
190 goto error;
191 }
192
193 BT_LOGD("Created graph object: addr=%p", graph);
194
195 end:
196 return graph;
197 error:
198 BT_PUT(graph);
199 goto end;
200 }
201
202 enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph,
203 struct bt_port *upstream_port, struct bt_port *downstream_port,
204 struct bt_connection **user_connection)
205 {
206 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
207 struct bt_connection *connection = NULL;
208 struct bt_graph *upstream_graph = NULL;
209 struct bt_graph *downstream_graph = NULL;
210 struct bt_component *upstream_component = NULL;
211 struct bt_component *downstream_component = NULL;
212 enum bt_component_status component_status;
213
214 if (!graph) {
215 BT_LOGW_STR("Invalid parameter: graph is NULL.");
216 status = BT_GRAPH_STATUS_INVALID;
217 goto end;
218 }
219
220 if (!upstream_port) {
221 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
222 status = BT_GRAPH_STATUS_INVALID;
223 goto end;
224 }
225
226 if (!downstream_port) {
227 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
228 status = BT_GRAPH_STATUS_INVALID;
229 goto end;
230 }
231
232 BT_LOGD("Connecting component ports within graph: "
233 "graph-addr=%p, "
234 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
235 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
236 graph, upstream_port, bt_port_get_name(upstream_port),
237 downstream_port, bt_port_get_name(downstream_port));
238
239 if (graph->canceled) {
240 BT_LOGW_STR("Invalid parameter: graph is canceled.");
241 status = BT_GRAPH_STATUS_CANCELED;
242 goto end;
243 }
244
245 /* Ensure appropriate types for upstream and downstream ports. */
246 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
247 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
248 status = BT_GRAPH_STATUS_INVALID;
249 goto end;
250 }
251 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
252 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
253 status = BT_GRAPH_STATUS_INVALID;
254 goto end;
255 }
256
257 /* Ensure that both ports are currently unconnected. */
258 if (bt_port_is_connected(upstream_port)) {
259 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
260 status = BT_GRAPH_STATUS_INVALID;
261 goto end;
262 }
263
264 if (bt_port_is_connected(downstream_port)) {
265 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
266 status = BT_GRAPH_STATUS_INVALID;
267 goto end;
268 }
269
270 /*
271 * Ensure that both ports are still attached to their creating
272 * component.
273 */
274 upstream_component = bt_port_get_component(upstream_port);
275 if (!upstream_component) {
276 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
277 status = BT_GRAPH_STATUS_INVALID;
278 goto end;
279 }
280
281 downstream_component = bt_port_get_component(downstream_port);
282 if (!downstream_component) {
283 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
284 status = BT_GRAPH_STATUS_INVALID;
285 goto end;
286 }
287
288 BT_LOGD("Connecting component ports: "
289 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
290 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
291 upstream_component, bt_component_get_name(upstream_component),
292 downstream_component, bt_component_get_name(downstream_component));
293
294 /*
295 * At this point the ports are not connected yet. Both
296 * components need to accept an eventual connection to their
297 * port by the other port before we continue.
298 */
299 BT_LOGD_STR("Asking upstream component to accept the connection.");
300 component_status = bt_component_accept_port_connection(
301 upstream_component, upstream_port, downstream_port);
302 if (component_status != BT_COMPONENT_STATUS_OK) {
303 if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
304 BT_LOGD_STR("Upstream component refused the connection.");
305 } else {
306 BT_LOGW("Cannot ask upstream component to accept the connection: "
307 "status=%s", bt_component_status_string(component_status));
308 }
309
310 status = bt_graph_status_from_component_status(
311 component_status);
312 goto end;
313 }
314
315 BT_LOGD_STR("Asking downstream component to accept the connection.");
316 component_status = bt_component_accept_port_connection(
317 downstream_component, downstream_port, upstream_port);
318 if (component_status != BT_COMPONENT_STATUS_OK) {
319 if (component_status == BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION) {
320 BT_LOGD_STR("Downstream component refused the connection.");
321 } else {
322 BT_LOGW("Cannot ask downstream component to accept the connection: "
323 "status=%s", bt_component_status_string(component_status));
324 }
325
326 status = bt_graph_status_from_component_status(
327 component_status);
328 goto end;
329 }
330
331 BT_LOGD_STR("Creating connection.");
332 connection = bt_connection_create(graph, upstream_port,
333 downstream_port);
334 if (!connection) {
335 BT_LOGW("Cannot create connection object.");
336 status = BT_GRAPH_STATUS_NOMEM;
337 goto end;
338 }
339
340 BT_LOGD("Connection object created: conn-addr=%p", connection);
341
342 /*
343 * Ownership of upstream_component/downstream_component and of
344 * the connection object is transferred to the graph.
345 */
346 g_ptr_array_add(graph->connections, connection);
347
348 /*
349 * Notify both components that their port is connected.
350 */
351 BT_LOGD_STR("Notifying upstream component that its port is connected.");
352 bt_component_port_connected(upstream_component, upstream_port,
353 downstream_port);
354 BT_LOGD_STR("Notifying downstream component that its port is connected.");
355 bt_component_port_connected(downstream_component, downstream_port,
356 upstream_port);
357
358 /*
359 * Notify the graph's creator that both ports are connected.
360 */
361 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
362 bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
363 BT_LOGD("Connected component ports within graph: "
364 "graph-addr=%p, "
365 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
366 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
367 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
368 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
369 graph,
370 upstream_component, bt_component_get_name(upstream_component),
371 downstream_component, bt_component_get_name(downstream_component),
372 upstream_port, bt_port_get_name(upstream_port),
373 downstream_port, bt_port_get_name(downstream_port));
374
375 if (user_connection) {
376 /* Move reference to user */
377 *user_connection = connection;
378 connection = NULL;
379 }
380
381 end:
382 bt_put(upstream_graph);
383 bt_put(downstream_graph);
384 bt_put(upstream_component);
385 bt_put(downstream_component);
386 bt_put(connection);
387 return status;
388 }
389
390 static
391 enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
392 {
393 struct bt_component *sink;
394 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
395 enum bt_component_status comp_status;
396 GList *current_node;
397
398 BT_LOGV("Making next sink consume: addr=%p", graph);
399
400 if (g_queue_is_empty(graph->sinks_to_consume)) {
401 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
402 status = BT_GRAPH_STATUS_END;
403 goto end;
404 }
405
406 current_node = g_queue_pop_head_link(graph->sinks_to_consume);
407 sink = current_node->data;
408 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
409 sink, bt_component_get_name(sink));
410 comp_status = bt_component_sink_consume(sink);
411 BT_LOGV("Consumed from sink: status=%s",
412 bt_component_status_string(comp_status));
413 switch (comp_status) {
414 case BT_COMPONENT_STATUS_OK:
415 break;
416 case BT_COMPONENT_STATUS_END:
417 status = BT_GRAPH_STATUS_END;
418 break;
419 case BT_COMPONENT_STATUS_AGAIN:
420 status = BT_GRAPH_STATUS_AGAIN;
421 break;
422 case BT_COMPONENT_STATUS_INVALID:
423 status = BT_GRAPH_STATUS_INVALID;
424 break;
425 default:
426 status = BT_GRAPH_STATUS_ERROR;
427 break;
428 }
429
430 if (status != BT_GRAPH_STATUS_END) {
431 g_queue_push_tail_link(graph->sinks_to_consume, current_node);
432 goto end;
433 }
434
435 /* End reached, the node is not added back to the queue and free'd. */
436 g_queue_delete_link(graph->sinks_to_consume, current_node);
437
438 /* Don't forward an END status if there are sinks left to consume. */
439 if (!g_queue_is_empty(graph->sinks_to_consume)) {
440 status = BT_GRAPH_STATUS_OK;
441 goto end;
442 }
443 end:
444 BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status));
445 return status;
446 }
447
448 enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
449 {
450 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
451
452 if (!graph) {
453 BT_LOGW_STR("Invalid parameter: graph is NULL.");
454 status = BT_GRAPH_STATUS_INVALID;
455 goto end;
456 }
457
458 if (graph->canceled) {
459 BT_LOGW("Invalid parameter: graph is canceled: "
460 "graph-addr=%p", graph);
461 status = BT_GRAPH_STATUS_CANCELED;
462 goto end;
463 }
464
465 status = bt_graph_consume_no_check(graph);
466
467 end:
468 return status;
469 }
470
471 enum bt_graph_status bt_graph_run(struct bt_graph *graph)
472 {
473 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
474
475 if (!graph) {
476 BT_LOGW_STR("Invalid parameter: graph is NULL.");
477 status = BT_GRAPH_STATUS_INVALID;
478 goto end;
479 }
480
481 if (graph->canceled) {
482 BT_LOGW("Invalid parameter: graph is canceled: "
483 "graph-addr=%p", graph);
484 status = BT_GRAPH_STATUS_CANCELED;
485 goto end;
486 }
487
488 BT_LOGV("Running graph: addr=%p", graph);
489
490 do {
491 /*
492 * Check if the graph is canceled at each iteration. If
493 * the graph was canceled by another thread or by a
494 * signal, this is not a warning nor an error, it was
495 * intentional: log with a DEBUG level only.
496 */
497 if (graph->canceled) {
498 BT_LOGD("Stopping the graph: graph is canceled: "
499 "graph-addr=%p", graph);
500 status = BT_GRAPH_STATUS_CANCELED;
501 goto end;
502 }
503
504 status = bt_graph_consume(graph);
505 if (status == BT_GRAPH_STATUS_AGAIN) {
506 /*
507 * If AGAIN is received and there are multiple
508 * sinks, go ahead and consume from the next
509 * sink.
510 *
511 * However, in the case where a single sink is
512 * left, the caller can decide to busy-wait and
513 * call bt_graph_run() continuously until the
514 * source is ready or it can decide to sleep for
515 * an arbitrary amount of time.
516 */
517 if (graph->sinks_to_consume->length > 1) {
518 status = BT_GRAPH_STATUS_OK;
519 }
520 }
521 } while (status == BT_GRAPH_STATUS_OK);
522
523 if (g_queue_is_empty(graph->sinks_to_consume)) {
524 status = BT_GRAPH_STATUS_END;
525 }
526
527 end:
528 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status));
529 return status;
530 }
531
532 static
533 int add_listener(GArray *listeners, void *func, void *data)
534 {
535 struct bt_graph_listener listener = {
536 .func = func,
537 .data = data,
538 };
539
540 g_array_append_val(listeners, listener);
541 return listeners->len - 1;
542 }
543
544 int bt_graph_add_port_added_listener(
545 struct bt_graph *graph,
546 bt_graph_port_added_listener listener, void *data)
547 {
548 int ret;
549
550 if (!graph) {
551 BT_LOGW_STR("Invalid parameter: graph is NULL.");
552 ret = -1;
553 goto end;
554 }
555
556 if (!listener) {
557 BT_LOGW_STR("Invalid parameter: listener is NULL.");
558 ret = -1;
559 goto end;
560 }
561
562 ret = add_listener(graph->listeners.port_added, listener, data);
563 BT_LOGV("Added \"port added\" listener to graph: "
564 "graph-addr=%p, listener-addr=%p, pos=%d",
565 graph, listener, ret);
566
567 end:
568 return ret;
569 }
570
571 int bt_graph_add_port_removed_listener(
572 struct bt_graph *graph,
573 bt_graph_port_removed_listener listener, void *data)
574 {
575 int ret;
576
577 if (!graph) {
578 BT_LOGW_STR("Invalid parameter: graph is NULL.");
579 ret = -1;
580 goto end;
581 }
582
583 if (!listener) {
584 BT_LOGW_STR("Invalid parameter: listener is NULL.");
585 ret = -1;
586 goto end;
587 }
588
589 ret = add_listener(graph->listeners.port_removed, listener, data);
590 BT_LOGV("Added \"port removed\" listener to graph: "
591 "graph-addr=%p, listener-addr=%p, pos=%d",
592 graph, listener, ret);
593
594 end:
595 return ret;
596 }
597
598 int bt_graph_add_ports_connected_listener(
599 struct bt_graph *graph,
600 bt_graph_ports_connected_listener listener, void *data)
601 {
602 int ret;
603
604 if (!graph) {
605 BT_LOGW_STR("Invalid parameter: graph is NULL.");
606 ret = -1;
607 goto end;
608 }
609
610 if (!listener) {
611 BT_LOGW_STR("Invalid parameter: listener is NULL.");
612 ret = -1;
613 goto end;
614 }
615
616 ret = add_listener(graph->listeners.ports_connected, listener, data);
617 BT_LOGV("Added \"port connected\" listener to graph: "
618 "graph-addr=%p, listener-addr=%p, pos=%d",
619 graph, listener, ret);
620
621 end:
622 return ret;
623 }
624
625 int bt_graph_add_ports_disconnected_listener(
626 struct bt_graph *graph,
627 bt_graph_ports_disconnected_listener listener, void *data)
628 {
629 int ret;
630
631 if (!graph) {
632 BT_LOGW_STR("Invalid parameter: graph is NULL.");
633 ret = -1;
634 goto end;
635 }
636
637 if (!listener) {
638 BT_LOGW_STR("Invalid parameter: listener is NULL.");
639 ret = -1;
640 goto end;
641 }
642
643 ret = add_listener(graph->listeners.ports_disconnected, listener, data);
644 BT_LOGV("Added \"port disconnected\" listener to graph: "
645 "graph-addr=%p, listener-addr=%p, pos=%d",
646 graph, listener, ret);
647
648 end:
649 return ret;
650 }
651
652 BT_HIDDEN
653 void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port)
654 {
655 size_t i;
656
657 BT_LOGV("Notifying graph listeners that a port was added: "
658 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
659 graph, port, bt_port_get_name(port));
660
661 for (i = 0; i < graph->listeners.port_added->len; i++) {
662 struct bt_graph_listener listener =
663 g_array_index(graph->listeners.port_added,
664 struct bt_graph_listener, i);
665 bt_graph_port_added_listener func = listener.func;
666
667 assert(func);
668 func(port, listener.data);
669 }
670 }
671
672 BT_HIDDEN
673 void bt_graph_notify_port_removed(struct bt_graph *graph,
674 struct bt_component *comp, struct bt_port *port)
675 {
676 size_t i;
677
678 BT_LOGV("Notifying graph listeners that a port was removed: "
679 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
680 graph, port, bt_port_get_name(port));
681
682 for (i = 0; i < graph->listeners.port_removed->len; i++) {
683 struct bt_graph_listener listener =
684 g_array_index(graph->listeners.port_removed,
685 struct bt_graph_listener, i);
686 bt_graph_port_removed_listener func = listener.func;
687
688 assert(func);
689 func(comp, port, listener.data);
690 }
691 }
692
693 BT_HIDDEN
694 void bt_graph_notify_ports_connected(struct bt_graph *graph,
695 struct bt_port *upstream_port, struct bt_port *downstream_port)
696 {
697 size_t i;
698
699 BT_LOGV("Notifying graph listeners that two ports were connected: "
700 "graph-addr=%p, "
701 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
702 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
703 graph, upstream_port, bt_port_get_name(upstream_port),
704 downstream_port, bt_port_get_name(downstream_port));
705
706 for (i = 0; i < graph->listeners.ports_connected->len; i++) {
707 struct bt_graph_listener listener =
708 g_array_index(graph->listeners.ports_connected,
709 struct bt_graph_listener, i);
710 bt_graph_ports_connected_listener func = listener.func;
711
712 assert(func);
713 func(upstream_port, downstream_port, listener.data);
714 }
715 }
716
717 BT_HIDDEN
718 void bt_graph_notify_ports_disconnected(struct bt_graph *graph,
719 struct bt_component *upstream_comp,
720 struct bt_component *downstream_comp,
721 struct bt_port *upstream_port, struct bt_port *downstream_port)
722 {
723 size_t i;
724
725 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
726 "graph-addr=%p, "
727 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
728 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
729 graph, upstream_port, bt_port_get_name(upstream_port),
730 downstream_port, bt_port_get_name(downstream_port));
731
732 for (i = 0; i < graph->listeners.ports_disconnected->len; i++) {
733 struct bt_graph_listener listener =
734 g_array_index(graph->listeners.ports_disconnected,
735 struct bt_graph_listener, i);
736 bt_graph_ports_disconnected_listener func = listener.func;
737
738 assert(func);
739 func(upstream_comp, downstream_comp, upstream_port,
740 downstream_port, listener.data);
741 }
742 }
743
744 extern enum bt_graph_status bt_graph_cancel(struct bt_graph *graph)
745 {
746 enum bt_graph_status ret = BT_GRAPH_STATUS_OK;
747
748 if (!graph) {
749 BT_LOGW_STR("Invalid parameter: graph is NULL.");
750 ret = BT_GRAPH_STATUS_INVALID;
751 goto end;
752 }
753
754 graph->canceled = BT_TRUE;
755 BT_LOGV("Canceled graph: addr=%p", graph);
756
757 end:
758 return ret;
759 }
760
761 extern bt_bool bt_graph_is_canceled(struct bt_graph *graph)
762 {
763 return graph ? graph->canceled : BT_FALSE;
764 }
765
766 BT_HIDDEN
767 void bt_graph_remove_connection(struct bt_graph *graph,
768 struct bt_connection *connection)
769 {
770 assert(graph);
771 assert(connection);
772 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
773 graph, connection);
774 g_ptr_array_remove(graph->connections, connection);
775 }
776
777 enum bt_graph_status bt_graph_add_component_with_init_method_data(
778 struct bt_graph *graph,
779 struct bt_component_class *component_class,
780 const char *name, struct bt_value *params,
781 void *init_method_data,
782 struct bt_component **user_component)
783 {
784 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
785 enum bt_component_status comp_status;
786 struct bt_component *component = NULL;
787 enum bt_component_class_type type;
788 size_t i;
789
790 bt_get(params);
791
792 if (!graph) {
793 BT_LOGW_STR("Invalid parameter: graph is NULL.");
794 graph_status = BT_GRAPH_STATUS_INVALID;
795 goto end;
796 }
797
798 if (!component_class) {
799 BT_LOGW_STR("Invalid parameter: component class is NULL.");
800 graph_status = BT_GRAPH_STATUS_INVALID;
801 goto end;
802 }
803
804 type = bt_component_class_get_type(component_class);
805 BT_LOGD("Adding component to graph: "
806 "graph-addr=%p, comp-cls-addr=%p, "
807 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
808 "init-method-data-addr=%p",
809 graph, component_class, bt_component_class_type_string(type),
810 name, params, init_method_data);
811
812 if (!name) {
813 BT_LOGW_STR("Invalid parameter: name is NULL.");
814 graph_status = BT_GRAPH_STATUS_INVALID;
815 goto end;
816 }
817
818 if (graph->canceled) {
819 BT_LOGW_STR("Invalid parameter: graph is canceled.");
820 graph_status = BT_GRAPH_STATUS_CANCELED;
821 goto end;
822 }
823
824 if (type != BT_COMPONENT_CLASS_TYPE_SOURCE &&
825 type != BT_COMPONENT_CLASS_TYPE_FILTER &&
826 type != BT_COMPONENT_CLASS_TYPE_SINK) {
827 BT_LOGW("Invalid parameter: unknown component class type: "
828 "type=%d", type);
829 graph_status = BT_GRAPH_STATUS_INVALID;
830 goto end;
831 }
832
833 for (i = 0; i < graph->components->len; i++) {
834 void *other_comp = graph->components->pdata[i];
835
836 if (strcmp(name, bt_component_get_name(other_comp)) == 0) {
837 BT_LOGW("Invalid parameter: another component with the same name already exists in the graph: "
838 "other-comp-addr=%p, name=\"%s\"",
839 other_comp, name);
840 graph_status = BT_GRAPH_STATUS_INVALID;
841 goto end;
842 }
843 }
844
845 /*
846 * Parameters must be a map value, but we create a convenient
847 * empty one if it's NULL.
848 */
849 if (params) {
850 if (!bt_value_is_map(params)) {
851 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
852 "type=%s",
853 bt_value_type_string(bt_value_get_type(params)));
854 graph_status = BT_GRAPH_STATUS_INVALID;
855 goto end;
856 }
857 } else {
858 params = bt_value_map_create();
859 if (!params) {
860 BT_LOGE_STR("Cannot create map value object.");
861 graph_status = BT_GRAPH_STATUS_NOMEM;
862 goto end;
863 }
864 }
865
866 comp_status = bt_component_create(component_class, name, &component);
867 if (comp_status != BT_COMPONENT_STATUS_OK) {
868 BT_LOGE("Cannot create empty component object: status=%s",
869 bt_component_status_string(comp_status));
870 graph_status = bt_graph_status_from_component_status(
871 comp_status);
872 goto end;
873 }
874
875 /*
876 * The user's initialization method needs to see that this
877 * component is part of the graph. If the user method fails, we
878 * immediately remove the component from the graph's components.
879 */
880 g_ptr_array_add(graph->components, component);
881 bt_component_set_graph(component, graph);
882
883 if (component_class->methods.init) {
884 BT_LOGD_STR("Calling user's initialization method.");
885 comp_status = component_class->methods.init(
886 bt_private_component_from_component(component), params,
887 init_method_data);
888 BT_LOGD("User method returned: status=%s",
889 bt_component_status_string(comp_status));
890 if (comp_status != BT_COMPONENT_STATUS_OK) {
891 BT_LOGW_STR("Initialization method failed.");
892 graph_status = bt_graph_status_from_component_status(
893 comp_status);
894 bt_component_set_graph(component, NULL);
895 g_ptr_array_remove_fast(graph->components, component);
896 goto end;
897 }
898 }
899
900 /*
901 * Mark the component as initialized so that its finalization
902 * method is called when it is destroyed.
903 */
904 component->initialized = true;
905
906 /*
907 * If it's a sink component, it needs to be part of the graph's
908 * sink queue to be consumed by bt_graph_consume().
909 */
910 if (bt_component_is_sink(component)) {
911 g_queue_push_tail(graph->sinks_to_consume, component);
912 }
913
914 /*
915 * Freeze the component class now that it's instantiated at
916 * least once.
917 */
918 BT_LOGD_STR("Freezing component class.");
919 bt_component_class_freeze(component->class);
920 BT_LOGD("Added component to graph: "
921 "graph-addr=%p, comp-cls-addr=%p, "
922 "comp-cls-type=%s, name=\"%s\", params-addr=%p, "
923 "init-method-data-addr=%p, comp-addr=%p",
924 graph, component_class, bt_component_class_type_string(type),
925 name, params, init_method_data, component);
926
927 if (user_component) {
928 /* Move reference to user */
929 *user_component = component;
930 component = NULL;
931 }
932
933 end:
934 bt_put(component);
935 bt_put(params);
936 return graph_status;
937 }
938
939 enum bt_graph_status bt_graph_add_component(
940 struct bt_graph *graph,
941 struct bt_component_class *component_class,
942 const char *name, struct bt_value *params,
943 struct bt_component **component)
944 {
945 return bt_graph_add_component_with_init_method_data(graph,
946 component_class, name, params, NULL, component);
947 }
This page took 0.048976 seconds and 4 git commands to generate.