diff -r a1783dab9ba6 -r b66392e80922 gmyth-stream/server/0.2/plugins/transcoders/mencoder.py --- a/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py Mon Apr 23 16:06:54 2007 +0100 +++ b/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py Thu Apr 26 16:30:23 2007 +0100 @@ -2,10 +2,13 @@ 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): @@ -16,6 +19,9 @@ args = {} proc = None + # only works with avi container + status = 0 + def _setup_params(self): params_first = self.params_first @@ -25,6 +31,11 @@ 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("://") @@ -84,7 +95,11 @@ def _setup_mencoder_opts(self, args): args.append(self.mencoder_path) - args.append(self.args["input"]) + + 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"]) @@ -99,7 +114,7 @@ self._arg_append(args, "-really-quiet") self._arg_append(args, "-o %s" % self.args["outfile"]) - self._arg_append(args, "2> %s" % os.devnull) + self._arg_append(args, "2>%s" % os.devnull) # _setup_args() @@ -113,6 +128,12 @@ 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(":") # _setup_filename() @@ -129,16 +150,79 @@ # __init__() - def start(self, outfd): - cmd = " ".join(self.mencoder_opts) - self.log.debug("Mencoder: %s" % cmd) + 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() try: - self.proc = subprocess.Popen(self.mencoder_opts, stdout=subprocess.PIPE, close_fds=True) + self.proc = subprocess.Popen(self.mencoder_opts, stdin=stdr, close_fds=True) except Exception, e: self.log.error("Error executing mencoder: %s" % e) 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): + try: + self.proc = subprocess.Popen(self.mencoder_opts, + stdout=subprocess.PIPE, close_fds=True) + except Exception, e: + self.log.error("Error executing mencoder: %s" % e) + return False + + try: while self.proc and self.proc.poll() == None: d = self.proc.stdout.read(1024) @@ -147,7 +231,19 @@ 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(self, outfd): + cmd = " ".join(self.mencoder_opts) + self.log.debug("Mencoder: %s" % cmd) + + if self.args["outfile"] == "-": + return self._start(outfd) + else: + return self._start_outfile(outfd) # start()