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_COMPONENT_REFUSES_PORT_CONNECTION = 111,
111 BT_GRAPH_STATUS_CANCELED = 125,
112 BT_GRAPH_STATUS_ERROR = -1,
113 BT_GRAPH_STATUS_NOMEM = -12,
116 extern bt_bool bt_graph_is_canceled(const bt_graph *graph);
118 extern void bt_graph_get_ref(const bt_graph *graph);
120 extern void bt_graph_put_ref(const bt_graph *graph);
124 typedef enum bt_graph_listener_status {
125 BT_GRAPH_LISTENER_STATUS_OK = 0,
126 BT_GRAPH_LISTENER_STATUS_ERROR = -1,
127 BT_GRAPH_LISTENER_STATUS_NOMEM = -12,
128 } bt_graph_listener_status;
131 typedef bt_graph_listener_status
132 (*bt_graph_filter_component_input_port_added_listener_func)(
133 const bt_component_filter *component,
134 const bt_port_input *port, void *data);
136 typedef bt_graph_listener_status
137 (*bt_graph_sink_component_input_port_added_listener_func)(
138 const bt_component_sink *component,
139 const bt_port_input *port, void *data);
141 typedef bt_graph_listener_status
142 (*bt_graph_source_component_output_port_added_listener_func)(
143 const bt_component_source *component,
144 const bt_port_output *port, void *data);
146 typedef bt_graph_listener_status
147 (*bt_graph_filter_component_output_port_added_listener_func)(
148 const bt_component_filter *component,
149 const bt_port_output *port, void *data);
151 typedef bt_graph_listener_status
152 (*bt_graph_source_filter_component_ports_connected_listener_func)(
153 const bt_component_source *source_component,
154 const bt_component_filter *filter_component,
155 const bt_port_output *upstream_port,
156 const bt_port_input *downstream_port, void *data);
158 typedef bt_graph_listener_status
159 (*bt_graph_source_sink_component_ports_connected_listener_func)(
160 const bt_component_source *source_component,
161 const bt_component_sink *sink_component,
162 const bt_port_output *upstream_port,
163 const bt_port_input *downstream_port, void *data);
165 typedef bt_graph_listener_status
166 (*bt_graph_filter_filter_component_ports_connected_listener_func)(
167 const bt_component_filter *filter_component_upstream,
168 const bt_component_filter *filter_component_downstream,
169 const bt_port_output *upstream_port,
170 const bt_port_input *downstream_port,
173 typedef bt_graph_listener_status
174 (*bt_graph_filter_sink_component_ports_connected_listener_func)(
175 const bt_component_filter *filter_component,
176 const bt_component_sink *sink_component,
177 const bt_port_output *upstream_port,
178 const bt_port_input *downstream_port, void *data);
180 typedef void (* bt_graph_listener_removed_func)(void *data);
182 extern bt_graph *bt_graph_create(void);
184 extern bt_graph_status bt_graph_add_source_component(bt_graph *graph,
185 const bt_component_class_source *component_class,
186 const char *name, const bt_value *params,
187 bt_logging_level log_level, const bt_component_source **OUT);
189 extern bt_graph_status bt_graph_add_source_component_with_init_method_data(
191 const bt_component_class_source *component_class,
192 const char *name, const bt_value *params,
193 void *init_method_data, bt_logging_level log_level,
194 const bt_component_source **OUT);
196 extern bt_graph_status bt_graph_add_filter_component(bt_graph *graph,
197 const bt_component_class_filter *component_class,
198 const char *name, const bt_value *params,
199 bt_logging_level log_level,
200 const bt_component_filter **OUT);
202 extern bt_graph_status bt_graph_add_filter_component_with_init_method_data(
204 const bt_component_class_filter *component_class,
205 const char *name, const bt_value *params,
206 void *init_method_data, bt_logging_level log_level,
207 const bt_component_filter **OUT);
209 extern bt_graph_status bt_graph_add_sink_component(
210 bt_graph *graph, const bt_component_class_sink *component_class,
211 const char *name, const bt_value *params,
212 bt_logging_level log_level,
213 const bt_component_sink **OUT);
215 extern bt_graph_status bt_graph_add_sink_component_with_init_method_data(
216 bt_graph *graph, const bt_component_class_sink *component_class,
217 const char *name, const bt_value *params,
218 void *init_method_data, bt_logging_level log_level,
219 const bt_component_sink **OUT);
221 extern bt_graph_status bt_graph_connect_ports(bt_graph *graph,
222 const bt_port_output *upstream,
223 const bt_port_input *downstream,
224 const bt_connection **BTOUTCONN);
226 extern bt_graph_status bt_graph_run(bt_graph *graph);
228 extern bt_graph_status bt_graph_consume(bt_graph *graph);
230 extern bt_graph_status bt_graph_add_filter_component_input_port_added_listener(
232 bt_graph_filter_component_input_port_added_listener_func listener,
233 bt_graph_listener_removed_func listener_removed, void *data,
236 extern bt_graph_status bt_graph_add_sink_component_input_port_added_listener(
238 bt_graph_sink_component_input_port_added_listener_func listener,
239 bt_graph_listener_removed_func listener_removed, void *data,
242 extern bt_graph_status bt_graph_add_source_component_output_port_added_listener(
244 bt_graph_source_component_output_port_added_listener_func listener,
245 bt_graph_listener_removed_func listener_removed, void *data,
248 extern bt_graph_status bt_graph_add_filter_component_output_port_added_listener(
250 bt_graph_filter_component_output_port_added_listener_func listener,
251 bt_graph_listener_removed_func listener_removed, void *data,
254 extern bt_graph_status
255 bt_graph_add_source_filter_component_ports_connected_listener(
257 bt_graph_source_filter_component_ports_connected_listener_func listener,
258 bt_graph_listener_removed_func listener_removed, void *data,
261 extern bt_graph_status
262 bt_graph_add_filter_filter_component_ports_connected_listener(
264 bt_graph_filter_filter_component_ports_connected_listener_func listener,
265 bt_graph_listener_removed_func listener_removed, void *data,
268 extern bt_graph_status
269 bt_graph_add_source_sink_component_ports_connected_listener(
271 bt_graph_source_sink_component_ports_connected_listener_func listener,
272 bt_graph_listener_removed_func listener_removed, void *data,
275 extern bt_graph_status
276 bt_graph_add_filter_sink_component_ports_connected_listener(
278 bt_graph_filter_sink_component_ports_connected_listener_func listener,
279 bt_graph_listener_removed_func listener_removed, void *data,
282 extern bt_graph_status bt_graph_cancel(bt_graph *graph);
284 /* Helper functions for Python */
288 static void graph_listener_removed(void *py_callable)
290 BT_ASSERT(py_callable);
291 Py_DECREF(py_callable);
294 static bt_graph_listener_status
296 const void *component,
297 swig_type_info *component_swig_type,
298 bt_component_class_type component_class_type,
300 swig_type_info *port_swig_type,
301 bt_port_type port_type,
304 PyObject *py_component_ptr = NULL;
305 PyObject *py_port_ptr = NULL;
306 PyObject *py_res = NULL;
307 bt_graph_listener_status status;
309 py_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(component), component_swig_type, 0);
310 if (!py_component_ptr) {
311 BT_LOGF_STR("Failed to create component SWIG pointer object.");
312 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
316 py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port), port_swig_type, 0);
318 BT_LOGF_STR("Failed to create port SWIG pointer object.");
319 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
323 py_res = PyObject_CallFunction(py_callable, "(OiOi)",
324 py_component_ptr, component_class_type, py_port_ptr, port_type);
326 bt2_py_loge_exception();
328 status = BT_GRAPH_LISTENER_STATUS_ERROR;
332 BT_ASSERT(py_res == Py_None);
333 status = BT_GRAPH_LISTENER_STATUS_OK;
337 Py_XDECREF(py_port_ptr);
338 Py_XDECREF(py_component_ptr);
343 static bt_graph_listener_status
344 source_component_output_port_added_listener(const bt_component_source *component_source,
345 const bt_port_output *port_output, void *py_callable)
347 return port_added_listener(
348 component_source, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
349 port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable);
352 static bt_graph_listener_status
353 filter_component_input_port_added_listener(const bt_component_filter *component_filter,
354 const bt_port_input *port_input, void *py_callable)
356 return port_added_listener(
357 component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
358 port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable);
361 static bt_graph_listener_status
362 filter_component_output_port_added_listener(const bt_component_filter *component_filter,
363 const bt_port_output *port_output, void *py_callable)
365 return port_added_listener(
366 component_filter, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
367 port_output, SWIGTYPE_p_bt_port_output, BT_PORT_TYPE_OUTPUT, py_callable);
370 static bt_graph_listener_status
371 sink_component_input_port_added_listener(const bt_component_sink *component_sink,
372 const bt_port_input *port_input, void *py_callable)
374 return port_added_listener(
375 component_sink, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
376 port_input, SWIGTYPE_p_bt_port_input, BT_PORT_TYPE_INPUT, py_callable);
380 bt_py3_graph_add_port_added_listener(struct bt_graph *graph,
381 PyObject *py_callable)
383 PyObject *py_listener_ids = NULL;
384 PyObject *py_listener_id = NULL;
386 bt_graph_status status;
389 BT_ASSERT(py_callable);
392 * Behind the scene, we will be registering 4 different listeners and
393 * return all of their ids.
395 py_listener_ids = PyTuple_New(4);
396 if (!py_listener_ids) {
400 /* source output port */
401 status = bt_graph_add_source_component_output_port_added_listener(
402 graph, source_component_output_port_added_listener,
403 graph_listener_removed, py_callable, &listener_id);
404 if (status != BT_GRAPH_STATUS_OK) {
408 py_listener_id = PyLong_FromLong(listener_id);
409 if (!py_listener_id) {
413 PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id);
414 py_listener_id = NULL;
416 /* filter input port */
417 status = bt_graph_add_filter_component_input_port_added_listener(
418 graph, filter_component_input_port_added_listener,
419 graph_listener_removed, py_callable, &listener_id);
420 if (status != BT_GRAPH_STATUS_OK) {
424 py_listener_id = PyLong_FromLong(listener_id);
425 if (!py_listener_id) {
429 PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id);
430 py_listener_id = NULL;
432 /* filter output port */
433 status = bt_graph_add_filter_component_output_port_added_listener(
434 graph, filter_component_output_port_added_listener,
435 graph_listener_removed, py_callable, &listener_id);
436 if (status != BT_GRAPH_STATUS_OK) {
440 py_listener_id = PyLong_FromLong(listener_id);
441 if (!py_listener_id) {
445 PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id);
446 py_listener_id = NULL;
448 /* sink input port */
449 status = bt_graph_add_sink_component_input_port_added_listener(
450 graph, sink_component_input_port_added_listener,
451 graph_listener_removed, py_callable, &listener_id);
452 if (status != BT_GRAPH_STATUS_OK) {
456 py_listener_id = PyLong_FromLong(listener_id);
457 if (!py_listener_id) {
462 PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id);
463 py_listener_id = NULL;
465 Py_INCREF(py_callable);
466 Py_INCREF(py_callable);
467 Py_INCREF(py_callable);
468 Py_INCREF(py_callable);
473 Py_XDECREF(py_listener_ids);
474 py_listener_ids = Py_None;
475 Py_INCREF(py_listener_ids);
479 Py_XDECREF(py_listener_id);
480 return py_listener_ids;
483 static bt_graph_listener_status
484 ports_connected_listener(
485 const void *upstream_component,
486 swig_type_info *upstream_component_swig_type,
487 bt_component_class_type upstream_component_class_type,
488 const bt_port_output *upstream_port,
489 const void *downstream_component,
490 swig_type_info *downstream_component_swig_type,
491 bt_component_class_type downstream_component_class_type,
492 const bt_port_input *downstream_port,
495 PyObject *py_upstream_component_ptr = NULL;
496 PyObject *py_upstream_port_ptr = NULL;
497 PyObject *py_downstream_component_ptr = NULL;
498 PyObject *py_downstream_port_ptr = NULL;
499 PyObject *py_res = NULL;
500 bt_graph_listener_status status;
502 py_upstream_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(upstream_component),
503 upstream_component_swig_type, 0);
504 if (!py_upstream_component_ptr) {
505 BT_LOGF_STR("Failed to create upstream component SWIG pointer object.");
506 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
510 py_upstream_port_ptr = SWIG_NewPointerObj(
511 SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port_output, 0);
512 if (!py_upstream_port_ptr) {
513 BT_LOGF_STR("Failed to create upstream port SWIG pointer object.");
514 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
518 py_downstream_component_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(downstream_component),
519 downstream_component_swig_type, 0);
520 if (!py_downstream_component_ptr) {
521 BT_LOGF_STR("Failed to create downstream component SWIG pointer object.");
522 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
526 py_downstream_port_ptr = SWIG_NewPointerObj(
527 SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port_input, 0);
528 if (!py_downstream_port_ptr) {
529 BT_LOGF_STR("Failed to create downstream port SWIG pointer object.");
530 status = BT_GRAPH_LISTENER_STATUS_NOMEM;
534 py_res = PyObject_CallFunction(py_callable, "(OiOOiO)",
535 py_upstream_component_ptr, upstream_component_class_type,
536 py_upstream_port_ptr,
537 py_downstream_component_ptr, downstream_component_class_type,
538 py_downstream_port_ptr);
540 bt2_py_loge_exception();
542 status = BT_GRAPH_LISTENER_STATUS_ERROR;
546 BT_ASSERT(py_res == Py_None);
547 status = BT_GRAPH_LISTENER_STATUS_OK;
550 Py_XDECREF(py_upstream_component_ptr);
551 Py_XDECREF(py_upstream_port_ptr);
552 Py_XDECREF(py_downstream_component_ptr);
553 Py_XDECREF(py_downstream_port_ptr);
559 static bt_graph_listener_status
560 source_filter_component_ports_connected_listener(
561 const bt_component_source *source_component,
562 const bt_component_filter *filter_component,
563 const bt_port_output *upstream_port,
564 const bt_port_input *downstream_port, void *py_callable)
566 return ports_connected_listener(
567 source_component, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
569 filter_component, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
574 static bt_graph_listener_status
575 source_sink_component_ports_connected_listener(
576 const bt_component_source *source_component,
577 const bt_component_sink *sink_component,
578 const bt_port_output *upstream_port,
579 const bt_port_input *downstream_port, void *py_callable)
581 return ports_connected_listener(
582 source_component, SWIGTYPE_p_bt_component_source, BT_COMPONENT_CLASS_TYPE_SOURCE,
584 sink_component, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
589 static bt_graph_listener_status
590 filter_filter_component_ports_connected_listener(
591 const bt_component_filter *filter_component_left,
592 const bt_component_filter *filter_component_right,
593 const bt_port_output *upstream_port,
594 const bt_port_input *downstream_port, void *py_callable)
596 return ports_connected_listener(
597 filter_component_left, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
599 filter_component_right, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
604 static bt_graph_listener_status
605 filter_sink_component_ports_connected_listener(
606 const bt_component_filter *filter_component,
607 const bt_component_sink *sink_component,
608 const bt_port_output *upstream_port,
609 const bt_port_input *downstream_port, void *py_callable)
611 return ports_connected_listener(
612 filter_component, SWIGTYPE_p_bt_component_filter, BT_COMPONENT_CLASS_TYPE_FILTER,
614 sink_component, SWIGTYPE_p_bt_component_sink, BT_COMPONENT_CLASS_TYPE_SINK,
620 bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph,
621 PyObject *py_callable)
623 PyObject *py_listener_ids = NULL;
624 PyObject *py_listener_id = NULL;
626 bt_graph_status status;
629 BT_ASSERT(py_callable);
631 /* Behind the scene, we will be registering 4 different listeners and
632 * return all of their ids. */
633 py_listener_ids = PyTuple_New(4);
634 if (!py_listener_ids) {
638 /* source -> filter connection */
639 status = bt_graph_add_source_filter_component_ports_connected_listener(
640 graph, source_filter_component_ports_connected_listener,
641 graph_listener_removed, py_callable, &listener_id);
642 if (status != BT_GRAPH_STATUS_OK) {
646 py_listener_id = PyLong_FromLong(listener_id);
647 if (!py_listener_id) {
651 PyTuple_SET_ITEM(py_listener_ids, 0, py_listener_id);
652 py_listener_id = NULL;
654 /* source -> sink connection */
655 status = bt_graph_add_source_sink_component_ports_connected_listener(
656 graph, source_sink_component_ports_connected_listener,
657 graph_listener_removed, py_callable, &listener_id);
658 if (status != BT_GRAPH_STATUS_OK) {
662 py_listener_id = PyLong_FromLong(listener_id);
663 if (!py_listener_id) {
667 PyTuple_SET_ITEM(py_listener_ids, 1, py_listener_id);
668 py_listener_id = NULL;
670 /* filter -> filter connection */
671 status = bt_graph_add_filter_filter_component_ports_connected_listener(
672 graph, filter_filter_component_ports_connected_listener,
673 graph_listener_removed, py_callable, &listener_id);
674 if (status != BT_GRAPH_STATUS_OK) {
678 py_listener_id = PyLong_FromLong(listener_id);
679 if (!py_listener_id) {
683 PyTuple_SET_ITEM(py_listener_ids, 2, py_listener_id);
684 py_listener_id = NULL;
686 /* filter -> sink connection */
687 status = bt_graph_add_filter_sink_component_ports_connected_listener(
688 graph, filter_sink_component_ports_connected_listener,
689 graph_listener_removed, py_callable, &listener_id);
690 if (status != BT_GRAPH_STATUS_OK) {
694 py_listener_id = PyLong_FromLong(listener_id);
695 if (!py_listener_id) {
699 PyTuple_SET_ITEM(py_listener_ids, 3, py_listener_id);
700 py_listener_id = NULL;
702 Py_INCREF(py_callable);
703 Py_INCREF(py_callable);
704 Py_INCREF(py_callable);
705 Py_INCREF(py_callable);
710 Py_XDECREF(py_listener_ids);
711 py_listener_ids = Py_None;
712 Py_INCREF(py_listener_ids);
716 Py_XDECREF(py_listener_id);
717 return py_listener_ids;
722 PyObject *bt_py3_graph_add_port_added_listener(struct bt_graph *graph,
723 PyObject *py_callable);
724 PyObject *bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph,
725 PyObject *py_callable);