# HG changeset patch # User morphbr # Date 1176387132 -3600 # Node ID e55310730feb643f4a069d317cc90e70cc5a62ec # Parent ba3564c925f341d076ccbfbdafa24060757652e7 [svn r538] Bug fixes inside Gmyth-Stream diff -r ba3564c925f3 -r e55310730feb gmyth-stream/server/lib.py --- a/gmyth-stream/server/lib.py Thu Apr 12 15:05:48 2007 +0100 +++ b/gmyth-stream/server/lib.py Thu Apr 12 15:12:12 2007 +0100 @@ -3,7 +3,7 @@ import os import stat -global ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov'] +ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov'] def now(): return time.strftime("%Y-%m-%d %H:%M:%S"); @@ -18,7 +18,7 @@ def which(prg): for d in bin_path_list: path = os.path.join(d, prg) - if os.path.exits(path): + if os.path.exists(path): st = os.stat(path) if st[stat.ST_MODE] & 0111: return path @@ -29,8 +29,8 @@ for name in files: if os.path.splitext(name)[1].strip(".") in ext: media = os.path.join(root,name) - if media not in file_list - file_list.append(os.path.join(root,name)) + if media not in file_list: + file_list.append(os.path.join(root,name)) return True diff -r ba3564c925f3 -r e55310730feb gmyth-stream/server/main.py --- a/gmyth-stream/server/main.py Thu Apr 12 15:05:48 2007 +0100 +++ b/gmyth-stream/server/main.py Thu Apr 12 15:12:12 2007 +0100 @@ -64,40 +64,61 @@ ''' nextport = 0 +setup = (False, "STOPPED") def do_setup(server, filename, mux, vcodec, vbitrate, fps, acodec, abitrate, width, height, *options): global nextport - nextport += 1 - ret = media.setup(filename, mux, vcodec, vbitrate, fps, acodec, - abitrate, width, height, nextport, options) - if ret: - server.sendOk() - else: - server.sendNotOk(ret) + global setup + + if setup[1] != "PLAYING": + nextport += 1 + ret = media.setup(filename, mux, vcodec, vbitrate, fps, acodec, + abitrate, width, height, nextport, options) + if ret[0]: + server.sendOk() + else: + server.sendNotOk(ret[1]) + + setup = (True, setup[1]) + + else: server.sendNotOk("You must STOP before SETingUP again") return True def do_play(server): - ret = media.play() - if ret: - server.sendOk("%d" % nextport) + global setup + + if setup[0] and setup[1] == "STOPPED": + setup = (setup[0], "PLAYING") + ret = media.play() + if ret[0]: + server.sendOk("%d" % nextport) + else: + server.sendNotOk(ret[1]) + else: - server.sendNotOk(ret) + if setup[1] == "STOPPED": + server.sendNotOk("You must SETUP before PLAYing") + else: + server.sendNotOk("You must STOP before PLAYing again") return True def do_stop(server): + global setup + media.stop() + setup = (False, "STOPPED") server.sendOk() return True def do_list(server, *directory): file_list = [] for j in directory: - list_media_files(j, file_list) + lib.list_media_files(j, file_list) - server.sendOk() + server.sendOk(file_list) return True def do_quit(server): @@ -115,6 +136,7 @@ "QUIT": do_quit, } + def dispatch(server, msg): pieces = msg.split() if len(pieces) < 1: @@ -151,9 +173,12 @@ log.info("Client %s sent command: %r" % (client, msg)) dispatch(server, msg) - log.info("Closing connection with %s" % (client,)) server.disconnect_client(conn) + try: + os.wait() + except Exception, e: + log.error(e) server.stop() log.info("Server stopped. Closing...") diff -r ba3564c925f3 -r e55310730feb gmyth-stream/server/plugins/media/mencoder.py --- a/gmyth-stream/server/plugins/media/mencoder.py Thu Apr 12 15:05:48 2007 +0100 +++ b/gmyth-stream/server/plugins/media/mencoder.py Thu Apr 12 15:12:12 2007 +0100 @@ -1,3 +1,5 @@ +from __future__ import division + import os import sys import lib @@ -16,6 +18,12 @@ def __init__(self, config): self.config = config + self.do_cleanup() + + # __init__() + + + def do_cleanup(self): self.path = "" self.args = [] self.language = None @@ -26,8 +34,14 @@ self.mplayer = None self.mencoder_pid = None self.mplayer_pid = None + self.audio_opts = None + self.video_opts = None + self.gst_pipe = None + self.gst_pid = None + self.transcode_local = None - # __init__ + # do_cleanup() + def setup_opts(self, options): @@ -42,7 +56,7 @@ if len(lan) < 2: self.language = lan except Exception, e: - log.error("Bad language option: %s" % e) + log.error("Bad language option: %s" % opt) elif opt.find("subtitle=") >= 0: try: @@ -50,15 +64,22 @@ if len(sub) < 2: self.language = sub except Exception, e: - log.error("Bad subtitle option: %s" % e) + log.error("Bad subtitle option: %s" % opt) elif opt.find("format=") >= 0: try: self.mpegopts = opt.split("=")[1] except Exception, e: - log.error("Bad format option: %s" % e) + log.error("Bad format option: %s" % opt) - # setup_opts + elif opt.find("outfile=") >= 0: + try: + self.transcode_local = opt.split("=")[1] + except Exception, e: + log.error("Bad outfile option: %s" % opt) + + # setup_opts() + def run_mplayer(self): msg = self.filename @@ -69,7 +90,8 @@ self.mplayer_pid = Popen([self.mplayer, self.filename, "1> %s" % os.devnull,\ "2> %s" % os.devnull], stdout=PIPE, close_fds=True) - # run_mplayer + # run_mplayer() + def setup_mencoder(self): self.path = self.config.get("Mencoder", "path") @@ -92,7 +114,8 @@ else: self.fifo = "-" - # setup_mencoder + # setup_mencoder() + def setup_audio(self): @@ -102,7 +125,7 @@ return "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (\ self.acodec, self.abitrate) - # setup_audio + # setup_audio() def setup_video(self): @@ -126,7 +149,7 @@ return video - # setup_video + # setup_video() def arg_append(self, args, options): @@ -134,12 +157,15 @@ for i in l: args.append(i) - # arg_append + # arg_append() + def setup_args(self, args): args.append(self.path) - args.append(self.filename) + + #args.append(self.filename) + args.append("-") if self.language != None: self.arg_append(args, "-alang %s" % self.language) @@ -156,7 +182,8 @@ self.arg_append(args, "-o %s" % self.fifo) self.arg_append(args, "2> %s" % os.devnull) - # setup_args + # setup_args() + def setup_filename(self, filename): try: @@ -173,13 +200,20 @@ elif self.kind == "dvd": self.filename = "dvd://" + filename + elif self.kind == "myth": + self.filename = "myth://" + filename + self.gst_pipe = os.pipe() + print self.gst_pipe[0] + print self.gst_pipe[1] + return (True, "") - # setup_filename + # setup_filename() + def setup_socket(self): if self.socket != None: - del(self.socket) + self.socket = None self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -193,7 +227,8 @@ return (True, "") - # setup_socket + # setup_socket() + ''' MENCODER SETUP DESCRIPTION @@ -216,6 +251,9 @@ def setup(self, filename, mux, vcodec, vbitrate,\ fps, acodec, abitrate, width, height, port, options): + if self.args != []: + self.do_cleanup() + self.mux = mux self.vcodec = vcodec self.vbitrate = vbitrate @@ -230,7 +268,7 @@ ret_val = self.setup_filename(filename) if not ret_val[0]: - return ret_val[1] + return ret_val self.setup_opts(options) self.audio_opts = self.setup_audio() @@ -238,38 +276,17 @@ self.setup_args(self.args) ret_val = self.setup_socket() - if not ret_val[0]: - return ret_val[1] + return ret_val - return True + # setup() - # setup + def play_loop(self, conn): + data = self.pout.read(4096) - def play(self): + conn.settimeout(5) + retry = 0 - log.info("Starting Mencoder: %s" % self.args ) - - try: - self.mencoder_pid = Popen(self.args, stdout=PIPE, close_fds=True) - except Exception, e: - msg = "Could not init Mencoder: %s" % e - log.error(msg) - return msg - - if self.mencoder_old: self.pout = open(self.fifo) - else: self.pout = self.mencoder_pid.stdout - - self.child_pid = os.fork() - - if self.child_pid == 0: - conn, addr = self.socket.accept() - log.info("Sending Data to client: %s" % addr[0]) - - data = self.pout.read(4096) - - conn.settimeout(5) - retry = 0 - + if not self.transcode_local: while data != "" and retry < 5: try: conn.send(data) @@ -285,6 +302,67 @@ data = self.pout.read(4096) + else: + local = open(self.transcode_local, "w") + total = os.path.getsize(self.filename) + partial = 4096 + + while data != "": + try: + local.write(data) + except Exception, e: + log.error("Write error: %s" % e) + + data = self.pout.read(4096) + partial += len(data) + conn.send("%.2f\n" % (partial * 100 / total) ) + + local.close() + conn.send("DONE\n") + + return retry + + # play_loop() + + + def play(self): + + log.info("Starting Mencoder: %s" % self.args ) + + if self.gst_pipe: + try: + gst = [ lib.which("gst-launch-0.10") ] + self.arg_append(gst, "filesrc location=/tmp/mpg/bad_day.mpg") + self.arg_append(gst, "! fdsink fd=%d" % self.gst_pipe[1]) + self.gst_pid = Popen(gst, stdout=self.gst_pipe[1], close_fds=True) + except Exception, e: + msg = "Could not init Gstreamer: %s" % e + log.error(msg) + return (False, msg) + + try: + if not self.gst_pipe: + self.stdin = open(self.filename) + else: + self.stdin = self.gst_pipe[0] + + self.mencoder_pid = Popen(self.args, stdin=self.stdin, stdout=PIPE, close_fds=True) + except Exception, e: + msg = "Could not init Mencoder: %s" % e + log.error(msg) + return (False, msg) + + if self.mencoder_old: self.pout = open(self.fifo) + else: self.pout = self.mencoder_pid.stdout + + self.child_pid = os.fork() + + if self.child_pid == 0: + conn, addr = self.socket.accept() + + log.info("Sending Data to client: %s" % addr[0]) + retry = self.play_loop(conn) + if retry < 5: log.info("Finished sending Data to client: %s" % addr[0]) else: @@ -293,29 +371,39 @@ os.kill(self.mencoder_pid.pid, signal.SIGKILL) sys.exit(0) - return True + return (True, "") - # play + # play() def stop(self): try: if self.mencoder_pid: - os.kill(self.mencoder_pid.pid, signal.SIGKILL) + os.kill(self.mencoder_pid.pid, signal.SIGTERM) self.mencoder_pid = None if self.mplayer_pid: - os.kill(self.mplayer_pid.pid, signal.SIGKILL) + os.kill(self.mplayer_pid.pid, signal.SIGTERM) self.mplayer_pid = None - if self.socket != None: + if self.socket: self.socket.close() + self.socket = None - if self.child_pid != None: - os.kill(self.child_pid, signal.SIGKILL) + if self.child_pid: + os.kill(self.child_pid, signal.SIGTERM) + self.child_pid = None + + if self.gst_pid: + os.kill(self.gst_pid.pid, signal.SIGTERM) + self.gst_pid = None + + self.do_cleanup() + + os.wait() except Exception, e: log.error("Stop error: %s" % e) - # stop + # stop()