-
Notifications
You must be signed in to change notification settings - Fork 0
/
slapdash.py
390 lines (292 loc) · 13 KB
/
slapdash.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
385
386
387
388
389
390
import os
import sys
import yaml
import schedule
from datetime import datetime
import asyncio, gbulb, websockets
gbulb.install()
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstVideo', '1.0')
from gi.repository import Gst, GstVideo, GLib
Gst.init(None)
if not sys.argv[1]:
print('Usage: slapdash.py <settings_file.yaml>')
quit()
settings = yaml.load(open(sys.argv[1]))
class Main:
def __init__(self):
self.loop = asyncio.get_event_loop()
self.sockets = set()
self.queues = set()
self.elements = set()
self.pipeline = Gst.Pipeline()
self.clock = self.pipeline.get_pipeline_clock()
self.stream_state = 'stopped'
self.force_keyframes = False
self.bus = self.pipeline.get_bus()
self.bus.add_signal_watch()
self.bus.connect('message', self.on_message)
def build_pipeline(self):
self.elements = set()
self.muxqs = set()
self.stop_actions = []
video_settings = settings.get('video_settings', {})
self.force_keyframes = video_settings.get('force_keyframes', True)
audio_encode_props = settings.get('audio_settings', {}).copy()
if audio_encode_props.get('encoder'):
audio_encoder = audio_encode_props['encoder']
del audio_encode_props['encoder']
else:
audio_encoder = 'avenc_aac'
if audio_encode_props.get('bitrate'):
audio_encode_props['bitrate'] *= 1000
self.malm(settings['audio_source'] + [
'audioconvert',
{audio_encoder: audio_encode_props},
'aacparse',
{'tee': {'name': 'aall'}}
])
self.malm(settings['video_source'] + [
'videoconvert',
{'tee': {'name': 'vinput'}}
])
for rate in settings['video_rates']:
name = list(rate)[0]
props = rate[name]
rate_is_used = False
for target in settings['targets']:
print(name, name in target[list(target)[0]]['rates'])
if name in target[list(target)[0]]['rates']:
rate_is_used = True
break
if not rate_is_used: continue
caps = ['video/x-raw']
if props.get('width'):
caps.append('width = {}'.format(props['width']))
del props['width']
if props.get('height'):
caps.append('height = {}'.format(props['height']))
del props['height']
if props.get('framerate'):
caps.append('framerate = {}'.format(props['framerate']))
del props['framerate']
caps = ', '.join(caps)
if props.get('tune') == None:
props['tune'] = 'zerolatency'
elif props.get('tune') == '':
del props['tune']
if props.get('option-string') == None:
props['option-string'] = 'scenecut=0'
elif props.get('option-string') == '':
del props['option-string']
if props.get('speed-preset') == None:
props['speed-preset'] = 1
elif props.get('speed-preset') == '':
del props['spreed-preset']
self.malm([
{'queue': {'name': 'v{}'.format(name)}},
'videorate',
'videoscale',
{'capsfilter': {'caps': caps}},
{'x264enc': props},
{'capsfilter': {'caps': 'video/x-h264, profile=baseline'}},
'h264parse',
{'tee': {'name': 't{}'.format(name)}}
])
self.vinput.link(getattr(self, 'v{}'.format(name)))
for target in settings['targets']:
name = list(target)[0]
props = target[name]
filenames = []
for rate in props['rates']:
if props['type'] == 'rtmp':
muxer = {'flvmux': {'name': 'm{}{}'.format(name, rate), 'streamable': True}}
sink = {'rtmpsink': {'location': props['location'] + rate}}
elif props['type'] == 'file':
ts = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
l = props['location']
prefix = l[:l.rfind('/')]
filename = '{} {}{}.{}'.format(ts, l[l.rfind('/') + 1:], rate, props['muxer'])
location = '{}/{}'.format(prefix, filename)
if props.get('erase_old'):
for filename in os.listdir(prefix):
if filename.endswith('{}{}.{}'.format(l[l.rfind('/') + 1:], rate, props['muxer'])):
os.unlink(os.path.join(prefix, filename))
sink = {'filesink': {'location': location}}
filenames.append(filename)
if props['muxer'] == 'mp4':
muxer = {'mp4mux': {'name': 'm{}{}'.format(name, rate), 'faststart': False}}
elif props['muxer'] == 'mkv':
muxer = {'matroskamux': {'name': 'm{}{}'.format(name, rate)}}
self.malm([muxer, sink])
mux = getattr(self, 'm{}{}'.format(name, rate))
rate_tee = getattr(self, 't{}'.format(rate))
vq = Gst.ElementFactory.make('queue')
aq = Gst.ElementFactory.make('queue')
self.muxqs.add(vq)
self.muxqs.add(aq)
self.pipeline.add(vq)
self.pipeline.add(aq)
rate_tee.link(vq)
vq.link(mux)
self.aall.link(aq)
aq.link(mux)
action = props.get('stop_action')
if action:
action['filenames'] = filenames
self.stop_actions.append(action)
def malm(self, to_add):
# Make-add-link multi
prev = None
for desc in to_add:
element = None
props = None
name = None
if type(desc) == str:
_type = desc
elif type(desc) == dict:
_type = list(desc)[0]
props = desc.get(_type)
name = props.get('name')
if name: del props['name']
else:
element = desc
if not element:
element = Gst.ElementFactory.make(_type, name)
if not element:
raise Exception('cannot create element {}'.format(_type))
self.elements.add(element)
if name: setattr(self, name, element)
if props:
for p, v in props.items():
if p == 'caps':
caps = Gst.Caps.from_string(v)
element.set_property('caps', caps)
else:
element.set_property(p, v)
self.pipeline.add(element)
if prev: prev.link(element)
prev = element
def run(self):
GLib.timeout_add(2 * 1000, self.do_keyframe, None)
asyncio.ensure_future(self.run_scheduler())
port = int(settings.get('port', 8081))
self.ws_server = websockets.serve(self.handler, '0.0.0.0', port)
self.loop.run_until_complete(self.ws_server)
self.loop.run_forever()
async def run_scheduler(self):
while True:
schedule.run_pending()
await asyncio.sleep(1)
def stop(self):
print('Exiting...')
self.stream_stop()
self.loop.stop()
def do_keyframe(self, user_data):
# Forces a keyframe on all video encoders
if self.stream_state == 'streaming' and self.force_keyframes:
event = GstVideo.video_event_new_downstream_force_key_unit(self.clock.get_time(), 0, 0, True, 0)
self.pipeline.send_event(event)
return True
def on_message(self, bus, msg):
sendmsg = None
if msg.type == Gst.MessageType.ERROR:
sendmsg = 'error {}'.format(msg.parse_error())
self.pipeline.set_state(Gst.State.NULL)
asyncio.ensure_future(self._stream_restart_delay())
if msg.type == Gst.MessageType.WARNING:
sendmsg = 'warning {}'.format(msg.parse_error())
if msg.type == Gst.MessageType.STATE_CHANGED:
statemap = {
Gst.State.NULL: 'stopped',
Gst.State.READY: 'ready',
Gst.State.PAUSED: 'paused',
Gst.State.PLAYING: 'streaming'
}
newstate = statemap[msg.parse_state_changed().newstate]
if not self.stream_state == newstate:
self.stream_state = newstate
sendmsg = 'state {}'.format(newstate)
if msg.type == Gst.MessageType.EOS:
self.pipeline.set_state(Gst.State.NULL)
self.stream_state = 'stopped'
sendmsg = 'state stopped'
if sendmsg:
print(sendmsg)
self.publish(sendmsg)
def stream_restart(self):
self.stream_stop()
asyncio.ensure_future(self._stream_restart_delay())
async def _stream_restart_delay(self):
await asyncio.sleep(1)
if self.stream_state == 'stopped':
self.stream_start()
else:
await self._stream_restart_delay()
def stream_stop(self):
if self.stream_state == 'stopped': return
print('stopping stream')
Gst.debug_bin_to_dot_file(self.pipeline, Gst.DebugGraphDetails.ALL, 'slapdash')
self.pipeline.send_event(Gst.Event.new_eos())
def stream_start(self):
if self.stream_state == 'streaming': return
print('starting stream')
for element in self.elements: self.pipeline.remove(element)
self.build_pipeline()
self.pipeline.set_state(Gst.State.PLAYING)
def publish(self, message):
for queue in self.queues:
queue.put_nowait(message)
async def consumer_handler(self, websocket):
while True:
try: message = await websocket.recv()
except websockets.exceptions.ConnectionClosed: break
if message == 'restart': self.stream_restart()
elif message == 'stop': self.stream_stop()
elif message == 'start': self.stream_start()
async def producer_handler(self, websocket, queue):
while True:
message = await queue.get()
await websocket.send(message)
async def handler(self, websocket, path):
print("connected: {}:{}".format(*websocket.remote_address))
self.sockets.add(websocket)
queue = asyncio.Queue()
self.queues.add(queue)
queue.put_nowait('state {}'.format(self.stream_state))
for job in schedule.jobs:
queue.put_nowait('schedule {} {}'.format(job.job_func.__name__, job.next_run))
try:
consumer_task = asyncio.ensure_future(self.consumer_handler(websocket))
producer_task = asyncio.ensure_future(self.producer_handler(websocket, queue))
done, pending = await asyncio.wait(
[consumer_task, producer_task],
return_when=asyncio.FIRST_COMPLETED,
)
finally:
print("disconnected: {c.host}:{c.port}".format(c = websocket))
self.sockets.remove(websocket)
self.queues.remove(queue)
for task in pending:
task.cancel()
main = Main()
for item in settings.get('schedule', []):
day = list(item)[0]
for time, action in item[day].items():
if action == 'start': action = main.stream_start
elif action == 'stop': action = main.stream_stop
else: raise Exception('Invalid schedule action: {}'.format(action))
if day == 'daily': schedule.every().day.at(time).do(action)
elif day == 'sunday': schedule.every().sunday.at(time).do(action)
elif day == 'monday': schedule.every().monday.at(time).do(action)
elif day == 'tuesday': schedule.every().tuesday.at(time).do(action)
elif day == 'wednesday': schedule.every().wednesday.at(time).do(action)
elif day == 'thursday': schedule.every().thursday.at(time).do(action)
elif day == 'friday': schedule.every().friday.at(time).do(action)
elif day == 'saturday': schedule.every().saturday.at(time).do(action)
else: raise Exception('Invalid schedule day: {}'.format(day))
try:
main.run()
except KeyboardInterrupt:
main.stop()