tubelib: use eServiceReference.idStream service for twitch
[enigma2-plugins.git] / audiosync / src / AC3delay.py
1 from AC3utils import AC3, PCM, AC3GLOB, PCMGLOB, AC3PCM
2 from Components.config import config
3 from enigma import eTimer
4 from Tools.ISO639 import LanguageCodes
5 from Tools.HardwareInfo import HardwareInfo
6 import os
7 import NavigationInstance
8
9 class AC3delay:
10     def __init__(self):
11         self.iService = None
12         self.iServiceReference = None
13         self.iAudioDelay = None
14         self.channelAudio = AC3
15         self.whichAudio = AC3
16         self.bIsRecording = False
17
18         # Current audio- delay
19         self.systemDelay = {}
20
21         self.getAudioInformation()
22
23         self.activateTimer = eTimer()
24         self.activateTimer_conn = self.activateTimer.timeout.connect(self.activateDelay)
25         self.activateWait = config.plugins.AC3LipSync.activationDelay.getValue()
26         
27         # Current value for movie start behaviour
28         self.movieStart = config.usage.on_movie_start.getValue()
29
30         # find out box type
31         self.oHWInfo = HardwareInfo()
32         self.bHasToRestartService = self.oHWInfo.get_device_name() == "dm7025"
33         
34     def initAudio(self):
35         self.iService = NavigationInstance.instance.getCurrentService()
36         self.iServiceReference = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
37         self.iAudioDelay = self.iService and self.iService.audioDelay()
38         self.iSeek = self.iService and self.iService.seek()
39
40     def deleteAudio(self):
41         self.iService = None
42         self.iAudioDelay = None
43         self.iSeek = None
44
45     def setChannelAudio(self, sAudio):
46         self.channelAudio = sAudio
47
48     def delayedActivateDelay(self):
49         if self.activateTimer.isActive:
50             self.activateTimer.stop()
51         self.activateTimer.start(self.activateWait, False)
52
53     def activateDelay(self):
54         # This activation code is only neccessary for DM7025. 
55         # DM800, DM8000 and DM500HD directly activate the delay after using "setAC3Delay" and "setPCMDelay", they don't need the service restart
56         if self.activateTimer.isActive:
57             self.activateTimer.stop()
58         if self.bHasToRestartService == True:
59             bInitialized = False
60             if self.iService == None:
61                 self.initAudio()
62                 bInitialized = True
63             if self.iServiceReference is not None:
64                 lCurPosition = self.cueGetCurrentPosition()
65                 self.deleteAudio()
66                 if self.whichAudio == self.channelAudio:
67                     config.usage.on_movie_start.setValue("beginning")
68                     NavigationInstance.instance.stopService()
69                     NavigationInstance.instance.playService(self.iServiceReference)
70                     config.usage.on_movie_start.setValue(self.movieStart)
71                     if lCurPosition is not None:
72                         self.lCurPosition = lCurPosition
73                         self.timer = eTimer()
74                         self.timer_conn = self.timer.timeout.connect(self.seekAfterWait)
75                         self.timer.start(200, False)
76             else:
77                 self.deleteAudio()
78         
79     def seekAfterWait(self):
80         self.timer.stop()
81         self.initAudio()
82         if self.iSeek is not None:
83             self.iSeek.seekTo(self.lCurPosition)
84         self.deleteAudio()
85
86     def cueGetCurrentPosition(self):
87         if self.iSeek is None:
88             return None
89         r = self.iSeek.getPlayPosition()
90         if r[0]:
91             return None
92         return long(r[1])
93
94     def getSystemDelay(self, sAudio):
95         bInitialized = False
96         if self.iService == None:
97             self.initAudio()
98             bInitialized = True
99         iDelay = 0
100         if self.iAudioDelay is not None:
101             if sAudio == AC3:
102                 iDelay = self.iAudioDelay.getAC3Delay()
103             elif sAudio == PCM:
104                 iDelay = self.iAudioDelay.getPCMDelay()
105             elif sAudio == AC3GLOB:
106                 iDelay = config.av.generalAC3delay.getValue()
107             else:
108                 iDelay = config.av.generalPCMdelay.getValue()
109         if bInitialized == True:
110             self.deleteAudio()
111         if iDelay == -1:
112             iDelay = 0
113         return iDelay
114
115     def setSystemDelay(self, sAudio, iDelay, bDelayStart):
116         bInitialized = False
117         if self.iService == None:
118             self.initAudio()
119             bInitialized = True
120         if self.iAudioDelay is not None:
121             iDelayLameDb = iDelay
122             if iDelayLameDb == 0:
123                 iDelayLameDb = -1
124             if sAudio == AC3:
125                 self.iAudioDelay.setAC3Delay(iDelayLameDb)
126             elif sAudio == PCM:
127                 self.iAudioDelay.setPCMDelay(iDelayLameDb)
128             elif sAudio == AC3GLOB:
129                 config.av.generalAC3delay.setValue(iDelay)
130                 config.av.generalAC3delay.save()
131                 #Setting the global delay does not activate it, so now we call setAC3Delay to activate the new delay..
132                 self.iAudioDelay.setAC3Delay(self.systemDelay[AC3])
133             else:
134                 config.av.generalPCMdelay.setValue(iDelay)
135                 config.av.generalPCMdelay.save()
136                 #Setting the global delay does not activate it, so now we call setPCMDelay to activate the new delay..
137                 self.iAudioDelay.setPCMDelay(self.systemDelay[PCM])
138         if bInitialized == True:
139             self.deleteAudio()
140         if bDelayStart == True:
141             self.delayedActivateDelay()
142         else:
143             self.activateDelay()
144
145     def getAudioInformation(self):
146         bInitialized = False
147         if self.iService == None:
148             self.initAudio()
149             bInitialized = True
150
151         # check if we are in a recording
152         lCurPosition = self.cueGetCurrentPosition()
153         if lCurPosition is not None:
154             self.bIsRecording = True
155             
156         # check if downmix is enabled
157         try:
158             bDownmixEnabled = config.av.downmix_ac3.value
159         except:
160             bDownmixEnabled = False
161
162         oAudioTracks = self.iService and self.iService.audioTracks()
163         n = oAudioTracks and oAudioTracks.getNumberOfTracks() or 0
164         tlist = []
165         self.selectedAudioIndex = oAudioTracks.getCurrentTrack()
166         if n >= 0:
167             for x in range(n):
168                 i = oAudioTracks.getTrackInfo(x)
169                 language = i.getLanguage()
170                 description = i.getDescription()
171                 if LanguageCodes.has_key(language):
172                     language = LanguageCodes[language][0]
173                 if len(description):
174                     description += " (" + language + ")"
175                 else:
176                     description = language
177
178                 tlist.append((description, x))
179                 if x == self.selectedAudioIndex:
180                     if ((description.find("AC3") != -1 or description.find("AC-3") != -1) and not bDownmixEnabled) or description.find("DTS") != -1:
181                         self.whichAudio = AC3
182                         self.channelAudio = AC3
183                     else:
184                         self.whichAudio = PCM
185                         self.channelAudio = PCM
186                     self.selectedAudioInfo = (description, x)
187             tlist.sort(key=lambda x: x[0])
188
189             self.audioTrackList = tlist
190         for sAudio in AC3PCM:
191             self.systemDelay[sAudio]=self.getSystemDelay(sAudio)
192         del oAudioTracks
193         if bInitialized == True:
194             self.deleteAudio()