189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project/*
289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * Copyright (C) 2006-2007 The Android Open Source Project
389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project *
489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * you may not use this file except in compliance with the License.
689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * You may obtain a copy of the License at
789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project *
889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project *
1089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * See the License for the specific language governing permissions and
1489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project * limitations under the License.
1589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project */
1689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#define LOG_TAG "AudioSystem"
1889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project//#define LOG_NDEBUG 0
1989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <utils/Log.h>
217562408b2261d38415453378b6188f74fda99d88Mathias Agopian#include <binder/IServiceManager.h>
2289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/AudioSystem.h>
231ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/IAudioFlinger.h>
24c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent#include <media/IAudioPolicyService.h>
2589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <math.h>
2689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2764760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
28fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
29c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent// ----------------------------------------------------------------------------
30c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
3189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectnamespace android {
3289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
3389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// client singleton for AudioFlinger binder interface
3489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source ProjectMutex AudioSystem::gLock;
35d2d089fc86c62843992e7d5b371ee9227189a1e6Glenn KastenMutex AudioSystem::gLockAPS;
3689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectsp<IAudioFlinger> AudioSystem::gAudioFlinger;
3789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectsp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
3889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectaudio_error_callback AudioSystem::gAudioErrorCallback = NULL;
39f613d42b12389335b2ecf06df18d0d095d6bfd44Jean-Michel Trividynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL;
402f4fe9f7df8f22c6bc8745407d19df73128810ecJean-Michel Trivirecord_config_callback  AudioSystem::gRecordConfigCallback = NULL;
41211eeaf17e5565b68447d29799dbf158a33cf4cfGlenn Kasten
4289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
4389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// establish binder interface to AudioFlinger service
440ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurentconst sp<IAudioFlinger> AudioSystem::get_audio_flinger()
4589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
460ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    sp<IAudioFlinger> af;
470ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    sp<AudioFlingerClient> afc;
480ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    {
490ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        Mutex::Autolock _l(gLock);
500ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        if (gAudioFlinger == 0) {
510ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            sp<IServiceManager> sm = defaultServiceManager();
520ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            sp<IBinder> binder;
530ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            do {
540ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                binder = sm->getService(String16("media.audio_flinger"));
550ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                if (binder != 0)
560ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                    break;
570ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                ALOGW("AudioFlinger not published, waiting...");
580ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                usleep(500000); // 0.5 s
590ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            } while (true);
600ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            if (gAudioFlingerClient == NULL) {
610ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                gAudioFlingerClient = new AudioFlingerClient();
620ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            } else {
630ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                if (gAudioErrorCallback) {
640ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                    gAudioErrorCallback(NO_ERROR);
650ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent                }
6689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            }
670ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            binder->linkToDeath(gAudioFlingerClient);
680ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            gAudioFlinger = interface_cast<IAudioFlinger>(binder);
690ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0);
700ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent            afc = gAudioFlingerClient;
71e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        }
720ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        af = gAudioFlinger;
7389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
740ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    if (afc != 0) {
750ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        af->registerClient(afc);
760ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    }
770ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    return af;
7889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
7989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
80296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurentconst sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient()
81296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent{
82296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    // calling get_audio_flinger() will initialize gAudioFlingerClient if needed
83296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
84296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (af == 0) return 0;
85296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    Mutex::Autolock _l(gLock);
86296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    return gAudioFlingerClient;
87296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent}
88296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
89296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurentsp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle)
90296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent{
91296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    sp<AudioIoDescriptor> desc;
92296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    const sp<AudioFlingerClient> afc = getAudioFlingerClient();
93296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (afc != 0) {
94296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        desc = afc->getIoDescriptor(ioHandle);
95296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
96296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    return desc;
97296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent}
98296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
9946291616486979986cba3ab83e894728ef53063fEric Laurent/* static */ status_t AudioSystem::checkAudioFlinger()
10046291616486979986cba3ab83e894728ef53063fEric Laurent{
10146291616486979986cba3ab83e894728ef53063fEric Laurent    if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) {
10246291616486979986cba3ab83e894728ef53063fEric Laurent        return NO_ERROR;
10346291616486979986cba3ab83e894728ef53063fEric Laurent    }
10446291616486979986cba3ab83e894728ef53063fEric Laurent    return DEAD_OBJECT;
10546291616486979986cba3ab83e894728ef53063fEric Laurent}
10646291616486979986cba3ab83e894728ef53063fEric Laurent
1074a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten// FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp
1084a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
1094944acb7355b3aa25748fd25945a363a69d65444Glenn Kastenstatus_t AudioSystem::muteMicrophone(bool state)
1104944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
11189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
11289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
11389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return af->setMicMute(state);
11489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
11589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1164944acb7355b3aa25748fd25945a363a69d65444Glenn Kastenstatus_t AudioSystem::isMicrophoneMuted(bool* state)
1174944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
11889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
11989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
12089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *state = af->getMicMute();
12189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
12289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
12389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
12489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioSystem::setMasterVolume(float value)
12589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
12689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
12789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
12889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    af->setMasterVolume(value);
12989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
13089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
13189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
13289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioSystem::setMasterMute(bool mute)
13389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
13489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
13589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
13689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    af->setMasterMute(mute);
13789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
13889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
13989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
14089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioSystem::getMasterVolume(float* volume)
14189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
14289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
14389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
14489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *volume = af->masterVolume();
14589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
14689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
14789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
14889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatus_t AudioSystem::getMasterMute(bool* mute)
14989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
15089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
15189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
15289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *mute = af->masterMute();
15389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
15489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
15589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
15672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
15772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
15889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
159fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
16089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
16189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
162c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    af->setStreamVolume(stream, value, output);
16389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
16489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
16589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
166fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
16789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
168fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
16989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
17089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
17189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    af->setStreamMute(stream, mute);
17289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
17389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
17489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
17572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
17672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
17789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
178fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
17989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
18089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
181c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    *volume = af->streamVolume(stream, output);
18289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
18389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
18489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
185fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
18689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
187fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
18889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
18989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
19089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    *mute = af->streamMute(stream);
19189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
19289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
19389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
194f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioSystem::setMode(audio_mode_t mode)
19589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
196930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
19789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
19889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
19989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return af->setMode(mode);
20089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
20189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2024944acb7355b3aa25748fd25945a363a69d65444Glenn Kastenstatus_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
2034944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
20489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
20589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    if (af == 0) return PERMISSION_DENIED;
206c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    return af->setParameters(ioHandle, keyValuePairs);
20789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
20889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2094944acb7355b3aa25748fd25945a363a69d65444Glenn KastenString8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys)
2104944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
21189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
212c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    String8 result = String8("");
213c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    if (af == 0) return result;
21489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
215c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    result = af->getParameters(ioHandle, keys);
216c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    return result;
21789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
21889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
219c23885ebb142b9da31543789ecc3f7cf7111bc67Glenn Kastenstatus_t AudioSystem::setParameters(const String8& keyValuePairs)
220c23885ebb142b9da31543789ecc3f7cf7111bc67Glenn Kasten{
221142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs);
222c23885ebb142b9da31543789ecc3f7cf7111bc67Glenn Kasten}
223c23885ebb142b9da31543789ecc3f7cf7111bc67Glenn Kasten
224c23885ebb142b9da31543789ecc3f7cf7111bc67Glenn KastenString8 AudioSystem::getParameters(const String8& keys)
225c23885ebb142b9da31543789ecc3f7cf7111bc67Glenn Kasten{
226142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    return getParameters(AUDIO_IO_HANDLE_NONE, keys);
227c23885ebb142b9da31543789ecc3f7cf7111bc67Glenn Kasten}
228c23885ebb142b9da31543789ecc3f7cf7111bc67Glenn Kasten
22989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// convert volume steps to natural log scale
23089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
23189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// change this value to change volume scaling
23289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatic const float dBPerStep = 0.5f;
23389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// shouldn't need to touch these
23489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatic const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
23589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectstatic const float dBConvertInverse = 1.0f / dBConvert;
23689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
23789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectfloat AudioSystem::linearToLog(int volume)
23889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
23989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
240b8a805261bf0282e992d3608035e47d05a898710Steve Block    // ALOGD("linearToLog(%d)=%f", volume, v);
24189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // return v;
24289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return volume ? exp(float(100 - volume) * dBConvert) : 0;
24389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
24489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
24589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectint AudioSystem::logToLinear(float volume)
24689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
24789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
248b8a805261bf0282e992d3608035e47d05a898710Steve Block    // ALOGD("logTolinear(%d)=%f", v, volume);
24989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // return v;
25089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
25189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
25289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2533b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kastenstatus_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType)
25489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
255c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    audio_io_handle_t output;
256c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
257fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (streamType == AUDIO_STREAM_DEFAULT) {
258fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        streamType = AUDIO_STREAM_MUSIC;
259c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
260c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
261fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    output = getOutput(streamType);
262c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    if (output == 0) {
263c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        return PERMISSION_DENIED;
264c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
265c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
266b7f24b101d43139b4c747129bfbc4ecf5c468b86Jean-Michel Trivi    return getSamplingRate(output, samplingRate);
2671a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent}
2681a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent
2692c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kastenstatus_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle,
2703b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten                                      uint32_t* samplingRate)
2711a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent{
272f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
273f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    if (af == 0) return PERMISSION_DENIED;
2742c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
2752c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    if (desc == 0) {
2762c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten        *samplingRate = af->sampleRate(ioHandle);
277c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    } else {
2782c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten        *samplingRate = desc->mSamplingRate;
279c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
280f94006ca0f30c27868c284e553f57bf467ae92bcGlenn Kasten    if (*samplingRate == 0) {
2812c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten        ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle);
282f94006ca0f30c27868c284e553f57bf467ae92bcGlenn Kasten        return BAD_VALUE;
283f94006ca0f30c27868c284e553f57bf467ae92bcGlenn Kasten    }
284c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
2852c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate);
28689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
28789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
28889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
28989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
290e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kastenstatus_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType)
29189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
292c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    audio_io_handle_t output;
293c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
294fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (streamType == AUDIO_STREAM_DEFAULT) {
295fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        streamType = AUDIO_STREAM_MUSIC;
296c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
29789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
298fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    output = getOutput(streamType);
299142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output == AUDIO_IO_HANDLE_NONE) {
300c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        return PERMISSION_DENIED;
301c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
302c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
303b7f24b101d43139b4c747129bfbc4ecf5c468b86Jean-Michel Trivi    return getFrameCount(output, frameCount);
3041a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent}
3051a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent
3062c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kastenstatus_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle,
307e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten                                    size_t* frameCount)
3081a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent{
309f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
310f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    if (af == 0) return PERMISSION_DENIED;
3112c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
3122c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    if (desc == 0) {
3132c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten        *frameCount = af->frameCount(ioHandle);
314c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    } else {
3152c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten        *frameCount = desc->mFrameCount;
316c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
317f94006ca0f30c27868c284e553f57bf467ae92bcGlenn Kasten    if (*frameCount == 0) {
3182c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten        ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle);
319f94006ca0f30c27868c284e553f57bf467ae92bcGlenn Kasten        return BAD_VALUE;
320f94006ca0f30c27868c284e553f57bf467ae92bcGlenn Kasten    }
32148f7f5e8359909ddfc6492a79a8b9c44759ca6c3Eric Laurent
3222c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
32389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
32489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
32589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
32689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
327fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
32889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
329c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    audio_io_handle_t output;
330c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
331fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (streamType == AUDIO_STREAM_DEFAULT) {
332fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        streamType = AUDIO_STREAM_MUSIC;
333c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
33489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
335fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    output = getOutput(streamType);
336142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output == AUDIO_IO_HANDLE_NONE) {
337c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        return PERMISSION_DENIED;
338c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
339c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
340241618f1b286f9e7e02fe61e96d9194e2e1e8a7aGlenn Kasten    return getLatency(output, latency);
3411a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent}
3421a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent
3431a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurentstatus_t AudioSystem::getLatency(audio_io_handle_t output,
3441a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent                                 uint32_t* latency)
3451a9ed11a472493cac7f6dfcbfac2064526a493edEric Laurent{
346f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
347f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    if (af == 0) return PERMISSION_DENIED;
348296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output);
34973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    if (outputDesc == 0) {
350c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        *latency = af->latency(output);
351c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    } else {
35273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        *latency = outputDesc->mLatency;
353c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    }
35448f7f5e8359909ddfc6492a79a8b9c44759ca6c3Eric Laurent
355241618f1b286f9e7e02fe61e96d9194e2e1e8a7aGlenn Kasten    ALOGV("getLatency() output %d, latency %d", output, *latency);
35689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
35789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    return NO_ERROR;
35889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
35989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
360dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastenstatus_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
361dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask, size_t* buffSize)
36289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
363296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    const sp<AudioFlingerClient> afc = getAudioFlingerClient();
364296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (afc == 0) {
365296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        return NO_INIT;
366296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
367296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize);
36889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
36989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
370f0ee6f4055e26fb35d9c526a596668a4dc9da5baEric Laurentstatus_t AudioSystem::setVoiceVolume(float value)
371f0ee6f4055e26fb35d9c526a596668a4dc9da5baEric Laurent{
372f0ee6f4055e26fb35d9c526a596668a4dc9da5baEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
373f0ee6f4055e26fb35d9c526a596668a4dc9da5baEric Laurent    if (af == 0) return PERMISSION_DENIED;
374f0ee6f4055e26fb35d9c526a596668a4dc9da5baEric Laurent    return af->setVoiceVolume(value);
375f0ee6f4055e26fb35d9c526a596668a4dc9da5baEric Laurent}
376f0ee6f4055e26fb35d9c526a596668a4dc9da5baEric Laurent
377377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames,
3780ed19594452c901c3c8665d06610fffe46895d06Glenn Kasten                                        uint32_t *dspFrames)
379342e9cf388cceb807def720e40e8b0a217f4bcaaEric Laurent{
380342e9cf388cceb807def720e40e8b0a217f4bcaaEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
381342e9cf388cceb807def720e40e8b0a217f4bcaaEric Laurent    if (af == 0) return PERMISSION_DENIED;
382342e9cf388cceb807def720e40e8b0a217f4bcaaEric Laurent
383ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    return af->getRenderPosition(halFrames, dspFrames, output);
384342e9cf388cceb807def720e40e8b0a217f4bcaaEric Laurent}
385342e9cf388cceb807def720e40e8b0a217f4bcaaEric Laurent
3864944acb7355b3aa25748fd25945a363a69d65444Glenn Kastenuint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle)
3874944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
38805bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
3895f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kasten    uint32_t result = 0;
39005bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent    if (af == 0) return result;
391142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) return result;
39205bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent
39305bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent    result = af->getInputFramesLost(ioHandle);
39405bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent    return result;
39505bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent}
39605bca2fde53bfe3063d2a0a877f2b6bfdd6052cfEric Laurent
397eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kastenaudio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use)
3984944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
399be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
400de3f8392fbf380ba6f09d009b00d7172477389a2Eric Laurent    if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE;
401eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    return af->newAudioUniqueId(use);
402be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent}
403be916aa1267e2e6b1c148f51d11bcbbc79cb864cEric Laurent
404d848eb48c121c119e8ba7583efc75415fe102570Glenn Kastenvoid AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
4054944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
4063a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
4073a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (af != 0) {
408d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        af->acquireAudioSessionId(audioSession, pid);
4093a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
4103a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen}
4113a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
412d848eb48c121c119e8ba7583efc75415fe102570Glenn Kastenvoid AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
4134944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
4143a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
4153a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (af != 0) {
416d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        af->releaseAudioSessionId(audioSession, pid);
4173a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
4183a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen}
4193a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
42093c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurentaudio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId)
42193c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent{
42293c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
42393c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    if (af == 0) return AUDIO_HW_SYNC_INVALID;
42493c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    return af->getAudioHwSyncForSession(sessionId);
42593c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent}
42693c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent
42772e3f39146fce4686bd96f11057c051bea376dfbEric Laurentstatus_t AudioSystem::systemReady()
42872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent{
42972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
43072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    if (af == 0) return NO_INIT;
43172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    return af->systemReady();
43272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent}
43372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
4344a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kastenstatus_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle,
4354a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten                                       size_t* frameCount)
4364a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten{
4374a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
4384a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    if (af == 0) return PERMISSION_DENIED;
4394a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle);
4404a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    if (desc == 0) {
4414a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten        *frameCount = af->frameCountHAL(ioHandle);
4424a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    } else {
4434a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten        *frameCount = desc->mFrameCountHAL;
4444a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    }
4454a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    if (*frameCount == 0) {
4464a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten        ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle);
4474a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten        return BAD_VALUE;
4484a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    }
4494a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
4504a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount);
4514a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
4524a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    return NO_ERROR;
4534a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten}
4544a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
45589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project// ---------------------------------------------------------------------------
45689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
45773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent
45873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioSystem::AudioFlingerClient::clearIoCache()
45973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent{
46073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    Mutex::Autolock _l(mLock);
46173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    mIoDescriptors.clear();
46273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    mInBuffSize = 0;
46373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    mInSamplingRate = 0;
46473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    mInFormat = AUDIO_FORMAT_DEFAULT;
46573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    mInChannelMask = AUDIO_CHANNEL_NONE;
46673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent}
46773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent
4684944acb7355b3aa25748fd25945a363a69d65444Glenn Kastenvoid AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused)
4694944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
470f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    audio_error_callback cb = NULL;
471f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    {
472f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent        Mutex::Autolock _l(AudioSystem::gLock);
473f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent        AudioSystem::gAudioFlinger.clear();
474f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent        cb = gAudioErrorCallback;
475f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    }
47689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
47773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    // clear output handles and stream to output map caches
47873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    clearIoCache();
47989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
480f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent    if (cb) {
481f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8Eric Laurent        cb(DEAD_OBJECT);
48289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
4835ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block    ALOGW("AudioFlinger server died!");
48489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
48589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
48673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event,
48773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent                                                      const sp<AudioIoDescriptor>& ioDesc) {
4883856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("ioConfigChanged() event %d", event);
489c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
49073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return;
49189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
492296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
493296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    Vector < sp<AudioDeviceCallback> > callbacks;
49489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
495296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    {
496296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        Mutex::Autolock _l(mLock);
497296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
498296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        switch (event) {
499296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        case AUDIO_OUTPUT_OPENED:
500296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        case AUDIO_INPUT_OPENED: {
50149fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavan            sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
5027c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent            if (oldDesc == 0) {
5037c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                mIoDescriptors.add(ioDesc->mIoHandle, ioDesc);
5047c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent            } else {
5057c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                deviceId = oldDesc->getDeviceId();
5067c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
507296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            }
508296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
509296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
510296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                deviceId = ioDesc->getDeviceId();
511296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
512296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                if (ioIndex >= 0) {
513296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                    callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
514296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                }
515296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            }
516296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            ALOGV("ioConfigChanged() new %s opened %d samplingRate %u, format %#x channel mask %#x "
517296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                    "frameCount %zu deviceId %d", event == AUDIO_OUTPUT_OPENED ? "output" : "input",
518296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                    ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask,
519296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                    ioDesc->mFrameCount, ioDesc->getDeviceId());
520296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            } break;
521296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        case AUDIO_OUTPUT_CLOSED:
522296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        case AUDIO_INPUT_CLOSED: {
52349fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavan            if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) {
524296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                ALOGW("ioConfigChanged() closing unknown %s %d",
525296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                      event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
526296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                break;
527296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            }
528296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            ALOGV("ioConfigChanged() %s %d closed",
52973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent                  event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle);
530c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
531296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            mIoDescriptors.removeItem(ioDesc->mIoHandle);
532296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle);
533296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            } break;
534c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
535296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        case AUDIO_OUTPUT_CONFIG_CHANGED:
536296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        case AUDIO_INPUT_CONFIG_CHANGED: {
53749fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavan            sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle);
538296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            if (oldDesc == 0) {
539296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle);
540296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                break;
541296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            }
542296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
543296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            deviceId = oldDesc->getDeviceId();
544296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc);
545296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
546296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            if (deviceId != ioDesc->getDeviceId()) {
547296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                deviceId = ioDesc->getDeviceId();
548296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle);
549296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                if (ioIndex >= 0) {
550296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                    callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
551296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                }
552296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            }
553296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x "
5544a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten                    "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d",
555296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                    event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input",
556296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                    ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat,
5574a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten                    ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL, ioDesc->getDeviceId());
558296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
559296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        } break;
560c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent        }
561296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
562296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    // callbacks.size() != 0 =>  ioDesc->mIoHandle and deviceId are valid
563296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    for (size_t i = 0; i < callbacks.size(); i++) {
564296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        callbacks[i]->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId);
56573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    }
56673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent}
56773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent
56873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentstatus_t AudioSystem::AudioFlingerClient::getInputBufferSize(
56973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent                                                uint32_t sampleRate, audio_format_t format,
57073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent                                                audio_channel_mask_t channelMask, size_t* buffSize)
57173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent{
57273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
57373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    if (af == 0) {
57473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        return PERMISSION_DENIED;
57573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    }
57673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    Mutex::Autolock _l(mLock);
57773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values
57873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat)
57973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        || (channelMask != mInChannelMask)) {
58073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
58173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        if (inBuffSize == 0) {
58273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x",
58373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent                    sampleRate, format, channelMask);
58473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            return BAD_VALUE;
58573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        }
58673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        // A benign race is possible here: we could overwrite a fresher cache entry
58773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        // save the request params
58873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        mInSamplingRate = sampleRate;
58973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        mInFormat = format;
59073e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        mInChannelMask = channelMask;
59173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent
59273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        mInBuffSize = inBuffSize;
59373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    }
59448f7f5e8359909ddfc6492a79a8b9c44759ca6c3Eric Laurent
59573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    *buffSize = mInBuffSize;
59673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent
59773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    return NO_ERROR;
59873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent}
59973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent
60049fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavansp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle)
60173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent{
60273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    sp<AudioIoDescriptor> desc;
60373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    ssize_t index = mIoDescriptors.indexOfKey(ioHandle);
60473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    if (index >= 0) {
60573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        desc = mIoDescriptors.valueAt(index);
60689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    }
60773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    return desc;
60889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project}
60989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
61049fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavansp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle)
61149fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavan{
61249fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavan    Mutex::Autolock _l(mLock);
61349fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavan    return getIoDescriptor_l(ioHandle);
61449fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavan}
61549fdeaff2f4c1405738320b7c3a540b528457e1bPraveen Chavan
616296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurentstatus_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback(
617296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
618296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent{
619296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    Mutex::Autolock _l(mLock);
620296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    Vector < sp<AudioDeviceCallback> > callbacks;
621296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
622296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (ioIndex >= 0) {
623296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
624296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
625296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
626296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
627296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        if (callbacks[cbIndex] == callback) {
628296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            return INVALID_OPERATION;
629296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        }
630296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
631296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    callbacks.add(callback);
632296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
633296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
634296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    return NO_ERROR;
635296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent}
636296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
637296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurentstatus_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback(
638296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo)
639296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent{
640296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    Mutex::Autolock _l(mLock);
641296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo);
642296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (ioIndex < 0) {
643296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        return INVALID_OPERATION;
644296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
645296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    Vector < sp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex);
646296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
647296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    size_t cbIndex;
648296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) {
649296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        if (callbacks[cbIndex] == callback) {
650296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent            break;
651296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        }
652296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
653296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (cbIndex == callbacks.size()) {
654296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        return INVALID_OPERATION;
655296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
656296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    callbacks.removeAt(cbIndex);
657296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    if (callbacks.size() != 0) {
658296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks);
659296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    } else {
660296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent        mAudioDeviceCallbacks.removeItem(audioIo);
661296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    }
662296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent    return NO_ERROR;
663296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent}
664296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent
665296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent/* static */ void AudioSystem::setErrorCallback(audio_error_callback cb)
6664944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten{
667c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    Mutex::Autolock _l(gLock);
668c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent    gAudioErrorCallback = cb;
669c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent}
670c2f1f07084818942352c6bbfb36af9b6b330eb4eEric Laurent
671f613d42b12389335b2ecf06df18d0d095d6bfd44Jean-Michel Trivi/*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb)
672f613d42b12389335b2ecf06df18d0d095d6bfd44Jean-Michel Trivi{
673f613d42b12389335b2ecf06df18d0d095d6bfd44Jean-Michel Trivi    Mutex::Autolock _l(gLock);
674f613d42b12389335b2ecf06df18d0d095d6bfd44Jean-Michel Trivi    gDynPolicyCallback = cb;
675f613d42b12389335b2ecf06df18d0d095d6bfd44Jean-Michel Trivi}
676