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 < wp<AudioDeviceCallback> > callbacks;
497
498 {
499 Mutex::Autolock _l(mLock);
500
501 switch (event) {
502 case AUDIO_OUTPUT_OPENED:
503 case AUDIO_OUTPUT_REGISTERED:
504 case AUDIO_INPUT_OPENED:
505 case AUDIO_INPUT_REGISTERED: {
506 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
507 if (oldDesc == 0) {
508 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
509 } else {
510 deviceId = oldDesc->getDeviceId();
511 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
512 }
513
514 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
515 deviceId = ioDesc->getDeviceId();
516 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
517 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
518 if (ioIndex >= 0) {
519 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
520 }
521 }
522 }
523 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
524 "frameCount %zu deviceId %d",
525 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
526 "output" : "input",
527 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
528 "opened" : "registered",
529 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
530 ioDesc->mFrameCount, ioDesc->getDeviceId());
531 } break;
532 case AUDIO_OUTPUT_CLOSED:
533 case AUDIO_INPUT_CLOSED: {
534 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
535 ALOGW("ioConfigChanged() closing unknown %s %d",
536 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
537 break;
538 }
539 ALOGV("ioConfigChanged() %s %d closed",
540 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
541
542 mIoDescriptors.removeItem(ioDesc->mIoHandle);
543 mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
544 } break;
545
546 case AUDIO_OUTPUT_CONFIG_CHANGED:
547 case AUDIO_INPUT_CONFIG_CHANGED: {
548 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
549 if (oldDesc == 0) {
550 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
551 break;
552 }
553
554 deviceId = oldDesc->getDeviceId();
555 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
556
557 if (deviceId != ioDesc->getDeviceId()) {
558 deviceId = ioDesc->getDeviceId();
559 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
560 if (ioIndex >= 0) {
561 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
562 }
563 }
564 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
565 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
566 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
567 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
568 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
569 ioDesc->getDeviceId());
570
571 } break;
572 }
573 }
574 bool callbackRemoved = false;
575 // callbacks.size() != 0 => ioDesc->mIoHandle and deviceId are valid
576 for (size_t i = 0; i < callbacks.size(); ) {
577 sp<AudioDeviceCallback> callback = callbacks[i].promote();
578 if (callback.get() != nullptr) {
579 callback->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
580 i++;
581 } else {
582 callbacks.removeAt(i);
583 callbackRemoved = true;
584 }
585 }
586 // clean up callback list while we are here if some clients have disappeared without
587 // unregistering their callback
588 if (callbackRemoved) {
589 Mutex::Autolock _l(mLock);
590 mAudioDeviceCallbacks.replaceValueFor(ioDesc->mIoHandle, callbacks);
591 }
592 }
593
getInputBufferSize(uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * buffSize)594 status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
595 uint32_t sampleRate, audio_format_t format,
596 audio_channel_mask_t channelMask, size_t* buffSize)
597 {
598 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
599 if (af == 0) {
600 return PERMISSION_DENIED;
601 }
602 Mutex::Autolock _l(mLock);
603 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
604 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
605 || (channelMask != mInChannelMask)) {
606 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
607 if (inBuffSize == 0) {
608 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
609 sampleRate, format, channelMask);
610 return BAD_VALUE;
611 }
612 // A benign race is possible here: we could overwrite a fresher cache entry
613 // save the request params
614 mInSamplingRate = sampleRate;
615 mInFormat = format;
616 mInChannelMask = channelMask;
617
618 mInBuffSize = inBuffSize;
619 }
620
621 *buffSize = mInBuffSize;
622
623 return NO_ERROR;
624 }
625
getIoDescriptor_l(audio_io_handle_t ioHandle)626 sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
627 {
628 sp<AudioIoDescriptor> desc;
629 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
630 if (index >= 0) {
631 desc = mIoDescriptors.valueAt(index);
632 }
633 return desc;
634 }
635
getIoDescriptor(audio_io_handle_t ioHandle)636 sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
637 {
638 Mutex::Autolock _l(mLock);
639 return getIoDescriptor_l(ioHandle);
640 }
641
addAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo)642 status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
643 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
644 {
645 Mutex::Autolock _l(mLock);
646 Vector < wp<AudioDeviceCallback> > callbacks;
647 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
648 if (ioIndex >= 0) {
649 callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
650 }
651
652 for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
653 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
654 return INVALID_OPERATION;
655 }
656 }
657 callbacks.add(callback);
658
659 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
660 return NO_ERROR;
661 }
662
removeAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo)663 status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
664 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
665 {
666 Mutex::Autolock _l(mLock);
667 ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
668 if (ioIndex < 0) {
669 return INVALID_OPERATION;
670 }
671 Vector < wp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
672
673 size_t cbIndex;
674 for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
675 if (callbacks[cbIndex].unsafe_get() == callback.unsafe_get()) {
676 break;
677 }
678 }
679 if (cbIndex == callbacks.size()) {
680 return INVALID_OPERATION;
681 }
682 callbacks.removeAt(cbIndex);
683 if (callbacks.size() != 0) {
684 mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
685 } else {
686 mAudioDeviceCallbacks.removeItem(audioIo);
687 }
688 return NO_ERROR;
689 }
690
setErrorCallback(audio_error_callback cb)691 /* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
692 {
693 Mutex::Autolock _l(gLock);
694 gAudioErrorCallback = cb;
695 }
696
setDynPolicyCallback(dynamic_policy_callback cb)697 /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
698 {
699 Mutex::Autolock _l(gLock);
700 gDynPolicyCallback = cb;
701 }
702
setRecordConfigCallback(record_config_callback cb)703 /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
704 {
705 Mutex::Autolock _l(gLock);
706 gRecordConfigCallback = cb;
707 }
708
709 // client singleton for AudioPolicyService binder interface
710 // protected by gLockAPS
711 sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
712 sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
713
714
715 // establish binder interface to AudioPolicy service
get_audio_policy_service()716 const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
717 {
718 sp<IAudioPolicyService> ap;
719 sp<AudioPolicyServiceClient> apc;
720 {
721 Mutex::Autolock _l(gLockAPS);
722 if (gAudioPolicyService == 0) {
723 sp<IServiceManager> sm = defaultServiceManager();
724 sp<IBinder> binder;
725 do {
726 binder = sm->getService(String16("media.audio_policy"));
727 if (binder != 0)
728 break;
729 ALOGW("AudioPolicyService not published, waiting...");
730 usleep(500000); // 0.5 s
731 } while (true);
732 if (gAudioPolicyServiceClient == NULL) {
733 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
734 }
735 binder->linkToDeath(gAudioPolicyServiceClient);
736 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
737 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
738 apc = gAudioPolicyServiceClient;
739 // Make sure callbacks can be received by gAudioPolicyServiceClient
740 ProcessState::self()->startThreadPool();
741 }
742 ap = gAudioPolicyService;
743 }
744 if (apc != 0) {
745 ap->registerClient(apc);
746 }
747
748 return ap;
749 }
750
751 // ---------------------------------------------------------------------------
752
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address,const char * device_name)753 status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
754 audio_policy_dev_state_t state,
755 const char *device_address,
756 const char *device_name)
757 {
758 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
759 const char *address = "";
760 const char *name = "";
761
762 if (aps == 0) return PERMISSION_DENIED;
763
764 if (device_address != NULL) {
765 address = device_address;
766 }
767 if (device_name != NULL) {
768 name = device_name;
769 }
770 return aps->setDeviceConnectionState(device, state, address, name);
771 }
772
getDeviceConnectionState(audio_devices_t device,const char * device_address)773 audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
774 const char *device_address)
775 {
776 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
777 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
778
779 return aps->getDeviceConnectionState(device, device_address);
780 }
781
handleDeviceConfigChange(audio_devices_t device,const char * device_address,const char * device_name)782 status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
783 const char *device_address,
784 const char *device_name)
785 {
786 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
787 const char *address = "";
788 const char *name = "";
789
790 if (aps == 0) return PERMISSION_DENIED;
791
792 if (device_address != NULL) {
793 address = device_address;
794 }
795 if (device_name != NULL) {
796 name = device_name;
797 }
798 return aps->handleDeviceConfigChange(device, address, name);
799 }
800
setPhoneState(audio_mode_t state)801 status_t AudioSystem::setPhoneState(audio_mode_t state)
802 {
803 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
804 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
805 if (aps == 0) return PERMISSION_DENIED;
806
807 return aps->setPhoneState(state);
808 }
809
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)810 status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
811 {
812 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
813 if (aps == 0) return PERMISSION_DENIED;
814 return aps->setForceUse(usage, config);
815 }
816
getForceUse(audio_policy_force_use_t usage)817 audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
818 {
819 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
820 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
821 return aps->getForceUse(usage);
822 }
823
824
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)825 audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
826 uint32_t samplingRate,
827 audio_format_t format,
828 audio_channel_mask_t channelMask,
829 audio_output_flags_t flags,
830 const audio_offload_info_t *offloadInfo)
831 {
832 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
833 if (aps == 0) return 0;
834 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
835 }
836
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)837 status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
838 audio_io_handle_t *output,
839 audio_session_t session,
840 audio_stream_type_t *stream,
841 uid_t uid,
842 const audio_config_t *config,
843 audio_output_flags_t flags,
844 audio_port_handle_t *selectedDeviceId,
845 audio_port_handle_t *portId)
846 {
847 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
848 if (aps == 0) return NO_INIT;
849 return aps->getOutputForAttr(attr, output, session, stream, uid,
850 config,
851 flags, selectedDeviceId, portId);
852 }
853
startOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)854 status_t AudioSystem::startOutput(audio_io_handle_t output,
855 audio_stream_type_t stream,
856 audio_session_t session)
857 {
858 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
859 if (aps == 0) return PERMISSION_DENIED;
860 return aps->startOutput(output, stream, session);
861 }
862
stopOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)863 status_t AudioSystem::stopOutput(audio_io_handle_t output,
864 audio_stream_type_t stream,
865 audio_session_t session)
866 {
867 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
868 if (aps == 0) return PERMISSION_DENIED;
869 return aps->stopOutput(output, stream, session);
870 }
871
releaseOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)872 void AudioSystem::releaseOutput(audio_io_handle_t output,
873 audio_stream_type_t stream,
874 audio_session_t session)
875 {
876 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
877 if (aps == 0) return;
878 aps->releaseOutput(output, stream, session);
879 }
880
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)881 status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
882 audio_io_handle_t *input,
883 audio_session_t session,
884 pid_t pid,
885 uid_t uid,
886 const audio_config_base_t *config,
887 audio_input_flags_t flags,
888 audio_port_handle_t *selectedDeviceId,
889 audio_port_handle_t *portId)
890 {
891 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
892 if (aps == 0) return NO_INIT;
893 return aps->getInputForAttr(
894 attr, input, session, pid, uid,
895 config, flags, selectedDeviceId, portId);
896 }
897
startInput(audio_io_handle_t input,audio_session_t session)898 status_t AudioSystem::startInput(audio_io_handle_t input,
899 audio_session_t session)
900 {
901 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
902 if (aps == 0) return PERMISSION_DENIED;
903 return aps->startInput(input, session);
904 }
905
stopInput(audio_io_handle_t input,audio_session_t session)906 status_t AudioSystem::stopInput(audio_io_handle_t input,
907 audio_session_t session)
908 {
909 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
910 if (aps == 0) return PERMISSION_DENIED;
911 return aps->stopInput(input, session);
912 }
913
releaseInput(audio_io_handle_t input,audio_session_t session)914 void AudioSystem::releaseInput(audio_io_handle_t input,
915 audio_session_t session)
916 {
917 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
918 if (aps == 0) return;
919 aps->releaseInput(input, session);
920 }
921
initStreamVolume(audio_stream_type_t stream,int indexMin,int indexMax)922 status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
923 int indexMin,
924 int indexMax)
925 {
926 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
927 if (aps == 0) return PERMISSION_DENIED;
928 return aps->initStreamVolume(stream, indexMin, indexMax);
929 }
930
setStreamVolumeIndex(audio_stream_type_t stream,int index,audio_devices_t device)931 status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
932 int index,
933 audio_devices_t device)
934 {
935 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
936 if (aps == 0) return PERMISSION_DENIED;
937 return aps->setStreamVolumeIndex(stream, index, device);
938 }
939
getStreamVolumeIndex(audio_stream_type_t stream,int * index,audio_devices_t device)940 status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
941 int *index,
942 audio_devices_t device)
943 {
944 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
945 if (aps == 0) return PERMISSION_DENIED;
946 return aps->getStreamVolumeIndex(stream, index, device);
947 }
948
getStrategyForStream(audio_stream_type_t stream)949 uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
950 {
951 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
952 if (aps == 0) return 0;
953 return aps->getStrategyForStream(stream);
954 }
955
getDevicesForStream(audio_stream_type_t stream)956 audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
957 {
958 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
959 if (aps == 0) return AUDIO_DEVICE_NONE;
960 return aps->getDevicesForStream(stream);
961 }
962
getOutputForEffect(const effect_descriptor_t * desc)963 audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
964 {
965 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
966 // FIXME change return type to status_t, and return PERMISSION_DENIED here
967 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
968 return aps->getOutputForEffect(desc);
969 }
970
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,uint32_t strategy,audio_session_t session,int id)971 status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
972 audio_io_handle_t io,
973 uint32_t strategy,
974 audio_session_t session,
975 int id)
976 {
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
978 if (aps == 0) return PERMISSION_DENIED;
979 return aps->registerEffect(desc, io, strategy, session, id);
980 }
981
unregisterEffect(int id)982 status_t AudioSystem::unregisterEffect(int id)
983 {
984 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
985 if (aps == 0) return PERMISSION_DENIED;
986 return aps->unregisterEffect(id);
987 }
988
setEffectEnabled(int id,bool enabled)989 status_t AudioSystem::setEffectEnabled(int id, bool enabled)
990 {
991 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
992 if (aps == 0) return PERMISSION_DENIED;
993 return aps->setEffectEnabled(id, enabled);
994 }
995
isStreamActive(audio_stream_type_t stream,bool * state,uint32_t inPastMs)996 status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
997 {
998 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
999 if (aps == 0) return PERMISSION_DENIED;
1000 if (state == NULL) return BAD_VALUE;
1001 *state = aps->isStreamActive(stream, inPastMs);
1002 return NO_ERROR;
1003 }
1004
isStreamActiveRemotely(audio_stream_type_t stream,bool * state,uint32_t inPastMs)1005 status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1006 uint32_t inPastMs)
1007 {
1008 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1009 if (aps == 0) return PERMISSION_DENIED;
1010 if (state == NULL) return BAD_VALUE;
1011 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1012 return NO_ERROR;
1013 }
1014
isSourceActive(audio_source_t stream,bool * state)1015 status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1016 {
1017 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1018 if (aps == 0) return PERMISSION_DENIED;
1019 if (state == NULL) return BAD_VALUE;
1020 *state = aps->isSourceActive(stream);
1021 return NO_ERROR;
1022 }
1023
getPrimaryOutputSamplingRate()1024 uint32_t AudioSystem::getPrimaryOutputSamplingRate()
1025 {
1026 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1027 if (af == 0) return 0;
1028 return af->getPrimaryOutputSamplingRate();
1029 }
1030
getPrimaryOutputFrameCount()1031 size_t AudioSystem::getPrimaryOutputFrameCount()
1032 {
1033 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1034 if (af == 0) return 0;
1035 return af->getPrimaryOutputFrameCount();
1036 }
1037
setLowRamDevice(bool isLowRamDevice)1038 status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
1039 {
1040 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1041 if (af == 0) return PERMISSION_DENIED;
1042 return af->setLowRamDevice(isLowRamDevice);
1043 }
1044
clearAudioConfigCache()1045 void AudioSystem::clearAudioConfigCache()
1046 {
1047 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
1048 ALOGV("clearAudioConfigCache()");
1049 {
1050 Mutex::Autolock _l(gLock);
1051 if (gAudioFlingerClient != 0) {
1052 gAudioFlingerClient->clearIoCache();
1053 }
1054 gAudioFlinger.clear();
1055 }
1056 {
1057 Mutex::Autolock _l(gLockAPS);
1058 gAudioPolicyService.clear();
1059 }
1060 }
1061
isOffloadSupported(const audio_offload_info_t & info)1062 bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1063 {
1064 ALOGV("isOffloadSupported()");
1065 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1066 if (aps == 0) return false;
1067 return aps->isOffloadSupported(info);
1068 }
1069
listAudioPorts(audio_port_role_t role,audio_port_type_t type,unsigned int * num_ports,struct audio_port * ports,unsigned int * generation)1070 status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1071 audio_port_type_t type,
1072 unsigned int *num_ports,
1073 struct audio_port *ports,
1074 unsigned int *generation)
1075 {
1076 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1077 if (aps == 0) return PERMISSION_DENIED;
1078 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1079 }
1080
getAudioPort(struct audio_port * port)1081 status_t AudioSystem::getAudioPort(struct audio_port *port)
1082 {
1083 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1084 if (aps == 0) return PERMISSION_DENIED;
1085 return aps->getAudioPort(port);
1086 }
1087
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle)1088 status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1089 audio_patch_handle_t *handle)
1090 {
1091 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1092 if (aps == 0) return PERMISSION_DENIED;
1093 return aps->createAudioPatch(patch, handle);
1094 }
1095
releaseAudioPatch(audio_patch_handle_t handle)1096 status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1097 {
1098 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1099 if (aps == 0) return PERMISSION_DENIED;
1100 return aps->releaseAudioPatch(handle);
1101 }
1102
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches,unsigned int * generation)1103 status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1104 struct audio_patch *patches,
1105 unsigned int *generation)
1106 {
1107 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1108 if (aps == 0) return PERMISSION_DENIED;
1109 return aps->listAudioPatches(num_patches, patches, generation);
1110 }
1111
setAudioPortConfig(const struct audio_port_config * config)1112 status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1113 {
1114 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1115 if (aps == 0) return PERMISSION_DENIED;
1116 return aps->setAudioPortConfig(config);
1117 }
1118
addAudioPortCallback(const sp<AudioPortCallback> & callback)1119 status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
1120 {
1121 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1122 if (aps == 0) return PERMISSION_DENIED;
1123
1124 Mutex::Autolock _l(gLockAPS);
1125 if (gAudioPolicyServiceClient == 0) {
1126 return NO_INIT;
1127 }
1128 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1129 if (ret == 1) {
1130 aps->setAudioPortCallbacksEnabled(true);
1131 }
1132 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1133 }
1134
1135 /*static*/
removeAudioPortCallback(const sp<AudioPortCallback> & callback)1136 status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
1137 {
1138 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1139 if (aps == 0) return PERMISSION_DENIED;
1140
1141 Mutex::Autolock _l(gLockAPS);
1142 if (gAudioPolicyServiceClient == 0) {
1143 return NO_INIT;
1144 }
1145 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1146 if (ret == 0) {
1147 aps->setAudioPortCallbacksEnabled(false);
1148 }
1149 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1150 }
1151
addAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo)1152 status_t AudioSystem::addAudioDeviceCallback(
1153 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
1154 {
1155 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1156 if (afc == 0) {
1157 return NO_INIT;
1158 }
1159 status_t status = afc->addAudioDeviceCallback(callback, audioIo);
1160 if (status == NO_ERROR) {
1161 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1162 if (af != 0) {
1163 af->registerClient(afc);
1164 }
1165 }
1166 return status;
1167 }
1168
removeAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo)1169 status_t AudioSystem::removeAudioDeviceCallback(
1170 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
1171 {
1172 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1173 if (afc == 0) {
1174 return NO_INIT;
1175 }
1176 return afc->removeAudioDeviceCallback(callback, audioIo);
1177 }
1178
getDeviceIdForIo(audio_io_handle_t audioIo)1179 audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1180 {
1181 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1182 if (af == 0) return PERMISSION_DENIED;
1183 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1184 if (desc == 0) {
1185 return AUDIO_PORT_HANDLE_NONE;
1186 }
1187 return desc->getDeviceId();
1188 }
1189
acquireSoundTriggerSession(audio_session_t * session,audio_io_handle_t * ioHandle,audio_devices_t * device)1190 status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1191 audio_io_handle_t *ioHandle,
1192 audio_devices_t *device)
1193 {
1194 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1195 if (aps == 0) return PERMISSION_DENIED;
1196 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1197 }
1198
releaseSoundTriggerSession(audio_session_t session)1199 status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1200 {
1201 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1202 if (aps == 0) return PERMISSION_DENIED;
1203 return aps->releaseSoundTriggerSession(session);
1204 }
1205
getPhoneState()1206 audio_mode_t AudioSystem::getPhoneState()
1207 {
1208 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1209 if (aps == 0) return AUDIO_MODE_INVALID;
1210 return aps->getPhoneState();
1211 }
1212
registerPolicyMixes(const Vector<AudioMix> & mixes,bool registration)1213 status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
1214 {
1215 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1216 if (aps == 0) return PERMISSION_DENIED;
1217 return aps->registerPolicyMixes(mixes, registration);
1218 }
1219
startAudioSource(const struct audio_port_config * source,const audio_attributes_t * attributes,audio_patch_handle_t * handle)1220 status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1221 const audio_attributes_t *attributes,
1222 audio_patch_handle_t *handle)
1223 {
1224 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1225 if (aps == 0) return PERMISSION_DENIED;
1226 return aps->startAudioSource(source, attributes, handle);
1227 }
1228
stopAudioSource(audio_patch_handle_t handle)1229 status_t AudioSystem::stopAudioSource(audio_patch_handle_t handle)
1230 {
1231 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1232 if (aps == 0) return PERMISSION_DENIED;
1233 return aps->stopAudioSource(handle);
1234 }
1235
setMasterMono(bool mono)1236 status_t AudioSystem::setMasterMono(bool mono)
1237 {
1238 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1239 if (aps == 0) return PERMISSION_DENIED;
1240 return aps->setMasterMono(mono);
1241 }
1242
getMasterMono(bool * mono)1243 status_t AudioSystem::getMasterMono(bool *mono)
1244 {
1245 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1246 if (aps == 0) return PERMISSION_DENIED;
1247 return aps->getMasterMono(mono);
1248 }
1249
getStreamVolumeDB(audio_stream_type_t stream,int index,audio_devices_t device)1250 float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1251 {
1252 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1253 if (aps == 0) return NAN;
1254 return aps->getStreamVolumeDB(stream, index, device);
1255 }
1256
1257 // ---------------------------------------------------------------------------
1258
addAudioPortCallback(const sp<AudioPortCallback> & callback)1259 int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1260 const sp<AudioPortCallback>& callback)
1261 {
1262 Mutex::Autolock _l(mLock);
1263 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1264 if (mAudioPortCallbacks[i] == callback) {
1265 return -1;
1266 }
1267 }
1268 mAudioPortCallbacks.add(callback);
1269 return mAudioPortCallbacks.size();
1270 }
1271
removeAudioPortCallback(const sp<AudioPortCallback> & callback)1272 int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1273 const sp<AudioPortCallback>& callback)
1274 {
1275 Mutex::Autolock _l(mLock);
1276 size_t i;
1277 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1278 if (mAudioPortCallbacks[i] == callback) {
1279 break;
1280 }
1281 }
1282 if (i == mAudioPortCallbacks.size()) {
1283 return -1;
1284 }
1285 mAudioPortCallbacks.removeAt(i);
1286 return mAudioPortCallbacks.size();
1287 }
1288
1289
onAudioPortListUpdate()1290 void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1291 {
1292 Mutex::Autolock _l(mLock);
1293 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1294 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1295 }
1296 }
1297
onAudioPatchListUpdate()1298 void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1299 {
1300 Mutex::Autolock _l(mLock);
1301 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1302 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1303 }
1304 }
1305
onDynamicPolicyMixStateUpdate(String8 regId,int32_t state)1306 void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1307 String8 regId, int32_t state)
1308 {
1309 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1310 dynamic_policy_callback cb = NULL;
1311 {
1312 Mutex::Autolock _l(AudioSystem::gLock);
1313 cb = gDynPolicyCallback;
1314 }
1315
1316 if (cb != NULL) {
1317 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1318 }
1319 }
1320
onRecordingConfigurationUpdate(int event,const record_client_info_t * clientInfo,const audio_config_base_t * clientConfig,const audio_config_base_t * deviceConfig,audio_patch_handle_t patchHandle)1321 void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
1322 int event, const record_client_info_t *clientInfo,
1323 const audio_config_base_t *clientConfig, const audio_config_base_t *deviceConfig,
1324 audio_patch_handle_t patchHandle) {
1325 record_config_callback cb = NULL;
1326 {
1327 Mutex::Autolock _l(AudioSystem::gLock);
1328 cb = gRecordConfigCallback;
1329 }
1330
1331 if (cb != NULL) {
1332 cb(event, clientInfo, clientConfig, deviceConfig, patchHandle);
1333 }
1334 }
1335
binderDied(const wp<IBinder> & who __unused)1336 void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1337 {
1338 {
1339 Mutex::Autolock _l(mLock);
1340 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1341 mAudioPortCallbacks[i]->onServiceDied();
1342 }
1343 }
1344 {
1345 Mutex::Autolock _l(gLockAPS);
1346 AudioSystem::gAudioPolicyService.clear();
1347 }
1348
1349 ALOGW("AudioPolicyService server died!");
1350 }
1351
1352 } // namespace android
1353