gmyth-stream/plugins/media/mencoder.py
author morphbr
Sat Mar 31 00:55:28 2007 +0100 (2007-03-31)
branchtrunk
changeset 474 07c12d2ceddf
parent 473 b94790c04fd5
child 475 2521ec986005
permissions -rw-r--r--
[svn r479] * GMyth-Stream:
- Error handling: file does not exist on setup (mencoder)
     1 import os
     2 import sys
     3 import lib
     4 import time
     5 import socket
     6 import ConfigParser
     7 
     8 class Media:
     9 
    10     def __init__(self, config):
    11 
    12         self.config = config
    13         self.socket = None
    14         self.child_pid = None
    15 
    16     def setup(self, filename, mux, vcodec, vbitrate,\
    17               fps, acodec, abitrate, width, height, port):
    18 
    19         if (os.path.exists(filename)):
    20 
    21             self.filename = filename
    22             self.mux = mux
    23             self.vcodec = vcodec
    24             self.vbitrate = int(vbitrate)
    25             self.fps = int(fps)
    26             self.acodec = acodec
    27             self.abitrate = int(abitrate)
    28             self.width = int(width)
    29             self.height = int(height)
    30 
    31             self.port = int(port)
    32             self.fifo = self.config.get("Mencoder", "fifo_path")
    33 
    34             # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
    35             self.path = self.config.get("Mencoder", "path")
    36             self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\
    37                          " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
    38                          " -o %s 1> /dev/null 2> /dev/null" % (
    39                 self.filename, self.fps, self.mux, self.acodec, self.abitrate,
    40                 self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
    41 
    42 
    43             if (self.socket != None):
    44                 del(self.socket)
    45 
    46             self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    47             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    48             self.socket.bind( ('', self.port) )
    49             self.socket.listen(1)
    50 
    51             return 0
    52 
    53         else:
    54             lib.log("File requested does not exist. SETUP failed.")
    55             return -1
    56 
    57     def play(self):
    58 
    59         try:
    60             os.mkfifo(self.fifo)
    61         except:
    62             lib.log("Fifo already exists")
    63 
    64         lib.log("Starting Mencoder: %s" % self.path )
    65         # exec Mencoder
    66         pin, self.pout = os.popen2(self.path)
    67         pin.close()
    68 
    69         fifo = open(self.fifo)
    70 
    71         self.child_pid = os.fork()
    72         if (self.child_pid == 0):
    73             conn,addr= self.socket.accept()
    74             lib.log("Sending Data to client: %s" % addr[0])
    75 
    76             data = fifo.read(1024)
    77             conn.settimeout(5)
    78             retry = 0
    79 
    80             while( data != "" and retry < 5):
    81                 try:
    82                     conn.send(data)
    83                 except socket.error:
    84                     lib.log("Socket error (maybe timeout ?)")
    85                     retry += 1
    86 
    87                 data = fifo.read(1024)
    88 
    89             if (retry < 5):
    90                 lib.log("Finished sending Data to client: %s" % addr[0])
    91             else:
    92                 lib.log("Client timed out")
    93 
    94 
    95     def stop(self):
    96 
    97         self.pout.close()
    98 
    99         if (self.socket != None):
   100             lib.log("Closing socket")
   101             self.socket.close()
   102 
   103             lib.log("Trying to stop Mencoder process")
   104             if (self.child_pid != None):
   105                 os.kill(self.child_pid, 9)