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