blob: 4478abddf39f5528d56d7fd5a283f4b517b4f9f1 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
Eric Laurent23f25cd2010-01-25 08:49:09 -0800173status_t AudioSystem::isStreamActive(int stream, bool* state) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
175 if (af == 0) return PERMISSION_DENIED;
Eric Laurent23f25cd2010-01-25 08:49:09 -0800176 *state = af->isStreamActive(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 return NO_ERROR;
178}
179
Eric Laurenta553c252009-07-17 12:17:14 -0700180
181status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
183 if (af == 0) return PERMISSION_DENIED;
Eric Laurenta553c252009-07-17 12:17:14 -0700184 return af->setParameters(ioHandle, keyValuePairs);
185}
186
187String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
188 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
189 String8 result = String8("");
190 if (af == 0) return result;
191
192 result = af->getParameters(ioHandle, keys);
193 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194}
195
196// convert volume steps to natural log scale
197
198// change this value to change volume scaling
199static const float dBPerStep = 0.5f;
200// shouldn't need to touch these
201static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
202static const float dBConvertInverse = 1.0f / dBConvert;
203
204float AudioSystem::linearToLog(int volume)
205{
206 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
207 // LOGD("linearToLog(%d)=%f", volume, v);
208 // return v;
209 return volume ? exp(float(100 - volume) * dBConvert) : 0;
210}
211
212int AudioSystem::logToLinear(float volume)
213{
214 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
215 // LOGD("logTolinear(%d)=%f", v, volume);
216 // return v;
217 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
218}
219
220status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType)
221{
Eric Laurenta553c252009-07-17 12:17:14 -0700222 OutputDescriptor *outputDesc;
223 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
Eric Laurenta553c252009-07-17 12:17:14 -0700225 if (streamType == DEFAULT) {
226 streamType = MUSIC;
227 }
228
229 output = getOutput((stream_type)streamType);
230 if (output == 0) {
231 return PERMISSION_DENIED;
232 }
233
234 gLock.lock();
235 outputDesc = AudioSystem::gOutputs.valueFor(output);
236 if (outputDesc == 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700237 LOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurenta553c252009-07-17 12:17:14 -0700238 gLock.unlock();
239 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
240 if (af == 0) return PERMISSION_DENIED;
241 *samplingRate = af->sampleRate(output);
242 } else {
243 LOGV("getOutputSamplingRate() reading from output desc");
244 *samplingRate = outputDesc->samplingRate;
245 gLock.unlock();
246 }
247
Eric Laurentddb78e72009-07-28 08:44:33 -0700248 LOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
Eric Laurenta553c252009-07-17 12:17:14 -0700249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 return NO_ERROR;
251}
252
253status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType)
254{
Eric Laurenta553c252009-07-17 12:17:14 -0700255 OutputDescriptor *outputDesc;
256 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Eric Laurenta553c252009-07-17 12:17:14 -0700258 if (streamType == DEFAULT) {
259 streamType = MUSIC;
260 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700261
Eric Laurenta553c252009-07-17 12:17:14 -0700262 output = getOutput((stream_type)streamType);
263 if (output == 0) {
264 return PERMISSION_DENIED;
265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Eric Laurenta553c252009-07-17 12:17:14 -0700267 gLock.lock();
268 outputDesc = AudioSystem::gOutputs.valueFor(output);
269 if (outputDesc == 0) {
270 gLock.unlock();
271 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
272 if (af == 0) return PERMISSION_DENIED;
273 *frameCount = af->frameCount(output);
274 } else {
275 *frameCount = outputDesc->frameCount;
276 gLock.unlock();
277 }
278
Eric Laurentddb78e72009-07-28 08:44:33 -0700279 LOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 return NO_ERROR;
282}
283
284status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType)
285{
Eric Laurenta553c252009-07-17 12:17:14 -0700286 OutputDescriptor *outputDesc;
287 audio_io_handle_t output;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
Eric Laurenta553c252009-07-17 12:17:14 -0700289 if (streamType == DEFAULT) {
290 streamType = MUSIC;
291 }
Eric Laurent3547cc02009-04-02 09:32:43 -0700292
Eric Laurenta553c252009-07-17 12:17:14 -0700293 output = getOutput((stream_type)streamType);
294 if (output == 0) {
295 return PERMISSION_DENIED;
296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
Eric Laurenta553c252009-07-17 12:17:14 -0700298 gLock.lock();
299 outputDesc = AudioSystem::gOutputs.valueFor(output);
300 if (outputDesc == 0) {
301 gLock.unlock();
302 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
303 if (af == 0) return PERMISSION_DENIED;
304 *latency = af->latency(output);
305 } else {
306 *latency = outputDesc->latency;
307 gLock.unlock();
308 }
309
Eric Laurentddb78e72009-07-28 08:44:33 -0700310 LOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
Eric Laurenta553c252009-07-17 12:17:14 -0700311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 return NO_ERROR;
313}
314
Eric Laurenta553c252009-07-17 12:17:14 -0700315status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 size_t* buffSize)
317{
318 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
Eric Laurenta553c252009-07-17 12:17:14 -0700319 if ((gInBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 || (channelCount != gPrevInChannelCount)) {
321 // save the request params
322 gPrevInSamplingRate = sampleRate;
Eric Laurenta553c252009-07-17 12:17:14 -0700323 gPrevInFormat = format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 gPrevInChannelCount = channelCount;
325
326 gInBuffSize = 0;
327 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
328 if (af == 0) {
329 return PERMISSION_DENIED;
330 }
331 gInBuffSize = af->getInputBufferSize(sampleRate, format, channelCount);
Eric Laurenta553c252009-07-17 12:17:14 -0700332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 *buffSize = gInBuffSize;
Eric Laurenta553c252009-07-17 12:17:14 -0700334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 return NO_ERROR;
336}
337
Eric Laurent415f3e22009-10-21 08:14:22 -0700338status_t AudioSystem::setVoiceVolume(float value)
339{
340 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
341 if (af == 0) return PERMISSION_DENIED;
342 return af->setVoiceVolume(value);
343}
344
Eric Laurent0986e792010-01-19 17:37:09 -0800345status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int stream)
346{
347 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
348 if (af == 0) return PERMISSION_DENIED;
349
350 if (stream == DEFAULT) {
351 stream = MUSIC;
352 }
353
354 return af->getRenderPosition(halFrames, dspFrames, getOutput((stream_type)stream));
355}
356
Eric Laurent47d0a922010-02-26 02:47:27 -0800357unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
358 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
359 unsigned int result = 0;
360 if (af == 0) return result;
Eric Laurentef9500f2010-03-11 14:47:00 -0800361 if (ioHandle == 0) return result;
Eric Laurent47d0a922010-02-26 02:47:27 -0800362
363 result = af->getInputFramesLost(ioHandle);
364 return result;
365}
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367// ---------------------------------------------------------------------------
368
Eric Laurenta553c252009-07-17 12:17:14 -0700369void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371
Eric Laurenta553c252009-07-17 12:17:14 -0700372 AudioSystem::gAudioFlinger.clear();
Eric Laurent787aa592010-01-25 10:27:15 -0800373 // clear output handles and stream to output map caches
374 AudioSystem::gStreamOutputMap.clear();
375 AudioSystem::gOutputs.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376
377 if (gAudioErrorCallback) {
378 gAudioErrorCallback(DEAD_OBJECT);
379 }
380 LOGW("AudioFlinger server died!");
381}
382
Eric Laurentddb78e72009-07-28 08:44:33 -0700383void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) {
Eric Laurenta553c252009-07-17 12:17:14 -0700384 LOGV("ioConfigChanged() event %d", event);
Eric Laurenta553c252009-07-17 12:17:14 -0700385 OutputDescriptor *desc;
386 uint32_t stream;
387
Eric Laurentddb78e72009-07-28 08:44:33 -0700388 if (ioHandle == 0) return;
Eric Laurenta553c252009-07-17 12:17:14 -0700389
390 Mutex::Autolock _l(AudioSystem::gLock);
391
392 switch (event) {
393 case STREAM_CONFIG_CHANGED:
394 if (param2 == 0) break;
395 stream = *(uint32_t *)param2;
Eric Laurentddb78e72009-07-28 08:44:33 -0700396 LOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700397 if (gStreamOutputMap.indexOfKey(stream) >= 0) {
398 gStreamOutputMap.replaceValueFor(stream, ioHandle);
399 }
400 break;
401 case OUTPUT_OPENED: {
402 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700403 LOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700404 break;
405 }
406 if (param2 == 0) break;
407 desc = (OutputDescriptor *)param2;
408
409 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
410 gOutputs.add(ioHandle, outputDesc);
411 LOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d",
412 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
413 } break;
414 case OUTPUT_CLOSED: {
415 if (gOutputs.indexOfKey(ioHandle) < 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700416 LOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700417 break;
418 }
Eric Laurentddb78e72009-07-28 08:44:33 -0700419 LOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700420
421 gOutputs.removeItem(ioHandle);
422 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) {
423 if (gStreamOutputMap.valueAt(i) == ioHandle) {
424 gStreamOutputMap.removeItemsAt(i);
425 }
426 }
427 } break;
428
429 case OUTPUT_CONFIG_CHANGED: {
430 int index = gOutputs.indexOfKey(ioHandle);
431 if (index < 0) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700432 LOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
Eric Laurenta553c252009-07-17 12:17:14 -0700433 break;
434 }
435 if (param2 == 0) break;
436 desc = (OutputDescriptor *)param2;
437
Eric Laurentddb78e72009-07-28 08:44:33 -0700438 LOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurenta553c252009-07-17 12:17:14 -0700439 ioHandle, desc->samplingRate, desc->format,
440 desc->channels, desc->frameCount, desc->latency);
441 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
442 delete outputDesc;
443 outputDesc = new OutputDescriptor(*desc);
444 gOutputs.replaceValueFor(ioHandle, outputDesc);
445 } break;
446 case INPUT_OPENED:
447 case INPUT_CLOSED:
448 case INPUT_CONFIG_CHANGED:
449 break;
450
451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452}
453
454void AudioSystem::setErrorCallback(audio_error_callback cb) {
Eric Laurenta553c252009-07-17 12:17:14 -0700455 Mutex::Autolock _l(gLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 gAudioErrorCallback = cb;
457}
458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459bool AudioSystem::routedToA2dpOutput(int streamType) {
460 switch(streamType) {
461 case MUSIC:
462 case VOICE_CALL:
463 case BLUETOOTH_SCO:
464 case SYSTEM:
465 return true;
466 default:
467 return false;
468 }
469}
470
471
Eric Laurenta553c252009-07-17 12:17:14 -0700472// client singleton for AudioPolicyService binder interface
473sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
474sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
475
476
477// establish binder interface to AudioFlinger service
478const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
479{
480 gLock.lock();
481 if (gAudioPolicyService.get() == 0) {
482 sp<IServiceManager> sm = defaultServiceManager();
483 sp<IBinder> binder;
484 do {
485 binder = sm->getService(String16("media.audio_policy"));
486 if (binder != 0)
487 break;
488 LOGW("AudioPolicyService not published, waiting...");
489 usleep(500000); // 0.5 s
490 } while(true);
491 if (gAudioPolicyServiceClient == NULL) {
492 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
493 }
494 binder->linkToDeath(gAudioPolicyServiceClient);
495 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
496 gLock.unlock();
497 } else {
498 gLock.unlock();
499 }
500 return gAudioPolicyService;
501}
502
503status_t AudioSystem::setDeviceConnectionState(audio_devices device,
504 device_connection_state state,
505 const char *device_address)
506{
507 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
508 if (aps == 0) return PERMISSION_DENIED;
509
510 return aps->setDeviceConnectionState(device, state, device_address);
511}
512
513AudioSystem::device_connection_state AudioSystem::getDeviceConnectionState(audio_devices device,
514 const char *device_address)
515{
516 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
517 if (aps == 0) return DEVICE_STATE_UNAVAILABLE;
518
519 return aps->getDeviceConnectionState(device, device_address);
520}
521
522status_t AudioSystem::setPhoneState(int state)
523{
524 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
525 if (aps == 0) return PERMISSION_DENIED;
526
527 return aps->setPhoneState(state);
528}
529
530status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask)
531{
532 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
533 if (aps == 0) return PERMISSION_DENIED;
534 return aps->setRingerMode(mode, mask);
535}
536
537status_t AudioSystem::setForceUse(force_use usage, forced_config config)
538{
539 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
540 if (aps == 0) return PERMISSION_DENIED;
541 return aps->setForceUse(usage, config);
542}
543
544AudioSystem::forced_config AudioSystem::getForceUse(force_use usage)
545{
546 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
547 if (aps == 0) return FORCE_NONE;
548 return aps->getForceUse(usage);
549}
550
551
552audio_io_handle_t AudioSystem::getOutput(stream_type stream,
553 uint32_t samplingRate,
554 uint32_t format,
555 uint32_t channels,
556 output_flags flags)
557{
Eric Laurentddb78e72009-07-28 08:44:33 -0700558 audio_io_handle_t output = 0;
Eric Laurentef9500f2010-03-11 14:47:00 -0800559 // Do not use stream to output map cache if the direct output
560 // flag is set or if we are likely to use a direct output
561 // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to
562 // a direct output on some platforms).
563 // TODO: the output cache and stream to output mapping implementation needs to
564 // be reworked for proper operation with direct outputs. This code is too specific
565 // to the first use case we want to cover (Voice Recognition and Voice Dialer over
566 // Bluetooth SCO
567 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0 &&
568 ((stream != AudioSystem::VOICE_CALL && stream != AudioSystem::BLUETOOTH_SCO) ||
569 channels != AudioSystem::CHANNEL_OUT_MONO ||
570 (samplingRate != 8000 && samplingRate != 16000))) {
Eric Laurenta553c252009-07-17 12:17:14 -0700571 Mutex::Autolock _l(gLock);
572 output = AudioSystem::gStreamOutputMap.valueFor(stream);
Eric Laurentddb78e72009-07-28 08:44:33 -0700573 LOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700574 }
Eric Laurentddb78e72009-07-28 08:44:33 -0700575 if (output == 0) {
Eric Laurenta553c252009-07-17 12:17:14 -0700576 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700577 if (aps == 0) return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700578 output = aps->getOutput(stream, samplingRate, format, channels, flags);
579 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0) {
580 Mutex::Autolock _l(gLock);
581 AudioSystem::gStreamOutputMap.add(stream, output);
582 }
583 }
584 return output;
585}
586
587status_t AudioSystem::startOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
588{
589 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
590 if (aps == 0) return PERMISSION_DENIED;
591 return aps->startOutput(output, stream);
592}
593
594status_t AudioSystem::stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
595{
596 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
597 if (aps == 0) return PERMISSION_DENIED;
598 return aps->stopOutput(output, stream);
599}
600
601void AudioSystem::releaseOutput(audio_io_handle_t output)
602{
603 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
604 if (aps == 0) return;
605 aps->releaseOutput(output);
606}
607
608audio_io_handle_t AudioSystem::getInput(int inputSource,
609 uint32_t samplingRate,
610 uint32_t format,
611 uint32_t channels,
612 audio_in_acoustics acoustics)
613{
614 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentddb78e72009-07-28 08:44:33 -0700615 if (aps == 0) return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700616 return aps->getInput(inputSource, samplingRate, format, channels, acoustics);
617}
618
619status_t AudioSystem::startInput(audio_io_handle_t input)
620{
621 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
622 if (aps == 0) return PERMISSION_DENIED;
623 return aps->startInput(input);
624}
625
626status_t AudioSystem::stopInput(audio_io_handle_t input)
627{
628 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
629 if (aps == 0) return PERMISSION_DENIED;
630 return aps->stopInput(input);
631}
632
633void AudioSystem::releaseInput(audio_io_handle_t input)
634{
635 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
636 if (aps == 0) return;
637 aps->releaseInput(input);
638}
639
640status_t AudioSystem::initStreamVolume(stream_type stream,
641 int indexMin,
642 int indexMax)
643{
644 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
645 if (aps == 0) return PERMISSION_DENIED;
646 return aps->initStreamVolume(stream, indexMin, indexMax);
647}
648
649status_t AudioSystem::setStreamVolumeIndex(stream_type stream, int index)
650{
651 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
652 if (aps == 0) return PERMISSION_DENIED;
653 return aps->setStreamVolumeIndex(stream, index);
654}
655
656status_t AudioSystem::getStreamVolumeIndex(stream_type stream, int *index)
657{
658 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
659 if (aps == 0) return PERMISSION_DENIED;
660 return aps->getStreamVolumeIndex(stream, index);
661}
662
663// ---------------------------------------------------------------------------
664
665void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
666 Mutex::Autolock _l(AudioSystem::gLock);
667 AudioSystem::gAudioPolicyService.clear();
668
669 LOGW("AudioPolicyService server died!");
670}
671
672// ---------------------------------------------------------------------------
673
674
675// use emulated popcount optimization
676// http://www.df.lth.se/~john_e/gems/gem002d.html
677uint32_t AudioSystem::popCount(uint32_t u)
678{
679 u = ((u&0x55555555) + ((u>>1)&0x55555555));
680 u = ((u&0x33333333) + ((u>>2)&0x33333333));
681 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
682 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
683 u = ( u&0x0000ffff) + (u>>16);
684 return u;
685}
686
687bool AudioSystem::isOutputDevice(audio_devices device)
688{
689 if ((popCount(device) == 1 ) &&
690 ((device & ~AudioSystem::DEVICE_OUT_ALL) == 0)) {
691 return true;
692 } else {
693 return false;
694 }
695}
696
697bool AudioSystem::isInputDevice(audio_devices device)
698{
699 if ((popCount(device) == 1 ) &&
700 ((device & ~AudioSystem::DEVICE_IN_ALL) == 0)) {
701 return true;
702 } else {
703 return false;
704 }
705}
706
707bool AudioSystem::isA2dpDevice(audio_devices device)
708{
709 if ((popCount(device) == 1 ) &&
710 (device & (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
711 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
712 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER))) {
713 return true;
714 } else {
715 return false;
716 }
717}
718
719bool AudioSystem::isBluetoothScoDevice(audio_devices device)
720{
721 if ((popCount(device) == 1 ) &&
722 (device & (AudioSystem::DEVICE_OUT_BLUETOOTH_SCO |
723 AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
Eric Laurentc6ea3532010-05-26 00:55:46 -0700724 AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
725 AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET))) {
Eric Laurenta553c252009-07-17 12:17:14 -0700726 return true;
727 } else {
728 return false;
729 }
730}
731
732bool AudioSystem::isLowVisibility(stream_type stream)
733{
Eric Laurentf5fe39492010-02-22 01:37:19 -0800734 if (stream == AudioSystem::SYSTEM ||
735 stream == AudioSystem::NOTIFICATION ||
736 stream == AudioSystem::RING) {
Eric Laurenta553c252009-07-17 12:17:14 -0700737 return true;
738 } else {
739 return false;
740 }
741}
742
743bool AudioSystem::isInputChannel(uint32_t channel)
744{
745 if ((channel & ~AudioSystem::CHANNEL_IN_ALL) == 0) {
746 return true;
747 } else {
748 return false;
749 }
750}
751
752bool AudioSystem::isOutputChannel(uint32_t channel)
753{
754 if ((channel & ~AudioSystem::CHANNEL_OUT_ALL) == 0) {
755 return true;
756 } else {
757 return false;
758 }
759}
760
761bool AudioSystem::isValidFormat(uint32_t format)
762{
763 switch (format & MAIN_FORMAT_MASK) {
764 case PCM:
765 case MP3:
766 case AMR_NB:
767 case AMR_WB:
768 case AAC:
769 case HE_AAC_V1:
770 case HE_AAC_V2:
771 case VORBIS:
772 return true;
773 default:
774 return false;
775 }
776}
777
778bool AudioSystem::isLinearPCM(uint32_t format)
779{
780 switch (format) {
781 case PCM_16_BIT:
782 case PCM_8_BIT:
783 return true;
784 default:
785 return false;
786 }
787}
788
789//------------------------- AudioParameter class implementation ---------------
790
791const char *AudioParameter::keyRouting = "routing";
792const char *AudioParameter::keySamplingRate = "sampling_rate";
793const char *AudioParameter::keyFormat = "format";
794const char *AudioParameter::keyChannels = "channels";
795const char *AudioParameter::keyFrameCount = "frame_count";
796
797AudioParameter::AudioParameter(const String8& keyValuePairs)
798{
799 char *str = new char[keyValuePairs.length()+1];
800 mKeyValuePairs = keyValuePairs;
801
802 strcpy(str, keyValuePairs.string());
803 char *pair = strtok(str, ";");
804 while (pair != NULL) {
805 if (strlen(pair) != 0) {
806 size_t eqIdx = strcspn(pair, "=");
807 String8 key = String8(pair, eqIdx);
808 String8 value;
809 if (eqIdx == strlen(pair)) {
810 value = String8("");
811 } else {
812 value = String8(pair + eqIdx + 1);
813 }
814 if (mParameters.indexOfKey(key) < 0) {
815 mParameters.add(key, value);
816 } else {
817 mParameters.replaceValueFor(key, value);
818 }
819 } else {
820 LOGV("AudioParameter() cstor empty key value pair");
821 }
822 pair = strtok(NULL, ";");
823 }
824
825 delete[] str;
826}
827
828AudioParameter::~AudioParameter()
829{
830 mParameters.clear();
831}
832
833String8 AudioParameter::toString()
834{
835 String8 str = String8("");
836
837 size_t size = mParameters.size();
838 for (size_t i = 0; i < size; i++) {
839 str += mParameters.keyAt(i);
840 str += "=";
841 str += mParameters.valueAt(i);
842 if (i < (size - 1)) str += ";";
843 }
844 return str;
845}
846
847status_t AudioParameter::add(const String8& key, const String8& value)
848{
849 if (mParameters.indexOfKey(key) < 0) {
850 mParameters.add(key, value);
851 return NO_ERROR;
852 } else {
853 mParameters.replaceValueFor(key, value);
854 return ALREADY_EXISTS;
855 }
856}
857
858status_t AudioParameter::addInt(const String8& key, const int value)
859{
860 char str[12];
861 if (snprintf(str, 12, "%d", value) > 0) {
862 String8 str8 = String8(str);
863 return add(key, str8);
864 } else {
865 return BAD_VALUE;
866 }
867}
868
869status_t AudioParameter::addFloat(const String8& key, const float value)
870{
871 char str[23];
872 if (snprintf(str, 23, "%.10f", value) > 0) {
873 String8 str8 = String8(str);
874 return add(key, str8);
875 } else {
876 return BAD_VALUE;
877 }
878}
879
880status_t AudioParameter::remove(const String8& key)
881{
882 if (mParameters.indexOfKey(key) >= 0) {
883 mParameters.removeItem(key);
884 return NO_ERROR;
885 } else {
886 return BAD_VALUE;
887 }
888}
889
890status_t AudioParameter::get(const String8& key, String8& value)
891{
892 if (mParameters.indexOfKey(key) >= 0) {
893 value = mParameters.valueFor(key);
894 return NO_ERROR;
895 } else {
896 return BAD_VALUE;
897 }
898}
899
900status_t AudioParameter::getInt(const String8& key, int& value)
901{
902 String8 str8;
903 status_t result = get(key, str8);
904 value = 0;
905 if (result == NO_ERROR) {
906 int val;
907 if (sscanf(str8.string(), "%d", &val) == 1) {
908 value = val;
909 } else {
910 result = INVALID_OPERATION;
911 }
912 }
913 return result;
914}
915
916status_t AudioParameter::getFloat(const String8& key, float& value)
917{
918 String8 str8;
919 status_t result = get(key, str8);
920 value = 0;
921 if (result == NO_ERROR) {
922 float val;
923 if (sscanf(str8.string(), "%f", &val) == 1) {
924 value = val;
925 } else {
926 result = INVALID_OPERATION;
927 }
928 }
929 return result;
930}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931
Eric Laurent327c27b2009-08-27 00:48:47 -0700932status_t AudioParameter::getAt(size_t index, String8& key, String8& value)
933{
934 if (mParameters.size() > index) {
935 key = mParameters.keyAt(index);
936 value = mParameters.valueAt(index);
937 return NO_ERROR;
938 } else {
939 return BAD_VALUE;
940 }
941}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942}; // namespace android
943