1 from Screens.Screen import Screen
2 from Components.ConfigList import ConfigListScreen
3 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSelection, ConfigInteger
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
10 from os import system as os_system
11 from __init__ import _
14 filename = "/proc/stb/info/vumodel"
15 if fileExists(filename):
16 return file(filename).read().strip()
19 def getProcValue(procPath):
20 fd = open(procPath,'r')
21 curValue = fd.read().strip(' ').strip('\n')
23 # print "[TranscodingSetup] get %s from %s" % (curValue, procPath)
26 def setProcValue(procPath, value):
27 print "[TranscodingSetup] set %s to %s" % (procPath, value)
28 fd = open(procPath,'w')
32 def checkSupportAdvanced():
33 if fileExists( g_procPath["aspectratio"] ):
37 transcodingsetupinit = None
40 "bitrate" : "/proc/stb/encoder/0/bitrate",
41 "framerate" : "/proc/stb/encoder/0/framerate",
42 "resolution" : "/proc/stb/encoder/0/display_format",
43 "aspectratio" : "/proc/stb/encoder/0/aspectratio",
44 "audiocodec" : "/proc/stb/encoder/0/audio_codec",
45 "videocodec" : "/proc/stb/encoder/0/video_codec",
46 "gopframeb" : "/proc/stb/encoder/0/gop_frameb",
47 "gopframep" : "/proc/stb/encoder/0/gop_framep",
48 "level" : "/proc/stb/encoder/0/level",
49 "profile" : "/proc/stb/encoder/0/profile",
50 "width" : "/proc/stb/encoder/0/width",
51 "height" : "/proc/stb/encoder/0/height",
54 config.plugins.transcodingsetup = ConfigSubsection()
55 config.plugins.transcodingsetup.transcoding = ConfigSelection(default = "enable", choices = [ ("enable", _("enable")), ("disable", _("disable"))] )
56 config.plugins.transcodingsetup.port = ConfigSelection(default = "8002", choices = [ ("8001", "8001"), ("8002", "8002")] )
58 if fileExists( g_procPath["bitrate"] ):
59 if getModel() == "solo2":
60 config.plugins.transcodingsetup.bitrate = ConfigInteger(default = 400000, limits = (50000, 1000000))
62 config.plugins.transcodingsetup.bitrate = ConfigInteger(default = 2000000, limits = (100000, 5000000))
64 if fileExists( g_procPath["framerate"] ):
65 config.plugins.transcodingsetup.framerate = ConfigSelection(default = "30000", choices = [ ("23976", _("23976")), ("24000", _("24000")), ("25000", _("25000")), ("29970", _("29970")), ("30000", _("30000")), ("50000", _("50000")), ("59940", _("59940")), ("60000", _("60000"))] )
67 if checkSupportAdvanced() and (hasattr(config.plugins.transcodingsetup, "bitrate") or hasattr(config.plugins.transcodingsetup, "framerate")):
68 config.plugins.transcodingsetup.automode = ConfigSelection(default = "Off", choices = [ ("On", _("On")), ("Off", _("Off")) ] )
70 if fileExists( g_procPath["resolution"] ):
71 config.plugins.transcodingsetup.resolution = ConfigSelection(default = "480p", choices = [ ("480p", _("480p")), ("576p", _("576p")), ("720p", _("720p")), ("320x240", _("320x240")), ("160x120", _("160x120")) ] )
73 if fileExists( g_procPath["aspectratio"] ):
74 config.plugins.transcodingsetup.aspectratio = ConfigSelection(default = "1", choices = [ ("0", _("auto")), ("1", _("4x3")), ("2", _("16x9")) ] )
76 if fileExists( g_procPath["audiocodec"] ):
77 config.plugins.transcodingsetup.audiocodec = ConfigSelection(default = "aac", choices = [("mpg", _("mpg")), ("mp3", _("mp3")), ("aac", _("aac")), ("aac+", _("aac+")), ("aac+loas", _("aac+loas")), ("aac+adts", _("aac+adts")), ("ac3", _("ac3"))] )
79 if fileExists( g_procPath["videocodec"] ):
80 config.plugins.transcodingsetup.videocodec = ConfigSelection(default = "h264", choices = [ ("h264", _("h264")), ("mpeg2", _("mpeg2")), ("mpeg4p2", _("mpeg4p2"))] )
82 if fileExists( g_procPath["gopframeb"] ):
83 config.plugins.transcodingsetup.gopframeb = ConfigInteger(default = 0, limits = (0, 60))
85 if fileExists( g_procPath["gopframep"] ):
86 config.plugins.transcodingsetup.gopframep = ConfigInteger(default = 29, limits = (0, 60))
88 if fileExists( g_procPath["level"] ):
89 config.plugins.transcodingsetup.level = ConfigSelection(default = "3.1", choices = [("1.0", _("1.0")), ("2.0", _("2.0")),
90 ("2.1", _("2.1")), ("2.2", _("2.2")), ("3.0", _("3.0")), ("3.1", _("3.1")),
91 ("3.2", _("3.2")), ("4.0", _("4.0")), ("4.1", _("4.1")), ("4.2", _("4.2")),
92 ("5.0", _("5.0")), ("low", _("low")), ("main", _("main")), ("high", _("high"))] )
94 if fileExists( g_procPath["profile"] ):
95 config.plugins.transcodingsetup.profile = ConfigSelection(default = "baseline", choices = [("baseline", _("baseline")), ("simple", _("simple")), ("main", _("main")), ("high", _("high")), ("advanced simple", _("advanced simple"))] )
97 class TranscodingSetupInit:
99 self.pluginsetup = None
100 config.plugins.transcodingsetup.port.addNotifier(self.setPort)
102 if hasattr(config.plugins.transcodingsetup, "automode"):
103 if config.plugins.transcodingsetup.automode.value == "On":
104 config.plugins.transcodingsetup.automode.addNotifier(self.setAutomode)
106 if hasattr(config.plugins.transcodingsetup, "bitrate"):
107 config.plugins.transcodingsetup.bitrate.addNotifier(self.setBitrate, False)
109 if hasattr(config.plugins.transcodingsetup, "framerate"):
110 config.plugins.transcodingsetup.framerate.addNotifier(self.setFramerate, False)
113 config.plugins.transcodingsetup.automode.addNotifier(self.setAutomode, False)
114 if hasattr(config.plugins.transcodingsetup, "bitrate"):
115 config.plugins.transcodingsetup.bitrate.addNotifier(self.setBitrate)
117 if hasattr(config.plugins.transcodingsetup, "framerate"):
118 config.plugins.transcodingsetup.framerate.addNotifier(self.setFramerate)
120 if hasattr(config.plugins.transcodingsetup, "resolution"):
121 config.plugins.transcodingsetup.resolution.addNotifier(self.setResolution)
123 if hasattr(config.plugins.transcodingsetup, "aspectratio"):
124 config.plugins.transcodingsetup.aspectratio.addNotifier(self.setAspectRatio)
126 if hasattr(config.plugins.transcodingsetup, "audiocodec"):
127 config.plugins.transcodingsetup.audiocodec.addNotifier(self.setAudioCodec)
129 if hasattr(config.plugins.transcodingsetup, "videocodec"):
130 config.plugins.transcodingsetup.videocodec.addNotifier(self.setVideoCodec)
132 if hasattr(config.plugins.transcodingsetup, "gopframeb"):
133 config.plugins.transcodingsetup.gopframeb.addNotifier(self.setGopFrameB)
135 if hasattr(config.plugins.transcodingsetup, "gopframep"):
136 config.plugins.transcodingsetup.gopframep.addNotifier(self.setGopFrameP)
138 if hasattr(config.plugins.transcodingsetup, "level"):
139 config.plugins.transcodingsetup.level.addNotifier(self.setLevel)
141 if hasattr(config.plugins.transcodingsetup, "profile"):
142 config.plugins.transcodingsetup.profile.addNotifier(self.setProfile)
144 def setConfig(self, procPath, value, configName = ""):
145 if not fileExists(procPath):
147 if isinstance(value, str):
148 value = value.strip(' ').strip('\n')
152 oldValue = getProcValue(procPath)
153 if oldValue != value:
154 # print "[TranscodingSetup] set %s "%procPath, value
155 setProcValue(procPath, value)
156 setValue = getProcValue(procPath)
157 if value != setValue:
158 print "[TranscodingSetup] set failed. (%s > %s)" % ( value, procPath )
162 print "setConfig exception error (%s > %s)" % ( value, procPath )
166 def setPort(self, configElement):
167 port = configElement.value
168 port2 = (port == "8001") and "8002" or "8001"
170 print "[TranscodingSetup] set port ",port
173 oldConfigData = file('/etc/inetd.conf').read()
174 for L in oldConfigData.splitlines():
177 newConfigData += L + '\n'
181 if LL[5] == '/usr/bin/streamproxy':
183 elif LL[5] == '/usr/bin/transtreamproxy':
185 newConfigData += ''.join(str(X) + " " for X in LL) + '\n'
187 if newConfigData.find("transtreamproxy") == -1:
188 newConfigData += port + " stream tcp nowait root /usr/bin/transtreamproxy transtreamproxy\n"
189 file('/etc/inetd.conf', 'w').write(newConfigData)
191 self.showMessage("Set port failed.", MessageBox.TYPE_ERROR)
196 msg = "Set port OK.\nPC Streaming is replaced with mobile streaming."
197 self.showMessage(msg, MessageBox.TYPE_INFO)
199 def setupConfig(self, configElement, configName):
200 # print "[TranscodingSetup] set %s to %s" % ( configName, configElement.value )
201 configValue = configElement.value
202 procPath = g_procPath[configName]
203 if self.setConfig(procPath, configValue):
204 # set config failed, reset to current proc value
205 self.getConfigFromProc(procPath, configElement)
206 self.showMessage("Set %s failed." % (configName), MessageBox.TYPE_ERROR)
208 def getConfigFromProc(self, procPath, configElement):
209 curValue = getProcValue(procPath)
210 if isinstance(configElement.value, int): # is int ?
211 curValue = int(curValue)
212 configElement.value = curValue
215 def setAutomode(self, configElement):
216 configName = "AutoMode"
217 # print "[TranscodingSetup] setAutomode, configName %s, value %s" % ( configName, configElement.value )
218 if configElement.value == "On":
220 if ((hasattr(config.plugins.transcodingsetup, "bitrate") and
221 self.setConfig(g_procPath["bitrate"], autoValue) ) or
222 (hasattr(config.plugins.transcodingsetup, "framerate") and
223 self.setConfig(g_procPath["framerate"], autoValue) ) ):
224 configElement.value = "Off" # set config failed, reset to previous value
226 self.showMessage("Set %s failed." % (configName), MessageBox.TYPE_ERROR)
228 if hasattr(config.plugins.transcodingsetup, "bitrate"):
229 self.setBitrate(config.plugins.transcodingsetup.bitrate)
230 if hasattr(config.plugins.transcodingsetup, "framerate"):
231 self.setFramerate(config.plugins.transcodingsetup.framerate)
233 def setBitrate(self, configElement):
234 self.setupConfig(configElement, "bitrate")
236 def setFramerate(self, configElement):
237 self.setupConfig(configElement, "framerate")
239 def setResolution(self, configElement):
240 resolution = configElement.value
241 if resolution in [ "320x240", "160x120" ]:
242 (width, height) = tuple(resolution.split('x'))
243 self.setConfig(g_procPath["resolution"], "custom")
244 self.setConfig(g_procPath["width"], width)
245 self.setConfig(g_procPath["height"], height)
247 self.setupConfig(configElement, "resolution")
249 def setAspectRatio(self, configElement):
250 self.setupConfig(configElement, "aspectratio")
252 def setAudioCodec(self, configElement):
253 self.setupConfig(configElement, "audiocodec")
255 def setVideoCodec(self, configElement):
256 self.setupConfig(configElement, "videocodec")
258 def setGopFrameB(self, configElement):
259 self.setupConfig(configElement, "gopframeb")
261 def setGopFrameP(self, configElement):
262 self.setupConfig(configElement, "gopframep")
264 def setLevel(self, configElement):
265 self.setupConfig(configElement, "level")
267 def setProfile(self, configElement):
268 self.setupConfig(configElement, "profile")
270 def inetdRestart(self):
271 if fileExists("/etc/init.d/inetd"):
272 os_system("/etc/init.d/inetd restart")
273 elif fileExists("/etc/init.d/inetd.busybox"):
274 os_system("/etc/init.d/inetd.busybox restart")
276 def showMessage(self, msg, msgType):
278 self.pluginsetup.showMessage(msg, msgType)
280 class TranscodingSetup(Screen, ConfigListScreen):
282 <screen position="center,center" size="600,450">
283 <ePixmap pixmap="skin_default/buttons/red.png" position="5,0" size="140,40" alphatest="on" />
284 <ePixmap pixmap="skin_default/buttons/green.png" position="155,0" size="140,40" alphatest="on" />
285 <ePixmap pixmap="skin_default/buttons/yellow.png" position="305,0" size="140,40" alphatest="on" />
286 <ePixmap pixmap="skin_default/buttons/blue.png" position="455,0" size="140,40" alphatest="on" />
287 <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" />
288 <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" />
289 <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" />
290 <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" />
291 <widget name="config" zPosition="2" position="25,70" size="560,300" scrollbarMode="showOnDemand" transparent="1" />
292 <widget source="description" render="Label" position="20,370" size="540,60" font="Regular;20" halign="center" valign="center" />
293 <widget source="text" render="Label" position="20,430" size="540,20" font="Regular;22" halign="center" valign="center" />
298 <screen position="center,center" size="540,290">
299 <ePixmap pixmap="skin_default/buttons/red.png" position="30,10" size="140,40" alphatest="on" />
300 <ePixmap pixmap="skin_default/buttons/green.png" position="200,10" size="140,40" alphatest="on" />
301 <ePixmap pixmap="skin_default/buttons/yellow.png" position="370,10" size="140,40" alphatest="on" />
302 <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" />
303 <widget source="key_green" render="Label" position="200,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
304 <widget source="key_yellow" render="Label" position="370,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" foregroundColor="#ffffff" transparent="1" />
305 <widget name="config" zPosition="2" position="20,70" size="500,120" scrollbarMode="showOnDemand" transparent="1" />
306 <widget source="description" render="Label" position="30,190" size="480,60" font="Regular;20" halign="center" valign="center" />
307 <widget source="text" render="Label" position="30,250" size="480,30" font="Regular;22" halign="center" valign="center" />
311 def __init__(self,session):
312 Screen.__init__(self,session)
313 self.session = session
314 self.setTitle(_("Transcoding Setup"))
316 if checkSupportAdvanced():
317 self.skin = TranscodingSetup.skin_expert
319 self.skin = TranscodingSetup.skin_normal
320 if getModel() == "solo2":
321 TEXT = _("Transcoding and PIP are mutually exclusive.")
323 TEXT = _("2nd transcoding and PIP are mutually exclusive.")
324 self["text"] = StaticText(_("%s")%TEXT)
326 self["key_red"] = StaticText(_("Cancel"))
327 self["key_green"] = StaticText(_("Save"))
328 self["key_yellow"] = StaticText(_("Default"))
329 self["key_blue"] = StaticText(_("Advanced"))
330 self["description"] = StaticText(_("Transcoding Setup"))
332 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
334 "cancel" : self.keyCancel,
335 "red" : self.keyCancel,
336 "green" : self.keySave,
337 "yellow" : self.KeyDefault,
338 "blue" : self.keyBlue,
342 ConfigListScreen.__init__(self, self.list,session = self.session)
343 self.setupMode = "Normal" # Normal / Advanced
346 self.onLayoutFinish.append(self.checkEncoder)
347 self.invaliedModelTimer = eTimer()
348 self.invaliedModelTimer.callback.append(self.invalidmodel)
349 global transcodingsetupinit
350 transcodingsetupinit.pluginsetup = self
351 self.onClose.append(self.onClosed)
354 transcodingsetupinit.pluginsetup = None
356 def checkEncoder(self):
357 if not fileExists("/proc/stb/encoder/enable"):
358 self.invaliedModelTimer.start(100,True)
360 def invalidmodel(self):
361 self.session.openWithCallback(self.close, MessageBox, _("This model is not support transcoding."), MessageBox.TYPE_ERROR)
363 def createSetup(self):
365 self.list.append(getConfigListEntry(_("Port"), config.plugins.transcodingsetup.port))
367 if self.automode is None and checkSupportAdvanced() and hasattr(config.plugins.transcodingsetup, "automode"):
368 self.automode = getConfigListEntry(_("Auto set Framerate / Bitrate"), config.plugins.transcodingsetup.automode)
370 if self.automode is not None:
371 self.list.append( self.automode )
373 if not ( hasattr(config.plugins.transcodingsetup, "automode") and config.plugins.transcodingsetup.automode.value == "On" ):
374 if hasattr(config.plugins.transcodingsetup, "bitrate"):
375 self.list.append(getConfigListEntry(_("Bitrate"), config.plugins.transcodingsetup.bitrate))
376 if hasattr(config.plugins.transcodingsetup, "framerate"):
377 self.list.append(getConfigListEntry(_("Framerate"), config.plugins.transcodingsetup.framerate))
379 if hasattr(config.plugins.transcodingsetup, "resolution"):
380 self.list.append(getConfigListEntry(_("Resolution"), config.plugins.transcodingsetup.resolution))
382 if checkSupportAdvanced() and self.setupMode != "Normal":
383 if hasattr(config.plugins.transcodingsetup, "aspectratio"):
384 self.list.append(getConfigListEntry(_("Aspect Ratio"), config.plugins.transcodingsetup.aspectratio))
386 if hasattr(config.plugins.transcodingsetup, "audiocodec"):
387 self.list.append(getConfigListEntry(_("Audio codec"), config.plugins.transcodingsetup.audiocodec))
389 if hasattr(config.plugins.transcodingsetup, "videocodec"):
390 self.list.append(getConfigListEntry(_("Video codec"), config.plugins.transcodingsetup.videocodec))
392 if hasattr(config.plugins.transcodingsetup, "gopframeb"):
393 self.list.append(getConfigListEntry(_("GOP Frame B"), config.plugins.transcodingsetup.gopframeb))
395 if hasattr(config.plugins.transcodingsetup, "gopframep"):
396 self.list.append(getConfigListEntry(_("GOP Frame P"), config.plugins.transcodingsetup.gopframep))
398 if hasattr(config.plugins.transcodingsetup, "level"):
399 self.list.append(getConfigListEntry(_("Level"), config.plugins.transcodingsetup.level))
401 if hasattr(config.plugins.transcodingsetup, "profile"):
402 self.list.append(getConfigListEntry(_("Profile"), config.plugins.transcodingsetup.profile))
404 self["config"].list = self.list
405 self["config"].l.setList(self.list)
406 if not self.showDescription in self["config"].onSelectionChanged:
407 self["config"].onSelectionChanged.append(self.showDescription)
409 def showDescription(self):
410 configName = "<%s>\n"%self["config"].getCurrent()[0]
411 current = self["config"].getCurrent()[1]
412 className = self["config"].getCurrent()[1].__class__.__name__
414 if className == "ConfigSelection":
416 for choice in current.choices.choices:
417 if text == configName:
420 text += ', ' + choice[1]
421 elif className == "ConfigInteger":
422 limits = current.limits[0]
424 text += "Max : %d, Min : %d" % (limits[0], limits[1])
425 self["description"].setText( _(text) )
427 def showMessage(self, msg, msgType = MessageBox.TYPE_ERROR):
428 self.session.open(MessageBox, _(msg), msgType)
434 def KeyDefault(self):
435 configs = config.plugins.transcodingsetup.dict()
436 for (configName, configElement) in configs.items():
437 if configName == "automode":
439 configElement.value = configElement.default
441 if "automode" in configs.keys():
442 configElement = configs["automode"]
443 configElement.value = configElement.default
448 if not checkSupportAdvanced():
450 if self.setupMode == "Normal":
451 self.setupMode = "Advanced"
452 self["key_blue"].setText( _("Normal") )
454 self.setupMode = "Normal"
455 self["key_blue"].setText( _("Advanced") )
458 def resetConfig(self):
459 for x in self["config"].list:
463 ConfigListScreen.keyLeft(self)
464 if self.automode is not None and (self["config"].getCurrent() == self.automode) :
468 ConfigListScreen.keyRight(self)
469 if self.automode is not None and (self["config"].getCurrent() == self.automode) :
472 def cancelConfirm(self, result):
476 configs = config.plugins.transcodingsetup.dict()
477 for (key, configElement) in configs.items():
478 if key == "automode":
480 configElement.cancel()
482 if "automode" in configs.keys():
483 configElement = configs["automode"]
484 configElement.cancel()
489 transcodingsetupinit.pluginsetup = None
490 if self["config"].isChanged():
491 self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
495 def main(session, **kwargs):
496 session.open(TranscodingSetup)
498 def Plugins(**kwargs):
499 return [PluginDescriptor(name=_("TranscodingSetup"), description=_("Transcoding Setup"), where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = False, fnc=main)]
501 transcodingsetupinit = TranscodingSetupInit()