blob: 13152d0b8485604117e71f0f92e74d15b6ca1782 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
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
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070027#include <system/audio_effects/effect_dynamicsprocessing.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070028#include <system/audio_effects/effect_ns.h>
29#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080030#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080031#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070032#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070033#include <media/audiohal/EffectHalInterface.h>
34#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070035#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080036
37#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080038
39// ----------------------------------------------------------------------------
40
41// Note: the following macro is used for extremely verbose logging message. In
42// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
43// 0; but one side effect of this is to turn all LOGV's as well. Some messages
44// are so verbose that we want to suppress them even when we have ALOG_ASSERT
45// turned on. Do not uncomment the #def below unless you really know what you
46// are doing and want to see all of the extremely verbose messages.
47//#define VERY_VERY_VERBOSE_LOGGING
48#ifdef VERY_VERY_VERBOSE_LOGGING
49#define ALOGVV ALOGV
50#else
51#define ALOGVV(a...) do { } while(0)
52#endif
53
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090054#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
55
Eric Laurentca7cc822012-11-19 14:55:58 -080056namespace android {
57
58// ----------------------------------------------------------------------------
59// EffectModule implementation
60// ----------------------------------------------------------------------------
61
62#undef LOG_TAG
63#define LOG_TAG "AudioFlinger::EffectModule"
64
65AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
66 const wp<AudioFlinger::EffectChain>& chain,
67 effect_descriptor_t *desc,
68 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080069 audio_session_t sessionId,
70 bool pinned)
71 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080072 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
73 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080074 // clear mConfig to ensure consistent initial value of buffer framecount
75 // in case buffers are associated by setInBuffer() or setOutBuffer()
76 // prior to configure().
77 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080078 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080079 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
80 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070081 mSuspended(false),
Andy Hung62aef7d2017-12-14 14:50:40 -080082 mOffloaded(false),
Eric Laurentaaa44472014-09-12 17:41:50 -070083 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070084#ifdef FLOAT_EFFECT_CHAIN
85 , mSupportsFloat(false)
86#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080087{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080088 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080089 int lStatus;
90
91 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070092 mStatus = -ENODEV;
93 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070094 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070095 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070096 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070097 mStatus = effectsFactory->createEffect(
98 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
99 }
100 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800101
102 if (mStatus != NO_ERROR) {
103 return;
104 }
105 lStatus = init();
106 if (lStatus < 0) {
107 mStatus = lStatus;
108 goto Error;
109 }
110
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800111 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700112 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800113
Eric Laurentca7cc822012-11-19 14:55:58 -0800114 return;
115Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700116 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800117 ALOGV("Constructor Error %d", mStatus);
118}
119
120AudioFlinger::EffectModule::~EffectModule()
121{
122 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700123 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700124 char uuidStr[64];
125 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
126 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
127 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800128 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800129 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800130
Eric Laurentca7cc822012-11-19 14:55:58 -0800131}
132
133status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
134{
135 status_t status;
136
137 Mutex::Autolock _l(mLock);
138 int priority = handle->priority();
139 size_t size = mHandles.size();
140 EffectHandle *controlHandle = NULL;
141 size_t i;
142 for (i = 0; i < size; i++) {
143 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800144 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800145 continue;
146 }
147 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700148 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800149 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700150 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800151 if (h->priority() <= priority) {
152 break;
153 }
154 }
155 // if inserted in first place, move effect control from previous owner to this handle
156 if (i == 0) {
157 bool enabled = false;
158 if (controlHandle != NULL) {
159 enabled = controlHandle->enabled();
160 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
161 }
162 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
163 status = NO_ERROR;
164 } else {
165 status = ALREADY_EXISTS;
166 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700167 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800168 mHandles.insertAt(handle, i);
169 return status;
170}
171
Eric Laurent6c796322019-04-09 14:13:17 -0700172status_t AudioFlinger::EffectModule::updatePolicyState()
173{
174 status_t status = NO_ERROR;
175 bool doRegister = false;
176 bool registered = false;
177 bool doEnable = false;
178 bool enabled = false;
179 audio_io_handle_t io;
180 uint32_t strategy;
181
182 {
183 Mutex::Autolock _l(mLock);
184 // register effect when first handle is attached and unregister when last handle is removed
185 if (mPolicyRegistered != mHandles.size() > 0) {
186 doRegister = true;
187 mPolicyRegistered = mHandles.size() > 0;
188 if (mPolicyRegistered) {
189 sp <EffectChain> chain = mChain.promote();
190 sp <ThreadBase> thread = mThread.promote();
191
192 if (thread == nullptr || chain == nullptr) {
193 return INVALID_OPERATION;
194 }
195 io = thread->id();
196 strategy = chain->strategy();
197 }
198 }
199 // enable effect when registered according to enable state requested by controlling handle
200 if (mHandles.size() > 0) {
201 EffectHandle *handle = controlHandle_l();
202 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
203 doEnable = true;
204 mPolicyEnabled = handle->enabled();
205 }
206 }
207 registered = mPolicyRegistered;
208 enabled = mPolicyEnabled;
209 mPolicyLock.lock();
210 }
211 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
212 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
213 if (doRegister) {
214 if (registered) {
215 status = AudioSystem::registerEffect(
216 &mDescriptor,
217 io,
218 strategy,
219 mSessionId,
220 mId);
221 } else {
222 status = AudioSystem::unregisterEffect(mId);
223 }
224 }
225 if (registered && doEnable) {
226 status = AudioSystem::setEffectEnabled(mId, enabled);
227 }
228 mPolicyLock.unlock();
229
230 return status;
231}
232
233
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800234ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800235{
236 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800237 return removeHandle_l(handle);
238}
239
240ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
241{
Eric Laurentca7cc822012-11-19 14:55:58 -0800242 size_t size = mHandles.size();
243 size_t i;
244 for (i = 0; i < size; i++) {
245 if (mHandles[i] == handle) {
246 break;
247 }
248 }
249 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800250 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
251 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800252 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800253 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800254
255 mHandles.removeAt(i);
256 // if removed from first place, move effect control from this handle to next in line
257 if (i == 0) {
258 EffectHandle *h = controlHandle_l();
259 if (h != NULL) {
260 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
261 }
262 }
263
264 // Prevent calls to process() and other functions on effect interface from now on.
265 // The effect engine will be released by the destructor when the last strong reference on
266 // this object is released which can happen after next process is called.
267 if (mHandles.size() == 0 && !mPinned) {
268 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800269 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800270 }
271
272 return mHandles.size();
273}
274
275// must be called with EffectModule::mLock held
276AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
277{
278 // the first valid handle in the list has control over the module
279 for (size_t i = 0; i < mHandles.size(); i++) {
280 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800281 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800282 return h;
283 }
284 }
285
286 return NULL;
287}
288
Eric Laurentf10c7092016-12-06 17:09:56 -0800289// unsafe method called when the effect parent thread has been destroyed
290ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
291{
292 ALOGV("disconnect() %p handle %p", this, handle);
293 Mutex::Autolock _l(mLock);
294 ssize_t numHandles = removeHandle_l(handle);
295 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurentf10c7092016-12-06 17:09:56 -0800296 sp<AudioFlinger> af = mAudioFlinger.promote();
297 if (af != 0) {
298 mLock.unlock();
299 af->updateOrphanEffectChains(this);
300 mLock.lock();
301 }
302 }
303 return numHandles;
304}
305
Eric Laurentfa1e1232016-08-02 19:01:49 -0700306bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800307 Mutex::Autolock _l(mLock);
308
Eric Laurentfa1e1232016-08-02 19:01:49 -0700309 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800310 switch (mState) {
311 case RESTART:
312 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700313 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800314
315 case STARTING:
316 // clear auxiliary effect input buffer for next accumulation
317 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
318 memset(mConfig.inputCfg.buffer.raw,
319 0,
320 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
321 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700322 if (start_l() == NO_ERROR) {
323 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700324 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700325 } else {
326 mState = IDLE;
327 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800328 break;
329 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900330 // volume control for offload and direct threads must take effect immediately.
331 if (stop_l() == NO_ERROR
332 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700333 mDisableWaitCnt = mMaxDisableWaitCnt;
334 } else {
335 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
336 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800337 mState = STOPPED;
338 break;
339 case STOPPED:
340 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
341 // turn off sequence.
342 if (--mDisableWaitCnt == 0) {
343 reset_l();
344 mState = IDLE;
345 }
346 break;
347 default: //IDLE , ACTIVE, DESTROYED
348 break;
349 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700350
351 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800352}
353
354void AudioFlinger::EffectModule::process()
355{
356 Mutex::Autolock _l(mLock);
357
Mikhail Naganov022b9952017-01-04 16:36:51 -0800358 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800359 return;
360 }
361
rago94a1ee82017-07-21 15:11:02 -0700362 const uint32_t inChannelCount =
363 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
364 const uint32_t outChannelCount =
365 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
366 const bool auxType =
367 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
368
Andy Hungfa69ca32017-11-30 10:07:53 -0800369 // safeInputOutputSampleCount is 0 if the channel count between input and output
370 // buffers do not match. This prevents automatic accumulation or copying between the
371 // input and output effect buffers without an intermediary effect process.
372 // TODO: consider implementing channel conversion.
373 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700374 mInChannelCountRequested != mOutChannelCountRequested ? 0
375 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800376 mConfig.inputCfg.buffer.frameCount,
377 mConfig.outputCfg.buffer.frameCount);
378 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
379#ifdef FLOAT_EFFECT_CHAIN
380 accumulate_float(
381 mConfig.outputCfg.buffer.f32,
382 mConfig.inputCfg.buffer.f32,
383 safeInputOutputSampleCount);
384#else
385 accumulate_i16(
386 mConfig.outputCfg.buffer.s16,
387 mConfig.inputCfg.buffer.s16,
388 safeInputOutputSampleCount);
389#endif
390 };
391 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
392#ifdef FLOAT_EFFECT_CHAIN
393 memcpy(
394 mConfig.outputCfg.buffer.f32,
395 mConfig.inputCfg.buffer.f32,
396 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
397
398#else
399 memcpy(
400 mConfig.outputCfg.buffer.s16,
401 mConfig.inputCfg.buffer.s16,
402 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
403#endif
404 };
405
Eric Laurentca7cc822012-11-19 14:55:58 -0800406 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700407 int ret;
408 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700409 if (auxType) {
410 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800411 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700412#ifdef FLOAT_EFFECT_CHAIN
413 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800414#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700415 // Do in-place float conversion for auxiliary effect input buffer.
416 static_assert(sizeof(float) <= sizeof(int32_t),
417 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
418
Andy Hungfa69ca32017-11-30 10:07:53 -0800419 memcpy_to_float_from_q4_27(
420 mConfig.inputCfg.buffer.f32,
421 mConfig.inputCfg.buffer.s32,
422 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800423#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800424 } else
Andy Hung116a4982017-11-30 10:15:08 -0800425#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800426 {
Andy Hung116a4982017-11-30 10:15:08 -0800427#ifdef FLOAT_AUX
428 memcpy_to_i16_from_float(
429 mConfig.inputCfg.buffer.s16,
430 mConfig.inputCfg.buffer.f32,
431 mConfig.inputCfg.buffer.frameCount);
432#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800433 memcpy_to_i16_from_q4_27(
434 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700435 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800436 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800437#endif
rago94a1ee82017-07-21 15:11:02 -0700438 }
rago94a1ee82017-07-21 15:11:02 -0700439 }
440#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800441 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
442 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
443
444 if (!auxType && mInChannelCountRequested != inChannelCount) {
445 adjust_channels(
446 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
447 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
448 sizeof(float),
449 sizeof(float)
450 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
451 inBuffer = mInConversionBuffer;
452 }
453 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
454 && mOutChannelCountRequested != outChannelCount) {
455 adjust_selected_channels(
456 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
457 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
458 sizeof(float),
459 sizeof(float)
460 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
461 outBuffer = mOutConversionBuffer;
462 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800463 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
464 if (!auxType) {
465 if (mInConversionBuffer.get() == nullptr) {
466 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
467 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700468 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800469 memcpy_to_i16_from_float(
470 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800471 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800472 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800473 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700474 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800475 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
476 if (mOutConversionBuffer.get() == nullptr) {
477 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
478 goto data_bypass;
479 }
480 memcpy_to_i16_from_float(
481 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800482 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800483 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800484 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700485 }
486 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800487#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800488 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800489#ifdef FLOAT_EFFECT_CHAIN
490 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800491 sp<EffectBufferHalInterface> target =
492 mOutChannelCountRequested != outChannelCount
493 ? mOutConversionBuffer : mOutBuffer;
494
Andy Hungfa69ca32017-11-30 10:07:53 -0800495 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800496 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800497 mOutConversionBuffer->audioBuffer()->s16,
498 outChannelCount * mConfig.outputCfg.buffer.frameCount);
499 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800500 if (mOutChannelCountRequested != outChannelCount) {
501 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
502 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
503 sizeof(float),
504 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
505 }
rago94a1ee82017-07-21 15:11:02 -0700506#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700507 } else {
rago94a1ee82017-07-21 15:11:02 -0700508#ifdef FLOAT_EFFECT_CHAIN
509 data_bypass:
510#endif
511 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800512 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700513 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800514 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700515 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800516 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700517 }
518 }
519 ret = -ENODATA;
520 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800521
Eric Laurentca7cc822012-11-19 14:55:58 -0800522 // force transition to IDLE state when engine is ready
523 if (mState == STOPPED && ret == -ENODATA) {
524 mDisableWaitCnt = 1;
525 }
526
527 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700528 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800529#ifdef FLOAT_AUX
530 const size_t size =
531 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
532#else
rago94a1ee82017-07-21 15:11:02 -0700533 const size_t size =
534 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800535#endif
rago94a1ee82017-07-21 15:11:02 -0700536 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800537 }
538 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700539 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800540 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
541 // If an insert effect is idle and input buffer is different from output buffer,
542 // accumulate input onto output
543 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800544 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700545 // similar handling with data_bypass above.
546 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
547 accumulateInputToOutput();
548 } else { // EFFECT_BUFFER_ACCESS_WRITE
549 copyInputToOutput();
550 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800551 }
552 }
553}
554
555void AudioFlinger::EffectModule::reset_l()
556{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700557 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800558 return;
559 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700560 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800561}
562
563status_t AudioFlinger::EffectModule::configure()
564{
rago94a1ee82017-07-21 15:11:02 -0700565 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700566 status_t status;
567 sp<ThreadBase> thread;
568 uint32_t size;
569 audio_channel_mask_t channelMask;
570
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700571 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700572 status = NO_INIT;
573 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800574 }
575
Eric Laurentd0ebb532013-04-02 16:41:41 -0700576 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800577 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700578 status = DEAD_OBJECT;
579 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800580 }
581
582 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800583 // TODO: handle configuration of input (record) SW effects above the HAL,
584 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
585 // in which case input channel masks should be used here.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700586 channelMask = thread->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800587 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700588 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800589
590 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800591 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
592 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
593 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
594 mConfig.inputCfg.channels);
595 }
596#ifndef MULTICHANNEL_EFFECT_CHAIN
597 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
598 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
599 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
600 mConfig.outputCfg.channels);
601 }
602#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800603 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800604#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700605 // TODO: Update this logic when multichannel effects are implemented.
606 // For offloaded tracks consider mono output as stereo for proper effect initialization
607 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
608 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
609 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
610 ALOGV("Overriding effect input and output as STEREO");
611 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800612#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800613 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800614 mInChannelCountRequested =
615 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
616 mOutChannelCountRequested =
617 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700618
rago94a1ee82017-07-21 15:11:02 -0700619 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
620 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900621
622 // Don't use sample rate for thread if effect isn't offloadable.
623 if ((thread->type() == ThreadBase::OFFLOAD) && !isOffloaded()) {
624 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
625 ALOGV("Overriding effect input as 48kHz");
626 } else {
627 mConfig.inputCfg.samplingRate = thread->sampleRate();
628 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800629 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
630 mConfig.inputCfg.bufferProvider.cookie = NULL;
631 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
632 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
633 mConfig.outputCfg.bufferProvider.cookie = NULL;
634 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
635 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
636 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
637 // Insert effect:
638 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
639 // always overwrites output buffer: input buffer == output buffer
640 // - in other sessions:
641 // last effect in the chain accumulates in output buffer: input buffer != output buffer
642 // other effect: overwrites output buffer: input buffer == output buffer
643 // Auxiliary effect:
644 // accumulates in output buffer: input buffer != output buffer
645 // Therefore: accumulate <=> input buffer != output buffer
646 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
647 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
648 } else {
649 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
650 }
651 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
652 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
653 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
654 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
655
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700656 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800657 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
658
659 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700660 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700661 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800662 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700663 &mConfig,
664 &size,
665 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700666 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800667 status = cmdStatus;
668 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800669
670#ifdef MULTICHANNEL_EFFECT_CHAIN
671 if (status != NO_ERROR &&
Andy Hunge6a61a52018-04-06 18:55:26 -0700672 thread->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800673 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
674 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
675 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700676 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
677 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800678 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
679 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
680 }
681 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
682 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
683 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
684 }
685 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700686 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800687 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700688 &mConfig,
689 &size,
690 &cmdStatus);
691 if (status == NO_ERROR) {
692 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800693 }
694 }
695#endif
696
697#ifdef FLOAT_EFFECT_CHAIN
698 if (status == NO_ERROR) {
699 mSupportsFloat = true;
700 }
701
702 if (status != NO_ERROR) {
703 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
704 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
705 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
706 size = sizeof(int);
707 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
708 sizeof(mConfig),
709 &mConfig,
710 &size,
711 &cmdStatus);
712 if (status == NO_ERROR) {
713 status = cmdStatus;
714 }
715 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700716 mSupportsFloat = false;
717 ALOGVV("config worked with 16 bit");
718 } else {
719 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800720 }
rago94a1ee82017-07-21 15:11:02 -0700721 }
722#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800723
rago94a1ee82017-07-21 15:11:02 -0700724 if (status == NO_ERROR) {
725 // Establish Buffer strategy
726 setInBuffer(mInBuffer);
727 setOutBuffer(mOutBuffer);
728
729 // Update visualizer latency
730 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
731 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
732 effect_param_t *p = (effect_param_t *)buf32;
733
734 p->psize = sizeof(uint32_t);
735 p->vsize = sizeof(uint32_t);
736 size = sizeof(int);
737 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
738
739 uint32_t latency = 0;
740 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
741 if (pbt != NULL) {
742 latency = pbt->latency_l();
743 }
744
745 *((int32_t *)p->data + 1)= latency;
746 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
747 sizeof(effect_param_t) + 8,
748 &buf32,
749 &size,
750 &cmdStatus);
751 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800752 }
753
Andy Hung05083ac2017-12-14 15:00:28 -0800754 // mConfig.outputCfg.buffer.frameCount cannot be zero.
755 mMaxDisableWaitCnt = (uint32_t)std::max(
756 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
757 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
758 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800759
Eric Laurentd0ebb532013-04-02 16:41:41 -0700760exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800761 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700762 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700763 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800764 return status;
765}
766
767status_t AudioFlinger::EffectModule::init()
768{
769 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700770 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800771 return NO_INIT;
772 }
773 status_t cmdStatus;
774 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700775 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
776 0,
777 NULL,
778 &size,
779 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800780 if (status == 0) {
781 status = cmdStatus;
782 }
783 return status;
784}
785
Eric Laurent1b928682014-10-02 19:41:47 -0700786void AudioFlinger::EffectModule::addEffectToHal_l()
787{
788 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
789 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
790 sp<ThreadBase> thread = mThread.promote();
791 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700792 sp<StreamHalInterface> stream = thread->stream();
793 if (stream != 0) {
794 status_t result = stream->addEffect(mEffectInterface);
795 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700796 }
797 }
798 }
799}
800
Eric Laurentfa1e1232016-08-02 19:01:49 -0700801// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800802status_t AudioFlinger::EffectModule::start()
803{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700804 sp<EffectChain> chain;
805 status_t status;
806 {
807 Mutex::Autolock _l(mLock);
808 status = start_l();
809 if (status == NO_ERROR) {
810 chain = mChain.promote();
811 }
812 }
813 if (chain != 0) {
814 chain->resetVolume_l();
815 }
816 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800817}
818
819status_t AudioFlinger::EffectModule::start_l()
820{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700821 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800822 return NO_INIT;
823 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700824 if (mStatus != NO_ERROR) {
825 return mStatus;
826 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800827 status_t cmdStatus;
828 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700829 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
830 0,
831 NULL,
832 &size,
833 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800834 if (status == 0) {
835 status = cmdStatus;
836 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700837 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700838 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 }
840 return status;
841}
842
843status_t AudioFlinger::EffectModule::stop()
844{
845 Mutex::Autolock _l(mLock);
846 return stop_l();
847}
848
849status_t AudioFlinger::EffectModule::stop_l()
850{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700851 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800852 return NO_INIT;
853 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700854 if (mStatus != NO_ERROR) {
855 return mStatus;
856 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800857 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800858 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900859
860 if (isVolumeControl() && isOffloadedOrDirect()) {
861 sp<EffectChain>chain = mChain.promote();
862 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
863 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
864 mSetVolumeReentrantTid = gettid();
865 chain->resetVolume_l();
866 mSetVolumeReentrantTid = INVALID_PID;
867 }
868
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700869 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
870 0,
871 NULL,
872 &size,
873 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800874 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800875 status = cmdStatus;
876 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800877 if (status == NO_ERROR) {
878 status = remove_effect_from_hal_l();
879 }
880 return status;
881}
882
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800883// must be called with EffectChain::mLock held
884void AudioFlinger::EffectModule::release_l()
885{
886 if (mEffectInterface != 0) {
887 remove_effect_from_hal_l();
888 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800889 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800890 mEffectInterface.clear();
891 }
892}
893
Eric Laurentbfb1b832013-01-07 09:53:42 -0800894status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
895{
896 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
897 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800898 sp<ThreadBase> thread = mThread.promote();
899 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700900 sp<StreamHalInterface> stream = thread->stream();
901 if (stream != 0) {
902 status_t result = stream->removeEffect(mEffectInterface);
903 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800904 }
905 }
906 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800907 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800908}
909
Andy Hunge4a1d912016-08-17 14:11:13 -0700910// round up delta valid if value and divisor are positive.
911template <typename T>
912static T roundUpDelta(const T &value, const T &divisor) {
913 T remainder = value % divisor;
914 return remainder == 0 ? 0 : divisor - remainder;
915}
916
Eric Laurentca7cc822012-11-19 14:55:58 -0800917status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
918 uint32_t cmdSize,
919 void *pCmdData,
920 uint32_t *replySize,
921 void *pReplyData)
922{
923 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700924 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800925
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700926 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800927 return NO_INIT;
928 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700929 if (mStatus != NO_ERROR) {
930 return mStatus;
931 }
Andy Hung110bc952016-06-20 15:22:52 -0700932 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700933 (sizeof(effect_param_t) > cmdSize ||
934 ((effect_param_t *)pCmdData)->psize > cmdSize
935 - sizeof(effect_param_t))) {
936 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800937 android_errorWriteLog(0x534e4554, "33003822");
938 return -EINVAL;
939 }
940 if (cmdCode == EFFECT_CMD_GET_PARAM &&
941 (*replySize < sizeof(effect_param_t) ||
942 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
943 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700944 return -EINVAL;
945 }
ragoe2759072016-11-22 18:02:48 -0800946 if (cmdCode == EFFECT_CMD_GET_PARAM &&
947 (sizeof(effect_param_t) > *replySize
948 || ((effect_param_t *)pCmdData)->psize > *replySize
949 - sizeof(effect_param_t)
950 || ((effect_param_t *)pCmdData)->vsize > *replySize
951 - sizeof(effect_param_t)
952 - ((effect_param_t *)pCmdData)->psize
953 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
954 *replySize
955 - sizeof(effect_param_t)
956 - ((effect_param_t *)pCmdData)->psize
957 - ((effect_param_t *)pCmdData)->vsize)) {
958 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
959 android_errorWriteLog(0x534e4554, "32705438");
960 return -EINVAL;
961 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700962 if ((cmdCode == EFFECT_CMD_SET_PARAM
963 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
964 (sizeof(effect_param_t) > cmdSize
965 || ((effect_param_t *)pCmdData)->psize > cmdSize
966 - sizeof(effect_param_t)
967 || ((effect_param_t *)pCmdData)->vsize > cmdSize
968 - sizeof(effect_param_t)
969 - ((effect_param_t *)pCmdData)->psize
970 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
971 cmdSize
972 - sizeof(effect_param_t)
973 - ((effect_param_t *)pCmdData)->psize
974 - ((effect_param_t *)pCmdData)->vsize)) {
975 android_errorWriteLog(0x534e4554, "30204301");
976 return -EINVAL;
977 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700978 status_t status = mEffectInterface->command(cmdCode,
979 cmdSize,
980 pCmdData,
981 replySize,
982 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800983 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
984 uint32_t size = (replySize == NULL) ? 0 : *replySize;
985 for (size_t i = 1; i < mHandles.size(); i++) {
986 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800987 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800988 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
989 }
990 }
991 }
992 return status;
993}
994
995status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
996{
997 Mutex::Autolock _l(mLock);
998 return setEnabled_l(enabled);
999}
1000
1001// must be called with EffectModule::mLock held
1002status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
1003{
1004
1005 ALOGV("setEnabled %p enabled %d", this, enabled);
1006
1007 if (enabled != isEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001008 switch (mState) {
1009 // going from disabled to enabled
1010 case IDLE:
1011 mState = STARTING;
1012 break;
1013 case STOPPED:
1014 mState = RESTART;
1015 break;
1016 case STOPPING:
1017 mState = ACTIVE;
1018 break;
1019
1020 // going from enabled to disabled
1021 case RESTART:
1022 mState = STOPPED;
1023 break;
1024 case STARTING:
1025 mState = IDLE;
1026 break;
1027 case ACTIVE:
1028 mState = STOPPING;
1029 break;
1030 case DESTROYED:
1031 return NO_ERROR; // simply ignore as we are being destroyed
1032 }
1033 for (size_t i = 1; i < mHandles.size(); i++) {
1034 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001035 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 h->setEnabled(enabled);
1037 }
1038 }
1039 }
1040 return NO_ERROR;
1041}
1042
1043bool AudioFlinger::EffectModule::isEnabled() const
1044{
1045 switch (mState) {
1046 case RESTART:
1047 case STARTING:
1048 case ACTIVE:
1049 return true;
1050 case IDLE:
1051 case STOPPING:
1052 case STOPPED:
1053 case DESTROYED:
1054 default:
1055 return false;
1056 }
1057}
1058
1059bool AudioFlinger::EffectModule::isProcessEnabled() const
1060{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001061 if (mStatus != NO_ERROR) {
1062 return false;
1063 }
1064
Eric Laurentca7cc822012-11-19 14:55:58 -08001065 switch (mState) {
1066 case RESTART:
1067 case ACTIVE:
1068 case STOPPING:
1069 case STOPPED:
1070 return true;
1071 case IDLE:
1072 case STARTING:
1073 case DESTROYED:
1074 default:
1075 return false;
1076 }
1077}
1078
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001079bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1080{
1081 return (mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT);
1082}
1083
1084bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1085{
1086 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1087}
1088
Mikhail Naganov022b9952017-01-04 16:36:51 -08001089void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001090 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001091
1092 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001093 if (buffer != 0) {
1094 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1095 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1096 } else {
1097 mConfig.inputCfg.buffer.raw = NULL;
1098 }
1099 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001100 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001101
1102#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001103 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001104 // Theoretically insert effects can also do in-place conversions (destroying
1105 // the original buffer) when the output buffer is identical to the input buffer,
1106 // but we don't optimize for it here.
1107 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001108 const uint32_t inChannelCount =
1109 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1110 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1111 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001112 // we need to translate - create hidl shared buffer and intercept
1113 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001114 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1115 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1116 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001117
1118 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1119 __func__, inChannels, inFrameCount, size);
1120
Andy Hungbded9c82017-11-30 18:47:35 -08001121 if (size > 0 && (mInConversionBuffer.get() == nullptr
1122 || size > mInConversionBuffer->getSize())) {
1123 mInConversionBuffer.clear();
1124 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001125 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1126 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1127 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001128 }
Andy Hungbded9c82017-11-30 18:47:35 -08001129 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001130 mInConversionBuffer->setFrameCount(inFrameCount);
1131 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001132 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001133 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001134 }
1135 }
1136#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001137}
1138
1139void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001140 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001141
1142 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001143 if (buffer != 0) {
1144 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1145 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1146 } else {
1147 mConfig.outputCfg.buffer.raw = NULL;
1148 }
1149 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001150 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001151
1152#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001153 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001154 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001155 const uint32_t outChannelCount =
1156 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1157 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1158 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001159 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001160 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1161 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1162 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001163
1164 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1165 __func__, outChannels, outFrameCount, size);
1166
Andy Hungbded9c82017-11-30 18:47:35 -08001167 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1168 || size > mOutConversionBuffer->getSize())) {
1169 mOutConversionBuffer.clear();
1170 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001171 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1172 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1173 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001174 }
Andy Hungbded9c82017-11-30 18:47:35 -08001175 if (mOutConversionBuffer.get() != nullptr) {
1176 mOutConversionBuffer->setFrameCount(outFrameCount);
1177 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001178 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001179 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001180 }
1181 }
1182#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001183}
1184
Eric Laurentca7cc822012-11-19 14:55:58 -08001185status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1186{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001187 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001188 if (mStatus != NO_ERROR) {
1189 return mStatus;
1190 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001191 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001192 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1193 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1194 if (isProcessEnabled() &&
1195 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001196 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1197 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001198 uint32_t volume[2];
1199 uint32_t *pVolume = NULL;
1200 uint32_t size = sizeof(volume);
1201 volume[0] = *left;
1202 volume[1] = *right;
1203 if (controller) {
1204 pVolume = volume;
1205 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001206 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1207 size,
1208 volume,
1209 &size,
1210 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001211 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1212 *left = volume[0];
1213 *right = volume[1];
1214 }
1215 }
1216 return status;
1217}
1218
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001219void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1220{
1221 sp<ThreadBase> thread = mThread.promote();
1222 if (thread != 0 &&
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09001223 (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::DIRECT) &&
1224 !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001225 PlaybackThread *t = (PlaybackThread *)thread.get();
1226 float vol_l = (float)left / (1 << 24);
1227 float vol_r = (float)right / (1 << 24);
1228 t->setVolumeForOutput_l(vol_l, vol_r);
1229 }
1230}
1231
Eric Laurentca7cc822012-11-19 14:55:58 -08001232status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1233{
1234 if (device == AUDIO_DEVICE_NONE) {
1235 return NO_ERROR;
1236 }
1237
1238 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001239 if (mStatus != NO_ERROR) {
1240 return mStatus;
1241 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001242 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001243 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001244 status_t cmdStatus;
1245 uint32_t size = sizeof(status_t);
1246 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1247 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001248 status = mEffectInterface->command(cmd,
1249 sizeof(uint32_t),
1250 &device,
1251 &size,
1252 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001253 }
1254 return status;
1255}
1256
1257status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1258{
1259 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001260 if (mStatus != NO_ERROR) {
1261 return mStatus;
1262 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001263 status_t status = NO_ERROR;
1264 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1265 status_t cmdStatus;
1266 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001267 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1268 sizeof(audio_mode_t),
1269 &mode,
1270 &size,
1271 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001272 if (status == NO_ERROR) {
1273 status = cmdStatus;
1274 }
1275 }
1276 return status;
1277}
1278
1279status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1280{
1281 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001282 if (mStatus != NO_ERROR) {
1283 return mStatus;
1284 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001285 status_t status = NO_ERROR;
1286 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1287 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001288 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1289 sizeof(audio_source_t),
1290 &source,
1291 &size,
1292 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001293 }
1294 return status;
1295}
1296
1297void AudioFlinger::EffectModule::setSuspended(bool suspended)
1298{
1299 Mutex::Autolock _l(mLock);
1300 mSuspended = suspended;
1301}
1302
1303bool AudioFlinger::EffectModule::suspended() const
1304{
1305 Mutex::Autolock _l(mLock);
1306 return mSuspended;
1307}
1308
1309bool AudioFlinger::EffectModule::purgeHandles()
1310{
1311 bool enabled = false;
1312 Mutex::Autolock _l(mLock);
Eric Laurent6c796322019-04-09 14:13:17 -07001313 EffectHandle *handle = controlHandle_l();
1314 if (handle != NULL) {
1315 enabled = handle->enabled();
Eric Laurentca7cc822012-11-19 14:55:58 -08001316 }
Eric Laurent6c796322019-04-09 14:13:17 -07001317 mHandles.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001318 return enabled;
1319}
1320
Eric Laurent5baf2af2013-09-12 17:37:00 -07001321status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1322{
1323 Mutex::Autolock _l(mLock);
1324 if (mStatus != NO_ERROR) {
1325 return mStatus;
1326 }
1327 status_t status = NO_ERROR;
1328 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1329 status_t cmdStatus;
1330 uint32_t size = sizeof(status_t);
1331 effect_offload_param_t cmd;
1332
1333 cmd.isOffload = offloaded;
1334 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001335 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1336 sizeof(effect_offload_param_t),
1337 &cmd,
1338 &size,
1339 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001340 if (status == NO_ERROR) {
1341 status = cmdStatus;
1342 }
1343 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1344 } else {
1345 if (offloaded) {
1346 status = INVALID_OPERATION;
1347 }
1348 mOffloaded = false;
1349 }
1350 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1351 return status;
1352}
1353
1354bool AudioFlinger::EffectModule::isOffloaded() const
1355{
1356 Mutex::Autolock _l(mLock);
1357 return mOffloaded;
1358}
1359
Marco Nelissenb2208842014-02-07 14:00:50 -08001360String8 effectFlagsToString(uint32_t flags) {
1361 String8 s;
1362
1363 s.append("conn. mode: ");
1364 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1365 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1366 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1367 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1368 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1369 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1370 default: s.append("unknown/reserved"); break;
1371 }
1372 s.append(", ");
1373
1374 s.append("insert pref: ");
1375 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1376 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1377 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1378 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1379 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1380 default: s.append("unknown/reserved"); break;
1381 }
1382 s.append(", ");
1383
1384 s.append("volume mgmt: ");
1385 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1386 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1387 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1388 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001389 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
Marco Nelissenb2208842014-02-07 14:00:50 -08001390 default: s.append("unknown/reserved"); break;
1391 }
1392 s.append(", ");
1393
1394 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1395 if (devind) {
1396 s.append("device indication: ");
1397 switch (devind) {
1398 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1399 default: s.append("unknown/reserved"); break;
1400 }
1401 s.append(", ");
1402 }
1403
1404 s.append("input mode: ");
1405 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1406 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1407 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1408 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1409 default: s.append("not set"); break;
1410 }
1411 s.append(", ");
1412
1413 s.append("output mode: ");
1414 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1415 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1416 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1417 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1418 default: s.append("not set"); break;
1419 }
1420 s.append(", ");
1421
1422 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1423 if (accel) {
1424 s.append("hardware acceleration: ");
1425 switch (accel) {
1426 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1427 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1428 default: s.append("unknown/reserved"); break;
1429 }
1430 s.append(", ");
1431 }
1432
1433 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1434 if (modeind) {
1435 s.append("mode indication: ");
1436 switch (modeind) {
1437 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1438 default: s.append("unknown/reserved"); break;
1439 }
1440 s.append(", ");
1441 }
1442
1443 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1444 if (srcind) {
1445 s.append("source indication: ");
1446 switch (srcind) {
1447 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1448 default: s.append("unknown/reserved"); break;
1449 }
1450 s.append(", ");
1451 }
1452
1453 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1454 s.append("offloadable, ");
1455 }
1456
1457 int len = s.length();
1458 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001459 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001460 s.unlockBuffer(len - 2);
1461 }
1462 return s;
1463}
1464
Andy Hungbded9c82017-11-30 18:47:35 -08001465static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1466 std::stringstream ss;
1467
1468 if (buffer.get() == nullptr) {
1469 return "nullptr"; // make different than below
1470 } else if (buffer->externalData() != nullptr) {
1471 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1472 << " -> "
1473 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1474 } else {
1475 ss << buffer->audioBuffer()->raw;
1476 }
1477 return ss.str();
1478}
Marco Nelissenb2208842014-02-07 14:00:50 -08001479
Glenn Kasten0f11b512014-01-31 16:18:54 -08001480void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001481{
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 String8 result;
1483
Andy Hung9718d662017-12-22 17:57:39 -08001484 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001485
1486 bool locked = AudioFlinger::dumpTryLock(mLock);
1487 // failed to lock - AudioFlinger is probably deadlocked
1488 if (!locked) {
1489 result.append("\t\tCould not lock Fx mutex:\n");
1490 }
1491
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001492 result.append("\t\tSession Status State Registered Enabled Suspended Engine:\n");
1493 result.appendFormat("\t\t%05d %03d %03d %s %s %s %p\n",
1494 mSessionId, mStatus, mState, mPolicyRegistered ? "y" : "n", mPolicyEnabled ? "y" : "n",
1495 mSuspended ? "y" : "n", mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001496
1497 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001498 char uuidStr[64];
1499 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001500 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001501 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001502 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1503 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001504 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001505 mDescriptor.flags,
1506 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001507 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001508 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001509
1510 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001511 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001512
1513 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001514
1515 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001516 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1517 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1518 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001519 mConfig.inputCfg.buffer.frameCount,
1520 mConfig.inputCfg.samplingRate,
1521 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001522 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001523 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001524
1525 result.append("\t\t- Output configuration:\n");
1526 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001527 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001528 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001529 mConfig.outputCfg.buffer.frameCount,
1530 mConfig.outputCfg.samplingRate,
1531 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001532 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001533 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001534
rago94a1ee82017-07-21 15:11:02 -07001535#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001536
Andy Hungbded9c82017-11-30 18:47:35 -08001537 result.appendFormat("\t\t- HAL buffers:\n"
1538 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1539 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1540 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1541 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1542 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001543#endif
1544
Andy Hung9718d662017-12-22 17:57:39 -08001545 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001546 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001547 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001548 for (size_t i = 0; i < mHandles.size(); ++i) {
1549 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001550 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001551 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001552 result.append(buffer);
1553 }
1554 }
1555
Eric Laurentca7cc822012-11-19 14:55:58 -08001556 write(fd, result.string(), result.length());
1557
Mikhail Naganov4d547672019-02-22 14:19:19 -08001558 if (mEffectInterface != 0) {
1559 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1560 (void)mEffectInterface->dump(fd);
1561 }
1562
Eric Laurentca7cc822012-11-19 14:55:58 -08001563 if (locked) {
1564 mLock.unlock();
1565 }
1566}
1567
1568// ----------------------------------------------------------------------------
1569// EffectHandle implementation
1570// ----------------------------------------------------------------------------
1571
1572#undef LOG_TAG
1573#define LOG_TAG "AudioFlinger::EffectHandle"
1574
1575AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1576 const sp<AudioFlinger::Client>& client,
1577 const sp<IEffectClient>& effectClient,
1578 int32_t priority)
1579 : BnEffect(),
1580 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001581 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001582{
1583 ALOGV("constructor %p", this);
1584
1585 if (client == 0) {
1586 return;
1587 }
1588 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1589 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001590 if (mCblkMemory == 0 ||
1591 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001592 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001593 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001594 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001595 return;
1596 }
Glenn Kastene75da402013-11-20 13:54:52 -08001597 new(mCblk) effect_param_cblk_t();
1598 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001599}
1600
1601AudioFlinger::EffectHandle::~EffectHandle()
1602{
1603 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001604 disconnect(false);
1605}
1606
Glenn Kastene75da402013-11-20 13:54:52 -08001607status_t AudioFlinger::EffectHandle::initCheck()
1608{
1609 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1610}
1611
Eric Laurentca7cc822012-11-19 14:55:58 -08001612status_t AudioFlinger::EffectHandle::enable()
1613{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001614 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001615 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001616 sp<EffectModule> effect = mEffect.promote();
1617 if (effect == 0 || mDisconnected) {
1618 return DEAD_OBJECT;
1619 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001620 if (!mHasControl) {
1621 return INVALID_OPERATION;
1622 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001623
1624 if (mEnabled) {
1625 return NO_ERROR;
1626 }
1627
1628 mEnabled = true;
1629
Eric Laurent6c796322019-04-09 14:13:17 -07001630 status_t status = effect->updatePolicyState();
1631 if (status != NO_ERROR) {
1632 mEnabled = false;
1633 return status;
1634 }
1635
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001636 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001637 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001638 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001639 }
1640
1641 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001642 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001643 return NO_ERROR;
1644 }
1645
Eric Laurent6c796322019-04-09 14:13:17 -07001646 status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001647 if (status != NO_ERROR) {
1648 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001649 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001650 }
1651 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001652 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001653 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001654 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1655 Mutex::Autolock _l(thread->mLock);
1656 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001657 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001658 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001659 if (thread->type() == ThreadBase::OFFLOAD) {
1660 PlaybackThread *t = (PlaybackThread *)thread.get();
1661 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1662 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001663 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001664 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1665 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001666 }
1667 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001668 }
1669 return status;
1670}
1671
1672status_t AudioFlinger::EffectHandle::disable()
1673{
1674 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001675 AutoMutex _l(mLock);
1676 sp<EffectModule> effect = mEffect.promote();
1677 if (effect == 0 || mDisconnected) {
1678 return DEAD_OBJECT;
1679 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001680 if (!mHasControl) {
1681 return INVALID_OPERATION;
1682 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001683
1684 if (!mEnabled) {
1685 return NO_ERROR;
1686 }
1687 mEnabled = false;
1688
Eric Laurent6c796322019-04-09 14:13:17 -07001689 effect->updatePolicyState();
1690
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001691 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001692 return NO_ERROR;
1693 }
1694
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001695 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001696
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001697 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001698 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001699 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001700 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1701 Mutex::Autolock _l(thread->mLock);
1702 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001703 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001704 }
1705
1706 return status;
1707}
1708
1709void AudioFlinger::EffectHandle::disconnect()
1710{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001711 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001712 disconnect(true);
1713}
1714
1715void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1716{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001717 AutoMutex _l(mLock);
1718 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1719 if (mDisconnected) {
1720 if (unpinIfLast) {
1721 android_errorWriteLog(0x534e4554, "32707507");
1722 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001723 return;
1724 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001725 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001726 {
1727 sp<EffectModule> effect = mEffect.promote();
1728 if (effect != 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001729 sp<ThreadBase> thread = effect->thread().promote();
1730 if (thread != 0) {
1731 thread->disconnectEffectHandle(this, unpinIfLast);
1732 } else if (effect->disconnectHandle(this, unpinIfLast) > 0) {
1733 ALOGW("%s Effect handle %p disconnected after thread destruction",
1734 __func__, this);
1735 }
1736 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001737 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001738 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001739
Eric Laurentca7cc822012-11-19 14:55:58 -08001740 if (mClient != 0) {
1741 if (mCblk != NULL) {
1742 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1743 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1744 }
1745 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001746 // Client destructor must run with AudioFlinger client mutex locked
1747 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001748 mClient.clear();
1749 }
1750}
1751
1752status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1753 uint32_t cmdSize,
1754 void *pCmdData,
1755 uint32_t *replySize,
1756 void *pReplyData)
1757{
1758 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001759 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001760
Eric Laurentc7ab3092017-06-15 18:43:46 -07001761 // reject commands reserved for internal use by audio framework if coming from outside
1762 // of audioserver
1763 switch(cmdCode) {
1764 case EFFECT_CMD_ENABLE:
1765 case EFFECT_CMD_DISABLE:
1766 case EFFECT_CMD_SET_PARAM:
1767 case EFFECT_CMD_SET_PARAM_DEFERRED:
1768 case EFFECT_CMD_SET_PARAM_COMMIT:
1769 case EFFECT_CMD_GET_PARAM:
1770 break;
1771 default:
1772 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1773 break;
1774 }
1775 android_errorWriteLog(0x534e4554, "62019992");
1776 return BAD_VALUE;
1777 }
1778
Eric Laurent1ffc5852016-12-15 14:46:09 -08001779 if (cmdCode == EFFECT_CMD_ENABLE) {
1780 if (*replySize < sizeof(int)) {
1781 android_errorWriteLog(0x534e4554, "32095713");
1782 return BAD_VALUE;
1783 }
1784 *(int *)pReplyData = NO_ERROR;
1785 *replySize = sizeof(int);
1786 return enable();
1787 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1788 if (*replySize < sizeof(int)) {
1789 android_errorWriteLog(0x534e4554, "32095713");
1790 return BAD_VALUE;
1791 }
1792 *(int *)pReplyData = NO_ERROR;
1793 *replySize = sizeof(int);
1794 return disable();
1795 }
1796
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001797 AutoMutex _l(mLock);
1798 sp<EffectModule> effect = mEffect.promote();
1799 if (effect == 0 || mDisconnected) {
1800 return DEAD_OBJECT;
1801 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001802 // only get parameter command is permitted for applications not controlling the effect
1803 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1804 return INVALID_OPERATION;
1805 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001806 if (mClient == 0) {
1807 return INVALID_OPERATION;
1808 }
1809
1810 // handle commands that are not forwarded transparently to effect engine
1811 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001812 if (*replySize < sizeof(int)) {
1813 android_errorWriteLog(0x534e4554, "32095713");
1814 return BAD_VALUE;
1815 }
1816 *(int *)pReplyData = NO_ERROR;
1817 *replySize = sizeof(int);
1818
Eric Laurentca7cc822012-11-19 14:55:58 -08001819 // No need to trylock() here as this function is executed in the binder thread serving a
1820 // particular client process: no risk to block the whole media server process or mixer
1821 // threads if we are stuck here
1822 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001823 // keep local copy of index in case of client corruption b/32220769
1824 const uint32_t clientIndex = mCblk->clientIndex;
1825 const uint32_t serverIndex = mCblk->serverIndex;
1826 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1827 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001828 mCblk->serverIndex = 0;
1829 mCblk->clientIndex = 0;
1830 return BAD_VALUE;
1831 }
1832 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001833 effect_param_t *param = NULL;
1834 for (uint32_t index = serverIndex; index < clientIndex;) {
1835 int *p = (int *)(mBuffer + index);
1836 const int size = *p++;
1837 if (size < 0
1838 || size > EFFECT_PARAM_BUFFER_SIZE
1839 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001841 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001842 break;
1843 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001844
1845 // copy to local memory in case of client corruption b/32220769
1846 param = (effect_param_t *)realloc(param, size);
1847 if (param == NULL) {
1848 ALOGW("command(): out of memory");
1849 status = NO_MEMORY;
1850 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001852 memcpy(param, p, size);
1853
1854 int reply = 0;
1855 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001856 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001857 size,
1858 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001859 &rsize,
1860 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001861
1862 // verify shared memory: server index shouldn't change; client index can't go back.
1863 if (serverIndex != mCblk->serverIndex
1864 || clientIndex > mCblk->clientIndex) {
1865 android_errorWriteLog(0x534e4554, "32220769");
1866 status = BAD_VALUE;
1867 break;
1868 }
1869
Eric Laurentca7cc822012-11-19 14:55:58 -08001870 // stop at first error encountered
1871 if (ret != NO_ERROR) {
1872 status = ret;
1873 *(int *)pReplyData = reply;
1874 break;
1875 } else if (reply != NO_ERROR) {
1876 *(int *)pReplyData = reply;
1877 break;
1878 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001879 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001880 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001881 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001882 mCblk->serverIndex = 0;
1883 mCblk->clientIndex = 0;
1884 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001885 }
1886
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001887 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001888}
1889
1890void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1891{
1892 ALOGV("setControl %p control %d", this, hasControl);
1893
1894 mHasControl = hasControl;
1895 mEnabled = enabled;
1896
1897 if (signal && mEffectClient != 0) {
1898 mEffectClient->controlStatusChanged(hasControl);
1899 }
1900}
1901
1902void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1903 uint32_t cmdSize,
1904 void *pCmdData,
1905 uint32_t replySize,
1906 void *pReplyData)
1907{
1908 if (mEffectClient != 0) {
1909 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1910 }
1911}
1912
1913
1914
1915void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1916{
1917 if (mEffectClient != 0) {
1918 mEffectClient->enableStatusChanged(enabled);
1919 }
1920}
1921
1922status_t AudioFlinger::EffectHandle::onTransact(
1923 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1924{
1925 return BnEffect::onTransact(code, data, reply, flags);
1926}
1927
1928
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001929void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001930{
1931 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1932
Marco Nelissenb2208842014-02-07 14:00:50 -08001933 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001934 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001935 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001936 mHasControl ? "yes" : "no",
1937 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001938 mCblk ? mCblk->clientIndex : 0,
1939 mCblk ? mCblk->serverIndex : 0
1940 );
1941
1942 if (locked) {
1943 mCblk->lock.unlock();
1944 }
1945}
1946
1947#undef LOG_TAG
1948#define LOG_TAG "AudioFlinger::EffectChain"
1949
1950AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001951 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001952 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001953 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001954 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001955{
1956 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1957 if (thread == NULL) {
1958 return;
1959 }
1960 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1961 thread->frameCount();
1962}
1963
1964AudioFlinger::EffectChain::~EffectChain()
1965{
Eric Laurentca7cc822012-11-19 14:55:58 -08001966}
1967
1968// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1969sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1970 effect_descriptor_t *descriptor)
1971{
1972 size_t size = mEffects.size();
1973
1974 for (size_t i = 0; i < size; i++) {
1975 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1976 return mEffects[i];
1977 }
1978 }
1979 return 0;
1980}
1981
1982// getEffectFromId_l() must be called with ThreadBase::mLock held
1983sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1984{
1985 size_t size = mEffects.size();
1986
1987 for (size_t i = 0; i < size; i++) {
1988 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1989 if (id == 0 || mEffects[i]->id() == id) {
1990 return mEffects[i];
1991 }
1992 }
1993 return 0;
1994}
1995
1996// getEffectFromType_l() must be called with ThreadBase::mLock held
1997sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1998 const effect_uuid_t *type)
1999{
2000 size_t size = mEffects.size();
2001
2002 for (size_t i = 0; i < size; i++) {
2003 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2004 return mEffects[i];
2005 }
2006 }
2007 return 0;
2008}
2009
Eric Laurent6c796322019-04-09 14:13:17 -07002010std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2011{
2012 std::vector<int> ids;
2013 Mutex::Autolock _l(mLock);
2014 for (size_t i = 0; i < mEffects.size(); i++) {
2015 ids.push_back(mEffects[i]->id());
2016 }
2017 return ids;
2018}
2019
Eric Laurentca7cc822012-11-19 14:55:58 -08002020void AudioFlinger::EffectChain::clearInputBuffer()
2021{
2022 Mutex::Autolock _l(mLock);
2023 sp<ThreadBase> thread = mThread.promote();
2024 if (thread == 0) {
2025 ALOGW("clearInputBuffer(): cannot promote mixer thread");
2026 return;
2027 }
2028 clearInputBuffer_l(thread);
2029}
2030
2031// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002032void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08002033{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002034 if (mInBuffer == NULL) {
2035 return;
2036 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002037 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08002038 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002039
Mikhail Naganov022b9952017-01-04 16:36:51 -08002040 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
2041 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002042}
2043
2044// Must be called with EffectChain::mLock locked
2045void AudioFlinger::EffectChain::process_l()
2046{
2047 sp<ThreadBase> thread = mThread.promote();
2048 if (thread == 0) {
2049 ALOGW("process_l(): cannot promote mixer thread");
2050 return;
2051 }
2052 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
2053 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002054 // never process effects when:
2055 // - on an OFFLOAD thread
2056 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08002057 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
2058 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08002059 if (!isGlobalSession) {
2060 bool tracksOnSession = (trackCnt() != 0);
2061
2062 if (!tracksOnSession && mTailBufferCount == 0) {
2063 doProcess = false;
2064 }
2065
2066 if (activeTrackCnt() == 0) {
2067 // if no track is active and the effect tail has not been rendered,
2068 // the input buffer must be cleared here as the mixer process will not do it
2069 if (tracksOnSession || mTailBufferCount > 0) {
2070 clearInputBuffer_l(thread);
2071 if (mTailBufferCount > 0) {
2072 mTailBufferCount--;
2073 }
2074 }
2075 }
2076 }
2077
2078 size_t size = mEffects.size();
2079 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002080 // Only the input and output buffers of the chain can be external,
2081 // and 'update' / 'commit' do nothing for allocated buffers, thus
2082 // it's not needed to consider any other buffers here.
2083 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002084 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2085 mOutBuffer->update();
2086 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002087 for (size_t i = 0; i < size; i++) {
2088 mEffects[i]->process();
2089 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002090 mInBuffer->commit();
2091 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2092 mOutBuffer->commit();
2093 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002094 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002095 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002096 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002097 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2098 }
2099 if (doResetVolume) {
2100 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002101 }
2102}
2103
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002104// createEffect_l() must be called with ThreadBase::mLock held
2105status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
2106 ThreadBase *thread,
2107 effect_descriptor_t *desc,
2108 int id,
2109 audio_session_t sessionId,
2110 bool pinned)
2111{
2112 Mutex::Autolock _l(mLock);
2113 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
2114 status_t lStatus = effect->status();
2115 if (lStatus == NO_ERROR) {
2116 lStatus = addEffect_ll(effect);
2117 }
2118 if (lStatus != NO_ERROR) {
2119 effect.clear();
2120 }
2121 return lStatus;
2122}
2123
2124// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002125status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2126{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002127 Mutex::Autolock _l(mLock);
2128 return addEffect_ll(effect);
2129}
2130// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2131status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2132{
Eric Laurentca7cc822012-11-19 14:55:58 -08002133 effect_descriptor_t desc = effect->desc();
2134 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2135
Eric Laurentca7cc822012-11-19 14:55:58 -08002136 effect->setChain(this);
2137 sp<ThreadBase> thread = mThread.promote();
2138 if (thread == 0) {
2139 return NO_INIT;
2140 }
2141 effect->setThread(thread);
2142
2143 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2144 // Auxiliary effects are inserted at the beginning of mEffects vector as
2145 // they are processed first and accumulated in chain input buffer
2146 mEffects.insertAt(effect, 0);
2147
2148 // the input buffer for auxiliary effect contains mono samples in
2149 // 32 bit format. This is to avoid saturation in AudoMixer
2150 // accumulation stage. Saturation is done in EffectModule::process() before
2151 // calling the process in effect engine
2152 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002153 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002154#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002155 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002156 numSamples * sizeof(float), &halBuffer);
2157#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002158 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002159 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002160#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002161 if (result != OK) return result;
2162 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002163 // auxiliary effects output samples to chain input buffer for further processing
2164 // by insert effects
2165 effect->setOutBuffer(mInBuffer);
2166 } else {
2167 // Insert effects are inserted at the end of mEffects vector as they are processed
2168 // after track and auxiliary effects.
2169 // Insert effect order as a function of indicated preference:
2170 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2171 // another effect is present
2172 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2173 // last effect claiming first position
2174 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2175 // first effect claiming last position
2176 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2177 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2178 // already present
2179
2180 size_t size = mEffects.size();
2181 size_t idx_insert = size;
2182 ssize_t idx_insert_first = -1;
2183 ssize_t idx_insert_last = -1;
2184
2185 for (size_t i = 0; i < size; i++) {
2186 effect_descriptor_t d = mEffects[i]->desc();
2187 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2188 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2189 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2190 // check invalid effect chaining combinations
2191 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2192 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2193 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2194 desc.name, d.name);
2195 return INVALID_OPERATION;
2196 }
2197 // remember position of first insert effect and by default
2198 // select this as insert position for new effect
2199 if (idx_insert == size) {
2200 idx_insert = i;
2201 }
2202 // remember position of last insert effect claiming
2203 // first position
2204 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2205 idx_insert_first = i;
2206 }
2207 // remember position of first insert effect claiming
2208 // last position
2209 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2210 idx_insert_last == -1) {
2211 idx_insert_last = i;
2212 }
2213 }
2214 }
2215
2216 // modify idx_insert from first position if needed
2217 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2218 if (idx_insert_last != -1) {
2219 idx_insert = idx_insert_last;
2220 } else {
2221 idx_insert = size;
2222 }
2223 } else {
2224 if (idx_insert_first != -1) {
2225 idx_insert = idx_insert_first + 1;
2226 }
2227 }
2228
2229 // always read samples from chain input buffer
2230 effect->setInBuffer(mInBuffer);
2231
2232 // if last effect in the chain, output samples to chain
2233 // output buffer, otherwise to chain input buffer
2234 if (idx_insert == size) {
2235 if (idx_insert != 0) {
2236 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2237 mEffects[idx_insert-1]->configure();
2238 }
2239 effect->setOutBuffer(mOutBuffer);
2240 } else {
2241 effect->setOutBuffer(mInBuffer);
2242 }
2243 mEffects.insertAt(effect, idx_insert);
2244
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002245 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002246 idx_insert);
2247 }
2248 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002249
Eric Laurentca7cc822012-11-19 14:55:58 -08002250 return NO_ERROR;
2251}
2252
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002253// removeEffect_l() must be called with ThreadBase::mLock held
2254size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2255 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002256{
2257 Mutex::Autolock _l(mLock);
2258 size_t size = mEffects.size();
2259 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2260
2261 for (size_t i = 0; i < size; i++) {
2262 if (effect == mEffects[i]) {
2263 // calling stop here will remove pre-processing effect from the audio HAL.
2264 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2265 // the middle of a read from audio HAL
2266 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2267 mEffects[i]->state() == EffectModule::STOPPING) {
2268 mEffects[i]->stop();
2269 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002270 if (release) {
2271 mEffects[i]->release_l();
2272 }
2273
Mikhail Naganov022b9952017-01-04 16:36:51 -08002274 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002275 if (i == size - 1 && i != 0) {
2276 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2277 mEffects[i - 1]->configure();
2278 }
2279 }
2280 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002281 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002282 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002283
Eric Laurentca7cc822012-11-19 14:55:58 -08002284 break;
2285 }
2286 }
2287
2288 return mEffects.size();
2289}
2290
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002291// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002292void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2293{
2294 size_t size = mEffects.size();
2295 for (size_t i = 0; i < size; i++) {
2296 mEffects[i]->setDevice(device);
2297 }
2298}
2299
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002300// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002301void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2302{
2303 size_t size = mEffects.size();
2304 for (size_t i = 0; i < size; i++) {
2305 mEffects[i]->setMode(mode);
2306 }
2307}
2308
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002309// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002310void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2311{
2312 size_t size = mEffects.size();
2313 for (size_t i = 0; i < size; i++) {
2314 mEffects[i]->setAudioSource(source);
2315 }
2316}
2317
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002318// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002319bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002320{
2321 uint32_t newLeft = *left;
2322 uint32_t newRight = *right;
2323 bool hasControl = false;
2324 int ctrlIdx = -1;
2325 size_t size = mEffects.size();
2326
2327 // first update volume controller
2328 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002329 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002330 ctrlIdx = i - 1;
2331 hasControl = true;
2332 break;
2333 }
2334 }
2335
Eric Laurentfa1e1232016-08-02 19:01:49 -07002336 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002337 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002338 if (hasControl) {
2339 *left = mNewLeftVolume;
2340 *right = mNewRightVolume;
2341 }
2342 return hasControl;
2343 }
2344
2345 mVolumeCtrlIdx = ctrlIdx;
2346 mLeftVolume = newLeft;
2347 mRightVolume = newRight;
2348
2349 // second get volume update from volume controller
2350 if (ctrlIdx >= 0) {
2351 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2352 mNewLeftVolume = newLeft;
2353 mNewRightVolume = newRight;
2354 }
2355 // then indicate volume to all other effects in chain.
2356 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002357 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002358 uint32_t lVol = newLeft;
2359 uint32_t rVol = newRight;
2360
2361 for (size_t i = 0; i < size; i++) {
2362 if ((int)i == ctrlIdx) {
2363 continue;
2364 }
2365 // this also works for ctrlIdx == -1 when there is no volume controller
2366 if ((int)i > ctrlIdx) {
2367 lVol = *left;
2368 rVol = *right;
2369 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002370 // Pass requested volume directly if this is volume monitor module
2371 if (mEffects[i]->isVolumeMonitor()) {
2372 mEffects[i]->setVolume(left, right, false);
2373 } else {
2374 mEffects[i]->setVolume(&lVol, &rVol, false);
2375 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002376 }
2377 *left = newLeft;
2378 *right = newRight;
2379
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002380 setVolumeForOutput_l(*left, *right);
2381
Eric Laurentca7cc822012-11-19 14:55:58 -08002382 return hasControl;
2383}
2384
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002385// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002386void AudioFlinger::EffectChain::resetVolume_l()
2387{
Eric Laurente7449bf2016-08-03 18:44:07 -07002388 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2389 uint32_t left = mLeftVolume;
2390 uint32_t right = mRightVolume;
2391 (void)setVolume_l(&left, &right, true);
2392 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002393}
2394
Eric Laurent1b928682014-10-02 19:41:47 -07002395void AudioFlinger::EffectChain::syncHalEffectsState()
2396{
2397 Mutex::Autolock _l(mLock);
2398 for (size_t i = 0; i < mEffects.size(); i++) {
2399 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2400 mEffects[i]->state() == EffectModule::STOPPING) {
2401 mEffects[i]->addEffectToHal_l();
2402 }
2403 }
2404}
2405
Eric Laurentca7cc822012-11-19 14:55:58 -08002406void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2407{
Eric Laurentca7cc822012-11-19 14:55:58 -08002408 String8 result;
2409
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002410 const size_t numEffects = mEffects.size();
2411 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002412
Marco Nelissenb2208842014-02-07 14:00:50 -08002413 if (numEffects) {
2414 bool locked = AudioFlinger::dumpTryLock(mLock);
2415 // failed to lock - AudioFlinger is probably deadlocked
2416 if (!locked) {
2417 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002418 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002419
Andy Hungbded9c82017-11-30 18:47:35 -08002420 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2421 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2422 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2423 (int)inBufferStr.size(), "In buffer ",
2424 (int)outBufferStr.size(), "Out buffer ");
2425 result.appendFormat("\t%s %s %d\n",
2426 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002427 write(fd, result.string(), result.size());
2428
2429 for (size_t i = 0; i < numEffects; ++i) {
2430 sp<EffectModule> effect = mEffects[i];
2431 if (effect != 0) {
2432 effect->dump(fd, args);
2433 }
2434 }
2435
2436 if (locked) {
2437 mLock.unlock();
2438 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002439 } else {
2440 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002441 }
2442}
2443
2444// must be called with ThreadBase::mLock held
2445void AudioFlinger::EffectChain::setEffectSuspended_l(
2446 const effect_uuid_t *type, bool suspend)
2447{
2448 sp<SuspendedEffectDesc> desc;
2449 // use effect type UUID timelow as key as there is no real risk of identical
2450 // timeLow fields among effect type UUIDs.
2451 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2452 if (suspend) {
2453 if (index >= 0) {
2454 desc = mSuspendedEffects.valueAt(index);
2455 } else {
2456 desc = new SuspendedEffectDesc();
2457 desc->mType = *type;
2458 mSuspendedEffects.add(type->timeLow, desc);
2459 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2460 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002461
Eric Laurentca7cc822012-11-19 14:55:58 -08002462 if (desc->mRefCount++ == 0) {
2463 sp<EffectModule> effect = getEffectIfEnabled(type);
2464 if (effect != 0) {
2465 desc->mEffect = effect;
2466 effect->setSuspended(true);
2467 effect->setEnabled(false);
2468 }
2469 }
2470 } else {
2471 if (index < 0) {
2472 return;
2473 }
2474 desc = mSuspendedEffects.valueAt(index);
2475 if (desc->mRefCount <= 0) {
2476 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002477 desc->mRefCount = 0;
2478 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002479 }
2480 if (--desc->mRefCount == 0) {
2481 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2482 if (desc->mEffect != 0) {
2483 sp<EffectModule> effect = desc->mEffect.promote();
2484 if (effect != 0) {
2485 effect->setSuspended(false);
2486 effect->lock();
2487 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002488 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002489 effect->setEnabled_l(handle->enabled());
2490 }
2491 effect->unlock();
2492 }
2493 desc->mEffect.clear();
2494 }
2495 mSuspendedEffects.removeItemsAt(index);
2496 }
2497 }
2498}
2499
2500// must be called with ThreadBase::mLock held
2501void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2502{
2503 sp<SuspendedEffectDesc> desc;
2504
2505 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2506 if (suspend) {
2507 if (index >= 0) {
2508 desc = mSuspendedEffects.valueAt(index);
2509 } else {
2510 desc = new SuspendedEffectDesc();
2511 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2512 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2513 }
2514 if (desc->mRefCount++ == 0) {
2515 Vector< sp<EffectModule> > effects;
2516 getSuspendEligibleEffects(effects);
2517 for (size_t i = 0; i < effects.size(); i++) {
2518 setEffectSuspended_l(&effects[i]->desc().type, true);
2519 }
2520 }
2521 } else {
2522 if (index < 0) {
2523 return;
2524 }
2525 desc = mSuspendedEffects.valueAt(index);
2526 if (desc->mRefCount <= 0) {
2527 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2528 desc->mRefCount = 1;
2529 }
2530 if (--desc->mRefCount == 0) {
2531 Vector<const effect_uuid_t *> types;
2532 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2533 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2534 continue;
2535 }
2536 types.add(&mSuspendedEffects.valueAt(i)->mType);
2537 }
2538 for (size_t i = 0; i < types.size(); i++) {
2539 setEffectSuspended_l(types[i], false);
2540 }
2541 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2542 mSuspendedEffects.keyAt(index));
2543 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2544 }
2545 }
2546}
2547
2548
2549// The volume effect is used for automated tests only
2550#ifndef OPENSL_ES_H_
2551static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2552 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2553const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2554#endif //OPENSL_ES_H_
2555
Eric Laurentd8365c52017-07-16 15:27:05 -07002556/* static */
2557bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2558{
2559 // Only NS and AEC are suspended when BtNRec is off
2560 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2561 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2562 return true;
2563 }
2564 return false;
2565}
2566
Eric Laurentca7cc822012-11-19 14:55:58 -08002567bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2568{
2569 // auxiliary effects and visualizer are never suspended on output mix
2570 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2571 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2572 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002573 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2574 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002575 return false;
2576 }
2577 return true;
2578}
2579
2580void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2581 Vector< sp<AudioFlinger::EffectModule> > &effects)
2582{
2583 effects.clear();
2584 for (size_t i = 0; i < mEffects.size(); i++) {
2585 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2586 effects.add(mEffects[i]);
2587 }
2588 }
2589}
2590
2591sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2592 const effect_uuid_t *type)
2593{
2594 sp<EffectModule> effect = getEffectFromType_l(type);
2595 return effect != 0 && effect->isEnabled() ? effect : 0;
2596}
2597
2598void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2599 bool enabled)
2600{
2601 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2602 if (enabled) {
2603 if (index < 0) {
2604 // if the effect is not suspend check if all effects are suspended
2605 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2606 if (index < 0) {
2607 return;
2608 }
2609 if (!isEffectEligibleForSuspend(effect->desc())) {
2610 return;
2611 }
2612 setEffectSuspended_l(&effect->desc().type, enabled);
2613 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2614 if (index < 0) {
2615 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2616 return;
2617 }
2618 }
2619 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2620 effect->desc().type.timeLow);
2621 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002622 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002623 if (desc->mEffect == 0) {
2624 desc->mEffect = effect;
2625 effect->setEnabled(false);
2626 effect->setSuspended(true);
2627 }
2628 } else {
2629 if (index < 0) {
2630 return;
2631 }
2632 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2633 effect->desc().type.timeLow);
2634 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2635 desc->mEffect.clear();
2636 effect->setSuspended(false);
2637 }
2638}
2639
Eric Laurent5baf2af2013-09-12 17:37:00 -07002640bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002641{
2642 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002643 return isNonOffloadableEnabled_l();
2644}
2645
2646bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2647{
Eric Laurent813e2a72013-08-31 12:59:48 -07002648 size_t size = mEffects.size();
2649 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002650 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002651 return true;
2652 }
2653 }
2654 return false;
2655}
2656
Eric Laurentaaa44472014-09-12 17:41:50 -07002657void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2658{
2659 Mutex::Autolock _l(mLock);
2660 mThread = thread;
2661 for (size_t i = 0; i < mEffects.size(); i++) {
2662 mEffects[i]->setThread(thread);
2663 }
2664}
2665
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002666void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2667{
2668 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2669 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2670 }
2671 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2672 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2673 }
2674}
2675
2676void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2677{
2678 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2679 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2680 }
2681 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2682 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2683 }
2684}
2685
2686bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002687{
2688 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002689 for (const auto &effect : mEffects) {
2690 if (effect->isProcessImplemented()) {
2691 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002692 }
2693 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002694 // Allow effects without processing.
2695 return true;
2696}
2697
2698bool AudioFlinger::EffectChain::isFastCompatible() const
2699{
2700 Mutex::Autolock _l(mLock);
2701 for (const auto &effect : mEffects) {
2702 if (effect->isProcessImplemented()
2703 && effect->isImplementationSoftware()) {
2704 return false;
2705 }
2706 }
2707 // Allow effects without processing or hw accelerated effects.
2708 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002709}
2710
2711// isCompatibleWithThread_l() must be called with thread->mLock held
2712bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2713{
2714 Mutex::Autolock _l(mLock);
2715 for (size_t i = 0; i < mEffects.size(); i++) {
2716 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2717 return false;
2718 }
2719 }
2720 return true;
2721}
2722
Glenn Kasten63238ef2015-03-02 15:50:29 -08002723} // namespace android