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 %s! %d",
581 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", ioDesc->mIoHandle);
582 break;
583 }
584
585 deviceId = oldDesc->getDeviceId();
586 mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
587
588 if (deviceId != ioDesc->getDeviceId()) {
589 deviceId = ioDesc->getDeviceId();
590 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
591 if (it != mAudioDeviceCallbacks.end()) {
592 callbacks = it->second;
593 }
594 }
595 ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
596 "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
597 event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
598 ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
599 ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL,
600 ioDesc->getDeviceId());
601
602 } break;
603 case AUDIO_CLIENT_STARTED: {
604 sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
605 if (oldDesc == 0) {
606 ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle);
607 break;
608 }
609 ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu",
610 ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size());
611 oldDesc->mPatch = ioDesc->mPatch;
612 auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle);
613 if (it != mAudioDeviceCallbacks.end()) {
614 auto cbks = it->second;
615 auto it2 = cbks.find(ioDesc->mPortId);
616 if (it2 != cbks.end()) {
617 callbacks.emplace(ioDesc->mPortId, it2->second);
618 deviceId = oldDesc->getDeviceId();
619 }
620 }
621 } break;
622 }
623
624 for (auto wpCbk : callbacks) {
625 sp<AudioDeviceCallback> spCbk = wpCbk.second.promote();
626 if (spCbk != nullptr) {
627 callbacksToCall.push_back(spCbk);
628 }
629 }
630 }
631
632 // Callbacks must be called without mLock held. May lead to dead lock if calling for
633 // example getRoutedDevice that updates the device and tries to acquire mLock.
634 for (auto cb : callbacksToCall) {
635 // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid
636 cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
637 }
638 }
639
getInputBufferSize(uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * buffSize)640 status_t AudioSystem::AudioFlingerClient::getInputBufferSize(
641 uint32_t sampleRate, audio_format_t format,
642 audio_channel_mask_t channelMask, size_t* buffSize)
643 {
644 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
645 if (af == 0) {
646 return PERMISSION_DENIED;
647 }
648 Mutex::Autolock _l(mLock);
649 // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
650 if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
651 || (channelMask != mInChannelMask)) {
652 size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
653 if (inBuffSize == 0) {
654 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x",
655 sampleRate, format, channelMask);
656 return BAD_VALUE;
657 }
658 // A benign race is possible here: we could overwrite a fresher cache entry
659 // save the request params
660 mInSamplingRate = sampleRate;
661 mInFormat = format;
662 mInChannelMask = channelMask;
663
664 mInBuffSize = inBuffSize;
665 }
666
667 *buffSize = mInBuffSize;
668
669 return NO_ERROR;
670 }
671
getIoDescriptor_l(audio_io_handle_t ioHandle)672 sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
673 {
674 sp<AudioIoDescriptor> desc;
675 ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
676 if (index >= 0) {
677 desc = mIoDescriptors.valueAt(index);
678 }
679 return desc;
680 }
681
getIoDescriptor(audio_io_handle_t ioHandle)682 sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
683 {
684 Mutex::Autolock _l(mLock);
685 return getIoDescriptor_l(ioHandle);
686 }
687
addAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo,audio_port_handle_t portId)688 status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
689 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
690 audio_port_handle_t portId)
691 {
692 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
693 Mutex::Autolock _l(mLock);
694 auto& callbacks = mAudioDeviceCallbacks.emplace(audioIo, std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second;
695 auto result = callbacks.try_emplace(portId, callback);
696 if (!result.second) {
697 return INVALID_OPERATION;
698 }
699 return NO_ERROR;
700 }
701
removeAudioDeviceCallback(const wp<AudioDeviceCallback> & callback __unused,audio_io_handle_t audioIo,audio_port_handle_t portId)702 status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
703 const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo,
704 audio_port_handle_t portId)
705 {
706 ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId);
707 Mutex::Autolock _l(mLock);
708 auto it = mAudioDeviceCallbacks.find(audioIo);
709 if (it == mAudioDeviceCallbacks.end()) {
710 return INVALID_OPERATION;
711 }
712 if (it->second.erase(portId) == 0) {
713 return INVALID_OPERATION;
714 }
715 if (it->second.size() == 0) {
716 mAudioDeviceCallbacks.erase(audioIo);
717 }
718 return NO_ERROR;
719 }
720
setErrorCallback(audio_error_callback cb)721 /* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
722 {
723 Mutex::Autolock _l(gLock);
724 gAudioErrorCallback = cb;
725 }
726
setDynPolicyCallback(dynamic_policy_callback cb)727 /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
728 {
729 Mutex::Autolock _l(gLock);
730 gDynPolicyCallback = cb;
731 }
732
setRecordConfigCallback(record_config_callback cb)733 /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb)
734 {
735 Mutex::Autolock _l(gLock);
736 gRecordConfigCallback = cb;
737 }
738
739 // client singleton for AudioPolicyService binder interface
740 // protected by gLockAPS
741 sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
742 sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
743
744
745 // establish binder interface to AudioPolicy service
get_audio_policy_service()746 const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()
747 {
748 sp<IAudioPolicyService> ap;
749 sp<AudioPolicyServiceClient> apc;
750 {
751 Mutex::Autolock _l(gLockAPS);
752 if (gAudioPolicyService == 0) {
753 sp<IServiceManager> sm = defaultServiceManager();
754 sp<IBinder> binder;
755 do {
756 binder = sm->getService(String16("media.audio_policy"));
757 if (binder != 0)
758 break;
759 ALOGW("AudioPolicyService not published, waiting...");
760 usleep(500000); // 0.5 s
761 } while (true);
762 if (gAudioPolicyServiceClient == NULL) {
763 gAudioPolicyServiceClient = new AudioPolicyServiceClient();
764 }
765 binder->linkToDeath(gAudioPolicyServiceClient);
766 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
767 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0);
768 apc = gAudioPolicyServiceClient;
769 // Make sure callbacks can be received by gAudioPolicyServiceClient
770 ProcessState::self()->startThreadPool();
771 }
772 ap = gAudioPolicyService;
773 }
774 if (apc != 0) {
775 int64_t token = IPCThreadState::self()->clearCallingIdentity();
776 ap->registerClient(apc);
777 ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled());
778 ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled());
779 IPCThreadState::self()->restoreCallingIdentity(token);
780 }
781
782 return ap;
783 }
784
785 // ---------------------------------------------------------------------------
786
onNewAudioModulesAvailable()787 void AudioSystem::onNewAudioModulesAvailable()
788 {
789 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
790 if (aps == 0) return;
791 aps->onNewAudioModulesAvailable();
792 }
793
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address,const char * device_name,audio_format_t encodedFormat)794 status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
795 audio_policy_dev_state_t state,
796 const char *device_address,
797 const char *device_name,
798 audio_format_t encodedFormat)
799 {
800 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
801 const char *address = "";
802 const char *name = "";
803
804 if (aps == 0) return PERMISSION_DENIED;
805
806 if (device_address != NULL) {
807 address = device_address;
808 }
809 if (device_name != NULL) {
810 name = device_name;
811 }
812 return aps->setDeviceConnectionState(device, state, address, name, encodedFormat);
813 }
814
getDeviceConnectionState(audio_devices_t device,const char * device_address)815 audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
816 const char *device_address)
817 {
818 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
819 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
820
821 return aps->getDeviceConnectionState(device, device_address);
822 }
823
handleDeviceConfigChange(audio_devices_t device,const char * device_address,const char * device_name,audio_format_t encodedFormat)824 status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device,
825 const char *device_address,
826 const char *device_name,
827 audio_format_t encodedFormat)
828 {
829 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
830 const char *address = "";
831 const char *name = "";
832
833 if (aps == 0) return PERMISSION_DENIED;
834
835 if (device_address != NULL) {
836 address = device_address;
837 }
838 if (device_name != NULL) {
839 name = device_name;
840 }
841 return aps->handleDeviceConfigChange(device, address, name, encodedFormat);
842 }
843
setPhoneState(audio_mode_t state)844 status_t AudioSystem::setPhoneState(audio_mode_t state)
845 {
846 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
847 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
848 if (aps == 0) return PERMISSION_DENIED;
849
850 return aps->setPhoneState(state);
851 }
852
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)853 status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
854 {
855 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
856 if (aps == 0) return PERMISSION_DENIED;
857 return aps->setForceUse(usage, config);
858 }
859
getForceUse(audio_policy_force_use_t usage)860 audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
861 {
862 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
863 if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
864 return aps->getForceUse(usage);
865 }
866
867
getOutput(audio_stream_type_t stream)868 audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream)
869 {
870 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
871 if (aps == 0) return 0;
872 return aps->getOutput(stream);
873 }
874
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)875 status_t AudioSystem::getOutputForAttr(audio_attributes_t *attr,
876 audio_io_handle_t *output,
877 audio_session_t session,
878 audio_stream_type_t *stream,
879 pid_t pid,
880 uid_t uid,
881 const audio_config_t *config,
882 audio_output_flags_t flags,
883 audio_port_handle_t *selectedDeviceId,
884 audio_port_handle_t *portId,
885 std::vector<audio_io_handle_t> *secondaryOutputs)
886 {
887 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
888 if (aps == 0) return NO_INIT;
889 return aps->getOutputForAttr(attr, output, session, stream, pid, uid,
890 config,
891 flags, selectedDeviceId, portId, secondaryOutputs);
892 }
893
startOutput(audio_port_handle_t portId)894 status_t AudioSystem::startOutput(audio_port_handle_t portId)
895 {
896 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
897 if (aps == 0) return PERMISSION_DENIED;
898 return aps->startOutput(portId);
899 }
900
stopOutput(audio_port_handle_t portId)901 status_t AudioSystem::stopOutput(audio_port_handle_t portId)
902 {
903 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
904 if (aps == 0) return PERMISSION_DENIED;
905 return aps->stopOutput(portId);
906 }
907
releaseOutput(audio_port_handle_t portId)908 void AudioSystem::releaseOutput(audio_port_handle_t portId)
909 {
910 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
911 if (aps == 0) return;
912 aps->releaseOutput(portId);
913 }
914
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)915 status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr,
916 audio_io_handle_t *input,
917 audio_unique_id_t riid,
918 audio_session_t session,
919 pid_t pid,
920 uid_t uid,
921 const String16& opPackageName,
922 const audio_config_base_t *config,
923 audio_input_flags_t flags,
924 audio_port_handle_t *selectedDeviceId,
925 audio_port_handle_t *portId)
926 {
927 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
928 if (aps == 0) return NO_INIT;
929 return aps->getInputForAttr(
930 attr, input, riid, session, pid, uid, opPackageName,
931 config, flags, selectedDeviceId, portId);
932 }
933
startInput(audio_port_handle_t portId)934 status_t AudioSystem::startInput(audio_port_handle_t portId)
935 {
936 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
937 if (aps == 0) return PERMISSION_DENIED;
938 return aps->startInput(portId);
939 }
940
stopInput(audio_port_handle_t portId)941 status_t AudioSystem::stopInput(audio_port_handle_t portId)
942 {
943 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
944 if (aps == 0) return PERMISSION_DENIED;
945 return aps->stopInput(portId);
946 }
947
releaseInput(audio_port_handle_t portId)948 void AudioSystem::releaseInput(audio_port_handle_t portId)
949 {
950 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
951 if (aps == 0) return;
952 aps->releaseInput(portId);
953 }
954
initStreamVolume(audio_stream_type_t stream,int indexMin,int indexMax)955 status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
956 int indexMin,
957 int indexMax)
958 {
959 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
960 if (aps == 0) return PERMISSION_DENIED;
961 return aps->initStreamVolume(stream, indexMin, indexMax);
962 }
963
setStreamVolumeIndex(audio_stream_type_t stream,int index,audio_devices_t device)964 status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
965 int index,
966 audio_devices_t device)
967 {
968 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
969 if (aps == 0) return PERMISSION_DENIED;
970 return aps->setStreamVolumeIndex(stream, index, device);
971 }
972
getStreamVolumeIndex(audio_stream_type_t stream,int * index,audio_devices_t device)973 status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
974 int *index,
975 audio_devices_t device)
976 {
977 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
978 if (aps == 0) return PERMISSION_DENIED;
979 return aps->getStreamVolumeIndex(stream, index, device);
980 }
981
setVolumeIndexForAttributes(const audio_attributes_t & attr,int index,audio_devices_t device)982 status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t &attr,
983 int index,
984 audio_devices_t device)
985 {
986 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
987 if (aps == 0) return PERMISSION_DENIED;
988 return aps->setVolumeIndexForAttributes(attr, index, device);
989 }
990
getVolumeIndexForAttributes(const audio_attributes_t & attr,int & index,audio_devices_t device)991 status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t &attr,
992 int &index,
993 audio_devices_t device)
994 {
995 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
996 if (aps == 0) return PERMISSION_DENIED;
997 return aps->getVolumeIndexForAttributes(attr, index, device);
998 }
999
getMaxVolumeIndexForAttributes(const audio_attributes_t & attr,int & index)1000 status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1001 {
1002 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1003 if (aps == 0) return PERMISSION_DENIED;
1004 return aps->getMaxVolumeIndexForAttributes(attr, index);
1005 }
1006
getMinVolumeIndexForAttributes(const audio_attributes_t & attr,int & index)1007 status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
1008 {
1009 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1010 if (aps == 0) return PERMISSION_DENIED;
1011 return aps->getMinVolumeIndexForAttributes(attr, index);
1012 }
1013
getStrategyForStream(audio_stream_type_t stream)1014 uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
1015 {
1016 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1017 if (aps == 0) return PRODUCT_STRATEGY_NONE;
1018 return aps->getStrategyForStream(stream);
1019 }
1020
getDevicesForStream(audio_stream_type_t stream)1021 audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
1022 {
1023 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1024 if (aps == 0) return AUDIO_DEVICE_NONE;
1025 return aps->getDevicesForStream(stream);
1026 }
1027
getOutputForEffect(const effect_descriptor_t * desc)1028 audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
1029 {
1030 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1031 // FIXME change return type to status_t, and return PERMISSION_DENIED here
1032 if (aps == 0) return AUDIO_IO_HANDLE_NONE;
1033 return aps->getOutputForEffect(desc);
1034 }
1035
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,uint32_t strategy,audio_session_t session,int id)1036 status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
1037 audio_io_handle_t io,
1038 uint32_t strategy,
1039 audio_session_t session,
1040 int id)
1041 {
1042 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1043 if (aps == 0) return PERMISSION_DENIED;
1044 return aps->registerEffect(desc, io, strategy, session, id);
1045 }
1046
unregisterEffect(int id)1047 status_t AudioSystem::unregisterEffect(int id)
1048 {
1049 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1050 if (aps == 0) return PERMISSION_DENIED;
1051 return aps->unregisterEffect(id);
1052 }
1053
setEffectEnabled(int id,bool enabled)1054 status_t AudioSystem::setEffectEnabled(int id, bool enabled)
1055 {
1056 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1057 if (aps == 0) return PERMISSION_DENIED;
1058 return aps->setEffectEnabled(id, enabled);
1059 }
1060
moveEffectsToIo(const std::vector<int> & ids,audio_io_handle_t io)1061 status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io)
1062 {
1063 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1064 if (aps == 0) return PERMISSION_DENIED;
1065 return aps->moveEffectsToIo(ids, io);
1066 }
1067
isStreamActive(audio_stream_type_t stream,bool * state,uint32_t inPastMs)1068 status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
1069 {
1070 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1071 if (aps == 0) return PERMISSION_DENIED;
1072 if (state == NULL) return BAD_VALUE;
1073 *state = aps->isStreamActive(stream, inPastMs);
1074 return NO_ERROR;
1075 }
1076
isStreamActiveRemotely(audio_stream_type_t stream,bool * state,uint32_t inPastMs)1077 status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
1078 uint32_t inPastMs)
1079 {
1080 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1081 if (aps == 0) return PERMISSION_DENIED;
1082 if (state == NULL) return BAD_VALUE;
1083 *state = aps->isStreamActiveRemotely(stream, inPastMs);
1084 return NO_ERROR;
1085 }
1086
isSourceActive(audio_source_t stream,bool * state)1087 status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
1088 {
1089 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1090 if (aps == 0) return PERMISSION_DENIED;
1091 if (state == NULL) return BAD_VALUE;
1092 *state = aps->isSourceActive(stream);
1093 return NO_ERROR;
1094 }
1095
getPrimaryOutputSamplingRate()1096 uint32_t AudioSystem::getPrimaryOutputSamplingRate()
1097 {
1098 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1099 if (af == 0) return 0;
1100 return af->getPrimaryOutputSamplingRate();
1101 }
1102
getPrimaryOutputFrameCount()1103 size_t AudioSystem::getPrimaryOutputFrameCount()
1104 {
1105 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1106 if (af == 0) return 0;
1107 return af->getPrimaryOutputFrameCount();
1108 }
1109
setLowRamDevice(bool isLowRamDevice,int64_t totalMemory)1110 status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory)
1111 {
1112 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1113 if (af == 0) return PERMISSION_DENIED;
1114 return af->setLowRamDevice(isLowRamDevice, totalMemory);
1115 }
1116
clearAudioConfigCache()1117 void AudioSystem::clearAudioConfigCache()
1118 {
1119 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances
1120 ALOGV("clearAudioConfigCache()");
1121 {
1122 Mutex::Autolock _l(gLock);
1123 if (gAudioFlingerClient != 0) {
1124 gAudioFlingerClient->clearIoCache();
1125 }
1126 gAudioFlinger.clear();
1127 }
1128 {
1129 Mutex::Autolock _l(gLockAPS);
1130 gAudioPolicyService.clear();
1131 }
1132 }
1133
setAllowedCapturePolicy(uid_t uid,audio_flags_mask_t flags)1134 status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) {
1135 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1136 if (aps == nullptr) return PERMISSION_DENIED;
1137 return aps->setAllowedCapturePolicy(uid, flags);
1138 }
1139
isOffloadSupported(const audio_offload_info_t & info)1140 bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
1141 {
1142 ALOGV("isOffloadSupported()");
1143 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1144 if (aps == 0) return false;
1145 return aps->isOffloadSupported(info);
1146 }
1147
listAudioPorts(audio_port_role_t role,audio_port_type_t type,unsigned int * num_ports,struct audio_port * ports,unsigned int * generation)1148 status_t AudioSystem::listAudioPorts(audio_port_role_t role,
1149 audio_port_type_t type,
1150 unsigned int *num_ports,
1151 struct audio_port *ports,
1152 unsigned int *generation)
1153 {
1154 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1155 if (aps == 0) return PERMISSION_DENIED;
1156 return aps->listAudioPorts(role, type, num_ports, ports, generation);
1157 }
1158
getAudioPort(struct audio_port * port)1159 status_t AudioSystem::getAudioPort(struct audio_port *port)
1160 {
1161 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1162 if (aps == 0) return PERMISSION_DENIED;
1163 return aps->getAudioPort(port);
1164 }
1165
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle)1166 status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
1167 audio_patch_handle_t *handle)
1168 {
1169 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1170 if (aps == 0) return PERMISSION_DENIED;
1171 return aps->createAudioPatch(patch, handle);
1172 }
1173
releaseAudioPatch(audio_patch_handle_t handle)1174 status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
1175 {
1176 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1177 if (aps == 0) return PERMISSION_DENIED;
1178 return aps->releaseAudioPatch(handle);
1179 }
1180
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches,unsigned int * generation)1181 status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
1182 struct audio_patch *patches,
1183 unsigned int *generation)
1184 {
1185 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1186 if (aps == 0) return PERMISSION_DENIED;
1187 return aps->listAudioPatches(num_patches, patches, generation);
1188 }
1189
setAudioPortConfig(const struct audio_port_config * config)1190 status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
1191 {
1192 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1193 if (aps == 0) return PERMISSION_DENIED;
1194 return aps->setAudioPortConfig(config);
1195 }
1196
addAudioPortCallback(const sp<AudioPortCallback> & callback)1197 status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback)
1198 {
1199 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1200 if (aps == 0) return PERMISSION_DENIED;
1201
1202 Mutex::Autolock _l(gLockAPS);
1203 if (gAudioPolicyServiceClient == 0) {
1204 return NO_INIT;
1205 }
1206 int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback);
1207 if (ret == 1) {
1208 aps->setAudioPortCallbacksEnabled(true);
1209 }
1210 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1211 }
1212
1213 /*static*/
removeAudioPortCallback(const sp<AudioPortCallback> & callback)1214 status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback)
1215 {
1216 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1217 if (aps == 0) return PERMISSION_DENIED;
1218
1219 Mutex::Autolock _l(gLockAPS);
1220 if (gAudioPolicyServiceClient == 0) {
1221 return NO_INIT;
1222 }
1223 int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback);
1224 if (ret == 0) {
1225 aps->setAudioPortCallbacksEnabled(false);
1226 }
1227 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1228 }
1229
addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback> & callback)1230 status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1231 {
1232 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1233 if (aps == 0) return PERMISSION_DENIED;
1234
1235 Mutex::Autolock _l(gLockAPS);
1236 if (gAudioPolicyServiceClient == 0) {
1237 return NO_INIT;
1238 }
1239 int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback);
1240 if (ret == 1) {
1241 aps->setAudioVolumeGroupCallbacksEnabled(true);
1242 }
1243 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1244 }
1245
removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback> & callback)1246 status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback)
1247 {
1248 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1249 if (aps == 0) return PERMISSION_DENIED;
1250
1251 Mutex::Autolock _l(gLockAPS);
1252 if (gAudioPolicyServiceClient == 0) {
1253 return NO_INIT;
1254 }
1255 int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback);
1256 if (ret == 0) {
1257 aps->setAudioVolumeGroupCallbacksEnabled(false);
1258 }
1259 return (ret < 0) ? INVALID_OPERATION : NO_ERROR;
1260 }
1261
addAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo,audio_port_handle_t portId)1262 status_t AudioSystem::addAudioDeviceCallback(
1263 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1264 audio_port_handle_t portId)
1265 {
1266 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1267 if (afc == 0) {
1268 return NO_INIT;
1269 }
1270 status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId);
1271 if (status == NO_ERROR) {
1272 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1273 if (af != 0) {
1274 af->registerClient(afc);
1275 }
1276 }
1277 return status;
1278 }
1279
removeAudioDeviceCallback(const wp<AudioDeviceCallback> & callback,audio_io_handle_t audioIo,audio_port_handle_t portId)1280 status_t AudioSystem::removeAudioDeviceCallback(
1281 const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo,
1282 audio_port_handle_t portId)
1283 {
1284 const sp<AudioFlingerClient> afc = getAudioFlingerClient();
1285 if (afc == 0) {
1286 return NO_INIT;
1287 }
1288 return afc->removeAudioDeviceCallback(callback, audioIo, portId);
1289 }
1290
getDeviceIdForIo(audio_io_handle_t audioIo)1291 audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo)
1292 {
1293 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1294 if (af == 0) return PERMISSION_DENIED;
1295 const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo);
1296 if (desc == 0) {
1297 return AUDIO_PORT_HANDLE_NONE;
1298 }
1299 return desc->getDeviceId();
1300 }
1301
acquireSoundTriggerSession(audio_session_t * session,audio_io_handle_t * ioHandle,audio_devices_t * device)1302 status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
1303 audio_io_handle_t *ioHandle,
1304 audio_devices_t *device)
1305 {
1306 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1307 if (aps == 0) return PERMISSION_DENIED;
1308 return aps->acquireSoundTriggerSession(session, ioHandle, device);
1309 }
1310
releaseSoundTriggerSession(audio_session_t session)1311 status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
1312 {
1313 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1314 if (aps == 0) return PERMISSION_DENIED;
1315 return aps->releaseSoundTriggerSession(session);
1316 }
1317
getPhoneState()1318 audio_mode_t AudioSystem::getPhoneState()
1319 {
1320 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1321 if (aps == 0) return AUDIO_MODE_INVALID;
1322 return aps->getPhoneState();
1323 }
1324
registerPolicyMixes(const Vector<AudioMix> & mixes,bool registration)1325 status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
1326 {
1327 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1328 if (aps == 0) return PERMISSION_DENIED;
1329 return aps->registerPolicyMixes(mixes, registration);
1330 }
1331
setUidDeviceAffinities(uid_t uid,const Vector<AudioDeviceTypeAddr> & devices)1332 status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1333 {
1334 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1335 if (aps == 0) return PERMISSION_DENIED;
1336 return aps->setUidDeviceAffinities(uid, devices);
1337 }
1338
removeUidDeviceAffinities(uid_t uid)1339 status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) {
1340 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1341 if (aps == 0) return PERMISSION_DENIED;
1342 return aps->removeUidDeviceAffinities(uid);
1343 }
1344
startAudioSource(const struct audio_port_config * source,const audio_attributes_t * attributes,audio_port_handle_t * portId)1345 status_t AudioSystem::startAudioSource(const struct audio_port_config *source,
1346 const audio_attributes_t *attributes,
1347 audio_port_handle_t *portId)
1348 {
1349 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1350 if (aps == 0) return PERMISSION_DENIED;
1351 return aps->startAudioSource(source, attributes, portId);
1352 }
1353
stopAudioSource(audio_port_handle_t portId)1354 status_t AudioSystem::stopAudioSource(audio_port_handle_t portId)
1355 {
1356 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1357 if (aps == 0) return PERMISSION_DENIED;
1358 return aps->stopAudioSource(portId);
1359 }
1360
setMasterMono(bool mono)1361 status_t AudioSystem::setMasterMono(bool mono)
1362 {
1363 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1364 if (aps == 0) return PERMISSION_DENIED;
1365 return aps->setMasterMono(mono);
1366 }
1367
getMasterMono(bool * mono)1368 status_t AudioSystem::getMasterMono(bool *mono)
1369 {
1370 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1371 if (aps == 0) return PERMISSION_DENIED;
1372 return aps->getMasterMono(mono);
1373 }
1374
setMasterBalance(float balance)1375 status_t AudioSystem::setMasterBalance(float balance)
1376 {
1377 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1378 if (af == 0) return PERMISSION_DENIED;
1379 return af->setMasterBalance(balance);
1380 }
1381
getMasterBalance(float * balance)1382 status_t AudioSystem::getMasterBalance(float *balance)
1383 {
1384 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1385 if (af == 0) return PERMISSION_DENIED;
1386 return af->getMasterBalance(balance);
1387 }
1388
getStreamVolumeDB(audio_stream_type_t stream,int index,audio_devices_t device)1389 float AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
1390 {
1391 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1392 if (aps == 0) return NAN;
1393 return aps->getStreamVolumeDB(stream, index, device);
1394 }
1395
getMicrophones(std::vector<media::MicrophoneInfo> * microphones)1396 status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo> *microphones)
1397 {
1398 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1399 if (af == 0) return PERMISSION_DENIED;
1400 return af->getMicrophones(microphones);
1401 }
1402
setAudioHalPids(const std::vector<pid_t> & pids)1403 status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) {
1404 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
1405 if (af == nullptr) return PERMISSION_DENIED;
1406 return af->setAudioHalPids(pids);
1407 }
1408
getSurroundFormats(unsigned int * numSurroundFormats,audio_format_t * surroundFormats,bool * surroundFormatsEnabled,bool reported)1409 status_t AudioSystem::getSurroundFormats(unsigned int *numSurroundFormats,
1410 audio_format_t *surroundFormats,
1411 bool *surroundFormatsEnabled,
1412 bool reported)
1413 {
1414 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1415 if (aps == 0) return PERMISSION_DENIED;
1416 return aps->getSurroundFormats(
1417 numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1418 }
1419
setSurroundFormatEnabled(audio_format_t audioFormat,bool enabled)1420 status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
1421 {
1422 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1423 if (aps == 0) return PERMISSION_DENIED;
1424 return aps->setSurroundFormatEnabled(audioFormat, enabled);
1425 }
1426
setAssistantUid(uid_t uid)1427 status_t AudioSystem::setAssistantUid(uid_t uid)
1428 {
1429 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1430 if (aps == 0) return PERMISSION_DENIED;
1431
1432 return aps->setAssistantUid(uid);
1433 }
1434
setA11yServicesUids(const std::vector<uid_t> & uids)1435 status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids)
1436 {
1437 const sp <IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1438 if (aps == 0) return PERMISSION_DENIED;
1439
1440 return aps->setA11yServicesUids(uids);
1441 }
1442
isHapticPlaybackSupported()1443 bool AudioSystem::isHapticPlaybackSupported()
1444 {
1445 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1446 if (aps == 0) return false;
1447 return aps->isHapticPlaybackSupported();
1448 }
1449
getHwOffloadEncodingFormatsSupportedForA2DP(std::vector<audio_format_t> * formats)1450 status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP(
1451 std::vector<audio_format_t> *formats) {
1452 const sp <IAudioPolicyService>
1453 & aps = AudioSystem::get_audio_policy_service();
1454 if (aps == 0) return PERMISSION_DENIED;
1455 return aps->getHwOffloadEncodingFormatsSupportedForA2DP(formats);
1456 }
1457
listAudioProductStrategies(AudioProductStrategyVector & strategies)1458 status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector &strategies)
1459 {
1460 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1461 if (aps == 0) return PERMISSION_DENIED;
1462 return aps->listAudioProductStrategies(strategies);
1463 }
1464
streamTypeToAttributes(audio_stream_type_t stream)1465 audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream)
1466 {
1467 AudioProductStrategyVector strategies;
1468 listAudioProductStrategies(strategies);
1469 for (const auto &strategy : strategies) {
1470 auto attrVect = strategy.getAudioAttributes();
1471 auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto &attributes) {
1472 return attributes.getStreamType() == stream; });
1473 if (iter != end(attrVect)) {
1474 return iter->getAttributes();
1475 }
1476 }
1477 ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str());
1478 return AUDIO_ATTRIBUTES_INITIALIZER;
1479 }
1480
attributesToStreamType(const audio_attributes_t & attr)1481 audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t &attr)
1482 {
1483 product_strategy_t psId;
1484 status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId);
1485 if (ret != NO_ERROR) {
1486 ALOGE("no strategy found for attributes %s", toString(attr).c_str());
1487 return AUDIO_STREAM_MUSIC;
1488 }
1489 AudioProductStrategyVector strategies;
1490 listAudioProductStrategies(strategies);
1491 for (const auto &strategy : strategies) {
1492 if (strategy.getId() == psId) {
1493 auto attrVect = strategy.getAudioAttributes();
1494 auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto &refAttr) {
1495 return AudioProductStrategy::attributesMatches(
1496 refAttr.getAttributes(), attr); });
1497 if (iter != end(attrVect)) {
1498 return iter->getStreamType();
1499 }
1500 }
1501 }
1502 switch (attr.usage) {
1503 case AUDIO_USAGE_VIRTUAL_SOURCE:
1504 // virtual source is not expected to have an associated product strategy
1505 break;
1506 default:
1507 ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str());
1508 break;
1509 }
1510 return AUDIO_STREAM_MUSIC;
1511 }
1512
getProductStrategyFromAudioAttributes(const AudioAttributes & aa,product_strategy_t & productStrategy)1513 status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1514 product_strategy_t &productStrategy)
1515 {
1516 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1517 if (aps == 0) return PERMISSION_DENIED;
1518 return aps->getProductStrategyFromAudioAttributes(aa,productStrategy);
1519 }
1520
listAudioVolumeGroups(AudioVolumeGroupVector & groups)1521 status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1522 {
1523 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1524 if (aps == 0) return PERMISSION_DENIED;
1525 return aps->listAudioVolumeGroups(groups);
1526 }
1527
getVolumeGroupFromAudioAttributes(const AudioAttributes & aa,volume_group_t & volumeGroup)1528 status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1529 volume_group_t &volumeGroup)
1530 {
1531 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1532 if (aps == 0) return PERMISSION_DENIED;
1533 return aps->getVolumeGroupFromAudioAttributes(aa, volumeGroup);
1534 }
1535
setRttEnabled(bool enabled)1536 status_t AudioSystem::setRttEnabled(bool enabled)
1537 {
1538 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1539 if (aps == 0) return PERMISSION_DENIED;
1540 return aps->setRttEnabled(enabled);
1541 }
1542
setPreferredDeviceForStrategy(product_strategy_t strategy,const AudioDeviceTypeAddr & device)1543 status_t AudioSystem::setPreferredDeviceForStrategy(product_strategy_t strategy,
1544 const AudioDeviceTypeAddr &device)
1545 {
1546 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1547 if (aps == 0) {
1548 return PERMISSION_DENIED;
1549 }
1550 return aps->setPreferredDeviceForStrategy(strategy, device);
1551 }
1552
removePreferredDeviceForStrategy(product_strategy_t strategy)1553 status_t AudioSystem::removePreferredDeviceForStrategy(product_strategy_t strategy)
1554 {
1555 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1556 if (aps == 0) {
1557 return PERMISSION_DENIED;
1558 }
1559 return aps->removePreferredDeviceForStrategy(strategy);
1560 }
1561
getPreferredDeviceForStrategy(product_strategy_t strategy,AudioDeviceTypeAddr & device)1562 status_t AudioSystem::getPreferredDeviceForStrategy(product_strategy_t strategy,
1563 AudioDeviceTypeAddr &device)
1564 {
1565 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
1566 if (aps == 0) {
1567 return PERMISSION_DENIED;
1568 }
1569 return aps->getPreferredDeviceForStrategy(strategy, device);
1570 }
1571
1572 // ---------------------------------------------------------------------------
1573
addAudioPortCallback(const sp<AudioPortCallback> & callback)1574 int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback(
1575 const sp<AudioPortCallback>& callback)
1576 {
1577 Mutex::Autolock _l(mLock);
1578 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1579 if (mAudioPortCallbacks[i] == callback) {
1580 return -1;
1581 }
1582 }
1583 mAudioPortCallbacks.add(callback);
1584 return mAudioPortCallbacks.size();
1585 }
1586
removeAudioPortCallback(const sp<AudioPortCallback> & callback)1587 int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback(
1588 const sp<AudioPortCallback>& callback)
1589 {
1590 Mutex::Autolock _l(mLock);
1591 size_t i;
1592 for (i = 0; i < mAudioPortCallbacks.size(); i++) {
1593 if (mAudioPortCallbacks[i] == callback) {
1594 break;
1595 }
1596 }
1597 if (i == mAudioPortCallbacks.size()) {
1598 return -1;
1599 }
1600 mAudioPortCallbacks.removeAt(i);
1601 return mAudioPortCallbacks.size();
1602 }
1603
1604
onAudioPortListUpdate()1605 void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
1606 {
1607 Mutex::Autolock _l(mLock);
1608 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1609 mAudioPortCallbacks[i]->onAudioPortListUpdate();
1610 }
1611 }
1612
onAudioPatchListUpdate()1613 void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
1614 {
1615 Mutex::Autolock _l(mLock);
1616 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1617 mAudioPortCallbacks[i]->onAudioPatchListUpdate();
1618 }
1619 }
1620
1621 // ----------------------------------------------------------------------------
addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback> & callback)1622 int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback(
1623 const sp<AudioVolumeGroupCallback>& callback)
1624 {
1625 Mutex::Autolock _l(mLock);
1626 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1627 if (mAudioVolumeGroupCallback[i] == callback) {
1628 return -1;
1629 }
1630 }
1631 mAudioVolumeGroupCallback.add(callback);
1632 return mAudioVolumeGroupCallback.size();
1633 }
1634
removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback> & callback)1635 int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback(
1636 const sp<AudioVolumeGroupCallback>& callback)
1637 {
1638 Mutex::Autolock _l(mLock);
1639 size_t i;
1640 for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1641 if (mAudioVolumeGroupCallback[i] == callback) {
1642 break;
1643 }
1644 }
1645 if (i == mAudioVolumeGroupCallback.size()) {
1646 return -1;
1647 }
1648 mAudioVolumeGroupCallback.removeAt(i);
1649 return mAudioVolumeGroupCallback.size();
1650 }
1651
onAudioVolumeGroupChanged(volume_group_t group,int flags)1652 void AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(volume_group_t group,
1653 int flags)
1654 {
1655 Mutex::Autolock _l(mLock);
1656 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1657 mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(group, flags);
1658 }
1659 }
1660 // ----------------------------------------------------------------------------
1661
onDynamicPolicyMixStateUpdate(String8 regId,int32_t state)1662 void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(
1663 String8 regId, int32_t state)
1664 {
1665 ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state);
1666 dynamic_policy_callback cb = NULL;
1667 {
1668 Mutex::Autolock _l(AudioSystem::gLock);
1669 cb = gDynPolicyCallback;
1670 }
1671
1672 if (cb != NULL) {
1673 cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state);
1674 }
1675 }
1676
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)1677 void AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate(
1678 int event,
1679 const record_client_info_t *clientInfo,
1680 const audio_config_base_t *clientConfig,
1681 std::vector<effect_descriptor_t> clientEffects,
1682 const audio_config_base_t *deviceConfig,
1683 std::vector<effect_descriptor_t> effects,
1684 audio_patch_handle_t patchHandle,
1685 audio_source_t source) {
1686 record_config_callback cb = NULL;
1687 {
1688 Mutex::Autolock _l(AudioSystem::gLock);
1689 cb = gRecordConfigCallback;
1690 }
1691
1692 if (cb != NULL) {
1693 cb(event, clientInfo, clientConfig, clientEffects,
1694 deviceConfig, effects, patchHandle, source);
1695 }
1696 }
1697
binderDied(const wp<IBinder> & who __unused)1698 void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
1699 {
1700 {
1701 Mutex::Autolock _l(mLock);
1702 for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) {
1703 mAudioPortCallbacks[i]->onServiceDied();
1704 }
1705 for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) {
1706 mAudioVolumeGroupCallback[i]->onServiceDied();
1707 }
1708 }
1709 {
1710 Mutex::Autolock _l(gLockAPS);
1711 AudioSystem::gAudioPolicyService.clear();
1712 }
1713
1714 ALOGW("AudioPolicyService server died!");
1715 }
1716
1717 } // namespace android
1718