blob: 2f694ba59cba921dfe539c419f639d1b8141b671 [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
Eric Laurenta553c252009-07-17 12:17:14 -070026// ----------------------------------------------------------------------------
27// the sim build doesn't have gettid
28
29#ifndef HAVE_GETTID
30# define gettid getpid
31#endif
32
33// ----------------------------------------------------------------------------
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035namespace android {
36
37// client singleton for AudioFlinger binder interface
38Mutex AudioSystem::gLock;
39sp<IAudioFlinger> AudioSystem::gAudioFlinger;
40sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
41audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
42// Cached values
Eric Laurenta553c252009-07-17 12:17:14 -070043DefaultKeyedVector<int, audio_io_handle_t> AudioSystem::gStreamOutputMap(0);
44DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0);
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046// Cached values for recording queries
47uint32_t AudioSystem::gPrevInSamplingRate = 16000;
48int AudioSystem::gPrevInFormat = AudioSystem::PCM_16_BIT;
49int AudioSystem::gPrevInChannelCount = 1;
50size_t AudioSystem::gInBuffSize = 0;
51
52
53// establish binder interface to AudioFlinger service
54const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
55{
56 Mutex::Autolock _l(gLock);
57 if (gAudioFlinger.get() == 0) {
58 sp<IServiceManager> sm = defaultServiceManager();
59 sp<IBinder> binder;
60 do {
61 binder = sm->getService(String16("media.audio_flinger"));
62 if (binder != 0)
63 break;
64 LOGW("AudioFlinger not published, waiting...");
65 usleep(500000); // 0.5 s
66 } while(true);
67 if (gAudioFlingerClient == NULL) {
68 gAudioFlingerClient = new AudioFlingerClient();
69 } else {
70 if (gAudioErrorCallback) {
71 gAudioErrorCallback(NO_ERROR);
72 }
73 }
74 binder->linkToDeath(gAudioFlingerClient);
75 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
76 gAudioFlinger->registerClient(gAudioFlingerClient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 }
78 LOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
Eric Laurenta553c252009-07-17 12:17:14 -070079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 return gAudioFlinger;
81}
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083status_t AudioSystem::muteMicrophone(bool state) {
84 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
85 if (af == 0) return PERMISSION_DENIED;
86 return af->setMicMute(state);
87}
88
89status_t AudioSystem::isMicrophoneMuted(bool* state) {
90 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
91 if (af == 0) return PERMISSION_DENIED;
92 *state = af->getMicMute();
93 return NO_ERROR;
94}
95
96status_t AudioSystem::setMasterVolume(float value)
97{
98 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
99 if (af == 0) return PERMISSION_DENIED;
100 af->setMasterVolume(value);
101 return NO_ERROR;
102}
103
104status_t AudioSystem::setMasterMute(bool mute)
105{
106 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
107 if (af == 0) return PERMISSION_DENIED;
108 af->setMasterMute(mute);
109 return NO_ERROR;
110}
111
112status_t AudioSystem::getMasterVolume(float* volume)
113{
114 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
115 if (af == 0) return PERMISSION_DENIED;
116 *volume = af->masterVolume();
117 return NO_ERROR;
118}
119
120status_t AudioSystem::getMasterMute(bool* mute)
121{
122 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
123 if (af == 0) return PERMISSION_DENIED;
124 *mute = af->masterMute();
125 return NO_ERROR;
126}
127
Eric Laurentddb78e72009-07-28 08:44:33 -0700128status_t AudioSystem::setStreamVolume(int stream, float value, int output)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129{
130 if (uint32_t(stream) >= NUM_STREAM_TYPES) return BAD_VALUE;
131 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
132 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700133 af->setStreamVolume(stream, value, output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 return NO_ERROR;
135}
136
137status_t AudioSystem::setStreamMute(int stream, bool mute)
138{
139 if (uint32_t(stream) >= NUM_STREAM_TYPES) return BAD_VALUE;
140 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
141 if (af == 0) return PERMISSION_DENIED;
142 af->setStreamMute(stream, mute);
143 return NO_ERROR;
144}
145
Eric Laurentddb78e72009-07-28 08:44:33 -0700146status_t AudioSystem::getStreamVolume(int stream, float* volume, int output)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147{
148 if (uint32_t(stream) >= NUM_STREAM_TYPES) return BAD_VALUE;
149 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
150 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700151 *volume = af->streamVolume(stream, output);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 return NO_ERROR;
153}
154
155status_t AudioSystem::getStreamMute(int stream, bool* mute)
156{
157 if (uint32_t(stream) >= NUM_STREAM_TYPES) return BAD_VALUE;
158 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
159 if (af == 0) return PERMISSION_DENIED;
160 *mute = af->streamMute(stream);
161 return NO_ERROR;
162}
163
164status_t AudioSystem::setMode(int mode)
165{
166 if (mode >= NUM_MODES) return BAD_VALUE;
167 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
168 if (af == 0) return PERMISSION_DENIED;
169 return af->setMode(mode);
170}
171
Eric Laurenta553c252009-07-17 12:17:14 -0700172status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
174 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700175 return af->setParameters(ioHandle, keyValuePairs);
176}
177
178String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
179 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
180 String8 result = String8("");
181 if (af == 0) return result;
182
183 result = af->getParameters(ioHandle, keys);
184 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185}
186
187// convert volume steps to natural log scale
188
189// change this value to change volume scaling
190static const float dBPerStep = 0.5f;
191// shouldn't need to touch these
192static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
193static const float dBConvertInverse = 1.0f / dBConvert;
194
195float AudioSystem::linearToLog(int volume)
196{
197 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
198 // LOGD("linearToLog(%d)=%f", volume, v);
199 // return v;
200 return volume ? exp(float(100 - volume) * dBConvert) : 0;
201}
202
203int AudioSystem::logToLinear(float volume)
204{
205 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
206 // LOGD("logTolinear(%d)=%f", v, volume);
207 // return v;
208 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
209}
210
211status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType)
212{
Eric Laurenta553c252009-07-17 12:17:14 -0700213 OutputDescriptor *outputDesc;
214 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
Eric Laurenta553c252009-07-17 12:17:14 -0700216 if (streamType == DEFAULT) {
217 streamType = MUSIC;
218 }
219
220 output = getOutput((stream_type)streamType);
221 if (output == 0) {
222 return PERMISSION_DENIED;
223 }
224
225 gLock.lock();
226 outputDesc = AudioSystem::gOutputs.valueFor(output);
227 if (outputDesc == 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700228 LOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700229 gLock.unlock();
230 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
231 if (af == 0) return PERMISSION_DENIED;
232 *samplingRate = af->sampleRate(output);
233 } else {
234 LOGV("getOutputSamplingRate() reading from output desc");
235 *samplingRate = outputDesc->samplingRate;
236 gLock.unlock();
237 }
238
Eric Laurentddb78e72009-07-28 08:44:33 -0700239 LOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
Eric Laurenta553c252009-07-17 12:17:14 -0700240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 return NO_ERROR;
242}
243
244status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType)
245{
Eric Laurenta553c252009-07-17 12:17:14 -0700246 OutputDescriptor *outputDesc;
247 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Eric Laurenta553c252009-07-17 12:17:14 -0700249 if (streamType == DEFAULT) {
250 streamType = MUSIC;
251 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700252
Eric Laurenta553c252009-07-17 12:17:14 -0700253 output = getOutput((stream_type)streamType);
254 if (output == 0) {
255 return PERMISSION_DENIED;
256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Eric Laurenta553c252009-07-17 12:17:14 -0700258 gLock.lock();
259 outputDesc = AudioSystem::gOutputs.valueFor(output);
260 if (outputDesc == 0) {
261 gLock.unlock();
262 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
263 if (af == 0) return PERMISSION_DENIED;
264 *frameCount = af->frameCount(output);
265 } else {
266 *frameCount = outputDesc->frameCount;
267 gLock.unlock();
268 }
269
Eric Laurentddb78e72009-07-28 08:44:33 -0700270 LOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 return NO_ERROR;
273}
274
275status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType)
276{
Eric Laurenta553c252009-07-17 12:17:14 -0700277 OutputDescriptor *outputDesc;
278 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279
Eric Laurenta553c252009-07-17 12:17:14 -0700280 if (streamType == DEFAULT) {
281 streamType = MUSIC;
282 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700283
Eric Laurenta553c252009-07-17 12:17:14 -0700284 output = getOutput((stream_type)streamType);
285 if (output == 0) {
286 return PERMISSION_DENIED;
287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
Eric Laurenta553c252009-07-17 12:17:14 -0700289 gLock.lock();
290 outputDesc = AudioSystem::gOutputs.valueFor(output);
291 if (outputDesc == 0) {
292 gLock.unlock();
293 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
294 if (af == 0) return PERMISSION_DENIED;
295 *latency = af->latency(output);
296 } else {
297 *latency = outputDesc->latency;
298 gLock.unlock();
299 }
300
Eric Laurentddb78e72009-07-28 08:44:33 -0700301 LOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
Eric Laurenta553c252009-07-17 12:17:14 -0700302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 return NO_ERROR;
304}
305
Eric Laurenta553c252009-07-17 12:17:14 -0700306status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 size_t* buffSize)
308{
309 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Eric Laurenta553c252009-07-17 12:17:14 -0700310 if ((gInBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 || (channelCount != gPrevInChannelCount)) {
312 // save the request params
313 gPrevInSamplingRate = sampleRate;
Eric Laurenta553c252009-07-17 12:17:14 -0700314 gPrevInFormat = format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 gPrevInChannelCount = channelCount;
316
317 gInBuffSize = 0;
318 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
319 if (af == 0) {
320 return PERMISSION_DENIED;
321 }
322 gInBuffSize = af->getInputBufferSize(sampleRate, format, channelCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 *buffSize = gInBuffSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 return NO_ERROR;
327}
328
Eric Laurent415f3e22009-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
Eric Laurent0986e792010-01-19 17:37:09 -0800336status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int stream)
337{
338 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
339 if (af == 0) return PERMISSION_DENIED;
340
341 if (stream == DEFAULT) {
342 stream = MUSIC;
343 }
344
345 return af->getRenderPosition(halFrames, dspFrames, getOutput((stream_type)stream));
346}
347
Eric Laurent47d0a922010-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 Laurentef9500f2010-03-11 14:47:00 -0800352 if (ioHandle == 0) return result;
Eric Laurent47d0a922010-02-26 02:47:27 -0800353
354 result = af->getInputFramesLost(ioHandle);
355 return result;
356}
357
Eric Laurent65b65452010-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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364// ---------------------------------------------------------------------------
365
Eric Laurenta553c252009-07-17 12:17:14 -0700366void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368
Eric Laurenta553c252009-07-17 12:17:14 -0700369 AudioSystem::gAudioFlinger.clear();
Eric Laurent787aa592010-01-25 10:27:15 -0800370 // clear output handles and stream to output map caches
371 AudioSystem::gStreamOutputMap.clear();
372 AudioSystem::gOutputs.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373
374 if (gAudioErrorCallback) {
375 gAudioErrorCallback(DEAD_OBJECT);
376 }
377 LOGW("AudioFlinger server died!");
378}
379
Eric Laurentddb78e72009-07-28 08:44:33 -0700380void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) {
Eric Laurenta553c252009-07-17 12:17:14 -0700381 LOGV("ioConfigChanged() event %d", event);
Eric Laurenta553c252009-07-17 12:17:14 -0700382 OutputDescriptor *desc;
383 uint32_t stream;
384
Eric Laurentddb78e72009-07-28 08:44:33 -0700385 if (ioHandle == 0) return;
Eric Laurenta553c252009-07-17 12:17:14 -0700386
387 Mutex::Autolock _l(AudioSystem::gLock);
388
389 switch (event) {
390 case STREAM_CONFIG_CHANGED:
391 if (param2 == 0) break;
392 stream = *(uint32_t *)param2;
Eric Laurentddb78e72009-07-28 08:44:33 -0700393 LOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700394 if (gStreamOutputMap.indexOfKey(stream) >= 0) {
395 gStreamOutputMap.replaceValueFor(stream, ioHandle);
396 }
397 break;
398 case OUTPUT_OPENED: {
399 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700400 LOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700401 break;
402 }
403 if (param2 == 0) break;
404 desc = (OutputDescriptor *)param2;
405
406 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
407 gOutputs.add(ioHandle, outputDesc);
408 LOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d",
409 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
410 } break;
411 case OUTPUT_CLOSED: {
412 if (gOutputs.indexOfKey(ioHandle) < 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700413 LOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700414 break;
415 }
Eric Laurentddb78e72009-07-28 08:44:33 -0700416 LOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700417
418 gOutputs.removeItem(ioHandle);
419 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) {
420 if (gStreamOutputMap.valueAt(i) == ioHandle) {
421 gStreamOutputMap.removeItemsAt(i);
422 }
423 }
424 } break;
425
426 case OUTPUT_CONFIG_CHANGED: {
427 int index = gOutputs.indexOfKey(ioHandle);
428 if (index < 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700429 LOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700430 break;
431 }
432 if (param2 == 0) break;
433 desc = (OutputDescriptor *)param2;
434
Eric Laurentddb78e72009-07-28 08:44:33 -0700435 LOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700436 ioHandle, desc->samplingRate, desc->format,
437 desc->channels, desc->frameCount, desc->latency);
438 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
439 delete outputDesc;
440 outputDesc = new OutputDescriptor(*desc);
441 gOutputs.replaceValueFor(ioHandle, outputDesc);
442 } break;
443 case INPUT_OPENED:
444 case INPUT_CLOSED:
445 case INPUT_CONFIG_CHANGED:
446 break;
447
448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449}
450
451void AudioSystem::setErrorCallback(audio_error_callback cb) {
Eric Laurenta553c252009-07-17 12:17:14 -0700452 Mutex::Autolock _l(gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 gAudioErrorCallback = cb;
454}
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456bool AudioSystem::routedToA2dpOutput(int streamType) {
457 switch(streamType) {
458 case MUSIC:
459 case VOICE_CALL:
460 case BLUETOOTH_SCO:
461 case SYSTEM:
462 return true;
463 default:
464 return false;
465 }
466}
467
468
Eric Laurenta553c252009-07-17 12:17:14 -0700469// client singleton for AudioPolicyService binder interface
470sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
471sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
472
473
474// establish binder interface to AudioFlinger service
475const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
476{
477 gLock.lock();
478 if (gAudioPolicyService.get() == 0) {
479 sp<IServiceManager> sm = defaultServiceManager();
480 sp<IBinder> binder;
481 do {
482 binder = sm->getService(String16("media.audio_policy"));
483 if (binder != 0)
484 break;
485 LOGW("AudioPolicyService not published, waiting...");
486 usleep(500000); // 0.5 s
487 } while(true);
488 if (gAudioPolicyServiceClient == NULL) {
489 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
490 }
491 binder->linkToDeath(gAudioPolicyServiceClient);
492 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
493 gLock.unlock();
494 } else {
495 gLock.unlock();
496 }
497 return gAudioPolicyService;
498}
499
500status_t AudioSystem::setDeviceConnectionState(audio_devices device,
501 device_connection_state state,
502 const char *device_address)
503{
504 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
505 if (aps == 0) return PERMISSION_DENIED;
506
507 return aps->setDeviceConnectionState(device, state, device_address);
508}
509
510AudioSystem::device_connection_state AudioSystem::getDeviceConnectionState(audio_devices device,
511 const char *device_address)
512{
513 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
514 if (aps == 0) return DEVICE_STATE_UNAVAILABLE;
515
516 return aps->getDeviceConnectionState(device, device_address);
517}
518
519status_t AudioSystem::setPhoneState(int state)
520{
521 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
522 if (aps == 0) return PERMISSION_DENIED;
523
524 return aps->setPhoneState(state);
525}
526
527status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask)
528{
529 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
530 if (aps == 0) return PERMISSION_DENIED;
531 return aps->setRingerMode(mode, mask);
532}
533
534status_t AudioSystem::setForceUse(force_use usage, forced_config config)
535{
536 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
537 if (aps == 0) return PERMISSION_DENIED;
538 return aps->setForceUse(usage, config);
539}
540
541AudioSystem::forced_config AudioSystem::getForceUse(force_use usage)
542{
543 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
544 if (aps == 0) return FORCE_NONE;
545 return aps->getForceUse(usage);
546}
547
548
549audio_io_handle_t AudioSystem::getOutput(stream_type stream,
550 uint32_t samplingRate,
551 uint32_t format,
552 uint32_t channels,
553 output_flags flags)
554{
Eric Laurentddb78e72009-07-28 08:44:33 -0700555 audio_io_handle_t output = 0;
Eric Laurentef9500f2010-03-11 14:47:00 -0800556 // Do not use stream to output map cache if the direct output
557 // flag is set or if we are likely to use a direct output
558 // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to
559 // a direct output on some platforms).
560 // TODO: the output cache and stream to output mapping implementation needs to
561 // be reworked for proper operation with direct outputs. This code is too specific
562 // to the first use case we want to cover (Voice Recognition and Voice Dialer over
563 // Bluetooth SCO
564 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0 &&
565 ((stream != AudioSystem::VOICE_CALL && stream != AudioSystem::BLUETOOTH_SCO) ||
566 channels != AudioSystem::CHANNEL_OUT_MONO ||
567 (samplingRate != 8000 && samplingRate != 16000))) {
Eric Laurenta553c252009-07-17 12:17:14 -0700568 Mutex::Autolock _l(gLock);
569 output = AudioSystem::gStreamOutputMap.valueFor(stream);
Eric Laurentddb78e72009-07-28 08:44:33 -0700570 LOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700571 }
Eric Laurentddb78e72009-07-28 08:44:33 -0700572 if (output == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -0700573 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700574 if (aps == 0) return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700575 output = aps->getOutput(stream, samplingRate, format, channels, flags);
576 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0) {
577 Mutex::Autolock _l(gLock);
578 AudioSystem::gStreamOutputMap.add(stream, output);
579 }
580 }
581 return output;
582}
583
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700584status_t AudioSystem::startOutput(audio_io_handle_t output,
585 AudioSystem::stream_type stream,
586 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700587{
588 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
589 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700590 return aps->startOutput(output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700591}
592
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700593status_t AudioSystem::stopOutput(audio_io_handle_t output,
594 AudioSystem::stream_type stream,
595 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700596{
597 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
598 if (aps == 0) return PERMISSION_DENIED;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700599 return aps->stopOutput(output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700600}
601
602void AudioSystem::releaseOutput(audio_io_handle_t output)
603{
604 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
605 if (aps == 0) return;
606 aps->releaseOutput(output);
607}
608
609audio_io_handle_t AudioSystem::getInput(int inputSource,
610 uint32_t samplingRate,
611 uint32_t format,
612 uint32_t channels,
613 audio_in_acoustics acoustics)
614{
615 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700616 if (aps == 0) return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700617 return aps->getInput(inputSource, samplingRate, format, channels, acoustics);
618}
619
620status_t AudioSystem::startInput(audio_io_handle_t input)
621{
622 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
623 if (aps == 0) return PERMISSION_DENIED;
624 return aps->startInput(input);
625}
626
627status_t AudioSystem::stopInput(audio_io_handle_t input)
628{
629 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
630 if (aps == 0) return PERMISSION_DENIED;
631 return aps->stopInput(input);
632}
633
634void AudioSystem::releaseInput(audio_io_handle_t input)
635{
636 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
637 if (aps == 0) return;
638 aps->releaseInput(input);
639}
640
641status_t AudioSystem::initStreamVolume(stream_type stream,
642 int indexMin,
643 int indexMax)
644{
645 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
646 if (aps == 0) return PERMISSION_DENIED;
647 return aps->initStreamVolume(stream, indexMin, indexMax);
648}
649
650status_t AudioSystem::setStreamVolumeIndex(stream_type stream, int index)
651{
652 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
653 if (aps == 0) return PERMISSION_DENIED;
654 return aps->setStreamVolumeIndex(stream, index);
655}
656
657status_t AudioSystem::getStreamVolumeIndex(stream_type stream, int *index)
658{
659 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
660 if (aps == 0) return PERMISSION_DENIED;
661 return aps->getStreamVolumeIndex(stream, index);
662}
663
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700664uint32_t AudioSystem::getStrategyForStream(AudioSystem::stream_type stream)
665{
666 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
667 if (aps == 0) return 0;
668 return aps->getStrategyForStream(stream);
669}
670
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800671uint32_t AudioSystem::getDevicesForStream(AudioSystem::stream_type stream)
672{
673 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
674 if (aps == 0) return 0;
675 return aps->getDevicesForStream(stream);
676}
677
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700678audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc)
679{
680 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
681 if (aps == 0) return PERMISSION_DENIED;
682 return aps->getOutputForEffect(desc);
683}
684
685status_t AudioSystem::registerEffect(effect_descriptor_t *desc,
686 audio_io_handle_t output,
687 uint32_t strategy,
688 int session,
689 int id)
690{
691 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
692 if (aps == 0) return PERMISSION_DENIED;
693 return aps->registerEffect(desc, output, strategy, session, id);
694}
695
696status_t AudioSystem::unregisterEffect(int id)
697{
698 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
699 if (aps == 0) return PERMISSION_DENIED;
700 return aps->unregisterEffect(id);
701}
702
Eric Laurent25101b02011-02-02 09:33:30 -0800703status_t AudioSystem::isStreamActive(int stream, bool* state, uint32_t inPastMs) {
704 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
705 if (aps == 0) return PERMISSION_DENIED;
706 *state = aps->isStreamActive(stream, inPastMs);
707 return NO_ERROR;
708}
709
710
Eric Laurenta553c252009-07-17 12:17:14 -0700711// ---------------------------------------------------------------------------
712
713void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
714 Mutex::Autolock _l(AudioSystem::gLock);
715 AudioSystem::gAudioPolicyService.clear();
716
717 LOGW("AudioPolicyService server died!");
718}
719
720// ---------------------------------------------------------------------------
721
722
723// use emulated popcount optimization
724// http://www.df.lth.se/~john_e/gems/gem002d.html
725uint32_t AudioSystem::popCount(uint32_t u)
726{
727 u = ((u&0x55555555) + ((u>>1)&0x55555555));
728 u = ((u&0x33333333) + ((u>>2)&0x33333333));
729 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
730 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
731 u = ( u&0x0000ffff) + (u>>16);
732 return u;
733}
734
735bool AudioSystem::isOutputDevice(audio_devices device)
736{
737 if ((popCount(device) == 1 ) &&
738 ((device & ~AudioSystem::DEVICE_OUT_ALL) == 0)) {
739 return true;
740 } else {
741 return false;
742 }
743}
744
745bool AudioSystem::isInputDevice(audio_devices device)
746{
747 if ((popCount(device) == 1 ) &&
748 ((device & ~AudioSystem::DEVICE_IN_ALL) == 0)) {
749 return true;
750 } else {
751 return false;
752 }
753}
754
755bool AudioSystem::isA2dpDevice(audio_devices device)
756{
757 if ((popCount(device) == 1 ) &&
758 (device & (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
759 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
760 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER))) {
761 return true;
762 } else {
763 return false;
764 }
765}
766
767bool AudioSystem::isBluetoothScoDevice(audio_devices device)
768{
769 if ((popCount(device) == 1 ) &&
770 (device & (AudioSystem::DEVICE_OUT_BLUETOOTH_SCO |
771 AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
Eric Laurentc6ea3532010-05-26 00:55:46 -0700772 AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
773 AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET))) {
Eric Laurenta553c252009-07-17 12:17:14 -0700774 return true;
775 } else {
776 return false;
777 }
778}
779
780bool AudioSystem::isLowVisibility(stream_type stream)
781{
Eric Laurentf5fe39492010-02-22 01:37:19 -0800782 if (stream == AudioSystem::SYSTEM ||
783 stream == AudioSystem::NOTIFICATION ||
784 stream == AudioSystem::RING) {
Eric Laurenta553c252009-07-17 12:17:14 -0700785 return true;
786 } else {
787 return false;
788 }
789}
790
791bool AudioSystem::isInputChannel(uint32_t channel)
792{
793 if ((channel & ~AudioSystem::CHANNEL_IN_ALL) == 0) {
794 return true;
795 } else {
796 return false;
797 }
798}
799
800bool AudioSystem::isOutputChannel(uint32_t channel)
801{
802 if ((channel & ~AudioSystem::CHANNEL_OUT_ALL) == 0) {
803 return true;
804 } else {
805 return false;
806 }
807}
808
809bool AudioSystem::isValidFormat(uint32_t format)
810{
811 switch (format & MAIN_FORMAT_MASK) {
812 case PCM:
813 case MP3:
814 case AMR_NB:
815 case AMR_WB:
816 case AAC:
817 case HE_AAC_V1:
818 case HE_AAC_V2:
819 case VORBIS:
820 return true;
821 default:
822 return false;
823 }
824}
825
826bool AudioSystem::isLinearPCM(uint32_t format)
827{
828 switch (format) {
829 case PCM_16_BIT:
830 case PCM_8_BIT:
831 return true;
832 default:
833 return false;
834 }
835}
836
837//------------------------- AudioParameter class implementation ---------------
838
839const char *AudioParameter::keyRouting = "routing";
840const char *AudioParameter::keySamplingRate = "sampling_rate";
841const char *AudioParameter::keyFormat = "format";
842const char *AudioParameter::keyChannels = "channels";
843const char *AudioParameter::keyFrameCount = "frame_count";
Jean-Michel Trivi1a22bdb2010-11-09 14:06:52 -0800844const char *AudioParameter::keyInputSource = "input_source";
Eric Laurenta553c252009-07-17 12:17:14 -0700845
846AudioParameter::AudioParameter(const String8& keyValuePairs)
847{
848 char *str = new char[keyValuePairs.length()+1];
849 mKeyValuePairs = keyValuePairs;
850
851 strcpy(str, keyValuePairs.string());
852 char *pair = strtok(str, ";");
853 while (pair != NULL) {
854 if (strlen(pair) != 0) {
855 size_t eqIdx = strcspn(pair, "=");
856 String8 key = String8(pair, eqIdx);
857 String8 value;
858 if (eqIdx == strlen(pair)) {
859 value = String8("");
860 } else {
861 value = String8(pair + eqIdx + 1);
862 }
863 if (mParameters.indexOfKey(key) < 0) {
864 mParameters.add(key, value);
865 } else {
866 mParameters.replaceValueFor(key, value);
867 }
868 } else {
869 LOGV("AudioParameter() cstor empty key value pair");
870 }
871 pair = strtok(NULL, ";");
872 }
873
874 delete[] str;
875}
876
877AudioParameter::~AudioParameter()
878{
879 mParameters.clear();
880}
881
882String8 AudioParameter::toString()
883{
884 String8 str = String8("");
885
886 size_t size = mParameters.size();
887 for (size_t i = 0; i < size; i++) {
888 str += mParameters.keyAt(i);
889 str += "=";
890 str += mParameters.valueAt(i);
891 if (i < (size - 1)) str += ";";
892 }
893 return str;
894}
895
896status_t AudioParameter::add(const String8& key, const String8& value)
897{
898 if (mParameters.indexOfKey(key) < 0) {
899 mParameters.add(key, value);
900 return NO_ERROR;
901 } else {
902 mParameters.replaceValueFor(key, value);
903 return ALREADY_EXISTS;
904 }
905}
906
907status_t AudioParameter::addInt(const String8& key, const int value)
908{
909 char str[12];
910 if (snprintf(str, 12, "%d", value) > 0) {
911 String8 str8 = String8(str);
912 return add(key, str8);
913 } else {
914 return BAD_VALUE;
915 }
916}
917
918status_t AudioParameter::addFloat(const String8& key, const float value)
919{
920 char str[23];
921 if (snprintf(str, 23, "%.10f", value) > 0) {
922 String8 str8 = String8(str);
923 return add(key, str8);
924 } else {
925 return BAD_VALUE;
926 }
927}
928
929status_t AudioParameter::remove(const String8& key)
930{
931 if (mParameters.indexOfKey(key) >= 0) {
932 mParameters.removeItem(key);
933 return NO_ERROR;
934 } else {
935 return BAD_VALUE;
936 }
937}
938
939status_t AudioParameter::get(const String8& key, String8& value)
940{
941 if (mParameters.indexOfKey(key) >= 0) {
942 value = mParameters.valueFor(key);
943 return NO_ERROR;
944 } else {
945 return BAD_VALUE;
946 }
947}
948
949status_t AudioParameter::getInt(const String8& key, int& value)
950{
951 String8 str8;
952 status_t result = get(key, str8);
953 value = 0;
954 if (result == NO_ERROR) {
955 int val;
956 if (sscanf(str8.string(), "%d", &val) == 1) {
957 value = val;
958 } else {
959 result = INVALID_OPERATION;
960 }
961 }
962 return result;
963}
964
965status_t AudioParameter::getFloat(const String8& key, float& value)
966{
967 String8 str8;
968 status_t result = get(key, str8);
969 value = 0;
970 if (result == NO_ERROR) {
971 float val;
972 if (sscanf(str8.string(), "%f", &val) == 1) {
973 value = val;
974 } else {
975 result = INVALID_OPERATION;
976 }
977 }
978 return result;
979}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980
Eric Laurent327c27b2009-08-27 00:48:47 -0700981status_t AudioParameter::getAt(size_t index, String8& key, String8& value)
982{
983 if (mParameters.size() > index) {
984 key = mParameters.keyAt(index);
985 value = mParameters.valueAt(index);
986 return NO_ERROR;
987 } else {
988 return BAD_VALUE;
989 }
990}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991}; // namespace android
992