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