gmyth-stream/server/0.2/plugins/transcoders/mencoder_lib/mythtv.py
author morphbr
Wed May 16 15:42:18 2007 +0100 (2007-05-16)
branchtrunk
changeset 664 464d715ddb7e
parent 653 3433df0d6ae3
child 676 b51559fec601
permissions -rw-r--r--
[svn r670] * GMyth-Streamer
- Updated mencoder uri schema
morphbr@653
     1
import os
morphbr@653
     2
import subprocess
morphbr@653
     3
import fcntl
morphbr@653
     4
morphbr@653
     5
import lib.utils as utils
morphbr@653
     6
import lib.server as server
morphbr@653
     7
morphbr@653
     8
from select import select
morphbr@653
     9
morphbr@664
    10
def _setup_mythfilename(self):
morphbr@664
    11
    # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv
morphbr@664
    12
morphbr@664
    13
    try:
morphbr@664
    14
        _mysql = self.args["input"].split("@")[0].split(":")
morphbr@664
    15
    except IndexError, e:
morphbr@664
    16
        _mysql = ["mythtv", "mythtv"]
morphbr@664
    17
morphbr@664
    18
    self.log.debug("aqui 1")
morphbr@664
    19
morphbr@664
    20
    _args = self.args["input"].split("@")[1].split(":")
morphbr@664
    21
    gmyth_dict = {}
morphbr@664
    22
    gmyth_dict["mysql"] = _mysql
morphbr@664
    23
    gmyth_dict["backend"] = _args[0]
morphbr@664
    24
    gmyth_dict["port"] = _args[1].split("/", 1)[0]
morphbr@664
    25
morphbr@664
    26
    self.log.debug("aqui 2 %s" % _args)
morphbr@664
    27
morphbr@664
    28
    _tmp_file = _args[1].split("/", 1)[1]
morphbr@664
    29
morphbr@664
    30
    self.log.debug("aqui 2a %s" % _tmp_file)
morphbr@664
    31
morphbr@664
    32
    if _tmp_file.find("channel") >= 0:
morphbr@664
    33
        gmyth_dict["kind"] = "c"
morphbr@664
    34
        gmyth_dict["cfile"] = _tmp_file.split("=")[1]
morphbr@664
    35
        self.log.debug("aqui 3")
morphbr@664
    36
    else:
morphbr@664
    37
        gmyth_dict["kind"] = "f"
morphbr@664
    38
        gmyth_dict["cfile"] = _tmp_file
morphbr@664
    39
        self.log.debug("aqui 4")
morphbr@664
    40
morphbr@664
    41
    self.log.debug("aqui 5")
morphbr@664
    42
morphbr@664
    43
    self.args["input"] = "-"
morphbr@664
    44
    return gmyth_dict
morphbr@664
    45
# _setup_mythfilename
morphbr@664
    46
morphbr@653
    47
def _setup_mythfile(err):
morphbr@653
    48
    size = err.read(20).split("\n")[0].split("Size:")[1]
morphbr@653
    49
    flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
morphbr@653
    50
    fcntl.fcntl(err, fcntl.F_SETFL, flags)
morphbr@653
    51
    return size
morphbr@653
    52
# _setup_mythfile
morphbr@653
    53
morphbr@653
    54
def _setup_gmythcat(self):
morphbr@653
    55
    gmyth_cat = utils.which("gmyth-cat")
morphbr@653
    56
    return [ utils.which("gmyth-cat"),
morphbr@664
    57
            "-h", self.args["gmyth-cat"]["backend"],
morphbr@664
    58
            "-p", self.args["gmyth-cat"]["port"],
morphbr@664
    59
            "-" + self.args["gmyth-cat"]["kind"],
morphbr@664
    60
            self.args["gmyth-cat"]["cfile"]
morphbr@653
    61
           ]
morphbr@653
    62
# _setup_gmythcat
morphbr@653
    63
morphbr@653
    64
def start_myth(self, outfd):
morphbr@653
    65
    opts = _setup_gmythcat(self)
morphbr@653
    66
    try:
morphbr@653
    67
        self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
morphbr@653
    68
                                      stderr=subprocess.PIPE,
morphbr@653
    69
                                      close_fds=True)
morphbr@653
    70
    except Exception, e:
morphbr@653
    71
        self.log.error("Error executing gmyth-cat: %s" % e)
morphbr@653
    72
        return False
morphbr@653
    73
morphbr@653
    74
    if not self._run_mencoder(input=self.gmyth.stdout,
morphbr@653
    75
                              output=subprocess.PIPE):
morphbr@653
    76
        return False
morphbr@653
    77
morphbr@664
    78
    if self.args["gmyth-cat"]["kind"] == "f":
morphbr@653
    79
        try:
morphbr@653
    80
            size = _setup_mythfile(self.gmyth.stderr)
morphbr@653
    81
            self.log.debug("Size of file: %s" % size)
morphbr@653
    82
        except Exception, e:
morphbr@653
    83
            self.log.error("Problems getting size of file: %s" % e)
morphbr@653
    84
            return False
morphbr@653
    85
morphbr@653
    86
    try:
morphbr@653
    87
        while self.proc and self.proc.poll() == None:
morphbr@653
    88
            r, w, x = select([self.gmyth.stderr, self.proc.stdout],
morphbr@653
    89
                             [], [], 0)
morphbr@653
    90
            if self.proc.stdout in r:
morphbr@653
    91
                d = self.proc.stdout.read(4096)
morphbr@653
    92
                outfd.write(d)
morphbr@653
    93
morphbr@653
    94
            if self.gmyth.stderr in r:
morphbr@653
    95
                partial = self.gmyth.stderr.read(50).split("\n")[-2]
morphbr@653
    96
                if partial != "":
morphbr@653
    97
                    self.status = utils.progress_bar(self.log,
morphbr@653
    98
                                                     int(partial),
morphbr@653
    99
                                                     int(size), 50)
morphbr@653
   100
morphbr@653
   101
    except IndexError, e:
morphbr@653
   102
        pass
morphbr@653
   103
    except Exception, e:
morphbr@653
   104
        self.log.error("Problems handling data: %s" % e)
morphbr@653
   105
        return False
morphbr@653
   106
morphbr@653
   107
    self.log.info("Finished sending data")
morphbr@653
   108
    return True
morphbr@653
   109
# _start_myth()