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
29 enum bt_graph_status {
30 BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION = 111,
31 BT_GRAPH_STATUS_CANCELED = 125,
32 BT_GRAPH_STATUS_AGAIN = 11,
33 BT_GRAPH_STATUS_END = 1,
34 BT_GRAPH_STATUS_OK = 0,
35 BT_GRAPH_STATUS_INVALID = -22,
36 BT_GRAPH_STATUS_NO_SINK = -6,
37 BT_GRAPH_STATUS_ERROR = -1,
38 BT_GRAPH_STATUS_NOMEM = -12,
42 struct bt_graph *bt_graph_create(void);
43 enum bt_graph_status bt_graph_add_component(
44 struct bt_graph *graph,
45 struct bt_component_class *component_class,
46 const char *name, struct bt_value *params,
47 struct bt_component **BTOUTCOMP);
48 enum bt_graph_status bt_graph_add_component_with_init_method_data(
49 struct bt_graph *graph,
50 struct bt_component_class *component_class,
51 const char *name, struct bt_value *params,
52 void *init_method_data,
53 struct bt_component **BTOUTCOMP);
54 enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph,
55 struct bt_port *upstream, struct bt_port *downstream,
56 struct bt_connection **BTOUTCONN);
57 enum bt_graph_status bt_graph_run(struct bt_graph *graph);
58 enum bt_graph_status bt_graph_consume(struct bt_graph *graph);
59 enum bt_graph_status bt_graph_cancel(struct bt_graph *graph);
60 int bt_graph_is_canceled(struct bt_graph *graph);
62 /* Helper functions for Python */
64 static void port_added_listener(struct bt_port *port, void *py_callable)
66 PyObject *py_port_ptr = NULL;
67 PyObject *py_res = NULL;
69 py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port),
70 SWIGTYPE_p_bt_port, 0);
72 BT_LOGF_STR("Failed to create a SWIG pointer object.");
76 py_res = PyObject_CallFunction(py_callable, "(O)", py_port_ptr);
77 assert(py_res == Py_None);
78 Py_DECREF(py_port_ptr);
82 static void port_removed_listener(struct bt_component *component,
83 struct bt_port *port, void *py_callable)
85 PyObject *py_port_ptr = NULL;
86 PyObject *py_res = NULL;
88 py_port_ptr = SWIG_NewPointerObj(SWIG_as_voidptr(port),
89 SWIGTYPE_p_bt_port, 0);
91 BT_LOGF_STR("Failed to create a SWIG pointer object.");
95 py_res = PyObject_CallFunction(py_callable, "(O)", py_port_ptr);
96 assert(py_res == Py_None);
97 Py_DECREF(py_port_ptr);
101 static void ports_connected_listener(struct bt_port *upstream_port,
102 struct bt_port *downstream_port, void *py_callable)
104 PyObject *py_upstream_port_ptr = NULL;
105 PyObject *py_downstream_port_ptr = NULL;
106 PyObject *py_res = NULL;
108 py_upstream_port_ptr = SWIG_NewPointerObj(
109 SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port, 0);
110 if (!py_upstream_port_ptr) {
111 BT_LOGF_STR("Failed to create a SWIG pointer object.");
115 py_downstream_port_ptr = SWIG_NewPointerObj(
116 SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port, 0);
117 if (!py_downstream_port_ptr) {
118 BT_LOGF_STR("Failed to create a SWIG pointer object.");
122 py_res = PyObject_CallFunction(py_callable, "(OO)",
123 py_upstream_port_ptr, py_downstream_port_ptr);
124 assert(py_res == Py_None);
125 Py_DECREF(py_upstream_port_ptr);
126 Py_DECREF(py_downstream_port_ptr);
130 static void ports_disconnected_listener(
131 struct bt_component *upstream_component,
132 struct bt_component *downstream_component,
133 struct bt_port *upstream_port, struct bt_port *downstream_port,
136 PyObject *py_upstream_comp_ptr = NULL;
137 PyObject *py_downstream_comp_ptr = NULL;
138 PyObject *py_upstream_port_ptr = NULL;
139 PyObject *py_downstream_port_ptr = NULL;
140 PyObject *py_res = NULL;
142 py_upstream_comp_ptr = SWIG_NewPointerObj(
143 SWIG_as_voidptr(upstream_component),
144 SWIGTYPE_p_bt_component, 0);
145 if (!py_upstream_comp_ptr) {
146 BT_LOGF_STR("Failed to create a SWIG pointer object.");
150 py_downstream_comp_ptr = SWIG_NewPointerObj(
151 SWIG_as_voidptr(downstream_component),
152 SWIGTYPE_p_bt_component, 0);
153 if (!py_downstream_comp_ptr) {
154 BT_LOGF_STR("Failed to create a SWIG pointer object.");
158 py_upstream_port_ptr = SWIG_NewPointerObj(
159 SWIG_as_voidptr(upstream_port), SWIGTYPE_p_bt_port, 0);
160 if (!py_upstream_port_ptr) {
161 BT_LOGF_STR("Failed to create a SWIG pointer object.");
165 py_downstream_port_ptr = SWIG_NewPointerObj(
166 SWIG_as_voidptr(downstream_port), SWIGTYPE_p_bt_port, 0);
167 if (!py_downstream_port_ptr) {
168 BT_LOGF_STR("Failed to create a SWIG pointer object.");
172 py_res = PyObject_CallFunction(py_callable, "(OOOO)",
173 py_upstream_comp_ptr, py_downstream_comp_ptr,
174 py_upstream_port_ptr, py_downstream_port_ptr);
175 assert(py_res == Py_None);
176 Py_DECREF(py_upstream_comp_ptr);
177 Py_DECREF(py_downstream_comp_ptr);
178 Py_DECREF(py_upstream_port_ptr);
179 Py_DECREF(py_downstream_port_ptr);
183 static void graph_listener_removed(void *py_callable)
186 Py_DECREF(py_callable);
189 static int bt_py3_graph_add_port_added_listener(struct bt_graph *graph,
190 PyObject *py_callable)
196 ret = bt_graph_add_port_added_listener(graph, port_added_listener,
197 graph_listener_removed, py_callable);
199 Py_INCREF(py_callable);
205 static int bt_py3_graph_add_port_removed_listener(struct bt_graph *graph,
206 PyObject *py_callable)
212 ret = bt_graph_add_port_removed_listener(graph, port_removed_listener,
213 graph_listener_removed, py_callable);
215 Py_INCREF(py_callable);
221 static int bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph,
222 PyObject *py_callable)
228 ret = bt_graph_add_ports_connected_listener(graph,
229 ports_connected_listener, graph_listener_removed, py_callable);
231 Py_INCREF(py_callable);
237 static int bt_py3_graph_add_ports_disconnected_listener(struct bt_graph *graph,
238 PyObject *py_callable)
244 ret = bt_graph_add_ports_disconnected_listener(graph,
245 ports_disconnected_listener, graph_listener_removed,
248 Py_INCREF(py_callable);
255 int bt_py3_graph_add_port_added_listener(struct bt_graph *graph,
256 PyObject *py_callable);
257 int bt_py3_graph_add_port_removed_listener(struct bt_graph *graph,
258 PyObject *py_callable);
259 int bt_py3_graph_add_ports_connected_listener(struct bt_graph *graph,
260 PyObject *py_callable);
261 int bt_py3_graph_add_ports_disconnected_listener(struct bt_graph *graph,
262 PyObject *py_callable);