1 /* AudioStreamOutALSA.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"
36 #ifndef ALSA_DEFAULT_SAMPLE_RATE
37 #define ALSA_DEFAULT_SAMPLE_RATE 44100 // in Hz
43 // ----------------------------------------------------------------------------
45 static const int DEFAULT_SAMPLE_RATE = ALSA_DEFAULT_SAMPLE_RATE;
47 // ----------------------------------------------------------------------------
49 AudioStreamOutALSA::AudioStreamOutALSA(AudioHardwareALSA *parent, alsa_handle_t *handle) :
50 ALSAStreamOps(parent, handle)
54 AudioStreamOutALSA::~AudioStreamOutALSA()
59 uint32_t AudioStreamOutALSA::channels() const
61 int c = ALSAStreamOps::channels();
65 status_t AudioStreamOutALSA::setVolume(float left, float right)
67 return mixer()->setVolume (mHandle->curDev, left, right);
70 ssize_t AudioStreamOutALSA::write(const void *buffer, size_t bytes)
72 AutoMutex lock(mLock);
75 acquire_wake_lock (PARTIAL_WAKE_LOCK, "AudioOutLock");
79 acoustic_device_t *aDev = acoustics();
81 // For output, we will pass the data on to the acoustics module, but the actual
82 // data is expected to be sent to the audio device directly as well.
83 if (aDev && aDev->write)
84 aDev->write(aDev, buffer, bytes);
91 n = snd_pcm_writei(mHandle->handle,
92 (char *)buffer + sent,
93 snd_pcm_bytes_to_frames(mHandle->handle, bytes - sent));
95 // Somehow the stream is in a bad state. The driver probably
96 // has a bug and snd_pcm_recover() doesn't seem to handle this.
97 mHandle->module->open(mHandle, mHandle->curDev, mHandle->curMode);
99 if (aDev && aDev->recover) aDev->recover(aDev, n);
102 if (mHandle->handle) {
103 // snd_pcm_recover() will return 0 if successful in recovering from
104 // an error, or -errno if the error was unrecoverable.
105 n = snd_pcm_recover(mHandle->handle, n, 1);
107 if (aDev && aDev->recover) aDev->recover(aDev, n);
109 if (n) return static_cast<ssize_t>(n);
113 sent += static_cast<ssize_t>(snd_pcm_frames_to_bytes(mHandle->handle, n));
115 } while (mHandle->handle && sent < bytes);
120 status_t AudioStreamOutALSA::dump(int fd, const Vector<String16>& args)
125 status_t AudioStreamOutALSA::open(int mode)
127 AutoMutex lock(mLock);
129 return ALSAStreamOps::open(mode);
132 status_t AudioStreamOutALSA::close()
134 AutoMutex lock(mLock);
136 snd_pcm_drain (mHandle->handle);
140 release_wake_lock ("AudioOutLock");
147 status_t AudioStreamOutALSA::standby()
149 AutoMutex lock(mLock);
151 snd_pcm_drain (mHandle->handle);
154 release_wake_lock ("AudioOutLock");
161 #define USEC_TO_MSEC(x) ((x + 999) / 1000)
163 uint32_t AudioStreamOutALSA::latency() const
165 // Android wants latency in milliseconds.
166 return USEC_TO_MSEC (mHandle->latency);
169 } // namespace android