• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/IAudioPolicyService.h>
24 #include <math.h>
25 
26 #include <system/audio.h>
27 
28 // ----------------------------------------------------------------------------
29 
30 namespace android {
31 
32 // client singleton for AudioFlinger binder interface
33 Mutex AudioSystem::gLock;
34 sp<IAudioFlinger> AudioSystem::gAudioFlinger;
35 sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
36 audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
37 // Cached values
38 
39 DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0);
40 
41 // Cached values for recording queries, all protected by gLock
42 uint32_t AudioSystem::gPrevInSamplingRate = 16000;
43 audio_format_t AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT;
44 audio_channel_mask_t AudioSystem::gPrevInChannelMask = AUDIO_CHANNEL_IN_MONO;
45 size_t AudioSystem::gInBuffSize = 0;
46 
47 
48 // establish binder interface to AudioFlinger service
get_audio_flinger()49 const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
50 {
51     Mutex::Autolock _l(gLock);
52     if (gAudioFlinger == 0) {
53         sp<IServiceManager> sm = defaultServiceManager();
54         sp<IBinder> binder;
55         do {
56             binder = sm->getService(String16("media.audio_flinger"));
57             if (binder != 0)
58                 break;
59             ALOGW("AudioFlinger not published, waiting...");
60             usleep(500000); // 0.5 s
61         } while (true);
62         if (gAudioFlingerClient == NULL) {
63             gAudioFlingerClient = new AudioFlingerClient();
64         } else {
65             if (gAudioErrorCallback) {
66                 gAudioErrorCallback(NO_ERROR);
67             }
68         }
69         binder->linkToDeath(gAudioFlingerClient);
70         gAudioFlinger = interface_cast<IAudioFlinger>(binder);
71         gAudioFlinger->registerClient(gAudioFlingerClient);
72     }
73     ALOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
74 
75     return gAudioFlinger;
76 }
77 
muteMicrophone(bool state)78 status_t AudioSystem::muteMicrophone(bool state) {
79     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
80     if (af == 0) return PERMISSION_DENIED;
81     return af->setMicMute(state);
82 }
83 
isMicrophoneMuted(bool * state)84 status_t AudioSystem::isMicrophoneMuted(bool* state) {
85     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
86     if (af == 0) return PERMISSION_DENIED;
87     *state = af->getMicMute();
88     return NO_ERROR;
89 }
90 
setMasterVolume(float value)91 status_t AudioSystem::setMasterVolume(float value)
92 {
93     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
94     if (af == 0) return PERMISSION_DENIED;
95     af->setMasterVolume(value);
96     return NO_ERROR;
97 }
98 
setMasterMute(bool mute)99 status_t AudioSystem::setMasterMute(bool mute)
100 {
101     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
102     if (af == 0) return PERMISSION_DENIED;
103     af->setMasterMute(mute);
104     return NO_ERROR;
105 }
106 
getMasterVolume(float * volume)107 status_t AudioSystem::getMasterVolume(float* volume)
108 {
109     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
110     if (af == 0) return PERMISSION_DENIED;
111     *volume = af->masterVolume();
112     return NO_ERROR;
113 }
114 
getMasterMute(bool * mute)115 status_t AudioSystem::getMasterMute(bool* mute)
116 {
117     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
118     if (af == 0) return PERMISSION_DENIED;
119     *mute = af->masterMute();
120     return NO_ERROR;
121 }
122 
setStreamVolume(audio_stream_type_t stream,float value,audio_io_handle_t output)123 status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
124         audio_io_handle_t output)
125 {
126     if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
127     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
128     if (af == 0) return PERMISSION_DENIED;
129     af->setStreamVolume(stream, value, output);
130     return NO_ERROR;
131 }
132 
setStreamMute(audio_stream_type_t stream,bool mute)133 status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
134 {
135     if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
136     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
137     if (af == 0) return PERMISSION_DENIED;
138     af->setStreamMute(stream, mute);
139     return NO_ERROR;
140 }
141 
getStreamVolume(audio_stream_type_t stream,float * volume,audio_io_handle_t output)142 status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
143         audio_io_handle_t output)
144 {
145     if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
146     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
147     if (af == 0) return PERMISSION_DENIED;
148     *volume = af->streamVolume(stream, output);
149     return NO_ERROR;
150 }
151 
getStreamMute(audio_stream_type_t stream,bool * mute)152 status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
153 {
154     if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
155     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
156     if (af == 0) return PERMISSION_DENIED;
157     *mute = af->streamMute(stream);
158     return NO_ERROR;
159 }
160 
setMode(audio_mode_t mode)161 status_t AudioSystem::setMode(audio_mode_t mode)
162 {
163     if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
164     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
165     if (af == 0) return PERMISSION_DENIED;
166     return af->setMode(mode);
167 }
168 
setParameters(audio_io_handle_t ioHandle,const String8 & keyValuePairs)169 status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
170     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
171     if (af == 0) return PERMISSION_DENIED;
172     return af->setParameters(ioHandle, keyValuePairs);
173 }
174 
getParameters(audio_io_handle_t ioHandle,const String8 & keys)175 String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
176     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
177     String8 result = String8("");
178     if (af == 0) return result;
179 
180     result = af->getParameters(ioHandle, keys);
181     return result;
182 }
183 
184 // convert volume steps to natural log scale
185 
186 // change this value to change volume scaling
187 static const float dBPerStep = 0.5f;
188 // shouldn't need to touch these
189 static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
190 static const float dBConvertInverse = 1.0f / dBConvert;
191 
linearToLog(int volume)192 float AudioSystem::linearToLog(int volume)
193 {
194     // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
195     // ALOGD("linearToLog(%d)=%f", volume, v);
196     // return v;
197     return volume ? exp(float(100 - volume) * dBConvert) : 0;
198 }
199 
logToLinear(float volume)200 int AudioSystem::logToLinear(float volume)
201 {
202     // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
203     // ALOGD("logTolinear(%d)=%f", v, volume);
204     // return v;
205     return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
206 }
207 
getOutputSamplingRate(uint32_t * samplingRate,audio_stream_type_t streamType)208 status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
209 {
210     audio_io_handle_t output;
211 
212     if (streamType == AUDIO_STREAM_DEFAULT) {
213         streamType = AUDIO_STREAM_MUSIC;
214     }
215 
216     output = getOutput(streamType);
217     if (output == 0) {
218         return PERMISSION_DENIED;
219     }
220 
221     return getSamplingRate(output, streamType, samplingRate);
222 }
223 
getSamplingRate(audio_io_handle_t output,audio_stream_type_t streamType,uint32_t * samplingRate)224 status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
225                                       audio_stream_type_t streamType,
226                                       uint32_t* samplingRate)
227 {
228     OutputDescriptor *outputDesc;
229 
230     gLock.lock();
231     outputDesc = AudioSystem::gOutputs.valueFor(output);
232     if (outputDesc == NULL) {
233         ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
234         gLock.unlock();
235         const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
236         if (af == 0) return PERMISSION_DENIED;
237         *samplingRate = af->sampleRate(output);
238     } else {
239         ALOGV("getOutputSamplingRate() reading from output desc");
240         *samplingRate = outputDesc->samplingRate;
241         gLock.unlock();
242     }
243 
244     ALOGV("getSamplingRate() streamType %d, output %d, sampling rate %u", streamType, output,
245             *samplingRate);
246 
247     return NO_ERROR;
248 }
249 
getOutputFrameCount(size_t * frameCount,audio_stream_type_t streamType)250 status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
251 {
252     audio_io_handle_t output;
253 
254     if (streamType == AUDIO_STREAM_DEFAULT) {
255         streamType = AUDIO_STREAM_MUSIC;
256     }
257 
258     output = getOutput(streamType);
259     if (output == 0) {
260         return PERMISSION_DENIED;
261     }
262 
263     return getFrameCount(output, streamType, frameCount);
264 }
265 
getFrameCount(audio_io_handle_t output,audio_stream_type_t streamType,size_t * frameCount)266 status_t AudioSystem::getFrameCount(audio_io_handle_t output,
267                                     audio_stream_type_t streamType,
268                                     size_t* frameCount)
269 {
270     OutputDescriptor *outputDesc;
271 
272     gLock.lock();
273     outputDesc = AudioSystem::gOutputs.valueFor(output);
274     if (outputDesc == NULL) {
275         gLock.unlock();
276         const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
277         if (af == 0) return PERMISSION_DENIED;
278         *frameCount = af->frameCount(output);
279     } else {
280         *frameCount = outputDesc->frameCount;
281         gLock.unlock();
282     }
283 
284     ALOGV("getFrameCount() streamType %d, output %d, frameCount %d", streamType, output,
285             *frameCount);
286 
287     return NO_ERROR;
288 }
289 
getOutputLatency(uint32_t * latency,audio_stream_type_t streamType)290 status_t AudioSystem::getOutputLatency(uint32_t* latency, 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 == 0) {
300         return PERMISSION_DENIED;
301     }
302 
303     return getLatency(output, streamType, latency);
304 }
305 
getLatency(audio_io_handle_t output,audio_stream_type_t streamType,uint32_t * latency)306 status_t AudioSystem::getLatency(audio_io_handle_t output,
307                                  audio_stream_type_t streamType,
308                                  uint32_t* latency)
309 {
310     OutputDescriptor *outputDesc;
311 
312     gLock.lock();
313     outputDesc = AudioSystem::gOutputs.valueFor(output);
314     if (outputDesc == NULL) {
315         gLock.unlock();
316         const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
317         if (af == 0) return PERMISSION_DENIED;
318         *latency = af->latency(output);
319     } else {
320         *latency = outputDesc->latency;
321         gLock.unlock();
322     }
323 
324     ALOGV("getLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
325 
326     return NO_ERROR;
327 }
328 
getInputBufferSize(uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t * buffSize)329 status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
330         audio_channel_mask_t channelMask, size_t* buffSize)
331 {
332     gLock.lock();
333     // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
334     size_t inBuffSize = gInBuffSize;
335     if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
336         || (channelMask != gPrevInChannelMask)) {
337         gLock.unlock();
338         const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
339         if (af == 0) {
340             return PERMISSION_DENIED;
341         }
342         inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
343         gLock.lock();
344         // save the request params
345         gPrevInSamplingRate = sampleRate;
346         gPrevInFormat = format;
347         gPrevInChannelMask = channelMask;
348 
349         gInBuffSize = inBuffSize;
350     }
351     gLock.unlock();
352     *buffSize = inBuffSize;
353 
354     return NO_ERROR;
355 }
356 
setVoiceVolume(float value)357 status_t AudioSystem::setVoiceVolume(float value)
358 {
359     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
360     if (af == 0) return PERMISSION_DENIED;
361     return af->setVoiceVolume(value);
362 }
363 
getRenderPosition(size_t * halFrames,size_t * dspFrames,audio_stream_type_t stream)364 status_t AudioSystem::getRenderPosition(size_t *halFrames, size_t *dspFrames,
365         audio_stream_type_t stream)
366 {
367     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
368     if (af == 0) return PERMISSION_DENIED;
369 
370     if (stream == AUDIO_STREAM_DEFAULT) {
371         stream = AUDIO_STREAM_MUSIC;
372     }
373 
374     return af->getRenderPosition(halFrames, dspFrames, getOutput(stream));
375 }
376 
getInputFramesLost(audio_io_handle_t ioHandle)377 size_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
378     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379     unsigned int result = 0;
380     if (af == 0) return result;
381     if (ioHandle == 0) return result;
382 
383     result = af->getInputFramesLost(ioHandle);
384     return result;
385 }
386 
newAudioSessionId()387 int AudioSystem::newAudioSessionId() {
388     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
389     if (af == 0) return 0;
390     return af->newAudioSessionId();
391 }
392 
acquireAudioSessionId(int audioSession)393 void AudioSystem::acquireAudioSessionId(int audioSession) {
394     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
395     if (af != 0) {
396         af->acquireAudioSessionId(audioSession);
397     }
398 }
399 
releaseAudioSessionId(int audioSession)400 void AudioSystem::releaseAudioSessionId(int audioSession) {
401     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
402     if (af != 0) {
403         af->releaseAudioSessionId(audioSession);
404     }
405 }
406 
407 // ---------------------------------------------------------------------------
408 
binderDied(const wp<IBinder> & who)409 void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
410     Mutex::Autolock _l(AudioSystem::gLock);
411 
412     AudioSystem::gAudioFlinger.clear();
413     // clear output handles and stream to output map caches
414     AudioSystem::gOutputs.clear();
415 
416     if (gAudioErrorCallback) {
417         gAudioErrorCallback(DEAD_OBJECT);
418     }
419     ALOGW("AudioFlinger server died!");
420 }
421 
ioConfigChanged(int event,audio_io_handle_t ioHandle,const void * param2)422 void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
423         const void *param2) {
424     ALOGV("ioConfigChanged() event %d", event);
425     const OutputDescriptor *desc;
426     audio_stream_type_t stream;
427 
428     if (ioHandle == 0) return;
429 
430     Mutex::Autolock _l(AudioSystem::gLock);
431 
432     switch (event) {
433     case STREAM_CONFIG_CHANGED:
434         break;
435     case OUTPUT_OPENED: {
436         if (gOutputs.indexOfKey(ioHandle) >= 0) {
437             ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
438             break;
439         }
440         if (param2 == NULL) break;
441         desc = (const OutputDescriptor *)param2;
442 
443         OutputDescriptor *outputDesc =  new OutputDescriptor(*desc);
444         gOutputs.add(ioHandle, outputDesc);
445         ALOGV("ioConfigChanged() new output samplingRate %u, format %d channels %#x frameCount %u "
446                 "latency %d",
447                 outputDesc->samplingRate, outputDesc->format, outputDesc->channels,
448                 outputDesc->frameCount, outputDesc->latency);
449         } break;
450     case OUTPUT_CLOSED: {
451         if (gOutputs.indexOfKey(ioHandle) < 0) {
452             ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
453             break;
454         }
455         ALOGV("ioConfigChanged() output %d closed", ioHandle);
456 
457         gOutputs.removeItem(ioHandle);
458         } break;
459 
460     case OUTPUT_CONFIG_CHANGED: {
461         int index = gOutputs.indexOfKey(ioHandle);
462         if (index < 0) {
463             ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
464             break;
465         }
466         if (param2 == NULL) break;
467         desc = (const OutputDescriptor *)param2;
468 
469         ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %d channels %#x "
470                 "frameCount %d latency %d",
471                 ioHandle, desc->samplingRate, desc->format,
472                 desc->channels, desc->frameCount, desc->latency);
473         OutputDescriptor *outputDesc = gOutputs.valueAt(index);
474         delete outputDesc;
475         outputDesc =  new OutputDescriptor(*desc);
476         gOutputs.replaceValueFor(ioHandle, outputDesc);
477     } break;
478     case INPUT_OPENED:
479     case INPUT_CLOSED:
480     case INPUT_CONFIG_CHANGED:
481         break;
482 
483     }
484 }
485 
setErrorCallback(audio_error_callback cb)486 void AudioSystem::setErrorCallback(audio_error_callback cb) {
487     Mutex::Autolock _l(gLock);
488     gAudioErrorCallback = cb;
489 }
490 
routedToA2dpOutput(audio_stream_type_t streamType)491 bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) {
492     switch (streamType) {
493     case AUDIO_STREAM_MUSIC:
494     case AUDIO_STREAM_VOICE_CALL:
495     case AUDIO_STREAM_BLUETOOTH_SCO:
496     case AUDIO_STREAM_SYSTEM:
497         return true;
498     default:
499         return false;
500     }
501 }
502 
503 
504 // client singleton for AudioPolicyService binder interface
505 sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
506 sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
507 
508 
509 // establish binder interface to AudioPolicy service
get_audio_policy_service()510 const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
511 {
512     gLock.lock();
513     if (gAudioPolicyService == 0) {
514         sp<IServiceManager> sm = defaultServiceManager();
515         sp<IBinder> binder;
516         do {
517             binder = sm->getService(String16("media.audio_policy"));
518             if (binder != 0)
519                 break;
520             ALOGW("AudioPolicyService not published, waiting...");
521             usleep(500000); // 0.5 s
522         } while (true);
523         if (gAudioPolicyServiceClient == NULL) {
524             gAudioPolicyServiceClient = new AudioPolicyServiceClient();
525         }
526         binder->linkToDeath(gAudioPolicyServiceClient);
527         gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
528         gLock.unlock();
529     } else {
530         gLock.unlock();
531     }
532     return gAudioPolicyService;
533 }
534 
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address)535 status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
536                                                audio_policy_dev_state_t state,
537                                                const char *device_address)
538 {
539     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
540     const char *address = "";
541 
542     if (aps == 0) return PERMISSION_DENIED;
543 
544     if (device_address != NULL) {
545         address = device_address;
546     }
547 
548     return aps->setDeviceConnectionState(device, state, address);
549 }
550 
getDeviceConnectionState(audio_devices_t device,const char * device_address)551 audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
552                                                   const char *device_address)
553 {
554     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
555     if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
556 
557     return aps->getDeviceConnectionState(device, device_address);
558 }
559 
setPhoneState(audio_mode_t state)560 status_t AudioSystem::setPhoneState(audio_mode_t state)
561 {
562     if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
563     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
564     if (aps == 0) return PERMISSION_DENIED;
565 
566     return aps->setPhoneState(state);
567 }
568 
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)569 status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
570 {
571     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
572     if (aps == 0) return PERMISSION_DENIED;
573     return aps->setForceUse(usage, config);
574 }
575 
getForceUse(audio_policy_force_use_t usage)576 audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
577 {
578     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
579     if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
580     return aps->getForceUse(usage);
581 }
582 
583 
getOutput(audio_stream_type_t stream,uint32_t samplingRate,audio_format_t format,audio_channel_mask_t channelMask,audio_output_flags_t flags)584 audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
585                                     uint32_t samplingRate,
586                                     audio_format_t format,
587                                     audio_channel_mask_t channelMask,
588                                     audio_output_flags_t flags)
589 {
590     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
591     if (aps == 0) return 0;
592     return aps->getOutput(stream, samplingRate, format, channelMask, flags);
593 }
594 
startOutput(audio_io_handle_t output,audio_stream_type_t stream,int session)595 status_t AudioSystem::startOutput(audio_io_handle_t output,
596                                   audio_stream_type_t stream,
597                                   int session)
598 {
599     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
600     if (aps == 0) return PERMISSION_DENIED;
601     return aps->startOutput(output, stream, session);
602 }
603 
stopOutput(audio_io_handle_t output,audio_stream_type_t stream,int session)604 status_t AudioSystem::stopOutput(audio_io_handle_t output,
605                                  audio_stream_type_t stream,
606                                  int session)
607 {
608     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
609     if (aps == 0) return PERMISSION_DENIED;
610     return aps->stopOutput(output, stream, session);
611 }
612 
releaseOutput(audio_io_handle_t output)613 void AudioSystem::releaseOutput(audio_io_handle_t output)
614 {
615     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
616     if (aps == 0) return;
617     aps->releaseOutput(output);
618 }
619 
getInput(audio_source_t inputSource,uint32_t samplingRate,audio_format_t format,audio_channel_mask_t channelMask,int sessionId)620 audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
621                                     uint32_t samplingRate,
622                                     audio_format_t format,
623                                     audio_channel_mask_t channelMask,
624                                     int sessionId)
625 {
626     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
627     if (aps == 0) return 0;
628     return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId);
629 }
630 
startInput(audio_io_handle_t input)631 status_t AudioSystem::startInput(audio_io_handle_t input)
632 {
633     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
634     if (aps == 0) return PERMISSION_DENIED;
635     return aps->startInput(input);
636 }
637 
stopInput(audio_io_handle_t input)638 status_t AudioSystem::stopInput(audio_io_handle_t input)
639 {
640     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
641     if (aps == 0) return PERMISSION_DENIED;
642     return aps->stopInput(input);
643 }
644 
releaseInput(audio_io_handle_t input)645 void AudioSystem::releaseInput(audio_io_handle_t input)
646 {
647     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
648     if (aps == 0) return;
649     aps->releaseInput(input);
650 }
651 
initStreamVolume(audio_stream_type_t stream,int indexMin,int indexMax)652 status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
653                                     int indexMin,
654                                     int indexMax)
655 {
656     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
657     if (aps == 0) return PERMISSION_DENIED;
658     return aps->initStreamVolume(stream, indexMin, indexMax);
659 }
660 
setStreamVolumeIndex(audio_stream_type_t stream,int index,audio_devices_t device)661 status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
662                                            int index,
663                                            audio_devices_t device)
664 {
665     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
666     if (aps == 0) return PERMISSION_DENIED;
667     return aps->setStreamVolumeIndex(stream, index, device);
668 }
669 
getStreamVolumeIndex(audio_stream_type_t stream,int * index,audio_devices_t device)670 status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
671                                            int *index,
672                                            audio_devices_t device)
673 {
674     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
675     if (aps == 0) return PERMISSION_DENIED;
676     return aps->getStreamVolumeIndex(stream, index, device);
677 }
678 
getStrategyForStream(audio_stream_type_t stream)679 uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
680 {
681     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
682     if (aps == 0) return 0;
683     return aps->getStrategyForStream(stream);
684 }
685 
getDevicesForStream(audio_stream_type_t stream)686 audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
687 {
688     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
689     if (aps == 0) return (audio_devices_t)0;
690     return aps->getDevicesForStream(stream);
691 }
692 
getOutputForEffect(const effect_descriptor_t * desc)693 audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
694 {
695     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
696     if (aps == 0) return PERMISSION_DENIED;
697     return aps->getOutputForEffect(desc);
698 }
699 
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,uint32_t strategy,int session,int id)700 status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
701                                 audio_io_handle_t io,
702                                 uint32_t strategy,
703                                 int session,
704                                 int id)
705 {
706     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
707     if (aps == 0) return PERMISSION_DENIED;
708     return aps->registerEffect(desc, io, strategy, session, id);
709 }
710 
unregisterEffect(int id)711 status_t AudioSystem::unregisterEffect(int id)
712 {
713     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
714     if (aps == 0) return PERMISSION_DENIED;
715     return aps->unregisterEffect(id);
716 }
717 
setEffectEnabled(int id,bool enabled)718 status_t AudioSystem::setEffectEnabled(int id, bool enabled)
719 {
720     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
721     if (aps == 0) return PERMISSION_DENIED;
722     return aps->setEffectEnabled(id, enabled);
723 }
724 
isStreamActive(audio_stream_type_t stream,bool * state,uint32_t inPastMs)725 status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
726 {
727     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
728     if (aps == 0) return PERMISSION_DENIED;
729     if (state == NULL) return BAD_VALUE;
730     *state = aps->isStreamActive(stream, inPastMs);
731     return NO_ERROR;
732 }
733 
isStreamActiveRemotely(audio_stream_type_t stream,bool * state,uint32_t inPastMs)734 status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
735         uint32_t inPastMs)
736 {
737     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
738     if (aps == 0) return PERMISSION_DENIED;
739     if (state == NULL) return BAD_VALUE;
740     *state = aps->isStreamActiveRemotely(stream, inPastMs);
741     return NO_ERROR;
742 }
743 
isSourceActive(audio_source_t stream,bool * state)744 status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
745 {
746     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
747     if (aps == 0) return PERMISSION_DENIED;
748     if (state == NULL) return BAD_VALUE;
749     *state = aps->isSourceActive(stream);
750     return NO_ERROR;
751 }
752 
getPrimaryOutputSamplingRate()753 uint32_t AudioSystem::getPrimaryOutputSamplingRate()
754 {
755     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
756     if (af == 0) return 0;
757     return af->getPrimaryOutputSamplingRate();
758 }
759 
getPrimaryOutputFrameCount()760 size_t AudioSystem::getPrimaryOutputFrameCount()
761 {
762     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
763     if (af == 0) return 0;
764     return af->getPrimaryOutputFrameCount();
765 }
766 
clearAudioConfigCache()767 void AudioSystem::clearAudioConfigCache()
768 {
769     Mutex::Autolock _l(gLock);
770     ALOGV("clearAudioConfigCache()");
771     gOutputs.clear();
772 }
773 
774 // ---------------------------------------------------------------------------
775 
binderDied(const wp<IBinder> & who)776 void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
777     Mutex::Autolock _l(AudioSystem::gLock);
778     AudioSystem::gAudioPolicyService.clear();
779 
780     ALOGW("AudioPolicyService server died!");
781 }
782 
783 }; // namespace android
784