AudioFlinger.cpp revision eeecb980ff4c202d0a3c4b0bfe040dce2f73336d
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>
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/String16.h>
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h>
3638ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent#include <utils/Atomic.h>
3765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <cutils/bitops.h>
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/properties.h>
4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4164760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
427394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin#include <hardware/audio.h>
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioMixer.h"
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioFlinger.h"
4644deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten#include "ServiceUtilities.h"
4765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
486770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung#include <media/AudioResamplerPublic.h>
496770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/EffectsFactoryApi.h>
516d8b694d999e9be7d5dcc336535832a80fb6f61fEric Laurent#include <audio_effects/effect_visualizer.h>
5259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_ns.h>
5359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_aec.h>
5465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
553b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten#include <audio_utils/primitives.h>
563b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten
57feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent#include <powermanager/PowerManager.h>
58190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
599e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten#include <media/IMediaLogService.h>
609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
61da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <media/nbaio/Pipe.h>
62da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <media/nbaio/PipeReader.h>
631ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/AudioParameter.h>
643f273d10817ddb2f792ae043de692efcdf1988aeWei Jia#include <mediautils/BatteryNotifier.h>
654182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten#include <private/android_filesystem_config.h>
66da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
6865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
691c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// Note: the following macro is used for extremely verbose logging message.  In
701c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
711c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// 0; but one side effect of this is to turn all LOGV's as well.  Some messages
721c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are so verbose that we want to suppress them even when we have ALOG_ASSERT
731c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// turned on.  Do not uncomment the #def below unless you really know what you
741c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are doing and want to see all of the extremely verbose messages.
751c345196edc61694f29307a1826a64a0d26028dcJohn Grossman//#define VERY_VERY_VERBOSE_LOGGING
761c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#ifdef VERY_VERY_VERBOSE_LOGGING
771c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV ALOGV
781c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#else
791c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV(a...) do { } while(0)
801c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#endif
81de070137f11d346fba77605bd76a44c040a618fcEric Laurent
8265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
8365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
85ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kHardwareLockedString[] = "Hardware lock is taken\n";
86021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentstatic const char kClientLockedString[] = "Client lock is taken\n";
8765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8858912562617941964939a4182cda71eaeb153d4bGlenn Kasten
894ff14bae91075eb274eb1c2975982358946e7e63John Grossmannsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
907cafbb32999049873d4746ba83bd20c88abe6ce6Eric Laurent
9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::mScreenState;
923ed292031dc50c56110cdadb1e3778117e3be76aGlenn Kasten
9346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
94da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkInputEnabled = false;
95da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkOutputEnabled = false;
96da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkTrackEnabled = false;
97da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
98da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
99da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
100da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
10146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
102da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
103813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
104813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// we define a minimum time during which a global effect is considered enabled.
105813e2a74853bde19e37d878c596a044b3f299efcEric Laurentstatic const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
106813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
10765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
10865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
109b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissenconst char *formatToString(audio_format_t format) {
110fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk    switch (audio_get_main_format(format)) {
1112829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_PCM:
1122829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        switch (format) {
1132829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
1142829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
1152829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
1162829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
1172829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
1182829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
1192829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        default:
1202829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad            break;
1212829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        }
1222829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        break;
123b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_MP3: return "mp3";
124b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_NB: return "amr-nb";
125b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_WB: return "amr-wb";
126b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AAC: return "aac";
127b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
128b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
129b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_VORBIS: return "vorbis";
1302829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_OPUS: return "opus";
1312829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_AC3: return "ac-3";
1322829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_E_AC3: return "e-ac-3";
133fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk    case AUDIO_FORMAT_IEC61937: return "iec61937";
134b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    default:
135b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        break;
136b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
137b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    return "unknown";
138b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen}
139b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
140f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
141799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
142f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    const hw_module_t *mod;
143799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc;
144799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
145f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
146f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
147f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
148f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
149799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
150f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
151f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = audio_hw_device_open(mod, dev);
152f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
153f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
154f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
155799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
156f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
1575806b3533437e3d83208e8e9d6bd74ed304e51ecEric Laurent    if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
158f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
159f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        rc = BAD_VALUE;
160f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        goto out;
161f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
162799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return 0;
163799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
164799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout:
165799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    *dev = NULL;
166799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return rc;
167799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
168799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
16965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger()
17265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    : BnAudioFlinger(),
1734ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPrimaryHardwareDev(NULL),
1749ea65d0f4a564478343b1a722fae4ce5883670c3Glenn Kasten      mAudioHwDevs(NULL),
1757d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten      mHardwareStatus(AUDIO_HW_IDLE),
1764ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterVolume(1.0f),
1774ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterMute(false),
178eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten      mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX),   // zero has a special meaning, so unavailable
1794ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMode(AUDIO_MODE_INVALID),
1804182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mBtNrecIsOff(false),
1814182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mIsLowRamDevice(true),
182813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mIsDeviceTypeKnown(false),
1834ea00a25cf85877b48ebd1e15a657cfaab29af58Glenn Kasten      mGlobalEffectEnableTime(0),
18472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent      mSystemReady(false)
18565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
186949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten    getpid_cached = getpid();
187ae0cff1d48b2cd10aeff9627398faf684894eeceGlenn Kasten    const bool doLog = property_get_bool("ro.test_harness", false);
1889e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (doLog) {
189b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten        mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
190b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten                MemoryHeapBase::READ_ONLY);
1919e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
1921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1933f273d10817ddb2f792ae043de692efcdf1988aeWei Jia    // reset battery stats.
1943f273d10817ddb2f792ae043de692efcdf1988aeWei Jia    // if the audio service has crashed, battery stats could be left
1953f273d10817ddb2f792ae043de692efcdf1988aeWei Jia    // in bad state, reset the state upon service start.
1963f273d10817ddb2f792ae043de692efcdf1988aeWei Jia    BatteryNotifier::getInstance().noteResetAudio();
1973f273d10817ddb2f792ae043de692efcdf1988aeWei Jia
19846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
1999a00399340c7c129714dff96f1ab59045fe43056Glenn Kasten    char value[PROPERTY_VALUE_MAX];
200da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    (void) property_get("ro.debuggable", value, "0");
201da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int debuggable = atoi(value);
202da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int teeEnabled = 0;
203da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    if (debuggable) {
204da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        (void) property_get("af.tee", value, "0");
205da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teeEnabled = atoi(value);
206da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    }
207f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten    // FIXME symbolic constants here
2086e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 1) {
209da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkInputEnabled = true;
2106e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2116e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 2) {
212da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkOutputEnabled = true;
2136e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2146e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 4) {
215da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkTrackEnabled = true;
2166e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
21746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2185a61d2f277af3098fc10b2881babca16391362daDima Zavin}
2195a61d2f277af3098fc10b2881babca16391362daDima Zavin
2205a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef()
2215a61d2f277af3098fc10b2881babca16391362daDima Zavin{
222799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc = 0;
223fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
224935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    Mutex::Autolock _l(mLock);
225935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
226799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    /* TODO: move all this work into an Init() function */
2274ff14bae91075eb274eb1c2975982358946e7e63John Grossman    char val_str[PROPERTY_VALUE_MAX] = { 0 };
2284ff14bae91075eb274eb1c2975982358946e7e63John Grossman    if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
2294ff14bae91075eb274eb1c2975982358946e7e63John Grossman        uint32_t int_val;
2304ff14bae91075eb274eb1c2975982358946e7e63John Grossman        if (1 == sscanf(val_str, "%u", &int_val)) {
2314ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = milliseconds(int_val);
2324ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using %u mSec as standby time.", int_val);
2334ff14bae91075eb274eb1c2975982358946e7e63John Grossman        } else {
2344ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
2354ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using default %u mSec as standby time.",
2364ff14bae91075eb274eb1c2975982358946e7e63John Grossman                    (uint32_t)(mStandbyTimeInNsecs / 1000000));
2374ff14bae91075eb274eb1c2975982358946e7e63John Grossman        }
2384ff14bae91075eb274eb1c2975982358946e7e63John Grossman    }
23965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2401c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    mPatchPanel = new PatchPanel(this);
2411c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
242a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    mMode = AUDIO_MODE_NORMAL;
24365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
24465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
24565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger()
24665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
24765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mRecordThreads.isEmpty()) {
248c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeInput_nonvirtual() will remove specified entry from mRecordThreads
249d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeInput_nonvirtual(mRecordThreads.keyAt(0));
25065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
25165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mPlaybackThreads.isEmpty()) {
252c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
253d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
25465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
255799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
2562b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2572b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten        // no mHardwareLock needed, as there are no other references to this
258a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
259a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        delete mAudioHwDevs.valueAt(i);
26065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
261481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
262481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Tell media.log service about any old writers that still need to be unregistered
263ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung    if (mLogMemoryDealer != 0) {
264ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung        sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
265ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung        if (binder != 0) {
266ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung            sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
267ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung            for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
268ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung                sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
269ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung                mUnregisteredWriters.pop();
270ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung                mediaLogService->unregisterWriter(iMemory);
271ce7176868977b0d44b245735bd8d3d8e54e61035Andy Hung            }
272481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
273481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
27465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
27565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
276a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = {
277a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_PRIMARY,
278a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_A2DP,
279a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_USB,
280a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent};
281a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
282a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
283062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioHwDevice* AudioFlinger::findSuitableHwDev_l(
284ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_module_handle_t module,
285ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_devices_t devices)
286799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
287a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // if module is 0, the request comes from an old policy manager and we should load
288a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // well known modules
289a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    if (module == 0) {
290a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
291a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
292a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            loadHwModule_l(audio_interfaces[i]);
293a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
294f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        // then try to find a module supporting the requested device.
295f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
296f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
297f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            audio_hw_device_t *dev = audioHwDevice->hwDevice();
298f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            if ((dev->get_supported_devices != NULL) &&
299f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    (dev->get_supported_devices(dev) & devices) == devices)
300f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                return audioHwDevice;
301f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        }
302a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    } else {
303a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        // check a match for the requested module handle
304ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
305ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (audioHwDevice != NULL) {
306ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            return audioHwDevice;
307a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
308a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    }
309a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
310799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return NULL;
311799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
31265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3130f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
31465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
31565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
31665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("Clients:\n");
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < mClients.size(); ++i) {
32177c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        sp<Client> client = mClients.valueAt(i).promote();
32277c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        if (client != 0) {
32377c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            snprintf(buffer, SIZE, "  pid: %d\n", client->pid());
32477c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            result.append(buffer);
32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
3273a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
328d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    result.append("Notification Clients:\n");
329d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); ++i) {
330d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        snprintf(buffer, SIZE, "  pid: %d\n", mNotificationClients.keyAt(i));
331d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        result.append(buffer);
332d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
333d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
3343a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    result.append("Global session refs:\n");
335b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    result.append("  session   pid count\n");
3363a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
3373a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *r = mAudioSessionRefs[i];
338b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
3393a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        result.append(buffer);
3403a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3450f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
350a4454b4765c5905f14186893b0688be375642283Glenn Kasten    hardware_call_state hardwareStatus = mHardwareStatus;
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3524ff14bae91075eb274eb1c2975982358946e7e63John Grossman    snprintf(buffer, SIZE, "Hardware status: %d\n"
3534ff14bae91075eb274eb1c2975982358946e7e63John Grossman                           "Standby Time mSec: %u\n",
3544ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            hardwareStatus,
3554ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            (uint32_t)(mStandbyTimeInNsecs / 1000000));
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3600f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioFlinger from pid=%d, uid=%d\n",
36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex)
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
37565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3817dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten        usleep(kDumpLockSleepUs);
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args)
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
38844deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd, args);
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // get state of hardware lock
39281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool hardwareLocked = dumpTryLock(mHardwareLock);
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!hardwareLocked) {
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kHardwareLockedString);
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
39765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mHardwareLock.unlock();
39865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
39965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool locked = dumpTryLock(mLock);
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // failed to lock - AudioFlinger is probably deadlocked
40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
40565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
40665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
40765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
408021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        bool clientLocked = dumpTryLock(mClientLock);
409021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (!clientLocked) {
410021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            String8 result(kClientLockedString);
411021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            write(fd, result.string(), result.size());
412021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
413d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen
414d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen        EffectDumpEffects(fd);
415d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen
41665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpClients(fd, args);
417021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (clientLocked) {
418021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mClientLock.unlock();
419021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
420021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent
42165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd, args);
42265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump playback threads
42465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
42565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mPlaybackThreads.valueAt(i)->dump(fd, args);
42665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
42765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump record threads
42965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
43065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mRecordThreads.valueAt(i)->dump(fd, args);
43165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
43265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
433aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        // dump orphan effect chains
434aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        if (mOrphanEffectChains.size() != 0) {
435aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            write(fd, "  Orphan Effect Chains\n", strlen("  Orphan Effect Chains\n"));
436aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
437aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                mOrphanEffectChains.valueAt(i)->dump(fd, args);
438aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
439aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
440799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        // dump all hardware devs
441799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
442a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
443799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            dev->dump(dev, fd);
44465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
445d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
44646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
447d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        // dump the serially shared record tee sink
448d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        if (mRecordTeeSource != 0) {
449d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten            dumpTee(fd, mRecordTeeSource);
450d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        }
45146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
452d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
453d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        if (locked) {
454d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            mLock.unlock();
455d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
4569e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4579e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // append a copy of media.log here by forwarding fd to it, but don't attempt
4589e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // to lookup the service if it's not running, as it will block for a second
4599e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        if (mLogMemoryDealer != 0) {
4609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
4619e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (binder != 0) {
4628b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "\nmedia.log:\n");
4639e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                Vector<String16> args;
4649e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                binder->dump(fd, args);
4659e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
4669e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        }
46765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
46865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
46965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
47065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
471021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentsp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
47298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{
473021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _cl(mClientLock);
47498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // If pid is already in the mClients wp<> map, then use that entry
47598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // (for which promote() is always != 0), otherwise create a new entry and Client.
47698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    sp<Client> client = mClients.valueFor(pid).promote();
47798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    if (client == 0) {
47898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = new Client(this, pid);
47998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        mClients.add(pid, client);
48098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    }
48198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten
48298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    return client;
48398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten}
48465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4859e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
4869e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
487481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If there is no memory allocated for logs, return a dummy writer that does nothing
4889e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (mLogMemoryDealer == 0) {
4899e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return new NBLog::Writer();
4909e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
4919e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
492481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Similarly if we can't contact the media.log service, also return a dummy writer
493481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder == 0) {
494481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
4959e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
496481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
497481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
498481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If allocation fails, consult the vector of previously unregistered writers
499481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // and garbage-collect one or more them until an allocation succeeds
500481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (shared == 0) {
501481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        Mutex::Autolock _l(mUnregisteredWritersLock);
502481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
503481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            {
504481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Pick the oldest stale writer to garbage-collect
505481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
506481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mUnregisteredWriters.removeAt(0);
507481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mediaLogService->unregisterWriter(iMemory);
508481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Now the media.log remote reference to IMemory is gone.  When our last local
509481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // reference to IMemory also drops to zero at end of this block,
510481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // the IMemory destructor will deallocate the region from mLogMemoryDealer.
511481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
512481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            // Re-attempt the allocation
513481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
514481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            if (shared != 0) {
515481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                goto success;
516481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
517481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
518481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // Even after garbage-collecting all old writers, there is still not enough memory,
519481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // so return a dummy writer
520481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
521481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
522481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kastensuccess:
523481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mediaLogService->registerWriter(shared, size, name);
524481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    return new NBLog::Writer(size, shared);
5259e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5269e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
5279e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
5289e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
529685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    if (writer == 0) {
530685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten        return;
531685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    }
5329e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IMemory> iMemory(writer->getIMemory());
5339e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (iMemory == 0) {
5349e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return;
5359e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
536481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Rather than removing the writer immediately, append it to a queue of old writers to
537481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // be garbage-collected later.  This allows us to continue to view old logs for a while.
538481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Mutex::Autolock _l(mUnregisteredWritersLock);
539481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mUnregisteredWriters.push(writer);
5409e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5419e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
54265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface
54365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
54465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
54565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack(
546fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
54765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t sampleRate,
54858f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
549254af180475346b6186b49c297f340c9c4817511Glenn Kasten        audio_channel_mask_t channelMask,
55074935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
551e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten        IAudioFlinger::track_flags_t *flags,
55265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const sp<IMemory>& sharedBuffer,
55372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output,
5543acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        pid_t tid,
55565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int *sessionId,
556462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int clientUid,
55765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status_t *status)
55865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
55965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<PlaybackThread::Track> track;
56065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<TrackHandle> trackHandle;
56165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<Client> client;
56265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t lStatus;
56365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int lSessionId;
56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
565263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
566263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // but if someone uses binder directly they could bypass that and cause us to crash
567263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
56829357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("createTrack() invalid stream type %d", streamType);
56965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        lStatus = BAD_VALUE;
57065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        goto Exit;
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
57353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createTrack_l() depending on the thread type
57453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
57553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid sample rate %u", sampleRate);
57653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
57753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
57853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
57953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
58053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createTrack_l() depending on the thread type
58153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_output_channel(channelMask)) {
58253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid channel mask %#x", channelMask);
58353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
58453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
58553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
58653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
587c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    // further format checks are performed by createTrack_l() depending on the thread type
588c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    if (!audio_is_valid_format(format)) {
589cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("createTrack() invalid format %#x", format);
59060a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        lStatus = BAD_VALUE;
591663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        goto Exit;
592663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    }
593663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten
594663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
595663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
596663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        lStatus = BAD_VALUE;
59760a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        goto Exit;
59860a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    }
59960a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten
60065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
60165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
60365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
604c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten            ALOGE("no playback thread found for output handle %d", output);
60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lStatus = BAD_VALUE;
60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            goto Exit;
60765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
60865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6098d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
610021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
61165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
612e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        PlaybackThread *effectThread = NULL;
613aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
614eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
615eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                ALOGE("createTrack() invalid session ID %d", *sessionId);
616eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                lStatus = BAD_VALUE;
617eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                goto Exit;
618eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            }
619570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            lSessionId = *sessionId;
620f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // check if an effect chain with the same session ID is present on another
621f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // output thread and move it here.
622de070137f11d346fba77605bd76a44c040a618fcEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
62339e94f8f723d445447fdee0822291e664b631f60Eric Laurent                sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
62439e94f8f723d445447fdee0822291e664b631f60Eric Laurent                if (mPlaybackThreads.keyAt(i) != output) {
625570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten                    uint32_t sessions = t->hasAudioSession(lSessionId);
62639e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    if (sessions & PlaybackThread::EFFECT_SESSION) {
62739e94f8f723d445447fdee0822291e664b631f60Eric Laurent                        effectThread = t.get();
628f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent                        break;
62939e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    }
630de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
631de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
63265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
633de070137f11d346fba77605bd76a44c040a618fcEric Laurent            // if no audio session id is provided, create one here
634eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            lSessionId = nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
63565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (sessionId != NULL) {
63665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                *sessionId = lSessionId;
63765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
63865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
6393856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() lSessionId: %d", lSessionId);
64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track = thread->createTrack_l(client, streamType, sampleRate, format,
642462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
64303e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
6442fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten        // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
64539e94f8f723d445447fdee0822291e664b631f60Eric Laurent
64639e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // move effect chain to this output thread if an effect on same session was waiting
64739e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // for a track to be created
64839e94f8f723d445447fdee0822291e664b631f60Eric Laurent        if (lStatus == NO_ERROR && effectThread != NULL) {
6492fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten            // no risk of deadlock because AudioFlinger::mLock is held
65039e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _dl(thread->mLock);
65139e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _sl(effectThread->mLock);
65239e94f8f723d445447fdee0822291e664b631f60Eric Laurent            moveEffectChain_l(lSessionId, effectThread, thread, true);
65339e94f8f723d445447fdee0822291e664b631f60Eric Laurent        }
654a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
655a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        // Look for sync events awaiting for a session to be used.
6563ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn        for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
657a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
658a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
6592986460984580833161bdaabc7f17da1005a8961Eric Laurent                    if (lStatus == NO_ERROR) {
660d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten                        (void) track->setSyncEvent(mPendingSyncEvents[i]);
6612986460984580833161bdaabc7f17da1005a8961Eric Laurent                    } else {
6622986460984580833161bdaabc7f17da1005a8961Eric Laurent                        mPendingSyncEvents[i]->cancel();
6632986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
664a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    mPendingSyncEvents.removeAt(i);
665a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    i--;
666a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                }
667a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            }
668a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
669e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
670fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
67165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
672e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
6733ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    if (lStatus != NO_ERROR) {
6743ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        // remove local strong reference to Client before deleting the Track so that the
675021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
676fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // Don't hold mClientLock when releasing the reference on the track as the
677fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // destructor will acquire it.
678fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        {
679fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
680fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
681fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track.clear();
6833ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        goto Exit;
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6863ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    // return handle to client
6873ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    trackHandle = new TrackHandle(track);
6883ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten
68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit:
6909156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return trackHandle;
69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
69365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
69472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
69665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
69865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6995ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("sampleRate() unknown thread %d", output);
70065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
70165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->sampleRate();
70365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
70465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
70572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(audio_io_handle_t output) const
70665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
70765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
7105ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("format() unknown thread %d", output);
71158f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        return AUDIO_FORMAT_INVALID;
71265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
71365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->format();
71465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
71565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
71672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastensize_t AudioFlinger::frameCount(audio_io_handle_t output) const
71765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
71865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
7215ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("frameCount() unknown thread %d", output);
72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
72365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
72458912562617941964939a4182cda71eaeb153d4bGlenn Kasten    // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
72558912562617941964939a4182cda71eaeb153d4bGlenn Kasten    //       should examine all callers and fix them to handle smaller counts
72665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->frameCount();
72765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
72865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
72972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const
73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
73165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
73365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
734c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten        ALOGW("latency(): no playback thread found for output handle %d", output);
73565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
73665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
73765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->latency();
73865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
73965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value)
74165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
742a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
743a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
744a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
745a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
746a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
74765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
74865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
74965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
75065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
75165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
752a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
753ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterVolume = value;
754a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
755ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master volume in the HALs which support it.
756ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
757ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
758ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
7594ff14bae91075eb274eb1c2975982358946e7e63John Grossman
760ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
761ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterVolume()) {
762ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
763935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent        }
764ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
76665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
767ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master volume in each playback thread.  Playback threads
768ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master volume support will apply
769ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // master volume during the mix operation.  Threads with HALs which do
770ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // support master volume will simply ignore the setting.
771f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
772f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
773f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent            continue;
774f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        }
775ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterVolume(value);
776f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    }
77765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
77965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
781f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode)
78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
783a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
784a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
785a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
786a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
78765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
78965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
792930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(mode) >= AUDIO_MODE_CNT) {
7935ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("Illegal value: setMode(%d)", mode);
79465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
79565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
79665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    { // scope for the lock
79865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex lock(mHardwareLock);
799ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_SET_MODE;
801ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        ret = dev->set_mode(dev, mode);
80265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_IDLE;
80365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (NO_ERROR == ret) {
80665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
80765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mMode = mode;
8088d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++)
809e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setMode(mode);
81065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
81165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
81265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
81365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
81465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
81565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state)
81665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
817a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
818a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
819a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
820a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
821a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
82265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
82365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
82465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
82565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
82765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
82865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
8292f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
8302f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
8312f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        status_t result = dev->set_mic_mute(dev, state);
8322f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        if (result != NO_ERROR) {
8332f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent            ret = result;
8342f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        }
8352f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent    }
83665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
83765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
83865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
83965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const
84165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
842a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
843a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
844a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return false;
845a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
8463e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    bool mute = true;
847fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    bool state = AUDIO_MODE_INVALID;
8482b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
84965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
8503e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
8513e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
8523e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        status_t result = dev->get_mic_mute(dev, &state);
8533e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        if (result == NO_ERROR) {
8543e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia            mute = mute && state;
8553e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        }
8563e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    }
85765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
8583e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia
8593e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    return mute;
86065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
86165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted)
86365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
864d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    status_t ret = initCheck();
865d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    if (ret != NO_ERROR) {
866d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        return ret;
867d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
868d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
87365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
874ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    Mutex::Autolock _l(mLock);
875ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterMute = muted;
876d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
877ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master mute in the HALs which support it.
878ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
879ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
880ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
881d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
882ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
883ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterMute()) {
884ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
885d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        }
886ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
887d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
888d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
889ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master mute in each playback thread.  Playback threads
890ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master mute support will apply master
891ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute during the mix operation.  Threads with HALs which do support master
892ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute will simply ignore the setting.
893f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
894f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
895f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent            continue;
896f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        }
897ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterMute(muted);
898f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    }
89965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
90165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
90265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const
90465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
9059806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
9069806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterVolume_l();
90765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
90865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const
91065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
9119806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
9129806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterMute_l();
91365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
91465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9154ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const
9164ff14bae91075eb274eb1c2975982358946e7e63John Grossman{
9174ff14bae91075eb274eb1c2975982358946e7e63John Grossman    return mMasterVolume;
9184ff14bae91075eb274eb1c2975982358946e7e63John Grossman}
9194ff14bae91075eb274eb1c2975982358946e7e63John Grossman
920d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const
921d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{
922ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    return mMasterMute;
923d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman}
924d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
925223fd5c9738e9665e495904d37d4632414b68c1eEric Laurentstatus_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
926223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent{
927223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
928223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        ALOGW("setStreamVolume() invalid stream %d", stream);
929223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return BAD_VALUE;
930223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
931223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
932223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
933223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
934223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return PERMISSION_DENIED;
935223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
936223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
937223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    return NO_ERROR;
938223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent}
939223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
94072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
94172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
94265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
94365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
94465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
94565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
94765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
948223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
949223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
950223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return status;
95165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
952223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
95365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
95565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = NULL;
956142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
95765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(output);
95865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
95965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return BAD_VALUE;
96065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
96165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
96265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
96365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].volume = value;
96465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
96565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
9668d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
967e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
96865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
96965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
97065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread->setStreamVolume(stream, value);
97165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
97265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
97365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
97465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
97565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
976fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
97765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
97865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
97965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
98065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
98165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
98265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
983223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
984223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
985223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return status;
986223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
987223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
988223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
989223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
99029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamMute() invalid stream %d", stream);
99165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
99265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
99365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
994935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    AutoMutex lock(mLock);
99565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].mute = muted;
9963ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
997e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
99865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
99965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
100065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
100165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
100272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
100365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1004223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
1005223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
100665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0.0f;
100765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
100865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
100965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    float volume;
1011142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
101265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
101365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
101465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return 0.0f;
101565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
101665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        volume = thread->streamVolume(stream);
101765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
10186637baae4244aec731c4014da72418d330636ae1Glenn Kasten        volume = streamVolume_l(stream);
101965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
102065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
102165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return volume;
102265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
102365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1024fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const
102565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1026223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
1027223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
102865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return true;
102965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
103065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
10316637baae4244aec731c4014da72418d330636ae1Glenn Kasten    AutoMutex lock(mLock);
10326637baae4244aec731c4014da72418d330636ae1Glenn Kasten    return streamMute_l(stream);
103365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
103465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1035054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
1036054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurentvoid AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1037054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent{
1038054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
1039054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent        mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1040054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    }
1041054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent}
1042054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
104372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
104465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1045827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1046827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
104865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
104965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
105065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
105165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
105265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1053142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1054142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
1055a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        Mutex::Autolock _l(mLock);
1056799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        status_t final_result = NO_ERROR;
10578abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        {
1058a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            AutoMutex lock(mHardwareLock);
1059a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1060a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1061a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1062a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                status_t result = dev->set_parameters(dev, keyValuePairs.string());
1063a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                final_result = result ?: final_result;
1064a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            }
1065a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
10668abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        }
106759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
106859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        AudioParameter param = AudioParameter(keyValuePairs);
106959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        String8 value;
107059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
1071bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1072bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            if (mBtNrecIsOff != btNrecIsOff) {
107359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
107459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    sp<RecordThread> thread = mRecordThreads.valueAt(i);
1075f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    audio_devices_t device = thread->inDevice();
1076510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1077510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // collect all of the thread's session IDs
1078510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    KeyedVector<int, bool> ids = thread->sessionIds();
1079510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // suspend effects associated with those session IDs
1080510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    for (size_t j = 0; j < ids.size(); ++j) {
1081510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                        int sessionId = ids.keyAt(j);
108259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_AEC,
108359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1084510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
108559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_NS,
108659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1087510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
108859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    }
108959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                }
1090bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent                mBtNrecIsOff = btNrecIsOff;
109159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent            }
109259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        }
109328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        String8 screenState;
109428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
109528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            bool isOff = screenState == "off";
109681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isOff != (AudioFlinger::mScreenState & 1)) {
109781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
109828ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            }
109928ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        }
1100799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        return final_result;
110165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
110265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
110365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // hold a strong ref on thread in case closeOutput() or closeInput() is called
110465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // and the thread is exited once the lock is released
110565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ThreadBase> thread;
110665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
110765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
110865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(ioHandle);
1109d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten        if (thread == 0) {
111065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            thread = checkRecordThread_l(ioHandle);
11117fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten        } else if (thread == primaryPlaybackThread_l()) {
11127c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // indicate output device change to all input threads for pre processing
11137c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            AudioParameter param = AudioParameter(keyValuePairs);
11147c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int value;
111589d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent            if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
111689d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent                    (value != 0)) {
1117054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                broacastParametersToRecordThreads_l(keyValuePairs);
11187c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
111965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
112065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
11217378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (thread != 0) {
11227378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten        return thread->setParameters(keyValuePairs);
112365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
112465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
112565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
112665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
112865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1129827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1130827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
113165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1132a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
1133a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
1134142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
1135fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        String8 out_s8;
1136fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1137799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
11388abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            char *s;
11398abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            {
11408abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            AutoMutex lock(mHardwareLock);
11418abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_GET_PARAMETER;
1142a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
11438abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            s = dev->get_parameters(dev, keys.string());
11448abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_IDLE;
11458abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            }
1146ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman            out_s8 += String8(s ? s : "");
1147799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            free(s);
1148799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        }
1149fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return out_s8;
115065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
115165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
115265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
115365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
115465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getParameters(keys);
115565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
115665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
115765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
115865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getParameters(keys);
115965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
116065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return String8("");
116165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
116265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1163dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1164dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask) const
116565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1166a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1167a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1168a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return 0;
1169a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1170cdf924a32579e92ea456e8e7d5c14ed5920b620fEric Laurent    if ((sampleRate == 0) ||
1171fdb3c07db5d44535eb8c3ec46dc78ad8446c01ebPhil Burk            !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
1172cdf924a32579e92ea456e8e7d5c14ed5920b620fEric Laurent            !audio_is_input_channel(channelMask)) {
11736770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        return 0;
11746770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
1175a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
11762b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
11772b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
11786770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    audio_config_t config, proposed;
11796770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    memset(&proposed, 0, sizeof(proposed));
11806770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.sample_rate = sampleRate;
11816770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.channel_mask = channelMask;
11826770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.format = format;
1183ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1184ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
11856770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    size_t frames;
11866770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    for (;;) {
11876770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // Note: config is currently a const parameter for get_input_buffer_size()
11886770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // but we use a copy from proposed in case config changes from the call.
11896770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        config = proposed;
11906770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        frames = dev->get_input_buffer_size(dev, &config);
11916770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        if (frames != 0) {
11926770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            break; // hal success, config is the result
11936770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        }
11946770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // change one parameter of the configuration each iteration to a more "common" value
11956770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // to see if the device will support it.
11966770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
11976770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            proposed.format = AUDIO_FORMAT_PCM_16_BIT;
11986770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
11996770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            proposed.sample_rate = 44100;           // legacy AudioRecord.java. TODO: Query hw?
12006770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        } else {
12016770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
12026770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                    "format %#x, channelMask 0x%X",
12036770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                    sampleRate, format, channelMask);
12046770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            break; // retries failed, break out of loop with frames == 0.
12056770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        }
12066770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
12072b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_IDLE;
12086770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    if (frames > 0 && config.sample_rate != sampleRate) {
12096770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
12106770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
12116770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    return frames; // may be converted to bytes at the Java level.
121265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
121365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
12145f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
121565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
121665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
121765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
121865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
121965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
122065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getInputFramesLost();
122165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
122265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return 0;
122365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
122465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
122565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value)
122665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1227a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1228a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1229a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
1230a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1231a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
123265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
123365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
123465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
123565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
123665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
123765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
1238ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
12398abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten    mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
1240ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_voice_volume(dev, value);
124165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
124265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
124365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
124465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
124565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1246377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
124772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output) const
124865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
124965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status;
125065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
125265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(output);
125465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
125565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getRenderPosition(halFrames, dspFrames);
125665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
125765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
125965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
126065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
126165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
126265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
126365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
126444622db93a24219123facbbe14259000f90de728Eric Laurent    if (client == 0) {
126544622db93a24219123facbbe14259000f90de728Eric Laurent        return;
126644622db93a24219123facbbe14259000f90de728Eric Laurent    }
12677c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    pid_t pid = IPCThreadState::self()->getCallingPid();
1268021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1269021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
1270021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(pid) < 0) {
1271021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            sp<NotificationClient> notificationClient = new NotificationClient(this,
1272021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                client,
1273021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                pid);
1274021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
127565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1276021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mNotificationClients.add(pid, notificationClient);
127765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
127806b46062d2f8bc82ca3061a23d197734ae51918bMarco Nelissen            sp<IBinder> binder = IInterface::asBinder(client);
1279021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            binder->linkToDeath(notificationClient);
1280021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
1281021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
128265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1283021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
1284fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent    // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
12857c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    // the config change is always sent from playback or record threads to avoid deadlock
12867c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    // with AudioSystem::gLock
12877c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
12887c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
12897c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    }
129065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
12917c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
12927c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
129365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
129465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
129565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
129665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid)
129765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
129865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
1299021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1300021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
1301021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        mNotificationClients.removeItem(pid);
1302021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
13033a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
13043856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("%d died, releasing its sessions", pid);
13058d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    size_t num = mAudioSessionRefs.size();
13063a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    bool removed = false;
13078d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i< num; ) {
13083a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1309012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        ALOGV(" pid %d @ %d", ref->mPid, i);
1310012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        if (ref->mPid == pid) {
1311012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten            ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
13123a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            mAudioSessionRefs.removeAt(i);
13133a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            delete ref;
13143a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            removed = true;
13153a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            num--;
13168d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        } else {
13178d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten            i++;
13183a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        }
13193a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
13203a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (removed) {
13213a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        purgeStaleEffects_l();
13223a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
132365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
132465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
132573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurentvoid AudioFlinger::ioConfigChanged(audio_io_config_event event,
13267c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                                   const sp<AudioIoDescriptor>& ioDesc,
13277c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                                   pid_t pid)
132865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1329021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _l(mClientLock);
1330021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    size_t size = mNotificationClients.size();
133165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < size; i++) {
13327c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
13337c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent            mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
13347c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        }
133565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
133665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
133765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1338021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// removeClient_l() must be called with AudioFlinger::mClientLock held
133965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid)
134065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1341827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("removeClient_l() pid %d, calling pid %d", pid,
134285ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            IPCThreadState::self()->getCallingPid());
134365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mClients.removeItem(pid);
134465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
134565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1346717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held
1347717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1348717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{
1349717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    sp<PlaybackThread> thread;
1350717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1351717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1352717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1353717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            ALOG_ASSERT(thread == 0);
1354717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            thread = mPlaybackThreads.valueAt(i);
1355717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        }
1356717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    }
1357717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1358717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    return thread;
1359717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent}
136065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
136365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
136465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
136581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
136681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   RefBase(),
136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger(audioFlinger),
1368d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten        mPid(pid)
136965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1370da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    size_t heapSize = kClientSharedHeapSizeBytes;
1371da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    // Increase heap size on non low ram devices to limit risk of reconnection failure for
1372da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    // invalidated tracks
1373da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    if (!audioFlinger->isLowRamDevice()) {
1374da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent        heapSize *= kClientSharedHeapSizeMultiplier;
1375da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    }
1376da73b6c7474aaa5616f0214e238776f12717f32bEric Laurent    mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
137765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
137865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1379021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// Client destructor must be called with AudioFlinger::mClientLock held
138081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client()
138165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeClient_l(mPid);
138365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
138465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const
138665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mMemoryDealer;
138865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
138965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
139181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
139281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     const sp<IAudioFlingerClient>& client,
139481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     pid_t pid)
139581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
1396896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{
139765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
139865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
139981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient()
140065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
140165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
140265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
14030f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
140465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
140581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NotificationClient> keep(this);
140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeNotificationClient(mPid);
140781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
140865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
140965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
141165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1412893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brownstatic bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1413893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown    return audio_is_remote_submix_device(inDevice);
1414893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown}
1415893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
141681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord(
141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t input,
141881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
142081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
1421be71aa29a3c86d2e01cd17839d2a72ab09a1bce5Svet Ganov        const String16& opPackageName,
142274935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
1423eeca32671896739e84050da5992d5f151a1629deGlenn Kasten        IAudioFlinger::track_flags_t *flags,
142481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
14254cb668392ee0433462251afbee109405c6efacc8Jean-Michel Trivi        int clientUid,
142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *sessionId,
14277df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten        size_t *notificationFrames,
1428d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& cblk,
1429d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& buffers,
143081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
14311d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{
143281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread::RecordTrack> recordTrack;
143381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordHandle> recordHandle;
143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Client> client;
143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int lSessionId;
14371d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1438d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk.clear();
1439d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers.clear();
1440d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
1441dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen    const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1442dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen    if (!isTrustedCallingUid(callingUid)) {
1443879db19219c120d424f7d7ad7164c3cfe56124e4Andy Hung        ALOGW_IF((uid_t)clientUid != callingUid,
1444dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen                "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1445dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen        clientUid = callingUid;
1446dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen    }
1447dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen
144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check calling permissions
1449dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen    if (!recordingAllowed(opPackageName, tid, clientUid)) {
1450e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten        ALOGE("openRecord() permission denied: recording not allowed");
145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
14541d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
145553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createRecordTrack_l()
145653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
145753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid sample rate %u", sampleRate);
145853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
145953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
146053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
146153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
14626770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    // we don't yet support anything other than linear PCM
14636770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
1464cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("openRecord() invalid format %#x", format);
1465291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        lStatus = BAD_VALUE;
146653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
146753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
146853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
146953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createRecordTrack_l()
147053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_input_channel(channelMask)) {
147153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid channel mask %#x", channelMask);
147253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
1473291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        goto Exit;
1474291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
1475291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten
147605997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten    {
147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
1478e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        RecordThread *thread = checkRecordThread_l(input);
147981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
1480e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() checkRecordThread_l failed");
148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
14831d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent        }
14841d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
14858d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
1486021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
1487feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1488aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
1489eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1490eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                lStatus = BAD_VALUE;
1491eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten                goto Exit;
1492eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            }
149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = *sessionId;
1494feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        } else {
1495570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            // if no audio session id is provided, create one here
1496eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten            lSessionId = nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId != NULL) {
149881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                *sessionId = lSessionId;
149981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1500feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        }
1501aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
1502570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten
150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
15047df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten                                                  frameCount, lSessionId, notificationFrames,
15054cb668392ee0433462251afbee109405c6efacc8Jean-Michel Trivi                                                  clientUid, flags, tid, &lStatus);
150603e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
15071b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
15081b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        if (lStatus == NO_ERROR) {
15091b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // Check if one effect chain was awaiting for an AudioRecord to be created on this
15101b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // session and move it to this thread.
15111b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
15121b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (chain != 0) {
15131b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                Mutex::Autolock _l(thread->mLock);
15141b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                thread->addEffectChain_l(chain);
15151b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
15161b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        }
1517feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1518e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
151981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
152081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove local strong reference to Client before deleting the RecordTrack so that the
1521021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
1522fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // Don't hold mClientLock when releasing the reference on the track as the
1523fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // destructor will acquire it.
1524fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        {
1525fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
1526fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
1527fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
152881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack.clear();
152981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
1530feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1531feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1532d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk = recordTrack->getCblk();
1533d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers = recordTrack->getBuffers();
1534d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
15352fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten    // return handle to client
153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordHandle = new RecordHandle(recordTrack);
1537feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
153881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
15399156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return recordHandle;
1541feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent}
1542feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1543feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
15441d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
154581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name)
154859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
154944622db93a24219123facbbe14259000f90de728Eric Laurent    if (name == NULL) {
155044622db93a24219123facbbe14259000f90de728Eric Laurent        return 0;
155144622db93a24219123facbbe14259000f90de728Eric Laurent    }
155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!settingsAllowed()) {
155381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
155481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
155559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
155681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return loadHwModule_l(name);
155759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
155859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held
156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
156159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
156281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("loadHwModule() module %s already loaded", name);
156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mAudioHwDevs.keyAt(i);
156659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
156759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
156859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *dev;
157059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
157181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int rc = load_audio_interface(name, &dev);
157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() error %d loading module %s ", rc, name);
157481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
157559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
157659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_INIT;
157881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    rc = dev->init_check(dev);
157981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
158081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
158359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
158459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Check and cache this HAL's level of support for master mute and master
158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volume.  If this is the first HAL opened, and it supports the get
158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // methods, use the initial values provided by the HAL as the current
158881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // master mute and volume settings.
158959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {  // scope for auto-lock pattern
159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mHardwareLock);
159359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (0 == mAudioHwDevs.size()) {
159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
159681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_volume) {
159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float mv;
159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_volume(dev, &mv)) {
159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterVolume = mv;
160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
160281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
160481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_mute) {
160581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool mm;
160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_mute(dev, &mm)) {
160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterMute = mm;
160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
161059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_volume) &&
161481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_volume(dev, mMasterVolume))) {
161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
161759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
161859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
162081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_mute) &&
162181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_mute(dev, mMasterMute))) {
162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
162381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
162481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
162559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
162681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_IDLE;
162759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
162859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
1629eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    audio_module_handle_t handle = nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
163083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent          name, dev->common.module->name, dev->common.module->id, handle);
163481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
163681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
163759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
163859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
164081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate()
164259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
164359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
164581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->sampleRate() : 0;
1646a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent}
164759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
164881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount()
1649a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{
165081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
165181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
165281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->frameCountHAL() : 0;
165359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
165459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
165565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
165665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16574182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
16584182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{
16594182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    uid_t uid = IPCThreadState::self()->getCallingUid();
16604182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (uid != AID_SYSTEM) {
16614182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return PERMISSION_DENIED;
16624182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
16634182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    Mutex::Autolock _l(mLock);
16644182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (mIsDeviceTypeKnown) {
16654182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return INVALID_OPERATION;
16664182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
16674182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsLowRamDevice = isLowRamDevice;
16684182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsDeviceTypeKnown = true;
16694182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    return NO_ERROR;
16704182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten}
16714182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
167293c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurentaudio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
167393c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent{
167493c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    Mutex::Autolock _l(mLock);
1675fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1676fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1677fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (index >= 0) {
1678fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1679fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent              mHwAvSyncIds.valueAt(index), sessionId);
1680fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return mHwAvSyncIds.valueAt(index);
1681fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1682fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1683fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1684fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (dev == NULL) {
1685fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return AUDIO_HW_SYNC_INVALID;
1686fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1687fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1688fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    AudioParameter param = AudioParameter(String8(reply));
1689fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    free(reply);
1690fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1691fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    int value;
1692fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1693fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1694fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return AUDIO_HW_SYNC_INVALID;
1695fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1696fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1697fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    // allow only one session for a given HW A/V sync ID.
1698fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1699fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1700fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1701fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent                  value, mHwAvSyncIds.keyAt(i));
1702fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            mHwAvSyncIds.removeItemsAt(i);
1703fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            break;
1704fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        }
1705fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1706fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1707fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    mHwAvSyncIds.add(sessionId, value);
1708fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
170993c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
171093c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1711fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        uint32_t sessions = thread->hasAudioSession(sessionId);
1712fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        if (sessions & PlaybackThread::TRACK_SESSION) {
1713fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            AudioParameter param = AudioParameter();
1714fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1715fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            thread->setParameters(param.toString());
171693c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            break;
171793c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        }
171893c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    }
1719fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1720fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1721fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    return (audio_hw_sync_t)value;
1722fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent}
1723fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
172472e3f39146fce4686bd96f11057c051bea376dfbEric Laurentstatus_t AudioFlinger::systemReady()
172572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent{
172672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    Mutex::Autolock _l(mLock);
172772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    ALOGI("%s", __FUNCTION__);
172872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    if (mSystemReady) {
172972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ALOGW("%s called twice", __FUNCTION__);
173072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        return NO_ERROR;
173172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
173272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    mSystemReady = true;
173372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
173472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
173572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        thread->systemReady();
173672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
173772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
173872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
173972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        thread->systemReady();
174072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    }
174172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    return NO_ERROR;
174272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent}
174372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
1744fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1745fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurentvoid AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1746fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent{
1747fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1748fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (index >= 0) {
1749fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1750fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1751fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        AudioParameter param = AudioParameter();
1752fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1753fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        thread->setParameters(param.toString());
1754fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
175593c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent}
175693c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent
1757fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
17584182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ----------------------------------------------------------------------------
17594182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
176065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
176183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
1762cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_io_handle_t *output,
1763cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_config_t *config,
1764cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_devices_t devices,
1765cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            const String8& address,
176683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                            audio_output_flags_t flags)
176783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
1768cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
17696e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (outHwDev == NULL) {
1770cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return 0;
17716e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
177265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
177381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
1774eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten
1775cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*output == AUDIO_IO_HANDLE_NONE) {
1776eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1777eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    } else {
1778eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        // Audio Policy does not currently request a specific output handle.
1779eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        // If this is ever needed, see openInput_l() for example code.
1780eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1781eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        return 0;
1782cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    }
178365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
178481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
178565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17866146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    // FOR TESTING ONLY:
17879a59276fb465e492138e0576523b54079671e8f4Andy Hung    // This if statement allows overriding the audio policy settings
17889a59276fb465e492138e0576523b54079671e8f4Andy Hung    // and forcing a specific format or channel mask to the HAL/Sink device for testing.
17899a59276fb465e492138e0576523b54079671e8f4Andy Hung    if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
17909a59276fb465e492138e0576523b54079671e8f4Andy Hung        // Check only for Normal Mixing mode
17919a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (kEnableExtendedPrecision) {
17929a59276fb465e492138e0576523b54079671e8f4Andy Hung            // Specify format (uncomment one below to choose)
17939a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_FLOAT;
17949a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
17959a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_32_BIT;
17969a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
1797cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
17989a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
17999a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (kEnableExtendedChannels) {
18009a59276fb465e492138e0576523b54079671e8f4Andy Hung            // Specify channel mask (uncomment one below to choose)
18019a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->channel_mask = audio_channel_out_mask_from_count(4);  // for USB 4ch
18029a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->channel_mask = audio_channel_mask_from_representation_and_bits(
18039a59276fb465e492138e0576523b54079671e8f4Andy Hung            //        AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1);  // another 4ch example
18049a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
18056146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    }
18066146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung
1807062e67a26e0553dd142be622821f493df541f0c6Phil Burk    AudioStreamOut *outputStream = NULL;
1808062e67a26e0553dd142be622821f493df541f0c6Phil Burk    status_t status = outHwDev->openOutputStream(
1809062e67a26e0553dd142be622821f493df541f0c6Phil Burk            &outputStream,
1810062e67a26e0553dd142be622821f493df541f0c6Phil Burk            *output,
1811062e67a26e0553dd142be622821f493df541f0c6Phil Burk            devices,
1812062e67a26e0553dd142be622821f493df541f0c6Phil Burk            flags,
1813062e67a26e0553dd142be622821f493df541f0c6Phil Burk            config,
1814062e67a26e0553dd142be622821f493df541f0c6Phil Burk            address.string());
181565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
181681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
181758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
1818062e67a26e0553dd142be622821f493df541f0c6Phil Burk    if (status == NO_ERROR) {
181965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
18203255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        PlaybackThread *thread;
1821bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
182272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
1823cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
18246146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
182583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                || !isValidPcmSinkFormat(config->format)
18269a59276fb465e492138e0576523b54079671e8f4Andy Hung                || !isValidPcmSinkChannelMask(config->channel_mask)) {
182772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
1828cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
182981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
183072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
1831cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
183265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
1833cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        mPlaybackThreads.add(*output, thread);
183483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        return thread;
183583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
183683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
183783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    return 0;
183883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
183983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1840cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurentstatus_t AudioFlinger::openOutput(audio_module_handle_t module,
1841cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_io_handle_t *output,
1842cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_config_t *config,
1843cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_devices_t *devices,
1844cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  const String8& address,
1845cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  uint32_t *latencyMs,
1846cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_output_flags_t flags)
184783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
1848062e67a26e0553dd142be622821f493df541f0c6Phil Burk    ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
184983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent              module,
1850cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              (devices != NULL) ? *devices : 0,
1851cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->sample_rate,
1852cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->format,
1853cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->channel_mask,
185483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent              flags);
185583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1856cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*devices == AUDIO_DEVICE_NONE) {
1857cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return BAD_VALUE;
185883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
185983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
186083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
186183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1862cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
186383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (thread != 0) {
1864cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *latencyMs = thread->latency();
186565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
186681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new output creation
186773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
186865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the first primary output opened designates the primary hw device
187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGI("Using module %d has the primary audio interface", module);
187283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AutoMutex lock(mHardwareLock);
187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_SET_MODE;
187683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1879cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return NO_ERROR;
188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
188165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1882cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    return NO_INIT;
188365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
188465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t output2)
188765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread1 = checkMixerThread_l(output1);
189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread2 = checkMixerThread_l(output2);
189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread1 == NULL || thread2 == NULL) {
189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                output2);
1895142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        return AUDIO_IO_HANDLE_NONE;
189665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
189765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1898eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
189972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent    DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
190081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->addOutputTrack(thread2);
190181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPlaybackThreads.add(id, thread);
190281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // notify client processes of the new output creation
190373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
190481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return id;
190565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
190665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
190781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output)
19082bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{
190981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeOutput_nonvirtual(output);
19102bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi}
19112bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi
191281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
191365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
191481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the playback thread so that
191581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
191681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<PlaybackThread> thread;
191781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
191881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
191981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkPlaybackThread_l(output);
192081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
192181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
192265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
192365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
192481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeOutput() %d", output);
1925de070137f11d346fba77605bd76a44c040a618fcEric Laurent
192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread->type() == ThreadBase::MIXER) {
192781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1928f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent                if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
192981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    DuplicatingThread *dupThread =
193081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    dupThread->removeOutputTrack((MixerThread *)thread.get());
1932de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
1933de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
1934de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
1935bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1936bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.removeItem(output);
1938bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // save all effects to the default thread
1939bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mPlaybackThreads.size()) {
1940bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1941bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (dstThread != NULL) {
1942bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // audioflinger lock is held here so the acquisition order of thread locks does not
1943bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // matter
1944bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _dl(dstThread->mLock);
1945bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _sl(thread->mLock);
1946bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1947bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
1948bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
1949bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
1950bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1951bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
195273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
195373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        ioDesc->mIoHandle = output;
195473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
19553acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
195681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
19593acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
1960f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    if (!thread->isDuplicating()) {
196183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        closeOutputFinish(thread);
196265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
196383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
196565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
196665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
196783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
196883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
196983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    AudioStreamOut *out = thread->clearOutput();
197083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
197183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // from now on thread->mOutput is NULL
197283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    out->hwDev()->close_output_stream(out->hwDev(), out->stream);
197383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    delete out;
197483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
197583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
197683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
197783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
197883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mPlaybackThreads.removeItem(thread->mId);
197983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    thread->exit();
198083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeOutputFinish(thread);
198183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
198283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
198381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output)
1984e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{
198581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
198781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
198981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1990e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent    }
1991e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("suspendOutput() %d", output);
199381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->suspend();
1994e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
199665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
199765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output)
199965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
20006637baae4244aec731c4014da72418d330636ae1Glenn Kasten    Mutex::Autolock _l(mLock);
200181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
200265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
200381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
200481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
2005ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
200665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
200781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("restoreOutput() %d", output);
200865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
200981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->restore();
201065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
201181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
201265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
201365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2014cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurentstatus_t AudioFlinger::openInput(audio_module_handle_t module,
2015cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_io_handle_t *input,
2016cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_config_t *config,
2017e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten                                          audio_devices_t *devices,
2018cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          const String8& address,
2019cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_source_t source,
2020ec40d284218466d8f0e832e7eb88e6ea6c479c88Glenn Kasten                                          audio_input_flags_t flags)
202165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
202283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
202383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
2024e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    if (*devices == AUDIO_DEVICE_NONE) {
2025cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return BAD_VALUE;
202683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
202783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
2028e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
202983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
203083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (thread != 0) {
203183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        // notify client processes of the new input creation
203273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        thread->ioConfigChanged(AUDIO_INPUT_OPENED);
2033cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return NO_ERROR;
2034b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent    }
2035cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    return NO_INIT;
203683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
2037b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent
203883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentsp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
2039cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_io_handle_t *input,
2040cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_config_t *config,
2041e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten                                                         audio_devices_t devices,
2042cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         const String8& address,
2043cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_source_t source,
204483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                         audio_input_flags_t flags)
204583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
2046e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
20476e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (inHwDev == NULL) {
2048cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *input = AUDIO_IO_HANDLE_NONE;
204981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
20506e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2051fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
2052eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    // Audio Policy can request a specific handle for hardware hotword.
2053eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    // The goal here is not to re-open an already opened input.
2054eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    // It is to use a pre-assigned I/O handle.
2055cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*input == AUDIO_IO_HANDLE_NONE) {
2056eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2057eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2058eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        ALOGE("openInput_l() requested input handle %d is invalid", *input);
2059eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        return 0;
2060eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2061eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        // This should not happen in a transient state with current design.
2062eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2063eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        return 0;
2064cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    }
2065b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
2066cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_config_t halconfig = *config;
2067cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_hw_device_t *inHwHal = inHwDev->hwDevice();
20683255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_in_t *inStream = NULL;
2069e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
2070cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                        &inStream, flags, address.string(), source);
2071cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
2072fd4c14883b268a0bc5514da135fe6b7d1ce2071bJean-Michel Trivi           ", Format %#x, Channels %x, flags %#x, status %d addr %s",
207381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            inStream,
2074cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.sample_rate,
2075cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.format,
2076cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.channel_mask,
2077ec40d284218466d8f0e832e7eb88e6ea6c479c88Glenn Kasten            flags,
2078fd4c14883b268a0bc5514da135fe6b7d1ce2071bJean-Michel Trivi            status, address.string());
207965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the input could not be opened with the requested parameters and we can handle the
20816770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    // conversion internally, try to open again with the proposed parameters.
208281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == BAD_VALUE &&
20836770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        audio_is_linear_pcm(config->format) &&
20846770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        audio_is_linear_pcm(halconfig.format) &&
20856770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
2086cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2087cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
20888594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME describe the change proposed by HAL (save old values so we can log them here)
2089cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inStream = NULL;
2091e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten        status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
2092cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                            &inStream, flags, address.string(), source);
20938594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME log this new status; HAL should not propose any further changes
209465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
209565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
209681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && inStream != NULL) {
209758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
209846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // or (re-)create if current Pipe is idle and does not match the new format
210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<NBAIO_Sink> teeSink;
210281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        enum {
210381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NO,    // don't copy input
210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NEW,   // copy input using a new pipe
210581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_OLD,   // copy input using an existing pipe
210681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } kind;
2107329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten        NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2108329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten                audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
2109da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (!mTeeSinkInputEnabled) {
2110da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            kind = TEE_SINK_NO;
21116e0d67d7b496ce17c0970a4ffd3a6f808860949cGlenn Kasten        } else if (!Format_isValid(format)) {
211281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink == 0) {
211481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
211581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink->getStrongCount() != 1) {
211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
2117f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten        } else if (Format_isEqual(format, mRecordTeeSink->format())) {
211881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_OLD;
21194adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten        } else {
212081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
212281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        switch (kind) {
212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NEW: {
2124da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numCounterOffers = 0;
212681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const NBAIO_Format offers[1] = {format};
212781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
212881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
212981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            PipeReader *pipeReader = new PipeReader(*pipe);
213081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            numCounterOffers = 0;
213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
213381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSink = pipe;
213481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSource = pipeReader;
213581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = pipe;
21364adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten            }
213781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
213881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_OLD:
213981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = mRecordTeeSink;
214081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
214181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NO:
214281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        default:
214381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
214458912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
214546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2146da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2147cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
214865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
214981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Start record thread
215034af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten        // RecordThread requires both input and output device indication to forward to audio
215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // pre processing modules
215283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        sp<RecordThread> thread = new RecordThread(this,
2153cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  inputStream,
2154cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  *input,
2155d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                  primaryOutputDevice_l(),
215672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                                  devices,
215772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                                  mSystemReady
215846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
215946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  , teeSink
216046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
216146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  );
2162cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        mRecordThreads.add(*input, thread);
2163cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
216483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        return thread;
21651afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent    }
216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2167cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    *input = AUDIO_IO_HANDLE_NONE;
216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
216965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
217065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input)
217265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
217381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeInput_nonvirtual(input);
217465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
217565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2177de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the record thread so that
217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
218081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread> thread;
218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
2186de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
218781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
218881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeInput() %d", input);
21891b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
21901b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // If we still have effect chains, it means that a client still holds a handle
21911b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // on at least one effect. We must either move the chain to an existing thread with the
21921b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // same session ID or put it aside in case a new record thread is opened for a
21931b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // new capture on the same session
21941b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        sp<EffectChain> chain;
2195aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        {
2196aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            Mutex::Autolock _sl(thread->mLock);
2197aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
21981b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // Note: maximum one chain per record thread
21991b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (effectChains.size() != 0) {
22001b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                chain = effectChains[0];
22011b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
22021b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        }
22031b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        if (chain != 0) {
22041b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // first check if a record thread is already opened with a client on the same session.
22051b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // This should only happen in case of overlap between one thread tear down and the
22061b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // creation of its replacement
22071b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            size_t i;
22081b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            for (i = 0; i < mRecordThreads.size(); i++) {
22091b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                sp<RecordThread> t = mRecordThreads.valueAt(i);
22101b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                if (t == thread) {
22111b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    continue;
22121b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                }
22131b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                if (t->hasAudioSession(chain->sessionId()) != 0) {
22141b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    Mutex::Autolock _l(t->mLock);
22151b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    ALOGV("closeInput() found thread %d for effect session %d",
22161b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                          t->id(), chain->sessionId());
22171b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    t->addEffectChain_l(chain);
22181b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    break;
22191b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                }
22201b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
22211b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // put the chain aside if we could not find a record thread with the same session id.
22221b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (i == mRecordThreads.size()) {
22231b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                putOrphanEffectChain_l(chain);
2224aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
2225aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
222673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
222773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        ioDesc->mIoHandle = input;
222873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.removeItem(input);
2230de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
223183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
223283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // we have a different lock for notification client
223383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeInputFinish(thread);
223483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    return NO_ERROR;
223583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
2236de070137f11d346fba77605bd76a44c040a618fcEric Laurent
223783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeInputFinish(sp<RecordThread> thread)
223883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
223983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    thread->exit();
224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *in = thread->clearInput();
224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // from now on thread->mInput is NULL
224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    in->hwDev()->close_input_stream(in->hwDev(), in->stream);
224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete in;
224583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
224665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
224783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
224883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
224983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mRecordThreads.removeItem(thread->mId);
225083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeInputFinish(thread);
2251b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
2252b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
2253d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kastenstatus_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
2254b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{
2255b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    Mutex::Autolock _l(mLock);
2256d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten    ALOGV("invalidateStream() stream %d", stream);
2257b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->invalidateTracks(stream);
2261b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    }
226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
2264b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
2265b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2267eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kastenaudio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
2268162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{
2269eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    return nextUniqueId(use);
2270162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent}
2271162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent
2272d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
2273a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{
2274a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    Mutex::Autolock _l(mLock);
227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2276d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2277d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2278d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2279d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
2280d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
2281021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
2282021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
2283021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Ignore requests received from processes not known as notification client. The request
2284021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2285021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // called from a different pid leaving a stale session reference.  Also we don't know how
2286021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // to clear this reference if the client process dies.
2287021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(caller) < 0) {
2288021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2289021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            return;
2290021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
2291d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
2292d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt++;
229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" incremented refcount to %d", ref->mCnt);
229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
2300a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
2301a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    }
230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV(" added new entry for %d", audioSession);
2304a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent}
2305a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
2306d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
230744a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{
230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2310d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2311d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2312d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2313d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt--;
231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" decremented refcount to %d", ref->mCnt);
232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mCnt == 0) {
232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioSessionRefs.removeAt(i);
232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                delete ref;
232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                purgeStaleEffects_l();
232444a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent            }
232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
232644a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent        }
232744a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent    }
2328d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // If the caller is mediaserver it is likely that the session being released was acquired
2329d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // on behalf of a process not in notification clients and we ignore the warning.
2330d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
233144a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent}
233244a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent
233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() {
233465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("purging stale effects");
2336fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten
233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > chains;
233858912562617941964939a4182cda71eaeb153d4bGlenn Kasten
233981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
234181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
234481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chains.push(ec);
234581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2346c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
234758912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
234881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
234981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread> t = mRecordThreads.valueAt(i);
235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chains.push(ec);
235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2354300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten    }
235565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < chains.size(); i++) {
235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> ec = chains[i];
235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionid = ec->sessionId();
235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> t = ec->mThread.promote();
236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numsessionrefs = mAudioSessionRefs.size();
236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool found = false;
236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t k = 0; k < numsessionrefs; k++) {
236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mSessionid == sessionid) {
236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV(" session %d still exists for %d with %d refs",
236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sessionid, ref->mPid, ref->mCnt);
237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                found = true;
237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
237258912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
237358912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!found) {
2375e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            Mutex::Autolock _l(t->mLock);
237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // remove all effects from the chain
237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            while (ec->mEffects.size()) {
237881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectModule> effect = ec->mEffects[0];
237981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->unPin();
238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                t->removeEffect_l(effect);
238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (effect->purgeHandles()) {
238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioSystem::unregisterEffect(effect->id());
238581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2386c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
238758912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return;
238965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
239065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2391eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten// checkThread_l() must be called with AudioFlinger::mLock held
2392eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn KastenAudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2393eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten{
2394eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    ThreadBase *thread = NULL;
2395eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    switch (audio_unique_id_get_use(ioHandle)) {
2396eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    case AUDIO_UNIQUE_ID_USE_OUTPUT:
2397eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        thread = checkPlaybackThread_l(ioHandle);
2398eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        break;
2399eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    case AUDIO_UNIQUE_ID_USE_INPUT:
2400eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        thread = checkRecordThread_l(ioHandle);
2401eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        break;
2402eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    default:
2403eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten        break;
2404eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    }
2405eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    return thread;
2406eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten}
2407eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten
240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
240981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
2410190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{
241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mPlaybackThreads.valueFor(output).get();
2412190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten}
2413190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
241481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held
241581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
241681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
241781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
241881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2420190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
242181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held
242281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
242381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
242481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mRecordThreads.valueFor(input).get();
242581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2426190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
2427eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kastenaudio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
242881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
2429eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    int32_t base = android_atomic_add(AUDIO_UNIQUE_ID_USE_MAX, &mNextUniqueId);
2430eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    // We have no way of recovering from wraparound
2431eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    LOG_ALWAYS_FATAL_IF(base == 0, "unique ID overflow");
2432eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
2433eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2434eeecb980ff4c202d0a3c4b0bfe040dce2f73336dGlenn Kasten    return (audio_unique_id_t) (base | use);
243581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
243683efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten
243781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
243837d825e72a6c606553a745da1212590a425996d3Glenn Kasten{
243981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
244081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
2441f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        if(thread->isDuplicating()) {
2442f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent            continue;
2443f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent        }
244481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *output = thread->getOutput();
244581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
244681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return thread;
244737d825e72a6c606553a745da1212590a425996d3Glenn Kasten        }
244837d825e72a6c606553a745da1212590a425996d3Glenn Kasten    }
244981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NULL;
245037d825e72a6c606553a745da1212590a425996d3Glenn Kasten}
245137d825e72a6c606553a745da1212590a425996d3Glenn Kasten
245281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const
245365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
245481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
2455000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
245681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
245781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
24589f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten    }
2459000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
246081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread->outDevice();
2461000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2462688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten
246381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
246481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int triggerSession,
246581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int listenerSession,
246681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    sync_event_callback_t callBack,
24678ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent                                    wp<RefBase> cookie)
246881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
246981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
247073ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten
247181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
247281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t playStatus = NAME_NOT_FOUND;
247381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t recStatus = NAME_NOT_FOUND;
247481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
247581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
247681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playStatus == NO_ERROR) {
247781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
247881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
247965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
248081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
248181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
248281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (recStatus == NO_ERROR) {
248381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
24849f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten        }
248565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
248681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
248781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPendingSyncEvents.add(event);
248881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
248981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createSyncEvent() invalid event %d", event->type());
249081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        event.clear();
249181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
249281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event;
249365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
249465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
249581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
249681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//  Effect management
249781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
249858912562617941964939a4182cda71eaeb153d4bGlenn Kasten
249958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
250081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
2501000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
250281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
250381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryNumberEffects(numEffects);
2504000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2505000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
250681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
250758912562617941964939a4182cda71eaeb153d4bGlenn Kasten{
250881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
250981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryEffect(index, descriptor);
251058912562617941964939a4182cda71eaeb153d4bGlenn Kasten}
251158912562617941964939a4182cda71eaeb153d4bGlenn Kasten
251281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
251381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *descriptor) const
2514000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
251581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
251681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectGetDescriptor(pUuid, descriptor);
2517000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2518000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
251981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25208d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect(
252181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *pDesc,
252281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
252381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
252481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t io,
252581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
2526be71aa29a3c86d2e01cd17839d2a72ab09a1bce5Svet Ganov        const String16& opPackageName,
252781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status,
252881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *id,
252981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled)
2530000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
253181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus = NO_ERROR;
253281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
253381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc;
2534000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
25358d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
253681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
253781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid, effectClient.get(), priority, sessionId, io);
2538000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
253981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDesc == NULL) {
254081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
254181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2542952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2543000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
254481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check audio settings permission for global effects
254581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
254681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
254781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2548952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2549000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
255081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
255181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that can only be created by audio policy manager (running in same process)
255281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
255381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
255481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2555952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
255665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
255781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
255881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!EffectIsNullUuid(&pDesc->uuid)) {
255981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is specified, request effect descriptor
256081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
256181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
256281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
256381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
256481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
256581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
256681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is not specified, look for an available implementation
256781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // of the required type in effect factory
256881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (EffectIsNullUuid(&pDesc->type)) {
256981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() no effect type");
257081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
257181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2572288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
257381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t numEffects = 0;
257481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect_descriptor_t d;
257581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            d.flags = 0; // prevent compiler warning
257681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool found = false;
2577288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
257881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectQueryNumberEffects(&numEffects);
257981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
258081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
258181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
258281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
258381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (uint32_t i = 0; i < numEffects; i++) {
258481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = EffectQueryEffect(i, &desc);
258581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (lStatus < 0) {
258681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
258781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
2588d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
258981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
259081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If matching type found save effect descriptor. If the session is
259181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // 0 and the effect is not auxiliary, continue enumeration in case
259281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // an auxiliary version of this effect type is available
259381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    found = true;
259481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    d = desc;
259581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
259681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2597d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                        break;
2598d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                    }
2599d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
2600288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
260181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (!found) {
260281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
260381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() effect not found");
260481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
260581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
260681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For same effect type, chose auxiliary version over insert version if
260781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // connect to output mix (Compliance to OpenSL ES)
260881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
260981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
261081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc = d;
261158912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
261258912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
261358912562617941964939a4182cda71eaeb153d4bGlenn Kasten
261481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Do not allow auxiliary effects on a session different from 0 (output mix)
261581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
261681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
261781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = INVALID_OPERATION;
261881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
26193dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent        }
262065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check recording permission for visualizer
262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2623dcb346b7dc5b88c3e85db8a70bbd6a2fee8192b9Marco Nelissen            !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
262481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = PERMISSION_DENIED;
262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
262681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
262765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
262881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return effect descriptor
262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *pDesc = desc;
2630142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2631eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // if the output returned by getOutputForEffect() is removed before we lock the
2632eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2633eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // and we will exit safely
2634eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            io = AudioSystem::getOutputForEffect(&desc);
2635eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            ALOGV("createEffect got output %d", io);
2636eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        }
2637eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent
2638eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        Mutex::Autolock _l(mLock);
263965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
264081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is not specified try to find a matching audio session ID in one of the
264181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // output threads.
264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
264381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // because of code checking output when entering the function.
264481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Note: io is never 0 when creating an effect on an input
2645142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE) {
26465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
26475baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // output must be specified by AudioPolicyManager when using session
26485baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // AUDIO_SESSION_OUTPUT_STAGE
26495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                lStatus = BAD_VALUE;
26505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                goto Exit;
26515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
2652eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // look for the thread where the specified audio session is present
2653eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2654eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2655eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    io = mPlaybackThreads.keyAt(i);
2656eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    break;
2657eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                }
2658d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
265981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0) {
2660eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
2661eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2662eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                        io = mRecordThreads.keyAt(i);
266381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
26642986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
266565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
266665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
266781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If no output thread contains the requested session ID, default to
266881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // first output. The effect chain will be moved to the correct output
266981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // thread when a track with the same session ID is created
2670142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten            if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
267181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                io = mPlaybackThreads.keyAt(0);
267281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
267381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect() got io %d for effect %s", io, desc.name);
267465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
267581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ThreadBase *thread = checkRecordThread_l(io);
267681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
267781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = checkPlaybackThread_l(io);
267881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (thread == NULL) {
267981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGE("createEffect() unknown output thread");
268081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
268181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2682288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
2683aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        } else {
2684aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            // Check if one effect chain was awaiting for an effect to be created on this
2685aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            // session and used it instead of creating a new one.
2686aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2687aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            if (chain != 0) {
26881b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                Mutex::Autolock _l(thread->mLock);
2689aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                thread->addEffectChain_l(chain);
2690aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
2691288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten        }
2692288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
2693021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        sp<Client> client = registerPid(pid);
269458912562617941964939a4182cda71eaeb153d4bGlenn Kasten
269581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect on selected output thread
269681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = thread->createEffect_l(client, effectClient, priority, sessionId,
269781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                &desc, enabled, &lStatus);
269881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (handle != 0 && id != NULL) {
269981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *id = handle->id();
270065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
2701fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        if (handle == 0) {
2702fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            // remove local strong reference to Client with mClientLock held
2703fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
2704fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
2705fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
270665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
270765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
270881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
27099156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
271081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
271166fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten}
271266fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten
271381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
271481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t dstOutput)
271565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
271681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
271781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcOutput, dstOutput);
271865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
271981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcOutput == dstOutput) {
272081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
272181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
272281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
272381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
272481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcThread == NULL) {
272581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad srcOutput %d", srcOutput);
272681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
272781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
272881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
272981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (dstThread == NULL) {
273081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad dstOutput %d", dstOutput);
273181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
273265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
273365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
273481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _dl(dstThread->mLock);
273581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _sl(srcThread->mLock);
27365baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return moveEffectChain_l(sessionId, srcThread, dstThread, false);
273765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
273865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
273981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
274081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffectChain_l(int sessionId,
274181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *srcThread,
274281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *dstThread,
274381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   bool reRegister)
274465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
274581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
274681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcThread, dstThread);
274765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
274881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
274981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
275081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
275181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sessionId, srcThread);
275281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
275381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
27549ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
27559a59276fb465e492138e0576523b54079671e8f4Andy Hung    // Check whether the destination thread has a channel count of FCC_2, which is
27569a59276fb465e492138e0576523b54079671e8f4Andy Hung    // currently required for (most) effects. Prevent moving the effect chain here rather
27579a59276fb465e492138e0576523b54079671e8f4Andy Hung    // than disabling the addEffect_l() call in dstThread below.
2758f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent    if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
2759b10352f4bc11ae6f3d93e464074709555ecf9e06Eric Laurent            dstThread->mChannelCount != FCC_2) {
27609a59276fb465e492138e0576523b54079671e8f4Andy Hung        ALOGW("moveEffectChain_l() effect chain failed because"
27619a59276fb465e492138e0576523b54079671e8f4Andy Hung                " destination thread %p channel count(%u) != %u",
27629a59276fb465e492138e0576523b54079671e8f4Andy Hung                dstThread, dstThread->mChannelCount, FCC_2);
27639a59276fb465e492138e0576523b54079671e8f4Andy Hung        return INVALID_OPERATION;
27649a59276fb465e492138e0576523b54079671e8f4Andy Hung    }
27659a59276fb465e492138e0576523b54079671e8f4Andy Hung
276681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
276781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // so that a new chain is created with correct parameters when first effect is added. This is
276881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
276981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // removed.
277081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    srcThread->removeEffectChain_l(chain);
27719ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
277281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // transfer all effects one by one so that new effect chain is created on new thread with
277381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
277481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> dstChain;
277581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy = 0; // prevent compiler warning
277681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect = chain->getEffectFromId_l(0);
27775baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    Vector< sp<EffectModule> > removed;
27785baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    status_t status = NO_ERROR;
277981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (effect != 0) {
278081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        srcThread->removeEffect_l(effect);
27815baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        removed.add(effect);
27825baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        status = dstThread->addEffect_l(effect);
27835baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (status != NO_ERROR) {
27845baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
27855baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
278681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // removeEffect_l() has stopped the effect if it was active so it must be restarted
278781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect->state() == EffectModule::ACTIVE ||
278881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->state() == EffectModule::STOPPING) {
278981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->start();
279065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
279181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the move request is not received from audio policy manager, the effect must be
279281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // re-registered with the new strategy and output
279381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (dstChain == 0) {
279481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            dstChain = effect->chain().promote();
279581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (dstChain == 0) {
279681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
27975baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                status = NO_INIT;
27985baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                break;
279965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
280081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            strategy = dstChain->strategy();
280165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
280281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (reRegister) {
280381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
280481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::registerEffect(&effect->desc(),
28055baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                        dstThread->id(),
280681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        strategy,
280781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        sessionId,
280881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        effect->id());
2809d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent            AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
281081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
281181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect = chain->getEffectFromId_l(0);
281258912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
281358912562617941964939a4182cda71eaeb153d4bGlenn Kasten
28145baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (status != NO_ERROR) {
28155baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        for (size_t i = 0; i < removed.size(); i++) {
28165baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            srcThread->addEffect_l(removed[i]);
28175baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (dstChain != 0 && reRegister) {
28185baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::unregisterEffect(removed[i]->id());
28195baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::registerEffect(&removed[i]->desc(),
28205baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            srcThread->id(),
28215baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            strategy,
28225baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            sessionId,
28235baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            removed[i]->id());
2824d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent                AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
28255baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
28265baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
28275baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    }
28285baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
28295baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return status;
283065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
283165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
28325baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
2833813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2834813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    if (mGlobalEffectEnableTime != 0 &&
2835813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2836813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        return true;
2837813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2838813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2839813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2840813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<EffectChain> ec =
2841813e2a74853bde19e37d878c596a044b3f299efcEric Laurent                mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
28425baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (ec != 0 && ec->isNonOffloadableEnabled()) {
2843813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            return true;
2844813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2845813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2846813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    return false;
2847813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2848813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
28495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable()
2850813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2851813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    Mutex::Autolock _l(mLock);
2852813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2853813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    mGlobalEffectEnableTime = systemTime();
2854813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2855813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2856813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2857813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        if (t->mType == ThreadBase::OFFLOAD) {
2858813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            t->invalidateTracks(AUDIO_STREAM_MUSIC);
2859813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2860813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2861813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2862813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2863813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2864aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentstatus_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2865aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2866aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    audio_session_t session = (audio_session_t)chain->sessionId();
2867aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2868aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2869aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2870aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2871aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        return ALREADY_EXISTS;
2872aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2873aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    mOrphanEffectChains.add(session, chain);
2874aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return NO_ERROR;
2875aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2876aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2877aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2878aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2879aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    sp<EffectChain> chain;
2880aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2881aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2882aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2883aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        chain = mOrphanEffectChains.valueAt(index);
2884aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        mOrphanEffectChains.removeItemsAt(index);
2885aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2886aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return chain;
2887aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2888aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2889aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentbool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2890aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2891aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    Mutex::Autolock _l(mLock);
2892aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    audio_session_t session = (audio_session_t)effect->sessionId();
2893aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2894aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2895aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2896aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2897aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        if (chain->removeEffect_l(effect) == 0) {
2898aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2899aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            mOrphanEffectChains.removeItemsAt(index);
2900aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
2901aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        return true;
2902aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2903aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return false;
2904aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2905aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2906aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2907da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry {
29082f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn 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
29092f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten    char mFileName[TEE_MAX_FILENAME];
2910da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten};
2911da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2912da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2)
2913da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{
29142f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten    return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
2915da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}
2916da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
291746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2918d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
291965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
2920d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten    NBAIO_Source *teeSource = source.get();
2921fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    if (teeSource != NULL) {
2922da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // .wav rotation
2923da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // There is a benign race condition if 2 threads call this simultaneously.
2924da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // They would both traverse the directory, but the result would simply be
2925da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // failures at unlink() which are ignored.  It's also unlikely since
2926da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // normally dumpsys is only done by bugreport or from the command line.
2927da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        char teePath[32+256];
29289a00399340c7c129714dff96f1ab59045fe43056Glenn Kasten        strcpy(teePath, "/data/misc/audioserver");
2929da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        size_t teePathLen = strlen(teePath);
2930da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        DIR *dir = opendir(teePath);
2931da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teePath[teePathLen++] = '/';
2932da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (dir != NULL) {
29332f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten#define TEE_MAX_SORT 20 // number of entries to sort
29342f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten#define TEE_MAX_KEEP 10 // number of entries to keep
29352f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            struct Entry entries[TEE_MAX_SORT];
2936da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            size_t entryCount = 0;
29372f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            while (entryCount < TEE_MAX_SORT) {
2938da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent de;
2939da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent *result = NULL;
2940da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                int rc = readdir_r(dir, &de, &result);
2941da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (rc != 0) {
2942da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r failed %d", rc);
2943da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2944da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2945da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result == NULL) {
2946da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2947da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2948da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result != &de) {
2949da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2950da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2951da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2952da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                // ignore non .wav file entries
2953da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                size_t nameLen = strlen(de.d_name);
29542f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
2955da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                        strcmp(&de.d_name[nameLen - 4], ".wav")) {
2956da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    continue;
2957da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
29582f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                strcpy(entries[entryCount++].mFileName, de.d_name);
2959da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2960da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            (void) closedir(dir);
29612f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            if (entryCount > TEE_MAX_KEEP) {
2962da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                qsort(entries, entryCount, sizeof(Entry), comparEntry);
29632f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
29642f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                    strcpy(&teePath[teePathLen], entries[i].mFileName);
2965da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    (void) unlink(teePath);
2966da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2967da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2968da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        } else {
2969da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
29709a00399340c7c129714dff96f1ab59045fe43056Glenn Kasten                dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
29719a00399340c7c129714dff96f1ab59045fe43056Glenn Kasten                        strerror(errno));
2972da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2973da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
2974d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        char teeTime[16];
2975fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct timeval tv;
2976fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        gettimeofday(&tv, NULL);
2977fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct tm tm;
2978fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        localtime_r(&tv.tv_sec, &tm);
2979da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2980da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2981da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2982da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
2983fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        if (teeFd >= 0) {
2984329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME use libsndfile
2985fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            char wavHeader[44];
2986fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            memcpy(wavHeader,
2987fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn 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",
2988fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                sizeof(wavHeader));
2989fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            NBAIO_Format format = teeSource->format();
2990fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            unsigned channelCount = Format_channelCount(format);
29913b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten            uint32_t sampleRate = Format_sampleRate(format);
2992329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            size_t frameSize = Format_frameSize(format);
2993fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[22] = channelCount;       // number of channels
2994fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[24] = sampleRate;         // sample rate
2995fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[25] = sampleRate >> 8;
2996329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            wavHeader[32] = frameSize;          // block alignment
2997329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            wavHeader[33] = frameSize >> 8;
2998fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, wavHeader, sizeof(wavHeader));
2999fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            size_t total = 0;
3000fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            bool firstRead = true;
3001329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten#define TEE_SINK_READ 1024                      // frames per I/O operation
3002329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            void *buffer = malloc(TEE_SINK_READ * frameSize);
3003fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            for (;;) {
3004fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                size_t count = TEE_SINK_READ;
3005d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten                ssize_t actual = teeSource->read(buffer, count);
3006fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                bool wasFirstRead = firstRead;
3007fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                firstRead = false;
3008fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                if (actual <= 0) {
3009fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3010fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                        continue;
3011fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    }
3012fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    break;
3013fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                }
3014a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent                ALOG_ASSERT(actual <= (ssize_t)count);
3015329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten                write(teeFd, buffer, actual * frameSize);
3016fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                total += actual;
3017fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            }
3018329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            free(buffer);
3019fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 4, SEEK_SET);
3020329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            uint32_t temp = 44 + total * frameSize - 8;
3021329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME not big-endian safe
3022fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
3023fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 40, SEEK_SET);
3024329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            temp =  total * frameSize;
3025329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME not big-endian safe
3026fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
3027fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            close(teeFd);
3028da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
30298b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "tee copied to %s\n", teePath);
3030da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
3031fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        } else {
3032da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
30338b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
3034da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
3035fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        }
3036fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    }
3037d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten}
303846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
3039d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
304065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
304165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
304265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact(
304365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
304465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
304565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioFlinger::onTransact(code, data, reply, flags);
304665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
304765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
304863238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android
3049