1 /* AudioHardwareALSA.cpp
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.
26 #define LOG_TAG "AudioHardwareALSA"
27 #include <utils/Log.h>
28 #include <utils/String8.h>
30 #include <cutils/properties.h>
31 #include <media/AudioRecord.h>
32 #include <hardware_legacy/power.h>
34 #include "AudioHardwareALSA.h"
39 // Function for dlsym() to look up for creating a new AudioHardwareInterface.
41 android::AudioHardwareInterface *createAudioHardware(void) {
42 return android::AudioHardwareALSA::create();
49 // ----------------------------------------------------------------------------
51 static void ALSAErrorHandler(const char *file,
63 l = snprintf(buf, BUFSIZ, "%s:%i:(%s) ", file, line, function);
64 vsnprintf(buf + l, BUFSIZ - l, fmt, arg);
66 LOG(LOG_ERROR, "ALSALib", buf);
70 AudioHardwareInterface *AudioHardwareALSA::create() {
71 return new AudioHardwareALSA();
74 AudioHardwareALSA::AudioHardwareALSA() :
78 snd_lib_error_set_handler(&ALSAErrorHandler);
79 mMixer = new ALSAMixer;
82 int err = hw_get_module(ALSA_HARDWARE_MODULE_ID,
83 (hw_module_t const**)&module);
87 err = module->methods->open(module, ALSA_HARDWARE_NAME, &device);
89 mALSADevice = (alsa_device_t *)device;
90 mALSADevice->init(mALSADevice, mDeviceList);
92 LOGE("ALSA Module could not be opened!!!");
94 LOGE("ALSA Module not found!!!");
96 err = hw_get_module(ACOUSTICS_HARDWARE_MODULE_ID,
97 (hw_module_t const**)&module);
101 err = module->methods->open(module, ACOUSTICS_HARDWARE_NAME, &device);
103 mAcousticDevice = (acoustic_device_t *)device;
105 LOGE("Acoustics Module not found.");
109 AudioHardwareALSA::~AudioHardwareALSA()
111 if (mMixer) delete mMixer;
113 mALSADevice->common.close(&mALSADevice->common);
115 mAcousticDevice->common.close(&mAcousticDevice->common);
118 status_t AudioHardwareALSA::initCheck()
123 if (!mMixer || !mMixer->isValid())
124 LOGW("ALSA Mixer is not valid. AudioFlinger will do software volume control.");
129 status_t AudioHardwareALSA::setVoiceVolume(float volume)
131 // The voice volume is used by the VOICE_CALL audio stream.
133 return mMixer->setVolume(AudioSystem::DEVICE_OUT_EARPIECE, volume, volume);
135 return INVALID_OPERATION;
138 status_t AudioHardwareALSA::setMasterVolume(float volume)
141 return mMixer->setMasterVolume(volume);
143 return INVALID_OPERATION;
146 status_t AudioHardwareALSA::setMode(int mode)
148 status_t status = NO_ERROR;
151 status = AudioHardwareBase::setMode(mode);
153 if (status == NO_ERROR) {
154 // take care of mode change.
155 for(ALSAHandleList::iterator it = mDeviceList.begin();
156 it != mDeviceList.end(); ++it) {
157 status = mALSADevice->route(&(*it), it->curDev, mode);
158 if (status != NO_ERROR)
168 AudioHardwareALSA::openOutputStream(uint32_t devices,
171 uint32_t *sampleRate,
174 AutoMutex lock(mLock);
176 LOGD("openOutputStream called for devices: 0x%08x", devices);
178 status_t err = BAD_VALUE;
179 AudioStreamOutALSA *out = 0;
181 if (devices & (devices - 1)) {
182 if (status) *status = err;
183 LOGD("openOutputStream called with bad devices");
187 // Find the appropriate alsa device
188 for(ALSAHandleList::iterator it = mDeviceList.begin();
189 it != mDeviceList.end(); ++it)
190 if (it->devices & devices) {
191 err = mALSADevice->open(&(*it), devices, mode());
193 out = new AudioStreamOutALSA(this, &(*it));
194 err = out->set(format, channels, sampleRate);
198 if (status) *status = err;
203 AudioHardwareALSA::closeOutputStream(AudioStreamOut* out)
205 AutoMutex lock(mLock);
210 AudioHardwareALSA::openInputStream(uint32_t devices,
213 uint32_t *sampleRate,
215 AudioSystem::audio_in_acoustics acoustics)
217 AutoMutex lock(mLock);
219 status_t err = BAD_VALUE;
220 AudioStreamInALSA *in = 0;
222 if (devices & (devices - 1)) {
223 if (status) *status = err;
227 // Find the appropriate alsa device
228 for(ALSAHandleList::iterator it = mDeviceList.begin();
229 it != mDeviceList.end(); ++it)
230 if (it->devices & devices) {
231 err = mALSADevice->open(&(*it), devices, mode());
233 in = new AudioStreamInALSA(this, &(*it), acoustics);
234 err = in->set(format, channels, sampleRate);
238 if (status) *status = err;
243 AudioHardwareALSA::closeInputStream(AudioStreamIn* in)
245 AutoMutex lock(mLock);
249 status_t AudioHardwareALSA::setMicMute(bool state)
252 return mMixer->setCaptureMuteState(AudioSystem::DEVICE_OUT_EARPIECE, state);
257 status_t AudioHardwareALSA::getMicMute(bool *state)
260 return mMixer->getCaptureMuteState(AudioSystem::DEVICE_OUT_EARPIECE, state);
265 status_t AudioHardwareALSA::dump(int fd, const Vector<String16>& args)
270 } // namespace android