| 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> | 
 | 24 | #include <audio_effects/effect_visualizer.h> | 
 | 25 | #include <audio_utils/primitives.h> | 
 | 26 | #include <private/media/AudioEffectShared.h> | 
 | 27 | #include <media/EffectsFactoryApi.h> | 
 | 28 |  | 
 | 29 | #include "AudioFlinger.h" | 
 | 30 | #include "ServiceUtilities.h" | 
 | 31 |  | 
 | 32 | // ---------------------------------------------------------------------------- | 
 | 33 |  | 
 | 34 | // Note: the following macro is used for extremely verbose logging message.  In | 
 | 35 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
 | 36 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
 | 37 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
 | 38 | // turned on.  Do not uncomment the #def below unless you really know what you | 
 | 39 | // are doing and want to see all of the extremely verbose messages. | 
 | 40 | //#define VERY_VERY_VERBOSE_LOGGING | 
 | 41 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
 | 42 | #define ALOGVV ALOGV | 
 | 43 | #else | 
 | 44 | #define ALOGVV(a...) do { } while(0) | 
 | 45 | #endif | 
 | 46 |  | 
 | 47 | namespace android { | 
 | 48 |  | 
 | 49 | // ---------------------------------------------------------------------------- | 
 | 50 | //  EffectModule implementation | 
 | 51 | // ---------------------------------------------------------------------------- | 
 | 52 |  | 
 | 53 | #undef LOG_TAG | 
 | 54 | #define LOG_TAG "AudioFlinger::EffectModule" | 
 | 55 |  | 
 | 56 | AudioFlinger::EffectModule::EffectModule(ThreadBase *thread, | 
 | 57 |                                         const wp<AudioFlinger::EffectChain>& chain, | 
 | 58 |                                         effect_descriptor_t *desc, | 
 | 59 |                                         int id, | 
 | 60 |                                         int sessionId) | 
 | 61 |     : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX), | 
 | 62 |       mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), | 
 | 63 |       mDescriptor(*desc), | 
 | 64 |       // mConfig is set by configure() and not used before then | 
 | 65 |       mEffectInterface(NULL), | 
 | 66 |       mStatus(NO_INIT), mState(IDLE), | 
 | 67 |       // mMaxDisableWaitCnt is set by configure() and not used before then | 
 | 68 |       // mDisableWaitCnt is set by process() and updateState() and not used before then | 
 | 69 |       mSuspended(false) | 
 | 70 | { | 
 | 71 |     ALOGV("Constructor %p", this); | 
 | 72 |     int lStatus; | 
 | 73 |  | 
 | 74 |     // create effect engine from effect factory | 
 | 75 |     mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface); | 
 | 76 |  | 
 | 77 |     if (mStatus != NO_ERROR) { | 
 | 78 |         return; | 
 | 79 |     } | 
 | 80 |     lStatus = init(); | 
 | 81 |     if (lStatus < 0) { | 
 | 82 |         mStatus = lStatus; | 
 | 83 |         goto Error; | 
 | 84 |     } | 
 | 85 |  | 
 | 86 |     ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface); | 
 | 87 |     return; | 
 | 88 | Error: | 
 | 89 |     EffectRelease(mEffectInterface); | 
 | 90 |     mEffectInterface = NULL; | 
 | 91 |     ALOGV("Constructor Error %d", mStatus); | 
 | 92 | } | 
 | 93 |  | 
 | 94 | AudioFlinger::EffectModule::~EffectModule() | 
 | 95 | { | 
 | 96 |     ALOGV("Destructor %p", this); | 
 | 97 |     if (mEffectInterface != NULL) { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 98 |         remove_effect_from_hal_l(); | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 99 |         // release effect engine | 
 | 100 |         EffectRelease(mEffectInterface); | 
 | 101 |     } | 
 | 102 | } | 
 | 103 |  | 
 | 104 | status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle) | 
 | 105 | { | 
 | 106 |     status_t status; | 
 | 107 |  | 
 | 108 |     Mutex::Autolock _l(mLock); | 
 | 109 |     int priority = handle->priority(); | 
 | 110 |     size_t size = mHandles.size(); | 
 | 111 |     EffectHandle *controlHandle = NULL; | 
 | 112 |     size_t i; | 
 | 113 |     for (i = 0; i < size; i++) { | 
 | 114 |         EffectHandle *h = mHandles[i]; | 
 | 115 |         if (h == NULL || h->destroyed_l()) { | 
 | 116 |             continue; | 
 | 117 |         } | 
 | 118 |         // first non destroyed handle is considered in control | 
 | 119 |         if (controlHandle == NULL) | 
 | 120 |             controlHandle = h; | 
 | 121 |         if (h->priority() <= priority) { | 
 | 122 |             break; | 
 | 123 |         } | 
 | 124 |     } | 
 | 125 |     // if inserted in first place, move effect control from previous owner to this handle | 
 | 126 |     if (i == 0) { | 
 | 127 |         bool enabled = false; | 
 | 128 |         if (controlHandle != NULL) { | 
 | 129 |             enabled = controlHandle->enabled(); | 
 | 130 |             controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/); | 
 | 131 |         } | 
 | 132 |         handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/); | 
 | 133 |         status = NO_ERROR; | 
 | 134 |     } else { | 
 | 135 |         status = ALREADY_EXISTS; | 
 | 136 |     } | 
 | 137 |     ALOGV("addHandle() %p added handle %p in position %d", this, handle, i); | 
 | 138 |     mHandles.insertAt(handle, i); | 
 | 139 |     return status; | 
 | 140 | } | 
 | 141 |  | 
 | 142 | size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle) | 
 | 143 | { | 
 | 144 |     Mutex::Autolock _l(mLock); | 
 | 145 |     size_t size = mHandles.size(); | 
 | 146 |     size_t i; | 
 | 147 |     for (i = 0; i < size; i++) { | 
 | 148 |         if (mHandles[i] == handle) { | 
 | 149 |             break; | 
 | 150 |         } | 
 | 151 |     } | 
 | 152 |     if (i == size) { | 
 | 153 |         return size; | 
 | 154 |     } | 
 | 155 |     ALOGV("removeHandle() %p removed handle %p in position %d", this, handle, i); | 
 | 156 |  | 
 | 157 |     mHandles.removeAt(i); | 
 | 158 |     // if removed from first place, move effect control from this handle to next in line | 
 | 159 |     if (i == 0) { | 
 | 160 |         EffectHandle *h = controlHandle_l(); | 
 | 161 |         if (h != NULL) { | 
 | 162 |             h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/); | 
 | 163 |         } | 
 | 164 |     } | 
 | 165 |  | 
 | 166 |     // Prevent calls to process() and other functions on effect interface from now on. | 
 | 167 |     // The effect engine will be released by the destructor when the last strong reference on | 
 | 168 |     // this object is released which can happen after next process is called. | 
 | 169 |     if (mHandles.size() == 0 && !mPinned) { | 
 | 170 |         mState = DESTROYED; | 
 | 171 |     } | 
 | 172 |  | 
 | 173 |     return mHandles.size(); | 
 | 174 | } | 
 | 175 |  | 
 | 176 | // must be called with EffectModule::mLock held | 
 | 177 | AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l() | 
 | 178 | { | 
 | 179 |     // the first valid handle in the list has control over the module | 
 | 180 |     for (size_t i = 0; i < mHandles.size(); i++) { | 
 | 181 |         EffectHandle *h = mHandles[i]; | 
 | 182 |         if (h != NULL && !h->destroyed_l()) { | 
 | 183 |             return h; | 
 | 184 |         } | 
 | 185 |     } | 
 | 186 |  | 
 | 187 |     return NULL; | 
 | 188 | } | 
 | 189 |  | 
 | 190 | size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast) | 
 | 191 | { | 
 | 192 |     ALOGV("disconnect() %p handle %p", this, handle); | 
 | 193 |     // keep a strong reference on this EffectModule to avoid calling the | 
 | 194 |     // destructor before we exit | 
 | 195 |     sp<EffectModule> keep(this); | 
 | 196 |     { | 
 | 197 |         sp<ThreadBase> thread = mThread.promote(); | 
 | 198 |         if (thread != 0) { | 
 | 199 |             thread->disconnectEffect(keep, handle, unpinIfLast); | 
 | 200 |         } | 
 | 201 |     } | 
 | 202 |     return mHandles.size(); | 
 | 203 | } | 
 | 204 |  | 
 | 205 | void AudioFlinger::EffectModule::updateState() { | 
 | 206 |     Mutex::Autolock _l(mLock); | 
 | 207 |  | 
 | 208 |     switch (mState) { | 
 | 209 |     case RESTART: | 
 | 210 |         reset_l(); | 
 | 211 |         // FALL THROUGH | 
 | 212 |  | 
 | 213 |     case STARTING: | 
 | 214 |         // clear auxiliary effect input buffer for next accumulation | 
 | 215 |         if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 216 |             memset(mConfig.inputCfg.buffer.raw, | 
 | 217 |                    0, | 
 | 218 |                    mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); | 
 | 219 |         } | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 220 |         if (start_l() == NO_ERROR) { | 
 | 221 |             mState = ACTIVE; | 
 | 222 |         } else { | 
 | 223 |             mState = IDLE; | 
 | 224 |         } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 225 |         break; | 
 | 226 |     case STOPPING: | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 227 |         if (stop_l() == NO_ERROR) { | 
 | 228 |             mDisableWaitCnt = mMaxDisableWaitCnt; | 
 | 229 |         } else { | 
 | 230 |             mDisableWaitCnt = 1; // will cause immediate transition to IDLE | 
 | 231 |         } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 232 |         mState = STOPPED; | 
 | 233 |         break; | 
 | 234 |     case STOPPED: | 
 | 235 |         // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the | 
 | 236 |         // turn off sequence. | 
 | 237 |         if (--mDisableWaitCnt == 0) { | 
 | 238 |             reset_l(); | 
 | 239 |             mState = IDLE; | 
 | 240 |         } | 
 | 241 |         break; | 
 | 242 |     default: //IDLE , ACTIVE, DESTROYED | 
 | 243 |         break; | 
 | 244 |     } | 
 | 245 | } | 
 | 246 |  | 
 | 247 | void AudioFlinger::EffectModule::process() | 
 | 248 | { | 
 | 249 |     Mutex::Autolock _l(mLock); | 
 | 250 |  | 
 | 251 |     if (mState == DESTROYED || mEffectInterface == NULL || | 
 | 252 |             mConfig.inputCfg.buffer.raw == NULL || | 
 | 253 |             mConfig.outputCfg.buffer.raw == NULL) { | 
 | 254 |         return; | 
 | 255 |     } | 
 | 256 |  | 
 | 257 |     if (isProcessEnabled()) { | 
 | 258 |         // do 32 bit to 16 bit conversion for auxiliary effect input buffer | 
 | 259 |         if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 260 |             ditherAndClamp(mConfig.inputCfg.buffer.s32, | 
 | 261 |                                         mConfig.inputCfg.buffer.s32, | 
 | 262 |                                         mConfig.inputCfg.buffer.frameCount/2); | 
 | 263 |         } | 
 | 264 |  | 
 | 265 |         // do the actual processing in the effect engine | 
 | 266 |         int ret = (*mEffectInterface)->process(mEffectInterface, | 
 | 267 |                                                &mConfig.inputCfg.buffer, | 
 | 268 |                                                &mConfig.outputCfg.buffer); | 
 | 269 |  | 
 | 270 |         // force transition to IDLE state when engine is ready | 
 | 271 |         if (mState == STOPPED && ret == -ENODATA) { | 
 | 272 |             mDisableWaitCnt = 1; | 
 | 273 |         } | 
 | 274 |  | 
 | 275 |         // clear auxiliary effect input buffer for next accumulation | 
 | 276 |         if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 277 |             memset(mConfig.inputCfg.buffer.raw, 0, | 
 | 278 |                    mConfig.inputCfg.buffer.frameCount*sizeof(int32_t)); | 
 | 279 |         } | 
 | 280 |     } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT && | 
 | 281 |                 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { | 
 | 282 |         // If an insert effect is idle and input buffer is different from output buffer, | 
 | 283 |         // accumulate input onto output | 
 | 284 |         sp<EffectChain> chain = mChain.promote(); | 
 | 285 |         if (chain != 0 && chain->activeTrackCnt() != 0) { | 
 | 286 |             size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2;  //always stereo here | 
 | 287 |             int16_t *in = mConfig.inputCfg.buffer.s16; | 
 | 288 |             int16_t *out = mConfig.outputCfg.buffer.s16; | 
 | 289 |             for (size_t i = 0; i < frameCnt; i++) { | 
 | 290 |                 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]); | 
 | 291 |             } | 
 | 292 |         } | 
 | 293 |     } | 
 | 294 | } | 
 | 295 |  | 
 | 296 | void AudioFlinger::EffectModule::reset_l() | 
 | 297 | { | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 298 |     if (mStatus != NO_ERROR || mEffectInterface == NULL) { | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 299 |         return; | 
 | 300 |     } | 
 | 301 |     (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL); | 
 | 302 | } | 
 | 303 |  | 
 | 304 | status_t AudioFlinger::EffectModule::configure() | 
 | 305 | { | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 306 |     status_t status; | 
 | 307 |     sp<ThreadBase> thread; | 
 | 308 |     uint32_t size; | 
 | 309 |     audio_channel_mask_t channelMask; | 
 | 310 |  | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 311 |     if (mEffectInterface == NULL) { | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 312 |         status = NO_INIT; | 
 | 313 |         goto exit; | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 314 |     } | 
 | 315 |  | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 316 |     thread = mThread.promote(); | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 317 |     if (thread == 0) { | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 318 |         status = DEAD_OBJECT; | 
 | 319 |         goto exit; | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 320 |     } | 
 | 321 |  | 
 | 322 |     // TODO: handle configuration of effects replacing track process | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 323 |     channelMask = thread->channelMask(); | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 324 |  | 
 | 325 |     if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 326 |         mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO; | 
 | 327 |     } else { | 
 | 328 |         mConfig.inputCfg.channels = channelMask; | 
 | 329 |     } | 
 | 330 |     mConfig.outputCfg.channels = channelMask; | 
 | 331 |     mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; | 
 | 332 |     mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; | 
 | 333 |     mConfig.inputCfg.samplingRate = thread->sampleRate(); | 
 | 334 |     mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate; | 
 | 335 |     mConfig.inputCfg.bufferProvider.cookie = NULL; | 
 | 336 |     mConfig.inputCfg.bufferProvider.getBuffer = NULL; | 
 | 337 |     mConfig.inputCfg.bufferProvider.releaseBuffer = NULL; | 
 | 338 |     mConfig.outputCfg.bufferProvider.cookie = NULL; | 
 | 339 |     mConfig.outputCfg.bufferProvider.getBuffer = NULL; | 
 | 340 |     mConfig.outputCfg.bufferProvider.releaseBuffer = NULL; | 
 | 341 |     mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; | 
 | 342 |     // Insert effect: | 
 | 343 |     // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE, | 
 | 344 |     // always overwrites output buffer: input buffer == output buffer | 
 | 345 |     // - in other sessions: | 
 | 346 |     //      last effect in the chain accumulates in output buffer: input buffer != output buffer | 
 | 347 |     //      other effect: overwrites output buffer: input buffer == output buffer | 
 | 348 |     // Auxiliary effect: | 
 | 349 |     //      accumulates in output buffer: input buffer != output buffer | 
 | 350 |     // Therefore: accumulate <=> input buffer != output buffer | 
 | 351 |     if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) { | 
 | 352 |         mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE; | 
 | 353 |     } else { | 
 | 354 |         mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; | 
 | 355 |     } | 
 | 356 |     mConfig.inputCfg.mask = EFFECT_CONFIG_ALL; | 
 | 357 |     mConfig.outputCfg.mask = EFFECT_CONFIG_ALL; | 
 | 358 |     mConfig.inputCfg.buffer.frameCount = thread->frameCount(); | 
 | 359 |     mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount; | 
 | 360 |  | 
 | 361 |     ALOGV("configure() %p thread %p buffer %p framecount %d", | 
 | 362 |             this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount); | 
 | 363 |  | 
 | 364 |     status_t cmdStatus; | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 365 |     size = sizeof(int); | 
 | 366 |     status = (*mEffectInterface)->command(mEffectInterface, | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 367 |                                                    EFFECT_CMD_SET_CONFIG, | 
 | 368 |                                                    sizeof(effect_config_t), | 
 | 369 |                                                    &mConfig, | 
 | 370 |                                                    &size, | 
 | 371 |                                                    &cmdStatus); | 
 | 372 |     if (status == 0) { | 
 | 373 |         status = cmdStatus; | 
 | 374 |     } | 
 | 375 |  | 
 | 376 |     if (status == 0 && | 
 | 377 |             (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) { | 
 | 378 |         uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2]; | 
 | 379 |         effect_param_t *p = (effect_param_t *)buf32; | 
 | 380 |  | 
 | 381 |         p->psize = sizeof(uint32_t); | 
 | 382 |         p->vsize = sizeof(uint32_t); | 
 | 383 |         size = sizeof(int); | 
 | 384 |         *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY; | 
 | 385 |  | 
 | 386 |         uint32_t latency = 0; | 
 | 387 |         PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId); | 
 | 388 |         if (pbt != NULL) { | 
 | 389 |             latency = pbt->latency_l(); | 
 | 390 |         } | 
 | 391 |  | 
 | 392 |         *((int32_t *)p->data + 1)= latency; | 
 | 393 |         (*mEffectInterface)->command(mEffectInterface, | 
 | 394 |                                      EFFECT_CMD_SET_PARAM, | 
 | 395 |                                      sizeof(effect_param_t) + 8, | 
 | 396 |                                      &buf32, | 
 | 397 |                                      &size, | 
 | 398 |                                      &cmdStatus); | 
 | 399 |     } | 
 | 400 |  | 
 | 401 |     mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) / | 
 | 402 |             (1000 * mConfig.outputCfg.buffer.frameCount); | 
 | 403 |  | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 404 | exit: | 
 | 405 |     mStatus = status; | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 406 |     return status; | 
 | 407 | } | 
 | 408 |  | 
 | 409 | status_t AudioFlinger::EffectModule::init() | 
 | 410 | { | 
 | 411 |     Mutex::Autolock _l(mLock); | 
 | 412 |     if (mEffectInterface == NULL) { | 
 | 413 |         return NO_INIT; | 
 | 414 |     } | 
 | 415 |     status_t cmdStatus; | 
 | 416 |     uint32_t size = sizeof(status_t); | 
 | 417 |     status_t status = (*mEffectInterface)->command(mEffectInterface, | 
 | 418 |                                                    EFFECT_CMD_INIT, | 
 | 419 |                                                    0, | 
 | 420 |                                                    NULL, | 
 | 421 |                                                    &size, | 
 | 422 |                                                    &cmdStatus); | 
 | 423 |     if (status == 0) { | 
 | 424 |         status = cmdStatus; | 
 | 425 |     } | 
 | 426 |     return status; | 
 | 427 | } | 
 | 428 |  | 
 | 429 | status_t AudioFlinger::EffectModule::start() | 
 | 430 | { | 
 | 431 |     Mutex::Autolock _l(mLock); | 
 | 432 |     return start_l(); | 
 | 433 | } | 
 | 434 |  | 
 | 435 | status_t AudioFlinger::EffectModule::start_l() | 
 | 436 | { | 
 | 437 |     if (mEffectInterface == NULL) { | 
 | 438 |         return NO_INIT; | 
 | 439 |     } | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 440 |     if (mStatus != NO_ERROR) { | 
 | 441 |         return mStatus; | 
 | 442 |     } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 443 |     status_t cmdStatus; | 
 | 444 |     uint32_t size = sizeof(status_t); | 
 | 445 |     status_t status = (*mEffectInterface)->command(mEffectInterface, | 
 | 446 |                                                    EFFECT_CMD_ENABLE, | 
 | 447 |                                                    0, | 
 | 448 |                                                    NULL, | 
 | 449 |                                                    &size, | 
 | 450 |                                                    &cmdStatus); | 
 | 451 |     if (status == 0) { | 
 | 452 |         status = cmdStatus; | 
 | 453 |     } | 
 | 454 |     if (status == 0 && | 
 | 455 |             ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || | 
 | 456 |              (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) { | 
 | 457 |         sp<ThreadBase> thread = mThread.promote(); | 
 | 458 |         if (thread != 0) { | 
 | 459 |             audio_stream_t *stream = thread->stream(); | 
 | 460 |             if (stream != NULL) { | 
 | 461 |                 stream->add_audio_effect(stream, mEffectInterface); | 
 | 462 |             } | 
 | 463 |         } | 
 | 464 |     } | 
 | 465 |     return status; | 
 | 466 | } | 
 | 467 |  | 
 | 468 | status_t AudioFlinger::EffectModule::stop() | 
 | 469 | { | 
 | 470 |     Mutex::Autolock _l(mLock); | 
 | 471 |     return stop_l(); | 
 | 472 | } | 
 | 473 |  | 
 | 474 | status_t AudioFlinger::EffectModule::stop_l() | 
 | 475 | { | 
 | 476 |     if (mEffectInterface == NULL) { | 
 | 477 |         return NO_INIT; | 
 | 478 |     } | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 479 |     if (mStatus != NO_ERROR) { | 
 | 480 |         return mStatus; | 
 | 481 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 482 |     status_t cmdStatus = NO_ERROR; | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 483 |     uint32_t size = sizeof(status_t); | 
 | 484 |     status_t status = (*mEffectInterface)->command(mEffectInterface, | 
 | 485 |                                                    EFFECT_CMD_DISABLE, | 
 | 486 |                                                    0, | 
 | 487 |                                                    NULL, | 
 | 488 |                                                    &size, | 
 | 489 |                                                    &cmdStatus); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 490 |     if (status == NO_ERROR) { | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 491 |         status = cmdStatus; | 
 | 492 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 493 |     if (status == NO_ERROR) { | 
 | 494 |         status = remove_effect_from_hal_l(); | 
 | 495 |     } | 
 | 496 |     return status; | 
 | 497 | } | 
 | 498 |  | 
 | 499 | status_t AudioFlinger::EffectModule::remove_effect_from_hal_l() | 
 | 500 | { | 
 | 501 |     if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC || | 
 | 502 |              (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) { | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 503 |         sp<ThreadBase> thread = mThread.promote(); | 
 | 504 |         if (thread != 0) { | 
 | 505 |             audio_stream_t *stream = thread->stream(); | 
 | 506 |             if (stream != NULL) { | 
 | 507 |                 stream->remove_audio_effect(stream, mEffectInterface); | 
 | 508 |             } | 
 | 509 |         } | 
 | 510 |     } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 511 |     return NO_ERROR; | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 512 | } | 
 | 513 |  | 
| Andy Hung | e4a1d91 | 2016-08-17 14:11:13 -0700 | [diff] [blame] | 514 | // round up delta valid if value and divisor are positive. | 
 | 515 | template <typename T> | 
 | 516 | static T roundUpDelta(const T &value, const T &divisor) { | 
 | 517 |     T remainder = value % divisor; | 
 | 518 |     return remainder == 0 ? 0 : divisor - remainder; | 
 | 519 | } | 
 | 520 |  | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 521 | status_t AudioFlinger::EffectModule::command(uint32_t cmdCode, | 
 | 522 |                                              uint32_t cmdSize, | 
 | 523 |                                              void *pCmdData, | 
 | 524 |                                              uint32_t *replySize, | 
 | 525 |                                              void *pReplyData) | 
 | 526 | { | 
 | 527 |     Mutex::Autolock _l(mLock); | 
 | 528 |     ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface); | 
 | 529 |  | 
 | 530 |     if (mState == DESTROYED || mEffectInterface == NULL) { | 
 | 531 |         return NO_INIT; | 
 | 532 |     } | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 533 |     if (mStatus != NO_ERROR) { | 
 | 534 |         return mStatus; | 
 | 535 |     } | 
| Andy Hung | 110bc95 | 2016-06-20 15:22:52 -0700 | [diff] [blame] | 536 |     if (cmdCode == EFFECT_CMD_GET_PARAM && | 
 | 537 |             (*replySize < sizeof(effect_param_t) || | 
 | 538 |                     ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) { | 
 | 539 |         android_errorWriteLog(0x534e4554, "29251553"); | 
 | 540 |         return -EINVAL; | 
 | 541 |     } | 
| Andy Hung | e4a1d91 | 2016-08-17 14:11:13 -0700 | [diff] [blame] | 542 |     if ((cmdCode == EFFECT_CMD_SET_PARAM | 
 | 543 |             || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) &&  // DEFERRED not generally used | 
 | 544 |         (sizeof(effect_param_t) > cmdSize | 
 | 545 |             || ((effect_param_t *)pCmdData)->psize > cmdSize | 
 | 546 |                                                      - sizeof(effect_param_t) | 
 | 547 |             || ((effect_param_t *)pCmdData)->vsize > cmdSize | 
 | 548 |                                                      - sizeof(effect_param_t) | 
 | 549 |                                                      - ((effect_param_t *)pCmdData)->psize | 
 | 550 |             || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) > | 
 | 551 |                                                      cmdSize | 
 | 552 |                                                      - sizeof(effect_param_t) | 
 | 553 |                                                      - ((effect_param_t *)pCmdData)->psize | 
 | 554 |                                                      - ((effect_param_t *)pCmdData)->vsize)) { | 
 | 555 |         android_errorWriteLog(0x534e4554, "30204301"); | 
 | 556 |         return -EINVAL; | 
 | 557 |     } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 558 |     status_t status = (*mEffectInterface)->command(mEffectInterface, | 
 | 559 |                                                    cmdCode, | 
 | 560 |                                                    cmdSize, | 
 | 561 |                                                    pCmdData, | 
 | 562 |                                                    replySize, | 
 | 563 |                                                    pReplyData); | 
 | 564 |     if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) { | 
 | 565 |         uint32_t size = (replySize == NULL) ? 0 : *replySize; | 
 | 566 |         for (size_t i = 1; i < mHandles.size(); i++) { | 
 | 567 |             EffectHandle *h = mHandles[i]; | 
 | 568 |             if (h != NULL && !h->destroyed_l()) { | 
 | 569 |                 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData); | 
 | 570 |             } | 
 | 571 |         } | 
 | 572 |     } | 
 | 573 |     return status; | 
 | 574 | } | 
 | 575 |  | 
 | 576 | status_t AudioFlinger::EffectModule::setEnabled(bool enabled) | 
 | 577 | { | 
 | 578 |     Mutex::Autolock _l(mLock); | 
 | 579 |     return setEnabled_l(enabled); | 
 | 580 | } | 
 | 581 |  | 
 | 582 | // must be called with EffectModule::mLock held | 
 | 583 | status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled) | 
 | 584 | { | 
 | 585 |  | 
 | 586 |     ALOGV("setEnabled %p enabled %d", this, enabled); | 
 | 587 |  | 
 | 588 |     if (enabled != isEnabled()) { | 
 | 589 |         status_t status = AudioSystem::setEffectEnabled(mId, enabled); | 
 | 590 |         if (enabled && status != NO_ERROR) { | 
 | 591 |             return status; | 
 | 592 |         } | 
 | 593 |  | 
 | 594 |         switch (mState) { | 
 | 595 |         // going from disabled to enabled | 
 | 596 |         case IDLE: | 
 | 597 |             mState = STARTING; | 
 | 598 |             break; | 
 | 599 |         case STOPPED: | 
 | 600 |             mState = RESTART; | 
 | 601 |             break; | 
 | 602 |         case STOPPING: | 
 | 603 |             mState = ACTIVE; | 
 | 604 |             break; | 
 | 605 |  | 
 | 606 |         // going from enabled to disabled | 
 | 607 |         case RESTART: | 
 | 608 |             mState = STOPPED; | 
 | 609 |             break; | 
 | 610 |         case STARTING: | 
 | 611 |             mState = IDLE; | 
 | 612 |             break; | 
 | 613 |         case ACTIVE: | 
 | 614 |             mState = STOPPING; | 
 | 615 |             break; | 
 | 616 |         case DESTROYED: | 
 | 617 |             return NO_ERROR; // simply ignore as we are being destroyed | 
 | 618 |         } | 
 | 619 |         for (size_t i = 1; i < mHandles.size(); i++) { | 
 | 620 |             EffectHandle *h = mHandles[i]; | 
 | 621 |             if (h != NULL && !h->destroyed_l()) { | 
 | 622 |                 h->setEnabled(enabled); | 
 | 623 |             } | 
 | 624 |         } | 
 | 625 |     } | 
 | 626 |     return NO_ERROR; | 
 | 627 | } | 
 | 628 |  | 
 | 629 | bool AudioFlinger::EffectModule::isEnabled() const | 
 | 630 | { | 
 | 631 |     switch (mState) { | 
 | 632 |     case RESTART: | 
 | 633 |     case STARTING: | 
 | 634 |     case ACTIVE: | 
 | 635 |         return true; | 
 | 636 |     case IDLE: | 
 | 637 |     case STOPPING: | 
 | 638 |     case STOPPED: | 
 | 639 |     case DESTROYED: | 
 | 640 |     default: | 
 | 641 |         return false; | 
 | 642 |     } | 
 | 643 | } | 
 | 644 |  | 
 | 645 | bool AudioFlinger::EffectModule::isProcessEnabled() const | 
 | 646 | { | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 647 |     if (mStatus != NO_ERROR) { | 
 | 648 |         return false; | 
 | 649 |     } | 
 | 650 |  | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 651 |     switch (mState) { | 
 | 652 |     case RESTART: | 
 | 653 |     case ACTIVE: | 
 | 654 |     case STOPPING: | 
 | 655 |     case STOPPED: | 
 | 656 |         return true; | 
 | 657 |     case IDLE: | 
 | 658 |     case STARTING: | 
 | 659 |     case DESTROYED: | 
 | 660 |     default: | 
 | 661 |         return false; | 
 | 662 |     } | 
 | 663 | } | 
 | 664 |  | 
 | 665 | status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller) | 
 | 666 | { | 
 | 667 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 668 |     if (mStatus != NO_ERROR) { | 
 | 669 |         return mStatus; | 
 | 670 |     } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 671 |     status_t status = NO_ERROR; | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 672 |     // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume | 
 | 673 |     // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set) | 
 | 674 |     if (isProcessEnabled() && | 
 | 675 |             ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL || | 
 | 676 |             (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) { | 
 | 677 |         status_t cmdStatus; | 
 | 678 |         uint32_t volume[2]; | 
 | 679 |         uint32_t *pVolume = NULL; | 
 | 680 |         uint32_t size = sizeof(volume); | 
 | 681 |         volume[0] = *left; | 
 | 682 |         volume[1] = *right; | 
 | 683 |         if (controller) { | 
 | 684 |             pVolume = volume; | 
 | 685 |         } | 
 | 686 |         status = (*mEffectInterface)->command(mEffectInterface, | 
 | 687 |                                               EFFECT_CMD_SET_VOLUME, | 
 | 688 |                                               size, | 
 | 689 |                                               volume, | 
 | 690 |                                               &size, | 
 | 691 |                                               pVolume); | 
 | 692 |         if (controller && status == NO_ERROR && size == sizeof(volume)) { | 
 | 693 |             *left = volume[0]; | 
 | 694 |             *right = volume[1]; | 
 | 695 |         } | 
 | 696 |     } | 
 | 697 |     return status; | 
 | 698 | } | 
 | 699 |  | 
 | 700 | status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device) | 
 | 701 | { | 
 | 702 |     if (device == AUDIO_DEVICE_NONE) { | 
 | 703 |         return NO_ERROR; | 
 | 704 |     } | 
 | 705 |  | 
 | 706 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 707 |     if (mStatus != NO_ERROR) { | 
 | 708 |         return mStatus; | 
 | 709 |     } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 710 |     status_t status = NO_ERROR; | 
| Eric Laurent | 7e1139c | 2013-06-06 18:29:01 -0700 | [diff] [blame] | 711 |     if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) { | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 712 |         status_t cmdStatus; | 
 | 713 |         uint32_t size = sizeof(status_t); | 
 | 714 |         uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE : | 
 | 715 |                             EFFECT_CMD_SET_INPUT_DEVICE; | 
 | 716 |         status = (*mEffectInterface)->command(mEffectInterface, | 
 | 717 |                                               cmd, | 
 | 718 |                                               sizeof(uint32_t), | 
 | 719 |                                               &device, | 
 | 720 |                                               &size, | 
 | 721 |                                               &cmdStatus); | 
 | 722 |     } | 
 | 723 |     return status; | 
 | 724 | } | 
 | 725 |  | 
 | 726 | status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode) | 
 | 727 | { | 
 | 728 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 729 |     if (mStatus != NO_ERROR) { | 
 | 730 |         return mStatus; | 
 | 731 |     } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 732 |     status_t status = NO_ERROR; | 
 | 733 |     if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) { | 
 | 734 |         status_t cmdStatus; | 
 | 735 |         uint32_t size = sizeof(status_t); | 
 | 736 |         status = (*mEffectInterface)->command(mEffectInterface, | 
 | 737 |                                               EFFECT_CMD_SET_AUDIO_MODE, | 
 | 738 |                                               sizeof(audio_mode_t), | 
 | 739 |                                               &mode, | 
 | 740 |                                               &size, | 
 | 741 |                                               &cmdStatus); | 
 | 742 |         if (status == NO_ERROR) { | 
 | 743 |             status = cmdStatus; | 
 | 744 |         } | 
 | 745 |     } | 
 | 746 |     return status; | 
 | 747 | } | 
 | 748 |  | 
 | 749 | status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source) | 
 | 750 | { | 
 | 751 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | d0ebb53 | 2013-04-02 16:41:41 -0700 | [diff] [blame] | 752 |     if (mStatus != NO_ERROR) { | 
 | 753 |         return mStatus; | 
 | 754 |     } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 755 |     status_t status = NO_ERROR; | 
 | 756 |     if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) { | 
 | 757 |         uint32_t size = 0; | 
 | 758 |         status = (*mEffectInterface)->command(mEffectInterface, | 
 | 759 |                                               EFFECT_CMD_SET_AUDIO_SOURCE, | 
 | 760 |                                               sizeof(audio_source_t), | 
 | 761 |                                               &source, | 
 | 762 |                                               &size, | 
 | 763 |                                               NULL); | 
 | 764 |     } | 
 | 765 |     return status; | 
 | 766 | } | 
 | 767 |  | 
 | 768 | void AudioFlinger::EffectModule::setSuspended(bool suspended) | 
 | 769 | { | 
 | 770 |     Mutex::Autolock _l(mLock); | 
 | 771 |     mSuspended = suspended; | 
 | 772 | } | 
 | 773 |  | 
 | 774 | bool AudioFlinger::EffectModule::suspended() const | 
 | 775 | { | 
 | 776 |     Mutex::Autolock _l(mLock); | 
 | 777 |     return mSuspended; | 
 | 778 | } | 
 | 779 |  | 
 | 780 | bool AudioFlinger::EffectModule::purgeHandles() | 
 | 781 | { | 
 | 782 |     bool enabled = false; | 
 | 783 |     Mutex::Autolock _l(mLock); | 
 | 784 |     for (size_t i = 0; i < mHandles.size(); i++) { | 
 | 785 |         EffectHandle *handle = mHandles[i]; | 
 | 786 |         if (handle != NULL && !handle->destroyed_l()) { | 
 | 787 |             handle->effect().clear(); | 
 | 788 |             if (handle->hasControl()) { | 
 | 789 |                 enabled = handle->enabled(); | 
 | 790 |             } | 
 | 791 |         } | 
 | 792 |     } | 
 | 793 |     return enabled; | 
 | 794 | } | 
 | 795 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 796 | status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io) | 
 | 797 | { | 
 | 798 |     Mutex::Autolock _l(mLock); | 
 | 799 |     if (mStatus != NO_ERROR) { | 
 | 800 |         return mStatus; | 
 | 801 |     } | 
 | 802 |     status_t status = NO_ERROR; | 
 | 803 |     if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) { | 
 | 804 |         status_t cmdStatus; | 
 | 805 |         uint32_t size = sizeof(status_t); | 
 | 806 |         effect_offload_param_t cmd; | 
 | 807 |  | 
 | 808 |         cmd.isOffload = offloaded; | 
 | 809 |         cmd.ioHandle = io; | 
 | 810 |         status = (*mEffectInterface)->command(mEffectInterface, | 
 | 811 |                                               EFFECT_CMD_OFFLOAD, | 
 | 812 |                                               sizeof(effect_offload_param_t), | 
 | 813 |                                               &cmd, | 
 | 814 |                                               &size, | 
 | 815 |                                               &cmdStatus); | 
 | 816 |         if (status == NO_ERROR) { | 
 | 817 |             status = cmdStatus; | 
 | 818 |         } | 
 | 819 |         mOffloaded = (status == NO_ERROR) ? offloaded : false; | 
 | 820 |     } else { | 
 | 821 |         if (offloaded) { | 
 | 822 |             status = INVALID_OPERATION; | 
 | 823 |         } | 
 | 824 |         mOffloaded = false; | 
 | 825 |     } | 
 | 826 |     ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status); | 
 | 827 |     return status; | 
 | 828 | } | 
 | 829 |  | 
 | 830 | bool AudioFlinger::EffectModule::isOffloaded() const | 
 | 831 | { | 
 | 832 |     Mutex::Autolock _l(mLock); | 
 | 833 |     return mOffloaded; | 
 | 834 | } | 
 | 835 |  | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 836 | void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args) | 
 | 837 | { | 
 | 838 |     const size_t SIZE = 256; | 
 | 839 |     char buffer[SIZE]; | 
 | 840 |     String8 result; | 
 | 841 |  | 
 | 842 |     snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId); | 
 | 843 |     result.append(buffer); | 
 | 844 |  | 
 | 845 |     bool locked = AudioFlinger::dumpTryLock(mLock); | 
 | 846 |     // failed to lock - AudioFlinger is probably deadlocked | 
 | 847 |     if (!locked) { | 
 | 848 |         result.append("\t\tCould not lock Fx mutex:\n"); | 
 | 849 |     } | 
 | 850 |  | 
 | 851 |     result.append("\t\tSession Status State Engine:\n"); | 
 | 852 |     snprintf(buffer, SIZE, "\t\t%05d   %03d    %03d   0x%08x\n", | 
 | 853 |             mSessionId, mStatus, mState, (uint32_t)mEffectInterface); | 
 | 854 |     result.append(buffer); | 
 | 855 |  | 
 | 856 |     result.append("\t\tDescriptor:\n"); | 
 | 857 |     snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", | 
 | 858 |             mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion, | 
 | 859 |             mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1], | 
 | 860 |                     mDescriptor.uuid.node[2], | 
 | 861 |             mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]); | 
 | 862 |     result.append(buffer); | 
 | 863 |     snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n", | 
 | 864 |                 mDescriptor.type.timeLow, mDescriptor.type.timeMid, | 
 | 865 |                     mDescriptor.type.timeHiAndVersion, | 
 | 866 |                 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1], | 
 | 867 |                     mDescriptor.type.node[2], | 
 | 868 |                 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]); | 
 | 869 |     result.append(buffer); | 
 | 870 |     snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n", | 
 | 871 |             mDescriptor.apiVersion, | 
 | 872 |             mDescriptor.flags); | 
 | 873 |     result.append(buffer); | 
 | 874 |     snprintf(buffer, SIZE, "\t\t- name: %s\n", | 
 | 875 |             mDescriptor.name); | 
 | 876 |     result.append(buffer); | 
 | 877 |     snprintf(buffer, SIZE, "\t\t- implementor: %s\n", | 
 | 878 |             mDescriptor.implementor); | 
 | 879 |     result.append(buffer); | 
 | 880 |  | 
 | 881 |     result.append("\t\t- Input configuration:\n"); | 
 | 882 |     result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n"); | 
 | 883 |     snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n", | 
 | 884 |             (uint32_t)mConfig.inputCfg.buffer.raw, | 
 | 885 |             mConfig.inputCfg.buffer.frameCount, | 
 | 886 |             mConfig.inputCfg.samplingRate, | 
 | 887 |             mConfig.inputCfg.channels, | 
 | 888 |             mConfig.inputCfg.format); | 
 | 889 |     result.append(buffer); | 
 | 890 |  | 
 | 891 |     result.append("\t\t- Output configuration:\n"); | 
 | 892 |     result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n"); | 
 | 893 |     snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n", | 
 | 894 |             (uint32_t)mConfig.outputCfg.buffer.raw, | 
 | 895 |             mConfig.outputCfg.buffer.frameCount, | 
 | 896 |             mConfig.outputCfg.samplingRate, | 
 | 897 |             mConfig.outputCfg.channels, | 
 | 898 |             mConfig.outputCfg.format); | 
 | 899 |     result.append(buffer); | 
 | 900 |  | 
 | 901 |     snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size()); | 
 | 902 |     result.append(buffer); | 
 | 903 |     result.append("\t\t\tPid   Priority Ctrl Locked client server\n"); | 
 | 904 |     for (size_t i = 0; i < mHandles.size(); ++i) { | 
 | 905 |         EffectHandle *handle = mHandles[i]; | 
 | 906 |         if (handle != NULL && !handle->destroyed_l()) { | 
 | 907 |             handle->dump(buffer, SIZE); | 
 | 908 |             result.append(buffer); | 
 | 909 |         } | 
 | 910 |     } | 
 | 911 |  | 
 | 912 |     result.append("\n"); | 
 | 913 |  | 
 | 914 |     write(fd, result.string(), result.length()); | 
 | 915 |  | 
 | 916 |     if (locked) { | 
 | 917 |         mLock.unlock(); | 
 | 918 |     } | 
 | 919 | } | 
 | 920 |  | 
 | 921 | // ---------------------------------------------------------------------------- | 
 | 922 | //  EffectHandle implementation | 
 | 923 | // ---------------------------------------------------------------------------- | 
 | 924 |  | 
 | 925 | #undef LOG_TAG | 
 | 926 | #define LOG_TAG "AudioFlinger::EffectHandle" | 
 | 927 |  | 
 | 928 | AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect, | 
 | 929 |                                         const sp<AudioFlinger::Client>& client, | 
 | 930 |                                         const sp<IEffectClient>& effectClient, | 
 | 931 |                                         int32_t priority) | 
 | 932 |     : BnEffect(), | 
 | 933 |     mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL), | 
 | 934 |     mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false) | 
 | 935 | { | 
 | 936 |     ALOGV("constructor %p", this); | 
 | 937 |  | 
 | 938 |     if (client == 0) { | 
 | 939 |         return; | 
 | 940 |     } | 
 | 941 |     int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int); | 
 | 942 |     mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset); | 
 | 943 |     if (mCblkMemory != 0) { | 
 | 944 |         mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer()); | 
 | 945 |  | 
 | 946 |         if (mCblk != NULL) { | 
 | 947 |             new(mCblk) effect_param_cblk_t(); | 
 | 948 |             mBuffer = (uint8_t *)mCblk + bufOffset; | 
 | 949 |         } | 
 | 950 |     } else { | 
 | 951 |         ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + | 
 | 952 |                 sizeof(effect_param_cblk_t)); | 
 | 953 |         return; | 
 | 954 |     } | 
 | 955 | } | 
 | 956 |  | 
 | 957 | AudioFlinger::EffectHandle::~EffectHandle() | 
 | 958 | { | 
 | 959 |     ALOGV("Destructor %p", this); | 
 | 960 |  | 
 | 961 |     if (mEffect == 0) { | 
 | 962 |         mDestroyed = true; | 
 | 963 |         return; | 
 | 964 |     } | 
 | 965 |     mEffect->lock(); | 
 | 966 |     mDestroyed = true; | 
 | 967 |     mEffect->unlock(); | 
 | 968 |     disconnect(false); | 
 | 969 | } | 
 | 970 |  | 
 | 971 | status_t AudioFlinger::EffectHandle::enable() | 
 | 972 | { | 
 | 973 |     ALOGV("enable %p", this); | 
 | 974 |     if (!mHasControl) { | 
 | 975 |         return INVALID_OPERATION; | 
 | 976 |     } | 
 | 977 |     if (mEffect == 0) { | 
 | 978 |         return DEAD_OBJECT; | 
 | 979 |     } | 
 | 980 |  | 
 | 981 |     if (mEnabled) { | 
 | 982 |         return NO_ERROR; | 
 | 983 |     } | 
 | 984 |  | 
 | 985 |     mEnabled = true; | 
 | 986 |  | 
 | 987 |     sp<ThreadBase> thread = mEffect->thread().promote(); | 
 | 988 |     if (thread != 0) { | 
 | 989 |         thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId()); | 
 | 990 |     } | 
 | 991 |  | 
 | 992 |     // checkSuspendOnEffectEnabled() can suspend this same effect when enabled | 
 | 993 |     if (mEffect->suspended()) { | 
 | 994 |         return NO_ERROR; | 
 | 995 |     } | 
 | 996 |  | 
 | 997 |     status_t status = mEffect->setEnabled(true); | 
 | 998 |     if (status != NO_ERROR) { | 
 | 999 |         if (thread != 0) { | 
 | 1000 |             thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
 | 1001 |         } | 
 | 1002 |         mEnabled = false; | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1003 |     } else { | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1004 |         if (thread != 0) { | 
 | 1005 |             if (thread->type() == ThreadBase::OFFLOAD) { | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1006 |                 PlaybackThread *t = (PlaybackThread *)thread.get(); | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1007 |                 Mutex::Autolock _l(t->mLock); | 
 | 1008 |                 t->broadcast_l(); | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1009 |             } | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1010 |             if (!mEffect->isOffloadable()) { | 
 | 1011 |                 if (thread->type() == ThreadBase::OFFLOAD) { | 
 | 1012 |                     PlaybackThread *t = (PlaybackThread *)thread.get(); | 
 | 1013 |                     t->invalidateTracks(AUDIO_STREAM_MUSIC); | 
 | 1014 |                 } | 
 | 1015 |                 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) { | 
 | 1016 |                     thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable(); | 
 | 1017 |                 } | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1018 |             } | 
 | 1019 |         } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1020 |     } | 
 | 1021 |     return status; | 
 | 1022 | } | 
 | 1023 |  | 
 | 1024 | status_t AudioFlinger::EffectHandle::disable() | 
 | 1025 | { | 
 | 1026 |     ALOGV("disable %p", this); | 
 | 1027 |     if (!mHasControl) { | 
 | 1028 |         return INVALID_OPERATION; | 
 | 1029 |     } | 
 | 1030 |     if (mEffect == 0) { | 
 | 1031 |         return DEAD_OBJECT; | 
 | 1032 |     } | 
 | 1033 |  | 
 | 1034 |     if (!mEnabled) { | 
 | 1035 |         return NO_ERROR; | 
 | 1036 |     } | 
 | 1037 |     mEnabled = false; | 
 | 1038 |  | 
 | 1039 |     if (mEffect->suspended()) { | 
 | 1040 |         return NO_ERROR; | 
 | 1041 |     } | 
 | 1042 |  | 
 | 1043 |     status_t status = mEffect->setEnabled(false); | 
 | 1044 |  | 
 | 1045 |     sp<ThreadBase> thread = mEffect->thread().promote(); | 
 | 1046 |     if (thread != 0) { | 
 | 1047 |         thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
| Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1048 |         if (thread->type() == ThreadBase::OFFLOAD) { | 
 | 1049 |             PlaybackThread *t = (PlaybackThread *)thread.get(); | 
 | 1050 |             Mutex::Autolock _l(t->mLock); | 
 | 1051 |             t->broadcast_l(); | 
 | 1052 |         } | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1053 |     } | 
 | 1054 |  | 
 | 1055 |     return status; | 
 | 1056 | } | 
 | 1057 |  | 
 | 1058 | void AudioFlinger::EffectHandle::disconnect() | 
 | 1059 | { | 
 | 1060 |     disconnect(true); | 
 | 1061 | } | 
 | 1062 |  | 
 | 1063 | void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast) | 
 | 1064 | { | 
 | 1065 |     ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false"); | 
 | 1066 |     if (mEffect == 0) { | 
 | 1067 |         return; | 
 | 1068 |     } | 
 | 1069 |     // restore suspended effects if the disconnected handle was enabled and the last one. | 
 | 1070 |     if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) { | 
 | 1071 |         sp<ThreadBase> thread = mEffect->thread().promote(); | 
 | 1072 |         if (thread != 0) { | 
 | 1073 |             thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId()); | 
 | 1074 |         } | 
 | 1075 |     } | 
 | 1076 |  | 
 | 1077 |     // release sp on module => module destructor can be called now | 
 | 1078 |     mEffect.clear(); | 
 | 1079 |     if (mClient != 0) { | 
 | 1080 |         if (mCblk != NULL) { | 
 | 1081 |             // unlike ~TrackBase(), mCblk is never a local new, so don't delete | 
 | 1082 |             mCblk->~effect_param_cblk_t();   // destroy our shared-structure. | 
 | 1083 |         } | 
 | 1084 |         mCblkMemory.clear();    // free the shared memory before releasing the heap it belongs to | 
 | 1085 |         // Client destructor must run with AudioFlinger mutex locked | 
 | 1086 |         Mutex::Autolock _l(mClient->audioFlinger()->mLock); | 
 | 1087 |         mClient.clear(); | 
 | 1088 |     } | 
 | 1089 | } | 
 | 1090 |  | 
 | 1091 | status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode, | 
 | 1092 |                                              uint32_t cmdSize, | 
 | 1093 |                                              void *pCmdData, | 
 | 1094 |                                              uint32_t *replySize, | 
 | 1095 |                                              void *pReplyData) | 
 | 1096 | { | 
 | 1097 |     ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p", | 
 | 1098 |             cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get()); | 
 | 1099 |  | 
 | 1100 |     // only get parameter command is permitted for applications not controlling the effect | 
 | 1101 |     if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) { | 
 | 1102 |         return INVALID_OPERATION; | 
 | 1103 |     } | 
 | 1104 |     if (mEffect == 0) { | 
 | 1105 |         return DEAD_OBJECT; | 
 | 1106 |     } | 
 | 1107 |     if (mClient == 0) { | 
 | 1108 |         return INVALID_OPERATION; | 
 | 1109 |     } | 
 | 1110 |  | 
 | 1111 |     // handle commands that are not forwarded transparently to effect engine | 
 | 1112 |     if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) { | 
 | 1113 |         // No need to trylock() here as this function is executed in the binder thread serving a | 
 | 1114 |         // particular client process:  no risk to block the whole media server process or mixer | 
 | 1115 |         // threads if we are stuck here | 
 | 1116 |         Mutex::Autolock _l(mCblk->lock); | 
 | 1117 |         if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE || | 
 | 1118 |             mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) { | 
 | 1119 |             mCblk->serverIndex = 0; | 
 | 1120 |             mCblk->clientIndex = 0; | 
 | 1121 |             return BAD_VALUE; | 
 | 1122 |         } | 
 | 1123 |         status_t status = NO_ERROR; | 
 | 1124 |         while (mCblk->serverIndex < mCblk->clientIndex) { | 
 | 1125 |             int reply; | 
 | 1126 |             uint32_t rsize = sizeof(int); | 
 | 1127 |             int *p = (int *)(mBuffer + mCblk->serverIndex); | 
 | 1128 |             int size = *p++; | 
 | 1129 |             if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) { | 
 | 1130 |                 ALOGW("command(): invalid parameter block size"); | 
 | 1131 |                 break; | 
 | 1132 |             } | 
 | 1133 |             effect_param_t *param = (effect_param_t *)p; | 
 | 1134 |             if (param->psize == 0 || param->vsize == 0) { | 
 | 1135 |                 ALOGW("command(): null parameter or value size"); | 
 | 1136 |                 mCblk->serverIndex += size; | 
 | 1137 |                 continue; | 
 | 1138 |             } | 
 | 1139 |             uint32_t psize = sizeof(effect_param_t) + | 
 | 1140 |                              ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + | 
 | 1141 |                              param->vsize; | 
 | 1142 |             status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM, | 
 | 1143 |                                             psize, | 
 | 1144 |                                             p, | 
 | 1145 |                                             &rsize, | 
 | 1146 |                                             &reply); | 
 | 1147 |             // stop at first error encountered | 
 | 1148 |             if (ret != NO_ERROR) { | 
 | 1149 |                 status = ret; | 
 | 1150 |                 *(int *)pReplyData = reply; | 
 | 1151 |                 break; | 
 | 1152 |             } else if (reply != NO_ERROR) { | 
 | 1153 |                 *(int *)pReplyData = reply; | 
 | 1154 |                 break; | 
 | 1155 |             } | 
 | 1156 |             mCblk->serverIndex += size; | 
 | 1157 |         } | 
 | 1158 |         mCblk->serverIndex = 0; | 
 | 1159 |         mCblk->clientIndex = 0; | 
 | 1160 |         return status; | 
 | 1161 |     } else if (cmdCode == EFFECT_CMD_ENABLE) { | 
 | 1162 |         *(int *)pReplyData = NO_ERROR; | 
 | 1163 |         return enable(); | 
 | 1164 |     } else if (cmdCode == EFFECT_CMD_DISABLE) { | 
 | 1165 |         *(int *)pReplyData = NO_ERROR; | 
 | 1166 |         return disable(); | 
 | 1167 |     } | 
 | 1168 |  | 
 | 1169 |     return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData); | 
 | 1170 | } | 
 | 1171 |  | 
 | 1172 | void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled) | 
 | 1173 | { | 
 | 1174 |     ALOGV("setControl %p control %d", this, hasControl); | 
 | 1175 |  | 
 | 1176 |     mHasControl = hasControl; | 
 | 1177 |     mEnabled = enabled; | 
 | 1178 |  | 
 | 1179 |     if (signal && mEffectClient != 0) { | 
 | 1180 |         mEffectClient->controlStatusChanged(hasControl); | 
 | 1181 |     } | 
 | 1182 | } | 
 | 1183 |  | 
 | 1184 | void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode, | 
 | 1185 |                                                  uint32_t cmdSize, | 
 | 1186 |                                                  void *pCmdData, | 
 | 1187 |                                                  uint32_t replySize, | 
 | 1188 |                                                  void *pReplyData) | 
 | 1189 | { | 
 | 1190 |     if (mEffectClient != 0) { | 
 | 1191 |         mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData); | 
 | 1192 |     } | 
 | 1193 | } | 
 | 1194 |  | 
 | 1195 |  | 
 | 1196 |  | 
 | 1197 | void AudioFlinger::EffectHandle::setEnabled(bool enabled) | 
 | 1198 | { | 
 | 1199 |     if (mEffectClient != 0) { | 
 | 1200 |         mEffectClient->enableStatusChanged(enabled); | 
 | 1201 |     } | 
 | 1202 | } | 
 | 1203 |  | 
 | 1204 | status_t AudioFlinger::EffectHandle::onTransact( | 
 | 1205 |     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
 | 1206 | { | 
 | 1207 |     return BnEffect::onTransact(code, data, reply, flags); | 
 | 1208 | } | 
 | 1209 |  | 
 | 1210 |  | 
 | 1211 | void AudioFlinger::EffectHandle::dump(char* buffer, size_t size) | 
 | 1212 | { | 
 | 1213 |     bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock); | 
 | 1214 |  | 
 | 1215 |     snprintf(buffer, size, "\t\t\t%05d %05d    %01u    %01u      %05u  %05u\n", | 
 | 1216 |             (mClient == 0) ? getpid_cached : mClient->pid(), | 
 | 1217 |             mPriority, | 
 | 1218 |             mHasControl, | 
 | 1219 |             !locked, | 
 | 1220 |             mCblk ? mCblk->clientIndex : 0, | 
 | 1221 |             mCblk ? mCblk->serverIndex : 0 | 
 | 1222 |             ); | 
 | 1223 |  | 
 | 1224 |     if (locked) { | 
 | 1225 |         mCblk->lock.unlock(); | 
 | 1226 |     } | 
 | 1227 | } | 
 | 1228 |  | 
 | 1229 | #undef LOG_TAG | 
 | 1230 | #define LOG_TAG "AudioFlinger::EffectChain" | 
 | 1231 |  | 
 | 1232 | AudioFlinger::EffectChain::EffectChain(ThreadBase *thread, | 
 | 1233 |                                         int sessionId) | 
 | 1234 |     : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0), | 
 | 1235 |       mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX), | 
 | 1236 |       mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX) | 
 | 1237 | { | 
 | 1238 |     mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC); | 
 | 1239 |     if (thread == NULL) { | 
 | 1240 |         return; | 
 | 1241 |     } | 
 | 1242 |     mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) / | 
 | 1243 |                                     thread->frameCount(); | 
 | 1244 | } | 
 | 1245 |  | 
 | 1246 | AudioFlinger::EffectChain::~EffectChain() | 
 | 1247 | { | 
 | 1248 |     if (mOwnInBuffer) { | 
 | 1249 |         delete mInBuffer; | 
 | 1250 |     } | 
 | 1251 |  | 
 | 1252 | } | 
 | 1253 |  | 
 | 1254 | // getEffectFromDesc_l() must be called with ThreadBase::mLock held | 
 | 1255 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l( | 
 | 1256 |         effect_descriptor_t *descriptor) | 
 | 1257 | { | 
 | 1258 |     size_t size = mEffects.size(); | 
 | 1259 |  | 
 | 1260 |     for (size_t i = 0; i < size; i++) { | 
 | 1261 |         if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) { | 
 | 1262 |             return mEffects[i]; | 
 | 1263 |         } | 
 | 1264 |     } | 
 | 1265 |     return 0; | 
 | 1266 | } | 
 | 1267 |  | 
 | 1268 | // getEffectFromId_l() must be called with ThreadBase::mLock held | 
 | 1269 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id) | 
 | 1270 | { | 
 | 1271 |     size_t size = mEffects.size(); | 
 | 1272 |  | 
 | 1273 |     for (size_t i = 0; i < size; i++) { | 
 | 1274 |         // by convention, return first effect if id provided is 0 (0 is never a valid id) | 
 | 1275 |         if (id == 0 || mEffects[i]->id() == id) { | 
 | 1276 |             return mEffects[i]; | 
 | 1277 |         } | 
 | 1278 |     } | 
 | 1279 |     return 0; | 
 | 1280 | } | 
 | 1281 |  | 
 | 1282 | // getEffectFromType_l() must be called with ThreadBase::mLock held | 
 | 1283 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l( | 
 | 1284 |         const effect_uuid_t *type) | 
 | 1285 | { | 
 | 1286 |     size_t size = mEffects.size(); | 
 | 1287 |  | 
 | 1288 |     for (size_t i = 0; i < size; i++) { | 
 | 1289 |         if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) { | 
 | 1290 |             return mEffects[i]; | 
 | 1291 |         } | 
 | 1292 |     } | 
 | 1293 |     return 0; | 
 | 1294 | } | 
 | 1295 |  | 
 | 1296 | void AudioFlinger::EffectChain::clearInputBuffer() | 
 | 1297 | { | 
 | 1298 |     Mutex::Autolock _l(mLock); | 
 | 1299 |     sp<ThreadBase> thread = mThread.promote(); | 
 | 1300 |     if (thread == 0) { | 
 | 1301 |         ALOGW("clearInputBuffer(): cannot promote mixer thread"); | 
 | 1302 |         return; | 
 | 1303 |     } | 
 | 1304 |     clearInputBuffer_l(thread); | 
 | 1305 | } | 
 | 1306 |  | 
 | 1307 | // Must be called with EffectChain::mLock locked | 
 | 1308 | void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread) | 
 | 1309 | { | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1310 |     memset(mInBuffer, 0, thread->frameCount() * thread->frameSize()); | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1311 | } | 
 | 1312 |  | 
 | 1313 | // Must be called with EffectChain::mLock locked | 
 | 1314 | void AudioFlinger::EffectChain::process_l() | 
 | 1315 | { | 
 | 1316 |     sp<ThreadBase> thread = mThread.promote(); | 
 | 1317 |     if (thread == 0) { | 
 | 1318 |         ALOGW("process_l(): cannot promote mixer thread"); | 
 | 1319 |         return; | 
 | 1320 |     } | 
 | 1321 |     bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) || | 
 | 1322 |             (mSessionId == AUDIO_SESSION_OUTPUT_STAGE); | 
| Jean-Michel Trivi | fed6292 | 2013-09-25 18:50:33 -0700 | [diff] [blame] | 1323 |     // never process effects when: | 
 | 1324 |     // - on an OFFLOAD thread | 
 | 1325 |     // - no more tracks are on the session and the effect tail has been rendered | 
 | 1326 |     bool doProcess = (thread->type() != ThreadBase::OFFLOAD); | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1327 |     if (!isGlobalSession) { | 
 | 1328 |         bool tracksOnSession = (trackCnt() != 0); | 
 | 1329 |  | 
 | 1330 |         if (!tracksOnSession && mTailBufferCount == 0) { | 
 | 1331 |             doProcess = false; | 
 | 1332 |         } | 
 | 1333 |  | 
 | 1334 |         if (activeTrackCnt() == 0) { | 
 | 1335 |             // if no track is active and the effect tail has not been rendered, | 
 | 1336 |             // the input buffer must be cleared here as the mixer process will not do it | 
 | 1337 |             if (tracksOnSession || mTailBufferCount > 0) { | 
 | 1338 |                 clearInputBuffer_l(thread); | 
 | 1339 |                 if (mTailBufferCount > 0) { | 
 | 1340 |                     mTailBufferCount--; | 
 | 1341 |                 } | 
 | 1342 |             } | 
 | 1343 |         } | 
 | 1344 |     } | 
 | 1345 |  | 
 | 1346 |     size_t size = mEffects.size(); | 
 | 1347 |     if (doProcess) { | 
 | 1348 |         for (size_t i = 0; i < size; i++) { | 
 | 1349 |             mEffects[i]->process(); | 
 | 1350 |         } | 
 | 1351 |     } | 
 | 1352 |     for (size_t i = 0; i < size; i++) { | 
 | 1353 |         mEffects[i]->updateState(); | 
 | 1354 |     } | 
 | 1355 | } | 
 | 1356 |  | 
 | 1357 | // addEffect_l() must be called with PlaybackThread::mLock held | 
 | 1358 | status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect) | 
 | 1359 | { | 
 | 1360 |     effect_descriptor_t desc = effect->desc(); | 
 | 1361 |     uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK; | 
 | 1362 |  | 
 | 1363 |     Mutex::Autolock _l(mLock); | 
 | 1364 |     effect->setChain(this); | 
 | 1365 |     sp<ThreadBase> thread = mThread.promote(); | 
 | 1366 |     if (thread == 0) { | 
 | 1367 |         return NO_INIT; | 
 | 1368 |     } | 
 | 1369 |     effect->setThread(thread); | 
 | 1370 |  | 
 | 1371 |     if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 1372 |         // Auxiliary effects are inserted at the beginning of mEffects vector as | 
 | 1373 |         // they are processed first and accumulated in chain input buffer | 
 | 1374 |         mEffects.insertAt(effect, 0); | 
 | 1375 |  | 
 | 1376 |         // the input buffer for auxiliary effect contains mono samples in | 
 | 1377 |         // 32 bit format. This is to avoid saturation in AudoMixer | 
 | 1378 |         // accumulation stage. Saturation is done in EffectModule::process() before | 
 | 1379 |         // calling the process in effect engine | 
 | 1380 |         size_t numSamples = thread->frameCount(); | 
 | 1381 |         int32_t *buffer = new int32_t[numSamples]; | 
 | 1382 |         memset(buffer, 0, numSamples * sizeof(int32_t)); | 
 | 1383 |         effect->setInBuffer((int16_t *)buffer); | 
 | 1384 |         // auxiliary effects output samples to chain input buffer for further processing | 
 | 1385 |         // by insert effects | 
 | 1386 |         effect->setOutBuffer(mInBuffer); | 
 | 1387 |     } else { | 
 | 1388 |         // Insert effects are inserted at the end of mEffects vector as they are processed | 
 | 1389 |         //  after track and auxiliary effects. | 
 | 1390 |         // Insert effect order as a function of indicated preference: | 
 | 1391 |         //  if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if | 
 | 1392 |         //  another effect is present | 
 | 1393 |         //  else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the | 
 | 1394 |         //  last effect claiming first position | 
 | 1395 |         //  else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the | 
 | 1396 |         //  first effect claiming last position | 
 | 1397 |         //  else if EFFECT_FLAG_INSERT_ANY insert after first or before last | 
 | 1398 |         // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is | 
 | 1399 |         // already present | 
 | 1400 |  | 
 | 1401 |         size_t size = mEffects.size(); | 
 | 1402 |         size_t idx_insert = size; | 
 | 1403 |         ssize_t idx_insert_first = -1; | 
 | 1404 |         ssize_t idx_insert_last = -1; | 
 | 1405 |  | 
 | 1406 |         for (size_t i = 0; i < size; i++) { | 
 | 1407 |             effect_descriptor_t d = mEffects[i]->desc(); | 
 | 1408 |             uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK; | 
 | 1409 |             uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK; | 
 | 1410 |             if (iMode == EFFECT_FLAG_TYPE_INSERT) { | 
 | 1411 |                 // check invalid effect chaining combinations | 
 | 1412 |                 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE || | 
 | 1413 |                     iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) { | 
 | 1414 |                     ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", | 
 | 1415 |                             desc.name, d.name); | 
 | 1416 |                     return INVALID_OPERATION; | 
 | 1417 |                 } | 
 | 1418 |                 // remember position of first insert effect and by default | 
 | 1419 |                 // select this as insert position for new effect | 
 | 1420 |                 if (idx_insert == size) { | 
 | 1421 |                     idx_insert = i; | 
 | 1422 |                 } | 
 | 1423 |                 // remember position of last insert effect claiming | 
 | 1424 |                 // first position | 
 | 1425 |                 if (iPref == EFFECT_FLAG_INSERT_FIRST) { | 
 | 1426 |                     idx_insert_first = i; | 
 | 1427 |                 } | 
 | 1428 |                 // remember position of first insert effect claiming | 
 | 1429 |                 // last position | 
 | 1430 |                 if (iPref == EFFECT_FLAG_INSERT_LAST && | 
 | 1431 |                     idx_insert_last == -1) { | 
 | 1432 |                     idx_insert_last = i; | 
 | 1433 |                 } | 
 | 1434 |             } | 
 | 1435 |         } | 
 | 1436 |  | 
 | 1437 |         // modify idx_insert from first position if needed | 
 | 1438 |         if (insertPref == EFFECT_FLAG_INSERT_LAST) { | 
 | 1439 |             if (idx_insert_last != -1) { | 
 | 1440 |                 idx_insert = idx_insert_last; | 
 | 1441 |             } else { | 
 | 1442 |                 idx_insert = size; | 
 | 1443 |             } | 
 | 1444 |         } else { | 
 | 1445 |             if (idx_insert_first != -1) { | 
 | 1446 |                 idx_insert = idx_insert_first + 1; | 
 | 1447 |             } | 
 | 1448 |         } | 
 | 1449 |  | 
 | 1450 |         // always read samples from chain input buffer | 
 | 1451 |         effect->setInBuffer(mInBuffer); | 
 | 1452 |  | 
 | 1453 |         // if last effect in the chain, output samples to chain | 
 | 1454 |         // output buffer, otherwise to chain input buffer | 
 | 1455 |         if (idx_insert == size) { | 
 | 1456 |             if (idx_insert != 0) { | 
 | 1457 |                 mEffects[idx_insert-1]->setOutBuffer(mInBuffer); | 
 | 1458 |                 mEffects[idx_insert-1]->configure(); | 
 | 1459 |             } | 
 | 1460 |             effect->setOutBuffer(mOutBuffer); | 
 | 1461 |         } else { | 
 | 1462 |             effect->setOutBuffer(mInBuffer); | 
 | 1463 |         } | 
 | 1464 |         mEffects.insertAt(effect, idx_insert); | 
 | 1465 |  | 
 | 1466 |         ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, | 
 | 1467 |                 idx_insert); | 
 | 1468 |     } | 
 | 1469 |     effect->configure(); | 
 | 1470 |     return NO_ERROR; | 
 | 1471 | } | 
 | 1472 |  | 
 | 1473 | // removeEffect_l() must be called with PlaybackThread::mLock held | 
 | 1474 | size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect) | 
 | 1475 | { | 
 | 1476 |     Mutex::Autolock _l(mLock); | 
 | 1477 |     size_t size = mEffects.size(); | 
 | 1478 |     uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK; | 
 | 1479 |  | 
 | 1480 |     for (size_t i = 0; i < size; i++) { | 
 | 1481 |         if (effect == mEffects[i]) { | 
 | 1482 |             // calling stop here will remove pre-processing effect from the audio HAL. | 
 | 1483 |             // This is safe as we hold the EffectChain mutex which guarantees that we are not in | 
 | 1484 |             // the middle of a read from audio HAL | 
 | 1485 |             if (mEffects[i]->state() == EffectModule::ACTIVE || | 
 | 1486 |                     mEffects[i]->state() == EffectModule::STOPPING) { | 
 | 1487 |                 mEffects[i]->stop(); | 
 | 1488 |             } | 
 | 1489 |             if (type == EFFECT_FLAG_TYPE_AUXILIARY) { | 
 | 1490 |                 delete[] effect->inBuffer(); | 
 | 1491 |             } else { | 
 | 1492 |                 if (i == size - 1 && i != 0) { | 
 | 1493 |                     mEffects[i - 1]->setOutBuffer(mOutBuffer); | 
 | 1494 |                     mEffects[i - 1]->configure(); | 
 | 1495 |                 } | 
 | 1496 |             } | 
 | 1497 |             mEffects.removeAt(i); | 
 | 1498 |             ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), | 
 | 1499 |                     this, i); | 
 | 1500 |             break; | 
 | 1501 |         } | 
 | 1502 |     } | 
 | 1503 |  | 
 | 1504 |     return mEffects.size(); | 
 | 1505 | } | 
 | 1506 |  | 
 | 1507 | // setDevice_l() must be called with PlaybackThread::mLock held | 
 | 1508 | void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device) | 
 | 1509 | { | 
 | 1510 |     size_t size = mEffects.size(); | 
 | 1511 |     for (size_t i = 0; i < size; i++) { | 
 | 1512 |         mEffects[i]->setDevice(device); | 
 | 1513 |     } | 
 | 1514 | } | 
 | 1515 |  | 
 | 1516 | // setMode_l() must be called with PlaybackThread::mLock held | 
 | 1517 | void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode) | 
 | 1518 | { | 
 | 1519 |     size_t size = mEffects.size(); | 
 | 1520 |     for (size_t i = 0; i < size; i++) { | 
 | 1521 |         mEffects[i]->setMode(mode); | 
 | 1522 |     } | 
 | 1523 | } | 
 | 1524 |  | 
 | 1525 | // setAudioSource_l() must be called with PlaybackThread::mLock held | 
 | 1526 | void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source) | 
 | 1527 | { | 
 | 1528 |     size_t size = mEffects.size(); | 
 | 1529 |     for (size_t i = 0; i < size; i++) { | 
 | 1530 |         mEffects[i]->setAudioSource(source); | 
 | 1531 |     } | 
 | 1532 | } | 
 | 1533 |  | 
 | 1534 | // setVolume_l() must be called with PlaybackThread::mLock held | 
 | 1535 | bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right) | 
 | 1536 | { | 
 | 1537 |     uint32_t newLeft = *left; | 
 | 1538 |     uint32_t newRight = *right; | 
 | 1539 |     bool hasControl = false; | 
 | 1540 |     int ctrlIdx = -1; | 
 | 1541 |     size_t size = mEffects.size(); | 
 | 1542 |  | 
 | 1543 |     // first update volume controller | 
 | 1544 |     for (size_t i = size; i > 0; i--) { | 
 | 1545 |         if (mEffects[i - 1]->isProcessEnabled() && | 
 | 1546 |             (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) { | 
 | 1547 |             ctrlIdx = i - 1; | 
 | 1548 |             hasControl = true; | 
 | 1549 |             break; | 
 | 1550 |         } | 
 | 1551 |     } | 
 | 1552 |  | 
 | 1553 |     if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) { | 
 | 1554 |         if (hasControl) { | 
 | 1555 |             *left = mNewLeftVolume; | 
 | 1556 |             *right = mNewRightVolume; | 
 | 1557 |         } | 
 | 1558 |         return hasControl; | 
 | 1559 |     } | 
 | 1560 |  | 
 | 1561 |     mVolumeCtrlIdx = ctrlIdx; | 
 | 1562 |     mLeftVolume = newLeft; | 
 | 1563 |     mRightVolume = newRight; | 
 | 1564 |  | 
 | 1565 |     // second get volume update from volume controller | 
 | 1566 |     if (ctrlIdx >= 0) { | 
 | 1567 |         mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true); | 
 | 1568 |         mNewLeftVolume = newLeft; | 
 | 1569 |         mNewRightVolume = newRight; | 
 | 1570 |     } | 
 | 1571 |     // then indicate volume to all other effects in chain. | 
 | 1572 |     // Pass altered volume to effects before volume controller | 
 | 1573 |     // and requested volume to effects after controller | 
 | 1574 |     uint32_t lVol = newLeft; | 
 | 1575 |     uint32_t rVol = newRight; | 
 | 1576 |  | 
 | 1577 |     for (size_t i = 0; i < size; i++) { | 
 | 1578 |         if ((int)i == ctrlIdx) { | 
 | 1579 |             continue; | 
 | 1580 |         } | 
 | 1581 |         // this also works for ctrlIdx == -1 when there is no volume controller | 
 | 1582 |         if ((int)i > ctrlIdx) { | 
 | 1583 |             lVol = *left; | 
 | 1584 |             rVol = *right; | 
 | 1585 |         } | 
 | 1586 |         mEffects[i]->setVolume(&lVol, &rVol, false); | 
 | 1587 |     } | 
 | 1588 |     *left = newLeft; | 
 | 1589 |     *right = newRight; | 
 | 1590 |  | 
 | 1591 |     return hasControl; | 
 | 1592 | } | 
 | 1593 |  | 
 | 1594 | void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args) | 
 | 1595 | { | 
 | 1596 |     const size_t SIZE = 256; | 
 | 1597 |     char buffer[SIZE]; | 
 | 1598 |     String8 result; | 
 | 1599 |  | 
 | 1600 |     snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId); | 
 | 1601 |     result.append(buffer); | 
 | 1602 |  | 
 | 1603 |     bool locked = AudioFlinger::dumpTryLock(mLock); | 
 | 1604 |     // failed to lock - AudioFlinger is probably deadlocked | 
 | 1605 |     if (!locked) { | 
 | 1606 |         result.append("\tCould not lock mutex:\n"); | 
 | 1607 |     } | 
 | 1608 |  | 
 | 1609 |     result.append("\tNum fx In buffer   Out buffer   Active tracks:\n"); | 
 | 1610 |     snprintf(buffer, SIZE, "\t%02d     0x%08x  0x%08x   %d\n", | 
 | 1611 |             mEffects.size(), | 
 | 1612 |             (uint32_t)mInBuffer, | 
 | 1613 |             (uint32_t)mOutBuffer, | 
 | 1614 |             mActiveTrackCnt); | 
 | 1615 |     result.append(buffer); | 
 | 1616 |     write(fd, result.string(), result.size()); | 
 | 1617 |  | 
 | 1618 |     for (size_t i = 0; i < mEffects.size(); ++i) { | 
 | 1619 |         sp<EffectModule> effect = mEffects[i]; | 
 | 1620 |         if (effect != 0) { | 
 | 1621 |             effect->dump(fd, args); | 
 | 1622 |         } | 
 | 1623 |     } | 
 | 1624 |  | 
 | 1625 |     if (locked) { | 
 | 1626 |         mLock.unlock(); | 
 | 1627 |     } | 
 | 1628 | } | 
 | 1629 |  | 
 | 1630 | // must be called with ThreadBase::mLock held | 
 | 1631 | void AudioFlinger::EffectChain::setEffectSuspended_l( | 
 | 1632 |         const effect_uuid_t *type, bool suspend) | 
 | 1633 | { | 
 | 1634 |     sp<SuspendedEffectDesc> desc; | 
 | 1635 |     // use effect type UUID timelow as key as there is no real risk of identical | 
 | 1636 |     // timeLow fields among effect type UUIDs. | 
 | 1637 |     ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow); | 
 | 1638 |     if (suspend) { | 
 | 1639 |         if (index >= 0) { | 
 | 1640 |             desc = mSuspendedEffects.valueAt(index); | 
 | 1641 |         } else { | 
 | 1642 |             desc = new SuspendedEffectDesc(); | 
 | 1643 |             desc->mType = *type; | 
 | 1644 |             mSuspendedEffects.add(type->timeLow, desc); | 
 | 1645 |             ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow); | 
 | 1646 |         } | 
 | 1647 |         if (desc->mRefCount++ == 0) { | 
 | 1648 |             sp<EffectModule> effect = getEffectIfEnabled(type); | 
 | 1649 |             if (effect != 0) { | 
 | 1650 |                 desc->mEffect = effect; | 
 | 1651 |                 effect->setSuspended(true); | 
 | 1652 |                 effect->setEnabled(false); | 
 | 1653 |             } | 
 | 1654 |         } | 
 | 1655 |     } else { | 
 | 1656 |         if (index < 0) { | 
 | 1657 |             return; | 
 | 1658 |         } | 
 | 1659 |         desc = mSuspendedEffects.valueAt(index); | 
 | 1660 |         if (desc->mRefCount <= 0) { | 
 | 1661 |             ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount); | 
 | 1662 |             desc->mRefCount = 1; | 
 | 1663 |         } | 
 | 1664 |         if (--desc->mRefCount == 0) { | 
 | 1665 |             ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index)); | 
 | 1666 |             if (desc->mEffect != 0) { | 
 | 1667 |                 sp<EffectModule> effect = desc->mEffect.promote(); | 
 | 1668 |                 if (effect != 0) { | 
 | 1669 |                     effect->setSuspended(false); | 
 | 1670 |                     effect->lock(); | 
 | 1671 |                     EffectHandle *handle = effect->controlHandle_l(); | 
 | 1672 |                     if (handle != NULL && !handle->destroyed_l()) { | 
 | 1673 |                         effect->setEnabled_l(handle->enabled()); | 
 | 1674 |                     } | 
 | 1675 |                     effect->unlock(); | 
 | 1676 |                 } | 
 | 1677 |                 desc->mEffect.clear(); | 
 | 1678 |             } | 
 | 1679 |             mSuspendedEffects.removeItemsAt(index); | 
 | 1680 |         } | 
 | 1681 |     } | 
 | 1682 | } | 
 | 1683 |  | 
 | 1684 | // must be called with ThreadBase::mLock held | 
 | 1685 | void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend) | 
 | 1686 | { | 
 | 1687 |     sp<SuspendedEffectDesc> desc; | 
 | 1688 |  | 
 | 1689 |     ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); | 
 | 1690 |     if (suspend) { | 
 | 1691 |         if (index >= 0) { | 
 | 1692 |             desc = mSuspendedEffects.valueAt(index); | 
 | 1693 |         } else { | 
 | 1694 |             desc = new SuspendedEffectDesc(); | 
 | 1695 |             mSuspendedEffects.add((int)kKeyForSuspendAll, desc); | 
 | 1696 |             ALOGV("setEffectSuspendedAll_l() add entry for 0"); | 
 | 1697 |         } | 
 | 1698 |         if (desc->mRefCount++ == 0) { | 
 | 1699 |             Vector< sp<EffectModule> > effects; | 
 | 1700 |             getSuspendEligibleEffects(effects); | 
 | 1701 |             for (size_t i = 0; i < effects.size(); i++) { | 
 | 1702 |                 setEffectSuspended_l(&effects[i]->desc().type, true); | 
 | 1703 |             } | 
 | 1704 |         } | 
 | 1705 |     } else { | 
 | 1706 |         if (index < 0) { | 
 | 1707 |             return; | 
 | 1708 |         } | 
 | 1709 |         desc = mSuspendedEffects.valueAt(index); | 
 | 1710 |         if (desc->mRefCount <= 0) { | 
 | 1711 |             ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount); | 
 | 1712 |             desc->mRefCount = 1; | 
 | 1713 |         } | 
 | 1714 |         if (--desc->mRefCount == 0) { | 
 | 1715 |             Vector<const effect_uuid_t *> types; | 
 | 1716 |             for (size_t i = 0; i < mSuspendedEffects.size(); i++) { | 
 | 1717 |                 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) { | 
 | 1718 |                     continue; | 
 | 1719 |                 } | 
 | 1720 |                 types.add(&mSuspendedEffects.valueAt(i)->mType); | 
 | 1721 |             } | 
 | 1722 |             for (size_t i = 0; i < types.size(); i++) { | 
 | 1723 |                 setEffectSuspended_l(types[i], false); | 
 | 1724 |             } | 
 | 1725 |             ALOGV("setEffectSuspendedAll_l() remove entry for %08x", | 
 | 1726 |                     mSuspendedEffects.keyAt(index)); | 
 | 1727 |             mSuspendedEffects.removeItem((int)kKeyForSuspendAll); | 
 | 1728 |         } | 
 | 1729 |     } | 
 | 1730 | } | 
 | 1731 |  | 
 | 1732 |  | 
 | 1733 | // The volume effect is used for automated tests only | 
 | 1734 | #ifndef OPENSL_ES_H_ | 
 | 1735 | static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6, | 
 | 1736 |                                             { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }; | 
 | 1737 | const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_; | 
 | 1738 | #endif //OPENSL_ES_H_ | 
 | 1739 |  | 
 | 1740 | bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc) | 
 | 1741 | { | 
 | 1742 |     // auxiliary effects and visualizer are never suspended on output mix | 
 | 1743 |     if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) && | 
 | 1744 |         (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) || | 
 | 1745 |          (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) || | 
 | 1746 |          (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) { | 
 | 1747 |         return false; | 
 | 1748 |     } | 
 | 1749 |     return true; | 
 | 1750 | } | 
 | 1751 |  | 
 | 1752 | void AudioFlinger::EffectChain::getSuspendEligibleEffects( | 
 | 1753 |         Vector< sp<AudioFlinger::EffectModule> > &effects) | 
 | 1754 | { | 
 | 1755 |     effects.clear(); | 
 | 1756 |     for (size_t i = 0; i < mEffects.size(); i++) { | 
 | 1757 |         if (isEffectEligibleForSuspend(mEffects[i]->desc())) { | 
 | 1758 |             effects.add(mEffects[i]); | 
 | 1759 |         } | 
 | 1760 |     } | 
 | 1761 | } | 
 | 1762 |  | 
 | 1763 | sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled( | 
 | 1764 |                                                             const effect_uuid_t *type) | 
 | 1765 | { | 
 | 1766 |     sp<EffectModule> effect = getEffectFromType_l(type); | 
 | 1767 |     return effect != 0 && effect->isEnabled() ? effect : 0; | 
 | 1768 | } | 
 | 1769 |  | 
 | 1770 | void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, | 
 | 1771 |                                                             bool enabled) | 
 | 1772 | { | 
 | 1773 |     ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); | 
 | 1774 |     if (enabled) { | 
 | 1775 |         if (index < 0) { | 
 | 1776 |             // if the effect is not suspend check if all effects are suspended | 
 | 1777 |             index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll); | 
 | 1778 |             if (index < 0) { | 
 | 1779 |                 return; | 
 | 1780 |             } | 
 | 1781 |             if (!isEffectEligibleForSuspend(effect->desc())) { | 
 | 1782 |                 return; | 
 | 1783 |             } | 
 | 1784 |             setEffectSuspended_l(&effect->desc().type, enabled); | 
 | 1785 |             index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow); | 
 | 1786 |             if (index < 0) { | 
 | 1787 |                 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!"); | 
 | 1788 |                 return; | 
 | 1789 |             } | 
 | 1790 |         } | 
 | 1791 |         ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x", | 
 | 1792 |             effect->desc().type.timeLow); | 
 | 1793 |         sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); | 
 | 1794 |         // if effect is requested to suspended but was not yet enabled, supend it now. | 
 | 1795 |         if (desc->mEffect == 0) { | 
 | 1796 |             desc->mEffect = effect; | 
 | 1797 |             effect->setEnabled(false); | 
 | 1798 |             effect->setSuspended(true); | 
 | 1799 |         } | 
 | 1800 |     } else { | 
 | 1801 |         if (index < 0) { | 
 | 1802 |             return; | 
 | 1803 |         } | 
 | 1804 |         ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x", | 
 | 1805 |             effect->desc().type.timeLow); | 
 | 1806 |         sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index); | 
 | 1807 |         desc->mEffect.clear(); | 
 | 1808 |         effect->setSuspended(false); | 
 | 1809 |     } | 
 | 1810 | } | 
 | 1811 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1812 | bool AudioFlinger::EffectChain::isNonOffloadableEnabled() | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1813 | { | 
 | 1814 |     Mutex::Autolock _l(mLock); | 
 | 1815 |     size_t size = mEffects.size(); | 
 | 1816 |     for (size_t i = 0; i < size; i++) { | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1817 |         if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) { | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1818 |             return true; | 
 | 1819 |         } | 
 | 1820 |     } | 
 | 1821 |     return false; | 
 | 1822 | } | 
 | 1823 |  | 
| Eric Laurent | ca7cc82 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1824 | }; // namespace android |