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