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