1 /* AudioStreamInALSA.cpp
3 ** Copyright 2008-2009 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 AudioStreamInALSA::AudioStreamInALSA(AudioHardwareALSA *parent,
40 alsa_handle_t *handle,
41 AudioSystem::audio_in_acoustics audio_acoustics) :
42 ALSAStreamOps(parent, handle),
44 mAcoustics(audio_acoustics)
46 acoustic_device_t *aDev = acoustics();
48 if (aDev) aDev->set_params(aDev, mAcoustics, NULL);
51 AudioStreamInALSA::~AudioStreamInALSA()
56 status_t AudioStreamInALSA::setGain(float gain)
58 return mixer() ? mixer()->setMasterGain(gain) : (status_t)NO_INIT;
61 ssize_t AudioStreamInALSA::read(void *buffer, ssize_t bytes)
63 AutoMutex lock(mLock);
66 acquire_wake_lock (PARTIAL_WAKE_LOCK, "AudioInLock");
70 acoustic_device_t *aDev = acoustics();
72 // If there is an acoustics module read method, then it overrides this
73 // implementation (unlike AudioStreamOutALSA write).
74 if (aDev && aDev->read)
75 return aDev->read(aDev, buffer, bytes);
77 snd_pcm_sframes_t n, frames = snd_pcm_bytes_to_frames(mHandle->handle, bytes);
81 n = snd_pcm_readi(mHandle->handle, buffer, frames);
83 if (mHandle->handle) {
85 n = snd_pcm_recover(mHandle->handle, n, 0);
87 if (aDev && aDev->recover) aDev->recover(aDev, n);
89 n = snd_pcm_prepare(mHandle->handle);
91 return static_cast<ssize_t>(n);
93 } while (n == -EAGAIN);
95 return static_cast<ssize_t>(snd_pcm_frames_to_bytes(mHandle->handle, n));
98 status_t AudioStreamInALSA::dump(int fd, const Vector<String16>& args)
103 status_t AudioStreamInALSA::open(int mode)
105 AutoMutex lock(mLock);
107 status_t status = ALSAStreamOps::open(mode);
109 acoustic_device_t *aDev = acoustics();
111 if (status == NO_ERROR && aDev)
112 status = aDev->use_handle(aDev, mHandle);
117 status_t AudioStreamInALSA::close()
119 AutoMutex lock(mLock);
121 acoustic_device_t *aDev = acoustics();
123 if (mHandle && aDev) aDev->cleanup(aDev);
125 ALSAStreamOps::close();
128 release_wake_lock ("AudioInLock");
135 status_t AudioStreamInALSA::standby()
137 AutoMutex lock(mLock);
140 release_wake_lock ("AudioInLock");
147 void AudioStreamInALSA::resetFramesLost()
149 AutoMutex lock(mLock);
153 unsigned int AudioStreamInALSA::getInputFramesLost() const
155 unsigned int count = mFramesLost;
156 // Stupid interface wants us to have a side effect of clearing the count
157 // but is defined as a const to prevent such a thing.
158 ((AudioStreamInALSA *)this)->resetFramesLost();
162 status_t AudioStreamInALSA::setAcousticParams(void *params)
164 AutoMutex lock(mLock);
166 acoustic_device_t *aDev = acoustics();
168 return aDev ? aDev->set_params(aDev, mAcoustics, params) : (status_t)NO_ERROR;
171 } // namespace android