morphbr@471: import os morphbr@471: import sys morphbr@471: import lib morphbr@471: import time morphbr@471: import socket morphbr@471: import ConfigParser morphbr@471: morphbr@471: class Media: morphbr@471: morphbr@471: def __init__(self, config): morphbr@471: morphbr@471: self.config = config morphbr@471: self.socket = None morphbr@471: self.child_pid = None morphbr@475: self.mplayer = None morphbr@475: morphbr@475: def set_args(self, options): morphbr@475: morphbr@475: for opt in options: morphbr@475: morphbr@475: if (opt == "file"): morphbr@475: self.kind = "file" morphbr@475: self.args += " %s -mf fps=%d -of %s -oac %s -lameopts vol=5 cbr:br=%d"\ morphbr@475: " -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\ morphbr@475: " -o %s 1> /dev/null 2> /dev/null" % ( morphbr@475: self.filename, self.fps, self.mux, self.acodec, self.abitrate, morphbr@475: self.vcodec, self.vbitrate, self.width, self.height, self.fifo) morphbr@475: morphbr@475: elif (opt == "dvd"): morphbr@475: self.kind = "dvd" morphbr@475: self.args += " dvd://%s -vf scale=%d:%d -oac %s -lameopts vol=5 cbr:br=%d"\ morphbr@475: " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -o %s"\ morphbr@475: " -mf fps=%d 1> /dev/null 2> /dev/null" % ( morphbr@475: self.filename, self.width, self.height, self.acodec, self.abitrate, morphbr@475: self.mux, self.vcodec, self.vbitrate, self.fifo, self.fps) morphbr@475: morphbr@475: elif (opt == "local"): morphbr@475: self.mplayer = os.popen("which mplayer").read().strip() morphbr@471: morphbr@471: def setup(self, filename, mux, vcodec, vbitrate,\ morphbr@475: fps, acodec, abitrate, width, height, port, options): morphbr@471: morphbr@475: self.filename = filename morphbr@475: self.mux = mux morphbr@475: self.vcodec = vcodec morphbr@475: self.vbitrate = int(vbitrate) morphbr@475: self.fps = int(fps) morphbr@475: self.acodec = acodec morphbr@475: self.abitrate = int(abitrate) morphbr@475: self.width = int(width) morphbr@475: self.height = int(height) morphbr@471: morphbr@475: self.port = int(port) morphbr@475: self.fifo = self.config.get("Mencoder", "fifo_path") morphbr@474: morphbr@475: self.args = "" morphbr@475: self.kind = "" morphbr@475: self.set_args(options) morphbr@474: morphbr@475: if (self.kind == "file" and not os.path.exists(self.filename)): morphbr@475: msg = "File requested does not exist. SETUP failed." morphbr@475: lib.log(msg) morphbr@475: return msg morphbr@474: morphbr@475: # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000 morphbr@475: #mencoder dvd://2 -vf scale=640:480 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4 morphbr@475: self.path = self.config.get("Mencoder", "path") morphbr@475: self.path += self.args morphbr@474: morphbr@475: if (self.socket != None): morphbr@475: del(self.socket) morphbr@474: morphbr@475: self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) morphbr@475: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) morphbr@475: self.socket.bind( ('', self.port) ) morphbr@475: self.socket.listen(1) morphbr@474: morphbr@475: return 0 morphbr@474: morphbr@474: morphbr@474: def play(self): morphbr@471: morphbr@471: try: morphbr@471: os.mkfifo(self.fifo) morphbr@471: except: morphbr@471: lib.log("Fifo already exists") morphbr@471: morphbr@471: lib.log("Starting Mencoder: %s" % self.path ) morphbr@471: # exec Mencoder morphbr@474: pin, self.pout = os.popen2(self.path) morphbr@471: pin.close() morphbr@471: morphbr@471: fifo = open(self.fifo) morphbr@471: morphbr@471: self.child_pid = os.fork() morphbr@475: morphbr@471: if (self.child_pid == 0): morphbr@471: conn,addr= self.socket.accept() morphbr@471: lib.log("Sending Data to client: %s" % addr[0]) morphbr@471: morphbr@473: data = fifo.read(1024) morphbr@471: conn.settimeout(5) morphbr@471: retry = 0 morphbr@471: morphbr@475: if (self.mplayer): morphbr@475: msg = "%s 1>/dev/null 2>/dev/null" % self.filename morphbr@475: if (self.kind == "dvd"): morphbr@475: msg = "dvd://" + msg morphbr@475: morphbr@475: self.mplayer += " " + msg morphbr@475: mplayer_in, self.mplayer_out = os.popen2(self.mplayer) morphbr@475: morphbr@471: while( data != "" and retry < 5): morphbr@471: try: morphbr@471: conn.send(data) morphbr@471: except socket.error: morphbr@471: lib.log("Socket error (maybe timeout ?)") morphbr@471: retry += 1 morphbr@471: morphbr@473: data = fifo.read(1024) morphbr@471: morphbr@471: if (retry < 5): morphbr@471: lib.log("Finished sending Data to client: %s" % addr[0]) morphbr@471: else: morphbr@471: lib.log("Client timed out") morphbr@471: morphbr@471: morphbr@471: def stop(self): morphbr@475: try: morphbr@475: self.pout.close() morphbr@475: self.mplayer_out.close() morphbr@475: except: morphbr@475: lib.log("Trying to stop before playing...") morphbr@471: morphbr@475: self.mplayer = None morphbr@474: morphbr@471: if (self.socket != None): morphbr@471: lib.log("Closing socket") morphbr@471: self.socket.close() morphbr@471: morphbr@471: lib.log("Trying to stop Mencoder process") morphbr@471: if (self.child_pid != None): morphbr@471: os.kill(self.child_pid, 9)