import os
import shlex
import signal
import subprocess
import time

import lib.utils as utils
import lib.server as server

from select import select

__all__ = ("TranscoderMencoder",)

class TranscoderMencoder(server.Transcoder):
    """Transcoder class that implements a transcoder using Mencoder"""
    mencoder_path = utils.which("mencoder")
    name = "mencoder"
    priority = -1
    args = {}
    proc = None
    gmyth = None

    # only works with avi container
    status = 0

    def _setup_params(self):
        params_first = self.params_first

        # general_opts
        self.args["local"]    = params_first("local", False)
        self.args["language"] = params_first("language", False)
        self.args["subtitle"] = params_first("subtitle", False)
        self.args["format"]   = params_first("format", "")
        self.args["outfile"]  = params_first("outfile", "-")
        self.args["sendback"] = params_first("sendback", True)

        # handle sendback variable
        if self.args["sendback"] == "False":
            self.args["sendback"] = False

        # input_opt
        uri = params_first("uri", "file:-").split(":", 1)
        self.args["type"]     = uri[0]
        self.args["input"]    = uri[1]

        # audio_opts
        self.args["acodec"]   = params_first("acodec", "mp2")
        self.args["abitrate"] = params_first("abitrate", 192)
        self.args["volume"]   = params_first("volume", 5)

        # video_opts
        self.args["mux"]      = params_first("mux", "mpeg")
        self.args["fps"]      = params_first("fps", 25)
        self.args["vcodec"]   = params_first("vcodec", "mpeg1video")
        self.args["vbitrate"] = params_first("vbitrate", 400)
        self.args["width"]    = params_first("width", 320)
        self.args["height"]   = params_first("height", 240)
    # _setup_params()


    def _setup_audio(self):
        if self.args["acodec"] == "mp3lame":
            audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
                self.args["abitrate"], self.args["volume"])
        else:
            audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
                self.args["acodec"], self.args["abitrate"])

        return audio
    # _setup_audio()


    def _setup_video(self):
        video = " -of %s" % self.args["mux"]
        video += " -ofps %s" % self.args["fps"]

        vcodec = self.args["vcodec"]
        if vcodec == "nuv" or vcodec == "xvid"\
               or vcodec == "qtvideo" or vcodec == "copy":
            video += " -ovc %s" % vcodec
        else:
            video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
                vcodec, self.args["vbitrate"])

        video += " %s" % self.args["format"]
        video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])

        return video
    # _setup_video()


    def _arg_append(self, args, options):
        for arg in shlex.split(options):
            args.append(arg)
    # arg_append()

    def _setup_mencoder_opts(self, args):
        args.append(self.mencoder_path)

        if self.args["outfile"] == "-" and self.args["type"]:
            args.append(self.args["input"])
        else:
            args.append("-")

        if self.args["language"]:
            self._arg_append(args, "-alang %s" % self.args["language"])

        if self.args["subtitle"]:
            self._arg_append(args, "-slang %s" % self.args["subtitle"])
            self._arg_append(args, "-subfps %s" % self.args["fps"])

        self._arg_append(args, "-idx")
        self._arg_append(args, "-cache 500")
        self._arg_append(args, self._setup_audio())
        self._arg_append(args, self._setup_video())

        self._arg_append(args, "-really-quiet")
        self._arg_append(args, "-o %s" % self.args["outfile"])
        self._arg_append(args, "2>%s" % os.devnull)
    # _setup_args()


    def _setup_filename(self):
        _type = self.args["type"]

        if _type == "file":
            if not os.path.exists(self.args["input"]):
                raise IOError,\
                      "File requested does not exist: %s." % self.args["input"]
            else:
                self.args["input"] = "file://%s" % self.args["input"]

        elif _type == "dvd":
            self.args["input"] = "dvd://".join(self.args["input"])

        elif _type == "myth":
            # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
            # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
            # myth://IP:PORT:type:file
            self.args["gmyth-cat"] = self.args["input"].split(":")
            self.args["input"] = "-"
    # _setup_filename()


    def __init__(self, params):
        server.Transcoder.__init__(self, params)
        self.mencoder_opts = []

        try:
            self._setup_params()
            self._setup_filename()
            self._setup_mencoder_opts(self.mencoder_opts)
        except Exception, e:
            self.log.error(e)
    # __init__()


    def _check_opened_file(self, stdw, _stdin):
        loop = True
        while loop:
            try:
                return open(self.args["outfile"])
            except:
                os.write(stdw, _stdin.read(1024))
    # _check_opened_file


    def _start_outfile(self, outfd):
        finished = False

        # fix this (not necessary)
        outfd.write("OK")

        # Configuring stdin
        _stdin = open(self.args["input"])
        size = int(os.path.getsize(self.args["input"]))
        self.status = 0
        total_read = 0

        # Configuring pipes
        stdr, stdw = os.pipe()

        if not self._run_mencoder(input=stdr):
            return False

        stdout = self._check_opened_file(stdw, _stdin)

        try:
            while self.proc and self.proc.poll() == None:
                if not finished:
                    data_in = _stdin.read(4096)
                    if data_in != "":
                        os.write(stdw, data_in)
                        total_read += 4096
                        d = stdout.read(4096)
                        if self.args["sendback"]:
                            outfd.write(d)
                        self.status = total_read * 100 / size
                    else:
                        finished = True
                        os.close(stdw)

                else:
                    d = stdout.read(4096)
                    if self.args["sendback"] and d != "":
                        outfd.write(d)

        except Exception, e:
            self.log.error("Problems handling data: %s" % e)
            return False

        self.log.info("%s: Finished sending data to client" % repr(self))
        if not self.args["sendback"]:
            outfd.write("DONE")

        return True
    # _start_outfile()

    def _start(self, outfd):
        if not self._run_mencoder(output=subprocess.PIPE):
            return False

        try:
            while self.proc and self.proc.poll() == None:
                d = self.proc.stdout.read(1024)
                outfd.write(d)
        except Exception, e:
            self.log.error("Problems handling data: %s" % e)
            return False

        self.log.info("%s: Finished sending data to client" % repr(self))
        return True
    # _start()

    def _start_myth(self, outfd):
        # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
        # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
        # myth://IP:PORT:type:file
        host = self.args["gmyth-cat"][0]
        port = self.args["gmyth-cat"][1]
        kind = self.args["gmyth-cat"][2]
        fchan = self.args["gmyth-cat"][3]

        gmyth_cat = utils.which("gmyth-cat")
        opts = [gmyth_cat, "-h", host, "-p", port, "-" + kind, fchan]
        gr, gw = os.pipe()

        try:
            self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE, close_fds=True)
        except Exception, e:
            self.log.error("Error executing gmyth-cat: %s" % e)
            return False

        if not self._run_mencoder(input=self.gmyth.stdout, output=subprocess.PIPE):
            return False

        try:
            while self.proc and self.proc.poll() == None:
                r, w, x = select([self.proc.stdout], [], [], 0)
                if self.proc.stdout in r:
                    d = self.proc.stdout.read(4096)
                    outfd.write(d)
        except Exception, e:
            self.log.error("Problems handling data: %s" % e)
            return False

        return True
    # _start_myth()

    def _run_mencoder(self, input=None, output=None):
        try:
            self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
                                         stdout=output, close_fds=True)
        except Exception, e:
            self.log.error("Error executing mencoder: %s" % e)
            return False

        return True
    # _run_mencoder()

    def start(self, outfd):
        cmd = " ".join(self.mencoder_opts)
        self.log.debug("Mencoder: %s" % cmd)

        ret = False

        if self.args["outfile"] == "-" and self.args["type"] in ["file", "dvd"]:
            ret = self._start(outfd)

        elif self.args["type"] == "myth":
            ret = self._start_myth(outfd)

        else:
            ret = self._start_outfile(outfd)

        self.stop()
        return ret
    # start()


    def stop(self):
        if self.proc:
            try:
                os.kill(self.proc.pid, signal.SIGKILL)
            except OSError, e:
                pass

            try:
                self.proc.wait()
            except Exception, e:
                pass

            self.proc = None

        if self.gmyth:
            try:
                os.kill(self.gmyth.pid, signal.SIGKILL)
            except OSError, e:
                pass

            try:
                self.gmyth.wait()
            except Exception, e:
                pass

            self.gmyth = None

    # stop()

# TranscoderMencoder
