AudioFlinger.cpp revision 6770c6faa3467c92eabc5ec9b23d60eb556a0d03
199e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten/*
265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Copyright 2007, The Android Open Source Project
465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Licensed under the Apache License, Version 2.0 (the "License");
665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** you may not use this file except in compliance with the License.
765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** You may obtain a copy of the License at
865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**     http://www.apache.org/licenses/LICENSE-2.0
1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Unless required by applicable law or agreed to in writing, software
1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** distributed under the License is distributed on an "AS IS" BASIS,
1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** See the License for the specific language governing permissions and
1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** limitations under the License.
1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian*/
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define LOG_TAG "AudioFlinger"
2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//#define LOG_NDEBUG 0
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
22153b9fe667e6e78e0218ff0159353097428c7657Glenn Kasten#include "Configuration.h"
23da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <dirent.h>
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <math.h>
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <signal.h>
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/time.h>
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/resource.h>
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
299ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang#include <binder/IPCThreadState.h>
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IServiceManager.h>
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Log.h>
32d8e6fd35ec2b59ee7d873daf1f1d9d348221c7bcGlenn Kasten#include <utils/Trace.h>
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/Parcel.h>
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/String16.h>
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h>
3638ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent#include <utils/Atomic.h>
3765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <cutils/bitops.h>
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/properties.h>
4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4164760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
427394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin#include <hardware/audio.h>
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioMixer.h"
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioFlinger.h"
4644deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten#include "ServiceUtilities.h"
4765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
486770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung#include <media/AudioResamplerPublic.h>
496770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/EffectsFactoryApi.h>
516d8b694d999e9be7d5dcc336535832a80fb6f61fEric Laurent#include <audio_effects/effect_visualizer.h>
5259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_ns.h>
5359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_aec.h>
5465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
553b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten#include <audio_utils/primitives.h>
563b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten
57feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent#include <powermanager/PowerManager.h>
58190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
594ff14bae91075eb274eb1c2975982358946e7e63John Grossman#include <common_time/cc_helper.h>
6058912562617941964939a4182cda71eaeb153d4bGlenn Kasten
619e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten#include <media/IMediaLogService.h>
629e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
63da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <media/nbaio/Pipe.h>
64da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <media/nbaio/PipeReader.h>
651ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/AudioParameter.h>
664182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten#include <private/android_filesystem_config.h>
67da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
6865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
701c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// Note: the following macro is used for extremely verbose logging message.  In
711c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
721c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// 0; but one side effect of this is to turn all LOGV's as well.  Some messages
731c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are so verbose that we want to suppress them even when we have ALOG_ASSERT
741c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// turned on.  Do not uncomment the #def below unless you really know what you
751c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are doing and want to see all of the extremely verbose messages.
761c345196edc61694f29307a1826a64a0d26028dcJohn Grossman//#define VERY_VERY_VERBOSE_LOGGING
771c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#ifdef VERY_VERY_VERBOSE_LOGGING
781c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV ALOGV
791c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#else
801c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV(a...) do { } while(0)
811c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#endif
82de070137f11d346fba77605bd76a44c040a618fcEric Laurent
8365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
8465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
86ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kHardwareLockedString[] = "Hardware lock is taken\n";
87021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentstatic const char kClientLockedString[] = "Client lock is taken\n";
8865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
904ff14bae91075eb274eb1c2975982358946e7e63John Grossmannsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
917cafbb32999049873d4746ba83bd20c88abe6ce6Eric Laurent
9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::mScreenState;
933ed292031dc50c56110cdadb1e3778117e3be76aGlenn Kasten
9446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
95da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkInputEnabled = false;
96da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkOutputEnabled = false;
97da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkTrackEnabled = false;
98da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
99da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
100da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
101da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
10246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
103da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
104813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
105813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// we define a minimum time during which a global effect is considered enabled.
106813e2a74853bde19e37d878c596a044b3f299efcEric Laurentstatic const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
107813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
10865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
10965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
110b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissenconst char *formatToString(audio_format_t format) {
1112829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    switch (format & AUDIO_FORMAT_MAIN_MASK) {
1122829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_PCM:
1132829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        switch (format) {
1142829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
1152829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
1162829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
1172829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
1182829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
1192829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
1202829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        default:
1212829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad            break;
1222829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        }
1232829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad        break;
124b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_MP3: return "mp3";
125b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_NB: return "amr-nb";
126b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AMR_WB: return "amr-wb";
127b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_AAC: return "aac";
128b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
129b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
130b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    case AUDIO_FORMAT_VORBIS: return "vorbis";
1312829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_OPUS: return "opus";
1322829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_AC3: return "ac-3";
1332829edccd7d2bb8244246f316face82b650b8949aarti jadhav-gaikwad    case AUDIO_FORMAT_E_AC3: return "e-ac-3";
134b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    default:
135b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        break;
136b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
137b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    return "unknown";
138b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen}
139b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
140f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
141799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
142f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    const hw_module_t *mod;
143799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc;
144799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
145f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
146f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
147f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
148f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
149799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
150f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
151f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    rc = audio_hw_device_open(mod, dev);
152f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
153f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
154f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    if (rc) {
155799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        goto out;
156f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
1575806b3533437e3d83208e8e9d6bd74ed304e51ecEric Laurent    if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
158f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
159f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        rc = BAD_VALUE;
160f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent        goto out;
161f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent    }
162799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return 0;
163799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
164799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout:
165799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    *dev = NULL;
166799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return rc;
167799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
168799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
16965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger()
17265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    : BnAudioFlinger(),
1734ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mPrimaryHardwareDev(NULL),
1749ea65d0f4a564478343b1a722fae4ce5883670c3Glenn Kasten      mAudioHwDevs(NULL),
1757d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten      mHardwareStatus(AUDIO_HW_IDLE),
1764ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterVolume(1.0f),
1774ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMasterMute(false),
1784ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mNextUniqueId(1),
1794ff14bae91075eb274eb1c2975982358946e7e63John Grossman      mMode(AUDIO_MODE_INVALID),
1804182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mBtNrecIsOff(false),
1814182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten      mIsLowRamDevice(true),
182813e2a74853bde19e37d878c596a044b3f299efcEric Laurent      mIsDeviceTypeKnown(false),
1834ea00a25cf85877b48ebd1e15a657cfaab29af58Glenn Kasten      mGlobalEffectEnableTime(0),
1844ea00a25cf85877b48ebd1e15a657cfaab29af58Glenn Kasten      mPrimaryOutputSampleRate(0)
18565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
186949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten    getpid_cached = getpid();
1879e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    char value[PROPERTY_VALUE_MAX];
1889e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
1899e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (doLog) {
190b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten        mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
191b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten                MemoryHeapBase::READ_ONLY);
1929e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
1931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
19446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
195da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    (void) property_get("ro.debuggable", value, "0");
196da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int debuggable = atoi(value);
197da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    int teeEnabled = 0;
198da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    if (debuggable) {
199da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        (void) property_get("af.tee", value, "0");
200da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teeEnabled = atoi(value);
201da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten    }
202f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten    // FIXME symbolic constants here
2036e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 1) {
204da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkInputEnabled = true;
2056e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2066e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 2) {
207da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkOutputEnabled = true;
2086e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2096e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (teeEnabled & 4) {
210da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        mTeeSinkTrackEnabled = true;
2116e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
21246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2135a61d2f277af3098fc10b2881babca16391362daDima Zavin}
2145a61d2f277af3098fc10b2881babca16391362daDima Zavin
2155a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef()
2165a61d2f277af3098fc10b2881babca16391362daDima Zavin{
217799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    int rc = 0;
218fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
219935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    Mutex::Autolock _l(mLock);
220935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
221799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    /* TODO: move all this work into an Init() function */
2224ff14bae91075eb274eb1c2975982358946e7e63John Grossman    char val_str[PROPERTY_VALUE_MAX] = { 0 };
2234ff14bae91075eb274eb1c2975982358946e7e63John Grossman    if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
2244ff14bae91075eb274eb1c2975982358946e7e63John Grossman        uint32_t int_val;
2254ff14bae91075eb274eb1c2975982358946e7e63John Grossman        if (1 == sscanf(val_str, "%u", &int_val)) {
2264ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = milliseconds(int_val);
2274ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using %u mSec as standby time.", int_val);
2284ff14bae91075eb274eb1c2975982358946e7e63John Grossman        } else {
2294ff14bae91075eb274eb1c2975982358946e7e63John Grossman            mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
2304ff14bae91075eb274eb1c2975982358946e7e63John Grossman            ALOGI("Using default %u mSec as standby time.",
2314ff14bae91075eb274eb1c2975982358946e7e63John Grossman                    (uint32_t)(mStandbyTimeInNsecs / 1000000));
2324ff14bae91075eb274eb1c2975982358946e7e63John Grossman        }
2334ff14bae91075eb274eb1c2975982358946e7e63John Grossman    }
23465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2351c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    mPatchPanel = new PatchPanel(this);
2361c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
237a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    mMode = AUDIO_MODE_NORMAL;
23865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
23965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
24065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger()
24165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
24265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mRecordThreads.isEmpty()) {
243c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeInput_nonvirtual() will remove specified entry from mRecordThreads
244d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeInput_nonvirtual(mRecordThreads.keyAt(0));
24565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
24665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!mPlaybackThreads.isEmpty()) {
247c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten        // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
248d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten        closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
24965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
250799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin
2512b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
2522b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten        // no mHardwareLock needed, as there are no other references to this
253a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
254a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        delete mAudioHwDevs.valueAt(i);
25565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
256481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
257481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Tell media.log service about any old writers that still need to be unregistered
258481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
259481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder != 0) {
260481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
261481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
262481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
263481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mUnregisteredWriters.pop();
264481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            mediaLogService->unregisterWriter(iMemory);
265481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
266481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
267481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten
26865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
26965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
270a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = {
271a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_PRIMARY,
272a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_A2DP,
273a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    AUDIO_HARDWARE_MODULE_ID_USB,
274a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent};
275a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
276a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
277062e67a26e0553dd142be622821f493df541f0c6Phil BurkAudioHwDevice* AudioFlinger::findSuitableHwDev_l(
278ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_module_handle_t module,
279ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_devices_t devices)
280799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{
281a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // if module is 0, the request comes from an old policy manager and we should load
282a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    // well known modules
283a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    if (module == 0) {
284a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
285a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
286a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            loadHwModule_l(audio_interfaces[i]);
287a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
288f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        // then try to find a module supporting the requested device.
289f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
290f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
291f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            audio_hw_device_t *dev = audioHwDevice->hwDevice();
292f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent            if ((dev->get_supported_devices != NULL) &&
293f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    (dev->get_supported_devices(dev) & devices) == devices)
294f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                return audioHwDevice;
295f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent        }
296a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    } else {
297a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        // check a match for the requested module handle
298ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
299ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (audioHwDevice != NULL) {
300ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            return audioHwDevice;
301a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        }
302a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    }
303a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
304799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin    return NULL;
305799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin}
30665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3070f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
30865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
30965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
31065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
31165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
31265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
31365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("Clients:\n");
31465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < mClients.size(); ++i) {
31577c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        sp<Client> client = mClients.valueAt(i).promote();
31677c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten        if (client != 0) {
31777c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            snprintf(buffer, SIZE, "  pid: %d\n", client->pid());
31877c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten            result.append(buffer);
31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
3213a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
322d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    result.append("Notification Clients:\n");
323d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); ++i) {
324d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        snprintf(buffer, SIZE, "  pid: %d\n", mNotificationClients.keyAt(i));
325d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent        result.append(buffer);
326d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
327d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
3283a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    result.append("Global session refs:\n");
329b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    result.append("  session   pid count\n");
3303a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
3313a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *r = mAudioSessionRefs[i];
332b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
3333a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        result.append(buffer);
3343a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
33565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
33665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3390f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
344a4454b4765c5905f14186893b0688be375642283Glenn Kasten    hardware_call_state hardwareStatus = mHardwareStatus;
34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3464ff14bae91075eb274eb1c2975982358946e7e63John Grossman    snprintf(buffer, SIZE, "Hardware status: %d\n"
3474ff14bae91075eb274eb1c2975982358946e7e63John Grossman                           "Standby Time mSec: %u\n",
3484ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            hardwareStatus,
3494ff14bae91075eb274eb1c2975982358946e7e63John Grossman                            (uint32_t)(mStandbyTimeInNsecs / 1000000));
35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3540f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioFlinger from pid=%d, uid=%d\n",
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex)
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3757dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten        usleep(kDumpLockSleepUs);
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args)
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
38244deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd, args);
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // get state of hardware lock
38681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool hardwareLocked = dumpTryLock(mHardwareLock);
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!hardwareLocked) {
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kHardwareLockedString);
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mHardwareLock.unlock();
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool locked = dumpTryLock(mLock);
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // failed to lock - AudioFlinger is probably deadlocked
39765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
39865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
39965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
402021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        bool clientLocked = dumpTryLock(mClientLock);
403021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (!clientLocked) {
404021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            String8 result(kClientLockedString);
405021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            write(fd, result.string(), result.size());
406021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
407d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen
408d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen        EffectDumpEffects(fd);
409d89eaddd1544dc9f6665e2578583e8083cac00daMarco Nelissen
41065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpClients(fd, args);
411021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (clientLocked) {
412021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mClientLock.unlock();
413021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
414021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent
41565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd, args);
41665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump playback threads
41865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
41965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mPlaybackThreads.valueAt(i)->dump(fd, args);
42065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
42165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // dump record threads
42365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
42465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mRecordThreads.valueAt(i)->dump(fd, args);
42565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
42665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
427aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        // dump orphan effect chains
428aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        if (mOrphanEffectChains.size() != 0) {
429aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            write(fd, "  Orphan Effect Chains\n", strlen("  Orphan Effect Chains\n"));
430aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
431aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                mOrphanEffectChains.valueAt(i)->dump(fd, args);
432aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
433aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
434799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        // dump all hardware devs
435799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
436a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
437799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            dev->dump(dev, fd);
43865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
439d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
44046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
441d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        // dump the serially shared record tee sink
442d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        if (mRecordTeeSource != 0) {
443d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten            dumpTee(fd, mRecordTeeSource);
444d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        }
44546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
446d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
447d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        if (locked) {
448d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            mLock.unlock();
449d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten        }
4509e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
4519e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // append a copy of media.log here by forwarding fd to it, but don't attempt
4529e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        // to lookup the service if it's not running, as it will block for a second
4539e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        if (mLogMemoryDealer != 0) {
4549e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
4559e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (binder != 0) {
4568b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "\nmedia.log:\n");
4579e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                Vector<String16> args;
4589e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                binder->dump(fd, args);
4599e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
4609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        }
46165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
46265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
46365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
46465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
465021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentsp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
46698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{
467021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _cl(mClientLock);
46898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // If pid is already in the mClients wp<> map, then use that entry
46998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    // (for which promote() is always != 0), otherwise create a new entry and Client.
47098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    sp<Client> client = mClients.valueFor(pid).promote();
47198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    if (client == 0) {
47298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        client = new Client(this, pid);
47398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten        mClients.add(pid, client);
47498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    }
47598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten
47698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten    return client;
47798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten}
47865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4799e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
4809e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
481481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If there is no memory allocated for logs, return a dummy writer that does nothing
4829e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (mLogMemoryDealer == 0) {
4839e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return new NBLog::Writer();
4849e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
4859e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
486481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Similarly if we can't contact the media.log service, also return a dummy writer
487481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (binder == 0) {
488481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
4899e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
490481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
491481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
492481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // If allocation fails, consult the vector of previously unregistered writers
493481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // and garbage-collect one or more them until an allocation succeeds
494481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    if (shared == 0) {
495481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        Mutex::Autolock _l(mUnregisteredWritersLock);
496481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
497481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            {
498481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Pick the oldest stale writer to garbage-collect
499481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
500481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mUnregisteredWriters.removeAt(0);
501481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                mediaLogService->unregisterWriter(iMemory);
502481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // Now the media.log remote reference to IMemory is gone.  When our last local
503481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // reference to IMemory also drops to zero at end of this block,
504481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                // the IMemory destructor will deallocate the region from mLogMemoryDealer.
505481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
506481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            // Re-attempt the allocation
507481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
508481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            if (shared != 0) {
509481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten                goto success;
510481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten            }
511481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        }
512481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // Even after garbage-collecting all old writers, there is still not enough memory,
513481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        // so return a dummy writer
514481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten        return new NBLog::Writer();
515481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    }
516481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kastensuccess:
517481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mediaLogService->registerWriter(shared, size, name);
518481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    return new NBLog::Writer(size, shared);
5199e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5209e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
5219e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
5229e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{
523685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    if (writer == 0) {
524685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten        return;
525685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten    }
5269e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<IMemory> iMemory(writer->getIMemory());
5279e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    if (iMemory == 0) {
5289e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        return;
5299e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    }
530481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // Rather than removing the writer immediately, append it to a queue of old writers to
531481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    // be garbage-collected later.  This allows us to continue to view old logs for a while.
532481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    Mutex::Autolock _l(mUnregisteredWritersLock);
533481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mUnregisteredWriters.push(writer);
5349e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten}
5359e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
53665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface
53765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
53865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
53965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack(
540fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t streamType,
54165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t sampleRate,
54258f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        audio_format_t format,
543254af180475346b6186b49c297f340c9c4817511Glenn Kasten        audio_channel_mask_t channelMask,
54474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
545e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten        IAudioFlinger::track_flags_t *flags,
54665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        const sp<IMemory>& sharedBuffer,
54772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output,
5483acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten        pid_t tid,
54965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int *sessionId,
550462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int clientUid,
55165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status_t *status)
55265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
55365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<PlaybackThread::Track> track;
55465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<TrackHandle> trackHandle;
55565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<Client> client;
55665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t lStatus;
55765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int lSessionId;
55865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
559263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
560263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    // but if someone uses binder directly they could bypass that and cause us to crash
561263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
56229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("createTrack() invalid stream type %d", streamType);
56365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        lStatus = BAD_VALUE;
56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        goto Exit;
56565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
56665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
56753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createTrack_l() depending on the thread type
56853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
56953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid sample rate %u", sampleRate);
57053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
57153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
57253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
57353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
57453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createTrack_l() depending on the thread type
57553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_output_channel(channelMask)) {
57653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("createTrack() invalid channel mask %#x", channelMask);
57753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
57853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
57953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
58053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
581c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    // further format checks are performed by createTrack_l() depending on the thread type
582c4b88a8d0f524666bf0f390075c334d047a104f2Glenn Kasten    if (!audio_is_valid_format(format)) {
583cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("createTrack() invalid format %#x", format);
58460a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        lStatus = BAD_VALUE;
585663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        goto Exit;
586663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    }
587663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten
588663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten    if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
589663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
590663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten        lStatus = BAD_VALUE;
59160a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten        goto Exit;
59260a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten    }
59360a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten
59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
59565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
59665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
59765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
598c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten            ALOGE("no playback thread found for output handle %d", output);
59965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            lStatus = BAD_VALUE;
60065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            goto Exit;
60165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6038d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
604021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
606e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        PlaybackThread *effectThread = NULL;
607aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
608570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            lSessionId = *sessionId;
609f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // check if an effect chain with the same session ID is present on another
610f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent            // output thread and move it here.
611de070137f11d346fba77605bd76a44c040a618fcEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
61239e94f8f723d445447fdee0822291e664b631f60Eric Laurent                sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
61339e94f8f723d445447fdee0822291e664b631f60Eric Laurent                if (mPlaybackThreads.keyAt(i) != output) {
614570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten                    uint32_t sessions = t->hasAudioSession(lSessionId);
61539e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    if (sessions & PlaybackThread::EFFECT_SESSION) {
61639e94f8f723d445447fdee0822291e664b631f60Eric Laurent                        effectThread = t.get();
617f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent                        break;
61839e94f8f723d445447fdee0822291e664b631f60Eric Laurent                    }
619de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
620de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } else {
622de070137f11d346fba77605bd76a44c040a618fcEric Laurent            // if no audio session id is provided, create one here
6237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            lSessionId = nextUniqueId();
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (sessionId != NULL) {
62565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                *sessionId = lSessionId;
62665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
62765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
6283856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("createTrack() lSessionId: %d", lSessionId);
62965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track = thread->createTrack_l(client, streamType, sampleRate, format,
631462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
63203e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
6332fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten        // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
63439e94f8f723d445447fdee0822291e664b631f60Eric Laurent
63539e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // move effect chain to this output thread if an effect on same session was waiting
63639e94f8f723d445447fdee0822291e664b631f60Eric Laurent        // for a track to be created
63739e94f8f723d445447fdee0822291e664b631f60Eric Laurent        if (lStatus == NO_ERROR && effectThread != NULL) {
6382fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten            // no risk of deadlock because AudioFlinger::mLock is held
63939e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _dl(thread->mLock);
64039e94f8f723d445447fdee0822291e664b631f60Eric Laurent            Mutex::Autolock _sl(effectThread->mLock);
64139e94f8f723d445447fdee0822291e664b631f60Eric Laurent            moveEffectChain_l(lSessionId, effectThread, thread, true);
64239e94f8f723d445447fdee0822291e664b631f60Eric Laurent        }
643a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
644a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        // Look for sync events awaiting for a session to be used.
6453ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn        for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
646a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
647a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
6482986460984580833161bdaabc7f17da1005a8961Eric Laurent                    if (lStatus == NO_ERROR) {
649d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten                        (void) track->setSyncEvent(mPendingSyncEvents[i]);
6502986460984580833161bdaabc7f17da1005a8961Eric Laurent                    } else {
6512986460984580833161bdaabc7f17da1005a8961Eric Laurent                        mPendingSyncEvents[i]->cancel();
6522986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
653a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    mPendingSyncEvents.removeAt(i);
654a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                    i--;
655a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent                }
656a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent            }
657a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
658e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
659fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
661e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
6623ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    if (lStatus != NO_ERROR) {
6633ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        // remove local strong reference to Client before deleting the Track so that the
664021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
665fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // Don't hold mClientLock when releasing the reference on the track as the
666fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // destructor will acquire it.
667fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        {
668fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
669fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
670fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
67165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        track.clear();
6723ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten        goto Exit;
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6753ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    // return handle to client
6763ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten    trackHandle = new TrackHandle(track);
6773ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten
67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit:
6799156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
68065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return trackHandle;
68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
68372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
68765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6885ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("sampleRate() unknown thread %d", output);
68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
69065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->sampleRate();
69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
69365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
69472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(audio_io_handle_t output) const
69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
69665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
69865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
6995ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("format() unknown thread %d", output);
70058f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        return AUDIO_FORMAT_INVALID;
70165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->format();
70365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
70465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
70572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastensize_t AudioFlinger::frameCount(audio_io_handle_t output) const
70665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
70765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
7105ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("frameCount() unknown thread %d", output);
71165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
71265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
71358912562617941964939a4182cda71eaeb153d4bGlenn Kasten    // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
71458912562617941964939a4182cda71eaeb153d4bGlenn Kasten    //       should examine all callers and fix them to handle smaller counts
71565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->frameCount();
71665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
71765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
71872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const
71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
72165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = checkPlaybackThread_l(output);
72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
723c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten        ALOGW("latency(): no playback thread found for output handle %d", output);
72465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
72565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
72665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return thread->latency();
72765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
72865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
72965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value)
73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
731a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
732a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
733a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
734a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
735a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
73665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
73765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
73865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
73965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
74065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
741a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
742ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterVolume = value;
743a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
744ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master volume in the HALs which support it.
745ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
746ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
747ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
7484ff14bae91075eb274eb1c2975982358946e7e63John Grossman
749ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
750ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterVolume()) {
751ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
752935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent        }
753ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
75465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
756ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master volume in each playback thread.  Playback threads
757ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master volume support will apply
758ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // master volume during the mix operation.  Threads with HALs which do
759ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // support master volume will simply ignore the setting.
7608d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
761ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterVolume(value);
76265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
76365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
76465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
766f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode)
76765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
768a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
769a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
770a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
771a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
77265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
77465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
77565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
77665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
777930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(mode) >= AUDIO_MODE_CNT) {
7785ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("Illegal value: setMode(%d)", mode);
77965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
78165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    { // scope for the lock
78365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex lock(mHardwareLock);
784ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
78565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_SET_MODE;
786ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        ret = dev->set_mode(dev, mode);
78765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mHardwareStatus = AUDIO_HW_IDLE;
78865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
78965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (NO_ERROR == ret) {
79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
79265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mMode = mode;
7938d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++)
794e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setMode(mode);
79565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
79665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
79865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
79965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state)
80165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
802a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
803a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
804a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
805a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
806a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
80765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
80865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
80965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
81065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
81165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
81265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
81365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
8142f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
8152f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
8162f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        status_t result = dev->set_mic_mute(dev, state);
8172f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        if (result != NO_ERROR) {
8182f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent            ret = result;
8192f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent        }
8202f035f59d1e28728d38d18a7f0f7a9c6e8b0c11bEric Laurent    }
82165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
82265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
82365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
82465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
82565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const
82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
827a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
828a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
829a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return false;
830a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
8313e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    bool mute = true;
832fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    bool state = AUDIO_MODE_INVALID;
8332b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
83465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
8353e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
8363e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
8373e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        status_t result = dev->get_mic_mute(dev, &state);
8383e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        if (result == NO_ERROR) {
8393e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia            mute = mute && state;
8403e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia        }
8413e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    }
84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
8433e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia
8443e91b5d11866599e8f33e63fb3b6c1d81ce1a1d2Ricardo Garcia    return mute;
84565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
84665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted)
84865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
849d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    status_t ret = initCheck();
850d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    if (ret != NO_ERROR) {
851d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        return ret;
852d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
853d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
85665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
85765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
85865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
859ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    Mutex::Autolock _l(mLock);
860ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    mMasterMute = muted;
861d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
862ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Set master mute in the HALs which support it.
863ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
864ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AutoMutex lock(mHardwareLock);
865ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
866d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
867ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
868ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        if (dev->canSetMasterMute()) {
869ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman            dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
870d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman        }
871ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mHardwareStatus = AUDIO_HW_IDLE;
872d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman    }
873d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
874ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // Now set the master mute in each playback thread.  Playback threads
875ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // assigned to HALs which do not have master mute support will apply master
876ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute during the mix operation.  Threads with HALs which do support master
877ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    // mute will simply ignore the setting.
8788d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
879ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman        mPlaybackThreads.valueAt(i)->setMasterMute(muted);
88065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
88365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const
88565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8869806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8879806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterVolume_l();
88865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
88965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
89065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const
89165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8929806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    Mutex::Autolock _l(mLock);
8939806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten    return masterMute_l();
89465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
89565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8964ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const
8974ff14bae91075eb274eb1c2975982358946e7e63John Grossman{
8984ff14bae91075eb274eb1c2975982358946e7e63John Grossman    return mMasterVolume;
8994ff14bae91075eb274eb1c2975982358946e7e63John Grossman}
9004ff14bae91075eb274eb1c2975982358946e7e63John Grossman
901d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const
902d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{
903ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    return mMasterMute;
904d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman}
905d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman
906223fd5c9738e9665e495904d37d4632414b68c1eEric Laurentstatus_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
907223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent{
908223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
909223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        ALOGW("setStreamVolume() invalid stream %d", stream);
910223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return BAD_VALUE;
911223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
912223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
913223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
914223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
915223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return PERMISSION_DENIED;
916223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
917223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
918223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    return NO_ERROR;
919223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent}
920223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
92172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
92272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output)
92365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
92465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
92565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
92665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
92765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
92865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
929223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
930223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
931223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return status;
93265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
933223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
93465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
93565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
93665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *thread = NULL;
937142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
93865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(output);
93965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
94065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return BAD_VALUE;
94165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
94265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
94365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
94465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].volume = value;
94565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (thread == NULL) {
9478d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
948e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
94965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
95165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread->setStreamVolume(stream, value);
95265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
95365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
95565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
95665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
957fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
95865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
95965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
96065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
96165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
96265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
96365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
964223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
965223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
966223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        return status;
967223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    }
968223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
969223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent
970223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
97129357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("setStreamMute() invalid stream %d", stream);
97265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
97365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
97465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
975935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    AutoMutex lock(mLock);
97665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mStreamTypes[stream].mute = muted;
9773ab368e0810d894dcbc0971350c095049478a055Mark Salyzyn    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
978e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
97965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
98065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
98165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
98265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
98372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
98465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
985223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
986223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
98765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0.0f;
98865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
98965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
99065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mLock);
99165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    float volume;
992142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (output != AUDIO_IO_HANDLE_NONE) {
99365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        PlaybackThread *thread = checkPlaybackThread_l(output);
99465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (thread == NULL) {
99565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return 0.0f;
99665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
99765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        volume = thread->streamVolume(stream);
99865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
9996637baae4244aec731c4014da72418d330636ae1Glenn Kasten        volume = streamVolume_l(stream);
100065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
100165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
100265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return volume;
100365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
100465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1005fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const
100665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1007223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    status_t status = checkStreamType(stream);
1008223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    if (status != NO_ERROR) {
100965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return true;
101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
101165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
10126637baae4244aec731c4014da72418d330636ae1Glenn Kasten    AutoMutex lock(mLock);
10136637baae4244aec731c4014da72418d330636ae1Glenn Kasten    return streamMute_l(stream);
101465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
101565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
101672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
101765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1018827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1019827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
102265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
102365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
102465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
102565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1026142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1027142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
1028a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent        Mutex::Autolock _l(mLock);
1029799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        status_t final_result = NO_ERROR;
10308abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        {
1031a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            AutoMutex lock(mHardwareLock);
1032a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1033a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1034a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1035a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                status_t result = dev->set_parameters(dev, keyValuePairs.string());
1036a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                final_result = result ?: final_result;
1037a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            }
1038a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
10398abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten        }
104059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
104159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        AudioParameter param = AudioParameter(keyValuePairs);
104259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        String8 value;
104359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
1044bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1045bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent            if (mBtNrecIsOff != btNrecIsOff) {
104659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
104759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    sp<RecordThread> thread = mRecordThreads.valueAt(i);
1048f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent                    audio_devices_t device = thread->inDevice();
1049510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1050510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // collect all of the thread's session IDs
1051510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    KeyedVector<int, bool> ids = thread->sessionIds();
1052510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    // suspend effects associated with those session IDs
1053510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                    for (size_t j = 0; j < ids.size(); ++j) {
1054510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                        int sessionId = ids.keyAt(j);
105559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_AEC,
105659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1057510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
105859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                        thread->setEffectSuspended(FX_IID_NS,
105959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                                                   suspend,
1060510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten                                                   sessionId);
106159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                    }
106259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent                }
1063bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent                mBtNrecIsOff = btNrecIsOff;
106459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent            }
106559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent        }
106628ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        String8 screenState;
106728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
106828ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            bool isOff = screenState == "off";
106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isOff != (AudioFlinger::mScreenState & 1)) {
107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
107128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten            }
107228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten        }
1073799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        return final_result;
107465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
107565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
107665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // hold a strong ref on thread in case closeOutput() or closeInput() is called
107765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // and the thread is exited once the lock is released
107865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    sp<ThreadBase> thread;
107965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
108065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        Mutex::Autolock _l(mLock);
108165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        thread = checkPlaybackThread_l(ioHandle);
1082d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten        if (thread == 0) {
108365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            thread = checkRecordThread_l(ioHandle);
10847fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten        } else if (thread == primaryPlaybackThread_l()) {
10857c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // indicate output device change to all input threads for pre processing
10867c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            AudioParameter param = AudioParameter(keyValuePairs);
10877c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int value;
108889d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent            if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
108989d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent                    (value != 0)) {
10907c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
10917c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                    mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
10927c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                }
10937c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
109465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
109565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
10967378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (thread != 0) {
10977378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten        return thread->setParameters(keyValuePairs);
109865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
109965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
110065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
110165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
110272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
110365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1104827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1105827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten            ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
110665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1107a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent    Mutex::Autolock _l(mLock);
1108a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent
1109142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten    if (ioHandle == AUDIO_IO_HANDLE_NONE) {
1110fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        String8 out_s8;
1111fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1112799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
11138abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            char *s;
11148abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            {
11158abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            AutoMutex lock(mHardwareLock);
11168abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_GET_PARAMETER;
1117a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent            audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
11188abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            s = dev->get_parameters(dev, keys.string());
11198abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            mHardwareStatus = AUDIO_HW_IDLE;
11208abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten            }
1121ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman            out_s8 += String8(s ? s : "");
1122799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin            free(s);
1123799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin        }
1124fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return out_s8;
112565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
112665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
112765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
112865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
112965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getParameters(keys);
113065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
113165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
113265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
113365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getParameters(keys);
113465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
113565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return String8("");
113665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
113765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1138dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1139dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten        audio_channel_mask_t channelMask) const
114065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1141a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1142a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1143a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return 0;
1144a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
11456770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
11466770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        return 0;
11476770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
1148a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
11492b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    AutoMutex lock(mHardwareLock);
11502b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
11516770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    audio_config_t config, proposed;
11526770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    memset(&proposed, 0, sizeof(proposed));
11536770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.sample_rate = sampleRate;
11546770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.channel_mask = channelMask;
11556770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    proposed.format = format;
1156ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald
1157ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
11586770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    size_t frames;
11596770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    for (;;) {
11606770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // Note: config is currently a const parameter for get_input_buffer_size()
11616770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // but we use a copy from proposed in case config changes from the call.
11626770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        config = proposed;
11636770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        frames = dev->get_input_buffer_size(dev, &config);
11646770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        if (frames != 0) {
11656770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            break; // hal success, config is the result
11666770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        }
11676770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // change one parameter of the configuration each iteration to a more "common" value
11686770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        // to see if the device will support it.
11696770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
11706770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            proposed.format = AUDIO_FORMAT_PCM_16_BIT;
11716770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
11726770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            proposed.sample_rate = 44100;           // legacy AudioRecord.java. TODO: Query hw?
11736770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        } else {
11746770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
11756770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                    "format %#x, channelMask 0x%X",
11766770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung                    sampleRate, format, channelMask);
11776770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung            break; // retries failed, break out of loop with frames == 0.
11786770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        }
11796770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
11802b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten    mHardwareStatus = AUDIO_HW_IDLE;
11816770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    if (frames > 0 && config.sample_rate != sampleRate) {
11826770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
11836770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    }
11846770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    return frames; // may be converted to bytes at the Java level.
118565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
118665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
11875f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
118865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
118965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
119065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
119165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    RecordThread *recordThread = checkRecordThread_l(ioHandle);
119265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (recordThread != NULL) {
119365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return recordThread->getInputFramesLost();
119465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
119565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return 0;
119665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
119765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
119865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value)
119965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1200a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    status_t ret = initCheck();
1201a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    if (ret != NO_ERROR) {
1202a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent        return ret;
1203a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent    }
1204a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent
120565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check calling permissions
120665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!settingsAllowed()) {
120765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
120865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
120965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
121065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AutoMutex lock(mHardwareLock);
1211ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
12128abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten    mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
1213ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    ret = dev->set_voice_volume(dev, value);
121465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mHardwareStatus = AUDIO_HW_IDLE;
121565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
121665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return ret;
121765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
121865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1219377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
122072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten        audio_io_handle_t output) const
122165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
122265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status;
122365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
122465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
122565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
122665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    PlaybackThread *playbackThread = checkPlaybackThread_l(output);
122765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (playbackThread != NULL) {
122865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return playbackThread->getRenderPosition(halFrames, dspFrames);
122965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
123065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
123165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BAD_VALUE;
123265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
123365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
123465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
123565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
123665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
123744622db93a24219123facbbe14259000f90de728Eric Laurent    if (client == 0) {
123844622db93a24219123facbbe14259000f90de728Eric Laurent        return;
123944622db93a24219123facbbe14259000f90de728Eric Laurent    }
1240021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    bool clientAdded = false;
1241021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1242021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
124365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1244021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        pid_t pid = IPCThreadState::self()->getCallingPid();
1245021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(pid) < 0) {
1246021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            sp<NotificationClient> notificationClient = new NotificationClient(this,
1247021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                client,
1248021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                                                                                pid);
1249021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
125065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1251021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            mNotificationClients.add(pid, notificationClient);
125265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
125306b46062d2f8bc82ca3061a23d197734ae51918bMarco Nelissen            sp<IBinder> binder = IInterface::asBinder(client);
1254021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            binder->linkToDeath(notificationClient);
1255021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            clientAdded = true;
1256021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
1257021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
125865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1259021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
1260fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent    // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
1261021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    if (clientAdded) {
126265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // the config change is always sent from playback or record threads to avoid deadlock
126365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // with AudioSystem::gLock
126465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1265896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
126665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
126765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
126865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t i = 0; i < mRecordThreads.size(); i++) {
1269896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent            mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
127065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
127165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
127265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
127365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
127465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid)
127565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
127665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
1277021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
1278021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
1279021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        mNotificationClients.removeItem(pid);
1280021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    }
12813a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen
12823856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("%d died, releasing its sessions", pid);
12838d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    size_t num = mAudioSessionRefs.size();
12843a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    bool removed = false;
12858d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i< num; ) {
12863a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1287012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        ALOGV(" pid %d @ %d", ref->mPid, i);
1288012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten        if (ref->mPid == pid) {
1289012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten            ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
12903a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            mAudioSessionRefs.removeAt(i);
12913a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            delete ref;
12923a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            removed = true;
12933a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen            num--;
12948d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten        } else {
12958d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten            i++;
12963a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        }
12973a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
12983a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    if (removed) {
12993a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen        purgeStaleEffects_l();
13003a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen    }
130165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
130265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1303021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
130465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1305021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    Mutex::Autolock _l(mClientLock);
1306021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    size_t size = mNotificationClients.size();
130765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t i = 0; i < size; i++) {
1308021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
13091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                                              ioHandle,
13101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                                              param2);
131165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
131265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
131365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1314021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// removeClient_l() must be called with AudioFlinger::mClientLock held
131565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid)
131665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
1317827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten    ALOGV("removeClient_l() pid %d, calling pid %d", pid,
131885ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten            IPCThreadState::self()->getCallingPid());
131965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mClients.removeItem(pid);
132065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
132165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1322717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held
1323717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1324717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{
1325717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    sp<PlaybackThread> thread;
1326717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1327717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1328717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1329717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            ALOG_ASSERT(thread == 0);
1330717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent            thread = mPlaybackThreads.valueAt(i);
1331717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent        }
1332717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    }
1333717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent
1334717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent    return thread;
1335717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent}
133665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
133781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
133881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
133965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
134065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
134181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   RefBase(),
134381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger(audioFlinger),
134481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPid(pid),
134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTimedTrackCount(0)
134865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
135065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
135165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1352021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent// Client destructor must be called with AudioFlinger::mClientLock held
135381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client()
135465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeClient_l(mPid);
135665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
135765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const
135965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mMemoryDealer;
136165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
136265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Reserve one of the limited slots for a timed audio track associated
136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// with this client
136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::Client::reserveTimedTrack()
136665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int kMaxTimedTracksPerClient = 4;
136865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
137065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("can not create timed track - pid %d has exceeded the limit",
137381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             mPid);
137481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return false;
137565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
137665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount++;
137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return true;
137965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
138065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Release a slot for a timed audio track
138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::Client::releaseTimedTrack()
138365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
138481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mTimedTrackLock);
138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTimedTrackCount--;
1386896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent}
1387896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent
138881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
139081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
139181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     const sp<IAudioFlingerClient>& client,
139281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                     pid_t pid)
139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
1394896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{
139565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
139665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
139781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient()
139865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
139965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
140065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
14010f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
140265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
140381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NotificationClient> keep(this);
140481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioFlinger->removeNotificationClient(mPid);
140581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
140665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
140765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
140965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1410893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brownstatic bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1411893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown    return audio_is_remote_submix_device(inDevice);
1412893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown}
1413893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown
141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord(
141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t input,
141681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
141881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
141974935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *frameCount,
1420eeca32671896739e84050da5992d5f151a1629deGlenn Kasten        IAudioFlinger::track_flags_t *flags,
142181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *sessionId,
14237df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten        size_t *notificationFrames,
1424d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& cblk,
1425d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten        sp<IMemory>& buffers,
142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
14271d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{
142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread::RecordTrack> recordTrack;
142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordHandle> recordHandle;
143081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Client> client;
143181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
143281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int lSessionId;
14331d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
1434d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk.clear();
1435d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers.clear();
1436d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check calling permissions
143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!recordingAllowed()) {
1439e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten        ALOGE("openRecord() permission denied: recording not allowed");
144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
14431d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
144453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further sample rate checks are performed by createRecordTrack_l()
144553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (sampleRate == 0) {
144653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid sample rate %u", sampleRate);
144753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
144853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
144953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
145053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
14516770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    // we don't yet support anything other than linear PCM
14526770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
1453cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("openRecord() invalid format %#x", format);
1454291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        lStatus = BAD_VALUE;
145553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        goto Exit;
145653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    }
145753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten
145853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    // further channel mask checks are performed by createRecordTrack_l()
145953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten    if (!audio_is_input_channel(channelMask)) {
146053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        ALOGE("openRecord() invalid channel mask %#x", channelMask);
146153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten        lStatus = BAD_VALUE;
1462291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten        goto Exit;
1463291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
1464291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten
146505997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten    {
146681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
1467e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten        RecordThread *thread = checkRecordThread_l(input);
146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
1469e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten            ALOGE("openRecord() checkRecordThread_l failed");
147081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
147181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
14721d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent        }
14731d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
14748d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten        pid_t pid = IPCThreadState::self()->getCallingPid();
1475021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        client = registerPid(pid);
1476feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1477aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten        if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = *sessionId;
1479feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        } else {
1480570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten            // if no audio session id is provided, create one here
148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lSessionId = nextUniqueId();
148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId != NULL) {
148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                *sessionId = lSessionId;
148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
1485feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent        }
1486aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
1487570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten
1488462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        // TODO: the uid should be passed in as a parameter to openRecord
148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
14907df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten                                                  frameCount, lSessionId, notificationFrames,
1491462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  IPCThreadState::self()->getCallingUid(),
1492462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                                  flags, tid, &lStatus);
149303e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George        LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
14941b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
14951b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        if (lStatus == NO_ERROR) {
14961b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // Check if one effect chain was awaiting for an AudioRecord to be created on this
14971b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // session and move it to this thread.
14981b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
14991b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (chain != 0) {
15001b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                Mutex::Autolock _l(thread->mLock);
15011b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                thread->addEffectChain_l(chain);
15021b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
15031b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        }
1504feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1505e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove local strong reference to Client before deleting the RecordTrack so that the
1508021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Client destructor is called by the TrackBase destructor with mClientLock held
1509fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // Don't hold mClientLock when releasing the reference on the track as the
1510fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        // destructor will acquire it.
1511fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        {
1512fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
1513fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
1514fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
151581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recordTrack.clear();
151681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
1517feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent    }
1518feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1519d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    cblk = recordTrack->getCblk();
1520d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten    buffers = recordTrack->getBuffers();
1521d776ac63ce9c013c9626226e43f7db606e035838Glenn Kasten
15222fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten    // return handle to client
152381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordHandle = new RecordHandle(recordTrack);
1524feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
152581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
15269156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
152781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return recordHandle;
1528feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent}
1529feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
1530feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent
15311d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent
153281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name)
153559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
153644622db93a24219123facbbe14259000f90de728Eric Laurent    if (name == NULL) {
153744622db93a24219123facbbe14259000f90de728Eric Laurent        return 0;
153844622db93a24219123facbbe14259000f90de728Eric Laurent    }
153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!settingsAllowed()) {
154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
154181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
154259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
154381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return loadHwModule_l(name);
154459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
154559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held
154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
154859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
154981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
155081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("loadHwModule() module %s already loaded", name);
155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mAudioHwDevs.keyAt(i);
155359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
155459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
155559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
155681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *dev;
155759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
155881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int rc = load_audio_interface(name, &dev);
155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() error %d loading module %s ", rc, name);
156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
156259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
156359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_INIT;
156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    rc = dev->init_check(dev);
156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (rc) {
156881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
157059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
157159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Check and cache this HAL's level of support for master mute and master
157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volume.  If this is the first HAL opened, and it supports the get
157481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // methods, use the initial values provided by the HAL as the current
157581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // master mute and volume settings.
157659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
157881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {  // scope for auto-lock pattern
157981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mHardwareLock);
158059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (0 == mAudioHwDevs.size()) {
158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
158381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_volume) {
158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float mv;
158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_volume(dev, &mv)) {
158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterVolume = mv;
158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
158881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (NULL != dev->get_master_mute) {
159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool mm;
159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (OK == dev->get_master_mute(dev, &mm)) {
159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMasterMute = mm;
159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
159681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
159759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_volume) &&
160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_volume(dev, mMasterVolume))) {
160281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
160459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent        }
160559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((NULL != dev->set_master_mute) &&
160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (OK == dev->set_master_mute(dev, mMasterMute))) {
160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            flags = static_cast<AudioHwDevice::Flags>(flags |
161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
161259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHardwareStatus = AUDIO_HW_IDLE;
161459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    }
161559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_module_handle_t handle = nextUniqueId();
161783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
162081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent          name, dev->common.module->name, dev->common.module->id, handle);
162181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
162381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
162459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
162559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
162681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
162781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
162881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate()
162959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{
163059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent    Mutex::Autolock _l(mLock);
163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->sampleRate() : 0;
1633a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent}
163459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount()
1636a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{
163781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL ? thread->frameCountHAL() : 0;
164059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent}
164159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent
164265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
164365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16444182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
16454182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{
16464182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    uid_t uid = IPCThreadState::self()->getCallingUid();
16474182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (uid != AID_SYSTEM) {
16484182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return PERMISSION_DENIED;
16494182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
16504182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    Mutex::Autolock _l(mLock);
16514182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    if (mIsDeviceTypeKnown) {
16524182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten        return INVALID_OPERATION;
16534182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    }
16544182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsLowRamDevice = isLowRamDevice;
16554182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    mIsDeviceTypeKnown = true;
16564182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    return NO_ERROR;
16574182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten}
16584182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
165993c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurentaudio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
166093c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent{
166193c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    Mutex::Autolock _l(mLock);
1662fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1663fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1664fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (index >= 0) {
1665fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1666fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent              mHwAvSyncIds.valueAt(index), sessionId);
1667fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return mHwAvSyncIds.valueAt(index);
1668fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1669fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1670fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1671fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (dev == NULL) {
1672fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return AUDIO_HW_SYNC_INVALID;
1673fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1674fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1675fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    AudioParameter param = AudioParameter(String8(reply));
1676fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    free(reply);
1677fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1678fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    int value;
1679fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1680fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1681fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        return AUDIO_HW_SYNC_INVALID;
1682fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1683fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1684fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    // allow only one session for a given HW A/V sync ID.
1685fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1686fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1687fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1688fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent                  value, mHwAvSyncIds.keyAt(i));
1689fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            mHwAvSyncIds.removeItemsAt(i);
1690fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            break;
1691fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        }
1692fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
1693fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1694fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    mHwAvSyncIds.add(sessionId, value);
1695fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
169693c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
169793c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1698fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        uint32_t sessions = thread->hasAudioSession(sessionId);
1699fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        if (sessions & PlaybackThread::TRACK_SESSION) {
1700fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            AudioParameter param = AudioParameter();
1701fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1702fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent            thread->setParameters(param.toString());
170393c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent            break;
170493c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent        }
170593c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent    }
1706fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1707fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1708fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    return (audio_hw_sync_t)value;
1709fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent}
1710fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
1711fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1712fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurentvoid AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1713fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent{
1714fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1715fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    if (index >= 0) {
1716fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1717fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1718fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        AudioParameter param = AudioParameter();
1719fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1720fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent        thread->setParameters(param.toString());
1721fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent    }
172293c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent}
172393c3d41bdb15e39dac0faea9c5b60f1637cd477cEric Laurent
1724fa90e84ed0cb2abdc0e0873a06d10ccf2b3c34f6Eric Laurent
17254182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ----------------------------------------------------------------------------
17264182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten
172765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
172883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
1729cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_io_handle_t *output,
1730cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_config_t *config,
1731cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            audio_devices_t devices,
1732cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                            const String8& address,
173383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                            audio_output_flags_t flags)
173483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
1735cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
17366e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (outHwDev == NULL) {
1737cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return 0;
17386e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
173965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
174081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
1741cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*output == AUDIO_IO_HANDLE_NONE) {
1742cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *output = nextUniqueId();
1743cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    }
174465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
174581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
174665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17476146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    // FOR TESTING ONLY:
17489a59276fb465e492138e0576523b54079671e8f4Andy Hung    // This if statement allows overriding the audio policy settings
17499a59276fb465e492138e0576523b54079671e8f4Andy Hung    // and forcing a specific format or channel mask to the HAL/Sink device for testing.
17509a59276fb465e492138e0576523b54079671e8f4Andy Hung    if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
17519a59276fb465e492138e0576523b54079671e8f4Andy Hung        // Check only for Normal Mixing mode
17529a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (kEnableExtendedPrecision) {
17539a59276fb465e492138e0576523b54079671e8f4Andy Hung            // Specify format (uncomment one below to choose)
17549a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_FLOAT;
17559a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
17569a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_32_BIT;
17579a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
1758cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
17599a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
17609a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (kEnableExtendedChannels) {
17619a59276fb465e492138e0576523b54079671e8f4Andy Hung            // Specify channel mask (uncomment one below to choose)
17629a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->channel_mask = audio_channel_out_mask_from_count(4);  // for USB 4ch
17639a59276fb465e492138e0576523b54079671e8f4Andy Hung            //config->channel_mask = audio_channel_mask_from_representation_and_bits(
17649a59276fb465e492138e0576523b54079671e8f4Andy Hung            //        AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1);  // another 4ch example
17659a59276fb465e492138e0576523b54079671e8f4Andy Hung        }
17666146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    }
17676146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung
1768062e67a26e0553dd142be622821f493df541f0c6Phil Burk    AudioStreamOut *outputStream = NULL;
1769062e67a26e0553dd142be622821f493df541f0c6Phil Burk    status_t status = outHwDev->openOutputStream(
1770062e67a26e0553dd142be622821f493df541f0c6Phil Burk            &outputStream,
1771062e67a26e0553dd142be622821f493df541f0c6Phil Burk            *output,
1772062e67a26e0553dd142be622821f493df541f0c6Phil Burk            devices,
1773062e67a26e0553dd142be622821f493df541f0c6Phil Burk            flags,
1774062e67a26e0553dd142be622821f493df541f0c6Phil Burk            config,
1775062e67a26e0553dd142be622821f493df541f0c6Phil Burk            address.string());
177665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
177781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mHardwareStatus = AUDIO_HW_IDLE;
177858912562617941964939a4182cda71eaeb153d4bGlenn Kasten
1779062e67a26e0553dd142be622821f493df541f0c6Phil Burk    if (status == NO_ERROR) {
178065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17813255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten        PlaybackThread *thread;
1782bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1783cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            thread = new OffloadThread(this, outputStream, *output, devices);
1784cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
17856146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
178683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                || !isValidPcmSinkFormat(config->format)
17879a59276fb465e492138e0576523b54079671e8f4Andy Hung                || !isValidPcmSinkChannelMask(config->channel_mask)) {
1788cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            thread = new DirectOutputThread(this, outputStream, *output, devices);
1789cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
179081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
1791cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            thread = new MixerThread(this, outputStream, *output, devices);
1792cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
179365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
1794cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        mPlaybackThreads.add(*output, thread);
179583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        return thread;
179683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
179783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
179883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    return 0;
179983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
180083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1801cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurentstatus_t AudioFlinger::openOutput(audio_module_handle_t module,
1802cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_io_handle_t *output,
1803cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_config_t *config,
1804cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_devices_t *devices,
1805cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  const String8& address,
1806cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  uint32_t *latencyMs,
1807cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  audio_output_flags_t flags)
180883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
1809062e67a26e0553dd142be622821f493df541f0c6Phil Burk    ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
181083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent              module,
1811cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              (devices != NULL) ? *devices : 0,
1812cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->sample_rate,
1813cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->format,
1814cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent              config->channel_mask,
181583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent              flags);
181683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1817cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*devices == AUDIO_DEVICE_NONE) {
1818cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return BAD_VALUE;
181983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
182083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
182183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
182283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1823cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
182483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (thread != 0) {
1825cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *latencyMs = thread->latency();
182665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // notify client processes of the new output creation
1828021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
182965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the first primary output opened designates the primary hw device
183181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
183281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGI("Using module %d has the primary audio interface", module);
183383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
183481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AutoMutex lock(mHardwareLock);
183681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_SET_MODE;
183783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHardwareStatus = AUDIO_HW_IDLE;
18394ea00a25cf85877b48ebd1e15a657cfaab29af58Glenn Kasten
1840cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            mPrimaryOutputSampleRate = config->sample_rate;
184181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1842cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return NO_ERROR;
184381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
184465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1845cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    return NO_INIT;
184665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
184765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
184881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t output2)
185065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
185181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread1 = checkMixerThread_l(output1);
185381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread *thread2 = checkMixerThread_l(output2);
185481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
185581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread1 == NULL || thread2 == NULL) {
185681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                output2);
1858142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        return AUDIO_IO_HANDLE_NONE;
185965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
186065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    audio_io_handle_t id = nextUniqueId();
186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
186381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->addOutputTrack(thread2);
186481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPlaybackThreads.add(id, thread);
186581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // notify client processes of the new output creation
1866021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return id;
186865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
186965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output)
18712bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{
187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeOutput_nonvirtual(output);
18732bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi}
18742bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi
187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
187665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the playback thread so that
187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<PlaybackThread> thread;
188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
188181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkPlaybackThread_l(output);
188381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
188565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
188665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeOutput() %d", output);
1888de070137f11d346fba77605bd76a44c040a618fcEric Laurent
188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread->type() == ThreadBase::MIXER) {
189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    DuplicatingThread *dupThread =
189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    dupThread->removeOutputTrack((MixerThread *)thread.get());
1895bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1896de070137f11d346fba77605bd76a44c040a618fcEric Laurent                }
1897de070137f11d346fba77605bd76a44c040a618fcEric Laurent            }
1898de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
1899bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1900bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
190181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPlaybackThreads.removeItem(output);
1902bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // save all effects to the default thread
1903bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mPlaybackThreads.size()) {
1904bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1905bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (dstThread != NULL) {
1906bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // audioflinger lock is held here so the acquisition order of thread locks does not
1907bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // matter
1908bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _dl(dstThread->mLock);
1909bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Mutex::Autolock _sl(thread->mLock);
1910bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1911bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
1912bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
1913bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
1914bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1915bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1916021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
19173acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten    }
191881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->exit();
191981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The thread entity (active unit of execution) is no longer running here,
192081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but the ThreadBase container still exists.
19213acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten
192281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread->type() != ThreadBase::DUPLICATING) {
192383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        closeOutputFinish(thread);
192465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
192583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
192765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
192865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
192983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
193083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
193183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    AudioStreamOut *out = thread->clearOutput();
193283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
193383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // from now on thread->mOutput is NULL
193483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    out->hwDev()->close_output_stream(out->hwDev(), out->stream);
193583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    delete out;
193683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
193783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
193883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
193983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
194083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mPlaybackThreads.removeItem(thread->mId);
194183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    thread->exit();
194283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeOutputFinish(thread);
194383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
194483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
194581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output)
1946e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{
194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
194881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1952e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent    }
1953e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
195481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("suspendOutput() %d", output);
195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->suspend();
1956e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent
195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
195865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
195965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output)
196165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
19626637baae4244aec731c4014da72418d330636ae1Glenn Kasten    Mutex::Autolock _l(mLock);
196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
196465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
196681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
1967ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman    }
196865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("restoreOutput() %d", output);
197065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
197181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    thread->restore();
197265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
197465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
197565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1976cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurentstatus_t AudioFlinger::openInput(audio_module_handle_t module,
1977cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_io_handle_t *input,
1978cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_config_t *config,
1979e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten                                          audio_devices_t *devices,
1980cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          const String8& address,
1981cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                          audio_source_t source,
1982ec40d284218466d8f0e832e7eb88e6ea6c479c88Glenn Kasten                                          audio_input_flags_t flags)
198365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
198483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
198583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1986e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    if (*devices == AUDIO_DEVICE_NONE) {
1987cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return BAD_VALUE;
198883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
198983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1990e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
199183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
199283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (thread != 0) {
199383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        // notify client processes of the new input creation
199483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
1995cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        return NO_ERROR;
1996b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent    }
1997cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    return NO_INIT;
199883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
1999b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent
200083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentsp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
2001cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_io_handle_t *input,
2002cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_config_t *config,
2003e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten                                                         audio_devices_t devices,
2004cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         const String8& address,
2005cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                                         audio_source_t source,
200683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                                                         audio_input_flags_t flags)
200783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
2008e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
20096e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (inHwDev == NULL) {
2010cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *input = AUDIO_IO_HANDLE_NONE;
201181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
20126e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
2013fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
2014cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    if (*input == AUDIO_IO_HANDLE_NONE) {
2015cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        *input = nextUniqueId();
2016cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    }
2017b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
2018cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_config_t halconfig = *config;
2019cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    audio_hw_device_t *inHwHal = inHwDev->hwDevice();
20203255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten    audio_stream_in_t *inStream = NULL;
2021e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten    status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
2022cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                        &inStream, flags, address.string(), source);
2023cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
2024fd4c14883b268a0bc5514da135fe6b7d1ce2071bJean-Michel Trivi           ", Format %#x, Channels %x, flags %#x, status %d addr %s",
202581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            inStream,
2026cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.sample_rate,
2027cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.format,
2028cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent            halconfig.channel_mask,
2029ec40d284218466d8f0e832e7eb88e6ea6c479c88Glenn Kasten            flags,
2030fd4c14883b268a0bc5514da135fe6b7d1ce2071bJean-Michel Trivi            status, address.string());
203165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
203281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the input could not be opened with the requested parameters and we can handle the
20336770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung    // conversion internally, try to open again with the proposed parameters.
203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == BAD_VALUE &&
20356770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        audio_is_linear_pcm(config->format) &&
20366770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        audio_is_linear_pcm(halconfig.format) &&
20376770c6faa3467c92eabc5ec9b23d60eb556a0d03Andy Hung        (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
2038cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2039cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
20408594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME describe the change proposed by HAL (save old values so we can log them here)
2041cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
204281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inStream = NULL;
2043e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten        status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
2044cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                            &inStream, flags, address.string(), source);
20458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // FIXME log this new status; HAL should not propose any further changes
204665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
204765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
204881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status == NO_ERROR && inStream != NULL) {
204958912562617941964939a4182cda71eaeb153d4bGlenn Kasten
205046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
205181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
205281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // or (re-)create if current Pipe is idle and does not match the new format
205381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<NBAIO_Sink> teeSink;
205481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        enum {
205581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NO,    // don't copy input
205681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_NEW,   // copy input using a new pipe
205781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            TEE_SINK_OLD,   // copy input using an existing pipe
205881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } kind;
2059329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten        NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2060329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten                audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
2061da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (!mTeeSinkInputEnabled) {
2062da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            kind = TEE_SINK_NO;
20636e0d67d7b496ce17c0970a4ffd3a6f808860949cGlenn Kasten        } else if (!Format_isValid(format)) {
206481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
206581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink == 0) {
206681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
206781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (mRecordTeeSink->getStrongCount() != 1) {
206881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NO;
2069f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten        } else if (Format_isEqual(format, mRecordTeeSink->format())) {
207081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_OLD;
20714adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten        } else {
207281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kind = TEE_SINK_NEW;
207381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
207481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        switch (kind) {
207581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NEW: {
2076da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
207781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numCounterOffers = 0;
207881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const NBAIO_Format offers[1] = {format};
207981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
208181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            PipeReader *pipeReader = new PipeReader(*pipe);
208281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            numCounterOffers = 0;
208381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
208481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(index == 0);
208581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSink = pipe;
208681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mRecordTeeSource = pipeReader;
208781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = pipe;
20884adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten            }
208981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_OLD:
209181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            teeSink = mRecordTeeSink;
209281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        case TEE_SINK_NO:
209481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        default:
209581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
209658912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
209746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2098da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2099cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
210065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Start record thread
210234af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten        // RecordThread requires both input and output device indication to forward to audio
210381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // pre processing modules
210483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        sp<RecordThread> thread = new RecordThread(this,
2105cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  inputStream,
2106cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent                                  *input,
2107d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                  primaryOutputDevice_l(),
2108e7d6671c1ab1fea7ab1c4a9ebd1cd8f899c87628Glenn Kasten                                  devices
210946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
211046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  , teeSink
211146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
211246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                  );
2113cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        mRecordThreads.add(*input, thread);
2114cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent        ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
211583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        return thread;
21161afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent    }
211781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2118cf2c0210c8afbe7d0661ccbbae3835b5ce73c0bfEric Laurent    *input = AUDIO_IO_HANDLE_NONE;
211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
212065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
212165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
212281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input)
212365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
212481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return closeInput_nonvirtual(input);
212565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
212665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
212781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2128de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
212981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // keep strong reference on the record thread so that
213081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is not destroyed while exit() is executed
213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordThread> thread;
213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
213381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
213481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread = checkRecordThread_l(input);
213581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
213681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return BAD_VALUE;
2137de070137f11d346fba77605bd76a44c040a618fcEric Laurent        }
213881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
213981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("closeInput() %d", input);
21401b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
21411b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // If we still have effect chains, it means that a client still holds a handle
21421b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // on at least one effect. We must either move the chain to an existing thread with the
21431b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // same session ID or put it aside in case a new record thread is opened for a
21441b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        // new capture on the same session
21451b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        sp<EffectChain> chain;
2146aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        {
2147aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            Mutex::Autolock _sl(thread->mLock);
2148aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
21491b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // Note: maximum one chain per record thread
21501b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (effectChains.size() != 0) {
21511b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                chain = effectChains[0];
21521b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
21531b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        }
21541b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent        if (chain != 0) {
21551b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // first check if a record thread is already opened with a client on the same session.
21561b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // This should only happen in case of overlap between one thread tear down and the
21571b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // creation of its replacement
21581b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            size_t i;
21591b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            for (i = 0; i < mRecordThreads.size(); i++) {
21601b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                sp<RecordThread> t = mRecordThreads.valueAt(i);
21611b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                if (t == thread) {
21621b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    continue;
21631b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                }
21641b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                if (t->hasAudioSession(chain->sessionId()) != 0) {
21651b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    Mutex::Autolock _l(t->mLock);
21661b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    ALOGV("closeInput() found thread %d for effect session %d",
21671b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                          t->id(), chain->sessionId());
21681b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    t->addEffectChain_l(chain);
21691b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                    break;
21701b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                }
21711b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            }
21721b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            // put the chain aside if we could not find a record thread with the same session id.
21731b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent            if (i == mRecordThreads.size()) {
21741b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                putOrphanEffectChain_l(chain);
2175aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
2176aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
2177021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mRecordThreads.removeItem(input);
2179de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
218083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
218183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    // we have a different lock for notification client
218283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeInputFinish(thread);
218383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    return NO_ERROR;
218483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
2185de070137f11d346fba77605bd76a44c040a618fcEric Laurent
218683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeInputFinish(sp<RecordThread> thread)
218783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
218883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    thread->exit();
218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *in = thread->clearInput();
219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // from now on thread->mInput is NULL
219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    in->hwDev()->close_input_stream(in->hwDev(), in->stream);
219381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete in;
219483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
219565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
219683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
219783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
219883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mRecordThreads.removeItem(thread->mId);
219983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    closeInputFinish(thread);
2200b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
2201b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
2202d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kastenstatus_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
2203b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{
2204b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    Mutex::Autolock _l(mLock);
2205d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten    ALOGV("invalidateStream() stream %d", stream);
2206b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->invalidateTracks(stream);
2210b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent    }
221181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
2213b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent}
2214b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent
221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2216de3f8392fbf380ba6f09d009b00d7172477389a2Eric Laurentaudio_unique_id_t AudioFlinger::newAudioUniqueId()
2217162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{
221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return nextUniqueId();
2219162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent}
2220162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent
2221d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
2222a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{
2223a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    Mutex::Autolock _l(mLock);
222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2225d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2226d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2227d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2228d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
2229d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
2230021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    {
2231021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        Mutex::Autolock _cl(mClientLock);
2232021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // Ignore requests received from processes not known as notification client. The request
2233021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2234021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // called from a different pid leaving a stale session reference.  Also we don't know how
2235021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        // to clear this reference if the client process dies.
2236021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        if (mNotificationClients.indexOfKey(caller) < 0) {
2237021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2238021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            return;
2239021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        }
2240d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    }
2241d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent
224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt++;
224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" incremented refcount to %d", ref->mCnt);
224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
2249a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent        }
2250a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent    }
225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV(" added new entry for %d", audioSession);
2253a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent}
2254a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent
2255d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
225644a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{
225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    pid_t caller = IPCThreadState::self()->getCallingPid();
2259d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2260d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    if (pid != -1 && (caller == getpid_cached)) {
2261d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        caller = pid;
2262d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen    }
226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t num = mAudioSessionRefs.size();
226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i< num; i++) {
226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ref->mSessionid == audioSession && ref->mPid == caller) {
226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ref->mCnt--;
226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV(" decremented refcount to %d", ref->mCnt);
226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mCnt == 0) {
227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioSessionRefs.removeAt(i);
227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                delete ref;
227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                purgeStaleEffects_l();
227344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent            }
227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
227544a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent        }
227644a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent    }
2277d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // If the caller is mediaserver it is likely that the session being released was acquired
2278d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    // on behalf of a process not in notification clients and we ignore the warning.
2279d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent    ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
228044a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent}
228144a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent
228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() {
228365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("purging stale effects");
2285fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten
228681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > chains;
228758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
228981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
229081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chains.push(ec);
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2295c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
229658912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread> t = mRecordThreads.valueAt(i);
229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<EffectChain> ec = t->mEffectChains[j];
230181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chains.push(ec);
230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2303300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten    }
230465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
230581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < chains.size(); i++) {
230681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> ec = chains[i];
230781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionid = ec->sessionId();
230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> t = ec->mThread.promote();
230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numsessionrefs = mAudioSessionRefs.size();
231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool found = false;
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t k = 0; k < numsessionrefs; k++) {
231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (ref->mSessionid == sessionid) {
231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV(" session %d still exists for %d with %d refs",
231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sessionid, ref->mPid, ref->mCnt);
231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                found = true;
232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
232158912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
232258912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!found) {
2324e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            Mutex::Autolock _l(t->mLock);
232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // remove all effects from the chain
232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            while (ec->mEffects.size()) {
232781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectModule> effect = ec->mEffects[0];
232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->unPin();
232981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                t->removeEffect_l(effect);
233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (effect->purgeHandles()) {
233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioSystem::unregisterEffect(effect->id());
233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
2335c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten        }
233658912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return;
233865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
233965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
234181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
2342190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{
234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mPlaybackThreads.valueFor(output).get();
2344190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten}
2345190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held
234781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
234881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
234981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = checkPlaybackThread_l(output);
235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2352190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held
235481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mRecordThreads.valueFor(input).get();
235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
2358190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten
235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::nextUniqueId()
236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
2361bcefec31bd1346133052356ffc8d7ac8a5b13fabGlenn Kasten    return (uint32_t) android_atomic_inc(&mNextUniqueId);
236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
236383efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten
236481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
236537d825e72a6c606553a745da1212590a425996d3Glenn Kasten{
236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut *output = thread->getOutput();
236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return thread;
237137d825e72a6c606553a745da1212590a425996d3Glenn Kasten        }
237237d825e72a6c606553a745da1212590a425996d3Glenn Kasten    }
237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NULL;
237437d825e72a6c606553a745da1212590a425996d3Glenn Kasten}
237537d825e72a6c606553a745da1212590a425996d3Glenn Kasten
237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const
237765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
237881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *thread = primaryPlaybackThread_l();
2379000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread == NULL) {
238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
23829f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten    }
2383000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return thread->outDevice();
2385000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2386688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten
238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int triggerSession,
238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int listenerSession,
239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    sync_event_callback_t callBack,
23918ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent                                    wp<RefBase> cookie)
239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
239473ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten
239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t playStatus = NAME_NOT_FOUND;
239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t recStatus = NAME_NOT_FOUND;
239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playStatus == NO_ERROR) {
240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
240281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
240365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
240481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mRecordThreads.size(); i++) {
240581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
240681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (recStatus == NO_ERROR) {
240781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return event;
24089f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten        }
240965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
241081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPendingSyncEvents.add(event);
241281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
241381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createSyncEvent() invalid event %d", event->type());
241481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        event.clear();
241581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
241681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event;
241765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
241865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
242081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//  Effect management
242181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
242258912562617941964939a4182cda71eaeb153d4bGlenn Kasten
242358912562617941964939a4182cda71eaeb153d4bGlenn Kasten
242481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
2425000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
242681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
242781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryNumberEffects(numEffects);
2428000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2429000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
243081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
243158912562617941964939a4182cda71eaeb153d4bGlenn Kasten{
243281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
243381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectQueryEffect(index, descriptor);
243458912562617941964939a4182cda71eaeb153d4bGlenn Kasten}
243558912562617941964939a4182cda71eaeb153d4bGlenn Kasten
243681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
243781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *descriptor) const
2438000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
243981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
244081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return EffectGetDescriptor(pUuid, descriptor);
2441000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten}
2442000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
244381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24448d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect(
244581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *pDesc,
244681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
244781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
244881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t io,
244981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
245081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status,
245181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *id,
245281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled)
2453000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{
245481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus = NO_ERROR;
245581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
245681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc;
2457000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
24588d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten    pid_t pid = IPCThreadState::self()->getCallingPid();
245981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
246081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid, effectClient.get(), priority, sessionId, io);
2461000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
246281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pDesc == NULL) {
246381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
246481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2465952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2466000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
246781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check audio settings permission for global effects
246881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
246981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
247081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2471952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
2472000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten
247381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
247481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that can only be created by audio policy manager (running in same process)
247581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
247681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = PERMISSION_DENIED;
247781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
2478952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten    }
247965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
248081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
248181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!EffectIsNullUuid(&pDesc->uuid)) {
248281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is specified, request effect descriptor
248381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
248481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
248581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
248681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
248781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
248881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
248981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // if uuid is not specified, look for an available implementation
249081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // of the required type in effect factory
249181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (EffectIsNullUuid(&pDesc->type)) {
249281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() no effect type");
249381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
249481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2495288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
249681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t numEffects = 0;
249781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect_descriptor_t d;
249881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            d.flags = 0; // prevent compiler warning
249981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool found = false;
2500288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
250181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = EffectQueryNumberEffects(&numEffects);
250281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus < 0) {
250381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
250481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
250581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
250681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (uint32_t i = 0; i < numEffects; i++) {
250781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = EffectQueryEffect(i, &desc);
250881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (lStatus < 0) {
250981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
251081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
2511d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
251281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
251381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If matching type found save effect descriptor. If the session is
251481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // 0 and the effect is not auxiliary, continue enumeration in case
251581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // an auxiliary version of this effect type is available
251681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    found = true;
251781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    d = desc;
251881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
251981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2520d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                        break;
2521d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                    }
2522d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten                }
2523288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
252481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (!found) {
252581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
252681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("createEffect() effect not found");
252781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
252881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
252981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For same effect type, chose auxiliary version over insert version if
253081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // connect to output mix (Compliance to OpenSL ES)
253181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
253281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
253381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc = d;
253458912562617941964939a4182cda71eaeb153d4bGlenn Kasten            }
253558912562617941964939a4182cda71eaeb153d4bGlenn Kasten        }
253658912562617941964939a4182cda71eaeb153d4bGlenn Kasten
253781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Do not allow auxiliary effects on a session different from 0 (output mix)
253881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
253981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
254081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = INVALID_OPERATION;
254181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
25423dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent        }
254365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
254481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check recording permission for visualizer
254581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
254681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            !recordingAllowed()) {
254781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = PERMISSION_DENIED;
254881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
254981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
255065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
255181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return effect descriptor
255281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *pDesc = desc;
2553142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
2554eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // if the output returned by getOutputForEffect() is removed before we lock the
2555eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2556eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // and we will exit safely
2557eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            io = AudioSystem::getOutputForEffect(&desc);
2558eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            ALOGV("createEffect got output %d", io);
2559eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        }
2560eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent
2561eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent        Mutex::Autolock _l(mLock);
256265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
256381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is not specified try to find a matching audio session ID in one of the
256481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // output threads.
256581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
256681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // because of code checking output when entering the function.
256781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Note: io is never 0 when creating an effect on an input
2568142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten        if (io == AUDIO_IO_HANDLE_NONE) {
25695baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
25705baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // output must be specified by AudioPolicyManager when using session
25715baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                // AUDIO_SESSION_OUTPUT_STAGE
25725baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                lStatus = BAD_VALUE;
25735baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                goto Exit;
25745baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
2575eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            // look for the thread where the specified audio session is present
2576eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2577eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2578eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    io = mPlaybackThreads.keyAt(i);
2579eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    break;
2580eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                }
2581d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten            }
258281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (io == 0) {
2583eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                for (size_t i = 0; i < mRecordThreads.size(); i++) {
2584eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                    if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2585eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent                        io = mRecordThreads.keyAt(i);
258681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
25872986460984580833161bdaabc7f17da1005a8961Eric Laurent                    }
258865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
258965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
259081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If no output thread contains the requested session ID, default to
259181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // first output. The effect chain will be moved to the correct output
259281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // thread when a track with the same session ID is created
2593142f519aa1acd5804d111e60d100f170fed28405Glenn Kasten            if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
259481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                io = mPlaybackThreads.keyAt(0);
259581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
259681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect() got io %d for effect %s", io, desc.name);
259765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
259881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ThreadBase *thread = checkRecordThread_l(io);
259981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == NULL) {
260081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            thread = checkPlaybackThread_l(io);
260181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (thread == NULL) {
260281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGE("createEffect() unknown output thread");
260381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
260481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
2605288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten            }
2606aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        } else {
2607aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            // Check if one effect chain was awaiting for an effect to be created on this
2608aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            // session and used it instead of creating a new one.
2609aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2610aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            if (chain != 0) {
26111b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent                Mutex::Autolock _l(thread->mLock);
2612aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent                thread->addEffectChain_l(chain);
2613aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            }
2614288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten        }
2615288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten
2616021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent        sp<Client> client = registerPid(pid);
261758912562617941964939a4182cda71eaeb153d4bGlenn Kasten
261881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect on selected output thread
261981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = thread->createEffect_l(client, effectClient, priority, sessionId,
262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                &desc, enabled, &lStatus);
262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (handle != 0 && id != NULL) {
262281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *id = handle->id();
262365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
2624fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        if (handle == 0) {
2625fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            // remove local strong reference to Client with mClientLock held
2626fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            Mutex::Autolock _cl(mClientLock);
2627fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent            client.clear();
2628fe1a94e68e173fe4dfe7699112422a94eddacb4eEric Laurent        }
262965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
263065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
263181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
26329156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
263466fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten}
263566fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten
263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
263781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t dstOutput)
263865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
264081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcOutput, dstOutput);
264165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcOutput == dstOutput) {
264381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
264481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
264581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
264681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
264781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (srcThread == NULL) {
264881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad srcOutput %d", srcOutput);
264981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
265081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
265181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
265281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (dstThread == NULL) {
265381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffects() bad dstOutput %d", dstOutput);
265481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
265565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
265665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
265781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _dl(dstThread->mLock);
265881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _sl(srcThread->mLock);
26595baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return moveEffectChain_l(sessionId, srcThread, dstThread, false);
266065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
266165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
266281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
266381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffectChain_l(int sessionId,
266481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *srcThread,
266581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   AudioFlinger::PlaybackThread *dstThread,
266681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                   bool reRegister)
266765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
266881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
266981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionId, srcThread, dstThread);
267065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
267181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
267281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
267381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
267481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sessionId, srcThread);
267581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
267681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
26779ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
26789a59276fb465e492138e0576523b54079671e8f4Andy Hung    // Check whether the destination thread has a channel count of FCC_2, which is
26799a59276fb465e492138e0576523b54079671e8f4Andy Hung    // currently required for (most) effects. Prevent moving the effect chain here rather
26809a59276fb465e492138e0576523b54079671e8f4Andy Hung    // than disabling the addEffect_l() call in dstThread below.
2681b10352f4bc11ae6f3d93e464074709555ecf9e06Eric Laurent    if ((dstThread->type() == ThreadBase::MIXER || dstThread->type() == ThreadBase::DUPLICATING) &&
2682b10352f4bc11ae6f3d93e464074709555ecf9e06Eric Laurent            dstThread->mChannelCount != FCC_2) {
26839a59276fb465e492138e0576523b54079671e8f4Andy Hung        ALOGW("moveEffectChain_l() effect chain failed because"
26849a59276fb465e492138e0576523b54079671e8f4Andy Hung                " destination thread %p channel count(%u) != %u",
26859a59276fb465e492138e0576523b54079671e8f4Andy Hung                dstThread, dstThread->mChannelCount, FCC_2);
26869a59276fb465e492138e0576523b54079671e8f4Andy Hung        return INVALID_OPERATION;
26879a59276fb465e492138e0576523b54079671e8f4Andy Hung    }
26889a59276fb465e492138e0576523b54079671e8f4Andy Hung
268981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
269081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // so that a new chain is created with correct parameters when first effect is added. This is
269181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
269281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // removed.
269381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    srcThread->removeEffectChain_l(chain);
26949ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
269581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // transfer all effects one by one so that new effect chain is created on new thread with
269681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
269781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> dstChain;
269881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy = 0; // prevent compiler warning
269981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect = chain->getEffectFromId_l(0);
27005baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    Vector< sp<EffectModule> > removed;
27015baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    status_t status = NO_ERROR;
270281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (effect != 0) {
270381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        srcThread->removeEffect_l(effect);
27045baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        removed.add(effect);
27055baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        status = dstThread->addEffect_l(effect);
27065baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (status != NO_ERROR) {
27075baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
27085baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
270981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // removeEffect_l() has stopped the effect if it was active so it must be restarted
271081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect->state() == EffectModule::ACTIVE ||
271181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                effect->state() == EffectModule::STOPPING) {
271281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->start();
271365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
271481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the move request is not received from audio policy manager, the effect must be
271581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // re-registered with the new strategy and output
271681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (dstChain == 0) {
271781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            dstChain = effect->chain().promote();
271881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (dstChain == 0) {
271981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
27205baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                status = NO_INIT;
27215baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                break;
272265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
272381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            strategy = dstChain->strategy();
272465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
272581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (reRegister) {
272681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
272781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::registerEffect(&effect->desc(),
27285baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                        dstThread->id(),
272981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        strategy,
273081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        sessionId,
273181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        effect->id());
2732d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent            AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
273381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
273481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect = chain->getEffectFromId_l(0);
273558912562617941964939a4182cda71eaeb153d4bGlenn Kasten    }
273658912562617941964939a4182cda71eaeb153d4bGlenn Kasten
27375baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (status != NO_ERROR) {
27385baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        for (size_t i = 0; i < removed.size(); i++) {
27395baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            srcThread->addEffect_l(removed[i]);
27405baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            if (dstChain != 0 && reRegister) {
27415baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::unregisterEffect(removed[i]->id());
27425baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                AudioSystem::registerEffect(&removed[i]->desc(),
27435baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            srcThread->id(),
27445baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            strategy,
27455baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            sessionId,
27465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                                            removed[i]->id());
2747d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent                AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
27485baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            }
27495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
27505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    }
27515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
27525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    return status;
275365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
275465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
27555baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
2756813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2757813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    if (mGlobalEffectEnableTime != 0 &&
2758813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2759813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        return true;
2760813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2761813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2762813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2763813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<EffectChain> ec =
2764813e2a74853bde19e37d878c596a044b3f299efcEric Laurent                mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
27655baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        if (ec != 0 && ec->isNonOffloadableEnabled()) {
2766813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            return true;
2767813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2768813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2769813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    return false;
2770813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2771813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
27725baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable()
2773813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{
2774813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    Mutex::Autolock _l(mLock);
2775813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2776813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    mGlobalEffectEnableTime = systemTime();
2777813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2778813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2779813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2780813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        if (t->mType == ThreadBase::OFFLOAD) {
2781813e2a74853bde19e37d878c596a044b3f299efcEric Laurent            t->invalidateTracks(AUDIO_STREAM_MUSIC);
2782813e2a74853bde19e37d878c596a044b3f299efcEric Laurent        }
2783813e2a74853bde19e37d878c596a044b3f299efcEric Laurent    }
2784813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2785813e2a74853bde19e37d878c596a044b3f299efcEric Laurent}
2786813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
2787aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentstatus_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2788aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2789aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    audio_session_t session = (audio_session_t)chain->sessionId();
2790aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2791aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2792aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2793aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2794aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        return ALREADY_EXISTS;
2795aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2796aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    mOrphanEffectChains.add(session, chain);
2797aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return NO_ERROR;
2798aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2799aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2800aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2801aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2802aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    sp<EffectChain> chain;
2803aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2804aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2805aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2806aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        chain = mOrphanEffectChains.valueAt(index);
2807aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        mOrphanEffectChains.removeItemsAt(index);
2808aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2809aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return chain;
2810aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2811aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2812aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurentbool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2813aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent{
2814aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    Mutex::Autolock _l(mLock);
2815aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    audio_session_t session = (audio_session_t)effect->sessionId();
2816aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ssize_t index = mOrphanEffectChains.indexOfKey(session);
2817aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2818aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    if (index >= 0) {
2819aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2820aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        if (chain->removeEffect_l(effect) == 0) {
2821aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2822aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent            mOrphanEffectChains.removeItemsAt(index);
2823aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        }
2824aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        return true;
2825aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    }
2826aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    return false;
2827aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent}
2828aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2829aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent
2830da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry {
28312f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
28322f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten    char mFileName[TEE_MAX_FILENAME];
2833da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten};
2834da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
2835da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2)
2836da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{
28372f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten    return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
2838da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}
2839da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten
284046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2841d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
284265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
2843d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten    NBAIO_Source *teeSource = source.get();
2844fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    if (teeSource != NULL) {
2845da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // .wav rotation
2846da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // There is a benign race condition if 2 threads call this simultaneously.
2847da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // They would both traverse the directory, but the result would simply be
2848da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // failures at unlink() which are ignored.  It's also unlikely since
2849da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // normally dumpsys is only done by bugreport or from the command line.
2850da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        char teePath[32+256];
2851da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strcpy(teePath, "/data/misc/media");
2852da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        size_t teePathLen = strlen(teePath);
2853da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        DIR *dir = opendir(teePath);
2854da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        teePath[teePathLen++] = '/';
2855da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (dir != NULL) {
28562f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten#define TEE_MAX_SORT 20 // number of entries to sort
28572f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten#define TEE_MAX_KEEP 10 // number of entries to keep
28582f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            struct Entry entries[TEE_MAX_SORT];
2859da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            size_t entryCount = 0;
28602f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            while (entryCount < TEE_MAX_SORT) {
2861da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent de;
2862da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                struct dirent *result = NULL;
2863da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                int rc = readdir_r(dir, &de, &result);
2864da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (rc != 0) {
2865da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r failed %d", rc);
2866da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2867da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2868da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result == NULL) {
2869da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2870da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2871da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                if (result != &de) {
2872da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2873da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    break;
2874da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2875da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                // ignore non .wav file entries
2876da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                size_t nameLen = strlen(de.d_name);
28772f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
2878da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                        strcmp(&de.d_name[nameLen - 4], ".wav")) {
2879da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    continue;
2880da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
28812f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                strcpy(entries[entryCount++].mFileName, de.d_name);
2882da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2883da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            (void) closedir(dir);
28842f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten            if (entryCount > TEE_MAX_KEEP) {
2885da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                qsort(entries, entryCount, sizeof(Entry), comparEntry);
28862f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
28872f55e7632dfa0dafde58efd8cb2af6593217fd91Glenn Kasten                    strcpy(&teePath[teePathLen], entries[i].mFileName);
2888da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                    (void) unlink(teePath);
2889da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten                }
2890da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2891da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        } else {
2892da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
28938b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2894da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2895da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
2896d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten        char teeTime[16];
2897fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct timeval tv;
2898fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        gettimeofday(&tv, NULL);
2899fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        struct tm tm;
2900fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        localtime_r(&tv.tv_sec, &tm);
2901da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2902da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2903da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2904da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
2905fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        if (teeFd >= 0) {
2906329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME use libsndfile
2907fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            char wavHeader[44];
2908fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            memcpy(wavHeader,
2909fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn 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",
2910fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                sizeof(wavHeader));
2911fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            NBAIO_Format format = teeSource->format();
2912fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            unsigned channelCount = Format_channelCount(format);
29133b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten            uint32_t sampleRate = Format_sampleRate(format);
2914329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            size_t frameSize = Format_frameSize(format);
2915fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[22] = channelCount;       // number of channels
2916fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[24] = sampleRate;         // sample rate
2917fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            wavHeader[25] = sampleRate >> 8;
2918329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            wavHeader[32] = frameSize;          // block alignment
2919329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            wavHeader[33] = frameSize >> 8;
2920fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, wavHeader, sizeof(wavHeader));
2921fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            size_t total = 0;
2922fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            bool firstRead = true;
2923329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten#define TEE_SINK_READ 1024                      // frames per I/O operation
2924329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            void *buffer = malloc(TEE_SINK_READ * frameSize);
2925fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            for (;;) {
2926fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                size_t count = TEE_SINK_READ;
29272c3b2da3049627264b7c6b449a1622f002210f03John Grossman                ssize_t actual = teeSource->read(buffer, count,
29282c3b2da3049627264b7c6b449a1622f002210f03John Grossman                        AudioBufferProvider::kInvalidPTS);
2929fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                bool wasFirstRead = firstRead;
2930fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                firstRead = false;
2931fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                if (actual <= 0) {
2932fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2933fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                        continue;
2934fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    }
2935fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                    break;
2936fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                }
2937a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent                ALOG_ASSERT(actual <= (ssize_t)count);
2938329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten                write(teeFd, buffer, actual * frameSize);
2939fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten                total += actual;
2940fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            }
2941329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            free(buffer);
2942fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 4, SEEK_SET);
2943329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            uint32_t temp = 44 + total * frameSize - 8;
2944329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME not big-endian safe
2945fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2946fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            lseek(teeFd, (off_t) 40, SEEK_SET);
2947329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            temp =  total * frameSize;
2948329f6511ee4e03a4605c70bbda8d3a96d2544884Glenn Kasten            // FIXME not big-endian safe
2949fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            write(teeFd, &temp, sizeof(temp));
2950fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten            close(teeFd);
2951da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
29528b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "tee copied to %s\n", teePath);
2953da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2954fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        } else {
2955da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            if (fd >= 0) {
29568b5f642eb2364ea7fe46a5b3af51b48b58f12183Elliott Hughes                dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2957da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            }
2958fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten        }
2959fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten    }
2960d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten}
296146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
2962d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten
296365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
296465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
296565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact(
296665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
296765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
296865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioFlinger::onTransact(code, data, reply, flags);
296965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
297065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
297163238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android
2972