added configurable activation delay to allow multiple keypresses before setting the...
[enigma2-plugins.git] / ac3lipsync / src / AC3delay.py
1 from AC3utils import AC3, PCM, AC3PCM, lFileDelay, dec2hex, hex2dec
2 from Components.config import config
3 from enigma import eTimer
4 from Tools.ISO639 import LanguageCodes
5 import os
6 import NavigationInstance
7
8 class AC3delay:
9     def __init__(self, session):
10         self.iService = None
11         self.iServiceReference = None
12         self.iAudioDelay = None
13         self.channelAudio = AC3
14         self.whichAudio = AC3
15         self.bIsRecording = False
16
17         # Current audio- delay
18         self.lamedbDelay = {}
19
20         self.getAudioInformation()
21
22         self.activateTimer = eTimer()
23         self.activateTimer.callback.append(self.activateDelay)
24         self.activateWait = config.plugins.AC3LipSync.activationDelay.getValue()
25         
26         # Current value for movie start behaviour
27         self.movieStart = config.usage.on_movie_start.getValue()
28
29     def initAudio(self):
30         self.iService = NavigationInstance.instance.getCurrentService()
31         self.iServiceReference = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
32         self.iAudioDelay = self.iService and self.iService.audioDelay()
33         self.iSeek = self.iService and self.iService.seek()
34
35     def deleteAudio(self):
36         self.iService = None
37         self.iAudioDelay = None
38         self.iSeek = None
39
40     def setChannelAudio(self, sAudio):
41         self.channelAudio = sAudio
42
43     def delayedActivateDelay(self):
44         if self.activateTimer.isActive:
45             self.activateTimer.stop()
46         self.activateTimer.start(self.activateWait, False)
47
48     def activateDelay(self):
49         if self.activateTimer.isActive:
50             self.activateTimer.stop()
51         bInitialized = False
52         if self.iService == None:
53             self.initAudio()
54             bInitialized = True
55         if self.iServiceReference is not None:
56             lCurPosition = self.cueGetCurrentPosition()
57             self.deleteAudio()
58             if self.whichAudio == self.channelAudio:
59                 config.usage.on_movie_start.setValue("beginning")
60                 NavigationInstance.instance.stopService()
61                 NavigationInstance.instance.playService(self.iServiceReference)
62                 config.usage.on_movie_start.setValue(self.movieStart)
63                 if lCurPosition is not None:
64                     self.lCurPosition = lCurPosition
65                     self.timer = eTimer()
66                     self.timer.callback.append(self.seekAfterWait)
67                     self.timer.start(200, False)
68         else:
69             self.deleteAudio()
70         
71     def seekAfterWait(self):
72         self.timer.stop()
73         self.initAudio()
74         if self.iSeek is not None:
75             self.iSeek.seekTo(self.lCurPosition)
76         self.deleteAudio()
77
78     def cueGetCurrentPosition(self):
79         if self.iSeek is None:
80             return None
81         r = self.iSeek.getPlayPosition()
82         if r[0]:
83             return None
84         return long(r[1])
85
86     def getLamedbDelay(self, sAudio):
87         bInitialized = False
88         if self.iService == None:
89             self.initAudio()
90             bInitialized = True
91         iDelay = 0
92         if self.iAudioDelay is not None:
93             if sAudio == AC3:
94                 iDelay = self.iAudioDelay.getAC3Delay()
95             else:
96                 iDelay = self.iAudioDelay.getPCMDelay()
97             if iDelay == -1:
98                 iDelay = 0
99         if bInitialized == True:
100             self.deleteAudio()
101         return iDelay
102
103     def getFileDelay(self, sAudio):
104         sFileName = lFileDelay[sAudio]
105         if os.path.exists(sFileName) == True:
106             delayfile = open(sFileName,"r")
107             delay = 0
108             delay = delayfile.readline()
109             delayfile.close()
110             iDelay = hex2dec(delay)/90
111         else:
112             iDelay = 0
113         return int(iDelay)
114
115     def setLamedbDelay(self, sAudio, iDelay):
116         self.initAudio()
117         if self.iAudioDelay is not None:
118             if iDelay == 0:
119                 iDelay = -1
120             if sAudio == AC3:
121                 self.iAudioDelay.setAC3Delay(iDelay)
122             else:
123                 self.iAudioDelay.setPCMDelay(iDelay)
124         #self.activateDelay()
125
126     def setFileDelay(self, sAudio, iDelay, bDelayStart):
127         hDelay = dec2hex(iDelay*90)
128         sFileName = lFileDelay[sAudio]
129         if os.path.exists(sFileName) == True:
130             delayfile = open(lFileDelay[sAudio],"w")
131             delayfile.write("%s\0" % hDelay)
132             delayfile.close()
133             if bDelayStart == True:
134                 self.delayedActivateDelay()
135             else:
136                 self.activateDelay()
137
138     def getAudioInformation(self):
139         bInitialized = False
140         if self.iService == None:
141             self.initAudio()
142             bInitialized = True
143         self.initAudio()
144         lCurPosition = self.cueGetCurrentPosition()
145         if lCurPosition is not None:
146             self.bIsRecording = True
147         oAudioTracks = self.iService and self.iService.audioTracks()
148         n = oAudioTracks and oAudioTracks.getNumberOfTracks() or 0
149         tlist = []
150         self.selectedAudioIndex = oAudioTracks.getCurrentTrack()
151         if n >= 0:
152             for x in range(n):
153                 i = oAudioTracks.getTrackInfo(x)
154                 language = i.getLanguage()
155                 description = i.getDescription()
156                 if LanguageCodes.has_key(language):
157                     language = LanguageCodes[language][0]
158                 if len(description):
159                     description += " (" + language + ")"
160                 else:
161                     description = language
162
163                 tlist.append((description, x))
164                 if x == self.selectedAudioIndex:
165                     if description.find("AC3") != -1 or description.find("DTS") != -1:
166                         self.whichAudio = AC3
167                         self.channelAudio = AC3
168                     else:
169                         self.whichAudio = PCM
170                         self.channelAudio = PCM
171                     self.selectedAudioInfo = (description, x)
172             tlist.sort(key=lambda x: x[0])
173
174             self.audioTrackList = tlist
175         for sAudio in AC3PCM:
176             self.lamedbDelay[sAudio]=self.getLamedbDelay(sAudio)
177         del oAudioTracks
178         if bInitialized == True:
179             self.deleteAudio()
180