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)
173 if hasAttr("bitrate", encoder):
174 getAttr("bitrate", encoder).addNotifier(self.setBitrate)
176 if hasAttr("framerate", encoder):
177 getAttr("framerate", encoder).addNotifier(self.setFramerate)
179 if hasAttr("resolution", encoder):
180 getAttr("resolution", encoder).addNotifier(self.setResolution)
182 if hasAttr("aspectratio", encoder):
183 getAttr("aspectratio", encoder).addNotifier(self.setAspectRatio)
185 if hasAttr("audiocodec", encoder):
186 getAttr("audiocodec", encoder).addNotifier(self.setAudioCodec)
188 if hasAttr("videocodec", encoder):
189 getAttr("videocodec", encoder).addNotifier(self.setVideoCodec)
191 if hasAttr("gopframeb", encoder):
192 getAttr("gopframeb", encoder).addNotifier(self.setGopFrameB)
194 if hasAttr("gopframep", encoder):
195 getAttr("gopframep", encoder).addNotifier(self.setGopFrameP)
197 if hasAttr("level", encoder):
198 getAttr("level", encoder).addNotifier(self.setLevel)
200 if hasAttr("profile", encoder):
201 getAttr("profile", encoder).addNotifier(self.setProfile)
203 def setConfig(self, procPath, value):
204 if not fileExists(procPath):
206 if isinstance(value, str):
207 value = value.strip(' ').strip('\n')
211 oldValue = getProcValue(procPath)
212 if oldValue != value:
213 # print "[TranscodingSetup] set %s "%procPath, value
214 setProcValue(procPath, value)
215 setValue = getProcValue(procPath)
216 if value != setValue:
217 print "[TranscodingSetup] set failed. (%s > %s)" % ( value, procPath )
221 print "setConfig exception error (%s > %s)" % ( value, procPath )
225 def setPort(self, configElement):
226 port = configElement.value
227 port2 = (port == "8001") and "8002" or "8001"
229 # print "[TranscodingSetup] set port ",port
232 oldConfigData = file('/etc/inetd.conf').read()
233 for L in oldConfigData.splitlines():
236 newConfigData += L + '\n'
240 if LL[5] == '/usr/bin/streamproxy':
242 elif LL[5] == '/usr/bin/transtreamproxy':
244 newConfigData += ''.join(str(X) + " " for X in LL) + '\n'
246 if newConfigData.find("transtreamproxy") == -1:
247 newConfigData += port + " stream tcp nowait root /usr/bin/transtreamproxy transtreamproxy\n"
248 file('/etc/inetd.conf', 'w').write(newConfigData)
250 self.showMessage("Set port failed.", MessageBox.TYPE_ERROR)
255 msg = "Set port OK.\nPC Streaming is replaced with mobile streaming."
256 self.showMessage(msg, MessageBox.TYPE_INFO)
258 def setupConfig(self, configElement, procPath):
259 # print "[TranscodingSetup] set %s to %s" % ( procPath, configElement.value )
260 configValue = configElement.value
261 if self.setConfig(procPath, configValue):
262 # set config failed, reset to current proc value
263 self.getConfigFromProc(procPath, configElement)
264 self.showMessage("Set %s failed." % (procPath), MessageBox.TYPE_ERROR)
266 def getConfigFromProc(self, procPath, configElement):
267 curValue = getProcValue(procPath)
268 if isinstance(configElement.value, int): # is int ?
269 curValue = int(curValue)
270 configElement.value = curValue
273 def setAutomode(self, configElement):
274 configName = "AutoMode"
275 # print "[TranscodingSetup] setAutomode, configName %s, value %s" % ( configName, configElement.value )
276 if configElement.value == "On":
278 if ((hasAttr("bitrate", configElement.encoder) and
279 self.setConfig(getProcPath(configElement.encoder ,"bitrate"), autoValue) ) or
280 (hasAttr("framerate", configElement.encoder) and
281 self.setConfig(getProcPath(configElement.encoder ,"framerate"), autoValue) ) ):
282 configElement.value = "Off" # set config failed, reset to previous value
284 self.showMessage("Set %s failed." % (configName), MessageBox.TYPE_ERROR)
286 if hasAttr("bitrate", configElement.encoder):
287 self.setBitrate(getAttr("bitrate", configElement.encoder))
288 if hasAttr("framerate", configElement.encoder):
289 self.setFramerate(getAttr("framerate", configElement.encoder))
291 def setBitrate(self, configElement):
292 self.setupConfig(configElement, getProcPath(configElement.encoder ,"bitrate"))
294 def setFramerate(self, configElement):
295 self.setupConfig(configElement, getProcPath(configElement.encoder ,"framerate"))
297 def setResolution(self, configElement):
298 resolution = configElement.value
299 if resolution in [ "320x240", "160x120" ]:
300 (width, height) = tuple(resolution.split('x'))
301 self.setConfig(getProcPath(configElement.encoder ,"resolution"), "custom")
302 self.setConfig(getProcPath(configElement.encoder ,"width"), width)
303 self.setConfig(getProcPath(configElement.encoder ,"height"), height)
305 self.setupConfig(configElement, getProcPath(configElement.encoder ,"resolution"))
307 def setAspectRatio(self, configElement):
308 self.setupConfig(configElement, getProcPath(configElement.encoder ,"aspectratio"))
310 def setAudioCodec(self, configElement):
311 self.setupConfig(configElement, getProcPath(configElement.encoder ,"audiocodec"))
313 def setVideoCodec(self, configElement):
314 self.setupConfig(configElement, getProcPath(configElement.encoder ,"videocodec"))
316 def setGopFrameB(self, configElement):
317 self.setupConfig(configElement, getProcPath(configElement.encoder ,"gopframeb"))
319 def setGopFrameP(self, configElement):
320 self.setupConfig(configElement, getProcPath(configElement.encoder ,"gopframep"))
322 def setLevel(self, configElement):
323 self.setupConfig(configElement, getProcPath(configElement.encoder ,"level"))
325 def setProfile(self, configElement):
326 self.setupConfig(configElement, getProcPath(configElement.encoder ,"profile"))
328 def inetdRestart(self):
329 if fileExists("/etc/init.d/inetd"):
330 os_system("/etc/init.d/inetd restart")
331 elif fileExists("/etc/init.d/inetd.busybox"):
332 os_system("/etc/init.d/inetd.busybox restart")
334 def showMessage(self, msg, msgType):
336 self.pluginsetup.showMessage(msg, msgType)
338 class TranscodingSetup(Screen, ConfigListScreen):
339 size = getDesktop(0).size()
340 if checkSupportAdvanced():
341 if size.width() > 750:
348 pos_h = ( size_h , size_h - 150 , (size_h - 150) + 70, (size_h - 150) + 70 + 60 )
350 <screen position="center,center" size="600,%d">
351 <ePixmap pixmap="skin_default/buttons/red.png" position="5,0" size="140,40" alphatest="on" />
352 <ePixmap pixmap="skin_default/buttons/green.png" position="155,0" size="140,40" alphatest="on" />
353 <ePixmap pixmap="skin_default/buttons/yellow.png" position="305,0" size="140,40" alphatest="on" />
354 <ePixmap pixmap="skin_default/buttons/blue.png" position="455,0" size="140,40" alphatest="on" />
355 <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" />
356 <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" />
357 <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" />
358 <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" />
359 <widget name="config" zPosition="2" position="25,70" size="560,%d" scrollbarMode="showOnDemand" transparent="1" />
360 <widget source="description" render="Label" position="20,%d" size="540,60" font="Regular;20" halign="center" valign="center" />
361 <widget source="text" render="Label" position="20,%d" size="540,20" font="Regular;22" halign="center" valign="center" />
366 <screen position="center,center" size="600,%d">
367 <ePixmap pixmap="skin_default/buttons/red.png" position="40,0" size="140,40" alphatest="on" />
368 <ePixmap pixmap="skin_default/buttons/green.png" position="230,0" size="140,40" alphatest="on" />
369 <ePixmap pixmap="skin_default/buttons/yellow.png" position="420,0" size="140,40" alphatest="on" />
370 <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" />
371 <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" />
372 <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" />
373 <widget name="config" zPosition="2" position="25,70" size="560,%d" scrollbarMode="showOnDemand" transparent="1" />
374 <widget source="description" render="Label" position="20,%d" size="540,60" font="Regular;20" halign="center" valign="center" />
375 <widget source="text" render="Label" position="20,%d" size="540,20" font="Regular;22" halign="center" valign="center" />
379 def __init__(self,session):
380 Screen.__init__(self,session)
381 self.session = session
382 self.setTitle(_("Transcoding Setup"))
384 if checkSupportAdvanced():
385 self.skin = TranscodingSetup.skin_advanced
387 self.skin = TranscodingSetup.skin_normal
389 if getModel() in ("solo2", "solose"):
390 TEXT = _("Transcoding and PIP are mutually exclusive.")
392 TEXT = _("2nd transcoding and PIP are mutually exclusive.")
393 self["text"] = StaticText(_("%s")%TEXT)
395 self["key_red"] = StaticText(_("Cancel"))
396 self["key_green"] = StaticText(_("Save"))
397 self["key_yellow"] = StaticText(_("Default"))
398 self["key_blue"] = StaticText(_("Advanced"))
399 self["description"] = StaticText(_("Transcoding Setup"))
401 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
403 "cancel" : self.keyCancel,
404 "red" : self.keyCancel,
405 "green" : self.keySave,
406 "yellow" : self.KeyDefault,
407 "blue" : self.keyBlue,
411 ConfigListScreen.__init__(self, self.list,session = self.session)
412 self.setupMode = "Normal" # Normal / Advanced
416 self.onLayoutFinish.append(self.checkEncoder)
417 self.invaliedModelTimer = eTimer()
418 self.invaliedModelTimer.callback.append(self.invalidmodel)
419 global transcodingsetupinit
420 transcodingsetupinit.pluginsetup = self
421 self.onClose.append(self.onClosed)
424 transcodingsetupinit.pluginsetup = None
426 def checkEncoder(self):
427 if not fileExists("/proc/stb/encoder/enable"):
428 self.invaliedModelTimer.start(100,True)
430 def invalidmodel(self):
431 self.session.openWithCallback(self.close, MessageBox, _("This model is not support transcoding."), MessageBox.TYPE_ERROR)
433 def createSetup(self):
435 self.list.append(getConfigListEntry(_("Port"), config.plugins.transcodingsetup.port))
438 if len(encoders) == 1:
439 encoder = encoders[0]
440 elif len(encoders) > 1:
441 self.encoder = getConfigListEntry(_("Encoder"), config.plugins.transcodingsetup.encoder)
442 self.list.append( self.encoder )
443 encoder = config.plugins.transcodingsetup.encoder.value
445 if encoder is not None:
447 if checkSupportAdvanced() and hasAttr('automode', encoder):
448 self.automode = getConfigListEntry(_("Auto set Framerate / Bitrate"), getAttr('automode', encoder))
450 if self.automode is not None:
451 self.list.append( self.automode )
453 if not ( hasAttr('automode', encoder) and getAttr('automode', encoder).value == "On" ):
454 if hasAttr('bitrate', encoder):
455 self.list.append(getConfigListEntry(_("Bitrate"), getAttr('bitrate', encoder)))
456 if hasAttr('framerate', encoder):
457 self.list.append(getConfigListEntry(_("Framerate"), getAttr('framerate', encoder)))
459 if hasAttr('resolution', encoder):
460 self.list.append(getConfigListEntry(_("Resolution"), getAttr('resolution', encoder)))
462 if checkSupportAdvanced() and self.setupMode != "Normal":
463 if hasAttr('aspectratio', encoder):
464 self.list.append(getConfigListEntry(_("Aspect Ratio"), getAttr('aspectratio', encoder)))
466 if hasAttr('audiocodec', encoder):
467 self.list.append(getConfigListEntry(_("Audio codec"), getAttr('audiocodec', encoder)))
469 if hasAttr('videocodec', encoder):
470 self.list.append(getConfigListEntry(_("Video codec"), getAttr('videocodec', encoder)))
472 if hasAttr('gopframe', encoder):
473 self.list.append(getConfigListEntry(_("GOP Frame B"), getAttr('gopframeb', encoder)))
475 if hasAttr('gopframep', encoder):
476 self.list.append(getConfigListEntry(_("GOP Frame P"), getAttr('gopframep', encoder)))
478 if hasAttr('level', encoder):
479 self.list.append(getConfigListEntry(_("Level"), getAttr('level', encoder)))
481 if hasAttr('profile', encoder):
482 self.list.append(getConfigListEntry(_("Profile"), getAttr('profile', encoder)))
484 self["config"].list = self.list
485 self["config"].l.setList(self.list)
486 if not self.showDescription in self["config"].onSelectionChanged:
487 self["config"].onSelectionChanged.append(self.showDescription)
489 def showDescription(self):
490 configName = "<%s>\n"%self["config"].getCurrent()[0]
491 current = self["config"].getCurrent()[1]
492 className = self["config"].getCurrent()[1].__class__.__name__
494 if className == "ConfigSelection" or className == "TconfigSelection":
496 for choice in current.choices.choices:
497 if text == configName:
500 text += ', ' + choice[1]
501 elif className == "ConfigInteger" or className == "TconfigInteger":
502 limits = current.limits[0]
504 text += "%s : %d, %s : %d" % (_("Min"), limits[0], _("Max"), limits[1])
505 self["description"].setText(text)
507 def showMessage(self, msg, msgType = MessageBox.TYPE_ERROR):
508 self.session.open(MessageBox, _(msg), msgType)
511 configs = config.plugins.transcodingsetup.dict()
512 for (configName, configElement) in configs.items():
519 def KeyDefault(self):
520 configs = config.plugins.transcodingsetup.dict()
521 for (configName, configElement) in configs.items():
522 if configName.startswith("automode"):
524 configElement.value = configElement.default
526 for (configName, configElement) in configs.items():
527 if configName.startswith("automode"):
528 configElement.value = configElement.default
533 if not checkSupportAdvanced():
535 if self.setupMode == "Normal":
536 self.setupMode = "Advanced"
537 self["key_blue"].setText( _("Normal") )
539 self.setupMode = "Normal"
540 self["key_blue"].setText( _("Advanced") )
543 def resetConfig(self):
544 for x in self["config"].list:
548 ConfigListScreen.keyLeft(self)
549 if self.encoder is not None and (self["config"].getCurrent() == self.encoder) or self.automode is not None and (self["config"].getCurrent() == self.automode):
553 ConfigListScreen.keyRight(self)
554 if self.encoder is not None and (self["config"].getCurrent() == self.encoder) or self.automode is not None and (self["config"].getCurrent() == self.automode):
557 def cancelConfirm(self, result):
561 configs = config.plugins.transcodingsetup.dict()
563 for (configName, configElement) in configs.items():
564 if configName.startswith("automode"):
566 configElement.cancel()
568 for (configName, configElement) in configs.items():
569 if configName.startswith("automode"):
570 configElement.cancel()
575 transcodingsetupinit.pluginsetup = None
576 if self["config"].isChanged():
577 self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
581 def main(session, **kwargs):
582 session.open(TranscodingSetup)
584 def Plugins(**kwargs):
585 return [PluginDescriptor(name=_("TranscodingSetup"), description=_("Transcoding Setup"), where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = False, fnc=main)]
587 transcodingsetupinit = TranscodingSetupInit()