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