changed: Add logic to properly handle subtitles for stacked files
[vuplus_xbmc] / xbmc / cores / AudioEngine / Engines / ActiveAE / ActiveAE.h
1 #pragma once
2 /*
3  *      Copyright (C) 2010-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include "system.h"
23 #include "threads/Thread.h"
24
25 #include "ActiveAESink.h"
26 #include "ActiveAEResample.h"
27 #include "Interfaces/AEStream.h"
28 #include "Interfaces/AESound.h"
29 #include "AEFactory.h"
30 #include "guilib/DispResource.h"
31
32 // ffmpeg
33 #include "DllAvFormat.h"
34 #include "DllAvCodec.h"
35 #include "DllAvUtil.h"
36
37 class IAESink;
38 class IAEEncoder;
39
40 namespace ActiveAE
41 {
42
43 class CActiveAESound;
44 class CActiveAEStream;
45
46 struct AudioSettings
47 {
48   std::string device;
49   std::string driver;
50   std::string passthoughdevice;
51   int channels;
52   bool ac3passthrough;
53   bool eac3passthrough;
54   bool dtspassthrough;
55   bool truehdpassthrough;
56   bool dtshdpassthrough;
57   bool stereoupmix;
58   bool normalizelevels;
59   bool passthrough;
60   int config;
61   unsigned int samplerate;
62   AEQuality resampleQuality;
63 };
64
65 class CActiveAEControlProtocol : public Protocol
66 {
67 public:
68   CActiveAEControlProtocol(std::string name, CEvent* inEvent, CEvent *outEvent) : Protocol(name, inEvent, outEvent) {};
69   enum OutSignal
70   {
71     INIT = 0,
72     RECONFIGURE,
73     SUSPEND,
74     MUTE,
75     VOLUME,
76     PAUSESTREAM,
77     RESUMESTREAM,
78     FLUSHSTREAM,
79     STREAMRGAIN,
80     STREAMVOLUME,
81     STREAMAMP,
82     STREAMRESAMPLERATIO,
83     STREAMFADE,
84     STOPSOUND,
85     SOUNDMODE,
86     GETSTATE,
87     DISPLAYLOST,
88     DISPLAYRESET,
89     TIMEOUT,
90   };
91   enum InSignal
92   {
93     ACC,
94     ERR,
95     STATS,
96   };
97 };
98
99 class CActiveAEDataProtocol : public Protocol
100 {
101 public:
102   CActiveAEDataProtocol(std::string name, CEvent* inEvent, CEvent *outEvent) : Protocol(name, inEvent, outEvent) {};
103   enum OutSignal
104   {
105     NEWSOUND = 0,
106     PLAYSOUND,
107     FREESOUND,
108     NEWSTREAM,
109     FREESTREAM,
110     STREAMSAMPLE,
111     DRAINSTREAM,
112   };
113   enum InSignal
114   {
115     ACC,
116     ERR,
117     STREAMBUFFER,
118     STREAMDRAINED,
119   };
120 };
121
122 struct MsgStreamNew
123 {
124   AEAudioFormat format;
125   unsigned int options;
126 };
127
128 struct MsgStreamSample
129 {
130   CSampleBuffer *buffer;
131   CActiveAEStream *stream;
132 };
133
134 struct MsgStreamParameter
135 {
136   CActiveAEStream *stream;
137   union
138   {
139     float float_par;
140     double double_par;
141   } parameter;
142 };
143
144 struct MsgStreamFade
145 {
146   CActiveAEStream *stream;
147   float from;
148   float target;
149   unsigned int millis;
150 };
151
152 class CEngineStats
153 {
154 public:
155   void Reset(unsigned int sampleRate);
156   void UpdateSinkDelay(double delay, int samples);
157   void AddSamples(int samples, std::list<CActiveAEStream*> &streams);
158   float GetDelay();
159   float GetDelay(CActiveAEStream *stream);
160   float GetCacheTime(CActiveAEStream *stream);
161   float GetCacheTotal(CActiveAEStream *stream);
162   float GetWaterLevel();
163   void SetSuspended(bool state);
164   void SetSinkCacheTotal(float time) { m_sinkCacheTotal = time; }
165   bool IsSuspended();
166   CCriticalSection *GetLock() { return &m_lock; }
167 protected:
168   float m_sinkDelay;
169   float m_sinkCacheTotal;
170   int m_bufferedSamples;
171   unsigned int m_sinkSampleRate;
172   unsigned int m_sinkUpdate;
173   bool m_suspended;
174   CCriticalSection m_lock;
175 };
176
177 #if defined(HAS_GLX) || defined(TARGET_DARWIN_OSX)
178 class CActiveAE : public IAE, public IDispResource, private CThread
179 #else
180 class CActiveAE : public IAE, private CThread
181 #endif
182 {
183 protected:
184   friend class ::CAEFactory;
185   friend class CActiveAESound;
186   friend class CActiveAEStream;
187   friend class CSoundPacket;
188   friend class CActiveAEBufferPoolResample;
189   CActiveAE();
190   virtual ~CActiveAE();
191   virtual bool  Initialize();
192
193 public:
194   virtual void   Shutdown();
195   virtual bool   Suspend();
196   virtual bool   Resume();
197   virtual bool   IsSuspended();
198   virtual void   OnSettingsChange(const std::string& setting);
199
200   virtual float GetVolume();
201   virtual void  SetVolume(const float volume);
202   virtual void  SetMute(const bool enabled);
203   virtual bool  IsMuted();
204   virtual void  SetSoundMode(const int mode);
205
206   /* returns a new stream for data in the specified format */
207   virtual IAEStream *MakeStream(enum AEDataFormat dataFormat, unsigned int sampleRate, unsigned int encodedSampleRate, CAEChannelInfo channelLayout, unsigned int options = 0);
208   virtual IAEStream *FreeStream(IAEStream *stream);
209
210   /* returns a new sound object */
211   virtual IAESound *MakeSound(const std::string& file);
212   virtual void      FreeSound(IAESound *sound);
213
214   virtual void GarbageCollect() {};
215
216   virtual void EnumerateOutputDevices(AEDeviceList &devices, bool passthrough);
217   virtual std::string GetDefaultDevice(bool passthrough);
218   virtual bool SupportsRaw(AEDataFormat format);
219   virtual bool SupportsSilenceTimeout();
220   virtual bool SupportsQualityLevel(enum AEQuality level);
221   virtual bool IsSettingVisible(const std::string &settingId);
222
223   virtual void RegisterAudioCallback(IAudioCallback* pCallback);
224   virtual void UnregisterAudioCallback();
225
226   virtual void OnLostDevice();
227   virtual void OnResetDevice();
228
229 protected:
230   void PlaySound(CActiveAESound *sound);
231   uint8_t **AllocSoundSample(SampleConfig &config, int &samples, int &bytes_per_sample, int &planes, int &linesize);
232   void FreeSoundSample(uint8_t **data);
233   float GetDelay(CActiveAEStream *stream) { return m_stats.GetDelay(stream); }
234   float GetCacheTime(CActiveAEStream *stream) { return m_stats.GetCacheTime(stream); }
235   float GetCacheTotal(CActiveAEStream *stream) { return m_stats.GetCacheTotal(stream); }
236   void FlushStream(CActiveAEStream *stream);
237   void PauseStream(CActiveAEStream *stream, bool pause);
238   void StopSound(CActiveAESound *sound);
239   void SetStreamAmplification(CActiveAEStream *stream, float amplify);
240   void SetStreamReplaygain(CActiveAEStream *stream, float rgain);
241   void SetStreamVolume(CActiveAEStream *stream, float volume);
242   void SetStreamResampleRatio(CActiveAEStream *stream, double ratio);
243   void SetStreamFade(CActiveAEStream *stream, float from, float target, unsigned int millis);
244
245 protected:
246   void Process();
247   void StateMachine(int signal, Protocol *port, Message *msg);
248   bool InitSink();
249   void DrainSink();
250   void UnconfigureSink();
251   bool IsSinkCompatible(const AEAudioFormat format, const std::string &device);
252   void Start();
253   void Dispose();
254   void LoadSettings();
255   bool NeedReconfigureBuffers();
256   bool NeedReconfigureSink();
257   void ApplySettingsToFormat(AEAudioFormat &format, AudioSettings &settings, int *mode = NULL);
258   void Configure(AEAudioFormat *desiredFmt = NULL);
259   AEAudioFormat GetInputFormat(AEAudioFormat *desiredFmt = NULL);
260   CActiveAEStream* CreateStream(MsgStreamNew *streamMsg);
261   void DiscardStream(CActiveAEStream *stream);
262   void SFlushStream(CActiveAEStream *stream);
263   void FlushEngine();
264   void ClearDiscardedBuffers();
265   void SStopSound(CActiveAESound *sound);
266   void DiscardSound(CActiveAESound *sound);
267   void ChangeResamplers();
268
269   bool RunStages();
270   bool HasWork();
271
272   void ResampleSounds();
273   bool ResampleSound(CActiveAESound *sound);
274   void MixSounds(CSoundPacket &dstSample);
275   void Deamplify(CSoundPacket &dstSample);
276
277   bool CompareFormat(AEAudioFormat &lhs, AEAudioFormat &rhs);
278
279   CEvent m_inMsgEvent;
280   CEvent m_outMsgEvent;
281   CActiveAEControlProtocol m_controlPort;
282   CActiveAEDataProtocol m_dataPort;
283   int m_state;
284   bool m_bStateMachineSelfTrigger;
285   int m_extTimeout;
286   bool m_extError;
287   bool m_extDrain;
288   XbmcThreads::EndTime m_extDrainTimer;
289   bool m_extDeferData;
290
291   enum
292   {
293     MODE_RAW,
294     MODE_TRANSCODE,
295     MODE_PCM
296   }m_mode;
297
298   CActiveAESink m_sink;
299   AEAudioFormat m_sinkFormat;
300   AEAudioFormat m_sinkRequestFormat;
301   AEAudioFormat m_encoderFormat;
302   AEAudioFormat m_internalFormat;
303   AEAudioFormat m_inputFormat;
304   AudioSettings m_settings;
305   CEngineStats m_stats;
306   IAEEncoder *m_encoder;
307
308   // buffers
309   CActiveAEBufferPoolResample *m_sinkBuffers;
310   CActiveAEBufferPoolResample *m_vizBuffers;
311   CActiveAEBufferPool *m_vizBuffersInput;
312   CActiveAEBufferPool *m_silenceBuffers;  // needed to drive gui sounds if we have no streams
313   CActiveAEBufferPool *m_encoderBuffers;
314
315   // streams
316   std::list<CActiveAEStream*> m_streams;
317   std::list<CActiveAEBufferPool*> m_discardBufferPools;
318
319   // gui sounds
320   struct SoundState
321   {
322     CActiveAESound *sound;
323     int samples_played;
324   };
325   std::list<SoundState> m_sounds_playing;
326   std::vector<CActiveAESound*> m_sounds;
327   int m_soundMode;
328
329   float m_volume;
330   bool m_muted;
331   bool m_sinkHasVolume;
332
333   // viz
334   IAudioCallback *m_audioCallback;
335   bool m_vizInitialized;
336   CCriticalSection m_vizLock;
337
338   // ffmpeg
339   DllAvFormat m_dllAvFormat;
340   DllAvCodec  m_dllAvCodec;
341   DllAvUtil   m_dllAvUtil;
342
343   // polled via the interface
344   float m_aeVolume;
345   bool m_aeMuted;
346 };
347 };