forked from timvideos/gst-switch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
246 lines (215 loc) · 8.52 KB
/
helpers.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
import testsource
from exception import RangeError, InvalidIndexError
__all__ = ["TestSources", "PreviewSinks"]
class TestSources(object):
"""A Controller of test sources feeding into the
gst-switch-srv
:param width: The width of the output video
:param height: The height of the output video
:param pattern: The videotestsrc pattern of the output video
:param timeoverlay: True to enable a running time over video
:param clockoverlay: True to enable current clock time over video
"""
def __init__(self, video_port=None, audio_port=None):
super(TestSources, self).__init__()
self._running_tests_video = []
self._running_tests_audio = []
if video_port:
self.video_port = video_port
if audio_port:
self.audio_port = audio_port
@property
def video_port(self):
return self._video_port
@video_port.setter
def video_port(self, video_port):
try:
i = int(video_port)
if i < 1 or i > 65535:
raise RangeError('video_port must be in range 1 to 65535')
else:
self._video_port = video_port
except TypeError:
raise TypeError("video_port must be a string or number,"
"not, '{0}'".format(type(video_port)))
except ValueError:
raise TypeError("Port must be a valid number")
@property
def audio_port(self):
return self._audio_port
@audio_port.setter
def audio_port(self, audio_port):
try:
i = int(audio_port)
if i < 1 or i > 65535:
raise RangeError('audio must be in range 1 to 65535')
else:
self._audio_port = audio_port
except TypeError:
raise TypeError("audio must be a string or number,"
"not, '{0}'".format(type(audio_port)))
except ValueError:
raise TypeError("Port must be a valid number")
@property
def running_tests_video(self):
return self._running_tests_video
@running_tests_video.setter
def running_tests_video(self, tests):
self._running_tests_video = tests
@property
def running_tests_audio(self):
return self._running_tests_audio
@running_tests_audio.setter
def running_tests_audio(self, tests):
self._running_tests_audio = tests
def new_test_video(self,
width=300,
height=200,
pattern=None,
timeoverlay=False,
clockoverlay=False):
"""Start a new test video
:param port: The port of where the TCP stream will be sent
Should be same as video port of gst-switch-src
:param width: The width of the output video
:param height: The height of the output video
:param pattern: The videotestsrc pattern of the output video
:param timeoverlay: True to enable a running time over video
:param clockoverlay: True to enable current clock time over video
"""
testsrc = testsource.VideoSrc(
self.video_port,
width,
height,
pattern,
timeoverlay,
clockoverlay)
testsrc.run()
self._running_tests_video.append(testsrc)
def get_test_video(self):
"""Returns a list of processes acting as video test sources running
:returns: A list containing all video test sources running
"""
i = 0
for test in self._running_tests_video:
print i, "pattern:", test.pattern
i += 1
return self._running_tests_video
def terminate_index_video(self, index):
"""Terminate video test source specified by index
:param index: The index of the video source to terminate
Use get_test_video for finding the index
"""
try:
testsrc = self._running_tests_video[int(index)]
except IndexError:
raise InvalidIndexError("No video source with index:{0}, use get_test_video() to determine index"
.format(index))
except ValueError:
raise InvalidIndexError("Index should be a valid integer")
except TypeError:
raise InvalidIndexError("Index should be a valid integer")
print 'End source with pattern %s' % (str(testsrc.pattern))
testsrc.end()
self._running_tests_video.remove(self._running_tests_video[index])
def terminate_video(self):
"""Terminate all test video sources
"""
print 'TESTS:', self._running_tests_video
for i in range(len(self._running_tests_video)):
self.terminate_index_video(0)
def new_test_audio(
self,
freq=110,
wave=None
):
"""Start a new test audio
:param port: The port of where the TCP stream will be sent
Should be same as audio port of gst-switch-src
:param width: The width of the output audio
:param height: The height of the output audio
:param pattern: The audiotestsrc pattern of the output audio
:param timeoverlay: True to enable a running time over audio
:param clockoverlay: True to enable current clock time over audio
"""
testsrc = testsource.AudioSrc(
self.audio_port,
freq,
wave)
testsrc.run()
# print testsrc
self._running_tests_audio.append(testsrc)
# print self._running_tests_audio
def get_test_audio(self):
"""Returns a list of processes acting as audio test sources running
:returns: A list containing all audio test sources running
"""
i = 0
for test in self._running_tests_audio:
print i, "wave:", test.wave
i += 1
return self._running_tests_audio
def terminate_index_audio(self, index):
"""Terminate audio test source specified by index
:param index: The index of the audio source to terminate
Use get_test_audio for finding the index
"""
try:
testsrc = self._running_tests_audio[int(index)]
except IndexError:
raise InvalidIndexError("No audio source with index:{0}, use get_test_audio() to determine index"
.format(index))
except ValueError:
raise InvalidIndexError("Index should be a valid integer")
except TypeError:
raise InvalidIndexError("Index should be a valid integer")
print 'End source with pattern %s' % (str(testsrc.wave))
testsrc.end()
self._running_tests_audio.remove(self._running_tests_audio[index])
def terminate_audio(self):
"""Terminate all test audio sources
"""
print 'TESTS:', self._running_tests_audio
for i in range(len(self._running_tests_audio)):
self.terminate_index_audio(0)
class PreviewSinks(object):
"""A Controller for preview sinks to preview ports of gst-switch-srv
:param preview_port: The preview port to get the preview
"""
# TODO set preview port from dbus call
def __init__(self, preview_port=3001):
super(PreviewSinks, self).__init__()
self.preview_port = preview_port
@property
def preview_port(self):
return self._preview_port
@preview_port.setter
def preview_port(self, preview_port):
if not preview_port:
raise ValueError("Preview_port: '{0}' cannot be blank"
.format(preview_port))
else:
try:
i = int(preview_port)
if i < 1 or i > 65535:
raise RangeError('Preview_port must be in range 1 to 65535')
else:
self._preview_port = preview_port
except TypeError:
raise TypeError("Preview_port must be a string or number,"
"not, '{0}'".format(type(preview_port)))
except ValueError:
raise TypeError("Port must be a valid number")
def run(self):
"""Run the Preview Sink"""
self.preview = testsource.Preview(self.preview_port)
self.preview.run()
print 'start preview'
def terminate(self):
"""End/Terminate the Preview Sink"""
try:
self.preview.end()
self.preview = None
print 'end preview'
except AttributeError:
raise AttributeError("No preview Sink to terminate")