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