gmyth-stream/plugins/media/mencoder.py
author morphbr
Sat Mar 31 00:55:28 2007 +0100 (2007-03-31)
branchtrunk
changeset 474 07c12d2ceddf
parent 473 b94790c04fd5
child 475 2521ec986005
permissions -rw-r--r--
[svn r479] * GMyth-Stream:
- Error handling: file does not exist on setup (mencoder)
morphbr@471
     1
import os
morphbr@471
     2
import sys
morphbr@471
     3
import lib
morphbr@471
     4
import time
morphbr@471
     5
import socket
morphbr@471
     6
import ConfigParser
morphbr@471
     7
morphbr@471
     8
class Media:
morphbr@471
     9
morphbr@471
    10
    def __init__(self, config):
morphbr@471
    11
morphbr@471
    12
        self.config = config
morphbr@471
    13
        self.socket = None
morphbr@471
    14
        self.child_pid = None
morphbr@471
    15
morphbr@471
    16
    def setup(self, filename, mux, vcodec, vbitrate,\
morphbr@471
    17
              fps, acodec, abitrate, width, height, port):
morphbr@471
    18
morphbr@474
    19
        if (os.path.exists(filename)):
morphbr@471
    20
morphbr@474
    21
            self.filename = filename
morphbr@474
    22
            self.mux = mux
morphbr@474
    23
            self.vcodec = vcodec
morphbr@474
    24
            self.vbitrate = int(vbitrate)
morphbr@474
    25
            self.fps = int(fps)
morphbr@474
    26
            self.acodec = acodec
morphbr@474
    27
            self.abitrate = int(abitrate)
morphbr@474
    28
            self.width = int(width)
morphbr@474
    29
            self.height = int(height)
morphbr@474
    30
morphbr@474
    31
            self.port = int(port)
morphbr@474
    32
            self.fifo = self.config.get("Mencoder", "fifo_path")
morphbr@474
    33
morphbr@474
    34
            # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
morphbr@474
    35
            self.path = self.config.get("Mencoder", "path")
morphbr@474
    36
            self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\
morphbr@474
    37
                         " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
morphbr@474
    38
                         " -o %s 1> /dev/null 2> /dev/null" % (
morphbr@474
    39
                self.filename, self.fps, self.mux, self.acodec, self.abitrate,
morphbr@474
    40
                self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
morphbr@474
    41
morphbr@474
    42
morphbr@474
    43
            if (self.socket != None):
morphbr@474
    44
                del(self.socket)
morphbr@474
    45
morphbr@474
    46
            self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
morphbr@474
    47
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
morphbr@474
    48
            self.socket.bind( ('', self.port) )
morphbr@474
    49
            self.socket.listen(1)
morphbr@474
    50
morphbr@474
    51
            return 0
morphbr@474
    52
morphbr@474
    53
        else:
morphbr@474
    54
            lib.log("File requested does not exist. SETUP failed.")
morphbr@474
    55
            return -1
morphbr@474
    56
morphbr@474
    57
    def play(self):
morphbr@471
    58
morphbr@471
    59
        try:
morphbr@471
    60
            os.mkfifo(self.fifo)
morphbr@471
    61
        except:
morphbr@471
    62
            lib.log("Fifo already exists")
morphbr@471
    63
morphbr@471
    64
        lib.log("Starting Mencoder: %s" % self.path )
morphbr@471
    65
        # exec Mencoder
morphbr@474
    66
        pin, self.pout = os.popen2(self.path)
morphbr@471
    67
        pin.close()
morphbr@471
    68
morphbr@471
    69
        fifo = open(self.fifo)
morphbr@471
    70
morphbr@471
    71
        self.child_pid = os.fork()
morphbr@471
    72
        if (self.child_pid == 0):
morphbr@471
    73
            conn,addr= self.socket.accept()
morphbr@471
    74
            lib.log("Sending Data to client: %s" % addr[0])
morphbr@471
    75
morphbr@473
    76
            data = fifo.read(1024)
morphbr@471
    77
            conn.settimeout(5)
morphbr@471
    78
            retry = 0
morphbr@471
    79
morphbr@471
    80
            while( data != "" and retry < 5):
morphbr@471
    81
                try:
morphbr@471
    82
                    conn.send(data)
morphbr@471
    83
                except socket.error:
morphbr@471
    84
                    lib.log("Socket error (maybe timeout ?)")
morphbr@471
    85
                    retry += 1
morphbr@471
    86
morphbr@473
    87
                data = fifo.read(1024)
morphbr@471
    88
morphbr@471
    89
            if (retry < 5):
morphbr@471
    90
                lib.log("Finished sending Data to client: %s" % addr[0])
morphbr@471
    91
            else:
morphbr@471
    92
                lib.log("Client timed out")
morphbr@471
    93
morphbr@471
    94
morphbr@471
    95
    def stop(self):
morphbr@471
    96
morphbr@474
    97
        self.pout.close()
morphbr@474
    98
morphbr@471
    99
        if (self.socket != None):
morphbr@471
   100
            lib.log("Closing socket")
morphbr@471
   101
            self.socket.close()
morphbr@471
   102
morphbr@471
   103
            lib.log("Trying to stop Mencoder process")
morphbr@471
   104
            if (self.child_pid != None):
morphbr@471
   105
                os.kill(self.child_pid, 9)