morphbr@471
|
1 |
import os
|
morphbr@471
|
2 |
import sys
|
morphbr@471
|
3 |
import lib
|
morphbr@471
|
4 |
import time
|
morphbr@471
|
5 |
import socket
|
morphbr@471
|
6 |
import ConfigParser
|
morphbr@471
|
7 |
|
morphbr@471
|
8 |
class Media:
|
morphbr@471
|
9 |
|
morphbr@471
|
10 |
def __init__(self, config):
|
morphbr@471
|
11 |
|
morphbr@471
|
12 |
self.config = config
|
morphbr@471
|
13 |
self.socket = None
|
morphbr@471
|
14 |
self.child_pid = None
|
morphbr@471
|
15 |
|
morphbr@471
|
16 |
def setup(self, filename, mux, vcodec, vbitrate,\
|
morphbr@471
|
17 |
fps, acodec, abitrate, width, height, port):
|
morphbr@471
|
18 |
|
morphbr@471
|
19 |
self.filename = filename
|
morphbr@471
|
20 |
self.mux = mux
|
morphbr@471
|
21 |
self.vcodec = vcodec
|
morphbr@471
|
22 |
self.vbitrate = int(vbitrate)
|
morphbr@471
|
23 |
self.fps = int(fps)
|
morphbr@471
|
24 |
self.acodec = acodec
|
morphbr@471
|
25 |
self.abitrate = int(abitrate)
|
morphbr@471
|
26 |
self.width = int(width)
|
morphbr@471
|
27 |
self.height = int(height)
|
morphbr@471
|
28 |
|
morphbr@471
|
29 |
self.port = int(port)
|
morphbr@471
|
30 |
self.fifo = self.config.get("Mencoder", "fifo_path")
|
morphbr@471
|
31 |
|
morphbr@471
|
32 |
try:
|
morphbr@471
|
33 |
os.mkfifo(self.fifo)
|
morphbr@471
|
34 |
except:
|
morphbr@471
|
35 |
lib.log("Fifo already exists")
|
morphbr@471
|
36 |
|
morphbr@471
|
37 |
# good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
|
morphbr@471
|
38 |
self.path = self.config.get("Mencoder", "path")
|
morphbr@471
|
39 |
self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\
|
morphbr@471
|
40 |
" -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
|
morphbr@471
|
41 |
" -o %s 1> /dev/null 2> /dev/null" % (
|
morphbr@471
|
42 |
self.filename, self.fps, self.mux, self.acodec, self.abitrate,
|
morphbr@471
|
43 |
self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
|
morphbr@471
|
44 |
|
morphbr@471
|
45 |
|
morphbr@471
|
46 |
if (self.socket != None):
|
morphbr@471
|
47 |
del(self.socket)
|
morphbr@471
|
48 |
|
morphbr@471
|
49 |
self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
morphbr@471
|
50 |
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
morphbr@471
|
51 |
self.socket.bind( ('', self.port) )
|
morphbr@471
|
52 |
self.socket.settimeout(10)
|
morphbr@471
|
53 |
self.socket.listen(1)
|
morphbr@471
|
54 |
|
morphbr@471
|
55 |
def play(self):
|
morphbr@471
|
56 |
|
morphbr@471
|
57 |
lib.log("Starting Mencoder: %s" % self.path )
|
morphbr@471
|
58 |
# exec Mencoder
|
morphbr@471
|
59 |
pin, pout = os.popen2(self.path)
|
morphbr@471
|
60 |
pin.close()
|
morphbr@471
|
61 |
|
morphbr@471
|
62 |
fifo = open(self.fifo)
|
morphbr@471
|
63 |
|
morphbr@471
|
64 |
self.child_pid = os.fork()
|
morphbr@471
|
65 |
if (self.child_pid == 0):
|
morphbr@471
|
66 |
conn,addr= self.socket.accept()
|
morphbr@471
|
67 |
lib.log("Sending Data to client: %s" % addr[0])
|
morphbr@471
|
68 |
|
morphbr@471
|
69 |
data = fifo.read(4096)
|
morphbr@471
|
70 |
conn.settimeout(5)
|
morphbr@471
|
71 |
retry = 0
|
morphbr@471
|
72 |
|
morphbr@471
|
73 |
while( data != "" and retry < 5):
|
morphbr@471
|
74 |
try:
|
morphbr@471
|
75 |
conn.send(data)
|
morphbr@471
|
76 |
except socket.error:
|
morphbr@471
|
77 |
lib.log("Socket error (maybe timeout ?)")
|
morphbr@471
|
78 |
retry += 1
|
morphbr@471
|
79 |
|
morphbr@471
|
80 |
data = fifo.read(4096)
|
morphbr@471
|
81 |
|
morphbr@471
|
82 |
if (retry < 5):
|
morphbr@471
|
83 |
lib.log("Finished sending Data to client: %s" % addr[0])
|
morphbr@471
|
84 |
else:
|
morphbr@471
|
85 |
lib.log("Client timed out")
|
morphbr@471
|
86 |
|
morphbr@471
|
87 |
pout.close()
|
morphbr@471
|
88 |
|
morphbr@471
|
89 |
def stop(self):
|
morphbr@471
|
90 |
|
morphbr@471
|
91 |
if (self.socket != None):
|
morphbr@471
|
92 |
lib.log("Closing socket")
|
morphbr@471
|
93 |
self.socket.close()
|
morphbr@471
|
94 |
|
morphbr@471
|
95 |
lib.log("Trying to stop Mencoder process")
|
morphbr@471
|
96 |
if (self.child_pid != None):
|
morphbr@471
|
97 |
os.kill(self.child_pid, 9)
|