renatofilho@589: import os
renatofilho@589: import shlex
renatofilho@589: import signal
renatofilho@589: import subprocess
renatofilho@672: import time
renatofilho@672: 
renatofilho@672: import select
renatofilho@589: 
renatofilho@589: import lib.utils as utils
renatofilho@589: import lib.server as server
renatofilho@589: 
renatofilho@589: __all__ = ("TranscoderGMencoder",)
renatofilho@589: 
renatofilho@589: class TranscoderGMencoder(server.Transcoder):
renatofilho@589:     gmencoder_path = utils.which("gmencoder")
renatofilho@589:     name = "gmencoder"
renatofilho@589:     priority = -1
renatofilho@589:     proc = None
renatofilho@589: 
renatofilho@589:     def __init__(self, params):
renatofilho@589:         server.Transcoder.__init__(self, params)
renatofilho@589:         self.opts = []
renatofilho@589:     # __init__()
renatofilho@589: 
renatofilho@589:     def _insert_param (self, name, value):
renatofilho@589:         if (value != ""):
renatofilho@589:             self.opts.append(name)
renatofilho@589:             self.opts.append(value)
renatofilho@589: 
renatofilho@589:     def _parser_params (self):
renatofilho@589:         self._insert_param("-i", \
renatofilho@672:             "%s://%s" % (self.params_first("type", "file"), self.params_first("uri", "")))
renatofilho@589:         self._insert_param("--video-encode", self.params_first("ve", ""))
renatofilho@589:         self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5")
renatofilho@589:         self._insert_param("--video-fps", self.params_first("fps", ""))
renatofilho@589:         self._insert_param("--video-width", self.params_first("width", ""))
renatofilho@589:         self._insert_param("--video-height", self.params_first("height", ""))
renatofilho@602:         self._insert_param("--audio-rate", "32000")
renatofilho@589:         self._insert_param("--audio-encode", self.params_first("ae", ""))
morphbr@595:     # _parse_params
morphbr@595: 
renatofilho@589:     def start(self, outfd):
renatofilho@589:         self.opts.append (self.gmencoder_path)
renatofilho@589:         self._parser_params ()
renatofilho@602:         self._insert_param ("-o", "fd://%d" % outfd.fileno())
renatofilho@589: 
renatofilho@589:         cmd = " ".join(self.opts)
morphbr@595:         self.log.info ("GMencoder: %s", cmd)
renatofilho@589: 
renatofilho@589:         try:
renatofilho@672:             self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
renatofilho@589:         except Exception, e:
morphbr@595:             self.log.error("Error executing GMencoder: %s" % e)
renatofilho@589:             return False
renatofilho@589: 
renatofilho@589:         try:
renatofilho@672: 			while (self.proc and self.proc.poll() == None):
renatofilho@672: 				r, w, x = select.select([self.proc.stdout], [], [], 0)
renatofilho@672: 				if self.proc.stdout in r:
renatofilho@672: 					progress = self.proc.stdout.readline()
renatofilho@680: 					self.log.info ("stdout %s" % progress)
renatofilho@672: 					if (progress.find ("PROGRESS") >= 0):
renatofilho@672: 						self.status = progress.split (":")[1]
renatofilho@680: 					#if (progress.find ("DONE") >= 0):
renatofilho@680: 					#	break
renatofilho@672: 			self.log.info ("Process exit")
renatofilho@589:         except Exception, e:
renatofilho@589:             self.log.error("Problems handling data: %s" % e)
renatofilho@589:             return False
renatofilho@589: 
renatofilho@589:         return True
renatofilho@589:     # start()
renatofilho@589: 
renatofilho@589: 
renatofilho@589:     def stop(self):
renatofilho@589:         if self.proc:
renatofilho@672:             self.log.info ("STOPED GMencoder plugin")
renatofilho@589:             try:
renatofilho@589:                 self.proc.stdin.write ("QUIT\n")
renatofilho@589:             except Exception, e:
renatofilho@589:                 pass
renatofilho@589: 
renatofilho@589:             try:
renatofilho@589:                 self.proc.wait()
renatofilho@589:             except Exception, e:
renatofilho@589:                 pass
renatofilho@589: 
renatofilho@589:             self.proc = None
renatofilho@589:     # stop()
renatofilho@589: 
renatofilho@589: # TranscoderGMencoder