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