TranscodingSetup : add bitrate/framerate support.
authorhschang <chang@dev3>
Wed, 3 Jul 2013 05:54:07 +0000 (14:54 +0900)
committerhschang <chang@dev3>
Wed, 3 Jul 2013 07:32:25 +0000 (16:32 +0900)
lib/python/Plugins/SystemPlugins/TransCodingSetup/plugin.py

index 7bb47a4..ec832bb 100755 (executable)
@@ -1,6 +1,6 @@
 from Screens.Screen import Screen
 from Components.ConfigList import ConfigListScreen
 from Screens.Screen import Screen
 from Components.ConfigList import ConfigListScreen
-from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSelection
+from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSelection, ConfigInteger
 from Components.ActionMap import ActionMap
 from Screens.MessageBox import MessageBox
 from Components.Sources.StaticText import StaticText
 from Components.ActionMap import ActionMap
 from Screens.MessageBox import MessageBox
 from Components.Sources.StaticText import StaticText
@@ -10,10 +10,6 @@ from enigma import eTimer
 from os import system as os_system
 from __init__ import _
 
 from os import system as os_system
 from __init__ import _
 
-config.plugins.transcodingsetup = ConfigSubsection()
-config.plugins.transcodingsetup.transcoding = ConfigSelection(default = "disabled", choices = [ ("enabled", _("enabled")), ("disabled", _("disabled"))] )
-config.plugins.transcodingsetup.port = ConfigSelection(default = "8002", choices = [ ("8001", "8001"), ("8002", "8002")] )
-
 error_msg ={
        -1 : "File not exist - /proc/stb/encoder/enable.",
        -2 : "File not exist - /etc/inetd.conf.",
 error_msg ={
        -1 : "File not exist - /proc/stb/encoder/enable.",
        -2 : "File not exist - /etc/inetd.conf.",
@@ -21,43 +17,72 @@ error_msg ={
        -4 : "File open error - /etc/inetd.conf.",
        -5 : "Set encoder error.",
        -6 : "Set port error.",
        -4 : "File open error - /etc/inetd.conf.",
        -5 : "Set encoder error.",
        -6 : "Set port error.",
-       -7 : "Setting value is incorrect."
+       -7 : "Setting value is incorrect.",
+       -8 : "Set encoder bitrate error.",
+       -9 : "Set encoder framerate error.",
 }
 }
+TranscodingConfigList = []
+
 class TranscodingSetupInit:
        def __init__(self):
 class TranscodingSetupInit:
        def __init__(self):
+               self.createConfigList()
+               self.createConfig()
                self.transcoding_value = config.plugins.transcodingsetup.transcoding.value
                self.transcoding_value = config.plugins.transcodingsetup.transcoding.value
-               if self.transcoding_value == "disabled":
+               if self.transcoding_value == "disable":
                        self.port_value = "8002"
                else:
                        self.port_value = config.plugins.transcodingsetup.port.value
                self.transcoding_old = config.plugins.transcodingsetup.transcoding.value
                        self.port_value = "8002"
                else:
                        self.port_value = config.plugins.transcodingsetup.port.value
                self.transcoding_old = config.plugins.transcodingsetup.transcoding.value
-               ret = self.setTranscoding(self.transcoding_value, self.port_value)
-               if ret is not None and ret < 0:
-                       print "[TranscodingSetup] set failed!(%s, %s)"%(self.transcoding_value, self.port_value)
+               res = self.setTranscoding(self.transcoding_value, self.port_value)
+               if res is not None and res < 0:
+                       print "[TranscodingSetup] set failed!(%s, %s, %d)"%(self.transcoding_value, self.port_value, res)
+
+       def createConfigList(self):
+               global TranscodingConfigList
+               configList = [
+                       ["Bitrate", "/proc/stb/encoder/0/bitrate", -8],
+                       ["Framerate", "/proc/stb/encoder/0/framerate", -9]
+               ]
+               for x in configList:
+                       if fileExists(x[1]):
+                               TranscodingConfigList.append(x)
+
+       def createConfig(self):
+               config.plugins.transcodingsetup = ConfigSubsection()
+               config.plugins.transcodingsetup.transcoding = ConfigSelection(default = "disable", choices = [ ("enable", _("enable")), ("disable", _("disable"))] )
+               config.plugins.transcodingsetup.port = ConfigSelection(default = "8002", choices = [ ("8001", "8001"), ("8002", "8002")] )
+               global TranscodingConfigList
+               for x in TranscodingConfigList:
+                       if x[0] == "Bitrate":
+                               config.plugins.transcodingsetup.bitrate = ConfigInteger(default = 2000000, limits = (100000, 5000000))
+                               x.append(config.plugins.transcodingsetup.bitrate)
+                       elif x[0] == "Framerate":
+                               config.plugins.transcodingsetup.framerate = ConfigSelection(default = "30000", choices = [ ("23976", _("23976")), ("24000", _("24000")), ("29970", _("29970")), ("30000", _("30000")), ("59940", _("59940")), ("60000", _("60000"))])
+                               x.append(config.plugins.transcodingsetup.framerate)
 
        def setTranscoding(self, transcoding, port):
 
        def setTranscoding(self, transcoding, port):
-               if not self.getModel():
-                       print "This plugin is only supported for solo2/duo2."
-                       return -8
-               if transcoding not in ["enabled","disabled"] or port not in ["8001","8002"]:
+               if transcoding not in ["enable","disable"] or port not in ["8001","8002"]:
                        print "Input error."
                        return -7
                if not fileExists("/proc/stb/encoder/enable"):
                        return -1
                elif not fileExists("/etc/inetd.conf"):
                        return -2
                        print "Input error."
                        return -7
                if not fileExists("/proc/stb/encoder/enable"):
                        return -1
                elif not fileExists("/etc/inetd.conf"):
                        return -2
-               if self.setEncoder(transcoding) < 0:
+               res = self.setEncoderExtra()
+               if res < 0:
+                       return res
+               if self.setEncoderEnable(transcoding) < 0:
                        return -5
                res = self.setPort(port)
                if res < 0:
                        return -5
                res = self.setPort(port)
                if res < 0:
-                       self.setEncoder(self.transcoding_old)
+                       self.setEncoderEnable(self.transcoding_old)
                        return res
                else:
                        self.inetdRestart()
                return res
 
                        return res
                else:
                        self.inetdRestart()
                return res
 
-       def setEncoder(self,mode = "disabled"):
-               print "<TranscodingSetup> set encoder %s" % mode
+       def setEncoderEnable(self,mode = "disable"):
+               print "<TranscodingSetup> set encoder %s" % mode
                mode = mode.strip(' ').strip('\n')
                try:
                        fd = open("/proc/stb/encoder/enable",'r')
                mode = mode.strip(' ').strip('\n')
                try:
                        fd = open("/proc/stb/encoder/enable",'r')
@@ -75,11 +100,11 @@ class TranscodingSetupInit:
 #                              print "<TranscodingSetup> can not setting."
                                return -1
                except:
 #                              print "<TranscodingSetup> can not setting."
                                return -1
                except:
-#                      print "setEncoder exception error"
+#                      print "setEncoderEnable exception error"
                        return -1
 
        def setPort(self, port = "8001"):
                        return -1
 
        def setPort(self, port = "8001"):
-               print "<TranscodingSetup> set port %s" % port
+               print "<TranscodingSetup> set port %s" % port
                try:
                        fp = file('/etc/inetd.conf', 'r')
                        datas = fp.readlines()
                try:
                        fp = file('/etc/inetd.conf', 'r')
                        datas = fp.readlines()
@@ -116,17 +141,37 @@ class TranscodingSetupInit:
                if fileExists("/etc/init.d/inetd"):
                        os_system("/etc/init.d/inetd restart")
 
                if fileExists("/etc/init.d/inetd"):
                        os_system("/etc/init.d/inetd restart")
 
-       def getModel(self):
-               if fileExists("/proc/stb/info/vumodel"):
-                       vumodel = open("/proc/stb/info/vumodel")
-                       info=vumodel.read().strip()
-                       vumodel.close()
-                       if info in ["solo2", "duo2"]:
-                               return True
-                       else:
-                               return False
+       def setEncoderExtra(self):
+               global TranscodingConfigList
+               for x in TranscodingConfigList:
+                       if self.setEncoder(x[1], x[3].value):
+                               return x[2]
+               return 0
+
+       def setEncoder(self, procPath, value):
+               print "<TranscodingSetup> set %s "%procPath, value
+               if not fileExists(procPath):
+                       return -1
+               if isinstance(value, str):
+                       value = value.strip(' ').strip('\n')
                else:
                else:
-                       return False
+                       value = str(value)
+               try:
+                       fd = open(procPath,'r')
+                       old_value = fd.read().strip(' ').strip('\n')
+                       fd.close()
+                       if old_value != value:
+                               fd = open(procPath,'w')
+                               fd.write(value)
+                               fd.close()
+                               fd = open(procPath,'r')
+                               encoder_value = fd.read().strip(' ').strip('\n')
+                               fd.close()
+                               if encoder_value != value:
+                                       return -1
+                       return 0
+               except:
+                       return -1
 
 class TranscodingSetup(Screen,ConfigListScreen, TranscodingSetupInit):
        skin =  """
 
 class TranscodingSetup(Screen,ConfigListScreen, TranscodingSetupInit):
        skin =  """
@@ -139,9 +184,31 @@ class TranscodingSetup(Screen,ConfigListScreen, TranscodingSetupInit):
                        <widget source="text" render="Label" position="20,140" size="370,130" font="Regular;18" halign="center" valign="center" />
                </screen>
                """
                        <widget source="text" render="Label" position="20,140" size="370,130" font="Regular;18" halign="center" valign="center" />
                </screen>
                """
+       skin_ext =  """
+               <screen position="center,center" size="400,320" title="Transcoding Setup" >
+                       <ePixmap pixmap="skin_default/buttons/red.png" position="30,10" size="140,40" alphatest="on" />
+                       <ePixmap pixmap="skin_default/buttons/green.png" position="230,10" size="140,40" alphatest="on" />
+                       <widget source="key_red" render="Label" position="30,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
+                       <widget source="key_green" render="Label" position="230,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
+                       <widget name="config" zPosition="2" position="5,70" size="390,120" scrollbarMode="showOnDemand" transparent="1" />
+                       <widget source="text" render="Label" position="20,190" size="370,130" font="Regular;18" halign="center" valign="center" />
+               </screen>
+               """
 
        def __init__(self,session):
 
        def __init__(self,session):
-               Screen.__init__(self,session)
+               if fileExists("/proc/stb/encoder/0/framerate"):
+                       self.skin = TranscodingSetup.skin_ext
+                       Screen.__init__(self,session)
+                       self.skinName = "TranscodingSetup_ext"
+               else:
+                       Screen.__init__(self,session)
+
+               if self.getModel() == "duo2":
+                       TEXT = _("Transcoding can be started when there is no corresponding channel recordings.")
+                       TEXT += _("\nWhen transcoding, PIP is disabled.")
+               else:
+                       TEXT = _("Transcoding can be started when there is no corresponding channel recordings.")
+                       TEXT += _("\nWhen transcoding, both PIP and analog video outputs are disabled.")
                self.session = session
                self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
                {
                self.session = session
                self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
                {
@@ -152,37 +219,48 @@ class TranscodingSetup(Screen,ConfigListScreen, TranscodingSetupInit):
                }, -2)
                self.list = []
                ConfigListScreen.__init__(self, self.list,session = self.session)
                }, -2)
                self.list = []
                ConfigListScreen.__init__(self, self.list,session = self.session)
-               TEXT = _("Transcoding can be started when there is no corresponding channel recordings.")
-               TEXT += _("\nWhen transcoding, both PIP and analog video outputs are disabled.")
                self["key_red"] = StaticText(_("Cancel"))
                self["key_green"] = StaticText(_("Ok"))
                self["text"] = StaticText(_("%s")%TEXT)
                self.createSetup()
                self["key_red"] = StaticText(_("Cancel"))
                self["key_green"] = StaticText(_("Ok"))
                self["text"] = StaticText(_("%s")%TEXT)
                self.createSetup()
-               self.onLayoutFinish.append(self.checkModel)
-               self.checkModelTimer = eTimer()
-               self.checkModelTimer.callback.append(self.invalidmodel)
+               self.onLayoutFinish.append(self.checkEncoder)
+               self.invaliedModelTimer = eTimer()
+               self.invaliedModelTimer.callback.append(self.invalidmodel)
+
+       def getModel(self):
+               if fileExists("/proc/stb/info/vumodel"):
+                       fd = open("/proc/stb/info/vumodel")
+                       vumodel=fd.read().strip()
+                       fd.close()
+                       return vumodel
+               else:
+                       return ""
 
 
-       def checkModel(self):
-               if not self.getModel():
-                       self.checkModelTimer.start(1000,True)
+       def checkEncoder(self):
+               if not fileExists("/proc/stb/encoder/enable"):
+                       self.invaliedModelTimer.start(100,True)
 
        def invalidmodel(self):
 
        def invalidmodel(self):
-                       self.session.openWithCallback(self.close, MessageBox, _("This plugin is available on SOLO2/DUO2"), MessageBox.TYPE_ERROR)
+               self.session.openWithCallback(self.close, MessageBox, _("This model is not support transcoding."), MessageBox.TYPE_ERROR)
 
        def createSetup(self):
 
        def createSetup(self):
+               global TranscodingConfigList
                self.list = []
                self.transcoding = getConfigListEntry(_("Transcoding"), config.plugins.transcodingsetup.transcoding)
                self.port = getConfigListEntry(_("Port"), config.plugins.transcodingsetup.port)
                self.list.append( self.transcoding )
                self.list = []
                self.transcoding = getConfigListEntry(_("Transcoding"), config.plugins.transcodingsetup.transcoding)
                self.port = getConfigListEntry(_("Port"), config.plugins.transcodingsetup.port)
                self.list.append( self.transcoding )
-               if config.plugins.transcodingsetup.transcoding.value == "enabled":
+               if config.plugins.transcodingsetup.transcoding.value == "enable":
                        self.list.append( self.port )
                        self.list.append( self.port )
+                       for x in TranscodingConfigList:
+                               self.list.append(getConfigListEntry(_(x[0]), x[3]))
+
                self["config"].list = self.list
                self["config"].l.setList(self.list)
 
        def keySave(self):
                transcoding = config.plugins.transcodingsetup.transcoding.value
                port = config.plugins.transcodingsetup.port.value
                self["config"].list = self.list
                self["config"].l.setList(self.list)
 
        def keySave(self):
                transcoding = config.plugins.transcodingsetup.transcoding.value
                port = config.plugins.transcodingsetup.port.value
-               print "<TranscodingSetup> Transcoding %s(port : %s)"%(transcoding, port)
+#              print "<TranscodingSetup> Transcoding %s(port : %s)"%(transcoding, port)
                ret = self.setupTranscoding(transcoding, port)
                if ret is not None and ret <0 :
                        self.resetConfig()
                ret = self.setupTranscoding(transcoding, port)
                if ret is not None and ret <0 :
                        self.resetConfig()
@@ -190,7 +268,7 @@ class TranscodingSetup(Screen,ConfigListScreen, TranscodingSetupInit):
                        self.session.openWithCallback(self.close, MessageBox, _("Failed, Encoder %s\n(%s).")%(transcoding, error_msg[ret]), MessageBox.TYPE_ERROR)
                else:
                        self.saveAll()
                        self.session.openWithCallback(self.close, MessageBox, _("Failed, Encoder %s\n(%s).")%(transcoding, error_msg[ret]), MessageBox.TYPE_ERROR)
                else:
                        self.saveAll()
-                       if transcoding == "enabled" and port == "8001" :
+                       if transcoding == "enable" and port == "8001" :
                                text = "PC Streaming is replaced with mobile streaming."
                                self.session.openWithCallback(self.close, MessageBox, _("OK. Encoder %s.\n%s")%(transcoding,text), MessageBox.TYPE_INFO)
                        else:
                                text = "PC Streaming is replaced with mobile streaming."
                                self.session.openWithCallback(self.close, MessageBox, _("OK. Encoder %s.\n%s")%(transcoding,text), MessageBox.TYPE_INFO)
                        else:
@@ -202,7 +280,7 @@ class TranscodingSetup(Screen,ConfigListScreen, TranscodingSetupInit):
                        x[1].cancel()
 
        def setupTranscoding(self, transcoding = None, port = None):
                        x[1].cancel()
 
        def setupTranscoding(self, transcoding = None, port = None):
-               if transcoding == "disabled":
+               if transcoding == "disable":
                        config.plugins.transcodingsetup.port.value = "8002"
                        port = "8002"
                return self.setTranscoding(transcoding, port)
                        config.plugins.transcodingsetup.port.value = "8002"
                        port = "8002"
                return self.setTranscoding(transcoding, port)