1 from Screens.Screen import Screen
2 from Components.ConfigList import ConfigListScreen
3 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSelection, ConfigInteger, integer_limits
4 from Components.ActionMap import ActionMap
5 from Screens.MessageBox import MessageBox
6 from Components.Sources.StaticText import StaticText
7 from Plugins.Plugin import PluginDescriptor
8 from Tools.Directories import fileExists
9 from enigma import eTimer, getDesktop
10 from os import system as os_system, path as os_path, listdir as os_listdir
11 from __init__ import _
13 class TconfigSelection(ConfigSelection):
14 def __init__(self, encoder, choices, default = None):
15 self.encoder = encoder
16 ConfigSelection.__init__(self, choices, default)
18 class TconfigInteger(ConfigInteger):
19 def __init__(self, encoder, default, limits = integer_limits):
20 self.encoder = encoder
21 ConfigInteger.__init__(self, default, limits)
24 filename = "/proc/stb/info/vumodel"
25 if fileExists(filename):
26 return file(filename).read().strip()
29 def getProcValue(procPath):
30 fd = open(procPath,'r')
31 curValue = fd.read().strip(' ').strip('\n')
33 # print "[TranscodingSetup] get %s from %s" % (curValue, procPath)
36 def setProcValue(procPath, value):
37 # print "[TranscodingSetup] set %s to %s" % (procPath, value)
38 fd = open(procPath,'w')
42 def getProcPath(encoder, configName):
44 "bitrate" : "bitrate",
45 "framerate" : "framerate",
46 "resolution" : "display_format",
47 "aspectratio" : "aspectratio",
48 "audiocodec" : "audio_codec",
49 "videocodec" : "video_codec",
50 "gopframeb" : "gop_frameb",
51 "gopframep" : "gop_framep",
53 "profile" : "profile",
57 return "/proc/stb/encoder/%s/%s" % (encoder, _configName)
59 def checkSupportAdvanced():
60 if fileExists( getProcPath(0, "aspectratio") ):
64 config.plugins.transcodingsetup = ConfigSubsection()
65 config.plugins.transcodingsetup.transcoding = ConfigSelection(default = "enable", choices = [ ("enable", _("enable")), ("disable", _("disable"))] )
66 config.plugins.transcodingsetup.port = ConfigSelection(default = "8002", choices = [ ("8001", "8001"), ("8002", "8002")] )
68 def getAttr(attr, encoder):
69 return getattr(config.plugins.transcodingsetup, encoder == '0' and attr or "%s_%s"%(attr, encoder))
71 def hasAttr(attr, encoder):
72 return hasattr(config.plugins.transcodingsetup, encoder == '0' and attr or "%s_%s"%(attr, encoder))
74 def setAttr(attr, encoder, value):
75 setattr(config.plugins.transcodingsetup, encoder == '0' and attr or "%s_%s"%(attr, encoder), value)
77 def createTransCodingConfig(encoder):
78 if fileExists( getProcPath(encoder ,"bitrate") ):
80 if vumodel in ("solo2", "solose"):
81 choice = TconfigInteger(encoder, default = 400000, limits = (50000, 1000000))
83 choice = TconfigInteger(encoder, default = 2000000, limits = (100000, 10000000))
84 setAttr("bitrate", encoder, choice)
86 if fileExists( getProcPath(encoder ,"framerate") ):
87 choice = TconfigSelection(encoder, default = "30000", choices = [ ("23976", _("23976")), ("24000", _("24000")), ("25000", _("25000")), ("29970", _("29970")), ("30000", _("30000")), ("50000", _("50000")), ("59940", _("59940")), ("60000", _("60000"))] )
88 setAttr("framerate", encoder, choice)
90 if checkSupportAdvanced() and (hasAttr("bitrate", encoder) or hasAttr("framerate", encoder)):
91 choice = TconfigSelection(encoder, default = "Off", choices = [ ("On", _("On")), ("Off", _("Off")) ] )
92 setAttr("automode", encoder, choice)
94 if fileExists( getProcPath(encoder, "resolution") ):
95 choice = TconfigSelection(encoder, default = "480p", choices = [ ("480p", _("480p")), ("576p", _("576p")), ("720p", _("720p")), ("320x240", _("320x240")), ("160x120", _("160x120")) ] )
96 setAttr("resolution", encoder, choice)
98 if fileExists( getProcPath(encoder, "aspectratio") ):
99 choice = TconfigSelection(encoder, default = "1", choices = [ ("0", _("auto")), ("1", _("4x3")), ("2", _("16x9")) ] )
100 setAttr("aspectratio", encoder, choice)
102 if fileExists( getProcPath(encoder, "audiocodec") ):
103 choice = TconfigSelection(encoder, default = "aac", choices = [("mpg", _("mpg")), ("mp3", _("mp3")), ("aac", _("aac")), ("aac+", _("aac+")), ("aac+loas", _("aac+loas")), ("aac+adts", _("aac+adts")), ("ac3", _("ac3"))] )
104 setAttr("audiocodec", encoder, choice)
106 if fileExists( getProcPath(encoder, "videocodec") ):
107 choice = TconfigSelection(encoder, default = "h264", choices = [ ("h264", _("h264")) ] )
108 setAttr("videocodec", encoder, choice)
110 if fileExists( getProcPath(encoder, "gopframeb") ):
111 choice = TconfigInteger(encoder, default = 0, limits = (0, 60))
112 setAttr("gopframeb", encoder, choice)
114 if fileExists( getProcPath(encoder, "gopframep") ):
115 choice = TconfigInteger(encoder, default = 29, limits = (0, 60))
116 setAttr("gopframep", encoder, choice)
118 if fileExists( getProcPath(encoder, "level") ):
119 choice = TconfigSelection(encoder, default = "3.1", choices = [("1.0", _("1.0")), ("2.0", _("2.0")),
120 ("2.1", _("2.1")), ("2.2", _("2.2")), ("3.0", _("3.0")), ("3.1", _("3.1")),
121 ("3.2", _("3.2")), ("4.0", _("4.0")), ("4.1", _("4.1")), ("4.2", _("4.2")),
122 ("5.0", _("5.0")), ("low", _("low")), ("main", _("main")), ("high", _("high"))] )
123 setAttr("level", encoder, choice)
125 if fileExists( getProcPath(encoder, "profile") ):
126 choice = TconfigSelection(encoder, default = "baseline", choices = [("baseline", _("baseline")), ("simple", _("simple")), ("main", _("main")), ("high", _("high")), ("advanced simple", _("advancedsimple"))] )
127 setAttr("profile", encoder, choice)
131 encoderPath = "/proc/stb/encoder"
132 for encoder in os_listdir(encoderPath):
133 encPath = os_path.join(encoderPath, encoder)
134 if not os_path.isdir(encPath):
136 if fileExists(os_path.join(encPath, "bitrate")):
137 encoders.append(encoder)
138 createTransCodingConfig(encoder)
140 if len(encoders) > 1:
143 for encoder in encoders:
144 choices.append((encoder, encoder))
145 config.plugins.transcodingsetup.encoder = ConfigSelection(default = '0', choices = choices )
147 transcodingsetupinit = None
148 class TranscodingSetupInit:
150 self.pluginsetup = None
151 config.plugins.transcodingsetup.port.addNotifier(self.setPort)
153 for encoder in encoders:
154 if hasAttr("automode", encoder):
155 if getAttr("automode", encoder).value == "On":
156 getAttr("automode", encoder).addNotifier(self.setAutomode)
158 if hasAttr("bitrate", encoder):
159 getAttr("bitrate", encoder).addNotifier(self.setBitrate, False)
161 if hasAttr("framerate", encoder):
162 getAttr("framerate", encoder).addNotifier(self.setFramerate, False)
165 getAttr("automode", encoder).addNotifier(self.setAutomode, False)
166 if hasAttr("bitrate", encoder):
167 getAttr("bitrate", encoder).addNotifier(self.setBitrate)
169 if hasAttr("framerate", encoder):
170 getAttr("framerate", encoder).addNotifier(self.setFramerate)
172 if hasAttr("resolution", encoder):
173 getAttr("resolution", encoder).addNotifier(self.setResolution)
175 if hasAttr("aspectratio", encoder):
176 getAttr("aspectratio", encoder).addNotifier(self.setAspectRatio)
178 if hasAttr("audiocodec", encoder):
179 getAttr("audiocodec", encoder).addNotifier(self.setAudioCodec)
181 if hasAttr("videocodec", encoder):
182 getAttr("videocodec", encoder).addNotifier(self.setVideoCodec)
184 if hasAttr("gopframeb", encoder):
185 getAttr("gopframeb", encoder).addNotifier(self.setGopFrameB)
187 if hasAttr("gopframep", encoder):
188 getAttr("gopframep", encoder).addNotifier(self.setGopFrameP)
190 if hasAttr("level", encoder):
191 getAttr("level", encoder).addNotifier(self.setLevel)
193 if hasAttr("profile", encoder):
194 getAttr("profile", encoder).addNotifier(self.setProfile)
196 def setConfig(self, procPath, value):
197 if not fileExists(procPath):
199 if isinstance(value, str):
200 value = value.strip(' ').strip('\n')
204 oldValue = getProcValue(procPath)
205 if oldValue != value:
206 # print "[TranscodingSetup] set %s "%procPath, value
207 setProcValue(procPath, value)
208 setValue = getProcValue(procPath)
209 if value != setValue:
210 print "[TranscodingSetup] set failed. (%s > %s)" % ( value, procPath )
214 print "setConfig exception error (%s > %s)" % ( value, procPath )
218 def setPort(self, configElement):
219 port = configElement.value
220 port2 = (port == "8001") and "8002" or "8001"
222 # print "[TranscodingSetup] set port ",port
225 oldConfigData = file('/etc/inetd.conf').read()
226 for L in oldConfigData.splitlines():
229 newConfigData += L + '\n'
233 if LL[5] == '/usr/bin/streamproxy':
235 elif LL[5] == '/usr/bin/transtreamproxy':
237 newConfigData += ''.join(str(X) + " " for X in LL) + '\n'
239 if newConfigData.find("transtreamproxy") == -1:
240 newConfigData += port + " stream tcp nowait root /usr/bin/transtreamproxy transtreamproxy\n"
241 file('/etc/inetd.conf', 'w').write(newConfigData)
243 self.showMessage("Set port failed.", MessageBox.TYPE_ERROR)
248 msg = "Set port OK.\nPC Streaming is replaced with mobile streaming."
249 self.showMessage(msg, MessageBox.TYPE_INFO)
251 def setupConfig(self, configElement, procPath):
252 # print "[TranscodingSetup] set %s to %s" % ( procPath, configElement.value )
253 configValue = configElement.value
254 if self.setConfig(procPath, configValue):
255 # set config failed, reset to current proc value
256 self.getConfigFromProc(procPath, configElement)
257 self.showMessage("Set %s failed." % (procPath), MessageBox.TYPE_ERROR)
259 def getConfigFromProc(self, procPath, configElement):
260 curValue = getProcValue(procPath)
261 if isinstance(configElement.value, int): # is int ?
262 curValue = int(curValue)
263 configElement.value = curValue
266 def setAutomode(self, configElement):
267 configName = "AutoMode"
268 # print "[TranscodingSetup] setAutomode, configName %s, value %s" % ( configName, configElement.value )
269 if configElement.value == "On":
271 if ((hasAttr("bitrate", configElement.encoder) and
272 self.setConfig(getProcPath(configElement.encoder ,"bitrate"), autoValue) ) or
273 (hasAttr("framerate", configElement.encoder) and
274 self.setConfig(getProcPath(configElement.encoder ,"framerate"), autoValue) ) ):
275 configElement.value = "Off" # set config failed, reset to previous value
277 self.showMessage("Set %s failed." % (configName), MessageBox.TYPE_ERROR)
279 if hasAttr("bitrate", configElement.encoder):
280 self.setBitrate(getAttr("bitrate", configElement.encoder))
281 if hasAttr("framerate", configElement.encoder):
282 self.setFramerate(getAttr("framerate", configElement.encoder))
284 def setBitrate(self, configElement):
285 self.setupConfig(configElement, getProcPath(configElement.encoder ,"bitrate"))
287 def setFramerate(self, configElement):
288 self.setupConfig(configElement, getProcPath(configElement.encoder ,"framerate"))
290 def setResolution(self, configElement):
291 resolution = configElement.value
292 if resolution in [ "320x240", "160x120" ]:
293 (width, height) = tuple(resolution.split('x'))
294 self.setConfig(getProcPath(configElement.encoder ,"resolution"), "custom")
295 self.setConfig(getProcPath(configElement.encoder ,"width"), width)
296 self.setConfig(getProcPath(configElement.encoder ,"height"), height)
298 self.setupConfig(configElement, getProcPath(configElement.encoder ,"resolution"))
300 def setAspectRatio(self, configElement):
301 self.setupConfig(configElement, getProcPath(configElement.encoder ,"aspectratio"))
303 def setAudioCodec(self, configElement):
304 self.setupConfig(configElement, getProcPath(configElement.encoder ,"audiocodec"))
306 def setVideoCodec(self, configElement):
307 self.setupConfig(configElement, getProcPath(configElement.encoder ,"videocodec"))
309 def setGopFrameB(self, configElement):
310 self.setupConfig(configElement, getProcPath(configElement.encoder ,"gopframeb"))
312 def setGopFrameP(self, configElement):
313 self.setupConfig(configElement, getProcPath(configElement.encoder ,"gopframep"))
315 def setLevel(self, configElement):
316 self.setupConfig(configElement, getProcPath(configElement.encoder ,"level"))
318 def setProfile(self, configElement):
319 self.setupConfig(configElement, getProcPath(configElement.encoder ,"profile"))
321 def inetdRestart(self):
322 if fileExists("/etc/init.d/inetd"):
323 os_system("/etc/init.d/inetd restart")
324 elif fileExists("/etc/init.d/inetd.busybox"):
325 os_system("/etc/init.d/inetd.busybox restart")
327 def showMessage(self, msg, msgType):
329 self.pluginsetup.showMessage(msg, msgType)
331 class TranscodingSetup(Screen, ConfigListScreen):
332 size = getDesktop(0).size()
333 if checkSupportAdvanced():
334 if size.width() > 750:
341 pos_h = ( size_h , size_h - 150 , (size_h - 150) + 70, (size_h - 150) + 70 + 60 )
343 <screen position="center,center" size="600,%d">
344 <ePixmap pixmap="skin_default/buttons/red.png" position="5,0" size="140,40" alphatest="on" />
345 <ePixmap pixmap="skin_default/buttons/green.png" position="155,0" size="140,40" alphatest="on" />
346 <ePixmap pixmap="skin_default/buttons/yellow.png" position="305,0" size="140,40" alphatest="on" />
347 <ePixmap pixmap="skin_default/buttons/blue.png" position="455,0" size="140,40" alphatest="on" />
348 <widget source="key_red" render="Label" position="5,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
349 <widget source="key_green" render="Label" position="155,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
350 <widget source="key_yellow" render="Label" position="305,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" foregroundColor="#ffffff" transparent="1" />
351 <widget source="key_blue" render="Label" position="455,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" foregroundColor="#ffffff" transparent="1" />
352 <widget name="config" zPosition="2" position="25,70" size="560,%d" scrollbarMode="showOnDemand" transparent="1" />
353 <widget source="description" render="Label" position="20,%d" size="540,60" font="Regular;20" halign="center" valign="center" />
354 <widget source="text" render="Label" position="20,%d" size="540,20" font="Regular;22" halign="center" valign="center" />
359 <screen position="center,center" size="600,%d">
360 <ePixmap pixmap="skin_default/buttons/red.png" position="40,0" size="140,40" alphatest="on" />
361 <ePixmap pixmap="skin_default/buttons/green.png" position="230,0" size="140,40" alphatest="on" />
362 <ePixmap pixmap="skin_default/buttons/yellow.png" position="420,0" size="140,40" alphatest="on" />
363 <widget source="key_red" render="Label" position="40,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
364 <widget source="key_green" render="Label" position="230,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
365 <widget source="key_yellow" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" foregroundColor="#ffffff" transparent="1" />
366 <widget name="config" zPosition="2" position="25,70" size="560,%d" scrollbarMode="showOnDemand" transparent="1" />
367 <widget source="description" render="Label" position="20,%d" size="540,60" font="Regular;20" halign="center" valign="center" />
368 <widget source="text" render="Label" position="20,%d" size="540,20" font="Regular;22" halign="center" valign="center" />
372 def __init__(self,session):
373 Screen.__init__(self,session)
374 self.session = session
375 self.setTitle(_("Transcoding Setup"))
377 if checkSupportAdvanced():
378 self.skin = TranscodingSetup.skin_advanced
380 self.skin = TranscodingSetup.skin_normal
382 if getModel() == "solo2":
383 TEXT = _("Transcoding and PIP are mutually exclusive.")
385 TEXT = _("2nd transcoding and PIP are mutually exclusive.")
386 self["text"] = StaticText(_("%s")%TEXT)
388 self["key_red"] = StaticText(_("Cancel"))
389 self["key_green"] = StaticText(_("Save"))
390 self["key_yellow"] = StaticText(_("Default"))
391 self["key_blue"] = StaticText(_("Advanced"))
392 self["description"] = StaticText(_("Transcoding Setup"))
394 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
396 "cancel" : self.keyCancel,
397 "red" : self.keyCancel,
398 "green" : self.keySave,
399 "yellow" : self.KeyDefault,
400 "blue" : self.keyBlue,
404 ConfigListScreen.__init__(self, self.list,session = self.session)
405 self.setupMode = "Normal" # Normal / Advanced
409 self.onLayoutFinish.append(self.checkEncoder)
410 self.invaliedModelTimer = eTimer()
411 self.invaliedModelTimer.callback.append(self.invalidmodel)
412 global transcodingsetupinit
413 transcodingsetupinit.pluginsetup = self
414 self.onClose.append(self.onClosed)
417 transcodingsetupinit.pluginsetup = None
419 def checkEncoder(self):
420 if not fileExists("/proc/stb/encoder/enable"):
421 self.invaliedModelTimer.start(100,True)
423 def invalidmodel(self):
424 self.session.openWithCallback(self.close, MessageBox, _("This model is not support transcoding."), MessageBox.TYPE_ERROR)
426 def createSetup(self):
428 self.list.append(getConfigListEntry(_("Port"), config.plugins.transcodingsetup.port))
431 if len(encoders) == 1:
432 encoder = encoders[0]
433 elif len(encoders) > 1:
434 self.encoder = getConfigListEntry(_("Encoder"), config.plugins.transcodingsetup.encoder)
435 self.list.append( self.encoder )
436 encoder = config.plugins.transcodingsetup.encoder.value
438 if encoder is not None:
440 if checkSupportAdvanced() and hasAttr('automode', encoder):
441 self.automode = getConfigListEntry(_("Auto set Framerate / Bitrate"), getAttr('automode', encoder))
443 if self.automode is not None:
444 self.list.append( self.automode )
446 if not ( hasAttr('automode', encoder) and getAttr('automode', encoder).value == "On" ):
447 if hasAttr('bitrate', encoder):
448 self.list.append(getConfigListEntry(_("Bitrate"), getAttr('bitrate', encoder)))
449 if hasAttr('framerate', encoder):
450 self.list.append(getConfigListEntry(_("Framerate"), getAttr('framerate', encoder)))
452 if hasAttr('resolution', encoder):
453 self.list.append(getConfigListEntry(_("Resolution"), getAttr('resolution', encoder)))
455 if checkSupportAdvanced() and self.setupMode != "Normal":
456 if hasAttr('aspectratio', encoder):
457 self.list.append(getConfigListEntry(_("Aspect Ratio"), getAttr('aspectratio', encoder)))
459 if hasAttr('audiocodec', encoder):
460 self.list.append(getConfigListEntry(_("Audio codec"), getAttr('audiocodec', encoder)))
462 if hasAttr('videocodec', encoder):
463 self.list.append(getConfigListEntry(_("Video codec"), getAttr('videocodec', encoder)))
465 if hasAttr('gopframe', encoder):
466 self.list.append(getConfigListEntry(_("GOP Frame B"), getAttr('gopframeb', encoder)))
468 if hasAttr('gopframep', encoder):
469 self.list.append(getConfigListEntry(_("GOP Frame P"), getAttr('gopframep', encoder)))
471 if hasAttr('level', encoder):
472 self.list.append(getConfigListEntry(_("Level"), getAttr('level', encoder)))
474 if hasAttr('profile', encoder):
475 self.list.append(getConfigListEntry(_("Profile"), getAttr('profile', encoder)))
477 self["config"].list = self.list
478 self["config"].l.setList(self.list)
479 if not self.showDescription in self["config"].onSelectionChanged:
480 self["config"].onSelectionChanged.append(self.showDescription)
482 def showDescription(self):
483 configName = "<%s>\n"%self["config"].getCurrent()[0]
484 current = self["config"].getCurrent()[1]
485 className = self["config"].getCurrent()[1].__class__.__name__
487 if className == "ConfigSelection" or className == "TconfigSelection":
489 for choice in current.choices.choices:
490 if text == configName:
493 text += ', ' + choice[1]
494 elif className == "ConfigInteger" or className == "TconfigInteger":
495 limits = current.limits[0]
497 text += "%s : %d, %s : %d" % (_("Min"), limits[0], _("Max"), limits[1])
498 self["description"].setText(text)
500 def showMessage(self, msg, msgType = MessageBox.TYPE_ERROR):
501 self.session.open(MessageBox, _(msg), msgType)
504 configs = config.plugins.transcodingsetup.dict()
505 for (configName, configElement) in configs.items():
512 def KeyDefault(self):
513 configs = config.plugins.transcodingsetup.dict()
514 for (configName, configElement) in configs.items():
515 if configName.startswith("automode"):
517 configElement.value = configElement.default
519 for (configName, configElement) in configs.items():
520 if configName.startswith("automode"):
521 configElement.value = configElement.default
526 if not checkSupportAdvanced():
528 if self.setupMode == "Normal":
529 self.setupMode = "Advanced"
530 self["key_blue"].setText( _("Normal") )
532 self.setupMode = "Normal"
533 self["key_blue"].setText( _("Advanced") )
536 def resetConfig(self):
537 for x in self["config"].list:
541 ConfigListScreen.keyLeft(self)
542 if self.encoder is not None and (self["config"].getCurrent() == self.encoder) or self.automode is not None and (self["config"].getCurrent() == self.automode):
546 ConfigListScreen.keyRight(self)
547 if self.encoder is not None and (self["config"].getCurrent() == self.encoder) or self.automode is not None and (self["config"].getCurrent() == self.automode):
550 def cancelConfirm(self, result):
554 configs = config.plugins.transcodingsetup.dict()
556 for (configName, configElement) in configs.items():
557 if configName.startswith("automode"):
559 configElement.cancel()
561 for (configName, configElement) in configs.items():
562 if configName.startswith("automode"):
563 configElement.cancel()
568 transcodingsetupinit.pluginsetup = None
569 if self["config"].isChanged():
570 self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
574 def main(session, **kwargs):
575 session.open(TranscodingSetup)
577 def Plugins(**kwargs):
578 return [PluginDescriptor(name=_("TranscodingSetup"), description=_("Transcoding Setup"), where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = False, fnc=main)]
580 transcodingsetupinit = TranscodingSetupInit()