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@475
|
15 |
self.mplayer = None
|
morphbr@475
|
16 |
|
morphbr@475
|
17 |
def set_args(self, options):
|
morphbr@475
|
18 |
|
morphbr@475
|
19 |
for opt in options:
|
morphbr@475
|
20 |
|
morphbr@475
|
21 |
if (opt == "file"):
|
morphbr@475
|
22 |
self.kind = "file"
|
morphbr@475
|
23 |
self.args += " %s -mf fps=%d -of %s -oac %s -lameopts vol=5 cbr:br=%d"\
|
morphbr@475
|
24 |
" -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
|
morphbr@475
|
25 |
" -o %s 1> /dev/null 2> /dev/null" % (
|
morphbr@475
|
26 |
self.filename, self.fps, self.mux, self.acodec, self.abitrate,
|
morphbr@475
|
27 |
self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
|
morphbr@475
|
28 |
|
morphbr@475
|
29 |
elif (opt == "dvd"):
|
morphbr@475
|
30 |
self.kind = "dvd"
|
morphbr@475
|
31 |
self.args += " dvd://%s -vf scale=%d:%d -oac %s -lameopts vol=5 cbr:br=%d"\
|
morphbr@475
|
32 |
" -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -o %s"\
|
morphbr@475
|
33 |
" -mf fps=%d 1> /dev/null 2> /dev/null" % (
|
morphbr@475
|
34 |
self.filename, self.width, self.height, self.acodec, self.abitrate,
|
morphbr@475
|
35 |
self.mux, self.vcodec, self.vbitrate, self.fifo, self.fps)
|
morphbr@475
|
36 |
|
morphbr@475
|
37 |
elif (opt == "local"):
|
morphbr@475
|
38 |
self.mplayer = os.popen("which mplayer").read().strip()
|
morphbr@471
|
39 |
|
morphbr@471
|
40 |
def setup(self, filename, mux, vcodec, vbitrate,\
|
morphbr@475
|
41 |
fps, acodec, abitrate, width, height, port, options):
|
morphbr@471
|
42 |
|
morphbr@475
|
43 |
self.filename = filename
|
morphbr@475
|
44 |
self.mux = mux
|
morphbr@475
|
45 |
self.vcodec = vcodec
|
morphbr@475
|
46 |
self.vbitrate = int(vbitrate)
|
morphbr@475
|
47 |
self.fps = int(fps)
|
morphbr@475
|
48 |
self.acodec = acodec
|
morphbr@475
|
49 |
self.abitrate = int(abitrate)
|
morphbr@475
|
50 |
self.width = int(width)
|
morphbr@475
|
51 |
self.height = int(height)
|
morphbr@471
|
52 |
|
morphbr@475
|
53 |
self.port = int(port)
|
morphbr@475
|
54 |
self.fifo = self.config.get("Mencoder", "fifo_path")
|
morphbr@474
|
55 |
|
morphbr@475
|
56 |
self.args = ""
|
morphbr@475
|
57 |
self.kind = ""
|
morphbr@475
|
58 |
self.set_args(options)
|
morphbr@474
|
59 |
|
morphbr@475
|
60 |
if (self.kind == "file" and not os.path.exists(self.filename)):
|
morphbr@475
|
61 |
msg = "File requested does not exist. SETUP failed."
|
morphbr@475
|
62 |
lib.log(msg)
|
morphbr@475
|
63 |
return msg
|
morphbr@474
|
64 |
|
morphbr@475
|
65 |
# good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
|
morphbr@475
|
66 |
#mencoder dvd://2 -vf scale=640:480 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4
|
morphbr@475
|
67 |
self.path = self.config.get("Mencoder", "path")
|
morphbr@475
|
68 |
self.path += self.args
|
morphbr@474
|
69 |
|
morphbr@475
|
70 |
if (self.socket != None):
|
morphbr@475
|
71 |
del(self.socket)
|
morphbr@474
|
72 |
|
morphbr@475
|
73 |
self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
morphbr@475
|
74 |
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
morphbr@475
|
75 |
self.socket.bind( ('', self.port) )
|
morphbr@475
|
76 |
self.socket.listen(1)
|
morphbr@474
|
77 |
|
morphbr@475
|
78 |
return 0
|
morphbr@474
|
79 |
|
morphbr@474
|
80 |
|
morphbr@474
|
81 |
def play(self):
|
morphbr@471
|
82 |
|
morphbr@471
|
83 |
try:
|
morphbr@471
|
84 |
os.mkfifo(self.fifo)
|
morphbr@471
|
85 |
except:
|
morphbr@471
|
86 |
lib.log("Fifo already exists")
|
morphbr@471
|
87 |
|
morphbr@471
|
88 |
lib.log("Starting Mencoder: %s" % self.path )
|
morphbr@471
|
89 |
# exec Mencoder
|
morphbr@474
|
90 |
pin, self.pout = os.popen2(self.path)
|
morphbr@471
|
91 |
pin.close()
|
morphbr@471
|
92 |
|
morphbr@471
|
93 |
fifo = open(self.fifo)
|
morphbr@471
|
94 |
|
morphbr@471
|
95 |
self.child_pid = os.fork()
|
morphbr@475
|
96 |
|
morphbr@471
|
97 |
if (self.child_pid == 0):
|
morphbr@471
|
98 |
conn,addr= self.socket.accept()
|
morphbr@471
|
99 |
lib.log("Sending Data to client: %s" % addr[0])
|
morphbr@471
|
100 |
|
morphbr@473
|
101 |
data = fifo.read(1024)
|
morphbr@471
|
102 |
conn.settimeout(5)
|
morphbr@471
|
103 |
retry = 0
|
morphbr@471
|
104 |
|
morphbr@475
|
105 |
if (self.mplayer):
|
morphbr@475
|
106 |
msg = "%s 1>/dev/null 2>/dev/null" % self.filename
|
morphbr@475
|
107 |
if (self.kind == "dvd"):
|
morphbr@475
|
108 |
msg = "dvd://" + msg
|
morphbr@475
|
109 |
|
morphbr@475
|
110 |
self.mplayer += " " + msg
|
morphbr@475
|
111 |
mplayer_in, self.mplayer_out = os.popen2(self.mplayer)
|
morphbr@475
|
112 |
|
morphbr@471
|
113 |
while( data != "" and retry < 5):
|
morphbr@471
|
114 |
try:
|
morphbr@471
|
115 |
conn.send(data)
|
morphbr@471
|
116 |
except socket.error:
|
morphbr@471
|
117 |
lib.log("Socket error (maybe timeout ?)")
|
morphbr@471
|
118 |
retry += 1
|
morphbr@471
|
119 |
|
morphbr@473
|
120 |
data = fifo.read(1024)
|
morphbr@471
|
121 |
|
morphbr@471
|
122 |
if (retry < 5):
|
morphbr@471
|
123 |
lib.log("Finished sending Data to client: %s" % addr[0])
|
morphbr@471
|
124 |
else:
|
morphbr@471
|
125 |
lib.log("Client timed out")
|
morphbr@471
|
126 |
|
morphbr@471
|
127 |
|
morphbr@471
|
128 |
def stop(self):
|
morphbr@475
|
129 |
try:
|
morphbr@475
|
130 |
self.pout.close()
|
morphbr@475
|
131 |
self.mplayer_out.close()
|
morphbr@475
|
132 |
except:
|
morphbr@475
|
133 |
lib.log("Trying to stop before playing...")
|
morphbr@471
|
134 |
|
morphbr@475
|
135 |
self.mplayer = None
|
morphbr@474
|
136 |
|
morphbr@471
|
137 |
if (self.socket != None):
|
morphbr@471
|
138 |
lib.log("Closing socket")
|
morphbr@471
|
139 |
self.socket.close()
|
morphbr@471
|
140 |
|
morphbr@471
|
141 |
lib.log("Trying to stop Mencoder process")
|
morphbr@471
|
142 |
if (self.child_pid != None):
|
morphbr@471
|
143 |
os.kill(self.child_pid, 9)
|