AudioSystem.cpp revision c813985abd8ba61e999b3505f6a332574f87a1be
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.get() == 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.get() == 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    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
552    if (aps == 0) return PERMISSION_DENIED;
553
554    return aps->setPhoneState(state);
555}
556
557status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask)
558{
559    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
560    if (aps == 0) return PERMISSION_DENIED;
561    return aps->setRingerMode(mode, mask);
562}
563
564status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
565{
566    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
567    if (aps == 0) return PERMISSION_DENIED;
568    return aps->setForceUse(usage, config);
569}
570
571audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
572{
573    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
574    if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
575    return aps->getForceUse(usage);
576}
577
578
579audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
580                                    uint32_t samplingRate,
581                                    uint32_t format,
582                                    uint32_t channels,
583                                    audio_policy_output_flags_t flags)
584{
585    audio_io_handle_t output = 0;
586    // Do not use stream to output map cache if the direct output
587    // flag is set or if we are likely to use a direct output
588    // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to
589    // a direct output on some platforms).
590    // TODO: the output cache and stream to output mapping implementation needs to
591    // be reworked for proper operation with direct outputs. This code is too specific
592    // to the first use case we want to cover (Voice Recognition and Voice Dialer over
593    // Bluetooth SCO
594    if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0 &&
595        ((stream != AUDIO_STREAM_VOICE_CALL && stream != AUDIO_STREAM_BLUETOOTH_SCO) ||
596         channels != AUDIO_CHANNEL_OUT_MONO ||
597         (samplingRate != 8000 && samplingRate != 16000))) {
598        Mutex::Autolock _l(gLock);
599        output = AudioSystem::gStreamOutputMap.valueFor(stream);
600        ALOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
601    }
602    if (output == 0) {
603        const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
604        if (aps == 0) return 0;
605        output = aps->getOutput(stream, samplingRate, format, channels, flags);
606        if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0) {
607            Mutex::Autolock _l(gLock);
608            AudioSystem::gStreamOutputMap.add(stream, output);
609        }
610    }
611    return output;
612}
613
614status_t AudioSystem::startOutput(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->startOutput(output, stream, session);
621}
622
623status_t AudioSystem::stopOutput(audio_io_handle_t output,
624                                 audio_stream_type_t stream,
625                                 int session)
626{
627    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
628    if (aps == 0) return PERMISSION_DENIED;
629    return aps->stopOutput(output, stream, session);
630}
631
632void AudioSystem::releaseOutput(audio_io_handle_t output)
633{
634    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
635    if (aps == 0) return;
636    aps->releaseOutput(output);
637}
638
639audio_io_handle_t AudioSystem::getInput(int inputSource,
640                                    uint32_t samplingRate,
641                                    uint32_t format,
642                                    uint32_t channels,
643                                    audio_in_acoustics_t acoustics,
644                                    int sessionId)
645{
646    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
647    if (aps == 0) return 0;
648    return aps->getInput(inputSource, samplingRate, format, channels, acoustics, sessionId);
649}
650
651status_t AudioSystem::startInput(audio_io_handle_t input)
652{
653    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
654    if (aps == 0) return PERMISSION_DENIED;
655    return aps->startInput(input);
656}
657
658status_t AudioSystem::stopInput(audio_io_handle_t input)
659{
660    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
661    if (aps == 0) return PERMISSION_DENIED;
662    return aps->stopInput(input);
663}
664
665void AudioSystem::releaseInput(audio_io_handle_t input)
666{
667    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
668    if (aps == 0) return;
669    aps->releaseInput(input);
670}
671
672status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
673                                    int indexMin,
674                                    int indexMax)
675{
676    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
677    if (aps == 0) return PERMISSION_DENIED;
678    return aps->initStreamVolume(stream, indexMin, indexMax);
679}
680
681status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
682                                           int index,
683                                           audio_devices_t device)
684{
685    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
686    if (aps == 0) return PERMISSION_DENIED;
687    return aps->setStreamVolumeIndex(stream, index, device);
688}
689
690status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
691                                           int *index,
692                                           audio_devices_t device)
693{
694    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
695    if (aps == 0) return PERMISSION_DENIED;
696    return aps->getStreamVolumeIndex(stream, index, device);
697}
698
699uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
700{
701    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
702    if (aps == 0) return 0;
703    return aps->getStrategyForStream(stream);
704}
705
706uint32_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
707{
708    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
709    if (aps == 0) return 0;
710    return aps->getDevicesForStream(stream);
711}
712
713audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc)
714{
715    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
716    if (aps == 0) return PERMISSION_DENIED;
717    return aps->getOutputForEffect(desc);
718}
719
720status_t AudioSystem::registerEffect(effect_descriptor_t *desc,
721                                audio_io_handle_t io,
722                                uint32_t strategy,
723                                int session,
724                                int id)
725{
726    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
727    if (aps == 0) return PERMISSION_DENIED;
728    return aps->registerEffect(desc, io, strategy, session, id);
729}
730
731status_t AudioSystem::unregisterEffect(int id)
732{
733    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
734    if (aps == 0) return PERMISSION_DENIED;
735    return aps->unregisterEffect(id);
736}
737
738status_t AudioSystem::setEffectEnabled(int id, bool enabled)
739{
740    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
741    if (aps == 0) return PERMISSION_DENIED;
742    return aps->setEffectEnabled(id, enabled);
743}
744
745status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
746{
747    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
748    if (aps == 0) return PERMISSION_DENIED;
749    if (state == NULL) return BAD_VALUE;
750    *state = aps->isStreamActive(stream, inPastMs);
751    return NO_ERROR;
752}
753
754
755void AudioSystem::clearAudioConfigCache()
756{
757    Mutex::Autolock _l(gLock);
758    ALOGV("clearAudioConfigCache()");
759    gStreamOutputMap.clear();
760    gOutputs.clear();
761}
762
763// ---------------------------------------------------------------------------
764
765void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
766    Mutex::Autolock _l(AudioSystem::gLock);
767    AudioSystem::gAudioPolicyService.clear();
768
769    ALOGW("AudioPolicyService server died!");
770}
771
772}; // namespace android
773
774