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