diff -r 000000000000 -r 2eef40885322 gmyth-stream/plugins/media/mencoder.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gmyth-stream/plugins/media/mencoder.py Fri Mar 30 23:07:54 2007 +0100 @@ -0,0 +1,97 @@ +import os +import sys +import lib +import time +import socket +import ConfigParser + +class Media: + + def __init__(self, config): + + self.config = config + self.socket = None + self.child_pid = None + + def setup(self, filename, mux, vcodec, vbitrate,\ + fps, acodec, abitrate, width, height, port): + + self.filename = filename + self.mux = mux + self.vcodec = vcodec + self.vbitrate = int(vbitrate) + self.fps = int(fps) + self.acodec = acodec + self.abitrate = int(abitrate) + self.width = int(width) + self.height = int(height) + + self.port = int(port) + self.fifo = self.config.get("Mencoder", "fifo_path") + + try: + os.mkfifo(self.fifo) + except: + lib.log("Fifo already exists") + + # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000 + self.path = self.config.get("Mencoder", "path") + self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\ + " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\ + " -o %s 1> /dev/null 2> /dev/null" % ( + self.filename, self.fps, self.mux, self.acodec, self.abitrate, + self.vcodec, self.vbitrate, self.width, self.height, self.fifo) + + + if (self.socket != None): + del(self.socket) + + self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.socket.bind( ('', self.port) ) + self.socket.settimeout(10) + self.socket.listen(1) + + def play(self): + + lib.log("Starting Mencoder: %s" % self.path ) + # exec Mencoder + pin, pout = os.popen2(self.path) + pin.close() + + fifo = open(self.fifo) + + self.child_pid = os.fork() + if (self.child_pid == 0): + conn,addr= self.socket.accept() + lib.log("Sending Data to client: %s" % addr[0]) + + data = fifo.read(4096) + conn.settimeout(5) + retry = 0 + + while( data != "" and retry < 5): + try: + conn.send(data) + except socket.error: + lib.log("Socket error (maybe timeout ?)") + retry += 1 + + data = fifo.read(4096) + + if (retry < 5): + lib.log("Finished sending Data to client: %s" % addr[0]) + else: + lib.log("Client timed out") + + pout.close() + + def stop(self): + + if (self.socket != None): + lib.log("Closing socket") + self.socket.close() + + lib.log("Trying to stop Mencoder process") + if (self.child_pid != None): + os.kill(self.child_pid, 9)