AudioFlinger.cpp revision 1c333e252cbca3337c1bedbc57a005f3b7d23fdb
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) {
109b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    switch(format) {
110b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_8_BIT: return "pcm8";
111b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_16_BIT: return "pcm16";
112b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_32_BIT: return "pcm32";
113b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_8_24_BIT: return "pcm8.24";
114b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED: return "pcm24";
115b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_FLOAT: return "pcmfloat";
116b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_MP3: return "mp3";
117b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_NB: return "amr-nb";
118b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_WB: return "amr-wb";
119b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AAC: return "aac";
120b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
121b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
122b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_VORBIS: return "vorbis";
123b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    default:
124b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        break;
125b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
126b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    return "unknown";
127b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen}
128b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
129f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
130799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
131f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    const hw_module_t *mod;
132799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc;
133799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
134f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
135f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
136f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
137f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
138799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
139f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
140f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = audio_hw_device_open(mod, dev);
141f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
142f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
143f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
144799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
145f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
1465806b3533437e3d83208e8e9d6bd74ed304e51ecEric Laurent    if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
147f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
148f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        rc = BAD_VALUE;
149f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        goto out;
150f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
151799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return 0;
152799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
153799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout:
154799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    *dev = NULL;
155799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return rc;
156799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
157799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
15865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
15965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger()
16165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    : BnAudioFlinger(),
1624ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPrimaryHardwareDev(NULL),
1639ea65d0f4a564478343b1a722fae4ce5883670c3Glenn Kasten      mAudioHwDevs(NULL),
1647d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten      mHardwareStatus(AUDIO_HW_IDLE),
1654ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterVolume(1.0f),
1664ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterMute(false),
1674ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mNextUniqueId(1),
1684ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMode(AUDIO_MODE_INVALID),
1694182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mBtNrecIsOff(false),
1704182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mIsLowRamDevice(true),
171813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mIsDeviceTypeKnown(false),
172813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mGlobalEffectEnableTime(0)
17365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
174949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten    getpid_cached = getpid();
1759e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    char value[PROPERTY_VALUE_MAX];
1769e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
1779e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (doLog) {
178cf515ab0030cf5e0bef843638af7c989a8f6843aGlenn Kasten        mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
1799e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
1801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
18146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
182da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    (void) property_get("ro.debuggable", value, "0");
183da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int debuggable = atoi(value);
184da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int teeEnabled = 0;
185da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    if (debuggable) {
186da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        (void) property_get("af.tee", value, "0");
187da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teeEnabled = atoi(value);
188da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    }
189f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten    // FIXME symbolic constants here
1906e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 1) {
191da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkInputEnabled = true;
1926e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1936e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 2) {
194da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkOutputEnabled = true;
1956e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1966e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 4) {
197da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkTrackEnabled = true;
1986e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
19946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2005a61d2f277af3098fc10b2881babca16391362daDima Zavin}
2015a61d2f277af3098fc10b2881babca16391362daDima Zavin
2025a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef()
2035a61d2f277af3098fc10b2881babca16391362daDima Zavin{
204799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc = 0;
205fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
206935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    Mutex::Autolock _l(mLock);
207935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
208799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    /* TODO: move all this work into an Init() function */
2094ff14bae91075eb274eb1c2975982358946e7e63John Grossman    char val_str[PROPERTY_VALUE_MAX] = { 0 };
2104ff14bae91075eb274eb1c2975982358946e7e63John Grossman    if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
2114ff14bae91075eb274eb1c2975982358946e7e63John Grossman        uint32_t int_val;
2124ff14bae91075eb274eb1c2975982358946e7e63John Grossman        if (1 == sscanf(val_str, "%u", &int_val)) {
2134ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = milliseconds(int_val);
2144ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using %u mSec as standby time.", int_val);
2154ff14bae91075eb274eb1c2975982358946e7e63John Grossman        } else {
2164ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
2174ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using default %u mSec as standby time.",
2184ff14bae91075eb274eb1c2975982358946e7e63John Grossman                    (uint32_t)(mStandbyTimeInNsecs / 1000000));
2194ff14bae91075eb274eb1c2975982358946e7e63John Grossman        }
2204ff14bae91075eb274eb1c2975982358946e7e63John Grossman    }
22165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2221c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    mPatchPanel = new PatchPanel(this);
2231c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
224a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    mMode = AUDIO_MODE_NORMAL;
22565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
22665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
22765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger()
22865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
22965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mRecordThreads.isEmpty()) {
230c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeInput_nonvirtual() will remove specified entry from mRecordThreads
231d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeInput_nonvirtual(mRecordThreads.keyAt(0));
23265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
23365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mPlaybackThreads.isEmpty()) {
234c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
235d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
23665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
237799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
2382b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2392b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten        // no mHardwareLock needed, as there are no other references to this
240a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
241a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        delete mAudioHwDevs.valueAt(i);
24265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
243481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
244481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Tell media.log service about any old writers that still need to be unregistered
245481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
246481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder != 0) {
247481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
248481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
249481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
250481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mUnregisteredWriters.pop();
251481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mediaLogService->unregisterWriter(iMemory);
252481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
253481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
254481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
25565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
25665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
257a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = {
258a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_PRIMARY,
259a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_A2DP,
260a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_USB,
261a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent};
262a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
263a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
264ee578c0330319f04a48bccbdb26b53fea0388d04John GrossmanAudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
265ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_module_handle_t module,
266ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_devices_t devices)
267799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
268a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // if module is 0, the request comes from an old policy manager and we should load
269a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // well known modules
270a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    if (module == 0) {
271a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
272a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
273a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            loadHwModule_l(audio_interfaces[i]);
274a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
275f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        // then try to find a module supporting the requested device.
276f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
277f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
278f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            audio_hw_device_t *dev = audioHwDevice->hwDevice();
279f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            if ((dev->get_supported_devices != NULL) &&
280f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    (dev->get_supported_devices(dev) & devices) == devices)
281f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                return audioHwDevice;
282f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        }
283a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    } else {
284a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        // check a match for the requested module handle
285ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
286ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (audioHwDevice != NULL) {
287ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            return audioHwDevice;
288a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
289a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    }
290a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
291799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return NULL;
292799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
29365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2940f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
29565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
29665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
29765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
29865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
29965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
30065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("Clients:\n");
30165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < mClients.size(); ++i) {
30277c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        sp<Client> client = mClients.valueAt(i).promote();
30377c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        if (client != 0) {
30477c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            snprintf(buffer, SIZE, "  pid: %d\n", client->pid());
30577c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            result.append(buffer);
30665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
30765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
3083a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
309d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    result.append("Notification Clients:\n");
310d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); ++i) {
311d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        snprintf(buffer, SIZE, "  pid: %d\n", mNotificationClients.keyAt(i));
312d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        result.append(buffer);
313d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
314d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
3153a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    result.append("Global session refs:\n");
316b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    result.append("  session   pid count\n");
3173a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
3183a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *r = mAudioSessionRefs[i];
319b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
3203a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        result.append(buffer);
3213a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3260f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
32765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
32865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
32965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
33065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
331a4454b4765c5905f14186893b0688be375642283Glenn Kasten    hardware_call_state hardwareStatus = mHardwareStatus;
33265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3334ff14bae91075eb274eb1c2975982358946e7e63John Grossman    snprintf(buffer, SIZE, "Hardware status: %d\n"
3344ff14bae91075eb274eb1c2975982358946e7e63John Grossman                           "Standby Time mSec: %u\n",
3354ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            hardwareStatus,
3364ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            (uint32_t)(mStandbyTimeInNsecs / 1000000));
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3410f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioFlinger from pid=%d, uid=%d\n",
34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex)
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3627dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten        usleep(kDumpLockSleepUs);
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args)
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
36944deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd, args);
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // get state of hardware lock
37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool hardwareLocked = dumpTryLock(mHardwareLock);
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!hardwareLocked) {
37565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kHardwareLockedString);
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mHardwareLock.unlock();
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool locked = dumpTryLock(mLock);
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // failed to lock - AudioFlinger is probably deadlocked
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
389021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        bool clientLocked = dumpTryLock(mClientLock);
390021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (!clientLocked) {
391021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            String8 result(kClientLockedString);
392021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            write(fd, result.string(), result.size());
393021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpClients(fd, args);
395021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (clientLocked) {
396021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mClientLock.unlock();
397021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
398021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent
39965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd, args);
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump playback threads
40265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mPlaybackThreads.valueAt(i)->dump(fd, args);
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
40565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump record threads
40765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
40865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mRecordThreads.valueAt(i)->dump(fd, args);
40965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
41065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
411799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        // dump all hardware devs
412799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
413a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
414799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            dev->dump(dev, fd);
41565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
416d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
41746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
418d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        // dump the serially shared record tee sink
419d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        if (mRecordTeeSource != 0) {
420d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten            dumpTee(fd, mRecordTeeSource);
421d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        }
42246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
423d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
424d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        if (locked) {
425d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            mLock.unlock();
426d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
4279e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4289e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // append a copy of media.log here by forwarding fd to it, but don't attempt
4299e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // to lookup the service if it's not running, as it will block for a second
4309e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        if (mLogMemoryDealer != 0) {
4319e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
4329e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (binder != 0) {
4338b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "\nmedia.log:\n");
4349e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                Vector<String16> args;
4359e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                binder->dump(fd, args);
4369e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
4379e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        }
43865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
43965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
44065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
44165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
442021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentsp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
44398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{
444021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _cl(mClientLock);
44598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // If pid is already in the mClients wp<> map, then use that entry
44698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // (for which promote() is always != 0), otherwise create a new entry and Client.
44798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    sp<Client> client = mClients.valueFor(pid).promote();
44898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    if (client == 0) {
44998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = new Client(this, pid);
45098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        mClients.add(pid, client);
45198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    }
45298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten
45398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    return client;
45498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten}
45565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4569e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
4579e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
458481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If there is no memory allocated for logs, return a dummy writer that does nothing
4599e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (mLogMemoryDealer == 0) {
4609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return new NBLog::Writer();
4619e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
4629e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
463481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Similarly if we can't contact the media.log service, also return a dummy writer
464481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder == 0) {
465481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
4669e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
467481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
468481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
469481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If allocation fails, consult the vector of previously unregistered writers
470481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // and garbage-collect one or more them until an allocation succeeds
471481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (shared == 0) {
472481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        Mutex::Autolock _l(mUnregisteredWritersLock);
473481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
474481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            {
475481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Pick the oldest stale writer to garbage-collect
476481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
477481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mUnregisteredWriters.removeAt(0);
478481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mediaLogService->unregisterWriter(iMemory);
479481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Now the media.log remote reference to IMemory is gone.  When our last local
480481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // reference to IMemory also drops to zero at end of this block,
481481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // the IMemory destructor will deallocate the region from mLogMemoryDealer.
482481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
483481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            // Re-attempt the allocation
484481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
485481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            if (shared != 0) {
486481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                goto success;
487481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
488481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
489481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // Even after garbage-collecting all old writers, there is still not enough memory,
490481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // so return a dummy writer
491481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
492481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
493481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kastensuccess:
494481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mediaLogService->registerWriter(shared, size, name);
495481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    return new NBLog::Writer(size, shared);
4969e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
4979e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4989e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
4999e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
500685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    if (writer == 0) {
501685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten        return;
502685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    }
5039e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IMemory> iMemory(writer->getIMemory());
5049e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (iMemory == 0) {
5059e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return;
5069e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
507481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Rather than removing the writer immediately, append it to a queue of old writers to
508481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // be garbage-collected later.  This allows us to continue to view old logs for a while.
509481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Mutex::Autolock _l(mUnregisteredWritersLock);
510481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mUnregisteredWriters.push(writer);
5119e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5129e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
51365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface
51465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
51565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
51665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack(
517fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
51865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t sampleRate,
51958f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
520254af180475346b6186b49c297f340c9c4817511Glenn Kasten        audio_channel_mask_t channelMask,
52174935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
522e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten        IAudioFlinger::track_flags_t *flags,
52365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const sp<IMemory>& sharedBuffer,
52472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output,
5253acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        pid_t tid,
52665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int *sessionId,
527462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int clientUid,
52865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status_t *status)
52965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
53065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<PlaybackThread::Track> track;
53165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<TrackHandle> trackHandle;
53265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<Client> client;
53365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t lStatus;
53465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int lSessionId;
53565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
536263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
537263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // but if someone uses binder directly they could bypass that and cause us to crash
538263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
53929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("createTrack() invalid stream type %d", streamType);
54065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        lStatus = BAD_VALUE;
54165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        goto Exit;
54265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
54365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
54453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createTrack_l() depending on the thread type
54553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
54653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid sample rate %u", sampleRate);
54753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
54853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
54953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
55053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
55153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createTrack_l() depending on the thread type
55253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_output_channel(channelMask)) {
55353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid channel mask %#x", channelMask);
55453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
55553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
55653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
55753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
558c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    // further format checks are performed by createTrack_l() depending on the thread type
559c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    if (!audio_is_valid_format(format)) {
560cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("createTrack() invalid format %#x", format);
56160a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        lStatus = BAD_VALUE;
562663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        goto Exit;
563663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    }
564663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten
565663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
566663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
567663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        lStatus = BAD_VALUE;
56860a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        goto Exit;
56960a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    }
57060a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
57365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
57465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
575c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten            ALOGE("no playback thread found for output handle %d", output);
57665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lStatus = BAD_VALUE;
57765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            goto Exit;
57865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
57965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5808d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
581021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
58265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
583e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        PlaybackThread *effectThread = NULL;
584aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
585570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            lSessionId = *sessionId;
586f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // check if an effect chain with the same session ID is present on another
587f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // output thread and move it here.
588de070137f11d346fba77605bd76a44c040a618fcEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
58939e94f8f723d445447fdee0822291e664b631f60Eric Laurent                sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
59039e94f8f723d445447fdee0822291e664b631f60Eric Laurent                if (mPlaybackThreads.keyAt(i) != output) {
591570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten                    uint32_t sessions = t->hasAudioSession(lSessionId);
59239e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    if (sessions & PlaybackThread::EFFECT_SESSION) {
59339e94f8f723d445447fdee0822291e664b631f60Eric Laurent                        effectThread = t.get();
594f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent                        break;
59539e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    }
596de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
597de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
59865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
599de070137f11d346fba77605bd76a44c040a618fcEric Laurent            // if no audio session id is provided, create one here
6007c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            lSessionId = nextUniqueId();
60165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (sessionId != NULL) {
60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                *sessionId = lSessionId;
60365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
60465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
6053856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() lSessionId: %d", lSessionId);
60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
60765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track = thread->createTrack_l(client, streamType, sampleRate, format,
608462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
60903e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
6102fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten        // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
61139e94f8f723d445447fdee0822291e664b631f60Eric Laurent
61239e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // move effect chain to this output thread if an effect on same session was waiting
61339e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // for a track to be created
61439e94f8f723d445447fdee0822291e664b631f60Eric Laurent        if (lStatus == NO_ERROR && effectThread != NULL) {
6152fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten            // no risk of deadlock because AudioFlinger::mLock is held
61639e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _dl(thread->mLock);
61739e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _sl(effectThread->mLock);
61839e94f8f723d445447fdee0822291e664b631f60Eric Laurent            moveEffectChain_l(lSessionId, effectThread, thread, true);
61939e94f8f723d445447fdee0822291e664b631f60Eric Laurent        }
620a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
621a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        // Look for sync events awaiting for a session to be used.
6223ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn        for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
623a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
624a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
6252986460984580833161bdaabc7f17da1005a8961Eric Laurent                    if (lStatus == NO_ERROR) {
626d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten                        (void) track->setSyncEvent(mPendingSyncEvents[i]);
6272986460984580833161bdaabc7f17da1005a8961Eric Laurent                    } else {
6282986460984580833161bdaabc7f17da1005a8961Eric Laurent                        mPendingSyncEvents[i]->cancel();
6292986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
630a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    mPendingSyncEvents.removeAt(i);
631a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    i--;
632a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                }
633a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            }
634a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
635e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
63665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
637e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
6383ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    if (lStatus != NO_ERROR) {
6393ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        // remove local strong reference to Client before deleting the Track so that the
640021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
641021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
64265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        client.clear();
64365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track.clear();
6443ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        goto Exit;
64565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6473ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    // return handle to client
6483ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    trackHandle = new TrackHandle(track);
6493ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten
65065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit:
6519156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
65265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return trackHandle;
65365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
65465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
65572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
65665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
65765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
65865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
65965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6605ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("sampleRate() unknown thread %d", output);
66165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
66265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->sampleRate();
66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
66565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
66672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenint AudioFlinger::channelCount(audio_io_handle_t output) const
66765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
66865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
66965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
67065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6715ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("channelCount() unknown thread %d", output);
67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->channelCount();
67565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
67665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
67772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(audio_io_handle_t output) const
67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
67965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
68065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6825ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("format() unknown thread %d", output);
68358f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        return AUDIO_FORMAT_INVALID;
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->format();
68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
68765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
68872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastensize_t AudioFlinger::frameCount(audio_io_handle_t output) const
68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
69065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6935ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("frameCount() unknown thread %d", output);
69465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
69658912562617941964939a4182cda71eaeb153d4bGlenn Kasten    // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
69758912562617941964939a4182cda71eaeb153d4bGlenn Kasten    //       should examine all callers and fix them to handle smaller counts
69865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->frameCount();
69965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
70065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
70172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
70365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
70465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
70565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
706c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten        ALOGW("latency(): no playback thread found for output handle %d", output);
70765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->latency();
71065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
71165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
71265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value)
71365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
714a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
715a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
716a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
717a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
718a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
72165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
72365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
724a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
725ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterVolume = value;
726a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
727ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master volume in the HALs which support it.
728ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
729ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
730ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
7314ff14bae91075eb274eb1c2975982358946e7e63John Grossman
732ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
733ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterVolume()) {
734ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
735935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent        }
736ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
73765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
73865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
739ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master volume in each playback thread.  Playback threads
740ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master volume support will apply
741ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // master volume during the mix operation.  Threads with HALs which do
742ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // support master volume will simply ignore the setting.
7438d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
744ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterVolume(value);
74565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
74765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
74865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
749f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode)
75065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
751a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
752a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
753a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
754a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
75765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
75865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
75965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
760930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(mode) >= AUDIO_MODE_CNT) {
7615ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("Illegal value: setMode(%d)", mode);
76265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
76365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
76465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    { // scope for the lock
76665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex lock(mHardwareLock);
767ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
76865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_SET_MODE;
769ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        ret = dev->set_mode(dev, mode);
77065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_IDLE;
77165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
77265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (NO_ERROR == ret) {
77465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
77565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mMode = mode;
7768d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++)
777e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setMode(mode);
77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
77965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
78165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state)
78465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
785a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
786a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
787a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
788a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
789a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
79265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
79365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
79465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
796ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
79765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
798ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_mic_mute(dev, state);
79965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
80165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
80265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const
80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
805a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
806a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
807a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return false;
808a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
809a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
810fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    bool state = AUDIO_MODE_INVALID;
8112b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
812ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
81365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
814ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    dev->get_mic_mute(dev, &state);
81565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
81665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return state;
81765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
81865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
81965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted)
82065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
821d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    status_t ret = initCheck();
822d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    if (ret != NO_ERROR) {
823d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        return ret;
824d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
825d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
82765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
82865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
82965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
83065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
831ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    Mutex::Autolock _l(mLock);
832ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterMute = muted;
833d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
834ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master mute in the HALs which support it.
835ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
836ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
837ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
838d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
839ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
840ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterMute()) {
841ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
842d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        }
843ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
844d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
845d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
846ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master mute in each playback thread.  Playback threads
847ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master mute support will apply master
848ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute during the mix operation.  Threads with HALs which do support master
849ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute will simply ignore the setting.
8508d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
851ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterMute(muted);
85265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const
85765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8589806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8599806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterVolume_l();
86065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
86165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const
86365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8649806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8659806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterMute_l();
86665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
86765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8684ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const
8694ff14bae91075eb274eb1c2975982358946e7e63John Grossman{
8704ff14bae91075eb274eb1c2975982358946e7e63John Grossman    return mMasterVolume;
8714ff14bae91075eb274eb1c2975982358946e7e63John Grossman}
8724ff14bae91075eb274eb1c2975982358946e7e63John Grossman
873d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const
874d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{
875ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    return mMasterMute;
876d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman}
877d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
87872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
87972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
88065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
88165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
88365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
88565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
886263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
88729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamVolume() invalid stream %d", stream);
88865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
88965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
89065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
89165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
89265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = NULL;
893142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
89465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(output);
89565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
89665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return BAD_VALUE;
89765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
89865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
89965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].volume = value;
90165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
9038d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
904e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
90665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
90765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread->setStreamVolume(stream, value);
90865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
90965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
91065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
91165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
91265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
913fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
91465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
91565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
91665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
91765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
91865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
91965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
920263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
921fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
92229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamMute() invalid stream %d", stream);
92365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
92465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
92565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
926935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    AutoMutex lock(mLock);
92765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].mute = muted;
9283ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
929e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
93065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
93165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
93265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
93365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
93472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
93565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
936263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
93765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0.0f;
93865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
93965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
94065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
94165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    float volume;
942142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
94365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
94465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
94565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return 0.0f;
94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
94765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        volume = thread->streamVolume(stream);
94865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
9496637baae4244aec731c4014da72418d330636ae1Glenn Kasten        volume = streamVolume_l(stream);
95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
95165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return volume;
95365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
95465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
955fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const
95665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
957263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
95865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return true;
95965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
96065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9616637baae4244aec731c4014da72418d330636ae1Glenn Kasten    AutoMutex lock(mLock);
9626637baae4244aec731c4014da72418d330636ae1Glenn Kasten    return streamMute_l(stream);
96365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
96465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
96572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
96665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
967827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
968827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
96981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
97065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
97165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
97265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
97365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
97465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
975142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
976142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
977a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        Mutex::Autolock _l(mLock);
978799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        status_t final_result = NO_ERROR;
9798abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        {
980a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            AutoMutex lock(mHardwareLock);
981a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_SET_PARAMETER;
982a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
983a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
984a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                status_t result = dev->set_parameters(dev, keyValuePairs.string());
985a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                final_result = result ?: final_result;
986a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            }
987a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
9888abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        }
98959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
99059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        AudioParameter param = AudioParameter(keyValuePairs);
99159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        String8 value;
99259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
993bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
994bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            if (mBtNrecIsOff != btNrecIsOff) {
99559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
99659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    sp<RecordThread> thread = mRecordThreads.valueAt(i);
997f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    audio_devices_t device = thread->inDevice();
998510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
999510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // collect all of the thread's session IDs
1000510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    KeyedVector<int, bool> ids = thread->sessionIds();
1001510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // suspend effects associated with those session IDs
1002510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    for (size_t j = 0; j < ids.size(); ++j) {
1003510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                        int sessionId = ids.keyAt(j);
100459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_AEC,
100559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1006510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
100759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_NS,
100859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1009510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
101059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    }
101159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                }
1012bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent                mBtNrecIsOff = btNrecIsOff;
101359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent            }
101459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        }
101528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        String8 screenState;
101628ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
101728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            bool isOff = screenState == "off";
101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isOff != (AudioFlinger::mScreenState & 1)) {
101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
102028ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            }
102128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        }
1022799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        return final_result;
102365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
102465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
102565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // hold a strong ref on thread in case closeOutput() or closeInput() is called
102665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // and the thread is exited once the lock is released
102765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ThreadBase> thread;
102865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
102965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
103065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(ioHandle);
1031d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten        if (thread == 0) {
103265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            thread = checkRecordThread_l(ioHandle);
10337fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten        } else if (thread == primaryPlaybackThread_l()) {
10347c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // indicate output device change to all input threads for pre processing
10357c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            AudioParameter param = AudioParameter(keyValuePairs);
10367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int value;
103789d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent            if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
103889d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent                    (value != 0)) {
10397c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
10407c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                    mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
10417c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                }
10427c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
104365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
104465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
10457378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (thread != 0) {
10467378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten        return thread->setParameters(keyValuePairs);
104765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
104865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
104965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
105065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
105172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
105265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1053827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1054827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
105565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1056a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
1057a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
1058142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
1059fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        String8 out_s8;
1060fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1061799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
10628abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            char *s;
10638abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            {
10648abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            AutoMutex lock(mHardwareLock);
10658abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_GET_PARAMETER;
1066a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
10678abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            s = dev->get_parameters(dev, keys.string());
10688abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_IDLE;
10698abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            }
1070ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman            out_s8 += String8(s ? s : "");
1071799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            free(s);
1072799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        }
1073fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return out_s8;
107465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
107565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
107665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
107765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
107865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getParameters(keys);
107965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
108065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
108165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
108265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getParameters(keys);
108365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
108465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return String8("");
108565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
108665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1087dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1088dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask) const
108965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1090a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1091a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1092a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return 0;
1093a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1094a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
10952b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
10962b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
1097ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1098ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    memset(&config, 0, sizeof(config));
1099ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = sampleRate;
1100ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = channelMask;
1101ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = format;
1102ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1103ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1104ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    size_t size = dev->get_input_buffer_size(dev, &config);
11052b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_IDLE;
11062b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    return size;
110765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
110865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
11095f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
111065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
111165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
111265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
111365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
111465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
111565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getInputFramesLost();
111665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
111765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return 0;
111865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
111965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value)
112165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1122a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1123a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1124a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
1125a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1126a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
112765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
112865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
112965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
113065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
113165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
113265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
1133ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
11348abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten    mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
1135ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_voice_volume(dev, value);
113665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
113765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
113865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
113965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
114065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1141377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
114272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output) const
114365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
114465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status;
114565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
114665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
114765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
114865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(output);
114965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
115065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getRenderPosition(halFrames, dspFrames);
115165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
115265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
115365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
115465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
115565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
115665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
115765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
115865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
1159021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    bool clientAdded = false;
1160021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1161021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
116265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1163021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        pid_t pid = IPCThreadState::self()->getCallingPid();
1164021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(pid) < 0) {
1165021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            sp<NotificationClient> notificationClient = new NotificationClient(this,
1166021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                client,
1167021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                pid);
1168021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
116965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1170021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mNotificationClients.add(pid, notificationClient);
117165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1172021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            sp<IBinder> binder = client->asBinder();
1173021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            binder->linkToDeath(notificationClient);
1174021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            clientAdded = true;
1175021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
1176021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
117765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1178021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
1179021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    // ThreadBase mutex and teh locknig order is ThreadBase::mLock then AudioFlinger::mClientLock.
1180021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    if (clientAdded) {
118165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // the config change is always sent from playback or record threads to avoid deadlock
118265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // with AudioSystem::gLock
118365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1184896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
118565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
118665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
118765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
1188896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
118965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
119065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
119165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
119265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
119365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid)
119465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
119565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
1196021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1197021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
1198021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        mNotificationClients.removeItem(pid);
1199021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
12003a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
12013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("%d died, releasing its sessions", pid);
12028d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    size_t num = mAudioSessionRefs.size();
12033a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    bool removed = false;
12048d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i< num; ) {
12053a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1206012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        ALOGV(" pid %d @ %d", ref->mPid, i);
1207012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        if (ref->mPid == pid) {
1208012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten            ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
12093a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            mAudioSessionRefs.removeAt(i);
12103a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            delete ref;
12113a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            removed = true;
12123a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            num--;
12138d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        } else {
12148d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten            i++;
12153a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        }
12163a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
12173a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (removed) {
12183a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        purgeStaleEffects_l();
12193a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
122065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
122165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1222021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
122365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1224021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _l(mClientLock);
1225021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    size_t size = mNotificationClients.size();
122665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < size; i++) {
1227021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
12281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                                              ioHandle,
12291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                                              param2);
123065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
123165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
123265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1233021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// removeClient_l() must be called with AudioFlinger::mClientLock held
123465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid)
123565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1236827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("removeClient_l() pid %d, calling pid %d", pid,
123785ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            IPCThreadState::self()->getCallingPid());
123865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mClients.removeItem(pid);
123965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
124065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1241717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held
1242717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1243717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{
1244717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    sp<PlaybackThread> thread;
1245717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1246717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1247717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1248717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            ALOG_ASSERT(thread == 0);
1249717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            thread = mPlaybackThreads.valueAt(i);
1250717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        }
1251717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    }
1252717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1253717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    return thread;
1254717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent}
125565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
125781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
125865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
125965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
126081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   RefBase(),
126281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger(audioFlinger),
126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
126581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPid(pid),
126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTimedTrackCount(0)
126765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
126965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
127065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1271021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// Client destructor must be called with AudioFlinger::mClientLock held
127281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client()
127365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeClient_l(mPid);
127565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
127665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const
127865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mMemoryDealer;
128065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
128165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Reserve one of the limited slots for a timed audio track associated
128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// with this client
128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::Client::reserveTimedTrack()
128565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int kMaxTimedTracksPerClient = 4;
128765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
128965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
129081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
129181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("can not create timed track - pid %d has exceeded the limit",
129281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             mPid);
129381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return false;
129465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
129565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount++;
129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return true;
129865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
129965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Release a slot for a timed audio track
130181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::Client::releaseTimedTrack()
130265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount--;
1305896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent}
1306896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent
130781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
130981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     const sp<IAudioFlingerClient>& client,
131181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     pid_t pid)
131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
1313896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{
131465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
131565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
131681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient()
131765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
131865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
131965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
13200f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
132165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
132281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NotificationClient> keep(this);
132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeNotificationClient(mPid);
132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
132565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
132665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
132781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
132865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1329893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brownstatic bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1330893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown    return audio_is_remote_submix_device(inDevice);
1331893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown}
1332893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
133381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord(
133481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t input,
133581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
133681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
133781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
133874935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
1339eeca32671896739e84050da5992d5f151a1629deGlenn Kasten        IAudioFlinger::track_flags_t *flags,
134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *sessionId,
1342d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& cblk,
1343d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& buffers,
134481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
13451d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{
134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread::RecordTrack> recordTrack;
134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordHandle> recordHandle;
134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Client> client;
134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int lSessionId;
13511d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1352d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk.clear();
1353d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers.clear();
1354d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check calling permissions
135681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!recordingAllowed()) {
1357e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten        ALOGE("openRecord() permission denied: recording not allowed");
135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
135981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
13611d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
136253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createRecordTrack_l()
136353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
136453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid sample rate %u", sampleRate);
136553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
136653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
136753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
136853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
13690e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten    // we don't yet support anything other than 16-bit PCM
13700e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten    if (!(audio_is_valid_format(format) &&
13710e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten            audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
1372cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("openRecord() invalid format %#x", format);
1373291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        lStatus = BAD_VALUE;
137453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
137553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
137653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
137753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createRecordTrack_l()
137853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_input_channel(channelMask)) {
137953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid channel mask %#x", channelMask);
138053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
1381291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        goto Exit;
1382291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
1383291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten
138405997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten    {
138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
1386e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        RecordThread *thread = checkRecordThread_l(input);
138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
1388e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() checkRecordThread_l failed");
138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
13911d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent        }
13921d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1393893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown        if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1394893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown                && !captureAudioOutputAllowed()) {
1395e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() permission denied: capture not allowed");
1396893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown            lStatus = PERMISSION_DENIED;
1397893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown            goto Exit;
1398893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown        }
1399893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
14008d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
1401021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
1402feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1403aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
140481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = *sessionId;
1405feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        } else {
1406570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            // if no audio session id is provided, create one here
140781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = nextUniqueId();
140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId != NULL) {
140981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                *sessionId = lSessionId;
141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1411feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        }
1412570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten        ALOGV("openRecord() lSessionId: %d", lSessionId);
1413570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten
1414462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        // TODO: the uid should be passed in as a parameter to openRecord
141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1416462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  frameCount, lSessionId,
1417462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  IPCThreadState::self()->getCallingUid(),
1418462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  flags, tid, &lStatus);
141903e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
1420feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1421e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove local strong reference to Client before deleting the RecordTrack so that the
1424021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
1425021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        client.clear();
142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack.clear();
142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
1429feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1430feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1431d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk = recordTrack->getCblk();
1432d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers = recordTrack->getBuffers();
1433d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
14342fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten    // return handle to client
143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordHandle = new RecordHandle(recordTrack);
1436feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
143781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
14389156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return recordHandle;
1440feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent}
1441feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1442feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
14431d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
144481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name)
144759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!settingsAllowed()) {
144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
145159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return loadHwModule_l(name);
145359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
145459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
145581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held
145681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
145759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
145881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("loadHwModule() module %s already loaded", name);
146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mAudioHwDevs.keyAt(i);
146259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
146359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
146459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
146581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *dev;
146659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
146781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int rc = load_audio_interface(name, &dev);
146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
146981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() error %d loading module %s ", rc, name);
147081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
147159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
147259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
147381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_INIT;
147481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    rc = dev->init_check(dev);
147581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
147959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
148059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Check and cache this HAL's level of support for master mute and master
148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volume.  If this is the first HAL opened, and it supports the get
148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // methods, use the initial values provided by the HAL as the current
148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // master mute and volume settings.
148559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
148781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {  // scope for auto-lock pattern
148881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mHardwareLock);
148959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
149081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (0 == mAudioHwDevs.size()) {
149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_volume) {
149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float mv;
149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_volume(dev, &mv)) {
149581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterVolume = mv;
149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
149881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
149981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
150081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_mute) {
150181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool mm;
150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_mute(dev, &mm)) {
150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterMute = mm;
150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
150659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_volume) &&
151081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_volume(dev, mMasterVolume))) {
151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
151359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
151459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
151581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
151681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_mute) &&
151781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_mute(dev, mMasterMute))) {
151881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
151981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
152081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
152159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
152281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_IDLE;
152359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
152459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
152581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_module_handle_t handle = nextUniqueId();
152681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
152781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
152881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
152981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent          name, dev->common.module->name, dev->common.module->id, handle);
153081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
153281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
153459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate()
153859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
153959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
154181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->sampleRate() : 0;
1542a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent}
154359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
154481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount()
1545a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{
154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->frameCountHAL() : 0;
154959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
155059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
155165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
155265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15534182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
15544182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{
15554182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    uid_t uid = IPCThreadState::self()->getCallingUid();
15564182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (uid != AID_SYSTEM) {
15574182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return PERMISSION_DENIED;
15584182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
15594182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    Mutex::Autolock _l(mLock);
15604182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (mIsDeviceTypeKnown) {
15614182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return INVALID_OPERATION;
15624182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
15634182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsLowRamDevice = isLowRamDevice;
15644182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsDeviceTypeKnown = true;
15654182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    return NO_ERROR;
15664182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten}
15674182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
15684182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ----------------------------------------------------------------------------
15694182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
157081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
157181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_devices_t *pDevices,
157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           uint32_t *pSamplingRate,
157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_format_t *pFormat,
157481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_channel_mask_t *pChannelMask,
157581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           uint32_t *pLatencyMs,
1576ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                           audio_output_flags_t flags,
1577ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                           const audio_offload_info_t *offloadInfo)
157865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1579ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1580fb872cc6f77f6d74011fab703b3edd7023c736cbGlenn Kasten    memset(&config, 0, sizeof(config));
1581ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1582ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1583ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1584937098b9b564a779ff1c8c2d9e60769ee5c69810Glenn Kasten    if (offloadInfo != NULL) {
1585ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald        config.offload_info = *offloadInfo;
1586ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    }
1587ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1588bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              module,
159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              (pDevices != NULL) ? *pDevices : 0,
159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.sample_rate,
159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.format,
159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.channel_mask,
159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              flags);
1595bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1596e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten          offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
159845faf7e02791993a487d6e038d16ff46395f1975Glenn Kasten    if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
1599142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        return AUDIO_IO_HANDLE_NONE;
1600ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
1601ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman
160281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
160365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16043255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
16056e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (outHwDev == NULL) {
1606142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        return AUDIO_IO_HANDLE_NONE;
16076e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
160865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
161165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
161365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16143255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_out_t *outStream = NULL;
161534542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten    status_t status = hwDevHal->open_output_stream(hwDevHal,
161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          id,
161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          *pDevices,
161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          (audio_output_flags_t)flags,
161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          &config,
162081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          &outStream);
162165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
1623bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
162481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "Channels %x, status %d",
162581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            outStream,
162681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.sample_rate,
162781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.format,
162881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.channel_mask,
162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status);
163058912562617941964939a4182cda71eaeb153d4bGlenn Kasten
163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && outStream != NULL) {
1632bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
163365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16343255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        PlaybackThread *thread;
1635bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1636bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            thread = new OffloadThread(this, output, id, *pDevices);
1637bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1638bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
164081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = new DirectOutputThread(this, output, id, *pDevices);
164281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
164381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = new MixerThread(this, output, id, *pDevices);
164581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
164665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
164781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.add(id, thread);
164888cbea8a918bbaf5e06e48aadd5af5e81d58d232Glenn Kasten
16497c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pSamplingRate != NULL) {
16507c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pSamplingRate = config.sample_rate;
16517c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
16527c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pFormat != NULL) {
16537c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pFormat = config.format;
16547c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
16557c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pChannelMask != NULL) {
16567c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pChannelMask = config.channel_mask;
16577c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
16587c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pLatencyMs != NULL) {
16597c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pLatencyMs = thread->latency();
16607c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
166165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
166281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new output creation
1663021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
166465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
166581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the first primary output opened designates the primary hw device
166681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
166781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGI("Using module %d has the primary audio interface", module);
166881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mPrimaryHardwareDev = outHwDev;
166981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
167081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AutoMutex lock(mHardwareLock);
167181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_SET_MODE;
167281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            hwDevHal->set_mode(hwDevHal, mMode);
167381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
167481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
167581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return id;
167681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
167765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1678142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    return AUDIO_IO_HANDLE_NONE;
167965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
168065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
168181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
168281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t output2)
168365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
168481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
168581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread1 = checkMixerThread_l(output1);
168681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread2 = checkMixerThread_l(output2);
168781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
168881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread1 == NULL || thread2 == NULL) {
168981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
169081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                output2);
1691142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        return AUDIO_IO_HANDLE_NONE;
169265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
169365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
169581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->addOutputTrack(thread2);
169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPlaybackThreads.add(id, thread);
169881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // notify client processes of the new output creation
1699021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
170081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return id;
170165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
170265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
170381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output)
17042bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{
170581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeOutput_nonvirtual(output);
17062bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi}
17072bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi
170881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
170965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
171081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the playback thread so that
171181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
171281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<PlaybackThread> thread;
171381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
171481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
171581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkPlaybackThread_l(output);
171681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
171781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
171865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
171965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
172081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeOutput() %d", output);
1721de070137f11d346fba77605bd76a44c040a618fcEric Laurent
172281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread->type() == ThreadBase::MIXER) {
172381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
172481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
172581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    DuplicatingThread *dupThread =
172681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
172781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    dupThread->removeOutputTrack((MixerThread *)thread.get());
1728bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1729de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
1730de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
1731de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
1732bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1733bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
173481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.removeItem(output);
1735bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // save all effects to the default thread
1736bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mPlaybackThreads.size()) {
1737bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1738bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (dstThread != NULL) {
1739bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // audioflinger lock is held here so the acquisition order of thread locks does not
1740bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // matter
1741bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _dl(dstThread->mLock);
1742bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _sl(thread->mLock);
1743bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1744bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
1745bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
1746bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
1747bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1748bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1749021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
17503acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
175181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
175381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
17543acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
175581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread->type() != ThreadBase::DUPLICATING) {
175681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *out = thread->clearOutput();
175781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // from now on thread->mOutput is NULL
175981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        out->hwDev()->close_output_stream(out->hwDev(), out->stream);
176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        delete out;
176165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
176281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
176365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
176465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output)
1766e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{
176781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
176881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
176981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
177181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1772e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent    }
1773e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("suspendOutput() %d", output);
177581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->suspend();
1776e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
177781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
177865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
177965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
178081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output)
178165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
17826637baae4244aec731c4014da72418d330636ae1Glenn Kasten    Mutex::Autolock _l(mLock);
178381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
178465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
178581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
178681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1787ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
178865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
178981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("restoreOutput() %d", output);
179065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
179181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->restore();
179265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
179381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
179465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
179565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
179681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_devices_t *pDevices,
179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          uint32_t *pSamplingRate,
179981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_format_t *pFormat,
180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_channel_mask_t *pChannelMask)
180165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1802ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1803fb872cc6f77f6d74011fab703b3edd7023c736cbGlenn Kasten    memset(&config, 0, sizeof(config));
1804ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1805ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1806ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1807ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t reqSamplingRate = config.sample_rate;
180981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_format_t reqFormat = config.format;
1810f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten    audio_channel_mask_t reqChannelMask = config.channel_mask;
181165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
181245faf7e02791993a487d6e038d16ff46395f1975Glenn Kasten    if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
1814b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent    }
1815b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent
181681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
181765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
18183255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
18196e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (inHwDev == NULL) {
182081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
18216e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1822fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *inHwHal = inHwDev->hwDevice();
182481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
1825b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
18263255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_in_t *inStream = NULL;
18273255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
182881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        &inStream);
1829cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten    ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, "
183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "status %d",
183181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            inStream,
183281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.sample_rate,
183381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.format,
183481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.channel_mask,
183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status);
183665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
183781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the input could not be opened with the requested parameters and we can handle the
183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
183981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs.
184081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == BAD_VALUE &&
184181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
184281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        (config.sample_rate <= 2 * reqSamplingRate) &&
1843e541269be94f3a1072932d51537905b120ef4733Andy Hung        (audio_channel_count_from_in_mask(config.channel_mask) <= FCC_2) &&
1844e541269be94f3a1072932d51537905b120ef4733Andy Hung        (audio_channel_count_from_in_mask(reqChannelMask) <= FCC_2)) {
18458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME describe the change proposed by HAL (save old values so we can log them here)
184681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("openInput() reopening with proposed sampling rate and channel mask");
184781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inStream = NULL;
184881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
18498594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME log this new status; HAL should not propose any further changes
185065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
185165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && inStream != NULL) {
185358912562617941964939a4182cda71eaeb153d4bGlenn Kasten
185446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
185581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
185681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // or (re-)create if current Pipe is idle and does not match the new format
185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<NBAIO_Sink> teeSink;
185881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        enum {
185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NO,    // don't copy input
186081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NEW,   // copy input using a new pipe
186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_OLD,   // copy input using an existing pipe
186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } kind;
186381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1864e541269be94f3a1072932d51537905b120ef4733Andy Hung                audio_channel_count_from_in_mask(
1865e541269be94f3a1072932d51537905b120ef4733Andy Hung                        inStream->common.get_channels(&inStream->common)));
1866da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (!mTeeSinkInputEnabled) {
1867da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            kind = TEE_SINK_NO;
18686e0d67d7b496ce17c0970a4ffd3a6f808860949cGlenn Kasten        } else if (!Format_isValid(format)) {
186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink == 0) {
187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink->getStrongCount() != 1) {
187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
1874f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten        } else if (Format_isEqual(format, mRecordTeeSink->format())) {
187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_OLD;
18764adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten        } else {
187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        switch (kind) {
188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NEW: {
1881da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numCounterOffers = 0;
188381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const NBAIO_Format offers[1] = {format};
188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            PipeReader *pipeReader = new PipeReader(*pipe);
188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            numCounterOffers = 0;
188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSink = pipe;
189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSource = pipeReader;
189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = pipe;
18934adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten            }
189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
189581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_OLD:
189681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = mRecordTeeSink;
189781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
189881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NO:
189981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        default:
190081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
190158912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
190246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
1903da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
190481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
190565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
190681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Start record thread
190734af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten        // RecordThread requires both input and output device indication to forward to audio
190881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // pre processing modules
19093255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        RecordThread *thread = new RecordThread(this,
191081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                  input,
191181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                  id,
1912d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                  primaryOutputDevice_l(),
191346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  *pDevices
191446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
191546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  , teeSink
191646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
191746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  );
191881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.add(id, thread);
191981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
19207c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pSamplingRate != NULL) {
19217c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pSamplingRate = reqSamplingRate;
19227c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
19237c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pFormat != NULL) {
19247c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pFormat = config.format;
19257c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
19267c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pChannelMask != NULL) {
1927f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten            *pChannelMask = reqChannelMask;
19287c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
192965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
193081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new input creation
1931021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return id;
19331afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent    }
193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
193665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
193765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
193881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input)
193965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeInput_nonvirtual(input);
194165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
194265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1944de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
194581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the record thread so that
194681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread> thread;
194881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
1953de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
195481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeInput() %d", input);
1956021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.removeItem(input);
1958de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
1962de070137f11d346fba77605bd76a44c040a618fcEric Laurent
196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *in = thread->clearInput();
196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // from now on thread->mInput is NULL
196681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    in->hwDev()->close_input_stream(in->hwDev(), in->stream);
196781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete in;
196865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
1970b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
1971b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
1972d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kastenstatus_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
1973b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{
1974b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    Mutex::Autolock _l(mLock);
1975d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten    ALOGV("invalidateStream() stream %d", stream);
1976b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
197881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
197981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->invalidateTracks(stream);
1980b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    }
198181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
198281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
1983b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
1984b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
198581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentint AudioFlinger::newAudioSessionId()
1987162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{
198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return nextUniqueId();
1989162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent}
1990162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent
1991d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
1992a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{
1993a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    Mutex::Autolock _l(mLock);
199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
1995d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
1996d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
1997d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
1998d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
1999d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
2000021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
2001021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
2002021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Ignore requests received from processes not known as notification client. The request
2003021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2004021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // called from a different pid leaving a stale session reference.  Also we don't know how
2005021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // to clear this reference if the client process dies.
2006021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(caller) < 0) {
2007021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2008021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            return;
2009021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
2010d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
2011d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
201281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
201381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
201481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
201581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt++;
201781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" incremented refcount to %d", ref->mCnt);
201881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
2019a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
2020a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    }
202181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV(" added new entry for %d", audioSession);
2023a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent}
2024a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
2025d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
202644a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{
202781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
202881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2029d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2030d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2031d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2032d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
203381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
203581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
203681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
203781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt--;
203881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" decremented refcount to %d", ref->mCnt);
203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mCnt == 0) {
204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioSessionRefs.removeAt(i);
204181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                delete ref;
204281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                purgeStaleEffects_l();
204344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent            }
204481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
204544a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent        }
204644a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent    }
2047d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // If the caller is mediaserver it is likely that the session being released was acquired
2048d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // on behalf of a process not in notification clients and we ignore the warning.
2049d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
205044a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent}
205144a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent
205281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() {
205365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
205481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("purging stale effects");
2055fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten
205681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > chains;
205758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
205881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
205981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
206081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
206181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
206281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
206381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chains.push(ec);
206481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2065c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
206658912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
206781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
206881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread> t = mRecordThreads.valueAt(i);
206981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
207081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
207181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chains.push(ec);
207281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2073300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten    }
207465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
207581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < chains.size(); i++) {
207681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> ec = chains[i];
207781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionid = ec->sessionId();
207881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> t = ec->mThread.promote();
207981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
208181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
208281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numsessionrefs = mAudioSessionRefs.size();
208381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool found = false;
208481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t k = 0; k < numsessionrefs; k++) {
208581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
208681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mSessionid == sessionid) {
208781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV(" session %d still exists for %d with %d refs",
208881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sessionid, ref->mPid, ref->mCnt);
208981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                found = true;
209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
209158912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
209258912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!found) {
2094e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            Mutex::Autolock _l(t->mLock);
209581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // remove all effects from the chain
209681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            while (ec->mEffects.size()) {
209781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectModule> effect = ec->mEffects[0];
209881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->unPin();
209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                t->removeEffect_l(effect);
210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (effect->purgeHandles()) {
210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
210281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
210381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioSystem::unregisterEffect(effect->id());
210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2105c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
210658912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
210781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return;
210865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
210965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
211181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
2112190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{
211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mPlaybackThreads.valueFor(output).get();
2114190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten}
2115190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held
211781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
211881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
212081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2122190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held
212481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
212681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mRecordThreads.valueFor(input).get();
212781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2128190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
212981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::nextUniqueId()
213081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
2131bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten    return (uint32_t) android_atomic_inc(&mNextUniqueId);
213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
213383efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten
213481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
213537d825e72a6c606553a745da1212590a425996d3Glenn Kasten{
213681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
213781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
213881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *output = thread->getOutput();
213981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
214081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return thread;
214137d825e72a6c606553a745da1212590a425996d3Glenn Kasten        }
214237d825e72a6c606553a745da1212590a425996d3Glenn Kasten    }
214381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NULL;
214437d825e72a6c606553a745da1212590a425996d3Glenn Kasten}
214537d825e72a6c606553a745da1212590a425996d3Glenn Kasten
214681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const
214765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
214881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
2149000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
215081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
21529f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten    }
2153000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
215481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread->outDevice();
2155000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2156688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten
215781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
215881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int triggerSession,
215981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int listenerSession,
216081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    sync_event_callback_t callBack,
21618ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent                                    wp<RefBase> cookie)
216281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
216381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
216473ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten
216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t playStatus = NAME_NOT_FOUND;
216781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t recStatus = NAME_NOT_FOUND;
216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
217081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playStatus == NO_ERROR) {
217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
217365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
217481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
217581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (recStatus == NO_ERROR) {
217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
21789f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten        }
217965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
218081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPendingSyncEvents.add(event);
218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createSyncEvent() invalid event %d", event->type());
218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        event.clear();
218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event;
218765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
218865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//  Effect management
219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
219258912562617941964939a4182cda71eaeb153d4bGlenn Kasten
219358912562617941964939a4182cda71eaeb153d4bGlenn Kasten
219481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
2195000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
219781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryNumberEffects(numEffects);
2198000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2199000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
220158912562617941964939a4182cda71eaeb153d4bGlenn Kasten{
220281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
220381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryEffect(index, descriptor);
220458912562617941964939a4182cda71eaeb153d4bGlenn Kasten}
220558912562617941964939a4182cda71eaeb153d4bGlenn Kasten
220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *descriptor) const
2208000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
221081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectGetDescriptor(pUuid, descriptor);
2211000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2212000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
221381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22148d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect(
221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *pDesc,
221681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t io,
221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status,
222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *id,
222281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled)
2223000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus = NO_ERROR;
222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc;
2227000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
22288d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid, effectClient.get(), priority, sessionId, io);
2231000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDesc == NULL) {
223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2235952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2236000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
223781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check audio settings permission for global effects
223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2241952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2242000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that can only be created by audio policy manager (running in same process)
224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2248952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
224965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!EffectIsNullUuid(&pDesc->uuid)) {
225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is specified, request effect descriptor
225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
225481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is not specified, look for an available implementation
226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // of the required type in effect factory
226181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (EffectIsNullUuid(&pDesc->type)) {
226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() no effect type");
226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2265288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t numEffects = 0;
226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect_descriptor_t d;
226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            d.flags = 0; // prevent compiler warning
226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool found = false;
2270288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectQueryNumberEffects(&numEffects);
227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (uint32_t i = 0; i < numEffects; i++) {
227781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = EffectQueryEffect(i, &desc);
227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (lStatus < 0) {
227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
2281d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If matching type found save effect descriptor. If the session is
228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // 0 and the effect is not auxiliary, continue enumeration in case
228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // an auxiliary version of this effect type is available
228681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    found = true;
228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    d = desc;
228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
228981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2290d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                        break;
2291d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                    }
2292d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
2293288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (!found) {
229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() effect not found");
229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For same effect type, chose auxiliary version over insert version if
230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // connect to output mix (Compliance to OpenSL ES)
230181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc = d;
230458912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
230558912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
230658912562617941964939a4182cda71eaeb153d4bGlenn Kasten
230781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Do not allow auxiliary effects on a session different from 0 (output mix)
230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = INVALID_OPERATION;
231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
23123dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent        }
231365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check recording permission for visualizer
231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            !recordingAllowed()) {
231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = PERMISSION_DENIED;
231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
232065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return effect descriptor
232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *pDesc = desc;
2323142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2324eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // if the output returned by getOutputForEffect() is removed before we lock the
2325eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2326eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // and we will exit safely
2327eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            io = AudioSystem::getOutputForEffect(&desc);
2328eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            ALOGV("createEffect got output %d", io);
2329eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        }
2330eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent
2331eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        Mutex::Autolock _l(mLock);
233265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is not specified try to find a matching audio session ID in one of the
233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // output threads.
233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // because of code checking output when entering the function.
233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Note: io is never 0 when creating an effect on an input
2338142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE) {
23395baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
23405baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // output must be specified by AudioPolicyManager when using session
23415baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // AUDIO_SESSION_OUTPUT_STAGE
23425baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                lStatus = BAD_VALUE;
23435baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                goto Exit;
23445baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
2345eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // look for the thread where the specified audio session is present
2346eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2347eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2348eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    io = mPlaybackThreads.keyAt(i);
2349eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    break;
2350eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                }
2351d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0) {
2353eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
2354eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2355eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                        io = mRecordThreads.keyAt(i);
235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
23572986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
235865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
235965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If no output thread contains the requested session ID, default to
236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // first output. The effect chain will be moved to the correct output
236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // thread when a track with the same session ID is created
2363142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten            if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                io = mPlaybackThreads.keyAt(0);
236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect() got io %d for effect %s", io, desc.name);
236765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ThreadBase *thread = checkRecordThread_l(io);
236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = checkPlaybackThread_l(io);
237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (thread == NULL) {
237281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGE("createEffect() unknown output thread");
237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2375288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
2376288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten        }
2377288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
2378021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        sp<Client> client = registerPid(pid);
237958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect on selected output thread
238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = thread->createEffect_l(client, effectClient, priority, sessionId,
238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                &desc, enabled, &lStatus);
238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (handle != 0 && id != NULL) {
238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *id = handle->id();
238565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
238665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
238765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
238881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
23899156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
239166fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten}
239266fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten
239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t dstOutput)
239565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcOutput, dstOutput);
239865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcOutput == dstOutput) {
240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
240281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
240381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
240481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcThread == NULL) {
240581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad srcOutput %d", srcOutput);
240681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
240781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
240981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (dstThread == NULL) {
241081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad dstOutput %d", dstOutput);
241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
241265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
241365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
241481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _dl(dstThread->mLock);
241581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _sl(srcThread->mLock);
24165baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return moveEffectChain_l(sessionId, srcThread, dstThread, false);
241765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
241865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
242081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffectChain_l(int sessionId,
242181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *srcThread,
242281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *dstThread,
242381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   bool reRegister)
242465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
242581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
242681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcThread, dstThread);
242765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
242881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
242981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
243081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
243181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sessionId, srcThread);
243281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
243381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
24349ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
243581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
243681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // so that a new chain is created with correct parameters when first effect is added. This is
243781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
243881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // removed.
243981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    srcThread->removeEffectChain_l(chain);
24409ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
244181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // transfer all effects one by one so that new effect chain is created on new thread with
244281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
244381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> dstChain;
244481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy = 0; // prevent compiler warning
244581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect = chain->getEffectFromId_l(0);
24465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    Vector< sp<EffectModule> > removed;
24475baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    status_t status = NO_ERROR;
244881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (effect != 0) {
244981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        srcThread->removeEffect_l(effect);
24505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        removed.add(effect);
24515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        status = dstThread->addEffect_l(effect);
24525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (status != NO_ERROR) {
24535baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
24545baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
245581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // removeEffect_l() has stopped the effect if it was active so it must be restarted
245681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect->state() == EffectModule::ACTIVE ||
245781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->state() == EffectModule::STOPPING) {
245881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->start();
245965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
246081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the move request is not received from audio policy manager, the effect must be
246181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // re-registered with the new strategy and output
246281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (dstChain == 0) {
246381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            dstChain = effect->chain().promote();
246481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (dstChain == 0) {
246581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
24665baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                status = NO_INIT;
24675baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                break;
246865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
246981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            strategy = dstChain->strategy();
247065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
247181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (reRegister) {
247281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
247381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::registerEffect(&effect->desc(),
24745baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                        dstThread->id(),
247581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        strategy,
247681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        sessionId,
247781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        effect->id());
2478d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent            AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
247981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
248081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect = chain->getEffectFromId_l(0);
248158912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
248258912562617941964939a4182cda71eaeb153d4bGlenn Kasten
24835baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (status != NO_ERROR) {
24845baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        for (size_t i = 0; i < removed.size(); i++) {
24855baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            srcThread->addEffect_l(removed[i]);
24865baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (dstChain != 0 && reRegister) {
24875baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::unregisterEffect(removed[i]->id());
24885baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::registerEffect(&removed[i]->desc(),
24895baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            srcThread->id(),
24905baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            strategy,
24915baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            sessionId,
24925baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            removed[i]->id());
2493d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent                AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
24945baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
24955baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
24965baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    }
24975baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
24985baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return status;
249965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
250065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
25015baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
2502813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2503813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    if (mGlobalEffectEnableTime != 0 &&
2504813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2505813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        return true;
2506813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2507813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2508813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2509813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<EffectChain> ec =
2510813e2a74853bde19e37d878c596a044b3f299efcEric Laurent                mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
25115baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (ec != 0 && ec->isNonOffloadableEnabled()) {
2512813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            return true;
2513813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2514813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2515813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    return false;
2516813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2517813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
25185baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable()
2519813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2520813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    Mutex::Autolock _l(mLock);
2521813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2522813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    mGlobalEffectEnableTime = systemTime();
2523813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2524813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2525813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2526813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        if (t->mType == ThreadBase::OFFLOAD) {
2527813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            t->invalidateTracks(AUDIO_STREAM_MUSIC);
2528813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2529813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2530813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2531813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2532813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2533da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry {
2534da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_NAME 32     // %Y%m%d%H%M%S_%d.wav
2535da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    char mName[MAX_NAME];
2536da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten};
2537da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2538da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2)
2539da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{
2540da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2541da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}
2542da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
254346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2544d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
254565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
2546d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten    NBAIO_Source *teeSource = source.get();
2547fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    if (teeSource != NULL) {
2548da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // .wav rotation
2549da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // There is a benign race condition if 2 threads call this simultaneously.
2550da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // They would both traverse the directory, but the result would simply be
2551da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // failures at unlink() which are ignored.  It's also unlikely since
2552da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // normally dumpsys is only done by bugreport or from the command line.
2553da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        char teePath[32+256];
2554da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strcpy(teePath, "/data/misc/media");
2555da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        size_t teePathLen = strlen(teePath);
2556da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        DIR *dir = opendir(teePath);
2557da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teePath[teePathLen++] = '/';
2558da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (dir != NULL) {
2559da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_SORT 20 // number of entries to sort
2560da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_KEEP 10 // number of entries to keep
2561da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            struct Entry entries[MAX_SORT];
2562da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            size_t entryCount = 0;
2563da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            while (entryCount < MAX_SORT) {
2564da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent de;
2565da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent *result = NULL;
2566da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                int rc = readdir_r(dir, &de, &result);
2567da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (rc != 0) {
2568da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r failed %d", rc);
2569da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2570da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2571da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result == NULL) {
2572da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2573da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2574da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result != &de) {
2575da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2576da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2577da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2578da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                // ignore non .wav file entries
2579da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                size_t nameLen = strlen(de.d_name);
2580da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (nameLen <= 4 || nameLen >= MAX_NAME ||
2581da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                        strcmp(&de.d_name[nameLen - 4], ".wav")) {
2582da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    continue;
2583da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2584da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                strcpy(entries[entryCount++].mName, de.d_name);
2585da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2586da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            (void) closedir(dir);
2587da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (entryCount > MAX_KEEP) {
2588da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                qsort(entries, entryCount, sizeof(Entry), comparEntry);
2589da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2590da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    strcpy(&teePath[teePathLen], entries[i].mName);
2591da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    (void) unlink(teePath);
2592da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2593da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2594da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        } else {
2595da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
25968b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2597da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2598da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
2599d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        char teeTime[16];
2600fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct timeval tv;
2601fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        gettimeofday(&tv, NULL);
2602fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct tm tm;
2603fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        localtime_r(&tv.tv_sec, &tm);
2604da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2605da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2606da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2607da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
2608fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        if (teeFd >= 0) {
2609fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            char wavHeader[44];
2610fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            memcpy(wavHeader,
2611fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn 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",
2612fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                sizeof(wavHeader));
2613fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            NBAIO_Format format = teeSource->format();
2614fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            unsigned channelCount = Format_channelCount(format);
2615fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            ALOG_ASSERT(channelCount <= FCC_2);
26163b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten            uint32_t sampleRate = Format_sampleRate(format);
2617fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[22] = channelCount;       // number of channels
2618fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[24] = sampleRate;         // sample rate
2619fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[25] = sampleRate >> 8;
2620fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[32] = channelCount * 2;   // block alignment
2621fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, wavHeader, sizeof(wavHeader));
2622fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            size_t total = 0;
2623fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            bool firstRead = true;
2624fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            for (;;) {
2625fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten#define TEE_SINK_READ 1024
2626fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                short buffer[TEE_SINK_READ * FCC_2];
2627fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                size_t count = TEE_SINK_READ;
26282c3b2da3049627264b7c6b449a1622f002210f03John Grossman                ssize_t actual = teeSource->read(buffer, count,
26292c3b2da3049627264b7c6b449a1622f002210f03John Grossman                        AudioBufferProvider::kInvalidPTS);
2630fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                bool wasFirstRead = firstRead;
2631fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                firstRead = false;
2632fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                if (actual <= 0) {
2633fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2634fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                        continue;
2635fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    }
2636fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    break;
2637fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                }
2638a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent                ALOG_ASSERT(actual <= (ssize_t)count);
2639fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                write(teeFd, buffer, actual * channelCount * sizeof(short));
2640fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                total += actual;
2641fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            }
2642fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 4, SEEK_SET);
2643fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2644fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2645fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 40, SEEK_SET);
2646fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            temp =  total * channelCount * sizeof(short);
2647fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2648fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            close(teeFd);
2649da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
26508b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "tee copied to %s\n", teePath);
2651da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2652fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        } else {
2653da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
26548b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2655da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2656fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        }
2657fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    }
2658d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten}
265946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2660d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
266165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
266265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
266365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact(
266465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
266565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
266665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioFlinger::onTransact(code, data, reply, flags);
266765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
266865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
266965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
2670