gmyth-stream/plugins/media/mencoder.py
author renatofilho
Mon Apr 02 16:57:57 2007 +0100 (2007-04-02)
branchtrunk
changeset 477 88bf59e8da5f
parent 474 07c12d2ceddf
child 478 d220053366be
permissions -rw-r--r--
[svn r482] dont make seek for push mode
     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         self.mplayer = None
    16 
    17     def set_args(self, options):
    18 
    19         for opt in options:
    20 
    21             if (opt == "file"):
    22                 self.kind = "file"
    23                 self.args += " %s -mf fps=%d -of %s -oac %s -lameopts vol=5 cbr:br=%d"\
    24                              " -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
    25                              " -o %s 1> /dev/null 2> /dev/null" % (
    26                     self.filename, self.fps, self.mux, self.acodec, self.abitrate,
    27                     self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
    28 
    29             elif (opt == "dvd"):
    30                 self.kind = "dvd"
    31                 self.args += " dvd://%s -vf scale=%d:%d -oac %s -lameopts vol=5 cbr:br=%d"\
    32                              " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -o %s"\
    33                              " -mf fps=%d 1> /dev/null 2> /dev/null" % (
    34                     self.filename, self.width, self.height, self.acodec, self.abitrate,
    35                     self.mux, self.vcodec, self.vbitrate, self.fifo, self.fps)
    36 
    37             elif (opt == "local"):
    38                 self.mplayer = os.popen("which mplayer").read().strip()
    39 
    40     def setup(self, filename, mux, vcodec, vbitrate,\
    41               fps, acodec, abitrate, width, height, port, options):
    42 
    43         self.filename = filename
    44         self.mux = mux
    45         self.vcodec = vcodec
    46         self.vbitrate = int(vbitrate)
    47         self.fps = int(fps)
    48         self.acodec = acodec
    49         self.abitrate = int(abitrate)
    50         self.width = int(width)
    51         self.height = int(height)
    52 
    53         self.port = int(port)
    54         self.fifo = self.config.get("Mencoder", "fifo_path")
    55 
    56         self.args = ""
    57         self.kind = ""
    58         self.set_args(options)
    59 
    60         if (self.kind == "file" and not os.path.exists(self.filename)):
    61             msg = "File requested does not exist. SETUP failed."
    62             lib.log(msg)
    63             return msg
    64 
    65         # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
    66         #mencoder dvd://2 -vf scale=640:480 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4
    67         self.path = self.config.get("Mencoder", "path")
    68         self.path += self.args
    69 
    70         if (self.socket != None):
    71             del(self.socket)
    72 
    73         self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    74         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    75         self.socket.bind( ('', self.port) )
    76         self.socket.listen(1)
    77 
    78         return 0
    79 
    80 
    81     def play(self):
    82 
    83         try:
    84             os.mkfifo(self.fifo)
    85         except:
    86             lib.log("Fifo already exists")
    87 
    88         lib.log("Starting Mencoder: %s" % self.path )
    89         # exec Mencoder
    90         pin, self.pout = os.popen2(self.path)
    91         pin.close()
    92 
    93         fifo = open(self.fifo)
    94 
    95         self.child_pid = os.fork()
    96 
    97         if (self.child_pid == 0):
    98             conn,addr= self.socket.accept()
    99             lib.log("Sending Data to client: %s" % addr[0])
   100 
   101             data = fifo.read(1024)
   102             conn.settimeout(5)
   103             retry = 0
   104 
   105             if (self.mplayer):
   106                 msg = "%s 1>/dev/null 2>/dev/null" % self.filename
   107                 if (self.kind == "dvd"):
   108                     msg = "dvd://" + msg
   109 
   110                 self.mplayer += " " + msg
   111                 mplayer_in, self.mplayer_out = os.popen2(self.mplayer)
   112 
   113             while( data != "" and retry < 5):
   114                 try:
   115                     conn.send(data)
   116                 except socket.error:
   117                     lib.log("Socket error (maybe timeout ?)")
   118                     retry += 1
   119 
   120                 data = fifo.read(1024)
   121 
   122             if (retry < 5):
   123                 lib.log("Finished sending Data to client: %s" % addr[0])
   124             else:
   125                 lib.log("Client timed out")
   126 
   127 
   128     def stop(self):
   129         try:
   130             self.pout.close()
   131             self.mplayer_out.close()
   132         except:
   133             lib.log("Trying to stop before playing...")
   134 
   135         self.mplayer = None
   136 
   137         if (self.socket != None):
   138             lib.log("Closing socket")
   139             self.socket.close()
   140 
   141             lib.log("Trying to stop Mencoder process")
   142             if (self.child_pid != None):
   143                 os.kill(self.child_pid, 9)