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 <binder/IPCThreadState.h>
24 #include <media/AudioResamplerPublic.h>
25 #include <media/AudioSystem.h>
26 #include <media/IAudioFlinger.h>
27 #include <media/IAudioPolicyService.h>
28 #include <media/TypeConverter.h>
29 #include <math.h>
30
31 #include <system/audio.h>
32
33 // ----------------------------------------------------------------------------
34
35 namespace android {
36
37 // client singleton for AudioFlinger binder interface
38 Mutex AudioSystem::gLock;
39 Mutex AudioSystem::gLockAPS;
40 sp<IAudioFlinger> AudioSystem::gAudioFlinger;
41 sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
42 audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
43 dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
44 record_config_callback AudioSystem::gRecordConfigCallback = NULL;
45
46 // establish binder interface to AudioFlinger service
get_audio_flinger()47 const sp<IAudioFlinger> AudioSystem::get_audio_flinger()
48 {
49 sp<IAudioFlinger> af;
50 sp<AudioFlingerClient> afc;
51 {
52 Mutex::Autolock _l(gLock);
53 if (gAudioFlinger == 0) {
54 sp<IServiceManager> sm = defaultServiceManager();
55 sp<IBinder> binder;
56 do {
57 binder = sm->getService(String16("media.audio_flinger"));
58 if (binder != 0)
59 break;
60 ALOGW("AudioFlinger not published, waiting...");
61 usleep(500000); // 0.5 s
62 } while (true);
63 if (gAudioFlingerClient == NULL) {
64 gAudioFlingerClient = new AudioFlingerClient();
65 } else {
66 if (gAudioErrorCallback) {
67 gAudioErrorCallback(NO_ERROR);
68 }
69 }
70 binder->linkToDeath(gAudioFlingerClient);
71 gAudioFlinger = interface_cast<IAudioFlinger>(binder);
72 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
73 afc = gAudioFlingerClient;
74 // Make sure callbacks can be received by gAudioFlingerClient
75 ProcessState::self()->startThreadPool();
76 }
77 af = gAudioFlinger;
78 }
79 if (afc != 0) {
80 int64_t token = IPCThreadState::self()->clearCallingIdentity();
81 af->registerClient(afc);
82 IPCThreadState::self()->restoreCallingIdentity(token);
83 }
84 return af;
85 }
86
getAudioFlingerClient()87 const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
88 {
89 // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
90 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
91 if (af == 0) return 0;
92 Mutex::Autolock _l(gLock);
93 return gAudioFlingerClient;
94 }
95
getIoDescriptor(audio_io_handle_t ioHandle)96 sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
97 {
98 sp<AudioIoDescriptor> desc;
99 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
100 if (afc != 0) {
101 desc = afc->getIoDescriptor(ioHandle);
102 }
103 return desc;
104 }
105
checkAudioFlinger()106 /* static */ status_t AudioSystem::checkAudioFlinger()
107 {
108 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
109 return NO_ERROR;
110 }
111 return DEAD_OBJECT;
112 }
113
114 // FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
115
muteMicrophone(bool state)116 status_t AudioSystem::muteMicrophone(bool state)
117 {
118 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
119 if (af == 0) return PERMISSION_DENIED;
120 return af->setMicMute(state);
121 }
122
isMicrophoneMuted(bool * state)123 status_t AudioSystem::isMicrophoneMuted(bool* state)
124 {
125 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
126 if (af == 0) return PERMISSION_DENIED;
127 *state = af->getMicMute();
128 return NO_ERROR;
129 }
130
setMasterVolume(float value)131 status_t AudioSystem::setMasterVolume(float value)
132 {
133 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
134 if (af == 0) return PERMISSION_DENIED;
135 af->setMasterVolume(value);
136 return NO_ERROR;
137 }
138
setMasterMute(bool mute)139 status_t AudioSystem::setMasterMute(bool mute)
140 {
141 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
142 if (af == 0) return PERMISSION_DENIED;
143 af->setMasterMute(mute);
144 return NO_ERROR;
145 }
146
getMasterVolume(float * volume)147 status_t AudioSystem::getMasterVolume(float* volume)
148 {
149 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
150 if (af == 0) return PERMISSION_DENIED;
151 *volume = af->masterVolume();
152 return NO_ERROR;
153 }
154
getMasterMute(bool * mute)155 status_t AudioSystem::getMasterMute(bool* mute)
156 {
157 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
158 if (af == 0) return PERMISSION_DENIED;
159 *mute = af->masterMute();
160 return NO_ERROR;
161 }
162
setStreamVolume(audio_stream_type_t stream,float value,audio_io_handle_t output)163 status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
164 audio_io_handle_t output)
165 {
166 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
167 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
168 if (af == 0) return PERMISSION_DENIED;
169 af->setStreamVolume(stream, value, output);
170 return NO_ERROR;
171 }
172
setStreamMute(audio_stream_type_t stream,bool mute)173 status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
174 {
175 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
176 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
177 if (af == 0) return PERMISSION_DENIED;
178 af->setStreamMute(stream, mute);
179 return NO_ERROR;
180 }
181
getStreamVolume(audio_stream_type_t stream,float * volume,audio_io_handle_t output)182 status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
183 audio_io_handle_t output)
184 {
185 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
186 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
187 if (af == 0) return PERMISSION_DENIED;
188 *volume = af->streamVolume(stream, output);
189 return NO_ERROR;
190 }
191
getStreamMute(audio_stream_type_t stream,bool * mute)192 status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
193 {
194 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
195 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
196 if (af == 0) return PERMISSION_DENIED;
197 *mute = af->streamMute(stream);
198 return NO_ERROR;
199 }
200
setMode(audio_mode_t mode)201 status_t AudioSystem::setMode(audio_mode_t mode)
202 {
203 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
204 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
205 if (af == 0) return PERMISSION_DENIED;
206 return af->setMode(mode);
207 }
208
setParameters(audio_io_handle_t ioHandle,const String8 & keyValuePairs)209 status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
210 {
211 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
212 if (af == 0) return PERMISSION_DENIED;
213 return af->setParameters(ioHandle, keyValuePairs);
214 }
215
getParameters(audio_io_handle_t ioHandle,const String8 & keys)216 String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
217 {
218 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
219 String8 result = String8("");
220 if (af == 0) return result;
221
222 result = af->getParameters(ioHandle, keys);
223 return result;
224 }
225
setParameters(const String8 & keyValuePairs)226 status_t AudioSystem::setParameters(const String8& keyValuePairs)
227 {
228 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
229 }
230
getParameters(const String8 & keys)231 String8 AudioSystem::getParameters(const String8& keys)
232 {
233 return getParameters(AUDIO_IO_HANDLE_NONE, keys);
234 }
235
236 // convert volume steps to natural log scale
237
238 // change this value to change volume scaling
239 static const float dBPerStep = 0.5f;
240 // shouldn't need to touch these
241 static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
242 static const float dBConvertInverse = 1.0f / dBConvert;
243
linearToLog(int volume)244 float AudioSystem::linearToLog(int volume)
245 {
246 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
247 // ALOGD("linearToLog(%d)=%f", volume, v);
248 // return v;
249 return volume ? exp(float(100 - volume) * dBConvert) : 0;
250 }
251
logToLinear(float volume)252 int AudioSystem::logToLinear(float volume)
253 {
254 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
255 // ALOGD("logTolinear(%d)=%f", v, volume);
256 // return v;
257 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
258 }
259
calculateMinFrameCount(uint32_t afLatencyMs,uint32_t afFrameCount,uint32_t afSampleRate,uint32_t sampleRate,float speed)260 /* static */ size_t AudioSystem::calculateMinFrameCount(
261 uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate,
262 uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/)
263 {
264 // Ensure that buffer depth covers at least audio hardware latency
265 uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate);
266 if (minBufCount < 2) {
267 minBufCount = 2;
268 }
269 #if 0
270 // The notificationsPerBufferReq parameter is not yet used for non-fast tracks,
271 // but keeping the code here to make it easier to add later.
272 if (minBufCount < notificationsPerBufferReq) {
273 minBufCount = notificationsPerBufferReq;
274 }
275 #endif
276 ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u "
277 "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/,
278 afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount
279 /*, notificationsPerBufferReq*/);
280 return minBufCount * sourceFramesNeededWithTimestretch(
281 sampleRate, afFrameCount, afSampleRate, speed);
282 }
283
284
getOutputSamplingRate(uint32_t * samplingRate,audio_stream_type_t streamType)285 status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
286 {
287 audio_io_handle_t output;
288
289 if (streamType == AUDIO_STREAM_DEFAULT) {
290 streamType = AUDIO_STREAM_MUSIC;
291 }
292
293 output = getOutput(streamType);
294 if (output == 0) {
295 return PERMISSION_DENIED;
296 }
297
298 return getSamplingRate(output, samplingRate);
299 }
300
getSamplingRate(audio_io_handle_t ioHandle,uint32_t * samplingRate)301 status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
302 uint32_t* samplingRate)
303 {
304 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
305 if (af == 0) return PERMISSION_DENIED;
306 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
307 if (desc == 0) {
308 *samplingRate = af->sampleRate(ioHandle);
309 } else {
310 *samplingRate = desc->mSamplingRate;
311 }
312 if (*samplingRate == 0) {
313 ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
314 return BAD_VALUE;
315 }
316
317 ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
318
319 return NO_ERROR;
320 }
321
getOutputFrameCount(size_t * frameCount,audio_stream_type_t streamType)322 status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
323 {
324 audio_io_handle_t output;
325
326 if (streamType == AUDIO_STREAM_DEFAULT) {
327 streamType = AUDIO_STREAM_MUSIC;
328 }
329
330 output = getOutput(streamType);
331 if (output == AUDIO_IO_HANDLE_NONE) {
332 return PERMISSION_DENIED;
333 }
334
335 return getFrameCount(output, frameCount);
336 }
337
getFrameCount(audio_io_handle_t ioHandle,size_t * frameCount)338 status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
339 size_t* frameCount)
340 {
341 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
342 if (af == 0) return PERMISSION_DENIED;
343 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
344 if (desc == 0) {
345 *frameCount = af->frameCount(ioHandle);
346 } else {
347 *frameCount = desc->mFrameCount;
348 }
349 if (*frameCount == 0) {
350 ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
351 return BAD_VALUE;
352 }
353
354 ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
355
356 return NO_ERROR;
357 }
358
getOutputLatency(uint32_t * latency,audio_stream_type_t streamType)359 status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
360 {
361 audio_io_handle_t output;
362
363 if (streamType == AUDIO_STREAM_DEFAULT) {
364 streamType = AUDIO_STREAM_MUSIC;
365 }
366
367 output = getOutput(streamType);
368 if (output == AUDIO_IO_HANDLE_NONE) {
369 return PERMISSION_DENIED;
370 }
371
372 return getLatency(output, latency);
373 }
374
getLatency(audio_io_handle_t output,uint32_t * latency)375 status_t AudioSystem::getLatency(audio_io_handle_t output,
376 uint32_t* latency)
377 {
378 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379 if (af == 0) return PERMISSION_DENIED;
380 sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
381 if (outputDesc == 0) {
382 *latency = af->latency(output);
383 } else {
384 *latency = outputDesc->mLatency;
385 }
386
387 ALOGV("getLatency() output %d, latency %d", output, *latency);
388
389 return NO_ERROR;
390 }
391
getInputBufferSize(uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * buffSize)392 status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
393 audio_channel_mask_t channelMask, size_t* buffSize)
394 {
395 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
396 if (afc == 0) {
397 return NO_INIT;
398 }
399 return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
400 }
401
setVoiceVolume(float value)402 status_t AudioSystem::setVoiceVolume(float value)
403 {
404 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
405 if (af == 0) return PERMISSION_DENIED;
406 return af->setVoiceVolume(value);
407 }
408
getRenderPosition(audio_io_handle_t output,uint32_t * halFrames,uint32_t * dspFrames)409 status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
410 uint32_t *dspFrames)
411 {
412 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
413 if (af == 0) return PERMISSION_DENIED;
414
415 return af->getRenderPosition(halFrames, dspFrames, output);
416 }
417
getInputFramesLost(audio_io_handle_t ioHandle)418 uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
419 {
420 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
421 uint32_t result = 0;
422 if (af == 0) return result;
423 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
424
425 result = af->getInputFramesLost(ioHandle);
426 return result;
427 }
428
newAudioUniqueId(audio_unique_id_use_t use)429 audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
430 {
431 // Must not use AF as IDs will re-roll on audioserver restart, b/130369529.
432 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
433 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
434 return af->newAudioUniqueId(use);
435 }
436
acquireAudioSessionId(audio_session_t audioSession,pid_t pid)437 void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
438 {
439 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
440 if (af != 0) {
441 af->acquireAudioSessionId(audioSession, pid);
442 }
443 }
444
releaseAudioSessionId(audio_session_t audioSession,pid_t pid)445 void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
446 {
447 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
448 if (af != 0) {
449 af->releaseAudioSessionId(audioSession, pid);
450 }
451 }
452
getAudioHwSyncForSession(audio_session_t sessionId)453 audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
454 {
455 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
456 if (af == 0) return AUDIO_HW_SYNC_INVALID;
457 return af->getAudioHwSyncForSession(sessionId);
458 }
459
systemReady()460 status_t AudioSystem::systemReady()
461 {
462 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
463 if (af == 0) return NO_INIT;
464 return af->systemReady();
465 }
466
getFrameCountHAL(audio_io_handle_t ioHandle,size_t * frameCount)467 status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
468 size_t* frameCount)
469 {
470 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
471 if (af == 0) return PERMISSION_DENIED;
472 sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
473 if (desc == 0) {
474 *frameCount = af->frameCountHAL(ioHandle);
475 } else {
476 *frameCount = desc->mFrameCountHAL;
477 }
478 if (*frameCount == 0) {
479 ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
480 return BAD_VALUE;
481 }
482
483 ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
484
485 return NO_ERROR;
486 }
487
488 // ---------------------------------------------------------------------------
489
490
clearIoCache()491 void AudioSystem::AudioFlingerClient::clearIoCache()
492 {
493 Mutex::Autolock _l(mLock);
494 mIoDescriptors.clear();
495 mInBuffSize = 0;
496 mInSamplingRate = 0;
497 mInFormat = AUDIO_FORMAT_DEFAULT;
498 mInChannelMask = AUDIO_CHANNEL_NONE;
499 }
500
binderDied(const wp<IBinder> & who __unused)501 void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
502 {
503 audio_error_callback cb = NULL;
504 {
505 Mutex::Autolock _l(AudioSystem::gLock);
506 AudioSystem::gAudioFlinger.clear();
507 cb = gAudioErrorCallback;
508 }
509
510 // clear output handles and stream to output map caches
511 clearIoCache();
512
513 if (cb) {
514 cb(DEAD_OBJECT);
515 }
516 ALOGW("AudioFlinger server died!");
517 }
518
ioConfigChanged(audio_io_config_event event,const sp<AudioIoDescriptor> & ioDesc)519 void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
520 const sp<AudioIoDescriptor>& ioDesc) {
521 ALOGV("ioConfigChanged() event %d", event);
522
523 if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
524
525 audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
526 std::vector<sp<AudioDeviceCallback>> callbacksToCall;
527 {
528 Mutex::Autolock _l(mLock);
529 auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>();
530
531 switch (event) {
532 case AUDIO_OUTPUT_OPENED:
533 case AUDIO_OUTPUT_REGISTERED:
534 case AUDIO_INPUT_OPENED:
535 case AUDIO_INPUT_REGISTERED: {
536 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
537 if (oldDesc == 0) {
538 mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
539 } else {
540 deviceId = oldDesc->getDeviceId();
541 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
542 }
543
544 if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
545 deviceId = ioDesc->getDeviceId();
546 if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) {
547 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
548 if (it != mAudioDeviceCallbacks.end()) {
549 callbacks = it->second;
550 }
551 }
552 }
553 ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x "
554 "frameCount %zu deviceId %d",
555 event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ?
556 "output" : "input",
557 event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ?
558 "opened" : "registered",
559 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
560 ioDesc->mFrameCount, ioDesc->getDeviceId());
561 } break;
562 case AUDIO_OUTPUT_CLOSED:
563 case AUDIO_INPUT_CLOSED: {
564 if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
565 ALOGW("ioConfigChanged() closing unknown %s %d",
566 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
567 break;
568 }
569 ALOGV("ioConfigChanged() %s %d closed",
570 event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
571
572 mIoDescriptors.removeItem(ioDesc->mIoHandle);
573 mAudioDeviceCallbacks.erase(ioDesc->mIoHandle);
574 } break;
575
576 case AUDIO_OUTPUT_CONFIG_CHANGED:
577 case AUDIO_INPUT_CONFIG_CHANGED: {
578 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
579 if (oldDesc == 0) {
580 ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
581 break;
582 }
583
584 deviceId = oldDesc->getDeviceId();
585 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
586
587 if (deviceId != ioDesc->getDeviceId()) {
588 deviceId = ioDesc->getDeviceId();
589 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
590 if (it != mAudioDeviceCallbacks.end()) {
591 callbacks = it->second;
592 }
593 }
594 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
595 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
596 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
597 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
598 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
599 ioDesc->getDeviceId());
600
601 } break;
602 case AUDIO_CLIENT_STARTED: {
603 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
604 if (oldDesc == 0) {
605 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
606 break;
607 }
608 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
609 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
610 oldDesc->mPatch = ioDesc->mPatch;
611 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
612 if (it != mAudioDeviceCallbacks.end()) {
613 auto cbks = it->second;
614 auto it2 = cbks.find(ioDesc->mPortId);
615 if (it2 != cbks.end()) {
616 callbacks.emplace(ioDesc->mPortId, it2->second);
617 deviceId = oldDesc->getDeviceId();
618 }
619 }
620 } break;
621 }
622
623 for (auto wpCbk : callbacks) {
624 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
625 if (spCbk != nullptr) {
626 callbacksToCall.push_back(spCbk);
627 }
628 }
629 }
630
631 // Callbacks must be called without mLock held. May lead to dead lock if calling for
632 // example getRoutedDevice that updates the device and tries to acquire mLock.
633 for (auto cb : callbacksToCall) {
634 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
635 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
636 }
637 }
638
getInputBufferSize(uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * buffSize)639 status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
640 uint32_t sampleRate, audio_format_t format,
641 audio_channel_mask_t channelMask, size_t* buffSize)
642 {
643 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
644 if (af == 0) {
645 return PERMISSION_DENIED;
646 }
647 Mutex::Autolock _l(mLock);
648 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
649 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
650 || (channelMask != mInChannelMask)) {
651 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
652 if (inBuffSize == 0) {
653 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
654 sampleRate, format, channelMask);
655 return BAD_VALUE;
656 }
657 // A benign race is possible here: we could overwrite a fresher cache entry
658 // save the request params
659 mInSamplingRate = sampleRate;
660 mInFormat = format;
661 mInChannelMask = channelMask;
662
663 mInBuffSize = inBuffSize;
664 }
665
666 *buffSize = mInBuffSize;
667
668 return NO_ERROR;
669 }
670
getIoDescriptor_l(audio_io_handle_t ioHandle)671 sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
672 {
673 sp<AudioIoDescriptor> desc;
674 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
675 if (index >= 0) {
676 desc = mIoDescriptors.valueAt(index);
677 }
678 return desc;
679 }
680
getIoDescriptor(audio_io_handle_t ioHandle)681 sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
682 {
683 Mutex::Autolock _l(mLock);
684 return getIoDescriptor_l(ioHandle);
685 }
686
addAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo,audio_port_handle_t portId)687 status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
688 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
689 audio_port_handle_t portId)
690 {
691 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
692 Mutex::Autolock _l(mLock);
693 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
694 auto result = callbacks.try_emplace(portId, callback);
695 if (!result.second) {
696 return INVALID_OPERATION;
697 }
698 return NO_ERROR;
699 }
700
removeAudioDeviceCallback(const wp<AudioDeviceCallback> & callback __unused,audio_io_handle_t audioIo,audio_port_handle_t portId)701 status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
702 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
703 audio_port_handle_t portId)
704 {
705 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
706 Mutex::Autolock _l(mLock);
707 auto it = mAudioDeviceCallbacks.find(audioIo);
708 if (it == mAudioDeviceCallbacks.end()) {
709 return INVALID_OPERATION;
710 }
711 if (it->second.erase(portId) == 0) {
712 return INVALID_OPERATION;
713 }
714 if (it->second.size() == 0) {
715 mAudioDeviceCallbacks.erase(audioIo);
716 }
717 return NO_ERROR;
718 }
719
setErrorCallback(audio_error_callback cb)720 /* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
721 {
722 Mutex::Autolock _l(gLock);
723 gAudioErrorCallback = cb;
724 }
725
setDynPolicyCallback(dynamic_policy_callback cb)726 /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
727 {
728 Mutex::Autolock _l(gLock);
729 gDynPolicyCallback = cb;
730 }
731
setRecordConfigCallback(record_config_callback cb)732 /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
733 {
734 Mutex::Autolock _l(gLock);
735 gRecordConfigCallback = cb;
736 }
737
738 // client singleton for AudioPolicyService binder interface
739 // protected by gLockAPS
740 sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
741 sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
742
743
744 // establish binder interface to AudioPolicy service
get_audio_policy_service()745 const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
746 {
747 sp<IAudioPolicyService> ap;
748 sp<AudioPolicyServiceClient> apc;
749 {
750 Mutex::Autolock _l(gLockAPS);
751 if (gAudioPolicyService == 0) {
752 sp<IServiceManager> sm = defaultServiceManager();
753 sp<IBinder> binder;
754 do {
755 binder = sm->getService(String16("media.audio_policy"));
756 if (binder != 0)
757 break;
758 ALOGW("AudioPolicyService not published, waiting...");
759 usleep(500000); // 0.5 s
760 } while (true);
761 if (gAudioPolicyServiceClient == NULL) {
762 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
763 }
764 binder->linkToDeath(gAudioPolicyServiceClient);
765 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
766 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
767 apc = gAudioPolicyServiceClient;
768 // Make sure callbacks can be received by gAudioPolicyServiceClient
769 ProcessState::self()->startThreadPool();
770 }
771 ap = gAudioPolicyService;
772 }
773 if (apc != 0) {
774 int64_t token = IPCThreadState::self()->clearCallingIdentity();
775 ap->registerClient(apc);
776 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
777 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
778 IPCThreadState::self()->restoreCallingIdentity(token);
779 }
780
781 return ap;
782 }
783
784 // ---------------------------------------------------------------------------
785
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address,const char * device_name,audio_format_t encodedFormat)786 status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
787 audio_policy_dev_state_t state,
788 const char *device_address,
789 const char *device_name,
790 audio_format_t encodedFormat)
791 {
792 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
793 const char *address = "";
794 const char *name = "";
795
796 if (aps == 0) return PERMISSION_DENIED;
797
798 if (device_address != NULL) {
799 address = device_address;
800 }
801 if (device_name != NULL) {
802 name = device_name;
803 }
804 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
805 }
806
getDeviceConnectionState(audio_devices_t device,const char * device_address)807 audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
808 const char *device_address)
809 {
810 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
811 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
812
813 return aps->getDeviceConnectionState(device, device_address);
814 }
815
handleDeviceConfigChange(audio_devices_t device,const char * device_address,const char * device_name,audio_format_t encodedFormat)816 status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
817 const char *device_address,
818 const char *device_name,
819 audio_format_t encodedFormat)
820 {
821 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
822 const char *address = "";
823 const char *name = "";
824
825 if (aps == 0) return PERMISSION_DENIED;
826
827 if (device_address != NULL) {
828 address = device_address;
829 }
830 if (device_name != NULL) {
831 name = device_name;
832 }
833 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
834 }
835
setPhoneState(audio_mode_t state)836 status_t AudioSystem::setPhoneState(audio_mode_t state)
837 {
838 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
839 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
840 if (aps == 0) return PERMISSION_DENIED;
841
842 return aps->setPhoneState(state);
843 }
844
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)845 status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
846 {
847 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
848 if (aps == 0) return PERMISSION_DENIED;
849 return aps->setForceUse(usage, config);
850 }
851
getForceUse(audio_policy_force_use_t usage)852 audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
853 {
854 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
855 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
856 return aps->getForceUse(usage);
857 }
858
859
getOutput(audio_stream_type_t stream)860 audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
861 {
862 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
863 if (aps == 0) return 0;
864 return aps->getOutput(stream);
865 }
866
getOutputForAttr(audio_attributes_t * attr,audio_io_handle_t * output,audio_session_t session,audio_stream_type_t * stream,pid_t pid,uid_t uid,const audio_config_t * config,audio_output_flags_t flags,audio_port_handle_t * selectedDeviceId,audio_port_handle_t * portId,std::vector<audio_io_handle_t> * secondaryOutputs)867 status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
868 audio_io_handle_t *output,
869 audio_session_t session,
870 audio_stream_type_t *stream,
871 pid_t pid,
872 uid_t uid,
873 const audio_config_t *config,
874 audio_output_flags_t flags,
875 audio_port_handle_t *selectedDeviceId,
876 audio_port_handle_t *portId,
877 std::vector<audio_io_handle_t> *secondaryOutputs)
878 {
879 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
880 if (aps == 0) return NO_INIT;
881 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
882 config,
883 flags, selectedDeviceId, portId, secondaryOutputs);
884 }
885
startOutput(audio_port_handle_t portId)886 status_t AudioSystem::startOutput(audio_port_handle_t portId)
887 {
888 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
889 if (aps == 0) return PERMISSION_DENIED;
890 return aps->startOutput(portId);
891 }
892
stopOutput(audio_port_handle_t portId)893 status_t AudioSystem::stopOutput(audio_port_handle_t portId)
894 {
895 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
896 if (aps == 0) return PERMISSION_DENIED;
897 return aps->stopOutput(portId);
898 }
899
releaseOutput(audio_port_handle_t portId)900 void AudioSystem::releaseOutput(audio_port_handle_t portId)
901 {
902 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
903 if (aps == 0) return;
904 aps->releaseOutput(portId);
905 }
906
getInputForAttr(const audio_attributes_t * attr,audio_io_handle_t * input,audio_unique_id_t riid,audio_session_t session,pid_t pid,uid_t uid,const String16 & opPackageName,const audio_config_base_t * config,audio_input_flags_t flags,audio_port_handle_t * selectedDeviceId,audio_port_handle_t * portId)907 status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
908 audio_io_handle_t *input,
909 audio_unique_id_t riid,
910 audio_session_t session,
911 pid_t pid,
912 uid_t uid,
913 const String16& opPackageName,
914 const audio_config_base_t *config,
915 audio_input_flags_t flags,
916 audio_port_handle_t *selectedDeviceId,
917 audio_port_handle_t *portId)
918 {
919 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
920 if (aps == 0) return NO_INIT;
921 return aps->getInputForAttr(
922 attr, input, riid, session, pid, uid, opPackageName,
923 config, flags, selectedDeviceId, portId);
924 }
925
startInput(audio_port_handle_t portId)926 status_t AudioSystem::startInput(audio_port_handle_t portId)
927 {
928 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
929 if (aps == 0) return PERMISSION_DENIED;
930 return aps->startInput(portId);
931 }
932
stopInput(audio_port_handle_t portId)933 status_t AudioSystem::stopInput(audio_port_handle_t portId)
934 {
935 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
936 if (aps == 0) return PERMISSION_DENIED;
937 return aps->stopInput(portId);
938 }
939
releaseInput(audio_port_handle_t portId)940 void AudioSystem::releaseInput(audio_port_handle_t portId)
941 {
942 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
943 if (aps == 0) return;
944 aps->releaseInput(portId);
945 }
946
initStreamVolume(audio_stream_type_t stream,int indexMin,int indexMax)947 status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
948 int indexMin,
949 int indexMax)
950 {
951 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
952 if (aps == 0) return PERMISSION_DENIED;
953 return aps->initStreamVolume(stream, indexMin, indexMax);
954 }
955
setStreamVolumeIndex(audio_stream_type_t stream,int index,audio_devices_t device)956 status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
957 int index,
958 audio_devices_t device)
959 {
960 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
961 if (aps == 0) return PERMISSION_DENIED;
962 return aps->setStreamVolumeIndex(stream, index, device);
963 }
964
getStreamVolumeIndex(audio_stream_type_t stream,int * index,audio_devices_t device)965 status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
966 int *index,
967 audio_devices_t device)
968 {
969 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
970 if (aps == 0) return PERMISSION_DENIED;
971 return aps->getStreamVolumeIndex(stream, index, device);
972 }
973
setVolumeIndexForAttributes(const audio_attributes_t & attr,int index,audio_devices_t device)974 status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
975 int index,
976 audio_devices_t device)
977 {
978 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
979 if (aps == 0) return PERMISSION_DENIED;
980 return aps->setVolumeIndexForAttributes(attr, index, device);
981 }
982
getVolumeIndexForAttributes(const audio_attributes_t & attr,int & index,audio_devices_t device)983 status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
984 int &index,
985 audio_devices_t device)
986 {
987 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
988 if (aps == 0) return PERMISSION_DENIED;
989 return aps->getVolumeIndexForAttributes(attr, index, device);
990 }
991
getMaxVolumeIndexForAttributes(const audio_attributes_t & attr,int & index)992 status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
993 {
994 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
995 if (aps == 0) return PERMISSION_DENIED;
996 return aps->getMaxVolumeIndexForAttributes(attr, index);
997 }
998
getMinVolumeIndexForAttributes(const audio_attributes_t & attr,int & index)999 status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1000 {
1001 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1002 if (aps == 0) return PERMISSION_DENIED;
1003 return aps->getMinVolumeIndexForAttributes(attr, index);
1004 }
1005
getStrategyForStream(audio_stream_type_t stream)1006 uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
1007 {
1008 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1009 if (aps == 0) return PRODUCT_STRATEGY_NONE;
1010 return aps->getStrategyForStream(stream);
1011 }
1012
getDevicesForStream(audio_stream_type_t stream)1013 audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
1014 {
1015 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1016 if (aps == 0) return AUDIO_DEVICE_NONE;
1017 return aps->getDevicesForStream(stream);
1018 }
1019
getOutputForEffect(const effect_descriptor_t * desc)1020 audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
1021 {
1022 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1023 // FIXME change return type to status_t, and return PERMISSION_DENIED here
1024 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1025 return aps->getOutputForEffect(desc);
1026 }
1027
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,uint32_t strategy,audio_session_t session,int id)1028 status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
1029 audio_io_handle_t io,
1030 uint32_t strategy,
1031 audio_session_t session,
1032 int id)
1033 {
1034 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1035 if (aps == 0) return PERMISSION_DENIED;
1036 return aps->registerEffect(desc, io, strategy, session, id);
1037 }
1038
unregisterEffect(int id)1039 status_t AudioSystem::unregisterEffect(int id)
1040 {
1041 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1042 if (aps == 0) return PERMISSION_DENIED;
1043 return aps->unregisterEffect(id);
1044 }
1045
setEffectEnabled(int id,bool enabled)1046 status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1047 {
1048 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1049 if (aps == 0) return PERMISSION_DENIED;
1050 return aps->setEffectEnabled(id, enabled);
1051 }
1052
moveEffectsToIo(const std::vector<int> & ids,audio_io_handle_t io)1053 status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1054 {
1055 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1056 if (aps == 0) return PERMISSION_DENIED;
1057 return aps->moveEffectsToIo(ids, io);
1058 }
1059
isStreamActive(audio_stream_type_t stream,bool * state,uint32_t inPastMs)1060 status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
1061 {
1062 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1063 if (aps == 0) return PERMISSION_DENIED;
1064 if (state == NULL) return BAD_VALUE;
1065 *state = aps->isStreamActive(stream, inPastMs);
1066 return NO_ERROR;
1067 }
1068
isStreamActiveRemotely(audio_stream_type_t stream,bool * state,uint32_t inPastMs)1069 status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1070 uint32_t inPastMs)
1071 {
1072 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1073 if (aps == 0) return PERMISSION_DENIED;
1074 if (state == NULL) return BAD_VALUE;
1075 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1076 return NO_ERROR;
1077 }
1078
isSourceActive(audio_source_t stream,bool * state)1079 status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1080 {
1081 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1082 if (aps == 0) return PERMISSION_DENIED;
1083 if (state == NULL) return BAD_VALUE;
1084 *state = aps->isSourceActive(stream);
1085 return NO_ERROR;
1086 }
1087
getPrimaryOutputSamplingRate()1088 uint32_t AudioSystem::getPrimaryOutputSamplingRate()
1089 {
1090 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1091 if (af == 0) return 0;
1092 return af->getPrimaryOutputSamplingRate();
1093 }
1094
getPrimaryOutputFrameCount()1095 size_t AudioSystem::getPrimaryOutputFrameCount()
1096 {
1097 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1098 if (af == 0) return 0;
1099 return af->getPrimaryOutputFrameCount();
1100 }
1101
setLowRamDevice(bool isLowRamDevice,int64_t totalMemory)1102 status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
1103 {
1104 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1105 if (af == 0) return PERMISSION_DENIED;
1106 return af->setLowRamDevice(isLowRamDevice, totalMemory);
1107 }
1108
clearAudioConfigCache()1109 void AudioSystem::clearAudioConfigCache()
1110 {
1111 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
1112 ALOGV("clearAudioConfigCache()");
1113 {
1114 Mutex::Autolock _l(gLock);
1115 if (gAudioFlingerClient != 0) {
1116 gAudioFlingerClient->clearIoCache();
1117 }
1118 gAudioFlinger.clear();
1119 }
1120 {
1121 Mutex::Autolock _l(gLockAPS);
1122 gAudioPolicyService.clear();
1123 }
1124 }
1125
setAllowedCapturePolicy(uid_t uid,audio_flags_mask_t flags)1126 status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1127 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1128 if (aps == nullptr) return PERMISSION_DENIED;
1129 return aps->setAllowedCapturePolicy(uid, flags);
1130 }
1131
isOffloadSupported(const audio_offload_info_t & info)1132 bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1133 {
1134 ALOGV("isOffloadSupported()");
1135 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1136 if (aps == 0) return false;
1137 return aps->isOffloadSupported(info);
1138 }
1139
listAudioPorts(audio_port_role_t role,audio_port_type_t type,unsigned int * num_ports,struct audio_port * ports,unsigned int * generation)1140 status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1141 audio_port_type_t type,
1142 unsigned int *num_ports,
1143 struct audio_port *ports,
1144 unsigned int *generation)
1145 {
1146 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1147 if (aps == 0) return PERMISSION_DENIED;
1148 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1149 }
1150
getAudioPort(struct audio_port * port)1151 status_t AudioSystem::getAudioPort(struct audio_port *port)
1152 {
1153 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1154 if (aps == 0) return PERMISSION_DENIED;
1155 return aps->getAudioPort(port);
1156 }
1157
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle)1158 status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1159 audio_patch_handle_t *handle)
1160 {
1161 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1162 if (aps == 0) return PERMISSION_DENIED;
1163 return aps->createAudioPatch(patch, handle);
1164 }
1165
releaseAudioPatch(audio_patch_handle_t handle)1166 status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1167 {
1168 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1169 if (aps == 0) return PERMISSION_DENIED;
1170 return aps->releaseAudioPatch(handle);
1171 }
1172
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches,unsigned int * generation)1173 status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1174 struct audio_patch *patches,
1175 unsigned int *generation)
1176 {
1177 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1178 if (aps == 0) return PERMISSION_DENIED;
1179 return aps->listAudioPatches(num_patches, patches, generation);
1180 }
1181
setAudioPortConfig(const struct audio_port_config * config)1182 status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1183 {
1184 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1185 if (aps == 0) return PERMISSION_DENIED;
1186 return aps->setAudioPortConfig(config);
1187 }
1188
addAudioPortCallback(const sp<AudioPortCallback> & callback)1189 status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
1190 {
1191 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1192 if (aps == 0) return PERMISSION_DENIED;
1193
1194 Mutex::Autolock _l(gLockAPS);
1195 if (gAudioPolicyServiceClient == 0) {
1196 return NO_INIT;
1197 }
1198 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1199 if (ret == 1) {
1200 aps->setAudioPortCallbacksEnabled(true);
1201 }
1202 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1203 }
1204
1205 /*static*/
removeAudioPortCallback(const sp<AudioPortCallback> & callback)1206 status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
1207 {
1208 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1209 if (aps == 0) return PERMISSION_DENIED;
1210
1211 Mutex::Autolock _l(gLockAPS);
1212 if (gAudioPolicyServiceClient == 0) {
1213 return NO_INIT;
1214 }
1215 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1216 if (ret == 0) {
1217 aps->setAudioPortCallbacksEnabled(false);
1218 }
1219 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1220 }
1221
addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback> & callback)1222 status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1223 {
1224 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1225 if (aps == 0) return PERMISSION_DENIED;
1226
1227 Mutex::Autolock _l(gLockAPS);
1228 if (gAudioPolicyServiceClient == 0) {
1229 return NO_INIT;
1230 }
1231 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1232 if (ret == 1) {
1233 aps->setAudioVolumeGroupCallbacksEnabled(true);
1234 }
1235 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1236 }
1237
removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback> & callback)1238 status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1239 {
1240 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1241 if (aps == 0) return PERMISSION_DENIED;
1242
1243 Mutex::Autolock _l(gLockAPS);
1244 if (gAudioPolicyServiceClient == 0) {
1245 return NO_INIT;
1246 }
1247 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1248 if (ret == 0) {
1249 aps->setAudioVolumeGroupCallbacksEnabled(false);
1250 }
1251 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1252 }
1253
addAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo,audio_port_handle_t portId)1254 status_t AudioSystem::addAudioDeviceCallback(
1255 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1256 audio_port_handle_t portId)
1257 {
1258 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1259 if (afc == 0) {
1260 return NO_INIT;
1261 }
1262 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
1263 if (status == NO_ERROR) {
1264 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1265 if (af != 0) {
1266 af->registerClient(afc);
1267 }
1268 }
1269 return status;
1270 }
1271
removeAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo,audio_port_handle_t portId)1272 status_t AudioSystem::removeAudioDeviceCallback(
1273 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1274 audio_port_handle_t portId)
1275 {
1276 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1277 if (afc == 0) {
1278 return NO_INIT;
1279 }
1280 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
1281 }
1282
getDeviceIdForIo(audio_io_handle_t audioIo)1283 audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1284 {
1285 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1286 if (af == 0) return PERMISSION_DENIED;
1287 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1288 if (desc == 0) {
1289 return AUDIO_PORT_HANDLE_NONE;
1290 }
1291 return desc->getDeviceId();
1292 }
1293
acquireSoundTriggerSession(audio_session_t * session,audio_io_handle_t * ioHandle,audio_devices_t * device)1294 status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1295 audio_io_handle_t *ioHandle,
1296 audio_devices_t *device)
1297 {
1298 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1299 if (aps == 0) return PERMISSION_DENIED;
1300 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1301 }
1302
releaseSoundTriggerSession(audio_session_t session)1303 status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1304 {
1305 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1306 if (aps == 0) return PERMISSION_DENIED;
1307 return aps->releaseSoundTriggerSession(session);
1308 }
1309
getPhoneState()1310 audio_mode_t AudioSystem::getPhoneState()
1311 {
1312 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1313 if (aps == 0) return AUDIO_MODE_INVALID;
1314 return aps->getPhoneState();
1315 }
1316
registerPolicyMixes(const Vector<AudioMix> & mixes,bool registration)1317 status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
1318 {
1319 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1320 if (aps == 0) return PERMISSION_DENIED;
1321 return aps->registerPolicyMixes(mixes, registration);
1322 }
1323
setUidDeviceAffinities(uid_t uid,const Vector<AudioDeviceTypeAddr> & devices)1324 status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1325 {
1326 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1327 if (aps == 0) return PERMISSION_DENIED;
1328 return aps->setUidDeviceAffinities(uid, devices);
1329 }
1330
removeUidDeviceAffinities(uid_t uid)1331 status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1332 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1333 if (aps == 0) return PERMISSION_DENIED;
1334 return aps->removeUidDeviceAffinities(uid);
1335 }
1336
startAudioSource(const struct audio_port_config * source,const audio_attributes_t * attributes,audio_port_handle_t * portId)1337 status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1338 const audio_attributes_t *attributes,
1339 audio_port_handle_t *portId)
1340 {
1341 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1342 if (aps == 0) return PERMISSION_DENIED;
1343 return aps->startAudioSource(source, attributes, portId);
1344 }
1345
stopAudioSource(audio_port_handle_t portId)1346 status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
1347 {
1348 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1349 if (aps == 0) return PERMISSION_DENIED;
1350 return aps->stopAudioSource(portId);
1351 }
1352
setMasterMono(bool mono)1353 status_t AudioSystem::setMasterMono(bool mono)
1354 {
1355 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1356 if (aps == 0) return PERMISSION_DENIED;
1357 return aps->setMasterMono(mono);
1358 }
1359
getMasterMono(bool * mono)1360 status_t AudioSystem::getMasterMono(bool *mono)
1361 {
1362 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1363 if (aps == 0) return PERMISSION_DENIED;
1364 return aps->getMasterMono(mono);
1365 }
1366
setMasterBalance(float balance)1367 status_t AudioSystem::setMasterBalance(float balance)
1368 {
1369 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1370 if (af == 0) return PERMISSION_DENIED;
1371 return af->setMasterBalance(balance);
1372 }
1373
getMasterBalance(float * balance)1374 status_t AudioSystem::getMasterBalance(float *balance)
1375 {
1376 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1377 if (af == 0) return PERMISSION_DENIED;
1378 return af->getMasterBalance(balance);
1379 }
1380
getStreamVolumeDB(audio_stream_type_t stream,int index,audio_devices_t device)1381 float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1382 {
1383 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1384 if (aps == 0) return NAN;
1385 return aps->getStreamVolumeDB(stream, index, device);
1386 }
1387
getMicrophones(std::vector<media::MicrophoneInfo> * microphones)1388 status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1389 {
1390 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1391 if (af == 0) return PERMISSION_DENIED;
1392 return af->getMicrophones(microphones);
1393 }
1394
getSurroundFormats(unsigned int * numSurroundFormats,audio_format_t * surroundFormats,bool * surroundFormatsEnabled,bool reported)1395 status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1396 audio_format_t *surroundFormats,
1397 bool *surroundFormatsEnabled,
1398 bool reported)
1399 {
1400 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1401 if (aps == 0) return PERMISSION_DENIED;
1402 return aps->getSurroundFormats(
1403 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1404 }
1405
setSurroundFormatEnabled(audio_format_t audioFormat,bool enabled)1406 status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1407 {
1408 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1409 if (aps == 0) return PERMISSION_DENIED;
1410 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1411 }
1412
setAssistantUid(uid_t uid)1413 status_t AudioSystem::setAssistantUid(uid_t uid)
1414 {
1415 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1416 if (aps == 0) return PERMISSION_DENIED;
1417
1418 return aps->setAssistantUid(uid);
1419 }
1420
setA11yServicesUids(const std::vector<uid_t> & uids)1421 status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1422 {
1423 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1424 if (aps == 0) return PERMISSION_DENIED;
1425
1426 return aps->setA11yServicesUids(uids);
1427 }
1428
isHapticPlaybackSupported()1429 bool AudioSystem::isHapticPlaybackSupported()
1430 {
1431 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1432 if (aps == 0) return false;
1433 return aps->isHapticPlaybackSupported();
1434 }
1435
getHwOffloadEncodingFormatsSupportedForA2DP(std::vector<audio_format_t> * formats)1436 status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
1437 std::vector<audio_format_t> *formats) {
1438 const sp <IAudioPolicyService>
1439 & aps = AudioSystem::get_audio_policy_service();
1440 if (aps == 0) return PERMISSION_DENIED;
1441 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1442 }
1443
listAudioProductStrategies(AudioProductStrategyVector & strategies)1444 status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
1445 {
1446 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1447 if (aps == 0) return PERMISSION_DENIED;
1448 return aps->listAudioProductStrategies(strategies);
1449 }
1450
streamTypeToAttributes(audio_stream_type_t stream)1451 audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1452 {
1453 AudioProductStrategyVector strategies;
1454 listAudioProductStrategies(strategies);
1455 for (const auto &strategy : strategies) {
1456 auto attrVect = strategy.getAudioAttributes();
1457 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1458 return attributes.getStreamType() == stream; });
1459 if (iter != end(attrVect)) {
1460 return iter->getAttributes();
1461 }
1462 }
1463 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1464 return AUDIO_ATTRIBUTES_INITIALIZER;
1465 }
1466
attributesToStreamType(const audio_attributes_t & attr)1467 audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1468 {
1469 product_strategy_t psId;
1470 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1471 if (ret != NO_ERROR) {
1472 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1473 return AUDIO_STREAM_MUSIC;
1474 }
1475 AudioProductStrategyVector strategies;
1476 listAudioProductStrategies(strategies);
1477 for (const auto &strategy : strategies) {
1478 if (strategy.getId() == psId) {
1479 auto attrVect = strategy.getAudioAttributes();
1480 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1481 return AudioProductStrategy::attributesMatches(
1482 refAttr.getAttributes(), attr); });
1483 if (iter != end(attrVect)) {
1484 return iter->getStreamType();
1485 }
1486 }
1487 }
1488 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1489 return AUDIO_STREAM_MUSIC;
1490 }
1491
getProductStrategyFromAudioAttributes(const AudioAttributes & aa,product_strategy_t & productStrategy)1492 status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1493 product_strategy_t &productStrategy)
1494 {
1495 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1496 if (aps == 0) return PERMISSION_DENIED;
1497 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1498 }
1499
listAudioVolumeGroups(AudioVolumeGroupVector & groups)1500 status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1501 {
1502 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1503 if (aps == 0) return PERMISSION_DENIED;
1504 return aps->listAudioVolumeGroups(groups);
1505 }
1506
getVolumeGroupFromAudioAttributes(const AudioAttributes & aa,volume_group_t & volumeGroup)1507 status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1508 volume_group_t &volumeGroup)
1509 {
1510 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1511 if (aps == 0) return PERMISSION_DENIED;
1512 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
1513 }
1514
setRttEnabled(bool enabled)1515 status_t AudioSystem::setRttEnabled(bool enabled)
1516 {
1517 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1518 if (aps == 0) return PERMISSION_DENIED;
1519 return aps->setRttEnabled(enabled);
1520 }
1521
1522 // ---------------------------------------------------------------------------
1523
addAudioPortCallback(const sp<AudioPortCallback> & callback)1524 int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1525 const sp<AudioPortCallback>& callback)
1526 {
1527 Mutex::Autolock _l(mLock);
1528 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1529 if (mAudioPortCallbacks[i] == callback) {
1530 return -1;
1531 }
1532 }
1533 mAudioPortCallbacks.add(callback);
1534 return mAudioPortCallbacks.size();
1535 }
1536
removeAudioPortCallback(const sp<AudioPortCallback> & callback)1537 int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1538 const sp<AudioPortCallback>& callback)
1539 {
1540 Mutex::Autolock _l(mLock);
1541 size_t i;
1542 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1543 if (mAudioPortCallbacks[i] == callback) {
1544 break;
1545 }
1546 }
1547 if (i == mAudioPortCallbacks.size()) {
1548 return -1;
1549 }
1550 mAudioPortCallbacks.removeAt(i);
1551 return mAudioPortCallbacks.size();
1552 }
1553
1554
onAudioPortListUpdate()1555 void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1556 {
1557 Mutex::Autolock _l(mLock);
1558 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1559 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1560 }
1561 }
1562
onAudioPatchListUpdate()1563 void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1564 {
1565 Mutex::Autolock _l(mLock);
1566 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1567 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1568 }
1569 }
1570
1571 // ----------------------------------------------------------------------------
addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback> & callback)1572 int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1573 const sp<AudioVolumeGroupCallback>& callback)
1574 {
1575 Mutex::Autolock _l(mLock);
1576 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1577 if (mAudioVolumeGroupCallback[i] == callback) {
1578 return -1;
1579 }
1580 }
1581 mAudioVolumeGroupCallback.add(callback);
1582 return mAudioVolumeGroupCallback.size();
1583 }
1584
removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback> & callback)1585 int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1586 const sp<AudioVolumeGroupCallback>& callback)
1587 {
1588 Mutex::Autolock _l(mLock);
1589 size_t i;
1590 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1591 if (mAudioVolumeGroupCallback[i] == callback) {
1592 break;
1593 }
1594 }
1595 if (i == mAudioVolumeGroupCallback.size()) {
1596 return -1;
1597 }
1598 mAudioVolumeGroupCallback.removeAt(i);
1599 return mAudioVolumeGroupCallback.size();
1600 }
1601
onAudioVolumeGroupChanged(volume_group_t group,int flags)1602 void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1603 int flags)
1604 {
1605 Mutex::Autolock _l(mLock);
1606 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1607 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1608 }
1609 }
1610 // ----------------------------------------------------------------------------
1611
onDynamicPolicyMixStateUpdate(String8 regId,int32_t state)1612 void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1613 String8 regId, int32_t state)
1614 {
1615 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1616 dynamic_policy_callback cb = NULL;
1617 {
1618 Mutex::Autolock _l(AudioSystem::gLock);
1619 cb = gDynPolicyCallback;
1620 }
1621
1622 if (cb != NULL) {
1623 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1624 }
1625 }
1626
onRecordingConfigurationUpdate(int event,const record_client_info_t * clientInfo,const audio_config_base_t * clientConfig,std::vector<effect_descriptor_t> clientEffects,const audio_config_base_t * deviceConfig,std::vector<effect_descriptor_t> effects,audio_patch_handle_t patchHandle,audio_source_t source)1627 void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
1628 int event,
1629 const record_client_info_t *clientInfo,
1630 const audio_config_base_t *clientConfig,
1631 std::vector<effect_descriptor_t> clientEffects,
1632 const audio_config_base_t *deviceConfig,
1633 std::vector<effect_descriptor_t> effects,
1634 audio_patch_handle_t patchHandle,
1635 audio_source_t source) {
1636 record_config_callback cb = NULL;
1637 {
1638 Mutex::Autolock _l(AudioSystem::gLock);
1639 cb = gRecordConfigCallback;
1640 }
1641
1642 if (cb != NULL) {
1643 cb(event, clientInfo, clientConfig, clientEffects,
1644 deviceConfig, effects, patchHandle, source);
1645 }
1646 }
1647
binderDied(const wp<IBinder> & who __unused)1648 void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1649 {
1650 {
1651 Mutex::Autolock _l(mLock);
1652 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1653 mAudioPortCallbacks[i]->onServiceDied();
1654 }
1655 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1656 mAudioVolumeGroupCallback[i]->onServiceDied();
1657 }
1658 }
1659 {
1660 Mutex::Autolock _l(gLockAPS);
1661 AudioSystem::gAudioPolicyService.clear();
1662 }
1663
1664 ALOGW("AudioPolicyService server died!");
1665 }
1666
1667 } // namespace android
1668