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