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@471: morphbr@471: def setup(self, filename, mux, vcodec, vbitrate,\ morphbr@471: fps, acodec, abitrate, width, height, port): morphbr@471: morphbr@474: if (os.path.exists(filename)): morphbr@471: morphbr@474: self.filename = filename morphbr@474: self.mux = mux morphbr@474: self.vcodec = vcodec morphbr@474: self.vbitrate = int(vbitrate) morphbr@474: self.fps = int(fps) morphbr@474: self.acodec = acodec morphbr@474: self.abitrate = int(abitrate) morphbr@474: self.width = int(width) morphbr@474: self.height = int(height) morphbr@474: morphbr@474: self.port = int(port) morphbr@474: self.fifo = self.config.get("Mencoder", "fifo_path") morphbr@474: morphbr@474: # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000 morphbr@474: self.path = self.config.get("Mencoder", "path") morphbr@474: self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\ morphbr@474: " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\ morphbr@474: " -o %s 1> /dev/null 2> /dev/null" % ( morphbr@474: self.filename, self.fps, self.mux, self.acodec, self.abitrate, morphbr@474: self.vcodec, self.vbitrate, self.width, self.height, self.fifo) morphbr@474: morphbr@474: morphbr@474: if (self.socket != None): morphbr@474: del(self.socket) morphbr@474: morphbr@474: self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) morphbr@474: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) morphbr@474: self.socket.bind( ('', self.port) ) morphbr@474: self.socket.listen(1) morphbr@474: morphbr@474: return 0 morphbr@474: morphbr@474: else: morphbr@474: lib.log("File requested does not exist. SETUP failed.") morphbr@474: return -1 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@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@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@471: morphbr@474: self.pout.close() 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)