Version 0.9: Correct issue, when global delay was set. Removed all the filehandling...
[enigma2-plugins.git] / ac3lipsync / src / AC3delay.py
1 from AC3utils import AC3, PCM, 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.lamedbDelay = {}
20
21         self.getAudioInformation()
22
23         self.activateTimer = eTimer()
24         self.activateTimer.callback.append(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         if self.oHWInfo.get_device_name() in ("dm800","dm8000","dm500hd"):
33             self.bHasToRestartService = False
34         else:
35             self.bHasToRestartService = True
36         
37     def initAudio(self):
38         self.iService = NavigationInstance.instance.getCurrentService()
39         self.iServiceReference = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
40         self.iAudioDelay = self.iService and self.iService.audioDelay()
41         self.iSeek = self.iService and self.iService.seek()
42
43     def deleteAudio(self):
44         self.iService = None
45         self.iAudioDelay = None
46         self.iSeek = None
47
48     def setChannelAudio(self, sAudio):
49         self.channelAudio = sAudio
50
51     def delayedActivateDelay(self):
52         if self.activateTimer.isActive:
53             self.activateTimer.stop()
54         self.activateTimer.start(self.activateWait, False)
55
56     def activateDelay(self):
57         if self.activateTimer.isActive:
58             self.activateTimer.stop()
59         if self.bHasToRestartService == True:
60             bInitialized = False
61             if self.iService == None:
62                 self.initAudio()
63                 bInitialized = True
64             if self.iServiceReference is not None:
65                 lCurPosition = self.cueGetCurrentPosition()
66                 self.deleteAudio()
67                 if self.whichAudio == self.channelAudio:
68                     config.usage.on_movie_start.setValue("beginning")
69                     NavigationInstance.instance.stopService()
70                     NavigationInstance.instance.playService(self.iServiceReference)
71                     config.usage.on_movie_start.setValue(self.movieStart)
72                     if lCurPosition is not None:
73                         self.lCurPosition = lCurPosition
74                         self.timer = eTimer()
75                         self.timer.callback.append(self.seekAfterWait)
76                         self.timer.start(200, False)
77             else:
78                 self.deleteAudio()
79         
80     def seekAfterWait(self):
81         self.timer.stop()
82         self.initAudio()
83         if self.iSeek is not None:
84             self.iSeek.seekTo(self.lCurPosition)
85         self.deleteAudio()
86
87     def cueGetCurrentPosition(self):
88         if self.iSeek is None:
89             return None
90         r = self.iSeek.getPlayPosition()
91         if r[0]:
92             return None
93         return long(r[1])
94
95     def getLamedbDelay(self, sAudio):
96         bInitialized = False
97         if self.iService == None:
98             self.initAudio()
99             bInitialized = True
100         iDelay = 0
101         if self.iAudioDelay is not None:
102             if sAudio == AC3:
103                 iDelay = self.iAudioDelay.getAC3Delay()
104             else:
105                 iDelay = self.iAudioDelay.getPCMDelay()
106         if bInitialized == True:
107             self.deleteAudio()
108         if iDelay == -1:
109             iDelay = 0
110         return iDelay
111
112     def setLamedbDelay(self, sAudio, iDelay, bDelayStart):
113         bInitialized = False
114         if self.iService == None:
115             self.initAudio()
116             bInitialized = True
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         if bInitialized == True:
125             self.deleteAudio()
126
127     def getAudioInformation(self):
128         bInitialized = False
129         if self.iService == None:
130             self.initAudio()
131             bInitialized = True
132
133         # check if we are in a recording
134         lCurPosition = self.cueGetCurrentPosition()
135         if lCurPosition is not None:
136             self.bIsRecording = True
137             
138         # check if downmix is enabled
139         try:
140             bDownmixEnabled = config.av.downmix_ac3.value
141         except:
142             bDownmixEnabled = False
143
144         oAudioTracks = self.iService and self.iService.audioTracks()
145         n = oAudioTracks and oAudioTracks.getNumberOfTracks() or 0
146         tlist = []
147         self.selectedAudioIndex = oAudioTracks.getCurrentTrack()
148         if n >= 0:
149             for x in range(n):
150                 i = oAudioTracks.getTrackInfo(x)
151                 language = i.getLanguage()
152                 description = i.getDescription()
153                 if LanguageCodes.has_key(language):
154                     language = LanguageCodes[language][0]
155                 if len(description):
156                     description += " (" + language + ")"
157                 else:
158                     description = language
159
160                 tlist.append((description, x))
161                 if x == self.selectedAudioIndex:
162                     if ((description.find("AC3") != -1 or description.find("AC-3") != -1) and not bDownmixEnabled) or description.find("DTS") != -1:
163                         self.whichAudio = AC3
164                         self.channelAudio = AC3
165                     else:
166                         self.whichAudio = PCM
167                         self.channelAudio = PCM
168                     self.selectedAudioInfo = (description, x)
169             tlist.sort(key=lambda x: x[0])
170
171             self.audioTrackList = tlist
172         for sAudio in AC3PCM:
173             self.lamedbDelay[sAudio]=self.getLamedbDelay(sAudio)
174         del oAudioTracks
175         if bInitialized == True:
176             self.deleteAudio()