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