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