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