Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2012, 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 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | |
Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 24 | #include "Configuration.h" |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 26 | #include <system/audio_effects/effect_aec.h> |
Ricardo Garcia | c2a3a82 | 2019-07-17 14:29:12 -0700 | [diff] [blame] | 27 | #include <system/audio_effects/effect_dynamicsprocessing.h> |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 28 | #include <system/audio_effects/effect_ns.h> |
| 29 | #include <system/audio_effects/effect_visualizer.h> |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 30 | #include <audio_utils/channels.h> |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 31 | #include <audio_utils/primitives.h> |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 32 | #include <media/AudioContainers.h> |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 33 | #include <media/AudioEffect.h> |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 34 | #include <media/AudioDeviceTypeAddr.h> |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 35 | #include <media/audiohal/EffectHalInterface.h> |
| 36 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 37 | #include <mediautils/ServiceUtilities.h> |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 38 | |
| 39 | #include "AudioFlinger.h" |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | |
| 43 | // Note: the following macro is used for extremely verbose logging message. In |
| 44 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 45 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 46 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 47 | // turned on. Do not uncomment the #def below unless you really know what you |
| 48 | // are doing and want to see all of the extremely verbose messages. |
| 49 | //#define VERY_VERY_VERBOSE_LOGGING |
| 50 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 51 | #define ALOGVV ALOGV |
| 52 | #else |
| 53 | #define ALOGVV(a...) do { } while(0) |
| 54 | #endif |
| 55 | |
Yuuki Yokoyama | e17f831 | 2017-05-26 19:06:33 +0900 | [diff] [blame] | 56 | #define DEFAULT_OUTPUT_SAMPLE_RATE 48000 |
| 57 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 58 | namespace android { |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 61 | // EffectBase implementation |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | // ---------------------------------------------------------------------------- |
| 63 | |
| 64 | #undef LOG_TAG |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 65 | #define LOG_TAG "AudioFlinger::EffectBase" |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 66 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 67 | AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback, |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 68 | effect_descriptor_t *desc, |
| 69 | int id, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 70 | audio_session_t sessionId, |
| 71 | bool pinned) |
| 72 | : mPinned(pinned), |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 73 | mCallback(callback), mId(id), mSessionId(sessionId), |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 74 | mDescriptor(*desc) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 75 | { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 78 | // must be called with EffectModule::mLock held |
| 79 | status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 80 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 81 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 82 | ALOGV("setEnabled %p enabled %d", this, enabled); |
| 83 | |
| 84 | if (enabled != isEnabled()) { |
| 85 | switch (mState) { |
| 86 | // going from disabled to enabled |
| 87 | case IDLE: |
| 88 | mState = STARTING; |
| 89 | break; |
| 90 | case STOPPED: |
| 91 | mState = RESTART; |
| 92 | break; |
| 93 | case STOPPING: |
| 94 | mState = ACTIVE; |
| 95 | break; |
| 96 | |
| 97 | // going from enabled to disabled |
| 98 | case RESTART: |
| 99 | mState = STOPPED; |
| 100 | break; |
| 101 | case STARTING: |
| 102 | mState = IDLE; |
| 103 | break; |
| 104 | case ACTIVE: |
| 105 | mState = STOPPING; |
| 106 | break; |
| 107 | case DESTROYED: |
| 108 | return NO_ERROR; // simply ignore as we are being destroyed |
| 109 | } |
| 110 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 111 | EffectHandle *h = mHandles[i]; |
| 112 | if (h != NULL && !h->disconnected()) { |
| 113 | h->setEnabled(enabled); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | return NO_ERROR; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 120 | status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle) |
| 121 | { |
| 122 | status_t status; |
| 123 | { |
| 124 | Mutex::Autolock _l(mLock); |
| 125 | status = setEnabled_l(enabled); |
| 126 | } |
| 127 | if (fromHandle) { |
| 128 | if (enabled) { |
| 129 | if (status != NO_ERROR) { |
| 130 | mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/); |
| 131 | } else { |
| 132 | mCallback->onEffectEnable(this); |
| 133 | } |
| 134 | } else { |
| 135 | mCallback->onEffectDisable(this); |
| 136 | } |
| 137 | } |
| 138 | return status; |
| 139 | } |
| 140 | |
| 141 | bool AudioFlinger::EffectBase::isEnabled() const |
| 142 | { |
| 143 | switch (mState) { |
| 144 | case RESTART: |
| 145 | case STARTING: |
| 146 | case ACTIVE: |
| 147 | return true; |
| 148 | case IDLE: |
| 149 | case STOPPING: |
| 150 | case STOPPED: |
| 151 | case DESTROYED: |
| 152 | default: |
| 153 | return false; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void AudioFlinger::EffectBase::setSuspended(bool suspended) |
| 158 | { |
| 159 | Mutex::Autolock _l(mLock); |
| 160 | mSuspended = suspended; |
| 161 | } |
| 162 | |
| 163 | bool AudioFlinger::EffectBase::suspended() const |
| 164 | { |
| 165 | Mutex::Autolock _l(mLock); |
| 166 | return mSuspended; |
| 167 | } |
| 168 | |
| 169 | status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 170 | { |
| 171 | status_t status; |
| 172 | |
| 173 | Mutex::Autolock _l(mLock); |
| 174 | int priority = handle->priority(); |
| 175 | size_t size = mHandles.size(); |
| 176 | EffectHandle *controlHandle = NULL; |
| 177 | size_t i; |
| 178 | for (i = 0; i < size; i++) { |
| 179 | EffectHandle *h = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 180 | if (h == NULL || h->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 181 | continue; |
| 182 | } |
| 183 | // first non destroyed handle is considered in control |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 184 | if (controlHandle == NULL) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 185 | controlHandle = h; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 186 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 187 | if (h->priority() <= priority) { |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | // if inserted in first place, move effect control from previous owner to this handle |
| 192 | if (i == 0) { |
| 193 | bool enabled = false; |
| 194 | if (controlHandle != NULL) { |
| 195 | enabled = controlHandle->enabled(); |
| 196 | controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
| 197 | } |
| 198 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
| 199 | status = NO_ERROR; |
| 200 | } else { |
| 201 | status = ALREADY_EXISTS; |
| 202 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 203 | ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 204 | mHandles.insertAt(handle, i); |
| 205 | return status; |
| 206 | } |
| 207 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 208 | status_t AudioFlinger::EffectBase::updatePolicyState() |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 209 | { |
| 210 | status_t status = NO_ERROR; |
| 211 | bool doRegister = false; |
| 212 | bool registered = false; |
| 213 | bool doEnable = false; |
| 214 | bool enabled = false; |
| 215 | audio_io_handle_t io; |
| 216 | uint32_t strategy; |
| 217 | |
| 218 | { |
| 219 | Mutex::Autolock _l(mLock); |
| 220 | // register effect when first handle is attached and unregister when last handle is removed |
| 221 | if (mPolicyRegistered != mHandles.size() > 0) { |
| 222 | doRegister = true; |
| 223 | mPolicyRegistered = mHandles.size() > 0; |
| 224 | if (mPolicyRegistered) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 225 | io = mCallback->io(); |
| 226 | strategy = mCallback->strategy(); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | // enable effect when registered according to enable state requested by controlling handle |
| 230 | if (mHandles.size() > 0) { |
| 231 | EffectHandle *handle = controlHandle_l(); |
| 232 | if (handle != nullptr && mPolicyEnabled != handle->enabled()) { |
| 233 | doEnable = true; |
| 234 | mPolicyEnabled = handle->enabled(); |
| 235 | } |
| 236 | } |
| 237 | registered = mPolicyRegistered; |
| 238 | enabled = mPolicyEnabled; |
| 239 | mPolicyLock.lock(); |
| 240 | } |
| 241 | ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d", |
| 242 | __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled); |
| 243 | if (doRegister) { |
| 244 | if (registered) { |
| 245 | status = AudioSystem::registerEffect( |
| 246 | &mDescriptor, |
| 247 | io, |
| 248 | strategy, |
| 249 | mSessionId, |
| 250 | mId); |
| 251 | } else { |
| 252 | status = AudioSystem::unregisterEffect(mId); |
| 253 | } |
| 254 | } |
| 255 | if (registered && doEnable) { |
| 256 | status = AudioSystem::setEffectEnabled(mId, enabled); |
| 257 | } |
| 258 | mPolicyLock.unlock(); |
| 259 | |
| 260 | return status; |
| 261 | } |
| 262 | |
| 263 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 264 | ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 265 | { |
| 266 | Mutex::Autolock _l(mLock); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 267 | return removeHandle_l(handle); |
| 268 | } |
| 269 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 270 | ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle) |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 271 | { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 272 | size_t size = mHandles.size(); |
| 273 | size_t i; |
| 274 | for (i = 0; i < size; i++) { |
| 275 | if (mHandles[i] == handle) { |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | if (i == size) { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 280 | ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle); |
| 281 | return BAD_VALUE; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 282 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 283 | ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 284 | |
| 285 | mHandles.removeAt(i); |
| 286 | // if removed from first place, move effect control from this handle to next in line |
| 287 | if (i == 0) { |
| 288 | EffectHandle *h = controlHandle_l(); |
| 289 | if (h != NULL) { |
| 290 | h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/); |
| 291 | } |
| 292 | } |
| 293 | |
Jaideep Sharma | ed868802 | 2020-08-07 14:09:16 +0530 | [diff] [blame] | 294 | // Prevent calls to process() and other functions on effect interface from now on. |
| 295 | // The effect engine will be released by the destructor when the last strong reference on |
| 296 | // this object is released which can happen after next process is called. |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 297 | if (mHandles.size() == 0 && !mPinned) { |
| 298 | mState = DESTROYED; |
| 299 | } |
| 300 | |
| 301 | return mHandles.size(); |
| 302 | } |
| 303 | |
| 304 | // must be called with EffectModule::mLock held |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 305 | AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l() |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 306 | { |
| 307 | // the first valid handle in the list has control over the module |
| 308 | for (size_t i = 0; i < mHandles.size(); i++) { |
| 309 | EffectHandle *h = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 310 | if (h != NULL && !h->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 311 | return h; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 318 | // unsafe method called when the effect parent thread has been destroyed |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 319 | ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast) |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 320 | { |
| 321 | ALOGV("disconnect() %p handle %p", this, handle); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 322 | if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) { |
| 323 | return mHandles.size(); |
| 324 | } |
| 325 | |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 326 | Mutex::Autolock _l(mLock); |
| 327 | ssize_t numHandles = removeHandle_l(handle); |
| 328 | if ((numHandles == 0) && (!mPinned || unpinIfLast)) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 329 | mLock.unlock(); |
| 330 | mCallback->updateOrphanEffectChains(this); |
| 331 | mLock.lock(); |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 332 | } |
| 333 | return numHandles; |
| 334 | } |
| 335 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 336 | bool AudioFlinger::EffectBase::purgeHandles() |
| 337 | { |
| 338 | bool enabled = false; |
| 339 | Mutex::Autolock _l(mLock); |
| 340 | EffectHandle *handle = controlHandle_l(); |
| 341 | if (handle != NULL) { |
| 342 | enabled = handle->enabled(); |
| 343 | } |
| 344 | mHandles.clear(); |
| 345 | return enabled; |
| 346 | } |
| 347 | |
| 348 | void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) { |
| 349 | mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked); |
| 350 | } |
| 351 | |
| 352 | static String8 effectFlagsToString(uint32_t flags) { |
| 353 | String8 s; |
| 354 | |
| 355 | s.append("conn. mode: "); |
| 356 | switch (flags & EFFECT_FLAG_TYPE_MASK) { |
| 357 | case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break; |
| 358 | case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break; |
| 359 | case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break; |
| 360 | case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break; |
| 361 | case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break; |
| 362 | default: s.append("unknown/reserved"); break; |
| 363 | } |
| 364 | s.append(", "); |
| 365 | |
| 366 | s.append("insert pref: "); |
| 367 | switch (flags & EFFECT_FLAG_INSERT_MASK) { |
| 368 | case EFFECT_FLAG_INSERT_ANY: s.append("any"); break; |
| 369 | case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break; |
| 370 | case EFFECT_FLAG_INSERT_LAST: s.append("last"); break; |
| 371 | case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break; |
| 372 | default: s.append("unknown/reserved"); break; |
| 373 | } |
| 374 | s.append(", "); |
| 375 | |
| 376 | s.append("volume mgmt: "); |
| 377 | switch (flags & EFFECT_FLAG_VOLUME_MASK) { |
| 378 | case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break; |
| 379 | case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break; |
| 380 | case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break; |
| 381 | case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break; |
| 382 | default: s.append("unknown/reserved"); break; |
| 383 | } |
| 384 | s.append(", "); |
| 385 | |
| 386 | uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK; |
| 387 | if (devind) { |
| 388 | s.append("device indication: "); |
| 389 | switch (devind) { |
| 390 | case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break; |
| 391 | default: s.append("unknown/reserved"); break; |
| 392 | } |
| 393 | s.append(", "); |
| 394 | } |
| 395 | |
| 396 | s.append("input mode: "); |
| 397 | switch (flags & EFFECT_FLAG_INPUT_MASK) { |
| 398 | case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break; |
| 399 | case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break; |
| 400 | case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break; |
| 401 | default: s.append("not set"); break; |
| 402 | } |
| 403 | s.append(", "); |
| 404 | |
| 405 | s.append("output mode: "); |
| 406 | switch (flags & EFFECT_FLAG_OUTPUT_MASK) { |
| 407 | case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break; |
| 408 | case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break; |
| 409 | case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break; |
| 410 | default: s.append("not set"); break; |
| 411 | } |
| 412 | s.append(", "); |
| 413 | |
| 414 | uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK; |
| 415 | if (accel) { |
| 416 | s.append("hardware acceleration: "); |
| 417 | switch (accel) { |
| 418 | case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break; |
| 419 | case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break; |
| 420 | default: s.append("unknown/reserved"); break; |
| 421 | } |
| 422 | s.append(", "); |
| 423 | } |
| 424 | |
| 425 | uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK; |
| 426 | if (modeind) { |
| 427 | s.append("mode indication: "); |
| 428 | switch (modeind) { |
| 429 | case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break; |
| 430 | default: s.append("unknown/reserved"); break; |
| 431 | } |
| 432 | s.append(", "); |
| 433 | } |
| 434 | |
| 435 | uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK; |
| 436 | if (srcind) { |
| 437 | s.append("source indication: "); |
| 438 | switch (srcind) { |
| 439 | case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break; |
| 440 | default: s.append("unknown/reserved"); break; |
| 441 | } |
| 442 | s.append(", "); |
| 443 | } |
| 444 | |
| 445 | if (flags & EFFECT_FLAG_OFFLOAD_MASK) { |
| 446 | s.append("offloadable, "); |
| 447 | } |
| 448 | |
| 449 | int len = s.length(); |
| 450 | if (s.length() > 2) { |
| 451 | (void) s.lockBuffer(len); |
| 452 | s.unlockBuffer(len - 2); |
| 453 | } |
| 454 | return s; |
| 455 | } |
| 456 | |
| 457 | void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused) |
| 458 | { |
| 459 | String8 result; |
| 460 | |
| 461 | result.appendFormat("\tEffect ID %d:\n", mId); |
| 462 | |
| 463 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 464 | // failed to lock - AudioFlinger is probably deadlocked |
| 465 | if (!locked) { |
| 466 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 467 | } |
| 468 | |
| 469 | result.append("\t\tSession State Registered Enabled Suspended:\n"); |
| 470 | result.appendFormat("\t\t%05d %03d %s %s %s\n", |
| 471 | mSessionId, mState, mPolicyRegistered ? "y" : "n", |
| 472 | mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n"); |
| 473 | |
| 474 | result.append("\t\tDescriptor:\n"); |
| 475 | char uuidStr[64]; |
| 476 | AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr)); |
| 477 | result.appendFormat("\t\t- UUID: %s\n", uuidStr); |
| 478 | AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr)); |
| 479 | result.appendFormat("\t\t- TYPE: %s\n", uuidStr); |
| 480 | result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n", |
| 481 | mDescriptor.apiVersion, |
| 482 | mDescriptor.flags, |
| 483 | effectFlagsToString(mDescriptor.flags).string()); |
| 484 | result.appendFormat("\t\t- name: %s\n", |
| 485 | mDescriptor.name); |
| 486 | |
| 487 | result.appendFormat("\t\t- implementor: %s\n", |
| 488 | mDescriptor.implementor); |
| 489 | |
| 490 | result.appendFormat("\t\t%zu Clients:\n", mHandles.size()); |
| 491 | result.append("\t\t\t Pid Priority Ctrl Locked client server\n"); |
| 492 | char buffer[256]; |
| 493 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 494 | EffectHandle *handle = mHandles[i]; |
| 495 | if (handle != NULL && !handle->disconnected()) { |
| 496 | handle->dumpToBuffer(buffer, sizeof(buffer)); |
| 497 | result.append(buffer); |
| 498 | } |
| 499 | } |
| 500 | if (locked) { |
| 501 | mLock.unlock(); |
| 502 | } |
| 503 | |
| 504 | write(fd, result.string(), result.length()); |
| 505 | } |
| 506 | |
| 507 | // ---------------------------------------------------------------------------- |
| 508 | // EffectModule implementation |
| 509 | // ---------------------------------------------------------------------------- |
| 510 | |
| 511 | #undef LOG_TAG |
| 512 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 513 | |
| 514 | AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback, |
| 515 | effect_descriptor_t *desc, |
| 516 | int id, |
| 517 | audio_session_t sessionId, |
Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 518 | bool pinned, |
| 519 | audio_port_handle_t deviceId) |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 520 | : EffectBase(callback, desc, id, sessionId, pinned), |
| 521 | // clear mConfig to ensure consistent initial value of buffer framecount |
| 522 | // in case buffers are associated by setInBuffer() or setOutBuffer() |
| 523 | // prior to configure(). |
| 524 | mConfig{{}, {}}, |
| 525 | mStatus(NO_INIT), |
| 526 | mMaxDisableWaitCnt(1), // set by configure(), should be >= 1 |
| 527 | mDisableWaitCnt(0), // set by process() and updateState() |
| 528 | mOffloaded(false) |
| 529 | #ifdef FLOAT_EFFECT_CHAIN |
| 530 | , mSupportsFloat(false) |
| 531 | #endif |
| 532 | { |
| 533 | ALOGV("Constructor %p pinned %d", this, pinned); |
| 534 | int lStatus; |
| 535 | |
| 536 | // create effect engine from effect factory |
| 537 | mStatus = callback->createEffectHal( |
Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 538 | &desc->uuid, sessionId, deviceId, &mEffectInterface); |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 539 | if (mStatus != NO_ERROR) { |
| 540 | return; |
| 541 | } |
| 542 | lStatus = init(); |
| 543 | if (lStatus < 0) { |
| 544 | mStatus = lStatus; |
| 545 | goto Error; |
| 546 | } |
| 547 | |
| 548 | setOffloaded(callback->isOffload(), callback->io()); |
| 549 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get()); |
| 550 | |
| 551 | return; |
| 552 | Error: |
| 553 | mEffectInterface.clear(); |
| 554 | ALOGV("Constructor Error %d", mStatus); |
| 555 | } |
| 556 | |
| 557 | AudioFlinger::EffectModule::~EffectModule() |
| 558 | { |
| 559 | ALOGV("Destructor %p", this); |
| 560 | if (mEffectInterface != 0) { |
| 561 | char uuidStr[64]; |
| 562 | AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr)); |
| 563 | ALOGW("EffectModule %p destructor called with unreleased interface, effect %s", |
| 564 | this, uuidStr); |
| 565 | release_l(); |
| 566 | } |
| 567 | |
| 568 | } |
| 569 | |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 570 | bool AudioFlinger::EffectModule::updateState() { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 571 | Mutex::Autolock _l(mLock); |
| 572 | |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 573 | bool started = false; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 574 | switch (mState) { |
| 575 | case RESTART: |
| 576 | reset_l(); |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 577 | FALLTHROUGH_INTENDED; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 578 | |
| 579 | case STARTING: |
| 580 | // clear auxiliary effect input buffer for next accumulation |
| 581 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 582 | memset(mConfig.inputCfg.buffer.raw, |
| 583 | 0, |
| 584 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 585 | } |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 586 | if (start_l() == NO_ERROR) { |
| 587 | mState = ACTIVE; |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 588 | started = true; |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 589 | } else { |
| 590 | mState = IDLE; |
| 591 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 592 | break; |
| 593 | case STOPPING: |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 594 | // volume control for offload and direct threads must take effect immediately. |
| 595 | if (stop_l() == NO_ERROR |
| 596 | && !(isVolumeControl() && isOffloadedOrDirect())) { |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 597 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 598 | } else { |
| 599 | mDisableWaitCnt = 1; // will cause immediate transition to IDLE |
| 600 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 601 | mState = STOPPED; |
| 602 | break; |
| 603 | case STOPPED: |
| 604 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 605 | // turn off sequence. |
| 606 | if (--mDisableWaitCnt == 0) { |
| 607 | reset_l(); |
| 608 | mState = IDLE; |
| 609 | } |
| 610 | break; |
| 611 | default: //IDLE , ACTIVE, DESTROYED |
| 612 | break; |
| 613 | } |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 614 | |
| 615 | return started; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | void AudioFlinger::EffectModule::process() |
| 619 | { |
| 620 | Mutex::Autolock _l(mLock); |
| 621 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 622 | if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 626 | const uint32_t inChannelCount = |
| 627 | audio_channel_count_from_out_mask(mConfig.inputCfg.channels); |
| 628 | const uint32_t outChannelCount = |
| 629 | audio_channel_count_from_out_mask(mConfig.outputCfg.channels); |
| 630 | const bool auxType = |
| 631 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY; |
| 632 | |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 633 | // safeInputOutputSampleCount is 0 if the channel count between input and output |
| 634 | // buffers do not match. This prevents automatic accumulation or copying between the |
| 635 | // input and output effect buffers without an intermediary effect process. |
| 636 | // TODO: consider implementing channel conversion. |
| 637 | const size_t safeInputOutputSampleCount = |
Andy Hung | dd2e7a8 | 2018-10-31 14:19:13 -0700 | [diff] [blame] | 638 | mInChannelCountRequested != mOutChannelCountRequested ? 0 |
| 639 | : mOutChannelCountRequested * std::min( |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 640 | mConfig.inputCfg.buffer.frameCount, |
| 641 | mConfig.outputCfg.buffer.frameCount); |
| 642 | const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() { |
| 643 | #ifdef FLOAT_EFFECT_CHAIN |
| 644 | accumulate_float( |
| 645 | mConfig.outputCfg.buffer.f32, |
| 646 | mConfig.inputCfg.buffer.f32, |
| 647 | safeInputOutputSampleCount); |
| 648 | #else |
| 649 | accumulate_i16( |
| 650 | mConfig.outputCfg.buffer.s16, |
| 651 | mConfig.inputCfg.buffer.s16, |
| 652 | safeInputOutputSampleCount); |
| 653 | #endif |
| 654 | }; |
| 655 | const auto copyInputToOutput = [this, safeInputOutputSampleCount]() { |
| 656 | #ifdef FLOAT_EFFECT_CHAIN |
| 657 | memcpy( |
| 658 | mConfig.outputCfg.buffer.f32, |
| 659 | mConfig.inputCfg.buffer.f32, |
| 660 | safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32)); |
| 661 | |
| 662 | #else |
| 663 | memcpy( |
| 664 | mConfig.outputCfg.buffer.s16, |
| 665 | mConfig.inputCfg.buffer.s16, |
| 666 | safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16)); |
| 667 | #endif |
| 668 | }; |
| 669 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 670 | if (isProcessEnabled()) { |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 671 | int ret; |
| 672 | if (isProcessImplemented()) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 673 | if (auxType) { |
| 674 | // We overwrite the aux input buffer here and clear after processing. |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 675 | // aux input is always mono. |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 676 | #ifdef FLOAT_EFFECT_CHAIN |
| 677 | if (mSupportsFloat) { |
Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 678 | #ifndef FLOAT_AUX |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 679 | // Do in-place float conversion for auxiliary effect input buffer. |
| 680 | static_assert(sizeof(float) <= sizeof(int32_t), |
| 681 | "in-place conversion requires sizeof(float) <= sizeof(int32_t)"); |
| 682 | |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 683 | memcpy_to_float_from_q4_27( |
| 684 | mConfig.inputCfg.buffer.f32, |
| 685 | mConfig.inputCfg.buffer.s32, |
| 686 | mConfig.inputCfg.buffer.frameCount); |
Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 687 | #endif // !FLOAT_AUX |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 688 | } else |
Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 689 | #endif // FLOAT_EFFECT_CHAIN |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 690 | { |
Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 691 | #ifdef FLOAT_AUX |
| 692 | memcpy_to_i16_from_float( |
| 693 | mConfig.inputCfg.buffer.s16, |
| 694 | mConfig.inputCfg.buffer.f32, |
| 695 | mConfig.inputCfg.buffer.frameCount); |
| 696 | #else |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 697 | memcpy_to_i16_from_q4_27( |
| 698 | mConfig.inputCfg.buffer.s16, |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 699 | mConfig.inputCfg.buffer.s32, |
Andy Hung | 5effdf6 | 2017-11-27 13:51:40 -0800 | [diff] [blame] | 700 | mConfig.inputCfg.buffer.frameCount); |
Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 701 | #endif |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 702 | } |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 703 | } |
| 704 | #ifdef FLOAT_EFFECT_CHAIN |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 705 | sp<EffectBufferHalInterface> inBuffer = mInBuffer; |
| 706 | sp<EffectBufferHalInterface> outBuffer = mOutBuffer; |
| 707 | |
| 708 | if (!auxType && mInChannelCountRequested != inChannelCount) { |
| 709 | adjust_channels( |
| 710 | inBuffer->audioBuffer()->f32, mInChannelCountRequested, |
| 711 | mInConversionBuffer->audioBuffer()->f32, inChannelCount, |
| 712 | sizeof(float), |
| 713 | sizeof(float) |
| 714 | * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount); |
| 715 | inBuffer = mInConversionBuffer; |
| 716 | } |
| 717 | if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE |
| 718 | && mOutChannelCountRequested != outChannelCount) { |
| 719 | adjust_selected_channels( |
| 720 | outBuffer->audioBuffer()->f32, mOutChannelCountRequested, |
| 721 | mOutConversionBuffer->audioBuffer()->f32, outChannelCount, |
| 722 | sizeof(float), |
| 723 | sizeof(float) |
| 724 | * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount); |
| 725 | outBuffer = mOutConversionBuffer; |
| 726 | } |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 727 | if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float. |
| 728 | if (!auxType) { |
| 729 | if (mInConversionBuffer.get() == nullptr) { |
| 730 | ALOGW("%s: mInConversionBuffer is null, bypassing", __func__); |
| 731 | goto data_bypass; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 732 | } |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 733 | memcpy_to_i16_from_float( |
| 734 | mInConversionBuffer->audioBuffer()->s16, |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 735 | inBuffer->audioBuffer()->f32, |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 736 | inChannelCount * mConfig.inputCfg.buffer.frameCount); |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 737 | inBuffer = mInConversionBuffer; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 738 | } |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 739 | if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
| 740 | if (mOutConversionBuffer.get() == nullptr) { |
| 741 | ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__); |
| 742 | goto data_bypass; |
| 743 | } |
| 744 | memcpy_to_i16_from_float( |
| 745 | mOutConversionBuffer->audioBuffer()->s16, |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 746 | outBuffer->audioBuffer()->f32, |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 747 | outChannelCount * mConfig.outputCfg.buffer.frameCount); |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 748 | outBuffer = mOutConversionBuffer; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 751 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 752 | ret = mEffectInterface->process(); |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 753 | #ifdef FLOAT_EFFECT_CHAIN |
| 754 | if (!mSupportsFloat) { // convert output int16_t back to float. |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 755 | sp<EffectBufferHalInterface> target = |
| 756 | mOutChannelCountRequested != outChannelCount |
| 757 | ? mOutConversionBuffer : mOutBuffer; |
| 758 | |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 759 | memcpy_to_float_from_i16( |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 760 | target->audioBuffer()->f32, |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 761 | mOutConversionBuffer->audioBuffer()->s16, |
| 762 | outChannelCount * mConfig.outputCfg.buffer.frameCount); |
| 763 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 764 | if (mOutChannelCountRequested != outChannelCount) { |
| 765 | adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount, |
| 766 | mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested, |
| 767 | sizeof(float), |
| 768 | sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount); |
| 769 | } |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 770 | #endif |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 771 | } else { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 772 | #ifdef FLOAT_EFFECT_CHAIN |
| 773 | data_bypass: |
| 774 | #endif |
| 775 | if (!auxType /* aux effects do not require data bypass */ |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 776 | && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 777 | if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 778 | accumulateInputToOutput(); |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 779 | } else { |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 780 | copyInputToOutput(); |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | ret = -ENODATA; |
| 784 | } |
Andy Hung | fa69ca3 | 2017-11-30 10:07:53 -0800 | [diff] [blame] | 785 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 786 | // force transition to IDLE state when engine is ready |
| 787 | if (mState == STOPPED && ret == -ENODATA) { |
| 788 | mDisableWaitCnt = 1; |
| 789 | } |
| 790 | |
| 791 | // clear auxiliary effect input buffer for next accumulation |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 792 | if (auxType) { |
Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 793 | #ifdef FLOAT_AUX |
| 794 | const size_t size = |
| 795 | mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float); |
| 796 | #else |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 797 | const size_t size = |
| 798 | mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t); |
Andy Hung | 116a498 | 2017-11-30 10:15:08 -0800 | [diff] [blame] | 799 | #endif |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 800 | memset(mConfig.inputCfg.buffer.raw, 0, size); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 801 | } |
| 802 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 803 | // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 804 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 805 | // If an insert effect is idle and input buffer is different from output buffer, |
| 806 | // accumulate input onto output |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 807 | if (mCallback->activeTrackCnt() != 0) { |
Andy Hung | e8ac1b2 | 2018-10-31 14:22:35 -0700 | [diff] [blame] | 808 | // similar handling with data_bypass above. |
| 809 | if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
| 810 | accumulateInputToOutput(); |
| 811 | } else { // EFFECT_BUFFER_ACCESS_WRITE |
| 812 | copyInputToOutput(); |
| 813 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | void AudioFlinger::EffectModule::reset_l() |
| 819 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 820 | if (mStatus != NO_ERROR || mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 821 | return; |
| 822 | } |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 823 | mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | status_t AudioFlinger::EffectModule::configure() |
| 827 | { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 828 | ALOGVV("configure() started"); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 829 | status_t status; |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 830 | uint32_t size; |
| 831 | audio_channel_mask_t channelMask; |
| 832 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 833 | if (mEffectInterface == 0) { |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 834 | status = NO_INIT; |
| 835 | goto exit; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 836 | } |
| 837 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 838 | // TODO: handle configuration of effects replacing track process |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 839 | // TODO: handle configuration of input (record) SW effects above the HAL, |
| 840 | // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE, |
| 841 | // in which case input channel masks should be used here. |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 842 | channelMask = mCallback->channelMask(); |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 843 | mConfig.inputCfg.channels = channelMask; |
Ricardo Garcia | d11da70 | 2015-05-28 12:14:12 -0700 | [diff] [blame] | 844 | mConfig.outputCfg.channels = channelMask; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 845 | |
| 846 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 847 | if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) { |
| 848 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
| 849 | ALOGV("Overriding auxiliary effect input channels %#x as MONO", |
| 850 | mConfig.inputCfg.channels); |
| 851 | } |
| 852 | #ifndef MULTICHANNEL_EFFECT_CHAIN |
| 853 | if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) { |
| 854 | mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 855 | ALOGV("Overriding auxiliary effect output channels %#x as STEREO", |
| 856 | mConfig.outputCfg.channels); |
| 857 | } |
| 858 | #endif |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 859 | } else { |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 860 | #ifndef MULTICHANNEL_EFFECT_CHAIN |
Ricardo Garcia | d11da70 | 2015-05-28 12:14:12 -0700 | [diff] [blame] | 861 | // TODO: Update this logic when multichannel effects are implemented. |
| 862 | // For offloaded tracks consider mono output as stereo for proper effect initialization |
| 863 | if (channelMask == AUDIO_CHANNEL_OUT_MONO) { |
| 864 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 865 | mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 866 | ALOGV("Overriding effect input and output as STEREO"); |
| 867 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 868 | #endif |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 869 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 870 | mInChannelCountRequested = |
| 871 | audio_channel_count_from_out_mask(mConfig.inputCfg.channels); |
| 872 | mOutChannelCountRequested = |
| 873 | audio_channel_count_from_out_mask(mConfig.outputCfg.channels); |
Ricardo Garcia | d11da70 | 2015-05-28 12:14:12 -0700 | [diff] [blame] | 874 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 875 | mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT; |
| 876 | mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT; |
Yuuki Yokoyama | e17f831 | 2017-05-26 19:06:33 +0900 | [diff] [blame] | 877 | |
| 878 | // Don't use sample rate for thread if effect isn't offloadable. |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 879 | if (mCallback->isOffloadOrDirect() && !isOffloaded()) { |
Yuuki Yokoyama | e17f831 | 2017-05-26 19:06:33 +0900 | [diff] [blame] | 880 | mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE; |
| 881 | ALOGV("Overriding effect input as 48kHz"); |
| 882 | } else { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 883 | mConfig.inputCfg.samplingRate = mCallback->sampleRate(); |
Yuuki Yokoyama | e17f831 | 2017-05-26 19:06:33 +0900 | [diff] [blame] | 884 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 885 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 886 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 887 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 888 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 889 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 890 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 891 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 892 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 893 | // Insert effect: |
Eric Laurent | a20c4e9 | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 894 | // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX), |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 895 | // always overwrites output buffer: input buffer == output buffer |
| 896 | // - in other sessions: |
| 897 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 898 | // other effect: overwrites output buffer: input buffer == output buffer |
| 899 | // Auxiliary effect: |
| 900 | // accumulates in output buffer: input buffer != output buffer |
| 901 | // Therefore: accumulate <=> input buffer != output buffer |
| 902 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 903 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 904 | } else { |
| 905 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 906 | } |
| 907 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 908 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 909 | mConfig.inputCfg.buffer.frameCount = mCallback->frameCount(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 910 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 911 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 912 | ALOGV("configure() %p chain %p buffer %p framecount %zu", |
| 913 | this, mCallback->chain().promote() != nullptr ? mCallback->chain().promote().get() : |
| 914 | nullptr, |
| 915 | mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 916 | |
| 917 | status_t cmdStatus; |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 918 | size = sizeof(int); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 919 | status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG, |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 920 | sizeof(mConfig), |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 921 | &mConfig, |
| 922 | &size, |
| 923 | &cmdStatus); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 924 | if (status == NO_ERROR) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 925 | status = cmdStatus; |
| 926 | } |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 927 | |
| 928 | #ifdef MULTICHANNEL_EFFECT_CHAIN |
| 929 | if (status != NO_ERROR && |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 930 | mCallback->isOutput() && |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 931 | (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO |
| 932 | || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) { |
| 933 | // Older effects may require exact STEREO position mask. |
Andy Hung | 01b3272 | 2018-05-18 13:52:02 -0700 | [diff] [blame] | 934 | if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO |
| 935 | && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 936 | ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels); |
| 937 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 938 | } |
| 939 | if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) { |
| 940 | ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels); |
| 941 | mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 942 | } |
| 943 | size = sizeof(int); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 944 | status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG, |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 945 | sizeof(mConfig), |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 946 | &mConfig, |
| 947 | &size, |
| 948 | &cmdStatus); |
| 949 | if (status == NO_ERROR) { |
| 950 | status = cmdStatus; |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | #endif |
| 954 | |
| 955 | #ifdef FLOAT_EFFECT_CHAIN |
| 956 | if (status == NO_ERROR) { |
| 957 | mSupportsFloat = true; |
| 958 | } |
| 959 | |
| 960 | if (status != NO_ERROR) { |
| 961 | ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t."); |
| 962 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 963 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 964 | size = sizeof(int); |
| 965 | status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG, |
| 966 | sizeof(mConfig), |
| 967 | &mConfig, |
| 968 | &size, |
| 969 | &cmdStatus); |
| 970 | if (status == NO_ERROR) { |
| 971 | status = cmdStatus; |
| 972 | } |
| 973 | if (status == NO_ERROR) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 974 | mSupportsFloat = false; |
| 975 | ALOGVV("config worked with 16 bit"); |
| 976 | } else { |
| 977 | ALOGE("%s failed %d with int16_t (as well as float)", __func__, status); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 978 | } |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 979 | } |
| 980 | #endif |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 981 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 982 | if (status == NO_ERROR) { |
| 983 | // Establish Buffer strategy |
| 984 | setInBuffer(mInBuffer); |
| 985 | setOutBuffer(mOutBuffer); |
| 986 | |
| 987 | // Update visualizer latency |
| 988 | if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) { |
| 989 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 990 | effect_param_t *p = (effect_param_t *)buf32; |
| 991 | |
| 992 | p->psize = sizeof(uint32_t); |
| 993 | p->vsize = sizeof(uint32_t); |
| 994 | size = sizeof(int); |
| 995 | *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY; |
| 996 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 997 | uint32_t latency = mCallback->latency(); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 998 | |
| 999 | *((int32_t *)p->data + 1)= latency; |
| 1000 | mEffectInterface->command(EFFECT_CMD_SET_PARAM, |
| 1001 | sizeof(effect_param_t) + 8, |
| 1002 | &buf32, |
| 1003 | &size, |
| 1004 | &cmdStatus); |
| 1005 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Andy Hung | 05083ac | 2017-12-14 15:00:28 -0800 | [diff] [blame] | 1008 | // mConfig.outputCfg.buffer.frameCount cannot be zero. |
| 1009 | mMaxDisableWaitCnt = (uint32_t)std::max( |
| 1010 | (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero. |
| 1011 | (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate |
| 1012 | / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount)); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1013 | |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1014 | exit: |
Andy Hung | 6f88dc4 | 2017-12-13 16:19:39 -0800 | [diff] [blame] | 1015 | // TODO: consider clearing mConfig on error. |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1016 | mStatus = status; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1017 | ALOGVV("configure ended"); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1018 | return status; |
| 1019 | } |
| 1020 | |
| 1021 | status_t AudioFlinger::EffectModule::init() |
| 1022 | { |
| 1023 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1024 | if (mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1025 | return NO_INIT; |
| 1026 | } |
| 1027 | status_t cmdStatus; |
| 1028 | uint32_t size = sizeof(status_t); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1029 | status_t status = mEffectInterface->command(EFFECT_CMD_INIT, |
| 1030 | 0, |
| 1031 | NULL, |
| 1032 | &size, |
| 1033 | &cmdStatus); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1034 | if (status == 0) { |
| 1035 | status = cmdStatus; |
| 1036 | } |
| 1037 | return status; |
| 1038 | } |
| 1039 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 1040 | void AudioFlinger::EffectModule::addEffectToHal_l() |
| 1041 | { |
| 1042 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 1043 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1044 | (void)mCallback->addEffectToHal(mEffectInterface); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 1048 | // start() must be called with PlaybackThread::mLock or EffectChain::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1049 | status_t AudioFlinger::EffectModule::start() |
| 1050 | { |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 1051 | status_t status; |
| 1052 | { |
| 1053 | Mutex::Autolock _l(mLock); |
| 1054 | status = start_l(); |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 1055 | } |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1056 | if (status == NO_ERROR) { |
| 1057 | mCallback->resetVolume(); |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 1058 | } |
| 1059 | return status; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | status_t AudioFlinger::EffectModule::start_l() |
| 1063 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1064 | if (mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1065 | return NO_INIT; |
| 1066 | } |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1067 | if (mStatus != NO_ERROR) { |
| 1068 | return mStatus; |
| 1069 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1070 | status_t cmdStatus; |
| 1071 | uint32_t size = sizeof(status_t); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1072 | status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE, |
| 1073 | 0, |
| 1074 | NULL, |
| 1075 | &size, |
| 1076 | &cmdStatus); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1077 | if (status == 0) { |
| 1078 | status = cmdStatus; |
| 1079 | } |
Eric Laurent | cb4b6e9 | 2014-10-01 14:26:10 -0700 | [diff] [blame] | 1080 | if (status == 0) { |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 1081 | addEffectToHal_l(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1082 | } |
| 1083 | return status; |
| 1084 | } |
| 1085 | |
| 1086 | status_t AudioFlinger::EffectModule::stop() |
| 1087 | { |
| 1088 | Mutex::Autolock _l(mLock); |
| 1089 | return stop_l(); |
| 1090 | } |
| 1091 | |
| 1092 | status_t AudioFlinger::EffectModule::stop_l() |
| 1093 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1094 | if (mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1095 | return NO_INIT; |
| 1096 | } |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1097 | if (mStatus != NO_ERROR) { |
| 1098 | return mStatus; |
| 1099 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1100 | status_t cmdStatus = NO_ERROR; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1101 | uint32_t size = sizeof(status_t); |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1102 | |
| 1103 | if (isVolumeControl() && isOffloadedOrDirect()) { |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1104 | // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume: |
| 1105 | // resetVolume_l --> setVolume_l --> EffectModule::setVolume |
| 1106 | mSetVolumeReentrantTid = gettid(); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1107 | mCallback->resetVolume(); |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1108 | mSetVolumeReentrantTid = INVALID_PID; |
| 1109 | } |
| 1110 | |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1111 | status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE, |
| 1112 | 0, |
| 1113 | NULL, |
| 1114 | &size, |
| 1115 | &cmdStatus); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1116 | if (status == NO_ERROR) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1117 | status = cmdStatus; |
| 1118 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1119 | if (status == NO_ERROR) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1120 | status = removeEffectFromHal_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1121 | } |
| 1122 | return status; |
| 1123 | } |
| 1124 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1125 | // must be called with EffectChain::mLock held |
| 1126 | void AudioFlinger::EffectModule::release_l() |
| 1127 | { |
| 1128 | if (mEffectInterface != 0) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1129 | removeEffectFromHal_l(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1130 | // release effect engine |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1131 | mEffectInterface->close(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1132 | mEffectInterface.clear(); |
| 1133 | } |
| 1134 | } |
| 1135 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1136 | status_t AudioFlinger::EffectModule::removeEffectFromHal_l() |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1137 | { |
| 1138 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 1139 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1140 | mCallback->removeEffectFromHal(mEffectInterface); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1141 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1142 | return NO_ERROR; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Andy Hung | e4a1d91 | 2016-08-17 14:11:13 -0700 | [diff] [blame] | 1145 | // round up delta valid if value and divisor are positive. |
| 1146 | template <typename T> |
| 1147 | static T roundUpDelta(const T &value, const T &divisor) { |
| 1148 | T remainder = value % divisor; |
| 1149 | return remainder == 0 ? 0 : divisor - remainder; |
| 1150 | } |
| 1151 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1152 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 1153 | uint32_t cmdSize, |
| 1154 | void *pCmdData, |
| 1155 | uint32_t *replySize, |
| 1156 | void *pReplyData) |
| 1157 | { |
| 1158 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1159 | ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1160 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 1161 | if (mState == DESTROYED || mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1162 | return NO_INIT; |
| 1163 | } |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1164 | if (mStatus != NO_ERROR) { |
| 1165 | return mStatus; |
| 1166 | } |
Andy Hung | 110bc95 | 2016-06-20 15:22:52 -0700 | [diff] [blame] | 1167 | if (cmdCode == EFFECT_CMD_GET_PARAM && |
Andy Hung | 6660f12 | 2016-11-04 19:40:53 -0700 | [diff] [blame] | 1168 | (sizeof(effect_param_t) > cmdSize || |
| 1169 | ((effect_param_t *)pCmdData)->psize > cmdSize |
| 1170 | - sizeof(effect_param_t))) { |
| 1171 | android_errorWriteLog(0x534e4554, "32438594"); |
Andy Hung | b345664 | 2016-11-28 13:50:21 -0800 | [diff] [blame] | 1172 | android_errorWriteLog(0x534e4554, "33003822"); |
| 1173 | return -EINVAL; |
| 1174 | } |
| 1175 | if (cmdCode == EFFECT_CMD_GET_PARAM && |
| 1176 | (*replySize < sizeof(effect_param_t) || |
| 1177 | ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) { |
| 1178 | android_errorWriteLog(0x534e4554, "29251553"); |
Andy Hung | 6660f12 | 2016-11-04 19:40:53 -0700 | [diff] [blame] | 1179 | return -EINVAL; |
| 1180 | } |
rago | e275907 | 2016-11-22 18:02:48 -0800 | [diff] [blame] | 1181 | if (cmdCode == EFFECT_CMD_GET_PARAM && |
| 1182 | (sizeof(effect_param_t) > *replySize |
| 1183 | || ((effect_param_t *)pCmdData)->psize > *replySize |
| 1184 | - sizeof(effect_param_t) |
| 1185 | || ((effect_param_t *)pCmdData)->vsize > *replySize |
| 1186 | - sizeof(effect_param_t) |
| 1187 | - ((effect_param_t *)pCmdData)->psize |
| 1188 | || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) > |
| 1189 | *replySize |
| 1190 | - sizeof(effect_param_t) |
| 1191 | - ((effect_param_t *)pCmdData)->psize |
| 1192 | - ((effect_param_t *)pCmdData)->vsize)) { |
| 1193 | ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent"); |
| 1194 | android_errorWriteLog(0x534e4554, "32705438"); |
| 1195 | return -EINVAL; |
| 1196 | } |
Andy Hung | e4a1d91 | 2016-08-17 14:11:13 -0700 | [diff] [blame] | 1197 | if ((cmdCode == EFFECT_CMD_SET_PARAM |
| 1198 | || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used |
| 1199 | (sizeof(effect_param_t) > cmdSize |
| 1200 | || ((effect_param_t *)pCmdData)->psize > cmdSize |
| 1201 | - sizeof(effect_param_t) |
| 1202 | || ((effect_param_t *)pCmdData)->vsize > cmdSize |
| 1203 | - sizeof(effect_param_t) |
| 1204 | - ((effect_param_t *)pCmdData)->psize |
| 1205 | || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) > |
| 1206 | cmdSize |
| 1207 | - sizeof(effect_param_t) |
| 1208 | - ((effect_param_t *)pCmdData)->psize |
| 1209 | - ((effect_param_t *)pCmdData)->vsize)) { |
| 1210 | android_errorWriteLog(0x534e4554, "30204301"); |
| 1211 | return -EINVAL; |
| 1212 | } |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1213 | status_t status = mEffectInterface->command(cmdCode, |
| 1214 | cmdSize, |
| 1215 | pCmdData, |
| 1216 | replySize, |
| 1217 | pReplyData); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1218 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
| 1219 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
| 1220 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 1221 | EffectHandle *h = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1222 | if (h != NULL && !h->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1223 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | return status; |
| 1228 | } |
| 1229 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1230 | bool AudioFlinger::EffectModule::isProcessEnabled() const |
| 1231 | { |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1232 | if (mStatus != NO_ERROR) { |
| 1233 | return false; |
| 1234 | } |
| 1235 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1236 | switch (mState) { |
| 1237 | case RESTART: |
| 1238 | case ACTIVE: |
| 1239 | case STOPPING: |
| 1240 | case STOPPED: |
| 1241 | return true; |
| 1242 | case IDLE: |
| 1243 | case STARTING: |
| 1244 | case DESTROYED: |
| 1245 | default: |
| 1246 | return false; |
| 1247 | } |
| 1248 | } |
| 1249 | |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1250 | bool AudioFlinger::EffectModule::isOffloadedOrDirect() const |
| 1251 | { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1252 | return mCallback->isOffloadOrDirect(); |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | bool AudioFlinger::EffectModule::isVolumeControlEnabled() const |
| 1256 | { |
| 1257 | return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled())); |
| 1258 | } |
| 1259 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1260 | void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1261 | ALOGVV("setInBuffer %p",(&buffer)); |
Andy Hung | 6f88dc4 | 2017-12-13 16:19:39 -0800 | [diff] [blame] | 1262 | |
| 1263 | // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet. |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1264 | if (buffer != 0) { |
| 1265 | mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw; |
| 1266 | buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount); |
| 1267 | } else { |
| 1268 | mConfig.inputCfg.buffer.raw = NULL; |
| 1269 | } |
| 1270 | mInBuffer = buffer; |
Andy Hung | c15aaee | 2017-11-27 17:02:40 -0800 | [diff] [blame] | 1271 | mEffectInterface->setInBuffer(buffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1272 | |
| 1273 | #ifdef FLOAT_EFFECT_CHAIN |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1274 | // aux effects do in place conversion to float - we don't allocate mInConversionBuffer. |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1275 | // Theoretically insert effects can also do in-place conversions (destroying |
| 1276 | // the original buffer) when the output buffer is identical to the input buffer, |
| 1277 | // but we don't optimize for it here. |
| 1278 | const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY; |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1279 | const uint32_t inChannelCount = |
| 1280 | audio_channel_count_from_out_mask(mConfig.inputCfg.channels); |
| 1281 | const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount; |
| 1282 | if (!auxType && formatMismatch && mInBuffer.get() != nullptr) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1283 | // we need to translate - create hidl shared buffer and intercept |
| 1284 | const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount; |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1285 | // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo. |
| 1286 | const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested); |
| 1287 | const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float)); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1288 | |
| 1289 | ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu", |
| 1290 | __func__, inChannels, inFrameCount, size); |
| 1291 | |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1292 | if (size > 0 && (mInConversionBuffer.get() == nullptr |
| 1293 | || size > mInConversionBuffer->getSize())) { |
| 1294 | mInConversionBuffer.clear(); |
| 1295 | ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1296 | (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1297 | } |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1298 | if (mInConversionBuffer.get() != nullptr) { |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1299 | mInConversionBuffer->setFrameCount(inFrameCount); |
| 1300 | mEffectInterface->setInBuffer(mInConversionBuffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1301 | } else if (size > 0) { |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1302 | ALOGE("%s cannot create mInConversionBuffer", __func__); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1309 | ALOGVV("setOutBuffer %p",(&buffer)); |
Andy Hung | 6f88dc4 | 2017-12-13 16:19:39 -0800 | [diff] [blame] | 1310 | |
| 1311 | // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet. |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1312 | if (buffer != 0) { |
| 1313 | mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw; |
| 1314 | buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount); |
| 1315 | } else { |
| 1316 | mConfig.outputCfg.buffer.raw = NULL; |
| 1317 | } |
| 1318 | mOutBuffer = buffer; |
Andy Hung | c15aaee | 2017-11-27 17:02:40 -0800 | [diff] [blame] | 1319 | mEffectInterface->setOutBuffer(buffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1320 | |
| 1321 | #ifdef FLOAT_EFFECT_CHAIN |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1322 | // Note: Any effect that does not accumulate does not need mOutConversionBuffer and |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1323 | // can do in-place conversion from int16_t to float. We don't optimize here. |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1324 | const uint32_t outChannelCount = |
| 1325 | audio_channel_count_from_out_mask(mConfig.outputCfg.channels); |
| 1326 | const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount; |
| 1327 | if (formatMismatch && mOutBuffer.get() != nullptr) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1328 | const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount; |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 1329 | // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo. |
| 1330 | const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested); |
| 1331 | const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float)); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1332 | |
| 1333 | ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu", |
| 1334 | __func__, outChannels, outFrameCount, size); |
| 1335 | |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1336 | if (size > 0 && (mOutConversionBuffer.get() == nullptr |
| 1337 | || size > mOutConversionBuffer->getSize())) { |
| 1338 | mOutConversionBuffer.clear(); |
| 1339 | ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1340 | (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1341 | } |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1342 | if (mOutConversionBuffer.get() != nullptr) { |
| 1343 | mOutConversionBuffer->setFrameCount(outFrameCount); |
| 1344 | mEffectInterface->setOutBuffer(mOutConversionBuffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1345 | } else if (size > 0) { |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1346 | ALOGE("%s cannot create mOutConversionBuffer", __func__); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1352 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 1353 | { |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1354 | AutoLockReentrant _l(mLock, mSetVolumeReentrantTid); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1355 | if (mStatus != NO_ERROR) { |
| 1356 | return mStatus; |
| 1357 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1358 | status_t status = NO_ERROR; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1359 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 1360 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) |
| 1361 | if (isProcessEnabled() && |
| 1362 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
Jasmine Cha | 934ecfb | 2019-01-23 18:19:14 +0800 | [diff] [blame] | 1363 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND || |
| 1364 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1365 | uint32_t volume[2]; |
| 1366 | uint32_t *pVolume = NULL; |
| 1367 | uint32_t size = sizeof(volume); |
| 1368 | volume[0] = *left; |
| 1369 | volume[1] = *right; |
| 1370 | if (controller) { |
| 1371 | pVolume = volume; |
| 1372 | } |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1373 | status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME, |
| 1374 | size, |
| 1375 | volume, |
| 1376 | &size, |
| 1377 | pVolume); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1378 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 1379 | *left = volume[0]; |
| 1380 | *right = volume[1]; |
| 1381 | } |
| 1382 | } |
| 1383 | return status; |
| 1384 | } |
| 1385 | |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1386 | void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right) |
| 1387 | { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1388 | if (mEffectCallback->isOffloadOrDirect() && !isNonOffloadableEnabled_l()) { |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1389 | float vol_l = (float)left / (1 << 24); |
| 1390 | float vol_r = (float)right / (1 << 24); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1391 | mEffectCallback->setVolumeForOutput(vol_l, vol_r); |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1395 | status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand( |
| 1396 | const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1397 | { |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1398 | audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices)); |
| 1399 | if (deviceType == AUDIO_DEVICE_NONE) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1400 | return NO_ERROR; |
| 1401 | } |
| 1402 | |
| 1403 | Mutex::Autolock _l(mLock); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1404 | if (mStatus != NO_ERROR) { |
| 1405 | return mStatus; |
| 1406 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1407 | status_t status = NO_ERROR; |
Eric Laurent | 7e1139c | 2013-06-06 18:29:01 -0700 | [diff] [blame] | 1408 | if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1409 | status_t cmdStatus; |
| 1410 | uint32_t size = sizeof(status_t); |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1411 | // FIXME: use audio device types and addresses when the hal interface is ready. |
| 1412 | status = mEffectInterface->command(cmdCode, |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1413 | sizeof(uint32_t), |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1414 | &deviceType, |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1415 | &size, |
| 1416 | &cmdStatus); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1417 | } |
| 1418 | return status; |
| 1419 | } |
| 1420 | |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 1421 | status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices) |
| 1422 | { |
| 1423 | return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE); |
| 1424 | } |
| 1425 | |
| 1426 | status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device) |
| 1427 | { |
| 1428 | return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE); |
| 1429 | } |
| 1430 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1431 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) |
| 1432 | { |
| 1433 | Mutex::Autolock _l(mLock); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1434 | if (mStatus != NO_ERROR) { |
| 1435 | return mStatus; |
| 1436 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1437 | status_t status = NO_ERROR; |
| 1438 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { |
| 1439 | status_t cmdStatus; |
| 1440 | uint32_t size = sizeof(status_t); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1441 | status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE, |
| 1442 | sizeof(audio_mode_t), |
| 1443 | &mode, |
| 1444 | &size, |
| 1445 | &cmdStatus); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1446 | if (status == NO_ERROR) { |
| 1447 | status = cmdStatus; |
| 1448 | } |
| 1449 | } |
| 1450 | return status; |
| 1451 | } |
| 1452 | |
| 1453 | status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source) |
| 1454 | { |
| 1455 | Mutex::Autolock _l(mLock); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1456 | if (mStatus != NO_ERROR) { |
| 1457 | return mStatus; |
| 1458 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1459 | status_t status = NO_ERROR; |
| 1460 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) { |
| 1461 | uint32_t size = 0; |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1462 | status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE, |
| 1463 | sizeof(audio_source_t), |
| 1464 | &source, |
| 1465 | &size, |
| 1466 | NULL); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1467 | } |
| 1468 | return status; |
| 1469 | } |
| 1470 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1471 | status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io) |
| 1472 | { |
| 1473 | Mutex::Autolock _l(mLock); |
| 1474 | if (mStatus != NO_ERROR) { |
| 1475 | return mStatus; |
| 1476 | } |
| 1477 | status_t status = NO_ERROR; |
| 1478 | if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) { |
| 1479 | status_t cmdStatus; |
| 1480 | uint32_t size = sizeof(status_t); |
| 1481 | effect_offload_param_t cmd; |
| 1482 | |
| 1483 | cmd.isOffload = offloaded; |
| 1484 | cmd.ioHandle = io; |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1485 | status = mEffectInterface->command(EFFECT_CMD_OFFLOAD, |
| 1486 | sizeof(effect_offload_param_t), |
| 1487 | &cmd, |
| 1488 | &size, |
| 1489 | &cmdStatus); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1490 | if (status == NO_ERROR) { |
| 1491 | status = cmdStatus; |
| 1492 | } |
| 1493 | mOffloaded = (status == NO_ERROR) ? offloaded : false; |
| 1494 | } else { |
| 1495 | if (offloaded) { |
| 1496 | status = INVALID_OPERATION; |
| 1497 | } |
| 1498 | mOffloaded = false; |
| 1499 | } |
| 1500 | ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status); |
| 1501 | return status; |
| 1502 | } |
| 1503 | |
| 1504 | bool AudioFlinger::EffectModule::isOffloaded() const |
| 1505 | { |
| 1506 | Mutex::Autolock _l(mLock); |
| 1507 | return mOffloaded; |
| 1508 | } |
| 1509 | |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1510 | static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) { |
| 1511 | std::stringstream ss; |
| 1512 | |
| 1513 | if (buffer.get() == nullptr) { |
| 1514 | return "nullptr"; // make different than below |
| 1515 | } else if (buffer->externalData() != nullptr) { |
| 1516 | ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw) |
| 1517 | << " -> " |
| 1518 | << (isInput ? buffer->audioBuffer()->raw : buffer->externalData()); |
| 1519 | } else { |
| 1520 | ss << buffer->audioBuffer()->raw; |
| 1521 | } |
| 1522 | return ss.str(); |
| 1523 | } |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1524 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1525 | void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1526 | { |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1527 | EffectBase::dump(fd, args); |
| 1528 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1529 | String8 result; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1530 | bool locked = AudioFlinger::dumpTryLock(mLock); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1531 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1532 | result.append("\t\tStatus Engine:\n"); |
| 1533 | result.appendFormat("\t\t%03d %p\n", |
| 1534 | mStatus, mEffectInterface.get()); |
Andy Hung | 9718d66 | 2017-12-22 17:57:39 -0800 | [diff] [blame] | 1535 | |
| 1536 | result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16"); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1537 | |
| 1538 | result.append("\t\t- Input configuration:\n"); |
Andy Hung | 9718d66 | 2017-12-22 17:57:39 -0800 | [diff] [blame] | 1539 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
| 1540 | result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n", |
| 1541 | mConfig.inputCfg.buffer.raw, |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1542 | mConfig.inputCfg.buffer.frameCount, |
| 1543 | mConfig.inputCfg.samplingRate, |
| 1544 | mConfig.inputCfg.channels, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1545 | mConfig.inputCfg.format, |
Andy Hung | 9718d66 | 2017-12-22 17:57:39 -0800 | [diff] [blame] | 1546 | formatToString((audio_format_t)mConfig.inputCfg.format).c_str()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1547 | |
| 1548 | result.append("\t\t- Output configuration:\n"); |
| 1549 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
Andy Hung | 9718d66 | 2017-12-22 17:57:39 -0800 | [diff] [blame] | 1550 | result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n", |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1551 | mConfig.outputCfg.buffer.raw, |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1552 | mConfig.outputCfg.buffer.frameCount, |
| 1553 | mConfig.outputCfg.samplingRate, |
| 1554 | mConfig.outputCfg.channels, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1555 | mConfig.outputCfg.format, |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 1556 | formatToString((audio_format_t)mConfig.outputCfg.format).c_str()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1557 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1558 | #ifdef FLOAT_EFFECT_CHAIN |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1559 | |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 1560 | result.appendFormat("\t\t- HAL buffers:\n" |
| 1561 | "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n", |
| 1562 | dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(), |
| 1563 | dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(), |
| 1564 | dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(), |
| 1565 | dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str()); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1566 | #endif |
| 1567 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1568 | write(fd, result.string(), result.length()); |
| 1569 | |
Mikhail Naganov | 4d54767 | 2019-02-22 14:19:19 -0800 | [diff] [blame] | 1570 | if (mEffectInterface != 0) { |
| 1571 | dprintf(fd, "\tEffect ID %d HAL dump:\n", mId); |
| 1572 | (void)mEffectInterface->dump(fd); |
| 1573 | } |
| 1574 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1575 | if (locked) { |
| 1576 | mLock.unlock(); |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | // ---------------------------------------------------------------------------- |
| 1581 | // EffectHandle implementation |
| 1582 | // ---------------------------------------------------------------------------- |
| 1583 | |
| 1584 | #undef LOG_TAG |
| 1585 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 1586 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1587 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect, |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1588 | const sp<AudioFlinger::Client>& client, |
| 1589 | const sp<IEffectClient>& effectClient, |
| 1590 | int32_t priority) |
| 1591 | : BnEffect(), |
| 1592 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1593 | mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1594 | { |
Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1595 | ALOGV("constructor %p client %p", this, client.get()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1596 | |
| 1597 | if (client == 0) { |
| 1598 | return; |
| 1599 | } |
| 1600 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 1601 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1602 | if (mCblkMemory == 0 || |
| 1603 | (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1604 | ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE + |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1605 | sizeof(effect_param_cblk_t)); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1606 | mCblkMemory.clear(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1607 | return; |
| 1608 | } |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1609 | new(mCblk) effect_param_cblk_t(); |
| 1610 | mBuffer = (uint8_t *)mCblk + bufOffset; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | AudioFlinger::EffectHandle::~EffectHandle() |
| 1614 | { |
| 1615 | ALOGV("Destructor %p", this); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1616 | disconnect(false); |
| 1617 | } |
| 1618 | |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1619 | status_t AudioFlinger::EffectHandle::initCheck() |
| 1620 | { |
| 1621 | return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY; |
| 1622 | } |
| 1623 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1624 | status_t AudioFlinger::EffectHandle::enable() |
| 1625 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1626 | AutoMutex _l(mLock); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1627 | ALOGV("enable %p", this); |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1628 | sp<EffectBase> effect = mEffect.promote(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1629 | if (effect == 0 || mDisconnected) { |
| 1630 | return DEAD_OBJECT; |
| 1631 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1632 | if (!mHasControl) { |
| 1633 | return INVALID_OPERATION; |
| 1634 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1635 | |
| 1636 | if (mEnabled) { |
| 1637 | return NO_ERROR; |
| 1638 | } |
| 1639 | |
| 1640 | mEnabled = true; |
| 1641 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1642 | status_t status = effect->updatePolicyState(); |
| 1643 | if (status != NO_ERROR) { |
| 1644 | mEnabled = false; |
| 1645 | return status; |
| 1646 | } |
| 1647 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1648 | effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1649 | |
| 1650 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1651 | if (effect->suspended()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1652 | return NO_ERROR; |
| 1653 | } |
| 1654 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1655 | status = effect->setEnabled(true, true /*fromHandle*/); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1656 | if (status != NO_ERROR) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1657 | mEnabled = false; |
| 1658 | } |
| 1659 | return status; |
| 1660 | } |
| 1661 | |
| 1662 | status_t AudioFlinger::EffectHandle::disable() |
| 1663 | { |
| 1664 | ALOGV("disable %p", this); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1665 | AutoMutex _l(mLock); |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1666 | sp<EffectBase> effect = mEffect.promote(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1667 | if (effect == 0 || mDisconnected) { |
| 1668 | return DEAD_OBJECT; |
| 1669 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1670 | if (!mHasControl) { |
| 1671 | return INVALID_OPERATION; |
| 1672 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1673 | |
| 1674 | if (!mEnabled) { |
| 1675 | return NO_ERROR; |
| 1676 | } |
| 1677 | mEnabled = false; |
| 1678 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1679 | effect->updatePolicyState(); |
| 1680 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1681 | if (effect->suspended()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1682 | return NO_ERROR; |
| 1683 | } |
| 1684 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1685 | status_t status = effect->setEnabled(false, true /*fromHandle*/); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1686 | return status; |
| 1687 | } |
| 1688 | |
| 1689 | void AudioFlinger::EffectHandle::disconnect() |
| 1690 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1691 | ALOGV("%s %p", __FUNCTION__, this); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1692 | disconnect(true); |
| 1693 | } |
| 1694 | |
| 1695 | void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) |
| 1696 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1697 | AutoMutex _l(mLock); |
| 1698 | ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this); |
| 1699 | if (mDisconnected) { |
| 1700 | if (unpinIfLast) { |
| 1701 | android_errorWriteLog(0x534e4554, "32707507"); |
| 1702 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1703 | return; |
| 1704 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1705 | mDisconnected = true; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1706 | { |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1707 | sp<EffectBase> effect = mEffect.promote(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1708 | if (effect != 0) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1709 | if (effect->disconnectHandle(this, unpinIfLast) > 0) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1710 | ALOGW("%s Effect handle %p disconnected after thread destruction", |
| 1711 | __func__, this); |
| 1712 | } |
| 1713 | effect->updatePolicyState(); |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 1714 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1715 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1716 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1717 | if (mClient != 0) { |
| 1718 | if (mCblk != NULL) { |
| 1719 | // unlike ~TrackBase(), mCblk is never a local new, so don't delete |
| 1720 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 1721 | } |
| 1722 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1723 | // Client destructor must run with AudioFlinger client mutex locked |
| 1724 | Mutex::Autolock _l(mClient->audioFlinger()->mClientLock); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | mClient.clear(); |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 1730 | uint32_t cmdSize, |
| 1731 | void *pCmdData, |
| 1732 | uint32_t *replySize, |
| 1733 | void *pReplyData) |
| 1734 | { |
| 1735 | ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1736 | cmdCode, mHasControl, mEffect.unsafe_get()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1737 | |
Eric Laurent | c7ab309 | 2017-06-15 18:43:46 -0700 | [diff] [blame] | 1738 | // reject commands reserved for internal use by audio framework if coming from outside |
| 1739 | // of audioserver |
| 1740 | switch(cmdCode) { |
| 1741 | case EFFECT_CMD_ENABLE: |
| 1742 | case EFFECT_CMD_DISABLE: |
| 1743 | case EFFECT_CMD_SET_PARAM: |
| 1744 | case EFFECT_CMD_SET_PARAM_DEFERRED: |
| 1745 | case EFFECT_CMD_SET_PARAM_COMMIT: |
| 1746 | case EFFECT_CMD_GET_PARAM: |
| 1747 | break; |
| 1748 | default: |
| 1749 | if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) { |
| 1750 | break; |
| 1751 | } |
| 1752 | android_errorWriteLog(0x534e4554, "62019992"); |
| 1753 | return BAD_VALUE; |
| 1754 | } |
| 1755 | |
Eric Laurent | 1ffc585 | 2016-12-15 14:46:09 -0800 | [diff] [blame] | 1756 | if (cmdCode == EFFECT_CMD_ENABLE) { |
| 1757 | if (*replySize < sizeof(int)) { |
| 1758 | android_errorWriteLog(0x534e4554, "32095713"); |
| 1759 | return BAD_VALUE; |
| 1760 | } |
| 1761 | *(int *)pReplyData = NO_ERROR; |
| 1762 | *replySize = sizeof(int); |
| 1763 | return enable(); |
| 1764 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
| 1765 | if (*replySize < sizeof(int)) { |
| 1766 | android_errorWriteLog(0x534e4554, "32095713"); |
| 1767 | return BAD_VALUE; |
| 1768 | } |
| 1769 | *(int *)pReplyData = NO_ERROR; |
| 1770 | *replySize = sizeof(int); |
| 1771 | return disable(); |
| 1772 | } |
| 1773 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1774 | AutoMutex _l(mLock); |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 1775 | sp<EffectBase> effect = mEffect.promote(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1776 | if (effect == 0 || mDisconnected) { |
| 1777 | return DEAD_OBJECT; |
| 1778 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1779 | // only get parameter command is permitted for applications not controlling the effect |
| 1780 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 1781 | return INVALID_OPERATION; |
| 1782 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1783 | |
| 1784 | // handle commands that are not forwarded transparently to effect engine |
| 1785 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1786 | if (mClient == 0) { |
| 1787 | return INVALID_OPERATION; |
| 1788 | } |
| 1789 | |
Eric Laurent | 1ffc585 | 2016-12-15 14:46:09 -0800 | [diff] [blame] | 1790 | if (*replySize < sizeof(int)) { |
| 1791 | android_errorWriteLog(0x534e4554, "32095713"); |
| 1792 | return BAD_VALUE; |
| 1793 | } |
| 1794 | *(int *)pReplyData = NO_ERROR; |
| 1795 | *replySize = sizeof(int); |
| 1796 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1797 | // No need to trylock() here as this function is executed in the binder thread serving a |
| 1798 | // particular client process: no risk to block the whole media server process or mixer |
| 1799 | // threads if we are stuck here |
| 1800 | Mutex::Autolock _l(mCblk->lock); |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1801 | // keep local copy of index in case of client corruption b/32220769 |
| 1802 | const uint32_t clientIndex = mCblk->clientIndex; |
| 1803 | const uint32_t serverIndex = mCblk->serverIndex; |
| 1804 | if (clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 1805 | serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1806 | mCblk->serverIndex = 0; |
| 1807 | mCblk->clientIndex = 0; |
| 1808 | return BAD_VALUE; |
| 1809 | } |
| 1810 | status_t status = NO_ERROR; |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1811 | effect_param_t *param = NULL; |
| 1812 | for (uint32_t index = serverIndex; index < clientIndex;) { |
| 1813 | int *p = (int *)(mBuffer + index); |
| 1814 | const int size = *p++; |
| 1815 | if (size < 0 |
| 1816 | || size > EFFECT_PARAM_BUFFER_SIZE |
| 1817 | || ((uint8_t *)p + size) > mBuffer + clientIndex) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1818 | ALOGW("command(): invalid parameter block size"); |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1819 | status = BAD_VALUE; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1820 | break; |
| 1821 | } |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1822 | |
| 1823 | // copy to local memory in case of client corruption b/32220769 |
George Burgess IV | 80a2216 | 2020-01-05 20:06:15 -0800 | [diff] [blame] | 1824 | auto *newParam = (effect_param_t *)realloc(param, size); |
| 1825 | if (newParam == NULL) { |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1826 | ALOGW("command(): out of memory"); |
| 1827 | status = NO_MEMORY; |
| 1828 | break; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1829 | } |
George Burgess IV | 80a2216 | 2020-01-05 20:06:15 -0800 | [diff] [blame] | 1830 | param = newParam; |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1831 | memcpy(param, p, size); |
| 1832 | |
| 1833 | int reply = 0; |
| 1834 | uint32_t rsize = sizeof(reply); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1835 | status_t ret = effect->command(EFFECT_CMD_SET_PARAM, |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1836 | size, |
| 1837 | param, |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1838 | &rsize, |
| 1839 | &reply); |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1840 | |
| 1841 | // verify shared memory: server index shouldn't change; client index can't go back. |
| 1842 | if (serverIndex != mCblk->serverIndex |
| 1843 | || clientIndex > mCblk->clientIndex) { |
| 1844 | android_errorWriteLog(0x534e4554, "32220769"); |
| 1845 | status = BAD_VALUE; |
| 1846 | break; |
| 1847 | } |
| 1848 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1849 | // stop at first error encountered |
| 1850 | if (ret != NO_ERROR) { |
| 1851 | status = ret; |
| 1852 | *(int *)pReplyData = reply; |
| 1853 | break; |
| 1854 | } else if (reply != NO_ERROR) { |
| 1855 | *(int *)pReplyData = reply; |
| 1856 | break; |
| 1857 | } |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1858 | index += size; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1859 | } |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1860 | free(param); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1861 | mCblk->serverIndex = 0; |
| 1862 | mCblk->clientIndex = 0; |
| 1863 | return status; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1864 | } |
| 1865 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1866 | return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
| 1870 | { |
| 1871 | ALOGV("setControl %p control %d", this, hasControl); |
| 1872 | |
| 1873 | mHasControl = hasControl; |
| 1874 | mEnabled = enabled; |
| 1875 | |
| 1876 | if (signal && mEffectClient != 0) { |
| 1877 | mEffectClient->controlStatusChanged(hasControl); |
| 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 1882 | uint32_t cmdSize, |
| 1883 | void *pCmdData, |
| 1884 | uint32_t replySize, |
| 1885 | void *pReplyData) |
| 1886 | { |
| 1887 | if (mEffectClient != 0) { |
| 1888 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 1895 | { |
| 1896 | if (mEffectClient != 0) { |
| 1897 | mEffectClient->enableStatusChanged(enabled); |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | status_t AudioFlinger::EffectHandle::onTransact( |
| 1902 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1903 | { |
| 1904 | return BnEffect::onTransact(code, data, reply, flags); |
| 1905 | } |
| 1906 | |
| 1907 | |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 1908 | void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1909 | { |
| 1910 | bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock); |
| 1911 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1912 | snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n", |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 1913 | (mClient == 0) ? getpid() : mClient->pid(), |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1914 | mPriority, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1915 | mHasControl ? "yes" : "no", |
| 1916 | locked ? "yes" : "no", |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1917 | mCblk ? mCblk->clientIndex : 0, |
| 1918 | mCblk ? mCblk->serverIndex : 0 |
| 1919 | ); |
| 1920 | |
| 1921 | if (locked) { |
| 1922 | mCblk->lock.unlock(); |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | #undef LOG_TAG |
| 1927 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 1928 | |
| 1929 | AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1930 | audio_session_t sessionId) |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1931 | : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1932 | mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1933 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), |
| 1934 | mEffectCallback(new EffectCallback(this, thread, thread->mAudioFlinger.get())) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1935 | { |
| 1936 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 1937 | if (thread == nullptr) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1938 | return; |
| 1939 | } |
| 1940 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 1941 | thread->frameCount(); |
| 1942 | } |
| 1943 | |
| 1944 | AudioFlinger::EffectChain::~EffectChain() |
| 1945 | { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
| 1949 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l( |
| 1950 | effect_descriptor_t *descriptor) |
| 1951 | { |
| 1952 | size_t size = mEffects.size(); |
| 1953 | |
| 1954 | for (size_t i = 0; i < size; i++) { |
| 1955 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
| 1956 | return mEffects[i]; |
| 1957 | } |
| 1958 | } |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
| 1963 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
| 1964 | { |
| 1965 | size_t size = mEffects.size(); |
| 1966 | |
| 1967 | for (size_t i = 0; i < size; i++) { |
| 1968 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 1969 | if (id == 0 || mEffects[i]->id() == id) { |
| 1970 | return mEffects[i]; |
| 1971 | } |
| 1972 | } |
| 1973 | return 0; |
| 1974 | } |
| 1975 | |
| 1976 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 1977 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 1978 | const effect_uuid_t *type) |
| 1979 | { |
| 1980 | size_t size = mEffects.size(); |
| 1981 | |
| 1982 | for (size_t i = 0; i < size; i++) { |
| 1983 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
| 1984 | return mEffects[i]; |
| 1985 | } |
| 1986 | } |
| 1987 | return 0; |
| 1988 | } |
| 1989 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1990 | std::vector<int> AudioFlinger::EffectChain::getEffectIds() |
| 1991 | { |
| 1992 | std::vector<int> ids; |
| 1993 | Mutex::Autolock _l(mLock); |
| 1994 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 1995 | ids.push_back(mEffects[i]->id()); |
| 1996 | } |
| 1997 | return ids; |
| 1998 | } |
| 1999 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2000 | void AudioFlinger::EffectChain::clearInputBuffer() |
| 2001 | { |
| 2002 | Mutex::Autolock _l(mLock); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2003 | clearInputBuffer_l(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | // Must be called with EffectChain::mLock locked |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2007 | void AudioFlinger::EffectChain::clearInputBuffer_l() |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2008 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2009 | if (mInBuffer == NULL) { |
| 2010 | return; |
| 2011 | } |
Ricardo Garcia | 726b6a7 | 2014-08-11 12:04:54 -0700 | [diff] [blame] | 2012 | const size_t frameSize = |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2013 | audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount(); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2014 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2015 | memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2016 | mInBuffer->commit(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | // Must be called with EffectChain::mLock locked |
| 2020 | void AudioFlinger::EffectChain::process_l() |
| 2021 | { |
Jean-Michel Trivi | fed6292 | 2013-09-25 18:50:33 -0700 | [diff] [blame] | 2022 | // never process effects when: |
| 2023 | // - on an OFFLOAD thread |
| 2024 | // - no more tracks are on the session and the effect tail has been rendered |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2025 | bool doProcess = !mEffectCallback->isOffloadOrMmap(); |
Eric Laurent | a20c4e9 | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 2026 | if (!audio_is_global_session(mSessionId)) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2027 | bool tracksOnSession = (trackCnt() != 0); |
| 2028 | |
| 2029 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 2030 | doProcess = false; |
| 2031 | } |
| 2032 | |
| 2033 | if (activeTrackCnt() == 0) { |
| 2034 | // if no track is active and the effect tail has not been rendered, |
| 2035 | // the input buffer must be cleared here as the mixer process will not do it |
| 2036 | if (tracksOnSession || mTailBufferCount > 0) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2037 | clearInputBuffer_l(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2038 | if (mTailBufferCount > 0) { |
| 2039 | mTailBufferCount--; |
| 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | size_t size = mEffects.size(); |
| 2046 | if (doProcess) { |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2047 | // Only the input and output buffers of the chain can be external, |
| 2048 | // and 'update' / 'commit' do nothing for allocated buffers, thus |
| 2049 | // it's not needed to consider any other buffers here. |
| 2050 | mInBuffer->update(); |
Mikhail Naganov | 0688880 | 2017-01-19 12:47:55 -0800 | [diff] [blame] | 2051 | if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) { |
| 2052 | mOutBuffer->update(); |
| 2053 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2054 | for (size_t i = 0; i < size; i++) { |
| 2055 | mEffects[i]->process(); |
| 2056 | } |
Mikhail Naganov | 0688880 | 2017-01-19 12:47:55 -0800 | [diff] [blame] | 2057 | mInBuffer->commit(); |
| 2058 | if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) { |
| 2059 | mOutBuffer->commit(); |
| 2060 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2061 | } |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2062 | bool doResetVolume = false; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2063 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2064 | doResetVolume = mEffects[i]->updateState() || doResetVolume; |
| 2065 | } |
| 2066 | if (doResetVolume) { |
| 2067 | resetVolume_l(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2068 | } |
| 2069 | } |
| 2070 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2071 | // createEffect_l() must be called with ThreadBase::mLock held |
| 2072 | status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2073 | effect_descriptor_t *desc, |
| 2074 | int id, |
| 2075 | audio_session_t sessionId, |
| 2076 | bool pinned) |
| 2077 | { |
| 2078 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 2079 | effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2080 | status_t lStatus = effect->status(); |
| 2081 | if (lStatus == NO_ERROR) { |
| 2082 | lStatus = addEffect_ll(effect); |
| 2083 | } |
| 2084 | if (lStatus != NO_ERROR) { |
| 2085 | effect.clear(); |
| 2086 | } |
| 2087 | return lStatus; |
| 2088 | } |
| 2089 | |
| 2090 | // addEffect_l() must be called with ThreadBase::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2091 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
| 2092 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2093 | Mutex::Autolock _l(mLock); |
| 2094 | return addEffect_ll(effect); |
| 2095 | } |
| 2096 | // addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held |
| 2097 | status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect) |
| 2098 | { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2099 | effect_descriptor_t desc = effect->desc(); |
| 2100 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 2101 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2102 | effect->setCallback(mEffectCallback); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2103 | |
| 2104 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 2105 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 2106 | // they are processed first and accumulated in chain input buffer |
| 2107 | mEffects.insertAt(effect, 0); |
| 2108 | |
| 2109 | // the input buffer for auxiliary effect contains mono samples in |
| 2110 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 2111 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 2112 | // calling the process in effect engine |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2113 | size_t numSamples = mEffectCallback->frameCount(); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2114 | sp<EffectBufferHalInterface> halBuffer; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2115 | #ifdef FLOAT_EFFECT_CHAIN |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2116 | status_t result = mEffectCallback->allocateHalBuffer( |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2117 | numSamples * sizeof(float), &halBuffer); |
| 2118 | #else |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2119 | status_t result = mEffectCallback->allocateHalBuffer( |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2120 | numSamples * sizeof(int32_t), &halBuffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 2121 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2122 | if (result != OK) return result; |
| 2123 | effect->setInBuffer(halBuffer); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2124 | // auxiliary effects output samples to chain input buffer for further processing |
| 2125 | // by insert effects |
| 2126 | effect->setOutBuffer(mInBuffer); |
| 2127 | } else { |
| 2128 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 2129 | // after track and auxiliary effects. |
| 2130 | // Insert effect order as a function of indicated preference: |
| 2131 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 2132 | // another effect is present |
| 2133 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 2134 | // last effect claiming first position |
| 2135 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 2136 | // first effect claiming last position |
| 2137 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 2138 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 2139 | // already present |
| 2140 | |
| 2141 | size_t size = mEffects.size(); |
| 2142 | size_t idx_insert = size; |
| 2143 | ssize_t idx_insert_first = -1; |
| 2144 | ssize_t idx_insert_last = -1; |
| 2145 | |
| 2146 | for (size_t i = 0; i < size; i++) { |
| 2147 | effect_descriptor_t d = mEffects[i]->desc(); |
| 2148 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 2149 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 2150 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 2151 | // check invalid effect chaining combinations |
| 2152 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 2153 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
| 2154 | ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", |
| 2155 | desc.name, d.name); |
| 2156 | return INVALID_OPERATION; |
| 2157 | } |
| 2158 | // remember position of first insert effect and by default |
| 2159 | // select this as insert position for new effect |
| 2160 | if (idx_insert == size) { |
| 2161 | idx_insert = i; |
| 2162 | } |
| 2163 | // remember position of last insert effect claiming |
| 2164 | // first position |
| 2165 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 2166 | idx_insert_first = i; |
| 2167 | } |
| 2168 | // remember position of first insert effect claiming |
| 2169 | // last position |
| 2170 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 2171 | idx_insert_last == -1) { |
| 2172 | idx_insert_last = i; |
| 2173 | } |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | // modify idx_insert from first position if needed |
| 2178 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 2179 | if (idx_insert_last != -1) { |
| 2180 | idx_insert = idx_insert_last; |
| 2181 | } else { |
| 2182 | idx_insert = size; |
| 2183 | } |
| 2184 | } else { |
| 2185 | if (idx_insert_first != -1) { |
| 2186 | idx_insert = idx_insert_first + 1; |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | // always read samples from chain input buffer |
| 2191 | effect->setInBuffer(mInBuffer); |
| 2192 | |
| 2193 | // if last effect in the chain, output samples to chain |
| 2194 | // output buffer, otherwise to chain input buffer |
| 2195 | if (idx_insert == size) { |
| 2196 | if (idx_insert != 0) { |
| 2197 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 2198 | mEffects[idx_insert-1]->configure(); |
| 2199 | } |
| 2200 | effect->setOutBuffer(mOutBuffer); |
| 2201 | } else { |
| 2202 | effect->setOutBuffer(mInBuffer); |
| 2203 | } |
| 2204 | mEffects.insertAt(effect, idx_insert); |
| 2205 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2206 | ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this, |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2207 | idx_insert); |
| 2208 | } |
| 2209 | effect->configure(); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2210 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2211 | return NO_ERROR; |
| 2212 | } |
| 2213 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2214 | // removeEffect_l() must be called with ThreadBase::mLock held |
| 2215 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect, |
| 2216 | bool release) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2217 | { |
| 2218 | Mutex::Autolock _l(mLock); |
| 2219 | size_t size = mEffects.size(); |
| 2220 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 2221 | |
| 2222 | for (size_t i = 0; i < size; i++) { |
| 2223 | if (effect == mEffects[i]) { |
| 2224 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 2225 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 2226 | // the middle of a read from audio HAL |
| 2227 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 2228 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 2229 | mEffects[i]->stop(); |
| 2230 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2231 | if (release) { |
| 2232 | mEffects[i]->release_l(); |
| 2233 | } |
| 2234 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2235 | if (type != EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2236 | if (i == size - 1 && i != 0) { |
| 2237 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 2238 | mEffects[i - 1]->configure(); |
| 2239 | } |
| 2240 | } |
| 2241 | mEffects.removeAt(i); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2242 | ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(), |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2243 | this, i); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2244 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2245 | break; |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | return mEffects.size(); |
| 2250 | } |
| 2251 | |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 2252 | // setDevices_l() must be called with ThreadBase::mLock held |
| 2253 | void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2254 | { |
| 2255 | size_t size = mEffects.size(); |
| 2256 | for (size_t i = 0; i < size; i++) { |
jiabin | b8269fd | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 2257 | mEffects[i]->setDevices(devices); |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | // setInputDevice_l() must be called with ThreadBase::mLock held |
| 2262 | void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device) |
| 2263 | { |
| 2264 | size_t size = mEffects.size(); |
| 2265 | for (size_t i = 0; i < size; i++) { |
| 2266 | mEffects[i]->setInputDevice(device); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2270 | // setMode_l() must be called with ThreadBase::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2271 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) |
| 2272 | { |
| 2273 | size_t size = mEffects.size(); |
| 2274 | for (size_t i = 0; i < size; i++) { |
| 2275 | mEffects[i]->setMode(mode); |
| 2276 | } |
| 2277 | } |
| 2278 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2279 | // setAudioSource_l() must be called with ThreadBase::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2280 | void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source) |
| 2281 | { |
| 2282 | size_t size = mEffects.size(); |
| 2283 | for (size_t i = 0; i < size; i++) { |
| 2284 | mEffects[i]->setAudioSource(source); |
| 2285 | } |
| 2286 | } |
| 2287 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2288 | // setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2289 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2290 | { |
| 2291 | uint32_t newLeft = *left; |
| 2292 | uint32_t newRight = *right; |
| 2293 | bool hasControl = false; |
| 2294 | int ctrlIdx = -1; |
| 2295 | size_t size = mEffects.size(); |
| 2296 | |
| 2297 | // first update volume controller |
| 2298 | for (size_t i = size; i > 0; i--) { |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 2299 | if (mEffects[i - 1]->isVolumeControlEnabled()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2300 | ctrlIdx = i - 1; |
| 2301 | hasControl = true; |
| 2302 | break; |
| 2303 | } |
| 2304 | } |
| 2305 | |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2306 | if (!force && ctrlIdx == mVolumeCtrlIdx && |
Eric Laurent | cb4b6e9 | 2014-10-01 14:26:10 -0700 | [diff] [blame] | 2307 | *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2308 | if (hasControl) { |
| 2309 | *left = mNewLeftVolume; |
| 2310 | *right = mNewRightVolume; |
| 2311 | } |
| 2312 | return hasControl; |
| 2313 | } |
| 2314 | |
| 2315 | mVolumeCtrlIdx = ctrlIdx; |
| 2316 | mLeftVolume = newLeft; |
| 2317 | mRightVolume = newRight; |
| 2318 | |
| 2319 | // second get volume update from volume controller |
| 2320 | if (ctrlIdx >= 0) { |
| 2321 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
| 2322 | mNewLeftVolume = newLeft; |
| 2323 | mNewRightVolume = newRight; |
| 2324 | } |
| 2325 | // then indicate volume to all other effects in chain. |
| 2326 | // Pass altered volume to effects before volume controller |
Jasmine Cha | 934ecfb | 2019-01-23 18:19:14 +0800 | [diff] [blame] | 2327 | // and requested volume to effects after controller or with volume monitor flag |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2328 | uint32_t lVol = newLeft; |
| 2329 | uint32_t rVol = newRight; |
| 2330 | |
| 2331 | for (size_t i = 0; i < size; i++) { |
| 2332 | if ((int)i == ctrlIdx) { |
| 2333 | continue; |
| 2334 | } |
| 2335 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 2336 | if ((int)i > ctrlIdx) { |
| 2337 | lVol = *left; |
| 2338 | rVol = *right; |
| 2339 | } |
Jasmine Cha | 934ecfb | 2019-01-23 18:19:14 +0800 | [diff] [blame] | 2340 | // Pass requested volume directly if this is volume monitor module |
| 2341 | if (mEffects[i]->isVolumeMonitor()) { |
| 2342 | mEffects[i]->setVolume(left, right, false); |
| 2343 | } else { |
| 2344 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 2345 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2346 | } |
| 2347 | *left = newLeft; |
| 2348 | *right = newRight; |
| 2349 | |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 2350 | setVolumeForOutput_l(*left, *right); |
| 2351 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2352 | return hasControl; |
| 2353 | } |
| 2354 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2355 | // resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2356 | void AudioFlinger::EffectChain::resetVolume_l() |
| 2357 | { |
Eric Laurent | e7449bf | 2016-08-03 18:44:07 -0700 | [diff] [blame] | 2358 | if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) { |
| 2359 | uint32_t left = mLeftVolume; |
| 2360 | uint32_t right = mRightVolume; |
| 2361 | (void)setVolume_l(&left, &right, true); |
| 2362 | } |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2365 | void AudioFlinger::EffectChain::syncHalEffectsState() |
| 2366 | { |
| 2367 | Mutex::Autolock _l(mLock); |
| 2368 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2369 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 2370 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 2371 | mEffects[i]->addEffectToHal_l(); |
| 2372 | } |
| 2373 | } |
| 2374 | } |
| 2375 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2376 | void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 2377 | { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2378 | String8 result; |
| 2379 | |
Mikhail Naganov | 19740ca | 2019-03-28 12:25:01 -0700 | [diff] [blame] | 2380 | const size_t numEffects = mEffects.size(); |
| 2381 | result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2382 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2383 | if (numEffects) { |
| 2384 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 2385 | // failed to lock - AudioFlinger is probably deadlocked |
| 2386 | if (!locked) { |
| 2387 | result.append("\tCould not lock mutex:\n"); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2388 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2389 | |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 2390 | const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer); |
| 2391 | const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer); |
| 2392 | result.appendFormat("\t%-*s%-*s Active tracks:\n", |
| 2393 | (int)inBufferStr.size(), "In buffer ", |
| 2394 | (int)outBufferStr.size(), "Out buffer "); |
| 2395 | result.appendFormat("\t%s %s %d\n", |
| 2396 | inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2397 | write(fd, result.string(), result.size()); |
| 2398 | |
| 2399 | for (size_t i = 0; i < numEffects; ++i) { |
| 2400 | sp<EffectModule> effect = mEffects[i]; |
| 2401 | if (effect != 0) { |
| 2402 | effect->dump(fd, args); |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | if (locked) { |
| 2407 | mLock.unlock(); |
| 2408 | } |
Mikhail Naganov | 19740ca | 2019-03-28 12:25:01 -0700 | [diff] [blame] | 2409 | } else { |
| 2410 | write(fd, result.string(), result.size()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | // must be called with ThreadBase::mLock held |
| 2415 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 2416 | const effect_uuid_t *type, bool suspend) |
| 2417 | { |
| 2418 | sp<SuspendedEffectDesc> desc; |
| 2419 | // use effect type UUID timelow as key as there is no real risk of identical |
| 2420 | // timeLow fields among effect type UUIDs. |
| 2421 | ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); |
| 2422 | if (suspend) { |
| 2423 | if (index >= 0) { |
| 2424 | desc = mSuspendedEffects.valueAt(index); |
| 2425 | } else { |
| 2426 | desc = new SuspendedEffectDesc(); |
| 2427 | desc->mType = *type; |
| 2428 | mSuspendedEffects.add(type->timeLow, desc); |
| 2429 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
| 2430 | } |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2431 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2432 | if (desc->mRefCount++ == 0) { |
| 2433 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 2434 | if (effect != 0) { |
| 2435 | desc->mEffect = effect; |
| 2436 | effect->setSuspended(true); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2437 | effect->setEnabled(false, false /*fromHandle*/); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | } else { |
| 2441 | if (index < 0) { |
| 2442 | return; |
| 2443 | } |
| 2444 | desc = mSuspendedEffects.valueAt(index); |
| 2445 | if (desc->mRefCount <= 0) { |
| 2446 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2447 | desc->mRefCount = 0; |
| 2448 | return; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2449 | } |
| 2450 | if (--desc->mRefCount == 0) { |
| 2451 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
| 2452 | if (desc->mEffect != 0) { |
| 2453 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 2454 | if (effect != 0) { |
| 2455 | effect->setSuspended(false); |
| 2456 | effect->lock(); |
| 2457 | EffectHandle *handle = effect->controlHandle_l(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2458 | if (handle != NULL && !handle->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2459 | effect->setEnabled_l(handle->enabled()); |
| 2460 | } |
| 2461 | effect->unlock(); |
| 2462 | } |
| 2463 | desc->mEffect.clear(); |
| 2464 | } |
| 2465 | mSuspendedEffects.removeItemsAt(index); |
| 2466 | } |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | // must be called with ThreadBase::mLock held |
| 2471 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 2472 | { |
| 2473 | sp<SuspendedEffectDesc> desc; |
| 2474 | |
| 2475 | ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 2476 | if (suspend) { |
| 2477 | if (index >= 0) { |
| 2478 | desc = mSuspendedEffects.valueAt(index); |
| 2479 | } else { |
| 2480 | desc = new SuspendedEffectDesc(); |
| 2481 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
| 2482 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
| 2483 | } |
| 2484 | if (desc->mRefCount++ == 0) { |
| 2485 | Vector< sp<EffectModule> > effects; |
| 2486 | getSuspendEligibleEffects(effects); |
| 2487 | for (size_t i = 0; i < effects.size(); i++) { |
| 2488 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 2489 | } |
| 2490 | } |
| 2491 | } else { |
| 2492 | if (index < 0) { |
| 2493 | return; |
| 2494 | } |
| 2495 | desc = mSuspendedEffects.valueAt(index); |
| 2496 | if (desc->mRefCount <= 0) { |
| 2497 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
| 2498 | desc->mRefCount = 1; |
| 2499 | } |
| 2500 | if (--desc->mRefCount == 0) { |
| 2501 | Vector<const effect_uuid_t *> types; |
| 2502 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 2503 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 2504 | continue; |
| 2505 | } |
| 2506 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 2507 | } |
| 2508 | for (size_t i = 0; i < types.size(); i++) { |
| 2509 | setEffectSuspended_l(types[i], false); |
| 2510 | } |
| 2511 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", |
| 2512 | mSuspendedEffects.keyAt(index)); |
| 2513 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 2514 | } |
| 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | |
| 2519 | // The volume effect is used for automated tests only |
| 2520 | #ifndef OPENSL_ES_H_ |
| 2521 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 2522 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 2523 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 2524 | #endif //OPENSL_ES_H_ |
| 2525 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2526 | /* static */ |
| 2527 | bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type) |
| 2528 | { |
| 2529 | // Only NS and AEC are suspended when BtNRec is off |
| 2530 | if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) || |
| 2531 | (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) { |
| 2532 | return true; |
| 2533 | } |
| 2534 | return false; |
| 2535 | } |
| 2536 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2537 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 2538 | { |
| 2539 | // auxiliary effects and visualizer are never suspended on output mix |
| 2540 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 2541 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
| 2542 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
Ricardo Garcia | c2a3a82 | 2019-07-17 14:29:12 -0700 | [diff] [blame] | 2543 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) || |
| 2544 | (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2545 | return false; |
| 2546 | } |
| 2547 | return true; |
| 2548 | } |
| 2549 | |
| 2550 | void AudioFlinger::EffectChain::getSuspendEligibleEffects( |
| 2551 | Vector< sp<AudioFlinger::EffectModule> > &effects) |
| 2552 | { |
| 2553 | effects.clear(); |
| 2554 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2555 | if (isEffectEligibleForSuspend(mEffects[i]->desc())) { |
| 2556 | effects.add(mEffects[i]); |
| 2557 | } |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 2562 | const effect_uuid_t *type) |
| 2563 | { |
| 2564 | sp<EffectModule> effect = getEffectFromType_l(type); |
| 2565 | return effect != 0 && effect->isEnabled() ? effect : 0; |
| 2566 | } |
| 2567 | |
| 2568 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 2569 | bool enabled) |
| 2570 | { |
| 2571 | ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
| 2572 | if (enabled) { |
| 2573 | if (index < 0) { |
| 2574 | // if the effect is not suspend check if all effects are suspended |
| 2575 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 2576 | if (index < 0) { |
| 2577 | return; |
| 2578 | } |
| 2579 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 2580 | return; |
| 2581 | } |
| 2582 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 2583 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
| 2584 | if (index < 0) { |
| 2585 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
| 2586 | return; |
| 2587 | } |
| 2588 | } |
| 2589 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
| 2590 | effect->desc().type.timeLow); |
| 2591 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2592 | // if effect is requested to suspended but was not yet enabled, suspend it now. |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2593 | if (desc->mEffect == 0) { |
| 2594 | desc->mEffect = effect; |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2595 | effect->setEnabled(false, false /*fromHandle*/); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2596 | effect->setSuspended(true); |
| 2597 | } |
| 2598 | } else { |
| 2599 | if (index < 0) { |
| 2600 | return; |
| 2601 | } |
| 2602 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
| 2603 | effect->desc().type.timeLow); |
| 2604 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 2605 | desc->mEffect.clear(); |
| 2606 | effect->setSuspended(false); |
| 2607 | } |
| 2608 | } |
| 2609 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2610 | bool AudioFlinger::EffectChain::isNonOffloadableEnabled() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2611 | { |
| 2612 | Mutex::Autolock _l(mLock); |
Shingo Kitajima | 1f8df9a | 2018-05-29 11:35:06 +0900 | [diff] [blame] | 2613 | return isNonOffloadableEnabled_l(); |
| 2614 | } |
| 2615 | |
| 2616 | bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l() |
| 2617 | { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2618 | size_t size = mEffects.size(); |
| 2619 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2620 | if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2621 | return true; |
| 2622 | } |
| 2623 | } |
| 2624 | return false; |
| 2625 | } |
| 2626 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2627 | void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread) |
| 2628 | { |
| 2629 | Mutex::Autolock _l(mLock); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2630 | mEffectCallback->setThread(thread.get()); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2631 | } |
| 2632 | |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2633 | void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const |
| 2634 | { |
| 2635 | if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) { |
| 2636 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW); |
| 2637 | } |
| 2638 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) { |
| 2639 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const |
| 2644 | { |
| 2645 | if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) { |
| 2646 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW); |
| 2647 | } |
| 2648 | if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) { |
| 2649 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | bool AudioFlinger::EffectChain::isRawCompatible() const |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2654 | { |
| 2655 | Mutex::Autolock _l(mLock); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2656 | for (const auto &effect : mEffects) { |
| 2657 | if (effect->isProcessImplemented()) { |
| 2658 | return false; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2659 | } |
| 2660 | } |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2661 | // Allow effects without processing. |
| 2662 | return true; |
| 2663 | } |
| 2664 | |
| 2665 | bool AudioFlinger::EffectChain::isFastCompatible() const |
| 2666 | { |
| 2667 | Mutex::Autolock _l(mLock); |
| 2668 | for (const auto &effect : mEffects) { |
| 2669 | if (effect->isProcessImplemented() |
| 2670 | && effect->isImplementationSoftware()) { |
| 2671 | return false; |
| 2672 | } |
| 2673 | } |
| 2674 | // Allow effects without processing or hw accelerated effects. |
| 2675 | return true; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | // isCompatibleWithThread_l() must be called with thread->mLock held |
| 2679 | bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const |
| 2680 | { |
| 2681 | Mutex::Autolock _l(mLock); |
| 2682 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2683 | if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) { |
| 2684 | return false; |
| 2685 | } |
| 2686 | } |
| 2687 | return true; |
| 2688 | } |
| 2689 | |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2690 | // EffectCallbackInterface implementation |
| 2691 | status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal( |
| 2692 | const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId, |
| 2693 | sp<EffectHalInterface> *effect) { |
| 2694 | status_t status = NO_INIT; |
| 2695 | sp<AudioFlinger> af = mAudioFlinger.promote(); |
| 2696 | if (af == nullptr) { |
| 2697 | return status; |
| 2698 | } |
| 2699 | sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory(); |
| 2700 | if (effectsFactory != 0) { |
| 2701 | status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect); |
| 2702 | } |
| 2703 | return status; |
| 2704 | } |
| 2705 | |
| 2706 | bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains( |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 2707 | const sp<AudioFlinger::EffectBase>& effect) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2708 | sp<AudioFlinger> af = mAudioFlinger.promote(); |
| 2709 | if (af == nullptr) { |
| 2710 | return false; |
| 2711 | } |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 2712 | // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe |
| 2713 | return af->updateOrphanEffectChains(effect->asEffectModule()); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer( |
| 2717 | size_t size, sp<EffectBufferHalInterface>* buffer) { |
| 2718 | sp<AudioFlinger> af = mAudioFlinger.promote(); |
| 2719 | LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger"); |
| 2720 | return af->mEffectsFactoryHal->allocateBuffer(size, buffer); |
| 2721 | } |
| 2722 | |
| 2723 | status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal( |
| 2724 | sp<EffectHalInterface> effect) { |
| 2725 | status_t result = NO_INIT; |
| 2726 | sp<ThreadBase> t = mThread.promote(); |
| 2727 | if (t == nullptr) { |
| 2728 | return result; |
| 2729 | } |
| 2730 | sp <StreamHalInterface> st = t->stream(); |
| 2731 | if (st == nullptr) { |
| 2732 | return result; |
| 2733 | } |
| 2734 | result = st->addEffect(effect); |
| 2735 | ALOGE_IF(result != OK, "Error when adding effect: %d", result); |
| 2736 | return result; |
| 2737 | } |
| 2738 | |
| 2739 | status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal( |
| 2740 | sp<EffectHalInterface> effect) { |
| 2741 | status_t result = NO_INIT; |
| 2742 | sp<ThreadBase> t = mThread.promote(); |
| 2743 | if (t == nullptr) { |
| 2744 | return result; |
| 2745 | } |
| 2746 | sp <StreamHalInterface> st = t->stream(); |
| 2747 | if (st == nullptr) { |
| 2748 | return result; |
| 2749 | } |
| 2750 | result = st->removeEffect(effect); |
| 2751 | ALOGE_IF(result != OK, "Error when removing effect: %d", result); |
| 2752 | return result; |
| 2753 | } |
| 2754 | |
| 2755 | audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const { |
| 2756 | sp<ThreadBase> t = mThread.promote(); |
| 2757 | if (t == nullptr) { |
| 2758 | return AUDIO_IO_HANDLE_NONE; |
| 2759 | } |
| 2760 | return t->id(); |
| 2761 | } |
| 2762 | |
| 2763 | bool AudioFlinger::EffectChain::EffectCallback::isOutput() const { |
| 2764 | sp<ThreadBase> t = mThread.promote(); |
| 2765 | if (t == nullptr) { |
| 2766 | return true; |
| 2767 | } |
| 2768 | return t->isOutput(); |
| 2769 | } |
| 2770 | |
| 2771 | bool AudioFlinger::EffectChain::EffectCallback::isOffload() const { |
| 2772 | sp<ThreadBase> t = mThread.promote(); |
| 2773 | if (t == nullptr) { |
| 2774 | return false; |
| 2775 | } |
| 2776 | return t->type() == ThreadBase::OFFLOAD; |
| 2777 | } |
| 2778 | |
| 2779 | bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const { |
| 2780 | sp<ThreadBase> t = mThread.promote(); |
| 2781 | if (t == nullptr) { |
| 2782 | return false; |
| 2783 | } |
| 2784 | return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT; |
| 2785 | } |
| 2786 | |
| 2787 | bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const { |
| 2788 | sp<ThreadBase> t = mThread.promote(); |
| 2789 | if (t == nullptr) { |
| 2790 | return false; |
| 2791 | } |
| 2792 | return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::MMAP; |
| 2793 | } |
| 2794 | |
| 2795 | uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const { |
| 2796 | sp<ThreadBase> t = mThread.promote(); |
| 2797 | if (t == nullptr) { |
| 2798 | return 0; |
| 2799 | } |
| 2800 | return t->sampleRate(); |
| 2801 | } |
| 2802 | |
| 2803 | audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const { |
| 2804 | sp<ThreadBase> t = mThread.promote(); |
| 2805 | if (t == nullptr) { |
| 2806 | return AUDIO_CHANNEL_NONE; |
| 2807 | } |
| 2808 | return t->channelMask(); |
| 2809 | } |
| 2810 | |
| 2811 | uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const { |
| 2812 | sp<ThreadBase> t = mThread.promote(); |
| 2813 | if (t == nullptr) { |
| 2814 | return 0; |
| 2815 | } |
| 2816 | return t->channelCount(); |
| 2817 | } |
| 2818 | |
| 2819 | size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const { |
| 2820 | sp<ThreadBase> t = mThread.promote(); |
| 2821 | if (t == nullptr) { |
| 2822 | return 0; |
| 2823 | } |
| 2824 | return t->frameCount(); |
| 2825 | } |
| 2826 | |
| 2827 | uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const { |
| 2828 | sp<ThreadBase> t = mThread.promote(); |
| 2829 | if (t == nullptr) { |
| 2830 | return 0; |
| 2831 | } |
| 2832 | return t->latency_l(); |
| 2833 | } |
| 2834 | |
| 2835 | void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const { |
| 2836 | sp<ThreadBase> t = mThread.promote(); |
| 2837 | if (t == nullptr) { |
| 2838 | return; |
| 2839 | } |
| 2840 | t->setVolumeForOutput_l(left, right); |
| 2841 | } |
| 2842 | |
| 2843 | void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled( |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 2844 | const sp<EffectBase>& effect, bool enabled, bool threadLocked) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2845 | sp<ThreadBase> t = mThread.promote(); |
| 2846 | if (t == nullptr) { |
| 2847 | return; |
| 2848 | } |
| 2849 | t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked); |
| 2850 | |
| 2851 | sp<EffectChain> c = mChain.promote(); |
| 2852 | if (c == nullptr) { |
| 2853 | return; |
| 2854 | } |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 2855 | // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe |
| 2856 | c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2857 | } |
| 2858 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 2859 | void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2860 | sp<ThreadBase> t = mThread.promote(); |
| 2861 | if (t == nullptr) { |
| 2862 | return; |
| 2863 | } |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 2864 | // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe |
| 2865 | t->onEffectEnable(effect->asEffectModule()); |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2866 | } |
| 2867 | |
Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 2868 | void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) { |
Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 2869 | checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/); |
| 2870 | |
| 2871 | sp<ThreadBase> t = mThread.promote(); |
| 2872 | if (t == nullptr) { |
| 2873 | return; |
| 2874 | } |
| 2875 | t->onEffectDisable(); |
| 2876 | } |
| 2877 | |
| 2878 | bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle, |
| 2879 | bool unpinIfLast) { |
| 2880 | sp<ThreadBase> t = mThread.promote(); |
| 2881 | if (t == nullptr) { |
| 2882 | return false; |
| 2883 | } |
| 2884 | t->disconnectEffectHandle(handle, unpinIfLast); |
| 2885 | return true; |
| 2886 | } |
| 2887 | |
| 2888 | void AudioFlinger::EffectChain::EffectCallback::resetVolume() { |
| 2889 | sp<EffectChain> c = mChain.promote(); |
| 2890 | if (c == nullptr) { |
| 2891 | return; |
| 2892 | } |
| 2893 | c->resetVolume_l(); |
| 2894 | |
| 2895 | } |
| 2896 | |
| 2897 | uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const { |
| 2898 | sp<EffectChain> c = mChain.promote(); |
| 2899 | if (c == nullptr) { |
| 2900 | return PRODUCT_STRATEGY_NONE; |
| 2901 | } |
| 2902 | return c->strategy(); |
| 2903 | } |
| 2904 | |
| 2905 | int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const { |
| 2906 | sp<EffectChain> c = mChain.promote(); |
| 2907 | if (c == nullptr) { |
| 2908 | return 0; |
| 2909 | } |
| 2910 | return c->activeTrackCnt(); |
| 2911 | } |
| 2912 | |
Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 2913 | |
| 2914 | #undef LOG_TAG |
| 2915 | #define LOG_TAG "AudioFlinger::DeviceEffectProxy" |
| 2916 | |
| 2917 | status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle) |
| 2918 | { |
| 2919 | status_t status = EffectBase::setEnabled(enabled, fromHandle); |
| 2920 | Mutex::Autolock _l(mProxyLock); |
| 2921 | if (status == NO_ERROR) { |
| 2922 | for (auto& handle : mEffectHandles) { |
| 2923 | if (enabled) { |
| 2924 | status = handle.second->enable(); |
| 2925 | } else { |
| 2926 | status = handle.second->disable(); |
| 2927 | } |
| 2928 | } |
| 2929 | } |
| 2930 | ALOGV("%s enable %d status %d", __func__, enabled, status); |
| 2931 | return status; |
| 2932 | } |
| 2933 | |
| 2934 | status_t AudioFlinger::DeviceEffectProxy::init( |
| 2935 | const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) { |
| 2936 | //For all audio patches |
| 2937 | //If src or sink device match |
| 2938 | //If the effect is HW accelerated |
| 2939 | // if no corresponding effect module |
| 2940 | // Create EffectModule: mHalEffect |
| 2941 | //Create and attach EffectHandle |
| 2942 | //If the effect is not HW accelerated and the patch sink or src is a mixer port |
| 2943 | // Create Effect on patch input or output thread on session -1 |
| 2944 | //Add EffectHandle to EffectHandle map of Effect Proxy: |
| 2945 | ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress()); |
| 2946 | status_t status = NO_ERROR; |
| 2947 | for (auto &patch : patches) { |
| 2948 | status = onCreatePatch(patch.first, patch.second); |
| 2949 | ALOGV("%s onCreatePatch status %d", __func__, status); |
| 2950 | if (status == BAD_VALUE) { |
| 2951 | return status; |
| 2952 | } |
| 2953 | } |
| 2954 | return status; |
| 2955 | } |
| 2956 | |
| 2957 | status_t AudioFlinger::DeviceEffectProxy::onCreatePatch( |
| 2958 | audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) { |
| 2959 | status_t status = NAME_NOT_FOUND; |
| 2960 | sp<EffectHandle> handle; |
| 2961 | // only consider source[0] as this is the only "true" source of a patch |
| 2962 | status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle); |
| 2963 | ALOGV("%s source checkPort status %d", __func__, status); |
| 2964 | for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) { |
| 2965 | status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle); |
| 2966 | ALOGV("%s sink %d checkPort status %d", __func__, i, status); |
| 2967 | } |
| 2968 | if (status == NO_ERROR || status == ALREADY_EXISTS) { |
| 2969 | Mutex::Autolock _l(mProxyLock); |
| 2970 | mEffectHandles.emplace(patchHandle, handle); |
| 2971 | } |
| 2972 | ALOGW_IF(status == BAD_VALUE, |
| 2973 | "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle); |
| 2974 | |
| 2975 | return status; |
| 2976 | } |
| 2977 | |
| 2978 | status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch, |
| 2979 | const struct audio_port_config *port, sp <EffectHandle> *handle) { |
| 2980 | |
| 2981 | ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d", |
| 2982 | __func__, port->type, port->ext.device.type, |
| 2983 | port->ext.device.address, port->id, patch.isSoftware()); |
| 2984 | if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType |
| 2985 | || port->ext.device.address != mDevice.mAddress) { |
| 2986 | return NAME_NOT_FOUND; |
| 2987 | } |
| 2988 | status_t status = NAME_NOT_FOUND; |
| 2989 | |
| 2990 | if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) { |
| 2991 | Mutex::Autolock _l(mProxyLock); |
| 2992 | mDevicePort = *port; |
| 2993 | mHalEffect = new EffectModule(mMyCallback, |
| 2994 | const_cast<effect_descriptor_t *>(&mDescriptor), |
| 2995 | mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE, |
| 2996 | false /* pinned */, port->id); |
| 2997 | if (audio_is_input_device(mDevice.mType)) { |
| 2998 | mHalEffect->setInputDevice(mDevice); |
| 2999 | } else { |
| 3000 | mHalEffect->setDevices({mDevice}); |
| 3001 | } |
| 3002 | *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/); |
| 3003 | status = (*handle)->initCheck(); |
| 3004 | if (status == OK) { |
| 3005 | status = mHalEffect->addHandle((*handle).get()); |
| 3006 | } else { |
| 3007 | mHalEffect.clear(); |
| 3008 | mDevicePort.id = AUDIO_PORT_HANDLE_NONE; |
| 3009 | } |
| 3010 | } else if (patch.isSoftware() || patch.thread().promote() != nullptr) { |
| 3011 | sp <ThreadBase> thread; |
| 3012 | if (audio_port_config_has_input_direction(port)) { |
| 3013 | if (patch.isSoftware()) { |
| 3014 | thread = patch.mRecord.thread(); |
| 3015 | } else { |
| 3016 | thread = patch.thread().promote(); |
| 3017 | } |
| 3018 | } else { |
| 3019 | if (patch.isSoftware()) { |
| 3020 | thread = patch.mPlayback.thread(); |
| 3021 | } else { |
| 3022 | thread = patch.thread().promote(); |
| 3023 | } |
| 3024 | } |
| 3025 | int enabled; |
| 3026 | *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE, |
| 3027 | const_cast<effect_descriptor_t *>(&mDescriptor), |
| 3028 | &enabled, &status, false); |
| 3029 | ALOGV("%s thread->createEffect_l status %d", __func__, status); |
| 3030 | } else { |
| 3031 | status = BAD_VALUE; |
| 3032 | } |
| 3033 | if (status == NO_ERROR || status == ALREADY_EXISTS) { |
| 3034 | if (isEnabled()) { |
| 3035 | (*handle)->enable(); |
| 3036 | } else { |
| 3037 | (*handle)->disable(); |
| 3038 | } |
| 3039 | } |
| 3040 | return status; |
| 3041 | } |
| 3042 | |
| 3043 | void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) { |
| 3044 | Mutex::Autolock _l(mProxyLock); |
| 3045 | mEffectHandles.erase(patchHandle); |
| 3046 | } |
| 3047 | |
| 3048 | |
| 3049 | size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect) |
| 3050 | { |
| 3051 | Mutex::Autolock _l(mProxyLock); |
| 3052 | if (effect == mHalEffect) { |
| 3053 | mHalEffect.clear(); |
| 3054 | mDevicePort.id = AUDIO_PORT_HANDLE_NONE; |
| 3055 | } |
| 3056 | return mHalEffect == nullptr ? 0 : 1; |
| 3057 | } |
| 3058 | |
| 3059 | status_t AudioFlinger::DeviceEffectProxy::addEffectToHal( |
| 3060 | sp<EffectHalInterface> effect) { |
| 3061 | if (mHalEffect == nullptr) { |
| 3062 | return NO_INIT; |
| 3063 | } |
| 3064 | return mManagerCallback->addEffectToHal( |
| 3065 | mDevicePort.id, mDevicePort.ext.device.hw_module, effect); |
| 3066 | } |
| 3067 | |
| 3068 | status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal( |
| 3069 | sp<EffectHalInterface> effect) { |
| 3070 | if (mHalEffect == nullptr) { |
| 3071 | return NO_INIT; |
| 3072 | } |
| 3073 | return mManagerCallback->removeEffectFromHal( |
| 3074 | mDevicePort.id, mDevicePort.ext.device.hw_module, effect); |
| 3075 | } |
| 3076 | |
| 3077 | bool AudioFlinger::DeviceEffectProxy::isOutput() const { |
| 3078 | if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) { |
| 3079 | return mDevicePort.role == AUDIO_PORT_ROLE_SINK; |
| 3080 | } |
| 3081 | return true; |
| 3082 | } |
| 3083 | |
| 3084 | uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const { |
| 3085 | if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE && |
| 3086 | (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) { |
| 3087 | return mDevicePort.sample_rate; |
| 3088 | } |
| 3089 | return DEFAULT_OUTPUT_SAMPLE_RATE; |
| 3090 | } |
| 3091 | |
| 3092 | audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const { |
| 3093 | if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE && |
| 3094 | (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) { |
| 3095 | return mDevicePort.channel_mask; |
| 3096 | } |
| 3097 | return AUDIO_CHANNEL_OUT_STEREO; |
| 3098 | } |
| 3099 | |
| 3100 | uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const { |
| 3101 | if (isOutput()) { |
| 3102 | return audio_channel_count_from_out_mask(channelMask()); |
| 3103 | } |
| 3104 | return audio_channel_count_from_in_mask(channelMask()); |
| 3105 | } |
| 3106 | |
| 3107 | void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) { |
| 3108 | const Vector<String16> args; |
| 3109 | EffectBase::dump(fd, args); |
| 3110 | |
| 3111 | const bool locked = dumpTryLock(mProxyLock); |
| 3112 | if (!locked) { |
| 3113 | String8 result("DeviceEffectProxy may be deadlocked\n"); |
| 3114 | write(fd, result.string(), result.size()); |
| 3115 | } |
| 3116 | |
| 3117 | String8 outStr; |
| 3118 | if (mHalEffect != nullptr) { |
| 3119 | outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id()); |
| 3120 | } else { |
| 3121 | outStr.appendFormat("%*sNO HAL Effect\n", spaces, ""); |
| 3122 | } |
| 3123 | write(fd, outStr.string(), outStr.size()); |
| 3124 | outStr.clear(); |
| 3125 | |
| 3126 | outStr.appendFormat("%*sSub Effects:\n", spaces, ""); |
| 3127 | write(fd, outStr.string(), outStr.size()); |
| 3128 | outStr.clear(); |
| 3129 | |
| 3130 | for (const auto& iter : mEffectHandles) { |
| 3131 | outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first); |
| 3132 | write(fd, outStr.string(), outStr.size()); |
| 3133 | outStr.clear(); |
| 3134 | sp<EffectBase> effect = iter.second->effect().promote(); |
| 3135 | if (effect != nullptr) { |
| 3136 | effect->dump(fd, args); |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | if (locked) { |
| 3141 | mLock.unlock(); |
| 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | #undef LOG_TAG |
| 3146 | #define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback" |
| 3147 | |
| 3148 | int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() { |
| 3149 | return mManagerCallback->newEffectId(); |
| 3150 | } |
| 3151 | |
| 3152 | |
| 3153 | bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle( |
| 3154 | EffectHandle *handle, bool unpinIfLast) { |
| 3155 | sp<EffectBase> effectBase = handle->effect().promote(); |
| 3156 | if (effectBase == nullptr) { |
| 3157 | return false; |
| 3158 | } |
| 3159 | |
| 3160 | sp<EffectModule> effect = effectBase->asEffectModule(); |
| 3161 | if (effect == nullptr) { |
| 3162 | return false; |
| 3163 | } |
| 3164 | |
| 3165 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 3166 | bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 3167 | if (remove) { |
| 3168 | sp<DeviceEffectProxy> proxy = mProxy.promote(); |
| 3169 | if (proxy != nullptr) { |
| 3170 | proxy->removeEffect(effect); |
| 3171 | } |
| 3172 | if (handle->enabled()) { |
| 3173 | effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/); |
| 3174 | } |
| 3175 | } |
| 3176 | return true; |
| 3177 | } |
| 3178 | |
| 3179 | status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal( |
| 3180 | const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId, |
| 3181 | sp<EffectHalInterface> *effect) { |
| 3182 | return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect); |
| 3183 | } |
| 3184 | |
| 3185 | status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal( |
| 3186 | sp<EffectHalInterface> effect) { |
| 3187 | sp<DeviceEffectProxy> proxy = mProxy.promote(); |
| 3188 | if (proxy == nullptr) { |
| 3189 | return NO_INIT; |
| 3190 | } |
| 3191 | return proxy->addEffectToHal(effect); |
| 3192 | } |
| 3193 | |
| 3194 | status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal( |
| 3195 | sp<EffectHalInterface> effect) { |
| 3196 | sp<DeviceEffectProxy> proxy = mProxy.promote(); |
| 3197 | if (proxy == nullptr) { |
| 3198 | return NO_INIT; |
| 3199 | } |
| 3200 | return proxy->addEffectToHal(effect); |
| 3201 | } |
| 3202 | |
| 3203 | bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const { |
| 3204 | sp<DeviceEffectProxy> proxy = mProxy.promote(); |
| 3205 | if (proxy == nullptr) { |
| 3206 | return true; |
| 3207 | } |
| 3208 | return proxy->isOutput(); |
| 3209 | } |
| 3210 | |
| 3211 | uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const { |
| 3212 | sp<DeviceEffectProxy> proxy = mProxy.promote(); |
| 3213 | if (proxy == nullptr) { |
| 3214 | return DEFAULT_OUTPUT_SAMPLE_RATE; |
| 3215 | } |
| 3216 | return proxy->sampleRate(); |
| 3217 | } |
| 3218 | |
| 3219 | audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const { |
| 3220 | sp<DeviceEffectProxy> proxy = mProxy.promote(); |
| 3221 | if (proxy == nullptr) { |
| 3222 | return AUDIO_CHANNEL_OUT_STEREO; |
| 3223 | } |
| 3224 | return proxy->channelMask(); |
| 3225 | } |
| 3226 | |
| 3227 | uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const { |
| 3228 | sp<DeviceEffectProxy> proxy = mProxy.promote(); |
| 3229 | if (proxy == nullptr) { |
| 3230 | return 2; |
| 3231 | } |
| 3232 | return proxy->channelCount(); |
| 3233 | } |
| 3234 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 3235 | } // namespace android |