blob: 5352234f7984299e8b83daefd619c92d2f0bc2b6 [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
Eric Laurentc2f1f072009-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 Project89fa4ad2009-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 Laurentc2f1f072009-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 Project89fa4ad2009-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 Project89fa4ad2009-03-03 19:31:44 -080077 }
78 LOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
Eric Laurentc2f1f072009-07-17 12:17:14 -070079
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080080 return gAudioFlinger;
81}
82
The Android Open Source Project89fa4ad2009-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 Laurentfa2877b2009-07-28 08:44:33 -0700128status_t AudioSystem::setStreamVolume(int stream, float value, int output)
The Android Open Source Project89fa4ad2009-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 Laurentc2f1f072009-07-17 12:17:14 -0700133 af->setStreamVolume(stream, value, output);
The Android Open Source Project89fa4ad2009-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 Laurentfa2877b2009-07-28 08:44:33 -0700146status_t AudioSystem::getStreamVolume(int stream, float* volume, int output)
The Android Open Source Project89fa4ad2009-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 Laurentc2f1f072009-07-17 12:17:14 -0700151 *volume = af->streamVolume(stream, output);
The Android Open Source Project89fa4ad2009-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 Project89fa4ad2009-03-03 19:31:44 -0800172
173status_t AudioSystem::isMusicActive(bool* state) {
174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
175 if (af == 0) return PERMISSION_DENIED;
176 *state = af->isMusicActive();
177 return NO_ERROR;
178}
179
Eric Laurentc2f1f072009-07-17 12:17:14 -0700180
181status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
183 if (af == 0) return PERMISSION_DENIED;
Eric Laurentc2f1f072009-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 Project89fa4ad2009-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 Laurentc2f1f072009-07-17 12:17:14 -0700222 OutputDescriptor *outputDesc;
223 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224
Eric Laurentc2f1f072009-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 Laurentfa2877b2009-07-28 08:44:33 -0700237 LOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
Eric Laurentc2f1f072009-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 Laurentfa2877b2009-07-28 08:44:33 -0700248 LOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250 return NO_ERROR;
251}
252
253status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType)
254{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700255 OutputDescriptor *outputDesc;
256 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257
Eric Laurentc2f1f072009-07-17 12:17:14 -0700258 if (streamType == DEFAULT) {
259 streamType = MUSIC;
260 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700261
Eric Laurentc2f1f072009-07-17 12:17:14 -0700262 output = getOutput((stream_type)streamType);
263 if (output == 0) {
264 return PERMISSION_DENIED;
265 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266
Eric Laurentc2f1f072009-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 Laurentfa2877b2009-07-28 08:44:33 -0700279 LOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 return NO_ERROR;
282}
283
284status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType)
285{
Eric Laurentc2f1f072009-07-17 12:17:14 -0700286 OutputDescriptor *outputDesc;
287 audio_io_handle_t output;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288
Eric Laurentc2f1f072009-07-17 12:17:14 -0700289 if (streamType == DEFAULT) {
290 streamType = MUSIC;
291 }
Eric Laurent48f7f5e2009-04-02 09:32:43 -0700292
Eric Laurentc2f1f072009-07-17 12:17:14 -0700293 output = getOutput((stream_type)streamType);
294 if (output == 0) {
295 return PERMISSION_DENIED;
296 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800297
Eric Laurentc2f1f072009-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 Laurentfa2877b2009-07-28 08:44:33 -0700310 LOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 return NO_ERROR;
313}
314
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
The Android Open Source Project89fa4ad2009-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 Laurentc2f1f072009-07-17 12:17:14 -0700319 if ((gInBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 || (channelCount != gPrevInChannelCount)) {
321 // save the request params
322 gPrevInSamplingRate = sampleRate;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700323 gPrevInFormat = format;
The Android Open Source Project89fa4ad2009-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 Laurentc2f1f072009-07-17 12:17:14 -0700332 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333 *buffSize = gInBuffSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700334
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800335 return NO_ERROR;
336}
337
Eric Laurentf0ee6f42009-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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800345// ---------------------------------------------------------------------------
346
Eric Laurentc2f1f072009-07-17 12:17:14 -0700347void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 Mutex::Autolock _l(AudioSystem::gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349
Eric Laurentc2f1f072009-07-17 12:17:14 -0700350 AudioSystem::gAudioFlinger.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351
352 if (gAudioErrorCallback) {
353 gAudioErrorCallback(DEAD_OBJECT);
354 }
355 LOGW("AudioFlinger server died!");
356}
357
Eric Laurentfa2877b2009-07-28 08:44:33 -0700358void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700359 LOGV("ioConfigChanged() event %d", event);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 OutputDescriptor *desc;
361 uint32_t stream;
362
Eric Laurentfa2877b2009-07-28 08:44:33 -0700363 if (ioHandle == 0) return;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700364
365 Mutex::Autolock _l(AudioSystem::gLock);
366
367 switch (event) {
368 case STREAM_CONFIG_CHANGED:
369 if (param2 == 0) break;
370 stream = *(uint32_t *)param2;
Eric Laurentfa2877b2009-07-28 08:44:33 -0700371 LOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700372 if (gStreamOutputMap.indexOfKey(stream) >= 0) {
373 gStreamOutputMap.replaceValueFor(stream, ioHandle);
374 }
375 break;
376 case OUTPUT_OPENED: {
377 if (gOutputs.indexOfKey(ioHandle) >= 0) {
Eric Laurentfa2877b2009-07-28 08:44:33 -0700378 LOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700379 break;
380 }
381 if (param2 == 0) break;
382 desc = (OutputDescriptor *)param2;
383
384 OutputDescriptor *outputDesc = new OutputDescriptor(*desc);
385 gOutputs.add(ioHandle, outputDesc);
386 LOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d",
387 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
388 } break;
389 case OUTPUT_CLOSED: {
390 if (gOutputs.indexOfKey(ioHandle) < 0) {
Eric Laurentfa2877b2009-07-28 08:44:33 -0700391 LOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700392 break;
393 }
Eric Laurentfa2877b2009-07-28 08:44:33 -0700394 LOGV("ioConfigChanged() output %d closed", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700395
396 gOutputs.removeItem(ioHandle);
397 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) {
398 if (gStreamOutputMap.valueAt(i) == ioHandle) {
399 gStreamOutputMap.removeItemsAt(i);
400 }
401 }
402 } break;
403
404 case OUTPUT_CONFIG_CHANGED: {
405 int index = gOutputs.indexOfKey(ioHandle);
406 if (index < 0) {
Eric Laurentfa2877b2009-07-28 08:44:33 -0700407 LOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700408 break;
409 }
410 if (param2 == 0) break;
411 desc = (OutputDescriptor *)param2;
412
Eric Laurentfa2877b2009-07-28 08:44:33 -0700413 LOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d",
Eric Laurentc2f1f072009-07-17 12:17:14 -0700414 ioHandle, desc->samplingRate, desc->format,
415 desc->channels, desc->frameCount, desc->latency);
416 OutputDescriptor *outputDesc = gOutputs.valueAt(index);
417 delete outputDesc;
418 outputDesc = new OutputDescriptor(*desc);
419 gOutputs.replaceValueFor(ioHandle, outputDesc);
420 } break;
421 case INPUT_OPENED:
422 case INPUT_CLOSED:
423 case INPUT_CONFIG_CHANGED:
424 break;
425
426 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427}
428
429void AudioSystem::setErrorCallback(audio_error_callback cb) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700430 Mutex::Autolock _l(gLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 gAudioErrorCallback = cb;
432}
433
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434bool AudioSystem::routedToA2dpOutput(int streamType) {
435 switch(streamType) {
436 case MUSIC:
437 case VOICE_CALL:
438 case BLUETOOTH_SCO:
439 case SYSTEM:
440 return true;
441 default:
442 return false;
443 }
444}
445
446
Eric Laurentc2f1f072009-07-17 12:17:14 -0700447// client singleton for AudioPolicyService binder interface
448sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
449sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
450
451
452// establish binder interface to AudioFlinger service
453const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
454{
455 gLock.lock();
456 if (gAudioPolicyService.get() == 0) {
457 sp<IServiceManager> sm = defaultServiceManager();
458 sp<IBinder> binder;
459 do {
460 binder = sm->getService(String16("media.audio_policy"));
461 if (binder != 0)
462 break;
463 LOGW("AudioPolicyService not published, waiting...");
464 usleep(500000); // 0.5 s
465 } while(true);
466 if (gAudioPolicyServiceClient == NULL) {
467 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
468 }
469 binder->linkToDeath(gAudioPolicyServiceClient);
470 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
471 gLock.unlock();
472 } else {
473 gLock.unlock();
474 }
475 return gAudioPolicyService;
476}
477
478status_t AudioSystem::setDeviceConnectionState(audio_devices device,
479 device_connection_state state,
480 const char *device_address)
481{
482 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
483 if (aps == 0) return PERMISSION_DENIED;
484
485 return aps->setDeviceConnectionState(device, state, device_address);
486}
487
488AudioSystem::device_connection_state AudioSystem::getDeviceConnectionState(audio_devices device,
489 const char *device_address)
490{
491 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
492 if (aps == 0) return DEVICE_STATE_UNAVAILABLE;
493
494 return aps->getDeviceConnectionState(device, device_address);
495}
496
497status_t AudioSystem::setPhoneState(int state)
498{
499 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
500 if (aps == 0) return PERMISSION_DENIED;
501
502 return aps->setPhoneState(state);
503}
504
505status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask)
506{
507 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
508 if (aps == 0) return PERMISSION_DENIED;
509 return aps->setRingerMode(mode, mask);
510}
511
512status_t AudioSystem::setForceUse(force_use usage, forced_config config)
513{
514 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
515 if (aps == 0) return PERMISSION_DENIED;
516 return aps->setForceUse(usage, config);
517}
518
519AudioSystem::forced_config AudioSystem::getForceUse(force_use usage)
520{
521 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
522 if (aps == 0) return FORCE_NONE;
523 return aps->getForceUse(usage);
524}
525
526
527audio_io_handle_t AudioSystem::getOutput(stream_type stream,
528 uint32_t samplingRate,
529 uint32_t format,
530 uint32_t channels,
531 output_flags flags)
532{
Eric Laurentfa2877b2009-07-28 08:44:33 -0700533 audio_io_handle_t output = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700534 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0) {
535 Mutex::Autolock _l(gLock);
536 output = AudioSystem::gStreamOutputMap.valueFor(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700537 LOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700538 }
Eric Laurentfa2877b2009-07-28 08:44:33 -0700539 if (output == 0) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700540 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700541 if (aps == 0) return 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700542 output = aps->getOutput(stream, samplingRate, format, channels, flags);
543 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0) {
544 Mutex::Autolock _l(gLock);
545 AudioSystem::gStreamOutputMap.add(stream, output);
546 }
547 }
548 return output;
549}
550
551status_t AudioSystem::startOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
552{
553 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
554 if (aps == 0) return PERMISSION_DENIED;
555 return aps->startOutput(output, stream);
556}
557
558status_t AudioSystem::stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream)
559{
560 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
561 if (aps == 0) return PERMISSION_DENIED;
562 return aps->stopOutput(output, stream);
563}
564
565void AudioSystem::releaseOutput(audio_io_handle_t output)
566{
567 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
568 if (aps == 0) return;
569 aps->releaseOutput(output);
570}
571
572audio_io_handle_t AudioSystem::getInput(int inputSource,
573 uint32_t samplingRate,
574 uint32_t format,
575 uint32_t channels,
576 audio_in_acoustics acoustics)
577{
578 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
Eric Laurentfa2877b2009-07-28 08:44:33 -0700579 if (aps == 0) return 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700580 return aps->getInput(inputSource, samplingRate, format, channels, acoustics);
581}
582
583status_t AudioSystem::startInput(audio_io_handle_t input)
584{
585 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
586 if (aps == 0) return PERMISSION_DENIED;
587 return aps->startInput(input);
588}
589
590status_t AudioSystem::stopInput(audio_io_handle_t input)
591{
592 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
593 if (aps == 0) return PERMISSION_DENIED;
594 return aps->stopInput(input);
595}
596
597void AudioSystem::releaseInput(audio_io_handle_t input)
598{
599 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
600 if (aps == 0) return;
601 aps->releaseInput(input);
602}
603
604status_t AudioSystem::initStreamVolume(stream_type stream,
605 int indexMin,
606 int indexMax)
607{
608 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
609 if (aps == 0) return PERMISSION_DENIED;
610 return aps->initStreamVolume(stream, indexMin, indexMax);
611}
612
613status_t AudioSystem::setStreamVolumeIndex(stream_type stream, int index)
614{
615 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
616 if (aps == 0) return PERMISSION_DENIED;
617 return aps->setStreamVolumeIndex(stream, index);
618}
619
620status_t AudioSystem::getStreamVolumeIndex(stream_type stream, int *index)
621{
622 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
623 if (aps == 0) return PERMISSION_DENIED;
624 return aps->getStreamVolumeIndex(stream, index);
625}
626
627// ---------------------------------------------------------------------------
628
629void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
630 Mutex::Autolock _l(AudioSystem::gLock);
631 AudioSystem::gAudioPolicyService.clear();
632
633 LOGW("AudioPolicyService server died!");
634}
635
636// ---------------------------------------------------------------------------
637
638
639// use emulated popcount optimization
640// http://www.df.lth.se/~john_e/gems/gem002d.html
641uint32_t AudioSystem::popCount(uint32_t u)
642{
643 u = ((u&0x55555555) + ((u>>1)&0x55555555));
644 u = ((u&0x33333333) + ((u>>2)&0x33333333));
645 u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
646 u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
647 u = ( u&0x0000ffff) + (u>>16);
648 return u;
649}
650
651bool AudioSystem::isOutputDevice(audio_devices device)
652{
653 if ((popCount(device) == 1 ) &&
654 ((device & ~AudioSystem::DEVICE_OUT_ALL) == 0)) {
655 return true;
656 } else {
657 return false;
658 }
659}
660
661bool AudioSystem::isInputDevice(audio_devices device)
662{
663 if ((popCount(device) == 1 ) &&
664 ((device & ~AudioSystem::DEVICE_IN_ALL) == 0)) {
665 return true;
666 } else {
667 return false;
668 }
669}
670
671bool AudioSystem::isA2dpDevice(audio_devices device)
672{
673 if ((popCount(device) == 1 ) &&
674 (device & (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
675 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
676 AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER))) {
677 return true;
678 } else {
679 return false;
680 }
681}
682
683bool AudioSystem::isBluetoothScoDevice(audio_devices device)
684{
685 if ((popCount(device) == 1 ) &&
686 (device & (AudioSystem::DEVICE_OUT_BLUETOOTH_SCO |
687 AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
688 AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT))) {
689 return true;
690 } else {
691 return false;
692 }
693}
694
695bool AudioSystem::isLowVisibility(stream_type stream)
696{
697 if (stream == AudioSystem::SYSTEM || stream == AudioSystem::NOTIFICATION) {
698 return true;
699 } else {
700 return false;
701 }
702}
703
704bool AudioSystem::isInputChannel(uint32_t channel)
705{
706 if ((channel & ~AudioSystem::CHANNEL_IN_ALL) == 0) {
707 return true;
708 } else {
709 return false;
710 }
711}
712
713bool AudioSystem::isOutputChannel(uint32_t channel)
714{
715 if ((channel & ~AudioSystem::CHANNEL_OUT_ALL) == 0) {
716 return true;
717 } else {
718 return false;
719 }
720}
721
722bool AudioSystem::isValidFormat(uint32_t format)
723{
724 switch (format & MAIN_FORMAT_MASK) {
725 case PCM:
726 case MP3:
727 case AMR_NB:
728 case AMR_WB:
729 case AAC:
730 case HE_AAC_V1:
731 case HE_AAC_V2:
732 case VORBIS:
733 return true;
734 default:
735 return false;
736 }
737}
738
739bool AudioSystem::isLinearPCM(uint32_t format)
740{
741 switch (format) {
742 case PCM_16_BIT:
743 case PCM_8_BIT:
744 return true;
745 default:
746 return false;
747 }
748}
749
750//------------------------- AudioParameter class implementation ---------------
751
752const char *AudioParameter::keyRouting = "routing";
753const char *AudioParameter::keySamplingRate = "sampling_rate";
754const char *AudioParameter::keyFormat = "format";
755const char *AudioParameter::keyChannels = "channels";
756const char *AudioParameter::keyFrameCount = "frame_count";
757
758AudioParameter::AudioParameter(const String8& keyValuePairs)
759{
760 char *str = new char[keyValuePairs.length()+1];
761 mKeyValuePairs = keyValuePairs;
762
763 strcpy(str, keyValuePairs.string());
764 char *pair = strtok(str, ";");
765 while (pair != NULL) {
766 if (strlen(pair) != 0) {
767 size_t eqIdx = strcspn(pair, "=");
768 String8 key = String8(pair, eqIdx);
769 String8 value;
770 if (eqIdx == strlen(pair)) {
771 value = String8("");
772 } else {
773 value = String8(pair + eqIdx + 1);
774 }
775 if (mParameters.indexOfKey(key) < 0) {
776 mParameters.add(key, value);
777 } else {
778 mParameters.replaceValueFor(key, value);
779 }
780 } else {
781 LOGV("AudioParameter() cstor empty key value pair");
782 }
783 pair = strtok(NULL, ";");
784 }
785
786 delete[] str;
787}
788
789AudioParameter::~AudioParameter()
790{
791 mParameters.clear();
792}
793
794String8 AudioParameter::toString()
795{
796 String8 str = String8("");
797
798 size_t size = mParameters.size();
799 for (size_t i = 0; i < size; i++) {
800 str += mParameters.keyAt(i);
801 str += "=";
802 str += mParameters.valueAt(i);
803 if (i < (size - 1)) str += ";";
804 }
805 return str;
806}
807
808status_t AudioParameter::add(const String8& key, const String8& value)
809{
810 if (mParameters.indexOfKey(key) < 0) {
811 mParameters.add(key, value);
812 return NO_ERROR;
813 } else {
814 mParameters.replaceValueFor(key, value);
815 return ALREADY_EXISTS;
816 }
817}
818
819status_t AudioParameter::addInt(const String8& key, const int value)
820{
821 char str[12];
822 if (snprintf(str, 12, "%d", value) > 0) {
823 String8 str8 = String8(str);
824 return add(key, str8);
825 } else {
826 return BAD_VALUE;
827 }
828}
829
830status_t AudioParameter::addFloat(const String8& key, const float value)
831{
832 char str[23];
833 if (snprintf(str, 23, "%.10f", value) > 0) {
834 String8 str8 = String8(str);
835 return add(key, str8);
836 } else {
837 return BAD_VALUE;
838 }
839}
840
841status_t AudioParameter::remove(const String8& key)
842{
843 if (mParameters.indexOfKey(key) >= 0) {
844 mParameters.removeItem(key);
845 return NO_ERROR;
846 } else {
847 return BAD_VALUE;
848 }
849}
850
851status_t AudioParameter::get(const String8& key, String8& value)
852{
853 if (mParameters.indexOfKey(key) >= 0) {
854 value = mParameters.valueFor(key);
855 return NO_ERROR;
856 } else {
857 return BAD_VALUE;
858 }
859}
860
861status_t AudioParameter::getInt(const String8& key, int& value)
862{
863 String8 str8;
864 status_t result = get(key, str8);
865 value = 0;
866 if (result == NO_ERROR) {
867 int val;
868 if (sscanf(str8.string(), "%d", &val) == 1) {
869 value = val;
870 } else {
871 result = INVALID_OPERATION;
872 }
873 }
874 return result;
875}
876
877status_t AudioParameter::getFloat(const String8& key, float& value)
878{
879 String8 str8;
880 status_t result = get(key, str8);
881 value = 0;
882 if (result == NO_ERROR) {
883 float val;
884 if (sscanf(str8.string(), "%f", &val) == 1) {
885 value = val;
886 } else {
887 result = INVALID_OPERATION;
888 }
889 }
890 return result;
891}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800892
Eric Laurenta9c322e2009-08-27 00:48:47 -0700893status_t AudioParameter::getAt(size_t index, String8& key, String8& value)
894{
895 if (mParameters.size() > index) {
896 key = mParameters.keyAt(index);
897 value = mParameters.valueAt(index);
898 return NO_ERROR;
899 } else {
900 return BAD_VALUE;
901 }
902}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903}; // namespace android
904