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