blob: f532d35dff6ac19460f36f6207a6172dd2e730ca [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioSystem"
18//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
Mathias Agopian07952722009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <media/AudioSystem.h>
Eric Laurenta553c252009-07-17 12:17:14 -070023#include <media/IAudioPolicyService.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <math.h>
25
Dima Zavin34bb4192011-05-11 14:15:23 -070026#include <system/audio.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070027
Eric Laurenta553c252009-07-17 12:17:14 -070028// ----------------------------------------------------------------------------
Eric Laurenta553c252009-07-17 12:17:14 -070029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030namespace android {
31
32// client singleton for AudioFlinger binder interface
33Mutex AudioSystem::gLock;
34sp<IAudioFlinger> AudioSystem::gAudioFlinger;
35sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
36audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
37// Cached values
Eric Laurenta553c252009-07-17 12:17:14 -070038DefaultKeyedVector<int, audio_io_handle_t> AudioSystem::gStreamOutputMap(0);
39DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0);
40
Glenn Kasten81801ad2012-01-10 09:01:19 -080041// Cached values for recording queries, all protected by gLock
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042uint32_t AudioSystem::gPrevInSamplingRate = 16000;
Dima Zavin24fc2fb2011-04-19 22:30:36 -070043int AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044int AudioSystem::gPrevInChannelCount = 1;
45size_t AudioSystem::gInBuffSize = 0;
46
47
48// establish binder interface to AudioFlinger service
49const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
50{
51 Mutex::Autolock _l(gLock);
52 if (gAudioFlinger.get() == 0) {
53 sp<IServiceManager> sm = defaultServiceManager();
54 sp<IBinder> binder;
55 do {
56 binder = sm->getService(String16("media.audio_flinger"));
57 if (binder != 0)
58 break;
Steve Block8564c8d2012-01-05 23:22:43 +000059 ALOGW("AudioFlinger not published, waiting...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 usleep(500000); // 0.5 s
61 } while(true);
62 if (gAudioFlingerClient == NULL) {
63 gAudioFlingerClient = new AudioFlingerClient();
64 } else {
65 if (gAudioErrorCallback) {
66 gAudioErrorCallback(NO_ERROR);
67 }
68 }
69 binder->linkToDeath(gAudioFlingerClient);
70 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
71 gAudioFlinger->registerClient(gAudioFlingerClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 }
Steve Block3762c312012-01-06 19:20:56 +000073 ALOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
Eric Laurenta553c252009-07-17 12:17:14 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 return gAudioFlinger;
76}
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078status_t AudioSystem::muteMicrophone(bool state) {
79 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
80 if (af == 0) return PERMISSION_DENIED;
81 return af->setMicMute(state);
82}
83
84status_t AudioSystem::isMicrophoneMuted(bool* state) {
85 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
86 if (af == 0) return PERMISSION_DENIED;
87 *state = af->getMicMute();
88 return NO_ERROR;
89}
90
91status_t AudioSystem::setMasterVolume(float value)
92{
93 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
94 if (af == 0) return PERMISSION_DENIED;
95 af->setMasterVolume(value);
96 return NO_ERROR;
97}
98
99status_t AudioSystem::setMasterMute(bool mute)
100{
101 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
102 if (af == 0) return PERMISSION_DENIED;
103 af->setMasterMute(mute);
104 return NO_ERROR;
105}
106
107status_t AudioSystem::getMasterVolume(float* volume)
108{
109 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
110 if (af == 0) return PERMISSION_DENIED;
111 *volume = af->masterVolume();
112 return NO_ERROR;
113}
114
115status_t AudioSystem::getMasterMute(bool* mute)
116{
117 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
118 if (af == 0) return PERMISSION_DENIED;
119 *mute = af->masterMute();
120 return NO_ERROR;
121}
122
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800123status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, int output)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700125 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
127 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700128 af->setStreamVolume(stream, value, output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 return NO_ERROR;
130}
131
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800132status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700134 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
136 if (af == 0) return PERMISSION_DENIED;
137 af->setStreamMute(stream, mute);
138 return NO_ERROR;
139}
140
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800141status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, int output)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700143 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
145 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700146 *volume = af->streamVolume(stream, output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 return NO_ERROR;
148}
149
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800150status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700152 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
154 if (af == 0) return PERMISSION_DENIED;
155 *mute = af->streamMute(stream);
156 return NO_ERROR;
157}
158
Glenn Kastenaccb1142012-01-04 11:00:47 -0800159status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160{
Glenn Kasten01aaf2c2012-01-06 16:47:31 -0800161 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
163 if (af == 0) return PERMISSION_DENIED;
164 return af->setMode(mode);
165}
166
Eric Laurenta553c252009-07-17 12:17:14 -0700167status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
169 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700170 return af->setParameters(ioHandle, keyValuePairs);
171}
172
173String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
175 String8 result = String8("");
176 if (af == 0) return result;
177
178 result = af->getParameters(ioHandle, keys);
179 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180}
181
182// convert volume steps to natural log scale
183
184// change this value to change volume scaling
185static const float dBPerStep = 0.5f;
186// shouldn't need to touch these
187static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
188static const float dBConvertInverse = 1.0f / dBConvert;
189
190float AudioSystem::linearToLog(int volume)
191{
192 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
Steve Block5baa3a62011-12-20 16:23:08 +0000193 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 // return v;
195 return volume ? exp(float(100 - volume) * dBConvert) : 0;
196}
197
198int AudioSystem::logToLinear(float volume)
199{
200 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
Steve Block5baa3a62011-12-20 16:23:08 +0000201 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 // return v;
203 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
204}
205
Andreas Huber28ea0132012-01-18 10:51:55 -0800206// DEPRECATED
207status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType) {
208 return getOutputSamplingRate(samplingRate, (audio_stream_type_t)streamType);
209}
210
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800211status_t AudioSystem::getOutputSamplingRate(int* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212{
Eric Laurenta553c252009-07-17 12:17:14 -0700213 OutputDescriptor *outputDesc;
214 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700216 if (streamType == AUDIO_STREAM_DEFAULT) {
217 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700218 }
219
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800220 output = getOutput(streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700221 if (output == 0) {
222 return PERMISSION_DENIED;
223 }
224
225 gLock.lock();
226 outputDesc = AudioSystem::gOutputs.valueFor(output);
227 if (outputDesc == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100228 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700229 gLock.unlock();
230 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
231 if (af == 0) return PERMISSION_DENIED;
232 *samplingRate = af->sampleRate(output);
233 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100234 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurenta553c252009-07-17 12:17:14 -0700235 *samplingRate = outputDesc->samplingRate;
236 gLock.unlock();
237 }
238
Steve Block71f2cf12011-10-20 11:56:00 +0100239 ALOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
Eric Laurenta553c252009-07-17 12:17:14 -0700240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 return NO_ERROR;
242}
243
Andreas Huber28ea0132012-01-18 10:51:55 -0800244// DEPRECATED
245status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType) {
246 return getOutputFrameCount(frameCount, (audio_stream_type_t)streamType);
247}
248
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800249status_t AudioSystem::getOutputFrameCount(int* frameCount, audio_stream_type_t streamType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250{
Eric Laurenta553c252009-07-17 12:17:14 -0700251 OutputDescriptor *outputDesc;
252 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700254 if (streamType == AUDIO_STREAM_DEFAULT) {
255 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700256 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700257
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800258 output = getOutput(streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700259 if (output == 0) {
260 return PERMISSION_DENIED;
261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Eric Laurenta553c252009-07-17 12:17:14 -0700263 gLock.lock();
264 outputDesc = AudioSystem::gOutputs.valueFor(output);
265 if (outputDesc == 0) {
266 gLock.unlock();
267 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
268 if (af == 0) return PERMISSION_DENIED;
269 *frameCount = af->frameCount(output);
270 } else {
271 *frameCount = outputDesc->frameCount;
272 gLock.unlock();
273 }
274
Steve Block71f2cf12011-10-20 11:56:00 +0100275 ALOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 return NO_ERROR;
278}
279
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800280status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281{
Eric Laurenta553c252009-07-17 12:17:14 -0700282 OutputDescriptor *outputDesc;
283 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700285 if (streamType == AUDIO_STREAM_DEFAULT) {
286 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700287 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700288
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800289 output = getOutput(streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700290 if (output == 0) {
291 return PERMISSION_DENIED;
292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
Eric Laurenta553c252009-07-17 12:17:14 -0700294 gLock.lock();
295 outputDesc = AudioSystem::gOutputs.valueFor(output);
296 if (outputDesc == 0) {
297 gLock.unlock();
298 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
299 if (af == 0) return PERMISSION_DENIED;
300 *latency = af->latency(output);
301 } else {
302 *latency = outputDesc->latency;
303 gLock.unlock();
304 }
305
Steve Block71f2cf12011-10-20 11:56:00 +0100306 ALOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
Eric Laurenta553c252009-07-17 12:17:14 -0700307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 return NO_ERROR;
309}
310
Eric Laurenta553c252009-07-17 12:17:14 -0700311status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 size_t* buffSize)
313{
Glenn Kasten81801ad2012-01-10 09:01:19 -0800314 gLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kasten81801ad2012-01-10 09:01:19 -0800316 size_t inBuffSize = gInBuffSize;
317 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 || (channelCount != gPrevInChannelCount)) {
Glenn Kasten81801ad2012-01-10 09:01:19 -0800319 gLock.unlock();
320 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
321 if (af == 0) {
322 return PERMISSION_DENIED;
323 }
324 inBuffSize = af->getInputBufferSize(sampleRate, format, channelCount);
325 gLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 // save the request params
327 gPrevInSamplingRate = sampleRate;
Eric Laurenta553c252009-07-17 12:17:14 -0700328 gPrevInFormat = format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 gPrevInChannelCount = channelCount;
330
Glenn Kasten81801ad2012-01-10 09:01:19 -0800331 gInBuffSize = inBuffSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700332 }
Glenn Kasten81801ad2012-01-10 09:01:19 -0800333 gLock.unlock();
334 *buffSize = inBuffSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 return NO_ERROR;
337}
338
Eric Laurent415f3e22009-10-21 08:14:22 -0700339status_t AudioSystem::setVoiceVolume(float value)
340{
341 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
342 if (af == 0) return PERMISSION_DENIED;
343 return af->setVoiceVolume(value);
344}
345
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800346status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, audio_stream_type_t stream)
Eric Laurent0986e792010-01-19 17:37:09 -0800347{
348 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
349 if (af == 0) return PERMISSION_DENIED;
350
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700351 if (stream == AUDIO_STREAM_DEFAULT) {
352 stream = AUDIO_STREAM_MUSIC;
Eric Laurent0986e792010-01-19 17:37:09 -0800353 }
354
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800355 return af->getRenderPosition(halFrames, dspFrames, getOutput(stream));
Eric Laurent0986e792010-01-19 17:37:09 -0800356}
357
Eric Laurent47d0a922010-02-26 02:47:27 -0800358unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
359 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
360 unsigned int result = 0;
361 if (af == 0) return result;
Eric Laurentef9500f2010-03-11 14:47:00 -0800362 if (ioHandle == 0) return result;
Eric Laurent47d0a922010-02-26 02:47:27 -0800363
364 result = af->getInputFramesLost(ioHandle);
365 return result;
366}
367
Eric Laurent65b65452010-06-01 23:49:17 -0700368int AudioSystem::newAudioSessionId() {
369 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
370 if (af == 0) return 0;
371 return af->newAudioSessionId();
372}
373
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700374void AudioSystem::acquireAudioSessionId(int audioSession) {
375 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
376 if (af != 0) {
377 af->acquireAudioSessionId(audioSession);
378 }
379}
380
381void AudioSystem::releaseAudioSessionId(int audioSession) {
382 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
383 if (af != 0) {
384 af->releaseAudioSessionId(audioSession);
385 }
386}
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388// ---------------------------------------------------------------------------
389
Eric Laurenta553c252009-07-17 12:17:14 -0700390void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392
Eric Laurenta553c252009-07-17 12:17:14 -0700393 AudioSystem::gAudioFlinger.clear();
Eric Laurent787aa592010-01-25 10:27:15 -0800394 // clear output handles and stream to output map caches
395 AudioSystem::gStreamOutputMap.clear();
396 AudioSystem::gOutputs.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397
398 if (gAudioErrorCallback) {
399 gAudioErrorCallback(DEAD_OBJECT);
400 }
Steve Block8564c8d2012-01-05 23:22:43 +0000401 ALOGW("AudioFlinger server died!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402}
403
Eric Laurentddb78e72009-07-28 08:44:33 -0700404void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) {
Steve Block71f2cf12011-10-20 11:56:00 +0100405 ALOGV("ioConfigChanged() event %d", event);
Eric Laurenta553c252009-07-17 12:17:14 -0700406 OutputDescriptor *desc;
407 uint32_t stream;
408
Eric Laurentddb78e72009-07-28 08:44:33 -0700409 if (ioHandle == 0) return;
Eric Laurenta553c252009-07-17 12:17:14 -0700410
411 Mutex::Autolock _l(AudioSystem::gLock);
412
413 switch (event) {
414 case STREAM_CONFIG_CHANGED:
415 if (param2 == 0) break;
416 stream = *(uint32_t *)param2;
Steve Block71f2cf12011-10-20 11:56:00 +0100417 ALOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700418 if (gStreamOutputMap.indexOfKey(stream) >= 0) {
419 gStreamOutputMap.replaceValueFor(stream, ioHandle);
420 }
421 break;
422 case OUTPUT_OPENED: {
423 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100424 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700425 break;
426 }
427 if (param2 == 0) break;
428 desc = (OutputDescriptor *)param2;
429
430 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
431 gOutputs.add(ioHandle, outputDesc);
Steve Block71f2cf12011-10-20 11:56:00 +0100432 ALOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700433 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
434 } break;
435 case OUTPUT_CLOSED: {
436 if (gOutputs.indexOfKey(ioHandle) < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000437 ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700438 break;
439 }
Steve Block71f2cf12011-10-20 11:56:00 +0100440 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700441
442 gOutputs.removeItem(ioHandle);
443 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) {
444 if (gStreamOutputMap.valueAt(i) == ioHandle) {
445 gStreamOutputMap.removeItemsAt(i);
446 }
447 }
448 } break;
449
450 case OUTPUT_CONFIG_CHANGED: {
451 int index = gOutputs.indexOfKey(ioHandle);
452 if (index < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000453 ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700454 break;
455 }
456 if (param2 == 0) break;
457 desc = (OutputDescriptor *)param2;
458
Steve Block71f2cf12011-10-20 11:56:00 +0100459 ALOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700460 ioHandle, desc->samplingRate, desc->format,
461 desc->channels, desc->frameCount, desc->latency);
462 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
463 delete outputDesc;
464 outputDesc = new OutputDescriptor(*desc);
465 gOutputs.replaceValueFor(ioHandle, outputDesc);
466 } break;
467 case INPUT_OPENED:
468 case INPUT_CLOSED:
469 case INPUT_CONFIG_CHANGED:
470 break;
471
472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473}
474
475void AudioSystem::setErrorCallback(audio_error_callback cb) {
Eric Laurenta553c252009-07-17 12:17:14 -0700476 Mutex::Autolock _l(gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 gAudioErrorCallback = cb;
478}
479
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800480bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 switch(streamType) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700482 case AUDIO_STREAM_MUSIC:
483 case AUDIO_STREAM_VOICE_CALL:
484 case AUDIO_STREAM_BLUETOOTH_SCO:
485 case AUDIO_STREAM_SYSTEM:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 return true;
487 default:
488 return false;
489 }
490}
491
492
Eric Laurenta553c252009-07-17 12:17:14 -0700493// client singleton for AudioPolicyService binder interface
494sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
495sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
496
497
498// establish binder interface to AudioFlinger service
499const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
500{
501 gLock.lock();
502 if (gAudioPolicyService.get() == 0) {
503 sp<IServiceManager> sm = defaultServiceManager();
504 sp<IBinder> binder;
505 do {
506 binder = sm->getService(String16("media.audio_policy"));
507 if (binder != 0)
508 break;
Steve Block8564c8d2012-01-05 23:22:43 +0000509 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurenta553c252009-07-17 12:17:14 -0700510 usleep(500000); // 0.5 s
511 } while(true);
512 if (gAudioPolicyServiceClient == NULL) {
513 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
514 }
515 binder->linkToDeath(gAudioPolicyServiceClient);
516 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
517 gLock.unlock();
518 } else {
519 gLock.unlock();
520 }
521 return gAudioPolicyService;
522}
523
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700524status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
525 audio_policy_dev_state_t state,
526 const char *device_address)
Eric Laurenta553c252009-07-17 12:17:14 -0700527{
528 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentdca56b92011-09-02 14:20:56 -0700529 const char *address = "";
530
Eric Laurenta553c252009-07-17 12:17:14 -0700531 if (aps == 0) return PERMISSION_DENIED;
532
Eric Laurentdca56b92011-09-02 14:20:56 -0700533 if (device_address != NULL) {
534 address = device_address;
535 }
536
537 return aps->setDeviceConnectionState(device, state, address);
Eric Laurenta553c252009-07-17 12:17:14 -0700538}
539
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700540audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurenta553c252009-07-17 12:17:14 -0700541 const char *device_address)
542{
543 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700544 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta553c252009-07-17 12:17:14 -0700545
546 return aps->getDeviceConnectionState(device, device_address);
547}
548
Glenn Kastenaccb1142012-01-04 11:00:47 -0800549status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurenta553c252009-07-17 12:17:14 -0700550{
551 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
552 if (aps == 0) return PERMISSION_DENIED;
553
554 return aps->setPhoneState(state);
555}
556
557status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask)
558{
559 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
560 if (aps == 0) return PERMISSION_DENIED;
561 return aps->setRingerMode(mode, mask);
562}
563
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700564status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurenta553c252009-07-17 12:17:14 -0700565{
566 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
567 if (aps == 0) return PERMISSION_DENIED;
568 return aps->setForceUse(usage, config);
569}
570
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700571audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurenta553c252009-07-17 12:17:14 -0700572{
573 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700574 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurenta553c252009-07-17 12:17:14 -0700575 return aps->getForceUse(usage);
576}
577
578
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700579audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -0700580 uint32_t samplingRate,
581 uint32_t format,
582 uint32_t channels,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700583 audio_policy_output_flags_t flags)
Eric Laurenta553c252009-07-17 12:17:14 -0700584{
Eric Laurentddb78e72009-07-28 08:44:33 -0700585 audio_io_handle_t output = 0;
Eric Laurentef9500f2010-03-11 14:47:00 -0800586 // Do not use stream to output map cache if the direct output
587 // flag is set or if we are likely to use a direct output
588 // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to
589 // a direct output on some platforms).
590 // TODO: the output cache and stream to output mapping implementation needs to
591 // be reworked for proper operation with direct outputs. This code is too specific
592 // to the first use case we want to cover (Voice Recognition and Voice Dialer over
593 // Bluetooth SCO
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700594 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0 &&
595 ((stream != AUDIO_STREAM_VOICE_CALL && stream != AUDIO_STREAM_BLUETOOTH_SCO) ||
596 channels != AUDIO_CHANNEL_OUT_MONO ||
Eric Laurentef9500f2010-03-11 14:47:00 -0800597 (samplingRate != 8000 && samplingRate != 16000))) {
Eric Laurenta553c252009-07-17 12:17:14 -0700598 Mutex::Autolock _l(gLock);
599 output = AudioSystem::gStreamOutputMap.valueFor(stream);
Steve Block71f2cf12011-10-20 11:56:00 +0100600 ALOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700601 }
Eric Laurentddb78e72009-07-28 08:44:33 -0700602 if (output == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -0700603 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700604 if (aps == 0) return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700605 output = aps->getOutput(stream, samplingRate, format, channels, flags);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700606 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -0700607 Mutex::Autolock _l(gLock);
608 AudioSystem::gStreamOutputMap.add(stream, output);
609 }
610 }
611 return output;
612}
613
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700614status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700615 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700616 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700617{
618 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
619 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700620 return aps->startOutput(output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700621}
622
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700623status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700624 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700625 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700626{
627 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
628 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700629 return aps->stopOutput(output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700630}
631
632void AudioSystem::releaseOutput(audio_io_handle_t output)
633{
634 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
635 if (aps == 0) return;
636 aps->releaseOutput(output);
637}
638
639audio_io_handle_t AudioSystem::getInput(int inputSource,
640 uint32_t samplingRate,
641 uint32_t format,
642 uint32_t channels,
Eric Laurent464d5b32011-06-17 21:29:58 -0700643 audio_in_acoustics_t acoustics,
644 int sessionId)
Eric Laurenta553c252009-07-17 12:17:14 -0700645{
646 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700647 if (aps == 0) return 0;
Eric Laurent464d5b32011-06-17 21:29:58 -0700648 return aps->getInput(inputSource, samplingRate, format, channels, acoustics, sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700649}
650
651status_t AudioSystem::startInput(audio_io_handle_t input)
652{
653 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
654 if (aps == 0) return PERMISSION_DENIED;
655 return aps->startInput(input);
656}
657
658status_t AudioSystem::stopInput(audio_io_handle_t input)
659{
660 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
661 if (aps == 0) return PERMISSION_DENIED;
662 return aps->stopInput(input);
663}
664
665void AudioSystem::releaseInput(audio_io_handle_t input)
666{
667 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
668 if (aps == 0) return;
669 aps->releaseInput(input);
670}
671
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700672status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -0700673 int indexMin,
674 int indexMax)
675{
676 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
677 if (aps == 0) return PERMISSION_DENIED;
678 return aps->initStreamVolume(stream, indexMin, indexMax);
679}
680
Eric Laurent9bc83582011-11-18 16:43:31 -0800681status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
682 int index,
683 audio_devices_t device)
Eric Laurenta553c252009-07-17 12:17:14 -0700684{
685 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
686 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent9bc83582011-11-18 16:43:31 -0800687 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurenta553c252009-07-17 12:17:14 -0700688}
689
Eric Laurent9bc83582011-11-18 16:43:31 -0800690status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
691 int *index,
692 audio_devices_t device)
Eric Laurenta553c252009-07-17 12:17:14 -0700693{
694 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
695 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent9bc83582011-11-18 16:43:31 -0800696 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurenta553c252009-07-17 12:17:14 -0700697}
698
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700699uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700700{
701 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
702 if (aps == 0) return 0;
703 return aps->getStrategyForStream(stream);
704}
705
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700706uint32_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800707{
708 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
709 if (aps == 0) return 0;
710 return aps->getDevicesForStream(stream);
711}
712
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700713audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc)
714{
715 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
716 if (aps == 0) return PERMISSION_DENIED;
717 return aps->getOutputForEffect(desc);
718}
719
720status_t AudioSystem::registerEffect(effect_descriptor_t *desc,
Eric Laurent464d5b32011-06-17 21:29:58 -0700721 audio_io_handle_t io,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700722 uint32_t strategy,
723 int session,
724 int id)
725{
726 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
727 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent464d5b32011-06-17 21:29:58 -0700728 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700729}
730
731status_t AudioSystem::unregisterEffect(int id)
732{
733 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
734 if (aps == 0) return PERMISSION_DENIED;
735 return aps->unregisterEffect(id);
736}
737
Eric Laurent6752ec82011-08-10 10:37:50 -0700738status_t AudioSystem::setEffectEnabled(int id, bool enabled)
739{
740 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
741 if (aps == 0) return PERMISSION_DENIED;
742 return aps->setEffectEnabled(id, enabled);
743}
744
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800745status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent464d5b32011-06-17 21:29:58 -0700746{
Eric Laurent25101b02011-02-02 09:33:30 -0800747 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
748 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent464d5b32011-06-17 21:29:58 -0700749 if (state == NULL) return BAD_VALUE;
Eric Laurent25101b02011-02-02 09:33:30 -0800750 *state = aps->isStreamActive(stream, inPastMs);
751 return NO_ERROR;
752}
753
754
Eric Laurent05ce0942011-08-30 10:18:54 -0700755void AudioSystem::clearAudioConfigCache()
756{
757 Mutex::Autolock _l(gLock);
Steve Block71f2cf12011-10-20 11:56:00 +0100758 ALOGV("clearAudioConfigCache()");
Eric Laurent05ce0942011-08-30 10:18:54 -0700759 gStreamOutputMap.clear();
760 gOutputs.clear();
761}
762
Eric Laurenta553c252009-07-17 12:17:14 -0700763// ---------------------------------------------------------------------------
764
765void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
766 Mutex::Autolock _l(AudioSystem::gLock);
767 AudioSystem::gAudioPolicyService.clear();
768
Steve Block8564c8d2012-01-05 23:22:43 +0000769 ALOGW("AudioPolicyService server died!");
Eric Laurenta553c252009-07-17 12:17:14 -0700770}
771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772}; // namespace android
773