diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2012-05-16 17:17:30 +0200 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2016-01-31 23:09:28 +0100 |
| commit | 176d5d73f91326fa3d4648decbe34ecf3e28b5d2 (patch) | |
| tree | 45897a22f1b63e95c1b920bdf41c055804a67cea | |
| parent | bca9152344655716e1e41d15cff994c540c93755 (diff) | |
added oggstreaming script
| -rwxr-xr-x | streamogg.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/streamogg.py b/streamogg.py new file mode 100755 index 0000000..08a50a0 --- /dev/null +++ b/streamogg.py @@ -0,0 +1,82 @@ +#!/usr/bin/python + +import threading +import os +import socket +import re + +filename = '/tmp/test.ogg' +port = 42001 + +lock = threading.Lock() + +connections = [] +header = '' + +fd = os.open(filename, os.O_RDONLY) +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.bind(('', port)) + + +def new_connection(): + global connections + + s.listen(0) + conn, addr = s.accept() + conn.sendall('HTTP/1.1 200 OK\r\nContent-Type: video/ogg\r\n\r\n') + conn.sendall(header) + with lock: + connections.append(conn) + print "new connection" + + t = threading.Thread(target=new_connection) + t.daemon = True + t.start() + + +def main(): + global header, connections, fd + + buf = '' + + # search header + while True: + buf += os.read(fd, 4096) + m = re.match(r'^(OggS.+?OggS.+?OggS.+?OggS.+?)(OggS.*)', buf, flags=re.DOTALL) + if m: + header = m.group(1) + #remainder = m.group(2) + print "header found" + break + + + t = threading.Thread(target=new_connection) + t.daemon = True + t.start() + + while True: + buf = os.read(fd, 4096) + if buf == '': + break + + with lock: + for conn in connections: + try: + conn.sendall(buf) + except socket.error: + conn.close() + with lock: + connections.remove(conn) + print "connection closed" + + os.close(fd) + + +try: + main() +except KeyboardInterrupt: + for conn in connections: + conn.close() + os.close(fd) + + |
