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