AudioFlinger.cpp revision 1b92868010b5c1409692a86f6b27e4a265b64c1a
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
4865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/EffectsFactoryApi.h>
496d8b694d999e9be7d5dcc336535832a80fb6f61fEric Laurent#include <audio_effects/effect_visualizer.h>
5059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_ns.h>
5159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_aec.h>
5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
533b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten#include <audio_utils/primitives.h>
543b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten
55feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent#include <powermanager/PowerManager.h>
56190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
574ff14bae91075eb274eb1c2975982358946e7e63John Grossman#include <common_time/cc_helper.h>
5858912562617941964939a4182cda71eaeb153d4bGlenn 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>
644182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten#include <private/android_filesystem_config.h>
65da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
6665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
681c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// Note: the following macro is used for extremely verbose logging message.  In
691c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
701c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// 0; but one side effect of this is to turn all LOGV's as well.  Some messages
711c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are so verbose that we want to suppress them even when we have ALOG_ASSERT
721c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// turned on.  Do not uncomment the #def below unless you really know what you
731c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are doing and want to see all of the extremely verbose messages.
741c345196edc61694f29307a1826a64a0d26028dcJohn Grossman//#define VERY_VERY_VERBOSE_LOGGING
751c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#ifdef VERY_VERY_VERBOSE_LOGGING
761c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV ALOGV
771c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#else
781c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV(a...) do { } while(0)
791c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#endif
80de070137f11d346fba77605bd76a44c040a618fcEric Laurent
8165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
8265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
83ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kHardwareLockedString[] = "Hardware lock is taken\n";
85021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentstatic const char kClientLockedString[] = "Client lock is taken\n";
8665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
884ff14bae91075eb274eb1c2975982358946e7e63John Grossmannsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
897cafbb32999049873d4746ba83bd20c88abe6ce6Eric Laurent
9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::mScreenState;
913ed292031dc50c56110cdadb1e3778117e3be76aGlenn Kasten
9246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
93da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkInputEnabled = false;
94da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkOutputEnabled = false;
95da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkTrackEnabled = false;
96da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
97da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
10046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
101da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
102813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
103813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// we define a minimum time during which a global effect is considered enabled.
104813e2a74853bde19e37d878c596a044b3f299efcEric Laurentstatic const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
105813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
10665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
10765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
108b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissenconst char *formatToString(audio_format_t format) {
1092829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    switch (format & AUDIO_FORMAT_MAIN_MASK) {
1102829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_PCM:
1112829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        switch (format) {
1122829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
1132829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
1142829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
1152829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
1162829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
1172829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
1182829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        default:
1192829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad            break;
1202829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        }
1212829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        break;
122b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_MP3: return "mp3";
123b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_NB: return "amr-nb";
124b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_WB: return "amr-wb";
125b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AAC: return "aac";
126b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
127b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
128b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_VORBIS: return "vorbis";
1292829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_OPUS: return "opus";
1302829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_AC3: return "ac-3";
1312829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_E_AC3: return "e-ac-3";
132b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    default:
133b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        break;
134b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
135b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    return "unknown";
136b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen}
137b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
138f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
139799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
140f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    const hw_module_t *mod;
141799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc;
142799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
143f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
144f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
145f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
146f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
147799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
148f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
149f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = audio_hw_device_open(mod, dev);
150f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
151f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
152f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
153799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
154f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
1555806b3533437e3d83208e8e9d6bd74ed304e51ecEric Laurent    if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
156f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
157f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        rc = BAD_VALUE;
158f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        goto out;
159f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
160799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return 0;
161799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
162799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout:
163799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    *dev = NULL;
164799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return rc;
165799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
166799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
16765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
16865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger()
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    : BnAudioFlinger(),
1714ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPrimaryHardwareDev(NULL),
1729ea65d0f4a564478343b1a722fae4ce5883670c3Glenn Kasten      mAudioHwDevs(NULL),
1737d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten      mHardwareStatus(AUDIO_HW_IDLE),
1744ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterVolume(1.0f),
1754ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterMute(false),
1764ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mNextUniqueId(1),
1774ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMode(AUDIO_MODE_INVALID),
1784182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mBtNrecIsOff(false),
1794182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mIsLowRamDevice(true),
180813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mIsDeviceTypeKnown(false),
1814ea00a25cf85877b48ebd1e15a657cfaab29af58Glenn Kasten      mGlobalEffectEnableTime(0),
1824ea00a25cf85877b48ebd1e15a657cfaab29af58Glenn Kasten      mPrimaryOutputSampleRate(0)
18365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
184949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten    getpid_cached = getpid();
1859e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    char value[PROPERTY_VALUE_MAX];
1869e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
1879e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (doLog) {
188cf515ab0030cf5e0bef843638af7c989a8f6843aGlenn Kasten        mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
1899e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
1901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
19146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
192da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    (void) property_get("ro.debuggable", value, "0");
193da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int debuggable = atoi(value);
194da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int teeEnabled = 0;
195da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    if (debuggable) {
196da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        (void) property_get("af.tee", value, "0");
197da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teeEnabled = atoi(value);
198da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    }
199f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten    // FIXME symbolic constants here
2006e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 1) {
201da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkInputEnabled = true;
2026e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2036e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 2) {
204da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkOutputEnabled = true;
2056e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2066e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 4) {
207da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkTrackEnabled = true;
2086e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
20946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2105a61d2f277af3098fc10b2881babca16391362daDima Zavin}
2115a61d2f277af3098fc10b2881babca16391362daDima Zavin
2125a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef()
2135a61d2f277af3098fc10b2881babca16391362daDima Zavin{
214799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc = 0;
215fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
216935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    Mutex::Autolock _l(mLock);
217935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
218799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    /* TODO: move all this work into an Init() function */
2194ff14bae91075eb274eb1c2975982358946e7e63John Grossman    char val_str[PROPERTY_VALUE_MAX] = { 0 };
2204ff14bae91075eb274eb1c2975982358946e7e63John Grossman    if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
2214ff14bae91075eb274eb1c2975982358946e7e63John Grossman        uint32_t int_val;
2224ff14bae91075eb274eb1c2975982358946e7e63John Grossman        if (1 == sscanf(val_str, "%u", &int_val)) {
2234ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = milliseconds(int_val);
2244ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using %u mSec as standby time.", int_val);
2254ff14bae91075eb274eb1c2975982358946e7e63John Grossman        } else {
2264ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
2274ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using default %u mSec as standby time.",
2284ff14bae91075eb274eb1c2975982358946e7e63John Grossman                    (uint32_t)(mStandbyTimeInNsecs / 1000000));
2294ff14bae91075eb274eb1c2975982358946e7e63John Grossman        }
2304ff14bae91075eb274eb1c2975982358946e7e63John Grossman    }
23165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2321c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    mPatchPanel = new PatchPanel(this);
2331c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
234a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    mMode = AUDIO_MODE_NORMAL;
23565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
23665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
23765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger()
23865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
23965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mRecordThreads.isEmpty()) {
240c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeInput_nonvirtual() will remove specified entry from mRecordThreads
241d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeInput_nonvirtual(mRecordThreads.keyAt(0));
24265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
24365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mPlaybackThreads.isEmpty()) {
244c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
245d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
24665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
247799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
2482b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2492b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten        // no mHardwareLock needed, as there are no other references to this
250a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
251a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        delete mAudioHwDevs.valueAt(i);
25265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
253481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
254481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Tell media.log service about any old writers that still need to be unregistered
255481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
256481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder != 0) {
257481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
258481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
259481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
260481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mUnregisteredWriters.pop();
261481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mediaLogService->unregisterWriter(iMemory);
262481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
263481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
264481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
26565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
26665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
267a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = {
268a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_PRIMARY,
269a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_A2DP,
270a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_USB,
271a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent};
272a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
273a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
274ee578c0330319f04a48bccbdb26b53fea0388d04John GrossmanAudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
275ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_module_handle_t module,
276ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_devices_t devices)
277799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
278a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // if module is 0, the request comes from an old policy manager and we should load
279a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // well known modules
280a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    if (module == 0) {
281a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
282a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
283a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            loadHwModule_l(audio_interfaces[i]);
284a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
285f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        // then try to find a module supporting the requested device.
286f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
287f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
288f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            audio_hw_device_t *dev = audioHwDevice->hwDevice();
289f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            if ((dev->get_supported_devices != NULL) &&
290f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    (dev->get_supported_devices(dev) & devices) == devices)
291f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                return audioHwDevice;
292f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        }
293a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    } else {
294a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        // check a match for the requested module handle
295ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
296ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (audioHwDevice != NULL) {
297ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            return audioHwDevice;
298a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
299a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    }
300a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
301799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return NULL;
302799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
30365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3040f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
30565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
30665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
30765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
30865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
30965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
31065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("Clients:\n");
31165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < mClients.size(); ++i) {
31277c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        sp<Client> client = mClients.valueAt(i).promote();
31377c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        if (client != 0) {
31477c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            snprintf(buffer, SIZE, "  pid: %d\n", client->pid());
31577c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            result.append(buffer);
31665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
3183a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
319d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    result.append("Notification Clients:\n");
320d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); ++i) {
321d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        snprintf(buffer, SIZE, "  pid: %d\n", mNotificationClients.keyAt(i));
322d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        result.append(buffer);
323d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
324d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
3253a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    result.append("Global session refs:\n");
326b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    result.append("  session   pid count\n");
3273a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
3283a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *r = mAudioSessionRefs[i];
329b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
3303a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        result.append(buffer);
3313a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
33265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
33365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
33465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
33565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3360f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
341a4454b4765c5905f14186893b0688be375642283Glenn Kasten    hardware_call_state hardwareStatus = mHardwareStatus;
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3434ff14bae91075eb274eb1c2975982358946e7e63John Grossman    snprintf(buffer, SIZE, "Hardware status: %d\n"
3444ff14bae91075eb274eb1c2975982358946e7e63John Grossman                           "Standby Time mSec: %u\n",
3454ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            hardwareStatus,
3464ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            (uint32_t)(mStandbyTimeInNsecs / 1000000));
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3510f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioFlinger from pid=%d, uid=%d\n",
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex)
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3727dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten        usleep(kDumpLockSleepUs);
37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
37565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args)
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
37944deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd, args);
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // get state of hardware lock
38381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool hardwareLocked = dumpTryLock(mHardwareLock);
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!hardwareLocked) {
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kHardwareLockedString);
38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mHardwareLock.unlock();
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool locked = dumpTryLock(mLock);
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // failed to lock - AudioFlinger is probably deadlocked
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
39765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
39865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
399021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        bool clientLocked = dumpTryLock(mClientLock);
400021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (!clientLocked) {
401021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            String8 result(kClientLockedString);
402021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            write(fd, result.string(), result.size());
403021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpClients(fd, args);
405021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (clientLocked) {
406021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mClientLock.unlock();
407021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
408021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent
40965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd, args);
41065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump playback threads
41265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
41365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mPlaybackThreads.valueAt(i)->dump(fd, args);
41465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
41565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump record threads
41765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
41865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mRecordThreads.valueAt(i)->dump(fd, args);
41965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
42065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
421aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        // dump orphan effect chains
422aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        if (mOrphanEffectChains.size() != 0) {
423aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            write(fd, "  Orphan Effect Chains\n", strlen("  Orphan Effect Chains\n"));
424aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
425aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                mOrphanEffectChains.valueAt(i)->dump(fd, args);
426aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
427aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
428799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        // dump all hardware devs
429799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
430a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
431799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            dev->dump(dev, fd);
43265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
433d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
43446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
435d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        // dump the serially shared record tee sink
436d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        if (mRecordTeeSource != 0) {
437d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten            dumpTee(fd, mRecordTeeSource);
438d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        }
43946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
440d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
441d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        if (locked) {
442d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            mLock.unlock();
443d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
4449e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4459e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // append a copy of media.log here by forwarding fd to it, but don't attempt
4469e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // to lookup the service if it's not running, as it will block for a second
4479e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        if (mLogMemoryDealer != 0) {
4489e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
4499e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (binder != 0) {
4508b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "\nmedia.log:\n");
4519e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                Vector<String16> args;
4529e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                binder->dump(fd, args);
4539e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
4549e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        }
45565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
45665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
45765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
45865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
459021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentsp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
46098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{
461021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _cl(mClientLock);
46298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // If pid is already in the mClients wp<> map, then use that entry
46398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // (for which promote() is always != 0), otherwise create a new entry and Client.
46498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    sp<Client> client = mClients.valueFor(pid).promote();
46598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    if (client == 0) {
46698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = new Client(this, pid);
46798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        mClients.add(pid, client);
46898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    }
46998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten
47098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    return client;
47198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten}
47265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4739e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
4749e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
475481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If there is no memory allocated for logs, return a dummy writer that does nothing
4769e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (mLogMemoryDealer == 0) {
4779e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return new NBLog::Writer();
4789e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
4799e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
480481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Similarly if we can't contact the media.log service, also return a dummy writer
481481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder == 0) {
482481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
4839e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
484481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
485481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
486481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If allocation fails, consult the vector of previously unregistered writers
487481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // and garbage-collect one or more them until an allocation succeeds
488481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (shared == 0) {
489481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        Mutex::Autolock _l(mUnregisteredWritersLock);
490481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
491481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            {
492481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Pick the oldest stale writer to garbage-collect
493481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
494481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mUnregisteredWriters.removeAt(0);
495481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mediaLogService->unregisterWriter(iMemory);
496481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Now the media.log remote reference to IMemory is gone.  When our last local
497481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // reference to IMemory also drops to zero at end of this block,
498481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // the IMemory destructor will deallocate the region from mLogMemoryDealer.
499481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
500481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            // Re-attempt the allocation
501481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
502481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            if (shared != 0) {
503481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                goto success;
504481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
505481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
506481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // Even after garbage-collecting all old writers, there is still not enough memory,
507481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // so return a dummy writer
508481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
509481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
510481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kastensuccess:
511481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mediaLogService->registerWriter(shared, size, name);
512481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    return new NBLog::Writer(size, shared);
5139e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5149e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
5159e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
5169e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
517685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    if (writer == 0) {
518685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten        return;
519685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    }
5209e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IMemory> iMemory(writer->getIMemory());
5219e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (iMemory == 0) {
5229e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return;
5239e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
524481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Rather than removing the writer immediately, append it to a queue of old writers to
525481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // be garbage-collected later.  This allows us to continue to view old logs for a while.
526481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Mutex::Autolock _l(mUnregisteredWritersLock);
527481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mUnregisteredWriters.push(writer);
5289e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5299e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
53065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface
53165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
53265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
53365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack(
534fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
53565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t sampleRate,
53658f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
537254af180475346b6186b49c297f340c9c4817511Glenn Kasten        audio_channel_mask_t channelMask,
53874935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
539e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten        IAudioFlinger::track_flags_t *flags,
54065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const sp<IMemory>& sharedBuffer,
54172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output,
5423acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        pid_t tid,
54365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int *sessionId,
544462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int clientUid,
54565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status_t *status)
54665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
54765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<PlaybackThread::Track> track;
54865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<TrackHandle> trackHandle;
54965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<Client> client;
55065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t lStatus;
55165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int lSessionId;
55265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
553263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
554263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // but if someone uses binder directly they could bypass that and cause us to crash
555263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
55629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("createTrack() invalid stream type %d", streamType);
55765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        lStatus = BAD_VALUE;
55865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        goto Exit;
55965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
56065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
56153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createTrack_l() depending on the thread type
56253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
56353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid sample rate %u", sampleRate);
56453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
56553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
56653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
56753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
56853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createTrack_l() depending on the thread type
56953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_output_channel(channelMask)) {
57053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid channel mask %#x", channelMask);
57153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
57253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
57353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
57453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
575c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    // further format checks are performed by createTrack_l() depending on the thread type
576c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    if (!audio_is_valid_format(format)) {
577cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("createTrack() invalid format %#x", format);
57860a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        lStatus = BAD_VALUE;
579663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        goto Exit;
580663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    }
581663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten
582663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
583663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
584663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        lStatus = BAD_VALUE;
58560a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        goto Exit;
58660a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    }
58760a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten
58865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
58965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
59065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
59165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
592c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten            ALOGE("no playback thread found for output handle %d", output);
59365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lStatus = BAD_VALUE;
59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            goto Exit;
59565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
59665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5978d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
598021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
59965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
600e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        PlaybackThread *effectThread = NULL;
601aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
602570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            lSessionId = *sessionId;
603f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // check if an effect chain with the same session ID is present on another
604f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // output thread and move it here.
605de070137f11d346fba77605bd76a44c040a618fcEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
60639e94f8f723d445447fdee0822291e664b631f60Eric Laurent                sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
60739e94f8f723d445447fdee0822291e664b631f60Eric Laurent                if (mPlaybackThreads.keyAt(i) != output) {
608570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten                    uint32_t sessions = t->hasAudioSession(lSessionId);
60939e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    if (sessions & PlaybackThread::EFFECT_SESSION) {
61039e94f8f723d445447fdee0822291e664b631f60Eric Laurent                        effectThread = t.get();
611f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent                        break;
61239e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    }
613de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
614de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
61565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
616de070137f11d346fba77605bd76a44c040a618fcEric Laurent            // if no audio session id is provided, create one here
6177c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            lSessionId = nextUniqueId();
61865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (sessionId != NULL) {
61965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                *sessionId = lSessionId;
62065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
6223856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() lSessionId: %d", lSessionId);
62365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track = thread->createTrack_l(client, streamType, sampleRate, format,
625462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
62603e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
6272fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten        // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
62839e94f8f723d445447fdee0822291e664b631f60Eric Laurent
62939e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // move effect chain to this output thread if an effect on same session was waiting
63039e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // for a track to be created
63139e94f8f723d445447fdee0822291e664b631f60Eric Laurent        if (lStatus == NO_ERROR && effectThread != NULL) {
6322fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten            // no risk of deadlock because AudioFlinger::mLock is held
63339e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _dl(thread->mLock);
63439e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _sl(effectThread->mLock);
63539e94f8f723d445447fdee0822291e664b631f60Eric Laurent            moveEffectChain_l(lSessionId, effectThread, thread, true);
63639e94f8f723d445447fdee0822291e664b631f60Eric Laurent        }
637a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
638a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        // Look for sync events awaiting for a session to be used.
6393ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn        for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
640a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
641a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
6422986460984580833161bdaabc7f17da1005a8961Eric Laurent                    if (lStatus == NO_ERROR) {
643d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten                        (void) track->setSyncEvent(mPendingSyncEvents[i]);
6442986460984580833161bdaabc7f17da1005a8961Eric Laurent                    } else {
6452986460984580833161bdaabc7f17da1005a8961Eric Laurent                        mPendingSyncEvents[i]->cancel();
6462986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
647a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    mPendingSyncEvents.removeAt(i);
648a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    i--;
649a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                }
650a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            }
651a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
652e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
65365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
654e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
6553ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    if (lStatus != NO_ERROR) {
6563ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        // remove local strong reference to Client before deleting the Track so that the
657021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
658fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // Don't hold mClientLock when releasing the reference on the track as the
659fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // destructor will acquire it.
660fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        {
661fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
662fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
663fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track.clear();
6653ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        goto Exit;
66665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
66765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6683ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    // return handle to client
6693ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    trackHandle = new TrackHandle(track);
6703ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten
67165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit:
6729156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return trackHandle;
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
67565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
67672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
67765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
67965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
68065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6815ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("sampleRate() unknown thread %d", output);
68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
68365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->sampleRate();
68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
68772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(audio_io_handle_t output) const
68865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
69065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6925ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("format() unknown thread %d", output);
69358f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        return AUDIO_FORMAT_INVALID;
69465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->format();
69665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
69872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastensize_t AudioFlinger::frameCount(audio_io_handle_t output) const
69965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
70065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
70165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
7035ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("frameCount() unknown thread %d", output);
70465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
70565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
70658912562617941964939a4182cda71eaeb153d4bGlenn Kasten    // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
70758912562617941964939a4182cda71eaeb153d4bGlenn Kasten    //       should examine all callers and fix them to handle smaller counts
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->frameCount();
70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
71065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
71172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const
71265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
71365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
71465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
71565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
716c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten        ALOGW("latency(): no playback thread found for output handle %d", output);
71765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
71865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->latency();
72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
72165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value)
72365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
724a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
725a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
726a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
727a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
728a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
72965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
73165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
73365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
734a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
735ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterVolume = value;
736a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
737ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master volume in the HALs which support it.
738ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
739ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
740ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
7414ff14bae91075eb274eb1c2975982358946e7e63John Grossman
742ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
743ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterVolume()) {
744ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
745935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent        }
746ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
74765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
74865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
749ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master volume in each playback thread.  Playback threads
750ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master volume support will apply
751ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // master volume during the mix operation.  Threads with HALs which do
752ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // support master volume will simply ignore the setting.
7538d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
754ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterVolume(value);
75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
75765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
75865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
759f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode)
76065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
761a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
762a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
763a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
764a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
76665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
76765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
76865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
76965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
770930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(mode) >= AUDIO_MODE_CNT) {
7715ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("Illegal value: setMode(%d)", mode);
77265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
77365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
77465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    { // scope for the lock
77665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex lock(mHardwareLock);
777ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_SET_MODE;
779ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        ret = dev->set_mode(dev, mode);
78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_IDLE;
78165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (NO_ERROR == ret) {
78465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
78565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mMode = mode;
7868d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++)
787e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setMode(mode);
78865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
78965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
79265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state)
79465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
795a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
796a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
797a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
798a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
799a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
80165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
80265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
80365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
80665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
8072f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
8082f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
8092f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        status_t result = dev->set_mic_mute(dev, state);
8102f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        if (result != NO_ERROR) {
8112f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent            ret = result;
8122f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        }
8132f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent    }
81465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
81565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
81665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
81765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
81865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const
81965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
820a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
821a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
822a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return false;
823a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
824a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
825fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    bool state = AUDIO_MODE_INVALID;
8262b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
827ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
82865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
829ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    dev->get_mic_mute(dev, &state);
83065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
83165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return state;
83265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
83365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
83465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted)
83565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
836d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    status_t ret = initCheck();
837d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    if (ret != NO_ERROR) {
838d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        return ret;
839d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
840d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
84165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
84365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
84465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
84565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
846ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    Mutex::Autolock _l(mLock);
847ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterMute = muted;
848d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
849ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master mute in the HALs which support it.
850ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
851ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
852ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
853d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
854ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
855ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterMute()) {
856ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
857d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        }
858ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
859d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
860d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
861ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master mute in each playback thread.  Playback threads
862ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master mute support will apply master
863ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute during the mix operation.  Threads with HALs which do support master
864ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute will simply ignore the setting.
8658d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
866ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterMute(muted);
86765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8739806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8749806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterVolume_l();
87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
87765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const
87865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8799806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8809806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterMute_l();
88165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8834ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const
8844ff14bae91075eb274eb1c2975982358946e7e63John Grossman{
8854ff14bae91075eb274eb1c2975982358946e7e63John Grossman    return mMasterVolume;
8864ff14bae91075eb274eb1c2975982358946e7e63John Grossman}
8874ff14bae91075eb274eb1c2975982358946e7e63John Grossman
888d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const
889d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{
890ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    return mMasterMute;
891d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman}
892d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
89372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
89472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
89565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
89665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
89765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
89865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
89965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
90065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
901263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
90229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamVolume() invalid stream %d", stream);
90365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
90465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
90765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = NULL;
908142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
90965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(output);
91065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
91165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return BAD_VALUE;
91265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
91365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
91465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
91565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].volume = value;
91665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
91765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
9188d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
919e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
92065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
92165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
92265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread->setStreamVolume(stream, value);
92365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
92465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
92565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
92665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
92765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
928fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
92965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
93065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
93165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
93265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
93365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
93465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
935263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
936fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
93729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamMute() invalid stream %d", stream);
93865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
93965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
94065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
941935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    AutoMutex lock(mLock);
94265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].mute = muted;
9433ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
944e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
94565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
94765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
94865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
94972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
951263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
95265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0.0f;
95365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
95465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
95665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    float volume;
957142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
95865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
95965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
96065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return 0.0f;
96165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
96265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        volume = thread->streamVolume(stream);
96365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
9646637baae4244aec731c4014da72418d330636ae1Glenn Kasten        volume = streamVolume_l(stream);
96565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
96665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
96765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return volume;
96865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
96965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
970fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const
97165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
972263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
97365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return true;
97465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
97565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9766637baae4244aec731c4014da72418d330636ae1Glenn Kasten    AutoMutex lock(mLock);
9776637baae4244aec731c4014da72418d330636ae1Glenn Kasten    return streamMute_l(stream);
97865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
97965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
98072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
98165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
982827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
983827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
98481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
98565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
98665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
98765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
98865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
98965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
990142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
991142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
992a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        Mutex::Autolock _l(mLock);
993799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        status_t final_result = NO_ERROR;
9948abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        {
995a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            AutoMutex lock(mHardwareLock);
996a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_SET_PARAMETER;
997a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
998a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
999a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                status_t result = dev->set_parameters(dev, keyValuePairs.string());
1000a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                final_result = result ?: final_result;
1001a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            }
1002a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
10038abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        }
100459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
100559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        AudioParameter param = AudioParameter(keyValuePairs);
100659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        String8 value;
100759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
1008bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1009bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            if (mBtNrecIsOff != btNrecIsOff) {
101059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
101159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    sp<RecordThread> thread = mRecordThreads.valueAt(i);
1012f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    audio_devices_t device = thread->inDevice();
1013510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1014510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // collect all of the thread's session IDs
1015510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    KeyedVector<int, bool> ids = thread->sessionIds();
1016510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // suspend effects associated with those session IDs
1017510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    for (size_t j = 0; j < ids.size(); ++j) {
1018510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                        int sessionId = ids.keyAt(j);
101959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_AEC,
102059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1021510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
102259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_NS,
102359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1024510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
102559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    }
102659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                }
1027bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent                mBtNrecIsOff = btNrecIsOff;
102859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent            }
102959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        }
103028ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        String8 screenState;
103128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
103228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            bool isOff = screenState == "off";
103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isOff != (AudioFlinger::mScreenState & 1)) {
103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
103528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            }
103628ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        }
1037799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        return final_result;
103865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
103965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
104065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // hold a strong ref on thread in case closeOutput() or closeInput() is called
104165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // and the thread is exited once the lock is released
104265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ThreadBase> thread;
104365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
104465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
104565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(ioHandle);
1046d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten        if (thread == 0) {
104765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            thread = checkRecordThread_l(ioHandle);
10487fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten        } else if (thread == primaryPlaybackThread_l()) {
10497c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // indicate output device change to all input threads for pre processing
10507c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            AudioParameter param = AudioParameter(keyValuePairs);
10517c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int value;
105289d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent            if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
105389d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent                    (value != 0)) {
10547c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
10557c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                    mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
10567c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                }
10577c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
105865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
105965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
10607378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (thread != 0) {
10617378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten        return thread->setParameters(keyValuePairs);
106265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
106365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
106465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
106565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
106672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
106765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1068827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1069827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
107065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1071a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
1072a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
1073142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
1074fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        String8 out_s8;
1075fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1076799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
10778abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            char *s;
10788abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            {
10798abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            AutoMutex lock(mHardwareLock);
10808abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_GET_PARAMETER;
1081a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
10828abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            s = dev->get_parameters(dev, keys.string());
10838abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_IDLE;
10848abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            }
1085ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman            out_s8 += String8(s ? s : "");
1086799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            free(s);
1087799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        }
1088fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return out_s8;
108965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
109065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
109165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
109265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
109365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getParameters(keys);
109465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
109565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
109665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
109765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getParameters(keys);
109865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
109965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return String8("");
110065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
110165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1102dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1103dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask) const
110465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1105a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1106a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1107a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return 0;
1108a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1109a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
11102b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
11112b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
1112cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_config_t config;
1113ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    memset(&config, 0, sizeof(config));
1114ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = sampleRate;
1115ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = channelMask;
1116ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = format;
1117ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1118ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1119ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    size_t size = dev->get_input_buffer_size(dev, &config);
11202b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_IDLE;
11212b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    return size;
112265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
112365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
11245f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
112565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
112665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
112765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
112965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
113065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getInputFramesLost();
113165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
113265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return 0;
113365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
113465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
113565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value)
113665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1137a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1138a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1139a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
1140a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1141a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
114265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
114365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
114465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
114565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
114665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
114765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
1148ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
11498abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten    mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
1150ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_voice_volume(dev, value);
115165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
115265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
115365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
115465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
115565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1156377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
115772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output) const
115865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
115965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status;
116065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
116165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
116265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
116365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(output);
116465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
116565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getRenderPosition(halFrames, dspFrames);
116665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
116765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
116865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
116965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
117065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
117165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
117265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
117365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
117444622db93a24219123facbbe14259000f90de728Eric Laurent    if (client == 0) {
117544622db93a24219123facbbe14259000f90de728Eric Laurent        return;
117644622db93a24219123facbbe14259000f90de728Eric Laurent    }
1177021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    bool clientAdded = false;
1178021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1179021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
118065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1181021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        pid_t pid = IPCThreadState::self()->getCallingPid();
1182021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(pid) < 0) {
1183021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            sp<NotificationClient> notificationClient = new NotificationClient(this,
1184021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                client,
1185021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                pid);
1186021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
118765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1188021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mNotificationClients.add(pid, notificationClient);
118965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1190021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            sp<IBinder> binder = client->asBinder();
1191021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            binder->linkToDeath(notificationClient);
1192021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            clientAdded = true;
1193021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
1194021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
119565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1196021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
1197fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent    // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
1198021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    if (clientAdded) {
119965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // the config change is always sent from playback or record threads to avoid deadlock
120065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // with AudioSystem::gLock
120165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1202896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
120365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
120465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
120565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
1206896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
120765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
120865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
120965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
121065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
121165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid)
121265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
121365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
1214021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1215021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
1216021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        mNotificationClients.removeItem(pid);
1217021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
12183a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
12193856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("%d died, releasing its sessions", pid);
12208d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    size_t num = mAudioSessionRefs.size();
12213a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    bool removed = false;
12228d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i< num; ) {
12233a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1224012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        ALOGV(" pid %d @ %d", ref->mPid, i);
1225012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        if (ref->mPid == pid) {
1226012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten            ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
12273a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            mAudioSessionRefs.removeAt(i);
12283a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            delete ref;
12293a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            removed = true;
12303a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            num--;
12318d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        } else {
12328d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten            i++;
12333a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        }
12343a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
12353a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (removed) {
12363a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        purgeStaleEffects_l();
12373a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
123865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
123965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1240021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
124165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1242021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _l(mClientLock);
1243021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    size_t size = mNotificationClients.size();
124465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < size; i++) {
1245021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
12461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                                              ioHandle,
12471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                                              param2);
124865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
124965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
125065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1251021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// removeClient_l() must be called with AudioFlinger::mClientLock held
125265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid)
125365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1254827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("removeClient_l() pid %d, calling pid %d", pid,
125585ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            IPCThreadState::self()->getCallingPid());
125665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mClients.removeItem(pid);
125765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
125865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1259717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held
1260717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1261717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{
1262717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    sp<PlaybackThread> thread;
1263717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1264717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1265717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1266717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            ALOG_ASSERT(thread == 0);
1267717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            thread = mPlaybackThreads.valueAt(i);
1268717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        }
1269717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    }
1270717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1271717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    return thread;
1272717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent}
127365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
127665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
127765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
127881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   RefBase(),
128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger(audioFlinger),
128181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPid(pid),
128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTimedTrackCount(0)
128565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
128765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
128865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1289021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// Client destructor must be called with AudioFlinger::mClientLock held
129081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client()
129165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
129281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeClient_l(mPid);
129365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
129465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const
129665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mMemoryDealer;
129865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
129965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Reserve one of the limited slots for a timed audio track associated
130181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// with this client
130281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::Client::reserveTimedTrack()
130365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int kMaxTimedTracksPerClient = 4;
130565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
130681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
130765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("can not create timed track - pid %d has exceeded the limit",
131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             mPid);
131181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return false;
131265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
131365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
131481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount++;
131581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return true;
131665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
131765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
131881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Release a slot for a timed audio track
131981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::Client::releaseTimedTrack()
132065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
132181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
132281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount--;
1323896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent}
1324896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent
132581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
132681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
132781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     const sp<IAudioFlingerClient>& client,
132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     pid_t pid)
133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
1331896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{
133265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
133365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
133481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient()
133565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
133665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
133765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
13380f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
133965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NotificationClient> keep(this);
134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeNotificationClient(mPid);
134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
134365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
134465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
134665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1347893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brownstatic bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1348893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown    return audio_is_remote_submix_device(inDevice);
1349893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown}
1350893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord(
135281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t input,
135381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
135674935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
1357eeca32671896739e84050da5992d5f151a1629deGlenn Kasten        IAudioFlinger::track_flags_t *flags,
135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
135981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *sessionId,
13607df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten        size_t *notificationFrames,
1361d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& cblk,
1362d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& buffers,
136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
13641d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{
136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread::RecordTrack> recordTrack;
136681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordHandle> recordHandle;
136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Client> client;
136881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int lSessionId;
13701d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1371d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk.clear();
1372d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers.clear();
1373d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
137481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check calling permissions
137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!recordingAllowed()) {
1376e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten        ALOGE("openRecord() permission denied: recording not allowed");
137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
13801d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
138153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createRecordTrack_l()
138253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
138353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid sample rate %u", sampleRate);
138453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
138553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
138653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
138753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
13880e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten    // we don't yet support anything other than 16-bit PCM
13890e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten    if (!(audio_is_valid_format(format) &&
13900e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten            audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
1391cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("openRecord() invalid format %#x", format);
1392291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        lStatus = BAD_VALUE;
139353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
139453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
139553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
139653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createRecordTrack_l()
139753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_input_channel(channelMask)) {
139853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid channel mask %#x", channelMask);
139953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
1400291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        goto Exit;
1401291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
1402291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten
140305997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten    {
140481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
1405e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        RecordThread *thread = checkRecordThread_l(input);
140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
1407e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() checkRecordThread_l failed");
140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
140981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
14101d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent        }
14111d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1412893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown        if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1413893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown                && !captureAudioOutputAllowed()) {
1414e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() permission denied: capture not allowed");
1415893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown            lStatus = PERMISSION_DENIED;
1416893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown            goto Exit;
1417893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown        }
1418893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
14198d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
1420021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
1421feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1422aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = *sessionId;
1424feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        } else {
1425570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            // if no audio session id is provided, create one here
142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = nextUniqueId();
142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId != NULL) {
142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                *sessionId = lSessionId;
142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1430feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        }
1431aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
1432570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten
1433462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        // TODO: the uid should be passed in as a parameter to openRecord
143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
14357df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten                                                  frameCount, lSessionId, notificationFrames,
1436462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  IPCThreadState::self()->getCallingUid(),
1437462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  flags, tid, &lStatus);
143803e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
14391b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
14401b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        if (lStatus == NO_ERROR) {
14411b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // Check if one effect chain was awaiting for an AudioRecord to be created on this
14421b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // session and move it to this thread.
14431b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
14441b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (chain != 0) {
14451b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                Mutex::Autolock _l(thread->mLock);
14461b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                thread->addEffectChain_l(chain);
14471b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
14481b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        }
1449feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1450e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove local strong reference to Client before deleting the RecordTrack so that the
1453021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
1454fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // Don't hold mClientLock when releasing the reference on the track as the
1455fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // destructor will acquire it.
1456fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        {
1457fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
1458fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
1459fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack.clear();
146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
1462feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1463feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1464d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk = recordTrack->getCblk();
1465d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers = recordTrack->getBuffers();
1466d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
14672fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten    // return handle to client
146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordHandle = new RecordHandle(recordTrack);
1469feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
147081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
14719156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return recordHandle;
1473feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent}
1474feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1475feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
14761d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
147981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name)
148059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
148144622db93a24219123facbbe14259000f90de728Eric Laurent    if (name == NULL) {
148244622db93a24219123facbbe14259000f90de728Eric Laurent        return 0;
148344622db93a24219123facbbe14259000f90de728Eric Laurent    }
148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!settingsAllowed()) {
148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
148759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
148881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return loadHwModule_l(name);
148959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
149059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held
149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
149359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
149581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("loadHwModule() module %s already loaded", name);
149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mAudioHwDevs.keyAt(i);
149859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
149959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
150059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
150181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *dev;
150259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int rc = load_audio_interface(name, &dev);
150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() error %d loading module %s ", rc, name);
150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
150759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
150859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_INIT;
151081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    rc = dev->init_check(dev);
151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
151481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
151559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
151659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
151781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Check and cache this HAL's level of support for master mute and master
151881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volume.  If this is the first HAL opened, and it supports the get
151981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // methods, use the initial values provided by the HAL as the current
152081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // master mute and volume settings.
152159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
152281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
152381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {  // scope for auto-lock pattern
152481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mHardwareLock);
152559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
152681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (0 == mAudioHwDevs.size()) {
152781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
152881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_volume) {
152981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float mv;
153081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_volume(dev, &mv)) {
153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterVolume = mv;
153281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
153481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_mute) {
153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool mm;
153881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_mute(dev, &mm)) {
153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterMute = mm;
154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
154181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
154259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
154381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
154481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
154581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_volume) &&
154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_volume(dev, mMasterVolume))) {
154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
154959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
155059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_mute) &&
155381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_mute(dev, mMasterMute))) {
155481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
155581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
155681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
155759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
155881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_IDLE;
155959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
156059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_module_handle_t handle = nextUniqueId();
156283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent          name, dev->common.module->name, dev->common.module->id, handle);
156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
156881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
156959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
157059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
157181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate()
157459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
157559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
157681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->sampleRate() : 0;
1578a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent}
157959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
158081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount()
1581a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{
158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
158381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->frameCountHAL() : 0;
158559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
158659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
158765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
158865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15894182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
15904182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{
15914182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    uid_t uid = IPCThreadState::self()->getCallingUid();
15924182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (uid != AID_SYSTEM) {
15934182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return PERMISSION_DENIED;
15944182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
15954182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    Mutex::Autolock _l(mLock);
15964182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (mIsDeviceTypeKnown) {
15974182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return INVALID_OPERATION;
15984182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
15994182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsLowRamDevice = isLowRamDevice;
16004182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsDeviceTypeKnown = true;
16014182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    return NO_ERROR;
16024182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten}
16034182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
160493c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurentaudio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
160593c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent{
160693c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    Mutex::Autolock _l(mLock);
160793c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
160893c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
160993c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        if ((thread->hasAudioSession(sessionId) & ThreadBase::TRACK_SESSION) != 0) {
161093c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            // A session can only be on one thread, so exit after first match
161193c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            String8 reply = thread->getParameters(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC));
161293c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            AudioParameter param = AudioParameter(reply);
161393c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            int value;
161493c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            if (param.getInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value) == NO_ERROR) {
161593c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent                return value;
161693c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            }
161793c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            break;
161893c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        }
161993c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    }
162093c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    return AUDIO_HW_SYNC_INVALID;
162193c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent}
162293c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent
16234182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ----------------------------------------------------------------------------
16244182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
162565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
162683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
1627cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_io_handle_t *output,
1628cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_config_t *config,
1629cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_devices_t devices,
1630cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            const String8& address,
163183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                            audio_output_flags_t flags)
163283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
1633cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
16346e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (outHwDev == NULL) {
1635cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return 0;
16366e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
163765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
1639cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*output == AUDIO_IO_HANDLE_NONE) {
1640cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *output = nextUniqueId();
1641cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    }
164265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
164381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
164465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16453255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_out_t *outStream = NULL;
16466146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung
16476146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    // FOR TESTING ONLY:
16489a59276fb465e492138e0576523b54079671e8f4Andy Hung    // This if statement allows overriding the audio policy settings
16499a59276fb465e492138e0576523b54079671e8f4Andy Hung    // and forcing a specific format or channel mask to the HAL/Sink device for testing.
16509a59276fb465e492138e0576523b54079671e8f4Andy Hung    if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
16519a59276fb465e492138e0576523b54079671e8f4Andy Hung        // Check only for Normal Mixing mode
16529a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (kEnableExtendedPrecision) {
16539a59276fb465e492138e0576523b54079671e8f4Andy Hung            // Specify format (uncomment one below to choose)
16549a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_FLOAT;
16559a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
16569a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_32_BIT;
16579a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
1658cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
16599a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
16609a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (kEnableExtendedChannels) {
16619a59276fb465e492138e0576523b54079671e8f4Andy Hung            // Specify channel mask (uncomment one below to choose)
16629a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->channel_mask = audio_channel_out_mask_from_count(4);  // for USB 4ch
16639a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->channel_mask = audio_channel_mask_from_representation_and_bits(
16649a59276fb465e492138e0576523b54079671e8f4Andy Hung            //        AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1);  // another 4ch example
16659a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
16666146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    }
16676146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung
166834542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten    status_t status = hwDevHal->open_output_stream(hwDevHal,
1669cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                   *output,
1670cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                   devices,
1671cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                   flags,
1672cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                   config,
1673cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                   &outStream,
1674cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                   address.string());
167565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
167681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
1677cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    ALOGV("openOutput_l() openOutputStream returned output %p, sampleRate %d, Format %#x, "
16789a59276fb465e492138e0576523b54079671e8f4Andy Hung            "channelMask %#x, status %d",
167981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            outStream,
168083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            config->sample_rate,
168183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            config->format,
168283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            config->channel_mask,
168381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status);
168458912562617941964939a4182cda71eaeb153d4bGlenn Kasten
168581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && outStream != NULL) {
1686cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        AudioStreamOut *outputStream = new AudioStreamOut(outHwDev, outStream, flags);
168765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16883255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        PlaybackThread *thread;
1689bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1690cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            thread = new OffloadThread(this, outputStream, *output, devices);
1691cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
16926146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
169383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                || !isValidPcmSinkFormat(config->format)
16949a59276fb465e492138e0576523b54079671e8f4Andy Hung                || !isValidPcmSinkChannelMask(config->channel_mask)) {
1695cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            thread = new DirectOutputThread(this, outputStream, *output, devices);
1696cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
1698cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            thread = new MixerThread(this, outputStream, *output, devices);
1699cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
170065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
1701cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        mPlaybackThreads.add(*output, thread);
170283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        return thread;
170383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
170483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
170583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    return 0;
170683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
170783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1708cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurentstatus_t AudioFlinger::openOutput(audio_module_handle_t module,
1709cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_io_handle_t *output,
1710cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_config_t *config,
1711cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_devices_t *devices,
1712cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  const String8& address,
1713cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  uint32_t *latencyMs,
1714cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_output_flags_t flags)
171583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
171683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
171783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent              module,
1718cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              (devices != NULL) ? *devices : 0,
1719cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->sample_rate,
1720cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->format,
1721cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->channel_mask,
172283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent              flags);
172383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1724cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*devices == AUDIO_DEVICE_NONE) {
1725cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return BAD_VALUE;
172683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
172783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
172883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
172983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1730cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
173183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (thread != 0) {
1732cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *latencyMs = thread->latency();
173365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
173481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new output creation
1735021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
173665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
173781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the first primary output opened designates the primary hw device
173881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
173981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGI("Using module %d has the primary audio interface", module);
174083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
174181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
174281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AutoMutex lock(mHardwareLock);
174381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_SET_MODE;
174483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
174581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
17464ea00a25cf85877b48ebd1e15a657cfaab29af58Glenn Kasten
1747cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            mPrimaryOutputSampleRate = config->sample_rate;
174881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1749cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return NO_ERROR;
175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
175165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1752cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    return NO_INIT;
175365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
175465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
175581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
175681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t output2)
175765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
175981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread1 = checkMixerThread_l(output1);
176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread2 = checkMixerThread_l(output2);
176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
176281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread1 == NULL || thread2 == NULL) {
176381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
176481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                output2);
1765142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        return AUDIO_IO_HANDLE_NONE;
176665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
176765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
176881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
176981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->addOutputTrack(thread2);
177181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPlaybackThreads.add(id, thread);
177281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // notify client processes of the new output creation
1773021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return id;
177565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
177665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
177781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output)
17782bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{
177981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeOutput_nonvirtual(output);
17802bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi}
17812bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi
178281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
178365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
178481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the playback thread so that
178581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
178681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<PlaybackThread> thread;
178781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
178881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
178981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkPlaybackThread_l(output);
179081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
179181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
179265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
179365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
179481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeOutput() %d", output);
1795de070137f11d346fba77605bd76a44c040a618fcEric Laurent
179681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread->type() == ThreadBase::MIXER) {
179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
179981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    DuplicatingThread *dupThread =
180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    dupThread->removeOutputTrack((MixerThread *)thread.get());
1802bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1803de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
1804de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
1805de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
1806bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1807bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.removeItem(output);
1809bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // save all effects to the default thread
1810bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mPlaybackThreads.size()) {
1811bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1812bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (dstThread != NULL) {
1813bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // audioflinger lock is held here so the acquisition order of thread locks does not
1814bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // matter
1815bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _dl(dstThread->mLock);
1816bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _sl(thread->mLock);
1817bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1818bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
1819bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
1820bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
1821bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1822bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1823021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
18243acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
182581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
182681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
18283acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
182981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread->type() != ThreadBase::DUPLICATING) {
183083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        closeOutputFinish(thread);
183165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
183283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
183381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
183465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
183565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
183683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
183783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
183883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    AudioStreamOut *out = thread->clearOutput();
183983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
184083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // from now on thread->mOutput is NULL
184183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    out->hwDev()->close_output_stream(out->hwDev(), out->stream);
184283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    delete out;
184383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
184483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
184583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
184683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
184783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mPlaybackThreads.removeItem(thread->mId);
184883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    thread->exit();
184983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeOutputFinish(thread);
185083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
185183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output)
1853e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{
185481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
185581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
185681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
185881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1859e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent    }
1860e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("suspendOutput() %d", output);
186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->suspend();
1863e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
186481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
186565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
186665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output)
186865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
18696637baae4244aec731c4014da72418d330636ae1Glenn Kasten    Mutex::Autolock _l(mLock);
187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
187165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1874ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
187565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("restoreOutput() %d", output);
187765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->restore();
187965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
188165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
188265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1883cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurentstatus_t AudioFlinger::openInput(audio_module_handle_t module,
1884cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_io_handle_t *input,
1885cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_config_t *config,
1886cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_devices_t *device,
1887cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          const String8& address,
1888cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_source_t source,
1889ec40d284218466d8f0e832e7eb88e6ea6c479c88Glenn Kasten                                          audio_input_flags_t flags)
189065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
189183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
189283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1893cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*device == AUDIO_DEVICE_NONE) {
1894cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return BAD_VALUE;
189583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
189683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1897cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    sp<RecordThread> thread = openInput_l(module, input, config, *device, address, source, flags);
189883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
189983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (thread != 0) {
190083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        // notify client processes of the new input creation
190183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
1902cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return NO_ERROR;
1903b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent    }
1904cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    return NO_INIT;
190583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
1906b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent
190783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentsp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
1908cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_io_handle_t *input,
1909cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_config_t *config,
191083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                         audio_devices_t device,
1911cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         const String8& address,
1912cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_source_t source,
191383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                         audio_input_flags_t flags)
191483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
191583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    AudioHwDevice *inHwDev = findSuitableHwDev_l(module, device);
19166e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (inHwDev == NULL) {
1917cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *input = AUDIO_IO_HANDLE_NONE;
191881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
19196e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1920fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1921cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*input == AUDIO_IO_HANDLE_NONE) {
1922cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *input = nextUniqueId();
1923cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    }
1924b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
1925cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_config_t halconfig = *config;
1926cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_hw_device_t *inHwHal = inHwDev->hwDevice();
19273255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_in_t *inStream = NULL;
1928cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    status_t status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1929cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                        &inStream, flags, address.string(), source);
1930cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
1931cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent           ", Format %#x, Channels %x, flags %#x, status %d",
193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            inStream,
1933cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.sample_rate,
1934cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.format,
1935cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.channel_mask,
1936ec40d284218466d8f0e832e7eb88e6ea6c479c88Glenn Kasten            flags,
193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status);
193865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
193981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the input could not be opened with the requested parameters and we can handle the
194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs.
194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == BAD_VALUE &&
1943cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            config->format == halconfig.format && halconfig.format == AUDIO_FORMAT_PCM_16_BIT &&
1944cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        (halconfig.sample_rate <= 2 * config->sample_rate) &&
1945cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
1946cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
19478594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME describe the change proposed by HAL (save old values so we can log them here)
1948cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inStream = NULL;
1950cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1951cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                            &inStream, flags, address.string(), source);
19528594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME log this new status; HAL should not propose any further changes
195365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
195465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && inStream != NULL) {
195658912562617941964939a4182cda71eaeb153d4bGlenn Kasten
195746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // or (re-)create if current Pipe is idle and does not match the new format
196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<NBAIO_Sink> teeSink;
196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        enum {
196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NO,    // don't copy input
196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NEW,   // copy input using a new pipe
196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_OLD,   // copy input using an existing pipe
196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } kind;
1966329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten        NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
1967329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten                audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
1968da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (!mTeeSinkInputEnabled) {
1969da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            kind = TEE_SINK_NO;
19706e0d67d7b496ce17c0970a4ffd3a6f808860949cGlenn Kasten        } else if (!Format_isValid(format)) {
197181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink == 0) {
197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink->getStrongCount() != 1) {
197581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
1976f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten        } else if (Format_isEqual(format, mRecordTeeSink->format())) {
197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_OLD;
19784adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten        } else {
197981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
198081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
198181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        switch (kind) {
198281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NEW: {
1983da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
198481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numCounterOffers = 0;
198581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const NBAIO_Format offers[1] = {format};
198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
198781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            PipeReader *pipeReader = new PipeReader(*pipe);
198981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            numCounterOffers = 0;
199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSink = pipe;
199381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSource = pipeReader;
199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = pipe;
19954adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten            }
199681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
199781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_OLD:
199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = mRecordTeeSink;
199981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NO:
200181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        default:
200281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
200358912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
200446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2005da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2006cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
200765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Start record thread
200934af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten        // RecordThread requires both input and output device indication to forward to audio
201081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // pre processing modules
201183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        sp<RecordThread> thread = new RecordThread(this,
2012cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  inputStream,
2013cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  *input,
2014d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                  primaryOutputDevice_l(),
201583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                  device
201646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
201746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  , teeSink
201846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
201946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  );
2020cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        mRecordThreads.add(*input, thread);
2021cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
202283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        return thread;
20231afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent    }
202481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2025cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    *input = AUDIO_IO_HANDLE_NONE;
202681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
202765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
202865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
202981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input)
203065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
203181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeInput_nonvirtual(input);
203265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
203365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2035de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
203681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the record thread so that
203781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
203881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread> thread;
203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
204181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
204281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
204381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
2044de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
204581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
204681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeInput() %d", input);
20471b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
20481b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // If we still have effect chains, it means that a client still holds a handle
20491b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // on at least one effect. We must either move the chain to an existing thread with the
20501b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // same session ID or put it aside in case a new record thread is opened for a
20511b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // new capture on the same session
20521b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        sp<EffectChain> chain;
2053aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        {
2054aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            Mutex::Autolock _sl(thread->mLock);
2055aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
20561b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // Note: maximum one chain per record thread
20571b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (effectChains.size() != 0) {
20581b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                chain = effectChains[0];
20591b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
20601b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        }
20611b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        if (chain != 0) {
20621b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // first check if a record thread is already opened with a client on the same session.
20631b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // This should only happen in case of overlap between one thread tear down and the
20641b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // creation of its replacement
20651b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            size_t i;
20661b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            for (i = 0; i < mRecordThreads.size(); i++) {
20671b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                sp<RecordThread> t = mRecordThreads.valueAt(i);
20681b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                if (t == thread) {
20691b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    continue;
20701b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                }
20711b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                if (t->hasAudioSession(chain->sessionId()) != 0) {
20721b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    Mutex::Autolock _l(t->mLock);
20731b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    ALOGV("closeInput() found thread %d for effect session %d",
20741b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                          t->id(), chain->sessionId());
20751b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    t->addEffectChain_l(chain);
20761b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    break;
20771b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                }
20781b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
20791b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // put the chain aside if we could not find a record thread with the same session id.
20801b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (i == mRecordThreads.size()) {
20811b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                putOrphanEffectChain_l(chain);
2082aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
2083aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
2084021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
208581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.removeItem(input);
2086de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
208783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
208883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // we have a different lock for notification client
208983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeInputFinish(thread);
209083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    return NO_ERROR;
209183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
2092de070137f11d346fba77605bd76a44c040a618fcEric Laurent
209383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeInputFinish(sp<RecordThread> thread)
209483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
209583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    thread->exit();
209681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *in = thread->clearInput();
209781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
209881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // from now on thread->mInput is NULL
209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    in->hwDev()->close_input_stream(in->hwDev(), in->stream);
210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete in;
210183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
210265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
210383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
210483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
210583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mRecordThreads.removeItem(thread->mId);
210683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeInputFinish(thread);
2107b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
2108b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
2109d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kastenstatus_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
2110b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{
2111b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    Mutex::Autolock _l(mLock);
2112d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten    ALOGV("invalidateStream() stream %d", stream);
2113b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
211481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
211581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->invalidateTracks(stream);
2117b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    }
211881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
2120b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
2121b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
212281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2123de3f8392fbf380ba6f09d009b00d7172477389a2Eric Laurentaudio_unique_id_t AudioFlinger::newAudioUniqueId()
2124162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{
212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return nextUniqueId();
2126162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent}
2127162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent
2128d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
2129a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{
2130a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    Mutex::Autolock _l(mLock);
213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2132d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2133d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2134d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2135d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
2136d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
2137021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
2138021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
2139021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Ignore requests received from processes not known as notification client. The request
2140021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2141021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // called from a different pid leaving a stale session reference.  Also we don't know how
2142021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // to clear this reference if the client process dies.
2143021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(caller) < 0) {
2144021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2145021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            return;
2146021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
2147d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
2148d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
214981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
215081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
215281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
215381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt++;
215481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" incremented refcount to %d", ref->mCnt);
215581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
2156a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
2157a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    }
215881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
215981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV(" added new entry for %d", audioSession);
2160a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent}
2161a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
2162d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
216344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{
216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2166d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2167d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2168d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2169d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
217081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
217381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
217481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt--;
217581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" decremented refcount to %d", ref->mCnt);
217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mCnt == 0) {
217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioSessionRefs.removeAt(i);
217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                delete ref;
217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                purgeStaleEffects_l();
218044a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent            }
218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
218244a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent        }
218344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent    }
2184d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // If the caller is mediaserver it is likely that the session being released was acquired
2185d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // on behalf of a process not in notification clients and we ignore the warning.
2186d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
218744a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent}
218844a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent
218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() {
219065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("purging stale effects");
2192fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten
219381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > chains;
219458912562617941964939a4182cda71eaeb153d4bGlenn Kasten
219581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
219781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
219881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
219981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chains.push(ec);
220181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2202c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
220358912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
220481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread> t = mRecordThreads.valueAt(i);
220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chains.push(ec);
220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2210300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten    }
221165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < chains.size(); i++) {
221381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> ec = chains[i];
221481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionid = ec->sessionId();
221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> t = ec->mThread.promote();
221681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numsessionrefs = mAudioSessionRefs.size();
222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool found = false;
222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t k = 0; k < numsessionrefs; k++) {
222281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
222381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mSessionid == sessionid) {
222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV(" session %d still exists for %d with %d refs",
222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sessionid, ref->mPid, ref->mCnt);
222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                found = true;
222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
222858912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
222958912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!found) {
2231e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            Mutex::Autolock _l(t->mLock);
223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // remove all effects from the chain
223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            while (ec->mEffects.size()) {
223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectModule> effect = ec->mEffects[0];
223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->unPin();
223681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                t->removeEffect_l(effect);
223781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (effect->purgeHandles()) {
223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioSystem::unregisterEffect(effect->id());
224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2242c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
224358912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return;
224565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
224665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
224881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
2249190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{
225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mPlaybackThreads.valueFor(output).get();
2251190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten}
2252190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held
225481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2259190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held
226181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mRecordThreads.valueFor(input).get();
226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2265190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::nextUniqueId()
226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
2268bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten    return (uint32_t) android_atomic_inc(&mNextUniqueId);
226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
227083efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten
227181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
227237d825e72a6c606553a745da1212590a425996d3Glenn Kasten{
227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *output = thread->getOutput();
227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
227781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return thread;
227837d825e72a6c606553a745da1212590a425996d3Glenn Kasten        }
227937d825e72a6c606553a745da1212590a425996d3Glenn Kasten    }
228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NULL;
228137d825e72a6c606553a745da1212590a425996d3Glenn Kasten}
228237d825e72a6c606553a745da1212590a425996d3Glenn Kasten
228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const
228465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
2286000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
22899f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten    }
2290000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread->outDevice();
2292000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2293688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int triggerSession,
229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int listenerSession,
229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    sync_event_callback_t callBack,
22988ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent                                    wp<RefBase> cookie)
229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
230173ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten
230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t playStatus = NAME_NOT_FOUND;
230481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t recStatus = NAME_NOT_FOUND;
230581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
230681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
230781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playStatus == NO_ERROR) {
230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
231065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (recStatus == NO_ERROR) {
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
23159f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten        }
231665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPendingSyncEvents.add(event);
231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createSyncEvent() invalid event %d", event->type());
232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        event.clear();
232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event;
232465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
232565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
232781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//  Effect management
232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
232958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
233058912562617941964939a4182cda71eaeb153d4bGlenn Kasten
233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
2332000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryNumberEffects(numEffects);
2335000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2336000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
233858912562617941964939a4182cda71eaeb153d4bGlenn Kasten{
233981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryEffect(index, descriptor);
234158912562617941964939a4182cda71eaeb153d4bGlenn Kasten}
234258912562617941964939a4182cda71eaeb153d4bGlenn Kasten
234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
234481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *descriptor) const
2345000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
234781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectGetDescriptor(pUuid, descriptor);
2348000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2349000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23518d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect(
235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *pDesc,
235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t io,
235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status,
235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *id,
235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled)
2360000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus = NO_ERROR;
236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc;
2364000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
23658d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid, effectClient.get(), priority, sessionId, io);
2368000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDesc == NULL) {
237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2372952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2373000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check audio settings permission for global effects
237581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2378952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2379000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that can only be created by audio policy manager (running in same process)
238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2385952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
238665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!EffectIsNullUuid(&pDesc->uuid)) {
238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is specified, request effect descriptor
239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
239181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is not specified, look for an available implementation
239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // of the required type in effect factory
239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (EffectIsNullUuid(&pDesc->type)) {
239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() no effect type");
240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2402288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
240381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t numEffects = 0;
240481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect_descriptor_t d;
240581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            d.flags = 0; // prevent compiler warning
240681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool found = false;
2407288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectQueryNumberEffects(&numEffects);
240981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
241081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
241281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
241381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (uint32_t i = 0; i < numEffects; i++) {
241481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = EffectQueryEffect(i, &desc);
241581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (lStatus < 0) {
241681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
241781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
2418d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
242081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If matching type found save effect descriptor. If the session is
242181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // 0 and the effect is not auxiliary, continue enumeration in case
242281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // an auxiliary version of this effect type is available
242381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    found = true;
242481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    d = desc;
242581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
242681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2427d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                        break;
2428d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                    }
2429d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
2430288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
243181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (!found) {
243281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
243381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() effect not found");
243481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
243581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
243681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For same effect type, chose auxiliary version over insert version if
243781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // connect to output mix (Compliance to OpenSL ES)
243881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
243981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
244081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc = d;
244158912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
244258912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
244358912562617941964939a4182cda71eaeb153d4bGlenn Kasten
244481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Do not allow auxiliary effects on a session different from 0 (output mix)
244581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
244681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
244781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = INVALID_OPERATION;
244881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
24493dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent        }
245065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
245181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check recording permission for visualizer
245281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
245381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            !recordingAllowed()) {
245481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = PERMISSION_DENIED;
245581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
245681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
245765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
245881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return effect descriptor
245981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *pDesc = desc;
2460142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2461eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // if the output returned by getOutputForEffect() is removed before we lock the
2462eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2463eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // and we will exit safely
2464eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            io = AudioSystem::getOutputForEffect(&desc);
2465eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            ALOGV("createEffect got output %d", io);
2466eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        }
2467eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent
2468eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        Mutex::Autolock _l(mLock);
246965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
247081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is not specified try to find a matching audio session ID in one of the
247181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // output threads.
247281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
247381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // because of code checking output when entering the function.
247481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Note: io is never 0 when creating an effect on an input
2475142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE) {
24765baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
24775baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // output must be specified by AudioPolicyManager when using session
24785baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // AUDIO_SESSION_OUTPUT_STAGE
24795baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                lStatus = BAD_VALUE;
24805baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                goto Exit;
24815baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
2482eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // look for the thread where the specified audio session is present
2483eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2484eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2485eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    io = mPlaybackThreads.keyAt(i);
2486eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    break;
2487eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                }
2488d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
248981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0) {
2490eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
2491eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2492eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                        io = mRecordThreads.keyAt(i);
249381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
24942986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
249565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
249665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
249781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If no output thread contains the requested session ID, default to
249881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // first output. The effect chain will be moved to the correct output
249981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // thread when a track with the same session ID is created
2500142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten            if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
250181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                io = mPlaybackThreads.keyAt(0);
250281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
250381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect() got io %d for effect %s", io, desc.name);
250465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
250581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ThreadBase *thread = checkRecordThread_l(io);
250681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
250781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = checkPlaybackThread_l(io);
250881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (thread == NULL) {
250981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGE("createEffect() unknown output thread");
251081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
251181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2512288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
2513aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        } else {
2514aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            // Check if one effect chain was awaiting for an effect to be created on this
2515aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            // session and used it instead of creating a new one.
2516aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2517aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            if (chain != 0) {
25181b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                Mutex::Autolock _l(thread->mLock);
2519aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                thread->addEffectChain_l(chain);
2520aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
2521288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten        }
2522288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
2523021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        sp<Client> client = registerPid(pid);
252458912562617941964939a4182cda71eaeb153d4bGlenn Kasten
252581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect on selected output thread
252681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = thread->createEffect_l(client, effectClient, priority, sessionId,
252781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                &desc, enabled, &lStatus);
252881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (handle != 0 && id != NULL) {
252981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *id = handle->id();
253065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
2531fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        if (handle == 0) {
2532fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            // remove local strong reference to Client with mClientLock held
2533fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
2534fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
2535fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
253665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
253765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
253881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
25399156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
254081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
254166fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten}
254266fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten
254381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
254481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t dstOutput)
254565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
254681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
254781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcOutput, dstOutput);
254865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
254981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcOutput == dstOutput) {
255081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
255181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
255281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
255381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
255481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcThread == NULL) {
255581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad srcOutput %d", srcOutput);
255681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
255781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
255881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
255981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (dstThread == NULL) {
256081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad dstOutput %d", dstOutput);
256181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
256265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
256365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
256481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _dl(dstThread->mLock);
256581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _sl(srcThread->mLock);
25665baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return moveEffectChain_l(sessionId, srcThread, dstThread, false);
256765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
256865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
256981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
257081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffectChain_l(int sessionId,
257181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *srcThread,
257281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *dstThread,
257381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   bool reRegister)
257465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
257581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
257681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcThread, dstThread);
257765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
257881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
257981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
258081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
258181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sessionId, srcThread);
258281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
258381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
25849ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
25859a59276fb465e492138e0576523b54079671e8f4Andy Hung    // Check whether the destination thread has a channel count of FCC_2, which is
25869a59276fb465e492138e0576523b54079671e8f4Andy Hung    // currently required for (most) effects. Prevent moving the effect chain here rather
25879a59276fb465e492138e0576523b54079671e8f4Andy Hung    // than disabling the addEffect_l() call in dstThread below.
25889a59276fb465e492138e0576523b54079671e8f4Andy Hung    if (dstThread->mChannelCount != FCC_2) {
25899a59276fb465e492138e0576523b54079671e8f4Andy Hung        ALOGW("moveEffectChain_l() effect chain failed because"
25909a59276fb465e492138e0576523b54079671e8f4Andy Hung                " destination thread %p channel count(%u) != %u",
25919a59276fb465e492138e0576523b54079671e8f4Andy Hung                dstThread, dstThread->mChannelCount, FCC_2);
25929a59276fb465e492138e0576523b54079671e8f4Andy Hung        return INVALID_OPERATION;
25939a59276fb465e492138e0576523b54079671e8f4Andy Hung    }
25949a59276fb465e492138e0576523b54079671e8f4Andy Hung
259581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
259681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // so that a new chain is created with correct parameters when first effect is added. This is
259781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
259881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // removed.
259981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    srcThread->removeEffectChain_l(chain);
26009ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
260181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // transfer all effects one by one so that new effect chain is created on new thread with
260281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
260381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> dstChain;
260481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy = 0; // prevent compiler warning
260581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect = chain->getEffectFromId_l(0);
26065baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    Vector< sp<EffectModule> > removed;
26075baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    status_t status = NO_ERROR;
260881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (effect != 0) {
260981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        srcThread->removeEffect_l(effect);
26105baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        removed.add(effect);
26115baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        status = dstThread->addEffect_l(effect);
26125baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (status != NO_ERROR) {
26135baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
26145baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
261581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // removeEffect_l() has stopped the effect if it was active so it must be restarted
261681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect->state() == EffectModule::ACTIVE ||
261781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->state() == EffectModule::STOPPING) {
261881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->start();
261965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the move request is not received from audio policy manager, the effect must be
262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // re-registered with the new strategy and output
262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (dstChain == 0) {
262381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            dstChain = effect->chain().promote();
262481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (dstChain == 0) {
262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
26265baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                status = NO_INIT;
26275baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                break;
262865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            strategy = dstChain->strategy();
263065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
263181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (reRegister) {
263281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::registerEffect(&effect->desc(),
26345baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                        dstThread->id(),
263581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        strategy,
263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        sessionId,
263781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        effect->id());
2638d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent            AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
264081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect = chain->getEffectFromId_l(0);
264158912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
264258912562617941964939a4182cda71eaeb153d4bGlenn Kasten
26435baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (status != NO_ERROR) {
26445baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        for (size_t i = 0; i < removed.size(); i++) {
26455baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            srcThread->addEffect_l(removed[i]);
26465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (dstChain != 0 && reRegister) {
26475baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::unregisterEffect(removed[i]->id());
26485baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::registerEffect(&removed[i]->desc(),
26495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            srcThread->id(),
26505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            strategy,
26515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            sessionId,
26525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            removed[i]->id());
2653d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent                AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
26545baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
26555baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
26565baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    }
26575baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
26585baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return status;
265965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
266065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
26615baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
2662813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2663813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    if (mGlobalEffectEnableTime != 0 &&
2664813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2665813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        return true;
2666813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2667813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2668813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2669813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<EffectChain> ec =
2670813e2a74853bde19e37d878c596a044b3f299efcEric Laurent                mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
26715baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (ec != 0 && ec->isNonOffloadableEnabled()) {
2672813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            return true;
2673813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2674813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2675813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    return false;
2676813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2677813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
26785baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable()
2679813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2680813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    Mutex::Autolock _l(mLock);
2681813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2682813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    mGlobalEffectEnableTime = systemTime();
2683813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2684813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2685813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2686813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        if (t->mType == ThreadBase::OFFLOAD) {
2687813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            t->invalidateTracks(AUDIO_STREAM_MUSIC);
2688813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2689813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2690813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2691813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2692813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2693aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentstatus_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2694aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2695aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    audio_session_t session = (audio_session_t)chain->sessionId();
2696aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2697aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2698aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2699aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2700aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        return ALREADY_EXISTS;
2701aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2702aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    mOrphanEffectChains.add(session, chain);
2703aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return NO_ERROR;
2704aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2705aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2706aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2707aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2708aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    sp<EffectChain> chain;
2709aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2710aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2711aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2712aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        chain = mOrphanEffectChains.valueAt(index);
2713aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        mOrphanEffectChains.removeItemsAt(index);
2714aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2715aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return chain;
2716aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2717aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2718aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentbool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2719aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2720aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    Mutex::Autolock _l(mLock);
2721aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    audio_session_t session = (audio_session_t)effect->sessionId();
2722aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2723aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2724aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2725aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2726aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        if (chain->removeEffect_l(effect) == 0) {
2727aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2728aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            mOrphanEffectChains.removeItemsAt(index);
2729aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
2730aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        return true;
2731aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2732aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return false;
2733aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2734aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2735aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2736da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry {
2737da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_NAME 32     // %Y%m%d%H%M%S_%d.wav
2738da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    char mName[MAX_NAME];
2739da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten};
2740da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2741da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2)
2742da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{
2743da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2744da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}
2745da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
274646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2747d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
274865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
2749d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten    NBAIO_Source *teeSource = source.get();
2750fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    if (teeSource != NULL) {
2751da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // .wav rotation
2752da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // There is a benign race condition if 2 threads call this simultaneously.
2753da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // They would both traverse the directory, but the result would simply be
2754da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // failures at unlink() which are ignored.  It's also unlikely since
2755da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // normally dumpsys is only done by bugreport or from the command line.
2756da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        char teePath[32+256];
2757da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strcpy(teePath, "/data/misc/media");
2758da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        size_t teePathLen = strlen(teePath);
2759da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        DIR *dir = opendir(teePath);
2760da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teePath[teePathLen++] = '/';
2761da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (dir != NULL) {
2762da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_SORT 20 // number of entries to sort
2763da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_KEEP 10 // number of entries to keep
2764da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            struct Entry entries[MAX_SORT];
2765da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            size_t entryCount = 0;
2766da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            while (entryCount < MAX_SORT) {
2767da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent de;
2768da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent *result = NULL;
2769da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                int rc = readdir_r(dir, &de, &result);
2770da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (rc != 0) {
2771da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r failed %d", rc);
2772da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2773da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2774da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result == NULL) {
2775da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2776da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2777da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result != &de) {
2778da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2779da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2780da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2781da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                // ignore non .wav file entries
2782da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                size_t nameLen = strlen(de.d_name);
2783da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (nameLen <= 4 || nameLen >= MAX_NAME ||
2784da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                        strcmp(&de.d_name[nameLen - 4], ".wav")) {
2785da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    continue;
2786da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2787da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                strcpy(entries[entryCount++].mName, de.d_name);
2788da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2789da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            (void) closedir(dir);
2790da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (entryCount > MAX_KEEP) {
2791da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                qsort(entries, entryCount, sizeof(Entry), comparEntry);
2792da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2793da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    strcpy(&teePath[teePathLen], entries[i].mName);
2794da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    (void) unlink(teePath);
2795da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2796da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2797da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        } else {
2798da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
27998b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2800da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2801da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
2802d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        char teeTime[16];
2803fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct timeval tv;
2804fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        gettimeofday(&tv, NULL);
2805fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct tm tm;
2806fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        localtime_r(&tv.tv_sec, &tm);
2807da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2808da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2809da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2810da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
2811fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        if (teeFd >= 0) {
2812329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME use libsndfile
2813fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            char wavHeader[44];
2814fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            memcpy(wavHeader,
2815fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn 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",
2816fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                sizeof(wavHeader));
2817fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            NBAIO_Format format = teeSource->format();
2818fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            unsigned channelCount = Format_channelCount(format);
28193b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten            uint32_t sampleRate = Format_sampleRate(format);
2820329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            size_t frameSize = Format_frameSize(format);
2821fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[22] = channelCount;       // number of channels
2822fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[24] = sampleRate;         // sample rate
2823fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[25] = sampleRate >> 8;
2824329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            wavHeader[32] = frameSize;          // block alignment
2825329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            wavHeader[33] = frameSize >> 8;
2826fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, wavHeader, sizeof(wavHeader));
2827fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            size_t total = 0;
2828fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            bool firstRead = true;
2829329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten#define TEE_SINK_READ 1024                      // frames per I/O operation
2830329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            void *buffer = malloc(TEE_SINK_READ * frameSize);
2831fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            for (;;) {
2832fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                size_t count = TEE_SINK_READ;
28332c3b2da3049627264b7c6b449a1622f002210f03John Grossman                ssize_t actual = teeSource->read(buffer, count,
28342c3b2da3049627264b7c6b449a1622f002210f03John Grossman                        AudioBufferProvider::kInvalidPTS);
2835fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                bool wasFirstRead = firstRead;
2836fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                firstRead = false;
2837fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                if (actual <= 0) {
2838fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2839fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                        continue;
2840fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    }
2841fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    break;
2842fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                }
2843a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent                ALOG_ASSERT(actual <= (ssize_t)count);
2844329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten                write(teeFd, buffer, actual * frameSize);
2845fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                total += actual;
2846fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            }
2847329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            free(buffer);
2848fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 4, SEEK_SET);
2849329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            uint32_t temp = 44 + total * frameSize - 8;
2850329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME not big-endian safe
2851fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2852fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 40, SEEK_SET);
2853329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            temp =  total * frameSize;
2854329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME not big-endian safe
2855fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2856fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            close(teeFd);
2857da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
28588b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "tee copied to %s\n", teePath);
2859da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2860fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        } else {
2861da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
28628b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2863da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2864fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        }
2865fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    }
2866d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten}
286746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2868d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
286965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
287065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
287165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact(
287265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
287365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
287465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioFlinger::onTransact(code, data, reply, flags);
287565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
287665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
287765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
2878