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":
38 if self.acodec == "mp3lame":
39 audio = "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate
41 audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (\
42 self.acodec, self.abitrate)
47 self.args += " %s -mf fps=%s -of %s %s"\
48 " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -vf scale=%s:%s"\
49 " -o %s 1> /dev/null 2> /dev/null" % (
50 self.filename, self.fps, self.mux, audio, self.vcodec,
51 self.vbitrate, self.width, self.height, self.fifo)
55 self.args += " dvd://%s -alang %s -vf scale=%s:%s %s"\
56 " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -o %s"\
57 " -ofps %s 1> /dev/null 2> /dev/null" % (
58 self.filename, self.language, self.width, self.height, audio,
59 self.mux, self.vcodec, self.vbitrate, self.fifo, self.fps)
62 self.mplayer = os.popen("which mplayer").read().strip()
64 elif opt.find("language=") >= 0:
66 self.language = opt.split("=")[1]
68 lib.log("Bad language option")
70 elif opt.find("format=") >= 0:
72 self.mux += " -mpegopts format=%s" % opt.split("=")[1]
74 lib.log("Bad format option")
77 def run_mplayer(self):
78 msg = "%s 1>/dev/null 2>/dev/null" % self.filename
80 if self.kind == "dvd":
83 self.mplayer += " " + msg
84 self.mplayer_pid = Popen(self.mplayer, shell=True)
86 def setup(self, filename, mux, vcodec, vbitrate,\
87 fps, acodec, abitrate, width, height, port, options):
89 self.filename = filename
92 self.vbitrate = vbitrate
95 self.abitrate = abitrate
100 self.fifo = self.config.get("Mencoder", "fifo_path")
104 self.set_args(options)
106 if self.kind == "file" and not os.path.exists(self.filename):
107 msg = "File requested does not exist. SETUP failed."
111 # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000 file
112 #4 mpeg mpeg1video 400 25 mp3lame 192 400 240 5000 language=en local dvd
113 self.path = self.config.get("Mencoder", "path")
115 if self.socket != None:
118 self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
119 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
120 self.socket.bind( ('', self.port) )
121 self.socket.listen(1)
131 lib.log("Fifo already exists")
133 lib.log("Starting Mencoder: %s %s" % (self.path, self.args) )
135 self.mencoder_pid = Popen(self.path + self.args, shell=True)
137 fifo = open(self.fifo)
139 self.child_pid = os.fork()
141 if self.child_pid == 0:
142 conn,addr= self.socket.accept()
143 lib.log("Sending Data to client: %s" % addr[0])
145 data = fifo.read(1024)
149 while data != "" and retry < 5:
152 r, w, x = select([conn], [], [], 0)
154 back = conn.recv(1024)
155 if back == "OK" and self.mplayer and not self.mplayer_pid:
159 lib.log("Socket error (maybe timeout ?)")
162 data = fifo.read(1024)
165 lib.log("Finished sending Data to client: %s" % addr[0])
167 lib.log("Client timed out")
174 os.kill(self.mencoder_pid.pid + 1, signal.SIGKILL)
177 lib.log("Trying to stop before playing...")
179 if self.socket != None:
180 lib.log("Closing socket")
183 lib.log("Trying to stop Mencoder process")
184 if self.child_pid != None:
185 os.kill(self.child_pid, signal.SIGABRT)