AudioSystem.cpp revision a19ffb656616feec70613ba67ddfe15a504a4e76
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/IAudioPolicyService.h>
24#include <math.h>
25
26#include <system/audio.h>
27
28// ----------------------------------------------------------------------------
29
30namespace android {
31
32// client singleton for AudioFlinger binder interface
33Mutex AudioSystem::gLock;
34sp<IAudioFlinger> AudioSystem::gAudioFlinger;
35sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
36audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
37// Cached values
38
39DefaultKeyedVector<audio_stream_type_t, audio_io_handle_t> AudioSystem::gStreamOutputMap(0);
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;
45int AudioSystem::gPrevInChannelCount = 1;
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
209// DEPRECATED
210status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType) {
211    return getOutputSamplingRate(samplingRate, (audio_stream_type_t)streamType);
212}
213
214status_t AudioSystem::getOutputSamplingRate(int* samplingRate, audio_stream_type_t streamType)
215{
216    OutputDescriptor *outputDesc;
217    audio_io_handle_t output;
218
219    if (streamType == AUDIO_STREAM_DEFAULT) {
220        streamType = AUDIO_STREAM_MUSIC;
221    }
222
223    output = getOutput(streamType);
224    if (output == 0) {
225        return PERMISSION_DENIED;
226    }
227
228    gLock.lock();
229    outputDesc = AudioSystem::gOutputs.valueFor(output);
230    if (outputDesc == NULL) {
231        ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
232        gLock.unlock();
233        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
234        if (af == 0) return PERMISSION_DENIED;
235        *samplingRate = af->sampleRate(output);
236    } else {
237        ALOGV("getOutputSamplingRate() reading from output desc");
238        *samplingRate = outputDesc->samplingRate;
239        gLock.unlock();
240    }
241
242    ALOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
243
244    return NO_ERROR;
245}
246
247// DEPRECATED
248status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType) {
249    return getOutputFrameCount(frameCount, (audio_stream_type_t)streamType);
250}
251
252status_t AudioSystem::getOutputFrameCount(int* frameCount, audio_stream_type_t streamType)
253{
254    OutputDescriptor *outputDesc;
255    audio_io_handle_t output;
256
257    if (streamType == AUDIO_STREAM_DEFAULT) {
258        streamType = AUDIO_STREAM_MUSIC;
259    }
260
261    output = getOutput(streamType);
262    if (output == 0) {
263        return PERMISSION_DENIED;
264    }
265
266    gLock.lock();
267    outputDesc = AudioSystem::gOutputs.valueFor(output);
268    if (outputDesc == NULL) {
269        gLock.unlock();
270        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
271        if (af == 0) return PERMISSION_DENIED;
272        *frameCount = af->frameCount(output);
273    } else {
274        *frameCount = outputDesc->frameCount;
275        gLock.unlock();
276    }
277
278    ALOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
279
280    return NO_ERROR;
281}
282
283status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
284{
285    OutputDescriptor *outputDesc;
286    audio_io_handle_t output;
287
288    if (streamType == AUDIO_STREAM_DEFAULT) {
289        streamType = AUDIO_STREAM_MUSIC;
290    }
291
292    output = getOutput(streamType);
293    if (output == 0) {
294        return PERMISSION_DENIED;
295    }
296
297    gLock.lock();
298    outputDesc = AudioSystem::gOutputs.valueFor(output);
299    if (outputDesc == NULL) {
300        gLock.unlock();
301        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
302        if (af == 0) return PERMISSION_DENIED;
303        *latency = af->latency(output);
304    } else {
305        *latency = outputDesc->latency;
306        gLock.unlock();
307    }
308
309    ALOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
310
311    return NO_ERROR;
312}
313
314status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount,
315    size_t* buffSize)
316{
317    gLock.lock();
318    // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
319    size_t inBuffSize = gInBuffSize;
320    if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
321        || (channelCount != gPrevInChannelCount)) {
322        gLock.unlock();
323        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
324        if (af == 0) {
325            return PERMISSION_DENIED;
326        }
327        inBuffSize = af->getInputBufferSize(sampleRate, format, channelCount);
328        gLock.lock();
329        // save the request params
330        gPrevInSamplingRate = sampleRate;
331        gPrevInFormat = format;
332        gPrevInChannelCount = channelCount;
333
334        gInBuffSize = inBuffSize;
335    }
336    gLock.unlock();
337    *buffSize = inBuffSize;
338
339    return NO_ERROR;
340}
341
342status_t AudioSystem::setVoiceVolume(float value)
343{
344    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
345    if (af == 0) return PERMISSION_DENIED;
346    return af->setVoiceVolume(value);
347}
348
349status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, audio_stream_type_t stream)
350{
351    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
352    if (af == 0) return PERMISSION_DENIED;
353
354    if (stream == AUDIO_STREAM_DEFAULT) {
355        stream = AUDIO_STREAM_MUSIC;
356    }
357
358    return af->getRenderPosition(halFrames, dspFrames, getOutput(stream));
359}
360
361unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
362    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
363    unsigned int result = 0;
364    if (af == 0) return result;
365    if (ioHandle == 0) return result;
366
367    result = af->getInputFramesLost(ioHandle);
368    return result;
369}
370
371int AudioSystem::newAudioSessionId() {
372    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
373    if (af == 0) return 0;
374    return af->newAudioSessionId();
375}
376
377void AudioSystem::acquireAudioSessionId(int audioSession) {
378    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379    if (af != 0) {
380        af->acquireAudioSessionId(audioSession);
381    }
382}
383
384void AudioSystem::releaseAudioSessionId(int audioSession) {
385    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
386    if (af != 0) {
387        af->releaseAudioSessionId(audioSession);
388    }
389}
390
391// ---------------------------------------------------------------------------
392
393void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
394    Mutex::Autolock _l(AudioSystem::gLock);
395
396    AudioSystem::gAudioFlinger.clear();
397    // clear output handles and stream to output map caches
398    AudioSystem::gStreamOutputMap.clear();
399    AudioSystem::gOutputs.clear();
400
401    if (gAudioErrorCallback) {
402        gAudioErrorCallback(DEAD_OBJECT);
403    }
404    ALOGW("AudioFlinger server died!");
405}
406
407void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
408        const void *param2) {
409    ALOGV("ioConfigChanged() event %d", event);
410    const OutputDescriptor *desc;
411    audio_stream_type_t stream;
412
413    if (ioHandle == 0) return;
414
415    Mutex::Autolock _l(AudioSystem::gLock);
416
417    switch (event) {
418    case STREAM_CONFIG_CHANGED:
419        if (param2 == NULL) break;
420        stream = *(const audio_stream_type_t *)param2;
421        ALOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle);
422        if (gStreamOutputMap.indexOfKey(stream) >= 0) {
423            gStreamOutputMap.replaceValueFor(stream, ioHandle);
424        }
425        break;
426    case OUTPUT_OPENED: {
427        if (gOutputs.indexOfKey(ioHandle) >= 0) {
428            ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
429            break;
430        }
431        if (param2 == NULL) break;
432        desc = (const OutputDescriptor *)param2;
433
434        OutputDescriptor *outputDesc =  new OutputDescriptor(*desc);
435        gOutputs.add(ioHandle, outputDesc);
436        ALOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d",
437                outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
438        } break;
439    case OUTPUT_CLOSED: {
440        if (gOutputs.indexOfKey(ioHandle) < 0) {
441            ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
442            break;
443        }
444        ALOGV("ioConfigChanged() output %d closed", ioHandle);
445
446        gOutputs.removeItem(ioHandle);
447        for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) {
448            if (gStreamOutputMap.valueAt(i) == ioHandle) {
449                gStreamOutputMap.removeItemsAt(i);
450            }
451        }
452        } break;
453
454    case OUTPUT_CONFIG_CHANGED: {
455        int index = gOutputs.indexOfKey(ioHandle);
456        if (index < 0) {
457            ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
458            break;
459        }
460        if (param2 == NULL) break;
461        desc = (const OutputDescriptor *)param2;
462
463        ALOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d",
464                ioHandle, desc->samplingRate, desc->format,
465                desc->channels, desc->frameCount, desc->latency);
466        OutputDescriptor *outputDesc = gOutputs.valueAt(index);
467        delete outputDesc;
468        outputDesc =  new OutputDescriptor(*desc);
469        gOutputs.replaceValueFor(ioHandle, outputDesc);
470    } break;
471    case INPUT_OPENED:
472    case INPUT_CLOSED:
473    case INPUT_CONFIG_CHANGED:
474        break;
475
476    }
477}
478
479void AudioSystem::setErrorCallback(audio_error_callback cb) {
480    Mutex::Autolock _l(gLock);
481    gAudioErrorCallback = cb;
482}
483
484bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) {
485    switch(streamType) {
486    case AUDIO_STREAM_MUSIC:
487    case AUDIO_STREAM_VOICE_CALL:
488    case AUDIO_STREAM_BLUETOOTH_SCO:
489    case AUDIO_STREAM_SYSTEM:
490        return true;
491    default:
492        return false;
493    }
494}
495
496
497// client singleton for AudioPolicyService binder interface
498sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
499sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
500
501
502// establish binder interface to AudioFlinger service
503const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
504{
505    gLock.lock();
506    if (gAudioPolicyService == 0) {
507        sp<IServiceManager> sm = defaultServiceManager();
508        sp<IBinder> binder;
509        do {
510            binder = sm->getService(String16("media.audio_policy"));
511            if (binder != 0)
512                break;
513            ALOGW("AudioPolicyService not published, waiting...");
514            usleep(500000); // 0.5 s
515        } while(true);
516        if (gAudioPolicyServiceClient == NULL) {
517            gAudioPolicyServiceClient = new AudioPolicyServiceClient();
518        }
519        binder->linkToDeath(gAudioPolicyServiceClient);
520        gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
521        gLock.unlock();
522    } else {
523        gLock.unlock();
524    }
525    return gAudioPolicyService;
526}
527
528status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
529                                               audio_policy_dev_state_t state,
530                                               const char *device_address)
531{
532    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
533    const char *address = "";
534
535    if (aps == 0) return PERMISSION_DENIED;
536
537    if (device_address != NULL) {
538        address = device_address;
539    }
540
541    return aps->setDeviceConnectionState(device, state, address);
542}
543
544audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
545                                                  const char *device_address)
546{
547    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
548    if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
549
550    return aps->getDeviceConnectionState(device, device_address);
551}
552
553status_t AudioSystem::setPhoneState(audio_mode_t state)
554{
555    if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
556    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
557    if (aps == 0) return PERMISSION_DENIED;
558
559    return aps->setPhoneState(state);
560}
561
562status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
563{
564    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
565    if (aps == 0) return PERMISSION_DENIED;
566    return aps->setForceUse(usage, config);
567}
568
569audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
570{
571    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
572    if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
573    return aps->getForceUse(usage);
574}
575
576
577audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
578                                    uint32_t samplingRate,
579                                    audio_format_t format,
580                                    uint32_t channels,
581                                    audio_policy_output_flags_t flags)
582{
583    audio_io_handle_t output = 0;
584    // Do not use stream to output map cache if the direct output
585    // flag is set or if we are likely to use a direct output
586    // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to
587    // a direct output on some platforms).
588    // TODO: the output cache and stream to output mapping implementation needs to
589    // be reworked for proper operation with direct outputs. This code is too specific
590    // to the first use case we want to cover (Voice Recognition and Voice Dialer over
591    // Bluetooth SCO
592    if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0 &&
593        ((stream != AUDIO_STREAM_VOICE_CALL && stream != AUDIO_STREAM_BLUETOOTH_SCO) ||
594         channels != AUDIO_CHANNEL_OUT_MONO ||
595         (samplingRate != 8000 && samplingRate != 16000))) {
596        Mutex::Autolock _l(gLock);
597        output = AudioSystem::gStreamOutputMap.valueFor(stream);
598        ALOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);
599    }
600    if (output == 0) {
601        const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
602        if (aps == 0) return 0;
603        output = aps->getOutput(stream, samplingRate, format, channels, flags);
604        if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0) {
605            Mutex::Autolock _l(gLock);
606            AudioSystem::gStreamOutputMap.add(stream, output);
607        }
608    }
609    return output;
610}
611
612status_t AudioSystem::startOutput(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->startOutput(output, stream, session);
619}
620
621status_t AudioSystem::stopOutput(audio_io_handle_t output,
622                                 audio_stream_type_t stream,
623                                 int session)
624{
625    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
626    if (aps == 0) return PERMISSION_DENIED;
627    return aps->stopOutput(output, stream, session);
628}
629
630void AudioSystem::releaseOutput(audio_io_handle_t output)
631{
632    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
633    if (aps == 0) return;
634    aps->releaseOutput(output);
635}
636
637audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
638                                    uint32_t samplingRate,
639                                    audio_format_t format,
640                                    uint32_t channels,
641                                    audio_in_acoustics_t acoustics,
642                                    int sessionId)
643{
644    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
645    if (aps == 0) return 0;
646    return aps->getInput(inputSource, samplingRate, format, channels, acoustics, sessionId);
647}
648
649status_t AudioSystem::startInput(audio_io_handle_t input)
650{
651    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
652    if (aps == 0) return PERMISSION_DENIED;
653    return aps->startInput(input);
654}
655
656status_t AudioSystem::stopInput(audio_io_handle_t input)
657{
658    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
659    if (aps == 0) return PERMISSION_DENIED;
660    return aps->stopInput(input);
661}
662
663void AudioSystem::releaseInput(audio_io_handle_t input)
664{
665    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
666    if (aps == 0) return;
667    aps->releaseInput(input);
668}
669
670status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
671                                    int indexMin,
672                                    int indexMax)
673{
674    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
675    if (aps == 0) return PERMISSION_DENIED;
676    return aps->initStreamVolume(stream, indexMin, indexMax);
677}
678
679status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
680                                           int index,
681                                           audio_devices_t device)
682{
683    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
684    if (aps == 0) return PERMISSION_DENIED;
685    return aps->setStreamVolumeIndex(stream, index, device);
686}
687
688status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
689                                           int *index,
690                                           audio_devices_t device)
691{
692    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
693    if (aps == 0) return PERMISSION_DENIED;
694    return aps->getStreamVolumeIndex(stream, index, device);
695}
696
697uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
698{
699    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
700    if (aps == 0) return 0;
701    return aps->getStrategyForStream(stream);
702}
703
704audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
705{
706    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
707    if (aps == 0) return (audio_devices_t)0;
708    return aps->getDevicesForStream(stream);
709}
710
711audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc)
712{
713    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
714    if (aps == 0) return PERMISSION_DENIED;
715    return aps->getOutputForEffect(desc);
716}
717
718status_t AudioSystem::registerEffect(effect_descriptor_t *desc,
719                                audio_io_handle_t io,
720                                uint32_t strategy,
721                                int session,
722                                int id)
723{
724    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
725    if (aps == 0) return PERMISSION_DENIED;
726    return aps->registerEffect(desc, io, strategy, session, id);
727}
728
729status_t AudioSystem::unregisterEffect(int id)
730{
731    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
732    if (aps == 0) return PERMISSION_DENIED;
733    return aps->unregisterEffect(id);
734}
735
736status_t AudioSystem::setEffectEnabled(int id, bool enabled)
737{
738    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
739    if (aps == 0) return PERMISSION_DENIED;
740    return aps->setEffectEnabled(id, enabled);
741}
742
743status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, 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->isStreamActive(stream, inPastMs);
749    return NO_ERROR;
750}
751
752
753void AudioSystem::clearAudioConfigCache()
754{
755    Mutex::Autolock _l(gLock);
756    ALOGV("clearAudioConfigCache()");
757    gStreamOutputMap.clear();
758    gOutputs.clear();
759}
760
761// ---------------------------------------------------------------------------
762
763void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
764    Mutex::Autolock _l(AudioSystem::gLock);
765    AudioSystem::gAudioPolicyService.clear();
766
767    ALOGW("AudioPolicyService server died!");
768}
769
770}; // namespace android
771
772