AudioSystem.cpp revision 93c3d41bdb15e39dac0faea9c5b60f1637cd477c
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, samplingRate);
246}
247
248status_t AudioSystem::getOutputSamplingRateForAttr(uint32_t* samplingRate,
249        const audio_attributes_t *attr)
250{
251    if (attr == NULL) {
252        return BAD_VALUE;
253    }
254    audio_io_handle_t output = getOutputForAttr(attr);
255    if (output == 0) {
256        return PERMISSION_DENIED;
257    }
258    return getSamplingRate(output, samplingRate);
259}
260
261status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
262                                      uint32_t* samplingRate)
263{
264    OutputDescriptor *outputDesc;
265
266    gLock.lock();
267    outputDesc = AudioSystem::gOutputs.valueFor(output);
268    if (outputDesc == NULL) {
269        ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
270        gLock.unlock();
271        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
272        if (af == 0) return PERMISSION_DENIED;
273        *samplingRate = af->sampleRate(output);
274    } else {
275        ALOGV("getOutputSamplingRate() reading from output desc");
276        *samplingRate = outputDesc->samplingRate;
277        gLock.unlock();
278    }
279    if (*samplingRate == 0) {
280        ALOGE("AudioSystem::getSamplingRate failed for output %d", output);
281        return BAD_VALUE;
282    }
283
284    ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate);
285
286    return NO_ERROR;
287}
288
289status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
290{
291    audio_io_handle_t output;
292
293    if (streamType == AUDIO_STREAM_DEFAULT) {
294        streamType = AUDIO_STREAM_MUSIC;
295    }
296
297    output = getOutput(streamType);
298    if (output == AUDIO_IO_HANDLE_NONE) {
299        return PERMISSION_DENIED;
300    }
301
302    return getFrameCount(output, frameCount);
303}
304
305status_t AudioSystem::getFrameCount(audio_io_handle_t output,
306                                    size_t* frameCount)
307{
308    OutputDescriptor *outputDesc;
309
310    gLock.lock();
311    outputDesc = AudioSystem::gOutputs.valueFor(output);
312    if (outputDesc == NULL) {
313        gLock.unlock();
314        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
315        if (af == 0) return PERMISSION_DENIED;
316        *frameCount = af->frameCount(output);
317    } else {
318        *frameCount = outputDesc->frameCount;
319        gLock.unlock();
320    }
321    if (*frameCount == 0) {
322        ALOGE("AudioSystem::getFrameCount failed for output %d", output);
323        return BAD_VALUE;
324    }
325
326    ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount);
327
328    return NO_ERROR;
329}
330
331status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
332{
333    audio_io_handle_t output;
334
335    if (streamType == AUDIO_STREAM_DEFAULT) {
336        streamType = AUDIO_STREAM_MUSIC;
337    }
338
339    output = getOutput(streamType);
340    if (output == AUDIO_IO_HANDLE_NONE) {
341        return PERMISSION_DENIED;
342    }
343
344    return getLatency(output, latency);
345}
346
347status_t AudioSystem::getLatency(audio_io_handle_t output,
348                                 uint32_t* latency)
349{
350    OutputDescriptor *outputDesc;
351
352    gLock.lock();
353    outputDesc = AudioSystem::gOutputs.valueFor(output);
354    if (outputDesc == NULL) {
355        gLock.unlock();
356        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
357        if (af == 0) return PERMISSION_DENIED;
358        *latency = af->latency(output);
359    } else {
360        *latency = outputDesc->latency;
361        gLock.unlock();
362    }
363
364    ALOGV("getLatency() output %d, latency %d", output, *latency);
365
366    return NO_ERROR;
367}
368
369status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
370        audio_channel_mask_t channelMask, size_t* buffSize)
371{
372    gLock.lock();
373    // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
374    size_t inBuffSize = gInBuffSize;
375    if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
376        || (channelMask != gPrevInChannelMask)) {
377        gLock.unlock();
378        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379        if (af == 0) {
380            return PERMISSION_DENIED;
381        }
382        inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
383        if (inBuffSize == 0) {
384            ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
385                    sampleRate, format, channelMask);
386            return BAD_VALUE;
387        }
388        // A benign race is possible here: we could overwrite a fresher cache entry
389        gLock.lock();
390        // save the request params
391        gPrevInSamplingRate = sampleRate;
392        gPrevInFormat = format;
393        gPrevInChannelMask = channelMask;
394
395        gInBuffSize = inBuffSize;
396    }
397    gLock.unlock();
398    *buffSize = inBuffSize;
399
400    return NO_ERROR;
401}
402
403status_t AudioSystem::setVoiceVolume(float value)
404{
405    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
406    if (af == 0) return PERMISSION_DENIED;
407    return af->setVoiceVolume(value);
408}
409
410status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
411                                        uint32_t *dspFrames)
412{
413    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
414    if (af == 0) return PERMISSION_DENIED;
415
416    return af->getRenderPosition(halFrames, dspFrames, output);
417}
418
419uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
420{
421    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
422    uint32_t result = 0;
423    if (af == 0) return result;
424    if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
425
426    result = af->getInputFramesLost(ioHandle);
427    return result;
428}
429
430audio_unique_id_t AudioSystem::newAudioUniqueId()
431{
432    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
433    if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
434    return af->newAudioUniqueId();
435}
436
437void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid)
438{
439    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
440    if (af != 0) {
441        af->acquireAudioSessionId(audioSession, pid);
442    }
443}
444
445void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid)
446{
447    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
448    if (af != 0) {
449        af->releaseAudioSessionId(audioSession, pid);
450    }
451}
452
453audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
454{
455    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
456    if (af == 0) return AUDIO_HW_SYNC_INVALID;
457    return af->getAudioHwSyncForSession(sessionId);
458}
459
460// ---------------------------------------------------------------------------
461
462void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
463{
464    Mutex::Autolock _l(AudioSystem::gLock);
465
466    AudioSystem::gAudioFlinger.clear();
467    // clear output handles and stream to output map caches
468    AudioSystem::gOutputs.clear();
469
470    if (gAudioErrorCallback) {
471        gAudioErrorCallback(DEAD_OBJECT);
472    }
473    ALOGW("AudioFlinger server died!");
474}
475
476void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
477        const void *param2) {
478    ALOGV("ioConfigChanged() event %d", event);
479    const OutputDescriptor *desc;
480    audio_stream_type_t stream;
481
482    if (ioHandle == AUDIO_IO_HANDLE_NONE) return;
483
484    Mutex::Autolock _l(AudioSystem::gLock);
485
486    switch (event) {
487    case STREAM_CONFIG_CHANGED:
488        break;
489    case OUTPUT_OPENED: {
490        if (gOutputs.indexOfKey(ioHandle) >= 0) {
491            ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
492            break;
493        }
494        if (param2 == NULL) break;
495        desc = (const OutputDescriptor *)param2;
496
497        OutputDescriptor *outputDesc =  new OutputDescriptor(*desc);
498        gOutputs.add(ioHandle, outputDesc);
499        ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %zu "
500                "latency %d",
501                outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask,
502                outputDesc->frameCount, outputDesc->latency);
503        } break;
504    case OUTPUT_CLOSED: {
505        if (gOutputs.indexOfKey(ioHandle) < 0) {
506            ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle);
507            break;
508        }
509        ALOGV("ioConfigChanged() output %d closed", ioHandle);
510
511        gOutputs.removeItem(ioHandle);
512        } break;
513
514    case OUTPUT_CONFIG_CHANGED: {
515        int index = gOutputs.indexOfKey(ioHandle);
516        if (index < 0) {
517            ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle);
518            break;
519        }
520        if (param2 == NULL) break;
521        desc = (const OutputDescriptor *)param2;
522
523        ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x "
524                "frameCount %zu latency %d",
525                ioHandle, desc->samplingRate, desc->format,
526                desc->channelMask, desc->frameCount, desc->latency);
527        OutputDescriptor *outputDesc = gOutputs.valueAt(index);
528        delete outputDesc;
529        outputDesc =  new OutputDescriptor(*desc);
530        gOutputs.replaceValueFor(ioHandle, outputDesc);
531    } break;
532    case INPUT_OPENED:
533    case INPUT_CLOSED:
534    case INPUT_CONFIG_CHANGED:
535        break;
536
537    }
538}
539
540void AudioSystem::setErrorCallback(audio_error_callback cb)
541{
542    Mutex::Autolock _l(gLock);
543    gAudioErrorCallback = cb;
544}
545
546
547bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType)
548{
549    switch (streamType) {
550    case AUDIO_STREAM_MUSIC:
551    case AUDIO_STREAM_VOICE_CALL:
552    case AUDIO_STREAM_BLUETOOTH_SCO:
553    case AUDIO_STREAM_SYSTEM:
554        return true;
555    default:
556        return false;
557    }
558}
559
560
561// client singleton for AudioPolicyService binder interface
562sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
563sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
564
565
566// establish binder interface to AudioPolicy service
567const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
568{
569    gLock.lock();
570    if (gAudioPolicyService == 0) {
571        sp<IServiceManager> sm = defaultServiceManager();
572        sp<IBinder> binder;
573        do {
574            binder = sm->getService(String16("media.audio_policy"));
575            if (binder != 0)
576                break;
577            ALOGW("AudioPolicyService not published, waiting...");
578            usleep(500000); // 0.5 s
579        } while (true);
580        if (gAudioPolicyServiceClient == NULL) {
581            gAudioPolicyServiceClient = new AudioPolicyServiceClient();
582        }
583        binder->linkToDeath(gAudioPolicyServiceClient);
584        gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
585        gAudioPolicyService->registerClient(gAudioPolicyServiceClient);
586        gLock.unlock();
587    } else {
588        gLock.unlock();
589    }
590    return gAudioPolicyService;
591}
592
593// ---------------------------------------------------------------------------
594
595status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
596                                               audio_policy_dev_state_t state,
597                                               const char *device_address)
598{
599    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
600    const char *address = "";
601
602    if (aps == 0) return PERMISSION_DENIED;
603
604    if (device_address != NULL) {
605        address = device_address;
606    }
607
608    return aps->setDeviceConnectionState(device, state, address);
609}
610
611audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
612                                                  const char *device_address)
613{
614    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
615    if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
616
617    return aps->getDeviceConnectionState(device, device_address);
618}
619
620status_t AudioSystem::setPhoneState(audio_mode_t state)
621{
622    if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
623    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
624    if (aps == 0) return PERMISSION_DENIED;
625
626    return aps->setPhoneState(state);
627}
628
629status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
630{
631    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
632    if (aps == 0) return PERMISSION_DENIED;
633    return aps->setForceUse(usage, config);
634}
635
636audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
637{
638    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
639    if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
640    return aps->getForceUse(usage);
641}
642
643
644audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
645                                    uint32_t samplingRate,
646                                    audio_format_t format,
647                                    audio_channel_mask_t channelMask,
648                                    audio_output_flags_t flags,
649                                    const audio_offload_info_t *offloadInfo)
650{
651    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
652    if (aps == 0) return 0;
653    return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo);
654}
655
656audio_io_handle_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr,
657                                    uint32_t samplingRate,
658                                    audio_format_t format,
659                                    audio_channel_mask_t channelMask,
660                                    audio_output_flags_t flags,
661                                    const audio_offload_info_t *offloadInfo)
662{
663    if (attr == NULL) return 0;
664    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
665    if (aps == 0) return 0;
666    return aps->getOutputForAttr(attr, samplingRate, format, channelMask, flags, offloadInfo);
667}
668
669status_t AudioSystem::startOutput(audio_io_handle_t output,
670                                  audio_stream_type_t stream,
671                                  int session)
672{
673    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
674    if (aps == 0) return PERMISSION_DENIED;
675    return aps->startOutput(output, stream, session);
676}
677
678status_t AudioSystem::stopOutput(audio_io_handle_t output,
679                                 audio_stream_type_t stream,
680                                 int session)
681{
682    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
683    if (aps == 0) return PERMISSION_DENIED;
684    return aps->stopOutput(output, stream, session);
685}
686
687void AudioSystem::releaseOutput(audio_io_handle_t output)
688{
689    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
690    if (aps == 0) return;
691    aps->releaseOutput(output);
692}
693
694audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
695                                    uint32_t samplingRate,
696                                    audio_format_t format,
697                                    audio_channel_mask_t channelMask,
698                                    int sessionId,
699                                    audio_input_flags_t flags)
700{
701    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
702    if (aps == 0) return 0;
703    return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId, flags);
704}
705
706status_t AudioSystem::startInput(audio_io_handle_t input,
707                                 audio_session_t session)
708{
709    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
710    if (aps == 0) return PERMISSION_DENIED;
711    return aps->startInput(input, session);
712}
713
714status_t AudioSystem::stopInput(audio_io_handle_t input,
715                                audio_session_t session)
716{
717    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
718    if (aps == 0) return PERMISSION_DENIED;
719    return aps->stopInput(input, session);
720}
721
722void AudioSystem::releaseInput(audio_io_handle_t input,
723                               audio_session_t session)
724{
725    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
726    if (aps == 0) return;
727    aps->releaseInput(input, session);
728}
729
730status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
731                                    int indexMin,
732                                    int indexMax)
733{
734    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
735    if (aps == 0) return PERMISSION_DENIED;
736    return aps->initStreamVolume(stream, indexMin, indexMax);
737}
738
739status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
740                                           int index,
741                                           audio_devices_t device)
742{
743    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
744    if (aps == 0) return PERMISSION_DENIED;
745    return aps->setStreamVolumeIndex(stream, index, device);
746}
747
748status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
749                                           int *index,
750                                           audio_devices_t device)
751{
752    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
753    if (aps == 0) return PERMISSION_DENIED;
754    return aps->getStreamVolumeIndex(stream, index, device);
755}
756
757uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
758{
759    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
760    if (aps == 0) return 0;
761    return aps->getStrategyForStream(stream);
762}
763
764audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
765{
766    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
767    if (aps == 0) return AUDIO_DEVICE_NONE;
768    return aps->getDevicesForStream(stream);
769}
770
771audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
772{
773    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
774    // FIXME change return type to status_t, and return PERMISSION_DENIED here
775    if (aps == 0) return AUDIO_IO_HANDLE_NONE;
776    return aps->getOutputForEffect(desc);
777}
778
779status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
780                                audio_io_handle_t io,
781                                uint32_t strategy,
782                                int session,
783                                int id)
784{
785    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
786    if (aps == 0) return PERMISSION_DENIED;
787    return aps->registerEffect(desc, io, strategy, session, id);
788}
789
790status_t AudioSystem::unregisterEffect(int id)
791{
792    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
793    if (aps == 0) return PERMISSION_DENIED;
794    return aps->unregisterEffect(id);
795}
796
797status_t AudioSystem::setEffectEnabled(int id, bool enabled)
798{
799    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
800    if (aps == 0) return PERMISSION_DENIED;
801    return aps->setEffectEnabled(id, enabled);
802}
803
804status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
805{
806    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
807    if (aps == 0) return PERMISSION_DENIED;
808    if (state == NULL) return BAD_VALUE;
809    *state = aps->isStreamActive(stream, inPastMs);
810    return NO_ERROR;
811}
812
813status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state,
814        uint32_t inPastMs)
815{
816    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
817    if (aps == 0) return PERMISSION_DENIED;
818    if (state == NULL) return BAD_VALUE;
819    *state = aps->isStreamActiveRemotely(stream, inPastMs);
820    return NO_ERROR;
821}
822
823status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
824{
825    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
826    if (aps == 0) return PERMISSION_DENIED;
827    if (state == NULL) return BAD_VALUE;
828    *state = aps->isSourceActive(stream);
829    return NO_ERROR;
830}
831
832uint32_t AudioSystem::getPrimaryOutputSamplingRate()
833{
834    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
835    if (af == 0) return 0;
836    return af->getPrimaryOutputSamplingRate();
837}
838
839size_t AudioSystem::getPrimaryOutputFrameCount()
840{
841    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
842    if (af == 0) return 0;
843    return af->getPrimaryOutputFrameCount();
844}
845
846status_t AudioSystem::setLowRamDevice(bool isLowRamDevice)
847{
848    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
849    if (af == 0) return PERMISSION_DENIED;
850    return af->setLowRamDevice(isLowRamDevice);
851}
852
853void AudioSystem::clearAudioConfigCache()
854{
855    Mutex::Autolock _l(gLock);
856    ALOGV("clearAudioConfigCache()");
857    gOutputs.clear();
858}
859
860bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info)
861{
862    ALOGV("isOffloadSupported()");
863    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
864    if (aps == 0) return false;
865    return aps->isOffloadSupported(info);
866}
867
868status_t AudioSystem::listAudioPorts(audio_port_role_t role,
869                                     audio_port_type_t type,
870                                     unsigned int *num_ports,
871                                     struct audio_port *ports,
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->listAudioPorts(role, type, num_ports, ports, generation);
877}
878
879status_t AudioSystem::getAudioPort(struct audio_port *port)
880{
881    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
882    if (aps == 0) return PERMISSION_DENIED;
883    return aps->getAudioPort(port);
884}
885
886status_t AudioSystem::createAudioPatch(const struct audio_patch *patch,
887                                   audio_patch_handle_t *handle)
888{
889    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
890    if (aps == 0) return PERMISSION_DENIED;
891    return aps->createAudioPatch(patch, handle);
892}
893
894status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle)
895{
896    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
897    if (aps == 0) return PERMISSION_DENIED;
898    return aps->releaseAudioPatch(handle);
899}
900
901status_t AudioSystem::listAudioPatches(unsigned int *num_patches,
902                                  struct audio_patch *patches,
903                                  unsigned int *generation)
904{
905    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
906    if (aps == 0) return PERMISSION_DENIED;
907    return aps->listAudioPatches(num_patches, patches, generation);
908}
909
910status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config)
911{
912    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
913    if (aps == 0) return PERMISSION_DENIED;
914    return aps->setAudioPortConfig(config);
915}
916
917void AudioSystem::setAudioPortCallback(sp<AudioPortCallback> callBack)
918{
919    Mutex::Autolock _l(gLock);
920    gAudioPortCallback = callBack;
921}
922
923status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session,
924                                       audio_io_handle_t *ioHandle,
925                                       audio_devices_t *device)
926{
927    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
928    if (aps == 0) return PERMISSION_DENIED;
929    return aps->acquireSoundTriggerSession(session, ioHandle, device);
930}
931
932status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session)
933{
934    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
935    if (aps == 0) return PERMISSION_DENIED;
936    return aps->releaseSoundTriggerSession(session);
937}
938// ---------------------------------------------------------------------------
939
940void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused)
941{
942    Mutex::Autolock _l(gLock);
943    if (gAudioPortCallback != 0) {
944        gAudioPortCallback->onServiceDied();
945    }
946    AudioSystem::gAudioPolicyService.clear();
947
948    ALOGW("AudioPolicyService server died!");
949}
950
951void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate()
952{
953    Mutex::Autolock _l(gLock);
954    if (gAudioPortCallback != 0) {
955        gAudioPortCallback->onAudioPortListUpdate();
956    }
957}
958
959void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate()
960{
961    Mutex::Autolock _l(gLock);
962    if (gAudioPortCallback != 0) {
963        gAudioPortCallback->onAudioPatchListUpdate();
964    }
965}
966
967}; // namespace android
968