[svn r601] * GMyth-Streamer trunk
authormorphbr
Wed Apr 25 15:53:19 2007 +0100 (2007-04-25)
branchtrunk
changeset 5955c5cff842d57
parent 594 f36075170a4e
child 596 2097ef19ce9a
[svn r601] * GMyth-Streamer
- Updated mencoder, gmencoder and server
gmyth-stream/server/0.2/lib/server.py
gmyth-stream/server/0.2/plugins/transcoders/gmencoder.py
gmyth-stream/server/0.2/plugins/transcoders/mencoder.py
     1.1 --- a/gmyth-stream/server/0.2/lib/server.py	Wed Apr 25 15:50:41 2007 +0100
     1.2 +++ b/gmyth-stream/server/0.2/lib/server.py	Wed Apr 25 15:53:19 2007 +0100
     1.3 @@ -22,6 +22,7 @@
     1.4      log = log.getLogger("gms.transcoder")
     1.5      priority = 0   # negative values have higher priorities
     1.6      name = None # to be used in requests
     1.7 +    status = None
     1.8  
     1.9      def __init__(self, params):
    1.10          self.params = params
    1.11 @@ -237,9 +238,10 @@
    1.12  
    1.13                  for transcoder, request in tl:
    1.14                      stopone = self._create_html_item("%s: %s:%s<a href='%s?request=%s:%s'>"
    1.15 -                                                     "[STOP]</a>" % (
    1.16 +                                                     "[STOP]</a> - Status: %s%%" % (
    1.17                          transcoder, request.client_address[0], request.client_address[1],
    1.18 -                        self.menu["Stop"], request.client_address[0], request.client_address[1]) )
    1.19 +                        self.menu["Stop"], request.client_address[0], request.client_address[1],
    1.20 +                        transcoder.status) )
    1.21  
    1.22              self.wfile.write(utils.getHTML("status",
    1.23                                             {"menu": self._nav_items(),
     2.1 --- a/gmyth-stream/server/0.2/plugins/transcoders/gmencoder.py	Wed Apr 25 15:50:41 2007 +0100
     2.2 +++ b/gmyth-stream/server/0.2/plugins/transcoders/gmencoder.py	Wed Apr 25 15:53:19 2007 +0100
     2.3 @@ -13,7 +13,6 @@
     2.4      name = "gmencoder"
     2.5      priority = -1
     2.6      proc = None
     2.7 -    args = {}
     2.8  
     2.9      def __init__(self, params):
    2.10          server.Transcoder.__init__(self, params)
    2.11 @@ -28,6 +27,9 @@
    2.12      def _parser_params (self):
    2.13          self._insert_param("-i", \
    2.14              "%s://%s" % (self.params_first("uri_prefix", "file"), self.params_first("uri_path", "")))
    2.15 +
    2.16 +        #self._insert_param("-i", self.params_first("uri", ""))
    2.17 +
    2.18          self._insert_param("--video-encode", self.params_first("ve", ""))
    2.19          self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5")
    2.20          self._insert_param("--video-fps", self.params_first("fps", ""))
    2.21 @@ -35,8 +37,8 @@
    2.22          self._insert_param("--video-height", self.params_first("height", ""))
    2.23          self._insert_param("--audio-encode", self.params_first("ae", ""))
    2.24          self._insert_param("--output-element", "fdsink")
    2.25 -    # _parse_params 
    2.26 -    
    2.27 +    # _parse_params
    2.28 +
    2.29      def start(self, outfd):
    2.30          self.opts.append (self.gmencoder_path)
    2.31          self._parser_params ()
    2.32 @@ -44,17 +46,16 @@
    2.33          self.opts.append ("fd=%d" % outfd.fileno())
    2.34  
    2.35          cmd = " ".join(self.opts)
    2.36 -        self.log.info ("Gmemconder: %s", cmd)
    2.37 +        self.log.info ("GMencoder: %s", cmd)
    2.38  
    2.39          try:
    2.40              self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE)
    2.41          except Exception, e:
    2.42 -            self.log.error("Error executing gmencoder: %s" % e)
    2.43 +            self.log.error("Error executing GMencoder: %s" % e)
    2.44              return False
    2.45  
    2.46          try:
    2.47               self.proc.wait()
    2.48 -            
    2.49          except Exception, e:
    2.50              self.log.error("Problems handling data: %s" % e)
    2.51              return False
    2.52 @@ -65,7 +66,7 @@
    2.53  
    2.54      def stop(self):
    2.55          if self.proc:
    2.56 -            self.log.info ("STOP")
    2.57 +            self.log.info ("STOPed GMencoder plugin")
    2.58              try:
    2.59                  self.proc.stdin.write ("QUIT\n")
    2.60              except Exception, e:
     3.1 --- a/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py	Wed Apr 25 15:50:41 2007 +0100
     3.2 +++ b/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py	Wed Apr 25 15:53:19 2007 +0100
     3.3 @@ -2,10 +2,13 @@
     3.4  import shlex
     3.5  import signal
     3.6  import subprocess
     3.7 +import time
     3.8  
     3.9  import lib.utils as utils
    3.10  import lib.server as server
    3.11  
    3.12 +from select import select
    3.13 +
    3.14  __all__ = ("TranscoderMencoder",)
    3.15  
    3.16  class TranscoderMencoder(server.Transcoder):
    3.17 @@ -16,6 +19,9 @@
    3.18      args = {}
    3.19      proc = None
    3.20  
    3.21 +    # only works with avi container
    3.22 +    status = 0
    3.23 +
    3.24      def _setup_params(self):
    3.25          params_first = self.params_first
    3.26  
    3.27 @@ -25,6 +31,11 @@
    3.28          self.args["subtitle"] = params_first("subtitle", False)
    3.29          self.args["format"]   = params_first("format", "")
    3.30          self.args["outfile"]  = params_first("outfile", "-")
    3.31 +        self.args["sendback"] = params_first("sendback", True)
    3.32 +
    3.33 +        # handle sendback variable
    3.34 +        if self.args["sendback"] == "False":
    3.35 +            self.args["sendback"] = False
    3.36  
    3.37          # input_opt
    3.38          uri = params_first("uri", "file://-").split("://")
    3.39 @@ -84,7 +95,11 @@
    3.40  
    3.41      def _setup_mencoder_opts(self, args):
    3.42          args.append(self.mencoder_path)
    3.43 -        args.append(self.args["input"])
    3.44 +
    3.45 +        if self.args["outfile"] == "-" and self.args["type"]:
    3.46 +            args.append(self.args["input"])
    3.47 +        else:
    3.48 +            args.append("-")
    3.49  
    3.50          if self.args["language"]:
    3.51              self._arg_append(args, "-alang %s" % self.args["language"])
    3.52 @@ -99,7 +114,7 @@
    3.53  
    3.54          self._arg_append(args, "-really-quiet")
    3.55          self._arg_append(args, "-o %s" % self.args["outfile"])
    3.56 -        self._arg_append(args, "2> %s" % os.devnull)
    3.57 +        self._arg_append(args, "2>%s" % os.devnull)
    3.58      # _setup_args()
    3.59  
    3.60  
    3.61 @@ -113,6 +128,12 @@
    3.62  
    3.63          elif _type == "dvd":
    3.64              self.args["input"] = "dvd://".join(self.args["input"])
    3.65 +
    3.66 +        elif _type == "myth":
    3.67 +            # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
    3.68 +            # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
    3.69 +            # myth://IP:PORT:type:file
    3.70 +            self.args["gmyth-cat"] = self.args["input"].split(":")
    3.71      # _setup_filename()
    3.72  
    3.73  
    3.74 @@ -129,16 +150,79 @@
    3.75      # __init__()
    3.76  
    3.77  
    3.78 -    def start(self, outfd):
    3.79 -        cmd = " ".join(self.mencoder_opts)
    3.80 -        self.log.debug("Mencoder: %s" % cmd)
    3.81 +    def _check_opened_file(self, stdw, _stdin):
    3.82 +        loop = True
    3.83 +        while loop:
    3.84 +            try:
    3.85 +                return open(self.args["outfile"])
    3.86 +            except:
    3.87 +                os.write(stdw, _stdin.read(1024))
    3.88 +    # _check_opened_file
    3.89 +
    3.90 +
    3.91 +    def _start_outfile(self, outfd):
    3.92 +        finished = False
    3.93 +
    3.94 +        # fix this (not necessary)
    3.95 +        outfd.write("OK")
    3.96 +
    3.97 +        # Configuring stdin
    3.98 +        _stdin = open(self.args["input"])
    3.99 +        size = int(os.path.getsize(self.args["input"]))
   3.100 +        self.status = 0
   3.101 +        total_read = 0
   3.102 +
   3.103 +        # Configuring pipes
   3.104 +        stdr, stdw = os.pipe()
   3.105  
   3.106          try:
   3.107 -            self.proc = subprocess.Popen(self.mencoder_opts, stdout=subprocess.PIPE, close_fds=True)
   3.108 +            self.proc = subprocess.Popen(self.mencoder_opts, stdin=stdr, close_fds=True)
   3.109          except Exception, e:
   3.110              self.log.error("Error executing mencoder: %s" % e)
   3.111              return False
   3.112  
   3.113 +        stdout = self._check_opened_file(stdw, _stdin)
   3.114 +
   3.115 +        try:
   3.116 +            while self.proc and self.proc.poll() == None:
   3.117 +                if not finished:
   3.118 +                    data_in = _stdin.read(4096)
   3.119 +                    if data_in != "":
   3.120 +                        os.write(stdw, data_in)
   3.121 +                        total_read += 4096
   3.122 +                        d = stdout.read(4096)
   3.123 +                        if self.args["sendback"]:
   3.124 +                            outfd.write(d)
   3.125 +                        self.status = total_read * 100 / size
   3.126 +                    else:
   3.127 +                        finished = True
   3.128 +                        os.close(stdw)
   3.129 +
   3.130 +                else:
   3.131 +                    d = stdout.read(4096)
   3.132 +                    if self.args["sendback"] and d != "":
   3.133 +                        outfd.write(d)
   3.134 +
   3.135 +        except Exception, e:
   3.136 +            self.log.error("Problems handling data: %s" % e)
   3.137 +            return False
   3.138 +
   3.139 +        self.log.info("%s: Finished sending data to client" % repr(self))
   3.140 +        if not self.args["sendback"]:
   3.141 +            outfd.write("DONE")
   3.142 +
   3.143 +        return True
   3.144 +    # _start_outfile()
   3.145 +
   3.146 +    def _start(self, outfd):
   3.147 +        try:
   3.148 +            self.proc = subprocess.Popen(self.mencoder_opts,
   3.149 +                                         stdout=subprocess.PIPE, close_fds=True)
   3.150 +        except Exception, e:
   3.151 +            self.log.error("Error executing mencoder: %s" % e)
   3.152 +            return False
   3.153 +
   3.154 +
   3.155          try:
   3.156              while self.proc and self.proc.poll() == None:
   3.157                  d = self.proc.stdout.read(1024)
   3.158 @@ -147,7 +231,19 @@
   3.159              self.log.error("Problems handling data: %s" % e)
   3.160              return False
   3.161  
   3.162 +        self.log.info("%s: Finished sending data to client" % repr(self))
   3.163          return True
   3.164 +    # _start()
   3.165 +
   3.166 +
   3.167 +    def start(self, outfd):
   3.168 +        cmd = " ".join(self.mencoder_opts)
   3.169 +        self.log.debug("Mencoder: %s" % cmd)
   3.170 +
   3.171 +        if self.args["outfile"] == "-":
   3.172 +            return self._start(outfd)
   3.173 +        else:
   3.174 +            return self._start_outfile(outfd)
   3.175      # start()
   3.176  
   3.177