blob: 61d0dad56aff2c018d13f48a3ae5c8724342541b [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041// Cached values for recording queries
42uint32_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;
59 LOGW("AudioFlinger not published, waiting...");
60 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 }
73 LOGE_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
Eric Laurentddb78e72009-07-28 08:44:33 -0700123status_t AudioSystem::setStreamVolume(int 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
132status_t AudioSystem::setStreamMute(int stream, bool mute)
133{
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
Eric Laurentddb78e72009-07-28 08:44:33 -0700141status_t AudioSystem::getStreamVolume(int 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
150status_t AudioSystem::getStreamMute(int stream, bool* mute)
151{
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
159status_t AudioSystem::setMode(int mode)
160{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700161 if (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
206status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType)
207{
Eric Laurenta553c252009-07-17 12:17:14 -0700208 OutputDescriptor *outputDesc;
209 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700211 if (streamType == AUDIO_STREAM_DEFAULT) {
212 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700213 }
214
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700215 output = getOutput((audio_stream_type_t)streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700216 if (output == 0) {
217 return PERMISSION_DENIED;
218 }
219
220 gLock.lock();
221 outputDesc = AudioSystem::gOutputs.valueFor(output);
222 if (outputDesc == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100223 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700224 gLock.unlock();
225 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
226 if (af == 0) return PERMISSION_DENIED;
227 *samplingRate = af->sampleRate(output);
228 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100229 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurenta553c252009-07-17 12:17:14 -0700230 *samplingRate = outputDesc->samplingRate;
231 gLock.unlock();
232 }
233
Steve Block71f2cf12011-10-20 11:56:00 +0100234 ALOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
Eric Laurenta553c252009-07-17 12:17:14 -0700235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 return NO_ERROR;
237}
238
239status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType)
240{
Eric Laurenta553c252009-07-17 12:17:14 -0700241 OutputDescriptor *outputDesc;
242 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700244 if (streamType == AUDIO_STREAM_DEFAULT) {
245 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700246 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700247
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700248 output = getOutput((audio_stream_type_t)streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700249 if (output == 0) {
250 return PERMISSION_DENIED;
251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
Eric Laurenta553c252009-07-17 12:17:14 -0700253 gLock.lock();
254 outputDesc = AudioSystem::gOutputs.valueFor(output);
255 if (outputDesc == 0) {
256 gLock.unlock();
257 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
258 if (af == 0) return PERMISSION_DENIED;
259 *frameCount = af->frameCount(output);
260 } else {
261 *frameCount = outputDesc->frameCount;
262 gLock.unlock();
263 }
264
Steve Block71f2cf12011-10-20 11:56:00 +0100265 ALOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 return NO_ERROR;
268}
269
270status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType)
271{
Eric Laurenta553c252009-07-17 12:17:14 -0700272 OutputDescriptor *outputDesc;
273 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700275 if (streamType == AUDIO_STREAM_DEFAULT) {
276 streamType = AUDIO_STREAM_MUSIC;
Eric Laurenta553c252009-07-17 12:17:14 -0700277 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700278
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700279 output = getOutput((audio_stream_type_t)streamType);
Eric Laurenta553c252009-07-17 12:17:14 -0700280 if (output == 0) {
281 return PERMISSION_DENIED;
282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
Eric Laurenta553c252009-07-17 12:17:14 -0700284 gLock.lock();
285 outputDesc = AudioSystem::gOutputs.valueFor(output);
286 if (outputDesc == 0) {
287 gLock.unlock();
288 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
289 if (af == 0) return PERMISSION_DENIED;
290 *latency = af->latency(output);
291 } else {
292 *latency = outputDesc->latency;
293 gLock.unlock();
294 }
295
Steve Block71f2cf12011-10-20 11:56:00 +0100296 ALOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
Eric Laurenta553c252009-07-17 12:17:14 -0700297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 return NO_ERROR;
299}
300
Eric Laurenta553c252009-07-17 12:17:14 -0700301status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 size_t* buffSize)
303{
304 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Eric Laurenta553c252009-07-17 12:17:14 -0700305 if ((gInBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 || (channelCount != gPrevInChannelCount)) {
307 // save the request params
308 gPrevInSamplingRate = sampleRate;
Eric Laurenta553c252009-07-17 12:17:14 -0700309 gPrevInFormat = format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 gPrevInChannelCount = channelCount;
311
312 gInBuffSize = 0;
313 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
314 if (af == 0) {
315 return PERMISSION_DENIED;
316 }
317 gInBuffSize = af->getInputBufferSize(sampleRate, format, channelCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 *buffSize = gInBuffSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 return NO_ERROR;
322}
323
Eric Laurent415f3e22009-10-21 08:14:22 -0700324status_t AudioSystem::setVoiceVolume(float value)
325{
326 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
327 if (af == 0) return PERMISSION_DENIED;
328 return af->setVoiceVolume(value);
329}
330
Eric Laurent0986e792010-01-19 17:37:09 -0800331status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int stream)
332{
333 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
334 if (af == 0) return PERMISSION_DENIED;
335
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700336 if (stream == AUDIO_STREAM_DEFAULT) {
337 stream = AUDIO_STREAM_MUSIC;
Eric Laurent0986e792010-01-19 17:37:09 -0800338 }
339
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700340 return af->getRenderPosition(halFrames, dspFrames, getOutput((audio_stream_type_t)stream));
Eric Laurent0986e792010-01-19 17:37:09 -0800341}
342
Eric Laurent47d0a922010-02-26 02:47:27 -0800343unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
344 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
345 unsigned int result = 0;
346 if (af == 0) return result;
Eric Laurentef9500f2010-03-11 14:47:00 -0800347 if (ioHandle == 0) return result;
Eric Laurent47d0a922010-02-26 02:47:27 -0800348
349 result = af->getInputFramesLost(ioHandle);
350 return result;
351}
352
Eric Laurent65b65452010-06-01 23:49:17 -0700353int AudioSystem::newAudioSessionId() {
354 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
355 if (af == 0) return 0;
356 return af->newAudioSessionId();
357}
358
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700359void AudioSystem::acquireAudioSessionId(int audioSession) {
360 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
361 if (af != 0) {
362 af->acquireAudioSessionId(audioSession);
363 }
364}
365
366void AudioSystem::releaseAudioSessionId(int audioSession) {
367 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
368 if (af != 0) {
369 af->releaseAudioSessionId(audioSession);
370 }
371}
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373// ---------------------------------------------------------------------------
374
Eric Laurenta553c252009-07-17 12:17:14 -0700375void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
Eric Laurenta553c252009-07-17 12:17:14 -0700378 AudioSystem::gAudioFlinger.clear();
Eric Laurent787aa592010-01-25 10:27:15 -0800379 // clear output handles and stream to output map caches
380 AudioSystem::gStreamOutputMap.clear();
381 AudioSystem::gOutputs.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382
383 if (gAudioErrorCallback) {
384 gAudioErrorCallback(DEAD_OBJECT);
385 }
386 LOGW("AudioFlinger server died!");
387}
388
Eric Laurentddb78e72009-07-28 08:44:33 -0700389void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) {
Steve Block71f2cf12011-10-20 11:56:00 +0100390 ALOGV("ioConfigChanged() event %d", event);
Eric Laurenta553c252009-07-17 12:17:14 -0700391 OutputDescriptor *desc;
392 uint32_t stream;
393
Eric Laurentddb78e72009-07-28 08:44:33 -0700394 if (ioHandle == 0) return;
Eric Laurenta553c252009-07-17 12:17:14 -0700395
396 Mutex::Autolock _l(AudioSystem::gLock);
397
398 switch (event) {
399 case STREAM_CONFIG_CHANGED:
400 if (param2 == 0) break;
401 stream = *(uint32_t *)param2;
Steve Block71f2cf12011-10-20 11:56:00 +0100402 ALOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700403 if (gStreamOutputMap.indexOfKey(stream) >= 0) {
404 gStreamOutputMap.replaceValueFor(stream, ioHandle);
405 }
406 break;
407 case OUTPUT_OPENED: {
408 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100409 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700410 break;
411 }
412 if (param2 == 0) break;
413 desc = (OutputDescriptor *)param2;
414
415 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
416 gOutputs.add(ioHandle, outputDesc);
Steve Block71f2cf12011-10-20 11:56:00 +0100417 ALOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700418 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
419 } break;
420 case OUTPUT_CLOSED: {
421 if (gOutputs.indexOfKey(ioHandle) < 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700422 LOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700423 break;
424 }
Steve Block71f2cf12011-10-20 11:56:00 +0100425 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700426
427 gOutputs.removeItem(ioHandle);
428 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) {
429 if (gStreamOutputMap.valueAt(i) == ioHandle) {
430 gStreamOutputMap.removeItemsAt(i);
431 }
432 }
433 } break;
434
435 case OUTPUT_CONFIG_CHANGED: {
436 int index = gOutputs.indexOfKey(ioHandle);
437 if (index < 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700438 LOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700439 break;
440 }
441 if (param2 == 0) break;
442 desc = (OutputDescriptor *)param2;
443
Steve Block71f2cf12011-10-20 11:56:00 +0100444 ALOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700445 ioHandle, desc->samplingRate, desc->format,
446 desc->channels, desc->frameCount, desc->latency);
447 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
448 delete outputDesc;
449 outputDesc = new OutputDescriptor(*desc);
450 gOutputs.replaceValueFor(ioHandle, outputDesc);
451 } break;
452 case INPUT_OPENED:
453 case INPUT_CLOSED:
454 case INPUT_CONFIG_CHANGED:
455 break;
456
457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458}
459
460void AudioSystem::setErrorCallback(audio_error_callback cb) {
Eric Laurenta553c252009-07-17 12:17:14 -0700461 Mutex::Autolock _l(gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 gAudioErrorCallback = cb;
463}
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465bool AudioSystem::routedToA2dpOutput(int streamType) {
466 switch(streamType) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700467 case AUDIO_STREAM_MUSIC:
468 case AUDIO_STREAM_VOICE_CALL:
469 case AUDIO_STREAM_BLUETOOTH_SCO:
470 case AUDIO_STREAM_SYSTEM:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 return true;
472 default:
473 return false;
474 }
475}
476
477
Eric Laurenta553c252009-07-17 12:17:14 -0700478// client singleton for AudioPolicyService binder interface
479sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
480sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
481
482
483// establish binder interface to AudioFlinger service
484const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
485{
486 gLock.lock();
487 if (gAudioPolicyService.get() == 0) {
488 sp<IServiceManager> sm = defaultServiceManager();
489 sp<IBinder> binder;
490 do {
491 binder = sm->getService(String16("media.audio_policy"));
492 if (binder != 0)
493 break;
494 LOGW("AudioPolicyService not published, waiting...");
495 usleep(500000); // 0.5 s
496 } while(true);
497 if (gAudioPolicyServiceClient == NULL) {
498 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
499 }
500 binder->linkToDeath(gAudioPolicyServiceClient);
501 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
502 gLock.unlock();
503 } else {
504 gLock.unlock();
505 }
506 return gAudioPolicyService;
507}
508
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700509status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
510 audio_policy_dev_state_t state,
511 const char *device_address)
Eric Laurenta553c252009-07-17 12:17:14 -0700512{
513 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentdca56b92011-09-02 14:20:56 -0700514 const char *address = "";
515
Eric Laurenta553c252009-07-17 12:17:14 -0700516 if (aps == 0) return PERMISSION_DENIED;
517
Eric Laurentdca56b92011-09-02 14:20:56 -0700518 if (device_address != NULL) {
519 address = device_address;
520 }
521
522 return aps->setDeviceConnectionState(device, state, address);
Eric Laurenta553c252009-07-17 12:17:14 -0700523}
524
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700525audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurenta553c252009-07-17 12:17:14 -0700526 const char *device_address)
527{
528 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700529 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta553c252009-07-17 12:17:14 -0700530
531 return aps->getDeviceConnectionState(device, device_address);
532}
533
534status_t AudioSystem::setPhoneState(int state)
535{
536 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
537 if (aps == 0) return PERMISSION_DENIED;
538
539 return aps->setPhoneState(state);
540}
541
542status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask)
543{
544 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
545 if (aps == 0) return PERMISSION_DENIED;
546 return aps->setRingerMode(mode, mask);
547}
548
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700549status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
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 return aps->setForceUse(usage, config);
554}
555
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700556audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurenta553c252009-07-17 12:17:14 -0700557{
558 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700559 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurenta553c252009-07-17 12:17:14 -0700560 return aps->getForceUse(usage);
561}
562
563
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700564audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -0700565 uint32_t samplingRate,
566 uint32_t format,
567 uint32_t channels,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700568 audio_policy_output_flags_t flags)
Eric Laurenta553c252009-07-17 12:17:14 -0700569{
Eric Laurentddb78e72009-07-28 08:44:33 -0700570 audio_io_handle_t output = 0;
Eric Laurentef9500f2010-03-11 14:47:00 -0800571 // Do not use stream to output map cache if the direct output
572 // flag is set or if we are likely to use a direct output
573 // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to
574 // a direct output on some platforms).
575 // TODO: the output cache and stream to output mapping implementation needs to
576 // be reworked for proper operation with direct outputs. This code is too specific
577 // to the first use case we want to cover (Voice Recognition and Voice Dialer over
578 // Bluetooth SCO
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700579 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0 &&
580 ((stream != AUDIO_STREAM_VOICE_CALL && stream != AUDIO_STREAM_BLUETOOTH_SCO) ||
581 channels != AUDIO_CHANNEL_OUT_MONO ||
Eric Laurentef9500f2010-03-11 14:47:00 -0800582 (samplingRate != 8000 && samplingRate != 16000))) {
Eric Laurenta553c252009-07-17 12:17:14 -0700583 Mutex::Autolock _l(gLock);
584 output = AudioSystem::gStreamOutputMap.valueFor(stream);
Steve Block71f2cf12011-10-20 11:56:00 +0100585 ALOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700586 }
Eric Laurentddb78e72009-07-28 08:44:33 -0700587 if (output == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -0700588 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700589 if (aps == 0) return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700590 output = aps->getOutput(stream, samplingRate, format, channels, flags);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700591 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -0700592 Mutex::Autolock _l(gLock);
593 AudioSystem::gStreamOutputMap.add(stream, output);
594 }
595 }
596 return output;
597}
598
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700599status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700600 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700601 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700602{
603 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
604 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700605 return aps->startOutput(output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700606}
607
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700608status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700609 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700610 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700611{
612 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
613 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700614 return aps->stopOutput(output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700615}
616
617void AudioSystem::releaseOutput(audio_io_handle_t output)
618{
619 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
620 if (aps == 0) return;
621 aps->releaseOutput(output);
622}
623
624audio_io_handle_t AudioSystem::getInput(int inputSource,
625 uint32_t samplingRate,
626 uint32_t format,
627 uint32_t channels,
Eric Laurent464d5b32011-06-17 21:29:58 -0700628 audio_in_acoustics_t acoustics,
629 int sessionId)
Eric Laurenta553c252009-07-17 12:17:14 -0700630{
631 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700632 if (aps == 0) return 0;
Eric Laurent464d5b32011-06-17 21:29:58 -0700633 return aps->getInput(inputSource, samplingRate, format, channels, acoustics, sessionId);
Eric Laurenta553c252009-07-17 12:17:14 -0700634}
635
636status_t AudioSystem::startInput(audio_io_handle_t input)
637{
638 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
639 if (aps == 0) return PERMISSION_DENIED;
640 return aps->startInput(input);
641}
642
643status_t AudioSystem::stopInput(audio_io_handle_t input)
644{
645 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
646 if (aps == 0) return PERMISSION_DENIED;
647 return aps->stopInput(input);
648}
649
650void AudioSystem::releaseInput(audio_io_handle_t input)
651{
652 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
653 if (aps == 0) return;
654 aps->releaseInput(input);
655}
656
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700657status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -0700658 int indexMin,
659 int indexMax)
660{
661 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
662 if (aps == 0) return PERMISSION_DENIED;
663 return aps->initStreamVolume(stream, indexMin, indexMax);
664}
665
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700666status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, int index)
Eric Laurenta553c252009-07-17 12:17:14 -0700667{
668 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
669 if (aps == 0) return PERMISSION_DENIED;
670 return aps->setStreamVolumeIndex(stream, index);
671}
672
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700673status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, int *index)
Eric Laurenta553c252009-07-17 12:17:14 -0700674{
675 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
676 if (aps == 0) return PERMISSION_DENIED;
677 return aps->getStreamVolumeIndex(stream, index);
678}
679
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700680uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700681{
682 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
683 if (aps == 0) return 0;
684 return aps->getStrategyForStream(stream);
685}
686
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700687uint32_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800688{
689 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
690 if (aps == 0) return 0;
691 return aps->getDevicesForStream(stream);
692}
693
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700694audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc)
695{
696 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
697 if (aps == 0) return PERMISSION_DENIED;
698 return aps->getOutputForEffect(desc);
699}
700
701status_t AudioSystem::registerEffect(effect_descriptor_t *desc,
Eric Laurent464d5b32011-06-17 21:29:58 -0700702 audio_io_handle_t io,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700703 uint32_t strategy,
704 int session,
705 int id)
706{
707 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
708 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent464d5b32011-06-17 21:29:58 -0700709 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700710}
711
712status_t AudioSystem::unregisterEffect(int id)
713{
714 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
715 if (aps == 0) return PERMISSION_DENIED;
716 return aps->unregisterEffect(id);
717}
718
Eric Laurent6752ec82011-08-10 10:37:50 -0700719status_t AudioSystem::setEffectEnabled(int id, bool enabled)
720{
721 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
722 if (aps == 0) return PERMISSION_DENIED;
723 return aps->setEffectEnabled(id, enabled);
724}
725
Eric Laurent464d5b32011-06-17 21:29:58 -0700726status_t AudioSystem::isStreamActive(int stream, bool* state, uint32_t inPastMs)
727{
Eric Laurent25101b02011-02-02 09:33:30 -0800728 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
729 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent464d5b32011-06-17 21:29:58 -0700730 if (state == NULL) return BAD_VALUE;
Eric Laurent25101b02011-02-02 09:33:30 -0800731 *state = aps->isStreamActive(stream, inPastMs);
732 return NO_ERROR;
733}
734
735
Eric Laurent05ce0942011-08-30 10:18:54 -0700736void AudioSystem::clearAudioConfigCache()
737{
738 Mutex::Autolock _l(gLock);
Steve Block71f2cf12011-10-20 11:56:00 +0100739 ALOGV("clearAudioConfigCache()");
Eric Laurent05ce0942011-08-30 10:18:54 -0700740 gStreamOutputMap.clear();
741 gOutputs.clear();
742}
743
Eric Laurenta553c252009-07-17 12:17:14 -0700744// ---------------------------------------------------------------------------
745
746void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
747 Mutex::Autolock _l(AudioSystem::gLock);
748 AudioSystem::gAudioPolicyService.clear();
749
750 LOGW("AudioPolicyService server died!");
751}
752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753}; // namespace android
754