blob: bbe4d8d1e8a6ff6e61aab75f1d442d6f73dc39b1 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-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 Agopian75624082009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <media/AudioSystem.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070023#include <media/IAudioPolicyService.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080024#include <math.h>
25
Dima Zavin64760242011-05-11 14:15:23 -070026#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070027
Eric Laurentc2f1f072009-07-17 12:17:14 -070028// ----------------------------------------------------------------------------
Eric Laurentc2f1f072009-07-17 12:17:14 -070029
The Android Open Source Project89fa4ad2009-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 Laurentc2f1f072009-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 Kastenf8c1a6f2012-01-10 09:01:19 -080041// Cached values for recording queries, all protected by gLock
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042uint32_t AudioSystem::gPrevInSamplingRate = 16000;
Dima Zavinfce7a472011-04-19 22:30:36 -070043int AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-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 Block5ff1dd52012-01-05 23:22:43 +000059 ALOGW("AudioFlinger not published, waiting...");
The Android Open Source Project89fa4ad2009-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 Project89fa4ad2009-03-03 19:31:44 -080072 }
Steve Block29357bc2012-01-06 19:20:56 +000073 ALOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
Eric Laurentc2f1f072009-07-17 12:17:14 -070074
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075 return gAudioFlinger;
76}
77
The Android Open Source Project89fa4ad2009-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 Kastenfff6d712012-01-12 16:38:12 -0800123status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, int output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124{
Dima Zavinfce7a472011-04-19 22:30:36 -0700125 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800126 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
127 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700128 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800129 return NO_ERROR;
130}
131
Glenn Kastenfff6d712012-01-12 16:38:12 -0800132status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800133{
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-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 Kastenfff6d712012-01-12 16:38:12 -0800141status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, int output)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142{
Dima Zavinfce7a472011-04-19 22:30:36 -0700143 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
145 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700146 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 return NO_ERROR;
148}
149
Glenn Kastenfff6d712012-01-12 16:38:12 -0800150status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151{
Dima Zavinfce7a472011-04-19 22:30:36 -0700152 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-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 Kastenf78aee72012-01-04 11:00:47 -0800159status_t AudioSystem::setMode(audio_mode_t mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160{
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800161 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-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 Laurentc2f1f072009-07-17 12:17:14 -0700167status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
169 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-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 Project89fa4ad2009-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 Blockb8a80522011-12-20 16:23:08 +0000193 // ALOGD("linearToLog(%d)=%f", volume, v);
The Android Open Source Project89fa4ad2009-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 Blockb8a80522011-12-20 16:23:08 +0000201 // ALOGD("logTolinear(%d)=%f", v, volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202 // return v;
203 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
204}
205
Glenn Kastenfff6d712012-01-12 16:38:12 -0800206status_t AudioSystem::getOutputSamplingRate(int* samplingRate, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700208 OutputDescriptor *outputDesc;
209 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210
Dima Zavinfce7a472011-04-19 22:30:36 -0700211 if (streamType == AUDIO_STREAM_DEFAULT) {
212 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700213 }
214
Glenn Kastenfff6d712012-01-12 16:38:12 -0800215 output = getOutput(streamType);
Eric Laurentc2f1f072009-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 Block3856b092011-10-20 11:56:00 +0100223 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-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 Block3856b092011-10-20 11:56:00 +0100229 ALOGV("getOutputSamplingRate() reading from output desc");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700230 *samplingRate = outputDesc->samplingRate;
231 gLock.unlock();
232 }
233
Steve Block3856b092011-10-20 11:56:00 +0100234 ALOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700235
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236 return NO_ERROR;
237}
238
Glenn Kastenfff6d712012-01-12 16:38:12 -0800239status_t AudioSystem::getOutputFrameCount(int* frameCount, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700241 OutputDescriptor *outputDesc;
242 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 if (streamType == AUDIO_STREAM_DEFAULT) {
245 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700246 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700247
Glenn Kastenfff6d712012-01-12 16:38:12 -0800248 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249 if (output == 0) {
250 return PERMISSION_DENIED;
251 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252
Eric Laurentc2f1f072009-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 Block3856b092011-10-20 11:56:00 +0100265 ALOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700266
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267 return NO_ERROR;
268}
269
Glenn Kastenfff6d712012-01-12 16:38:12 -0800270status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272 OutputDescriptor *outputDesc;
273 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 if (streamType == AUDIO_STREAM_DEFAULT) {
276 streamType = AUDIO_STREAM_MUSIC;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700277 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700278
Glenn Kastenfff6d712012-01-12 16:38:12 -0800279 output = getOutput(streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280 if (output == 0) {
281 return PERMISSION_DENIED;
282 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283
Eric Laurentc2f1f072009-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 Block3856b092011-10-20 11:56:00 +0100296 ALOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700297
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 return NO_ERROR;
299}
300
Eric Laurentc2f1f072009-07-17 12:17:14 -0700301status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302 size_t* buffSize)
303{
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800304 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800306 size_t inBuffSize = gInBuffSize;
307 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 || (channelCount != gPrevInChannelCount)) {
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800309 gLock.unlock();
310 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
311 if (af == 0) {
312 return PERMISSION_DENIED;
313 }
314 inBuffSize = af->getInputBufferSize(sampleRate, format, channelCount);
315 gLock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 // save the request params
317 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700318 gPrevInFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 gPrevInChannelCount = channelCount;
320
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800321 gInBuffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700322 }
Glenn Kastenf8c1a6f2012-01-10 09:01:19 -0800323 gLock.unlock();
324 *buffSize = inBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700325
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326 return NO_ERROR;
327}
328
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700329status_t AudioSystem::setVoiceVolume(float value)
330{
331 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
332 if (af == 0) return PERMISSION_DENIED;
333 return af->setVoiceVolume(value);
334}
335
Glenn Kastenfff6d712012-01-12 16:38:12 -0800336status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, audio_stream_type_t stream)
Eric Laurent342e9cf2010-01-19 17:37:09 -0800337{
338 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
339 if (af == 0) return PERMISSION_DENIED;
340
Dima Zavinfce7a472011-04-19 22:30:36 -0700341 if (stream == AUDIO_STREAM_DEFAULT) {
342 stream = AUDIO_STREAM_MUSIC;
Eric Laurent342e9cf2010-01-19 17:37:09 -0800343 }
344
Glenn Kastenfff6d712012-01-12 16:38:12 -0800345 return af->getRenderPosition(halFrames, dspFrames, getOutput(stream));
Eric Laurent342e9cf2010-01-19 17:37:09 -0800346}
347
Eric Laurent05bca2f2010-02-26 02:47:27 -0800348unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
349 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
350 unsigned int result = 0;
351 if (af == 0) return result;
Eric Laurentbe55a2d2010-03-11 14:47:00 -0800352 if (ioHandle == 0) return result;
Eric Laurent05bca2f2010-02-26 02:47:27 -0800353
354 result = af->getInputFramesLost(ioHandle);
355 return result;
356}
357
Eric Laurentbe916aa2010-06-01 23:49:17 -0700358int AudioSystem::newAudioSessionId() {
359 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
360 if (af == 0) return 0;
361 return af->newAudioSessionId();
362}
363
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700364void AudioSystem::acquireAudioSessionId(int audioSession) {
365 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
366 if (af != 0) {
367 af->acquireAudioSessionId(audioSession);
368 }
369}
370
371void AudioSystem::releaseAudioSessionId(int audioSession) {
372 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
373 if (af != 0) {
374 af->releaseAudioSessionId(audioSession);
375 }
376}
377
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378// ---------------------------------------------------------------------------
379
Eric Laurentc2f1f072009-07-17 12:17:14 -0700380void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382
Eric Laurentc2f1f072009-07-17 12:17:14 -0700383 AudioSystem::gAudioFlinger.clear();
Eric Laurent0ef583f2010-01-25 10:27:15 -0800384 // clear output handles and stream to output map caches
385 AudioSystem::gStreamOutputMap.clear();
386 AudioSystem::gOutputs.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387
388 if (gAudioErrorCallback) {
389 gAudioErrorCallback(DEAD_OBJECT);
390 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000391 ALOGW("AudioFlinger server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392}
393
Eric Laurentfa2877b2009-07-28 08:44:33 -0700394void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) {
Steve Block3856b092011-10-20 11:56:00 +0100395 ALOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700396 OutputDescriptor *desc;
397 uint32_t stream;
398
Eric Laurentfa2877b2009-07-28 08:44:33 -0700399 if (ioHandle == 0) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700400
401 Mutex::Autolock _l(AudioSystem::gLock);
402
403 switch (event) {
404 case STREAM_CONFIG_CHANGED:
405 if (param2 == 0) break;
406 stream = *(uint32_t *)param2;
Steve Block3856b092011-10-20 11:56:00 +0100407 ALOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700408 if (gStreamOutputMap.indexOfKey(stream) >= 0) {
409 gStreamOutputMap.replaceValueFor(stream, ioHandle);
410 }
411 break;
412 case OUTPUT_OPENED: {
413 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Steve Block3856b092011-10-20 11:56:00 +0100414 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700415 break;
416 }
417 if (param2 == 0) break;
418 desc = (OutputDescriptor *)param2;
419
420 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
421 gOutputs.add(ioHandle, outputDesc);
Steve Block3856b092011-10-20 11:56:00 +0100422 ALOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700423 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
424 } break;
425 case OUTPUT_CLOSED: {
426 if (gOutputs.indexOfKey(ioHandle) < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000427 ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700428 break;
429 }
Steve Block3856b092011-10-20 11:56:00 +0100430 ALOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700431
432 gOutputs.removeItem(ioHandle);
433 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) {
434 if (gStreamOutputMap.valueAt(i) == ioHandle) {
435 gStreamOutputMap.removeItemsAt(i);
436 }
437 }
438 } break;
439
440 case OUTPUT_CONFIG_CHANGED: {
441 int index = gOutputs.indexOfKey(ioHandle);
442 if (index < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000443 ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700444 break;
445 }
446 if (param2 == 0) break;
447 desc = (OutputDescriptor *)param2;
448
Steve Block3856b092011-10-20 11:56:00 +0100449 ALOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700450 ioHandle, desc->samplingRate, desc->format,
451 desc->channels, desc->frameCount, desc->latency);
452 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
453 delete outputDesc;
454 outputDesc = new OutputDescriptor(*desc);
455 gOutputs.replaceValueFor(ioHandle, outputDesc);
456 } break;
457 case INPUT_OPENED:
458 case INPUT_CLOSED:
459 case INPUT_CONFIG_CHANGED:
460 break;
461
462 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463}
464
465void AudioSystem::setErrorCallback(audio_error_callback cb) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700466 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467 gAudioErrorCallback = cb;
468}
469
Glenn Kastenfff6d712012-01-12 16:38:12 -0800470bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 switch(streamType) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700472 case AUDIO_STREAM_MUSIC:
473 case AUDIO_STREAM_VOICE_CALL:
474 case AUDIO_STREAM_BLUETOOTH_SCO:
475 case AUDIO_STREAM_SYSTEM:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 return true;
477 default:
478 return false;
479 }
480}
481
482
Eric Laurentc2f1f072009-07-17 12:17:14 -0700483// client singleton for AudioPolicyService binder interface
484sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
485sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
486
487
488// establish binder interface to AudioFlinger service
489const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
490{
491 gLock.lock();
492 if (gAudioPolicyService.get() == 0) {
493 sp<IServiceManager> sm = defaultServiceManager();
494 sp<IBinder> binder;
495 do {
496 binder = sm->getService(String16("media.audio_policy"));
497 if (binder != 0)
498 break;
Steve Block5ff1dd52012-01-05 23:22:43 +0000499 ALOGW("AudioPolicyService not published, waiting...");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700500 usleep(500000); // 0.5 s
501 } while(true);
502 if (gAudioPolicyServiceClient == NULL) {
503 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
504 }
505 binder->linkToDeath(gAudioPolicyServiceClient);
506 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
507 gLock.unlock();
508 } else {
509 gLock.unlock();
510 }
511 return gAudioPolicyService;
512}
513
Dima Zavinfce7a472011-04-19 22:30:36 -0700514status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
515 audio_policy_dev_state_t state,
516 const char *device_address)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700517{
518 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurent71b63e32011-09-02 14:20:56 -0700519 const char *address = "";
520
Eric Laurentc2f1f072009-07-17 12:17:14 -0700521 if (aps == 0) return PERMISSION_DENIED;
522
Eric Laurent71b63e32011-09-02 14:20:56 -0700523 if (device_address != NULL) {
524 address = device_address;
525 }
526
527 return aps->setDeviceConnectionState(device, state, address);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700528}
529
Dima Zavinfce7a472011-04-19 22:30:36 -0700530audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700531 const char *device_address)
532{
533 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700534 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700535
536 return aps->getDeviceConnectionState(device, device_address);
537}
538
Glenn Kastenf78aee72012-01-04 11:00:47 -0800539status_t AudioSystem::setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700540{
Glenn Kasten347966c2012-01-18 14:58:32 -0800541 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700542 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
543 if (aps == 0) return PERMISSION_DENIED;
544
545 return aps->setPhoneState(state);
546}
547
548status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask)
549{
550 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
551 if (aps == 0) return PERMISSION_DENIED;
552 return aps->setRingerMode(mode, mask);
553}
554
Dima Zavinfce7a472011-04-19 22:30:36 -0700555status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700556{
557 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
558 if (aps == 0) return PERMISSION_DENIED;
559 return aps->setForceUse(usage, config);
560}
561
Dima Zavinfce7a472011-04-19 22:30:36 -0700562audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700563{
564 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Dima Zavinfce7a472011-04-19 22:30:36 -0700565 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700566 return aps->getForceUse(usage);
567}
568
569
Dima Zavinfce7a472011-04-19 22:30:36 -0700570audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700571 uint32_t samplingRate,
572 uint32_t format,
573 uint32_t channels,
Dima Zavinfce7a472011-04-19 22:30:36 -0700574 audio_policy_output_flags_t flags)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700575{
Eric Laurentfa2877b2009-07-28 08:44:33 -0700576 audio_io_handle_t output = 0;
Eric Laurentbe55a2d2010-03-11 14:47:00 -0800577 // Do not use stream to output map cache if the direct output
578 // flag is set or if we are likely to use a direct output
579 // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to
580 // a direct output on some platforms).
581 // TODO: the output cache and stream to output mapping implementation needs to
582 // be reworked for proper operation with direct outputs. This code is too specific
583 // to the first use case we want to cover (Voice Recognition and Voice Dialer over
584 // Bluetooth SCO
Dima Zavinfce7a472011-04-19 22:30:36 -0700585 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0 &&
586 ((stream != AUDIO_STREAM_VOICE_CALL && stream != AUDIO_STREAM_BLUETOOTH_SCO) ||
587 channels != AUDIO_CHANNEL_OUT_MONO ||
Eric Laurentbe55a2d2010-03-11 14:47:00 -0800588 (samplingRate != 8000 && samplingRate != 16000))) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700589 Mutex::Autolock _l(gLock);
590 output = AudioSystem::gStreamOutputMap.valueFor(stream);
Steve Block3856b092011-10-20 11:56:00 +0100591 ALOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700592 }
Eric Laurentfa2877b2009-07-28 08:44:33 -0700593 if (output == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700594 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700595 if (aps == 0) return 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700596 output = aps->getOutput(stream, samplingRate, format, channels, flags);
Dima Zavinfce7a472011-04-19 22:30:36 -0700597 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700598 Mutex::Autolock _l(gLock);
599 AudioSystem::gStreamOutputMap.add(stream, output);
600 }
601 }
602 return output;
603}
604
Eric Laurentde070132010-07-13 04:45:46 -0700605status_t AudioSystem::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700606 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700607 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700608{
609 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
610 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700611 return aps->startOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700612}
613
Eric Laurentde070132010-07-13 04:45:46 -0700614status_t AudioSystem::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700615 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700616 int session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700617{
618 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
619 if (aps == 0) return PERMISSION_DENIED;
Eric Laurentde070132010-07-13 04:45:46 -0700620 return aps->stopOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700621}
622
623void AudioSystem::releaseOutput(audio_io_handle_t output)
624{
625 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
626 if (aps == 0) return;
627 aps->releaseOutput(output);
628}
629
630audio_io_handle_t AudioSystem::getInput(int inputSource,
631 uint32_t samplingRate,
632 uint32_t format,
633 uint32_t channels,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700634 audio_in_acoustics_t acoustics,
635 int sessionId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700636{
637 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700638 if (aps == 0) return 0;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700639 return aps->getInput(inputSource, samplingRate, format, channels, acoustics, sessionId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700640}
641
642status_t AudioSystem::startInput(audio_io_handle_t input)
643{
644 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
645 if (aps == 0) return PERMISSION_DENIED;
646 return aps->startInput(input);
647}
648
649status_t AudioSystem::stopInput(audio_io_handle_t input)
650{
651 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
652 if (aps == 0) return PERMISSION_DENIED;
653 return aps->stopInput(input);
654}
655
656void AudioSystem::releaseInput(audio_io_handle_t input)
657{
658 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
659 if (aps == 0) return;
660 aps->releaseInput(input);
661}
662
Dima Zavinfce7a472011-04-19 22:30:36 -0700663status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700664 int indexMin,
665 int indexMax)
666{
667 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
668 if (aps == 0) return PERMISSION_DENIED;
669 return aps->initStreamVolume(stream, indexMin, indexMax);
670}
671
Eric Laurent83844cc2011-11-18 16:43:31 -0800672status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
673 int index,
674 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700675{
676 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
677 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800678 return aps->setStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700679}
680
Eric Laurent83844cc2011-11-18 16:43:31 -0800681status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
682 int *index,
683 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700684{
685 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
686 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent83844cc2011-11-18 16:43:31 -0800687 return aps->getStreamVolumeIndex(stream, index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700688}
689
Dima Zavinfce7a472011-04-19 22:30:36 -0700690uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700691{
692 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
693 if (aps == 0) return 0;
694 return aps->getStrategyForStream(stream);
695}
696
Dima Zavinfce7a472011-04-19 22:30:36 -0700697uint32_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800698{
699 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
700 if (aps == 0) return 0;
701 return aps->getDevicesForStream(stream);
702}
703
Eric Laurentde070132010-07-13 04:45:46 -0700704audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc)
705{
706 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
707 if (aps == 0) return PERMISSION_DENIED;
708 return aps->getOutputForEffect(desc);
709}
710
711status_t AudioSystem::registerEffect(effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700712 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700713 uint32_t strategy,
714 int session,
715 int id)
716{
717 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
718 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700719 return aps->registerEffect(desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700720}
721
722status_t AudioSystem::unregisterEffect(int id)
723{
724 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
725 if (aps == 0) return PERMISSION_DENIED;
726 return aps->unregisterEffect(id);
727}
728
Eric Laurentdb7c0792011-08-10 10:37:50 -0700729status_t AudioSystem::setEffectEnabled(int id, bool enabled)
730{
731 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
732 if (aps == 0) return PERMISSION_DENIED;
733 return aps->setEffectEnabled(id, enabled);
734}
735
Glenn Kastenfff6d712012-01-12 16:38:12 -0800736status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700737{
Eric Laurenteda6c362011-02-02 09:33:30 -0800738 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
739 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700740 if (state == NULL) return BAD_VALUE;
Eric Laurenteda6c362011-02-02 09:33:30 -0800741 *state = aps->isStreamActive(stream, inPastMs);
742 return NO_ERROR;
743}
744
745
Eric Laurent9f6530f2011-08-30 10:18:54 -0700746void AudioSystem::clearAudioConfigCache()
747{
748 Mutex::Autolock _l(gLock);
Steve Block3856b092011-10-20 11:56:00 +0100749 ALOGV("clearAudioConfigCache()");
Eric Laurent9f6530f2011-08-30 10:18:54 -0700750 gStreamOutputMap.clear();
751 gOutputs.clear();
752}
753
Eric Laurentc2f1f072009-07-17 12:17:14 -0700754// ---------------------------------------------------------------------------
755
756void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
757 Mutex::Autolock _l(AudioSystem::gLock);
758 AudioSystem::gAudioPolicyService.clear();
759
Steve Block5ff1dd52012-01-05 23:22:43 +0000760 ALOGW("AudioPolicyService server died!");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700761}
762
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800763}; // namespace android
764