3 ** Copyright 2008-2010, Wind River Systems
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef ANDROID_AUDIO_HARDWARE_ALSA_H
19 #define ANDROID_AUDIO_HARDWARE_ALSA_H
21 #include <utils/List.h>
22 #include <hardware_legacy/AudioHardwareBase.h>
24 #include <alsa/asoundlib.h>
26 #include <hardware/hardware.h>
31 class AudioHardwareALSA;
34 * The id of ALSA module
36 #define ALSA_HARDWARE_MODULE_ID "alsa"
37 #define ALSA_HARDWARE_NAME "alsa"
41 struct alsa_handle_t {
42 alsa_device_t * module;
47 snd_pcm_format_t format;
50 unsigned int latency; // Delay in usec
51 unsigned int bufferSize; // Size of sample buffer
55 typedef List<alsa_handle_t> ALSAHandleList;
57 struct alsa_device_t {
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);
67 * The id of acoustics module
69 #define ACOUSTICS_HARDWARE_MODULE_ID "acoustics"
70 #define ACOUSTICS_HARDWARE_NAME "acoustics"
72 struct acoustic_device_t {
75 // Required methods...
76 status_t (*use_handle)(acoustic_device_t *, alsa_handle_t *);
77 status_t (*cleanup)(acoustic_device_t *);
79 status_t (*set_params)(acoustic_device_t *, AudioSystem::audio_in_acoustics, void *);
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);
89 // ----------------------------------------------------------------------------
97 bool isValid() { return !!mMixer[SND_PCM_STREAM_PLAYBACK]; }
98 status_t setMasterVolume(float volume);
99 status_t setMasterGain(float gain);
101 status_t setVolume(uint32_t device, float left, float right);
102 status_t setGain(uint32_t device, float gain);
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);
110 snd_mixer_t * mMixer[SND_PCM_STREAM_LAST+1];
116 ALSAControl(const char *device = "hw:00");
117 virtual ~ALSAControl();
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);
122 status_t set(const char *name, const char *);
131 ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle);
132 virtual ~ALSAStreamOps();
134 status_t set(int *format, uint32_t *channels, uint32_t *rate);
136 status_t setParameters(const String8& keyValuePairs);
137 String8 getParameters(const String8& keys);
139 uint32_t sampleRate() const;
140 size_t bufferSize() const;
142 uint32_t channels() const;
144 status_t open(int mode);
148 friend class AudioHardwareALSA;
150 acoustic_device_t *acoustics();
153 AudioHardwareALSA * mParent;
154 alsa_handle_t * mHandle;
160 // ----------------------------------------------------------------------------
162 class AudioStreamOutALSA : public AudioStreamOut, public ALSAStreamOps
165 AudioStreamOutALSA(AudioHardwareALSA *parent, alsa_handle_t *handle);
166 virtual ~AudioStreamOutALSA();
168 virtual uint32_t sampleRate() const
170 return ALSAStreamOps::sampleRate();
173 virtual size_t bufferSize() const
175 return ALSAStreamOps::bufferSize();
178 virtual uint32_t channels() const;
180 virtual int format() const
182 return ALSAStreamOps::format();
185 virtual uint32_t latency() const;
187 virtual ssize_t write(const void *buffer, size_t bytes);
188 virtual status_t dump(int fd, const Vector<String16>& args);
190 status_t setVolume(float left, float right);
192 virtual status_t standby();
194 virtual status_t setParameters(const String8& keyValuePairs) {
195 return ALSAStreamOps::setParameters(keyValuePairs);
198 virtual String8 getParameters(const String8& keys) {
199 return ALSAStreamOps::getParameters(keys);
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);
206 status_t open(int mode);
210 uint32_t mFrameCount;
213 class AudioStreamInALSA : public AudioStreamIn, public ALSAStreamOps
216 AudioStreamInALSA(AudioHardwareALSA *parent,
217 alsa_handle_t *handle,
218 AudioSystem::audio_in_acoustics audio_acoustics);
219 virtual ~AudioStreamInALSA();
221 virtual uint32_t sampleRate() const
223 return ALSAStreamOps::sampleRate();
226 virtual size_t bufferSize() const
228 return ALSAStreamOps::bufferSize();
231 virtual uint32_t channels() const
233 return ALSAStreamOps::channels();
236 virtual int format() const
238 return ALSAStreamOps::format();
241 virtual ssize_t read(void* buffer, ssize_t bytes);
242 virtual status_t dump(int fd, const Vector<String16>& args);
244 virtual status_t setGain(float gain);
246 virtual status_t standby();
248 virtual status_t setParameters(const String8& keyValuePairs)
250 return ALSAStreamOps::setParameters(keyValuePairs);
253 virtual String8 getParameters(const String8& keys)
255 return ALSAStreamOps::getParameters(keys);
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;
264 status_t setAcousticParams(void* params);
266 status_t open(int mode);
270 void resetFramesLost();
272 unsigned int mFramesLost;
273 AudioSystem::audio_in_acoustics mAcoustics;
276 class AudioHardwareALSA : public AudioHardwareBase
280 virtual ~AudioHardwareALSA();
283 * check to see if the audio hardware interface has been initialized.
284 * return status based on values defined in include/utils/Errors.h
286 virtual status_t initCheck();
288 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
289 virtual status_t setVoiceVolume(float volume);
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.
296 virtual status_t setMasterVolume(float volume);
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.
303 virtual status_t setMode(int mode);
306 virtual status_t setMicMute(bool state);
307 virtual status_t getMicMute(bool* state);
309 // set/get global audio parameters
310 //virtual status_t setParameters(const String8& keyValuePairs);
311 //virtual String8 getParameters(const String8& keys);
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);
317 /** This method creates and opens the audio hardware output stream */
318 virtual AudioStreamOut* openOutputStream(
321 uint32_t *channels=0,
322 uint32_t *sampleRate=0,
324 virtual void closeOutputStream(AudioStreamOut* out);
326 /** This method creates and opens the audio hardware input stream */
327 virtual AudioStreamIn* openInputStream(
331 uint32_t *sampleRate,
333 AudioSystem::audio_in_acoustics acoustics);
334 virtual void closeInputStream(AudioStreamIn* in);
336 /**This method dumps the state of the audio hardware */
337 //virtual status_t dumpState(int fd, const Vector<String16>& args);
339 static AudioHardwareInterface* create();
347 virtual status_t dump(int fd, const Vector<String16>& args);
349 friend class AudioStreamOutALSA;
350 friend class AudioStreamInALSA;
351 friend class ALSAStreamOps;
355 alsa_device_t * mALSADevice;
356 acoustic_device_t * mAcousticDevice;
358 ALSAHandleList mDeviceList;
361 // ----------------------------------------------------------------------------
363 }; // namespace android
364 #endif // ANDROID_AUDIO_HARDWARE_ALSA_H