10 from subprocess import *
14 def __init__(self, config):
21 self.mencoder_pid = None
22 self.mplayer_pid = None
23 signal.signal(signal.SIGABRT, self.kill_handler)
25 def kill_handler(self, sig, frame):
27 os.kill(self.mplayer_pid.pid + 1, signal.SIGKILL)
30 lib.log("Problems closing child")
32 def set_args(self, options):
36 if (opt == "file" or opt == "dvd"):
37 if (self.acodec == "mp3lame"):
38 audio = "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate
40 audio = "-oac lavc -lavcopts acodec=%s abitrate=%s" % (\
41 self.acodec, self.abitrate)
45 self.args += " %s -mf fps=%s -of %s %s"\
46 " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -vf scale=%s:%s"\
47 " -o %s 1> /dev/null 2> /dev/null" % (
48 self.filename, self.fps, self.mux, audio, self.vcodec,
49 self.vbitrate, self.width, self.height, self.fifo)
53 self.args += " dvd://%s -alang %s -vf scale=%s:%s %s"\
54 " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -o %s"\
55 " -ofps %s 1> /dev/null 2> /dev/null" % (
56 self.filename, self.language, self.width, self.height, audio,
57 self.mux, self.vcodec, self.vbitrate, self.fifo, self.fps)
59 elif (opt == "local"):
60 self.mplayer = os.popen("which mplayer").read().strip()
62 elif (opt.find("language=") >= 0):
64 self.language = opt.split("=")[1]
66 lib.log("Bad language option")
69 def run_mplayer(self):
70 msg = "%s 1>/dev/null 2>/dev/null" % self.filename
71 if (self.kind == "dvd"):
74 self.mplayer += " " + msg
75 self.mplayer_pid = Popen(self.mplayer, shell=True)
77 def setup(self, filename, mux, vcodec, vbitrate,\
78 fps, acodec, abitrate, width, height, port, options):
80 self.filename = filename
83 self.vbitrate = vbitrate
86 self.abitrate = abitrate
91 self.fifo = self.config.get("Mencoder", "fifo_path")
95 self.set_args(options)
97 if (self.kind == "file" and not os.path.exists(self.filename)):
98 msg = "File requested does not exist. SETUP failed."
102 # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
103 self.path = self.config.get("Mencoder", "path")
105 if (self.socket != None):
108 self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
109 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
110 self.socket.bind( ('', self.port) )
111 self.socket.listen(1)
121 lib.log("Fifo already exists")
123 lib.log("Starting Mencoder: %s %s" % (self.path, self.args) )
125 self.mencoder_pid = Popen(self.path + self.args, shell=True)
127 fifo = open(self.fifo)
129 self.child_pid = os.fork()
131 if (self.child_pid == 0):
132 conn,addr= self.socket.accept()
133 lib.log("Sending Data to client: %s" % addr[0])
135 data = fifo.read(1024)
139 while( data != "" and retry < 5):
142 r, w, x = select([conn], [], [], 0)
144 back = conn.recv(1024)
145 if (back == "OK" and self.mplayer and not self.mplayer_pid):
149 lib.log("Socket error (maybe timeout ?)")
152 data = fifo.read(1024)
155 lib.log("Finished sending Data to client: %s" % addr[0])
157 lib.log("Client timed out")
164 os.kill(self.mencoder_pid.pid + 1, signal.SIGKILL)
167 lib.log("Trying to stop before playing...")
169 if (self.socket != None):
170 lib.log("Closing socket")
173 lib.log("Trying to stop Mencoder process")
174 if (self.child_pid != None):
175 os.kill(self.child_pid, signal.SIGABRT)