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