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