2dd6ad9049e7ca56a0106e6f65bd1039f9d11381
[vuplus_dvbapp] / lib / python / Screens / AudioSelection.py
1 from Screen import Screen
2 from Components.ServiceEventTracker import ServiceEventTracker
3 from Components.ActionMap import ActionMap
4 from Components.ConfigList import ConfigListScreen
5 from Components.ChoiceList import ChoiceList, ChoiceEntryComponent
6 from Components.config import config, ConfigSubsection, getConfigListEntry, ConfigNothing, ConfigSelection, ConfigOnOff
7 from Components.MultiContent import MultiContentEntryText
8 from Components.Sources.List import List
9 from Components.Sources.Boolean import Boolean
10 from Components.SystemInfo import SystemInfo
11
12 from enigma import iPlayableService
13
14 from Tools.ISO639 import LanguageCodes
15 from Tools.BoundFunction import boundFunction
16 FOCUS_CONFIG, FOCUS_STREAMS = range(2)
17 [PAGE_AUDIO, PAGE_SUBTITLES] = ["audio", "subtitles"]
18
19 class AudioSelection(Screen, ConfigListScreen):
20         def __init__(self, session, infobar=None, page=PAGE_AUDIO):
21                 Screen.__init__(self, session)
22
23                 self["streams"] = List([])
24                 self["key_red"] = Boolean(False)
25                 self["key_green"] = Boolean(False)
26                 self["key_yellow"] = Boolean(True)
27                 self["key_blue"] = Boolean(False)
28                 
29                 ConfigListScreen.__init__(self, [])
30                 self.infobar = infobar or self.session.infobar
31
32                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
33                         {
34                                 iPlayableService.evUpdatedInfo: self.__updatedInfo
35                         })
36                 self.cached_subtitle_checked = False
37                 self.__selected_subtitle = None
38         
39                 self["actions"] = ActionMap(["ColorActions", "SetupActions", "DirectionActions"],
40                 {
41                         "red": self.keyRed,
42                         "green": self.keyGreen,
43                         "yellow": self.keyYellow,
44                         "blue": self.keyBlue,
45                         "ok": self.keyOk,
46                         "cancel": self.cancel,
47                         "up": self.keyUp,
48                         "down": self.keyDown,
49                 }, -3)
50
51                 self.settings = ConfigSubsection()
52                 choicelist = [(PAGE_AUDIO,_("audio tracks")), (PAGE_SUBTITLES,_("Subtitles"))]
53                 self.settings.menupage = ConfigSelection(choices = choicelist, default=page)
54                 self.settings.menupage.addNotifier(self.fillList)
55                 self.onLayoutFinish.append(self.__layoutFinished)
56
57         def __layoutFinished(self):
58                 self["config"].instance.setSelectionEnable(False)
59                 self.focus = FOCUS_STREAMS
60
61         def fillList(self, arg=None):
62                 streams = []
63                 conflist = []
64                 selectedidx = 0
65                 
66                 service = self.session.nav.getCurrentService()
67                 self.audioTracks = audio = service and service.audioTracks()
68                 n = audio and audio.getNumberOfTracks() or 0
69                 
70                 if self.settings.menupage.getValue() == PAGE_AUDIO:
71                         self.setTitle(_("Select audio track"))
72                         if SystemInfo["CanDownmixAC3"]:
73                                 print "config.av.downmix_ac3.value=", config.av.downmix_ac3.value
74                                 self.settings.downmix = ConfigOnOff(default=config.av.downmix_ac3.value)
75                                 self.settings.downmix.addNotifier(self.changeAC3Downmix, initial_call = False)
76                                 conflist.append(getConfigListEntry(_("AC3 downmix"), self.settings.downmix))
77                                 self["key_red"].setBoolean(True)
78
79                         if n > 0:
80                                 self.audioChannel = service.audioChannel()
81                                 print "self.audioChannel.getCurrentChannel()", self.audioChannel.getCurrentChannel()
82                                 choicelist = [("0",_("left")), ("1",_("stereo")), ("2", _("right"))]
83                                 self.settings.channelmode = ConfigSelection(choices = choicelist, default = str(self.audioChannel.getCurrentChannel()))
84                                 self.settings.channelmode.addNotifier(self.changeMode, initial_call = False)
85                                 conflist.append(getConfigListEntry(_("Channel"), self.settings.channelmode))
86                                 self["key_green"].setBoolean(True)
87                                 
88                                 selectedAudio = self.audioTracks.getCurrentTrack()
89                                 print "selectedAudio:", selectedAudio
90
91                                 for x in range(n):
92                                         number = str(x)
93                                         i = audio.getTrackInfo(x)
94                                         languages = i.getLanguage().split('/')
95                                         description = i.getDescription() or _("<unknown>")
96                                         selected = ""
97                                         language = ""
98
99                                         if selectedAudio == x:
100                                                 selected = _("Running")
101                                                 selectedidx = x
102
103                                         cnt = 0
104                                         for lang in languages:
105                                                 if cnt:
106                                                         language += ' / '
107                                                 if LanguageCodes.has_key(lang):
108                                                         language += LanguageCodes[lang][0]
109                                                 elif lang == "und":
110                                                         _("<unknown>")
111                                                 else:
112                                                         language += lang
113                                                 cnt += 1
114
115                                         streams.append((x, "", number, description, language, selected))
116
117                         else:
118                                 streams = []
119                                 self.settings.dummy = ConfigNothing()
120                                 conflist.append(getConfigListEntry("", self.settings.dummy))
121                                 self["key_green"].setBoolean(False)
122
123                 elif self.settings.menupage.getValue() == PAGE_SUBTITLES:
124                         self.setTitle(_("Subtitle selection"))
125                         
126                         self.settings.dummy = ConfigNothing()
127                         conflist.append(getConfigListEntry("", self.settings.dummy))
128                         self["key_red"].setBoolean(False)
129                         conflist.append(getConfigListEntry("", self.settings.dummy))
130                         self["key_green"].setBoolean(False)
131
132                         if self.subtitlesEnabled():
133                                 sel = self.infobar.selected_subtitle
134                         else:
135                                 sel = None
136
137                         idx = 0
138                         
139                         subtitlelist = self.getSubtitleList()
140
141                         if len(subtitlelist):
142                                 for x in subtitlelist:
143                                         number = str(x[1])
144                                         description = "?"
145                                         language = _("<unknown>")
146                                         selected = ""
147
148                                         if sel and x[:4] == sel[:4]:
149                                                 selected = _("Running")
150                                                 selectedidx = idx
151                                         
152                                         if x[4] != "und":
153                                                 if LanguageCodes.has_key(x[4]):
154                                                         language = LanguageCodes[x[4]][0]
155                                                 else:
156                                                         language = x[4]
157
158                                         if x[0] == 0:
159                                                 description = "DVB"
160                                                 number = "%x" % (x[1])
161
162                                         elif x[0] == 1:
163                                                 description = "TTX"
164                                                 number = "%x%02x" % (x[3],x[2])
165
166                                         elif x[0] == 2:
167                                                 types = ("UTF-8 text","SSA / AAS",".SRT file")
168                                                 description = types[x[2]]
169
170                                         streams.append((x, "", number, description, language, selected))
171                                         idx += 1
172                         
173                         else:
174                                 streams = []
175
176                 conflist.append(getConfigListEntry(_("Menu"), self.settings.menupage))
177                 
178                 from Components.PluginComponent import plugins
179                 from Plugins.Plugin import PluginDescriptor
180                 
181                 if hasattr(self.infobar, "runPlugin"):
182                         class PluginCaller:
183                                 def __init__(self, fnc, *args):
184                                         self.fnc = fnc
185                                         self.args = args
186                                 def __call__(self, *args, **kwargs):
187                                         self.fnc(*self.args)
188
189                         Plugins = [ (p.name, PluginCaller(self.infobar.runPlugin, p)) for p in plugins.getPlugins(where = PluginDescriptor.WHERE_AUDIOMENU) ]
190                         
191                         print "Plugins", Plugins
192
193                         self.settings.dummy = ConfigNothing()
194                         if len(Plugins):
195                                 self["key_blue"].setBoolean(True)
196                                 conflist.append(getConfigListEntry(Plugins[0][0], self.settings.dummy))
197                                 self.plugincallfunc = Plugins[0][1]
198
199                 self["config"].list = conflist
200                 self["config"].l.setList(conflist)
201
202                 print "setting self[streams].list:", streams
203                 self["streams"].list = streams
204                 print "setting self[streams].setIndex:", selectedidx
205                 self["streams"].setIndex(selectedidx)
206                 print "debug1"
207
208         def __updatedInfo(self):
209                 self.fillList()
210
211         def getSubtitleList(self):
212                 s = self.infobar and self.infobar.getCurrentServiceSubtitle()
213                 l = s and s.getSubtitleList() or [ ]
214                 return l
215
216         def subtitlesEnabled(self):
217                 return self.infobar.subtitles_enabled
218
219         def enableSubtitle(self, subtitles):
220                 print "[enableSubtitle]", subtitles
221                 if self.infobar.selected_subtitle != subtitles:
222                         self.infobar.subtitles_enabled = False
223                         self.infobar.selected_subtitle = subtitles
224                         if subtitles:
225                                 self.infobar.subtitles_enabled = True
226
227         def changeAC3Downmix(self, downmix):
228                 print "changeAC3Downmix config.av.downmix_ac3.value=", config.av.downmix_ac3.value, downmix.getValue()
229                 if downmix.getValue() == True:
230                         config.av.downmix_ac3.value = True
231                 else:
232                         config.av.downmix_ac3.value = False
233                 config.av.downmix_ac3.save()
234
235         def changeMode(self, mode):
236                 print "changeMode", mode, mode.getValue()
237                 if mode is not None:
238                         self.audioChannel.selectChannel(int(mode.getValue()))
239
240         def changeAudio(self, audio):
241                 print "changeAudio", audio, "self.session.nav.getCurrentService().audioTracks().getNumberOfTracks():", self.session.nav.getCurrentService().audioTracks().getNumberOfTracks()
242                 track = int(audio)
243                 if isinstance(track, int):
244                         if self.session.nav.getCurrentService().audioTracks().getNumberOfTracks() > track:
245                                 self.audioTracks.selectTrack(track)
246
247         def keyLeft(self):
248                 if self.focus == FOCUS_CONFIG:
249                         ConfigListScreen.keyLeft(self)
250                 elif self.focus == FOCUS_STREAMS:
251                         self["streams"].setIndex(0)
252
253         def keyRight(self, config = False):
254                 if config or self.focus == FOCUS_CONFIG:
255                         if self["config"].getCurrentIndex() < 3:
256                                 ConfigListScreen.keyRight(self)
257                         elif hasattr(self, "plugincallfunc"):
258                                 self.plugincallfunc()
259                 if self.focus == FOCUS_STREAMS and self["streams"].count() and config == False:
260                         self["streams"].setIndex(self["streams"].count()-1)
261
262         def keyRed(self):
263                 if self["key_red"].getBoolean():
264                         self.colorkey(0)
265
266         def keyGreen(self):
267                 if self["key_green"].getBoolean():
268                         self.colorkey(1)
269
270         def keyYellow(self):
271                 if self["key_yellow"].getBoolean():
272                         self.colorkey(2)
273
274         def keyBlue(self):
275                 if self["key_blue"].getBoolean():
276                         print "colorkey(3)"
277                         self.colorkey(3)
278
279         def colorkey(self, idx):
280                 self["config"].setCurrentIndex(idx)
281                 self.keyRight(True)
282
283         def keyUp(self):
284                 print "[keyUp]", self["streams"].getIndex()
285                 if self.focus == FOCUS_CONFIG:
286                         self["config"].instance.moveSelection(self["config"].instance.moveUp)
287                 elif self.focus == FOCUS_STREAMS:
288                         if self["streams"].getIndex() == 0:
289                                 self["config"].instance.setSelectionEnable(True)
290                                 self["streams"].style = "notselected"
291                                 self["config"].setCurrentIndex(len(self["config"].getList())-1)
292                                 self.focus = FOCUS_CONFIG
293                         else:
294                                 self["streams"].selectPrevious()
295
296         def keyDown(self):
297                 print "[keyDown]", self["config"].getCurrentIndex(), len(self["config"].getList())-1
298                 if self.focus == FOCUS_CONFIG:
299                         if self["config"].getCurrentIndex() < len(self["config"].getList())-1:
300                                 self["config"].instance.moveSelection(self["config"].instance.moveDown)
301                         else:
302                                 self["config"].instance.setSelectionEnable(False)
303                                 self["streams"].style = "default"
304                                 self.focus = FOCUS_STREAMS
305                 elif self.focus == FOCUS_STREAMS:
306                         self["streams"].selectNext()
307
308         def keyOk(self):
309                 print "[keyok]", self["streams"].list, self["streams"].getCurrent()
310                 if self.focus == FOCUS_STREAMS and self["streams"].list:
311                         cur = self["streams"].getCurrent()
312                         if self.settings.menupage.getValue() == PAGE_AUDIO and cur[0] is not None:
313                                 self.changeAudio(cur[2])
314                                 self.__updatedInfo()
315                         if self.settings.menupage.getValue() == PAGE_SUBTITLES and cur[0] is not None:
316                                 if self.infobar.selected_subtitle == cur[0]:
317                                         self.enableSubtitle(None)
318                                         selectedidx = self["streams"].getIndex()
319                                         self.__updatedInfo()
320                                         self["streams"].setIndex(selectedidx)
321                                 else:
322                                         self.enableSubtitle(cur[0])
323                                         self.__updatedInfo()
324                 #self.close()
325                 elif self.focus == FOCUS_CONFIG:
326                         self.keyRight()
327
328         def cancel(self):
329                 self.close(0)
330
331 class SubtitleSelection(AudioSelection):
332         def __init__(self, session, infobar=None):
333                 AudioSelection.__init__(self, session, infobar, page=PAGE_SUBTITLES)
334                 self.skinName = ["AudioSelection"]