Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
811644b8 PP |
2 | # |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
811644b8 PP |
4 | |
5 | from bt2 import native_bt, object, utils | |
3fb99a22 PP |
6 | from bt2 import interrupter as bt2_interrupter |
7 | from bt2 import connection as bt2_connection | |
8 | from bt2 import component as bt2_component | |
811644b8 | 9 | import functools |
3fb99a22 PP |
10 | from bt2 import port as bt2_port |
11 | from bt2 import logging as bt2_logging | |
811644b8 PP |
12 | import bt2 |
13 | ||
14 | ||
cfbd7cf3 FD |
15 | def _graph_port_added_listener_from_native( |
16 | user_listener, component_ptr, component_type, port_ptr, port_type | |
17 | ): | |
615238be | 18 | component = bt2_component._create_component_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
19 | component_ptr, component_type |
20 | ) | |
5813b3a3 | 21 | port = bt2_port._create_from_const_ptr_and_get_ref(port_ptr, port_type) |
5f25509b | 22 | user_listener(component, port) |
811644b8 PP |
23 | |
24 | ||
78288f58 | 25 | class Graph(object._SharedObject): |
2f16a6a2 PP |
26 | _get_ref = staticmethod(native_bt.graph_get_ref) |
27 | _put_ref = staticmethod(native_bt.graph_put_ref) | |
601c0026 | 28 | |
056deb59 PP |
29 | def __init__(self, mip_version=0): |
30 | utils._check_uint64(mip_version) | |
31 | ||
32 | if mip_version > bt2.get_maximal_mip_version(): | |
33 | raise ValueError('unknown MIP version {}'.format(mip_version)) | |
34 | ||
35 | ptr = native_bt.graph_create(mip_version) | |
811644b8 PP |
36 | |
37 | if ptr is None: | |
694c792b | 38 | raise bt2._MemoryError('cannot create graph object') |
811644b8 PP |
39 | |
40 | super().__init__(ptr) | |
41 | ||
f3d6b4c2 PP |
42 | # list of listener partials to keep a reference as long as |
43 | # this graph exists | |
44 | self._listener_partials = [] | |
45 | ||
cfbd7cf3 FD |
46 | def add_component( |
47 | self, | |
48 | component_class, | |
49 | name, | |
50 | params=None, | |
66964f3f | 51 | obj=None, |
3fb99a22 | 52 | logging_level=bt2_logging.LoggingLevel.NONE, |
cfbd7cf3 | 53 | ): |
615238be | 54 | if isinstance(component_class, bt2_component._SourceComponentClassConst): |
5f25509b | 55 | cc_ptr = component_class._ptr |
66964f3f | 56 | add_fn = native_bt.bt2_graph_add_source_component |
894a8df5 | 57 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
615238be | 58 | elif isinstance(component_class, bt2_component._FilterComponentClassConst): |
5f25509b | 59 | cc_ptr = component_class._ptr |
66964f3f | 60 | add_fn = native_bt.bt2_graph_add_filter_component |
894a8df5 | 61 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
615238be | 62 | elif isinstance(component_class, bt2_component._SinkComponentClassConst): |
5f25509b | 63 | cc_ptr = component_class._ptr |
66964f3f | 64 | add_fn = native_bt.bt2_graph_add_sink_component |
5f25509b | 65 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
3fb99a22 | 66 | elif issubclass(component_class, bt2_component._UserSourceComponent): |
85906b6b | 67 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 68 | add_fn = native_bt.bt2_graph_add_source_component |
5f25509b | 69 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
3fb99a22 | 70 | elif issubclass(component_class, bt2_component._UserSinkComponent): |
85906b6b | 71 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 72 | add_fn = native_bt.bt2_graph_add_sink_component |
894a8df5 | 73 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
3fb99a22 | 74 | elif issubclass(component_class, bt2_component._UserFilterComponent): |
85906b6b | 75 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 76 | add_fn = native_bt.bt2_graph_add_filter_component |
5f25509b | 77 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
811644b8 | 78 | else: |
cfbd7cf3 FD |
79 | raise TypeError( |
80 | "'{}' is not a component class".format( | |
81 | component_class.__class__.__name__ | |
82 | ) | |
83 | ) | |
811644b8 PP |
84 | |
85 | utils._check_str(name) | |
e874da19 | 86 | utils._check_log_level(logging_level) |
66964f3f PP |
87 | base_cc_ptr = component_class._bt_component_class_ptr() |
88 | ||
89 | if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr): | |
90 | raise ValueError('cannot pass a Python object to a non-Python component') | |
811644b8 | 91 | |
401b7022 FD |
92 | if params is not None and not isinstance(params, (dict, bt2.MapValue)): |
93 | raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.") | |
94 | ||
66964f3f | 95 | params = bt2.create_value(params) |
401b7022 | 96 | |
601c0026 | 97 | params_ptr = params._ptr if params is not None else None |
811644b8 | 98 | |
66964f3f PP |
99 | status, comp_ptr = add_fn( |
100 | self._ptr, cc_ptr, name, params_ptr, obj, logging_level | |
101 | ) | |
d24d5663 | 102 | utils._handle_func_status(status, 'cannot add component to graph') |
894a8df5 | 103 | assert comp_ptr |
a91462c9 PP |
104 | return bt2_component._create_component_from_const_ptr_and_get_ref( |
105 | comp_ptr, cc_type | |
106 | ) | |
811644b8 PP |
107 | |
108 | def connect_ports(self, upstream_port, downstream_port): | |
5813b3a3 FD |
109 | utils._check_type(upstream_port, bt2_port._OutputPortConst) |
110 | utils._check_type(downstream_port, bt2_port._InputPortConst) | |
cfbd7cf3 FD |
111 | status, conn_ptr = native_bt.graph_connect_ports( |
112 | self._ptr, upstream_port._ptr, downstream_port._ptr | |
113 | ) | |
114 | utils._handle_func_status(status, 'cannot connect component ports within graph') | |
115 | assert conn_ptr | |
a91462c9 | 116 | return bt2_connection._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
811644b8 | 117 | |
5f25509b SM |
118 | def add_port_added_listener(self, listener): |
119 | if not callable(listener): | |
811644b8 PP |
120 | raise TypeError("'listener' parameter is not callable") |
121 | ||
d24d5663 | 122 | fn = native_bt.bt2_graph_add_port_added_listener |
cfbd7cf3 FD |
123 | listener_from_native = functools.partial( |
124 | _graph_port_added_listener_from_native, listener | |
125 | ) | |
811644b8 | 126 | |
5f25509b SM |
127 | listener_ids = fn(self._ptr, listener_from_native) |
128 | if listener_ids is None: | |
694c792b | 129 | raise bt2._Error('cannot add listener to graph object') |
416379bc | 130 | |
f3d6b4c2 PP |
131 | # keep the partial's reference |
132 | self._listener_partials.append(listener_from_native) | |
133 | ||
8cc0e6ea PP |
134 | def run_once(self): |
135 | status = native_bt.graph_run_once(self._ptr) | |
136 | utils._handle_func_status(status, 'graph object could not run once') | |
137 | ||
811644b8 PP |
138 | def run(self): |
139 | status = native_bt.graph_run(self._ptr) | |
9669d693 | 140 | utils._handle_func_status(status, 'graph object stopped running') |
811644b8 | 141 | |
9b4f9b42 | 142 | def add_interrupter(self, interrupter): |
3fb99a22 | 143 | utils._check_type(interrupter, bt2_interrupter.Interrupter) |
9b4f9b42 | 144 | native_bt.graph_add_interrupter(self._ptr, interrupter._ptr) |
811644b8 | 145 | |
c513d240 SM |
146 | @property |
147 | def default_interrupter(self): | |
148 | ptr = native_bt.graph_borrow_default_interrupter(self._ptr) | |
149 | return bt2_interrupter.Interrupter._create_from_ptr_and_get_ref(ptr) |