2 * The MIT License (MIT)
4 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* Output argument typemap for connection output (always appends) */
26 %typemap(in, numinputs=0)
27 (const bt_connection **BTOUTCONN)
28 (bt_connection *temp_conn = NULL) {
33 (const bt_connection **BTOUTCONN) {
35 /* SWIG_Python_AppendOutput() steals the created object */
36 $result = SWIG_Python_AppendOutput($result,
37 SWIG_NewPointerObj(SWIG_as_voidptr(*$1),
38 SWIGTYPE_p_bt_connection, 0));
40 /* SWIG_Python_AppendOutput() steals Py_None */
42 $result = SWIG_Python_AppendOutput($result, Py_None);
46 /* Output argument typemap for component output (always appends) */
47 %typemap(in, numinputs=0)
48 (const bt_component_source **OUT)
49 (bt_component_source *temp_comp = NULL) {
53 %typemap(in, numinputs=0)
54 (const bt_component_filter **OUT)
55 (bt_component_filter *temp_comp = NULL) {
59 %typemap(in, numinputs=0)
60 (const bt_component_sink **OUT)
61 (bt_component_sink *temp_comp = NULL) {
65 %typemap(argout) (const bt_component_source **OUT) {
67 /* SWIG_Python_AppendOutput() steals the created object */
68 $result = SWIG_Python_AppendOutput($result,
69 SWIG_NewPointerObj(SWIG_as_voidptr(*$1),
70 SWIGTYPE_p_bt_component_source, 0));
72 /* SWIG_Python_AppendOutput() steals Py_None */
74 $result = SWIG_Python_AppendOutput($result, Py_None);
78 %typemap(argout) (const bt_component_filter **OUT) {
80 /* SWIG_Python_AppendOutput() steals the created object */
81 $result = SWIG_Python_AppendOutput($result,
82 SWIG_NewPointerObj(SWIG_as_voidptr(*$1),
83 SWIGTYPE_p_bt_component_filter, 0));
85 /* SWIG_Python_AppendOutput() steals Py_None */
87 $result = SWIG_Python_AppendOutput($result, Py_None);
91 %typemap(argout) (const bt_component_sink **OUT) {
93 /* SWIG_Python_AppendOutput() steals the created object */
94 $result = SWIG_Python_AppendOutput($result,
95 SWIG_NewPointerObj(SWIG_as_voidptr(*$1),
96 SWIGTYPE_p_bt_component_sink, 0));
98 /* SWIG_Python_AppendOutput() steals Py_None */
100 $result = SWIG_Python_AppendOutput($result, Py_None);
104 /* From graph-const.h */
106 typedef enum bt_graph_status {
107 BT_GRAPH_STATUS_OK = 0,
108 BT_GRAPH_STATUS_END = 1,
109 BT_GRAPH_STATUS_AGAIN = 11,
110 BT_GRAPH_STATUS_CANCELED = 125,
111 BT_GRAPH_STATUS_ERROR = -1,
112 BT_GRAPH_STATUS_NOMEM = -12,
115 extern bt_bool bt_graph_is_canceled(const bt_graph *graph);
117 extern void bt_graph_get_ref(const bt_graph *graph);
119 extern void bt_graph_put_ref(const bt_graph *graph);
123 typedef enum bt_graph_listener_status {
124 BT_GRAPH_LISTENER_STATUS_OK = 0,
125 BT_GRAPH_LISTENER_STATUS_ERROR = -1,
126 BT_GRAPH_LISTENER_STATUS_NOMEM = -12,
127 } bt_graph_listener_status;
130 typedef bt_graph_listener_status
131 (*bt_graph_filter_component_input_port_added_listener_func)(
132 const bt_component_filter *component,
133 const bt_port_input *port, void *data);
135 typedef bt_graph_listener_status
136 (*bt_graph_sink_component_input_port_added_listener_func)(
137 const bt_component_sink *component,
138 const bt_port_input *port, void *data);
140 typedef bt_graph_listener_status
141 (*bt_graph_source_component_output_port_added_listener_func)(
142 const bt_component_source *component,
143 const bt_port_output *port, void *data);
145 typedef bt_graph_listener_status
146 (*bt_graph_filter_component_output_port_added_listener_func)(
147 const bt_component_filter *component,
148 const bt_port_output *port, void *data);
150 typedef bt_graph_listener_status
151 (*bt_graph_source_filter_component_ports_connected_listener_func)(
152 const bt_component_source *source_component,
153 const bt_component_filter *filter_component,
154 const bt_port_output *upstream_port,
155 const bt_port_input *downstream_port, void *data);
157 typedef bt_graph_listener_status
158 (*bt_graph_source_sink_component_ports_connected_listener_func)(
159 const bt_component_source *source_component,
160 const bt_component_sink *sink_component,
161 const bt_port_output *upstream_port,
162 const bt_port_input *downstream_port, void *data);
164 typedef bt_graph_listener_status
165 (*bt_graph_filter_filter_component_ports_connected_listener_func)(
166 const bt_component_filter *filter_component_upstream,
167 const bt_component_filter *filter_component_downstream,
168 const bt_port_output *upstream_port,
169 const bt_port_input *downstream_port,
172 typedef bt_graph_listener_status
173 (*bt_graph_filter_sink_component_ports_connected_listener_func)(
174 const bt_component_filter *filter_component,
175 const bt_component_sink *sink_component,
176 const bt_port_output *upstream_port,
177 const bt_port_input *downstream_port, void *data);
179 typedef void (* bt_graph_listener_removed_func)(void *data);
181 extern bt_graph *bt_graph_create(void);
183 extern bt_graph_status bt_graph_add_source_component(bt_graph *graph,
184 const bt_component_class_source *component_class,
185 const char *name, const bt_value *params,
186 bt_logging_level log_level, const bt_component_source **OUT);
188 extern bt_graph_status bt_graph_add_source_component_with_init_method_data(
190 const bt_component_class_source *component_class,
191 const char *name, const bt_value *params,
192 void *init_method_data, bt_logging_level log_level,
193 const bt_component_source **OUT);
195 extern bt_graph_status bt_graph_add_filter_component(bt_graph *graph,
196 const bt_component_class_filter *component_class,
197 const char *name, const bt_value *params,
198 bt_logging_level log_level,
199 const bt_component_filter **OUT);
201 extern bt_graph_status bt_graph_add_filter_component_with_init_method_data(
203 const bt_component_class_filter *component_class,
204 const char *name, const bt_value *params,
205 void *init_method_data, bt_logging_level log_level,
206 const bt_component_filter **OUT);
208 extern bt_graph_status bt_graph_add_sink_component(
209 bt_graph *graph, const bt_component_class_sink *component_class,
210 const char *name, const bt_value *params,
211 bt_logging_level log_level,
212 const bt_component_sink **OUT);
214 extern bt_graph_status bt_graph_add_sink_component_with_init_method_data(
215 bt_graph *graph, const bt_component_class_sink *component_class,
216 const char *name, const bt_value *params,
217 void *init_method_data, bt_logging_level log_level,
218 const bt_component_sink **OUT);
220 extern bt_graph_status bt_graph_connect_ports(bt_graph *graph,
221 const bt_port_output *upstream,
222 const bt_port_input *downstream,
223 const bt_connection **BTOUTCONN);
225 extern bt_graph_status bt_graph_run(bt_graph *graph);
227 extern bt_graph_status bt_graph_consume(bt_graph *graph);
229 extern bt_graph_status bt_graph_add_filter_component_input_port_added_listener(
231 bt_graph_filter_component_input_port_added_listener_func listener,
232 bt_graph_listener_removed_func listener_removed, void *data,
235 extern bt_graph_status bt_graph_add_sink_component_input_port_added_listener(
237 bt_graph_sink_component_input_port_added_listener_func listener,
238 bt_graph_listener_removed_func listener_removed, void *data,
241 extern bt_graph_status bt_graph_add_source_component_output_port_added_listener(
243 bt_graph_source_component_output_port_added_listener_func listener,
244 bt_graph_listener_removed_func listener_removed, void *data,
247 extern bt_graph_status bt_graph_add_filter_component_output_port_added_listener(
249 bt_graph_filter_component_output_port_added_listener_func listener,
250 bt_graph_listener_removed_func listener_removed, void *data,
253 extern bt_graph_status
254 bt_graph_add_source_filter_component_ports_connected_listener(
256 bt_graph_source_filter_component_ports_connected_listener_func listener,
257 bt_graph_listener_removed_func listener_removed, void *data,
260 extern bt_graph_status
261 bt_graph_add_filter_filter_component_ports_connected_listener(
263 bt_graph_filter_filter_component_ports_connected_listener_func listener,
264 bt_graph_listener_removed_func listener_removed, void *data,
267 extern bt_graph_status
268 bt_graph_add_source_sink_component_ports_connected_listener(
270 bt_graph_source_sink_component_ports_connected_listener_func listener,
271 bt_graph_listener_removed_func listener_removed, void *data,
274 extern bt_graph_status
275 bt_graph_add_filter_sink_component_ports_connected_listener(
277 bt_graph_filter_sink_component_ports_connected_listener_func listener,
278 bt_graph_listener_removed_func listener_removed, void *data,
281 extern bt_graph_status bt_graph_cancel(bt_graph *graph);
283 /* Helper functions for Python */
287 static void graph_listener_removed(void *py_callable)
289 BT_ASSERT(py_callable);
290 Py_DECREF(py_callable);
293 static bt_graph_listener_status
295 const void *component,
296 swig_type_info *component_swig_type,
297 bt_component_class_type component_class_type,
299 swig_type_info *port_swig_type,
300 bt_port_type port_type,
303 PyObject *py_component_ptr = NULL;
304 PyObject *py_port_ptr = NULL;
305 PyObject *py_res = NULL;
306 bt_graph_listener_status status;
308 py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0);
309 if (!py_component_ptr) {
310 BT_LOGF_STR("Failed to create component SWIG pointer object.");
311 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
315 py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0);
317 BT_LOGF_STR("Failed to create port SWIG pointer object.");
318 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
322 py_res = PyObject_CallFunction(py_callable, "(OiOi)",
323 py_component_ptr, component_class_type, py_port_ptr, port_type);
325 bt2_py_loge_exception();
327 status = BT_GRAPH_LISTENER_STATUS_ERROR;
331 BT_ASSERT(py_res == Py_None);
332 status = BT_GRAPH_LISTENER_STATUS_OK;
336 Py_XDECREF(py_port_ptr);
337 Py_XDECREF(py_component_ptr);
342 static bt_graph_listener_status
343 source_component_output_port_added_listener(const bt_component_source *component_source,
344 const bt_port_output *port_output, void *py_callable)
346 return port_added_listener(
347 component_source, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
348 port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable);
351 static bt_graph_listener_status
352 filter_component_input_port_added_listener(const bt_component_filter *component_filter,
353 const bt_port_input *port_input, void *py_callable)
355 return port_added_listener(
356 component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
357 port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable);
360 static bt_graph_listener_status
361 filter_component_output_port_added_listener(const bt_component_filter *component_filter,
362 const bt_port_output *port_output, void *py_callable)
364 return port_added_listener(
365 component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
366 port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable);
369 static bt_graph_listener_status
370 sink_component_input_port_added_listener(const bt_component_sink *component_sink,
371 const bt_port_input *port_input, void *py_callable)
373 return port_added_listener(
374 component_sink, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
375 port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable);
379 bt_py3_graph_add_port_added_listener(struct bt_graph *graph,
380 PyObject *py_callable)
382 PyObject *py_listener_ids = NULL;
383 PyObject *py_listener_id = NULL;
385 bt_graph_status status;
388 BT_ASSERT(py_callable);
391 * Behind the scene, we will be registering 4 different listeners and
392 * return all of their ids.
394 py_listener_ids = PyTuple_New(4);
395 if (!py_listener_ids) {
399 /* source output port */
400 status = bt_graph_add_source_component_output_port_added_listener(
401 graph, source_component_output_port_added_listener,
402 graph_listener_removed, py_callable, &listener_id);
403 if (status != BT_GRAPH_STATUS_OK) {
407 py_listener_id = PyLong_FromLong(listener_id);
408 if (!py_listener_id) {
412 PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id);
413 py_listener_id = NULL;
415 /* filter input port */
416 status = bt_graph_add_filter_component_input_port_added_listener(
417 graph, filter_component_input_port_added_listener,
418 graph_listener_removed, py_callable, &listener_id);
419 if (status != BT_GRAPH_STATUS_OK) {
423 py_listener_id = PyLong_FromLong(listener_id);
424 if (!py_listener_id) {
428 PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id);
429 py_listener_id = NULL;
431 /* filter output port */
432 status = bt_graph_add_filter_component_output_port_added_listener(
433 graph, filter_component_output_port_added_listener,
434 graph_listener_removed, py_callable, &listener_id);
435 if (status != BT_GRAPH_STATUS_OK) {
439 py_listener_id = PyLong_FromLong(listener_id);
440 if (!py_listener_id) {
444 PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id);
445 py_listener_id = NULL;
447 /* sink input port */
448 status = bt_graph_add_sink_component_input_port_added_listener(
449 graph, sink_component_input_port_added_listener,
450 graph_listener_removed, py_callable, &listener_id);
451 if (status != BT_GRAPH_STATUS_OK) {
455 py_listener_id = PyLong_FromLong(listener_id);
456 if (!py_listener_id) {
461 PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id);
462 py_listener_id = NULL;
464 Py_INCREF(py_callable);
465 Py_INCREF(py_callable);
466 Py_INCREF(py_callable);
467 Py_INCREF(py_callable);
472 Py_XDECREF(py_listener_ids);
473 py_listener_ids = Py_None;
474 Py_INCREF(py_listener_ids);
478 Py_XDECREF(py_listener_id);
479 return py_listener_ids;
482 static bt_graph_listener_status
483 ports_connected_listener(
484 const void *upstream_component,
485 swig_type_info *upstream_component_swig_type,
486 bt_component_class_type upstream_component_class_type,
487 const bt_port_output *upstream_port,
488 const void *downstream_component,
489 swig_type_info *downstream_component_swig_type,
490 bt_component_class_type downstream_component_class_type,
491 const bt_port_input *downstream_port,
494 PyObject *py_upstream_component_ptr = NULL;
495 PyObject *py_upstream_port_ptr = NULL;
496 PyObject *py_downstream_component_ptr = NULL;
497 PyObject *py_downstream_port_ptr = NULL;
498 PyObject *py_res = NULL;
499 bt_graph_listener_status status;
501 py_upstream_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(upstream_component),
502 upstream_component_swig_type, 0);
503 if (!py_upstream_component_ptr) {
504 BT_LOGF_STR("Failed to create upstream component SWIG pointer object.");
505 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
509 py_upstream_port_ptr = SWIG_NewPointerObj(
510 SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port_output, 0);
511 if (!py_upstream_port_ptr) {
512 BT_LOGF_STR("Failed to create upstream port SWIG pointer object.");
513 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
517 py_downstream_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(downstream_component),
518 downstream_component_swig_type, 0);
519 if (!py_downstream_component_ptr) {
520 BT_LOGF_STR("Failed to create downstream component SWIG pointer object.");
521 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
525 py_downstream_port_ptr = SWIG_NewPointerObj(
526 SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port_input, 0);
527 if (!py_downstream_port_ptr) {
528 BT_LOGF_STR("Failed to create downstream port SWIG pointer object.");
529 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
533 py_res = PyObject_CallFunction(py_callable, "(OiOOiO)",
534 py_upstream_component_ptr, upstream_component_class_type,
535 py_upstream_port_ptr,
536 py_downstream_component_ptr, downstream_component_class_type,
537 py_downstream_port_ptr);
539 bt2_py_loge_exception();
541 status = BT_GRAPH_LISTENER_STATUS_ERROR;
545 BT_ASSERT(py_res == Py_None);
546 status = BT_GRAPH_LISTENER_STATUS_OK;
549 Py_XDECREF(py_upstream_component_ptr);
550 Py_XDECREF(py_upstream_port_ptr);
551 Py_XDECREF(py_downstream_component_ptr);
552 Py_XDECREF(py_downstream_port_ptr);
558 static bt_graph_listener_status
559 source_filter_component_ports_connected_listener(
560 const bt_component_source *source_component,
561 const bt_component_filter *filter_component,
562 const bt_port_output *upstream_port,
563 const bt_port_input *downstream_port, void *py_callable)
565 return ports_connected_listener(
566 source_component, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
568 filter_component, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
573 static bt_graph_listener_status
574 source_sink_component_ports_connected_listener(
575 const bt_component_source *source_component,
576 const bt_component_sink *sink_component,
577 const bt_port_output *upstream_port,
578 const bt_port_input *downstream_port, void *py_callable)
580 return ports_connected_listener(
581 source_component, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
583 sink_component, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
588 static bt_graph_listener_status
589 filter_filter_component_ports_connected_listener(
590 const bt_component_filter *filter_component_left,
591 const bt_component_filter *filter_component_right,
592 const bt_port_output *upstream_port,
593 const bt_port_input *downstream_port, void *py_callable)
595 return ports_connected_listener(
596 filter_component_left, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
598 filter_component_right, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
603 static bt_graph_listener_status
604 filter_sink_component_ports_connected_listener(
605 const bt_component_filter *filter_component,
606 const bt_component_sink *sink_component,
607 const bt_port_output *upstream_port,
608 const bt_port_input *downstream_port, void *py_callable)
610 return ports_connected_listener(
611 filter_component, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
613 sink_component, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
619 bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph,
620 PyObject *py_callable)
622 PyObject *py_listener_ids = NULL;
623 PyObject *py_listener_id = NULL;
625 bt_graph_status status;
628 BT_ASSERT(py_callable);
630 /* Behind the scene, we will be registering 4 different listeners and
631 * return all of their ids. */
632 py_listener_ids = PyTuple_New(4);
633 if (!py_listener_ids) {
637 /* source -> filter connection */
638 status = bt_graph_add_source_filter_component_ports_connected_listener(
639 graph, source_filter_component_ports_connected_listener,
640 graph_listener_removed, py_callable, &listener_id);
641 if (status != BT_GRAPH_STATUS_OK) {
645 py_listener_id = PyLong_FromLong(listener_id);
646 if (!py_listener_id) {
650 PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id);
651 py_listener_id = NULL;
653 /* source -> sink connection */
654 status = bt_graph_add_source_sink_component_ports_connected_listener(
655 graph, source_sink_component_ports_connected_listener,
656 graph_listener_removed, py_callable, &listener_id);
657 if (status != BT_GRAPH_STATUS_OK) {
661 py_listener_id = PyLong_FromLong(listener_id);
662 if (!py_listener_id) {
666 PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id);
667 py_listener_id = NULL;
669 /* filter -> filter connection */
670 status = bt_graph_add_filter_filter_component_ports_connected_listener(
671 graph, filter_filter_component_ports_connected_listener,
672 graph_listener_removed, py_callable, &listener_id);
673 if (status != BT_GRAPH_STATUS_OK) {
677 py_listener_id = PyLong_FromLong(listener_id);
678 if (!py_listener_id) {
682 PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id);
683 py_listener_id = NULL;
685 /* filter -> sink connection */
686 status = bt_graph_add_filter_sink_component_ports_connected_listener(
687 graph, filter_sink_component_ports_connected_listener,
688 graph_listener_removed, py_callable, &listener_id);
689 if (status != BT_GRAPH_STATUS_OK) {
693 py_listener_id = PyLong_FromLong(listener_id);
694 if (!py_listener_id) {
698 PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id);
699 py_listener_id = NULL;
701 Py_INCREF(py_callable);
702 Py_INCREF(py_callable);
703 Py_INCREF(py_callable);
704 Py_INCREF(py_callable);
709 Py_XDECREF(py_listener_ids);
710 py_listener_ids = Py_None;
711 Py_INCREF(py_listener_ids);
715 Py_XDECREF(py_listener_id);
716 return py_listener_ids;
721 PyObject *bt_py3_graph_add_port_added_listener(struct bt_graph *graph,
722 PyObject *py_callable);
723 PyObject *bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph,
724 PyObject *py_callable);