OSDN Git Service

change libasound to be a shared library
[android-x86/hardware-alsa_sound.git] / AudioHardwareALSA.h
1 /* AudioHardwareALSA.h
2  **
3  ** Copyright 2008-2009, Wind River Systems
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  **     http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  */
17
18 #ifndef ANDROID_AUDIO_HARDWARE_ALSA_H
19 #define ANDROID_AUDIO_HARDWARE_ALSA_H
20
21 #include <hardware_legacy/AudioHardwareBase.h>
22
23 #include <alsa/asoundlib.h>
24
25 #include <hardware/hardware.h>
26
27 namespace android
28 {
29     class AudioHardwareALSA;
30
31     /**
32      * The id of acoustics module
33      */
34 #define ACOUSTICS_HARDWARE_MODULE_ID "acoustics"
35 #define ACOUSTICS_HARDWARE_NAME "name"
36
37     struct acoustic_device_t {
38         hw_device_t common;
39
40         /**
41          * Set the provided acoustics for a particular ALSA pcm device.
42          *
43          * Returns: 0 on succes, error code on failure.
44          */
45         status_t (*set_acoustics)(snd_pcm_t *, AudioSystem::audio_in_acoustics);
46
47         /**
48          * Read callback with PCM data so that filtering may be applied.
49          *
50          * Returns: frames filtered on success, error code on failure.
51          */
52         ssize_t (*filter)(snd_pcm_t *, void *, ssize_t);
53     };
54
55     // ----------------------------------------------------------------------------
56
57     class ALSAMixer
58     {
59         public:
60                                     ALSAMixer();
61             virtual                ~ALSAMixer();
62
63             bool                    isValid() { return !!mMixer[SND_PCM_STREAM_PLAYBACK]; }
64             status_t                setMasterVolume(float volume);
65             status_t                setMasterGain(float gain);
66
67             status_t                setVolume(uint32_t device, float volume);
68             status_t                setGain(uint32_t device, float gain);
69
70             status_t                setCaptureMuteState(uint32_t device, bool state);
71             status_t                getCaptureMuteState(uint32_t device, bool *state);
72             status_t                setPlaybackMuteState(uint32_t device, bool state);
73             status_t                getPlaybackMuteState(uint32_t device, bool *state);
74
75         private:
76             snd_mixer_t            *mMixer[SND_PCM_STREAM_LAST+1];
77     };
78
79     class ALSAControl
80     {
81         public:
82                                     ALSAControl(const char *device = "default");
83             virtual                ~ALSAControl();
84
85             status_t                get(const char *name, unsigned int &value, int index = 0);
86             status_t                set(const char *name, unsigned int value, int index = -1);
87
88         private:
89             snd_ctl_t              *mHandle;
90     };
91
92     class ALSAStreamOps
93     {
94         protected:
95             friend class AudioStreamOutALSA;
96             friend class AudioStreamInALSA;
97
98             struct StreamDefaults
99             {
100                 const char *        devicePrefix;
101                 snd_pcm_stream_t    direction;       // playback or capture
102                 snd_pcm_format_t    format;
103                 int                 channels;
104                 uint32_t            sampleRate;
105                 unsigned int        latency;         // Delay in usec
106                 unsigned int        bufferSize;      // Size of sample buffer
107             };
108
109                                     ALSAStreamOps(AudioHardwareALSA *parent);
110             virtual                ~ALSAStreamOps();
111
112             status_t                set(int format,
113                                         int channels,
114                                         uint32_t rate);
115             virtual uint32_t        sampleRate() const;
116             status_t                sampleRate(uint32_t rate);
117             virtual size_t          bufferSize() const;
118             virtual int             format() const;
119             virtual int             channelCount() const;
120             status_t                channelCount(int channels);
121
122             status_t                open(int mode, uint32_t device);
123             void                    close();
124             status_t                setSoftwareParams();
125             status_t                setPCMFormat(snd_pcm_format_t format);
126             status_t                setHardwareResample(bool resample);
127
128             status_t                setDevice(int mode, uint32_t device);
129
130             const char             *streamName();
131             const char             *deviceName(int mode, uint32_t device);
132
133             void                    setStreamDefaults(StreamDefaults *dev) {
134                 mDefaults = dev;
135             }
136
137         private:
138             AudioHardwareALSA      *mParent;
139             snd_pcm_t              *mHandle;
140             snd_pcm_hw_params_t    *mHardwareParams;
141             snd_pcm_sw_params_t    *mSoftwareParams;
142             int                     mMode;
143             uint32_t                mDevice;
144
145             StreamDefaults         *mDefaults;
146
147             Mutex                   mLock;
148             bool                    mPowerLock;
149 };
150
151     // ----------------------------------------------------------------------------
152
153     class AudioStreamOutALSA : public AudioStreamOut, public ALSAStreamOps
154     {
155         public:
156                                     AudioStreamOutALSA(AudioHardwareALSA *parent);
157             virtual                ~AudioStreamOutALSA();
158
159             status_t                set(int format          = 0,
160                                         int channelCount    = 0,
161                                         uint32_t sampleRate = 0) {
162                 return ALSAStreamOps::set(format, channelCount, sampleRate);
163             }
164
165             virtual uint32_t        sampleRate() const
166             {
167                 return ALSAStreamOps::sampleRate();
168             }
169
170             virtual size_t          bufferSize() const
171             {
172                 return ALSAStreamOps::bufferSize();
173             }
174
175             virtual int             channelCount() const;
176
177             virtual int             format() const
178             {
179                 return ALSAStreamOps::format();
180             }
181
182             virtual uint32_t        latency() const;
183
184             virtual ssize_t         write(const void *buffer, size_t bytes);
185             virtual status_t        dump(int fd, const Vector<String16>& args);
186
187             status_t                setVolume(float volume);
188
189             virtual status_t        standby();
190
191         protected:
192             friend class AudioHardwareALSA;
193
194             status_t                setDevice(int mode, uint32_t newDevice);
195     };
196
197     class AudioStreamInALSA : public AudioStreamIn, public ALSAStreamOps
198     {
199         public:
200                                     AudioStreamInALSA(AudioHardwareALSA *parent,
201                                                       AudioSystem::audio_in_acoustics acoustics);
202             virtual                ~AudioStreamInALSA();
203
204             status_t                set(int      format       = 0,
205                                         int      channelCount = 0,
206                                         uint32_t sampleRate   = 0) {
207                 return ALSAStreamOps::set(format, channelCount, sampleRate);
208             }
209
210             virtual uint32_t        sampleRate() {
211                 return ALSAStreamOps::sampleRate();
212             }
213
214             virtual size_t          bufferSize() const
215             {
216                 return ALSAStreamOps::bufferSize();
217             }
218
219             virtual int             channelCount() const
220             {
221                 return ALSAStreamOps::channelCount();
222             }
223
224             virtual int             format() const
225             {
226                 return ALSAStreamOps::format();
227             }
228
229             virtual ssize_t         read(void* buffer, ssize_t bytes);
230             virtual status_t        dump(int fd, const Vector<String16>& args);
231
232             virtual status_t        setGain(float gain);
233
234             virtual status_t        standby();
235
236         protected:
237             friend class AudioHardwareALSA;
238
239             status_t                setDevice(int mode, uint32_t newDevice);
240
241         private:
242             AudioSystem::audio_in_acoustics mAcoustics;
243     };
244
245     class AudioHardwareALSA : public AudioHardwareBase
246     {
247         public:
248                                     AudioHardwareALSA();
249             virtual                ~AudioHardwareALSA();
250
251             /**
252              * check to see if the audio hardware interface has been initialized.
253              * return status based on values defined in include/utils/Errors.h
254              */
255             virtual status_t        initCheck();
256
257             /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
258             virtual status_t        setVoiceVolume(float volume);
259
260             /**
261              * set the audio volume for all audio activities other than voice call.
262              * Range between 0.0 and 1.0. If any value other than NO_ERROR is returned,
263              * the software mixer will emulate this capability.
264              */
265             virtual status_t        setMasterVolume(float volume);
266
267             // mic mute
268             virtual status_t        setMicMute(bool state);
269             virtual status_t        getMicMute(bool* state);
270
271             /** This method creates and opens the audio hardware output stream */
272             virtual AudioStreamOut* openOutputStream(
273                 int format=0,
274                 int channelCount=0,
275                 uint32_t sampleRate=0,
276                 status_t *status=0);
277
278             /** This method creates and opens the audio hardware input stream */
279             virtual AudioStreamIn*  openInputStream(
280                 int inputSource,
281                 int format,
282                 int channelCount,
283                 uint32_t sampleRate,
284                 status_t *status,
285                 AudioSystem::audio_in_acoustics acoustics);
286
287         protected:
288             /**
289              * doRouting actually initiates the routing. A call to setRouting
290              * or setMode may result in a routing change. The generic logic calls
291              * doRouting when required. If the device has any special requirements these
292              * methods can be overriden.
293              */
294             virtual status_t    doRouting();
295
296             virtual status_t    dump(int fd, const Vector<String16>& args);
297
298             friend class AudioStreamOutALSA;
299             friend class AudioStreamInALSA;
300
301             ALSAMixer          *mMixer;
302             AudioStreamOutALSA *mOutput;
303             AudioStreamInALSA  *mInput;
304
305             acoustic_device_t *mAcousticDevice;
306
307         private:
308             Mutex                   mLock;
309     };
310
311     // ----------------------------------------------------------------------------
312
313 };        // namespace android
314 #endif    // ANDROID_AUDIO_HARDWARE_ALSA_H