AudioFlinger.cpp revision b220884bf3129253cc5bc8d030bc475411ea4911
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";
8565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8658912562617941964939a4182cda71eaeb153d4bGlenn Kasten
874ff14bae91075eb274eb1c2975982358946e7e63John Grossmannsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
887cafbb32999049873d4746ba83bd20c88abe6ce6Eric Laurent
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::mScreenState;
903ed292031dc50c56110cdadb1e3778117e3be76aGlenn Kasten
9146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
92da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkInputEnabled = false;
93da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkOutputEnabled = false;
94da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkTrackEnabled = false;
95da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
96da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
97da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
98da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
9946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
100da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
101813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
102813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// we define a minimum time during which a global effect is considered enabled.
103813e2a74853bde19e37d878c596a044b3f299efcEric Laurentstatic const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
104813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
10565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
10665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
107b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissenconst char *formatToString(audio_format_t format) {
108b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    switch(format) {
109b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_8_BIT: return "pcm8";
110b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_16_BIT: return "pcm16";
111b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_32_BIT: return "pcm32";
112b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_8_24_BIT: return "pcm8.24";
113b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED: return "pcm24";
114b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_PCM_SUB_FLOAT: return "pcmfloat";
115b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_MP3: return "mp3";
116b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_NB: return "amr-nb";
117b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_WB: return "amr-wb";
118b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AAC: return "aac";
119b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
120b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
121b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_VORBIS: return "vorbis";
122b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    default:
123b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        break;
124b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
125b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    return "unknown";
126b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen}
127b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
128f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
129799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
130f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    const hw_module_t *mod;
131799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc;
132799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
133f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
134f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
135f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
136f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
137799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
138f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
139f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = audio_hw_device_open(mod, dev);
140f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
141f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
142f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
143799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
144f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
145f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
146f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
147f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        rc = BAD_VALUE;
148f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        goto out;
149f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
150799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return 0;
151799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
152799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout:
153799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    *dev = NULL;
154799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return rc;
155799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
156799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
15765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
15865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger()
16065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    : BnAudioFlinger(),
1614ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPrimaryHardwareDev(NULL),
1627d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten      mHardwareStatus(AUDIO_HW_IDLE),
1634ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterVolume(1.0f),
1644ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterMute(false),
1654ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mNextUniqueId(1),
1664ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMode(AUDIO_MODE_INVALID),
1674182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mBtNrecIsOff(false),
1684182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mIsLowRamDevice(true),
169813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mIsDeviceTypeKnown(false),
170813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mGlobalEffectEnableTime(0)
17165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
172949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten    getpid_cached = getpid();
1739e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    char value[PROPERTY_VALUE_MAX];
1749e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
1759e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (doLog) {
1769e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
1779e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
17846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
179da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    (void) property_get("ro.debuggable", value, "0");
180da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int debuggable = atoi(value);
181da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int teeEnabled = 0;
182da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    if (debuggable) {
183da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        (void) property_get("af.tee", value, "0");
184da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teeEnabled = atoi(value);
185da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    }
1866e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 1) {
187da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkInputEnabled = true;
1886e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1896e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 2) {
190da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkOutputEnabled = true;
1916e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1926e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 4) {
193da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkTrackEnabled = true;
1946e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
19546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
1965a61d2f277af3098fc10b2881babca16391362daDima Zavin}
1975a61d2f277af3098fc10b2881babca16391362daDima Zavin
1985a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef()
1995a61d2f277af3098fc10b2881babca16391362daDima Zavin{
200799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc = 0;
201fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
202935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    Mutex::Autolock _l(mLock);
203935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
204799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    /* TODO: move all this work into an Init() function */
2054ff14bae91075eb274eb1c2975982358946e7e63John Grossman    char val_str[PROPERTY_VALUE_MAX] = { 0 };
2064ff14bae91075eb274eb1c2975982358946e7e63John Grossman    if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
2074ff14bae91075eb274eb1c2975982358946e7e63John Grossman        uint32_t int_val;
2084ff14bae91075eb274eb1c2975982358946e7e63John Grossman        if (1 == sscanf(val_str, "%u", &int_val)) {
2094ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = milliseconds(int_val);
2104ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using %u mSec as standby time.", int_val);
2114ff14bae91075eb274eb1c2975982358946e7e63John Grossman        } else {
2124ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
2134ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using default %u mSec as standby time.",
2144ff14bae91075eb274eb1c2975982358946e7e63John Grossman                    (uint32_t)(mStandbyTimeInNsecs / 1000000));
2154ff14bae91075eb274eb1c2975982358946e7e63John Grossman        }
2164ff14bae91075eb274eb1c2975982358946e7e63John Grossman    }
21765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
218a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    mMode = AUDIO_MODE_NORMAL;
21965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
22065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
22165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger()
22265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
22365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mRecordThreads.isEmpty()) {
224c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeInput_nonvirtual() will remove specified entry from mRecordThreads
225d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeInput_nonvirtual(mRecordThreads.keyAt(0));
22665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
22765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mPlaybackThreads.isEmpty()) {
228c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
229d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
23065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
231799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
2322b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2332b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten        // no mHardwareLock needed, as there are no other references to this
234a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
235a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        delete mAudioHwDevs.valueAt(i);
23665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
237481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
238481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Tell media.log service about any old writers that still need to be unregistered
239481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
240481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder != 0) {
241481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
242481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
243481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
244481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mUnregisteredWriters.pop();
245481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mediaLogService->unregisterWriter(iMemory);
246481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
247481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
248481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
24965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
25065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
251a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = {
252a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_PRIMARY,
253a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_A2DP,
254a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_USB,
255a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent};
256a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
257a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
258ee578c0330319f04a48bccbdb26b53fea0388d04John GrossmanAudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
259ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_module_handle_t module,
260ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_devices_t devices)
261799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
262a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // if module is 0, the request comes from an old policy manager and we should load
263a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // well known modules
264a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    if (module == 0) {
265a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
266a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
267a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            loadHwModule_l(audio_interfaces[i]);
268a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
269f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        // then try to find a module supporting the requested device.
270f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
271f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
272f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            audio_hw_device_t *dev = audioHwDevice->hwDevice();
273f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            if ((dev->get_supported_devices != NULL) &&
274f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    (dev->get_supported_devices(dev) & devices) == devices)
275f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                return audioHwDevice;
276f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        }
277a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    } else {
278a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        // check a match for the requested module handle
279ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
280ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (audioHwDevice != NULL) {
281ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            return audioHwDevice;
282a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
283a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    }
284a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
285799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return NULL;
286799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
28765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2880f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
28965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
29065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
29165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
29265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
29365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
29465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("Clients:\n");
29565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < mClients.size(); ++i) {
29677c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        sp<Client> client = mClients.valueAt(i).promote();
29777c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        if (client != 0) {
29877c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            snprintf(buffer, SIZE, "  pid: %d\n", client->pid());
29977c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            result.append(buffer);
30065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
30165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
3023a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
303d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    result.append("Notification Clients:\n");
304d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); ++i) {
305d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        snprintf(buffer, SIZE, "  pid: %d\n", mNotificationClients.keyAt(i));
306d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        result.append(buffer);
307d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
308d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
3093a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    result.append("Global session refs:\n");
310b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    result.append("  session   pid count\n");
3113a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
3123a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *r = mAudioSessionRefs[i];
313b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
3143a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        result.append(buffer);
3153a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
31665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3200f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
325a4454b4765c5905f14186893b0688be375642283Glenn Kasten    hardware_call_state hardwareStatus = mHardwareStatus;
32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3274ff14bae91075eb274eb1c2975982358946e7e63John Grossman    snprintf(buffer, SIZE, "Hardware status: %d\n"
3284ff14bae91075eb274eb1c2975982358946e7e63John Grossman                           "Standby Time mSec: %u\n",
3294ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            hardwareStatus,
3304ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            (uint32_t)(mStandbyTimeInNsecs / 1000000));
33165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
33265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
33365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
33465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3350f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
33665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioFlinger from pid=%d, uid=%d\n",
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex)
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3567dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten        usleep(kDumpLockSleepUs);
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args)
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
36344deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd, args);
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // get state of hardware lock
36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool hardwareLocked = dumpTryLock(mHardwareLock);
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!hardwareLocked) {
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kHardwareLockedString);
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mHardwareLock.unlock();
37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool locked = dumpTryLock(mLock);
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // failed to lock - AudioFlinger is probably deadlocked
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpClients(fd, args);
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd, args);
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump playback threads
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mPlaybackThreads.valueAt(i)->dump(fd, args);
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump record threads
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mRecordThreads.valueAt(i)->dump(fd, args);
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
396799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        // dump all hardware devs
397799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
398a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
399799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            dev->dump(dev, fd);
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
401d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
40246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
403d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        // dump the serially shared record tee sink
404d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        if (mRecordTeeSource != 0) {
405d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten            dumpTee(fd, mRecordTeeSource);
406d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        }
40746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
408d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
409d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        if (locked) {
410d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            mLock.unlock();
411d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
4129e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4139e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // append a copy of media.log here by forwarding fd to it, but don't attempt
4149e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // to lookup the service if it's not running, as it will block for a second
4159e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        if (mLogMemoryDealer != 0) {
4169e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
4179e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (binder != 0) {
4189e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                fdprintf(fd, "\nmedia.log:\n");
4199e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                Vector<String16> args;
4209e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                binder->dump(fd, args);
4219e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
4229e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        }
42365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
42465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
42565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
42665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kastensp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
42898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{
42998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // If pid is already in the mClients wp<> map, then use that entry
43098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // (for which promote() is always != 0), otherwise create a new entry and Client.
43198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    sp<Client> client = mClients.valueFor(pid).promote();
43298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    if (client == 0) {
43398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = new Client(this, pid);
43498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        mClients.add(pid, client);
43598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    }
43698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten
43798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    return client;
43898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten}
43965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4409e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
4419e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
442481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If there is no memory allocated for logs, return a dummy writer that does nothing
4439e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (mLogMemoryDealer == 0) {
4449e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return new NBLog::Writer();
4459e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
4469e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
447481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Similarly if we can't contact the media.log service, also return a dummy writer
448481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder == 0) {
449481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
4509e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
451481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
452481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
453481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If allocation fails, consult the vector of previously unregistered writers
454481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // and garbage-collect one or more them until an allocation succeeds
455481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (shared == 0) {
456481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        Mutex::Autolock _l(mUnregisteredWritersLock);
457481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
458481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            {
459481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Pick the oldest stale writer to garbage-collect
460481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
461481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mUnregisteredWriters.removeAt(0);
462481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mediaLogService->unregisterWriter(iMemory);
463481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Now the media.log remote reference to IMemory is gone.  When our last local
464481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // reference to IMemory also drops to zero at end of this block,
465481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // the IMemory destructor will deallocate the region from mLogMemoryDealer.
466481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
467481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            // Re-attempt the allocation
468481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
469481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            if (shared != 0) {
470481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                goto success;
471481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
472481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
473481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // Even after garbage-collecting all old writers, there is still not enough memory,
474481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // so return a dummy writer
475481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
476481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
477481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kastensuccess:
478481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mediaLogService->registerWriter(shared, size, name);
479481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    return new NBLog::Writer(size, shared);
4809e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
4819e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4829e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
4839e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
484685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    if (writer == 0) {
485685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten        return;
486685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    }
4879e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IMemory> iMemory(writer->getIMemory());
4889e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (iMemory == 0) {
4899e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return;
4909e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
491481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Rather than removing the writer immediately, append it to a queue of old writers to
492481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // be garbage-collected later.  This allows us to continue to view old logs for a while.
493481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Mutex::Autolock _l(mUnregisteredWritersLock);
494481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mUnregisteredWriters.push(writer);
4959e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
4969e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
49765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface
49865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
49965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
50065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack(
501fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
50265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t sampleRate,
50358f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
504254af180475346b6186b49c297f340c9c4817511Glenn Kasten        audio_channel_mask_t channelMask,
50574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
506e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten        IAudioFlinger::track_flags_t *flags,
50765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const sp<IMemory>& sharedBuffer,
50872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output,
5093acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        pid_t tid,
51065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int *sessionId,
511d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten        String8& name,
512462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int clientUid,
51365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status_t *status)
51465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
51565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<PlaybackThread::Track> track;
51665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<TrackHandle> trackHandle;
51765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<Client> client;
51865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t lStatus;
51965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int lSessionId;
52065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
521263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
522263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // but if someone uses binder directly they could bypass that and cause us to crash
523263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
52429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("createTrack() invalid stream type %d", streamType);
52565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        lStatus = BAD_VALUE;
52665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        goto Exit;
52765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
52865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
52960a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
53060a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    // and we don't yet support 8.24 or 32-bit PCM
53160a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
53260a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        ALOGE("createTrack() invalid format %d", format);
53360a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        lStatus = BAD_VALUE;
534663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        goto Exit;
535663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    }
536663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten
537663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
538663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
539663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        lStatus = BAD_VALUE;
54060a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        goto Exit;
54160a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    }
54260a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten
54365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
54465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
54565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
54639e94f8f723d445447fdee0822291e664b631f60Eric Laurent        PlaybackThread *effectThread = NULL;
54765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
548c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten            ALOGE("no playback thread found for output handle %d", output);
54965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lStatus = BAD_VALUE;
55065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            goto Exit;
55165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
55265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5538d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
554462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
55598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = registerPid_l(pid);
55665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
558aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
559f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // check if an effect chain with the same session ID is present on another
560f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // output thread and move it here.
561de070137f11d346fba77605bd76a44c040a618fcEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
56239e94f8f723d445447fdee0822291e664b631f60Eric Laurent                sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
56339e94f8f723d445447fdee0822291e664b631f60Eric Laurent                if (mPlaybackThreads.keyAt(i) != output) {
56439e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    uint32_t sessions = t->hasAudioSession(*sessionId);
56539e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    if (sessions & PlaybackThread::EFFECT_SESSION) {
56639e94f8f723d445447fdee0822291e664b631f60Eric Laurent                        effectThread = t.get();
567f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent                        break;
56839e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    }
569de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
570de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lSessionId = *sessionId;
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
573de070137f11d346fba77605bd76a44c040a618fcEric Laurent            // if no audio session id is provided, create one here
5747c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            lSessionId = nextUniqueId();
57565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (sessionId != NULL) {
57665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                *sessionId = lSessionId;
57765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
57865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
5793856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() lSessionId: %d", lSessionId);
58065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
58165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track = thread->createTrack_l(client, streamType, sampleRate, format,
582462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
58303e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
5842fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten        // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
58539e94f8f723d445447fdee0822291e664b631f60Eric Laurent
58639e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // move effect chain to this output thread if an effect on same session was waiting
58739e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // for a track to be created
58839e94f8f723d445447fdee0822291e664b631f60Eric Laurent        if (lStatus == NO_ERROR && effectThread != NULL) {
5892fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten            // no risk of deadlock because AudioFlinger::mLock is held
59039e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _dl(thread->mLock);
59139e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _sl(effectThread->mLock);
59239e94f8f723d445447fdee0822291e664b631f60Eric Laurent            moveEffectChain_l(lSessionId, effectThread, thread, true);
59339e94f8f723d445447fdee0822291e664b631f60Eric Laurent        }
594a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
595a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        // Look for sync events awaiting for a session to be used.
596a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
597a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
598a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
5992986460984580833161bdaabc7f17da1005a8961Eric Laurent                    if (lStatus == NO_ERROR) {
600d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten                        (void) track->setSyncEvent(mPendingSyncEvents[i]);
6012986460984580833161bdaabc7f17da1005a8961Eric Laurent                    } else {
6022986460984580833161bdaabc7f17da1005a8961Eric Laurent                        mPendingSyncEvents[i]->cancel();
6032986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
604a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    mPendingSyncEvents.removeAt(i);
605a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    i--;
606a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                }
607a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            }
608a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
609e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
611e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
61265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (lStatus == NO_ERROR) {
613d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten        // s for server's pid, n for normal mixer name, f for fast index
614d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten        name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
615d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten                track->fastIndex());
61665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        trackHandle = new TrackHandle(track);
61765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
61865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // remove local strong reference to Client before deleting the Track so that the Client
61965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // destructor is called by the TrackBase destructor with mLock held
62065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        client.clear();
62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track.clear();
62265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
62365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit:
6259156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
62665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return trackHandle;
62765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
62865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
63065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
63165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
63265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
63365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6345ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("sampleRate() unknown thread %d", output);
63565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
63665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
63765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->sampleRate();
63865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenint AudioFlinger::channelCount(audio_io_handle_t output) const
64165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
64265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
64365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
64465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6455ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("channelCount() unknown thread %d", output);
64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
64765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
64865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->channelCount();
64965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
65065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
65172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(audio_io_handle_t output) const
65265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
65365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
65465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
65565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6565ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("format() unknown thread %d", output);
65758f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        return AUDIO_FORMAT_INVALID;
65865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
65965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->format();
66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
66165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
66272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastensize_t AudioFlinger::frameCount(audio_io_handle_t output) const
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
66565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
66665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6675ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("frameCount() unknown thread %d", output);
66865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
66965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
67058912562617941964939a4182cda71eaeb153d4bGlenn Kasten    // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
67158912562617941964939a4182cda71eaeb153d4bGlenn Kasten    //       should examine all callers and fix them to handle smaller counts
67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->frameCount();
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
67572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const
67665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
67765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
67965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
680c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten        ALOGW("latency(): no playback thread found for output handle %d", output);
68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
68365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->latency();
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value)
68765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
688a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
689a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
690a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
691a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
692a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
69365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
69465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
69665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
698a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
699ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterVolume = value;
700a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
701ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master volume in the HALs which support it.
702ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
703ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
704ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
7054ff14bae91075eb274eb1c2975982358946e7e63John Grossman
706ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
707ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterVolume()) {
708ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
709935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent        }
710ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
71165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
71265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
713ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master volume in each playback thread.  Playback threads
714ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master volume support will apply
715ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // master volume during the mix operation.  Threads with HALs which do
716ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // support master volume will simply ignore the setting.
7178d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
718ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterVolume(value);
71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
72165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
723f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode)
72465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
725a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
726a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
727a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
728a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
72965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
73165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
73365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
734930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(mode) >= AUDIO_MODE_CNT) {
7355ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("Illegal value: setMode(%d)", mode);
73665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
73765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
73865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
73965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    { // scope for the lock
74065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex lock(mHardwareLock);
741ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
74265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_SET_MODE;
743ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        ret = dev->set_mode(dev, mode);
74465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_IDLE;
74565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
74665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (NO_ERROR == ret) {
74865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
74965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mMode = mode;
7508d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++)
751e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setMode(mode);
75265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
75365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
75665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state)
75865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
759a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
760a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
761a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
762a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
763a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
76465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
76665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
76765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
76865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
76965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
770ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
77165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
772ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_mic_mute(dev, state);
77365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
77465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
77565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
77665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const
77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
779a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
780a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
781a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return false;
782a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
783a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
784fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    bool state = AUDIO_MODE_INVALID;
7852b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
786ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
78765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
788ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    dev->get_mic_mute(dev, &state);
78965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return state;
79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
79265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted)
79465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
795d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    status_t ret = initCheck();
796d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    if (ret != NO_ERROR) {
797d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        return ret;
798d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
799d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
80165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
80265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
80365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
805ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    Mutex::Autolock _l(mLock);
806ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterMute = muted;
807d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
808ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master mute in the HALs which support it.
809ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
810ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
811ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
812d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
813ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
814ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterMute()) {
815ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
816d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        }
817ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
818d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
819d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
820ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master mute in each playback thread.  Playback threads
821ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master mute support will apply master
822ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute during the mix operation.  Threads with HALs which do support master
823ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute will simply ignore the setting.
8248d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
825ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterMute(muted);
82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
82765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
82865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
82965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
83065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const
83165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8329806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8339806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterVolume_l();
83465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
83565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
83665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const
83765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8389806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8399806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterMute_l();
84065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
84165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8424ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const
8434ff14bae91075eb274eb1c2975982358946e7e63John Grossman{
8444ff14bae91075eb274eb1c2975982358946e7e63John Grossman    return mMasterVolume;
8454ff14bae91075eb274eb1c2975982358946e7e63John Grossman}
8464ff14bae91075eb274eb1c2975982358946e7e63John Grossman
847d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const
848d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{
849ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    return mMasterMute;
850d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman}
851d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
85272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
85372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
85665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
85765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
85865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
85965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
860263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
86129357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamVolume() invalid stream %d", stream);
86265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
86365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
86465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
86665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = NULL;
86765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (output) {
86865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(output);
86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return BAD_VALUE;
87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
87365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
87465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].volume = value;
87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
8778d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
878e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
87965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
88065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
88165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread->setStreamVolume(stream, value);
88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
88365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
88565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
88665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
887fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
88865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
88965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
89065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
89165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
89265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
89365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
894263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
895fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
89629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamMute() invalid stream %d", stream);
89765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
89865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
89965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
900935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    AutoMutex lock(mLock);
90165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].mute = muted;
90265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
903e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
90465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
90665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
90765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
90965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
910263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
91165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0.0f;
91265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
91365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
91465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
91565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    float volume;
91665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (output) {
91765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
91865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
91965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return 0.0f;
92065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
92165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        volume = thread->streamVolume(stream);
92265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
9236637baae4244aec731c4014da72418d330636ae1Glenn Kasten        volume = streamVolume_l(stream);
92465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
92565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
92665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return volume;
92765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
92865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
929fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const
93065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
931263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
93265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return true;
93365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
93465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9356637baae4244aec731c4014da72418d330636ae1Glenn Kasten    AutoMutex lock(mLock);
9366637baae4244aec731c4014da72418d330636ae1Glenn Kasten    return streamMute_l(stream);
93765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
93865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
93972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
94065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
941827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
942827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
94381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
94465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
94565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
94765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
94865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
94965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // ioHandle == 0 means the parameters are global to the audio hardware interface
95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (ioHandle == 0) {
951a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        Mutex::Autolock _l(mLock);
952799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        status_t final_result = NO_ERROR;
9538abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        {
954a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            AutoMutex lock(mHardwareLock);
955a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_SET_PARAMETER;
956a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
957a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
958a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                status_t result = dev->set_parameters(dev, keyValuePairs.string());
959a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                final_result = result ?: final_result;
960a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            }
961a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
9628abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        }
96359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
96459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        AudioParameter param = AudioParameter(keyValuePairs);
96559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        String8 value;
96659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
967bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
968bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            if (mBtNrecIsOff != btNrecIsOff) {
96959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
97059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    sp<RecordThread> thread = mRecordThreads.valueAt(i);
971f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    audio_devices_t device = thread->inDevice();
972510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
973510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // collect all of the thread's session IDs
974510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    KeyedVector<int, bool> ids = thread->sessionIds();
975510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // suspend effects associated with those session IDs
976510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    for (size_t j = 0; j < ids.size(); ++j) {
977510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                        int sessionId = ids.keyAt(j);
97859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_AEC,
97959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
980510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
98159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_NS,
98259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
983510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
98459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    }
98559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                }
986bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent                mBtNrecIsOff = btNrecIsOff;
98759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent            }
98859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        }
98928ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        String8 screenState;
99028ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
99128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            bool isOff = screenState == "off";
99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isOff != (AudioFlinger::mScreenState & 1)) {
99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
99428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            }
99528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        }
996799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        return final_result;
99765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
99865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
99965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // hold a strong ref on thread in case closeOutput() or closeInput() is called
100065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // and the thread is exited once the lock is released
100165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ThreadBase> thread;
100265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
100365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
100465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(ioHandle);
1005d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten        if (thread == 0) {
100665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            thread = checkRecordThread_l(ioHandle);
10077fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten        } else if (thread == primaryPlaybackThread_l()) {
10087c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // indicate output device change to all input threads for pre processing
10097c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            AudioParameter param = AudioParameter(keyValuePairs);
10107c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int value;
101189d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent            if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
101289d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent                    (value != 0)) {
10137c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
10147c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                    mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
10157c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                }
10167c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
101765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
101865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
10197378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (thread != 0) {
10207378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten        return thread->setParameters(keyValuePairs);
102165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
102265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
102365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
102465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
102572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
102665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1027827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1028827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
102965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1030a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
1031a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
103265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (ioHandle == 0) {
1033fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        String8 out_s8;
1034fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1035799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
10368abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            char *s;
10378abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            {
10388abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            AutoMutex lock(mHardwareLock);
10398abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_GET_PARAMETER;
1040a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
10418abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            s = dev->get_parameters(dev, keys.string());
10428abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_IDLE;
10438abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            }
1044ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman            out_s8 += String8(s ? s : "");
1045799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            free(s);
1046799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        }
1047fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return out_s8;
104865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
104965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
105065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
105165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
105265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getParameters(keys);
105365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
105465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
105565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
105665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getParameters(keys);
105765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
105865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return String8("");
105965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
106065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1061dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1062dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask) const
106365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1064a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1065a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1066a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return 0;
1067a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1068a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
10692b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
10702b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
1071ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1072ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    memset(&config, 0, sizeof(config));
1073ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = sampleRate;
1074ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = channelMask;
1075ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = format;
1076ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1077ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1078ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    size_t size = dev->get_input_buffer_size(dev, &config);
10792b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_IDLE;
10802b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    return size;
108165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
108265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
10835f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
108465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
108565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
108665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
108765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
108865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
108965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getInputFramesLost();
109065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
109165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return 0;
109265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
109365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
109465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value)
109565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1096a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1097a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1098a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
1099a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1100a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
110165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
110265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
110365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
110465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
110565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
110665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
1107ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
11088abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten    mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
1109ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_voice_volume(dev, value);
111065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
111165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
111265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
111365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
111465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
111526c77556efc30800466b60b3975bc35a70c8c28bGlenn Kastenstatus_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
111672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output) const
111765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
111865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status;
111965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
112165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(output);
112365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
112465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getRenderPosition(halFrames, dspFrames);
112565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
112665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
112865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
112965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
113065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
113165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
113265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
113365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
113465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1135bb001926447d0b7dc71ca8bb3c9856f3136d8f4cGlenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
113665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (mNotificationClients.indexOfKey(pid) < 0) {
113765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<NotificationClient> notificationClient = new NotificationClient(this,
113865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                                                            client,
113965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                                                            pid);
11403856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
114165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
114265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mNotificationClients.add(pid, notificationClient);
114365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
114465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<IBinder> binder = client->asBinder();
114565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        binder->linkToDeath(notificationClient);
114665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
114765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // the config change is always sent from playback or record threads to avoid deadlock
114865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // with AudioSystem::gLock
114965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1150896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
115165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
115265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
115365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
1154896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
115565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
115665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
115765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
115865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
115965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid)
116065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
116165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
116265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1163a3b09254d44cd8d66ec947abe547538c4cfeaa89Glenn Kasten    mNotificationClients.removeItem(pid);
11643a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
11653856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("%d died, releasing its sessions", pid);
11668d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    size_t num = mAudioSessionRefs.size();
11673a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    bool removed = false;
11688d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i< num; ) {
11693a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1170012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        ALOGV(" pid %d @ %d", ref->mPid, i);
1171012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        if (ref->mPid == pid) {
1172012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten            ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
11733a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            mAudioSessionRefs.removeAt(i);
11743a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            delete ref;
11753a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            removed = true;
11763a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            num--;
11778d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        } else {
11788d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten            i++;
11793a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        }
11803a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
11813a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (removed) {
11823a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        purgeStaleEffects_l();
11833a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
118465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
118565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
118665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// audioConfigChanged_l() must be called with AudioFlinger::mLock held
1187b81cc8c6f3eec9edb255ea99b6a6f243585b1e38Glenn Kastenvoid AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
118865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
118965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    size_t size = mNotificationClients.size();
119065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < size; i++) {
119184afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
119284afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten                                                                               param2);
119365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
119465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
119565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
119665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// removeClient_l() must be called with AudioFlinger::mLock held
119765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid)
119865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1199827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("removeClient_l() pid %d, calling pid %d", pid,
120085ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            IPCThreadState::self()->getCallingPid());
120165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mClients.removeItem(pid);
120265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
120365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1204717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held
1205717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1206717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{
1207717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    sp<PlaybackThread> thread;
1208717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1209717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1210717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1211717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            ALOG_ASSERT(thread == 0);
1212717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            thread = mPlaybackThreads.valueAt(i);
1213717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        }
1214717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    }
1215717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1216717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    return thread;
1217717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent}
121865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
121981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
122081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
122165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
122265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
122381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
122481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   RefBase(),
122581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger(audioFlinger),
122681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
122781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
122881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPid(pid),
122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTimedTrackCount(0)
123065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
123265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
123365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
123481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Client destructor must be called with AudioFlinger::mLock held
123581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client()
123665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
123781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeClient_l(mPid);
123865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
123965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
124081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const
124165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
124281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mMemoryDealer;
124365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
124465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
124581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Reserve one of the limited slots for a timed audio track associated
124681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// with this client
124781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::Client::reserveTimedTrack()
124865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
124981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int kMaxTimedTracksPerClient = 4;
125065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
125265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
125481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("can not create timed track - pid %d has exceeded the limit",
125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             mPid);
125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return false;
125765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
125865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount++;
126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return true;
126165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
126265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Release a slot for a timed audio track
126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::Client::releaseTimedTrack()
126565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount--;
1268896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent}
1269896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent
127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
127281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     const sp<IAudioFlingerClient>& client,
127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     pid_t pid)
127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
1276896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{
127765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
127865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
127981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient()
128065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
128165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
128265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
12830f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
128465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
128581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NotificationClient> keep(this);
128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeNotificationClient(mPid);
128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
128865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
128965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
129081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
129165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1292893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brownstatic bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1293893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown    return audio_is_remote_submix_device(inDevice);
1294893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown}
1295893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord(
129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t input,
129881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
129981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
130174935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
1302eeca32671896739e84050da5992d5f151a1629deGlenn Kasten        IAudioFlinger::track_flags_t *flags,
130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *sessionId,
130581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
13061d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{
130781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread::RecordTrack> recordTrack;
130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordHandle> recordHandle;
130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Client> client;
131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
131181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    RecordThread *thread;
131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t inFrameCount;
131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int lSessionId;
13141d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
131581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check calling permissions
131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!recordingAllowed()) {
1317e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten        ALOGE("openRecord() permission denied: recording not allowed");
131881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
131981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
13211d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1322291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    if (format != AUDIO_FORMAT_PCM_16_BIT) {
1323291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        ALOGE("openRecord() invalid format %d", format);
1324291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        lStatus = BAD_VALUE;
1325291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        goto Exit;
1326291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
1327291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten
132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // add client to list
132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    { // scope for mLock
133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
1333e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() checkRecordThread_l failed");
133481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
133581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
13361d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent        }
13371d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1338893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown        if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1339893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown                && !captureAudioOutputAllowed()) {
1340e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() permission denied: capture not allowed");
1341893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown            lStatus = PERMISSION_DENIED;
1342893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown            goto Exit;
1343893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown        }
1344893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
13458d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        client = registerPid_l(pid);
1347feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If no audio session id is provided, create one here
1349aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = *sessionId;
1351feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        } else {
135281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = nextUniqueId();
135381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId != NULL) {
135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                *sessionId = lSessionId;
135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1356feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        }
135781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create new record track.
135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // The record track uses one track in mHardwareMixerThread by convention.
1359462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        // TODO: the uid should be passed in as a parameter to openRecord
136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1361462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  frameCount, lSessionId,
1362462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  IPCThreadState::self()->getCallingUid(),
1363462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  flags, tid, &lStatus);
136403e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
1365feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1366e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
136881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove local strong reference to Client before deleting the RecordTrack so that the
136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Client destructor is called by the TrackBase destructor with mLock held
137081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        client.clear();
137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack.clear();
137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
1373feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1374feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
13752fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten    // return handle to client
137681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordHandle = new RecordHandle(recordTrack);
1377feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
137881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
13799156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return recordHandle;
1381feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent}
1382feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1383feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
13841d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name)
138859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!settingsAllowed()) {
139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
139181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
139259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return loadHwModule_l(name);
139459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
139559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
139681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held
139781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
139859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
139981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
140081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
140181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("loadHwModule() module %s already loaded", name);
140281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mAudioHwDevs.keyAt(i);
140359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
140459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
140559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *dev;
140759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int rc = load_audio_interface(name, &dev);
140981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() error %d loading module %s ", rc, name);
141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
141259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
141359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_INIT;
141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    rc = dev->init_check(dev);
141681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
141881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
142059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
142159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Check and cache this HAL's level of support for master mute and master
142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volume.  If this is the first HAL opened, and it supports the get
142481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // methods, use the initial values provided by the HAL as the current
142581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // master mute and volume settings.
142659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {  // scope for auto-lock pattern
142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mHardwareLock);
143059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
143181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (0 == mAudioHwDevs.size()) {
143281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
143381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_volume) {
143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float mv;
143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_volume(dev, &mv)) {
143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterVolume = mv;
143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_mute) {
144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool mm;
144381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_mute(dev, &mm)) {
144481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterMute = mm;
144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
144759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_volume) &&
145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_volume(dev, mMasterVolume))) {
145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
145459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
145559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
145681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_mute) &&
145881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_mute(dev, mMasterMute))) {
145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
146259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_IDLE;
146459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
146559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
146681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_module_handle_t handle = nextUniqueId();
146781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
146981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
147081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent          name, dev->common.module->name, dev->common.module->id, handle);
147181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
147381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
147459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
147559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate()
147959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
148059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->sampleRate() : 0;
1483a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent}
148459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount()
1486a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{
148781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
148881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->frameCountHAL() : 0;
149059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
149159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
149265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
149365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
14944182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
14954182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{
14964182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    uid_t uid = IPCThreadState::self()->getCallingUid();
14974182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (uid != AID_SYSTEM) {
14984182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return PERMISSION_DENIED;
14994182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
15004182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    Mutex::Autolock _l(mLock);
15014182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (mIsDeviceTypeKnown) {
15024182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return INVALID_OPERATION;
15034182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
15044182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsLowRamDevice = isLowRamDevice;
15054182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsDeviceTypeKnown = true;
15064182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    return NO_ERROR;
15074182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten}
15084182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
15094182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ----------------------------------------------------------------------------
15104182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_devices_t *pDevices,
151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           uint32_t *pSamplingRate,
151481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_format_t *pFormat,
151581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_channel_mask_t *pChannelMask,
151681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           uint32_t *pLatencyMs,
1517ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                           audio_output_flags_t flags,
1518ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                           const audio_offload_info_t *offloadInfo)
151965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1520ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1521fb872cc6f77f6d74011fab703b3edd7023c736cbGlenn Kasten    memset(&config, 0, sizeof(config));
1522ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1523ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1524ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1525937098b9b564a779ff1c8c2d9e60769ee5c69810Glenn Kasten    if (offloadInfo != NULL) {
1526ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald        config.offload_info = *offloadInfo;
1527ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    }
1528ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1529bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
153081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              module,
153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              (pDevices != NULL) ? *pDevices : 0,
153281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.sample_rate,
153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.format,
153481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.channel_mask,
153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              flags);
1536bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1537e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten          offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
153881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDevices == NULL || *pDevices == 0) {
154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
1541ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
1542ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman
154381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
154465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15453255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
15466e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (outHwDev == NULL) {
154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
15486e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
154965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
155081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
155265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
155381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
155465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15553255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_out_t *outStream = NULL;
155634542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten    status_t status = hwDevHal->open_output_stream(hwDevHal,
155781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          id,
155881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          *pDevices,
155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          (audio_output_flags_t)flags,
156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          &config,
156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          &outStream);
156265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
1564bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "Channels %x, status %d",
156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            outStream,
156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.sample_rate,
156881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.format,
156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.channel_mask,
157081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status);
157158912562617941964939a4182cda71eaeb153d4bGlenn Kasten
157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && outStream != NULL) {
1573bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
157465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15753255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        PlaybackThread *thread;
1576bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1577bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            thread = new OffloadThread(this, output, id, *pDevices);
1578bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1579bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
158081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = new DirectOutputThread(this, output, id, *pDevices);
158381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = new MixerThread(this, output, id, *pDevices);
158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
158765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
158881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.add(id, thread);
158988cbea8a918bbaf5e06e48aadd5af5e81d58d232Glenn Kasten
15907c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pSamplingRate != NULL) {
15917c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pSamplingRate = config.sample_rate;
15927c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
15937c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pFormat != NULL) {
15947c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pFormat = config.format;
15957c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
15967c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pChannelMask != NULL) {
15977c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pChannelMask = config.channel_mask;
15987c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
15997c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pLatencyMs != NULL) {
16007c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pLatencyMs = thread->latency();
16017c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
160265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new output creation
160481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
160565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the first primary output opened designates the primary hw device
160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGI("Using module %d has the primary audio interface", module);
160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mPrimaryHardwareDev = outHwDev;
161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AutoMutex lock(mHardwareLock);
161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_SET_MODE;
161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            hwDevHal->set_mode(hwDevHal, mMode);
161481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return id;
161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
161865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
162065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
162165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
162381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t output2)
162465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
162581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
162681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread1 = checkMixerThread_l(output1);
162781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread2 = checkMixerThread_l(output2);
162881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread1 == NULL || thread2 == NULL) {
163081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                output2);
163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
163365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
163465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
163681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
163781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->addOutputTrack(thread2);
163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPlaybackThreads.add(id, thread);
163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // notify client processes of the new output creation
164081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return id;
164265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
164365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output)
16452bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{
164681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeOutput_nonvirtual(output);
16472bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi}
16482bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi
164981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
165065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
165181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the playback thread so that
165281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
165381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<PlaybackThread> thread;
165481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
165581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
165681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkPlaybackThread_l(output);
165781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
165881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
165965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
166065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeOutput() %d", output);
1662de070137f11d346fba77605bd76a44c040a618fcEric Laurent
166381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread->type() == ThreadBase::MIXER) {
166481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
166581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
166681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    DuplicatingThread *dupThread =
166781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
166881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    dupThread->removeOutputTrack((MixerThread *)thread.get());
1669bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1670de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
1671de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
1672de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
1673bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1674bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
167581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.removeItem(output);
1676bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // save all effects to the default thread
1677bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mPlaybackThreads.size()) {
1678bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1679bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (dstThread != NULL) {
1680bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // audioflinger lock is held here so the acquisition order of thread locks does not
1681bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // matter
1682bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _dl(dstThread->mLock);
1683bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _sl(thread->mLock);
1684bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1685bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
1686bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
1687bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
1688bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1689bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1690bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
16913acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
169281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
16953acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread->type() != ThreadBase::DUPLICATING) {
169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *out = thread->clearOutput();
169881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
169981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // from now on thread->mOutput is NULL
170081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        out->hwDev()->close_output_stream(out->hwDev(), out->stream);
170181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        delete out;
170265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
170381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
170465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
170565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
170681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output)
1707e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{
170881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
170981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
171081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
171181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
171281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1713e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent    }
1714e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
171581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("suspendOutput() %d", output);
171681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->suspend();
1717e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
171881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
171965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
172065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
172181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output)
172265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
17236637baae4244aec731c4014da72418d330636ae1Glenn Kasten    Mutex::Autolock _l(mLock);
172481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
172565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
172681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
172781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1728ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
172965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
173081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("restoreOutput() %d", output);
173165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
173281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->restore();
173365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
173481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
173565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
173665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
173781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
173881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_devices_t *pDevices,
173981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          uint32_t *pSamplingRate,
174081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_format_t *pFormat,
174181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_channel_mask_t *pChannelMask)
174265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1743ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1744fb872cc6f77f6d74011fab703b3edd7023c736cbGlenn Kasten    memset(&config, 0, sizeof(config));
1745ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1746ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1747ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1748ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t reqSamplingRate = config.sample_rate;
175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_format_t reqFormat = config.format;
1751f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten    audio_channel_mask_t reqChannelMask = config.channel_mask;
175265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
175381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDevices == NULL || *pDevices == 0) {
175481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
1755b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent    }
1756b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent
175781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
175865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17593255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
17606e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (inHwDev == NULL) {
176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
17626e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1763fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
176481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *inHwHal = inHwDev->hwDevice();
176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
1766b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
17673255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_in_t *inStream = NULL;
17683255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
176981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        &inStream);
177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
177181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "status %d",
177281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            inStream,
177381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.sample_rate,
177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.format,
177581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.channel_mask,
177681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status);
177765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
177881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the input could not be opened with the requested parameters and we can handle the
177981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
178081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs.
178181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == BAD_VALUE &&
178281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
178381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        (config.sample_rate <= 2 * reqSamplingRate) &&
1784f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten        (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
17858594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME describe the change proposed by HAL (save old values so we can log them here)
178681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("openInput() reopening with proposed sampling rate and channel mask");
178781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inStream = NULL;
178881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
17898594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME log this new status; HAL should not propose any further changes
179065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
179165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
179281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && inStream != NULL) {
179358912562617941964939a4182cda71eaeb153d4bGlenn Kasten
179446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
179581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
179681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // or (re-)create if current Pipe is idle and does not match the new format
179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<NBAIO_Sink> teeSink;
179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        enum {
179981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NO,    // don't copy input
180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NEW,   // copy input using a new pipe
180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_OLD,   // copy input using an existing pipe
180281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } kind;
180381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
180481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        popcount(inStream->common.get_channels(&inStream->common)));
1805da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (!mTeeSinkInputEnabled) {
1806da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            kind = TEE_SINK_NO;
18076e0d67d7b496ce17c0970a4ffd3a6f808860949cGlenn Kasten        } else if (!Format_isValid(format)) {
180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
180981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink == 0) {
181081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
181181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink->getStrongCount() != 1) {
181281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (format == mRecordTeeSink->format()) {
181481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_OLD;
18154adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten        } else {
181681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
181781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
181881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        switch (kind) {
181981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NEW: {
1820da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
182181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numCounterOffers = 0;
182281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const NBAIO_Format offers[1] = {format};
182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
182481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
182581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            PipeReader *pipeReader = new PipeReader(*pipe);
182681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            numCounterOffers = 0;
182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
182881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
182981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSink = pipe;
183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSource = pipeReader;
183181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = pipe;
18324adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten            }
183381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
183481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_OLD:
183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = mRecordTeeSink;
183681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
183781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NO:
183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        default:
183981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
184058912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
184146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
1842da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
184381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
184465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
184581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Start record thread
184634af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten        // RecordThread requires both input and output device indication to forward to audio
184781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // pre processing modules
18483255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        RecordThread *thread = new RecordThread(this,
184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                  input,
185081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                  reqSamplingRate,
1851f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten                                  reqChannelMask,
185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                  id,
1853d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                  primaryOutputDevice_l(),
185446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  *pDevices
185546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
185646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  , teeSink
185746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
185846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  );
185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.add(id, thread);
186081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
18617c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pSamplingRate != NULL) {
18627c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pSamplingRate = reqSamplingRate;
18637c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
18647c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pFormat != NULL) {
18657c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pFormat = config.format;
18667c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
18677c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pChannelMask != NULL) {
1868f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten            *pChannelMask = reqChannelMask;
18697c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
187065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new input creation
187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return id;
18741afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent    }
187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
187765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
187865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input)
188065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
188181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeInput_nonvirtual(input);
188265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
188365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1885de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the record thread so that
188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread> thread;
188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
1894de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
189581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
189681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeInput() %d", input);
189781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
189881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.removeItem(input);
1899de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
190081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
190181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
190281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
1903de070137f11d346fba77605bd76a44c040a618fcEric Laurent
190481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *in = thread->clearInput();
190581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
190681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // from now on thread->mInput is NULL
190781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    in->hwDev()->close_input_stream(in->hwDev(), in->stream);
190881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete in;
190965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
191081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
1911b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
1912b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
191381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
1914b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{
1915b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    Mutex::Autolock _l(mLock);
191681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("setStreamOutput() stream %d to output %d", stream, output);
1917b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
191881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
191981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
192081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->invalidateTracks(stream);
1921b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    }
192281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
192381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
1924b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
1925b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
192781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentint AudioFlinger::newAudioSessionId()
1928162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{
192981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return nextUniqueId();
1930162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent}
1931162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent
193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::acquireAudioSessionId(int audioSession)
1933a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{
1934a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    Mutex::Autolock _l(mLock);
193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
193681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("acquiring %d from %d", audioSession, caller);
1937d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
1938d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // Ignore requests received from processes not known as notification client. The request
1939d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1940d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // called from a different pid leaving a stale session reference.  Also we don't know how
1941d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // to clear this reference if the client process dies.
1942d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    if (mNotificationClients.indexOfKey(caller) < 0) {
1943d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1944d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        return;
1945d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
1946d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
194881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt++;
195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" incremented refcount to %d", ref->mCnt);
195381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
1954a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
1955a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    }
195681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV(" added new entry for %d", audioSession);
1958a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent}
1959a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::releaseAudioSessionId(int audioSession)
196144a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{
196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("releasing %d from %d", audioSession, caller);
196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
196681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
196781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
196881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt--;
197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" decremented refcount to %d", ref->mCnt);
197181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mCnt == 0) {
197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioSessionRefs.removeAt(i);
197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                delete ref;
197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                purgeStaleEffects_l();
197544a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent            }
197681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
197744a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent        }
197844a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent    }
1979d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // If the caller is mediaserver it is likely that the session being released was acquired
1980d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // on behalf of a process not in notification clients and we ignore the warning.
1981d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
198244a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent}
198344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent
198481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() {
198565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("purging stale effects");
1987fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten
198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > chains;
198958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
199381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chains.push(ec);
199681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1997c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
199858912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
199981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread> t = mRecordThreads.valueAt(i);
200181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
200281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
200381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chains.push(ec);
200481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2005300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten    }
200665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
200781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < chains.size(); i++) {
200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> ec = chains[i];
200981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionid = ec->sessionId();
201081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> t = ec->mThread.promote();
201181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
201281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
201381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
201481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numsessionrefs = mAudioSessionRefs.size();
201581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool found = false;
201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t k = 0; k < numsessionrefs; k++) {
201781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
201881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mSessionid == sessionid) {
201981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV(" session %d still exists for %d with %d refs",
202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sessionid, ref->mPid, ref->mCnt);
202181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                found = true;
202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
202358912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
202458912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
202581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!found) {
2026e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            Mutex::Autolock _l(t->mLock);
202781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // remove all effects from the chain
202881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            while (ec->mEffects.size()) {
202981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectModule> effect = ec->mEffects[0];
203081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->unPin();
203181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                t->removeEffect_l(effect);
203281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (effect->purgeHandles()) {
203381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
203581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioSystem::unregisterEffect(effect->id());
203681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2037c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
203858912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return;
204065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
204165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
204281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
204381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
2044190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{
204581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mPlaybackThreads.valueFor(output).get();
2046190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten}
2047190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
204881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held
204981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
205081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
205181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
205281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
205381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2054190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
205581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held
205681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
205781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
205881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mRecordThreads.valueFor(input).get();
205981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2060190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
206181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::nextUniqueId()
206281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
206381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return android_atomic_inc(&mNextUniqueId);
206481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
206583efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten
206681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
206737d825e72a6c606553a745da1212590a425996d3Glenn Kasten{
206881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
206981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
207081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *output = thread->getOutput();
207181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
207281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return thread;
207337d825e72a6c606553a745da1212590a425996d3Glenn Kasten        }
207437d825e72a6c606553a745da1212590a425996d3Glenn Kasten    }
207581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NULL;
207637d825e72a6c606553a745da1212590a425996d3Glenn Kasten}
207737d825e72a6c606553a745da1212590a425996d3Glenn Kasten
207881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const
207965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
2081000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
208281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
208381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
20849f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten    }
2085000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
208681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread->outDevice();
2087000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2088688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten
208981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int triggerSession,
209181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int listenerSession,
209281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    sync_event_callback_t callBack,
209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    void *cookie)
209481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
209581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
209673ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten
209781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
209881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t playStatus = NAME_NOT_FOUND;
209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t recStatus = NAME_NOT_FOUND;
210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
210281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playStatus == NO_ERROR) {
210381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
210565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
210681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
210781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
210881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (recStatus == NO_ERROR) {
210981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
21109f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten        }
211165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
211281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPendingSyncEvents.add(event);
211481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
211581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createSyncEvent() invalid event %d", event->type());
211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        event.clear();
211781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
211881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event;
211965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
212065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
212281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//  Effect management
212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
212458912562617941964939a4182cda71eaeb153d4bGlenn Kasten
212558912562617941964939a4182cda71eaeb153d4bGlenn Kasten
212681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
2127000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
212881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
212981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryNumberEffects(numEffects);
2130000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2131000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
213358912562617941964939a4182cda71eaeb153d4bGlenn Kasten{
213481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
213581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryEffect(index, descriptor);
213658912562617941964939a4182cda71eaeb153d4bGlenn Kasten}
213758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
213881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
213981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *descriptor) const
2140000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
214181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
214281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectGetDescriptor(pUuid, descriptor);
2143000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2144000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
214581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21468d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect(
214781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *pDesc,
214881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
214981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
215081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t io,
215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
215281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status,
215381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *id,
215481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled)
2155000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
215681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus = NO_ERROR;
215781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
215881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc;
2159000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
21608d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
216181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
216281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid, effectClient.get(), priority, sessionId, io);
2163000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDesc == NULL) {
216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2167952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2168000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check audio settings permission for global effects
217081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2173952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2174000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
217581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that can only be created by audio policy manager (running in same process)
217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2180952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
218165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!EffectIsNullUuid(&pDesc->uuid)) {
218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is specified, request effect descriptor
218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
218781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
218881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is not specified, look for an available implementation
219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // of the required type in effect factory
219381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (EffectIsNullUuid(&pDesc->type)) {
219481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() no effect type");
219581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2197288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
219881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t numEffects = 0;
219981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect_descriptor_t d;
220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            d.flags = 0; // prevent compiler warning
220181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool found = false;
2202288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
220381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectQueryNumberEffects(&numEffects);
220481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (uint32_t i = 0; i < numEffects; i++) {
220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = EffectQueryEffect(i, &desc);
221081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (lStatus < 0) {
221181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
2213d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
221481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If matching type found save effect descriptor. If the session is
221681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // 0 and the effect is not auxiliary, continue enumeration in case
221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // an auxiliary version of this effect type is available
221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    found = true;
221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    d = desc;
222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2222d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                        break;
2223d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                    }
2224d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
2225288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (!found) {
222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
222881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() effect not found");
222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For same effect type, chose auxiliary version over insert version if
223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // connect to output mix (Compliance to OpenSL ES)
223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc = d;
223658912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
223758912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
223858912562617941964939a4182cda71eaeb153d4bGlenn Kasten
223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Do not allow auxiliary effects on a session different from 0 (output mix)
224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = INVALID_OPERATION;
224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
22443dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent        }
224565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check recording permission for visualizer
224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            !recordingAllowed()) {
224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = PERMISSION_DENIED;
225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
225265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return effect descriptor
225481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *pDesc = desc;
2255eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2256eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // if the output returned by getOutputForEffect() is removed before we lock the
2257eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2258eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // and we will exit safely
2259eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            io = AudioSystem::getOutputForEffect(&desc);
2260eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            ALOGV("createEffect got output %d", io);
2261eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        }
2262eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent
2263eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        Mutex::Autolock _l(mLock);
226465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is not specified try to find a matching audio session ID in one of the
226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // output threads.
226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // because of code checking output when entering the function.
226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Note: io is never 0 when creating an effect on an input
227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (io == 0) {
22715baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
22725baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // output must be specified by AudioPolicyManager when using session
22735baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // AUDIO_SESSION_OUTPUT_STAGE
22745baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                lStatus = BAD_VALUE;
22755baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                goto Exit;
22765baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
2277eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // look for the thread where the specified audio session is present
2278eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2279eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2280eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    io = mPlaybackThreads.keyAt(i);
2281eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    break;
2282eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                }
2283d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0) {
2285eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
2286eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2287eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                        io = mRecordThreads.keyAt(i);
228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
22892986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
229065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
229165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If no output thread contains the requested session ID, default to
229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // first output. The effect chain will be moved to the correct output
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // thread when a track with the same session ID is created
229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0 && mPlaybackThreads.size()) {
229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                io = mPlaybackThreads.keyAt(0);
229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect() got io %d for effect %s", io, desc.name);
229965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ThreadBase *thread = checkRecordThread_l(io);
230181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = checkPlaybackThread_l(io);
230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (thread == NULL) {
230481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGE("createEffect() unknown output thread");
230581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
230681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2307288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
2308288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten        }
2309288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Client> client = registerPid_l(pid);
231158912562617941964939a4182cda71eaeb153d4bGlenn Kasten
231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect on selected output thread
231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = thread->createEffect_l(client, effectClient, priority, sessionId,
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                &desc, enabled, &lStatus);
231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (handle != 0 && id != NULL) {
231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *id = handle->id();
231765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
231865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
231965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
232081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
23219156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
232366fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten}
232466fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten
232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t dstOutput)
232765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
232981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcOutput, dstOutput);
233065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcOutput == dstOutput) {
233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcThread == NULL) {
233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad srcOutput %d", srcOutput);
233881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
233981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
234181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (dstThread == NULL) {
234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad dstOutput %d", dstOutput);
234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
234465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
234565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _dl(dstThread->mLock);
234781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _sl(srcThread->mLock);
23485baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return moveEffectChain_l(sessionId, srcThread, dstThread, false);
234965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
235065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffectChain_l(int sessionId,
235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *srcThread,
235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *dstThread,
235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   bool reRegister)
235665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcThread, dstThread);
235965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sessionId, srcThread);
236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
23669ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // so that a new chain is created with correct parameters when first effect is added. This is
236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // removed.
237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    srcThread->removeEffectChain_l(chain);
23729ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // transfer all effects one by one so that new effect chain is created on new thread with
237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
237581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> dstChain;
237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy = 0; // prevent compiler warning
237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect = chain->getEffectFromId_l(0);
23785baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    Vector< sp<EffectModule> > removed;
23795baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    status_t status = NO_ERROR;
238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (effect != 0) {
238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        srcThread->removeEffect_l(effect);
23825baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        removed.add(effect);
23835baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        status = dstThread->addEffect_l(effect);
23845baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (status != NO_ERROR) {
23855baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
23865baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // removeEffect_l() has stopped the effect if it was active so it must be restarted
238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect->state() == EffectModule::ACTIVE ||
238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->state() == EffectModule::STOPPING) {
239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->start();
239165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the move request is not received from audio policy manager, the effect must be
239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // re-registered with the new strategy and output
239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (dstChain == 0) {
239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            dstChain = effect->chain().promote();
239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (dstChain == 0) {
239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
23985baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                status = NO_INIT;
23995baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                break;
240065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            strategy = dstChain->strategy();
240265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
240381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (reRegister) {
240481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
240581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::registerEffect(&effect->desc(),
24065baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                        dstThread->id(),
240781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        strategy,
240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        sessionId,
240981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        effect->id());
2410d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent            AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
241281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect = chain->getEffectFromId_l(0);
241358912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
241458912562617941964939a4182cda71eaeb153d4bGlenn Kasten
24155baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (status != NO_ERROR) {
24165baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        for (size_t i = 0; i < removed.size(); i++) {
24175baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            srcThread->addEffect_l(removed[i]);
24185baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (dstChain != 0 && reRegister) {
24195baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::unregisterEffect(removed[i]->id());
24205baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::registerEffect(&removed[i]->desc(),
24215baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            srcThread->id(),
24225baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            strategy,
24235baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            sessionId,
24245baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            removed[i]->id());
2425d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent                AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
24265baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
24275baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
24285baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    }
24295baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
24305baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return status;
243165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
243265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
24335baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
2434813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2435813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    if (mGlobalEffectEnableTime != 0 &&
2436813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2437813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        return true;
2438813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2439813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2440813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2441813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<EffectChain> ec =
2442813e2a74853bde19e37d878c596a044b3f299efcEric Laurent                mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
24435baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (ec != 0 && ec->isNonOffloadableEnabled()) {
2444813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            return true;
2445813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2446813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2447813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    return false;
2448813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2449813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
24505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable()
2451813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2452813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    Mutex::Autolock _l(mLock);
2453813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2454813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    mGlobalEffectEnableTime = systemTime();
2455813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2456813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2457813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2458813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        if (t->mType == ThreadBase::OFFLOAD) {
2459813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            t->invalidateTracks(AUDIO_STREAM_MUSIC);
2460813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2461813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2462813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2463813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2464813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2465da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry {
2466da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_NAME 32     // %Y%m%d%H%M%S_%d.wav
2467da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    char mName[MAX_NAME];
2468da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten};
2469da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2470da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2)
2471da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{
2472da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2473da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}
2474da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
247546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2476d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
247765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
2478d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten    NBAIO_Source *teeSource = source.get();
2479fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    if (teeSource != NULL) {
2480da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // .wav rotation
2481da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // There is a benign race condition if 2 threads call this simultaneously.
2482da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // They would both traverse the directory, but the result would simply be
2483da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // failures at unlink() which are ignored.  It's also unlikely since
2484da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // normally dumpsys is only done by bugreport or from the command line.
2485da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        char teePath[32+256];
2486da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strcpy(teePath, "/data/misc/media");
2487da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        size_t teePathLen = strlen(teePath);
2488da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        DIR *dir = opendir(teePath);
2489da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teePath[teePathLen++] = '/';
2490da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (dir != NULL) {
2491da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_SORT 20 // number of entries to sort
2492da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_KEEP 10 // number of entries to keep
2493da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            struct Entry entries[MAX_SORT];
2494da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            size_t entryCount = 0;
2495da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            while (entryCount < MAX_SORT) {
2496da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent de;
2497da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent *result = NULL;
2498da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                int rc = readdir_r(dir, &de, &result);
2499da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (rc != 0) {
2500da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r failed %d", rc);
2501da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2502da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2503da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result == NULL) {
2504da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2505da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2506da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result != &de) {
2507da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2508da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2509da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2510da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                // ignore non .wav file entries
2511da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                size_t nameLen = strlen(de.d_name);
2512da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (nameLen <= 4 || nameLen >= MAX_NAME ||
2513da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                        strcmp(&de.d_name[nameLen - 4], ".wav")) {
2514da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    continue;
2515da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2516da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                strcpy(entries[entryCount++].mName, de.d_name);
2517da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2518da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            (void) closedir(dir);
2519da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (entryCount > MAX_KEEP) {
2520da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                qsort(entries, entryCount, sizeof(Entry), comparEntry);
2521da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2522da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    strcpy(&teePath[teePathLen], entries[i].mName);
2523da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    (void) unlink(teePath);
2524da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2525da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2526da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        } else {
2527da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
2528da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2529da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2530da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
2531d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        char teeTime[16];
2532fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct timeval tv;
2533fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        gettimeofday(&tv, NULL);
2534fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct tm tm;
2535fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        localtime_r(&tv.tv_sec, &tm);
2536da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2537da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2538da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2539da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
2540fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        if (teeFd >= 0) {
2541fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            char wavHeader[44];
2542fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            memcpy(wavHeader,
2543fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn 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",
2544fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                sizeof(wavHeader));
2545fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            NBAIO_Format format = teeSource->format();
2546fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            unsigned channelCount = Format_channelCount(format);
2547fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            ALOG_ASSERT(channelCount <= FCC_2);
25483b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten            uint32_t sampleRate = Format_sampleRate(format);
2549fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[22] = channelCount;       // number of channels
2550fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[24] = sampleRate;         // sample rate
2551fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[25] = sampleRate >> 8;
2552fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[32] = channelCount * 2;   // block alignment
2553fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, wavHeader, sizeof(wavHeader));
2554fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            size_t total = 0;
2555fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            bool firstRead = true;
2556fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            for (;;) {
2557fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten#define TEE_SINK_READ 1024
2558fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                short buffer[TEE_SINK_READ * FCC_2];
2559fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                size_t count = TEE_SINK_READ;
25602c3b2da3049627264b7c6b449a1622f002210f03John Grossman                ssize_t actual = teeSource->read(buffer, count,
25612c3b2da3049627264b7c6b449a1622f002210f03John Grossman                        AudioBufferProvider::kInvalidPTS);
2562fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                bool wasFirstRead = firstRead;
2563fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                firstRead = false;
2564fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                if (actual <= 0) {
2565fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2566fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                        continue;
2567fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    }
2568fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    break;
2569fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                }
2570a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent                ALOG_ASSERT(actual <= (ssize_t)count);
2571fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                write(teeFd, buffer, actual * channelCount * sizeof(short));
2572fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                total += actual;
2573fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            }
2574fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 4, SEEK_SET);
2575fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2576fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2577fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 40, SEEK_SET);
2578fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            temp =  total * channelCount * sizeof(short);
2579fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2580fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            close(teeFd);
2581da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
2582da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                fdprintf(fd, "tee copied to %s\n", teePath);
2583da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2584fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        } else {
2585da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
2586da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2587da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2588fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        }
2589fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    }
2590d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten}
259146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2592d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
259365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
259465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
259565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact(
259665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
259765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
259865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioFlinger::onTransact(code, data, reply, flags);
259965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
260065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
260165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
2602