gmyth-stream/plugins/media/mencoder.py
author morphbr
Fri Mar 30 23:12:52 2007 +0100 (2007-03-30)
branchtrunk
changeset 473 b94790c04fd5
parent 471 2eef40885322
child 474 07c12d2ceddf
permissions -rw-r--r--
[svn r478] * GMyth-Stream:
- Fixed bug regarding buffer length
     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         self.filename = filename
    20         self.mux = mux
    21         self.vcodec = vcodec
    22         self.vbitrate = int(vbitrate)
    23         self.fps = int(fps)
    24         self.acodec = acodec
    25         self.abitrate = int(abitrate)
    26         self.width = int(width)
    27         self.height = int(height)
    28 
    29         self.port = int(port)
    30         self.fifo = self.config.get("Mencoder", "fifo_path")
    31 
    32         try:
    33             os.mkfifo(self.fifo)
    34         except:
    35             lib.log("Fifo already exists")
    36 
    37         # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
    38         self.path = self.config.get("Mencoder", "path")
    39         self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\
    40                      " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
    41                      " -o %s 1> /dev/null 2> /dev/null" % (
    42             self.filename, self.fps, self.mux, self.acodec, self.abitrate,
    43             self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
    44 
    45 
    46         if (self.socket != None):
    47             del(self.socket)
    48 
    49         self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    50         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    51         self.socket.bind( ('', self.port) )
    52         self.socket.listen(1)
    53 
    54     def play(self):
    55 
    56         lib.log("Starting Mencoder: %s" % self.path )
    57         # exec Mencoder
    58         pin, pout = os.popen2(self.path)
    59         pin.close()
    60 
    61         fifo = open(self.fifo)
    62 
    63         self.child_pid = os.fork()
    64         if (self.child_pid == 0):
    65             conn,addr= self.socket.accept()
    66             lib.log("Sending Data to client: %s" % addr[0])
    67 
    68             data = fifo.read(1024)
    69             conn.settimeout(5)
    70             retry = 0
    71 
    72             while( data != "" and retry < 5):
    73                 try:
    74                     conn.send(data)
    75                 except socket.error:
    76                     lib.log("Socket error (maybe timeout ?)")
    77                     retry += 1
    78 
    79                 data = fifo.read(1024)
    80 
    81             if (retry < 5):
    82                 lib.log("Finished sending Data to client: %s" % addr[0])
    83             else:
    84                 lib.log("Client timed out")
    85 
    86         pout.close()
    87 
    88     def stop(self):
    89 
    90         if (self.socket != None):
    91             lib.log("Closing socket")
    92             self.socket.close()
    93 
    94             lib.log("Trying to stop Mencoder process")
    95             if (self.child_pid != None):
    96                 os.kill(self.child_pid, 9)