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