AudioFlinger.cpp revision 481fb67a595f23c5b7f5be84b06db9b84a41a42f
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
107f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
108799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
109f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    const hw_module_t *mod;
110799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc;
111799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
112f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
113f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
114f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
115f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
116799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
117f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
118f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = audio_hw_device_open(mod, dev);
119f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
120f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
121f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
122799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
123f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
124f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
125f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
126f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        rc = BAD_VALUE;
127f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        goto out;
128f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
129799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return 0;
130799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
131799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout:
132799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    *dev = NULL;
133799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return rc;
134799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
135799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
13665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
13765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
13865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger()
13965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    : BnAudioFlinger(),
1404ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPrimaryHardwareDev(NULL),
1417d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten      mHardwareStatus(AUDIO_HW_IDLE),
1424ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterVolume(1.0f),
1434ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterMute(false),
1444ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mNextUniqueId(1),
1454ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMode(AUDIO_MODE_INVALID),
1464182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mBtNrecIsOff(false),
1474182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mIsLowRamDevice(true),
148813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mIsDeviceTypeKnown(false),
149813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mGlobalEffectEnableTime(0)
15065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
151949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten    getpid_cached = getpid();
1529e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    char value[PROPERTY_VALUE_MAX];
1539e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
1549e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (doLog) {
1559e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
1569e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
15746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
158da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    (void) property_get("ro.debuggable", value, "0");
159da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int debuggable = atoi(value);
160da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int teeEnabled = 0;
161da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    if (debuggable) {
162da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        (void) property_get("af.tee", value, "0");
163da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teeEnabled = atoi(value);
164da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    }
1656e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 1) {
166da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkInputEnabled = true;
1676e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1686e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 2) {
169da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkOutputEnabled = true;
1706e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1716e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 4) {
172da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkTrackEnabled = true;
1736e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
17446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
1755a61d2f277af3098fc10b2881babca16391362daDima Zavin}
1765a61d2f277af3098fc10b2881babca16391362daDima Zavin
1775a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef()
1785a61d2f277af3098fc10b2881babca16391362daDima Zavin{
179799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc = 0;
180fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
181935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    Mutex::Autolock _l(mLock);
182935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
183799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    /* TODO: move all this work into an Init() function */
1844ff14bae91075eb274eb1c2975982358946e7e63John Grossman    char val_str[PROPERTY_VALUE_MAX] = { 0 };
1854ff14bae91075eb274eb1c2975982358946e7e63John Grossman    if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
1864ff14bae91075eb274eb1c2975982358946e7e63John Grossman        uint32_t int_val;
1874ff14bae91075eb274eb1c2975982358946e7e63John Grossman        if (1 == sscanf(val_str, "%u", &int_val)) {
1884ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = milliseconds(int_val);
1894ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using %u mSec as standby time.", int_val);
1904ff14bae91075eb274eb1c2975982358946e7e63John Grossman        } else {
1914ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
1924ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using default %u mSec as standby time.",
1934ff14bae91075eb274eb1c2975982358946e7e63John Grossman                    (uint32_t)(mStandbyTimeInNsecs / 1000000));
1944ff14bae91075eb274eb1c2975982358946e7e63John Grossman        }
1954ff14bae91075eb274eb1c2975982358946e7e63John Grossman    }
19665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
197a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    mMode = AUDIO_MODE_NORMAL;
19865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
19965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
20065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger()
20165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
20265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mRecordThreads.isEmpty()) {
203c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeInput_nonvirtual() will remove specified entry from mRecordThreads
204d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeInput_nonvirtual(mRecordThreads.keyAt(0));
20565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
20665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mPlaybackThreads.isEmpty()) {
207c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
208d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
20965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
210799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
2112b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2122b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten        // no mHardwareLock needed, as there are no other references to this
213a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
214a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        delete mAudioHwDevs.valueAt(i);
21565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
216481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
217481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Tell media.log service about any old writers that still need to be unregistered
218481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
219481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder != 0) {
220481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
221481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
222481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
223481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mUnregisteredWriters.pop();
224481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mediaLogService->unregisterWriter(iMemory);
225481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
226481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
227481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
22865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
22965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
230a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = {
231a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_PRIMARY,
232a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_A2DP,
233a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_USB,
234a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent};
235a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
236a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
237ee578c0330319f04a48bccbdb26b53fea0388d04John GrossmanAudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
238ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_module_handle_t module,
239ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_devices_t devices)
240799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
241a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // if module is 0, the request comes from an old policy manager and we should load
242a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // well known modules
243a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    if (module == 0) {
244a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
245a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
246a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            loadHwModule_l(audio_interfaces[i]);
247a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
248f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        // then try to find a module supporting the requested device.
249f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
250f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
251f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            audio_hw_device_t *dev = audioHwDevice->hwDevice();
252f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            if ((dev->get_supported_devices != NULL) &&
253f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    (dev->get_supported_devices(dev) & devices) == devices)
254f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                return audioHwDevice;
255f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        }
256a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    } else {
257a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        // check a match for the requested module handle
258ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
259ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (audioHwDevice != NULL) {
260ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            return audioHwDevice;
261a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
262a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    }
263a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
264799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return NULL;
265799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
26665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
267be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
26865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
26965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
27065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
27165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
27265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
27365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("Clients:\n");
27465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < mClients.size(); ++i) {
27577c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        sp<Client> client = mClients.valueAt(i).promote();
27677c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        if (client != 0) {
27777c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            snprintf(buffer, SIZE, "  pid: %d\n", client->pid());
27877c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            result.append(buffer);
27965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
28065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
2813a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
282d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    result.append("Notification Clients:\n");
283d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); ++i) {
284d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        snprintf(buffer, SIZE, "  pid: %d\n", mNotificationClients.keyAt(i));
285d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        result.append(buffer);
286d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
287d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
2883a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    result.append("Global session refs:\n");
289012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten    result.append(" session pid count\n");
2903a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
2913a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *r = mAudioSessionRefs[i];
292012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
2933a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        result.append(buffer);
2943a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
29565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
29665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
29765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
29865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
299be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
30065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
30165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
30265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
30365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
304a4454b4765c5905f14186893b0688be375642283Glenn Kasten    hardware_call_state hardwareStatus = mHardwareStatus;
30565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3064ff14bae91075eb274eb1c2975982358946e7e63John Grossman    snprintf(buffer, SIZE, "Hardware status: %d\n"
3074ff14bae91075eb274eb1c2975982358946e7e63John Grossman                           "Standby Time mSec: %u\n",
3084ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            hardwareStatus,
3094ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            (uint32_t)(mStandbyTimeInNsecs / 1000000));
31065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
31165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
31265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
31365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
314be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
31565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
31665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioFlinger from pid=%d, uid=%d\n",
32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex)
32865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
32965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
33065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
33165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
33265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
33365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
33465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3357dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten        usleep(kDumpLockSleepUs);
33665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args)
34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
34244deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd, args);
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // get state of hardware lock
34681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool hardwareLocked = dumpTryLock(mHardwareLock);
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!hardwareLocked) {
34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kHardwareLockedString);
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mHardwareLock.unlock();
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool locked = dumpTryLock(mLock);
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // failed to lock - AudioFlinger is probably deadlocked
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpClients(fd, args);
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd, args);
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump playback threads
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mPlaybackThreads.valueAt(i)->dump(fd, args);
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump record threads
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mRecordThreads.valueAt(i)->dump(fd, args);
37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
375799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        // dump all hardware devs
376799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
377a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
378799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            dev->dump(dev, fd);
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
380d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
38146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
382d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        // dump the serially shared record tee sink
383d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        if (mRecordTeeSource != 0) {
384d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten            dumpTee(fd, mRecordTeeSource);
385d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        }
38646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
387d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
388d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        if (locked) {
389d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            mLock.unlock();
390d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
3919e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
3929e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // append a copy of media.log here by forwarding fd to it, but don't attempt
3939e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // to lookup the service if it's not running, as it will block for a second
3949e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        if (mLogMemoryDealer != 0) {
3959e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
3969e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (binder != 0) {
3979e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                fdprintf(fd, "\nmedia.log:\n");
3989e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                Vector<String16> args;
3999e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                binder->dump(fd, args);
4009e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
4019e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        }
40265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
40565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kastensp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
40798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{
40898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // If pid is already in the mClients wp<> map, then use that entry
40998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // (for which promote() is always != 0), otherwise create a new entry and Client.
41098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    sp<Client> client = mClients.valueFor(pid).promote();
41198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    if (client == 0) {
41298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = new Client(this, pid);
41398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        mClients.add(pid, client);
41498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    }
41598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten
41698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    return client;
41798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten}
41865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4199e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
4209e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
421481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If there is no memory allocated for logs, return a dummy writer that does nothing
4229e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (mLogMemoryDealer == 0) {
4239e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return new NBLog::Writer();
4249e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
4259e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
426481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Similarly if we can't contact the media.log service, also return a dummy writer
427481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder == 0) {
428481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
4299e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
430481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
431481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
432481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If allocation fails, consult the vector of previously unregistered writers
433481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // and garbage-collect one or more them until an allocation succeeds
434481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (shared == 0) {
435481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        Mutex::Autolock _l(mUnregisteredWritersLock);
436481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
437481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            {
438481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Pick the oldest stale writer to garbage-collect
439481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
440481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mUnregisteredWriters.removeAt(0);
441481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mediaLogService->unregisterWriter(iMemory);
442481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Now the media.log remote reference to IMemory is gone.  When our last local
443481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // reference to IMemory also drops to zero at end of this block,
444481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // the IMemory destructor will deallocate the region from mLogMemoryDealer.
445481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
446481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            // Re-attempt the allocation
447481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
448481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            if (shared != 0) {
449481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                goto success;
450481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
451481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
452481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // Even after garbage-collecting all old writers, there is still not enough memory,
453481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // so return a dummy writer
454481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
455481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
456481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kastensuccess:
457481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mediaLogService->registerWriter(shared, size, name);
458481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    return new NBLog::Writer(size, shared);
4599e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
4609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4619e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
4629e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
463685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    if (writer == 0) {
464685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten        return;
465685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    }
4669e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IMemory> iMemory(writer->getIMemory());
4679e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (iMemory == 0) {
4689e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return;
4699e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
470481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Rather than removing the writer immediately, append it to a queue of old writers to
471481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // be garbage-collected later.  This allows us to continue to view old logs for a while.
472481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Mutex::Autolock _l(mUnregisteredWritersLock);
473481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mUnregisteredWriters.push(writer);
4749e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
4759e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
47665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface
47765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
47865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
47965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack(
480fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
48165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t sampleRate,
48258f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
483254af180475346b6186b49c297f340c9c4817511Glenn Kasten        audio_channel_mask_t channelMask,
484e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten        size_t frameCount,
485e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten        IAudioFlinger::track_flags_t *flags,
48665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const sp<IMemory>& sharedBuffer,
48772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output,
4883acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        pid_t tid,
48965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int *sessionId,
490d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten        String8& name,
491462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int clientUid,
49265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status_t *status)
49365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
49465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<PlaybackThread::Track> track;
49565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<TrackHandle> trackHandle;
49665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<Client> client;
49765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t lStatus;
49865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int lSessionId;
49965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
500263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
501263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // but if someone uses binder directly they could bypass that and cause us to crash
502263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
50329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("createTrack() invalid stream type %d", streamType);
50465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        lStatus = BAD_VALUE;
50565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        goto Exit;
50665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
50765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
50860a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
50960a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    // and we don't yet support 8.24 or 32-bit PCM
51060a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
51160a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        ALOGE("createTrack() invalid format %d", format);
51260a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        lStatus = BAD_VALUE;
513663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        goto Exit;
514663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    }
515663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten
516663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
517663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
518663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        lStatus = BAD_VALUE;
51960a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        goto Exit;
52060a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    }
52160a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten
52265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
52365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
52465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
52539e94f8f723d445447fdee0822291e664b631f60Eric Laurent        PlaybackThread *effectThread = NULL;
52665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
527c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten            ALOGE("no playback thread found for output handle %d", output);
52865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lStatus = BAD_VALUE;
52965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            goto Exit;
53065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
53165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5328d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
533462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
53498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = registerPid_l(pid);
53565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5363856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
537fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
538f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // check if an effect chain with the same session ID is present on another
539f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // output thread and move it here.
540de070137f11d346fba77605bd76a44c040a618fcEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
54139e94f8f723d445447fdee0822291e664b631f60Eric Laurent                sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
54239e94f8f723d445447fdee0822291e664b631f60Eric Laurent                if (mPlaybackThreads.keyAt(i) != output) {
54339e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    uint32_t sessions = t->hasAudioSession(*sessionId);
54439e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    if (sessions & PlaybackThread::EFFECT_SESSION) {
54539e94f8f723d445447fdee0822291e664b631f60Eric Laurent                        effectThread = t.get();
546f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent                        break;
54739e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    }
548de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
549de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
55065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lSessionId = *sessionId;
55165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
552de070137f11d346fba77605bd76a44c040a618fcEric Laurent            // if no audio session id is provided, create one here
5537c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            lSessionId = nextUniqueId();
55465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (sessionId != NULL) {
55565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                *sessionId = lSessionId;
55665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
55765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
5583856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() lSessionId: %d", lSessionId);
55965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
56065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track = thread->createTrack_l(client, streamType, sampleRate, format,
561462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
5622fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten        // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
56339e94f8f723d445447fdee0822291e664b631f60Eric Laurent
56439e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // move effect chain to this output thread if an effect on same session was waiting
56539e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // for a track to be created
56639e94f8f723d445447fdee0822291e664b631f60Eric Laurent        if (lStatus == NO_ERROR && effectThread != NULL) {
5672fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten            // no risk of deadlock because AudioFlinger::mLock is held
56839e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _dl(thread->mLock);
56939e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _sl(effectThread->mLock);
57039e94f8f723d445447fdee0822291e664b631f60Eric Laurent            moveEffectChain_l(lSessionId, effectThread, thread, true);
57139e94f8f723d445447fdee0822291e664b631f60Eric Laurent        }
572a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
573a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        // Look for sync events awaiting for a session to be used.
574a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
575a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
576a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
5772986460984580833161bdaabc7f17da1005a8961Eric Laurent                    if (lStatus == NO_ERROR) {
578d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten                        (void) track->setSyncEvent(mPendingSyncEvents[i]);
5792986460984580833161bdaabc7f17da1005a8961Eric Laurent                    } else {
5802986460984580833161bdaabc7f17da1005a8961Eric Laurent                        mPendingSyncEvents[i]->cancel();
5812986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
582a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    mPendingSyncEvents.removeAt(i);
583a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    i--;
584a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                }
585a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            }
586a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
587e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
58865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
589e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
59065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (lStatus == NO_ERROR) {
591d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten        // s for server's pid, n for normal mixer name, f for fast index
592d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten        name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
593d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten                track->fastIndex());
59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        trackHandle = new TrackHandle(track);
59565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
59665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // remove local strong reference to Client before deleting the Track so that the Client
59765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // destructor is called by the TrackBase destructor with mLock held
59865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        client.clear();
59965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track.clear();
60065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
60165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit:
6039156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
60465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return trackHandle;
60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
60772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
60865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
60965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
61165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6125ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("sampleRate() unknown thread %d", output);
61365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
61465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
61565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->sampleRate();
61665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
61765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenint AudioFlinger::channelCount(audio_io_handle_t output) const
61965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
62065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
62265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6235ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("channelCount() unknown thread %d", output);
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
62565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
62665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->channelCount();
62765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
62865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(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("format() unknown thread %d", output);
63558f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        return AUDIO_FORMAT_INVALID;
63665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
63765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->format();
63865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastensize_t AudioFlinger::frameCount(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("frameCount() unknown thread %d", output);
64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
64765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
64858912562617941964939a4182cda71eaeb153d4bGlenn Kasten    // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
64958912562617941964939a4182cda71eaeb153d4bGlenn Kasten    //       should examine all callers and fix them to handle smaller counts
65065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->frameCount();
65165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
65265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
65372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const
65465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
65565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
65665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
65765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
658c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten        ALOGW("latency(): no playback thread found for output handle %d", output);
65965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
66165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->latency();
66265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value)
66565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
666a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
667a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
668a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
669a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
670a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
67165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
67565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
676a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
677ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterVolume = value;
678a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
679ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master volume in the HALs which support it.
680ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
681ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
682ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
6834ff14bae91075eb274eb1c2975982358946e7e63John Grossman
684ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
685ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterVolume()) {
686ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
687935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent        }
688ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
69065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
691ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master volume in each playback thread.  Playback threads
692ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master volume support will apply
693ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // master volume during the mix operation.  Threads with HALs which do
694ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // support master volume will simply ignore the setting.
6958d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
696ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterVolume(value);
69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
69865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
69965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
70065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
701f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode)
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
703a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
704a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
705a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
706a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
70765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
71065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
71165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
712930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(mode) >= AUDIO_MODE_CNT) {
7135ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("Illegal value: setMode(%d)", mode);
71465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
71565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
71665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
71765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    { // scope for the lock
71865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex lock(mHardwareLock);
719ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_SET_MODE;
721ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        ret = dev->set_mode(dev, mode);
72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_IDLE;
72365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
72465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
72565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (NO_ERROR == ret) {
72665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
72765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mMode = mode;
7288d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++)
729e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setMode(mode);
73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
73165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
73365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
73465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
73565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state)
73665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
737a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
738a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
739a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
740a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
741a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
74265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
74365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
74465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
74565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
74665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
748ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
74965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
750ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_mic_mute(dev, state);
75165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
75265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
75365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
75465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const
75665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
757a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
758a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
759a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return false;
760a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
761a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
762fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    bool state = AUDIO_MODE_INVALID;
7632b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
764ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
766ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    dev->get_mic_mute(dev, &state);
76765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
76865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return state;
76965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
77065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted)
77265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
773d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    status_t ret = initCheck();
774d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    if (ret != NO_ERROR) {
775d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        return ret;
776d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
777d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
77965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
78165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
783ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    Mutex::Autolock _l(mLock);
784ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterMute = muted;
785d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
786ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master mute in the HALs which support it.
787ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
788ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
789ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
790d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
791ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
792ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterMute()) {
793ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
794d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        }
795ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
796d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
797d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
798ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master mute in each playback thread.  Playback threads
799ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master mute support will apply master
800ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute during the mix operation.  Threads with HALs which do support master
801ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute will simply ignore the setting.
8028d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
803ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterMute(muted);
80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
80665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
80765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const
80965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8109806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8119806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterVolume_l();
81265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
81365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
81465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const
81565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8169806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8179806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterMute_l();
81865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
81965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8204ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const
8214ff14bae91075eb274eb1c2975982358946e7e63John Grossman{
8224ff14bae91075eb274eb1c2975982358946e7e63John Grossman    return mMasterVolume;
8234ff14bae91075eb274eb1c2975982358946e7e63John Grossman}
8244ff14bae91075eb274eb1c2975982358946e7e63John Grossman
825d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const
826d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{
827ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    return mMasterMute;
828d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman}
829d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
83072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
83172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
83265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
83365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
83465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
83565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
83665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
83765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
838263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
83929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamVolume() invalid stream %d", stream);
84065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
84165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
84465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = NULL;
84565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (output) {
84665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(output);
84765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
84865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return BAD_VALUE;
84965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
85065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
85165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].volume = value;
85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
8558d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
856e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
85765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
85865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
85965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread->setStreamVolume(stream, value);
86065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
86165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
86365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
86465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
865fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
86665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
86765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
86865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
872263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
873fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
87429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamMute() invalid stream %d", stream);
87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
87765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
878935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    AutoMutex lock(mLock);
87965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].mute = muted;
88065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
881e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
88565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
88765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
888263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
88965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0.0f;
89065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
89165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
89265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
89365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    float volume;
89465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (output) {
89565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
89665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
89765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return 0.0f;
89865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
89965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        volume = thread->streamVolume(stream);
90065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
9016637baae4244aec731c4014da72418d330636ae1Glenn Kasten        volume = streamVolume_l(stream);
90265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
90365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return volume;
90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
90665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
907fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const
90865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
909263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
91065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return true;
91165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
91265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9136637baae4244aec731c4014da72418d330636ae1Glenn Kasten    AutoMutex lock(mLock);
9146637baae4244aec731c4014da72418d330636ae1Glenn Kasten    return streamMute_l(stream);
91565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
91665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
91772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
91865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
919827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
920827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
92365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
92465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
92565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
92665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
92765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // ioHandle == 0 means the parameters are global to the audio hardware interface
92865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (ioHandle == 0) {
929a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        Mutex::Autolock _l(mLock);
930799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        status_t final_result = NO_ERROR;
9318abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        {
932a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            AutoMutex lock(mHardwareLock);
933a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_SET_PARAMETER;
934a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
935a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
936a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                status_t result = dev->set_parameters(dev, keyValuePairs.string());
937a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                final_result = result ?: final_result;
938a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            }
939a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
9408abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        }
94159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
94259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        AudioParameter param = AudioParameter(keyValuePairs);
94359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        String8 value;
94459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
945bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
946bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            if (mBtNrecIsOff != btNrecIsOff) {
94759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
94859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    sp<RecordThread> thread = mRecordThreads.valueAt(i);
949f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    audio_devices_t device = thread->inDevice();
950510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
951510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // collect all of the thread's session IDs
952510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    KeyedVector<int, bool> ids = thread->sessionIds();
953510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // suspend effects associated with those session IDs
954510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    for (size_t j = 0; j < ids.size(); ++j) {
955510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                        int sessionId = ids.keyAt(j);
95659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_AEC,
95759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
958510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
95959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_NS,
96059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
961510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
96259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    }
96359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                }
964bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent                mBtNrecIsOff = btNrecIsOff;
96559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent            }
96659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        }
96728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        String8 screenState;
96828ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
96928ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            bool isOff = screenState == "off";
97081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isOff != (AudioFlinger::mScreenState & 1)) {
97181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
97228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            }
97328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        }
974799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        return final_result;
97565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
97665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
97765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // hold a strong ref on thread in case closeOutput() or closeInput() is called
97865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // and the thread is exited once the lock is released
97965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ThreadBase> thread;
98065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
98165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
98265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(ioHandle);
983d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten        if (thread == 0) {
98465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            thread = checkRecordThread_l(ioHandle);
9857fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten        } else if (thread == primaryPlaybackThread_l()) {
9867c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // indicate output device change to all input threads for pre processing
9877c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            AudioParameter param = AudioParameter(keyValuePairs);
9887c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int value;
98989d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent            if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
99089d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent                    (value != 0)) {
9917c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
9927c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                    mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
9937c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                }
9947c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
99565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
99665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
9977378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (thread != 0) {
9987378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten        return thread->setParameters(keyValuePairs);
99965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
100065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
100165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
100265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
100372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
100465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1005827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1006827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
100765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1008a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
1009a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (ioHandle == 0) {
1011fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        String8 out_s8;
1012fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1013799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
10148abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            char *s;
10158abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            {
10168abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            AutoMutex lock(mHardwareLock);
10178abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_GET_PARAMETER;
1018a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
10198abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            s = dev->get_parameters(dev, keys.string());
10208abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_IDLE;
10218abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            }
1022ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman            out_s8 += String8(s ? s : "");
1023799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            free(s);
1024799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        }
1025fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return out_s8;
102665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
102765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
102865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
102965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
103065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getParameters(keys);
103165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
103265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
103365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
103465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getParameters(keys);
103565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
103665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return String8("");
103765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
103865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1039dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1040dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask) const
104165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1042a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1043a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1044a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return 0;
1045a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1046a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
10472b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
10482b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
1049ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1050ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    memset(&config, 0, sizeof(config));
1051ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = sampleRate;
1052ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = channelMask;
1053ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = format;
1054ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1055ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1056ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    size_t size = dev->get_input_buffer_size(dev, &config);
10572b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_IDLE;
10582b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    return size;
105965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
106065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
106172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenunsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
106265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
106365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
106465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
106565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
106665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
106765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getInputFramesLost();
106865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
106965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return 0;
107065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
107165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
107265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value)
107365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1074a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1075a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1076a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
1077a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1078a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
107965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
108065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
108165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
108265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
108365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
108465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
1085ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
10868abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten    mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
1087ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_voice_volume(dev, value);
108865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
108965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
109065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
109165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
109265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
109326c77556efc30800466b60b3975bc35a70c8c28bGlenn Kastenstatus_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
109472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output) const
109565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
109665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status;
109765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
109865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
109965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
110065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(output);
110165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
110265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getRenderPosition(halFrames, dspFrames);
110365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
110465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
110565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
110665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
110765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
110865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
110965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
111065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
111165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
111265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1113bb001926447d0b7dc71ca8bb3c9856f3136d8f4cGlenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
111465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (mNotificationClients.indexOfKey(pid) < 0) {
111565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<NotificationClient> notificationClient = new NotificationClient(this,
111665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                                                            client,
111765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                                                            pid);
11183856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
111965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mNotificationClients.add(pid, notificationClient);
112165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        sp<IBinder> binder = client->asBinder();
112365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        binder->linkToDeath(notificationClient);
112465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // the config change is always sent from playback or record threads to avoid deadlock
112665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // with AudioSystem::gLock
112765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1128896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
112965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
113065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
113165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
1132896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
113365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
113465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
113565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
113665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
113765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid)
113865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
113965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
114065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1141a3b09254d44cd8d66ec947abe547538c4cfeaa89Glenn Kasten    mNotificationClients.removeItem(pid);
11423a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
11433856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("%d died, releasing its sessions", pid);
11448d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    size_t num = mAudioSessionRefs.size();
11453a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    bool removed = false;
11468d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i< num; ) {
11473a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1148012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        ALOGV(" pid %d @ %d", ref->mPid, i);
1149012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        if (ref->mPid == pid) {
1150012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten            ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
11513a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            mAudioSessionRefs.removeAt(i);
11523a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            delete ref;
11533a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            removed = true;
11543a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            num--;
11558d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        } else {
11568d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten            i++;
11573a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        }
11583a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
11593a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (removed) {
11603a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        purgeStaleEffects_l();
11613a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
116265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
116365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
116465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// audioConfigChanged_l() must be called with AudioFlinger::mLock held
1165b81cc8c6f3eec9edb255ea99b6a6f243585b1e38Glenn Kastenvoid AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
116665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
116765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    size_t size = mNotificationClients.size();
116865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < size; i++) {
116984afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
117084afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten                                                                               param2);
117165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
117265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
117365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
117465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// removeClient_l() must be called with AudioFlinger::mLock held
117565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid)
117665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1177827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("removeClient_l() pid %d, calling pid %d", pid,
117885ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            IPCThreadState::self()->getCallingPid());
117965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mClients.removeItem(pid);
118065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
118165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1182717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held
1183717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1184717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{
1185717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    sp<PlaybackThread> thread;
1186717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1187717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1188717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1189717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            ALOG_ASSERT(thread == 0);
1190717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            thread = mPlaybackThreads.valueAt(i);
1191717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        }
1192717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    }
1193717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1194717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    return thread;
1195717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent}
119665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
119781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
119881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
119965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
120065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
120181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
120281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   RefBase(),
120381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger(audioFlinger),
120481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
120581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
120681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPid(pid),
120781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTimedTrackCount(0)
120865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
120981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
121065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
121165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
121281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Client destructor must be called with AudioFlinger::mLock held
121381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client()
121465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
121581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeClient_l(mPid);
121665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
121765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
121881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const
121965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
122081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mMemoryDealer;
122165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
122265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
122381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Reserve one of the limited slots for a timed audio track associated
122481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// with this client
122581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::Client::reserveTimedTrack()
122665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
122781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int kMaxTimedTracksPerClient = 4;
122865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
123065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("can not create timed track - pid %d has exceeded the limit",
123381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             mPid);
123481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return false;
123565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
123665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
123781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount++;
123881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return true;
123965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
124065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
124181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Release a slot for a timed audio track
124281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::Client::releaseTimedTrack()
124365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
124481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
124581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount--;
1246896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent}
1247896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent
124881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
124981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
125081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
125181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     const sp<IAudioFlingerClient>& client,
125281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     pid_t pid)
125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
1254896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{
125565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
125665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient()
125865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
125965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
126065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
126265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NotificationClient> keep(this);
126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeNotificationClient(mPid);
126581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
126665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
126765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
126965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1270893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brownstatic bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1271893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown    return audio_is_remote_submix_device(inDevice);
1272893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown}
1273893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord(
127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t input,
127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t frameCount,
1280eeca32671896739e84050da5992d5f151a1629deGlenn Kasten        IAudioFlinger::track_flags_t *flags,
128181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *sessionId,
128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
12841d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{
128581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread::RecordTrack> recordTrack;
128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordHandle> recordHandle;
128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Client> client;
128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
128981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    RecordThread *thread;
129081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t inFrameCount;
129181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int lSessionId;
12921d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
129381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check calling permissions
129481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!recordingAllowed()) {
1295e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten        ALOGE("openRecord() permission denied: recording not allowed");
129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
129881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
12991d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1300291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    if (format != AUDIO_FORMAT_PCM_16_BIT) {
1301291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        ALOGE("openRecord() invalid format %d", format);
1302291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        lStatus = BAD_VALUE;
1303291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        goto Exit;
1304291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
1305291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten
130681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // add client to list
130781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    { // scope for mLock
130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
1311e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() checkRecordThread_l failed");
131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
13141d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent        }
13151d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1316893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown        if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1317893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown                && !captureAudioOutputAllowed()) {
1318e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() permission denied: capture not allowed");
1319893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown            lStatus = PERMISSION_DENIED;
1320893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown            goto Exit;
1321893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown        }
1322893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
13238d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        client = registerPid_l(pid);
1325feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
132681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If no audio session id is provided, create one here
132781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = *sessionId;
1329feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        } else {
133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = nextUniqueId();
133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId != NULL) {
133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                *sessionId = lSessionId;
133381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1334feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        }
133581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create new record track.
133681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // The record track uses one track in mHardwareMixerThread by convention.
1337462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        // TODO: the uid should be passed in as a parameter to openRecord
133881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1339462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  frameCount, lSessionId,
1340462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  IPCThreadState::self()->getCallingUid(),
1341462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  flags, tid, &lStatus);
1342e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten        LOG_ALWAYS_FATAL_IF((recordTrack != 0) != (lStatus == NO_ERROR));
1343feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1344e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove local strong reference to Client before deleting the RecordTrack so that the
134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Client destructor is called by the TrackBase destructor with mLock held
134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        client.clear();
134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack.clear();
135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
1351feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1352feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
13532fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten    // return handle to client
135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordHandle = new RecordHandle(recordTrack);
1355feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
135681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
13579156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return recordHandle;
1359feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent}
1360feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1361feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
13621d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name)
136659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!settingsAllowed()) {
136881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
137059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return loadHwModule_l(name);
137259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
137359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
137481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held
137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
137659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("loadHwModule() module %s already loaded", name);
138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mAudioHwDevs.keyAt(i);
138159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
138259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
138359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
138481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *dev;
138559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int rc = load_audio_interface(name, &dev);
138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
138881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() error %d loading module %s ", rc, name);
138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
139059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
139159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
139281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_INIT;
139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    rc = dev->init_check(dev);
139481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
139581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
139681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
139781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
139859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
139959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
140081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Check and cache this HAL's level of support for master mute and master
140181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volume.  If this is the first HAL opened, and it supports the get
140281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // methods, use the initial values provided by the HAL as the current
140381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // master mute and volume settings.
140459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
140581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {  // scope for auto-lock pattern
140781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mHardwareLock);
140859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
140981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (0 == mAudioHwDevs.size()) {
141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_volume) {
141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float mv;
141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_volume(dev, &mv)) {
141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterVolume = mv;
141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
141681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
141881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_mute) {
142081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool mm;
142181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_mute(dev, &mm)) {
142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterMute = mm;
142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
142481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
142559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_volume) &&
142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_volume(dev, mMasterVolume))) {
143081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
143181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
143259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
143359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_mute) &&
143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_mute(dev, mMasterMute))) {
143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
144059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_IDLE;
144259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
144359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
144481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_module_handle_t handle = nextUniqueId();
144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
144781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent          name, dev->common.module->name, dev->common.module->id, handle);
144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
145259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
145359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
145481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
145581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
145681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate()
145759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
145859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->sampleRate() : 0;
1461a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent}
146259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount()
1464a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{
146581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
146681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
146781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->frameCountHAL() : 0;
146859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
146959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
147065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
147165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
14724182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
14734182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{
14744182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    uid_t uid = IPCThreadState::self()->getCallingUid();
14754182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (uid != AID_SYSTEM) {
14764182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return PERMISSION_DENIED;
14774182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
14784182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    Mutex::Autolock _l(mLock);
14794182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (mIsDeviceTypeKnown) {
14804182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return INVALID_OPERATION;
14814182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
14824182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsLowRamDevice = isLowRamDevice;
14834182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsDeviceTypeKnown = true;
14844182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    return NO_ERROR;
14854182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten}
14864182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
14874182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ----------------------------------------------------------------------------
14884182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
149081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_devices_t *pDevices,
149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           uint32_t *pSamplingRate,
149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_format_t *pFormat,
149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           audio_channel_mask_t *pChannelMask,
149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           uint32_t *pLatencyMs,
1495ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                           audio_output_flags_t flags,
1496ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                           const audio_offload_info_t *offloadInfo)
149765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1498ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1499fb872cc6f77f6d74011fab703b3edd7023c736cbGlenn Kasten    memset(&config, 0, sizeof(config));
1500ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1501ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1502ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1503937098b9b564a779ff1c8c2d9e60769ee5c69810Glenn Kasten    if (offloadInfo != NULL) {
1504ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald        config.offload_info = *offloadInfo;
1505ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    }
1506ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1507bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              module,
150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              (pDevices != NULL) ? *pDevices : 0,
151081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.sample_rate,
151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.format,
151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              config.channel_mask,
151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              flags);
1514bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1515e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten          offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
151681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
151781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDevices == NULL || *pDevices == 0) {
151881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
1519ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
1520ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman
152181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
152265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15233255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
15246e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (outHwDev == NULL) {
152581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
15266e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
152765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
152881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
152981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
153065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
153265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15333255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_out_t *outStream = NULL;
153434542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten    status_t status = hwDevHal->open_output_stream(hwDevHal,
153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          id,
153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          *pDevices,
153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          (audio_output_flags_t)flags,
153881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          &config,
153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          &outStream);
154065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
154181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
1542bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
154381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "Channels %x, status %d",
154481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            outStream,
154581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.sample_rate,
154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.format,
154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.channel_mask,
154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status);
154958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
155081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && outStream != NULL) {
1551bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
155265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
15533255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        PlaybackThread *thread;
1554bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1555bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            thread = new OffloadThread(this, output, id, *pDevices);
1556bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1557bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
155881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = new DirectOutputThread(this, output, id, *pDevices);
156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
156281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = new MixerThread(this, output, id, *pDevices);
156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
156565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.add(id, thread);
156788cbea8a918bbaf5e06e48aadd5af5e81d58d232Glenn Kasten
15687c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pSamplingRate != NULL) {
15697c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pSamplingRate = config.sample_rate;
15707c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
15717c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pFormat != NULL) {
15727c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pFormat = config.format;
15737c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
15747c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pChannelMask != NULL) {
15757c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pChannelMask = config.channel_mask;
15767c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
15777c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pLatencyMs != NULL) {
15787c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pLatencyMs = thread->latency();
15797c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
158065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new output creation
158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
158365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the first primary output opened designates the primary hw device
158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGI("Using module %d has the primary audio interface", module);
158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mPrimaryHardwareDev = outHwDev;
158881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AutoMutex lock(mHardwareLock);
159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_SET_MODE;
159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            hwDevHal->set_mode(hwDevHal, mMode);
159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return id;
159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
159665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
159865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
159965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t output2)
160265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
160481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread1 = checkMixerThread_l(output1);
160581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread2 = checkMixerThread_l(output2);
160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread1 == NULL || thread2 == NULL) {
160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                output2);
161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
161165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
161265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
161481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->addOutputTrack(thread2);
161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPlaybackThreads.add(id, thread);
161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // notify client processes of the new output creation
161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return id;
162065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
162165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output)
16232bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{
162481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeOutput_nonvirtual(output);
16252bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi}
16262bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi
162781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
162865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the playback thread so that
163081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<PlaybackThread> thread;
163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
163481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkPlaybackThread_l(output);
163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
163681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
163765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
163865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeOutput() %d", output);
1640de070137f11d346fba77605bd76a44c040a618fcEric Laurent
164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread->type() == ThreadBase::MIXER) {
164281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
164381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    DuplicatingThread *dupThread =
164581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
164681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    dupThread->removeOutputTrack((MixerThread *)thread.get());
1647bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1648de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
1649de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
1650de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
1651bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1652bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
165381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.removeItem(output);
1654bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // save all effects to the default thread
1655bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mPlaybackThreads.size()) {
1656bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1657bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (dstThread != NULL) {
1658bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // audioflinger lock is held here so the acquisition order of thread locks does not
1659bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // matter
1660bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _dl(dstThread->mLock);
1661bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _sl(thread->mLock);
1662bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1663bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
1664bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
1665bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
1666bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1667bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1668bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
16693acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
167081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
167181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
167281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
16733acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
167481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread->type() != ThreadBase::DUPLICATING) {
167581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *out = thread->clearOutput();
167681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
167781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // from now on thread->mOutput is NULL
167881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        out->hwDev()->close_output_stream(out->hwDev(), out->stream);
167981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        delete out;
168065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
168181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
168265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
168365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
168481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output)
1685e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{
168681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
168781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
168881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
168981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
169081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1691e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent    }
1692e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("suspendOutput() %d", output);
169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->suspend();
1695e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
169765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
169865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
169981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output)
170065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
17016637baae4244aec731c4014da72418d330636ae1Glenn Kasten    Mutex::Autolock _l(mLock);
170281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
170365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
170481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
170581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1706ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
170765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
170881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("restoreOutput() %d", output);
170965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
171081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->restore();
171165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
171281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
171365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
171465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
171581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
171681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_devices_t *pDevices,
171781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          uint32_t *pSamplingRate,
171881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_format_t *pFormat,
171981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          audio_channel_mask_t *pChannelMask)
172065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1721ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    struct audio_config config;
1722fb872cc6f77f6d74011fab703b3edd7023c736cbGlenn Kasten    memset(&config, 0, sizeof(config));
1723ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1724ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1725ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald    config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1726ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
172781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t reqSamplingRate = config.sample_rate;
172881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_format_t reqFormat = config.format;
1729f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten    audio_channel_mask_t reqChannelMask = config.channel_mask;
173065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
173181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDevices == NULL || *pDevices == 0) {
173281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
1733b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent    }
1734b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent
173581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
173665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17373255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
17386e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (inHwDev == NULL) {
173981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
17406e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1741fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
174281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *inHwHal = inHwDev->hwDevice();
174381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
1744b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
17453255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_in_t *inStream = NULL;
17463255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
174781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        &inStream);
174881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "status %d",
175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            inStream,
175181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.sample_rate,
175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.format,
175381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            config.channel_mask,
175481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status);
175565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
175681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the input could not be opened with the requested parameters and we can handle the
175781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs.
175981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == BAD_VALUE &&
176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        (config.sample_rate <= 2 * reqSamplingRate) &&
1762f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten        (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
17638594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME describe the change proposed by HAL (save old values so we can log them here)
176481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("openInput() reopening with proposed sampling rate and channel mask");
176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inStream = NULL;
176681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
17678594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME log this new status; HAL should not propose any further changes
176865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
176965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && inStream != NULL) {
177158912562617941964939a4182cda71eaeb153d4bGlenn Kasten
177246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
177381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // or (re-)create if current Pipe is idle and does not match the new format
177581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<NBAIO_Sink> teeSink;
177681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        enum {
177781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NO,    // don't copy input
177881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NEW,   // copy input using a new pipe
177981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_OLD,   // copy input using an existing pipe
178081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } kind;
178181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
178281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        popcount(inStream->common.get_channels(&inStream->common)));
1783da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (!mTeeSinkInputEnabled) {
1784da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            kind = TEE_SINK_NO;
1785da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        } else if (format == Format_Invalid) {
178681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
178781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink == 0) {
178881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
178981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink->getStrongCount() != 1) {
179081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
179181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (format == mRecordTeeSink->format()) {
179281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_OLD;
17934adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten        } else {
179481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
179581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
179681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        switch (kind) {
179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NEW: {
1798da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
179981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numCounterOffers = 0;
180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const NBAIO_Format offers[1] = {format};
180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
180281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
180381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            PipeReader *pipeReader = new PipeReader(*pipe);
180481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            numCounterOffers = 0;
180581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
180681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
180781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSink = pipe;
180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSource = pipeReader;
180981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = pipe;
18104adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten            }
181181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
181281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_OLD:
181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = mRecordTeeSink;
181481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
181581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NO:
181681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        default:
181781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
181858912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
181946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
1820da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
182181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
182265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Start record thread
182434af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten        // RecordThread requires both input and output device indication to forward to audio
182581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // pre processing modules
18263255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        RecordThread *thread = new RecordThread(this,
182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                  input,
182881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                  reqSamplingRate,
1829f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten                                  reqChannelMask,
183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                  id,
1831d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                  primaryOutputDevice_l(),
183246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  *pDevices
183346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
183446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  , teeSink
183546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
183646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  );
183781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.add(id, thread);
183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
18397c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pSamplingRate != NULL) {
18407c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pSamplingRate = reqSamplingRate;
18417c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
18427c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pFormat != NULL) {
18437c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten            *pFormat = config.format;
18447c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
18457c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        if (pChannelMask != NULL) {
1846f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten            *pChannelMask = reqChannelMask;
18477c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten        }
184865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new input creation
185081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
185181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return id;
18521afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent    }
185381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
185481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
185565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
185665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input)
185865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeInput_nonvirtual(input);
186065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
186165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1863de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
186481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the record thread so that
186581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
186681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread> thread;
186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
186881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
1872de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeInput() %d", input);
187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.removeItem(input);
1877de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
1881de070137f11d346fba77605bd76a44c040a618fcEric Laurent
188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *in = thread->clearInput();
188381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // from now on thread->mInput is NULL
188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    in->hwDev()->close_input_stream(in->hwDev(), in->stream);
188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete in;
188765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
1889b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
1890b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
1892b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{
1893b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    Mutex::Autolock _l(mLock);
189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("setStreamOutput() stream %d to output %d", stream, output);
1895b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
189681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
189781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
189881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->invalidateTracks(stream);
1899b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    }
190081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
190181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
1902b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
1903b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
190481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
190581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentint AudioFlinger::newAudioSessionId()
1906162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{
190781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return nextUniqueId();
1908162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent}
1909162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent
191081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::acquireAudioSessionId(int audioSession)
1911a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{
1912a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    Mutex::Autolock _l(mLock);
191381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
191481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("acquiring %d from %d", audioSession, caller);
1915d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
1916d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // Ignore requests received from processes not known as notification client. The request
1917d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
1918d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // called from a different pid leaving a stale session reference.  Also we don't know how
1919d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // to clear this reference if the client process dies.
1920d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    if (mNotificationClients.indexOfKey(caller) < 0) {
1921d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
1922d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        return;
1923d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
1924d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
192581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
192781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
192881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
192981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt++;
193081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" incremented refcount to %d", ref->mCnt);
193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
1932a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
1933a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    }
193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV(" added new entry for %d", audioSession);
1936a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent}
1937a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
193881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::releaseAudioSessionId(int audioSession)
193944a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{
194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("releasing %d from %d", audioSession, caller);
194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
194581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
194681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt--;
194881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" decremented refcount to %d", ref->mCnt);
194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mCnt == 0) {
195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioSessionRefs.removeAt(i);
195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                delete ref;
195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                purgeStaleEffects_l();
195344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent            }
195481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
195544a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent        }
195644a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent    }
1957d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // If the caller is mediaserver it is likely that the session being released was acquired
1958d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // on behalf of a process not in notification clients and we ignore the warning.
1959d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
196044a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent}
196144a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent
196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() {
196365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("purging stale effects");
1965fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten
196681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > chains;
196758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
196881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
197181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chains.push(ec);
197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1975c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
197658912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
197881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread> t = mRecordThreads.valueAt(i);
197981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
198081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
198181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chains.push(ec);
198281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1983300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten    }
198465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
198581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < chains.size(); i++) {
198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> ec = chains[i];
198781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionid = ec->sessionId();
198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> t = ec->mThread.promote();
198981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numsessionrefs = mAudioSessionRefs.size();
199381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool found = false;
199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t k = 0; k < numsessionrefs; k++) {
199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
199681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mSessionid == sessionid) {
199781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV(" session %d still exists for %d with %d refs",
199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sessionid, ref->mPid, ref->mCnt);
199981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                found = true;
200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
200158912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
200258912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
200381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!found) {
2004e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            Mutex::Autolock _l(t->mLock);
200581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // remove all effects from the chain
200681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            while (ec->mEffects.size()) {
200781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectModule> effect = ec->mEffects[0];
200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->unPin();
200981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                t->removeEffect_l(effect);
201081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (effect->purgeHandles()) {
201181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
201281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
201381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioSystem::unregisterEffect(effect->id());
201481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2015c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
201658912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
201781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return;
201865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
201965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
202181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
2022190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{
202381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mPlaybackThreads.valueFor(output).get();
2024190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten}
2025190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
202681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held
202781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
202881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
202981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
203081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
203181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2032190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
203381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held
203481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
203581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
203681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mRecordThreads.valueFor(input).get();
203781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2038190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::nextUniqueId()
204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
204181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return android_atomic_inc(&mNextUniqueId);
204281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
204383efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten
204481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
204537d825e72a6c606553a745da1212590a425996d3Glenn Kasten{
204681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
204781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
204881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *output = thread->getOutput();
204981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
205081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return thread;
205137d825e72a6c606553a745da1212590a425996d3Glenn Kasten        }
205237d825e72a6c606553a745da1212590a425996d3Glenn Kasten    }
205381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NULL;
205437d825e72a6c606553a745da1212590a425996d3Glenn Kasten}
205537d825e72a6c606553a745da1212590a425996d3Glenn Kasten
205681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const
205765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
205881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
2059000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
206081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
206181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
20629f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten    }
2063000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
206481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread->outDevice();
2065000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2066688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten
206781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
206881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int triggerSession,
206981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int listenerSession,
207081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    sync_event_callback_t callBack,
207181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    void *cookie)
207281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
207381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
207473ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten
207581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
207681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t playStatus = NAME_NOT_FOUND;
207781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t recStatus = NAME_NOT_FOUND;
207881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
207981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playStatus == NO_ERROR) {
208181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
208281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
208365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
208481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
208581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
208681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (recStatus == NO_ERROR) {
208781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
20889f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten        }
208965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
209181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPendingSyncEvents.add(event);
209281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createSyncEvent() invalid event %d", event->type());
209481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        event.clear();
209581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
209681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event;
209765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
209865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//  Effect management
210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
210258912562617941964939a4182cda71eaeb153d4bGlenn Kasten
210358912562617941964939a4182cda71eaeb153d4bGlenn Kasten
210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
2105000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
210681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
210781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryNumberEffects(numEffects);
2108000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2109000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
211158912562617941964939a4182cda71eaeb153d4bGlenn Kasten{
211281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryEffect(index, descriptor);
211458912562617941964939a4182cda71eaeb153d4bGlenn Kasten}
211558912562617941964939a4182cda71eaeb153d4bGlenn Kasten
211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
211781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *descriptor) const
2118000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
212081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectGetDescriptor(pUuid, descriptor);
2121000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2122000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21248d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect(
212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *pDesc,
212681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
212781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
212881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t io,
212981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
213081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status,
213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *id,
213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled)
2133000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
213481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus = NO_ERROR;
213581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
213681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc;
2137000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
21388d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
213981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
214081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid, effectClient.get(), priority, sessionId, io);
2141000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
214281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDesc == NULL) {
214381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
214481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2145952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2146000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
214781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check audio settings permission for global effects
214881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
214981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
215081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2151952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2152000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
215381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
215481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that can only be created by audio policy manager (running in same process)
215581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
215681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
215781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2158952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
215965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
216081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
216181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!EffectIsNullUuid(&pDesc->uuid)) {
216281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is specified, request effect descriptor
216381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
216781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is not specified, look for an available implementation
217081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // of the required type in effect factory
217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (EffectIsNullUuid(&pDesc->type)) {
217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() no effect type");
217381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
217481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2175288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t numEffects = 0;
217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect_descriptor_t d;
217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            d.flags = 0; // prevent compiler warning
217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool found = false;
2180288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectQueryNumberEffects(&numEffects);
218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (uint32_t i = 0; i < numEffects; i++) {
218781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = EffectQueryEffect(i, &desc);
218881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (lStatus < 0) {
218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
2191d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
219381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If matching type found save effect descriptor. If the session is
219481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // 0 and the effect is not auxiliary, continue enumeration in case
219581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // an auxiliary version of this effect type is available
219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    found = true;
219781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    d = desc;
219881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
219981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2200d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                        break;
2201d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                    }
2202d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
2203288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
220481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (!found) {
220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() effect not found");
220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For same effect type, chose auxiliary version over insert version if
221081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // connect to output mix (Compliance to OpenSL ES)
221181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
221381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc = d;
221458912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
221558912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
221658912562617941964939a4182cda71eaeb153d4bGlenn Kasten
221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Do not allow auxiliary effects on a session different from 0 (output mix)
221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = INVALID_OPERATION;
222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
22223dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent        }
222365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check recording permission for visualizer
222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            !recordingAllowed()) {
222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = PERMISSION_DENIED;
222881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
223065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return effect descriptor
223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *pDesc = desc;
2233eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2234eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // if the output returned by getOutputForEffect() is removed before we lock the
2235eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2236eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // and we will exit safely
2237eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            io = AudioSystem::getOutputForEffect(&desc);
2238eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            ALOGV("createEffect got output %d", io);
2239eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        }
2240eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent
2241eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        Mutex::Autolock _l(mLock);
224265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is not specified try to find a matching audio session ID in one of the
224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // output threads.
224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // because of code checking output when entering the function.
224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Note: io is never 0 when creating an effect on an input
224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (io == 0) {
22495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
22505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // output must be specified by AudioPolicyManager when using session
22515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // AUDIO_SESSION_OUTPUT_STAGE
22525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                lStatus = BAD_VALUE;
22535baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                goto Exit;
22545baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
2255eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // look for the thread where the specified audio session is present
2256eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2257eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2258eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    io = mPlaybackThreads.keyAt(i);
2259eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    break;
2260eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                }
2261d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0) {
2263eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
2264eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2265eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                        io = mRecordThreads.keyAt(i);
226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
22672986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
226865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
226965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If no output thread contains the requested session ID, default to
227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // first output. The effect chain will be moved to the correct output
227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // thread when a track with the same session ID is created
227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0 && mPlaybackThreads.size()) {
227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                io = mPlaybackThreads.keyAt(0);
227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect() got io %d for effect %s", io, desc.name);
227765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ThreadBase *thread = checkRecordThread_l(io);
227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = checkPlaybackThread_l(io);
228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (thread == NULL) {
228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGE("createEffect() unknown output thread");
228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2285288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
2286288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten        }
2287288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Client> client = registerPid_l(pid);
228958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
229081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect on selected output thread
229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = thread->createEffect_l(client, effectClient, priority, sessionId,
229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                &desc, enabled, &lStatus);
229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (handle != 0 && id != NULL) {
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *id = handle->id();
229565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
229665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
229765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
229881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
22999156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
230166fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten}
230266fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten
230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
230481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t dstOutput)
230565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
230681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
230781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcOutput, dstOutput);
230865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcOutput == dstOutput) {
231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcThread == NULL) {
231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad srcOutput %d", srcOutput);
231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (dstThread == NULL) {
232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad dstOutput %d", dstOutput);
232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
232265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
232365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
232481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _dl(dstThread->mLock);
232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _sl(srcThread->mLock);
23265baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return moveEffectChain_l(sessionId, srcThread, dstThread, false);
232765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
232865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
232981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffectChain_l(int sessionId,
233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *srcThread,
233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *dstThread,
233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   bool reRegister)
233465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcThread, dstThread);
233765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
233881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
233981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
234181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sessionId, srcThread);
234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
23449ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
234581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // so that a new chain is created with correct parameters when first effect is added. This is
234781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
234881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // removed.
234981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    srcThread->removeEffectChain_l(chain);
23509ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // transfer all effects one by one so that new effect chain is created on new thread with
235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> dstChain;
235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy = 0; // prevent compiler warning
235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect = chain->getEffectFromId_l(0);
23565baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    Vector< sp<EffectModule> > removed;
23575baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    status_t status = NO_ERROR;
235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (effect != 0) {
235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        srcThread->removeEffect_l(effect);
23605baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        removed.add(effect);
23615baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        status = dstThread->addEffect_l(effect);
23625baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (status != NO_ERROR) {
23635baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
23645baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // removeEffect_l() has stopped the effect if it was active so it must be restarted
236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect->state() == EffectModule::ACTIVE ||
236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->state() == EffectModule::STOPPING) {
236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->start();
236965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the move request is not received from audio policy manager, the effect must be
237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // re-registered with the new strategy and output
237281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (dstChain == 0) {
237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            dstChain = effect->chain().promote();
237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (dstChain == 0) {
237581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
23765baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                status = NO_INIT;
23775baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                break;
237865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
237981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            strategy = dstChain->strategy();
238065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (reRegister) {
238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::registerEffect(&effect->desc(),
23845baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                        dstThread->id(),
238581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        strategy,
238681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        sessionId,
238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        effect->id());
2388d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent            AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect = chain->getEffectFromId_l(0);
239158912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
239258912562617941964939a4182cda71eaeb153d4bGlenn Kasten
23935baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (status != NO_ERROR) {
23945baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        for (size_t i = 0; i < removed.size(); i++) {
23955baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            srcThread->addEffect_l(removed[i]);
23965baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (dstChain != 0 && reRegister) {
23975baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::unregisterEffect(removed[i]->id());
23985baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::registerEffect(&removed[i]->desc(),
23995baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            srcThread->id(),
24005baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            strategy,
24015baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            sessionId,
24025baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            removed[i]->id());
2403d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent                AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
24045baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
24055baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
24065baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    }
24075baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
24085baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return status;
240965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
241065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
24115baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
2412813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2413813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    if (mGlobalEffectEnableTime != 0 &&
2414813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2415813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        return true;
2416813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2417813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2418813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2419813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<EffectChain> ec =
2420813e2a74853bde19e37d878c596a044b3f299efcEric Laurent                mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
24215baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (ec != 0 && ec->isNonOffloadableEnabled()) {
2422813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            return true;
2423813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2424813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2425813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    return false;
2426813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2427813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
24285baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable()
2429813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2430813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    Mutex::Autolock _l(mLock);
2431813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2432813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    mGlobalEffectEnableTime = systemTime();
2433813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2434813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2435813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2436813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        if (t->mType == ThreadBase::OFFLOAD) {
2437813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            t->invalidateTracks(AUDIO_STREAM_MUSIC);
2438813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2439813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2440813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2441813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2442813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2443da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry {
2444da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_NAME 32     // %Y%m%d%H%M%S_%d.wav
2445da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    char mName[MAX_NAME];
2446da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten};
2447da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2448da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2)
2449da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{
2450da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2451da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}
2452da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
245346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2454d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
245565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
2456d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten    NBAIO_Source *teeSource = source.get();
2457fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    if (teeSource != NULL) {
2458da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // .wav rotation
2459da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // There is a benign race condition if 2 threads call this simultaneously.
2460da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // They would both traverse the directory, but the result would simply be
2461da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // failures at unlink() which are ignored.  It's also unlikely since
2462da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // normally dumpsys is only done by bugreport or from the command line.
2463da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        char teePath[32+256];
2464da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strcpy(teePath, "/data/misc/media");
2465da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        size_t teePathLen = strlen(teePath);
2466da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        DIR *dir = opendir(teePath);
2467da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teePath[teePathLen++] = '/';
2468da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (dir != NULL) {
2469da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_SORT 20 // number of entries to sort
2470da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_KEEP 10 // number of entries to keep
2471da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            struct Entry entries[MAX_SORT];
2472da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            size_t entryCount = 0;
2473da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            while (entryCount < MAX_SORT) {
2474da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent de;
2475da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent *result = NULL;
2476da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                int rc = readdir_r(dir, &de, &result);
2477da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (rc != 0) {
2478da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r failed %d", rc);
2479da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2480da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2481da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result == NULL) {
2482da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2483da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2484da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result != &de) {
2485da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2486da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2487da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2488da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                // ignore non .wav file entries
2489da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                size_t nameLen = strlen(de.d_name);
2490da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (nameLen <= 4 || nameLen >= MAX_NAME ||
2491da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                        strcmp(&de.d_name[nameLen - 4], ".wav")) {
2492da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    continue;
2493da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2494da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                strcpy(entries[entryCount++].mName, de.d_name);
2495da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2496da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            (void) closedir(dir);
2497da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (entryCount > MAX_KEEP) {
2498da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                qsort(entries, entryCount, sizeof(Entry), comparEntry);
2499da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2500da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    strcpy(&teePath[teePathLen], entries[i].mName);
2501da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    (void) unlink(teePath);
2502da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2503da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2504da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        } else {
2505da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
2506da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2507da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2508da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
2509d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        char teeTime[16];
2510fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct timeval tv;
2511fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        gettimeofday(&tv, NULL);
2512fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct tm tm;
2513fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        localtime_r(&tv.tv_sec, &tm);
2514da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2515da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2516da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2517da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
2518fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        if (teeFd >= 0) {
2519fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            char wavHeader[44];
2520fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            memcpy(wavHeader,
2521fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn 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",
2522fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                sizeof(wavHeader));
2523fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            NBAIO_Format format = teeSource->format();
2524fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            unsigned channelCount = Format_channelCount(format);
2525fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            ALOG_ASSERT(channelCount <= FCC_2);
25263b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten            uint32_t sampleRate = Format_sampleRate(format);
2527fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[22] = channelCount;       // number of channels
2528fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[24] = sampleRate;         // sample rate
2529fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[25] = sampleRate >> 8;
2530fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[32] = channelCount * 2;   // block alignment
2531fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, wavHeader, sizeof(wavHeader));
2532fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            size_t total = 0;
2533fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            bool firstRead = true;
2534fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            for (;;) {
2535fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten#define TEE_SINK_READ 1024
2536fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                short buffer[TEE_SINK_READ * FCC_2];
2537fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                size_t count = TEE_SINK_READ;
25382c3b2da3049627264b7c6b449a1622f002210f03John Grossman                ssize_t actual = teeSource->read(buffer, count,
25392c3b2da3049627264b7c6b449a1622f002210f03John Grossman                        AudioBufferProvider::kInvalidPTS);
2540fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                bool wasFirstRead = firstRead;
2541fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                firstRead = false;
2542fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                if (actual <= 0) {
2543fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2544fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                        continue;
2545fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    }
2546fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    break;
2547fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                }
2548a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent                ALOG_ASSERT(actual <= (ssize_t)count);
2549fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                write(teeFd, buffer, actual * channelCount * sizeof(short));
2550fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                total += actual;
2551fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            }
2552fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 4, SEEK_SET);
2553fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2554fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2555fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 40, SEEK_SET);
2556fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            temp =  total * channelCount * sizeof(short);
2557fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2558fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            close(teeFd);
2559da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
2560da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                fdprintf(fd, "tee copied to %s\n", teePath);
2561da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2562fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        } else {
2563da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
2564da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2565da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2566fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        }
2567fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    }
2568d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten}
256946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2570d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
257165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
257265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
257365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact(
257465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
257565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
257665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioFlinger::onTransact(code, data, reply, flags);
257765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
257865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
257965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
2580