gmyth-stream/plugins/media/mencoder.py
branchtrunk
changeset 471 2eef40885322
child 473 b94790c04fd5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth-stream/plugins/media/mencoder.py	Fri Mar 30 23:07:54 2007 +0100
     1.3 @@ -0,0 +1,97 @@
     1.4 +import os
     1.5 +import sys
     1.6 +import lib
     1.7 +import time
     1.8 +import socket
     1.9 +import ConfigParser
    1.10 +
    1.11 +class Media:
    1.12 +
    1.13 +    def __init__(self, config):
    1.14 +
    1.15 +        self.config = config
    1.16 +        self.socket = None
    1.17 +        self.child_pid = None
    1.18 +
    1.19 +    def setup(self, filename, mux, vcodec, vbitrate,\
    1.20 +              fps, acodec, abitrate, width, height, port):
    1.21 +
    1.22 +        self.filename = filename
    1.23 +        self.mux = mux
    1.24 +        self.vcodec = vcodec
    1.25 +        self.vbitrate = int(vbitrate)
    1.26 +        self.fps = int(fps)
    1.27 +        self.acodec = acodec
    1.28 +        self.abitrate = int(abitrate)
    1.29 +        self.width = int(width)
    1.30 +        self.height = int(height)
    1.31 +
    1.32 +        self.port = int(port)
    1.33 +        self.fifo = self.config.get("Mencoder", "fifo_path")
    1.34 +
    1.35 +        try:
    1.36 +            os.mkfifo(self.fifo)
    1.37 +        except:
    1.38 +            lib.log("Fifo already exists")
    1.39 +
    1.40 +        # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
    1.41 +        self.path = self.config.get("Mencoder", "path")
    1.42 +        self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\
    1.43 +                     " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
    1.44 +                     " -o %s 1> /dev/null 2> /dev/null" % (
    1.45 +            self.filename, self.fps, self.mux, self.acodec, self.abitrate,
    1.46 +            self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
    1.47 +
    1.48 +
    1.49 +        if (self.socket != None):
    1.50 +            del(self.socket)
    1.51 +
    1.52 +        self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    1.53 +        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    1.54 +        self.socket.bind( ('', self.port) )
    1.55 +        self.socket.settimeout(10)
    1.56 +        self.socket.listen(1)
    1.57 +
    1.58 +    def play(self):
    1.59 +
    1.60 +        lib.log("Starting Mencoder: %s" % self.path )
    1.61 +        # exec Mencoder
    1.62 +        pin, pout = os.popen2(self.path)
    1.63 +        pin.close()
    1.64 +
    1.65 +        fifo = open(self.fifo)
    1.66 +
    1.67 +        self.child_pid = os.fork()
    1.68 +        if (self.child_pid == 0):
    1.69 +            conn,addr= self.socket.accept()
    1.70 +            lib.log("Sending Data to client: %s" % addr[0])
    1.71 +
    1.72 +            data = fifo.read(4096)
    1.73 +            conn.settimeout(5)
    1.74 +            retry = 0
    1.75 +
    1.76 +            while( data != "" and retry < 5):
    1.77 +                try:
    1.78 +                    conn.send(data)
    1.79 +                except socket.error:
    1.80 +                    lib.log("Socket error (maybe timeout ?)")
    1.81 +                    retry += 1
    1.82 +
    1.83 +                data = fifo.read(4096)
    1.84 +
    1.85 +            if (retry < 5):
    1.86 +                lib.log("Finished sending Data to client: %s" % addr[0])
    1.87 +            else:
    1.88 +                lib.log("Client timed out")
    1.89 +
    1.90 +        pout.close()
    1.91 +
    1.92 +    def stop(self):
    1.93 +
    1.94 +        if (self.socket != None):
    1.95 +            lib.log("Closing socket")
    1.96 +            self.socket.close()
    1.97 +
    1.98 +            lib.log("Trying to stop Mencoder process")
    1.99 +            if (self.child_pid != None):
   1.100 +                os.kill(self.child_pid, 9)