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@471: self.filename = filename morphbr@471: self.mux = mux morphbr@471: self.vcodec = vcodec morphbr@471: self.vbitrate = int(vbitrate) morphbr@471: self.fps = int(fps) morphbr@471: self.acodec = acodec morphbr@471: self.abitrate = int(abitrate) morphbr@471: self.width = int(width) morphbr@471: self.height = int(height) morphbr@471: morphbr@471: self.port = int(port) morphbr@471: self.fifo = self.config.get("Mencoder", "fifo_path") 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: # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000 morphbr@471: self.path = self.config.get("Mencoder", "path") morphbr@471: self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\ morphbr@471: " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\ morphbr@471: " -o %s 1> /dev/null 2> /dev/null" % ( morphbr@471: self.filename, self.fps, self.mux, self.acodec, self.abitrate, morphbr@471: self.vcodec, self.vbitrate, self.width, self.height, self.fifo) morphbr@471: morphbr@471: morphbr@471: if (self.socket != None): morphbr@471: del(self.socket) morphbr@471: morphbr@471: self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) morphbr@471: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) morphbr@471: self.socket.bind( ('', self.port) ) morphbr@471: self.socket.settimeout(10) morphbr@471: self.socket.listen(1) morphbr@471: morphbr@471: def play(self): morphbr@471: morphbr@471: lib.log("Starting Mencoder: %s" % self.path ) morphbr@471: # exec Mencoder morphbr@471: pin, 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@471: data = fifo.read(4096) 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@471: data = fifo.read(4096) 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: pout.close() morphbr@471: morphbr@471: def stop(self): 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@471: os.kill(self.child_pid, 9)