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