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