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