gmyth-stream/server/0.1/plugins/media/ffmpeg.py
author renatofilho
Fri May 04 23:03:05 2007 +0100 (2007-05-04)
branchtrunk
changeset 629 7ad2e23504e6
permissions -rw-r--r--
[svn r635] fixed makefile
morphbr@565
     1
import os
morphbr@565
     2
import sys
morphbr@565
     3
import lib
morphbr@565
     4
import time
morphbr@565
     5
import socket
morphbr@565
     6
import ConfigParser
morphbr@565
     7
morphbr@565
     8
class Media:
morphbr@565
     9
morphbr@565
    10
    def __init__(self, config):
morphbr@565
    11
morphbr@565
    12
        self.config = config
morphbr@565
    13
        self.socket = None
morphbr@565
    14
        self.child_pid = None
morphbr@565
    15
morphbr@565
    16
    def setup(self, filename, mux, vcodec, vbitrate,\
morphbr@565
    17
              fps, acodec, abitrate, width, height, port):
morphbr@565
    18
morphbr@565
    19
        self.filename = filename
morphbr@565
    20
        self.mux = mux
morphbr@565
    21
        self.vcodec = vcodec
morphbr@565
    22
        self.vbitrate = int(vbitrate)
morphbr@565
    23
        self.fps = int(fps)
morphbr@565
    24
        self.acodec = acodec
morphbr@565
    25
        self.abitrate = int(abitrate)
morphbr@565
    26
        self.width = int(width)
morphbr@565
    27
        self.height = int(height)
morphbr@565
    28
morphbr@565
    29
        self.port = int(port)
morphbr@565
    30
morphbr@565
    31
        # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000
morphbr@565
    32
        self.path = self.config.get("FFmpeg", "path")
morphbr@565
    33
        self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % (
morphbr@565
    34
            self.filename, self.mux, self.vcodec, self.vbitrate,\
morphbr@565
    35
            self.fps, self.acodec, self.abitrate, self.width, self.height)
morphbr@565
    36
morphbr@565
    37
        if (self.socket != None):
morphbr@565
    38
            del(self.socket)
morphbr@565
    39
morphbr@565
    40
        self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
morphbr@565
    41
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
morphbr@565
    42
        self.socket.bind( ('', self.port) )
morphbr@565
    43
        self.socket.settimeout(10)
morphbr@565
    44
        self.socket.listen(1)
morphbr@565
    45
morphbr@565
    46
    def play(self):
morphbr@565
    47
morphbr@565
    48
        lib.log("Starting FFmpeg: %s" % self.path)
morphbr@565
    49
morphbr@565
    50
        # exec FFmpeg and get stdout
morphbr@565
    51
        child_stdin, child_stdout = os.popen2(self.path)
morphbr@565
    52
        child_stdin.close()
morphbr@565
    53
morphbr@565
    54
        self.child_pid = os.fork()
morphbr@565
    55
        if (self.child_pid == 0):
morphbr@565
    56
            #child
morphbr@565
    57
morphbr@565
    58
            conn,addr= self.socket.accept()
morphbr@565
    59
            lib.log("Sending Data to client: %s" % addr[0])
morphbr@565
    60
            data = child_stdout.read(1024)
morphbr@565
    61
            conn.settimeout(5)
morphbr@565
    62
            retry = 0
morphbr@565
    63
morphbr@565
    64
            while( data != "" and retry < 5):
morphbr@565
    65
                try:
morphbr@565
    66
                    conn.send(data)
morphbr@565
    67
                except socket.error:
morphbr@565
    68
                    lib.log("Socket error (maybe timeout ?)")
morphbr@565
    69
                    retry = retry + 1
morphbr@565
    70
morphbr@565
    71
                data = child_stdout.read(1024)
morphbr@565
    72
morphbr@565
    73
            if (retry < 5):
morphbr@565
    74
                lib.log("Finished sending Data to client: %s" % addr[0])
morphbr@565
    75
            else:
morphbr@565
    76
                lib.log("Client timed out")
morphbr@565
    77
morphbr@565
    78
            child_stdout.close()
morphbr@565
    79
            #conn.close()
morphbr@565
    80
            #sys.exit()
morphbr@565
    81
morphbr@565
    82
morphbr@565
    83
    def stop(self):
morphbr@565
    84
morphbr@565
    85
        if (self.socket != None):
morphbr@565
    86
            lib.log("Closing socket")
morphbr@565
    87
            self.socket.close()
morphbr@565
    88
morphbr@565
    89
        lib.log("Trying to stop FFmpeg process")
morphbr@565
    90
        if (self.child_pid != None):
morphbr@565
    91
            os.kill(self.child_pid, 9)