-
Notifications
You must be signed in to change notification settings - Fork 0
/
matt
executable file
·64 lines (58 loc) · 2.05 KB
/
matt
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
#!/usr/bin/env python
from sys import *
import socketserver as ss
import subprocess as sp
from threading import Thread
from queue import Queue
from time import time
import socket
readsize = 1024
# Audio delay in seconds.
delay = 10
rate = 2*44100*16
# Audio delay, in bytes.
delaybytes = rate*delay
class ClientDisconnected(Exception):
pass
class MattHandler(ss.StreamRequestHandler):
def handle(self):
stderr.write('Client has connected.\n')
self.request.settimeout(60)
try:
p = sp.Popen('gst-launch-1.0 filesrc location=/dev/stdin ! '
'decodebin ! queue max-size-bytes={} max-size-buffers=0 '
'max-size-time=0 leaky=downstream ! autoaudiosink '
'sync=false'.format(delaybytes).split(), stdin=sp.PIPE)
while True:
try:
# This call does not time out. Unless the stream sends a
# TCP disconnect, when the connection is permanently lost,
# it hangs forever.
data = self.request.recv(readsize)
except BrokenPipeError:
raise ClientDisconnected()
except ConnectionResetError:
raise ClientDisconnected()
if len(data) == 0:
raise ClientDisconnected()
p.stdin.write(data)
except KeyboardInterrupt:
pass
except ClientDisconnected:
stderr.write('Client has disconnected.\n')
except socket.timeout:
stderr.write('Connection has timed out.\n')
p.stdin.close()
# The previous call is enough to stop the command, but it will be a
# zombie until after the exit code is read. This is what this last
# p.wait() call is for. DIE ZOMBIES!!
p.wait()
# Create the server.
server = ss.TCPServer(('', 4444), MattHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
try:
server.serve_forever()
except KeyboardInterrupt:
print()
pass