AudioSystem.cpp revision 5f972c031d4061f4f037c9fda1ea4bd9b6a756cd
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;
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    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        if (inBuffSize == 0) {
373            ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %x channelMask %x",
374                    sampleRate, format, channelMask);
375            return BAD_VALUE;
376        }
377        // A benign race is possible here: we could overwrite a fresher cache entry
378        gLock.lock();
379        // save the request params
380        gPrevInSamplingRate = sampleRate;
381        gPrevInFormat = format;
382        gPrevInChannelMask = channelMask;
383
384        gInBuffSize = inBuffSize;
385    }
386    gLock.unlock();
387    *buffSize = inBuffSize;
388
389    return NO_ERROR;
390}
391
392status_t AudioSystem::setVoiceVolume(float value)
393{
394    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
395    if (af == 0) return PERMISSION_DENIED;
396    return af->setVoiceVolume(value);
397}
398
399status_t AudioSystem::getRenderPosition(audio_io_handle_t output, size_t *halFrames,
400                                        size_t *dspFrames, audio_stream_type_t stream)
401{
402    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
403    if (af == 0) return PERMISSION_DENIED;
404
405    if (stream == AUDIO_STREAM_DEFAULT) {
406        stream = AUDIO_STREAM_MUSIC;
407    }
408
409    if (output == 0) {
410        output = getOutput(stream);
411    }
412
413    return af->getRenderPosition(halFrames, dspFrames, output);
414}
415
416uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
417    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
418    uint32_t result = 0;
419    if (af == 0) return result;
420    if (ioHandle == 0) return result;
421
422    result = af->getInputFramesLost(ioHandle);
423    return result;
424}
425
426int AudioSystem::newAudioSessionId() {
427    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
428    if (af == 0) return 0;
429    return af->newAudioSessionId();
430}
431
432void AudioSystem::acquireAudioSessionId(int audioSession) {
433    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
434    if (af != 0) {
435        af->acquireAudioSessionId(audioSession);
436    }
437}
438
439void AudioSystem::releaseAudioSessionId(int audioSession) {
440    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
441    if (af != 0) {
442        af->releaseAudioSessionId(audioSession);
443    }
444}
445
446// ---------------------------------------------------------------------------
447
448void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) {
449    Mutex::Autolock _l(AudioSystem::gLock);
450
451    AudioSystem::gAudioFlinger.clear();
452    // clear output handles and stream to output map caches
453    AudioSystem::gOutputs.clear();
454
455    if (gAudioErrorCallback) {
456        gAudioErrorCallback(DEAD_OBJECT);
457    }
458    ALOGW("AudioFlinger server died!");
459}
460
461void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
462        const void *param2) {
463    ALOGV("ioConfigChanged() event %d", event);
464    const OutputDescriptor *desc;
465    audio_stream_type_t stream;
466
467    if (ioHandle == 0) return;
468
469    Mutex::Autolock _l(AudioSystem::gLock);
470
471    switch (event) {
472    case STREAM_CONFIG_CHANGED:
473        break;
474    case OUTPUT_OPENED: {
475        if (gOutputs.indexOfKey(ioHandle) >= 0) {
476            ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
477            break;
478        }
479        if (param2 == NULL) break;
480        desc = (const OutputDescriptor *)param2;
481
482        OutputDescriptor *outputDesc =  new OutputDescriptor(*desc);
483        gOutputs.add(ioHandle, outputDesc);
484        ALOGV("ioConfigChanged() new output samplingRate %u, format %d channel mask %#x frameCount %u "
485                "latency %d",
486                outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
487                outputDesc->frameCount, outputDesc->latency);
488        } break;
489    case OUTPUT_CLOSED: {
490        if (gOutputs.indexOfKey(ioHandle) < 0) {
491            ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
492            break;
493        }
494        ALOGV("ioConfigChanged() output %d closed", ioHandle);
495
496        gOutputs.removeItem(ioHandle);
497        } break;
498
499    case OUTPUT_CONFIG_CHANGED: {
500        int index = gOutputs.indexOfKey(ioHandle);
501        if (index < 0) {
502            ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
503            break;
504        }
505        if (param2 == NULL) break;
506        desc = (const OutputDescriptor *)param2;
507
508        ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %d channel mask %#x "
509                "frameCount %d latency %d",
510                ioHandle, desc->samplingRate, desc->format,
511                desc->channelMask, desc->frameCount, desc->latency);
512        OutputDescriptor *outputDesc = gOutputs.valueAt(index);
513        delete outputDesc;
514        outputDesc =  new OutputDescriptor(*desc);
515        gOutputs.replaceValueFor(ioHandle, outputDesc);
516    } break;
517    case INPUT_OPENED:
518    case INPUT_CLOSED:
519    case INPUT_CONFIG_CHANGED:
520        break;
521
522    }
523}
524
525void AudioSystem::setErrorCallback(audio_error_callback cb) {
526    Mutex::Autolock _l(gLock);
527    gAudioErrorCallback = cb;
528}
529
530bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) {
531    switch (streamType) {
532    case AUDIO_STREAM_MUSIC:
533    case AUDIO_STREAM_VOICE_CALL:
534    case AUDIO_STREAM_BLUETOOTH_SCO:
535    case AUDIO_STREAM_SYSTEM:
536        return true;
537    default:
538        return false;
539    }
540}
541
542
543// client singleton for AudioPolicyService binder interface
544sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
545sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
546
547
548// establish binder interface to AudioPolicy service
549const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
550{
551    gLock.lock();
552    if (gAudioPolicyService == 0) {
553        sp<IServiceManager> sm = defaultServiceManager();
554        sp<IBinder> binder;
555        do {
556            binder = sm->getService(String16("media.audio_policy"));
557            if (binder != 0)
558                break;
559            ALOGW("AudioPolicyService not published, waiting...");
560            usleep(500000); // 0.5 s
561        } while (true);
562        if (gAudioPolicyServiceClient == NULL) {
563            gAudioPolicyServiceClient = new AudioPolicyServiceClient();
564        }
565        binder->linkToDeath(gAudioPolicyServiceClient);
566        gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
567        gLock.unlock();
568    } else {
569        gLock.unlock();
570    }
571    return gAudioPolicyService;
572}
573
574// ---------------------------------------------------------------------------
575
576status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
577                                               audio_policy_dev_state_t state,
578                                               const char *device_address)
579{
580    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
581    const char *address = "";
582
583    if (aps == 0) return PERMISSION_DENIED;
584
585    if (device_address != NULL) {
586        address = device_address;
587    }
588
589    return aps->setDeviceConnectionState(device, state, address);
590}
591
592audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
593                                                  const char *device_address)
594{
595    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
596    if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
597
598    return aps->getDeviceConnectionState(device, device_address);
599}
600
601status_t AudioSystem::setPhoneState(audio_mode_t state)
602{
603    if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
604    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
605    if (aps == 0) return PERMISSION_DENIED;
606
607    return aps->setPhoneState(state);
608}
609
610status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
611{
612    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
613    if (aps == 0) return PERMISSION_DENIED;
614    return aps->setForceUse(usage, config);
615}
616
617audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
618{
619    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
620    if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
621    return aps->getForceUse(usage);
622}
623
624
625audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
626                                    uint32_t samplingRate,
627                                    audio_format_t format,
628                                    audio_channel_mask_t channelMask,
629                                    audio_output_flags_t flags,
630                                    const audio_offload_info_t *offloadInfo)
631{
632    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
633    if (aps == 0) return 0;
634    return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
635}
636
637status_t AudioSystem::startOutput(audio_io_handle_t output,
638                                  audio_stream_type_t stream,
639                                  int session)
640{
641    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
642    if (aps == 0) return PERMISSION_DENIED;
643    return aps->startOutput(output, stream, session);
644}
645
646status_t AudioSystem::stopOutput(audio_io_handle_t output,
647                                 audio_stream_type_t stream,
648                                 int session)
649{
650    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
651    if (aps == 0) return PERMISSION_DENIED;
652    return aps->stopOutput(output, stream, session);
653}
654
655void AudioSystem::releaseOutput(audio_io_handle_t output)
656{
657    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
658    if (aps == 0) return;
659    aps->releaseOutput(output);
660}
661
662audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
663                                    uint32_t samplingRate,
664                                    audio_format_t format,
665                                    audio_channel_mask_t channelMask,
666                                    int sessionId)
667{
668    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
669    if (aps == 0) return 0;
670    return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId);
671}
672
673status_t AudioSystem::startInput(audio_io_handle_t input)
674{
675    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
676    if (aps == 0) return PERMISSION_DENIED;
677    return aps->startInput(input);
678}
679
680status_t AudioSystem::stopInput(audio_io_handle_t input)
681{
682    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
683    if (aps == 0) return PERMISSION_DENIED;
684    return aps->stopInput(input);
685}
686
687void AudioSystem::releaseInput(audio_io_handle_t input)
688{
689    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
690    if (aps == 0) return;
691    aps->releaseInput(input);
692}
693
694status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
695                                    int indexMin,
696                                    int indexMax)
697{
698    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
699    if (aps == 0) return PERMISSION_DENIED;
700    return aps->initStreamVolume(stream, indexMin, indexMax);
701}
702
703status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
704                                           int index,
705                                           audio_devices_t device)
706{
707    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
708    if (aps == 0) return PERMISSION_DENIED;
709    return aps->setStreamVolumeIndex(stream, index, device);
710}
711
712status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
713                                           int *index,
714                                           audio_devices_t device)
715{
716    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
717    if (aps == 0) return PERMISSION_DENIED;
718    return aps->getStreamVolumeIndex(stream, index, device);
719}
720
721uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
722{
723    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
724    if (aps == 0) return 0;
725    return aps->getStrategyForStream(stream);
726}
727
728audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
729{
730    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
731    if (aps == 0) return (audio_devices_t)0;
732    return aps->getDevicesForStream(stream);
733}
734
735audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
736{
737    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
738    if (aps == 0) return PERMISSION_DENIED;
739    return aps->getOutputForEffect(desc);
740}
741
742status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
743                                audio_io_handle_t io,
744                                uint32_t strategy,
745                                int session,
746                                int id)
747{
748    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
749    if (aps == 0) return PERMISSION_DENIED;
750    return aps->registerEffect(desc, io, strategy, session, id);
751}
752
753status_t AudioSystem::unregisterEffect(int id)
754{
755    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
756    if (aps == 0) return PERMISSION_DENIED;
757    return aps->unregisterEffect(id);
758}
759
760status_t AudioSystem::setEffectEnabled(int id, bool enabled)
761{
762    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
763    if (aps == 0) return PERMISSION_DENIED;
764    return aps->setEffectEnabled(id, enabled);
765}
766
767status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
768{
769    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
770    if (aps == 0) return PERMISSION_DENIED;
771    if (state == NULL) return BAD_VALUE;
772    *state = aps->isStreamActive(stream, inPastMs);
773    return NO_ERROR;
774}
775
776status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
777        uint32_t inPastMs)
778{
779    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
780    if (aps == 0) return PERMISSION_DENIED;
781    if (state == NULL) return BAD_VALUE;
782    *state = aps->isStreamActiveRemotely(stream, inPastMs);
783    return NO_ERROR;
784}
785
786status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
787{
788    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
789    if (aps == 0) return PERMISSION_DENIED;
790    if (state == NULL) return BAD_VALUE;
791    *state = aps->isSourceActive(stream);
792    return NO_ERROR;
793}
794
795uint32_t AudioSystem::getPrimaryOutputSamplingRate()
796{
797    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
798    if (af == 0) return 0;
799    return af->getPrimaryOutputSamplingRate();
800}
801
802size_t AudioSystem::getPrimaryOutputFrameCount()
803{
804    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
805    if (af == 0) return 0;
806    return af->getPrimaryOutputFrameCount();
807}
808
809status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
810{
811    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
812    if (af == 0) return PERMISSION_DENIED;
813    return af->setLowRamDevice(isLowRamDevice);
814}
815
816void AudioSystem::clearAudioConfigCache()
817{
818    Mutex::Autolock _l(gLock);
819    ALOGV("clearAudioConfigCache()");
820    gOutputs.clear();
821}
822
823bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
824{
825    ALOGV("isOffloadSupported()");
826    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
827    if (aps == 0) return false;
828    return aps->isOffloadSupported(info);
829}
830
831// ---------------------------------------------------------------------------
832
833void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) {
834    Mutex::Autolock _l(AudioSystem::gLock);
835    AudioSystem::gAudioPolicyService.clear();
836
837    ALOGW("AudioPolicyService server died!");
838}
839
840}; // namespace android
841