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