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") ):
79 if getModel() == "solo2":
80 choice = TconfigInteger(encoder, default = 400000, limits = (50000, 1000000))
82 choice = TconfigInteger(encoder, default = 2000000, limits = (100000, 5000000))
83 setAttr("bitrate", encoder, choice)
85 if fileExists( getProcPath(encoder ,"framerate") ):
86 choice = TconfigSelection(encoder, default = "30000", choices = [ ("23976", _("23976")), ("24000", _("24000")), ("25000", _("25000")), ("29970", _("29970")), ("30000", _("30000")), ("50000", _("50000")), ("59940", _("59940")), ("60000", _("60000"))] )
87 setAttr("framerate", encoder, choice)
89 if checkSupportAdvanced() and (hasAttr("bitrate", encoder) or hasAttr("framerate", encoder)):
90 choice = TconfigSelection(encoder, default = "Off", choices = [ ("On", _("On")), ("Off", _("Off")) ] )
91 setAttr("automode", encoder, choice)
93 if fileExists( getProcPath(encoder, "resolution") ):
94 choice = TconfigSelection(encoder, default = "480p", choices = [ ("480p", _("480p")), ("576p", _("576p")), ("720p", _("720p")), ("320x240", _("320x240")), ("160x120", _("160x120")) ] )
95 setAttr("resolution", encoder, choice)
97 if fileExists( getProcPath(encoder, "aspectratio") ):
98 choice = TconfigSelection(encoder, default = "1", choices = [ ("0", _("auto")), ("1", _("4x3")), ("2", _("16x9")) ] )
99 setAttr("aspectratio", encoder, choice)
101 if fileExists( getProcPath(encoder, "audiocodec") ):
102 choice = TconfigSelection(encoder, default = "aac", choices = [("mpg", _("mpg")), ("mp3", _("mp3")), ("aac", _("aac")), ("aac+", _("aac+")), ("aac+loas", _("aac+loas")), ("aac+adts", _("aac+adts")), ("ac3", _("ac3"))] )
103 setAttr("audiocodec", encoder, choice)
105 if fileExists( getProcPath(encoder, "videocodec") ):
106 choice = TconfigSelection(encoder, default = "h264", choices = [ ("h264", _("h264")) ] )
107 setAttr("videocodec", encoder, choice)
109 if fileExists( getProcPath(encoder, "gopframeb") ):
110 choice = TconfigInteger(encoder, default = 0, limits = (0, 60))
111 setAttr("gopframeb", encoder, choice)
113 if fileExists( getProcPath(encoder, "gopframep") ):
114 choice = TconfigInteger(encoder, default = 29, limits = (0, 60))
115 setAttr("gopframep", encoder, choice)
117 if fileExists( getProcPath(encoder, "level") ):
118 choice = TconfigSelection(encoder, default = "3.1", choices = [("1.0", _("1.0")), ("2.0", _("2.0")),
119 ("2.1", _("2.1")), ("2.2", _("2.2")), ("3.0", _("3.0")), ("3.1", _("3.1")),
120 ("3.2", _("3.2")), ("4.0", _("4.0")), ("4.1", _("4.1")), ("4.2", _("4.2")),
121 ("5.0", _("5.0")), ("low", _("low")), ("main", _("main")), ("high", _("high"))] )
122 setAttr("level", encoder, choice)
124 if fileExists( getProcPath(encoder, "profile") ):
125 choice = TconfigSelection(encoder, default = "baseline", choices = [("baseline", _("baseline")), ("simple", _("simple")), ("main", _("main")), ("high", _("high")), ("advanced simple", _("advancedsimple"))] )
126 setAttr("profile", encoder, choice)
130 encoderPath = "/proc/stb/encoder"
131 for encoder in os_listdir(encoderPath):
132 encPath = os_path.join(encoderPath, encoder)
133 if not os_path.isdir(encPath):
135 if fileExists(os_path.join(encPath, "bitrate")):
136 encoders.append(encoder)
137 createTransCodingConfig(encoder)
139 if len(encoders) > 1:
142 for encoder in encoders:
143 choices.append((encoder, encoder))
144 config.plugins.transcodingsetup.encoder = ConfigSelection(default = '0', choices = choices )
146 transcodingsetupinit = None
147 class TranscodingSetupInit:
149 self.pluginsetup = None
150 config.plugins.transcodingsetup.port.addNotifier(self.setPort)
152 for encoder in encoders:
153 if hasAttr("automode", encoder):
154 if getAttr("automode", encoder).value == "On":
155 getAttr("automode", encoder).addNotifier(self.setAutomode)
157 if hasAttr("bitrate", encoder):
158 getAttr("bitrate", encoder).addNotifier(self.setBitrate, False)
160 if hasAttr("framerate", encoder):
161 getAttr("framerate", encoder).addNotifier(self.setFramerate, False)
164 getAttr("automode", encoder).addNotifier(self.setAutomode, False)
165 if hasAttr("bitrate", encoder):
166 getAttr("bitrate", encoder).addNotifier(self.setBitrate)
168 if hasAttr("framerate", encoder):
169 getAttr("framerate", encoder).addNotifier(self.setFramerate)
171 if hasAttr("resolution", encoder):
172 getAttr("resolution", encoder).addNotifier(self.setResolution)
174 if hasAttr("aspectratio", encoder):
175 getAttr("aspectratio", encoder).addNotifier(self.setAspectRatio)
177 if hasAttr("audiocodec", encoder):
178 getAttr("audiocodec", encoder).addNotifier(self.setAudioCodec)
180 if hasAttr("videocodec", encoder):
181 getAttr("videocodec", encoder).addNotifier(self.setVideoCodec)
183 if hasAttr("gopframeb", encoder):
184 getAttr("gopframeb", encoder).addNotifier(self.setGopFrameB)
186 if hasAttr("gopframep", encoder):
187 getAttr("gopframep", encoder).addNotifier(self.setGopFrameP)
189 if hasAttr("level", encoder):
190 getAttr("level", encoder).addNotifier(self.setLevel)
192 if hasAttr("profile", encoder):
193 getAttr("profile", encoder).addNotifier(self.setProfile)
195 def setConfig(self, procPath, value):
196 if not fileExists(procPath):
198 if isinstance(value, str):
199 value = value.strip(' ').strip('\n')
203 oldValue = getProcValue(procPath)
204 if oldValue != value:
205 # print "[TranscodingSetup] set %s "%procPath, value
206 setProcValue(procPath, value)
207 setValue = getProcValue(procPath)
208 if value != setValue:
209 print "[TranscodingSetup] set failed. (%s > %s)" % ( value, procPath )
213 print "setConfig exception error (%s > %s)" % ( value, procPath )
217 def setPort(self, configElement):
218 port = configElement.value
219 port2 = (port == "8001") and "8002" or "8001"
221 # print "[TranscodingSetup] set port ",port
224 oldConfigData = file('/etc/inetd.conf').read()
225 for L in oldConfigData.splitlines():
228 newConfigData += L + '\n'
232 if LL[5] == '/usr/bin/streamproxy':
234 elif LL[5] == '/usr/bin/transtreamproxy':
236 newConfigData += ''.join(str(X) + " " for X in LL) + '\n'
238 if newConfigData.find("transtreamproxy") == -1:
239 newConfigData += port + " stream tcp nowait root /usr/bin/transtreamproxy transtreamproxy\n"
240 file('/etc/inetd.conf', 'w').write(newConfigData)
242 self.showMessage("Set port failed.", MessageBox.TYPE_ERROR)
247 msg = "Set port OK.\nPC Streaming is replaced with mobile streaming."
248 self.showMessage(msg, MessageBox.TYPE_INFO)
250 def setupConfig(self, configElement, procPath):
251 # print "[TranscodingSetup] set %s to %s" % ( procPath, configElement.value )
252 configValue = configElement.value
253 if self.setConfig(procPath, configValue):
254 # set config failed, reset to current proc value
255 self.getConfigFromProc(procPath, configElement)
256 self.showMessage("Set %s failed." % (procPath), MessageBox.TYPE_ERROR)
258 def getConfigFromProc(self, procPath, configElement):
259 curValue = getProcValue(procPath)
260 if isinstance(configElement.value, int): # is int ?
261 curValue = int(curValue)
262 configElement.value = curValue
265 def setAutomode(self, configElement):
266 configName = "AutoMode"
267 # print "[TranscodingSetup] setAutomode, configName %s, value %s" % ( configName, configElement.value )
268 if configElement.value == "On":
270 if ((hasAttr("bitrate", configElement.encoder) and
271 self.setConfig(getProcPath(configElement.encoder ,"bitrate"), autoValue) ) or
272 (hasAttr("framerate", configElement.encoder) and
273 self.setConfig(getProcPath(configElement.encoder ,"framerate"), autoValue) ) ):
274 configElement.value = "Off" # set config failed, reset to previous value
276 self.showMessage("Set %s failed." % (configName), MessageBox.TYPE_ERROR)
278 if hasAttr("bitrate", configElement.encoder):
279 self.setBitrate(getAttr("bitrate", configElement.encoder))
280 if hasAttr("framerate", configElement.encoder):
281 self.setFramerate(getAttr("framerate", configElement.encoder))
283 def setBitrate(self, configElement):
284 self.setupConfig(configElement, getProcPath(configElement.encoder ,"bitrate"))
286 def setFramerate(self, configElement):
287 self.setupConfig(configElement, getProcPath(configElement.encoder ,"framerate"))
289 def setResolution(self, configElement):
290 resolution = configElement.value
291 if resolution in [ "320x240", "160x120" ]:
292 (width, height) = tuple(resolution.split('x'))
293 self.setConfig(getProcPath(configElement.encoder ,"resolution"), "custom")
294 self.setConfig(getProcPath(configElement.encoder ,"width"), width)
295 self.setConfig(getProcPath(configElement.encoder ,"height"), height)
297 self.setupConfig(configElement, getProcPath(configElement.encoder ,"resolution"))
299 def setAspectRatio(self, configElement):
300 self.setupConfig(configElement, getProcPath(configElement.encoder ,"aspectratio"))
302 def setAudioCodec(self, configElement):
303 self.setupConfig(configElement, getProcPath(configElement.encoder ,"audiocodec"))
305 def setVideoCodec(self, configElement):
306 self.setupConfig(configElement, getProcPath(configElement.encoder ,"videocodec"))
308 def setGopFrameB(self, configElement):
309 self.setupConfig(configElement, getProcPath(configElement.encoder ,"gopframeb"))
311 def setGopFrameP(self, configElement):
312 self.setupConfig(configElement, getProcPath(configElement.encoder ,"gopframep"))
314 def setLevel(self, configElement):
315 self.setupConfig(configElement, getProcPath(configElement.encoder ,"level"))
317 def setProfile(self, configElement):
318 self.setupConfig(configElement, getProcPath(configElement.encoder ,"profile"))
320 def inetdRestart(self):
321 if fileExists("/etc/init.d/inetd"):
322 os_system("/etc/init.d/inetd restart")
323 elif fileExists("/etc/init.d/inetd.busybox"):
324 os_system("/etc/init.d/inetd.busybox restart")
326 def showMessage(self, msg, msgType):
328 self.pluginsetup.showMessage(msg, msgType)
330 class TranscodingSetup(Screen, ConfigListScreen):
331 size = getDesktop(0).size()
332 if checkSupportAdvanced():
333 if size.width() > 750:
340 pos_h = ( size_h , size_h - 150 , (size_h - 150) + 70, (size_h - 150) + 70 + 60 )
342 <screen position="center,center" size="600,%d">
343 <ePixmap pixmap="skin_default/buttons/red.png" position="5,0" size="140,40" alphatest="on" />
344 <ePixmap pixmap="skin_default/buttons/green.png" position="155,0" size="140,40" alphatest="on" />
345 <ePixmap pixmap="skin_default/buttons/yellow.png" position="305,0" size="140,40" alphatest="on" />
346 <ePixmap pixmap="skin_default/buttons/blue.png" position="455,0" size="140,40" alphatest="on" />
347 <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" />
348 <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" />
349 <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" />
350 <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" />
351 <widget name="config" zPosition="2" position="25,70" size="560,%d" scrollbarMode="showOnDemand" transparent="1" />
352 <widget source="description" render="Label" position="20,%d" size="540,60" font="Regular;20" halign="center" valign="center" />
353 <widget source="text" render="Label" position="20,%d" size="540,20" font="Regular;22" halign="center" valign="center" />
358 <screen position="center,center" size="600,%d">
359 <ePixmap pixmap="skin_default/buttons/red.png" position="40,0" size="140,40" alphatest="on" />
360 <ePixmap pixmap="skin_default/buttons/green.png" position="230,0" size="140,40" alphatest="on" />
361 <ePixmap pixmap="skin_default/buttons/yellow.png" position="420,0" size="140,40" alphatest="on" />
362 <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" />
363 <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" />
364 <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" />
365 <widget name="config" zPosition="2" position="25,70" size="560,%d" scrollbarMode="showOnDemand" transparent="1" />
366 <widget source="description" render="Label" position="20,%d" size="540,60" font="Regular;20" halign="center" valign="center" />
367 <widget source="text" render="Label" position="20,%d" size="540,20" font="Regular;22" halign="center" valign="center" />
371 def __init__(self,session):
372 Screen.__init__(self,session)
373 self.session = session
374 self.setTitle(_("Transcoding Setup"))
376 if checkSupportAdvanced():
377 self.skin = TranscodingSetup.skin_advanced
379 self.skin = TranscodingSetup.skin_normal
381 if getModel() == "solo2":
382 TEXT = _("Transcoding and PIP are mutually exclusive.")
384 TEXT = _("2nd transcoding and PIP are mutually exclusive.")
385 self["text"] = StaticText(_("%s")%TEXT)
387 self["key_red"] = StaticText(_("Cancel"))
388 self["key_green"] = StaticText(_("Save"))
389 self["key_yellow"] = StaticText(_("Default"))
390 self["key_blue"] = StaticText(_("Advanced"))
391 self["description"] = StaticText(_("Transcoding Setup"))
393 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
395 "cancel" : self.keyCancel,
396 "red" : self.keyCancel,
397 "green" : self.keySave,
398 "yellow" : self.KeyDefault,
399 "blue" : self.keyBlue,
403 ConfigListScreen.__init__(self, self.list,session = self.session)
404 self.setupMode = "Normal" # Normal / Advanced
408 self.onLayoutFinish.append(self.checkEncoder)
409 self.invaliedModelTimer = eTimer()
410 self.invaliedModelTimer.callback.append(self.invalidmodel)
411 global transcodingsetupinit
412 transcodingsetupinit.pluginsetup = self
413 self.onClose.append(self.onClosed)
416 transcodingsetupinit.pluginsetup = None
418 def checkEncoder(self):
419 if not fileExists("/proc/stb/encoder/enable"):
420 self.invaliedModelTimer.start(100,True)
422 def invalidmodel(self):
423 self.session.openWithCallback(self.close, MessageBox, _("This model is not support transcoding."), MessageBox.TYPE_ERROR)
425 def createSetup(self):
427 self.list.append(getConfigListEntry(_("Port"), config.plugins.transcodingsetup.port))
430 if len(encoders) == 1:
431 encoder = encoders[0]
432 elif len(encoders) > 1:
433 self.encoder = getConfigListEntry(_("Encoder"), config.plugins.transcodingsetup.encoder)
434 self.list.append( self.encoder )
435 encoder = config.plugins.transcodingsetup.encoder.value
437 if encoder is not None:
439 if checkSupportAdvanced() and hasAttr('automode', encoder):
440 self.automode = getConfigListEntry(_("Auto set Framerate / Bitrate"), getAttr('automode', encoder))
442 if self.automode is not None:
443 self.list.append( self.automode )
445 if not ( hasAttr('automode', encoder) and getAttr('automode', encoder).value == "On" ):
446 if hasAttr('bitrate', encoder):
447 self.list.append(getConfigListEntry(_("Bitrate"), getAttr('bitrate', encoder)))
448 if hasAttr('framerate', encoder):
449 self.list.append(getConfigListEntry(_("Framerate"), getAttr('framerate', encoder)))
451 if hasAttr('resolution', encoder):
452 self.list.append(getConfigListEntry(_("Resolution"), getAttr('resolution', encoder)))
454 if checkSupportAdvanced() and self.setupMode != "Normal":
455 if hasAttr('aspectratio', encoder):
456 self.list.append(getConfigListEntry(_("Aspect Ratio"), getAttr('aspectratio', encoder)))
458 if hasAttr('audiocodec', encoder):
459 self.list.append(getConfigListEntry(_("Audio codec"), getAttr('audiocodec', encoder)))
461 if hasAttr('videocodec', encoder):
462 self.list.append(getConfigListEntry(_("Video codec"), getAttr('videocodec', encoder)))
464 if hasAttr('gopframe', encoder):
465 self.list.append(getConfigListEntry(_("GOP Frame B"), getAttr('gopframeb', encoder)))
467 if hasAttr('gopframep', encoder):
468 self.list.append(getConfigListEntry(_("GOP Frame P"), getAttr('gopframep', encoder)))
470 if hasAttr('level', encoder):
471 self.list.append(getConfigListEntry(_("Level"), getAttr('level', encoder)))
473 if hasAttr('profile', encoder):
474 self.list.append(getConfigListEntry(_("Profile"), getAttr('profile', encoder)))
476 self["config"].list = self.list
477 self["config"].l.setList(self.list)
478 if not self.showDescription in self["config"].onSelectionChanged:
479 self["config"].onSelectionChanged.append(self.showDescription)
481 def showDescription(self):
482 configName = "<%s>\n"%self["config"].getCurrent()[0]
483 current = self["config"].getCurrent()[1]
484 className = self["config"].getCurrent()[1].__class__.__name__
486 if className == "ConfigSelection" or className == "TconfigSelection":
488 for choice in current.choices.choices:
489 if text == configName:
492 text += ', ' + choice[1]
493 elif className == "ConfigInteger" or className == "TconfigInteger":
494 limits = current.limits[0]
496 text += "%s : %d, %s : %d" % (_("Max"), limits[0], _("Min"), limits[1])
497 self["description"].setText(text)
499 def showMessage(self, msg, msgType = MessageBox.TYPE_ERROR):
500 self.session.open(MessageBox, _(msg), msgType)
503 configs = config.plugins.transcodingsetup.dict()
504 for (configName, configElement) in configs.items():
511 def KeyDefault(self):
512 configs = config.plugins.transcodingsetup.dict()
513 for (configName, configElement) in configs.items():
514 if configName.startswith("automode"):
516 configElement.value = configElement.default
518 for (configName, configElement) in configs.items():
519 if configName.startswith("automode"):
520 configElement.value = configElement.default
525 if not checkSupportAdvanced():
527 if self.setupMode == "Normal":
528 self.setupMode = "Advanced"
529 self["key_blue"].setText( _("Normal") )
531 self.setupMode = "Normal"
532 self["key_blue"].setText( _("Advanced") )
535 def resetConfig(self):
536 for x in self["config"].list:
540 ConfigListScreen.keyLeft(self)
541 if self.encoder is not None and (self["config"].getCurrent() == self.encoder) or self.automode is not None and (self["config"].getCurrent() == self.automode):
545 ConfigListScreen.keyRight(self)
546 if self.encoder is not None and (self["config"].getCurrent() == self.encoder) or self.automode is not None and (self["config"].getCurrent() == self.automode):
549 def cancelConfirm(self, result):
553 configs = config.plugins.transcodingsetup.dict()
555 for (configName, configElement) in configs.items():
556 if configName.startswith("automode"):
558 configElement.cancel()
560 for (configName, configElement) in configs.items():
561 if configName.startswith("automode"):
562 configElement.cancel()
567 transcodingsetupinit.pluginsetup = None
568 if self["config"].isChanged():
569 self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
573 def main(session, **kwargs):
574 session.open(TranscodingSetup)
576 def Plugins(**kwargs):
577 return [PluginDescriptor(name=_("TranscodingSetup"), description=_("Transcoding Setup"), where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = False, fnc=main)]
579 transcodingsetupinit = TranscodingSetupInit()