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