1 /*
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>
21 #include <binder/IServiceManager.h>
22 #include <binder/ProcessState.h>
23 #include <media/AudioSystem.h>
24 #include <media/IAudioFlinger.h>
25 #include <media/IAudioPolicyService.h>
26 #include <math.h>
27
28 #include <system/audio.h>
29
30 // ----------------------------------------------------------------------------
31
32 namespace android {
33
34 // client singleton for AudioFlinger binder interface
35 Mutex AudioSystem::gLock;
36 Mutex AudioSystem::gLockAPS;
37 sp<IAudioFlinger> AudioSystem::gAudioFlinger;
38 sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
39 audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
40 dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
41 record_config_callback AudioSystem::gRecordConfigCallback = NULL;
42
43
44 // establish binder interface to AudioFlinger service
get_audio_flinger()45 const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
46 {
47 sp<IAudioFlinger> af;
48 sp<AudioFlingerClient> afc;
49 {
50 Mutex::Autolock _l(gLock);
51 if (gAudioFlinger == 0) {
52 sp<IServiceManager> sm = defaultServiceManager();
53 sp<IBinder> binder;
54 do {
55 binder = sm->getService(String16("media.audio_flinger"));
56 if (binder != 0)
57 break;
58 ALOGW("AudioFlinger not published, waiting...");
59 usleep(500000); // 0.5 s
60 } while (true);
61 if (gAudioFlingerClient == NULL) {
62 gAudioFlingerClient = new AudioFlingerClient();
63 } else {
64 if (gAudioErrorCallback) {
65 gAudioErrorCallback(NO_ERROR);
66 }
67 }
68 binder->linkToDeath(gAudioFlingerClient);
69 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
70 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
71 afc = gAudioFlingerClient;
72 // Make sure callbacks can be received by gAudioFlingerClient
73 ProcessState::self()->startThreadPool();
74 }
75 af = gAudioFlinger;
76 }
77 if (afc != 0) {
78 af->registerClient(afc);
79 }
80 return af;
81 }
82
getAudioFlingerClient()83 const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
84 {
85 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
86 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
87 if (af == 0) return 0;
88 Mutex::Autolock _l(gLock);
89 return gAudioFlingerClient;
90 }
91
getIoDescriptor(audio_io_handle_t ioHandle)92 sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
93 {
94 sp<AudioIoDescriptor> desc;
95 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
96 if (afc != 0) {
97 desc = afc->getIoDescriptor(ioHandle);
98 }
99 return desc;
100 }
101
checkAudioFlinger()102 /* static */ status_t AudioSystem::checkAudioFlinger()
103 {
104 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
105 return NO_ERROR;
106 }
107 return DEAD_OBJECT;
108 }
109
110 // FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
111
muteMicrophone(bool state)112 status_t AudioSystem::muteMicrophone(bool state)
113 {
114 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
115 if (af == 0) return PERMISSION_DENIED;
116 return af->setMicMute(state);
117 }
118
isMicrophoneMuted(bool * state)119 status_t AudioSystem::isMicrophoneMuted(bool* state)
120 {
121 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
122 if (af == 0) return PERMISSION_DENIED;
123 *state = af->getMicMute();
124 return NO_ERROR;
125 }
126
setMasterVolume(float value)127 status_t AudioSystem::setMasterVolume(float value)
128 {
129 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
130 if (af == 0) return PERMISSION_DENIED;
131 af->setMasterVolume(value);
132 return NO_ERROR;
133 }
134
setMasterMute(bool mute)135 status_t AudioSystem::setMasterMute(bool mute)
136 {
137 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
138 if (af == 0) return PERMISSION_DENIED;
139 af->setMasterMute(mute);
140 return NO_ERROR;
141 }
142
getMasterVolume(float * volume)143 status_t AudioSystem::getMasterVolume(float* volume)
144 {
145 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
146 if (af == 0) return PERMISSION_DENIED;
147 *volume = af->masterVolume();
148 return NO_ERROR;
149 }
150
getMasterMute(bool * mute)151 status_t AudioSystem::getMasterMute(bool* mute)
152 {
153 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
154 if (af == 0) return PERMISSION_DENIED;
155 *mute = af->masterMute();
156 return NO_ERROR;
157 }
158
setStreamVolume(audio_stream_type_t stream,float value,audio_io_handle_t output)159 status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
160 audio_io_handle_t output)
161 {
162 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
163 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
164 if (af == 0) return PERMISSION_DENIED;
165 af->setStreamVolume(stream, value, output);
166 return NO_ERROR;
167 }
168
setStreamMute(audio_stream_type_t stream,bool mute)169 status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
170 {
171 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
172 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
173 if (af == 0) return PERMISSION_DENIED;
174 af->setStreamMute(stream, mute);
175 return NO_ERROR;
176 }
177
getStreamVolume(audio_stream_type_t stream,float * volume,audio_io_handle_t output)178 status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
179 audio_io_handle_t output)
180 {
181 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
182 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
183 if (af == 0) return PERMISSION_DENIED;
184 *volume = af->streamVolume(stream, output);
185 return NO_ERROR;
186 }
187
getStreamMute(audio_stream_type_t stream,bool * mute)188 status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
189 {
190 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
191 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
192 if (af == 0) return PERMISSION_DENIED;
193 *mute = af->streamMute(stream);
194 return NO_ERROR;
195 }
196
setMode(audio_mode_t mode)197 status_t AudioSystem::setMode(audio_mode_t mode)
198 {
199 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
200 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
201 if (af == 0) return PERMISSION_DENIED;
202 return af->setMode(mode);
203 }
204
setParameters(audio_io_handle_t ioHandle,const String8 & keyValuePairs)205 status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
206 {
207 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
208 if (af == 0) return PERMISSION_DENIED;
209 return af->setParameters(ioHandle, keyValuePairs);
210 }
211
getParameters(audio_io_handle_t ioHandle,const String8 & keys)212 String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
213 {
214 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
215 String8 result = String8("");
216 if (af == 0) return result;
217
218 result = af->getParameters(ioHandle, keys);
219 return result;
220 }
221
setParameters(const String8 & keyValuePairs)222 status_t AudioSystem::setParameters(const String8& keyValuePairs)
223 {
224 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
225 }
226
getParameters(const String8 & keys)227 String8 AudioSystem::getParameters(const String8& keys)
228 {
229 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
230 }
231
232 // convert volume steps to natural log scale
233
234 // change this value to change volume scaling
235 static const float dBPerStep = 0.5f;
236 // shouldn't need to touch these
237 static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
238 static const float dBConvertInverse = 1.0f / dBConvert;
239
linearToLog(int volume)240 float AudioSystem::linearToLog(int volume)
241 {
242 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
243 // ALOGD("linearToLog(%d)=%f", volume, v);
244 // return v;
245 return volume ? exp(float(100 - volume) * dBConvert) : 0;
246 }
247
logToLinear(float volume)248 int AudioSystem::logToLinear(float volume)
249 {
250 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
251 // ALOGD("logTolinear(%d)=%f", v, volume);
252 // return v;
253 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
254 }
255
getOutputSamplingRate(uint32_t * samplingRate,audio_stream_type_t streamType)256 status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
257 {
258 audio_io_handle_t output;
259
260 if (streamType == AUDIO_STREAM_DEFAULT) {
261 streamType = AUDIO_STREAM_MUSIC;
262 }
263
264 output = getOutput(streamType);
265 if (output == 0) {
266 return PERMISSION_DENIED;
267 }
268
269 return getSamplingRate(output, samplingRate);
270 }
271
getSamplingRate(audio_io_handle_t ioHandle,uint32_t * samplingRate)272 status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
273 uint32_t* samplingRate)
274 {
275 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
276 if (af == 0) return PERMISSION_DENIED;
277 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
278 if (desc == 0) {
279 *samplingRate = af->sampleRate(ioHandle);
280 } else {
281 *samplingRate = desc->mSamplingRate;
282 }
283 if (*samplingRate == 0) {
284 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
285 return BAD_VALUE;
286 }
287
288 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
289
290 return NO_ERROR;
291 }
292
getOutputFrameCount(size_t * frameCount,audio_stream_type_t streamType)293 status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
294 {
295 audio_io_handle_t output;
296
297 if (streamType == AUDIO_STREAM_DEFAULT) {
298 streamType = AUDIO_STREAM_MUSIC;
299 }
300
301 output = getOutput(streamType);
302 if (output == AUDIO_IO_HANDLE_NONE) {
303 return PERMISSION_DENIED;
304 }
305
306 return getFrameCount(output, frameCount);
307 }
308
getFrameCount(audio_io_handle_t ioHandle,size_t * frameCount)309 status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
310 size_t* frameCount)
311 {
312 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
313 if (af == 0) return PERMISSION_DENIED;
314 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
315 if (desc == 0) {
316 *frameCount = af->frameCount(ioHandle);
317 } else {
318 *frameCount = desc->mFrameCount;
319 }
320 if (*frameCount == 0) {
321 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
322 return BAD_VALUE;
323 }
324
325 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
326
327 return NO_ERROR;
328 }
329
getOutputLatency(uint32_t * latency,audio_stream_type_t streamType)330 status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
331 {
332 audio_io_handle_t output;
333
334 if (streamType == AUDIO_STREAM_DEFAULT) {
335 streamType = AUDIO_STREAM_MUSIC;
336 }
337
338 output = getOutput(streamType);
339 if (output == AUDIO_IO_HANDLE_NONE) {
340 return PERMISSION_DENIED;
341 }
342
343 return getLatency(output, latency);
344 }
345
getLatency(audio_io_handle_t output,uint32_t * latency)346 status_t AudioSystem::getLatency(audio_io_handle_t output,
347 uint32_t* latency)
348 {
349 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
350 if (af == 0) return PERMISSION_DENIED;
351 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
352 if (outputDesc == 0) {
353 *latency = af->latency(output);
354 } else {
355 *latency = outputDesc->mLatency;
356 }
357
358 ALOGV("getLatency() output %d, latency %d", output, *latency);
359
360 return NO_ERROR;
361 }
362
getInputBufferSize(uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * buffSize)363 status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
364 audio_channel_mask_t channelMask, size_t* buffSize)
365 {
366 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
367 if (afc == 0) {
368 return NO_INIT;
369 }
370 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
371 }
372
setVoiceVolume(float value)373 status_t AudioSystem::setVoiceVolume(float value)
374 {
375 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
376 if (af == 0) return PERMISSION_DENIED;
377 return af->setVoiceVolume(value);
378 }
379
getRenderPosition(audio_io_handle_t output,uint32_t * halFrames,uint32_t * dspFrames)380 status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
381 uint32_t *dspFrames)
382 {
383 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
384 if (af == 0) return PERMISSION_DENIED;
385
386 return af->getRenderPosition(halFrames, dspFrames, output);
387 }
388
getInputFramesLost(audio_io_handle_t ioHandle)389 uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
390 {
391 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
392 uint32_t result = 0;
393 if (af == 0) return result;
394 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
395
396 result = af->getInputFramesLost(ioHandle);
397 return result;
398 }
399
newAudioUniqueId(audio_unique_id_use_t use)400 audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
401 {
402 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
403 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
404 return af->newAudioUniqueId(use);
405 }
406
acquireAudioSessionId(audio_session_t audioSession,pid_t pid)407 void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
408 {
409 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
410 if (af != 0) {
411 af->acquireAudioSessionId(audioSession, pid);
412 }
413 }
414
releaseAudioSessionId(audio_session_t audioSession,pid_t pid)415 void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
416 {
417 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
418 if (af != 0) {
419 af->releaseAudioSessionId(audioSession, pid);
420 }
421 }
422
getAudioHwSyncForSession(audio_session_t sessionId)423 audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
424 {
425 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
426 if (af == 0) return AUDIO_HW_SYNC_INVALID;
427 return af->getAudioHwSyncForSession(sessionId);
428 }
429
systemReady()430 status_t AudioSystem::systemReady()
431 {
432 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
433 if (af == 0) return NO_INIT;
434 return af->systemReady();
435 }
436
getFrameCountHAL(audio_io_handle_t ioHandle,size_t * frameCount)437 status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
438 size_t* frameCount)
439 {
440 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
441 if (af == 0) return PERMISSION_DENIED;
442 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
443 if (desc == 0) {
444 *frameCount = af->frameCountHAL(ioHandle);
445 } else {
446 *frameCount = desc->mFrameCountHAL;
447 }
448 if (*frameCount == 0) {
449 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
450 return BAD_VALUE;
451 }
452
453 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
454
455 return NO_ERROR;
456 }
457
458 // ---------------------------------------------------------------------------
459
460
clearIoCache()461 void AudioSystem::AudioFlingerClient::clearIoCache()
462 {
463 Mutex::Autolock _l(mLock);
464 mIoDescriptors.clear();
465 mInBuffSize = 0;
466 mInSamplingRate = 0;
467 mInFormat = AUDIO_FORMAT_DEFAULT;
468 mInChannelMask = AUDIO_CHANNEL_NONE;
469 }
470
binderDied(const wp<IBinder> & who __unused)471 void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
472 {
473 audio_error_callback cb = NULL;
474 {
475 Mutex::Autolock _l(AudioSystem::gLock);
476 AudioSystem::gAudioFlinger.clear();
477 cb = gAudioErrorCallback;
478 }
479
480 // clear output handles and stream to output map caches
481 clearIoCache();
482
483 if (cb) {
484 cb(DEAD_OBJECT);
485 }
486 ALOGW("AudioFlinger server died!");
487 }
488
ioConfigChanged(audio_io_config_event event,const sp<AudioIoDescriptor> & ioDesc)489 void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
490 const sp<AudioIoDescriptor>& ioDesc) {
491 ALOGV("ioConfigChanged() event %d", event);
492
493 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
494
495 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
496 Vector < sp<AudioDeviceCallback> > callbacks;
497
498 {
499 Mutex::Autolock _l(mLock);
500
501 switch (event) {
502 case AUDIO_OUTPUT_OPENED:
503 case AUDIO_INPUT_OPENED: {
504 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
505 if (oldDesc == 0) {
506 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
507 } else {
508 deviceId = oldDesc->getDeviceId();
509 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
510 }
511
512 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
513 deviceId = ioDesc->getDeviceId();
514 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
515 if (ioIndex >= 0) {
516 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
517 }
518 }
519 ALOGV("ioConfigChanged() new %s opened %d samplingRate %u, format %#x channel mask %#x "
520 "frameCount %zu deviceId %d", event == AUDIO_OUTPUT_OPENED ? "output" : "input",
521 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
522 ioDesc->mFrameCount, ioDesc->getDeviceId());
523 } break;
524 case AUDIO_OUTPUT_CLOSED:
525 case AUDIO_INPUT_CLOSED: {
526 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
527 ALOGW("ioConfigChanged() closing unknown %s %d",
528 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
529 break;
530 }
531 ALOGV("ioConfigChanged() %s %d closed",
532 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
533
534 mIoDescriptors.removeItem(ioDesc->mIoHandle);
535 mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
536 } break;
537
538 case AUDIO_OUTPUT_CONFIG_CHANGED:
539 case AUDIO_INPUT_CONFIG_CHANGED: {
540 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
541 if (oldDesc == 0) {
542 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
543 break;
544 }
545
546 deviceId = oldDesc->getDeviceId();
547 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
548
549 if (deviceId != ioDesc->getDeviceId()) {
550 deviceId = ioDesc->getDeviceId();
551 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
552 if (ioIndex >= 0) {
553 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
554 }
555 }
556 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
557 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
558 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
559 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
560 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
561 ioDesc->getDeviceId());
562
563 } break;
564 }
565 }
566 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
567 for (size_t i = 0; i < callbacks.size(); i++) {
568 callbacks[i]->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
569 }
570 }
571
getInputBufferSize(uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * buffSize)572 status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
573 uint32_t sampleRate, audio_format_t format,
574 audio_channel_mask_t channelMask, size_t* buffSize)
575 {
576 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
577 if (af == 0) {
578 return PERMISSION_DENIED;
579 }
580 Mutex::Autolock _l(mLock);
581 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
582 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
583 || (channelMask != mInChannelMask)) {
584 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
585 if (inBuffSize == 0) {
586 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
587 sampleRate, format, channelMask);
588 return BAD_VALUE;
589 }
590 // A benign race is possible here: we could overwrite a fresher cache entry
591 // save the request params
592 mInSamplingRate = sampleRate;
593 mInFormat = format;
594 mInChannelMask = channelMask;
595
596 mInBuffSize = inBuffSize;
597 }
598
599 *buffSize = mInBuffSize;
600
601 return NO_ERROR;
602 }
603
getIoDescriptor_l(audio_io_handle_t ioHandle)604 sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
605 {
606 sp<AudioIoDescriptor> desc;
607 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
608 if (index >= 0) {
609 desc = mIoDescriptors.valueAt(index);
610 }
611 return desc;
612 }
613
getIoDescriptor(audio_io_handle_t ioHandle)614 sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
615 {
616 Mutex::Autolock _l(mLock);
617 return getIoDescriptor_l(ioHandle);
618 }
619
addAudioDeviceCallback(const sp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo)620 status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
621 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
622 {
623 Mutex::Autolock _l(mLock);
624 Vector < sp<AudioDeviceCallback> > callbacks;
625 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
626 if (ioIndex >= 0) {
627 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
628 }
629
630 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
631 if (callbacks[cbIndex] == callback) {
632 return INVALID_OPERATION;
633 }
634 }
635 callbacks.add(callback);
636
637 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
638 return NO_ERROR;
639 }
640
removeAudioDeviceCallback(const sp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo)641 status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
642 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
643 {
644 Mutex::Autolock _l(mLock);
645 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
646 if (ioIndex < 0) {
647 return INVALID_OPERATION;
648 }
649 Vector < sp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
650
651 size_t cbIndex;
652 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
653 if (callbacks[cbIndex] == callback) {
654 break;
655 }
656 }
657 if (cbIndex == callbacks.size()) {
658 return INVALID_OPERATION;
659 }
660 callbacks.removeAt(cbIndex);
661 if (callbacks.size() != 0) {
662 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
663 } else {
664 mAudioDeviceCallbacks.removeItem(audioIo);
665 }
666 return NO_ERROR;
667 }
668
setErrorCallback(audio_error_callback cb)669 /* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
670 {
671 Mutex::Autolock _l(gLock);
672 gAudioErrorCallback = cb;
673 }
674
setDynPolicyCallback(dynamic_policy_callback cb)675 /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
676 {
677 Mutex::Autolock _l(gLock);
678 gDynPolicyCallback = cb;
679 }
680
setRecordConfigCallback(record_config_callback cb)681 /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
682 {
683 Mutex::Autolock _l(gLock);
684 gRecordConfigCallback = cb;
685 }
686
687 // client singleton for AudioPolicyService binder interface
688 // protected by gLockAPS
689 sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
690 sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
691
692
693 // establish binder interface to AudioPolicy service
get_audio_policy_service()694 const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
695 {
696 sp<IAudioPolicyService> ap;
697 sp<AudioPolicyServiceClient> apc;
698 {
699 Mutex::Autolock _l(gLockAPS);
700 if (gAudioPolicyService == 0) {
701 sp<IServiceManager> sm = defaultServiceManager();
702 sp<IBinder> binder;
703 do {
704 binder = sm->getService(String16("media.audio_policy"));
705 if (binder != 0)
706 break;
707 ALOGW("AudioPolicyService not published, waiting...");
708 usleep(500000); // 0.5 s
709 } while (true);
710 if (gAudioPolicyServiceClient == NULL) {
711 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
712 }
713 binder->linkToDeath(gAudioPolicyServiceClient);
714 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
715 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
716 apc = gAudioPolicyServiceClient;
717 // Make sure callbacks can be received by gAudioPolicyServiceClient
718 ProcessState::self()->startThreadPool();
719 }
720 ap = gAudioPolicyService;
721 }
722 if (apc != 0) {
723 ap->registerClient(apc);
724 }
725
726 return ap;
727 }
728
729 // ---------------------------------------------------------------------------
730
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address,const char * device_name)731 status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
732 audio_policy_dev_state_t state,
733 const char *device_address,
734 const char *device_name)
735 {
736 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
737 const char *address = "";
738 const char *name = "";
739
740 if (aps == 0) return PERMISSION_DENIED;
741
742 if (device_address != NULL) {
743 address = device_address;
744 }
745 if (device_name != NULL) {
746 name = device_name;
747 }
748 return aps->setDeviceConnectionState(device, state, address, name);
749 }
750
getDeviceConnectionState(audio_devices_t device,const char * device_address)751 audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
752 const char *device_address)
753 {
754 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
755 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
756
757 return aps->getDeviceConnectionState(device, device_address);
758 }
759
handleDeviceConfigChange(audio_devices_t device,const char * device_address,const char * device_name)760 status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
761 const char *device_address,
762 const char *device_name)
763 {
764 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
765 const char *address = "";
766 const char *name = "";
767
768 if (aps == 0) return PERMISSION_DENIED;
769
770 if (device_address != NULL) {
771 address = device_address;
772 }
773 if (device_name != NULL) {
774 name = device_name;
775 }
776 return aps->handleDeviceConfigChange(device, address, name);
777 }
778
setPhoneState(audio_mode_t state)779 status_t AudioSystem::setPhoneState(audio_mode_t state)
780 {
781 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
782 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
783 if (aps == 0) return PERMISSION_DENIED;
784
785 return aps->setPhoneState(state);
786 }
787
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)788 status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
789 {
790 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
791 if (aps == 0) return PERMISSION_DENIED;
792 return aps->setForceUse(usage, config);
793 }
794
getForceUse(audio_policy_force_use_t usage)795 audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
796 {
797 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
798 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
799 return aps->getForceUse(usage);
800 }
801
802
getOutput(audio_stream_type_t stream,uint32_t samplingRate,audio_format_t format,audio_channel_mask_t channelMask,audio_output_flags_t flags,const audio_offload_info_t * offloadInfo)803 audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
804 uint32_t samplingRate,
805 audio_format_t format,
806 audio_channel_mask_t channelMask,
807 audio_output_flags_t flags,
808 const audio_offload_info_t *offloadInfo)
809 {
810 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
811 if (aps == 0) return 0;
812 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
813 }
814
getOutputForAttr(const audio_attributes_t * attr,audio_io_handle_t * output,audio_session_t session,audio_stream_type_t * stream,uid_t uid,const audio_config_t * config,audio_output_flags_t flags,audio_port_handle_t selectedDeviceId,audio_port_handle_t * portId)815 status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
816 audio_io_handle_t *output,
817 audio_session_t session,
818 audio_stream_type_t *stream,
819 uid_t uid,
820 const audio_config_t *config,
821 audio_output_flags_t flags,
822 audio_port_handle_t selectedDeviceId,
823 audio_port_handle_t *portId)
824 {
825 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
826 if (aps == 0) return NO_INIT;
827 return aps->getOutputForAttr(attr, output, session, stream, uid,
828 config,
829 flags, selectedDeviceId, portId);
830 }
831
startOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)832 status_t AudioSystem::startOutput(audio_io_handle_t output,
833 audio_stream_type_t stream,
834 audio_session_t session)
835 {
836 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
837 if (aps == 0) return PERMISSION_DENIED;
838 return aps->startOutput(output, stream, session);
839 }
840
stopOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)841 status_t AudioSystem::stopOutput(audio_io_handle_t output,
842 audio_stream_type_t stream,
843 audio_session_t session)
844 {
845 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
846 if (aps == 0) return PERMISSION_DENIED;
847 return aps->stopOutput(output, stream, session);
848 }
849
releaseOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)850 void AudioSystem::releaseOutput(audio_io_handle_t output,
851 audio_stream_type_t stream,
852 audio_session_t session)
853 {
854 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
855 if (aps == 0) return;
856 aps->releaseOutput(output, stream, session);
857 }
858
getInputForAttr(const audio_attributes_t * attr,audio_io_handle_t * input,audio_session_t session,pid_t pid,uid_t uid,const audio_config_base_t * config,audio_input_flags_t flags,audio_port_handle_t selectedDeviceId,audio_port_handle_t * portId)859 status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
860 audio_io_handle_t *input,
861 audio_session_t session,
862 pid_t pid,
863 uid_t uid,
864 const audio_config_base_t *config,
865 audio_input_flags_t flags,
866 audio_port_handle_t selectedDeviceId,
867 audio_port_handle_t *portId)
868 {
869 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
870 if (aps == 0) return NO_INIT;
871 return aps->getInputForAttr(
872 attr, input, session, pid, uid,
873 config, flags, selectedDeviceId, portId);
874 }
875
startInput(audio_io_handle_t input,audio_session_t session)876 status_t AudioSystem::startInput(audio_io_handle_t input,
877 audio_session_t session)
878 {
879 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
880 if (aps == 0) return PERMISSION_DENIED;
881 return aps->startInput(input, session);
882 }
883
stopInput(audio_io_handle_t input,audio_session_t session)884 status_t AudioSystem::stopInput(audio_io_handle_t input,
885 audio_session_t session)
886 {
887 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
888 if (aps == 0) return PERMISSION_DENIED;
889 return aps->stopInput(input, session);
890 }
891
releaseInput(audio_io_handle_t input,audio_session_t session)892 void AudioSystem::releaseInput(audio_io_handle_t input,
893 audio_session_t session)
894 {
895 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
896 if (aps == 0) return;
897 aps->releaseInput(input, session);
898 }
899
initStreamVolume(audio_stream_type_t stream,int indexMin,int indexMax)900 status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
901 int indexMin,
902 int indexMax)
903 {
904 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
905 if (aps == 0) return PERMISSION_DENIED;
906 return aps->initStreamVolume(stream, indexMin, indexMax);
907 }
908
setStreamVolumeIndex(audio_stream_type_t stream,int index,audio_devices_t device)909 status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
910 int index,
911 audio_devices_t device)
912 {
913 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
914 if (aps == 0) return PERMISSION_DENIED;
915 return aps->setStreamVolumeIndex(stream, index, device);
916 }
917
getStreamVolumeIndex(audio_stream_type_t stream,int * index,audio_devices_t device)918 status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
919 int *index,
920 audio_devices_t device)
921 {
922 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
923 if (aps == 0) return PERMISSION_DENIED;
924 return aps->getStreamVolumeIndex(stream, index, device);
925 }
926
getStrategyForStream(audio_stream_type_t stream)927 uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
928 {
929 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
930 if (aps == 0) return 0;
931 return aps->getStrategyForStream(stream);
932 }
933
getDevicesForStream(audio_stream_type_t stream)934 audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
935 {
936 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
937 if (aps == 0) return AUDIO_DEVICE_NONE;
938 return aps->getDevicesForStream(stream);
939 }
940
getOutputForEffect(const effect_descriptor_t * desc)941 audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
942 {
943 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
944 // FIXME change return type to status_t, and return PERMISSION_DENIED here
945 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
946 return aps->getOutputForEffect(desc);
947 }
948
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,uint32_t strategy,audio_session_t session,int id)949 status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
950 audio_io_handle_t io,
951 uint32_t strategy,
952 audio_session_t session,
953 int id)
954 {
955 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
956 if (aps == 0) return PERMISSION_DENIED;
957 return aps->registerEffect(desc, io, strategy, session, id);
958 }
959
unregisterEffect(int id)960 status_t AudioSystem::unregisterEffect(int id)
961 {
962 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
963 if (aps == 0) return PERMISSION_DENIED;
964 return aps->unregisterEffect(id);
965 }
966
setEffectEnabled(int id,bool enabled)967 status_t AudioSystem::setEffectEnabled(int id, bool enabled)
968 {
969 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
970 if (aps == 0) return PERMISSION_DENIED;
971 return aps->setEffectEnabled(id, enabled);
972 }
973
isStreamActive(audio_stream_type_t stream,bool * state,uint32_t inPastMs)974 status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
975 {
976 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
977 if (aps == 0) return PERMISSION_DENIED;
978 if (state == NULL) return BAD_VALUE;
979 *state = aps->isStreamActive(stream, inPastMs);
980 return NO_ERROR;
981 }
982
isStreamActiveRemotely(audio_stream_type_t stream,bool * state,uint32_t inPastMs)983 status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
984 uint32_t inPastMs)
985 {
986 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
987 if (aps == 0) return PERMISSION_DENIED;
988 if (state == NULL) return BAD_VALUE;
989 *state = aps->isStreamActiveRemotely(stream, inPastMs);
990 return NO_ERROR;
991 }
992
isSourceActive(audio_source_t stream,bool * state)993 status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
994 {
995 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
996 if (aps == 0) return PERMISSION_DENIED;
997 if (state == NULL) return BAD_VALUE;
998 *state = aps->isSourceActive(stream);
999 return NO_ERROR;
1000 }
1001
getPrimaryOutputSamplingRate()1002 uint32_t AudioSystem::getPrimaryOutputSamplingRate()
1003 {
1004 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1005 if (af == 0) return 0;
1006 return af->getPrimaryOutputSamplingRate();
1007 }
1008
getPrimaryOutputFrameCount()1009 size_t AudioSystem::getPrimaryOutputFrameCount()
1010 {
1011 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1012 if (af == 0) return 0;
1013 return af->getPrimaryOutputFrameCount();
1014 }
1015
setLowRamDevice(bool isLowRamDevice)1016 status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
1017 {
1018 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1019 if (af == 0) return PERMISSION_DENIED;
1020 return af->setLowRamDevice(isLowRamDevice);
1021 }
1022
clearAudioConfigCache()1023 void AudioSystem::clearAudioConfigCache()
1024 {
1025 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
1026 ALOGV("clearAudioConfigCache()");
1027 {
1028 Mutex::Autolock _l(gLock);
1029 if (gAudioFlingerClient != 0) {
1030 gAudioFlingerClient->clearIoCache();
1031 }
1032 gAudioFlinger.clear();
1033 }
1034 {
1035 Mutex::Autolock _l(gLockAPS);
1036 gAudioPolicyService.clear();
1037 }
1038 }
1039
isOffloadSupported(const audio_offload_info_t & info)1040 bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1041 {
1042 ALOGV("isOffloadSupported()");
1043 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1044 if (aps == 0) return false;
1045 return aps->isOffloadSupported(info);
1046 }
1047
listAudioPorts(audio_port_role_t role,audio_port_type_t type,unsigned int * num_ports,struct audio_port * ports,unsigned int * generation)1048 status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1049 audio_port_type_t type,
1050 unsigned int *num_ports,
1051 struct audio_port *ports,
1052 unsigned int *generation)
1053 {
1054 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1055 if (aps == 0) return PERMISSION_DENIED;
1056 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1057 }
1058
getAudioPort(struct audio_port * port)1059 status_t AudioSystem::getAudioPort(struct audio_port *port)
1060 {
1061 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1062 if (aps == 0) return PERMISSION_DENIED;
1063 return aps->getAudioPort(port);
1064 }
1065
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle)1066 status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1067 audio_patch_handle_t *handle)
1068 {
1069 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1070 if (aps == 0) return PERMISSION_DENIED;
1071 return aps->createAudioPatch(patch, handle);
1072 }
1073
releaseAudioPatch(audio_patch_handle_t handle)1074 status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1075 {
1076 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1077 if (aps == 0) return PERMISSION_DENIED;
1078 return aps->releaseAudioPatch(handle);
1079 }
1080
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches,unsigned int * generation)1081 status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1082 struct audio_patch *patches,
1083 unsigned int *generation)
1084 {
1085 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1086 if (aps == 0) return PERMISSION_DENIED;
1087 return aps->listAudioPatches(num_patches, patches, generation);
1088 }
1089
setAudioPortConfig(const struct audio_port_config * config)1090 status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1091 {
1092 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1093 if (aps == 0) return PERMISSION_DENIED;
1094 return aps->setAudioPortConfig(config);
1095 }
1096
addAudioPortCallback(const sp<AudioPortCallback> & callback)1097 status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
1098 {
1099 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1100 if (aps == 0) return PERMISSION_DENIED;
1101
1102 Mutex::Autolock _l(gLockAPS);
1103 if (gAudioPolicyServiceClient == 0) {
1104 return NO_INIT;
1105 }
1106 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1107 if (ret == 1) {
1108 aps->setAudioPortCallbacksEnabled(true);
1109 }
1110 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1111 }
1112
1113 /*static*/
removeAudioPortCallback(const sp<AudioPortCallback> & callback)1114 status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
1115 {
1116 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1117 if (aps == 0) return PERMISSION_DENIED;
1118
1119 Mutex::Autolock _l(gLockAPS);
1120 if (gAudioPolicyServiceClient == 0) {
1121 return NO_INIT;
1122 }
1123 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1124 if (ret == 0) {
1125 aps->setAudioPortCallbacksEnabled(false);
1126 }
1127 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1128 }
1129
addAudioDeviceCallback(const sp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo)1130 status_t AudioSystem::addAudioDeviceCallback(
1131 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
1132 {
1133 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1134 if (afc == 0) {
1135 return NO_INIT;
1136 }
1137 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1138 if (status == NO_ERROR) {
1139 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1140 if (af != 0) {
1141 af->registerClient(afc);
1142 }
1143 }
1144 return status;
1145 }
1146
removeAudioDeviceCallback(const sp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo)1147 status_t AudioSystem::removeAudioDeviceCallback(
1148 const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
1149 {
1150 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1151 if (afc == 0) {
1152 return NO_INIT;
1153 }
1154 return afc->removeAudioDeviceCallback(callback, audioIo);
1155 }
1156
getDeviceIdForIo(audio_io_handle_t audioIo)1157 audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1158 {
1159 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1160 if (af == 0) return PERMISSION_DENIED;
1161 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1162 if (desc == 0) {
1163 return AUDIO_PORT_HANDLE_NONE;
1164 }
1165 return desc->getDeviceId();
1166 }
1167
acquireSoundTriggerSession(audio_session_t * session,audio_io_handle_t * ioHandle,audio_devices_t * device)1168 status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1169 audio_io_handle_t *ioHandle,
1170 audio_devices_t *device)
1171 {
1172 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1173 if (aps == 0) return PERMISSION_DENIED;
1174 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1175 }
1176
releaseSoundTriggerSession(audio_session_t session)1177 status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1178 {
1179 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1180 if (aps == 0) return PERMISSION_DENIED;
1181 return aps->releaseSoundTriggerSession(session);
1182 }
1183
getPhoneState()1184 audio_mode_t AudioSystem::getPhoneState()
1185 {
1186 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1187 if (aps == 0) return AUDIO_MODE_INVALID;
1188 return aps->getPhoneState();
1189 }
1190
registerPolicyMixes(const Vector<AudioMix> & mixes,bool registration)1191 status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
1192 {
1193 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1194 if (aps == 0) return PERMISSION_DENIED;
1195 return aps->registerPolicyMixes(mixes, registration);
1196 }
1197
startAudioSource(const struct audio_port_config * source,const audio_attributes_t * attributes,audio_patch_handle_t * handle)1198 status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1199 const audio_attributes_t *attributes,
1200 audio_patch_handle_t *handle)
1201 {
1202 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1203 if (aps == 0) return PERMISSION_DENIED;
1204 return aps->startAudioSource(source, attributes, handle);
1205 }
1206
stopAudioSource(audio_patch_handle_t handle)1207 status_t AudioSystem::stopAudioSource(audio_patch_handle_t handle)
1208 {
1209 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1210 if (aps == 0) return PERMISSION_DENIED;
1211 return aps->stopAudioSource(handle);
1212 }
1213
setMasterMono(bool mono)1214 status_t AudioSystem::setMasterMono(bool mono)
1215 {
1216 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1217 if (aps == 0) return PERMISSION_DENIED;
1218 return aps->setMasterMono(mono);
1219 }
1220
getMasterMono(bool * mono)1221 status_t AudioSystem::getMasterMono(bool *mono)
1222 {
1223 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1224 if (aps == 0) return PERMISSION_DENIED;
1225 return aps->getMasterMono(mono);
1226 }
1227
1228 // ---------------------------------------------------------------------------
1229
addAudioPortCallback(const sp<AudioPortCallback> & callback)1230 int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1231 const sp<AudioPortCallback>& callback)
1232 {
1233 Mutex::Autolock _l(mLock);
1234 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1235 if (mAudioPortCallbacks[i] == callback) {
1236 return -1;
1237 }
1238 }
1239 mAudioPortCallbacks.add(callback);
1240 return mAudioPortCallbacks.size();
1241 }
1242
removeAudioPortCallback(const sp<AudioPortCallback> & callback)1243 int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1244 const sp<AudioPortCallback>& callback)
1245 {
1246 Mutex::Autolock _l(mLock);
1247 size_t i;
1248 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1249 if (mAudioPortCallbacks[i] == callback) {
1250 break;
1251 }
1252 }
1253 if (i == mAudioPortCallbacks.size()) {
1254 return -1;
1255 }
1256 mAudioPortCallbacks.removeAt(i);
1257 return mAudioPortCallbacks.size();
1258 }
1259
1260
onAudioPortListUpdate()1261 void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1262 {
1263 Mutex::Autolock _l(mLock);
1264 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1265 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1266 }
1267 }
1268
onAudioPatchListUpdate()1269 void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1270 {
1271 Mutex::Autolock _l(mLock);
1272 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1273 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1274 }
1275 }
1276
onDynamicPolicyMixStateUpdate(String8 regId,int32_t state)1277 void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1278 String8 regId, int32_t state)
1279 {
1280 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1281 dynamic_policy_callback cb = NULL;
1282 {
1283 Mutex::Autolock _l(AudioSystem::gLock);
1284 cb = gDynPolicyCallback;
1285 }
1286
1287 if (cb != NULL) {
1288 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1289 }
1290 }
1291
onRecordingConfigurationUpdate(int event,audio_session_t session,audio_source_t source,const audio_config_base_t * clientConfig,const audio_config_base_t * deviceConfig,audio_patch_handle_t patchHandle)1292 void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
1293 int event, audio_session_t session, audio_source_t source,
1294 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1295 audio_patch_handle_t patchHandle) {
1296 record_config_callback cb = NULL;
1297 {
1298 Mutex::Autolock _l(AudioSystem::gLock);
1299 cb = gRecordConfigCallback;
1300 }
1301
1302 if (cb != NULL) {
1303 cb(event, session, source, clientConfig, deviceConfig, patchHandle);
1304 }
1305 }
1306
binderDied(const wp<IBinder> & who __unused)1307 void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1308 {
1309 {
1310 Mutex::Autolock _l(mLock);
1311 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1312 mAudioPortCallbacks[i]->onServiceDied();
1313 }
1314 }
1315 {
1316 Mutex::Autolock _l(gLockAPS);
1317 AudioSystem::gAudioPolicyService.clear();
1318 }
1319
1320 ALOGW("AudioPolicyService server died!");
1321 }
1322
1323 } // namespace android
1324