blob: e08a55be7f9669d44467c364803bb992042d3892 [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 Zavin24fc2fb2011-04-19 22:30:36 -070026#include <hardware/audio.h>
27
Eric Laurenta553c252009-07-17 12:17:14 -070028// ----------------------------------------------------------------------------
29// the sim build doesn't have gettid
30
31#ifndef HAVE_GETTID
32# define gettid getpid
33#endif
34
35// ----------------------------------------------------------------------------
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037namespace android {
38
39// client singleton for AudioFlinger binder interface
40Mutex AudioSystem::gLock;
41sp<IAudioFlinger> AudioSystem::gAudioFlinger;
42sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
43audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
44// Cached values
Eric Laurenta553c252009-07-17 12:17:14 -070045DefaultKeyedVector<int, audio_io_handle_t> AudioSystem::gStreamOutputMap(0);
46DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0);
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048// Cached values for recording queries
49uint32_t AudioSystem::gPrevInSamplingRate = 16000;
Dima Zavin24fc2fb2011-04-19 22:30:36 -070050int AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051int AudioSystem::gPrevInChannelCount = 1;
52size_t AudioSystem::gInBuffSize = 0;
53
54
55// establish binder interface to AudioFlinger service
56const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
57{
58 Mutex::Autolock _l(gLock);
59 if (gAudioFlinger.get() == 0) {
60 sp<IServiceManager> sm = defaultServiceManager();
61 sp<IBinder> binder;
62 do {
63 binder = sm->getService(String16("media.audio_flinger"));
64 if (binder != 0)
65 break;
66 LOGW("AudioFlinger not published, waiting...");
67 usleep(500000); // 0.5 s
68 } while(true);
69 if (gAudioFlingerClient == NULL) {
70 gAudioFlingerClient = new AudioFlingerClient();
71 } else {
72 if (gAudioErrorCallback) {
73 gAudioErrorCallback(NO_ERROR);
74 }
75 }
76 binder->linkToDeath(gAudioFlingerClient);
77 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
78 gAudioFlinger->registerClient(gAudioFlingerClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 }
80 LOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
Eric Laurenta553c252009-07-17 12:17:14 -070081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 return gAudioFlinger;
83}
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085status_t AudioSystem::muteMicrophone(bool state) {
86 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
87 if (af == 0) return PERMISSION_DENIED;
88 return af->setMicMute(state);
89}
90
91status_t AudioSystem::isMicrophoneMuted(bool* state) {
92 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
93 if (af == 0) return PERMISSION_DENIED;
94 *state = af->getMicMute();
95 return NO_ERROR;
96}
97
98status_t AudioSystem::setMasterVolume(float value)
99{
100 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
101 if (af == 0) return PERMISSION_DENIED;
102 af->setMasterVolume(value);
103 return NO_ERROR;
104}
105
106status_t AudioSystem::setMasterMute(bool mute)
107{
108 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
109 if (af == 0) return PERMISSION_DENIED;
110 af->setMasterMute(mute);
111 return NO_ERROR;
112}
113
114status_t AudioSystem::getMasterVolume(float* volume)
115{
116 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
117 if (af == 0) return PERMISSION_DENIED;
118 *volume = af->masterVolume();
119 return NO_ERROR;
120}
121
122status_t AudioSystem::getMasterMute(bool* mute)
123{
124 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
125 if (af == 0) return PERMISSION_DENIED;
126 *mute = af->masterMute();
127 return NO_ERROR;
128}
129
Eric Laurentddb78e72009-07-28 08:44:33 -0700130status_t AudioSystem::setStreamVolume(int stream, float value, int output)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700132 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
134 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700135 af->setStreamVolume(stream, value, output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 return NO_ERROR;
137}
138
139status_t AudioSystem::setStreamMute(int stream, bool mute)
140{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700141 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
143 if (af == 0) return PERMISSION_DENIED;
144 af->setStreamMute(stream, mute);
145 return NO_ERROR;
146}
147
Eric Laurentddb78e72009-07-28 08:44:33 -0700148status_t AudioSystem::getStreamVolume(int stream, float* volume, int output)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700150 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
152 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700153 *volume = af->streamVolume(stream, output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 return NO_ERROR;
155}
156
157status_t AudioSystem::getStreamMute(int stream, bool* mute)
158{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700159 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
161 if (af == 0) return PERMISSION_DENIED;
162 *mute = af->streamMute(stream);
163 return NO_ERROR;
164}
165
166status_t AudioSystem::setMode(int mode)
167{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700168 if (mode >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
170 if (af == 0) return PERMISSION_DENIED;
171 return af->setMode(mode);
172}
173
Eric Laurenta553c252009-07-17 12:17:14 -0700174status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
176 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700177 return af->setParameters(ioHandle, keyValuePairs);
178}
179
180String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
181 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
182 String8 result = String8("");
183 if (af == 0) return result;
184
185 result = af->getParameters(ioHandle, keys);
186 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187}
188
189// convert volume steps to natural log scale
190
191// change this value to change volume scaling
192static const float dBPerStep = 0.5f;
193// shouldn't need to touch these
194static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
195static const float dBConvertInverse = 1.0f / dBConvert;
196
197float AudioSystem::linearToLog(int volume)
198{
199 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
200 // LOGD("linearToLog(%d)=%f", volume, v);
201 // return v;
202 return volume ? exp(float(100 - volume) * dBConvert) : 0;
203}
204
205int AudioSystem::logToLinear(float volume)
206{
207 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
208 // LOGD("logTolinear(%d)=%f", v, volume);
209 // return v;
210 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
211}
212
213status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType)
214{
Eric Laurenta553c252009-07-17 12:17:14 -0700215 OutputDescriptor *outputDesc;
216 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700218 if (streamType == AUDIO_STREAM_DEFAULT) {
219 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700220 }
221
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700222 output = getOutput((audio_stream_type_t)streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700223 if (output == 0) {
224 return PERMISSION_DENIED;
225 }
226
227 gLock.lock();
228 outputDesc = AudioSystem::gOutputs.valueFor(output);
229 if (outputDesc == 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700230 LOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700231 gLock.unlock();
232 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
233 if (af == 0) return PERMISSION_DENIED;
234 *samplingRate = af->sampleRate(output);
235 } else {
236 LOGV("getOutputSamplingRate() reading from output desc");
237 *samplingRate = outputDesc->samplingRate;
238 gLock.unlock();
239 }
240
Eric Laurentddb78e72009-07-28 08:44:33 -0700241 LOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
Eric Laurenta553c252009-07-17 12:17:14 -0700242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 return NO_ERROR;
244}
245
246status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType)
247{
Eric Laurenta553c252009-07-17 12:17:14 -0700248 OutputDescriptor *outputDesc;
249 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700251 if (streamType == AUDIO_STREAM_DEFAULT) {
252 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700253 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700254
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700255 output = getOutput((audio_stream_type_t)streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700256 if (output == 0) {
257 return PERMISSION_DENIED;
258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
Eric Laurenta553c252009-07-17 12:17:14 -0700260 gLock.lock();
261 outputDesc = AudioSystem::gOutputs.valueFor(output);
262 if (outputDesc == 0) {
263 gLock.unlock();
264 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
265 if (af == 0) return PERMISSION_DENIED;
266 *frameCount = af->frameCount(output);
267 } else {
268 *frameCount = outputDesc->frameCount;
269 gLock.unlock();
270 }
271
Eric Laurentddb78e72009-07-28 08:44:33 -0700272 LOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 return NO_ERROR;
275}
276
277status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType)
278{
Eric Laurenta553c252009-07-17 12:17:14 -0700279 OutputDescriptor *outputDesc;
280 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700282 if (streamType == AUDIO_STREAM_DEFAULT) {
283 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700284 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700285
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700286 output = getOutput((audio_stream_type_t)streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700287 if (output == 0) {
288 return PERMISSION_DENIED;
289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
Eric Laurenta553c252009-07-17 12:17:14 -0700291 gLock.lock();
292 outputDesc = AudioSystem::gOutputs.valueFor(output);
293 if (outputDesc == 0) {
294 gLock.unlock();
295 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
296 if (af == 0) return PERMISSION_DENIED;
297 *latency = af->latency(output);
298 } else {
299 *latency = outputDesc->latency;
300 gLock.unlock();
301 }
302
Eric Laurentddb78e72009-07-28 08:44:33 -0700303 LOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
Eric Laurenta553c252009-07-17 12:17:14 -0700304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 return NO_ERROR;
306}
307
Eric Laurenta553c252009-07-17 12:17:14 -0700308status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 size_t* buffSize)
310{
311 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Eric Laurenta553c252009-07-17 12:17:14 -0700312 if ((gInBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 || (channelCount != gPrevInChannelCount)) {
314 // save the request params
315 gPrevInSamplingRate = sampleRate;
Eric Laurenta553c252009-07-17 12:17:14 -0700316 gPrevInFormat = format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 gPrevInChannelCount = channelCount;
318
319 gInBuffSize = 0;
320 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
321 if (af == 0) {
322 return PERMISSION_DENIED;
323 }
324 gInBuffSize = af->getInputBufferSize(sampleRate, format, channelCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 *buffSize = gInBuffSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 return NO_ERROR;
329}
330
Eric Laurent415f3e22009-10-21 08:14:22 -0700331status_t AudioSystem::setVoiceVolume(float value)
332{
333 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
334 if (af == 0) return PERMISSION_DENIED;
335 return af->setVoiceVolume(value);
336}
337
Eric Laurent0986e792010-01-19 17:37:09 -0800338status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int stream)
339{
340 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
341 if (af == 0) return PERMISSION_DENIED;
342
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700343 if (stream == AUDIO_STREAM_DEFAULT) {
344 stream = AUDIO_STREAM_MUSIC;
Eric Laurent0986e792010-01-19 17:37:09 -0800345 }
346
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700347 return af->getRenderPosition(halFrames, dspFrames, getOutput((audio_stream_type_t)stream));
Eric Laurent0986e792010-01-19 17:37:09 -0800348}
349
Eric Laurent47d0a922010-02-26 02:47:27 -0800350unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
351 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
352 unsigned int result = 0;
353 if (af == 0) return result;
Eric Laurentef9500f2010-03-11 14:47:00 -0800354 if (ioHandle == 0) return result;
Eric Laurent47d0a922010-02-26 02:47:27 -0800355
356 result = af->getInputFramesLost(ioHandle);
357 return result;
358}
359
Eric Laurent65b65452010-06-01 23:49:17 -0700360int AudioSystem::newAudioSessionId() {
361 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
362 if (af == 0) return 0;
363 return af->newAudioSessionId();
364}
365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366// ---------------------------------------------------------------------------
367
Eric Laurenta553c252009-07-17 12:17:14 -0700368void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
Eric Laurenta553c252009-07-17 12:17:14 -0700371 AudioSystem::gAudioFlinger.clear();
Eric Laurent787aa592010-01-25 10:27:15 -0800372 // clear output handles and stream to output map caches
373 AudioSystem::gStreamOutputMap.clear();
374 AudioSystem::gOutputs.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375
376 if (gAudioErrorCallback) {
377 gAudioErrorCallback(DEAD_OBJECT);
378 }
379 LOGW("AudioFlinger server died!");
380}
381
Eric Laurentddb78e72009-07-28 08:44:33 -0700382void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) {
Eric Laurenta553c252009-07-17 12:17:14 -0700383 LOGV("ioConfigChanged() event %d", event);
Eric Laurenta553c252009-07-17 12:17:14 -0700384 OutputDescriptor *desc;
385 uint32_t stream;
386
Eric Laurentddb78e72009-07-28 08:44:33 -0700387 if (ioHandle == 0) return;
Eric Laurenta553c252009-07-17 12:17:14 -0700388
389 Mutex::Autolock _l(AudioSystem::gLock);
390
391 switch (event) {
392 case STREAM_CONFIG_CHANGED:
393 if (param2 == 0) break;
394 stream = *(uint32_t *)param2;
Eric Laurentddb78e72009-07-28 08:44:33 -0700395 LOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700396 if (gStreamOutputMap.indexOfKey(stream) >= 0) {
397 gStreamOutputMap.replaceValueFor(stream, ioHandle);
398 }
399 break;
400 case OUTPUT_OPENED: {
401 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700402 LOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700403 break;
404 }
405 if (param2 == 0) break;
406 desc = (OutputDescriptor *)param2;
407
408 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
409 gOutputs.add(ioHandle, outputDesc);
410 LOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d",
411 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
412 } break;
413 case OUTPUT_CLOSED: {
414 if (gOutputs.indexOfKey(ioHandle) < 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700415 LOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700416 break;
417 }
Eric Laurentddb78e72009-07-28 08:44:33 -0700418 LOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700419
420 gOutputs.removeItem(ioHandle);
421 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) {
422 if (gStreamOutputMap.valueAt(i) == ioHandle) {
423 gStreamOutputMap.removeItemsAt(i);
424 }
425 }
426 } break;
427
428 case OUTPUT_CONFIG_CHANGED: {
429 int index = gOutputs.indexOfKey(ioHandle);
430 if (index < 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700431 LOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700432 break;
433 }
434 if (param2 == 0) break;
435 desc = (OutputDescriptor *)param2;
436
Eric Laurentddb78e72009-07-28 08:44:33 -0700437 LOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700438 ioHandle, desc->samplingRate, desc->format,
439 desc->channels, desc->frameCount, desc->latency);
440 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
441 delete outputDesc;
442 outputDesc = new OutputDescriptor(*desc);
443 gOutputs.replaceValueFor(ioHandle, outputDesc);
444 } break;
445 case INPUT_OPENED:
446 case INPUT_CLOSED:
447 case INPUT_CONFIG_CHANGED:
448 break;
449
450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451}
452
453void AudioSystem::setErrorCallback(audio_error_callback cb) {
Eric Laurenta553c252009-07-17 12:17:14 -0700454 Mutex::Autolock _l(gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 gAudioErrorCallback = cb;
456}
457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458bool AudioSystem::routedToA2dpOutput(int streamType) {
459 switch(streamType) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700460 case AUDIO_STREAM_MUSIC:
461 case AUDIO_STREAM_VOICE_CALL:
462 case AUDIO_STREAM_BLUETOOTH_SCO:
463 case AUDIO_STREAM_SYSTEM:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 return true;
465 default:
466 return false;
467 }
468}
469
470
Eric Laurenta553c252009-07-17 12:17:14 -0700471// client singleton for AudioPolicyService binder interface
472sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
473sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
474
475
476// establish binder interface to AudioFlinger service
477const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
478{
479 gLock.lock();
480 if (gAudioPolicyService.get() == 0) {
481 sp<IServiceManager> sm = defaultServiceManager();
482 sp<IBinder> binder;
483 do {
484 binder = sm->getService(String16("media.audio_policy"));
485 if (binder != 0)
486 break;
487 LOGW("AudioPolicyService not published, waiting...");
488 usleep(500000); // 0.5 s
489 } while(true);
490 if (gAudioPolicyServiceClient == NULL) {
491 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
492 }
493 binder->linkToDeath(gAudioPolicyServiceClient);
494 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
495 gLock.unlock();
496 } else {
497 gLock.unlock();
498 }
499 return gAudioPolicyService;
500}
501
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700502status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
503 audio_policy_dev_state_t state,
504 const char *device_address)
Eric Laurenta553c252009-07-17 12:17:14 -0700505{
506 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
507 if (aps == 0) return PERMISSION_DENIED;
508
509 return aps->setDeviceConnectionState(device, state, device_address);
510}
511
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700512audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurenta553c252009-07-17 12:17:14 -0700513 const char *device_address)
514{
515 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700516 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta553c252009-07-17 12:17:14 -0700517
518 return aps->getDeviceConnectionState(device, device_address);
519}
520
521status_t AudioSystem::setPhoneState(int state)
522{
523 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
524 if (aps == 0) return PERMISSION_DENIED;
525
526 return aps->setPhoneState(state);
527}
528
529status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask)
530{
531 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
532 if (aps == 0) return PERMISSION_DENIED;
533 return aps->setRingerMode(mode, mask);
534}
535
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700536status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurenta553c252009-07-17 12:17:14 -0700537{
538 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
539 if (aps == 0) return PERMISSION_DENIED;
540 return aps->setForceUse(usage, config);
541}
542
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700543audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurenta553c252009-07-17 12:17:14 -0700544{
545 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700546 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurenta553c252009-07-17 12:17:14 -0700547 return aps->getForceUse(usage);
548}
549
550
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700551audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -0700552 uint32_t samplingRate,
553 uint32_t format,
554 uint32_t channels,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700555 audio_policy_output_flags_t flags)
Eric Laurenta553c252009-07-17 12:17:14 -0700556{
Eric Laurentddb78e72009-07-28 08:44:33 -0700557 audio_io_handle_t output = 0;
Eric Laurentef9500f2010-03-11 14:47:00 -0800558 // Do not use stream to output map cache if the direct output
559 // flag is set or if we are likely to use a direct output
560 // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to
561 // a direct output on some platforms).
562 // TODO: the output cache and stream to output mapping implementation needs to
563 // be reworked for proper operation with direct outputs. This code is too specific
564 // to the first use case we want to cover (Voice Recognition and Voice Dialer over
565 // Bluetooth SCO
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700566 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0 &&
567 ((stream != AUDIO_STREAM_VOICE_CALL && stream != AUDIO_STREAM_BLUETOOTH_SCO) ||
568 channels != AUDIO_CHANNEL_OUT_MONO ||
Eric Laurentef9500f2010-03-11 14:47:00 -0800569 (samplingRate != 8000 && samplingRate != 16000))) {
Eric Laurenta553c252009-07-17 12:17:14 -0700570 Mutex::Autolock _l(gLock);
571 output = AudioSystem::gStreamOutputMap.valueFor(stream);
Eric Laurentddb78e72009-07-28 08:44:33 -0700572 LOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700573 }
Eric Laurentddb78e72009-07-28 08:44:33 -0700574 if (output == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -0700575 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700576 if (aps == 0) return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700577 output = aps->getOutput(stream, samplingRate, format, channels, flags);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700578 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -0700579 Mutex::Autolock _l(gLock);
580 AudioSystem::gStreamOutputMap.add(stream, output);
581 }
582 }
583 return output;
584}
585
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700586status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700587 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700588 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700589{
590 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
591 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700592 return aps->startOutput(output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700593}
594
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700595status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700596 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700597 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700598{
599 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
600 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700601 return aps->stopOutput(output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700602}
603
604void AudioSystem::releaseOutput(audio_io_handle_t output)
605{
606 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
607 if (aps == 0) return;
608 aps->releaseOutput(output);
609}
610
611audio_io_handle_t AudioSystem::getInput(int inputSource,
612 uint32_t samplingRate,
613 uint32_t format,
614 uint32_t channels,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700615 audio_in_acoustics_t acoustics)
Eric Laurenta553c252009-07-17 12:17:14 -0700616{
617 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700618 if (aps == 0) return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700619 return aps->getInput(inputSource, samplingRate, format, channels, acoustics);
620}
621
622status_t AudioSystem::startInput(audio_io_handle_t input)
623{
624 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
625 if (aps == 0) return PERMISSION_DENIED;
626 return aps->startInput(input);
627}
628
629status_t AudioSystem::stopInput(audio_io_handle_t input)
630{
631 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
632 if (aps == 0) return PERMISSION_DENIED;
633 return aps->stopInput(input);
634}
635
636void AudioSystem::releaseInput(audio_io_handle_t input)
637{
638 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
639 if (aps == 0) return;
640 aps->releaseInput(input);
641}
642
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700643status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -0700644 int indexMin,
645 int indexMax)
646{
647 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
648 if (aps == 0) return PERMISSION_DENIED;
649 return aps->initStreamVolume(stream, indexMin, indexMax);
650}
651
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700652status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, int index)
Eric Laurenta553c252009-07-17 12:17:14 -0700653{
654 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
655 if (aps == 0) return PERMISSION_DENIED;
656 return aps->setStreamVolumeIndex(stream, index);
657}
658
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700659status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, int *index)
Eric Laurenta553c252009-07-17 12:17:14 -0700660{
661 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
662 if (aps == 0) return PERMISSION_DENIED;
663 return aps->getStreamVolumeIndex(stream, index);
664}
665
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700666uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700667{
668 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
669 if (aps == 0) return 0;
670 return aps->getStrategyForStream(stream);
671}
672
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700673uint32_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800674{
675 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
676 if (aps == 0) return 0;
677 return aps->getDevicesForStream(stream);
678}
679
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700680audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc)
681{
682 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
683 if (aps == 0) return PERMISSION_DENIED;
684 return aps->getOutputForEffect(desc);
685}
686
687status_t AudioSystem::registerEffect(effect_descriptor_t *desc,
688 audio_io_handle_t output,
689 uint32_t strategy,
690 int session,
691 int id)
692{
693 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
694 if (aps == 0) return PERMISSION_DENIED;
695 return aps->registerEffect(desc, output, strategy, session, id);
696}
697
698status_t AudioSystem::unregisterEffect(int id)
699{
700 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
701 if (aps == 0) return PERMISSION_DENIED;
702 return aps->unregisterEffect(id);
703}
704
Eric Laurent25101b02011-02-02 09:33:30 -0800705status_t AudioSystem::isStreamActive(int stream, bool* state, uint32_t inPastMs) {
706 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
707 if (aps == 0) return PERMISSION_DENIED;
708 *state = aps->isStreamActive(stream, inPastMs);
709 return NO_ERROR;
710}
711
712
Eric Laurenta553c252009-07-17 12:17:14 -0700713// ---------------------------------------------------------------------------
714
715void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
716 Mutex::Autolock _l(AudioSystem::gLock);
717 AudioSystem::gAudioPolicyService.clear();
718
719 LOGW("AudioPolicyService server died!");
720}
721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722}; // namespace android
723