OSDN Git Service

29bc583c533bb6689b53a83bd3e011d52b73c404
[android-x86/hardware-alsa_sound.git] / AudioHardwareALSA.h
1 /* AudioHardwareALSA.h
2  **
3  ** Copyright 2008-2010, 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 <utils/List.h>
22 #include <hardware_legacy/AudioHardwareBase.h>
23
24 #include <alsa/asoundlib.h>
25
26 #include <hardware/hardware.h>
27
28 namespace android
29 {
30
31 class AudioHardwareALSA;
32
33 /**
34  * The id of ALSA module
35  */
36 #define ALSA_HARDWARE_MODULE_ID "alsa"
37 #define ALSA_HARDWARE_NAME      "alsa"
38
39 struct alsa_device_t;
40
41 struct alsa_handle_t {
42     alsa_device_t *     module;
43     uint32_t            devices;
44     uint32_t            curDev;
45     int                 curMode;
46     snd_pcm_t *         handle;
47     snd_pcm_format_t    format;
48     uint32_t            channels;
49     uint32_t            sampleRate;
50     unsigned int        latency;         // Delay in usec
51     unsigned int        bufferSize;      // Size of sample buffer
52     void *              modPrivate;
53 };
54
55 typedef List<alsa_handle_t> ALSAHandleList;
56
57 struct alsa_device_t {
58     hw_device_t common;
59
60     status_t (*init)(alsa_device_t *, ALSAHandleList &);
61     status_t (*open)(alsa_handle_t *, uint32_t, int);
62     status_t (*close)(alsa_handle_t *);
63     status_t (*route)(alsa_handle_t *, uint32_t, int);
64 };
65
66 /**
67  * The id of acoustics module
68  */
69 #define ACOUSTICS_HARDWARE_MODULE_ID    "acoustics"
70 #define ACOUSTICS_HARDWARE_NAME         "acoustics"
71
72 struct acoustic_device_t {
73     hw_device_t common;
74
75     // Required methods...
76     status_t (*use_handle)(acoustic_device_t *, alsa_handle_t *);
77     status_t (*cleanup)(acoustic_device_t *);
78
79     status_t (*set_params)(acoustic_device_t *, AudioSystem::audio_in_acoustics, void *);
80
81     // Optional methods...
82     ssize_t (*read)(acoustic_device_t *, void *, size_t);
83     ssize_t (*write)(acoustic_device_t *, const void *, size_t);
84     status_t (*recover)(acoustic_device_t *, int);
85
86     void *              modPrivate;
87 };
88
89 // ----------------------------------------------------------------------------
90
91 class ALSAMixer
92 {
93 public:
94     ALSAMixer();
95     virtual                ~ALSAMixer();
96
97     bool                    isValid() { return !!mMixer[SND_PCM_STREAM_PLAYBACK]; }
98     status_t                setMasterVolume(float volume);
99     status_t                setMasterGain(float gain);
100
101     status_t                setVolume(uint32_t device, float left, float right);
102     status_t                setGain(uint32_t device, float gain);
103
104     status_t                setCaptureMuteState(uint32_t device, bool state);
105     status_t                getCaptureMuteState(uint32_t device, bool *state);
106     status_t                setPlaybackMuteState(uint32_t device, bool state);
107     status_t                getPlaybackMuteState(uint32_t device, bool *state);
108
109 private:
110     snd_mixer_t *           mMixer[SND_PCM_STREAM_LAST+1];
111 };
112
113 class ALSAControl
114 {
115 public:
116     ALSAControl(const char *device = "hw:00");
117     virtual                ~ALSAControl();
118
119     status_t                get(const char *name, unsigned int &value, int index = 0);
120     status_t                set(const char *name, unsigned int value, int index = -1);
121
122     status_t                set(const char *name, const char *);
123
124 private:
125     snd_ctl_t *             mHandle;
126 };
127
128 class ALSAStreamOps
129 {
130 public:
131     ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle);
132     virtual            ~ALSAStreamOps();
133
134     status_t            set(int *format, uint32_t *channels, uint32_t *rate);
135
136     status_t            setParameters(const String8& keyValuePairs);
137     String8             getParameters(const String8& keys);
138
139     uint32_t            sampleRate() const;
140     size_t              bufferSize() const;
141     int                 format() const;
142     uint32_t            channels() const;
143
144     status_t            open(int mode);
145     void                close();
146
147 protected:
148     friend class AudioHardwareALSA;
149
150     acoustic_device_t *acoustics();
151     ALSAMixer *mixer();
152
153     AudioHardwareALSA *     mParent;
154     alsa_handle_t *         mHandle;
155
156     Mutex                   mLock;
157     bool                    mPowerLock;
158 };
159
160 // ----------------------------------------------------------------------------
161
162 class AudioStreamOutALSA : public AudioStreamOut, public ALSAStreamOps
163 {
164 public:
165     AudioStreamOutALSA(AudioHardwareALSA *parent, alsa_handle_t *handle);
166     virtual            ~AudioStreamOutALSA();
167
168     virtual uint32_t    sampleRate() const
169     {
170         return ALSAStreamOps::sampleRate();
171     }
172
173     virtual size_t      bufferSize() const
174     {
175         return ALSAStreamOps::bufferSize();
176     }
177
178     virtual uint32_t    channels() const;
179
180     virtual int         format() const
181     {
182         return ALSAStreamOps::format();
183     }
184
185     virtual uint32_t    latency() const;
186
187     virtual ssize_t     write(const void *buffer, size_t bytes);
188     virtual status_t    dump(int fd, const Vector<String16>& args);
189
190     status_t            setVolume(float left, float right);
191
192     virtual status_t    standby();
193
194     virtual status_t    setParameters(const String8& keyValuePairs) {
195         return ALSAStreamOps::setParameters(keyValuePairs);
196     }
197
198     virtual String8     getParameters(const String8& keys) {
199         return ALSAStreamOps::getParameters(keys);
200     }
201
202     // return the number of audio frames written by the audio dsp to DAC since
203     // the output has exited standby
204     virtual status_t    getRenderPosition(uint32_t *dspFrames);
205
206     status_t            open(int mode);
207     status_t            close();
208
209 private:
210     uint32_t            mFrameCount;
211 };
212
213 class AudioStreamInALSA : public AudioStreamIn, public ALSAStreamOps
214 {
215 public:
216     AudioStreamInALSA(AudioHardwareALSA *parent,
217             alsa_handle_t *handle,
218             AudioSystem::audio_in_acoustics audio_acoustics);
219     virtual            ~AudioStreamInALSA();
220
221     virtual uint32_t    sampleRate() const
222     {
223         return ALSAStreamOps::sampleRate();
224     }
225
226     virtual size_t      bufferSize() const
227     {
228         return ALSAStreamOps::bufferSize();
229     }
230
231     virtual uint32_t    channels() const
232     {
233         return ALSAStreamOps::channels();
234     }
235
236     virtual int         format() const
237     {
238         return ALSAStreamOps::format();
239     }
240
241     virtual ssize_t     read(void* buffer, ssize_t bytes);
242     virtual status_t    dump(int fd, const Vector<String16>& args);
243
244     virtual status_t    setGain(float gain);
245
246     virtual status_t    standby();
247
248     virtual status_t    setParameters(const String8& keyValuePairs)
249     {
250         return ALSAStreamOps::setParameters(keyValuePairs);
251     }
252
253     virtual String8     getParameters(const String8& keys)
254     {
255         return ALSAStreamOps::getParameters(keys);
256     }
257
258     // Return the amount of input frames lost in the audio driver since the last call of this function.
259     // Audio driver is expected to reset the value to 0 and restart counting upon returning the current value by this function call.
260     // Such loss typically occurs when the user space process is blocked longer than the capacity of audio driver buffers.
261     // Unit: the number of input audio frames
262     virtual unsigned int  getInputFramesLost() const;
263
264     status_t            setAcousticParams(void* params);
265
266     status_t            open(int mode);
267     status_t            close();
268
269 private:
270     void                resetFramesLost();
271
272     unsigned int        mFramesLost;
273     AudioSystem::audio_in_acoustics mAcoustics;
274 };
275
276 class AudioHardwareALSA : public AudioHardwareBase
277 {
278 public:
279     AudioHardwareALSA();
280     virtual            ~AudioHardwareALSA();
281
282     /**
283      * check to see if the audio hardware interface has been initialized.
284      * return status based on values defined in include/utils/Errors.h
285      */
286     virtual status_t    initCheck();
287
288     /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
289     virtual status_t    setVoiceVolume(float volume);
290
291     /**
292      * set the audio volume for all audio activities other than voice call.
293      * Range between 0.0 and 1.0. If any value other than NO_ERROR is returned,
294      * the software mixer will emulate this capability.
295      */
296     virtual status_t    setMasterVolume(float volume);
297
298     /**
299      * setMode is called when the audio mode changes. NORMAL mode is for
300      * standard audio playback, RINGTONE when a ringtone is playing, and IN_CALL
301      * when a call is in progress.
302      */
303     virtual status_t    setMode(int mode);
304
305     // mic mute
306     virtual status_t    setMicMute(bool state);
307     virtual status_t    getMicMute(bool* state);
308
309     // set/get global audio parameters
310     //virtual status_t    setParameters(const String8& keyValuePairs);
311     //virtual String8     getParameters(const String8& keys);
312
313     // Returns audio input buffer size according to parameters passed or 0 if one of the
314     // parameters is not supported
315     //virtual size_t    getInputBufferSize(uint32_t sampleRate, int format, int channels);
316
317     /** This method creates and opens the audio hardware output stream */
318     virtual AudioStreamOut* openOutputStream(
319             uint32_t devices,
320             int *format=0,
321             uint32_t *channels=0,
322             uint32_t *sampleRate=0,
323             status_t *status=0);
324     virtual    void        closeOutputStream(AudioStreamOut* out);
325
326     /** This method creates and opens the audio hardware input stream */
327     virtual AudioStreamIn* openInputStream(
328             uint32_t devices,
329             int *format,
330             uint32_t *channels,
331             uint32_t *sampleRate,
332             status_t *status,
333             AudioSystem::audio_in_acoustics acoustics);
334     virtual    void        closeInputStream(AudioStreamIn* in);
335
336     /**This method dumps the state of the audio hardware */
337     //virtual status_t dumpState(int fd, const Vector<String16>& args);
338
339     static AudioHardwareInterface* create();
340
341     int                 mode()
342     {
343         return mMode;
344     }
345
346 protected:
347     virtual status_t    dump(int fd, const Vector<String16>& args);
348
349     friend class AudioStreamOutALSA;
350     friend class AudioStreamInALSA;
351     friend class ALSAStreamOps;
352
353     ALSAMixer *         mMixer;
354
355     alsa_device_t *     mALSADevice;
356     acoustic_device_t * mAcousticDevice;
357
358     ALSAHandleList      mDeviceList;
359 };
360
361 // ----------------------------------------------------------------------------
362
363 };        // namespace android
364 #endif    // ANDROID_AUDIO_HARDWARE_ALSA_H