diff -r 5c5cff842d57 -r 4691abcf4f45 gmyth-stream/server/0.2/plugins/transcoders/mencoder.py --- a/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py Wed Apr 25 15:53:19 2007 +0100 +++ b/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py Thu May 03 14:39:25 2007 +0100 @@ -18,6 +18,7 @@ priority = -1 args = {} proc = None + gmyth = None # only works with avi container status = 0 @@ -38,7 +39,7 @@ self.args["sendback"] = False # input_opt - uri = params_first("uri", "file://-").split("://") + uri = params_first("uri", "file:-").split(":", 1) self.args["type"] = uri[0] self.args["input"] = uri[1] @@ -109,6 +110,7 @@ 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()) @@ -125,6 +127,8 @@ 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"]) @@ -134,6 +138,7 @@ # 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() @@ -175,10 +180,7 @@ # Configuring pipes stdr, stdw = os.pipe() - try: - self.proc = subprocess.Popen(self.mencoder_opts, stdin=stdr, close_fds=True) - except Exception, e: - self.log.error("Error executing mencoder: %s" % e) + if not self._run_mencoder(input=stdr): return False stdout = self._check_opened_file(stdw, _stdin) @@ -215,14 +217,9 @@ # _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) + 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) @@ -235,22 +232,76 @@ 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) - if self.args["outfile"] == "-": - return self._start(outfd) + 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: - return self._start_outfile(outfd) + ret = self._start_outfile(outfd) + + self.stop() + return ret # start() def stop(self): if self.proc: try: - os.kill(self.proc.pid, signal.SIGTERM) + os.kill(self.proc.pid, signal.SIGKILL) except OSError, e: pass @@ -260,6 +311,20 @@ 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