gmyth-stream/server/plugins/media/ffmpeg.py
author rosfran
Fri Apr 13 22:40:49 2007 +0100 (2007-04-13)
branchtrunk
changeset 550 aaf53e3d0b26
permissions -rw-r--r--
[svn r555] Fixes the _dispose handler, and adds the GMythFile instances.
     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 
    31         # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000
    32         self.path = self.config.get("FFmpeg", "path")
    33         self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % (
    34             self.filename, self.mux, self.vcodec, self.vbitrate,\
    35             self.fps, self.acodec, self.abitrate, self.width, self.height)
    36 
    37         if (self.socket != None):
    38             del(self.socket)
    39 
    40         self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    41         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    42         self.socket.bind( ('', self.port) )
    43         self.socket.settimeout(10)
    44         self.socket.listen(1)
    45 
    46     def play(self):
    47 
    48         lib.log("Starting FFmpeg: %s" % self.path)
    49 
    50         # exec FFmpeg and get stdout
    51         child_stdin, child_stdout = os.popen2(self.path)
    52         child_stdin.close()
    53 
    54         self.child_pid = os.fork()
    55         if (self.child_pid == 0):
    56             #child
    57 
    58             conn,addr= self.socket.accept()
    59             lib.log("Sending Data to client: %s" % addr[0])
    60             data = child_stdout.read(1024)
    61             conn.settimeout(5)
    62             retry = 0
    63 
    64             while( data != "" and retry < 5):
    65                 try:
    66                     conn.send(data)
    67                 except socket.error:
    68                     lib.log("Socket error (maybe timeout ?)")
    69                     retry = retry + 1
    70 
    71                 data = child_stdout.read(1024)
    72 
    73             if (retry < 5):
    74                 lib.log("Finished sending Data to client: %s" % addr[0])
    75             else:
    76                 lib.log("Client timed out")
    77 
    78             child_stdout.close()
    79             #conn.close()
    80             #sys.exit()
    81 
    82 
    83     def stop(self):
    84 
    85         if (self.socket != None):
    86             lib.log("Closing socket")
    87             self.socket.close()
    88 
    89         lib.log("Trying to stop FFmpeg process")
    90         if (self.child_pid != None):
    91             os.kill(self.child_pid, 9)