strip added smb:// shares of their user/pass when adding, and instead store that...
[vuplus_xbmc] / xbmc / cores / AudioEngine / Engines / SoftAE / SoftAE.h
1 #pragma once
2 /*
3  *      Copyright (C) 2010-2012 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, write to
18  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  *  http://www.gnu.org/copyleft/gpl.html
20  *
21  */
22
23 #include <list>
24 #include <vector>
25 #include <map>
26
27 #include "system.h"
28 #include "threads/Thread.h"
29 #include "threads/CriticalSection.h"
30 #include "threads/SharedSection.h"
31
32 #include "Interfaces/ThreadedAE.h"
33 #include "Interfaces/AESink.h"
34 #include "Interfaces/AEEncoder.h"
35 #include "Utils/AEConvert.h"
36 #include "Utils/AERemap.h"
37 #include "Utils/AEBuffer.h"
38 #include "AEAudioFormat.h"
39 #include "AESinkFactory.h"
40
41 #include "SoftAEStream.h"
42 #include "SoftAESound.h"
43
44 #include "cores/IAudioCallback.h"
45
46 /* forward declarations */
47 class IThreadedAE;
48 class CSoftAEStream;
49 class CSoftAESound;
50 class IAESink;
51
52 class CSoftAE : public IThreadedAE
53 {
54 protected:
55   friend class CAEFactory;
56   CSoftAE();
57   virtual ~CSoftAE();
58
59 public:
60   virtual void  Shutdown();
61   virtual bool  Initialize();
62   virtual void  OnSettingsChange(std::string setting);
63
64   virtual void   Run();
65   virtual void   Stop();
66   virtual double GetDelay();
67
68   virtual float GetVolume();
69   virtual void  SetVolume(const float volume);
70   virtual void  SetMute(const bool enabled) { m_muted = enabled; }
71   virtual bool  IsMuted() { return m_muted; }
72   virtual void  SetSoundMode(const int mode);
73
74   /* returns a new stream for data in the specified format */
75   virtual IAEStream *MakeStream(enum AEDataFormat dataFormat, unsigned int sampleRate, unsigned int encodedSampleRate, CAEChannelInfo channelLayout, unsigned int options = 0);
76   virtual IAEStream *FreeStream(IAEStream *stream);
77
78   /* returns a new sound object */
79   virtual IAESound *MakeSound(const std::string& file);
80   virtual void      FreeSound(IAESound *sound);
81   void PlaySound(IAESound *sound);
82   void StopSound(IAESound *sound);
83
84   /* free's sounds that have expired */
85   virtual void GarbageCollect();
86
87   /* these are for the streams so they can provide compatible data */
88   unsigned int          GetSampleRate   ();
89   unsigned int          GetChannelCount () {return m_chLayout.Count()      ;}
90   CAEChannelInfo&       GetChannelLayout() {return m_chLayout              ;}
91   enum AEStdChLayout    GetStdChLayout  () {return m_stdChLayout           ;}
92   unsigned int          GetFrames       () {return m_sinkFormat.m_frames   ;}
93   unsigned int          GetFrameSize    () {return m_frameSize             ;}
94
95   /* these are for streams that are in RAW mode */
96   const AEAudioFormat*  GetSinkAudioFormat() {return &m_sinkFormat               ;}
97   enum AEDataFormat     GetSinkDataFormat () {return m_sinkFormat.m_dataFormat   ;}
98   CAEChannelInfo&       GetSinkChLayout   () {return m_sinkFormat.m_channelLayout;}
99   unsigned int          GetSinkFrameSize  () {return m_sinkFormat.m_frameSize    ;}
100
101   /* for streams so they can calc cachetimes correct */
102   double GetCacheTime();
103   double GetCacheTotal();
104
105   virtual void EnumerateOutputDevices(AEDeviceList &devices, bool passthrough);
106   virtual std::string GetDefaultDevice(bool passthrough);
107   virtual bool SupportsRaw();
108
109   /* internal stream methods */
110   void PauseStream (CSoftAEStream *stream);
111   void ResumeStream(CSoftAEStream *stream);
112
113 private:
114   CThread *m_thread;
115
116   CSoftAEStream *GetMasterStream();
117
118   void LoadSettings();
119   void VerifySoundDevice(std::string &device, bool passthrough);
120   void OpenSink();
121
122   void InternalOpenSink();
123   void ResetEncoder();
124   bool SetupEncoder(AEAudioFormat &format);
125   void Deinitialize();
126
127   IAESink *GetSink(AEAudioFormat &desiredFormat, bool passthrough, std::string &device);
128   void StopAllSounds();
129
130   enum AEStdChLayout m_stdChLayout;
131   std::string m_device;
132   std::string m_passthroughDevice;
133   bool m_audiophile;
134   bool m_stereoUpmix;
135
136   /* internal vars */
137   bool             m_running, m_reOpen;
138   CEvent           m_reOpenEvent;
139
140   CCriticalSection m_runningLock;     /* released when the thread exits */
141   CCriticalSection m_streamLock;      /* m_streams lock */
142   CCriticalSection m_soundLock;       /* m_sounds lock */
143   CCriticalSection m_soundSampleLock; /* m_playing_sounds lock */
144   CSharedSection   m_sinkLock;        /* lock for m_sink on re-open */
145
146   /* the current configuration */
147   float               m_volume;
148   bool                m_muted;
149   CAEChannelInfo      m_chLayout;
150   unsigned int        m_frameSize;
151
152   /* the sink, its format information, and conversion function */
153   AESinkInfoList            m_sinkInfoList;
154   IAESink                  *m_sink;
155   AEAudioFormat             m_sinkFormat;
156   float                     m_sinkFormatSampleRateMul;
157   float                     m_sinkFormatFrameSizeMul;
158   unsigned int              m_sinkBlockSize;
159   AEAudioFormat             m_encoderFormat;
160   float                     m_encoderFrameSizeMul;
161   unsigned int              m_bytesPerSample;
162   CAEConvert::AEConvertFrFn m_convertFn;
163
164   /* currently playing sounds */
165   typedef struct {
166     CSoftAESound *owner;
167     float        *samples;
168     unsigned int  sampleCount;
169   } SoundState;
170
171   typedef std::vector<CSoftAEStream*> StreamList;
172   typedef std::list  <CSoftAESound* > SoundList;
173   typedef std::list  <SoundState    > SoundStateList;  
174     
175   /* the streams, sounds, output buffer and output buffer fill size */
176   bool           m_transcode;
177   bool           m_rawPassthrough;
178   StreamList     m_newStreams, m_streams, m_playingStreams;
179   SoundList      m_sounds;
180   SoundStateList m_playing_sounds;
181   int            m_soundMode;
182   bool           m_streamsPlaying;
183
184   /* this will contain either float, or uint8_t depending on if we are in raw mode or not */
185   CAEBuffer      m_buffer;
186
187   /* the encoder */
188   IAEEncoder    *m_encoder;
189   CAEBuffer      m_encodedBuffer;
190
191   /* the output conversion buffer  */
192   uint8_t        *m_converted;
193   size_t          m_convertedSize;
194
195   /* thread run stages */
196   void         MixSounds        (float *buffer, unsigned int samples);
197   void         FinalizeSamples  (float *buffer, unsigned int samples);
198
199   CSoftAEStream *m_masterStream;
200
201   void         (CSoftAE::*m_outputStageFn)();
202   void         RunOutputStage   ();
203   void         RunRawOutputStage();
204   void         RunTranscodeStage();
205
206   unsigned int (CSoftAE::*m_streamStageFn)(unsigned int channelCount, void *out, bool &restart);
207   unsigned int RunRawStreamStage (unsigned int channelCount, void *out, bool &restart);
208   unsigned int RunStreamStage    (unsigned int channelCount, void *out, bool &restart);
209
210   void         ResumeSlaveStreams(const StreamList &streams);
211   void         RunNormalizeStage (unsigned int channelCount, void *out, unsigned int mixed);
212
213   void         RemoveStream(StreamList &streams, CSoftAEStream *stream);
214 };
215