Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 22 | #include "Configuration.h" |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 23 | #include <dirent.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 24 | #include <math.h> |
| 25 | #include <signal.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/resource.h> |
| 28 | |
Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 29 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <binder/IServiceManager.h> |
| 31 | #include <utils/Log.h> |
Glenn Kasten | d8e6fd3 | 2012-05-07 11:07:57 -0700 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | #include <binder/Parcel.h> |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 34 | #include <media/audiohal/DeviceHalInterface.h> |
| 35 | #include <media/audiohal/DevicesFactoryHalInterface.h> |
| 36 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 37 | #include <media/AudioParameter.h> |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 38 | #include <media/TypeConverter.h> |
Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 39 | #include <memunreachable/memunreachable.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | #include <utils/String16.h> |
| 41 | #include <utils/threads.h> |
Eric Laurent | 38ccae2 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 42 | #include <utils/Atomic.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 44 | #include <cutils/bitops.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | #include <cutils/properties.h> |
| 46 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 47 | #include <system/audio.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | |
| 49 | #include "AudioMixer.h" |
| 50 | #include "AudioFlinger.h" |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 51 | #include "ServiceUtilities.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 53 | #include <media/AudioResamplerPublic.h> |
| 54 | |
Mikhail Naganov | 9fe9401 | 2016-10-14 14:57:40 -0700 | [diff] [blame] | 55 | #include <system/audio_effects/effect_visualizer.h> |
| 56 | #include <system/audio_effects/effect_ns.h> |
| 57 | #include <system/audio_effects/effect_aec.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | |
Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 59 | #include <audio_utils/primitives.h> |
| 60 | |
Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 61 | #include <powermanager/PowerManager.h> |
Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 62 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 63 | #include <media/IMediaLogService.h> |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 64 | #include <media/MemoryLeakTrackUtil.h> |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 65 | #include <media/nbaio/Pipe.h> |
| 66 | #include <media/nbaio/PipeReader.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 67 | #include <media/AudioParameter.h> |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 68 | #include <mediautils/BatteryNotifier.h> |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 69 | #include <private/android_filesystem_config.h> |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 70 | |
rago | 3bd1c87 | 2016-09-26 12:58:14 -0700 | [diff] [blame] | 71 | //#define BUFLOG_NDEBUG 0 |
| 72 | #include <BufLog.h> |
| 73 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 74 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 75 | |
John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 76 | // Note: the following macro is used for extremely verbose logging message. In |
| 77 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 78 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 79 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 80 | // turned on. Do not uncomment the #def below unless you really know what you |
| 81 | // are doing and want to see all of the extremely verbose messages. |
| 82 | //#define VERY_VERY_VERBOSE_LOGGING |
| 83 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 84 | #define ALOGVV ALOGV |
| 85 | #else |
| 86 | #define ALOGVV(a...) do { } while(0) |
| 87 | #endif |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 88 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 89 | namespace android { |
| 90 | |
Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 91 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; |
| 92 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 93 | static const char kClientLockedString[] = "Client lock is taken\n"; |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 94 | static const char kNoEffectsFactory[] = "Effects Factory is absent\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 95 | |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 96 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 97 | nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 98 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 99 | uint32_t AudioFlinger::mScreenState; |
Glenn Kasten | 3ed2920 | 2012-08-07 15:24:44 -0700 | [diff] [blame] | 100 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 101 | #ifdef TEE_SINK |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 102 | bool AudioFlinger::mTeeSinkInputEnabled = false; |
| 103 | bool AudioFlinger::mTeeSinkOutputEnabled = false; |
| 104 | bool AudioFlinger::mTeeSinkTrackEnabled = false; |
| 105 | |
| 106 | size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault; |
| 107 | size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault; |
| 108 | size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault; |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 109 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 110 | |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 111 | // In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off |
| 112 | // we define a minimum time during which a global effect is considered enabled. |
| 113 | static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200); |
| 114 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 115 | // ---------------------------------------------------------------------------- |
| 116 | |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 117 | std::string formatToString(audio_format_t format) { |
| 118 | std::string result; |
| 119 | FormatConverter::toString(format, result); |
| 120 | return result; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 123 | // ---------------------------------------------------------------------------- |
| 124 | |
| 125 | AudioFlinger::AudioFlinger() |
| 126 | : BnAudioFlinger(), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 127 | mPrimaryHardwareDev(NULL), |
Glenn Kasten | 9ea65d0 | 2014-01-17 10:21:24 -0800 | [diff] [blame] | 128 | mAudioHwDevs(NULL), |
Glenn Kasten | 7d6c35b | 2012-07-02 12:45:10 -0700 | [diff] [blame] | 129 | mHardwareStatus(AUDIO_HW_IDLE), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 130 | mMasterVolume(1.0f), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 131 | mMasterMute(false), |
Glenn Kasten | d2e67e1 | 2016-04-11 08:26:37 -0700 | [diff] [blame] | 132 | // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX), |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 133 | mMode(AUDIO_MODE_INVALID), |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 134 | mBtNrecIsOff(false), |
| 135 | mIsLowRamDevice(true), |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 136 | mIsDeviceTypeKnown(false), |
Glenn Kasten | 4ea00a2 | 2014-06-02 08:29:22 -0700 | [diff] [blame] | 137 | mGlobalEffectEnableTime(0), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 138 | mSystemReady(false) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 139 | { |
Glenn Kasten | d2e67e1 | 2016-04-11 08:26:37 -0700 | [diff] [blame] | 140 | // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum |
| 141 | for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) { |
| 142 | // zero ID has a special meaning, so unavailable |
| 143 | mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX; |
| 144 | } |
| 145 | |
Glenn Kasten | 949a926 | 2013-04-16 12:35:20 -0700 | [diff] [blame] | 146 | getpid_cached = getpid(); |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 147 | const bool doLog = property_get_bool("ro.test_harness", false); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 148 | if (doLog) { |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 149 | mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", |
| 150 | MemoryHeapBase::READ_ONLY); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 151 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 152 | |
Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 153 | // reset battery stats. |
| 154 | // if the audio service has crashed, battery stats could be left |
| 155 | // in bad state, reset the state upon service start. |
| 156 | BatteryNotifier::getInstance().noteResetAudio(); |
| 157 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 158 | mDevicesFactoryHal = DevicesFactoryHalInterface::create(); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 159 | mEffectsFactoryHal = EffectsFactoryHalInterface::create(); |
| 160 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 161 | #ifdef TEE_SINK |
Glenn Kasten | 9a00399 | 2016-02-23 15:24:34 -0800 | [diff] [blame] | 162 | char value[PROPERTY_VALUE_MAX]; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 163 | (void) property_get("ro.debuggable", value, "0"); |
| 164 | int debuggable = atoi(value); |
| 165 | int teeEnabled = 0; |
| 166 | if (debuggable) { |
| 167 | (void) property_get("af.tee", value, "0"); |
| 168 | teeEnabled = atoi(value); |
| 169 | } |
Glenn Kasten | f66b422 | 2014-02-20 10:23:28 -0800 | [diff] [blame] | 170 | // FIXME symbolic constants here |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 171 | if (teeEnabled & 1) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 172 | mTeeSinkInputEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 173 | } |
| 174 | if (teeEnabled & 2) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 175 | mTeeSinkOutputEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 176 | } |
| 177 | if (teeEnabled & 4) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 178 | mTeeSinkTrackEnabled = true; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 179 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 180 | #endif |
Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void AudioFlinger::onFirstRef() |
| 184 | { |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 185 | Mutex::Autolock _l(mLock); |
| 186 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 187 | /* TODO: move all this work into an Init() function */ |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 188 | char val_str[PROPERTY_VALUE_MAX] = { 0 }; |
| 189 | if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { |
| 190 | uint32_t int_val; |
| 191 | if (1 == sscanf(val_str, "%u", &int_val)) { |
| 192 | mStandbyTimeInNsecs = milliseconds(int_val); |
| 193 | ALOGI("Using %u mSec as standby time.", int_val); |
| 194 | } else { |
| 195 | mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; |
| 196 | ALOGI("Using default %u mSec as standby time.", |
| 197 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
| 198 | } |
| 199 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 200 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 201 | mPatchPanel = new PatchPanel(this); |
Andy Hung | deb0335 | 2016-08-29 14:40:14 -0700 | [diff] [blame] | 202 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 203 | mMode = AUDIO_MODE_NORMAL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | AudioFlinger::~AudioFlinger() |
| 207 | { |
| 208 | while (!mRecordThreads.isEmpty()) { |
Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 209 | // closeInput_nonvirtual() will remove specified entry from mRecordThreads |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 210 | closeInput_nonvirtual(mRecordThreads.keyAt(0)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 211 | } |
| 212 | while (!mPlaybackThreads.isEmpty()) { |
Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 213 | // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 214 | closeOutput_nonvirtual(mPlaybackThreads.keyAt(0)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 215 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 216 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 217 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 218 | // no mHardwareLock needed, as there are no other references to this |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 219 | delete mAudioHwDevs.valueAt(i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 220 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 221 | |
| 222 | // Tell media.log service about any old writers that still need to be unregistered |
Andy Hung | ce71768 | 2015-12-22 13:40:32 -0800 | [diff] [blame] | 223 | if (mLogMemoryDealer != 0) { |
| 224 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
| 225 | if (binder != 0) { |
| 226 | sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder)); |
| 227 | for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { |
| 228 | sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory()); |
| 229 | mUnregisteredWriters.pop(); |
| 230 | mediaLogService->unregisterWriter(iMemory); |
| 231 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 236 | static const char * const audio_interfaces[] = { |
| 237 | AUDIO_HARDWARE_MODULE_ID_PRIMARY, |
| 238 | AUDIO_HARDWARE_MODULE_ID_A2DP, |
| 239 | AUDIO_HARDWARE_MODULE_ID_USB, |
| 240 | }; |
| 241 | #define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) |
| 242 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 243 | AudioHwDevice* AudioFlinger::findSuitableHwDev_l( |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 244 | audio_module_handle_t module, |
| 245 | audio_devices_t devices) |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 246 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 247 | // if module is 0, the request comes from an old policy manager and we should load |
| 248 | // well known modules |
| 249 | if (module == 0) { |
| 250 | ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); |
| 251 | for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { |
| 252 | loadHwModule_l(audio_interfaces[i]); |
| 253 | } |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 254 | // then try to find a module supporting the requested device. |
| 255 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 256 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i); |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 257 | sp<DeviceHalInterface> dev = audioHwDevice->hwDevice(); |
| 258 | uint32_t supportedDevices; |
| 259 | if (dev->getSupportedDevices(&supportedDevices) == OK && |
| 260 | (supportedDevices & devices) == devices) { |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 261 | return audioHwDevice; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 262 | } |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 263 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 264 | } else { |
| 265 | // check a match for the requested module handle |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 266 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module); |
| 267 | if (audioHwDevice != NULL) { |
| 268 | return audioHwDevice; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 271 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 274 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 275 | void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 276 | { |
| 277 | const size_t SIZE = 256; |
| 278 | char buffer[SIZE]; |
| 279 | String8 result; |
| 280 | |
| 281 | result.append("Clients:\n"); |
| 282 | for (size_t i = 0; i < mClients.size(); ++i) { |
Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 283 | sp<Client> client = mClients.valueAt(i).promote(); |
| 284 | if (client != 0) { |
| 285 | snprintf(buffer, SIZE, " pid: %d\n", client->pid()); |
| 286 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 289 | |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 290 | result.append("Notification Clients:\n"); |
| 291 | for (size_t i = 0; i < mNotificationClients.size(); ++i) { |
| 292 | snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i)); |
| 293 | result.append(buffer); |
| 294 | } |
| 295 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 296 | result.append("Global session refs:\n"); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 297 | result.append(" session pid count\n"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 298 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { |
| 299 | AudioSessionRef *r = mAudioSessionRefs[i]; |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 300 | snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 301 | result.append(buffer); |
| 302 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 303 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 307 | void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 308 | { |
| 309 | const size_t SIZE = 256; |
| 310 | char buffer[SIZE]; |
| 311 | String8 result; |
Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 312 | hardware_call_state hardwareStatus = mHardwareStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 313 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 314 | snprintf(buffer, SIZE, "Hardware status: %d\n" |
| 315 | "Standby Time mSec: %u\n", |
| 316 | hardwareStatus, |
| 317 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 318 | result.append(buffer); |
| 319 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 322 | void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 323 | { |
| 324 | const size_t SIZE = 256; |
| 325 | char buffer[SIZE]; |
| 326 | String8 result; |
| 327 | snprintf(buffer, SIZE, "Permission Denial: " |
| 328 | "can't dump AudioFlinger from pid=%d, uid=%d\n", |
| 329 | IPCThreadState::self()->getCallingPid(), |
| 330 | IPCThreadState::self()->getCallingUid()); |
| 331 | result.append(buffer); |
| 332 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 335 | bool AudioFlinger::dumpTryLock(Mutex& mutex) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 336 | { |
| 337 | bool locked = false; |
| 338 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 339 | if (mutex.tryLock() == NO_ERROR) { |
| 340 | locked = true; |
| 341 | break; |
| 342 | } |
Glenn Kasten | 7dede87 | 2011-12-13 11:04:14 -0800 | [diff] [blame] | 343 | usleep(kDumpLockSleepUs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 344 | } |
| 345 | return locked; |
| 346 | } |
| 347 | |
| 348 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) |
| 349 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 350 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 351 | dumpPermissionDenial(fd, args); |
| 352 | } else { |
| 353 | // get state of hardware lock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 354 | bool hardwareLocked = dumpTryLock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 355 | if (!hardwareLocked) { |
| 356 | String8 result(kHardwareLockedString); |
| 357 | write(fd, result.string(), result.size()); |
| 358 | } else { |
| 359 | mHardwareLock.unlock(); |
| 360 | } |
| 361 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 362 | bool locked = dumpTryLock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 363 | |
| 364 | // failed to lock - AudioFlinger is probably deadlocked |
| 365 | if (!locked) { |
| 366 | String8 result(kDeadlockedString); |
| 367 | write(fd, result.string(), result.size()); |
| 368 | } |
| 369 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 370 | bool clientLocked = dumpTryLock(mClientLock); |
| 371 | if (!clientLocked) { |
| 372 | String8 result(kClientLockedString); |
| 373 | write(fd, result.string(), result.size()); |
| 374 | } |
Marco Nelissen | d89eadd | 2014-10-07 13:28:44 -0700 | [diff] [blame] | 375 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 376 | if (mEffectsFactoryHal != 0) { |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 377 | mEffectsFactoryHal->dumpEffects(fd); |
| 378 | } else { |
| 379 | String8 result(kNoEffectsFactory); |
| 380 | write(fd, result.string(), result.size()); |
| 381 | } |
Marco Nelissen | d89eadd | 2014-10-07 13:28:44 -0700 | [diff] [blame] | 382 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 383 | dumpClients(fd, args); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 384 | if (clientLocked) { |
| 385 | mClientLock.unlock(); |
| 386 | } |
| 387 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 388 | dumpInternals(fd, args); |
| 389 | |
| 390 | // dump playback threads |
| 391 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 392 | mPlaybackThreads.valueAt(i)->dump(fd, args); |
| 393 | } |
| 394 | |
| 395 | // dump record threads |
| 396 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 397 | mRecordThreads.valueAt(i)->dump(fd, args); |
| 398 | } |
| 399 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 400 | // dump orphan effect chains |
| 401 | if (mOrphanEffectChains.size() != 0) { |
| 402 | write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n")); |
| 403 | for (size_t i = 0; i < mOrphanEffectChains.size(); i++) { |
| 404 | mOrphanEffectChains.valueAt(i)->dump(fd, args); |
| 405 | } |
| 406 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 407 | // dump all hardware devs |
| 408 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 409 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 410 | dev->dump(fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 411 | } |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 412 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 413 | #ifdef TEE_SINK |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 414 | // dump the serially shared record tee sink |
| 415 | if (mRecordTeeSource != 0) { |
| 416 | dumpTee(fd, mRecordTeeSource); |
| 417 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 418 | #endif |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 419 | |
rago | 3bd1c87 | 2016-09-26 12:58:14 -0700 | [diff] [blame] | 420 | BUFLOG_RESET; |
| 421 | |
Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 422 | if (locked) { |
| 423 | mLock.unlock(); |
| 424 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 425 | |
| 426 | // append a copy of media.log here by forwarding fd to it, but don't attempt |
| 427 | // to lookup the service if it's not running, as it will block for a second |
| 428 | if (mLogMemoryDealer != 0) { |
| 429 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
| 430 | if (binder != 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 431 | dprintf(fd, "\nmedia.log:\n"); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 432 | Vector<String16> args; |
| 433 | binder->dump(fd, args); |
| 434 | } |
| 435 | } |
Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 436 | |
| 437 | // check for optional arguments |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 438 | bool dumpMem = false; |
Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 439 | bool unreachableMemory = false; |
| 440 | for (const auto &arg : args) { |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 441 | if (arg == String16("-m")) { |
| 442 | dumpMem = true; |
| 443 | } else if (arg == String16("--unreachable")) { |
Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 444 | unreachableMemory = true; |
| 445 | } |
| 446 | } |
| 447 | |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 448 | if (dumpMem) { |
| 449 | dprintf(fd, "\nDumping memory:\n"); |
| 450 | std::string s = dumpMemoryAddresses(100 /* limit */); |
| 451 | write(fd, s.c_str(), s.size()); |
| 452 | } |
Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 453 | if (unreachableMemory) { |
| 454 | dprintf(fd, "\nDumping unreachable memory:\n"); |
| 455 | // TODO - should limit be an argument parameter? |
Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 456 | std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */); |
Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 457 | write(fd, s.c_str(), s.size()); |
| 458 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 459 | } |
| 460 | return NO_ERROR; |
| 461 | } |
| 462 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 463 | sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid) |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 464 | { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 465 | Mutex::Autolock _cl(mClientLock); |
Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 466 | // If pid is already in the mClients wp<> map, then use that entry |
| 467 | // (for which promote() is always != 0), otherwise create a new entry and Client. |
| 468 | sp<Client> client = mClients.valueFor(pid).promote(); |
| 469 | if (client == 0) { |
| 470 | client = new Client(this, pid); |
| 471 | mClients.add(pid, client); |
| 472 | } |
| 473 | |
| 474 | return client; |
| 475 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 476 | |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 477 | sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name) |
| 478 | { |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 479 | // If there is no memory allocated for logs, return a dummy writer that does nothing |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 480 | if (mLogMemoryDealer == 0) { |
| 481 | return new NBLog::Writer(); |
| 482 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 483 | sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 484 | // Similarly if we can't contact the media.log service, also return a dummy writer |
| 485 | if (binder == 0) { |
| 486 | return new NBLog::Writer(); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 487 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 488 | sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder)); |
| 489 | sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); |
| 490 | // If allocation fails, consult the vector of previously unregistered writers |
| 491 | // and garbage-collect one or more them until an allocation succeeds |
| 492 | if (shared == 0) { |
| 493 | Mutex::Autolock _l(mUnregisteredWritersLock); |
| 494 | for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { |
| 495 | { |
| 496 | // Pick the oldest stale writer to garbage-collect |
| 497 | sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory()); |
| 498 | mUnregisteredWriters.removeAt(0); |
| 499 | mediaLogService->unregisterWriter(iMemory); |
| 500 | // Now the media.log remote reference to IMemory is gone. When our last local |
| 501 | // reference to IMemory also drops to zero at end of this block, |
| 502 | // the IMemory destructor will deallocate the region from mLogMemoryDealer. |
| 503 | } |
| 504 | // Re-attempt the allocation |
| 505 | shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); |
| 506 | if (shared != 0) { |
| 507 | goto success; |
| 508 | } |
| 509 | } |
| 510 | // Even after garbage-collecting all old writers, there is still not enough memory, |
| 511 | // so return a dummy writer |
| 512 | return new NBLog::Writer(); |
| 513 | } |
| 514 | success: |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 515 | NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer(); |
| 516 | new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding |
| 517 | // explicit destructor not needed since it is POD |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 518 | mediaLogService->registerWriter(shared, size, name); |
Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 519 | return new NBLog::Writer(shared, size); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer) |
| 523 | { |
Glenn Kasten | 685ef09 | 2013-02-04 08:15:34 -0800 | [diff] [blame] | 524 | if (writer == 0) { |
| 525 | return; |
| 526 | } |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 527 | sp<IMemory> iMemory(writer->getIMemory()); |
| 528 | if (iMemory == 0) { |
| 529 | return; |
| 530 | } |
Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 531 | // Rather than removing the writer immediately, append it to a queue of old writers to |
| 532 | // be garbage-collected later. This allows us to continue to view old logs for a while. |
| 533 | Mutex::Autolock _l(mUnregisteredWritersLock); |
| 534 | mUnregisteredWriters.push(writer); |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 537 | // IAudioFlinger interface |
| 538 | |
| 539 | |
| 540 | sp<IAudioTrack> AudioFlinger::createTrack( |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 541 | audio_stream_type_t streamType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 542 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 543 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 544 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 545 | size_t *frameCount, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 546 | audio_output_flags_t *flags, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 547 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 548 | audio_io_handle_t output, |
Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 549 | pid_t pid, |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 550 | pid_t tid, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 551 | audio_session_t *sessionId, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 552 | int clientUid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 553 | status_t *status, |
| 554 | audio_port_handle_t portId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 555 | { |
| 556 | sp<PlaybackThread::Track> track; |
| 557 | sp<TrackHandle> trackHandle; |
| 558 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 559 | status_t lStatus; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 560 | audio_session_t lSessionId; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 561 | |
Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 562 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 563 | if (pid == -1 || !isTrustedCallingUid(callingUid)) { |
| 564 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 565 | ALOGW_IF(pid != -1 && pid != callingPid, |
| 566 | "%s uid %d pid %d tried to pass itself off as pid %d", |
| 567 | __func__, callingUid, callingPid, pid); |
| 568 | pid = callingPid; |
| 569 | } |
| 570 | |
Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 571 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, |
| 572 | // but if someone uses binder directly they could bypass that and cause us to crash |
| 573 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 574 | ALOGE("createTrack() invalid stream type %d", streamType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 575 | lStatus = BAD_VALUE; |
| 576 | goto Exit; |
| 577 | } |
| 578 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 579 | // further sample rate checks are performed by createTrack_l() depending on the thread type |
| 580 | if (sampleRate == 0) { |
| 581 | ALOGE("createTrack() invalid sample rate %u", sampleRate); |
| 582 | lStatus = BAD_VALUE; |
| 583 | goto Exit; |
| 584 | } |
| 585 | |
| 586 | // further channel mask checks are performed by createTrack_l() depending on the thread type |
| 587 | if (!audio_is_output_channel(channelMask)) { |
| 588 | ALOGE("createTrack() invalid channel mask %#x", channelMask); |
| 589 | lStatus = BAD_VALUE; |
| 590 | goto Exit; |
| 591 | } |
| 592 | |
Glenn Kasten | c4b88a8 | 2014-04-30 16:54:30 -0700 | [diff] [blame] | 593 | // further format checks are performed by createTrack_l() depending on the thread type |
| 594 | if (!audio_is_valid_format(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 595 | ALOGE("createTrack() invalid format %#x", format); |
Glenn Kasten | 60a8392 | 2012-06-21 12:56:37 -0700 | [diff] [blame] | 596 | lStatus = BAD_VALUE; |
| 597 | goto Exit; |
| 598 | } |
| 599 | |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 600 | if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) { |
| 601 | ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()"); |
| 602 | lStatus = BAD_VALUE; |
| 603 | goto Exit; |
| 604 | } |
| 605 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 606 | { |
| 607 | Mutex::Autolock _l(mLock); |
| 608 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 609 | if (thread == NULL) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 610 | ALOGE("no playback thread found for output handle %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 611 | lStatus = BAD_VALUE; |
| 612 | goto Exit; |
| 613 | } |
| 614 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 615 | client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 616 | |
Glenn Kasten | e848bd9 | 2014-03-13 15:00:32 -0700 | [diff] [blame] | 617 | PlaybackThread *effectThread = NULL; |
Glenn Kasten | aea7ea0 | 2013-06-26 09:25:47 -0700 | [diff] [blame] | 618 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 619 | if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) { |
| 620 | ALOGE("createTrack() invalid session ID %d", *sessionId); |
| 621 | lStatus = BAD_VALUE; |
| 622 | goto Exit; |
| 623 | } |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 624 | lSessionId = *sessionId; |
Eric Laurent | f436fdc | 2012-05-24 11:07:14 -0700 | [diff] [blame] | 625 | // check if an effect chain with the same session ID is present on another |
| 626 | // output thread and move it here. |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 627 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 628 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 629 | if (mPlaybackThreads.keyAt(i) != output) { |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 630 | uint32_t sessions = t->hasAudioSession(lSessionId); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 631 | if (sessions & ThreadBase::EFFECT_SESSION) { |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 632 | effectThread = t.get(); |
Eric Laurent | f436fdc | 2012-05-24 11:07:14 -0700 | [diff] [blame] | 633 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 634 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 637 | } else { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 638 | // if no audio session id is provided, create one here |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 639 | lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 640 | if (sessionId != NULL) { |
| 641 | *sessionId = lSessionId; |
| 642 | } |
| 643 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 644 | ALOGV("createTrack() lSessionId: %d", lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 645 | |
| 646 | track = thread->createTrack_l(client, streamType, sampleRate, format, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 647 | channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, |
| 648 | clientUid, &lStatus, portId); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 649 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0)); |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 650 | // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 651 | |
| 652 | // move effect chain to this output thread if an effect on same session was waiting |
| 653 | // for a track to be created |
| 654 | if (lStatus == NO_ERROR && effectThread != NULL) { |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 655 | // no risk of deadlock because AudioFlinger::mLock is held |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 656 | Mutex::Autolock _dl(thread->mLock); |
| 657 | Mutex::Autolock _sl(effectThread->mLock); |
| 658 | moveEffectChain_l(lSessionId, effectThread, thread, true); |
| 659 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 660 | |
| 661 | // Look for sync events awaiting for a session to be used. |
Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 662 | for (size_t i = 0; i < mPendingSyncEvents.size(); i++) { |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 663 | if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { |
| 664 | if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 665 | if (lStatus == NO_ERROR) { |
Glenn Kasten | d23eedc | 2012-08-02 13:35:47 -0700 | [diff] [blame] | 666 | (void) track->setSyncEvent(mPendingSyncEvents[i]); |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 667 | } else { |
| 668 | mPendingSyncEvents[i]->cancel(); |
| 669 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 670 | mPendingSyncEvents.removeAt(i); |
| 671 | i--; |
| 672 | } |
| 673 | } |
| 674 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 675 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 676 | setAudioHwSyncForSession_l(thread, lSessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 677 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 678 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 679 | if (lStatus != NO_ERROR) { |
| 680 | // remove local strong reference to Client before deleting the Track so that the |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 681 | // Client destructor is called by the TrackBase destructor with mClientLock held |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 682 | // Don't hold mClientLock when releasing the reference on the track as the |
| 683 | // destructor will acquire it. |
| 684 | { |
| 685 | Mutex::Autolock _cl(mClientLock); |
| 686 | client.clear(); |
| 687 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 688 | track.clear(); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 689 | goto Exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 692 | // return handle to client |
| 693 | trackHandle = new TrackHandle(track); |
| 694 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 695 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 696 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 697 | return trackHandle; |
| 698 | } |
| 699 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 700 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 701 | { |
| 702 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 703 | ThreadBase *thread = checkThread_l(ioHandle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 704 | if (thread == NULL) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 705 | ALOGW("sampleRate() unknown thread %d", ioHandle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | return thread->sampleRate(); |
| 709 | } |
| 710 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 711 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 712 | { |
| 713 | Mutex::Autolock _l(mLock); |
| 714 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 715 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 716 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 717 | return AUDIO_FORMAT_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 718 | } |
| 719 | return thread->format(); |
| 720 | } |
| 721 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 722 | size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 723 | { |
| 724 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 725 | ThreadBase *thread = checkThread_l(ioHandle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 726 | if (thread == NULL) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 727 | ALOGW("frameCount() unknown thread %d", ioHandle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 728 | return 0; |
| 729 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 730 | // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; |
| 731 | // should examine all callers and fix them to handle smaller counts |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 732 | return thread->frameCount(); |
| 733 | } |
| 734 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 735 | size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const |
| 736 | { |
| 737 | Mutex::Autolock _l(mLock); |
| 738 | ThreadBase *thread = checkThread_l(ioHandle); |
| 739 | if (thread == NULL) { |
| 740 | ALOGW("frameCountHAL() unknown thread %d", ioHandle); |
| 741 | return 0; |
| 742 | } |
| 743 | return thread->frameCountHAL(); |
| 744 | } |
| 745 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 746 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 747 | { |
| 748 | Mutex::Autolock _l(mLock); |
| 749 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 750 | if (thread == NULL) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 751 | ALOGW("latency(): no playback thread found for output handle %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | return thread->latency(); |
| 755 | } |
| 756 | |
| 757 | status_t AudioFlinger::setMasterVolume(float value) |
| 758 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 759 | status_t ret = initCheck(); |
| 760 | if (ret != NO_ERROR) { |
| 761 | return ret; |
| 762 | } |
| 763 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 764 | // check calling permissions |
| 765 | if (!settingsAllowed()) { |
| 766 | return PERMISSION_DENIED; |
| 767 | } |
| 768 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 769 | Mutex::Autolock _l(mLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 770 | mMasterVolume = value; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 771 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 772 | // Set master volume in the HALs which support it. |
| 773 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 774 | AutoMutex lock(mHardwareLock); |
| 775 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 776 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 777 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 778 | if (dev->canSetMasterVolume()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 779 | dev->hwDevice()->setMasterVolume(value); |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 780 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 781 | mHardwareStatus = AUDIO_HW_IDLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 782 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 783 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 784 | // Now set the master volume in each playback thread. Playback threads |
| 785 | // assigned to HALs which do not have master volume support will apply |
| 786 | // master volume during the mix operation. Threads with HALs which do |
| 787 | // support master volume will simply ignore the setting. |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 788 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 789 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
| 790 | continue; |
| 791 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 792 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 793 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 794 | |
| 795 | return NO_ERROR; |
| 796 | } |
| 797 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 798 | status_t AudioFlinger::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 799 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 800 | status_t ret = initCheck(); |
| 801 | if (ret != NO_ERROR) { |
| 802 | return ret; |
| 803 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 804 | |
| 805 | // check calling permissions |
| 806 | if (!settingsAllowed()) { |
| 807 | return PERMISSION_DENIED; |
| 808 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 809 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 810 | ALOGW("Illegal value: setMode(%d)", mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 811 | return BAD_VALUE; |
| 812 | } |
| 813 | |
| 814 | { // scope for the lock |
| 815 | AutoMutex lock(mHardwareLock); |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 816 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 817 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 818 | ret = dev->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 819 | mHardwareStatus = AUDIO_HW_IDLE; |
| 820 | } |
| 821 | |
| 822 | if (NO_ERROR == ret) { |
| 823 | Mutex::Autolock _l(mLock); |
| 824 | mMode = mode; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 825 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 826 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | status_t AudioFlinger::setMicMute(bool state) |
| 833 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 834 | status_t ret = initCheck(); |
| 835 | if (ret != NO_ERROR) { |
| 836 | return ret; |
| 837 | } |
| 838 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 839 | // check calling permissions |
| 840 | if (!settingsAllowed()) { |
| 841 | return PERMISSION_DENIED; |
| 842 | } |
| 843 | |
| 844 | AutoMutex lock(mHardwareLock); |
| 845 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | 2f035f5 | 2014-09-14 12:11:52 -0700 | [diff] [blame] | 846 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 847 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 848 | status_t result = dev->setMicMute(state); |
Eric Laurent | 2f035f5 | 2014-09-14 12:11:52 -0700 | [diff] [blame] | 849 | if (result != NO_ERROR) { |
| 850 | ret = result; |
| 851 | } |
| 852 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 853 | mHardwareStatus = AUDIO_HW_IDLE; |
| 854 | return ret; |
| 855 | } |
| 856 | |
| 857 | bool AudioFlinger::getMicMute() const |
| 858 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 859 | status_t ret = initCheck(); |
| 860 | if (ret != NO_ERROR) { |
| 861 | return false; |
| 862 | } |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 863 | bool mute = true; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 864 | bool state = AUDIO_MODE_INVALID; |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 865 | AutoMutex lock(mHardwareLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 866 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 867 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 868 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 869 | status_t result = dev->getMicMute(&state); |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 870 | if (result == NO_ERROR) { |
| 871 | mute = mute && state; |
| 872 | } |
| 873 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 874 | mHardwareStatus = AUDIO_HW_IDLE; |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 875 | |
| 876 | return mute; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | status_t AudioFlinger::setMasterMute(bool muted) |
| 880 | { |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 881 | status_t ret = initCheck(); |
| 882 | if (ret != NO_ERROR) { |
| 883 | return ret; |
| 884 | } |
| 885 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 886 | // check calling permissions |
| 887 | if (!settingsAllowed()) { |
| 888 | return PERMISSION_DENIED; |
| 889 | } |
| 890 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 891 | Mutex::Autolock _l(mLock); |
| 892 | mMasterMute = muted; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 893 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 894 | // Set master mute in the HALs which support it. |
| 895 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 896 | AutoMutex lock(mHardwareLock); |
| 897 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 898 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 899 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; |
| 900 | if (dev->canSetMasterMute()) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 901 | dev->hwDevice()->setMasterMute(muted); |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 902 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 903 | mHardwareStatus = AUDIO_HW_IDLE; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 904 | } |
| 905 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 906 | // Now set the master mute in each playback thread. Playback threads |
| 907 | // assigned to HALs which do not have master mute support will apply master |
| 908 | // mute during the mix operation. Threads with HALs which do support master |
| 909 | // mute will simply ignore the setting. |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 910 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 911 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
| 912 | continue; |
| 913 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 914 | mPlaybackThreads.valueAt(i)->setMasterMute(muted); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 915 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 916 | |
| 917 | return NO_ERROR; |
| 918 | } |
| 919 | |
| 920 | float AudioFlinger::masterVolume() const |
| 921 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 922 | Mutex::Autolock _l(mLock); |
| 923 | return masterVolume_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | bool AudioFlinger::masterMute() const |
| 927 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 928 | Mutex::Autolock _l(mLock); |
| 929 | return masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 930 | } |
| 931 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 932 | float AudioFlinger::masterVolume_l() const |
| 933 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 934 | return mMasterVolume; |
| 935 | } |
| 936 | |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 937 | bool AudioFlinger::masterMute_l() const |
| 938 | { |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 939 | return mMasterMute; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 942 | status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const |
| 943 | { |
| 944 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
| 945 | ALOGW("setStreamVolume() invalid stream %d", stream); |
| 946 | return BAD_VALUE; |
| 947 | } |
| 948 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
| 949 | if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) { |
| 950 | ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream); |
| 951 | return PERMISSION_DENIED; |
| 952 | } |
| 953 | |
| 954 | return NO_ERROR; |
| 955 | } |
| 956 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 957 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, |
| 958 | audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 959 | { |
| 960 | // check calling permissions |
| 961 | if (!settingsAllowed()) { |
| 962 | return PERMISSION_DENIED; |
| 963 | } |
| 964 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 965 | status_t status = checkStreamType(stream); |
| 966 | if (status != NO_ERROR) { |
| 967 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 968 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 969 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 970 | |
| 971 | AutoMutex lock(mLock); |
| 972 | PlaybackThread *thread = NULL; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 973 | if (output != AUDIO_IO_HANDLE_NONE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 974 | thread = checkPlaybackThread_l(output); |
| 975 | if (thread == NULL) { |
| 976 | return BAD_VALUE; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | mStreamTypes[stream].volume = value; |
| 981 | |
| 982 | if (thread == NULL) { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 983 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 984 | mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 985 | } |
| 986 | } else { |
| 987 | thread->setStreamVolume(stream, value); |
| 988 | } |
| 989 | |
| 990 | return NO_ERROR; |
| 991 | } |
| 992 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 993 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 994 | { |
| 995 | // check calling permissions |
| 996 | if (!settingsAllowed()) { |
| 997 | return PERMISSION_DENIED; |
| 998 | } |
| 999 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1000 | status_t status = checkStreamType(stream); |
| 1001 | if (status != NO_ERROR) { |
| 1002 | return status; |
| 1003 | } |
| 1004 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH"); |
| 1005 | |
| 1006 | if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1007 | ALOGE("setStreamMute() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1008 | return BAD_VALUE; |
| 1009 | } |
| 1010 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 1011 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1012 | mStreamTypes[stream].mute = muted; |
Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 1013 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1014 | mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1015 | |
| 1016 | return NO_ERROR; |
| 1017 | } |
| 1018 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1019 | float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1020 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1021 | status_t status = checkStreamType(stream); |
| 1022 | if (status != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1023 | return 0.0f; |
| 1024 | } |
| 1025 | |
| 1026 | AutoMutex lock(mLock); |
| 1027 | float volume; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1028 | if (output != AUDIO_IO_HANDLE_NONE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1029 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 1030 | if (thread == NULL) { |
| 1031 | return 0.0f; |
| 1032 | } |
| 1033 | volume = thread->streamVolume(stream); |
| 1034 | } else { |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1035 | volume = streamVolume_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | return volume; |
| 1039 | } |
| 1040 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1041 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1042 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1043 | status_t status = checkStreamType(stream); |
| 1044 | if (status != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1045 | return true; |
| 1046 | } |
| 1047 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1048 | AutoMutex lock(mLock); |
| 1049 | return streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1052 | |
| 1053 | void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs) |
| 1054 | { |
| 1055 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1056 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 1057 | } |
| 1058 | } |
| 1059 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1060 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1061 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1062 | ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d", |
| 1063 | ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1064 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1065 | // check calling permissions |
| 1066 | if (!settingsAllowed()) { |
| 1067 | return PERMISSION_DENIED; |
| 1068 | } |
| 1069 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1070 | // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface |
| 1071 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1072 | Mutex::Autolock _l(mLock); |
Eric Laurent | d21bcd2 | 2016-09-08 18:25:54 -0700 | [diff] [blame] | 1073 | // result will remain NO_INIT if no audio device is present |
| 1074 | status_t final_result = NO_INIT; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1075 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1076 | AutoMutex lock(mHardwareLock); |
| 1077 | mHardwareStatus = AUDIO_HW_SET_PARAMETER; |
| 1078 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1079 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 1080 | status_t result = dev->setParameters(keyValuePairs); |
Eric Laurent | d21bcd2 | 2016-09-08 18:25:54 -0700 | [diff] [blame] | 1081 | // return success if at least one audio device accepts the parameters as not all |
| 1082 | // HALs are requested to support all parameters. If no audio device supports the |
| 1083 | // requested parameters, the last error is reported. |
| 1084 | if (final_result != NO_ERROR) { |
| 1085 | final_result = result; |
| 1086 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1087 | } |
| 1088 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1089 | } |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1090 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings |
| 1091 | AudioParameter param = AudioParameter(keyValuePairs); |
| 1092 | String8 value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1093 | if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) { |
| 1094 | bool btNrecIsOff = (value == AudioParameter::valueOff); |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 1095 | if (mBtNrecIsOff != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1096 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1097 | sp<RecordThread> thread = mRecordThreads.valueAt(i); |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 1098 | audio_devices_t device = thread->inDevice(); |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1099 | bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; |
| 1100 | // collect all of the thread's session IDs |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1101 | KeyedVector<audio_session_t, bool> ids = thread->sessionIds(); |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1102 | // suspend effects associated with those session IDs |
| 1103 | for (size_t j = 0; j < ids.size(); ++j) { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1104 | audio_session_t sessionId = ids.keyAt(j); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1105 | thread->setEffectSuspended(FX_IID_AEC, |
| 1106 | suspend, |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1107 | sessionId); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1108 | thread->setEffectSuspended(FX_IID_NS, |
| 1109 | suspend, |
Glenn Kasten | 510a3d6 | 2012-07-16 14:24:34 -0700 | [diff] [blame] | 1110 | sessionId); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1111 | } |
| 1112 | } |
Eric Laurent | bee5337 | 2011-08-29 12:42:48 -0700 | [diff] [blame] | 1113 | mBtNrecIsOff = btNrecIsOff; |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1114 | } |
| 1115 | } |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1116 | String8 screenState; |
| 1117 | if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) { |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1118 | bool isOff = (screenState == AudioParameter::valueOff); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1119 | if (isOff != (AudioFlinger::mScreenState & 1)) { |
| 1120 | AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff; |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1121 | } |
| 1122 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1123 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 1127 | // and the thread is exited once the lock is released |
| 1128 | sp<ThreadBase> thread; |
| 1129 | { |
| 1130 | Mutex::Autolock _l(mLock); |
| 1131 | thread = checkPlaybackThread_l(ioHandle); |
Glenn Kasten | d5903ec | 2012-03-18 10:33:27 -0700 | [diff] [blame] | 1132 | if (thread == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1133 | thread = checkRecordThread_l(ioHandle); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 1134 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1135 | // indicate output device change to all input threads for pre processing |
| 1136 | AudioParameter param = AudioParameter(keyValuePairs); |
| 1137 | int value; |
Eric Laurent | 89d94e7 | 2012-03-16 20:37:59 -0700 | [diff] [blame] | 1138 | if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && |
| 1139 | (value != 0)) { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1140 | broacastParametersToRecordThreads_l(keyValuePairs); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1141 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1142 | } |
| 1143 | } |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1144 | if (thread != 0) { |
| 1145 | return thread->setParameters(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1146 | } |
| 1147 | return BAD_VALUE; |
| 1148 | } |
| 1149 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1150 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1151 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1152 | ALOGVV("getParameters() io %d, keys %s, calling pid %d", |
| 1153 | ioHandle, keys.string(), IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1154 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1155 | Mutex::Autolock _l(mLock); |
| 1156 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1157 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1158 | String8 out_s8; |
| 1159 | |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1160 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1161 | String8 s; |
| 1162 | status_t result; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1163 | { |
| 1164 | AutoMutex lock(mHardwareLock); |
| 1165 | mHardwareStatus = AUDIO_HW_GET_PARAMETER; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1166 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
| 1167 | result = dev->getParameters(keys, &s); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1168 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1169 | } |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1170 | if (result == OK) out_s8 += s; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1171 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1172 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1175 | PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); |
| 1176 | if (playbackThread != NULL) { |
| 1177 | return playbackThread->getParameters(keys); |
| 1178 | } |
| 1179 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 1180 | if (recordThread != NULL) { |
| 1181 | return recordThread->getParameters(keys); |
| 1182 | } |
| 1183 | return String8(""); |
| 1184 | } |
| 1185 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 1186 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| 1187 | audio_channel_mask_t channelMask) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1188 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1189 | status_t ret = initCheck(); |
| 1190 | if (ret != NO_ERROR) { |
| 1191 | return 0; |
| 1192 | } |
Eric Laurent | cdf924a | 2016-02-04 15:57:56 -0800 | [diff] [blame] | 1193 | if ((sampleRate == 0) || |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1194 | !audio_is_valid_format(format) || !audio_has_proportional_frames(format) || |
Eric Laurent | cdf924a | 2016-02-04 15:57:56 -0800 | [diff] [blame] | 1195 | !audio_is_input_channel(channelMask)) { |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1196 | return 0; |
| 1197 | } |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1198 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1199 | AutoMutex lock(mHardwareLock); |
| 1200 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1201 | audio_config_t config, proposed; |
| 1202 | memset(&proposed, 0, sizeof(proposed)); |
| 1203 | proposed.sample_rate = sampleRate; |
| 1204 | proposed.channel_mask = channelMask; |
| 1205 | proposed.format = format; |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1206 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1207 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1208 | size_t frames; |
| 1209 | for (;;) { |
| 1210 | // Note: config is currently a const parameter for get_input_buffer_size() |
| 1211 | // but we use a copy from proposed in case config changes from the call. |
| 1212 | config = proposed; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1213 | status_t result = dev->getInputBufferSize(&config, &frames); |
| 1214 | if (result == OK && frames != 0) { |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1215 | break; // hal success, config is the result |
| 1216 | } |
| 1217 | // change one parameter of the configuration each iteration to a more "common" value |
| 1218 | // to see if the device will support it. |
| 1219 | if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) { |
| 1220 | proposed.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1221 | } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as |
| 1222 | proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw? |
| 1223 | } else { |
| 1224 | ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, " |
| 1225 | "format %#x, channelMask 0x%X", |
| 1226 | sampleRate, format, channelMask); |
| 1227 | break; // retries failed, break out of loop with frames == 0. |
| 1228 | } |
| 1229 | } |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1230 | mHardwareStatus = AUDIO_HW_IDLE; |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1231 | if (frames > 0 && config.sample_rate != sampleRate) { |
| 1232 | frames = destinationFramesPossible(frames, sampleRate, config.sample_rate); |
| 1233 | } |
| 1234 | return frames; // may be converted to bytes at the Java level. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 1237 | uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1238 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1239 | Mutex::Autolock _l(mLock); |
| 1240 | |
| 1241 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 1242 | if (recordThread != NULL) { |
| 1243 | return recordThread->getInputFramesLost(); |
| 1244 | } |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | status_t AudioFlinger::setVoiceVolume(float value) |
| 1249 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1250 | status_t ret = initCheck(); |
| 1251 | if (ret != NO_ERROR) { |
| 1252 | return ret; |
| 1253 | } |
| 1254 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1255 | // check calling permissions |
| 1256 | if (!settingsAllowed()) { |
| 1257 | return PERMISSION_DENIED; |
| 1258 | } |
| 1259 | |
| 1260 | AutoMutex lock(mHardwareLock); |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1261 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1262 | mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1263 | ret = dev->setVoiceVolume(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1264 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1265 | |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1269 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1270 | audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1271 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1272 | Mutex::Autolock _l(mLock); |
| 1273 | |
| 1274 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 1275 | if (playbackThread != NULL) { |
| 1276 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 1277 | } |
| 1278 | |
| 1279 | return BAD_VALUE; |
| 1280 | } |
| 1281 | |
| 1282 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 1283 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1284 | Mutex::Autolock _l(mLock); |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1285 | if (client == 0) { |
| 1286 | return; |
| 1287 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1288 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1289 | { |
| 1290 | Mutex::Autolock _cl(mClientLock); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1291 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 1292 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 1293 | client, |
| 1294 | pid); |
| 1295 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1296 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1297 | mNotificationClients.add(pid, notificationClient); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1298 | |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1299 | sp<IBinder> binder = IInterface::asBinder(client); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1300 | binder->linkToDeath(notificationClient); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1301 | } |
| 1302 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1303 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1304 | // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 1305 | // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock. |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1306 | // the config change is always sent from playback or record threads to avoid deadlock |
| 1307 | // with AudioSystem::gLock |
| 1308 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1309 | mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid); |
| 1310 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1311 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1312 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1313 | mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 1318 | { |
| 1319 | Mutex::Autolock _l(mLock); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1320 | { |
| 1321 | Mutex::Autolock _cl(mClientLock); |
| 1322 | mNotificationClients.removeItem(pid); |
| 1323 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1324 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1325 | ALOGV("%d died, releasing its sessions", pid); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1326 | size_t num = mAudioSessionRefs.size(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1327 | bool removed = false; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1328 | for (size_t i = 0; i < num; ) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1329 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1330 | ALOGV(" pid %d @ %zu", ref->mPid, i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 1331 | if (ref->mPid == pid) { |
| 1332 | ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1333 | mAudioSessionRefs.removeAt(i); |
| 1334 | delete ref; |
| 1335 | removed = true; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1336 | num--; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1337 | } else { |
| 1338 | i++; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | if (removed) { |
| 1342 | purgeStaleEffects_l(); |
| 1343 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1346 | void AudioFlinger::ioConfigChanged(audio_io_config_event event, |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1347 | const sp<AudioIoDescriptor>& ioDesc, |
| 1348 | pid_t pid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1349 | { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1350 | Mutex::Autolock _l(mClientLock); |
| 1351 | size_t size = mNotificationClients.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1352 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1353 | if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) { |
| 1354 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc); |
| 1355 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1359 | // removeClient_l() must be called with AudioFlinger::mClientLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1360 | void AudioFlinger::removeClient_l(pid_t pid) |
| 1361 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1362 | ALOGV("removeClient_l() pid %d, calling pid %d", pid, |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1363 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1364 | mClients.removeItem(pid); |
| 1365 | } |
| 1366 | |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1367 | // getEffectThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1368 | sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId, |
| 1369 | int EffectId) |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1370 | { |
| 1371 | sp<PlaybackThread> thread; |
| 1372 | |
| 1373 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1374 | if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) { |
| 1375 | ALOG_ASSERT(thread == 0); |
| 1376 | thread = mPlaybackThreads.valueAt(i); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | return thread; |
| 1381 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1382 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1383 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1384 | |
| 1385 | // ---------------------------------------------------------------------------- |
| 1386 | |
| 1387 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 1388 | : RefBase(), |
| 1389 | mAudioFlinger(audioFlinger), |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1390 | mPid(pid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1391 | { |
Sumit Bhattacharya | cd64e0e | 2016-02-11 01:37:20 +0530 | [diff] [blame] | 1392 | size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0); |
| 1393 | heapSize *= 1024; |
| 1394 | if (!heapSize) { |
| 1395 | heapSize = kClientSharedHeapSizeBytes; |
| 1396 | // Increase heap size on non low ram devices to limit risk of reconnection failure for |
| 1397 | // invalidated tracks |
| 1398 | if (!audioFlinger->isLowRamDevice()) { |
| 1399 | heapSize *= kClientSharedHeapSizeMultiplier; |
| 1400 | } |
Eric Laurent | da73b6c | 2015-08-20 16:18:53 -0700 | [diff] [blame] | 1401 | } |
| 1402 | mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1405 | // Client destructor must be called with AudioFlinger::mClientLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1406 | AudioFlinger::Client::~Client() |
| 1407 | { |
| 1408 | mAudioFlinger->removeClient_l(mPid); |
| 1409 | } |
| 1410 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 1411 | sp<MemoryDealer> AudioFlinger::Client::heap() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1412 | { |
| 1413 | return mMemoryDealer; |
| 1414 | } |
| 1415 | |
| 1416 | // ---------------------------------------------------------------------------- |
| 1417 | |
| 1418 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 1419 | const sp<IAudioFlingerClient>& client, |
| 1420 | pid_t pid) |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1421 | : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1422 | { |
| 1423 | } |
| 1424 | |
| 1425 | AudioFlinger::NotificationClient::~NotificationClient() |
| 1426 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1429 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1430 | { |
| 1431 | sp<NotificationClient> keep(this); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 1432 | mAudioFlinger->removeNotificationClient(mPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1435 | |
| 1436 | // ---------------------------------------------------------------------------- |
| 1437 | |
| 1438 | sp<IAudioRecord> AudioFlinger::openRecord( |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1439 | audio_io_handle_t input, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1440 | uint32_t sampleRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1441 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 1442 | audio_channel_mask_t channelMask, |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 1443 | const String16& opPackageName, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1444 | size_t *frameCount, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1445 | audio_input_flags_t *flags, |
Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1446 | pid_t pid, |
Glenn Kasten | 1879fff | 2012-07-11 15:36:59 -0700 | [diff] [blame] | 1447 | pid_t tid, |
Jean-Michel Trivi | 4cb6683 | 2015-05-01 18:34:17 -0700 | [diff] [blame] | 1448 | int clientUid, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1449 | audio_session_t *sessionId, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 1450 | size_t *notificationFrames, |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1451 | sp<IMemory>& cblk, |
| 1452 | sp<IMemory>& buffers, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1453 | status_t *status, |
| 1454 | audio_port_handle_t portId) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1455 | { |
| 1456 | sp<RecordThread::RecordTrack> recordTrack; |
| 1457 | sp<RecordHandle> recordHandle; |
| 1458 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1459 | status_t lStatus; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1460 | audio_session_t lSessionId; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1461 | |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1462 | cblk.clear(); |
| 1463 | buffers.clear(); |
| 1464 | |
Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1465 | bool updatePid = (pid == -1); |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1466 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1467 | if (!isTrustedCallingUid(callingUid)) { |
Andy Hung | 879db19 | 2016-01-05 16:00:34 -0800 | [diff] [blame] | 1468 | ALOGW_IF((uid_t)clientUid != callingUid, |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1469 | "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid); |
| 1470 | clientUid = callingUid; |
Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1471 | updatePid = true; |
| 1472 | } |
| 1473 | |
| 1474 | if (updatePid) { |
| 1475 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 1476 | ALOGW_IF(pid != -1 && pid != callingPid, |
| 1477 | "%s uid %d pid %d tried to pass itself off as pid %d", |
| 1478 | __func__, callingUid, callingPid, pid); |
| 1479 | pid = callingPid; |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1482 | // check calling permissions |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1483 | if (!recordingAllowed(opPackageName, tid, clientUid)) { |
Glenn Kasten | e93cf2c | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1484 | ALOGE("openRecord() permission denied: recording not allowed"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1485 | lStatus = PERMISSION_DENIED; |
| 1486 | goto Exit; |
| 1487 | } |
| 1488 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 1489 | // further sample rate checks are performed by createRecordTrack_l() |
| 1490 | if (sampleRate == 0) { |
| 1491 | ALOGE("openRecord() invalid sample rate %u", sampleRate); |
| 1492 | lStatus = BAD_VALUE; |
| 1493 | goto Exit; |
| 1494 | } |
| 1495 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1496 | // we don't yet support anything other than linear PCM |
| 1497 | if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 1498 | ALOGE("openRecord() invalid format %#x", format); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 1499 | lStatus = BAD_VALUE; |
| 1500 | goto Exit; |
| 1501 | } |
| 1502 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 1503 | // further channel mask checks are performed by createRecordTrack_l() |
| 1504 | if (!audio_is_input_channel(channelMask)) { |
| 1505 | ALOGE("openRecord() invalid channel mask %#x", channelMask); |
| 1506 | lStatus = BAD_VALUE; |
| 1507 | goto Exit; |
| 1508 | } |
| 1509 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 1510 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1511 | Mutex::Autolock _l(mLock); |
Glenn Kasten | e848bd9 | 2014-03-13 15:00:32 -0700 | [diff] [blame] | 1512 | RecordThread *thread = checkRecordThread_l(input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1513 | if (thread == NULL) { |
Glenn Kasten | e93cf2c | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 1514 | ALOGE("openRecord() checkRecordThread_l failed"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1515 | lStatus = BAD_VALUE; |
| 1516 | goto Exit; |
| 1517 | } |
| 1518 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1519 | client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1520 | |
Glenn Kasten | aea7ea0 | 2013-06-26 09:25:47 -0700 | [diff] [blame] | 1521 | if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 1522 | if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) { |
| 1523 | lStatus = BAD_VALUE; |
| 1524 | goto Exit; |
| 1525 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1526 | lSessionId = *sessionId; |
| 1527 | } else { |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1528 | // if no audio session id is provided, create one here |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1529 | lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1530 | if (sessionId != NULL) { |
| 1531 | *sessionId = lSessionId; |
| 1532 | } |
| 1533 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 1534 | ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input); |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 1535 | |
Glenn Kasten | 1879fff | 2012-07-11 15:36:59 -0700 | [diff] [blame] | 1536 | recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask, |
Glenn Kasten | 7df8c0b | 2014-07-03 12:23:29 -0700 | [diff] [blame] | 1537 | frameCount, lSessionId, notificationFrames, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1538 | clientUid, flags, tid, &lStatus, portId); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 1539 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0)); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 1540 | |
| 1541 | if (lStatus == NO_ERROR) { |
| 1542 | // Check if one effect chain was awaiting for an AudioRecord to be created on this |
| 1543 | // session and move it to this thread. |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1544 | sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 1545 | if (chain != 0) { |
| 1546 | Mutex::Autolock _l(thread->mLock); |
| 1547 | thread->addEffectChain_l(chain); |
| 1548 | } |
| 1549 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1550 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 1551 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1552 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1553 | // remove local strong reference to Client before deleting the RecordTrack so that the |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1554 | // Client destructor is called by the TrackBase destructor with mClientLock held |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 1555 | // Don't hold mClientLock when releasing the reference on the track as the |
| 1556 | // destructor will acquire it. |
| 1557 | { |
| 1558 | Mutex::Autolock _cl(mClientLock); |
| 1559 | client.clear(); |
| 1560 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1561 | recordTrack.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1562 | goto Exit; |
| 1563 | } |
| 1564 | |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1565 | cblk = recordTrack->getCblk(); |
| 1566 | buffers = recordTrack->getBuffers(); |
| 1567 | |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 1568 | // return handle to client |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1569 | recordHandle = new RecordHandle(recordTrack); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1570 | |
| 1571 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1572 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1573 | return recordHandle; |
| 1574 | } |
| 1575 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 1576 | |
| 1577 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1578 | // ---------------------------------------------------------------------------- |
| 1579 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1580 | audio_module_handle_t AudioFlinger::loadHwModule(const char *name) |
| 1581 | { |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1582 | if (name == NULL) { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 1583 | return AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1584 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1585 | if (!settingsAllowed()) { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 1586 | return AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1587 | } |
| 1588 | Mutex::Autolock _l(mLock); |
| 1589 | return loadHwModule_l(name); |
| 1590 | } |
| 1591 | |
| 1592 | // loadHwModule_l() must be called with AudioFlinger::mLock held |
| 1593 | audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) |
| 1594 | { |
| 1595 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 1596 | if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { |
| 1597 | ALOGW("loadHwModule() module %s already loaded", name); |
| 1598 | return mAudioHwDevs.keyAt(i); |
| 1599 | } |
| 1600 | } |
| 1601 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1602 | sp<DeviceHalInterface> dev; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1603 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1604 | int rc = mDevicesFactoryHal->openDevice(name, &dev); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1605 | if (rc) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1606 | ALOGE("loadHwModule() error %d loading module %s", rc, name); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 1607 | return AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | mHardwareStatus = AUDIO_HW_INIT; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1611 | rc = dev->initCheck(); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1612 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1613 | if (rc) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1614 | ALOGE("loadHwModule() init check error %d for module %s", rc, name); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 1615 | return AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1618 | // Check and cache this HAL's level of support for master mute and master |
| 1619 | // volume. If this is the first HAL opened, and it supports the get |
| 1620 | // methods, use the initial values provided by the HAL as the current |
| 1621 | // master mute and volume settings. |
| 1622 | |
| 1623 | AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0); |
| 1624 | { // scope for auto-lock pattern |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1625 | AutoMutex lock(mHardwareLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1626 | |
| 1627 | if (0 == mAudioHwDevs.size()) { |
| 1628 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1629 | float mv; |
| 1630 | if (OK == dev->getMasterVolume(&mv)) { |
| 1631 | mMasterVolume = mv; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1635 | bool mm; |
| 1636 | if (OK == dev->getMasterMute(&mm)) { |
| 1637 | mMasterMute = mm; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1638 | } |
| 1639 | } |
| 1640 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1641 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1642 | if (OK == dev->setMasterVolume(mMasterVolume)) { |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1643 | flags = static_cast<AudioHwDevice::Flags>(flags | |
| 1644 | AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME); |
| 1645 | } |
| 1646 | |
| 1647 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1648 | if (OK == dev->setMasterMute(mMasterMute)) { |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1649 | flags = static_cast<AudioHwDevice::Flags>(flags | |
| 1650 | AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE); |
| 1651 | } |
| 1652 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1653 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1654 | } |
| 1655 | |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 1656 | audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1657 | mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags)); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1658 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1659 | ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1660 | |
| 1661 | return handle; |
| 1662 | |
| 1663 | } |
| 1664 | |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1665 | // ---------------------------------------------------------------------------- |
| 1666 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 1667 | uint32_t AudioFlinger::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1668 | { |
| 1669 | Mutex::Autolock _l(mLock); |
Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 1670 | PlaybackThread *thread = fastPlaybackThread_l(); |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1671 | return thread != NULL ? thread->sampleRate() : 0; |
| 1672 | } |
| 1673 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 1674 | size_t AudioFlinger::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1675 | { |
| 1676 | Mutex::Autolock _l(mLock); |
Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 1677 | PlaybackThread *thread = fastPlaybackThread_l(); |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1678 | return thread != NULL ? thread->frameCountHAL() : 0; |
| 1679 | } |
| 1680 | |
| 1681 | // ---------------------------------------------------------------------------- |
| 1682 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1683 | status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice) |
| 1684 | { |
| 1685 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1686 | if (uid != AID_SYSTEM) { |
| 1687 | return PERMISSION_DENIED; |
| 1688 | } |
| 1689 | Mutex::Autolock _l(mLock); |
| 1690 | if (mIsDeviceTypeKnown) { |
| 1691 | return INVALID_OPERATION; |
| 1692 | } |
| 1693 | mIsLowRamDevice = isLowRamDevice; |
| 1694 | mIsDeviceTypeKnown = true; |
| 1695 | return NO_ERROR; |
| 1696 | } |
| 1697 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1698 | audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId) |
| 1699 | { |
| 1700 | Mutex::Autolock _l(mLock); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1701 | |
| 1702 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); |
| 1703 | if (index >= 0) { |
| 1704 | ALOGV("getAudioHwSyncForSession found ID %d for session %d", |
| 1705 | mHwAvSyncIds.valueAt(index), sessionId); |
| 1706 | return mHwAvSyncIds.valueAt(index); |
| 1707 | } |
| 1708 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1709 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1710 | if (dev == NULL) { |
| 1711 | return AUDIO_HW_SYNC_INVALID; |
| 1712 | } |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1713 | String8 reply; |
| 1714 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1715 | if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1716 | param = AudioParameter(reply); |
| 1717 | } |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1718 | |
| 1719 | int value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1720 | if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) { |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1721 | ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId); |
| 1722 | return AUDIO_HW_SYNC_INVALID; |
| 1723 | } |
| 1724 | |
| 1725 | // allow only one session for a given HW A/V sync ID. |
| 1726 | for (size_t i = 0; i < mHwAvSyncIds.size(); i++) { |
| 1727 | if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) { |
| 1728 | ALOGV("getAudioHwSyncForSession removing ID %d for session %d", |
| 1729 | value, mHwAvSyncIds.keyAt(i)); |
| 1730 | mHwAvSyncIds.removeItemsAt(i); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1731 | break; |
| 1732 | } |
| 1733 | } |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1734 | |
| 1735 | mHwAvSyncIds.add(sessionId, value); |
| 1736 | |
| 1737 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1738 | sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i); |
| 1739 | uint32_t sessions = thread->hasAudioSession(sessionId); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 1740 | if (sessions & ThreadBase::TRACK_SESSION) { |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1741 | AudioParameter param = AudioParameter(); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1742 | param.addInt(String8(AudioParameter::keyStreamHwAvSync), value); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1743 | thread->setParameters(param.toString()); |
| 1744 | break; |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId); |
| 1749 | return (audio_hw_sync_t)value; |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1752 | status_t AudioFlinger::systemReady() |
| 1753 | { |
| 1754 | Mutex::Autolock _l(mLock); |
| 1755 | ALOGI("%s", __FUNCTION__); |
| 1756 | if (mSystemReady) { |
| 1757 | ALOGW("%s called twice", __FUNCTION__); |
| 1758 | return NO_ERROR; |
| 1759 | } |
| 1760 | mSystemReady = true; |
| 1761 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1762 | ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get(); |
| 1763 | thread->systemReady(); |
| 1764 | } |
| 1765 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1766 | ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get(); |
| 1767 | thread->systemReady(); |
| 1768 | } |
| 1769 | return NO_ERROR; |
| 1770 | } |
| 1771 | |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1772 | // setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held |
| 1773 | void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId) |
| 1774 | { |
| 1775 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); |
| 1776 | if (index >= 0) { |
| 1777 | audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index); |
| 1778 | ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId); |
| 1779 | AudioParameter param = AudioParameter(); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1780 | param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 1781 | thread->setParameters(param.toString()); |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1786 | // ---------------------------------------------------------------------------- |
| 1787 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1788 | |
| 1789 | sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1790 | audio_io_handle_t *output, |
| 1791 | audio_config_t *config, |
| 1792 | audio_devices_t devices, |
| 1793 | const String8& address, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1794 | audio_output_flags_t flags) |
| 1795 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1796 | AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1797 | if (outHwDev == NULL) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1798 | return 0; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1801 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 1802 | *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT); |
| 1803 | } else { |
| 1804 | // Audio Policy does not currently request a specific output handle. |
| 1805 | // If this is ever needed, see openInput_l() for example code. |
| 1806 | ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output); |
| 1807 | return 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1808 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1809 | |
| 1810 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 1811 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1812 | // FOR TESTING ONLY: |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1813 | // This if statement allows overriding the audio policy settings |
| 1814 | // and forcing a specific format or channel mask to the HAL/Sink device for testing. |
| 1815 | if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) { |
| 1816 | // Check only for Normal Mixing mode |
| 1817 | if (kEnableExtendedPrecision) { |
| 1818 | // Specify format (uncomment one below to choose) |
| 1819 | //config->format = AUDIO_FORMAT_PCM_FLOAT; |
| 1820 | //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 1821 | //config->format = AUDIO_FORMAT_PCM_32_BIT; |
| 1822 | //config->format = AUDIO_FORMAT_PCM_8_24_BIT; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1823 | // ALOGV("openOutput_l() upgrading format to %#08x", config->format); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1824 | } |
| 1825 | if (kEnableExtendedChannels) { |
| 1826 | // Specify channel mask (uncomment one below to choose) |
| 1827 | //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch |
| 1828 | //config->channel_mask = audio_channel_mask_from_representation_and_bits( |
| 1829 | // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example |
| 1830 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1833 | AudioStreamOut *outputStream = NULL; |
| 1834 | status_t status = outHwDev->openOutputStream( |
| 1835 | &outputStream, |
| 1836 | *output, |
| 1837 | devices, |
| 1838 | flags, |
| 1839 | config, |
| 1840 | address.string()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1841 | |
| 1842 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1843 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1844 | if (status == NO_ERROR) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1845 | |
| 1846 | PlaybackThread *thread; |
| 1847 | if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1848 | thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1849 | ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1850 | } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) |
| 1851 | || !isValidPcmSinkFormat(config->format) |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 1852 | || !isValidPcmSinkChannelMask(config->channel_mask)) { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1853 | thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1854 | ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1855 | } else { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1856 | thread = new MixerThread(this, outputStream, *output, devices, mSystemReady); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1857 | ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1858 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1859 | mPlaybackThreads.add(*output, thread); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1860 | return thread; |
| 1861 | } |
| 1862 | |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1866 | status_t AudioFlinger::openOutput(audio_module_handle_t module, |
| 1867 | audio_io_handle_t *output, |
| 1868 | audio_config_t *config, |
| 1869 | audio_devices_t *devices, |
| 1870 | const String8& address, |
| 1871 | uint32_t *latencyMs, |
| 1872 | audio_output_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1873 | { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1874 | ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x", |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1875 | module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1876 | (devices != NULL) ? *devices : 0, |
| 1877 | config->sample_rate, |
| 1878 | config->format, |
| 1879 | config->channel_mask, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1880 | flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1881 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1882 | if (*devices == AUDIO_DEVICE_NONE) { |
| 1883 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1884 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1885 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1886 | Mutex::Autolock _l(mLock); |
| 1887 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1888 | sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1889 | if (thread != 0) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1890 | *latencyMs = thread->latency(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1891 | |
| 1892 | // notify client processes of the new output creation |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1893 | thread->ioConfigChanged(AUDIO_OUTPUT_OPENED); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1894 | |
| 1895 | // the first primary output opened designates the primary hw device |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 1896 | if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1897 | ALOGI("Using module %d has the primary audio interface", module); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1898 | mPrimaryHardwareDev = thread->getOutput()->audioHwDev; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1899 | |
| 1900 | AutoMutex lock(mHardwareLock); |
| 1901 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1902 | mPrimaryHardwareDev->hwDevice()->setMode(mMode); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1903 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1904 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1905 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1908 | return NO_INIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1911 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, |
| 1912 | audio_io_handle_t output2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1913 | { |
| 1914 | Mutex::Autolock _l(mLock); |
| 1915 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 1916 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 1917 | |
| 1918 | if (thread1 == NULL || thread2 == NULL) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1919 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, |
| 1920 | output2); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1921 | return AUDIO_IO_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 1924 | audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1925 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1926 | thread->addOutputTrack(thread2); |
| 1927 | mPlaybackThreads.add(id, thread); |
| 1928 | // notify client processes of the new output creation |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1929 | thread->ioConfigChanged(AUDIO_OUTPUT_OPENED); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1930 | return id; |
| 1931 | } |
| 1932 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1933 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1934 | { |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 1935 | return closeOutput_nonvirtual(output); |
| 1936 | } |
| 1937 | |
| 1938 | status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output) |
| 1939 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1940 | // keep strong reference on the playback thread so that |
| 1941 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1942 | sp<PlaybackThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1943 | { |
| 1944 | Mutex::Autolock _l(mLock); |
| 1945 | thread = checkPlaybackThread_l(output); |
| 1946 | if (thread == NULL) { |
| 1947 | return BAD_VALUE; |
| 1948 | } |
| 1949 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1950 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1951 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1952 | if (thread->type() == ThreadBase::MIXER) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1953 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1954 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1955 | DuplicatingThread *dupThread = |
| 1956 | (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1957 | dupThread->removeOutputTrack((MixerThread *)thread.get()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | |
| 1963 | mPlaybackThreads.removeItem(output); |
| 1964 | // save all effects to the default thread |
| 1965 | if (mPlaybackThreads.size()) { |
| 1966 | PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0)); |
| 1967 | if (dstThread != NULL) { |
| 1968 | // audioflinger lock is held here so the acquisition order of thread locks does not |
| 1969 | // matter |
| 1970 | Mutex::Autolock _dl(dstThread->mLock); |
| 1971 | Mutex::Autolock _sl(thread->mLock); |
| 1972 | Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l(); |
| 1973 | for (size_t i = 0; i < effectChains.size(); i ++) { |
| 1974 | moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1975 | } |
| 1976 | } |
| 1977 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1978 | const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor(); |
| 1979 | ioDesc->mIoHandle = output; |
| 1980 | ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1981 | } |
| 1982 | thread->exit(); |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 1983 | // The thread entity (active unit of execution) is no longer running here, |
| 1984 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1985 | |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1986 | if (!thread->isDuplicating()) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1987 | closeOutputFinish(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1988 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1989 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1990 | return NO_ERROR; |
| 1991 | } |
| 1992 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1993 | void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1994 | { |
| 1995 | AudioStreamOut *out = thread->clearOutput(); |
| 1996 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); |
| 1997 | // from now on thread->mOutput is NULL |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1998 | delete out; |
| 1999 | } |
| 2000 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2001 | void AudioFlinger::closeOutputInternal_l(const sp<PlaybackThread>& thread) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2002 | { |
| 2003 | mPlaybackThreads.removeItem(thread->mId); |
| 2004 | thread->exit(); |
| 2005 | closeOutputFinish(thread); |
| 2006 | } |
| 2007 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2008 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2009 | { |
| 2010 | Mutex::Autolock _l(mLock); |
| 2011 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 2012 | |
| 2013 | if (thread == NULL) { |
| 2014 | return BAD_VALUE; |
| 2015 | } |
| 2016 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2017 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2018 | thread->suspend(); |
| 2019 | |
| 2020 | return NO_ERROR; |
| 2021 | } |
| 2022 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2023 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2024 | { |
| 2025 | Mutex::Autolock _l(mLock); |
| 2026 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 2027 | |
| 2028 | if (thread == NULL) { |
| 2029 | return BAD_VALUE; |
| 2030 | } |
| 2031 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2032 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2033 | |
| 2034 | thread->restore(); |
| 2035 | |
| 2036 | return NO_ERROR; |
| 2037 | } |
| 2038 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2039 | status_t AudioFlinger::openInput(audio_module_handle_t module, |
| 2040 | audio_io_handle_t *input, |
| 2041 | audio_config_t *config, |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2042 | audio_devices_t *devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2043 | const String8& address, |
| 2044 | audio_source_t source, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2045 | audio_input_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2046 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2047 | Mutex::Autolock _l(mLock); |
| 2048 | |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2049 | if (*devices == AUDIO_DEVICE_NONE) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2050 | return BAD_VALUE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2053 | sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2054 | |
| 2055 | if (thread != 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2056 | // notify client processes of the new input creation |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2057 | thread->ioConfigChanged(AUDIO_INPUT_OPENED); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2058 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2059 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2060 | return NO_INIT; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2061 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2062 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2063 | sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2064 | audio_io_handle_t *input, |
| 2065 | audio_config_t *config, |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2066 | audio_devices_t devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2067 | const String8& address, |
| 2068 | audio_source_t source, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2069 | audio_input_flags_t flags) |
| 2070 | { |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2071 | AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2072 | if (inHwDev == NULL) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2073 | *input = AUDIO_IO_HANDLE_NONE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2074 | return 0; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2075 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2076 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2077 | // Audio Policy can request a specific handle for hardware hotword. |
| 2078 | // The goal here is not to re-open an already opened input. |
| 2079 | // It is to use a pre-assigned I/O handle. |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2080 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2081 | *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
| 2082 | } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) { |
| 2083 | ALOGE("openInput_l() requested input handle %d is invalid", *input); |
| 2084 | return 0; |
| 2085 | } else if (mRecordThreads.indexOfKey(*input) >= 0) { |
| 2086 | // This should not happen in a transient state with current design. |
| 2087 | ALOGE("openInput_l() requested input handle %d is already assigned", *input); |
| 2088 | return 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2089 | } |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2090 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2091 | audio_config_t halconfig = *config; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2092 | sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2093 | sp<StreamInHalInterface> inStream; |
| 2094 | status_t status = inHwHal->openInputStream( |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2095 | *input, devices, &halconfig, flags, address.string(), source, &inStream); |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 2096 | ALOGV("openInput_l() openInputStream returned input %p, devices %x, SamplingRate %d" |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2097 | ", Format %#x, Channels %x, flags %#x, status %d addr %s", |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2098 | inStream.get(), |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 2099 | devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2100 | halconfig.sample_rate, |
| 2101 | halconfig.format, |
| 2102 | halconfig.channel_mask, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2103 | flags, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2104 | status, address.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2105 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2106 | // If the input could not be opened with the requested parameters and we can handle the |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2107 | // conversion internally, try to open again with the proposed parameters. |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2108 | if (status == BAD_VALUE && |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2109 | audio_is_linear_pcm(config->format) && |
| 2110 | audio_is_linear_pcm(halconfig.format) && |
| 2111 | (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) && |
vivek mehta | 7534666 | 2016-05-04 18:45:46 -0700 | [diff] [blame] | 2112 | (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) && |
| 2113 | (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2114 | // FIXME describe the change proposed by HAL (save old values so we can log them here) |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2115 | ALOGV("openInput_l() reopening with proposed sampling rate and channel mask"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2116 | inStream.clear(); |
| 2117 | status = inHwHal->openInputStream( |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2118 | *input, devices, &halconfig, flags, address.string(), source, &inStream); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2119 | // FIXME log this new status; HAL should not propose any further changes |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2122 | if (status == NO_ERROR && inStream != 0) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2123 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2124 | #ifdef TEE_SINK |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2125 | // Try to re-use most recently used Pipe to archive a copy of input for dumpsys, |
| 2126 | // or (re-)create if current Pipe is idle and does not match the new format |
| 2127 | sp<NBAIO_Sink> teeSink; |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2128 | enum { |
| 2129 | TEE_SINK_NO, // don't copy input |
| 2130 | TEE_SINK_NEW, // copy input using a new pipe |
| 2131 | TEE_SINK_OLD, // copy input using an existing pipe |
| 2132 | } kind; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 2133 | NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate, |
| 2134 | audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2135 | if (!mTeeSinkInputEnabled) { |
| 2136 | kind = TEE_SINK_NO; |
Glenn Kasten | 6e0d67d | 2014-01-31 09:41:08 -0800 | [diff] [blame] | 2137 | } else if (!Format_isValid(format)) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2138 | kind = TEE_SINK_NO; |
| 2139 | } else if (mRecordTeeSink == 0) { |
| 2140 | kind = TEE_SINK_NEW; |
| 2141 | } else if (mRecordTeeSink->getStrongCount() != 1) { |
| 2142 | kind = TEE_SINK_NO; |
Glenn Kasten | f66b422 | 2014-02-20 10:23:28 -0800 | [diff] [blame] | 2143 | } else if (Format_isEqual(format, mRecordTeeSink->format())) { |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2144 | kind = TEE_SINK_OLD; |
| 2145 | } else { |
| 2146 | kind = TEE_SINK_NEW; |
| 2147 | } |
| 2148 | switch (kind) { |
| 2149 | case TEE_SINK_NEW: { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2150 | Pipe *pipe = new Pipe(mTeeSinkInputFrames, format); |
Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 2151 | size_t numCounterOffers = 0; |
| 2152 | const NBAIO_Format offers[1] = {format}; |
| 2153 | ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); |
| 2154 | ALOG_ASSERT(index == 0); |
| 2155 | PipeReader *pipeReader = new PipeReader(*pipe); |
| 2156 | numCounterOffers = 0; |
| 2157 | index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); |
| 2158 | ALOG_ASSERT(index == 0); |
| 2159 | mRecordTeeSink = pipe; |
| 2160 | mRecordTeeSource = pipeReader; |
| 2161 | teeSink = pipe; |
| 2162 | } |
| 2163 | break; |
| 2164 | case TEE_SINK_OLD: |
| 2165 | teeSink = mRecordTeeSink; |
| 2166 | break; |
| 2167 | case TEE_SINK_NO: |
| 2168 | default: |
| 2169 | break; |
| 2170 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2171 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 2172 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2173 | AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2174 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2175 | // Start record thread |
Glenn Kasten | 34af026 | 2013-07-30 11:52:39 -0700 | [diff] [blame] | 2176 | // RecordThread requires both input and output device indication to forward to audio |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2177 | // pre processing modules |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2178 | sp<RecordThread> thread = new RecordThread(this, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2179 | inputStream, |
| 2180 | *input, |
Eric Laurent | d3922f7 | 2013-02-01 17:57:04 -0800 | [diff] [blame] | 2181 | primaryOutputDevice_l(), |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2182 | devices, |
| 2183 | mSystemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 2184 | #ifdef TEE_SINK |
| 2185 | , teeSink |
| 2186 | #endif |
| 2187 | ); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2188 | mRecordThreads.add(*input, thread); |
| 2189 | ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2190 | return thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2193 | *input = AUDIO_IO_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2194 | return 0; |
| 2195 | } |
| 2196 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2197 | status_t AudioFlinger::closeInput(audio_io_handle_t input) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2198 | { |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 2199 | return closeInput_nonvirtual(input); |
| 2200 | } |
| 2201 | |
| 2202 | status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input) |
| 2203 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2204 | // keep strong reference on the record thread so that |
| 2205 | // it is not destroyed while exit() is executed |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2206 | sp<RecordThread> thread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2207 | { |
| 2208 | Mutex::Autolock _l(mLock); |
| 2209 | thread = checkRecordThread_l(input); |
Glenn Kasten | d5903ec | 2012-03-18 10:33:27 -0700 | [diff] [blame] | 2210 | if (thread == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2211 | return BAD_VALUE; |
| 2212 | } |
| 2213 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2214 | ALOGV("closeInput() %d", input); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2215 | |
| 2216 | // If we still have effect chains, it means that a client still holds a handle |
| 2217 | // on at least one effect. We must either move the chain to an existing thread with the |
| 2218 | // same session ID or put it aside in case a new record thread is opened for a |
| 2219 | // new capture on the same session |
| 2220 | sp<EffectChain> chain; |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2221 | { |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2222 | Mutex::Autolock _sl(thread->mLock); |
| 2223 | Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l(); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2224 | // Note: maximum one chain per record thread |
| 2225 | if (effectChains.size() != 0) { |
| 2226 | chain = effectChains[0]; |
| 2227 | } |
| 2228 | } |
| 2229 | if (chain != 0) { |
| 2230 | // first check if a record thread is already opened with a client on the same session. |
| 2231 | // This should only happen in case of overlap between one thread tear down and the |
| 2232 | // creation of its replacement |
| 2233 | size_t i; |
| 2234 | for (i = 0; i < mRecordThreads.size(); i++) { |
| 2235 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 2236 | if (t == thread) { |
| 2237 | continue; |
| 2238 | } |
| 2239 | if (t->hasAudioSession(chain->sessionId()) != 0) { |
| 2240 | Mutex::Autolock _l(t->mLock); |
| 2241 | ALOGV("closeInput() found thread %d for effect session %d", |
| 2242 | t->id(), chain->sessionId()); |
| 2243 | t->addEffectChain_l(chain); |
| 2244 | break; |
| 2245 | } |
| 2246 | } |
| 2247 | // put the chain aside if we could not find a record thread with the same session id. |
| 2248 | if (i == mRecordThreads.size()) { |
| 2249 | putOrphanEffectChain_l(chain); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2250 | } |
| 2251 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2252 | const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor(); |
| 2253 | ioDesc->mIoHandle = input; |
| 2254 | ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2255 | mRecordThreads.removeItem(input); |
| 2256 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2257 | // FIXME: calling thread->exit() without mLock held should not be needed anymore now that |
| 2258 | // we have a different lock for notification client |
| 2259 | closeInputFinish(thread); |
| 2260 | return NO_ERROR; |
| 2261 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2262 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2263 | void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2264 | { |
| 2265 | thread->exit(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2266 | AudioStreamIn *in = thread->clearInput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 2267 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2268 | // from now on thread->mInput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2269 | delete in; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2270 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2271 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2272 | void AudioFlinger::closeInputInternal_l(const sp<RecordThread>& thread) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2273 | { |
| 2274 | mRecordThreads.removeItem(thread->mId); |
| 2275 | closeInputFinish(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2278 | status_t AudioFlinger::invalidateStream(audio_stream_type_t stream) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2279 | { |
| 2280 | Mutex::Autolock _l(mLock); |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2281 | ALOGV("invalidateStream() stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2282 | |
| 2283 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2284 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | 2216785 | 2012-06-20 12:26:32 -0700 | [diff] [blame] | 2285 | thread->invalidateTracks(stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2286 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2287 | |
| 2288 | return NO_ERROR; |
| 2289 | } |
| 2290 | |
| 2291 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2292 | audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2293 | { |
Glenn Kasten | 9d00313 | 2016-04-06 14:38:09 -0700 | [diff] [blame] | 2294 | // This is a binder API, so a malicious client could pass in a bad parameter. |
| 2295 | // Check for that before calling the internal API nextUniqueId(). |
| 2296 | if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) { |
| 2297 | ALOGE("newAudioUniqueId invalid use %d", use); |
| 2298 | return AUDIO_UNIQUE_ID_ALLOCATE; |
| 2299 | } |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2300 | return nextUniqueId(use); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2303 | void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2304 | { |
| 2305 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 2306 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2307 | ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid); |
| 2308 | if (pid != -1 && (caller == getpid_cached)) { |
| 2309 | caller = pid; |
| 2310 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2311 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2312 | { |
| 2313 | Mutex::Autolock _cl(mClientLock); |
| 2314 | // Ignore requests received from processes not known as notification client. The request |
| 2315 | // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be |
| 2316 | // called from a different pid leaving a stale session reference. Also we don't know how |
| 2317 | // to clear this reference if the client process dies. |
| 2318 | if (mNotificationClients.indexOfKey(caller) < 0) { |
| 2319 | ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession); |
| 2320 | return; |
| 2321 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2324 | size_t num = mAudioSessionRefs.size(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2325 | for (size_t i = 0; i < num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2326 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2327 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 2328 | ref->mCnt++; |
| 2329 | ALOGV(" incremented refcount to %d", ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2330 | return; |
| 2331 | } |
| 2332 | } |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 2333 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); |
| 2334 | ALOGV(" added new entry for %d", audioSession); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2335 | } |
| 2336 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2337 | void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2338 | { |
| 2339 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 2340 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2341 | ALOGV("releasing %d from %d for %d", audioSession, caller, pid); |
| 2342 | if (pid != -1 && (caller == getpid_cached)) { |
| 2343 | caller = pid; |
| 2344 | } |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2345 | size_t num = mAudioSessionRefs.size(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2346 | for (size_t i = 0; i < num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2347 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2348 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 2349 | ref->mCnt--; |
| 2350 | ALOGV(" decremented refcount to %d", ref->mCnt); |
| 2351 | if (ref->mCnt == 0) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2352 | mAudioSessionRefs.removeAt(i); |
| 2353 | delete ref; |
| 2354 | purgeStaleEffects_l(); |
| 2355 | } |
| 2356 | return; |
| 2357 | } |
| 2358 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2359 | // If the caller is mediaserver it is likely that the session being released was acquired |
| 2360 | // on behalf of a process not in notification clients and we ignore the warning. |
| 2361 | ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2364 | bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession) |
| 2365 | { |
| 2366 | size_t num = mAudioSessionRefs.size(); |
| 2367 | for (size_t i = 0; i < num; i++) { |
| 2368 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 2369 | if (ref->mSessionid == audioSession) { |
| 2370 | return true; |
| 2371 | } |
| 2372 | } |
| 2373 | return false; |
| 2374 | } |
| 2375 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2376 | void AudioFlinger::purgeStaleEffects_l() { |
| 2377 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2378 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2379 | |
| 2380 | Vector< sp<EffectChain> > chains; |
| 2381 | |
| 2382 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2383 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 2384 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 2385 | sp<EffectChain> ec = t->mEffectChains[j]; |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 2386 | if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { |
| 2387 | chains.push(ec); |
| 2388 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2392 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 2393 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 2394 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 2395 | chains.push(ec); |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | for (size_t i = 0; i < chains.size(); i++) { |
| 2400 | sp<EffectChain> ec = chains[i]; |
| 2401 | int sessionid = ec->sessionId(); |
| 2402 | sp<ThreadBase> t = ec->mThread.promote(); |
| 2403 | if (t == 0) { |
| 2404 | continue; |
| 2405 | } |
| 2406 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 2407 | bool found = false; |
| 2408 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 2409 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2410 | if (ref->mSessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2411 | ALOGV(" session %d still exists for %d with %d refs", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2412 | sessionid, ref->mPid, ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2413 | found = true; |
| 2414 | break; |
| 2415 | } |
| 2416 | } |
| 2417 | if (!found) { |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 2418 | Mutex::Autolock _l(t->mLock); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2419 | // remove all effects from the chain |
| 2420 | while (ec->mEffects.size()) { |
| 2421 | sp<EffectModule> effect = ec->mEffects[0]; |
| 2422 | effect->unPin(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2423 | t->removeEffect_l(effect); |
Eric Laurent | a5f44eb | 2012-06-25 11:38:29 -0700 | [diff] [blame] | 2424 | if (effect->purgeHandles()) { |
| 2425 | t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2426 | } |
| 2427 | AudioSystem::unregisterEffect(effect->id()); |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | return; |
| 2432 | } |
| 2433 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2434 | // checkThread_l() must be called with AudioFlinger::mLock held |
| 2435 | AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const |
| 2436 | { |
| 2437 | ThreadBase *thread = NULL; |
| 2438 | switch (audio_unique_id_get_use(ioHandle)) { |
| 2439 | case AUDIO_UNIQUE_ID_USE_OUTPUT: |
| 2440 | thread = checkPlaybackThread_l(ioHandle); |
| 2441 | break; |
| 2442 | case AUDIO_UNIQUE_ID_USE_INPUT: |
| 2443 | thread = checkRecordThread_l(ioHandle); |
| 2444 | break; |
| 2445 | default: |
| 2446 | break; |
| 2447 | } |
| 2448 | return thread; |
| 2449 | } |
| 2450 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2451 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2452 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2453 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2454 | return mPlaybackThreads.valueFor(output).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2458 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2459 | { |
| 2460 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2461 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2465 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2466 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 2467 | return mRecordThreads.valueFor(input).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2468 | } |
| 2469 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2470 | audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2471 | { |
Glenn Kasten | 9d00313 | 2016-04-06 14:38:09 -0700 | [diff] [blame] | 2472 | // This is the internal API, so it is OK to assert on bad parameter. |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2473 | LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX); |
Glenn Kasten | d2e67e1 | 2016-04-11 08:26:37 -0700 | [diff] [blame] | 2474 | const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1; |
| 2475 | for (int retry = 0; retry < maxRetries; retry++) { |
| 2476 | // The cast allows wraparound from max positive to min negative instead of abort |
| 2477 | uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use], |
| 2478 | (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel); |
| 2479 | ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED); |
| 2480 | // allow wrap by skipping 0 and -1 for session ids |
| 2481 | if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) { |
| 2482 | ALOGW_IF(retry != 0, "unique ID overflow for use %d", use); |
| 2483 | return (audio_unique_id_t) (base | use); |
| 2484 | } |
| 2485 | } |
| 2486 | // We have no way of recovering from wraparound |
| 2487 | LOG_ALWAYS_FATAL("unique ID overflow for use %d", use); |
| 2488 | // TODO Use a floor after wraparound. This may need a mutex. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2489 | } |
| 2490 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 2491 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2492 | { |
| 2493 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2494 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 2495 | if(thread->isDuplicating()) { |
| 2496 | continue; |
| 2497 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2498 | AudioStreamOut *output = thread->getOutput(); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2499 | if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2500 | return thread; |
| 2501 | } |
| 2502 | } |
| 2503 | return NULL; |
| 2504 | } |
| 2505 | |
Glenn Kasten | bb4350d | 2012-07-03 15:56:38 -0700 | [diff] [blame] | 2506 | audio_devices_t AudioFlinger::primaryOutputDevice_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2507 | { |
| 2508 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 2509 | |
| 2510 | if (thread == NULL) { |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 2514 | return thread->outDevice(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 2517 | AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const |
| 2518 | { |
| 2519 | size_t minFrameCount = 0; |
| 2520 | PlaybackThread *minThread = NULL; |
| 2521 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2522 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
| 2523 | if (!thread->isDuplicating()) { |
| 2524 | size_t frameCount = thread->frameCountHAL(); |
| 2525 | if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount || |
| 2526 | (frameCount == minFrameCount && thread->hasFastMixer() && |
| 2527 | /*minThread != NULL &&*/ !minThread->hasFastMixer()))) { |
| 2528 | minFrameCount = frameCount; |
| 2529 | minThread = thread; |
| 2530 | } |
| 2531 | } |
| 2532 | } |
| 2533 | return minThread; |
| 2534 | } |
| 2535 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2536 | sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2537 | audio_session_t triggerSession, |
| 2538 | audio_session_t listenerSession, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2539 | sync_event_callback_t callBack, |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2540 | const wp<RefBase>& cookie) |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 2541 | { |
| 2542 | Mutex::Autolock _l(mLock); |
| 2543 | |
| 2544 | sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); |
| 2545 | status_t playStatus = NAME_NOT_FOUND; |
| 2546 | status_t recStatus = NAME_NOT_FOUND; |
| 2547 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2548 | playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); |
| 2549 | if (playStatus == NO_ERROR) { |
| 2550 | return event; |
| 2551 | } |
| 2552 | } |
| 2553 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2554 | recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); |
| 2555 | if (recStatus == NO_ERROR) { |
| 2556 | return event; |
| 2557 | } |
| 2558 | } |
| 2559 | if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { |
| 2560 | mPendingSyncEvents.add(event); |
| 2561 | } else { |
| 2562 | ALOGV("createSyncEvent() invalid event %d", event->type()); |
| 2563 | event.clear(); |
| 2564 | } |
| 2565 | return event; |
| 2566 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2567 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2568 | // ---------------------------------------------------------------------------- |
| 2569 | // Effect management |
| 2570 | // ---------------------------------------------------------------------------- |
| 2571 | |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2572 | sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() { |
| 2573 | return mEffectsFactoryHal; |
| 2574 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2575 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2576 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2577 | { |
| 2578 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2579 | if (mEffectsFactoryHal.get()) { |
| 2580 | return mEffectsFactoryHal->queryNumberEffects(numEffects); |
| 2581 | } else { |
| 2582 | return -ENODEV; |
| 2583 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2586 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2587 | { |
| 2588 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2589 | if (mEffectsFactoryHal.get()) { |
| 2590 | return mEffectsFactoryHal->getDescriptor(index, descriptor); |
| 2591 | } else { |
| 2592 | return -ENODEV; |
| 2593 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 2596 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 2597 | effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2598 | { |
| 2599 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2600 | if (mEffectsFactoryHal.get()) { |
| 2601 | return mEffectsFactoryHal->getDescriptor(pUuid, descriptor); |
| 2602 | } else { |
| 2603 | return -ENODEV; |
| 2604 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 2608 | sp<IEffect> AudioFlinger::createEffect( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2609 | effect_descriptor_t *pDesc, |
| 2610 | const sp<IEffectClient>& effectClient, |
| 2611 | int32_t priority, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2612 | audio_io_handle_t io, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2613 | audio_session_t sessionId, |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 2614 | const String16& opPackageName, |
Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 2615 | pid_t pid, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2616 | status_t *status, |
| 2617 | int *id, |
| 2618 | int *enabled) |
| 2619 | { |
| 2620 | status_t lStatus = NO_ERROR; |
| 2621 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2622 | effect_descriptor_t desc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2623 | |
Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 2624 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 2625 | if (pid == -1 || !isTrustedCallingUid(callingUid)) { |
| 2626 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 2627 | ALOGW_IF(pid != -1 && pid != callingPid, |
| 2628 | "%s uid %d pid %d tried to pass itself off as pid %d", |
| 2629 | __func__, callingUid, callingPid, pid); |
| 2630 | pid = callingPid; |
| 2631 | } |
| 2632 | |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2633 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p", |
| 2634 | pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2635 | |
| 2636 | if (pDesc == NULL) { |
| 2637 | lStatus = BAD_VALUE; |
| 2638 | goto Exit; |
| 2639 | } |
| 2640 | |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2641 | // check audio settings permission for global effects |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2642 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2643 | lStatus = PERMISSION_DENIED; |
| 2644 | goto Exit; |
| 2645 | } |
| 2646 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2647 | // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2648 | // that can only be created by audio policy manager (running in same process) |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 2649 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2650 | lStatus = PERMISSION_DENIED; |
| 2651 | goto Exit; |
| 2652 | } |
| 2653 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2654 | if (mEffectsFactoryHal == 0) { |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2655 | lStatus = NO_INIT; |
| 2656 | goto Exit; |
| 2657 | } |
| 2658 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2659 | { |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2660 | if (!EffectsFactoryHalInterface::isNullUuid(&pDesc->uuid)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2661 | // if uuid is specified, request effect descriptor |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2662 | lStatus = mEffectsFactoryHal->getDescriptor(&pDesc->uuid, &desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2663 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2664 | ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2665 | goto Exit; |
| 2666 | } |
| 2667 | } else { |
| 2668 | // if uuid is not specified, look for an available implementation |
| 2669 | // of the required type in effect factory |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2670 | if (EffectsFactoryHalInterface::isNullUuid(&pDesc->type)) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2671 | ALOGW("createEffect() no effect type"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2672 | lStatus = BAD_VALUE; |
| 2673 | goto Exit; |
| 2674 | } |
| 2675 | uint32_t numEffects = 0; |
| 2676 | effect_descriptor_t d; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2677 | d.flags = 0; // prevent compiler warning |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2678 | bool found = false; |
| 2679 | |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2680 | lStatus = mEffectsFactoryHal->queryNumberEffects(&numEffects); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2681 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2682 | ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2683 | goto Exit; |
| 2684 | } |
| 2685 | for (uint32_t i = 0; i < numEffects; i++) { |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 2686 | lStatus = mEffectsFactoryHal->getDescriptor(i, &desc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2687 | if (lStatus < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2688 | ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2689 | continue; |
| 2690 | } |
| 2691 | if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { |
| 2692 | // If matching type found save effect descriptor. If the session is |
| 2693 | // 0 and the effect is not auxiliary, continue enumeration in case |
| 2694 | // an auxiliary version of this effect type is available |
| 2695 | found = true; |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2696 | d = desc; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2697 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX || |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2698 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2699 | break; |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | if (!found) { |
| 2704 | lStatus = BAD_VALUE; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2705 | ALOGW("createEffect() effect not found"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2706 | goto Exit; |
| 2707 | } |
| 2708 | // For same effect type, chose auxiliary version over insert version if |
| 2709 | // connect to output mix (Compliance to OpenSL ES) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2710 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2711 | (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2712 | desc = d; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | // Do not allow auxiliary effects on a session different from 0 (output mix) |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2717 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2718 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2719 | lStatus = INVALID_OPERATION; |
| 2720 | goto Exit; |
| 2721 | } |
| 2722 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2723 | // check recording permission for visualizer |
| 2724 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 2725 | !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2726 | lStatus = PERMISSION_DENIED; |
| 2727 | goto Exit; |
| 2728 | } |
| 2729 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2730 | // return effect descriptor |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 2731 | *pDesc = desc; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2732 | if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2733 | // if the output returned by getOutputForEffect() is removed before we lock the |
| 2734 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
| 2735 | // and we will exit safely |
| 2736 | io = AudioSystem::getOutputForEffect(&desc); |
| 2737 | ALOGV("createEffect got output %d", io); |
| 2738 | } |
| 2739 | |
| 2740 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2741 | |
| 2742 | // If output is not specified try to find a matching audio session ID in one of the |
| 2743 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2744 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 2745 | // because of code checking output when entering the function. |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2746 | // Note: io is never 0 when creating an effect on an input |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2747 | if (io == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2748 | if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 2749 | // output must be specified by AudioPolicyManager when using session |
| 2750 | // AUDIO_SESSION_OUTPUT_STAGE |
| 2751 | lStatus = BAD_VALUE; |
| 2752 | goto Exit; |
| 2753 | } |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2754 | // look for the thread where the specified audio session is present |
| 2755 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2756 | if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 2757 | io = mPlaybackThreads.keyAt(i); |
| 2758 | break; |
| 2759 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2760 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2761 | if (io == 0) { |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 2762 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2763 | if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { |
| 2764 | io = mRecordThreads.keyAt(i); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2765 | break; |
| 2766 | } |
| 2767 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2768 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2769 | // If no output thread contains the requested session ID, default to |
| 2770 | // first output. The effect chain will be moved to the correct output |
| 2771 | // thread when a track with the same session ID is created |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2772 | if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2773 | io = mPlaybackThreads.keyAt(0); |
| 2774 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2775 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2776 | } |
| 2777 | ThreadBase *thread = checkRecordThread_l(io); |
| 2778 | if (thread == NULL) { |
| 2779 | thread = checkPlaybackThread_l(io); |
| 2780 | if (thread == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2781 | ALOGE("createEffect() unknown output thread"); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2782 | lStatus = BAD_VALUE; |
| 2783 | goto Exit; |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2784 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2785 | } else { |
| 2786 | // Check if one effect chain was awaiting for an effect to be created on this |
| 2787 | // session and used it instead of creating a new one. |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2788 | sp<EffectChain> chain = getOrphanEffectChain_l(sessionId); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2789 | if (chain != 0) { |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2790 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2791 | thread->addEffectChain_l(chain); |
| 2792 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2793 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 2794 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2795 | sp<Client> client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2796 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2797 | // create effect on selected output thread |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2798 | bool pinned = (sessionId > AUDIO_SESSION_OUTPUT_MIX) && isSessionAcquired_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2799 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2800 | &desc, enabled, &lStatus, pinned); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2801 | if (handle != 0 && id != NULL) { |
| 2802 | *id = handle->id(); |
| 2803 | } |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 2804 | if (handle == 0) { |
| 2805 | // remove local strong reference to Client with mClientLock held |
| 2806 | Mutex::Autolock _cl(mClientLock); |
| 2807 | client.clear(); |
| 2808 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2812 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2813 | return handle; |
| 2814 | } |
| 2815 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2816 | status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2817 | audio_io_handle_t dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2818 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2819 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2820 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2821 | Mutex::Autolock _l(mLock); |
| 2822 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2823 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2824 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2825 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2826 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 2827 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2828 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2829 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2830 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2831 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 2832 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2833 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2834 | return BAD_VALUE; |
| 2835 | } |
| 2836 | |
| 2837 | Mutex::Autolock _dl(dstThread->mLock); |
| 2838 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2839 | return moveEffectChain_l(sessionId, srcThread, dstThread, false); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2840 | } |
| 2841 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2842 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2843 | status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2844 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2845 | AudioFlinger::PlaybackThread *dstThread, |
| 2846 | bool reRegister) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2847 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2848 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2849 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2850 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2851 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2852 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2853 | ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2854 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2855 | return INVALID_OPERATION; |
| 2856 | } |
| 2857 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2858 | // Check whether the destination thread and all effects in the chain are compatible |
| 2859 | if (!chain->isCompatibleWithThread_l(dstThread)) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2860 | ALOGW("moveEffectChain_l() effect chain failed because" |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2861 | " destination thread %p is not compatible with effects in the chain", |
| 2862 | dstThread); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2863 | return INVALID_OPERATION; |
| 2864 | } |
| 2865 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2866 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2867 | // so that a new chain is created with correct parameters when first effect is added. This is |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 2868 | // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2869 | // removed. |
| 2870 | srcThread->removeEffectChain_l(chain); |
| 2871 | |
| 2872 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 2873 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2874 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2875 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2876 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2877 | Vector< sp<EffectModule> > removed; |
| 2878 | status_t status = NO_ERROR; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2879 | while (effect != 0) { |
| 2880 | srcThread->removeEffect_l(effect); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2881 | removed.add(effect); |
| 2882 | status = dstThread->addEffect_l(effect); |
| 2883 | if (status != NO_ERROR) { |
| 2884 | break; |
| 2885 | } |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 2886 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 2887 | if (effect->state() == EffectModule::ACTIVE || |
| 2888 | effect->state() == EffectModule::STOPPING) { |
| 2889 | effect->start(); |
| 2890 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2891 | // if the move request is not received from audio policy manager, the effect must be |
| 2892 | // re-registered with the new strategy and output |
| 2893 | if (dstChain == 0) { |
| 2894 | dstChain = effect->chain().promote(); |
| 2895 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2896 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2897 | status = NO_INIT; |
| 2898 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2899 | } |
| 2900 | strategy = dstChain->strategy(); |
| 2901 | } |
| 2902 | if (reRegister) { |
| 2903 | AudioSystem::unregisterEffect(effect->id()); |
| 2904 | AudioSystem::registerEffect(&effect->desc(), |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2905 | dstThread->id(), |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2906 | strategy, |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 2907 | sessionId, |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2908 | effect->id()); |
Eric Laurent | d72b7c0 | 2013-10-12 16:17:46 -0700 | [diff] [blame] | 2909 | AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 2910 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2911 | effect = chain->getEffectFromId_l(0); |
| 2912 | } |
| 2913 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2914 | if (status != NO_ERROR) { |
| 2915 | for (size_t i = 0; i < removed.size(); i++) { |
| 2916 | srcThread->addEffect_l(removed[i]); |
| 2917 | if (dstChain != 0 && reRegister) { |
| 2918 | AudioSystem::unregisterEffect(removed[i]->id()); |
| 2919 | AudioSystem::registerEffect(&removed[i]->desc(), |
| 2920 | srcThread->id(), |
| 2921 | strategy, |
| 2922 | sessionId, |
| 2923 | removed[i]->id()); |
Eric Laurent | d72b7c0 | 2013-10-12 16:17:46 -0700 | [diff] [blame] | 2924 | AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2932 | bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2933 | { |
| 2934 | if (mGlobalEffectEnableTime != 0 && |
| 2935 | ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) { |
| 2936 | return true; |
| 2937 | } |
| 2938 | |
| 2939 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2940 | sp<EffectChain> ec = |
| 2941 | mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2942 | if (ec != 0 && ec->isNonOffloadableEnabled()) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2943 | return true; |
| 2944 | } |
| 2945 | } |
| 2946 | return false; |
| 2947 | } |
| 2948 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2949 | void AudioFlinger::onNonOffloadableGlobalEffectEnable() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2950 | { |
| 2951 | Mutex::Autolock _l(mLock); |
| 2952 | |
| 2953 | mGlobalEffectEnableTime = systemTime(); |
| 2954 | |
| 2955 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2956 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 2957 | if (t->mType == ThreadBase::OFFLOAD) { |
| 2958 | t->invalidateTracks(AUDIO_STREAM_MUSIC); |
| 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | } |
| 2963 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2964 | status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain) |
| 2965 | { |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2966 | audio_session_t session = chain->sessionId(); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2967 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2968 | ALOGV("putOrphanEffectChain_l session %d index %zd", session, index); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2969 | if (index >= 0) { |
| 2970 | ALOGW("putOrphanEffectChain_l chain for session %d already present", session); |
| 2971 | return ALREADY_EXISTS; |
| 2972 | } |
| 2973 | mOrphanEffectChains.add(session, chain); |
| 2974 | return NO_ERROR; |
| 2975 | } |
| 2976 | |
| 2977 | sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session) |
| 2978 | { |
| 2979 | sp<EffectChain> chain; |
| 2980 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2981 | ALOGV("getOrphanEffectChain_l session %d index %zd", session, index); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2982 | if (index >= 0) { |
| 2983 | chain = mOrphanEffectChains.valueAt(index); |
| 2984 | mOrphanEffectChains.removeItemsAt(index); |
| 2985 | } |
| 2986 | return chain; |
| 2987 | } |
| 2988 | |
| 2989 | bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect) |
| 2990 | { |
| 2991 | Mutex::Autolock _l(mLock); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2992 | audio_session_t session = effect->sessionId(); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2993 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2994 | ALOGV("updateOrphanEffectChains session %d index %zd", session, index); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2995 | if (index >= 0) { |
| 2996 | sp<EffectChain> chain = mOrphanEffectChains.valueAt(index); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2997 | if (chain->removeEffect_l(effect, true) == 0) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2998 | ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2999 | mOrphanEffectChains.removeItemsAt(index); |
| 3000 | } |
| 3001 | return true; |
| 3002 | } |
| 3003 | return false; |
| 3004 | } |
| 3005 | |
| 3006 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3007 | struct Entry { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 3008 | #define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21 |
| 3009 | char mFileName[TEE_MAX_FILENAME]; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3010 | }; |
| 3011 | |
| 3012 | int comparEntry(const void *p1, const void *p2) |
| 3013 | { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 3014 | return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3015 | } |
| 3016 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3017 | #ifdef TEE_SINK |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3018 | void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3019 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3020 | NBAIO_Source *teeSource = source.get(); |
| 3021 | if (teeSource != NULL) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3022 | // .wav rotation |
| 3023 | // There is a benign race condition if 2 threads call this simultaneously. |
| 3024 | // They would both traverse the directory, but the result would simply be |
| 3025 | // failures at unlink() which are ignored. It's also unlikely since |
| 3026 | // normally dumpsys is only done by bugreport or from the command line. |
| 3027 | char teePath[32+256]; |
Glenn Kasten | 9a00399 | 2016-02-23 15:24:34 -0800 | [diff] [blame] | 3028 | strcpy(teePath, "/data/misc/audioserver"); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3029 | size_t teePathLen = strlen(teePath); |
| 3030 | DIR *dir = opendir(teePath); |
| 3031 | teePath[teePathLen++] = '/'; |
| 3032 | if (dir != NULL) { |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 3033 | #define TEE_MAX_SORT 20 // number of entries to sort |
| 3034 | #define TEE_MAX_KEEP 10 // number of entries to keep |
| 3035 | struct Entry entries[TEE_MAX_SORT]; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3036 | size_t entryCount = 0; |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 3037 | while (entryCount < TEE_MAX_SORT) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3038 | struct dirent de; |
| 3039 | struct dirent *result = NULL; |
| 3040 | int rc = readdir_r(dir, &de, &result); |
| 3041 | if (rc != 0) { |
| 3042 | ALOGW("readdir_r failed %d", rc); |
| 3043 | break; |
| 3044 | } |
| 3045 | if (result == NULL) { |
| 3046 | break; |
| 3047 | } |
| 3048 | if (result != &de) { |
| 3049 | ALOGW("readdir_r returned unexpected result %p != %p", result, &de); |
| 3050 | break; |
| 3051 | } |
| 3052 | // ignore non .wav file entries |
| 3053 | size_t nameLen = strlen(de.d_name); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 3054 | if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME || |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3055 | strcmp(&de.d_name[nameLen - 4], ".wav")) { |
| 3056 | continue; |
| 3057 | } |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 3058 | strcpy(entries[entryCount++].mFileName, de.d_name); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3059 | } |
| 3060 | (void) closedir(dir); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 3061 | if (entryCount > TEE_MAX_KEEP) { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3062 | qsort(entries, entryCount, sizeof(Entry), comparEntry); |
Glenn Kasten | 2f55e76 | 2015-03-05 16:05:08 -0800 | [diff] [blame] | 3063 | for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) { |
| 3064 | strcpy(&teePath[teePathLen], entries[i].mFileName); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3065 | (void) unlink(teePath); |
| 3066 | } |
| 3067 | } |
| 3068 | } else { |
| 3069 | if (fd >= 0) { |
Glenn Kasten | fbd87e8 | 2016-07-18 15:45:56 -0700 | [diff] [blame] | 3070 | dprintf(fd, "unable to rotate tees in %.*s: %s\n", (int) teePathLen, teePath, |
Glenn Kasten | 9a00399 | 2016-02-23 15:24:34 -0800 | [diff] [blame] | 3071 | strerror(errno)); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3072 | } |
| 3073 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3074 | char teeTime[16]; |
| 3075 | struct timeval tv; |
| 3076 | gettimeofday(&tv, NULL); |
| 3077 | struct tm tm; |
| 3078 | localtime_r(&tv.tv_sec, &tm); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3079 | strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm); |
| 3080 | snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id); |
| 3081 | // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd |
| 3082 | int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3083 | if (teeFd >= 0) { |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 3084 | // FIXME use libsndfile |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3085 | char wavHeader[44]; |
| 3086 | memcpy(wavHeader, |
| 3087 | "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", |
| 3088 | sizeof(wavHeader)); |
| 3089 | NBAIO_Format format = teeSource->format(); |
| 3090 | unsigned channelCount = Format_channelCount(format); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3091 | uint32_t sampleRate = Format_sampleRate(format); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 3092 | size_t frameSize = Format_frameSize(format); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3093 | wavHeader[22] = channelCount; // number of channels |
| 3094 | wavHeader[24] = sampleRate; // sample rate |
| 3095 | wavHeader[25] = sampleRate >> 8; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 3096 | wavHeader[32] = frameSize; // block alignment |
| 3097 | wavHeader[33] = frameSize >> 8; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3098 | write(teeFd, wavHeader, sizeof(wavHeader)); |
| 3099 | size_t total = 0; |
| 3100 | bool firstRead = true; |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 3101 | #define TEE_SINK_READ 1024 // frames per I/O operation |
| 3102 | void *buffer = malloc(TEE_SINK_READ * frameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3103 | for (;;) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3104 | size_t count = TEE_SINK_READ; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3105 | ssize_t actual = teeSource->read(buffer, count); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3106 | bool wasFirstRead = firstRead; |
| 3107 | firstRead = false; |
| 3108 | if (actual <= 0) { |
| 3109 | if (actual == (ssize_t) OVERRUN && wasFirstRead) { |
| 3110 | continue; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3111 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3112 | break; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3113 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3114 | ALOG_ASSERT(actual <= (ssize_t)count); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 3115 | write(teeFd, buffer, actual * frameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3116 | total += actual; |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3117 | } |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 3118 | free(buffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3119 | lseek(teeFd, (off_t) 4, SEEK_SET); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 3120 | uint32_t temp = 44 + total * frameSize - 8; |
| 3121 | // FIXME not big-endian safe |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3122 | write(teeFd, &temp, sizeof(temp)); |
| 3123 | lseek(teeFd, (off_t) 40, SEEK_SET); |
Glenn Kasten | 329f651 | 2014-08-28 16:23:16 -0700 | [diff] [blame] | 3124 | temp = total * frameSize; |
| 3125 | // FIXME not big-endian safe |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 3126 | write(teeFd, &temp, sizeof(temp)); |
| 3127 | close(teeFd); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3128 | if (fd >= 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 3129 | dprintf(fd, "tee copied to %s\n", teePath); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3130 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3131 | } else { |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3132 | if (fd >= 0) { |
Elliott Hughes | 8b5f642 | 2014-05-22 01:22:06 -0700 | [diff] [blame] | 3133 | dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno)); |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 3134 | } |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3135 | } |
| 3136 | } |
| 3137 | } |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 3138 | #endif |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3139 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3140 | // ---------------------------------------------------------------------------- |
| 3141 | |
| 3142 | status_t AudioFlinger::onTransact( |
| 3143 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 3144 | { |
| 3145 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 3146 | } |
| 3147 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 3148 | } // namespace android |