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