AudioFlinger.cpp revision 050677873c10d4da308ac222f8533c96cca3207e
199e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten/*
265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Copyright 2007, The Android Open Source Project
465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Licensed under the Apache License, Version 2.0 (the "License");
665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** you may not use this file except in compliance with the License.
765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** You may obtain a copy of the License at
865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**     http://www.apache.org/licenses/LICENSE-2.0
1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Unless required by applicable law or agreed to in writing, software
1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** distributed under the License is distributed on an "AS IS" BASIS,
1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** See the License for the specific language governing permissions and
1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** limitations under the License.
1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian*/
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define LOG_TAG "AudioFlinger"
2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//#define LOG_NDEBUG 0
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
22153b9fe667e6e78e0218ff0159353097428c7657Glenn Kasten#include "Configuration.h"
23da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <dirent.h>
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <math.h>
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <signal.h>
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/time.h>
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/resource.h>
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
299ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang#include <binder/IPCThreadState.h>
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IServiceManager.h>
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Log.h>
32d8e6fd35ec2b59ee7d873daf1f1d9d348221c7bcGlenn Kasten#include <utils/Trace.h>
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/Parcel.h>
3435fec5f61393124c9e13958941637b8fe386385eAndy Hung#include <memunreachable/memunreachable.h>
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/String16.h>
3665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h>
3738ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent#include <utils/Atomic.h>
3865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <cutils/bitops.h>
4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/properties.h>
4165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4264760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
437394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin#include <hardware/audio.h>
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioMixer.h"
4665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioFlinger.h"
4744deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten#include "ServiceUtilities.h"
4865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
496770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung#include <media/AudioResamplerPublic.h>
506770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung
5165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/EffectsFactoryApi.h>
526d8b694d999e9be7d5dcc336535832a80fb6f61fEric Laurent#include <audio_effects/effect_visualizer.h>
5359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_ns.h>
5459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_aec.h>
5565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
563b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten#include <audio_utils/primitives.h>
573b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten
58feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent#include <powermanager/PowerManager.h>
59190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten#include <media/IMediaLogService.h>
6107b745e166ee62030960ccea37e117caadf71c32Andy Hung#include <media/MemoryLeakTrackUtil.h>
62da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <media/nbaio/Pipe.h>
63da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <media/nbaio/PipeReader.h>
641ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/AudioParameter.h>
653f273d10817ddb2f792ae043de692efcdf1988aeWei Jia#include <mediautils/BatteryNotifier.h>
664182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten#include <private/android_filesystem_config.h>
67da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
6865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
701c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// Note: the following macro is used for extremely verbose logging message.  In
711c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
721c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// 0; but one side effect of this is to turn all LOGV's as well.  Some messages
731c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are so verbose that we want to suppress them even when we have ALOG_ASSERT
741c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// turned on.  Do not uncomment the #def below unless you really know what you
751c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are doing and want to see all of the extremely verbose messages.
761c345196edc61694f29307a1826a64a0d26028dcJohn Grossman//#define VERY_VERY_VERBOSE_LOGGING
771c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#ifdef VERY_VERY_VERBOSE_LOGGING
781c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV ALOGV
791c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#else
801c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV(a...) do { } while(0)
811c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#endif
82de070137f11d346fba77605bd76a44c040a618fcEric Laurent
8365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
8465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
86ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kHardwareLockedString[] = "Hardware lock is taken\n";
87021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentstatic const char kClientLockedString[] = "Client lock is taken\n";
8865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
904ff14bae91075eb274eb1c2975982358946e7e63John Grossmannsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
917cafbb32999049873d4746ba83bd20c88abe6ce6Eric Laurent
9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::mScreenState;
933ed292031dc50c56110cdadb1e3778117e3be76aGlenn Kasten
9446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
95da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkInputEnabled = false;
96da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkOutputEnabled = false;
97da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkTrackEnabled = false;
98da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
99da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
100da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
101da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
10246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
103da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
104813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
105813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// we define a minimum time during which a global effect is considered enabled.
106813e2a74853bde19e37d878c596a044b3f299efcEric Laurentstatic const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
107813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
10865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
10965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
110b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissenconst char *formatToString(audio_format_t format) {
111fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk    switch (audio_get_main_format(format)) {
1122829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_PCM:
1132829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        switch (format) {
1142829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
1152829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
1162829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
1172829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
1182829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
1192829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
1202829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        default:
1212829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad            break;
1222829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        }
1232829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        break;
124b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_MP3: return "mp3";
125b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_NB: return "amr-nb";
126b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_WB: return "amr-wb";
127b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AAC: return "aac";
128b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
129b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
130b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_VORBIS: return "vorbis";
1312829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_OPUS: return "opus";
1322829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_AC3: return "ac-3";
1332829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_E_AC3: return "e-ac-3";
134fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk    case AUDIO_FORMAT_IEC61937: return "iec61937";
135b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    default:
136b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        break;
137b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
138b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    return "unknown";
139b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen}
140b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
141f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
142799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
143f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    const hw_module_t *mod;
144799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc;
145799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
146f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
147f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
148f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
149f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
150799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
151f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
152f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = audio_hw_device_open(mod, dev);
153f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
154f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
155f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
156799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
157f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
1585806b3533437e3d83208e8e9d6bd74ed304e51ecEric Laurent    if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
159f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
160f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        rc = BAD_VALUE;
161f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        goto out;
162f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
163799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return 0;
164799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
165799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout:
166799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    *dev = NULL;
167799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return rc;
168799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
169799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
17165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger()
17365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    : BnAudioFlinger(),
1744ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPrimaryHardwareDev(NULL),
1759ea65d0f4a564478343b1a722fae4ce5883670c3Glenn Kasten      mAudioHwDevs(NULL),
1767d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten      mHardwareStatus(AUDIO_HW_IDLE),
1774ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterVolume(1.0f),
1784ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterMute(false),
179d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten      // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),
1804ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMode(AUDIO_MODE_INVALID),
1814182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mBtNrecIsOff(false),
1824182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mIsLowRamDevice(true),
183813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mIsDeviceTypeKnown(false),
1844ea00a25cf85877b48ebd1e15a657cfaab29af58Glenn Kasten      mGlobalEffectEnableTime(0),
18572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent      mSystemReady(false)
18665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
187d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum
188d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) {
189d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten        // zero ID has a special meaning, so unavailable
190d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten        mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX;
191d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    }
192d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten
193949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten    getpid_cached = getpid();
194ae0cff1d48b2cd10aeff9627398faf684894eeceGlenn Kasten    const bool doLog = property_get_bool("ro.test_harness", false);
1959e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (doLog) {
196b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten        mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
197b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten                MemoryHeapBase::READ_ONLY);
1989e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
1991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2003f273d10817ddb2f792ae043de692efcdf1988aeWei Jia    // reset battery stats.
2013f273d10817ddb2f792ae043de692efcdf1988aeWei Jia    // if the audio service has crashed, battery stats could be left
2023f273d10817ddb2f792ae043de692efcdf1988aeWei Jia    // in bad state, reset the state upon service start.
2033f273d10817ddb2f792ae043de692efcdf1988aeWei Jia    BatteryNotifier::getInstance().noteResetAudio();
2043f273d10817ddb2f792ae043de692efcdf1988aeWei Jia
20546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2069a00399340c7c129714dff96f1ab59045fe43056Glenn Kasten    char value[PROPERTY_VALUE_MAX];
207da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    (void) property_get("ro.debuggable", value, "0");
208da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int debuggable = atoi(value);
209da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int teeEnabled = 0;
210da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    if (debuggable) {
211da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        (void) property_get("af.tee", value, "0");
212da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teeEnabled = atoi(value);
213da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    }
214f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten    // FIXME symbolic constants here
2156e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 1) {
216da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkInputEnabled = true;
2176e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2186e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 2) {
219da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkOutputEnabled = true;
2206e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2216e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 4) {
222da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkTrackEnabled = true;
2236e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
22446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2255a61d2f277af3098fc10b2881babca16391362daDima Zavin}
2265a61d2f277af3098fc10b2881babca16391362daDima Zavin
2275a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef()
2285a61d2f277af3098fc10b2881babca16391362daDima Zavin{
229935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    Mutex::Autolock _l(mLock);
230935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
231799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    /* TODO: move all this work into an Init() function */
2324ff14bae91075eb274eb1c2975982358946e7e63John Grossman    char val_str[PROPERTY_VALUE_MAX] = { 0 };
2334ff14bae91075eb274eb1c2975982358946e7e63John Grossman    if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
2344ff14bae91075eb274eb1c2975982358946e7e63John Grossman        uint32_t int_val;
2354ff14bae91075eb274eb1c2975982358946e7e63John Grossman        if (1 == sscanf(val_str, "%u", &int_val)) {
2364ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = milliseconds(int_val);
2374ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using %u mSec as standby time.", int_val);
2384ff14bae91075eb274eb1c2975982358946e7e63John Grossman        } else {
2394ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
2404ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using default %u mSec as standby time.",
2414ff14bae91075eb274eb1c2975982358946e7e63John Grossman                    (uint32_t)(mStandbyTimeInNsecs / 1000000));
2424ff14bae91075eb274eb1c2975982358946e7e63John Grossman        }
2434ff14bae91075eb274eb1c2975982358946e7e63John Grossman    }
24465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2451c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    mPatchPanel = new PatchPanel(this);
2461c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
247a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    mMode = AUDIO_MODE_NORMAL;
24865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
24965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
25065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger()
25165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
25265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mRecordThreads.isEmpty()) {
253c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeInput_nonvirtual() will remove specified entry from mRecordThreads
254d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeInput_nonvirtual(mRecordThreads.keyAt(0));
25565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
25665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mPlaybackThreads.isEmpty()) {
257c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
258d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
25965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
260799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
2612b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2622b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten        // no mHardwareLock needed, as there are no other references to this
263a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
264a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        delete mAudioHwDevs.valueAt(i);
26565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
266481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
267481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Tell media.log service about any old writers that still need to be unregistered
268ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung    if (mLogMemoryDealer != 0) {
269ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung        sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
270ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung        if (binder != 0) {
271ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung            sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
272ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung            for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
273ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung                sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
274ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung                mUnregisteredWriters.pop();
275ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung                mediaLogService->unregisterWriter(iMemory);
276ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung            }
277481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
278481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
27965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
28065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
281a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = {
282a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_PRIMARY,
283a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_A2DP,
284a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_USB,
285a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent};
286a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
287a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
288062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioHwDevice* AudioFlinger::findSuitableHwDev_l(
289ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_module_handle_t module,
290ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_devices_t devices)
291799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
292a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // if module is 0, the request comes from an old policy manager and we should load
293a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // well known modules
294a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    if (module == 0) {
295a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
296a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
297a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            loadHwModule_l(audio_interfaces[i]);
298a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
299f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        // then try to find a module supporting the requested device.
300f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
301f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
302f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            audio_hw_device_t *dev = audioHwDevice->hwDevice();
303f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            if ((dev->get_supported_devices != NULL) &&
304f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    (dev->get_supported_devices(dev) & devices) == devices)
305f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                return audioHwDevice;
306f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        }
307a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    } else {
308a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        // check a match for the requested module handle
309ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
310ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (audioHwDevice != NULL) {
311ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            return audioHwDevice;
312a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
313a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    }
314a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
315799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return NULL;
316799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3180f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("Clients:\n");
32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < mClients.size(); ++i) {
32677c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        sp<Client> client = mClients.valueAt(i).promote();
32777c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        if (client != 0) {
32877c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            snprintf(buffer, SIZE, "  pid: %d\n", client->pid());
32977c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            result.append(buffer);
33065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
33165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
3323a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
333d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    result.append("Notification Clients:\n");
334d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); ++i) {
335d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        snprintf(buffer, SIZE, "  pid: %d\n", mNotificationClients.keyAt(i));
336d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        result.append(buffer);
337d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
338d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
3393a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    result.append("Global session refs:\n");
340b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    result.append("  session   pid count\n");
3413a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
3423a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *r = mAudioSessionRefs[i];
343b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
3443a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        result.append(buffer);
3453a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3500f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
355a4454b4765c5905f14186893b0688be375642283Glenn Kasten    hardware_call_state hardwareStatus = mHardwareStatus;
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3574ff14bae91075eb274eb1c2975982358946e7e63John Grossman    snprintf(buffer, SIZE, "Hardware status: %d\n"
3584ff14bae91075eb274eb1c2975982358946e7e63John Grossman                           "Standby Time mSec: %u\n",
3594ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            hardwareStatus,
3604ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            (uint32_t)(mStandbyTimeInNsecs / 1000000));
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3650f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioFlinger from pid=%d, uid=%d\n",
37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
37565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex)
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3867dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten        usleep(kDumpLockSleepUs);
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args)
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
39344deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd, args);
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // get state of hardware lock
39781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool hardwareLocked = dumpTryLock(mHardwareLock);
39865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!hardwareLocked) {
39965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kHardwareLockedString);
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
40265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mHardwareLock.unlock();
40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool locked = dumpTryLock(mLock);
40665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // failed to lock - AudioFlinger is probably deadlocked
40865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
40965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
41065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
41165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
41265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
413021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        bool clientLocked = dumpTryLock(mClientLock);
414021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (!clientLocked) {
415021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            String8 result(kClientLockedString);
416021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            write(fd, result.string(), result.size());
417021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
418d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen
419d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen        EffectDumpEffects(fd);
420d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen
42165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpClients(fd, args);
422021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (clientLocked) {
423021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mClientLock.unlock();
424021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
425021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent
42665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd, args);
42765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump playback threads
42965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
43065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mPlaybackThreads.valueAt(i)->dump(fd, args);
43165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
43265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
43365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump record threads
43465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
43565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mRecordThreads.valueAt(i)->dump(fd, args);
43665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
438aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        // dump orphan effect chains
439aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        if (mOrphanEffectChains.size() != 0) {
440aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            write(fd, "  Orphan Effect Chains\n", strlen("  Orphan Effect Chains\n"));
441aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
442aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                mOrphanEffectChains.valueAt(i)->dump(fd, args);
443aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
444aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
445799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        // dump all hardware devs
446799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
447a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
448799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            dev->dump(dev, fd);
44965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
450d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
45146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
452d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        // dump the serially shared record tee sink
453d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        if (mRecordTeeSource != 0) {
454d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten            dumpTee(fd, mRecordTeeSource);
455d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        }
45646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
457d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
458d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        if (locked) {
459d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            mLock.unlock();
460d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
4619e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4629e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // append a copy of media.log here by forwarding fd to it, but don't attempt
4639e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // to lookup the service if it's not running, as it will block for a second
4649e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        if (mLogMemoryDealer != 0) {
4659e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
4669e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (binder != 0) {
4678b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "\nmedia.log:\n");
4689e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                Vector<String16> args;
4699e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                binder->dump(fd, args);
4709e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
4719e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        }
47235fec5f61393124c9e13958941637b8fe386385eAndy Hung
47335fec5f61393124c9e13958941637b8fe386385eAndy Hung        // check for optional arguments
47407b745e166ee62030960ccea37e117caadf71c32Andy Hung        bool dumpMem = false;
47535fec5f61393124c9e13958941637b8fe386385eAndy Hung        bool unreachableMemory = false;
47635fec5f61393124c9e13958941637b8fe386385eAndy Hung        for (const auto &arg : args) {
47707b745e166ee62030960ccea37e117caadf71c32Andy Hung            if (arg == String16("-m")) {
47807b745e166ee62030960ccea37e117caadf71c32Andy Hung                dumpMem = true;
47907b745e166ee62030960ccea37e117caadf71c32Andy Hung            } else if (arg == String16("--unreachable")) {
48035fec5f61393124c9e13958941637b8fe386385eAndy Hung                unreachableMemory = true;
48135fec5f61393124c9e13958941637b8fe386385eAndy Hung            }
48235fec5f61393124c9e13958941637b8fe386385eAndy Hung        }
48335fec5f61393124c9e13958941637b8fe386385eAndy Hung
48407b745e166ee62030960ccea37e117caadf71c32Andy Hung        if (dumpMem) {
48507b745e166ee62030960ccea37e117caadf71c32Andy Hung            dprintf(fd, "\nDumping memory:\n");
48607b745e166ee62030960ccea37e117caadf71c32Andy Hung            std::string s = dumpMemoryAddresses(100 /* limit */);
48707b745e166ee62030960ccea37e117caadf71c32Andy Hung            write(fd, s.c_str(), s.size());
48807b745e166ee62030960ccea37e117caadf71c32Andy Hung        }
48935fec5f61393124c9e13958941637b8fe386385eAndy Hung        if (unreachableMemory) {
49035fec5f61393124c9e13958941637b8fe386385eAndy Hung            dprintf(fd, "\nDumping unreachable memory:\n");
49135fec5f61393124c9e13958941637b8fe386385eAndy Hung            // TODO - should limit be an argument parameter?
49207b745e166ee62030960ccea37e117caadf71c32Andy Hung            std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
49335fec5f61393124c9e13958941637b8fe386385eAndy Hung            write(fd, s.c_str(), s.size());
49435fec5f61393124c9e13958941637b8fe386385eAndy Hung        }
49565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
49665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
49765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
49865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
499021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentsp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
50098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{
501021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _cl(mClientLock);
50298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // If pid is already in the mClients wp<> map, then use that entry
50398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // (for which promote() is always != 0), otherwise create a new entry and Client.
50498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    sp<Client> client = mClients.valueFor(pid).promote();
50598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    if (client == 0) {
50698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = new Client(this, pid);
50798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        mClients.add(pid, client);
50898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    }
50998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten
51098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    return client;
51198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten}
51265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5139e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
5149e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
515481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If there is no memory allocated for logs, return a dummy writer that does nothing
5169e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (mLogMemoryDealer == 0) {
5179e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return new NBLog::Writer();
5189e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
5199e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
520481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Similarly if we can't contact the media.log service, also return a dummy writer
521481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder == 0) {
522481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
5239e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
524481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
525481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
526481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If allocation fails, consult the vector of previously unregistered writers
527481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // and garbage-collect one or more them until an allocation succeeds
528481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (shared == 0) {
529481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        Mutex::Autolock _l(mUnregisteredWritersLock);
530481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
531481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            {
532481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Pick the oldest stale writer to garbage-collect
533481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
534481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mUnregisteredWriters.removeAt(0);
535481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mediaLogService->unregisterWriter(iMemory);
536481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Now the media.log remote reference to IMemory is gone.  When our last local
537481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // reference to IMemory also drops to zero at end of this block,
538481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // the IMemory destructor will deallocate the region from mLogMemoryDealer.
539481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
540481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            // Re-attempt the allocation
541481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
542481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            if (shared != 0) {
543481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                goto success;
544481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
545481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
546481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // Even after garbage-collecting all old writers, there is still not enough memory,
547481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // so return a dummy writer
548481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
549481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
550481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kastensuccess:
551481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mediaLogService->registerWriter(shared, size, name);
552481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    return new NBLog::Writer(size, shared);
5539e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5549e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
5559e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
5569e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
557685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    if (writer == 0) {
558685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten        return;
559685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    }
5609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IMemory> iMemory(writer->getIMemory());
5619e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (iMemory == 0) {
5629e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return;
5639e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
564481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Rather than removing the writer immediately, append it to a queue of old writers to
565481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // be garbage-collected later.  This allows us to continue to view old logs for a while.
566481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Mutex::Autolock _l(mUnregisteredWritersLock);
567481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mUnregisteredWriters.push(writer);
5689e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5699e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
57065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
57365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack(
574fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
57565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t sampleRate,
57658f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
577254af180475346b6186b49c297f340c9c4817511Glenn Kasten        audio_channel_mask_t channelMask,
57874935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
579050677873c10d4da308ac222f8533c96cca3207eEric Laurent        audio_output_flags_t *flags,
58065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const sp<IMemory>& sharedBuffer,
58172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output,
5829ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        pid_t pid,
5833acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        pid_t tid,
584d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten        audio_session_t *sessionId,
585462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int clientUid,
58665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status_t *status)
58765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
58865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<PlaybackThread::Track> track;
58965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<TrackHandle> trackHandle;
59065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<Client> client;
59165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t lStatus;
592d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten    audio_session_t lSessionId;
59365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5949ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George    const uid_t callingUid = IPCThreadState::self()->getCallingUid();
5959ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George    if (pid == -1 || !isTrustedCallingUid(callingUid)) {
5969ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        const pid_t callingPid = IPCThreadState::self()->getCallingPid();
5979ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        ALOGW_IF(pid != -1 && pid != callingPid,
5989ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George                 "%s uid %d pid %d tried to pass itself off as pid %d",
5999ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George                 __func__, callingUid, callingPid, pid);
6009ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        pid = callingPid;
6019ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George    }
6029ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George
603263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
604263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // but if someone uses binder directly they could bypass that and cause us to crash
605263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
60629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("createTrack() invalid stream type %d", streamType);
60765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        lStatus = BAD_VALUE;
60865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        goto Exit;
60965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createTrack_l() depending on the thread type
61253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
61353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid sample rate %u", sampleRate);
61453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
61553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
61653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
61753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
61853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createTrack_l() depending on the thread type
61953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_output_channel(channelMask)) {
62053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid channel mask %#x", channelMask);
62153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
62253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
62353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
62453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
625c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    // further format checks are performed by createTrack_l() depending on the thread type
626c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    if (!audio_is_valid_format(format)) {
627cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("createTrack() invalid format %#x", format);
62860a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        lStatus = BAD_VALUE;
629663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        goto Exit;
630663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    }
631663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten
632663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
633663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
634663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        lStatus = BAD_VALUE;
63560a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        goto Exit;
63660a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    }
63760a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten
63865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
64165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
642c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten            ALOGE("no playback thread found for output handle %d", output);
64365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lStatus = BAD_VALUE;
64465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            goto Exit;
64565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
647021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
64865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
649e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        PlaybackThread *effectThread = NULL;
650aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
651eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
652eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                ALOGE("createTrack() invalid session ID %d", *sessionId);
653eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                lStatus = BAD_VALUE;
654eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                goto Exit;
655eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            }
656570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            lSessionId = *sessionId;
657f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // check if an effect chain with the same session ID is present on another
658f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // output thread and move it here.
659de070137f11d346fba77605bd76a44c040a618fcEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
66039e94f8f723d445447fdee0822291e664b631f60Eric Laurent                sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
66139e94f8f723d445447fdee0822291e664b631f60Eric Laurent                if (mPlaybackThreads.keyAt(i) != output) {
662570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten                    uint32_t sessions = t->hasAudioSession(lSessionId);
66339e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    if (sessions & PlaybackThread::EFFECT_SESSION) {
66439e94f8f723d445447fdee0822291e664b631f60Eric Laurent                        effectThread = t.get();
665f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent                        break;
66639e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    }
667de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
668de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
66965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
670de070137f11d346fba77605bd76a44c040a618fcEric Laurent            // if no audio session id is provided, create one here
671d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten            lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (sessionId != NULL) {
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                *sessionId = lSessionId;
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
67565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
6763856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() lSessionId: %d", lSessionId);
67765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track = thread->createTrack_l(client, streamType, sampleRate, format,
679462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
68003e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
6812fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten        // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
68239e94f8f723d445447fdee0822291e664b631f60Eric Laurent
68339e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // move effect chain to this output thread if an effect on same session was waiting
68439e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // for a track to be created
68539e94f8f723d445447fdee0822291e664b631f60Eric Laurent        if (lStatus == NO_ERROR && effectThread != NULL) {
6862fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten            // no risk of deadlock because AudioFlinger::mLock is held
68739e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _dl(thread->mLock);
68839e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _sl(effectThread->mLock);
68939e94f8f723d445447fdee0822291e664b631f60Eric Laurent            moveEffectChain_l(lSessionId, effectThread, thread, true);
69039e94f8f723d445447fdee0822291e664b631f60Eric Laurent        }
691a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
692a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        // Look for sync events awaiting for a session to be used.
6933ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn        for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
694a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
695a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
6962986460984580833161bdaabc7f17da1005a8961Eric Laurent                    if (lStatus == NO_ERROR) {
697d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten                        (void) track->setSyncEvent(mPendingSyncEvents[i]);
6982986460984580833161bdaabc7f17da1005a8961Eric Laurent                    } else {
6992986460984580833161bdaabc7f17da1005a8961Eric Laurent                        mPendingSyncEvents[i]->cancel();
7002986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
701a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    mPendingSyncEvents.removeAt(i);
702a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    i--;
703a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                }
704a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            }
705a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
706e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
707d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten        setAudioHwSyncForSession_l(thread, lSessionId);
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
709e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
7103ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    if (lStatus != NO_ERROR) {
7113ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        // remove local strong reference to Client before deleting the Track so that the
712021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
713fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // Don't hold mClientLock when releasing the reference on the track as the
714fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // destructor will acquire it.
715fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        {
716fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
717fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
718fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track.clear();
7203ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        goto Exit;
72165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7233ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    // return handle to client
7243ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    trackHandle = new TrackHandle(track);
7253ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten
72665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit:
7279156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
72865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return trackHandle;
72965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7312c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
73365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
7342c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    ThreadBase *thread = checkThread_l(ioHandle);
73565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
7362c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten        ALOGW("sampleRate() unknown thread %d", ioHandle);
73765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
73865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
73965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->sampleRate();
74065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
74165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(audio_io_handle_t output) const
74365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
74465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
74565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
74665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
7475ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("format() unknown thread %d", output);
74858f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        return AUDIO_FORMAT_INVALID;
74965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
75065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->format();
75165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
75265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7532c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kastensize_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
75465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
7562c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten    ThreadBase *thread = checkThread_l(ioHandle);
75765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
7582c073da0f02c3cf7cd4795af2d861222cbcab72aGlenn Kasten        ALOGW("frameCount() unknown thread %d", ioHandle);
75965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
76065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
76158912562617941964939a4182cda71eaeb153d4bGlenn Kasten    // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
76258912562617941964939a4182cda71eaeb153d4bGlenn Kasten    //       should examine all callers and fix them to handle smaller counts
76365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->frameCount();
76465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7664a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kastensize_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const
7674a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten{
7684a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    Mutex::Autolock _l(mLock);
7694a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    ThreadBase *thread = checkThread_l(ioHandle);
7704a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    if (thread == NULL) {
7714a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten        ALOGW("frameCountHAL() unknown thread %d", ioHandle);
7724a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten        return 0;
7734a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    }
7744a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten    return thread->frameCountHAL();
7754a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten}
7764a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
77772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const
77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
77965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
78165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
782c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten        ALOGW("latency(): no playback thread found for output handle %d", output);
78365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
78465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
78565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->latency();
78665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
78765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value)
78965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
790a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
791a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
792a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
793a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
794a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
79565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
79665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
79765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
79865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
79965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
800a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
801ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterVolume = value;
802a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
803ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master volume in the HALs which support it.
804ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
805ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
806ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
8074ff14bae91075eb274eb1c2975982358946e7e63John Grossman
808ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
809ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterVolume()) {
810ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
811935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent        }
812ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
81365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
81465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
815ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master volume in each playback thread.  Playback threads
816ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master volume support will apply
817ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // master volume during the mix operation.  Threads with HALs which do
818ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // support master volume will simply ignore the setting.
819f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
820f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
821f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent            continue;
822f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        }
823ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterVolume(value);
824f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    }
82565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
82765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
82865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
829f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode)
83065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
831a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
832a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
833a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
834a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
83565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
83665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
83765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
83865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
83965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
840930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(mode) >= AUDIO_MODE_CNT) {
8415ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("Illegal value: setMode(%d)", mode);
84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
84365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
84465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    { // scope for the lock
84665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex lock(mHardwareLock);
847ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
84865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_SET_MODE;
849ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        ret = dev->set_mode(dev, mode);
85065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_IDLE;
85165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
85265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (NO_ERROR == ret) {
85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mMode = mode;
8568d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++)
857e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setMode(mode);
85865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
85965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
86165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
86265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state)
86465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
865a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
866a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
867a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
868a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
869a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
87365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
87465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
8772f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
8782f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
8792f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        status_t result = dev->set_mic_mute(dev, state);
8802f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        if (result != NO_ERROR) {
8812f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent            ret = result;
8822f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        }
8832f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent    }
88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
88565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
88665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
88765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const
88965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
890a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
891a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
892a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return false;
893a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
8943e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    bool mute = true;
895fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    bool state = AUDIO_MODE_INVALID;
8962b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
89765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
8983e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
8993e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
9003e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        status_t result = dev->get_mic_mute(dev, &state);
9013e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        if (result == NO_ERROR) {
9023e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia            mute = mute && state;
9033e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        }
9043e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    }
90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
9063e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia
9073e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    return mute;
90865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
90965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
91065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted)
91165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
912d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    status_t ret = initCheck();
913d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    if (ret != NO_ERROR) {
914d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        return ret;
915d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
916d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
91765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
91865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
91965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
92065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
92165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
922ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    Mutex::Autolock _l(mLock);
923ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterMute = muted;
924d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
925ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master mute in the HALs which support it.
926ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
927ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
928ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
929d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
930ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
931ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterMute()) {
932ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
933d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        }
934ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
935d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
936d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
937ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master mute in each playback thread.  Playback threads
938ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master mute support will apply master
939ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute during the mix operation.  Threads with HALs which do support master
940ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute will simply ignore the setting.
941f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
942f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
943f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent            continue;
944f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        }
945ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterMute(muted);
946f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    }
94765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
94865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
94965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const
95265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
9539806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
9549806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterVolume_l();
95565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
95665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const
95865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
9599806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
9609806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterMute_l();
96165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
96265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9634ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const
9644ff14bae91075eb274eb1c2975982358946e7e63John Grossman{
9654ff14bae91075eb274eb1c2975982358946e7e63John Grossman    return mMasterVolume;
9664ff14bae91075eb274eb1c2975982358946e7e63John Grossman}
9674ff14bae91075eb274eb1c2975982358946e7e63John Grossman
968d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const
969d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{
970ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    return mMasterMute;
971d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman}
972d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
973223fd5c9738e9665e495904d37d4632414b68c1eEric Laurentstatus_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
974223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent{
975223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
976223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        ALOGW("setStreamVolume() invalid stream %d", stream);
977223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return BAD_VALUE;
978223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
979223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
980223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
981223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
982223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return PERMISSION_DENIED;
983223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
984223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
985223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    return NO_ERROR;
986223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent}
987223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
98872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
98972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
99065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
99165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
99265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
99365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
99465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
99565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
996223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
997223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
998223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return status;
99965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
1000223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
100165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
100265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
100365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = NULL;
1004142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
100565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(output);
100665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
100765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return BAD_VALUE;
100865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
100965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
101165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].volume = value;
101265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
101365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
10148d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1015e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
101665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
101765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
101865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread->setStreamVolume(stream, value);
101965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
102065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
102165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
102265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
102365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1024fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
102565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
102665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
102765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
102865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
102965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
103065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1031223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
1032223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
1033223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return status;
1034223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
1035223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
1036223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
1037223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
103829357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamMute() invalid stream %d", stream);
103965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
104065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
104165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1042935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    AutoMutex lock(mLock);
104365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].mute = muted;
10443ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
1045e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
104665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
104765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
104865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
104965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
105072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
105165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1052223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
1053223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
105465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0.0f;
105565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
105665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
105765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
105865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    float volume;
1059142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
106065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
106165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
106265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return 0.0f;
106365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
106465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        volume = thread->streamVolume(stream);
106565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
10666637baae4244aec731c4014da72418d330636ae1Glenn Kasten        volume = streamVolume_l(stream);
106765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
106865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
106965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return volume;
107065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
107165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1072fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const
107365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1074223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
1075223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
107665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return true;
107765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
107865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
10796637baae4244aec731c4014da72418d330636ae1Glenn Kasten    AutoMutex lock(mLock);
10806637baae4244aec731c4014da72418d330636ae1Glenn Kasten    return streamMute_l(stream);
108165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
108265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1083054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
1084054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurentvoid AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1085054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent{
1086054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
1087054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1088054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
1089054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent}
1090054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
109172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
109265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1093827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1094827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
109665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
109765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
109865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
109965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
110065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1101142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1102142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
1103a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        Mutex::Autolock _l(mLock);
1104799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        status_t final_result = NO_ERROR;
11058abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        {
1106a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            AutoMutex lock(mHardwareLock);
1107a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1108a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1109a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1110a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                status_t result = dev->set_parameters(dev, keyValuePairs.string());
1111a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                final_result = result ?: final_result;
1112a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            }
1113a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
11148abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        }
111559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
111659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        AudioParameter param = AudioParameter(keyValuePairs);
111759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        String8 value;
111859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
1119bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1120bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            if (mBtNrecIsOff != btNrecIsOff) {
112159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
112259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    sp<RecordThread> thread = mRecordThreads.valueAt(i);
1123f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    audio_devices_t device = thread->inDevice();
1124510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1125510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // collect all of the thread's session IDs
1126d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                    KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
1127510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // suspend effects associated with those session IDs
1128510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    for (size_t j = 0; j < ids.size(); ++j) {
1129d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                        audio_session_t sessionId = ids.keyAt(j);
113059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_AEC,
113159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1132510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
113359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_NS,
113459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1135510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
113659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    }
113759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                }
1138bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent                mBtNrecIsOff = btNrecIsOff;
113959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent            }
114059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        }
114128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        String8 screenState;
114228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
114328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            bool isOff = screenState == "off";
114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isOff != (AudioFlinger::mScreenState & 1)) {
114581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
114628ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            }
114728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        }
1148799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        return final_result;
114965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
115065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
115165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // hold a strong ref on thread in case closeOutput() or closeInput() is called
115265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // and the thread is exited once the lock is released
115365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ThreadBase> thread;
115465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
115565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
115665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(ioHandle);
1157d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten        if (thread == 0) {
115865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            thread = checkRecordThread_l(ioHandle);
11597fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten        } else if (thread == primaryPlaybackThread_l()) {
11607c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // indicate output device change to all input threads for pre processing
11617c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            AudioParameter param = AudioParameter(keyValuePairs);
11627c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int value;
116389d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent            if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
116489d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent                    (value != 0)) {
1165054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                broacastParametersToRecordThreads_l(keyValuePairs);
11667c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
116765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
116865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
11697378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (thread != 0) {
11707378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten        return thread->setParameters(keyValuePairs);
117165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
117265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
117365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
117465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
117572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
117665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1177827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1178827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
117965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1180a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
1181a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
1182142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
1183fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        String8 out_s8;
1184fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1185799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
11868abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            char *s;
11878abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            {
11888abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            AutoMutex lock(mHardwareLock);
11898abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_GET_PARAMETER;
1190a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
11918abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            s = dev->get_parameters(dev, keys.string());
11928abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_IDLE;
11938abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            }
1194ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman            out_s8 += String8(s ? s : "");
1195799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            free(s);
1196799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        }
1197fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return out_s8;
119865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
119965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
120065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
120165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
120265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getParameters(keys);
120365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
120465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
120565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
120665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getParameters(keys);
120765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
120865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return String8("");
120965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
121065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1211dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1212dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask) const
121365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1214a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1215a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1216a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return 0;
1217a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1218cdf924a32579e92ea456e8e7d5c14ed5920b620fEric Laurent    if ((sampleRate == 0) ||
1219fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk            !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
1220cdf924a32579e92ea456e8e7d5c14ed5920b620fEric Laurent            !audio_is_input_channel(channelMask)) {
12216770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        return 0;
12226770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
1223a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
12242b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
12252b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
12266770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    audio_config_t config, proposed;
12276770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    memset(&proposed, 0, sizeof(proposed));
12286770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.sample_rate = sampleRate;
12296770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.channel_mask = channelMask;
12306770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.format = format;
1231ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1232ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
12336770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    size_t frames;
12346770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    for (;;) {
12356770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // Note: config is currently a const parameter for get_input_buffer_size()
12366770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // but we use a copy from proposed in case config changes from the call.
12376770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        config = proposed;
12386770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        frames = dev->get_input_buffer_size(dev, &config);
12396770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        if (frames != 0) {
12406770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            break; // hal success, config is the result
12416770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        }
12426770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // change one parameter of the configuration each iteration to a more "common" value
12436770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // to see if the device will support it.
12446770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
12456770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            proposed.format = AUDIO_FORMAT_PCM_16_BIT;
12466770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
12476770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            proposed.sample_rate = 44100;           // legacy AudioRecord.java. TODO: Query hw?
12486770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        } else {
12496770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
12506770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                    "format %#x, channelMask 0x%X",
12516770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                    sampleRate, format, channelMask);
12526770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            break; // retries failed, break out of loop with frames == 0.
12536770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        }
12546770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
12552b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_IDLE;
12566770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    if (frames > 0 && config.sample_rate != sampleRate) {
12576770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
12586770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
12596770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    return frames; // may be converted to bytes at the Java level.
126065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
126165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
12625f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
126365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
126465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
126565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
126665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
126765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
126865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getInputFramesLost();
126965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
127065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return 0;
127165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
127265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
127365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value)
127465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1275a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1276a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1277a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
1278a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1279a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
128065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
128165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
128265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
128365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
128465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
128565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
1286ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
12878abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten    mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
1288ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_voice_volume(dev, value);
128965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
129065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
129165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
129265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
129365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1294377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
129572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output) const
129665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
129765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
129865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
129965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(output);
130065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
130165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getRenderPosition(halFrames, dspFrames);
130265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
130365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
130465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
130565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
130665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
130765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
130865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
130965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
131044622db93a24219123facbbe14259000f90de728Eric Laurent    if (client == 0) {
131144622db93a24219123facbbe14259000f90de728Eric Laurent        return;
131244622db93a24219123facbbe14259000f90de728Eric Laurent    }
13137c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    pid_t pid = IPCThreadState::self()->getCallingPid();
1314021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1315021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
1316021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(pid) < 0) {
1317021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            sp<NotificationClient> notificationClient = new NotificationClient(this,
1318021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                client,
1319021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                pid);
1320021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
132165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1322021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mNotificationClients.add(pid, notificationClient);
132365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
132406b46062d2f8bc82ca3061a23d197734ae51918bMarco Nelissen            sp<IBinder> binder = IInterface::asBinder(client);
1325021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            binder->linkToDeath(notificationClient);
1326021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
1327021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
132865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1329021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
1330fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent    // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
13317c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    // the config change is always sent from playback or record threads to avoid deadlock
13327c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    // with AudioSystem::gLock
13337c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
13347c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
13357c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    }
133665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
13377c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
13387c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
133965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
134065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
134165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
134265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid)
134365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
134465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
1345021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1346021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
1347021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        mNotificationClients.removeItem(pid);
1348021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
13493a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
13503856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("%d died, releasing its sessions", pid);
13518d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    size_t num = mAudioSessionRefs.size();
13523a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    bool removed = false;
13538d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i< num; ) {
13543a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1355c42e9b462661673dff480ee71757a58b0f806370Glenn Kasten        ALOGV(" pid %d @ %zu", ref->mPid, i);
1356012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        if (ref->mPid == pid) {
1357012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten            ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
13583a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            mAudioSessionRefs.removeAt(i);
13593a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            delete ref;
13603a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            removed = true;
13613a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            num--;
13628d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        } else {
13638d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten            i++;
13643a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        }
13653a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
13663a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (removed) {
13673a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        purgeStaleEffects_l();
13683a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
136965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
137065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
137173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::ioConfigChanged(audio_io_config_event event,
13727c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                                   const sp<AudioIoDescriptor>& ioDesc,
13737c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                                   pid_t pid)
137465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1375021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _l(mClientLock);
1376021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    size_t size = mNotificationClients.size();
137765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < size; i++) {
13787c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
13797c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent            mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
13807c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        }
138165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
138265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
138365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1384021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// removeClient_l() must be called with AudioFlinger::mClientLock held
138565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid)
138665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1387827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("removeClient_l() pid %d, calling pid %d", pid,
138885ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            IPCThreadState::self()->getCallingPid());
138965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mClients.removeItem(pid);
139065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
139165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1392717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held
1393d848eb48c121c119e8ba7583efc75415fe102570Glenn Kastensp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1394d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten        int EffectId)
1395717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{
1396717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    sp<PlaybackThread> thread;
1397717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1398717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1399717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1400717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            ALOG_ASSERT(thread == 0);
1401717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            thread = mPlaybackThreads.valueAt(i);
1402717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        }
1403717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    }
1404717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1405717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    return thread;
1406717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent}
140765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
140981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
141065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
141165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
141281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   RefBase(),
141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger(audioFlinger),
1415d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten        mPid(pid)
141665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1417da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    size_t heapSize = kClientSharedHeapSizeBytes;
1418da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    // Increase heap size on non low ram devices to limit risk of reconnection failure for
1419da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    // invalidated tracks
1420da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    if (!audioFlinger->isLowRamDevice()) {
1421da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent        heapSize *= kClientSharedHeapSizeMultiplier;
1422da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    }
1423da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
142465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
142565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1426021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// Client destructor must be called with AudioFlinger::mClientLock held
142781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client()
142865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeClient_l(mPid);
143065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
143165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
143281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const
143365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mMemoryDealer;
143565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
143665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
143981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     const sp<IAudioFlingerClient>& client,
144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     pid_t pid)
144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
1443896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{
144465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
144565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
144681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient()
144765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
144865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
144965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
14500f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
145165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NotificationClient> keep(this);
145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeNotificationClient(mPid);
145481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
145565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
145665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
145865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord(
146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t input,
146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
146281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
1464be71aa29a3c86d2e01cd17839d2a72ab09a1bce5Svet Ganov        const String16& opPackageName,
146574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
1466050677873c10d4da308ac222f8533c96cca3207eEric Laurent        audio_input_flags_t *flags,
14679ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        pid_t pid,
146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
14694cb668392ee0433462251afbee109405c6efacc8Jean-Michel Trivi        int clientUid,
1470d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten        audio_session_t *sessionId,
14717df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten        size_t *notificationFrames,
1472d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& cblk,
1473d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& buffers,
147481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
14751d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{
147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread::RecordTrack> recordTrack;
147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordHandle> recordHandle;
147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Client> client;
147981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
1480d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten    audio_session_t lSessionId;
14811d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1482d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk.clear();
1483d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers.clear();
1484d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
14859ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George    bool updatePid = (pid == -1);
1486dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen    const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1487dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen    if (!isTrustedCallingUid(callingUid)) {
1488879db19219c120d424f7d7ad7164c3cfe56124e4Andy Hung        ALOGW_IF((uid_t)clientUid != callingUid,
1489dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen                "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1490dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen        clientUid = callingUid;
14919ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        updatePid = true;
14929ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George    }
14939ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George
14949ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George    if (updatePid) {
14959ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        const pid_t callingPid = IPCThreadState::self()->getCallingPid();
14969ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        ALOGW_IF(pid != -1 && pid != callingPid,
14979ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George                 "%s uid %d pid %d tried to pass itself off as pid %d",
14989ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George                 __func__, callingUid, callingPid, pid);
14999ea77cdba8c422940adb57a790b44ac04fe0353fHaynes Mathew George        pid = callingPid;
1500dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen    }
1501dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen
150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check calling permissions
1503dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen    if (!recordingAllowed(opPackageName, tid, clientUid)) {
1504e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten        ALOGE("openRecord() permission denied: recording not allowed");
150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
15081d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
150953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createRecordTrack_l()
151053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
151153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid sample rate %u", sampleRate);
151253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
151353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
151453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
151553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
15166770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    // we don't yet support anything other than linear PCM
15176770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
1518cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("openRecord() invalid format %#x", format);
1519291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        lStatus = BAD_VALUE;
152053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
152153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
152253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
152353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createRecordTrack_l()
152453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_input_channel(channelMask)) {
152553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid channel mask %#x", channelMask);
152653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
1527291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        goto Exit;
1528291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
1529291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten
153005997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten    {
153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
1532e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        RecordThread *thread = checkRecordThread_l(input);
153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
1534e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() checkRecordThread_l failed");
153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
15371d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent        }
15381d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1539021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
1540feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1541aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
1542eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1543eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                lStatus = BAD_VALUE;
1544eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                goto Exit;
1545eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            }
154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = *sessionId;
1547feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        } else {
1548570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            // if no audio session id is provided, create one here
1549d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten            lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
155081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId != NULL) {
155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                *sessionId = lSessionId;
155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1553feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        }
1554aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
1555570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten
155681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
15577df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten                                                  frameCount, lSessionId, notificationFrames,
15584cb668392ee0433462251afbee109405c6efacc8Jean-Michel Trivi                                                  clientUid, flags, tid, &lStatus);
155903e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
15601b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
15611b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        if (lStatus == NO_ERROR) {
15621b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // Check if one effect chain was awaiting for an AudioRecord to be created on this
15631b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // session and move it to this thread.
1564d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten            sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
15651b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (chain != 0) {
15661b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                Mutex::Autolock _l(thread->mLock);
15671b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                thread->addEffectChain_l(chain);
15681b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
15691b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        }
1570feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1571e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove local strong reference to Client before deleting the RecordTrack so that the
1574021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
1575fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // Don't hold mClientLock when releasing the reference on the track as the
1576fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // destructor will acquire it.
1577fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        {
1578fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
1579fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
1580fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack.clear();
158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
1583feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1584feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1585d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk = recordTrack->getCblk();
1586d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers = recordTrack->getBuffers();
1587d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
15882fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten    // return handle to client
158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordHandle = new RecordHandle(recordTrack);
1590feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
159181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
15929156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return recordHandle;
1594feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent}
1595feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1596feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
15971d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name)
160159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
160244622db93a24219123facbbe14259000f90de728Eric Laurent    if (name == NULL) {
1603a13cde98a880341f0a56d91da6364b093fb5d24eGlenn Kasten        return AUDIO_MODULE_HANDLE_NONE;
160444622db93a24219123facbbe14259000f90de728Eric Laurent    }
160581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!settingsAllowed()) {
1606a13cde98a880341f0a56d91da6364b093fb5d24eGlenn Kasten        return AUDIO_MODULE_HANDLE_NONE;
160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
160859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return loadHwModule_l(name);
161059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
161159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held
161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
161459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("loadHwModule() module %s already loaded", name);
161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mAudioHwDevs.keyAt(i);
161959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
162059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
162159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *dev;
162359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
162481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int rc = load_audio_interface(name, &dev);
162581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
1626c42e9b462661673dff480ee71757a58b0f806370Glenn Kasten        ALOGE("loadHwModule() error %d loading module %s", rc, name);
1627a13cde98a880341f0a56d91da6364b093fb5d24eGlenn Kasten        return AUDIO_MODULE_HANDLE_NONE;
162859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
162959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
163081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_INIT;
163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    rc = dev->init_check(dev);
163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
1634c42e9b462661673dff480ee71757a58b0f806370Glenn Kasten        ALOGE("loadHwModule() init check error %d for module %s", rc, name);
1635a13cde98a880341f0a56d91da6364b093fb5d24eGlenn Kasten        return AUDIO_MODULE_HANDLE_NONE;
163659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
163759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Check and cache this HAL's level of support for master mute and master
163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volume.  If this is the first HAL opened, and it supports the get
164081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // methods, use the initial values provided by the HAL as the current
164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // master mute and volume settings.
164259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
164381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {  // scope for auto-lock pattern
164581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mHardwareLock);
164659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
164781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (0 == mAudioHwDevs.size()) {
164881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
164981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_volume) {
165081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float mv;
165181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_volume(dev, &mv)) {
165281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterVolume = mv;
165381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
165481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
165581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
165681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
165781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_mute) {
165881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool mm;
165981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_mute(dev, &mm)) {
166081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterMute = mm;
166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
166281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
166359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
166481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
166581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
166681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_volume) &&
166781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_volume(dev, mMasterVolume))) {
166881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
166981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
167059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
167159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
167281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
167381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_mute) &&
167481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_mute(dev, mMasterMute))) {
167581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
167681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
167781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
167859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
167981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_IDLE;
168059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
168159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
1682a13cde98a880341f0a56d91da6364b093fb5d24eGlenn Kasten    audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
168383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
168481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
168581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
168681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent          name, dev->common.module->name, dev->common.module->id, handle);
168781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
168881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
168981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
169059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
169159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
169281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate()
169559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
169659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
169881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->sampleRate() : 0;
1699a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent}
170059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
170181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount()
1702a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{
170381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
170481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
170581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->frameCountHAL() : 0;
170659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
170759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
170865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
170965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17104182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
17114182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{
17124182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    uid_t uid = IPCThreadState::self()->getCallingUid();
17134182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (uid != AID_SYSTEM) {
17144182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return PERMISSION_DENIED;
17154182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
17164182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    Mutex::Autolock _l(mLock);
17174182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (mIsDeviceTypeKnown) {
17184182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return INVALID_OPERATION;
17194182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
17204182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsLowRamDevice = isLowRamDevice;
17214182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsDeviceTypeKnown = true;
17224182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    return NO_ERROR;
17234182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten}
17244182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
172593c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurentaudio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
172693c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent{
172793c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    Mutex::Autolock _l(mLock);
1728fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1729fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1730fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (index >= 0) {
1731fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1732fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent              mHwAvSyncIds.valueAt(index), sessionId);
1733fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return mHwAvSyncIds.valueAt(index);
1734fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1735fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1736fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1737fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (dev == NULL) {
1738fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return AUDIO_HW_SYNC_INVALID;
1739fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1740fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1741fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    AudioParameter param = AudioParameter(String8(reply));
1742fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    free(reply);
1743fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1744fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    int value;
1745fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1746fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1747fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return AUDIO_HW_SYNC_INVALID;
1748fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1749fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1750fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    // allow only one session for a given HW A/V sync ID.
1751fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1752fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1753fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1754fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent                  value, mHwAvSyncIds.keyAt(i));
1755fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            mHwAvSyncIds.removeItemsAt(i);
1756fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            break;
1757fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        }
1758fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1759fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1760fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    mHwAvSyncIds.add(sessionId, value);
1761fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
176293c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
176393c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1764fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        uint32_t sessions = thread->hasAudioSession(sessionId);
1765fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        if (sessions & PlaybackThread::TRACK_SESSION) {
1766fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            AudioParameter param = AudioParameter();
1767fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1768fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            thread->setParameters(param.toString());
176993c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            break;
177093c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        }
177193c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    }
1772fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1773fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1774fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    return (audio_hw_sync_t)value;
1775fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent}
1776fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
177772e3f39146fce4686bd96f11057c051bea376dfbEric Laurentstatus_t AudioFlinger::systemReady()
177872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent{
177972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    Mutex::Autolock _l(mLock);
178072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    ALOGI("%s", __FUNCTION__);
178172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    if (mSystemReady) {
178272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ALOGW("%s called twice", __FUNCTION__);
178372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        return NO_ERROR;
178472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
178572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    mSystemReady = true;
178672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
178772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
178872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        thread->systemReady();
178972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
179072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
179172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
179272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        thread->systemReady();
179372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
179472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    return NO_ERROR;
179572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent}
179672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
1797fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1798fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurentvoid AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1799fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent{
1800fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1801fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (index >= 0) {
1802fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1803fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1804fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        AudioParameter param = AudioParameter();
1805fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1806fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        thread->setParameters(param.toString());
1807fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
180893c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent}
180993c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent
1810fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
18114182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ----------------------------------------------------------------------------
18124182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
181365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
181483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
1815cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_io_handle_t *output,
1816cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_config_t *config,
1817cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_devices_t devices,
1818cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            const String8& address,
181983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                            audio_output_flags_t flags)
182083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
1821cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
18226e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (outHwDev == NULL) {
1823cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return 0;
18246e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
182565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1826cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*output == AUDIO_IO_HANDLE_NONE) {
1827eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1828eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    } else {
1829eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        // Audio Policy does not currently request a specific output handle.
1830eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        // If this is ever needed, see openInput_l() for example code.
1831eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1832eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        return 0;
1833cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    }
183465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
183665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
18376146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    // FOR TESTING ONLY:
18389a59276fb465e492138e0576523b54079671e8f4Andy Hung    // This if statement allows overriding the audio policy settings
18399a59276fb465e492138e0576523b54079671e8f4Andy Hung    // and forcing a specific format or channel mask to the HAL/Sink device for testing.
18409a59276fb465e492138e0576523b54079671e8f4Andy Hung    if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
18419a59276fb465e492138e0576523b54079671e8f4Andy Hung        // Check only for Normal Mixing mode
18429a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (kEnableExtendedPrecision) {
18439a59276fb465e492138e0576523b54079671e8f4Andy Hung            // Specify format (uncomment one below to choose)
18449a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_FLOAT;
18459a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
18469a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_32_BIT;
18479a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
1848cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
18499a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
18509a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (kEnableExtendedChannels) {
18519a59276fb465e492138e0576523b54079671e8f4Andy Hung            // Specify channel mask (uncomment one below to choose)
18529a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->channel_mask = audio_channel_out_mask_from_count(4);  // for USB 4ch
18539a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->channel_mask = audio_channel_mask_from_representation_and_bits(
18549a59276fb465e492138e0576523b54079671e8f4Andy Hung            //        AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1);  // another 4ch example
18559a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
18566146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    }
18576146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung
1858062e67a26e0553dd142be622821f493df541f0c6Phil Burk    AudioStreamOut *outputStream = NULL;
1859062e67a26e0553dd142be622821f493df541f0c6Phil Burk    status_t status = outHwDev->openOutputStream(
1860062e67a26e0553dd142be622821f493df541f0c6Phil Burk            &outputStream,
1861062e67a26e0553dd142be622821f493df541f0c6Phil Burk            *output,
1862062e67a26e0553dd142be622821f493df541f0c6Phil Burk            devices,
1863062e67a26e0553dd142be622821f493df541f0c6Phil Burk            flags,
1864062e67a26e0553dd142be622821f493df541f0c6Phil Burk            config,
1865062e67a26e0553dd142be622821f493df541f0c6Phil Burk            address.string());
186665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
186858912562617941964939a4182cda71eaeb153d4bGlenn Kasten
1869062e67a26e0553dd142be622821f493df541f0c6Phil Burk    if (status == NO_ERROR) {
187065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
18713255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        PlaybackThread *thread;
1872bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1873e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent            thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
1874cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
18756146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
187683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                || !isValidPcmSinkFormat(config->format)
18779a59276fb465e492138e0576523b54079671e8f4Andy Hung                || !isValidPcmSinkChannelMask(config->channel_mask)) {
187872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
1879cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
188172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
1882cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
188365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
1884cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        mPlaybackThreads.add(*output, thread);
188583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        return thread;
188683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
188783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
188883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    return 0;
188983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
189083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1891cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurentstatus_t AudioFlinger::openOutput(audio_module_handle_t module,
1892cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_io_handle_t *output,
1893cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_config_t *config,
1894cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_devices_t *devices,
1895cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  const String8& address,
1896cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  uint32_t *latencyMs,
1897cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_output_flags_t flags)
189883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
1899062e67a26e0553dd142be622821f493df541f0c6Phil Burk    ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
190083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent              module,
1901cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              (devices != NULL) ? *devices : 0,
1902cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->sample_rate,
1903cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->format,
1904cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->channel_mask,
190583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent              flags);
190683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1907cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*devices == AUDIO_DEVICE_NONE) {
1908cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return BAD_VALUE;
190983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
191083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
191183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
191283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1913cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
191483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (thread != 0) {
1915cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *latencyMs = thread->latency();
191665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
191781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new output creation
191873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
191965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
192081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the first primary output opened designates the primary hw device
192181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
192281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGI("Using module %d has the primary audio interface", module);
192383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
192481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
192581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AutoMutex lock(mHardwareLock);
192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_SET_MODE;
192783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
192881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
192981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1930cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return NO_ERROR;
193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
193265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1933cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    return NO_INIT;
193465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
193565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
193681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t output2)
193865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
193981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread1 = checkMixerThread_l(output1);
194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread2 = checkMixerThread_l(output2);
194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread1 == NULL || thread2 == NULL) {
194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
194581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                output2);
1946142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        return AUDIO_IO_HANDLE_NONE;
194765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
194865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1949eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
195072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->addOutputTrack(thread2);
195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPlaybackThreads.add(id, thread);
195381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // notify client processes of the new output creation
195473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return id;
195665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
195765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output)
19592bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{
196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeOutput_nonvirtual(output);
19612bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi}
19622bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi
196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
196465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the playback thread so that
196681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
196781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<PlaybackThread> thread;
196881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkPlaybackThread_l(output);
197181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
197365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
197465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
197581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeOutput() %d", output);
1976de070137f11d346fba77605bd76a44c040a618fcEric Laurent
197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread->type() == ThreadBase::MIXER) {
197881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1979f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent                if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
198081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    DuplicatingThread *dupThread =
198181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
198281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    dupThread->removeOutputTrack((MixerThread *)thread.get());
1983de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
1984de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
1985de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
1986bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1987bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.removeItem(output);
1989bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // save all effects to the default thread
1990bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mPlaybackThreads.size()) {
1991bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1992bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (dstThread != NULL) {
1993bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // audioflinger lock is held here so the acquisition order of thread locks does not
1994bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // matter
1995bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _dl(dstThread->mLock);
1996bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _sl(thread->mLock);
1997bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1998bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
1999bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
2000bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
2001bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
2002bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
200373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
200473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        ioDesc->mIoHandle = output;
200573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
20063acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
200781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
200981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
20103acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
2011f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    if (!thread->isDuplicating()) {
201283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        closeOutputFinish(thread);
201365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
201483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
201581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
201665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
201765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
201883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
201983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
202083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    AudioStreamOut *out = thread->clearOutput();
202183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
202283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // from now on thread->mOutput is NULL
202383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    out->hwDev()->close_output_stream(out->hwDev(), out->stream);
202483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    delete out;
202583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
202683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
202783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
202883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
202983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mPlaybackThreads.removeItem(thread->mId);
203083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    thread->exit();
203183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeOutputFinish(thread);
203283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
203383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output)
2035e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{
203681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
203781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
203881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
2041e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent    }
2042e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
204381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("suspendOutput() %d", output);
204481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->suspend();
2045e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
204681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
204765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
204865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
204981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output)
205065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
20516637baae4244aec731c4014da72418d330636ae1Glenn Kasten    Mutex::Autolock _l(mLock);
205281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
205365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
205481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
205581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
2056ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
205765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
205881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("restoreOutput() %d", output);
205965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
206081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->restore();
206165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
206281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
206365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
206465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2065cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurentstatus_t AudioFlinger::openInput(audio_module_handle_t module,
2066cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_io_handle_t *input,
2067cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_config_t *config,
2068e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten                                          audio_devices_t *devices,
2069cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          const String8& address,
2070cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_source_t source,
2071ec40d284218466d8f0e832e7eb88e6ea6c479c88Glenn Kasten                                          audio_input_flags_t flags)
207265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
207383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
207483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
2075e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    if (*devices == AUDIO_DEVICE_NONE) {
2076cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return BAD_VALUE;
207783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
207883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
2079e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
208083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
208183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (thread != 0) {
208283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        // notify client processes of the new input creation
208373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        thread->ioConfigChanged(AUDIO_INPUT_OPENED);
2084cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return NO_ERROR;
2085b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent    }
2086cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    return NO_INIT;
208783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
2088b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent
208983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentsp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
2090cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_io_handle_t *input,
2091cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_config_t *config,
2092e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten                                                         audio_devices_t devices,
2093cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         const String8& address,
2094cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_source_t source,
209583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                         audio_input_flags_t flags)
209683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
2097e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
20986e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (inHwDev == NULL) {
2099cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *input = AUDIO_IO_HANDLE_NONE;
210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
21016e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2102fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
2103eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    // Audio Policy can request a specific handle for hardware hotword.
2104eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    // The goal here is not to re-open an already opened input.
2105eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    // It is to use a pre-assigned I/O handle.
2106cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*input == AUDIO_IO_HANDLE_NONE) {
2107eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2108eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2109eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        ALOGE("openInput_l() requested input handle %d is invalid", *input);
2110eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        return 0;
2111eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2112eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        // This should not happen in a transient state with current design.
2113eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2114eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        return 0;
2115cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    }
2116b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
2117cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_config_t halconfig = *config;
2118cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_hw_device_t *inHwHal = inHwDev->hwDevice();
21193255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_in_t *inStream = NULL;
2120e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
2121cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                        &inStream, flags, address.string(), source);
2122cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
2123fd4c14883b268a0bc5514da135fe6b7d1ce2071bJean-Michel Trivi           ", Format %#x, Channels %x, flags %#x, status %d addr %s",
212481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            inStream,
2125cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.sample_rate,
2126cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.format,
2127cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.channel_mask,
2128ec40d284218466d8f0e832e7eb88e6ea6c479c88Glenn Kasten            flags,
2129fd4c14883b268a0bc5514da135fe6b7d1ce2071bJean-Michel Trivi            status, address.string());
213065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the input could not be opened with the requested parameters and we can handle the
21326770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    // conversion internally, try to open again with the proposed parameters.
213381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == BAD_VALUE &&
21346770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        audio_is_linear_pcm(config->format) &&
21356770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        audio_is_linear_pcm(halconfig.format) &&
21366770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
2137753466603a88f1ea3d966d17ab79256519553759vivek mehta        (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) &&
2138753466603a88f1ea3d966d17ab79256519553759vivek mehta        (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) {
21398594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME describe the change proposed by HAL (save old values so we can log them here)
2140cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
214181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inStream = NULL;
2142e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten        status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
2143cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                            &inStream, flags, address.string(), source);
21448594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME log this new status; HAL should not propose any further changes
214565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
214665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
214781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && inStream != NULL) {
214858912562617941964939a4182cda71eaeb153d4bGlenn Kasten
214946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
215081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // or (re-)create if current Pipe is idle and does not match the new format
215281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<NBAIO_Sink> teeSink;
215381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        enum {
215481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NO,    // don't copy input
215581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NEW,   // copy input using a new pipe
215681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_OLD,   // copy input using an existing pipe
215781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } kind;
2158329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten        NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2159329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten                audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
2160da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (!mTeeSinkInputEnabled) {
2161da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            kind = TEE_SINK_NO;
21626e0d67d7b496ce17c0970a4ffd3a6f808860949cGlenn Kasten        } else if (!Format_isValid(format)) {
216381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink == 0) {
216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink->getStrongCount() != 1) {
216781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
2168f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten        } else if (Format_isEqual(format, mRecordTeeSink->format())) {
216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_OLD;
21704adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten        } else {
217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
217381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        switch (kind) {
217481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NEW: {
2175da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numCounterOffers = 0;
217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const NBAIO_Format offers[1] = {format};
217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
218081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            PipeReader *pipeReader = new PipeReader(*pipe);
218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            numCounterOffers = 0;
218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSink = pipe;
218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSource = pipeReader;
218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = pipe;
21874adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten            }
218881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_OLD:
219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = mRecordTeeSink;
219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NO:
219381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        default:
219481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
219558912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
219646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2197da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2198050677873c10d4da308ac222f8533c96cca3207eEric Laurent        AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags);
219965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Start record thread
220134af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten        // RecordThread requires both input and output device indication to forward to audio
220281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // pre processing modules
220383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        sp<RecordThread> thread = new RecordThread(this,
2204cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  inputStream,
2205cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  *input,
2206d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                  primaryOutputDevice_l(),
220772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                                  devices,
220872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                                  mSystemReady
220946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
221046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  , teeSink
221146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
221246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  );
2213cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        mRecordThreads.add(*input, thread);
2214cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
221583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        return thread;
22161afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent    }
221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2218cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    *input = AUDIO_IO_HANDLE_NONE;
221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
222065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
222165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
222281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input)
222365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeInput_nonvirtual(input);
222565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
222665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2228de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the record thread so that
223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread> thread;
223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
223681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
2237de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeInput() %d", input);
22401b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
22411b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // If we still have effect chains, it means that a client still holds a handle
22421b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // on at least one effect. We must either move the chain to an existing thread with the
22431b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // same session ID or put it aside in case a new record thread is opened for a
22441b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // new capture on the same session
22451b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        sp<EffectChain> chain;
2246aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        {
2247aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            Mutex::Autolock _sl(thread->mLock);
2248aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
22491b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // Note: maximum one chain per record thread
22501b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (effectChains.size() != 0) {
22511b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                chain = effectChains[0];
22521b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
22531b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        }
22541b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        if (chain != 0) {
22551b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // first check if a record thread is already opened with a client on the same session.
22561b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // This should only happen in case of overlap between one thread tear down and the
22571b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // creation of its replacement
22581b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            size_t i;
22591b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            for (i = 0; i < mRecordThreads.size(); i++) {
22601b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                sp<RecordThread> t = mRecordThreads.valueAt(i);
22611b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                if (t == thread) {
22621b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    continue;
22631b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                }
22641b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                if (t->hasAudioSession(chain->sessionId()) != 0) {
22651b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    Mutex::Autolock _l(t->mLock);
22661b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    ALOGV("closeInput() found thread %d for effect session %d",
22671b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                          t->id(), chain->sessionId());
22681b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    t->addEffectChain_l(chain);
22691b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    break;
22701b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                }
22711b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
22721b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // put the chain aside if we could not find a record thread with the same session id.
22731b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (i == mRecordThreads.size()) {
22741b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                putOrphanEffectChain_l(chain);
2275aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
2276aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
227773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
227873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        ioDesc->mIoHandle = input;
227973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.removeItem(input);
2281de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
228283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
228383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // we have a different lock for notification client
228483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeInputFinish(thread);
228583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    return NO_ERROR;
228683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
2287de070137f11d346fba77605bd76a44c040a618fcEric Laurent
228883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeInputFinish(sp<RecordThread> thread)
228983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
229083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    thread->exit();
229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *in = thread->clearInput();
229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // from now on thread->mInput is NULL
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    in->hwDev()->close_input_stream(in->hwDev(), in->stream);
229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete in;
229683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
229765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
229883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
229983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
230083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mRecordThreads.removeItem(thread->mId);
230183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeInputFinish(thread);
2302b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
2303b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
2304d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kastenstatus_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
2305b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{
2306b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    Mutex::Autolock _l(mLock);
2307d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten    ALOGV("invalidateStream() stream %d", stream);
2308b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->invalidateTracks(stream);
2312b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    }
231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
2315b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
2316b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2318eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kastenaudio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
2319162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{
23209d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten    // This is a binder API, so a malicious client could pass in a bad parameter.
23219d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten    // Check for that before calling the internal API nextUniqueId().
23229d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten    if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
23239d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten        ALOGE("newAudioUniqueId invalid use %d", use);
23249d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten        return AUDIO_UNIQUE_ID_ALLOCATE;
23259d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten    }
2326eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    return nextUniqueId(use);
2327162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent}
2328162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent
2329d848eb48c121c119e8ba7583efc75415fe102570Glenn Kastenvoid AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
2330a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{
2331a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    Mutex::Autolock _l(mLock);
233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2333d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2334d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2335d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2336d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
2337d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
2338021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
2339021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
2340021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Ignore requests received from processes not known as notification client. The request
2341021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2342021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // called from a different pid leaving a stale session reference.  Also we don't know how
2343021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // to clear this reference if the client process dies.
2344021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(caller) < 0) {
2345021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2346021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            return;
2347021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
2348d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
2349d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt++;
235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" incremented refcount to %d", ref->mCnt);
235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
2357a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
2358a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    }
235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV(" added new entry for %d", audioSession);
2361a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent}
2362a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
2363d848eb48c121c119e8ba7583efc75415fe102570Glenn Kastenvoid AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
236444a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{
236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2367d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2368d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2369d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2370d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
237281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
237581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt--;
237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" decremented refcount to %d", ref->mCnt);
237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mCnt == 0) {
237881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioSessionRefs.removeAt(i);
237981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                delete ref;
238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                purgeStaleEffects_l();
238144a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent            }
238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
238344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent        }
238444a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent    }
2385d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // If the caller is mediaserver it is likely that the session being released was acquired
2386d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // on behalf of a process not in notification clients and we ignore the warning.
2387d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
238844a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent}
238944a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent
239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() {
239165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("purging stale effects");
2393fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten
239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > chains;
239558912562617941964939a4182cda71eaeb153d4bGlenn Kasten
239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chains.push(ec);
240281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2403c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
240458912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
240581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
240681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread> t = mRecordThreads.valueAt(i);
240781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
240981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chains.push(ec);
241081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2411300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten    }
241265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
241381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < chains.size(); i++) {
241481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> ec = chains[i];
241581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionid = ec->sessionId();
241681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> t = ec->mThread.promote();
241781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
241881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
242081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numsessionrefs = mAudioSessionRefs.size();
242181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool found = false;
242281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t k = 0; k < numsessionrefs; k++) {
242381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
242481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mSessionid == sessionid) {
242581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV(" session %d still exists for %d with %d refs",
242681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sessionid, ref->mPid, ref->mCnt);
242781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                found = true;
242881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
242958912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
243058912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
243181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!found) {
2432e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            Mutex::Autolock _l(t->mLock);
243381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // remove all effects from the chain
243481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            while (ec->mEffects.size()) {
243581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectModule> effect = ec->mEffects[0];
243681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->unPin();
243781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                t->removeEffect_l(effect);
243881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (effect->purgeHandles()) {
243981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
244081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
244181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioSystem::unregisterEffect(effect->id());
244281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2443c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
244458912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
244581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return;
244665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
244765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2448eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten// checkThread_l() must be called with AudioFlinger::mLock held
2449eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn KastenAudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2450eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten{
2451eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    ThreadBase *thread = NULL;
2452eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    switch (audio_unique_id_get_use(ioHandle)) {
2453eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    case AUDIO_UNIQUE_ID_USE_OUTPUT:
2454eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        thread = checkPlaybackThread_l(ioHandle);
2455eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        break;
2456eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    case AUDIO_UNIQUE_ID_USE_INPUT:
2457eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        thread = checkRecordThread_l(ioHandle);
2458eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        break;
2459eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    default:
2460eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        break;
2461eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    }
2462eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    return thread;
2463eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten}
2464eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten
246581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
246681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
2467190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{
246881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mPlaybackThreads.valueFor(output).get();
2469190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten}
2470190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
247181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held
247281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
247381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
247481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
247581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
247681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2477190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
247881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held
247981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
248081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
248181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mRecordThreads.valueFor(input).get();
248281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2483190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
2484eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kastenaudio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
248581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
24869d003131329450081c8129b3fddd85cf20d2d9d9Glenn Kasten    // This is the internal API, so it is OK to assert on bad parameter.
2487eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
2488d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1;
2489d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    for (int retry = 0; retry < maxRetries; retry++) {
2490d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten        // The cast allows wraparound from max positive to min negative instead of abort
2491d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten        uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use],
2492d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten                (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel);
2493d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten        ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2494d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten        // allow wrap by skipping 0 and -1 for session ids
2495d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten        if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) {
2496d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten            ALOGW_IF(retry != 0, "unique ID overflow for use %d", use);
2497d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten            return (audio_unique_id_t) (base | use);
2498d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten        }
2499d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    }
2500d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    // We have no way of recovering from wraparound
2501d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    LOG_ALWAYS_FATAL("unique ID overflow for use %d", use);
2502d2e67e1ef59921101fd7b047e2acf84e5d16d66eGlenn Kasten    // TODO Use a floor after wraparound.  This may need a mutex.
250381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
250483efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten
250581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
250637d825e72a6c606553a745da1212590a425996d3Glenn Kasten{
250781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
250881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2509f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        if(thread->isDuplicating()) {
2510f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent            continue;
2511f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        }
251281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *output = thread->getOutput();
251381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
251481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return thread;
251537d825e72a6c606553a745da1212590a425996d3Glenn Kasten        }
251637d825e72a6c606553a745da1212590a425996d3Glenn Kasten    }
251781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NULL;
251837d825e72a6c606553a745da1212590a425996d3Glenn Kasten}
251937d825e72a6c606553a745da1212590a425996d3Glenn Kasten
252081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const
252165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
252281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
2523000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
252481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
252581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
25269f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten    }
2527000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
252881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread->outDevice();
2529000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2530688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten
253181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2532d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                    audio_session_t triggerSession,
2533d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                    audio_session_t listenerSession,
253481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    sync_event_callback_t callBack,
25358ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent                                    wp<RefBase> cookie)
253681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
253781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
253873ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten
253981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
254081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t playStatus = NAME_NOT_FOUND;
254181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t recStatus = NAME_NOT_FOUND;
254281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
254381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
254481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playStatus == NO_ERROR) {
254581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
254681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
254765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
254881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
254981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
255081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (recStatus == NO_ERROR) {
255181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
25529f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten        }
255365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
255481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
255581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPendingSyncEvents.add(event);
255681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
255781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createSyncEvent() invalid event %d", event->type());
255881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        event.clear();
255981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
256081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event;
256165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
256265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
256381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
256481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//  Effect management
256581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
256658912562617941964939a4182cda71eaeb153d4bGlenn Kasten
256758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
256881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
2569000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
257081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
257181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryNumberEffects(numEffects);
2572000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2573000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
257481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
257558912562617941964939a4182cda71eaeb153d4bGlenn Kasten{
257681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
257781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryEffect(index, descriptor);
257858912562617941964939a4182cda71eaeb153d4bGlenn Kasten}
257958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
258081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
258181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *descriptor) const
2582000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
258381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
258481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectGetDescriptor(pUuid, descriptor);
2585000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2586000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
258781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25888d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect(
258981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *pDesc,
259081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
259181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
259281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t io,
2593d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten        audio_session_t sessionId,
2594be71aa29a3c86d2e01cd17839d2a72ab09a1bce5Svet Ganov        const String16& opPackageName,
259581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status,
259681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *id,
259781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled)
2598000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
259981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus = NO_ERROR;
260081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
260181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc;
2602000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
26038d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
260481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
260581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid, effectClient.get(), priority, sessionId, io);
2606000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
260781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDesc == NULL) {
260881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
260981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2610952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2611000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
261281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check audio settings permission for global effects
261381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
261481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
261581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2616952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2617000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
261881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
261981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that can only be created by audio policy manager (running in same process)
262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2623952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
262465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
262681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!EffectIsNullUuid(&pDesc->uuid)) {
262781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is specified, request effect descriptor
262881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
263081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
263181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
263281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
263481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is not specified, look for an available implementation
263581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // of the required type in effect factory
263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (EffectIsNullUuid(&pDesc->type)) {
263781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() no effect type");
263881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2640288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
264181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t numEffects = 0;
264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect_descriptor_t d;
264381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            d.flags = 0; // prevent compiler warning
264481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool found = false;
2645288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
264681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectQueryNumberEffects(&numEffects);
264781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
264881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
264981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
265081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
265181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (uint32_t i = 0; i < numEffects; i++) {
265281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = EffectQueryEffect(i, &desc);
265381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (lStatus < 0) {
265481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
265581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
2656d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
265781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
265881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If matching type found save effect descriptor. If the session is
265981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // 0 and the effect is not auxiliary, continue enumeration in case
266081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // an auxiliary version of this effect type is available
266181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    found = true;
266281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    d = desc;
266381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
266481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2665d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                        break;
2666d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                    }
2667d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
2668288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
266981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (!found) {
267081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
267181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() effect not found");
267281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
267381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
267481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For same effect type, chose auxiliary version over insert version if
267581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // connect to output mix (Compliance to OpenSL ES)
267681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
267781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
267881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc = d;
267958912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
268058912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
268158912562617941964939a4182cda71eaeb153d4bGlenn Kasten
268281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Do not allow auxiliary effects on a session different from 0 (output mix)
268381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
268481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
268581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = INVALID_OPERATION;
268681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
26873dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent        }
268865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
268981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check recording permission for visualizer
269081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2691dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen            !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
269281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = PERMISSION_DENIED;
269381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
269481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
269565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
269681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return effect descriptor
269781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *pDesc = desc;
2698142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2699eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // if the output returned by getOutputForEffect() is removed before we lock the
2700eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2701eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // and we will exit safely
2702eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            io = AudioSystem::getOutputForEffect(&desc);
2703eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            ALOGV("createEffect got output %d", io);
2704eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        }
2705eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent
2706eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        Mutex::Autolock _l(mLock);
270765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
270881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is not specified try to find a matching audio session ID in one of the
270981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // output threads.
271081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
271181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // because of code checking output when entering the function.
271281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Note: io is never 0 when creating an effect on an input
2713142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE) {
27145baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
27155baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // output must be specified by AudioPolicyManager when using session
27165baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // AUDIO_SESSION_OUTPUT_STAGE
27175baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                lStatus = BAD_VALUE;
27185baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                goto Exit;
27195baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
2720eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // look for the thread where the specified audio session is present
2721eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2722eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2723eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    io = mPlaybackThreads.keyAt(i);
2724eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    break;
2725eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                }
2726d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
272781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0) {
2728eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
2729eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2730eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                        io = mRecordThreads.keyAt(i);
273181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
27322986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
273365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
273465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
273581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If no output thread contains the requested session ID, default to
273681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // first output. The effect chain will be moved to the correct output
273781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // thread when a track with the same session ID is created
2738142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten            if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
273981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                io = mPlaybackThreads.keyAt(0);
274081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
274181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect() got io %d for effect %s", io, desc.name);
274265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
274381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ThreadBase *thread = checkRecordThread_l(io);
274481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
274581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = checkPlaybackThread_l(io);
274681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (thread == NULL) {
274781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGE("createEffect() unknown output thread");
274881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
274981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2750288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
2751aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        } else {
2752aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            // Check if one effect chain was awaiting for an effect to be created on this
2753aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            // session and used it instead of creating a new one.
2754d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten            sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
2755aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            if (chain != 0) {
27561b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                Mutex::Autolock _l(thread->mLock);
2757aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                thread->addEffectChain_l(chain);
2758aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
2759288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten        }
2760288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
2761021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        sp<Client> client = registerPid(pid);
276258912562617941964939a4182cda71eaeb153d4bGlenn Kasten
276381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect on selected output thread
276481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = thread->createEffect_l(client, effectClient, priority, sessionId,
276581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                &desc, enabled, &lStatus);
276681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (handle != 0 && id != NULL) {
276781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *id = handle->id();
276865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
2769fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        if (handle == 0) {
2770fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            // remove local strong reference to Client with mClientLock held
2771fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
2772fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
2773fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
277465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
277565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
277681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
27779156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
277881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
277966fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten}
278066fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten
2781d848eb48c121c119e8ba7583efc75415fe102570Glenn Kastenstatus_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
278281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t dstOutput)
278365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
278481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
278581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcOutput, dstOutput);
278665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
278781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcOutput == dstOutput) {
278881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
278981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
279081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
279181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
279281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcThread == NULL) {
279381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad srcOutput %d", srcOutput);
279481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
279581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
279681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
279781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (dstThread == NULL) {
279881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad dstOutput %d", dstOutput);
279981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
280065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
280165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
280281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _dl(dstThread->mLock);
280381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _sl(srcThread->mLock);
28045baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return moveEffectChain_l(sessionId, srcThread, dstThread, false);
280565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
280665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
280781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
2808d848eb48c121c119e8ba7583efc75415fe102570Glenn Kastenstatus_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
280981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *srcThread,
281081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *dstThread,
281181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   bool reRegister)
281265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
281381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
281481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcThread, dstThread);
281565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
281681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
281781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
281881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
281981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sessionId, srcThread);
282081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
282181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
28229ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
28239a59276fb465e492138e0576523b54079671e8f4Andy Hung    // Check whether the destination thread has a channel count of FCC_2, which is
28249a59276fb465e492138e0576523b54079671e8f4Andy Hung    // currently required for (most) effects. Prevent moving the effect chain here rather
28259a59276fb465e492138e0576523b54079671e8f4Andy Hung    // than disabling the addEffect_l() call in dstThread below.
2826f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
2827b10352f4bc11ae6f3d93e464074709555ecf9e06Eric Laurent            dstThread->mChannelCount != FCC_2) {
28289a59276fb465e492138e0576523b54079671e8f4Andy Hung        ALOGW("moveEffectChain_l() effect chain failed because"
28299a59276fb465e492138e0576523b54079671e8f4Andy Hung                " destination thread %p channel count(%u) != %u",
28309a59276fb465e492138e0576523b54079671e8f4Andy Hung                dstThread, dstThread->mChannelCount, FCC_2);
28319a59276fb465e492138e0576523b54079671e8f4Andy Hung        return INVALID_OPERATION;
28329a59276fb465e492138e0576523b54079671e8f4Andy Hung    }
28339a59276fb465e492138e0576523b54079671e8f4Andy Hung
283481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
283581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // so that a new chain is created with correct parameters when first effect is added. This is
283681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
283781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // removed.
283881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    srcThread->removeEffectChain_l(chain);
28399ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
284081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // transfer all effects one by one so that new effect chain is created on new thread with
284181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
284281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> dstChain;
284381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy = 0; // prevent compiler warning
284481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect = chain->getEffectFromId_l(0);
28455baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    Vector< sp<EffectModule> > removed;
28465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    status_t status = NO_ERROR;
284781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (effect != 0) {
284881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        srcThread->removeEffect_l(effect);
28495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        removed.add(effect);
28505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        status = dstThread->addEffect_l(effect);
28515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (status != NO_ERROR) {
28525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
28535baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
285481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // removeEffect_l() has stopped the effect if it was active so it must be restarted
285581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect->state() == EffectModule::ACTIVE ||
285681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->state() == EffectModule::STOPPING) {
285781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->start();
285865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
285981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the move request is not received from audio policy manager, the effect must be
286081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // re-registered with the new strategy and output
286181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (dstChain == 0) {
286281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            dstChain = effect->chain().promote();
286381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (dstChain == 0) {
286481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
28655baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                status = NO_INIT;
28665baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                break;
286765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
286881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            strategy = dstChain->strategy();
286965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
287081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (reRegister) {
287181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
287281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::registerEffect(&effect->desc(),
28735baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                        dstThread->id(),
287481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        strategy,
287581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        sessionId,
287681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        effect->id());
2877d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent            AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
287881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
287981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect = chain->getEffectFromId_l(0);
288058912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
288158912562617941964939a4182cda71eaeb153d4bGlenn Kasten
28825baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (status != NO_ERROR) {
28835baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        for (size_t i = 0; i < removed.size(); i++) {
28845baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            srcThread->addEffect_l(removed[i]);
28855baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (dstChain != 0 && reRegister) {
28865baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::unregisterEffect(removed[i]->id());
28875baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::registerEffect(&removed[i]->desc(),
28885baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            srcThread->id(),
28895baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            strategy,
28905baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            sessionId,
28915baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            removed[i]->id());
2892d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent                AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
28935baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
28945baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
28955baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    }
28965baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
28975baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return status;
289865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
289965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
29005baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
2901813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2902813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    if (mGlobalEffectEnableTime != 0 &&
2903813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2904813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        return true;
2905813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2906813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2907813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2908813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<EffectChain> ec =
2909813e2a74853bde19e37d878c596a044b3f299efcEric Laurent                mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
29105baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (ec != 0 && ec->isNonOffloadableEnabled()) {
2911813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            return true;
2912813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2913813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2914813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    return false;
2915813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2916813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
29175baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable()
2918813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2919813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    Mutex::Autolock _l(mLock);
2920813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2921813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    mGlobalEffectEnableTime = systemTime();
2922813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2923813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2924813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2925813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        if (t->mType == ThreadBase::OFFLOAD) {
2926813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            t->invalidateTracks(AUDIO_STREAM_MUSIC);
2927813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2928813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2929813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2930813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2931813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2932aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentstatus_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2933aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2934d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten    audio_session_t session = chain->sessionId();
2935aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2936c42e9b462661673dff480ee71757a58b0f806370Glenn Kasten    ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
2937aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2938aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2939aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        return ALREADY_EXISTS;
2940aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2941aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    mOrphanEffectChains.add(session, chain);
2942aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return NO_ERROR;
2943aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2944aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2945aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2946aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2947aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    sp<EffectChain> chain;
2948aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2949c42e9b462661673dff480ee71757a58b0f806370Glenn Kasten    ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
2950aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2951aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        chain = mOrphanEffectChains.valueAt(index);
2952aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        mOrphanEffectChains.removeItemsAt(index);
2953aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2954aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return chain;
2955aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2956aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2957aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentbool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2958aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2959aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    Mutex::Autolock _l(mLock);
2960d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten    audio_session_t session = effect->sessionId();
2961aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2962c42e9b462661673dff480ee71757a58b0f806370Glenn Kasten    ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
2963aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2964aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2965aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        if (chain->removeEffect_l(effect) == 0) {
2966c42e9b462661673dff480ee71757a58b0f806370Glenn Kasten            ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
2967aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            mOrphanEffectChains.removeItemsAt(index);
2968aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
2969aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        return true;
2970aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2971aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return false;
2972aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2973aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2974aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2975da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry {
29762f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
29772f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten    char mFileName[TEE_MAX_FILENAME];
2978da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten};
2979da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2980da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2)
2981da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{
29822f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten    return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
2983da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}
2984da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
298546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2986d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
298765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
2988d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten    NBAIO_Source *teeSource = source.get();
2989fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    if (teeSource != NULL) {
2990da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // .wav rotation
2991da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // There is a benign race condition if 2 threads call this simultaneously.
2992da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // They would both traverse the directory, but the result would simply be
2993da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // failures at unlink() which are ignored.  It's also unlikely since
2994da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // normally dumpsys is only done by bugreport or from the command line.
2995da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        char teePath[32+256];
29969a00399340c7c129714dff96f1ab59045fe43056Glenn Kasten        strcpy(teePath, "/data/misc/audioserver");
2997da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        size_t teePathLen = strlen(teePath);
2998da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        DIR *dir = opendir(teePath);
2999da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teePath[teePathLen++] = '/';
3000da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (dir != NULL) {
30012f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten#define TEE_MAX_SORT 20 // number of entries to sort
30022f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten#define TEE_MAX_KEEP 10 // number of entries to keep
30032f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            struct Entry entries[TEE_MAX_SORT];
3004da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            size_t entryCount = 0;
30052f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            while (entryCount < TEE_MAX_SORT) {
3006da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent de;
3007da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent *result = NULL;
3008da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                int rc = readdir_r(dir, &de, &result);
3009da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (rc != 0) {
3010da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r failed %d", rc);
3011da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
3012da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
3013da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result == NULL) {
3014da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
3015da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
3016da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result != &de) {
3017da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
3018da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
3019da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
3020da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                // ignore non .wav file entries
3021da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                size_t nameLen = strlen(de.d_name);
30222f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
3023da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                        strcmp(&de.d_name[nameLen - 4], ".wav")) {
3024da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    continue;
3025da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
30262f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                strcpy(entries[entryCount++].mFileName, de.d_name);
3027da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
3028da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            (void) closedir(dir);
30292f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            if (entryCount > TEE_MAX_KEEP) {
3030da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                qsort(entries, entryCount, sizeof(Entry), comparEntry);
30312f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
30322f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                    strcpy(&teePath[teePathLen], entries[i].mFileName);
3033da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    (void) unlink(teePath);
3034da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
3035da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
3036da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        } else {
3037da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
30389a00399340c7c129714dff96f1ab59045fe43056Glenn Kasten                dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
30399a00399340c7c129714dff96f1ab59045fe43056Glenn Kasten                        strerror(errno));
3040da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
3041da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
3042d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        char teeTime[16];
3043fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct timeval tv;
3044fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        gettimeofday(&tv, NULL);
3045fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct tm tm;
3046fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        localtime_r(&tv.tv_sec, &tm);
3047da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
3048da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
3049da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
3050da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
3051fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        if (teeFd >= 0) {
3052329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME use libsndfile
3053fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            char wavHeader[44];
3054fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            memcpy(wavHeader,
3055fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0",
3056fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                sizeof(wavHeader));
3057fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            NBAIO_Format format = teeSource->format();
3058fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            unsigned channelCount = Format_channelCount(format);
30593b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten            uint32_t sampleRate = Format_sampleRate(format);
3060329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            size_t frameSize = Format_frameSize(format);
3061fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[22] = channelCount;       // number of channels
3062fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[24] = sampleRate;         // sample rate
3063fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[25] = sampleRate >> 8;
3064329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            wavHeader[32] = frameSize;          // block alignment
3065329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            wavHeader[33] = frameSize >> 8;
3066fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, wavHeader, sizeof(wavHeader));
3067fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            size_t total = 0;
3068fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            bool firstRead = true;
3069329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten#define TEE_SINK_READ 1024                      // frames per I/O operation
3070329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            void *buffer = malloc(TEE_SINK_READ * frameSize);
3071fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            for (;;) {
3072fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                size_t count = TEE_SINK_READ;
3073d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten                ssize_t actual = teeSource->read(buffer, count);
3074fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                bool wasFirstRead = firstRead;
3075fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                firstRead = false;
3076fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                if (actual <= 0) {
3077fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3078fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                        continue;
3079fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    }
3080fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    break;
3081fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                }
3082a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent                ALOG_ASSERT(actual <= (ssize_t)count);
3083329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten                write(teeFd, buffer, actual * frameSize);
3084fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                total += actual;
3085fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            }
3086329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            free(buffer);
3087fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 4, SEEK_SET);
3088329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            uint32_t temp = 44 + total * frameSize - 8;
3089329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME not big-endian safe
3090fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
3091fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 40, SEEK_SET);
3092329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            temp =  total * frameSize;
3093329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME not big-endian safe
3094fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
3095fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            close(teeFd);
3096da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
30978b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "tee copied to %s\n", teePath);
3098da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
3099fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        } else {
3100da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
31018b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
3102da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
3103fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        }
3104fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    }
3105d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten}
310646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
3107d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
310865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
310965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
311065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact(
311165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
311265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
311365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioFlinger::onTransact(code, data, reply, flags);
311465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
311565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
311663238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android
3117