morphbr@466: import os morphbr@466: import sys morphbr@470: import lib morphbr@466: import time morphbr@466: import socket morphbr@466: import ConfigParser morphbr@466: morphbr@466: class Media: morphbr@466: morphbr@466: def __init__(self, config): morphbr@466: morphbr@466: self.config = config morphbr@470: self.socket = None morphbr@470: self.child_pid = None morphbr@466: morphbr@466: def setup(self, filename, mux, vcodec, vbitrate,\ morphbr@466: fps, acodec, abitrate, width, height, port): morphbr@466: morphbr@466: self.filename = filename morphbr@466: self.mux = mux morphbr@466: self.vcodec = vcodec morphbr@466: self.vbitrate = int(vbitrate) morphbr@466: self.fps = int(fps) morphbr@466: self.acodec = acodec morphbr@466: self.abitrate = int(abitrate) morphbr@466: self.width = int(width) morphbr@466: self.height = int(height) morphbr@466: morphbr@466: self.port = int(port) morphbr@466: morphbr@466: # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000 morphbr@466: self.path = self.config.get("FFmpeg", "path") morphbr@473: self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % ( morphbr@466: self.filename, self.mux, self.vcodec, self.vbitrate,\ morphbr@466: self.fps, self.acodec, self.abitrate, self.width, self.height) morphbr@466: morphbr@470: if (self.socket != None): morphbr@470: del(self.socket) morphbr@470: morphbr@466: self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) morphbr@471: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) morphbr@466: self.socket.bind( ('', self.port) ) morphbr@470: self.socket.settimeout(10) morphbr@471: self.socket.listen(1) morphbr@466: morphbr@466: def play(self): morphbr@466: morphbr@470: lib.log("Starting FFmpeg: %s" % self.path) morphbr@466: morphbr@466: # exec FFmpeg and get stdout morphbr@466: child_stdin, child_stdout = os.popen2(self.path) morphbr@466: child_stdin.close() morphbr@466: morphbr@466: self.child_pid = os.fork() morphbr@466: if (self.child_pid == 0): morphbr@466: #child morphbr@466: morphbr@466: conn,addr= self.socket.accept() morphbr@470: lib.log("Sending Data to client: %s" % addr[0]) morphbr@473: data = child_stdout.read(1024) morphbr@470: conn.settimeout(5) morphbr@470: retry = 0 morphbr@466: morphbr@470: while( data != "" and retry < 5): morphbr@466: try: morphbr@466: conn.send(data) morphbr@466: except socket.error: morphbr@470: lib.log("Socket error (maybe timeout ?)") morphbr@470: retry = retry + 1 morphbr@470: morphbr@466: data = child_stdout.read(1024) morphbr@466: morphbr@470: if (retry < 5): morphbr@470: lib.log("Finished sending Data to client: %s" % addr[0]) morphbr@470: else: morphbr@470: lib.log("Client timed out") morphbr@470: morphbr@470: child_stdout.close() morphbr@470: #conn.close() morphbr@470: #sys.exit() morphbr@466: morphbr@466: morphbr@466: def stop(self): morphbr@466: morphbr@471: if (self.socket != None): morphbr@471: lib.log("Closing socket") morphbr@471: self.socket.close() morphbr@466: morphbr@470: lib.log("Trying to stop FFmpeg process") morphbr@470: if (self.child_pid != None): morphbr@470: os.kill(self.child_pid, 9)