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