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