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> |
| 27 | #include <system/audio_effects/effect_ns.h> |
| 28 | #include <system/audio_effects/effect_visualizer.h> |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 29 | #include <audio_utils/primitives.h> |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 30 | #include <media/AudioEffect.h> |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 31 | #include <media/audiohal/EffectHalInterface.h> |
| 32 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 33 | |
| 34 | #include "AudioFlinger.h" |
| 35 | #include "ServiceUtilities.h" |
| 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
| 39 | // Note: the following macro is used for extremely verbose logging message. In |
| 40 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 41 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 42 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 43 | // turned on. Do not uncomment the #def below unless you really know what you |
| 44 | // are doing and want to see all of the extremely verbose messages. |
| 45 | //#define VERY_VERY_VERBOSE_LOGGING |
| 46 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 47 | #define ALOGVV ALOGV |
| 48 | #else |
| 49 | #define ALOGVV(a...) do { } while(0) |
| 50 | #endif |
| 51 | |
| 52 | namespace android { |
| 53 | |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | // EffectModule implementation |
| 56 | // ---------------------------------------------------------------------------- |
| 57 | |
| 58 | #undef LOG_TAG |
| 59 | #define LOG_TAG "AudioFlinger::EffectModule" |
| 60 | |
| 61 | AudioFlinger::EffectModule::EffectModule(ThreadBase *thread, |
| 62 | const wp<AudioFlinger::EffectChain>& chain, |
| 63 | effect_descriptor_t *desc, |
| 64 | int id, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 65 | audio_session_t sessionId, |
| 66 | bool pinned) |
| 67 | : mPinned(pinned), |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 68 | mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), |
| 69 | mDescriptor(*desc), |
| 70 | // mConfig is set by configure() and not used before then |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 71 | mStatus(NO_INIT), mState(IDLE), |
| 72 | // mMaxDisableWaitCnt is set by configure() and not used before then |
| 73 | // mDisableWaitCnt is set by process() and updateState() and not used before then |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 74 | mSuspended(false), |
| 75 | mAudioFlinger(thread->mAudioFlinger) |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 76 | #ifdef FLOAT_EFFECT_CHAIN |
| 77 | , mSupportsFloat(false) |
| 78 | #endif |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 79 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 80 | ALOGV("Constructor %p pinned %d", this, pinned); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 81 | int lStatus; |
| 82 | |
| 83 | // create effect engine from effect factory |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 84 | mStatus = -ENODEV; |
| 85 | sp<AudioFlinger> audioFlinger = mAudioFlinger.promote(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 86 | if (audioFlinger != 0) { |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 87 | sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 88 | if (effectsFactory != 0) { |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 89 | mStatus = effectsFactory->createEffect( |
| 90 | &desc->uuid, sessionId, thread->id(), &mEffectInterface); |
| 91 | } |
| 92 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 93 | |
| 94 | if (mStatus != NO_ERROR) { |
| 95 | return; |
| 96 | } |
| 97 | lStatus = init(); |
| 98 | if (lStatus < 0) { |
| 99 | mStatus = lStatus; |
| 100 | goto Error; |
| 101 | } |
| 102 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 103 | setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id()); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 104 | ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get()); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 105 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 106 | return; |
| 107 | Error: |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 108 | mEffectInterface.clear(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 109 | ALOGV("Constructor Error %d", mStatus); |
| 110 | } |
| 111 | |
| 112 | AudioFlinger::EffectModule::~EffectModule() |
| 113 | { |
| 114 | ALOGV("Destructor %p", this); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 115 | if (mEffectInterface != 0) { |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 116 | char uuidStr[64]; |
| 117 | AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr)); |
| 118 | ALOGW("EffectModule %p destructor called with unreleased interface, effect %s", |
| 119 | this, uuidStr); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 120 | release_l(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 121 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 122 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle) |
| 126 | { |
| 127 | status_t status; |
| 128 | |
| 129 | Mutex::Autolock _l(mLock); |
| 130 | int priority = handle->priority(); |
| 131 | size_t size = mHandles.size(); |
| 132 | EffectHandle *controlHandle = NULL; |
| 133 | size_t i; |
| 134 | for (i = 0; i < size; i++) { |
| 135 | EffectHandle *h = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 136 | if (h == NULL || h->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 137 | continue; |
| 138 | } |
| 139 | // first non destroyed handle is considered in control |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 140 | if (controlHandle == NULL) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 141 | controlHandle = h; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 142 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 143 | if (h->priority() <= priority) { |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | // if inserted in first place, move effect control from previous owner to this handle |
| 148 | if (i == 0) { |
| 149 | bool enabled = false; |
| 150 | if (controlHandle != NULL) { |
| 151 | enabled = controlHandle->enabled(); |
| 152 | controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); |
| 153 | } |
| 154 | handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); |
| 155 | status = NO_ERROR; |
| 156 | } else { |
| 157 | status = ALREADY_EXISTS; |
| 158 | } |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 159 | ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 160 | mHandles.insertAt(handle, i); |
| 161 | return status; |
| 162 | } |
| 163 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 164 | ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 165 | { |
| 166 | Mutex::Autolock _l(mLock); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 167 | return removeHandle_l(handle); |
| 168 | } |
| 169 | |
| 170 | ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle) |
| 171 | { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 172 | size_t size = mHandles.size(); |
| 173 | size_t i; |
| 174 | for (i = 0; i < size; i++) { |
| 175 | if (mHandles[i] == handle) { |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | if (i == size) { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 180 | ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle); |
| 181 | return BAD_VALUE; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 182 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 183 | 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] | 184 | |
| 185 | mHandles.removeAt(i); |
| 186 | // if removed from first place, move effect control from this handle to next in line |
| 187 | if (i == 0) { |
| 188 | EffectHandle *h = controlHandle_l(); |
| 189 | if (h != NULL) { |
| 190 | h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Prevent calls to process() and other functions on effect interface from now on. |
| 195 | // The effect engine will be released by the destructor when the last strong reference on |
| 196 | // this object is released which can happen after next process is called. |
| 197 | if (mHandles.size() == 0 && !mPinned) { |
| 198 | mState = DESTROYED; |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 199 | mEffectInterface->close(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return mHandles.size(); |
| 203 | } |
| 204 | |
| 205 | // must be called with EffectModule::mLock held |
| 206 | AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l() |
| 207 | { |
| 208 | // the first valid handle in the list has control over the module |
| 209 | for (size_t i = 0; i < mHandles.size(); i++) { |
| 210 | EffectHandle *h = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 211 | if (h != NULL && !h->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 212 | return h; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return NULL; |
| 217 | } |
| 218 | |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 219 | // unsafe method called when the effect parent thread has been destroyed |
| 220 | ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast) |
| 221 | { |
| 222 | ALOGV("disconnect() %p handle %p", this, handle); |
| 223 | Mutex::Autolock _l(mLock); |
| 224 | ssize_t numHandles = removeHandle_l(handle); |
| 225 | if ((numHandles == 0) && (!mPinned || unpinIfLast)) { |
| 226 | AudioSystem::unregisterEffect(mId); |
| 227 | sp<AudioFlinger> af = mAudioFlinger.promote(); |
| 228 | if (af != 0) { |
| 229 | mLock.unlock(); |
| 230 | af->updateOrphanEffectChains(this); |
| 231 | mLock.lock(); |
| 232 | } |
| 233 | } |
| 234 | return numHandles; |
| 235 | } |
| 236 | |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 237 | bool AudioFlinger::EffectModule::updateState() { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 238 | Mutex::Autolock _l(mLock); |
| 239 | |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 240 | bool started = false; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 241 | switch (mState) { |
| 242 | case RESTART: |
| 243 | reset_l(); |
| 244 | // FALL THROUGH |
| 245 | |
| 246 | case STARTING: |
| 247 | // clear auxiliary effect input buffer for next accumulation |
| 248 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 249 | memset(mConfig.inputCfg.buffer.raw, |
| 250 | 0, |
| 251 | mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); |
| 252 | } |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 253 | if (start_l() == NO_ERROR) { |
| 254 | mState = ACTIVE; |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 255 | started = true; |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 256 | } else { |
| 257 | mState = IDLE; |
| 258 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 259 | break; |
| 260 | case STOPPING: |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 261 | if (stop_l() == NO_ERROR) { |
| 262 | mDisableWaitCnt = mMaxDisableWaitCnt; |
| 263 | } else { |
| 264 | mDisableWaitCnt = 1; // will cause immediate transition to IDLE |
| 265 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 266 | mState = STOPPED; |
| 267 | break; |
| 268 | case STOPPED: |
| 269 | // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the |
| 270 | // turn off sequence. |
| 271 | if (--mDisableWaitCnt == 0) { |
| 272 | reset_l(); |
| 273 | mState = IDLE; |
| 274 | } |
| 275 | break; |
| 276 | default: //IDLE , ACTIVE, DESTROYED |
| 277 | break; |
| 278 | } |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 279 | |
| 280 | return started; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void AudioFlinger::EffectModule::process() |
| 284 | { |
| 285 | Mutex::Autolock _l(mLock); |
| 286 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 287 | if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 291 | // TODO: Implement multichannel effects; here outChannelCount == FCC_2 == 2 |
| 292 | const uint32_t inChannelCount = |
| 293 | audio_channel_count_from_out_mask(mConfig.inputCfg.channels); |
| 294 | const uint32_t outChannelCount = |
| 295 | audio_channel_count_from_out_mask(mConfig.outputCfg.channels); |
| 296 | const bool auxType = |
| 297 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY; |
| 298 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 299 | if (isProcessEnabled()) { |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 300 | int ret; |
| 301 | if (isProcessImplemented()) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 302 | if (auxType) { |
| 303 | // We overwrite the aux input buffer here and clear after processing. |
| 304 | // Note that aux input buffers are format q4_27. |
| 305 | #ifdef FLOAT_EFFECT_CHAIN |
| 306 | if (mSupportsFloat) { |
| 307 | // Do in-place float conversion for auxiliary effect input buffer. |
| 308 | static_assert(sizeof(float) <= sizeof(int32_t), |
| 309 | "in-place conversion requires sizeof(float) <= sizeof(int32_t)"); |
| 310 | |
| 311 | const int32_t * const p32 = mConfig.inputCfg.buffer.s32; |
| 312 | float * const pFloat = mConfig.inputCfg.buffer.f32; |
| 313 | memcpy_to_float_from_q4_27(pFloat, p32, mConfig.inputCfg.buffer.frameCount); |
| 314 | } else { |
Andy Hung | 5effdf6 | 2017-11-27 13:51:40 -0800 | [diff] [blame] | 315 | memcpy_to_i16_from_q4_27(mConfig.inputCfg.buffer.s16, |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 316 | mConfig.inputCfg.buffer.s32, |
Andy Hung | 5effdf6 | 2017-11-27 13:51:40 -0800 | [diff] [blame] | 317 | mConfig.inputCfg.buffer.frameCount); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 318 | } |
| 319 | #else |
Andy Hung | 5effdf6 | 2017-11-27 13:51:40 -0800 | [diff] [blame] | 320 | memcpy_to_i16_from_q4_27(mConfig.inputCfg.buffer.s16, |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 321 | mConfig.inputCfg.buffer.s32, |
Andy Hung | 5effdf6 | 2017-11-27 13:51:40 -0800 | [diff] [blame] | 322 | mConfig.inputCfg.buffer.frameCount); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 323 | #endif |
| 324 | } |
| 325 | #ifdef FLOAT_EFFECT_CHAIN |
| 326 | if (mSupportsFloat) { |
| 327 | ret = mEffectInterface->process(); |
| 328 | } else { |
| 329 | { // convert input to int16_t as effect doesn't support float. |
| 330 | if (!auxType) { |
| 331 | if (mInBuffer16.get() == nullptr) { |
| 332 | ALOGW("%s: mInBuffer16 is null, bypassing", __func__); |
| 333 | goto data_bypass; |
| 334 | } |
| 335 | const float * const pIn = mInBuffer->audioBuffer()->f32; |
| 336 | int16_t * const pIn16 = mInBuffer16->audioBuffer()->s16; |
| 337 | memcpy_to_i16_from_float( |
| 338 | pIn16, pIn, inChannelCount * mConfig.inputCfg.buffer.frameCount); |
| 339 | } |
| 340 | if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
| 341 | if (mOutBuffer16.get() == nullptr) { |
| 342 | ALOGW("%s: mOutBuffer16 is null, bypassing", __func__); |
| 343 | goto data_bypass; |
| 344 | } |
| 345 | int16_t * const pOut16 = mOutBuffer16->audioBuffer()->s16; |
| 346 | const float * const pOut = mOutBuffer->audioBuffer()->f32; |
| 347 | memcpy_to_i16_from_float( |
| 348 | pOut16, |
| 349 | pOut, |
| 350 | outChannelCount * mConfig.outputCfg.buffer.frameCount); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | ret = mEffectInterface->process(); |
| 355 | |
| 356 | { // convert output back to float. |
| 357 | const int16_t * const pOut16 = mOutBuffer16->audioBuffer()->s16; |
| 358 | float * const pOut = mOutBuffer->audioBuffer()->f32; |
| 359 | memcpy_to_float_from_i16( |
| 360 | pOut, pOut16, outChannelCount * mConfig.outputCfg.buffer.frameCount); |
| 361 | } |
| 362 | } |
| 363 | #else |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 364 | ret = mEffectInterface->process(); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 365 | #endif |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 366 | } else { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 367 | #ifdef FLOAT_EFFECT_CHAIN |
| 368 | data_bypass: |
| 369 | #endif |
| 370 | if (!auxType /* aux effects do not require data bypass */ |
| 371 | && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw |
| 372 | && inChannelCount == outChannelCount) { |
| 373 | const size_t sampleCount = std::min( |
| 374 | mConfig.inputCfg.buffer.frameCount, |
| 375 | mConfig.outputCfg.buffer.frameCount) * outChannelCount; |
| 376 | |
| 377 | #ifdef FLOAT_EFFECT_CHAIN |
| 378 | const float * const in = mConfig.inputCfg.buffer.f32; |
| 379 | float * const out = mConfig.outputCfg.buffer.f32; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 380 | |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 381 | if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 382 | accumulate_float(out, in, sampleCount); |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 383 | } else { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 384 | memcpy(mConfig.outputCfg.buffer.f32, mConfig.inputCfg.buffer.f32, |
| 385 | sampleCount * sizeof(*mConfig.outputCfg.buffer.f32)); |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 386 | } |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 387 | |
| 388 | #else |
| 389 | const int16_t * const in = mConfig.inputCfg.buffer.s16; |
| 390 | int16_t * const out = mConfig.outputCfg.buffer.s16; |
| 391 | |
| 392 | if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) { |
| 393 | accumulate_i16(out, in, sampleCount); |
| 394 | } else { |
| 395 | memcpy(mConfig.outputCfg.buffer.s16, mConfig.inputCfg.buffer.s16, |
| 396 | sampleCount * sizeof(*mConfig.outputCfg.buffer.s16)); |
| 397 | } |
| 398 | #endif |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 399 | } |
| 400 | ret = -ENODATA; |
| 401 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 402 | // force transition to IDLE state when engine is ready |
| 403 | if (mState == STOPPED && ret == -ENODATA) { |
| 404 | mDisableWaitCnt = 1; |
| 405 | } |
| 406 | |
| 407 | // clear auxiliary effect input buffer for next accumulation |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 408 | if (auxType) { |
| 409 | // input always q4_27 regardless of FLOAT_EFFECT_CHAIN. |
| 410 | const size_t size = |
| 411 | mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t); |
| 412 | memset(mConfig.inputCfg.buffer.raw, 0, size); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 413 | } |
| 414 | } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 415 | // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 416 | mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 417 | // If an insert effect is idle and input buffer is different from output buffer, |
| 418 | // accumulate input onto output |
| 419 | sp<EffectChain> chain = mChain.promote(); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 420 | if (chain != 0 |
| 421 | && chain->activeTrackCnt() != 0 |
| 422 | && inChannelCount == outChannelCount) { |
| 423 | const size_t sampleCount = std::min( |
| 424 | mConfig.inputCfg.buffer.frameCount, |
| 425 | mConfig.outputCfg.buffer.frameCount) * outChannelCount; |
| 426 | #ifdef FLOAT_EFFECT_CHAIN |
| 427 | const float * const in = mConfig.inputCfg.buffer.f32; |
| 428 | float * const out = mConfig.outputCfg.buffer.f32; |
| 429 | accumulate_float(out, in, sampleCount); |
| 430 | #else |
| 431 | const int16_t * const in = mConfig.inputCfg.buffer.s16; |
| 432 | int16_t * const out = mConfig.outputCfg.buffer.s16; |
| 433 | accumulate_i16(out, in, sampleCount); |
| 434 | #endif |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void AudioFlinger::EffectModule::reset_l() |
| 440 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 441 | if (mStatus != NO_ERROR || mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 442 | return; |
| 443 | } |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 444 | mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | status_t AudioFlinger::EffectModule::configure() |
| 448 | { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 449 | ALOGVV("configure() started"); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 450 | status_t status; |
| 451 | sp<ThreadBase> thread; |
| 452 | uint32_t size; |
| 453 | audio_channel_mask_t channelMask; |
| 454 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 455 | if (mEffectInterface == 0) { |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 456 | status = NO_INIT; |
| 457 | goto exit; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 460 | thread = mThread.promote(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 461 | if (thread == 0) { |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 462 | status = DEAD_OBJECT; |
| 463 | goto exit; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // TODO: handle configuration of effects replacing track process |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 467 | channelMask = thread->channelMask(); |
Ricardo Garcia | d11da70 | 2015-05-28 12:14:12 -0700 | [diff] [blame] | 468 | mConfig.outputCfg.channels = channelMask; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 469 | |
| 470 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 471 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; |
Yuuki Yokoyama | 12ccef7 | 2016-08-23 17:11:03 +0900 | [diff] [blame] | 472 | mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 473 | ALOGV("Overriding auxiliary effect input as MONO and output as STEREO"); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 474 | } else { |
| 475 | mConfig.inputCfg.channels = channelMask; |
Ricardo Garcia | d11da70 | 2015-05-28 12:14:12 -0700 | [diff] [blame] | 476 | // TODO: Update this logic when multichannel effects are implemented. |
| 477 | // For offloaded tracks consider mono output as stereo for proper effect initialization |
| 478 | if (channelMask == AUDIO_CHANNEL_OUT_MONO) { |
| 479 | mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 480 | mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; |
| 481 | ALOGV("Overriding effect input and output as STEREO"); |
| 482 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 483 | } |
Ricardo Garcia | d11da70 | 2015-05-28 12:14:12 -0700 | [diff] [blame] | 484 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 485 | mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT; |
| 486 | mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 487 | mConfig.inputCfg.samplingRate = thread->sampleRate(); |
| 488 | mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; |
| 489 | mConfig.inputCfg.bufferProvider.cookie = NULL; |
| 490 | mConfig.inputCfg.bufferProvider.getBuffer = NULL; |
| 491 | mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; |
| 492 | mConfig.outputCfg.bufferProvider.cookie = NULL; |
| 493 | mConfig.outputCfg.bufferProvider.getBuffer = NULL; |
| 494 | mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; |
| 495 | mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; |
| 496 | // Insert effect: |
| 497 | // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, |
| 498 | // always overwrites output buffer: input buffer == output buffer |
| 499 | // - in other sessions: |
| 500 | // last effect in the chain accumulates in output buffer: input buffer != output buffer |
| 501 | // other effect: overwrites output buffer: input buffer == output buffer |
| 502 | // Auxiliary effect: |
| 503 | // accumulates in output buffer: input buffer != output buffer |
| 504 | // Therefore: accumulate <=> input buffer != output buffer |
| 505 | if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { |
| 506 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; |
| 507 | } else { |
| 508 | mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; |
| 509 | } |
| 510 | mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; |
| 511 | mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; |
| 512 | mConfig.inputCfg.buffer.frameCount = thread->frameCount(); |
| 513 | mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; |
| 514 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 515 | ALOGV("configure() %p thread %p buffer %p framecount %zu", |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 516 | this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); |
| 517 | |
| 518 | status_t cmdStatus; |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 519 | size = sizeof(int); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 520 | status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG, |
| 521 | sizeof(effect_config_t), |
| 522 | &mConfig, |
| 523 | &size, |
| 524 | &cmdStatus); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 525 | if (status == NO_ERROR) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 526 | status = cmdStatus; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 527 | #ifdef FLOAT_EFFECT_CHAIN |
| 528 | mSupportsFloat = true; |
| 529 | #endif |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 530 | } |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 531 | #ifdef FLOAT_EFFECT_CHAIN |
| 532 | else { |
| 533 | ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t."); |
| 534 | mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 535 | mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 536 | status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG, |
| 537 | sizeof(effect_config_t), |
| 538 | &mConfig, |
| 539 | &size, |
| 540 | &cmdStatus); |
| 541 | if (status == NO_ERROR) { |
| 542 | status = cmdStatus; |
| 543 | mSupportsFloat = false; |
| 544 | ALOGVV("config worked with 16 bit"); |
| 545 | } else { |
| 546 | 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] | 547 | } |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 548 | } |
| 549 | #endif |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 550 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 551 | if (status == NO_ERROR) { |
| 552 | // Establish Buffer strategy |
| 553 | setInBuffer(mInBuffer); |
| 554 | setOutBuffer(mOutBuffer); |
| 555 | |
| 556 | // Update visualizer latency |
| 557 | if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) { |
| 558 | uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; |
| 559 | effect_param_t *p = (effect_param_t *)buf32; |
| 560 | |
| 561 | p->psize = sizeof(uint32_t); |
| 562 | p->vsize = sizeof(uint32_t); |
| 563 | size = sizeof(int); |
| 564 | *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY; |
| 565 | |
| 566 | uint32_t latency = 0; |
| 567 | PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId); |
| 568 | if (pbt != NULL) { |
| 569 | latency = pbt->latency_l(); |
| 570 | } |
| 571 | |
| 572 | *((int32_t *)p->data + 1)= latency; |
| 573 | mEffectInterface->command(EFFECT_CMD_SET_PARAM, |
| 574 | sizeof(effect_param_t) + 8, |
| 575 | &buf32, |
| 576 | &size, |
| 577 | &cmdStatus); |
| 578 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / |
| 582 | (1000 * mConfig.outputCfg.buffer.frameCount); |
| 583 | |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 584 | exit: |
| 585 | mStatus = status; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 586 | ALOGVV("configure ended"); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 587 | return status; |
| 588 | } |
| 589 | |
| 590 | status_t AudioFlinger::EffectModule::init() |
| 591 | { |
| 592 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 593 | if (mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 594 | return NO_INIT; |
| 595 | } |
| 596 | status_t cmdStatus; |
| 597 | uint32_t size = sizeof(status_t); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 598 | status_t status = mEffectInterface->command(EFFECT_CMD_INIT, |
| 599 | 0, |
| 600 | NULL, |
| 601 | &size, |
| 602 | &cmdStatus); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 603 | if (status == 0) { |
| 604 | status = cmdStatus; |
| 605 | } |
| 606 | return status; |
| 607 | } |
| 608 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 609 | void AudioFlinger::EffectModule::addEffectToHal_l() |
| 610 | { |
| 611 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 612 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
| 613 | sp<ThreadBase> thread = mThread.promote(); |
| 614 | if (thread != 0) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 615 | sp<StreamHalInterface> stream = thread->stream(); |
| 616 | if (stream != 0) { |
| 617 | status_t result = stream->addEffect(mEffectInterface); |
| 618 | ALOGE_IF(result != OK, "Error when adding effect: %d", result); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 624 | // start() must be called with PlaybackThread::mLock or EffectChain::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 625 | status_t AudioFlinger::EffectModule::start() |
| 626 | { |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 627 | sp<EffectChain> chain; |
| 628 | status_t status; |
| 629 | { |
| 630 | Mutex::Autolock _l(mLock); |
| 631 | status = start_l(); |
| 632 | if (status == NO_ERROR) { |
| 633 | chain = mChain.promote(); |
| 634 | } |
| 635 | } |
| 636 | if (chain != 0) { |
| 637 | chain->resetVolume_l(); |
| 638 | } |
| 639 | return status; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | status_t AudioFlinger::EffectModule::start_l() |
| 643 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 644 | if (mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 645 | return NO_INIT; |
| 646 | } |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 647 | if (mStatus != NO_ERROR) { |
| 648 | return mStatus; |
| 649 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 650 | status_t cmdStatus; |
| 651 | uint32_t size = sizeof(status_t); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 652 | status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE, |
| 653 | 0, |
| 654 | NULL, |
| 655 | &size, |
| 656 | &cmdStatus); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 657 | if (status == 0) { |
| 658 | status = cmdStatus; |
| 659 | } |
Eric Laurent | cb4b6e9 | 2014-10-01 14:26:10 -0700 | [diff] [blame] | 660 | if (status == 0) { |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 661 | addEffectToHal_l(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 662 | } |
| 663 | return status; |
| 664 | } |
| 665 | |
| 666 | status_t AudioFlinger::EffectModule::stop() |
| 667 | { |
| 668 | Mutex::Autolock _l(mLock); |
| 669 | return stop_l(); |
| 670 | } |
| 671 | |
| 672 | status_t AudioFlinger::EffectModule::stop_l() |
| 673 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 674 | if (mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 675 | return NO_INIT; |
| 676 | } |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 677 | if (mStatus != NO_ERROR) { |
| 678 | return mStatus; |
| 679 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 680 | status_t cmdStatus = NO_ERROR; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 681 | uint32_t size = sizeof(status_t); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 682 | status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE, |
| 683 | 0, |
| 684 | NULL, |
| 685 | &size, |
| 686 | &cmdStatus); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 687 | if (status == NO_ERROR) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 688 | status = cmdStatus; |
| 689 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 690 | if (status == NO_ERROR) { |
| 691 | status = remove_effect_from_hal_l(); |
| 692 | } |
| 693 | return status; |
| 694 | } |
| 695 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 696 | // must be called with EffectChain::mLock held |
| 697 | void AudioFlinger::EffectModule::release_l() |
| 698 | { |
| 699 | if (mEffectInterface != 0) { |
| 700 | remove_effect_from_hal_l(); |
| 701 | // release effect engine |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 702 | mEffectInterface->close(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 703 | mEffectInterface.clear(); |
| 704 | } |
| 705 | } |
| 706 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 707 | status_t AudioFlinger::EffectModule::remove_effect_from_hal_l() |
| 708 | { |
| 709 | if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || |
| 710 | (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 711 | sp<ThreadBase> thread = mThread.promote(); |
| 712 | if (thread != 0) { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 713 | sp<StreamHalInterface> stream = thread->stream(); |
| 714 | if (stream != 0) { |
| 715 | status_t result = stream->removeEffect(mEffectInterface); |
| 716 | ALOGE_IF(result != OK, "Error when removing effect: %d", result); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 720 | return NO_ERROR; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Andy Hung | e4a1d91 | 2016-08-17 14:11:13 -0700 | [diff] [blame] | 723 | // round up delta valid if value and divisor are positive. |
| 724 | template <typename T> |
| 725 | static T roundUpDelta(const T &value, const T &divisor) { |
| 726 | T remainder = value % divisor; |
| 727 | return remainder == 0 ? 0 : divisor - remainder; |
| 728 | } |
| 729 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 730 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, |
| 731 | uint32_t cmdSize, |
| 732 | void *pCmdData, |
| 733 | uint32_t *replySize, |
| 734 | void *pReplyData) |
| 735 | { |
| 736 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 737 | ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 738 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 739 | if (mState == DESTROYED || mEffectInterface == 0) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 740 | return NO_INIT; |
| 741 | } |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 742 | if (mStatus != NO_ERROR) { |
| 743 | return mStatus; |
| 744 | } |
Andy Hung | 110bc95 | 2016-06-20 15:22:52 -0700 | [diff] [blame] | 745 | if (cmdCode == EFFECT_CMD_GET_PARAM && |
Andy Hung | 6660f12 | 2016-11-04 19:40:53 -0700 | [diff] [blame] | 746 | (sizeof(effect_param_t) > cmdSize || |
| 747 | ((effect_param_t *)pCmdData)->psize > cmdSize |
| 748 | - sizeof(effect_param_t))) { |
| 749 | android_errorWriteLog(0x534e4554, "32438594"); |
Andy Hung | b345664 | 2016-11-28 13:50:21 -0800 | [diff] [blame] | 750 | android_errorWriteLog(0x534e4554, "33003822"); |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | if (cmdCode == EFFECT_CMD_GET_PARAM && |
| 754 | (*replySize < sizeof(effect_param_t) || |
| 755 | ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) { |
| 756 | android_errorWriteLog(0x534e4554, "29251553"); |
Andy Hung | 6660f12 | 2016-11-04 19:40:53 -0700 | [diff] [blame] | 757 | return -EINVAL; |
| 758 | } |
rago | e275907 | 2016-11-22 18:02:48 -0800 | [diff] [blame] | 759 | if (cmdCode == EFFECT_CMD_GET_PARAM && |
| 760 | (sizeof(effect_param_t) > *replySize |
| 761 | || ((effect_param_t *)pCmdData)->psize > *replySize |
| 762 | - sizeof(effect_param_t) |
| 763 | || ((effect_param_t *)pCmdData)->vsize > *replySize |
| 764 | - sizeof(effect_param_t) |
| 765 | - ((effect_param_t *)pCmdData)->psize |
| 766 | || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) > |
| 767 | *replySize |
| 768 | - sizeof(effect_param_t) |
| 769 | - ((effect_param_t *)pCmdData)->psize |
| 770 | - ((effect_param_t *)pCmdData)->vsize)) { |
| 771 | ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent"); |
| 772 | android_errorWriteLog(0x534e4554, "32705438"); |
| 773 | return -EINVAL; |
| 774 | } |
Andy Hung | e4a1d91 | 2016-08-17 14:11:13 -0700 | [diff] [blame] | 775 | if ((cmdCode == EFFECT_CMD_SET_PARAM |
| 776 | || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used |
| 777 | (sizeof(effect_param_t) > cmdSize |
| 778 | || ((effect_param_t *)pCmdData)->psize > cmdSize |
| 779 | - sizeof(effect_param_t) |
| 780 | || ((effect_param_t *)pCmdData)->vsize > cmdSize |
| 781 | - sizeof(effect_param_t) |
| 782 | - ((effect_param_t *)pCmdData)->psize |
| 783 | || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) > |
| 784 | cmdSize |
| 785 | - sizeof(effect_param_t) |
| 786 | - ((effect_param_t *)pCmdData)->psize |
| 787 | - ((effect_param_t *)pCmdData)->vsize)) { |
| 788 | android_errorWriteLog(0x534e4554, "30204301"); |
| 789 | return -EINVAL; |
| 790 | } |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 791 | status_t status = mEffectInterface->command(cmdCode, |
| 792 | cmdSize, |
| 793 | pCmdData, |
| 794 | replySize, |
| 795 | pReplyData); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 796 | if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { |
| 797 | uint32_t size = (replySize == NULL) ? 0 : *replySize; |
| 798 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 799 | EffectHandle *h = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 800 | if (h != NULL && !h->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 801 | h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | return status; |
| 806 | } |
| 807 | |
| 808 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) |
| 809 | { |
| 810 | Mutex::Autolock _l(mLock); |
| 811 | return setEnabled_l(enabled); |
| 812 | } |
| 813 | |
| 814 | // must be called with EffectModule::mLock held |
| 815 | status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled) |
| 816 | { |
| 817 | |
| 818 | ALOGV("setEnabled %p enabled %d", this, enabled); |
| 819 | |
| 820 | if (enabled != isEnabled()) { |
| 821 | status_t status = AudioSystem::setEffectEnabled(mId, enabled); |
| 822 | if (enabled && status != NO_ERROR) { |
| 823 | return status; |
| 824 | } |
| 825 | |
| 826 | switch (mState) { |
| 827 | // going from disabled to enabled |
| 828 | case IDLE: |
| 829 | mState = STARTING; |
| 830 | break; |
| 831 | case STOPPED: |
| 832 | mState = RESTART; |
| 833 | break; |
| 834 | case STOPPING: |
| 835 | mState = ACTIVE; |
| 836 | break; |
| 837 | |
| 838 | // going from enabled to disabled |
| 839 | case RESTART: |
| 840 | mState = STOPPED; |
| 841 | break; |
| 842 | case STARTING: |
| 843 | mState = IDLE; |
| 844 | break; |
| 845 | case ACTIVE: |
| 846 | mState = STOPPING; |
| 847 | break; |
| 848 | case DESTROYED: |
| 849 | return NO_ERROR; // simply ignore as we are being destroyed |
| 850 | } |
| 851 | for (size_t i = 1; i < mHandles.size(); i++) { |
| 852 | EffectHandle *h = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 853 | if (h != NULL && !h->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 854 | h->setEnabled(enabled); |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | return NO_ERROR; |
| 859 | } |
| 860 | |
| 861 | bool AudioFlinger::EffectModule::isEnabled() const |
| 862 | { |
| 863 | switch (mState) { |
| 864 | case RESTART: |
| 865 | case STARTING: |
| 866 | case ACTIVE: |
| 867 | return true; |
| 868 | case IDLE: |
| 869 | case STOPPING: |
| 870 | case STOPPED: |
| 871 | case DESTROYED: |
| 872 | default: |
| 873 | return false; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | bool AudioFlinger::EffectModule::isProcessEnabled() const |
| 878 | { |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 879 | if (mStatus != NO_ERROR) { |
| 880 | return false; |
| 881 | } |
| 882 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 883 | switch (mState) { |
| 884 | case RESTART: |
| 885 | case ACTIVE: |
| 886 | case STOPPING: |
| 887 | case STOPPED: |
| 888 | return true; |
| 889 | case IDLE: |
| 890 | case STARTING: |
| 891 | case DESTROYED: |
| 892 | default: |
| 893 | return false; |
| 894 | } |
| 895 | } |
| 896 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 897 | void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 898 | ALOGVV("setInBuffer %p",(&buffer)); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 899 | if (buffer != 0) { |
| 900 | mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw; |
| 901 | buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount); |
| 902 | } else { |
| 903 | mConfig.inputCfg.buffer.raw = NULL; |
| 904 | } |
| 905 | mInBuffer = buffer; |
Andy Hung | c15aaee | 2017-11-27 17:02:40 -0800 | [diff] [blame] | 906 | mEffectInterface->setInBuffer(buffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 907 | |
| 908 | #ifdef FLOAT_EFFECT_CHAIN |
| 909 | // aux effects do in place conversion to float - we don't allocate mInBuffer16 for them. |
| 910 | // Theoretically insert effects can also do in-place conversions (destroying |
| 911 | // the original buffer) when the output buffer is identical to the input buffer, |
| 912 | // but we don't optimize for it here. |
| 913 | const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY; |
| 914 | if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) { |
| 915 | // we need to translate - create hidl shared buffer and intercept |
| 916 | const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount; |
| 917 | const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels); |
| 918 | const size_t size = inChannels * inFrameCount * sizeof(int16_t); |
| 919 | |
| 920 | ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu", |
| 921 | __func__, inChannels, inFrameCount, size); |
| 922 | |
| 923 | if (size > 0 && (mInBuffer16.get() == nullptr || size > mInBuffer16->getSize())) { |
| 924 | mInBuffer16.clear(); |
| 925 | ALOGV("%s: allocating mInBuffer16 %zu", __func__, size); |
| 926 | (void)EffectBufferHalInterface::allocate(size, &mInBuffer16); |
| 927 | } |
| 928 | if (mInBuffer16.get() != nullptr) { |
| 929 | // FIXME: confirm buffer has enough size. |
| 930 | mInBuffer16->setFrameCount(inFrameCount); |
| 931 | mEffectInterface->setInBuffer(mInBuffer16); |
| 932 | } else if (size > 0) { |
| 933 | ALOGE("%s cannot create mInBuffer16", __func__); |
| 934 | } |
| 935 | } |
| 936 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) { |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 940 | ALOGVV("setOutBuffer %p",(&buffer)); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 941 | if (buffer != 0) { |
| 942 | mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw; |
| 943 | buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount); |
| 944 | } else { |
| 945 | mConfig.outputCfg.buffer.raw = NULL; |
| 946 | } |
| 947 | mOutBuffer = buffer; |
Andy Hung | c15aaee | 2017-11-27 17:02:40 -0800 | [diff] [blame] | 948 | mEffectInterface->setOutBuffer(buffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 949 | |
| 950 | #ifdef FLOAT_EFFECT_CHAIN |
| 951 | // Note: Any effect that does not accumulate does not need mOutBuffer16 and |
| 952 | // can do in-place conversion from int16_t to float. We don't optimize here. |
| 953 | if (!mSupportsFloat && mOutBuffer.get() != nullptr) { |
| 954 | const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount; |
| 955 | const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels); |
| 956 | const size_t size = outChannels * outFrameCount * sizeof(int16_t); |
| 957 | |
| 958 | ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu", |
| 959 | __func__, outChannels, outFrameCount, size); |
| 960 | |
| 961 | if (size > 0 && (mOutBuffer16.get() == nullptr || size > mOutBuffer16->getSize())) { |
| 962 | mOutBuffer16.clear(); |
| 963 | ALOGV("%s: allocating mOutBuffer16 %zu", __func__, size); |
| 964 | (void)EffectBufferHalInterface::allocate(size, &mOutBuffer16); |
| 965 | } |
| 966 | if (mOutBuffer16.get() != nullptr) { |
| 967 | mOutBuffer16->setFrameCount(outFrameCount); |
| 968 | mEffectInterface->setOutBuffer(mOutBuffer16); |
| 969 | } else if (size > 0) { |
| 970 | ALOGE("%s cannot create mOutBuffer16", __func__); |
| 971 | } |
| 972 | } |
| 973 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 976 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) |
| 977 | { |
| 978 | Mutex::Autolock _l(mLock); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 979 | if (mStatus != NO_ERROR) { |
| 980 | return mStatus; |
| 981 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 982 | status_t status = NO_ERROR; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 983 | // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume |
| 984 | // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) |
| 985 | if (isProcessEnabled() && |
| 986 | ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || |
| 987 | (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 988 | uint32_t volume[2]; |
| 989 | uint32_t *pVolume = NULL; |
| 990 | uint32_t size = sizeof(volume); |
| 991 | volume[0] = *left; |
| 992 | volume[1] = *right; |
| 993 | if (controller) { |
| 994 | pVolume = volume; |
| 995 | } |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 996 | status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME, |
| 997 | size, |
| 998 | volume, |
| 999 | &size, |
| 1000 | pVolume); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1001 | if (controller && status == NO_ERROR && size == sizeof(volume)) { |
| 1002 | *left = volume[0]; |
| 1003 | *right = volume[1]; |
| 1004 | } |
| 1005 | } |
| 1006 | return status; |
| 1007 | } |
| 1008 | |
| 1009 | status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device) |
| 1010 | { |
| 1011 | if (device == AUDIO_DEVICE_NONE) { |
| 1012 | return NO_ERROR; |
| 1013 | } |
| 1014 | |
| 1015 | Mutex::Autolock _l(mLock); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1016 | if (mStatus != NO_ERROR) { |
| 1017 | return mStatus; |
| 1018 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1019 | status_t status = NO_ERROR; |
Eric Laurent | 7e1139c | 2013-06-06 18:29:01 -0700 | [diff] [blame] | 1020 | if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1021 | status_t cmdStatus; |
| 1022 | uint32_t size = sizeof(status_t); |
| 1023 | uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE : |
| 1024 | EFFECT_CMD_SET_INPUT_DEVICE; |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1025 | status = mEffectInterface->command(cmd, |
| 1026 | sizeof(uint32_t), |
| 1027 | &device, |
| 1028 | &size, |
| 1029 | &cmdStatus); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1030 | } |
| 1031 | return status; |
| 1032 | } |
| 1033 | |
| 1034 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) |
| 1035 | { |
| 1036 | Mutex::Autolock _l(mLock); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1037 | if (mStatus != NO_ERROR) { |
| 1038 | return mStatus; |
| 1039 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1040 | status_t status = NO_ERROR; |
| 1041 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { |
| 1042 | status_t cmdStatus; |
| 1043 | uint32_t size = sizeof(status_t); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1044 | status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE, |
| 1045 | sizeof(audio_mode_t), |
| 1046 | &mode, |
| 1047 | &size, |
| 1048 | &cmdStatus); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1049 | if (status == NO_ERROR) { |
| 1050 | status = cmdStatus; |
| 1051 | } |
| 1052 | } |
| 1053 | return status; |
| 1054 | } |
| 1055 | |
| 1056 | status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source) |
| 1057 | { |
| 1058 | Mutex::Autolock _l(mLock); |
Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 1059 | if (mStatus != NO_ERROR) { |
| 1060 | return mStatus; |
| 1061 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1062 | status_t status = NO_ERROR; |
| 1063 | if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) { |
| 1064 | uint32_t size = 0; |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1065 | status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE, |
| 1066 | sizeof(audio_source_t), |
| 1067 | &source, |
| 1068 | &size, |
| 1069 | NULL); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1070 | } |
| 1071 | return status; |
| 1072 | } |
| 1073 | |
| 1074 | void AudioFlinger::EffectModule::setSuspended(bool suspended) |
| 1075 | { |
| 1076 | Mutex::Autolock _l(mLock); |
| 1077 | mSuspended = suspended; |
| 1078 | } |
| 1079 | |
| 1080 | bool AudioFlinger::EffectModule::suspended() const |
| 1081 | { |
| 1082 | Mutex::Autolock _l(mLock); |
| 1083 | return mSuspended; |
| 1084 | } |
| 1085 | |
| 1086 | bool AudioFlinger::EffectModule::purgeHandles() |
| 1087 | { |
| 1088 | bool enabled = false; |
| 1089 | Mutex::Autolock _l(mLock); |
| 1090 | for (size_t i = 0; i < mHandles.size(); i++) { |
| 1091 | EffectHandle *handle = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1092 | if (handle != NULL && !handle->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1093 | if (handle->hasControl()) { |
| 1094 | enabled = handle->enabled(); |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | return enabled; |
| 1099 | } |
| 1100 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1101 | status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io) |
| 1102 | { |
| 1103 | Mutex::Autolock _l(mLock); |
| 1104 | if (mStatus != NO_ERROR) { |
| 1105 | return mStatus; |
| 1106 | } |
| 1107 | status_t status = NO_ERROR; |
| 1108 | if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) { |
| 1109 | status_t cmdStatus; |
| 1110 | uint32_t size = sizeof(status_t); |
| 1111 | effect_offload_param_t cmd; |
| 1112 | |
| 1113 | cmd.isOffload = offloaded; |
| 1114 | cmd.ioHandle = io; |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1115 | status = mEffectInterface->command(EFFECT_CMD_OFFLOAD, |
| 1116 | sizeof(effect_offload_param_t), |
| 1117 | &cmd, |
| 1118 | &size, |
| 1119 | &cmdStatus); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1120 | if (status == NO_ERROR) { |
| 1121 | status = cmdStatus; |
| 1122 | } |
| 1123 | mOffloaded = (status == NO_ERROR) ? offloaded : false; |
| 1124 | } else { |
| 1125 | if (offloaded) { |
| 1126 | status = INVALID_OPERATION; |
| 1127 | } |
| 1128 | mOffloaded = false; |
| 1129 | } |
| 1130 | ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status); |
| 1131 | return status; |
| 1132 | } |
| 1133 | |
| 1134 | bool AudioFlinger::EffectModule::isOffloaded() const |
| 1135 | { |
| 1136 | Mutex::Autolock _l(mLock); |
| 1137 | return mOffloaded; |
| 1138 | } |
| 1139 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1140 | String8 effectFlagsToString(uint32_t flags) { |
| 1141 | String8 s; |
| 1142 | |
| 1143 | s.append("conn. mode: "); |
| 1144 | switch (flags & EFFECT_FLAG_TYPE_MASK) { |
| 1145 | case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break; |
| 1146 | case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break; |
| 1147 | case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break; |
| 1148 | case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break; |
| 1149 | case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break; |
| 1150 | default: s.append("unknown/reserved"); break; |
| 1151 | } |
| 1152 | s.append(", "); |
| 1153 | |
| 1154 | s.append("insert pref: "); |
| 1155 | switch (flags & EFFECT_FLAG_INSERT_MASK) { |
| 1156 | case EFFECT_FLAG_INSERT_ANY: s.append("any"); break; |
| 1157 | case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break; |
| 1158 | case EFFECT_FLAG_INSERT_LAST: s.append("last"); break; |
| 1159 | case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break; |
| 1160 | default: s.append("unknown/reserved"); break; |
| 1161 | } |
| 1162 | s.append(", "); |
| 1163 | |
| 1164 | s.append("volume mgmt: "); |
| 1165 | switch (flags & EFFECT_FLAG_VOLUME_MASK) { |
| 1166 | case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break; |
| 1167 | case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break; |
| 1168 | case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break; |
| 1169 | default: s.append("unknown/reserved"); break; |
| 1170 | } |
| 1171 | s.append(", "); |
| 1172 | |
| 1173 | uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK; |
| 1174 | if (devind) { |
| 1175 | s.append("device indication: "); |
| 1176 | switch (devind) { |
| 1177 | case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break; |
| 1178 | default: s.append("unknown/reserved"); break; |
| 1179 | } |
| 1180 | s.append(", "); |
| 1181 | } |
| 1182 | |
| 1183 | s.append("input mode: "); |
| 1184 | switch (flags & EFFECT_FLAG_INPUT_MASK) { |
| 1185 | case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break; |
| 1186 | case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break; |
| 1187 | case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break; |
| 1188 | default: s.append("not set"); break; |
| 1189 | } |
| 1190 | s.append(", "); |
| 1191 | |
| 1192 | s.append("output mode: "); |
| 1193 | switch (flags & EFFECT_FLAG_OUTPUT_MASK) { |
| 1194 | case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break; |
| 1195 | case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break; |
| 1196 | case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break; |
| 1197 | default: s.append("not set"); break; |
| 1198 | } |
| 1199 | s.append(", "); |
| 1200 | |
| 1201 | uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK; |
| 1202 | if (accel) { |
| 1203 | s.append("hardware acceleration: "); |
| 1204 | switch (accel) { |
| 1205 | case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break; |
| 1206 | case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break; |
| 1207 | default: s.append("unknown/reserved"); break; |
| 1208 | } |
| 1209 | s.append(", "); |
| 1210 | } |
| 1211 | |
| 1212 | uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK; |
| 1213 | if (modeind) { |
| 1214 | s.append("mode indication: "); |
| 1215 | switch (modeind) { |
| 1216 | case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break; |
| 1217 | default: s.append("unknown/reserved"); break; |
| 1218 | } |
| 1219 | s.append(", "); |
| 1220 | } |
| 1221 | |
| 1222 | uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK; |
| 1223 | if (srcind) { |
| 1224 | s.append("source indication: "); |
| 1225 | switch (srcind) { |
| 1226 | case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break; |
| 1227 | default: s.append("unknown/reserved"); break; |
| 1228 | } |
| 1229 | s.append(", "); |
| 1230 | } |
| 1231 | |
| 1232 | if (flags & EFFECT_FLAG_OFFLOAD_MASK) { |
| 1233 | s.append("offloadable, "); |
| 1234 | } |
| 1235 | |
| 1236 | int len = s.length(); |
| 1237 | if (s.length() > 2) { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 1238 | (void) s.lockBuffer(len); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1239 | s.unlockBuffer(len - 2); |
| 1240 | } |
| 1241 | return s; |
| 1242 | } |
| 1243 | |
| 1244 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1245 | void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1246 | { |
| 1247 | const size_t SIZE = 256; |
| 1248 | char buffer[SIZE]; |
| 1249 | String8 result; |
| 1250 | |
| 1251 | snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); |
| 1252 | result.append(buffer); |
| 1253 | |
| 1254 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 1255 | // failed to lock - AudioFlinger is probably deadlocked |
| 1256 | if (!locked) { |
| 1257 | result.append("\t\tCould not lock Fx mutex:\n"); |
| 1258 | } |
| 1259 | |
| 1260 | result.append("\t\tSession Status State Engine:\n"); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1261 | snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n", |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 1262 | mSessionId, mStatus, mState, mEffectInterface.get()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1263 | result.append(buffer); |
| 1264 | |
| 1265 | result.append("\t\tDescriptor:\n"); |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 1266 | char uuidStr[64]; |
| 1267 | AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr)); |
| 1268 | snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1269 | result.append(buffer); |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 1270 | AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr)); |
| 1271 | snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1272 | result.append(buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1273 | snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n", |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1274 | mDescriptor.apiVersion, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1275 | mDescriptor.flags, |
| 1276 | effectFlagsToString(mDescriptor.flags).string()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1277 | result.append(buffer); |
| 1278 | snprintf(buffer, SIZE, "\t\t- name: %s\n", |
| 1279 | mDescriptor.name); |
| 1280 | result.append(buffer); |
| 1281 | snprintf(buffer, SIZE, "\t\t- implementor: %s\n", |
| 1282 | mDescriptor.implementor); |
| 1283 | result.append(buffer); |
| 1284 | |
| 1285 | result.append("\t\t- Input configuration:\n"); |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1286 | result.append("\t\t\tFrames Smp rate Channels Format Buffer\n"); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 1287 | snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n", |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1288 | mConfig.inputCfg.buffer.frameCount, |
| 1289 | mConfig.inputCfg.samplingRate, |
| 1290 | mConfig.inputCfg.channels, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1291 | mConfig.inputCfg.format, |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 1292 | formatToString((audio_format_t)mConfig.inputCfg.format).c_str(), |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1293 | mConfig.inputCfg.buffer.raw); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1294 | result.append(buffer); |
| 1295 | |
| 1296 | result.append("\t\t- Output configuration:\n"); |
| 1297 | result.append("\t\t\tBuffer Frames Smp rate Channels Format\n"); |
Narayan Kamath | 1d6fa7a | 2014-02-11 13:47:53 +0000 | [diff] [blame] | 1298 | snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n", |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1299 | mConfig.outputCfg.buffer.raw, |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1300 | mConfig.outputCfg.buffer.frameCount, |
| 1301 | mConfig.outputCfg.samplingRate, |
| 1302 | mConfig.outputCfg.channels, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1303 | mConfig.outputCfg.format, |
Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 1304 | formatToString((audio_format_t)mConfig.outputCfg.format).c_str()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1305 | result.append(buffer); |
| 1306 | |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1307 | #ifdef FLOAT_EFFECT_CHAIN |
| 1308 | if (!mSupportsFloat) { |
| 1309 | int16_t* pIn16 = mInBuffer16 != 0 ? mInBuffer16->audioBuffer()->s16 : NULL; |
| 1310 | int16_t* pOut16 = mOutBuffer16 != 0 ? mOutBuffer16->audioBuffer()->s16 : NULL; |
| 1311 | |
| 1312 | result.append("\t\t- Float and int16 buffers\n"); |
| 1313 | result.append("\t\t\tIn_float In_int16 Out_float Out_int16\n"); |
| 1314 | snprintf(buffer, SIZE,"\t\t\t%p %p %p %p\n", |
| 1315 | mConfig.inputCfg.buffer.raw, |
| 1316 | pIn16, |
| 1317 | pOut16, |
| 1318 | mConfig.outputCfg.buffer.raw); |
| 1319 | result.append(buffer); |
| 1320 | } |
| 1321 | #endif |
| 1322 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1323 | snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1324 | result.append(buffer); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1325 | result.append("\t\t\t Pid Priority Ctrl Locked client server\n"); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1326 | for (size_t i = 0; i < mHandles.size(); ++i) { |
| 1327 | EffectHandle *handle = mHandles[i]; |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1328 | if (handle != NULL && !handle->disconnected()) { |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 1329 | handle->dumpToBuffer(buffer, SIZE); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1330 | result.append(buffer); |
| 1331 | } |
| 1332 | } |
| 1333 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1334 | write(fd, result.string(), result.length()); |
| 1335 | |
| 1336 | if (locked) { |
| 1337 | mLock.unlock(); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | // ---------------------------------------------------------------------------- |
| 1342 | // EffectHandle implementation |
| 1343 | // ---------------------------------------------------------------------------- |
| 1344 | |
| 1345 | #undef LOG_TAG |
| 1346 | #define LOG_TAG "AudioFlinger::EffectHandle" |
| 1347 | |
| 1348 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, |
| 1349 | const sp<AudioFlinger::Client>& client, |
| 1350 | const sp<IEffectClient>& effectClient, |
| 1351 | int32_t priority) |
| 1352 | : BnEffect(), |
| 1353 | mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1354 | mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1355 | { |
| 1356 | ALOGV("constructor %p", this); |
| 1357 | |
| 1358 | if (client == 0) { |
| 1359 | return; |
| 1360 | } |
| 1361 | int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); |
| 1362 | mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1363 | if (mCblkMemory == 0 || |
| 1364 | (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1365 | ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE + |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1366 | sizeof(effect_param_cblk_t)); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1367 | mCblkMemory.clear(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1368 | return; |
| 1369 | } |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1370 | new(mCblk) effect_param_cblk_t(); |
| 1371 | mBuffer = (uint8_t *)mCblk + bufOffset; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | AudioFlinger::EffectHandle::~EffectHandle() |
| 1375 | { |
| 1376 | ALOGV("Destructor %p", this); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1377 | disconnect(false); |
| 1378 | } |
| 1379 | |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 1380 | status_t AudioFlinger::EffectHandle::initCheck() |
| 1381 | { |
| 1382 | return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY; |
| 1383 | } |
| 1384 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1385 | status_t AudioFlinger::EffectHandle::enable() |
| 1386 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1387 | AutoMutex _l(mLock); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1388 | ALOGV("enable %p", this); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1389 | sp<EffectModule> effect = mEffect.promote(); |
| 1390 | if (effect == 0 || mDisconnected) { |
| 1391 | return DEAD_OBJECT; |
| 1392 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1393 | if (!mHasControl) { |
| 1394 | return INVALID_OPERATION; |
| 1395 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1396 | |
| 1397 | if (mEnabled) { |
| 1398 | return NO_ERROR; |
| 1399 | } |
| 1400 | |
| 1401 | mEnabled = true; |
| 1402 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1403 | sp<ThreadBase> thread = effect->thread().promote(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1404 | if (thread != 0) { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1405 | thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | // checkSuspendOnEffectEnabled() can suspend this same effect when enabled |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1409 | if (effect->suspended()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1410 | return NO_ERROR; |
| 1411 | } |
| 1412 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1413 | status_t status = effect->setEnabled(true); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1414 | if (status != NO_ERROR) { |
| 1415 | if (thread != 0) { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1416 | thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1417 | } |
| 1418 | mEnabled = false; |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1419 | } else { |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1420 | if (thread != 0) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1421 | if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) { |
| 1422 | Mutex::Autolock _l(thread->mLock); |
| 1423 | thread->broadcast_l(); |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1424 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1425 | if (!effect->isOffloadable()) { |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1426 | if (thread->type() == ThreadBase::OFFLOAD) { |
| 1427 | PlaybackThread *t = (PlaybackThread *)thread.get(); |
| 1428 | t->invalidateTracks(AUDIO_STREAM_MUSIC); |
| 1429 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1430 | if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1431 | thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable(); |
| 1432 | } |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1433 | } |
| 1434 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1435 | } |
| 1436 | return status; |
| 1437 | } |
| 1438 | |
| 1439 | status_t AudioFlinger::EffectHandle::disable() |
| 1440 | { |
| 1441 | ALOGV("disable %p", this); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1442 | AutoMutex _l(mLock); |
| 1443 | sp<EffectModule> effect = mEffect.promote(); |
| 1444 | if (effect == 0 || mDisconnected) { |
| 1445 | return DEAD_OBJECT; |
| 1446 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1447 | if (!mHasControl) { |
| 1448 | return INVALID_OPERATION; |
| 1449 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1450 | |
| 1451 | if (!mEnabled) { |
| 1452 | return NO_ERROR; |
| 1453 | } |
| 1454 | mEnabled = false; |
| 1455 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1456 | if (effect->suspended()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1457 | return NO_ERROR; |
| 1458 | } |
| 1459 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1460 | status_t status = effect->setEnabled(false); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1461 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1462 | sp<ThreadBase> thread = effect->thread().promote(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1463 | if (thread != 0) { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1464 | thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1465 | if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) { |
| 1466 | Mutex::Autolock _l(thread->mLock); |
| 1467 | thread->broadcast_l(); |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1468 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | return status; |
| 1472 | } |
| 1473 | |
| 1474 | void AudioFlinger::EffectHandle::disconnect() |
| 1475 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1476 | ALOGV("%s %p", __FUNCTION__, this); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1477 | disconnect(true); |
| 1478 | } |
| 1479 | |
| 1480 | void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) |
| 1481 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1482 | AutoMutex _l(mLock); |
| 1483 | ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this); |
| 1484 | if (mDisconnected) { |
| 1485 | if (unpinIfLast) { |
| 1486 | android_errorWriteLog(0x534e4554, "32707507"); |
| 1487 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1488 | return; |
| 1489 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1490 | mDisconnected = true; |
| 1491 | sp<ThreadBase> thread; |
| 1492 | { |
| 1493 | sp<EffectModule> effect = mEffect.promote(); |
| 1494 | if (effect != 0) { |
| 1495 | thread = effect->thread().promote(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1496 | } |
| 1497 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1498 | if (thread != 0) { |
| 1499 | thread->disconnectEffectHandle(this, unpinIfLast); |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 1500 | } else { |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 1501 | // try to cleanup as much as we can |
| 1502 | sp<EffectModule> effect = mEffect.promote(); |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 1503 | if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) { |
| 1504 | ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this); |
Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 1505 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1506 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1507 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1508 | if (mClient != 0) { |
| 1509 | if (mCblk != NULL) { |
| 1510 | // unlike ~TrackBase(), mCblk is never a local new, so don't delete |
| 1511 | mCblk->~effect_param_cblk_t(); // destroy our shared-structure. |
| 1512 | } |
| 1513 | 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] | 1514 | // Client destructor must run with AudioFlinger client mutex locked |
| 1515 | Mutex::Autolock _l(mClient->audioFlinger()->mClientLock); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1516 | mClient.clear(); |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, |
| 1521 | uint32_t cmdSize, |
| 1522 | void *pCmdData, |
| 1523 | uint32_t *replySize, |
| 1524 | void *pReplyData) |
| 1525 | { |
| 1526 | ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1527 | cmdCode, mHasControl, mEffect.unsafe_get()); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1528 | |
Eric Laurent | c7ab309 | 2017-06-15 18:43:46 -0700 | [diff] [blame] | 1529 | // reject commands reserved for internal use by audio framework if coming from outside |
| 1530 | // of audioserver |
| 1531 | switch(cmdCode) { |
| 1532 | case EFFECT_CMD_ENABLE: |
| 1533 | case EFFECT_CMD_DISABLE: |
| 1534 | case EFFECT_CMD_SET_PARAM: |
| 1535 | case EFFECT_CMD_SET_PARAM_DEFERRED: |
| 1536 | case EFFECT_CMD_SET_PARAM_COMMIT: |
| 1537 | case EFFECT_CMD_GET_PARAM: |
| 1538 | break; |
| 1539 | default: |
| 1540 | if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) { |
| 1541 | break; |
| 1542 | } |
| 1543 | android_errorWriteLog(0x534e4554, "62019992"); |
| 1544 | return BAD_VALUE; |
| 1545 | } |
| 1546 | |
Eric Laurent | 1ffc585 | 2016-12-15 14:46:09 -0800 | [diff] [blame] | 1547 | if (cmdCode == EFFECT_CMD_ENABLE) { |
| 1548 | if (*replySize < sizeof(int)) { |
| 1549 | android_errorWriteLog(0x534e4554, "32095713"); |
| 1550 | return BAD_VALUE; |
| 1551 | } |
| 1552 | *(int *)pReplyData = NO_ERROR; |
| 1553 | *replySize = sizeof(int); |
| 1554 | return enable(); |
| 1555 | } else if (cmdCode == EFFECT_CMD_DISABLE) { |
| 1556 | if (*replySize < sizeof(int)) { |
| 1557 | android_errorWriteLog(0x534e4554, "32095713"); |
| 1558 | return BAD_VALUE; |
| 1559 | } |
| 1560 | *(int *)pReplyData = NO_ERROR; |
| 1561 | *replySize = sizeof(int); |
| 1562 | return disable(); |
| 1563 | } |
| 1564 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1565 | AutoMutex _l(mLock); |
| 1566 | sp<EffectModule> effect = mEffect.promote(); |
| 1567 | if (effect == 0 || mDisconnected) { |
| 1568 | return DEAD_OBJECT; |
| 1569 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1570 | // only get parameter command is permitted for applications not controlling the effect |
| 1571 | if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { |
| 1572 | return INVALID_OPERATION; |
| 1573 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1574 | if (mClient == 0) { |
| 1575 | return INVALID_OPERATION; |
| 1576 | } |
| 1577 | |
| 1578 | // handle commands that are not forwarded transparently to effect engine |
| 1579 | if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { |
Eric Laurent | 1ffc585 | 2016-12-15 14:46:09 -0800 | [diff] [blame] | 1580 | if (*replySize < sizeof(int)) { |
| 1581 | android_errorWriteLog(0x534e4554, "32095713"); |
| 1582 | return BAD_VALUE; |
| 1583 | } |
| 1584 | *(int *)pReplyData = NO_ERROR; |
| 1585 | *replySize = sizeof(int); |
| 1586 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1587 | // No need to trylock() here as this function is executed in the binder thread serving a |
| 1588 | // particular client process: no risk to block the whole media server process or mixer |
| 1589 | // threads if we are stuck here |
| 1590 | Mutex::Autolock _l(mCblk->lock); |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1591 | // keep local copy of index in case of client corruption b/32220769 |
| 1592 | const uint32_t clientIndex = mCblk->clientIndex; |
| 1593 | const uint32_t serverIndex = mCblk->serverIndex; |
| 1594 | if (clientIndex > EFFECT_PARAM_BUFFER_SIZE || |
| 1595 | serverIndex > EFFECT_PARAM_BUFFER_SIZE) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1596 | mCblk->serverIndex = 0; |
| 1597 | mCblk->clientIndex = 0; |
| 1598 | return BAD_VALUE; |
| 1599 | } |
| 1600 | status_t status = NO_ERROR; |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1601 | effect_param_t *param = NULL; |
| 1602 | for (uint32_t index = serverIndex; index < clientIndex;) { |
| 1603 | int *p = (int *)(mBuffer + index); |
| 1604 | const int size = *p++; |
| 1605 | if (size < 0 |
| 1606 | || size > EFFECT_PARAM_BUFFER_SIZE |
| 1607 | || ((uint8_t *)p + size) > mBuffer + clientIndex) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1608 | ALOGW("command(): invalid parameter block size"); |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1609 | status = BAD_VALUE; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1610 | break; |
| 1611 | } |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1612 | |
| 1613 | // copy to local memory in case of client corruption b/32220769 |
| 1614 | param = (effect_param_t *)realloc(param, size); |
| 1615 | if (param == NULL) { |
| 1616 | ALOGW("command(): out of memory"); |
| 1617 | status = NO_MEMORY; |
| 1618 | break; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1619 | } |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1620 | memcpy(param, p, size); |
| 1621 | |
| 1622 | int reply = 0; |
| 1623 | uint32_t rsize = sizeof(reply); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1624 | status_t ret = effect->command(EFFECT_CMD_SET_PARAM, |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1625 | size, |
| 1626 | param, |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1627 | &rsize, |
| 1628 | &reply); |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1629 | |
| 1630 | // verify shared memory: server index shouldn't change; client index can't go back. |
| 1631 | if (serverIndex != mCblk->serverIndex |
| 1632 | || clientIndex > mCblk->clientIndex) { |
| 1633 | android_errorWriteLog(0x534e4554, "32220769"); |
| 1634 | status = BAD_VALUE; |
| 1635 | break; |
| 1636 | } |
| 1637 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1638 | // stop at first error encountered |
| 1639 | if (ret != NO_ERROR) { |
| 1640 | status = ret; |
| 1641 | *(int *)pReplyData = reply; |
| 1642 | break; |
| 1643 | } else if (reply != NO_ERROR) { |
| 1644 | *(int *)pReplyData = reply; |
| 1645 | break; |
| 1646 | } |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1647 | index += size; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1648 | } |
Andy Hung | a447a0f | 2016-11-15 17:19:58 -0800 | [diff] [blame] | 1649 | free(param); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1650 | mCblk->serverIndex = 0; |
| 1651 | mCblk->clientIndex = 0; |
| 1652 | return status; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1655 | return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) |
| 1659 | { |
| 1660 | ALOGV("setControl %p control %d", this, hasControl); |
| 1661 | |
| 1662 | mHasControl = hasControl; |
| 1663 | mEnabled = enabled; |
| 1664 | |
| 1665 | if (signal && mEffectClient != 0) { |
| 1666 | mEffectClient->controlStatusChanged(hasControl); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, |
| 1671 | uint32_t cmdSize, |
| 1672 | void *pCmdData, |
| 1673 | uint32_t replySize, |
| 1674 | void *pReplyData) |
| 1675 | { |
| 1676 | if (mEffectClient != 0) { |
| 1677 | mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | |
| 1682 | |
| 1683 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) |
| 1684 | { |
| 1685 | if (mEffectClient != 0) { |
| 1686 | mEffectClient->enableStatusChanged(enabled); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | status_t AudioFlinger::EffectHandle::onTransact( |
| 1691 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1692 | { |
| 1693 | return BnEffect::onTransact(code, data, reply, flags); |
| 1694 | } |
| 1695 | |
| 1696 | |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 1697 | void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1698 | { |
| 1699 | bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock); |
| 1700 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1701 | snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n", |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1702 | (mClient == 0) ? getpid_cached : mClient->pid(), |
| 1703 | mPriority, |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 1704 | mHasControl ? "yes" : "no", |
| 1705 | locked ? "yes" : "no", |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1706 | mCblk ? mCblk->clientIndex : 0, |
| 1707 | mCblk ? mCblk->serverIndex : 0 |
| 1708 | ); |
| 1709 | |
| 1710 | if (locked) { |
| 1711 | mCblk->lock.unlock(); |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | #undef LOG_TAG |
| 1716 | #define LOG_TAG "AudioFlinger::EffectChain" |
| 1717 | |
| 1718 | AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1719 | audio_session_t sessionId) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1720 | : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1721 | mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 1722 | mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1723 | { |
| 1724 | mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); |
| 1725 | if (thread == NULL) { |
| 1726 | return; |
| 1727 | } |
| 1728 | mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / |
| 1729 | thread->frameCount(); |
| 1730 | } |
| 1731 | |
| 1732 | AudioFlinger::EffectChain::~EffectChain() |
| 1733 | { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held |
| 1737 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l( |
| 1738 | effect_descriptor_t *descriptor) |
| 1739 | { |
| 1740 | size_t size = mEffects.size(); |
| 1741 | |
| 1742 | for (size_t i = 0; i < size; i++) { |
| 1743 | if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { |
| 1744 | return mEffects[i]; |
| 1745 | } |
| 1746 | } |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | // getEffectFromId_l() must be called with ThreadBase::mLock held |
| 1751 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) |
| 1752 | { |
| 1753 | size_t size = mEffects.size(); |
| 1754 | |
| 1755 | for (size_t i = 0; i < size; i++) { |
| 1756 | // by convention, return first effect if id provided is 0 (0 is never a valid id) |
| 1757 | if (id == 0 || mEffects[i]->id() == id) { |
| 1758 | return mEffects[i]; |
| 1759 | } |
| 1760 | } |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | // getEffectFromType_l() must be called with ThreadBase::mLock held |
| 1765 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( |
| 1766 | const effect_uuid_t *type) |
| 1767 | { |
| 1768 | size_t size = mEffects.size(); |
| 1769 | |
| 1770 | for (size_t i = 0; i < size; i++) { |
| 1771 | if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { |
| 1772 | return mEffects[i]; |
| 1773 | } |
| 1774 | } |
| 1775 | return 0; |
| 1776 | } |
| 1777 | |
| 1778 | void AudioFlinger::EffectChain::clearInputBuffer() |
| 1779 | { |
| 1780 | Mutex::Autolock _l(mLock); |
| 1781 | sp<ThreadBase> thread = mThread.promote(); |
| 1782 | if (thread == 0) { |
| 1783 | ALOGW("clearInputBuffer(): cannot promote mixer thread"); |
| 1784 | return; |
| 1785 | } |
| 1786 | clearInputBuffer_l(thread); |
| 1787 | } |
| 1788 | |
| 1789 | // Must be called with EffectChain::mLock locked |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1790 | void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1791 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1792 | if (mInBuffer == NULL) { |
| 1793 | return; |
| 1794 | } |
Ricardo Garcia | 322bab2 | 2014-08-06 11:43:46 -0700 | [diff] [blame] | 1795 | // TODO: This will change in the future, depending on multichannel |
| 1796 | // and sample format changes for effects. |
| 1797 | // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT |
| 1798 | // (4 bytes frame size) |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1799 | |
Ricardo Garcia | 726b6a7 | 2014-08-11 12:04:54 -0700 | [diff] [blame] | 1800 | const size_t frameSize = |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1801 | audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) |
| 1802 | * std::min((uint32_t)FCC_2, thread->channelCount()); |
| 1803 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1804 | memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize); |
| 1805 | mInBuffer->commit(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | // Must be called with EffectChain::mLock locked |
| 1809 | void AudioFlinger::EffectChain::process_l() |
| 1810 | { |
| 1811 | sp<ThreadBase> thread = mThread.promote(); |
| 1812 | if (thread == 0) { |
| 1813 | ALOGW("process_l(): cannot promote mixer thread"); |
| 1814 | return; |
| 1815 | } |
| 1816 | bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || |
| 1817 | (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); |
Jean-Michel Trivi | fed6292 | 2013-09-25 18:50:33 -0700 | [diff] [blame] | 1818 | // never process effects when: |
| 1819 | // - on an OFFLOAD thread |
| 1820 | // - no more tracks are on the session and the effect tail has been rendered |
Phil Burk | 869fab1 | 2017-02-27 18:44:19 -0800 | [diff] [blame] | 1821 | bool doProcess = (thread->type() != ThreadBase::OFFLOAD) |
| 1822 | && (thread->type() != ThreadBase::MMAP); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1823 | if (!isGlobalSession) { |
| 1824 | bool tracksOnSession = (trackCnt() != 0); |
| 1825 | |
| 1826 | if (!tracksOnSession && mTailBufferCount == 0) { |
| 1827 | doProcess = false; |
| 1828 | } |
| 1829 | |
| 1830 | if (activeTrackCnt() == 0) { |
| 1831 | // if no track is active and the effect tail has not been rendered, |
| 1832 | // the input buffer must be cleared here as the mixer process will not do it |
| 1833 | if (tracksOnSession || mTailBufferCount > 0) { |
| 1834 | clearInputBuffer_l(thread); |
| 1835 | if (mTailBufferCount > 0) { |
| 1836 | mTailBufferCount--; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | size_t size = mEffects.size(); |
| 1843 | if (doProcess) { |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1844 | // Only the input and output buffers of the chain can be external, |
| 1845 | // and 'update' / 'commit' do nothing for allocated buffers, thus |
| 1846 | // it's not needed to consider any other buffers here. |
| 1847 | mInBuffer->update(); |
Mikhail Naganov | 0688880 | 2017-01-19 12:47:55 -0800 | [diff] [blame] | 1848 | if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) { |
| 1849 | mOutBuffer->update(); |
| 1850 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1851 | for (size_t i = 0; i < size; i++) { |
| 1852 | mEffects[i]->process(); |
| 1853 | } |
Mikhail Naganov | 0688880 | 2017-01-19 12:47:55 -0800 | [diff] [blame] | 1854 | mInBuffer->commit(); |
| 1855 | if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) { |
| 1856 | mOutBuffer->commit(); |
| 1857 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1858 | } |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 1859 | bool doResetVolume = false; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1860 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 1861 | doResetVolume = mEffects[i]->updateState() || doResetVolume; |
| 1862 | } |
| 1863 | if (doResetVolume) { |
| 1864 | resetVolume_l(); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1868 | // createEffect_l() must be called with ThreadBase::mLock held |
| 1869 | status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect, |
| 1870 | ThreadBase *thread, |
| 1871 | effect_descriptor_t *desc, |
| 1872 | int id, |
| 1873 | audio_session_t sessionId, |
| 1874 | bool pinned) |
| 1875 | { |
| 1876 | Mutex::Autolock _l(mLock); |
| 1877 | effect = new EffectModule(thread, this, desc, id, sessionId, pinned); |
| 1878 | status_t lStatus = effect->status(); |
| 1879 | if (lStatus == NO_ERROR) { |
| 1880 | lStatus = addEffect_ll(effect); |
| 1881 | } |
| 1882 | if (lStatus != NO_ERROR) { |
| 1883 | effect.clear(); |
| 1884 | } |
| 1885 | return lStatus; |
| 1886 | } |
| 1887 | |
| 1888 | // addEffect_l() must be called with ThreadBase::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1889 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) |
| 1890 | { |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 1891 | Mutex::Autolock _l(mLock); |
| 1892 | return addEffect_ll(effect); |
| 1893 | } |
| 1894 | // addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held |
| 1895 | status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect) |
| 1896 | { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1897 | effect_descriptor_t desc = effect->desc(); |
| 1898 | uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; |
| 1899 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1900 | effect->setChain(this); |
| 1901 | sp<ThreadBase> thread = mThread.promote(); |
| 1902 | if (thread == 0) { |
| 1903 | return NO_INIT; |
| 1904 | } |
| 1905 | effect->setThread(thread); |
| 1906 | |
| 1907 | if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 1908 | // Auxiliary effects are inserted at the beginning of mEffects vector as |
| 1909 | // they are processed first and accumulated in chain input buffer |
| 1910 | mEffects.insertAt(effect, 0); |
| 1911 | |
| 1912 | // the input buffer for auxiliary effect contains mono samples in |
| 1913 | // 32 bit format. This is to avoid saturation in AudoMixer |
| 1914 | // accumulation stage. Saturation is done in EffectModule::process() before |
| 1915 | // calling the process in effect engine |
| 1916 | size_t numSamples = thread->frameCount(); |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1917 | sp<EffectBufferHalInterface> halBuffer; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1918 | #ifdef FLOAT_EFFECT_CHAIN |
| 1919 | status_t result = EffectBufferHalInterface::allocate( |
| 1920 | numSamples * sizeof(float), &halBuffer); |
| 1921 | #else |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1922 | status_t result = EffectBufferHalInterface::allocate( |
| 1923 | numSamples * sizeof(int32_t), &halBuffer); |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 1924 | #endif |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 1925 | if (result != OK) return result; |
| 1926 | effect->setInBuffer(halBuffer); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1927 | // auxiliary effects output samples to chain input buffer for further processing |
| 1928 | // by insert effects |
| 1929 | effect->setOutBuffer(mInBuffer); |
| 1930 | } else { |
| 1931 | // Insert effects are inserted at the end of mEffects vector as they are processed |
| 1932 | // after track and auxiliary effects. |
| 1933 | // Insert effect order as a function of indicated preference: |
| 1934 | // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if |
| 1935 | // another effect is present |
| 1936 | // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the |
| 1937 | // last effect claiming first position |
| 1938 | // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the |
| 1939 | // first effect claiming last position |
| 1940 | // else if EFFECT_FLAG_INSERT_ANY insert after first or before last |
| 1941 | // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is |
| 1942 | // already present |
| 1943 | |
| 1944 | size_t size = mEffects.size(); |
| 1945 | size_t idx_insert = size; |
| 1946 | ssize_t idx_insert_first = -1; |
| 1947 | ssize_t idx_insert_last = -1; |
| 1948 | |
| 1949 | for (size_t i = 0; i < size; i++) { |
| 1950 | effect_descriptor_t d = mEffects[i]->desc(); |
| 1951 | uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; |
| 1952 | uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; |
| 1953 | if (iMode == EFFECT_FLAG_TYPE_INSERT) { |
| 1954 | // check invalid effect chaining combinations |
| 1955 | if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || |
| 1956 | iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { |
| 1957 | ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", |
| 1958 | desc.name, d.name); |
| 1959 | return INVALID_OPERATION; |
| 1960 | } |
| 1961 | // remember position of first insert effect and by default |
| 1962 | // select this as insert position for new effect |
| 1963 | if (idx_insert == size) { |
| 1964 | idx_insert = i; |
| 1965 | } |
| 1966 | // remember position of last insert effect claiming |
| 1967 | // first position |
| 1968 | if (iPref == EFFECT_FLAG_INSERT_FIRST) { |
| 1969 | idx_insert_first = i; |
| 1970 | } |
| 1971 | // remember position of first insert effect claiming |
| 1972 | // last position |
| 1973 | if (iPref == EFFECT_FLAG_INSERT_LAST && |
| 1974 | idx_insert_last == -1) { |
| 1975 | idx_insert_last = i; |
| 1976 | } |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | // modify idx_insert from first position if needed |
| 1981 | if (insertPref == EFFECT_FLAG_INSERT_LAST) { |
| 1982 | if (idx_insert_last != -1) { |
| 1983 | idx_insert = idx_insert_last; |
| 1984 | } else { |
| 1985 | idx_insert = size; |
| 1986 | } |
| 1987 | } else { |
| 1988 | if (idx_insert_first != -1) { |
| 1989 | idx_insert = idx_insert_first + 1; |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | // always read samples from chain input buffer |
| 1994 | effect->setInBuffer(mInBuffer); |
| 1995 | |
| 1996 | // if last effect in the chain, output samples to chain |
| 1997 | // output buffer, otherwise to chain input buffer |
| 1998 | if (idx_insert == size) { |
| 1999 | if (idx_insert != 0) { |
| 2000 | mEffects[idx_insert-1]->setOutBuffer(mInBuffer); |
| 2001 | mEffects[idx_insert-1]->configure(); |
| 2002 | } |
| 2003 | effect->setOutBuffer(mOutBuffer); |
| 2004 | } else { |
| 2005 | effect->setOutBuffer(mInBuffer); |
| 2006 | } |
| 2007 | mEffects.insertAt(effect, idx_insert); |
| 2008 | |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2009 | 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] | 2010 | idx_insert); |
| 2011 | } |
| 2012 | effect->configure(); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2013 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2014 | return NO_ERROR; |
| 2015 | } |
| 2016 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2017 | // removeEffect_l() must be called with ThreadBase::mLock held |
| 2018 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect, |
| 2019 | bool release) |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2020 | { |
| 2021 | Mutex::Autolock _l(mLock); |
| 2022 | size_t size = mEffects.size(); |
| 2023 | uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; |
| 2024 | |
| 2025 | for (size_t i = 0; i < size; i++) { |
| 2026 | if (effect == mEffects[i]) { |
| 2027 | // calling stop here will remove pre-processing effect from the audio HAL. |
| 2028 | // This is safe as we hold the EffectChain mutex which guarantees that we are not in |
| 2029 | // the middle of a read from audio HAL |
| 2030 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 2031 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 2032 | mEffects[i]->stop(); |
| 2033 | } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2034 | if (release) { |
| 2035 | mEffects[i]->release_l(); |
| 2036 | } |
| 2037 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 2038 | if (type != EFFECT_FLAG_TYPE_AUXILIARY) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2039 | if (i == size - 1 && i != 0) { |
| 2040 | mEffects[i - 1]->setOutBuffer(mOutBuffer); |
| 2041 | mEffects[i - 1]->configure(); |
| 2042 | } |
| 2043 | } |
| 2044 | mEffects.removeAt(i); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2045 | 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] | 2046 | this, i); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2047 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2048 | break; |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | return mEffects.size(); |
| 2053 | } |
| 2054 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2055 | // setDevice_l() must be called with ThreadBase::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2056 | void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device) |
| 2057 | { |
| 2058 | size_t size = mEffects.size(); |
| 2059 | for (size_t i = 0; i < size; i++) { |
| 2060 | mEffects[i]->setDevice(device); |
| 2061 | } |
| 2062 | } |
| 2063 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2064 | // setMode_l() must be called with ThreadBase::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2065 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) |
| 2066 | { |
| 2067 | size_t size = mEffects.size(); |
| 2068 | for (size_t i = 0; i < size; i++) { |
| 2069 | mEffects[i]->setMode(mode); |
| 2070 | } |
| 2071 | } |
| 2072 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2073 | // setAudioSource_l() must be called with ThreadBase::mLock held |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2074 | void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source) |
| 2075 | { |
| 2076 | size_t size = mEffects.size(); |
| 2077 | for (size_t i = 0; i < size; i++) { |
| 2078 | mEffects[i]->setAudioSource(source); |
| 2079 | } |
| 2080 | } |
| 2081 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2082 | // setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2083 | 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] | 2084 | { |
| 2085 | uint32_t newLeft = *left; |
| 2086 | uint32_t newRight = *right; |
| 2087 | bool hasControl = false; |
| 2088 | int ctrlIdx = -1; |
| 2089 | size_t size = mEffects.size(); |
| 2090 | |
| 2091 | // first update volume controller |
| 2092 | for (size_t i = size; i > 0; i--) { |
| 2093 | if (mEffects[i - 1]->isProcessEnabled() && |
| 2094 | (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { |
| 2095 | ctrlIdx = i - 1; |
| 2096 | hasControl = true; |
| 2097 | break; |
| 2098 | } |
| 2099 | } |
| 2100 | |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2101 | if (!force && ctrlIdx == mVolumeCtrlIdx && |
Eric Laurent | cb4b6e9 | 2014-10-01 14:26:10 -0700 | [diff] [blame] | 2102 | *left == mLeftVolume && *right == mRightVolume) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2103 | if (hasControl) { |
| 2104 | *left = mNewLeftVolume; |
| 2105 | *right = mNewRightVolume; |
| 2106 | } |
| 2107 | return hasControl; |
| 2108 | } |
| 2109 | |
| 2110 | mVolumeCtrlIdx = ctrlIdx; |
| 2111 | mLeftVolume = newLeft; |
| 2112 | mRightVolume = newRight; |
| 2113 | |
| 2114 | // second get volume update from volume controller |
| 2115 | if (ctrlIdx >= 0) { |
| 2116 | mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); |
| 2117 | mNewLeftVolume = newLeft; |
| 2118 | mNewRightVolume = newRight; |
| 2119 | } |
| 2120 | // then indicate volume to all other effects in chain. |
| 2121 | // Pass altered volume to effects before volume controller |
| 2122 | // and requested volume to effects after controller |
| 2123 | uint32_t lVol = newLeft; |
| 2124 | uint32_t rVol = newRight; |
| 2125 | |
| 2126 | for (size_t i = 0; i < size; i++) { |
| 2127 | if ((int)i == ctrlIdx) { |
| 2128 | continue; |
| 2129 | } |
| 2130 | // this also works for ctrlIdx == -1 when there is no volume controller |
| 2131 | if ((int)i > ctrlIdx) { |
| 2132 | lVol = *left; |
| 2133 | rVol = *right; |
| 2134 | } |
| 2135 | mEffects[i]->setVolume(&lVol, &rVol, false); |
| 2136 | } |
| 2137 | *left = newLeft; |
| 2138 | *right = newRight; |
| 2139 | |
| 2140 | return hasControl; |
| 2141 | } |
| 2142 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2143 | // resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2144 | void AudioFlinger::EffectChain::resetVolume_l() |
| 2145 | { |
Eric Laurent | e7449bf | 2016-08-03 18:44:07 -0700 | [diff] [blame] | 2146 | if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) { |
| 2147 | uint32_t left = mLeftVolume; |
| 2148 | uint32_t right = mRightVolume; |
| 2149 | (void)setVolume_l(&left, &right, true); |
| 2150 | } |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2153 | void AudioFlinger::EffectChain::syncHalEffectsState() |
| 2154 | { |
| 2155 | Mutex::Autolock _l(mLock); |
| 2156 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2157 | if (mEffects[i]->state() == EffectModule::ACTIVE || |
| 2158 | mEffects[i]->state() == EffectModule::STOPPING) { |
| 2159 | mEffects[i]->addEffectToHal_l(); |
| 2160 | } |
| 2161 | } |
| 2162 | } |
| 2163 | |
Mikhail Naganov | 0688880 | 2017-01-19 12:47:55 -0800 | [diff] [blame] | 2164 | static void dumpInOutBuffer( |
| 2165 | char *dump, size_t dumpSize, bool isInput, EffectBufferHalInterface *buffer) { |
Mikhail Naganov | c778e59 | 2017-01-25 10:35:30 -0800 | [diff] [blame] | 2166 | if (buffer == nullptr) { |
| 2167 | snprintf(dump, dumpSize, "%p", buffer); |
| 2168 | } else if (buffer->externalData() != nullptr) { |
Mikhail Naganov | 0688880 | 2017-01-19 12:47:55 -0800 | [diff] [blame] | 2169 | snprintf(dump, dumpSize, "%p -> %p", |
| 2170 | isInput ? buffer->externalData() : buffer->audioBuffer()->raw, |
| 2171 | isInput ? buffer->audioBuffer()->raw : buffer->externalData()); |
| 2172 | } else { |
| 2173 | snprintf(dump, dumpSize, "%p", buffer->audioBuffer()->raw); |
| 2174 | } |
| 2175 | } |
| 2176 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2177 | void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) |
| 2178 | { |
| 2179 | const size_t SIZE = 256; |
| 2180 | char buffer[SIZE]; |
| 2181 | String8 result; |
| 2182 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2183 | size_t numEffects = mEffects.size(); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2184 | snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2185 | result.append(buffer); |
| 2186 | |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2187 | if (numEffects) { |
| 2188 | bool locked = AudioFlinger::dumpTryLock(mLock); |
| 2189 | // failed to lock - AudioFlinger is probably deadlocked |
| 2190 | if (!locked) { |
| 2191 | result.append("\tCould not lock mutex:\n"); |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2192 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2193 | |
Mikhail Naganov | 0688880 | 2017-01-19 12:47:55 -0800 | [diff] [blame] | 2194 | char inBufferStr[64], outBufferStr[64]; |
| 2195 | dumpInOutBuffer(inBufferStr, sizeof(inBufferStr), true, mInBuffer.get()); |
| 2196 | dumpInOutBuffer(outBufferStr, sizeof(outBufferStr), false, mOutBuffer.get()); |
| 2197 | snprintf(buffer, SIZE, "\t%-*s%-*s Active tracks:\n", |
| 2198 | (int)strlen(inBufferStr), "In buffer ", |
| 2199 | (int)strlen(outBufferStr), "Out buffer "); |
| 2200 | result.append(buffer); |
| 2201 | snprintf(buffer, SIZE, "\t%s %s %d\n", inBufferStr, outBufferStr, mActiveTrackCnt); |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2202 | result.append(buffer); |
| 2203 | write(fd, result.string(), result.size()); |
| 2204 | |
| 2205 | for (size_t i = 0; i < numEffects; ++i) { |
| 2206 | sp<EffectModule> effect = mEffects[i]; |
| 2207 | if (effect != 0) { |
| 2208 | effect->dump(fd, args); |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | if (locked) { |
| 2213 | mLock.unlock(); |
| 2214 | } |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | // must be called with ThreadBase::mLock held |
| 2219 | void AudioFlinger::EffectChain::setEffectSuspended_l( |
| 2220 | const effect_uuid_t *type, bool suspend) |
| 2221 | { |
| 2222 | sp<SuspendedEffectDesc> desc; |
| 2223 | // use effect type UUID timelow as key as there is no real risk of identical |
| 2224 | // timeLow fields among effect type UUIDs. |
| 2225 | ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); |
| 2226 | if (suspend) { |
| 2227 | if (index >= 0) { |
| 2228 | desc = mSuspendedEffects.valueAt(index); |
| 2229 | } else { |
| 2230 | desc = new SuspendedEffectDesc(); |
| 2231 | desc->mType = *type; |
| 2232 | mSuspendedEffects.add(type->timeLow, desc); |
| 2233 | ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); |
| 2234 | } |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2235 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2236 | if (desc->mRefCount++ == 0) { |
| 2237 | sp<EffectModule> effect = getEffectIfEnabled(type); |
| 2238 | if (effect != 0) { |
| 2239 | desc->mEffect = effect; |
| 2240 | effect->setSuspended(true); |
| 2241 | effect->setEnabled(false); |
| 2242 | } |
| 2243 | } |
| 2244 | } else { |
| 2245 | if (index < 0) { |
| 2246 | return; |
| 2247 | } |
| 2248 | desc = mSuspendedEffects.valueAt(index); |
| 2249 | if (desc->mRefCount <= 0) { |
| 2250 | ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2251 | desc->mRefCount = 0; |
| 2252 | return; |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2253 | } |
| 2254 | if (--desc->mRefCount == 0) { |
| 2255 | ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); |
| 2256 | if (desc->mEffect != 0) { |
| 2257 | sp<EffectModule> effect = desc->mEffect.promote(); |
| 2258 | if (effect != 0) { |
| 2259 | effect->setSuspended(false); |
| 2260 | effect->lock(); |
| 2261 | EffectHandle *handle = effect->controlHandle_l(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2262 | if (handle != NULL && !handle->disconnected()) { |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2263 | effect->setEnabled_l(handle->enabled()); |
| 2264 | } |
| 2265 | effect->unlock(); |
| 2266 | } |
| 2267 | desc->mEffect.clear(); |
| 2268 | } |
| 2269 | mSuspendedEffects.removeItemsAt(index); |
| 2270 | } |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | // must be called with ThreadBase::mLock held |
| 2275 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) |
| 2276 | { |
| 2277 | sp<SuspendedEffectDesc> desc; |
| 2278 | |
| 2279 | ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 2280 | if (suspend) { |
| 2281 | if (index >= 0) { |
| 2282 | desc = mSuspendedEffects.valueAt(index); |
| 2283 | } else { |
| 2284 | desc = new SuspendedEffectDesc(); |
| 2285 | mSuspendedEffects.add((int)kKeyForSuspendAll, desc); |
| 2286 | ALOGV("setEffectSuspendedAll_l() add entry for 0"); |
| 2287 | } |
| 2288 | if (desc->mRefCount++ == 0) { |
| 2289 | Vector< sp<EffectModule> > effects; |
| 2290 | getSuspendEligibleEffects(effects); |
| 2291 | for (size_t i = 0; i < effects.size(); i++) { |
| 2292 | setEffectSuspended_l(&effects[i]->desc().type, true); |
| 2293 | } |
| 2294 | } |
| 2295 | } else { |
| 2296 | if (index < 0) { |
| 2297 | return; |
| 2298 | } |
| 2299 | desc = mSuspendedEffects.valueAt(index); |
| 2300 | if (desc->mRefCount <= 0) { |
| 2301 | ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); |
| 2302 | desc->mRefCount = 1; |
| 2303 | } |
| 2304 | if (--desc->mRefCount == 0) { |
| 2305 | Vector<const effect_uuid_t *> types; |
| 2306 | for (size_t i = 0; i < mSuspendedEffects.size(); i++) { |
| 2307 | if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { |
| 2308 | continue; |
| 2309 | } |
| 2310 | types.add(&mSuspendedEffects.valueAt(i)->mType); |
| 2311 | } |
| 2312 | for (size_t i = 0; i < types.size(); i++) { |
| 2313 | setEffectSuspended_l(types[i], false); |
| 2314 | } |
| 2315 | ALOGV("setEffectSuspendedAll_l() remove entry for %08x", |
| 2316 | mSuspendedEffects.keyAt(index)); |
| 2317 | mSuspendedEffects.removeItem((int)kKeyForSuspendAll); |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | // The volume effect is used for automated tests only |
| 2324 | #ifndef OPENSL_ES_H_ |
| 2325 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, |
| 2326 | { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; |
| 2327 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; |
| 2328 | #endif //OPENSL_ES_H_ |
| 2329 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2330 | /* static */ |
| 2331 | bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type) |
| 2332 | { |
| 2333 | // Only NS and AEC are suspended when BtNRec is off |
| 2334 | if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) || |
| 2335 | (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) { |
| 2336 | return true; |
| 2337 | } |
| 2338 | return false; |
| 2339 | } |
| 2340 | |
Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2341 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) |
| 2342 | { |
| 2343 | // auxiliary effects and visualizer are never suspended on output mix |
| 2344 | if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && |
| 2345 | (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || |
| 2346 | (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || |
| 2347 | (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { |
| 2348 | return false; |
| 2349 | } |
| 2350 | return true; |
| 2351 | } |
| 2352 | |
| 2353 | void AudioFlinger::EffectChain::getSuspendEligibleEffects( |
| 2354 | Vector< sp<AudioFlinger::EffectModule> > &effects) |
| 2355 | { |
| 2356 | effects.clear(); |
| 2357 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2358 | if (isEffectEligibleForSuspend(mEffects[i]->desc())) { |
| 2359 | effects.add(mEffects[i]); |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( |
| 2365 | const effect_uuid_t *type) |
| 2366 | { |
| 2367 | sp<EffectModule> effect = getEffectFromType_l(type); |
| 2368 | return effect != 0 && effect->isEnabled() ? effect : 0; |
| 2369 | } |
| 2370 | |
| 2371 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, |
| 2372 | bool enabled) |
| 2373 | { |
| 2374 | ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
| 2375 | if (enabled) { |
| 2376 | if (index < 0) { |
| 2377 | // if the effect is not suspend check if all effects are suspended |
| 2378 | index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); |
| 2379 | if (index < 0) { |
| 2380 | return; |
| 2381 | } |
| 2382 | if (!isEffectEligibleForSuspend(effect->desc())) { |
| 2383 | return; |
| 2384 | } |
| 2385 | setEffectSuspended_l(&effect->desc().type, enabled); |
| 2386 | index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); |
| 2387 | if (index < 0) { |
| 2388 | ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); |
| 2389 | return; |
| 2390 | } |
| 2391 | } |
| 2392 | ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", |
| 2393 | effect->desc().type.timeLow); |
| 2394 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2395 | // 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] | 2396 | if (desc->mEffect == 0) { |
| 2397 | desc->mEffect = effect; |
| 2398 | effect->setEnabled(false); |
| 2399 | effect->setSuspended(true); |
| 2400 | } |
| 2401 | } else { |
| 2402 | if (index < 0) { |
| 2403 | return; |
| 2404 | } |
| 2405 | ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", |
| 2406 | effect->desc().type.timeLow); |
| 2407 | sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); |
| 2408 | desc->mEffect.clear(); |
| 2409 | effect->setSuspended(false); |
| 2410 | } |
| 2411 | } |
| 2412 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2413 | bool AudioFlinger::EffectChain::isNonOffloadableEnabled() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2414 | { |
| 2415 | Mutex::Autolock _l(mLock); |
| 2416 | size_t size = mEffects.size(); |
| 2417 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 2418 | if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 2419 | return true; |
| 2420 | } |
| 2421 | } |
| 2422 | return false; |
| 2423 | } |
| 2424 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2425 | void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread) |
| 2426 | { |
| 2427 | Mutex::Autolock _l(mLock); |
| 2428 | mThread = thread; |
| 2429 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2430 | mEffects[i]->setThread(thread); |
| 2431 | } |
| 2432 | } |
| 2433 | |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2434 | void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const |
| 2435 | { |
| 2436 | if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) { |
| 2437 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW); |
| 2438 | } |
| 2439 | if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) { |
| 2440 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST); |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const |
| 2445 | { |
| 2446 | if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) { |
| 2447 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW); |
| 2448 | } |
| 2449 | if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) { |
| 2450 | *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | bool AudioFlinger::EffectChain::isRawCompatible() const |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2455 | { |
| 2456 | Mutex::Autolock _l(mLock); |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2457 | for (const auto &effect : mEffects) { |
| 2458 | if (effect->isProcessImplemented()) { |
| 2459 | return false; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2460 | } |
| 2461 | } |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 2462 | // Allow effects without processing. |
| 2463 | return true; |
| 2464 | } |
| 2465 | |
| 2466 | bool AudioFlinger::EffectChain::isFastCompatible() const |
| 2467 | { |
| 2468 | Mutex::Autolock _l(mLock); |
| 2469 | for (const auto &effect : mEffects) { |
| 2470 | if (effect->isProcessImplemented() |
| 2471 | && effect->isImplementationSoftware()) { |
| 2472 | return false; |
| 2473 | } |
| 2474 | } |
| 2475 | // Allow effects without processing or hw accelerated effects. |
| 2476 | return true; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | // isCompatibleWithThread_l() must be called with thread->mLock held |
| 2480 | bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const |
| 2481 | { |
| 2482 | Mutex::Autolock _l(mLock); |
| 2483 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2484 | if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) { |
| 2485 | return false; |
| 2486 | } |
| 2487 | } |
| 2488 | return true; |
| 2489 | } |
| 2490 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 2491 | } // namespace android |