forked from timvideos/gst-switch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.py
384 lines (338 loc) · 12.3 KB
/
controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
"""
The controller is the interface for all remote method calls over dbus.
The Controller class creates the controller,
which can be used to invoke the remote methods.
"""
import ast
from .connection import Connection
from .exception import ConnectionReturnError
__all__ = ["Controller", ]
class Controller(object):
"""A Class to control all interactions with the gst-switch-srv over dbus.
Provides the interface for higher level interactions
:param: None
"""
COMPOSITE_NONE = 0
COMPOSITE_PIP = 1
COMPOSITE_DUAL_PREVIEW = 2
COMPOSITE_DUAL_EQUAL = 3
SWITCH_VIDEO_1 = 4
SWITCH_VIDEO_2 = 98
SWITCH_AUDIO = 65
def __init__(
self,
address="unix:abstract=gstswitch",
bus_name='info.duzy.gst.switch.SwitchController',
object_path="/info/duzy/gst/switch/SwitchController",
default_interface="info.duzy.gst.switch.SwitchControllerInterface"
):
super(Controller, self).__init__()
self._address = None
self._bus_name = None
self._object_path = None
self._default_interface = None
self.connection = None
self.address = address
self.bus_name = bus_name
self.object_path = object_path
self.default_interface = default_interface
@property
def address(self):
"""
Get the address
"""
return self._address
@address.setter
def address(self, address):
"""Set the Address
http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
"""
if not address:
raise ValueError("Address '{0}' cannot be blank")
else:
adr = str(address)
if adr.find(':') > 0:
self._address = adr
else:
raise ValueError("Address must follow specifications mentioned"
" at http://dbus.freedesktop.org/doc/"
"dbus-specification.html#addresses")
@property
def bus_name(self):
"""Get the bus name
"""
if self._bus_name is None:
return None
return self._bus_name
@bus_name.setter
def bus_name(self, bus_name):
"""Set the Bus Name
http://dbus.freedesktop.org/doc/dbus-specification.html\
#message-protocol-names-bus
"""
if bus_name is None:
self._bus_name = None
return
bus = str(bus_name)
self._bus_name = bus
@property
def object_path(self):
"""Get the object path
"""
return self._object_path
@object_path.setter
def object_path(self, object_path):
"""Set the object_path
http://dbus.freedesktop.org/doc/dbus-specification.html\
#message-protocol-marshaling-object-path
"""
if not object_path:
raise ValueError("object_path '{0} cannot be blank'")
else:
obj = str(object_path)
if obj[0] == '/':
self._object_path = obj
else:
raise ValueError("object_path must follow specifications"
" mentioned at "
"http://dbus.freedesktop.org/doc/"
"dbus-specification.html"
"#message-protocol-marshaling-object-path""")
@property
def default_interface(self):
"""Get the default interface
"""
return self._default_interface
@default_interface.setter
def default_interface(self, default_interface):
"""Set the default_interface
http://dbus.freedesktop.org/doc/dbus-specification.html\
#message-protocol-names-interface
"""
if not default_interface:
raise ValueError("default_interface '{0} cannot be blank'")
else:
intr = str(default_interface)
if intr.count('.') > 1:
self._default_interface = intr
else:
raise ValueError("default_interface must follow "
"specifications mentioned at "
"http://dbus.freedesktop.org/"
"doc/dbus-specification.html"
"#message-protocol-names-interface")
def establish_connection(self):
"""Establishes a fresh connection to the dbus
Connection stored as self.connection
:param: None
:returns: None
"""
self.connection = Connection(
address=self.address,
bus_name=self.bus_name,
object_path=self.object_path,
default_interface=self.default_interface)
self.connection.connect_dbus()
def get_compose_port(self):
"""Get the compose port number
:param: None
:returns: compose port number
"""
conn = self.connection.get_compose_port()
try:
compose_port = conn.unpack()[0]
return compose_port
except AttributeError:
raise ConnectionReturnError('Connection returned invalid values.'
'Should return a GVariant tuple')
def get_encode_port(self):
"""Get the encode port number
:param: None
:returns: encode port number
"""
conn = self.connection.get_encode_port()
try:
encode_port = conn.unpack()[0]
return encode_port
except AttributeError:
raise ConnectionReturnError('Connection returned invalid values.'
' Should return a GVariant tuple')
def get_audio_port(self):
"""Get the audio port number
:param: None
:returns: audio port number
"""
conn = self.connection.get_audio_port()
try:
audio_port = conn.unpack()[0]
return audio_port
except AttributeError:
raise ConnectionReturnError('Connection returned invalid values. '
'Should return a GVariant tuple')
def get_preview_ports(self):
"""Get all the preview ports
:param: None
:returns: list of all preview ports
"""
conn = self.connection.get_preview_ports()
try:
res = conn.unpack()[0]
preview_ports = self.parse_preview_ports(res)
return preview_ports
except AttributeError:
raise ConnectionReturnError('Connection returned invalid values. '
'Should return a GVariant tuple')
def set_composite_mode(self, mode):
"""Set the current composite mode.
Modes allowed are:
- COMPOSITE_NONE
- COMPOSITE_PIP
- COMPOSITE_DUAL_PREVIEW
- COMPOSITE_DUAL_EQUAL
:param mode: new composite mode
:returns: True when requested
"""
self.establish_connection()
# only modes from 0 to 3 are supported
res = None
if mode in range(0, 4):
try:
conn = self.connection.set_composite_mode(mode)
print conn
res = conn.unpack()[0]
if res is True:
print "Set composite mode to %s" % (str(mode))
except AttributeError:
raise ConnectionReturnError('Connection returned invalid '
'values. Should return a '
'GVariant tuple')
else:
pass
# raise some Exception
return res
def set_encode_mode(self, channel):
"""Set the encode mode
WARNING: THIS DOES NOT WORK.
:param: channel
:returns: True when requested
"""
self.establish_connection()
try:
conn = self.connection.set_encode_mode(channel)
res = conn.unpack()[0]
if res is True:
print "Set encode mode to %s" % (str(channel))
else:
pass
# raise some exception
return res
except AttributeError:
raise ConnectionReturnError('Connection returned invalid values. '
'Should return a GVariant tuple')
def new_record(self):
"""Start a new recording
:param: None
"""
self.establish_connection()
try:
conn = self.connection.new_record()
res = conn.unpack()[0]
if res is True:
# logging
print "New record"
else:
pass
except AttributeError:
raise ConnectionReturnError('Connection returned invalid values. '
'Should return a GVariant tuple')
return res
def adjust_pip(self, xpos, ypos, width, height):
"""Change the PIP position and size
:param xpos: the x position of the PIP
:param ypos: the y position of the PIP
:param width: the width of the PIP
:param height: the height of the PIP
:returns: result - PIP has been changed succefully
"""
self.establish_connection()
try:
conn = self.connection.adjust_pip(xpos, ypos, width, height)
res = conn.unpack()[0]
print "adjust pip xpos:%s ypos:%s w:%s h:%s" % (
str(xpos), str(ypos), str(width), str(height))
except AttributeError:
raise ConnectionReturnError('Connection returned invalid values. '
'Should return a GVariant tuple')
# to-do - parse
return res
def switch(self, channel, port):
"""Switch the channel to the target port
:param channel: The channel to be switched:
SWITCH_VIDEO_1
SWITCH_VIDEO_2
SWITCH_AUDIO
:param port: The target port number
:returns: True when requested
"""
self.establish_connection()
try:
conn = self.connection.switch(channel, port)
res = conn.unpack()[0]
if res is True:
print "Switch channel:%s port:%s" % (str(channel), str(port))
else:
pass
return res
except AttributeError:
raise ConnectionReturnError('Connection returned invalid values. '
'Should return a GVariant tuple')
def click_video(self, xpos, ypos, width, height):
"""User click on the video
:param xpos:
:param ypos:
:param width:
:param height:
:returns: True when requested
"""
self.establish_connection()
try:
conn = self.connection.click_video(xpos, ypos, width, height)
res = conn.unpack()[0]
if res is True:
print "Click video: xpos:%s ypos:%s width:%s height:%s" % (
str(xpos), str(ypos), str(width), str(height))
else:
pass
except:
raise ConnectionReturnError('Connection returned invalid values. '
'Should return a GVariant tuple')
return res
def mark_face(self, faces):
"""Mark faces
:param faces: tuple having four elements
:returns: True when requested
"""
# faces is list of a tuple of four elements
self.establish_connection()
self.connection.mark_face(faces)
def mark_tracking(self, faces):
"""Mark tracking
:param faces: tuple having four elements
:returns: True when requested
"""
self.establish_connection()
self.connection.mark_tracking(faces)
@classmethod
def parse_preview_ports(cls, res):
"""Parses the preview_ports string"""
# res = '[(a, b, c), (a, b, c)*]'
try:
liststr = ast.literal_eval(res)
except (ValueError, SyntaxError):
raise ConnectionReturnError(("Connection returned "
"invalid values:{0}")
.format(res))
preview_ports = []
for tupl in liststr:
preview_ports.append(int(tupl[0]))
return preview_ports