3 ** Copyright 2008, 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
22 #include <sys/types.h>
23 #include <alsa/asoundlib.h>
25 #include <hardware_legacy/AudioHardwareBase.h>
30 class AudioHardwareALSA;
32 // ----------------------------------------------------------------------------
40 bool isValid() { return !!mMixer[SND_PCM_STREAM_PLAYBACK]; }
41 status_t setMasterVolume(float volume);
42 status_t setMasterGain(float gain);
44 status_t setVolume(uint32_t device, float volume);
45 status_t setGain(uint32_t device, float gain);
47 status_t setCaptureMuteState(uint32_t device, bool state);
48 status_t getCaptureMuteState(uint32_t device, bool *state);
49 status_t setPlaybackMuteState(uint32_t device, bool state);
50 status_t getPlaybackMuteState(uint32_t device, bool *state);
53 snd_mixer_t *mMixer[SND_PCM_STREAM_LAST+1];
59 ALSAControl(const char *device = "default");
60 virtual ~ALSAControl();
62 status_t get(const char *name, unsigned int &value, int index = 0);
63 status_t set(const char *name, unsigned int value, int index = -1);
74 const char * devicePrefix;
75 snd_pcm_stream_t direction; // playback or capture
76 snd_pcm_format_t format;
79 unsigned int latency; // Delay in usec
80 unsigned int bufferSize; // Size of sample buffer
84 virtual ~ALSAStreamOps();
86 status_t set(int format,
89 virtual uint32_t sampleRate() const;
90 status_t sampleRate(uint32_t rate);
91 virtual size_t bufferSize() const;
92 virtual int format() const;
93 virtual int channelCount() const;
94 status_t channelCount(int channels);
95 const char *streamName();
96 virtual status_t setDevice(int mode, uint32_t device);
98 const char *deviceName(int mode, uint32_t device);
101 friend class AudioStreamOutALSA;
102 friend class AudioStreamInALSA;
104 status_t open(int mode, uint32_t device);
106 status_t setSoftwareParams();
107 status_t setPCMFormat(snd_pcm_format_t format);
108 status_t setHardwareResample(bool resample);
110 void setStreamDefaults(StreamDefaults *dev) {
118 snd_pcm_hw_params_t *mHardwareParams;
119 snd_pcm_sw_params_t *mSoftwareParams;
123 StreamDefaults *mDefaults;
126 // ----------------------------------------------------------------------------
128 class AudioStreamOutALSA : public AudioStreamOut, public ALSAStreamOps
131 AudioStreamOutALSA(AudioHardwareALSA *parent);
132 virtual ~AudioStreamOutALSA();
134 status_t set(int format = 0,
135 int channelCount = 0,
136 uint32_t sampleRate = 0) {
137 return ALSAStreamOps::set(format, channelCount, sampleRate);
140 virtual uint32_t sampleRate() const
142 return ALSAStreamOps::sampleRate();
145 virtual size_t bufferSize() const
147 return ALSAStreamOps::bufferSize();
150 virtual int channelCount() const;
152 virtual int format() const
154 return ALSAStreamOps::format();
157 virtual uint32_t latency() const;
159 virtual ssize_t write(const void *buffer, size_t bytes);
160 virtual status_t dump(int fd, const Vector<String16>& args);
161 virtual status_t setDevice(int mode, uint32_t newDevice);
163 status_t setVolume(float volume);
169 AudioHardwareALSA *mParent;
173 class AudioStreamInALSA : public AudioStreamIn, public ALSAStreamOps
176 AudioStreamInALSA(AudioHardwareALSA *parent);
177 virtual ~AudioStreamInALSA();
179 status_t set(int format = 0,
180 int channelCount = 0,
181 uint32_t sampleRate = 0) {
182 return ALSAStreamOps::set(format, channelCount, sampleRate);
185 virtual uint32_t sampleRate() {
186 return ALSAStreamOps::sampleRate();
189 virtual size_t bufferSize() const
191 return ALSAStreamOps::bufferSize();
194 virtual int channelCount() const
196 return ALSAStreamOps::channelCount();
199 virtual int format() const
201 return ALSAStreamOps::format();
204 virtual ssize_t read(void* buffer, ssize_t bytes);
205 virtual status_t dump(int fd, const Vector<String16>& args);
206 virtual status_t setDevice(int mode, uint32_t newDevice);
208 virtual status_t setGain(float gain);
210 virtual status_t standby();
213 AudioHardwareALSA *mParent;
216 class AudioHardwareALSA : public AudioHardwareBase
220 virtual ~AudioHardwareALSA();
223 * check to see if the audio hardware interface has been initialized.
224 * return status based on values defined in include/utils/Errors.h
226 virtual status_t initCheck();
229 * put the audio hardware into standby mode to conserve power. Returns
230 * status based on include/utils/Errors.h
232 virtual status_t standby();
234 /** set the audio volume of a voice call. Range is between 0.0 and 1.0 */
235 virtual status_t setVoiceVolume(float volume);
238 * set the audio volume for all audio activities other than voice call.
239 * Range between 0.0 and 1.0. If any value other than NO_ERROR is returned,
240 * the software mixer will emulate this capability.
242 virtual status_t setMasterVolume(float volume);
245 virtual status_t setMicMute(bool state);
246 virtual status_t getMicMute(bool* state);
248 /** This method creates and opens the audio hardware output stream */
249 virtual AudioStreamOut* openOutputStream(
252 uint32_t sampleRate=0,
255 /** This method creates and opens the audio hardware input stream */
256 virtual AudioStreamIn* openInputStream(
261 AudioSystem::audio_in_acoustics acoustics);
265 * doRouting actually initiates the routing. A call to setRouting
266 * or setMode may result in a routing change. The generic logic calls
267 * doRouting when required. If the device has any special requirements these
268 * methods can be overriden.
270 virtual status_t doRouting();
272 virtual status_t dump(int fd, const Vector<String16>& args);
274 friend class AudioStreamOutALSA;
275 friend class AudioStreamInALSA;
278 AudioStreamOutALSA *mOutput;
279 AudioStreamInALSA *mInput;
285 // ----------------------------------------------------------------------------
287 }; // namespace android
288 #endif // ANDROID_AUDIO_HARDWARE_ALSA_H