[svn r538] Bug fixes inside Gmyth-Stream trunk
authormorphbr
Thu Apr 12 15:12:12 2007 +0100 (2007-04-12)
branchtrunk
changeset 533e55310730feb
parent 532 ba3564c925f3
child 534 cb1c3e2988b9
[svn r538] Bug fixes inside Gmyth-Stream
gmyth-stream/server/lib.py
gmyth-stream/server/main.py
gmyth-stream/server/plugins/media/mencoder.py
     1.1 --- a/gmyth-stream/server/lib.py	Thu Apr 12 15:05:48 2007 +0100
     1.2 +++ b/gmyth-stream/server/lib.py	Thu Apr 12 15:12:12 2007 +0100
     1.3 @@ -3,7 +3,7 @@
     1.4  import os
     1.5  import stat
     1.6  
     1.7 -global ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov']
     1.8 +ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov']
     1.9  
    1.10  def now():
    1.11      return time.strftime("%Y-%m-%d %H:%M:%S");
    1.12 @@ -18,7 +18,7 @@
    1.13  def which(prg):
    1.14      for d in bin_path_list:
    1.15          path = os.path.join(d, prg)
    1.16 -        if os.path.exits(path):
    1.17 +        if os.path.exists(path):
    1.18              st = os.stat(path)
    1.19              if st[stat.ST_MODE] & 0111:
    1.20                  return path
    1.21 @@ -29,8 +29,8 @@
    1.22          for name in files:
    1.23              if os.path.splitext(name)[1].strip(".") in ext:
    1.24                  media = os.path.join(root,name)
    1.25 -                if media not in file_list
    1.26 -                file_list.append(os.path.join(root,name))
    1.27 +                if media not in file_list:
    1.28 +                    file_list.append(os.path.join(root,name))
    1.29  
    1.30      return True
    1.31  
     2.1 --- a/gmyth-stream/server/main.py	Thu Apr 12 15:05:48 2007 +0100
     2.2 +++ b/gmyth-stream/server/main.py	Thu Apr 12 15:12:12 2007 +0100
     2.3 @@ -64,40 +64,61 @@
     2.4  
     2.5  '''
     2.6  nextport = 0
     2.7 +setup = (False, "STOPPED")
     2.8  
     2.9  def do_setup(server, filename, mux, vcodec, vbitrate, fps, acodec, abitrate,
    2.10               width, height, *options):
    2.11      global nextport
    2.12 -    nextport += 1
    2.13 -    ret = media.setup(filename, mux, vcodec, vbitrate, fps, acodec,
    2.14 -                      abitrate, width, height, nextport, options)
    2.15 -    if ret:
    2.16 -        server.sendOk()
    2.17 -    else:
    2.18 -        server.sendNotOk(ret)
    2.19 +    global setup
    2.20 +
    2.21 +    if setup[1] != "PLAYING":
    2.22 +        nextport += 1
    2.23 +        ret = media.setup(filename, mux, vcodec, vbitrate, fps, acodec,
    2.24 +                          abitrate, width, height, nextport, options)
    2.25 +        if ret[0]:
    2.26 +            server.sendOk()
    2.27 +        else:
    2.28 +            server.sendNotOk(ret[1])
    2.29 +
    2.30 +        setup = (True, setup[1])
    2.31 +
    2.32 +    else: server.sendNotOk("You must STOP before SETingUP again")
    2.33  
    2.34      return True
    2.35  
    2.36  def do_play(server):
    2.37 -    ret = media.play()
    2.38 -    if ret:
    2.39 -        server.sendOk("%d" % nextport)
    2.40 +    global setup
    2.41 +
    2.42 +    if setup[0] and setup[1] == "STOPPED":
    2.43 +        setup = (setup[0], "PLAYING")
    2.44 +        ret = media.play()
    2.45 +        if ret[0]:
    2.46 +            server.sendOk("%d" % nextport)
    2.47 +        else:
    2.48 +            server.sendNotOk(ret[1])
    2.49 +
    2.50      else:
    2.51 -        server.sendNotOk(ret)
    2.52 +        if setup[1] == "STOPPED":
    2.53 +            server.sendNotOk("You must SETUP before PLAYing")
    2.54 +        else:
    2.55 +            server.sendNotOk("You must STOP before PLAYing again")
    2.56  
    2.57      return True
    2.58  
    2.59  def do_stop(server):
    2.60 +    global setup
    2.61 +
    2.62      media.stop()
    2.63 +    setup = (False, "STOPPED")
    2.64      server.sendOk()
    2.65      return True
    2.66  
    2.67  def do_list(server, *directory):
    2.68      file_list = []
    2.69      for j in directory:
    2.70 -        list_media_files(j, file_list)
    2.71 +        lib.list_media_files(j, file_list)
    2.72  
    2.73 -    server.sendOk()
    2.74 +    server.sendOk(file_list)
    2.75      return True
    2.76  
    2.77  def do_quit(server):
    2.78 @@ -115,6 +136,7 @@
    2.79      "QUIT": do_quit,
    2.80      }
    2.81  
    2.82 +
    2.83  def dispatch(server, msg):
    2.84      pieces = msg.split()
    2.85      if len(pieces) < 1:
    2.86 @@ -151,9 +173,12 @@
    2.87          log.info("Client %s sent command: %r" % (client, msg))
    2.88          dispatch(server, msg)
    2.89  
    2.90 -
    2.91      log.info("Closing connection with %s" % (client,))
    2.92      server.disconnect_client(conn)
    2.93 +    try:
    2.94 +        os.wait()
    2.95 +    except Exception, e:
    2.96 +        log.error(e)
    2.97  
    2.98  server.stop()
    2.99  log.info("Server stopped. Closing...")
     3.1 --- a/gmyth-stream/server/plugins/media/mencoder.py	Thu Apr 12 15:05:48 2007 +0100
     3.2 +++ b/gmyth-stream/server/plugins/media/mencoder.py	Thu Apr 12 15:12:12 2007 +0100
     3.3 @@ -1,3 +1,5 @@
     3.4 +from __future__ import division
     3.5 +
     3.6  import os
     3.7  import sys
     3.8  import lib
     3.9 @@ -16,6 +18,12 @@
    3.10      def __init__(self, config):
    3.11  
    3.12          self.config = config
    3.13 +        self.do_cleanup()
    3.14 +
    3.15 +    # __init__()
    3.16 +
    3.17 +
    3.18 +    def do_cleanup(self):
    3.19          self.path = ""
    3.20          self.args = []
    3.21          self.language = None
    3.22 @@ -26,8 +34,14 @@
    3.23          self.mplayer = None
    3.24          self.mencoder_pid = None
    3.25          self.mplayer_pid = None
    3.26 +        self.audio_opts = None
    3.27 +        self.video_opts = None
    3.28 +        self.gst_pipe = None
    3.29 +        self.gst_pid = None
    3.30 +        self.transcode_local = None
    3.31  
    3.32 -    # __init__
    3.33 +    # do_cleanup()
    3.34 +
    3.35  
    3.36      def setup_opts(self, options):
    3.37  
    3.38 @@ -42,7 +56,7 @@
    3.39                      if len(lan) < 2:
    3.40                          self.language = lan
    3.41                  except Exception, e:
    3.42 -                    log.error("Bad language option: %s" % e)
    3.43 +                    log.error("Bad language option: %s" % opt)
    3.44  
    3.45              elif opt.find("subtitle=") >= 0:
    3.46                  try:
    3.47 @@ -50,15 +64,22 @@
    3.48                      if len(sub) < 2:
    3.49                          self.language = sub
    3.50                  except Exception, e:
    3.51 -                    log.error("Bad subtitle option: %s" % e)
    3.52 +                    log.error("Bad subtitle option: %s" % opt)
    3.53  
    3.54              elif opt.find("format=") >= 0:
    3.55                  try:
    3.56                      self.mpegopts = opt.split("=")[1]
    3.57                  except Exception, e:
    3.58 -                    log.error("Bad format option: %s" % e)
    3.59 +                    log.error("Bad format option: %s" % opt)
    3.60  
    3.61 -    # setup_opts
    3.62 +            elif opt.find("outfile=") >= 0:
    3.63 +                try:
    3.64 +                    self.transcode_local = opt.split("=")[1]
    3.65 +                except Exception, e:
    3.66 +                    log.error("Bad outfile option: %s" % opt)
    3.67 +
    3.68 +    # setup_opts()
    3.69 +
    3.70  
    3.71      def run_mplayer(self):
    3.72          msg = self.filename
    3.73 @@ -69,7 +90,8 @@
    3.74          self.mplayer_pid = Popen([self.mplayer, self.filename, "1> %s" % os.devnull,\
    3.75                                    "2> %s" % os.devnull], stdout=PIPE, close_fds=True)
    3.76  
    3.77 -    # run_mplayer
    3.78 +    # run_mplayer()
    3.79 +
    3.80  
    3.81      def setup_mencoder(self):
    3.82          self.path = self.config.get("Mencoder", "path")
    3.83 @@ -92,7 +114,8 @@
    3.84          else:
    3.85              self.fifo = "-"
    3.86  
    3.87 -    # setup_mencoder
    3.88 +    # setup_mencoder()
    3.89 +
    3.90  
    3.91      def setup_audio(self):
    3.92  
    3.93 @@ -102,7 +125,7 @@
    3.94              return "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (\
    3.95                  self.acodec, self.abitrate)
    3.96  
    3.97 -    # setup_audio
    3.98 +    # setup_audio()
    3.99  
   3.100  
   3.101      def setup_video(self):
   3.102 @@ -126,7 +149,7 @@
   3.103  
   3.104          return video
   3.105  
   3.106 -    # setup_video
   3.107 +    # setup_video()
   3.108  
   3.109  
   3.110      def arg_append(self, args, options):
   3.111 @@ -134,12 +157,15 @@
   3.112          for i in l:
   3.113              args.append(i)
   3.114  
   3.115 -    # arg_append
   3.116 +    # arg_append()
   3.117 +
   3.118  
   3.119      def setup_args(self, args):
   3.120  
   3.121          args.append(self.path)
   3.122 -        args.append(self.filename)
   3.123 +
   3.124 +        #args.append(self.filename)
   3.125 +        args.append("-")
   3.126  
   3.127          if self.language != None:
   3.128              self.arg_append(args, "-alang %s" % self.language)
   3.129 @@ -156,7 +182,8 @@
   3.130          self.arg_append(args, "-o %s" % self.fifo)
   3.131          self.arg_append(args, "2> %s" % os.devnull)
   3.132  
   3.133 -    # setup_args
   3.134 +    # setup_args()
   3.135 +
   3.136  
   3.137      def setup_filename(self, filename):
   3.138          try:
   3.139 @@ -173,13 +200,20 @@
   3.140          elif self.kind == "dvd":
   3.141              self.filename = "dvd://" + filename
   3.142  
   3.143 +        elif self.kind == "myth":
   3.144 +            self.filename = "myth://" + filename
   3.145 +            self.gst_pipe = os.pipe()
   3.146 +            print self.gst_pipe[0]
   3.147 +            print self.gst_pipe[1]
   3.148 +
   3.149          return (True, "")
   3.150  
   3.151 -    # setup_filename
   3.152 +    # setup_filename()
   3.153 +
   3.154  
   3.155      def setup_socket(self):
   3.156          if self.socket != None:
   3.157 -            del(self.socket)
   3.158 +            self.socket = None
   3.159  
   3.160          self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
   3.161          self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
   3.162 @@ -193,7 +227,8 @@
   3.163  
   3.164          return (True, "")
   3.165  
   3.166 -    # setup_socket
   3.167 +    # setup_socket()
   3.168 +
   3.169  
   3.170      '''
   3.171      MENCODER SETUP DESCRIPTION
   3.172 @@ -216,6 +251,9 @@
   3.173      def setup(self, filename, mux, vcodec, vbitrate,\
   3.174                fps, acodec, abitrate, width, height, port, options):
   3.175  
   3.176 +        if self.args != []:
   3.177 +            self.do_cleanup()
   3.178 +
   3.179          self.mux = mux
   3.180          self.vcodec = vcodec
   3.181          self.vbitrate = vbitrate
   3.182 @@ -230,7 +268,7 @@
   3.183  
   3.184          ret_val = self.setup_filename(filename)
   3.185          if not ret_val[0]:
   3.186 -            return ret_val[1]
   3.187 +            return ret_val
   3.188  
   3.189          self.setup_opts(options)
   3.190          self.audio_opts = self.setup_audio()
   3.191 @@ -238,38 +276,17 @@
   3.192          self.setup_args(self.args)
   3.193  
   3.194          ret_val = self.setup_socket()
   3.195 -        if not ret_val[0]:
   3.196 -            return ret_val[1]
   3.197 +        return ret_val
   3.198  
   3.199 -        return True
   3.200 +    # setup()
   3.201  
   3.202 -    # setup
   3.203 +    def play_loop(self, conn):
   3.204 +        data = self.pout.read(4096)
   3.205  
   3.206 -    def play(self):
   3.207 +        conn.settimeout(5)
   3.208 +        retry = 0
   3.209  
   3.210 -        log.info("Starting Mencoder: %s" % self.args )
   3.211 -
   3.212 -        try:
   3.213 -            self.mencoder_pid = Popen(self.args, stdout=PIPE, close_fds=True)
   3.214 -        except Exception, e:
   3.215 -            msg = "Could not init Mencoder: %s" % e
   3.216 -            log.error(msg)
   3.217 -            return msg
   3.218 -
   3.219 -        if self.mencoder_old: self.pout = open(self.fifo)
   3.220 -        else: self.pout = self.mencoder_pid.stdout
   3.221 -
   3.222 -        self.child_pid = os.fork()
   3.223 -
   3.224 -        if self.child_pid == 0:
   3.225 -            conn, addr = self.socket.accept()
   3.226 -            log.info("Sending Data to client: %s" % addr[0])
   3.227 -
   3.228 -            data = self.pout.read(4096)
   3.229 -
   3.230 -            conn.settimeout(5)
   3.231 -            retry = 0
   3.232 -
   3.233 +        if not self.transcode_local:
   3.234              while data != "" and retry < 5:
   3.235                  try:
   3.236                      conn.send(data)
   3.237 @@ -285,6 +302,67 @@
   3.238  
   3.239                  data = self.pout.read(4096)
   3.240  
   3.241 +        else:
   3.242 +            local = open(self.transcode_local, "w")
   3.243 +            total = os.path.getsize(self.filename)
   3.244 +            partial = 4096
   3.245 +
   3.246 +            while data != "":
   3.247 +                try:
   3.248 +                    local.write(data)
   3.249 +                except Exception, e:
   3.250 +                    log.error("Write error: %s" % e)
   3.251 +
   3.252 +                data = self.pout.read(4096)
   3.253 +                partial += len(data)
   3.254 +                conn.send("%.2f\n" % (partial * 100 / total) )
   3.255 +
   3.256 +            local.close()
   3.257 +            conn.send("DONE\n")
   3.258 +
   3.259 +        return retry
   3.260 +
   3.261 +    # play_loop()
   3.262 +
   3.263 +
   3.264 +    def play(self):
   3.265 +
   3.266 +        log.info("Starting Mencoder: %s" % self.args )
   3.267 +
   3.268 +        if self.gst_pipe:
   3.269 +            try:
   3.270 +                gst = [ lib.which("gst-launch-0.10") ]
   3.271 +                self.arg_append(gst, "filesrc location=/tmp/mpg/bad_day.mpg")
   3.272 +                self.arg_append(gst, "! fdsink fd=%d" % self.gst_pipe[1])
   3.273 +                self.gst_pid = Popen(gst, stdout=self.gst_pipe[1], close_fds=True)
   3.274 +            except Exception, e:
   3.275 +                msg = "Could not init Gstreamer: %s" % e
   3.276 +                log.error(msg)
   3.277 +                return (False, msg)
   3.278 +
   3.279 +        try:
   3.280 +            if not self.gst_pipe:
   3.281 +                self.stdin = open(self.filename)
   3.282 +            else:
   3.283 +                self.stdin = self.gst_pipe[0]
   3.284 +
   3.285 +            self.mencoder_pid = Popen(self.args, stdin=self.stdin, stdout=PIPE, close_fds=True)
   3.286 +        except Exception, e:
   3.287 +            msg = "Could not init Mencoder: %s" % e
   3.288 +            log.error(msg)
   3.289 +            return (False, msg)
   3.290 +
   3.291 +        if self.mencoder_old: self.pout = open(self.fifo)
   3.292 +        else: self.pout = self.mencoder_pid.stdout
   3.293 +
   3.294 +        self.child_pid = os.fork()
   3.295 +
   3.296 +        if self.child_pid == 0:
   3.297 +            conn, addr = self.socket.accept()
   3.298 +
   3.299 +            log.info("Sending Data to client: %s" % addr[0])
   3.300 +            retry = self.play_loop(conn)
   3.301 +
   3.302              if retry < 5:
   3.303                  log.info("Finished sending Data to client: %s" % addr[0])
   3.304              else:
   3.305 @@ -293,29 +371,39 @@
   3.306              os.kill(self.mencoder_pid.pid, signal.SIGKILL)
   3.307              sys.exit(0)
   3.308  
   3.309 -        return True
   3.310 +        return (True, "")
   3.311  
   3.312 -        # play
   3.313 +    # play()
   3.314  
   3.315  
   3.316      def stop(self):
   3.317          try:
   3.318  
   3.319              if self.mencoder_pid:
   3.320 -                os.kill(self.mencoder_pid.pid, signal.SIGKILL)
   3.321 +                os.kill(self.mencoder_pid.pid, signal.SIGTERM)
   3.322                  self.mencoder_pid = None
   3.323  
   3.324              if self.mplayer_pid:
   3.325 -                os.kill(self.mplayer_pid.pid, signal.SIGKILL)
   3.326 +                os.kill(self.mplayer_pid.pid, signal.SIGTERM)
   3.327                  self.mplayer_pid = None
   3.328  
   3.329 -            if self.socket != None:
   3.330 +            if self.socket:
   3.331                  self.socket.close()
   3.332 +                self.socket = None
   3.333  
   3.334 -            if self.child_pid != None:
   3.335 -                os.kill(self.child_pid, signal.SIGKILL)
   3.336 +            if self.child_pid:
   3.337 +                os.kill(self.child_pid, signal.SIGTERM)
   3.338 +                self.child_pid = None
   3.339 +
   3.340 +            if self.gst_pid:
   3.341 +                os.kill(self.gst_pid.pid, signal.SIGTERM)
   3.342 +                self.gst_pid = None
   3.343 +
   3.344 +            self.do_cleanup()
   3.345 +
   3.346 +            os.wait()
   3.347  
   3.348          except Exception, e:
   3.349              log.error("Stop error: %s" % e)
   3.350  
   3.351 -    # stop
   3.352 +    # stop()