morphbr@471: import os morphbr@471: import sys morphbr@471: import lib morphbr@471: import time morphbr@478: import signal morphbr@471: import socket morphbr@471: import ConfigParser morphbr@471: morphbr@478: from select import * morphbr@478: from subprocess import * morphbr@478: morphbr@471: class Media: morphbr@471: morphbr@471: def __init__(self, config): morphbr@471: morphbr@471: self.config = config morphbr@478: self.language = "en" morphbr@471: self.socket = None morphbr@471: self.child_pid = None morphbr@475: self.mplayer = None morphbr@478: self.mencoder_pid = None morphbr@478: self.mplayer_pid = None morphbr@478: signal.signal(signal.SIGABRT, self.kill_handler) morphbr@478: morphbr@478: def kill_handler(self, sig, frame): morphbr@478: try: morphbr@478: os.kill(self.mplayer_pid.pid + 1, signal.SIGKILL) morphbr@478: sys.exit(0) morphbr@478: except: morphbr@478: lib.log("Problems closing child") morphbr@475: morphbr@475: def set_args(self, options): morphbr@475: morphbr@475: for opt in options: morphbr@475: morphbr@478: if (opt == "file" or opt == "dvd"): morphbr@478: if (self.acodec == "mp3lame"): morphbr@478: audio = "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate morphbr@478: else: morphbr@478: audio = "-oac lavc -lavcopts acodec=%s abitrate=%s" % (\ morphbr@478: self.acodec, self.abitrate) morphbr@478: morphbr@475: if (opt == "file"): morphbr@475: self.kind = "file" morphbr@478: self.args += " %s -mf fps=%s -of %s %s"\ morphbr@478: " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -vf scale=%s:%s"\ morphbr@475: " -o %s 1> /dev/null 2> /dev/null" % ( morphbr@478: self.filename, self.fps, self.mux, audio, self.vcodec, morphbr@478: self.vbitrate, self.width, self.height, self.fifo) morphbr@475: morphbr@475: elif (opt == "dvd"): morphbr@475: self.kind = "dvd" morphbr@478: self.args += " dvd://%s -alang %s -vf scale=%s:%s %s"\ morphbr@478: " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -o %s"\ morphbr@478: " -ofps %s 1> /dev/null 2> /dev/null" % ( morphbr@478: self.filename, self.language, self.width, self.height, audio, 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@478: elif (opt.find("language=") >= 0): morphbr@478: try: morphbr@478: self.language = opt.split("=")[1] morphbr@478: except: morphbr@478: lib.log("Bad language option") morphbr@478: morphbr@478: morphbr@478: def run_mplayer(self): morphbr@478: msg = "%s 1>/dev/null 2>/dev/null" % self.filename morphbr@478: if (self.kind == "dvd"): morphbr@478: msg = "dvd://" + msg morphbr@478: morphbr@478: self.mplayer += " " + msg morphbr@478: self.mplayer_pid = Popen(self.mplayer, shell=True) morphbr@478: 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@478: self.vbitrate = vbitrate morphbr@478: self.fps = fps morphbr@475: self.acodec = acodec morphbr@478: self.abitrate = abitrate morphbr@478: self.width = width morphbr@478: self.height = 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: self.path = self.config.get("Mencoder", "path") 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@478: lib.log("Starting Mencoder: %s %s" % (self.path, self.args) ) morphbr@471: # exec Mencoder morphbr@478: self.mencoder_pid = Popen(self.path + self.args, shell=True) 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@471: while( data != "" and retry < 5): morphbr@471: try: morphbr@471: conn.send(data) morphbr@478: r, w, x = select([conn], [], [], 0) morphbr@478: if conn in r: morphbr@478: back = conn.recv(1024) morphbr@478: if (back == "OK" and self.mplayer and not self.mplayer_pid): morphbr@478: self.run_mplayer() morphbr@478: 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@478: sys.exit(0) morphbr@478: morphbr@471: morphbr@471: def stop(self): morphbr@475: try: morphbr@478: os.kill(self.mencoder_pid.pid + 1, signal.SIGKILL) morphbr@478: self.mplayer = None morphbr@475: except: morphbr@475: lib.log("Trying to stop before playing...") morphbr@471: 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@478: os.kill(self.child_pid, signal.SIGABRT)