blob: 3c4fbba15675911eb0694fa620371aa84abc9663 [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>
27#include <system/audio_effects/effect_ns.h>
28#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080029#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080030#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070031#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070032#include <media/audiohal/EffectHalInterface.h>
33#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070034#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080035
36#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080037
38// ----------------------------------------------------------------------------
39
40// Note: the following macro is used for extremely verbose logging message. In
41// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
42// 0; but one side effect of this is to turn all LOGV's as well. Some messages
43// are so verbose that we want to suppress them even when we have ALOG_ASSERT
44// turned on. Do not uncomment the #def below unless you really know what you
45// are doing and want to see all of the extremely verbose messages.
46//#define VERY_VERY_VERBOSE_LOGGING
47#ifdef VERY_VERY_VERBOSE_LOGGING
48#define ALOGVV ALOGV
49#else
50#define ALOGVV(a...) do { } while(0)
51#endif
52
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090053#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
54
Eric Laurentca7cc822012-11-19 14:55:58 -080055namespace android {
56
57// ----------------------------------------------------------------------------
58// EffectModule implementation
59// ----------------------------------------------------------------------------
60
61#undef LOG_TAG
62#define LOG_TAG "AudioFlinger::EffectModule"
63
64AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
65 const wp<AudioFlinger::EffectChain>& chain,
66 effect_descriptor_t *desc,
67 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080068 audio_session_t sessionId,
69 bool pinned)
70 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080071 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
72 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080073 // clear mConfig to ensure consistent initial value of buffer framecount
74 // in case buffers are associated by setInBuffer() or setOutBuffer()
75 // prior to configure().
76 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080077 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080078 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
79 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070080 mSuspended(false),
Andy Hung62aef7d2017-12-14 14:50:40 -080081 mOffloaded(false),
Eric Laurentaaa44472014-09-12 17:41:50 -070082 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070083#ifdef FLOAT_EFFECT_CHAIN
84 , mSupportsFloat(false)
85#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080086{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080087 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080088 int lStatus;
89
90 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070091 mStatus = -ENODEV;
92 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070093 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070094 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070095 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070096 mStatus = effectsFactory->createEffect(
97 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
98 }
99 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800100
101 if (mStatus != NO_ERROR) {
102 return;
103 }
104 lStatus = init();
105 if (lStatus < 0) {
106 mStatus = lStatus;
107 goto Error;
108 }
109
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700111 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800112
Eric Laurentca7cc822012-11-19 14:55:58 -0800113 return;
114Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700115 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800116 ALOGV("Constructor Error %d", mStatus);
117}
118
119AudioFlinger::EffectModule::~EffectModule()
120{
121 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700122 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700123 char uuidStr[64];
124 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
125 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
126 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800127 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800128 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800129
Eric Laurentca7cc822012-11-19 14:55:58 -0800130}
131
132status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
133{
134 status_t status;
135
136 Mutex::Autolock _l(mLock);
137 int priority = handle->priority();
138 size_t size = mHandles.size();
139 EffectHandle *controlHandle = NULL;
140 size_t i;
141 for (i = 0; i < size; i++) {
142 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800143 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800144 continue;
145 }
146 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700147 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800148 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700149 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800150 if (h->priority() <= priority) {
151 break;
152 }
153 }
154 // if inserted in first place, move effect control from previous owner to this handle
155 if (i == 0) {
156 bool enabled = false;
157 if (controlHandle != NULL) {
158 enabled = controlHandle->enabled();
159 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
160 }
161 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
162 status = NO_ERROR;
163 } else {
164 status = ALREADY_EXISTS;
165 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700166 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800167 mHandles.insertAt(handle, i);
168 return status;
169}
170
Eric Laurent6c796322019-04-09 14:13:17 -0700171status_t AudioFlinger::EffectModule::updatePolicyState()
172{
173 status_t status = NO_ERROR;
174 bool doRegister = false;
175 bool registered = false;
176 bool doEnable = false;
177 bool enabled = false;
178 audio_io_handle_t io;
179 uint32_t strategy;
180
181 {
182 Mutex::Autolock _l(mLock);
183 // register effect when first handle is attached and unregister when last handle is removed
184 if (mPolicyRegistered != mHandles.size() > 0) {
185 doRegister = true;
186 mPolicyRegistered = mHandles.size() > 0;
187 if (mPolicyRegistered) {
188 sp <EffectChain> chain = mChain.promote();
189 sp <ThreadBase> thread = mThread.promote();
190
191 if (thread == nullptr || chain == nullptr) {
192 return INVALID_OPERATION;
193 }
194 io = thread->id();
195 strategy = chain->strategy();
196 }
197 }
198 // enable effect when registered according to enable state requested by controlling handle
199 if (mHandles.size() > 0) {
200 EffectHandle *handle = controlHandle_l();
201 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
202 doEnable = true;
203 mPolicyEnabled = handle->enabled();
204 }
205 }
206 registered = mPolicyRegistered;
207 enabled = mPolicyEnabled;
208 mPolicyLock.lock();
209 }
210 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
211 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
212 if (doRegister) {
213 if (registered) {
214 status = AudioSystem::registerEffect(
215 &mDescriptor,
216 io,
217 strategy,
218 mSessionId,
219 mId);
220 } else {
221 status = AudioSystem::unregisterEffect(mId);
222 }
223 }
224 if (registered && doEnable) {
225 status = AudioSystem::setEffectEnabled(mId, enabled);
226 }
227 mPolicyLock.unlock();
228
229 return status;
230}
231
232
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800233ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800234{
235 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800236 return removeHandle_l(handle);
237}
238
239ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
240{
Eric Laurentca7cc822012-11-19 14:55:58 -0800241 size_t size = mHandles.size();
242 size_t i;
243 for (i = 0; i < size; i++) {
244 if (mHandles[i] == handle) {
245 break;
246 }
247 }
248 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800249 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
250 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800251 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800252 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800253
254 mHandles.removeAt(i);
255 // if removed from first place, move effect control from this handle to next in line
256 if (i == 0) {
257 EffectHandle *h = controlHandle_l();
258 if (h != NULL) {
259 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
260 }
261 }
262
263 // Prevent calls to process() and other functions on effect interface from now on.
264 // The effect engine will be released by the destructor when the last strong reference on
265 // this object is released which can happen after next process is called.
266 if (mHandles.size() == 0 && !mPinned) {
267 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800268 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800269 }
270
271 return mHandles.size();
272}
273
274// must be called with EffectModule::mLock held
275AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
276{
277 // the first valid handle in the list has control over the module
278 for (size_t i = 0; i < mHandles.size(); i++) {
279 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800280 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800281 return h;
282 }
283 }
284
285 return NULL;
286}
287
Eric Laurentf10c7092016-12-06 17:09:56 -0800288// unsafe method called when the effect parent thread has been destroyed
289ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
290{
291 ALOGV("disconnect() %p handle %p", this, handle);
292 Mutex::Autolock _l(mLock);
293 ssize_t numHandles = removeHandle_l(handle);
294 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurentf10c7092016-12-06 17:09:56 -0800295 sp<AudioFlinger> af = mAudioFlinger.promote();
296 if (af != 0) {
297 mLock.unlock();
298 af->updateOrphanEffectChains(this);
299 mLock.lock();
300 }
301 }
302 return numHandles;
303}
304
Eric Laurentfa1e1232016-08-02 19:01:49 -0700305bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800306 Mutex::Autolock _l(mLock);
307
Eric Laurentfa1e1232016-08-02 19:01:49 -0700308 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800309 switch (mState) {
310 case RESTART:
311 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700312 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800313
314 case STARTING:
315 // clear auxiliary effect input buffer for next accumulation
316 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
317 memset(mConfig.inputCfg.buffer.raw,
318 0,
319 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
320 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700321 if (start_l() == NO_ERROR) {
322 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700323 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700324 } else {
325 mState = IDLE;
326 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800327 break;
328 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900329 // volume control for offload and direct threads must take effect immediately.
330 if (stop_l() == NO_ERROR
331 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700332 mDisableWaitCnt = mMaxDisableWaitCnt;
333 } else {
334 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
335 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800336 mState = STOPPED;
337 break;
338 case STOPPED:
339 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
340 // turn off sequence.
341 if (--mDisableWaitCnt == 0) {
342 reset_l();
343 mState = IDLE;
344 }
345 break;
346 default: //IDLE , ACTIVE, DESTROYED
347 break;
348 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700349
350 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800351}
352
353void AudioFlinger::EffectModule::process()
354{
355 Mutex::Autolock _l(mLock);
356
Mikhail Naganov022b9952017-01-04 16:36:51 -0800357 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800358 return;
359 }
360
rago94a1ee82017-07-21 15:11:02 -0700361 const uint32_t inChannelCount =
362 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
363 const uint32_t outChannelCount =
364 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
365 const bool auxType =
366 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
367
Andy Hungfa69ca32017-11-30 10:07:53 -0800368 // safeInputOutputSampleCount is 0 if the channel count between input and output
369 // buffers do not match. This prevents automatic accumulation or copying between the
370 // input and output effect buffers without an intermediary effect process.
371 // TODO: consider implementing channel conversion.
372 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700373 mInChannelCountRequested != mOutChannelCountRequested ? 0
374 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800375 mConfig.inputCfg.buffer.frameCount,
376 mConfig.outputCfg.buffer.frameCount);
377 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
378#ifdef FLOAT_EFFECT_CHAIN
379 accumulate_float(
380 mConfig.outputCfg.buffer.f32,
381 mConfig.inputCfg.buffer.f32,
382 safeInputOutputSampleCount);
383#else
384 accumulate_i16(
385 mConfig.outputCfg.buffer.s16,
386 mConfig.inputCfg.buffer.s16,
387 safeInputOutputSampleCount);
388#endif
389 };
390 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
391#ifdef FLOAT_EFFECT_CHAIN
392 memcpy(
393 mConfig.outputCfg.buffer.f32,
394 mConfig.inputCfg.buffer.f32,
395 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
396
397#else
398 memcpy(
399 mConfig.outputCfg.buffer.s16,
400 mConfig.inputCfg.buffer.s16,
401 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
402#endif
403 };
404
Eric Laurentca7cc822012-11-19 14:55:58 -0800405 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700406 int ret;
407 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700408 if (auxType) {
409 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800410 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700411#ifdef FLOAT_EFFECT_CHAIN
412 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800413#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700414 // Do in-place float conversion for auxiliary effect input buffer.
415 static_assert(sizeof(float) <= sizeof(int32_t),
416 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
417
Andy Hungfa69ca32017-11-30 10:07:53 -0800418 memcpy_to_float_from_q4_27(
419 mConfig.inputCfg.buffer.f32,
420 mConfig.inputCfg.buffer.s32,
421 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800422#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800423 } else
Andy Hung116a4982017-11-30 10:15:08 -0800424#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800425 {
Andy Hung116a4982017-11-30 10:15:08 -0800426#ifdef FLOAT_AUX
427 memcpy_to_i16_from_float(
428 mConfig.inputCfg.buffer.s16,
429 mConfig.inputCfg.buffer.f32,
430 mConfig.inputCfg.buffer.frameCount);
431#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800432 memcpy_to_i16_from_q4_27(
433 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700434 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800435 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800436#endif
rago94a1ee82017-07-21 15:11:02 -0700437 }
rago94a1ee82017-07-21 15:11:02 -0700438 }
439#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800440 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
441 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
442
443 if (!auxType && mInChannelCountRequested != inChannelCount) {
444 adjust_channels(
445 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
446 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
447 sizeof(float),
448 sizeof(float)
449 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
450 inBuffer = mInConversionBuffer;
451 }
452 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
453 && mOutChannelCountRequested != outChannelCount) {
454 adjust_selected_channels(
455 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
456 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
457 sizeof(float),
458 sizeof(float)
459 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
460 outBuffer = mOutConversionBuffer;
461 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800462 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
463 if (!auxType) {
464 if (mInConversionBuffer.get() == nullptr) {
465 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
466 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700467 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800468 memcpy_to_i16_from_float(
469 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800470 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800471 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800472 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700473 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800474 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
475 if (mOutConversionBuffer.get() == nullptr) {
476 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
477 goto data_bypass;
478 }
479 memcpy_to_i16_from_float(
480 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800481 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800482 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800483 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700484 }
485 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800486#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800487 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800488#ifdef FLOAT_EFFECT_CHAIN
489 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800490 sp<EffectBufferHalInterface> target =
491 mOutChannelCountRequested != outChannelCount
492 ? mOutConversionBuffer : mOutBuffer;
493
Andy Hungfa69ca32017-11-30 10:07:53 -0800494 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800495 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800496 mOutConversionBuffer->audioBuffer()->s16,
497 outChannelCount * mConfig.outputCfg.buffer.frameCount);
498 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800499 if (mOutChannelCountRequested != outChannelCount) {
500 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
501 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
502 sizeof(float),
503 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
504 }
rago94a1ee82017-07-21 15:11:02 -0700505#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700506 } else {
rago94a1ee82017-07-21 15:11:02 -0700507#ifdef FLOAT_EFFECT_CHAIN
508 data_bypass:
509#endif
510 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800511 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700512 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800513 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700514 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800515 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700516 }
517 }
518 ret = -ENODATA;
519 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800520
Eric Laurentca7cc822012-11-19 14:55:58 -0800521 // force transition to IDLE state when engine is ready
522 if (mState == STOPPED && ret == -ENODATA) {
523 mDisableWaitCnt = 1;
524 }
525
526 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700527 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800528#ifdef FLOAT_AUX
529 const size_t size =
530 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
531#else
rago94a1ee82017-07-21 15:11:02 -0700532 const size_t size =
533 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800534#endif
rago94a1ee82017-07-21 15:11:02 -0700535 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800536 }
537 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700538 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800539 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
540 // If an insert effect is idle and input buffer is different from output buffer,
541 // accumulate input onto output
542 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800543 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700544 // similar handling with data_bypass above.
545 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
546 accumulateInputToOutput();
547 } else { // EFFECT_BUFFER_ACCESS_WRITE
548 copyInputToOutput();
549 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800550 }
551 }
552}
553
554void AudioFlinger::EffectModule::reset_l()
555{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700556 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800557 return;
558 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700559 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800560}
561
562status_t AudioFlinger::EffectModule::configure()
563{
rago94a1ee82017-07-21 15:11:02 -0700564 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700565 status_t status;
566 sp<ThreadBase> thread;
567 uint32_t size;
568 audio_channel_mask_t channelMask;
569
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700570 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700571 status = NO_INIT;
572 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800573 }
574
Eric Laurentd0ebb532013-04-02 16:41:41 -0700575 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800576 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700577 status = DEAD_OBJECT;
578 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 }
580
581 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800582 // TODO: handle configuration of input (record) SW effects above the HAL,
583 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
584 // in which case input channel masks should be used here.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700585 channelMask = thread->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800586 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700587 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800588
589 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800590 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
591 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
592 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
593 mConfig.inputCfg.channels);
594 }
595#ifndef MULTICHANNEL_EFFECT_CHAIN
596 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
597 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
598 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
599 mConfig.outputCfg.channels);
600 }
601#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800602 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800603#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700604 // TODO: Update this logic when multichannel effects are implemented.
605 // For offloaded tracks consider mono output as stereo for proper effect initialization
606 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
607 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
608 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
609 ALOGV("Overriding effect input and output as STEREO");
610 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800611#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800612 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800613 mInChannelCountRequested =
614 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
615 mOutChannelCountRequested =
616 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700617
rago94a1ee82017-07-21 15:11:02 -0700618 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
619 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900620
621 // Don't use sample rate for thread if effect isn't offloadable.
622 if ((thread->type() == ThreadBase::OFFLOAD) && !isOffloaded()) {
623 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
624 ALOGV("Overriding effect input as 48kHz");
625 } else {
626 mConfig.inputCfg.samplingRate = thread->sampleRate();
627 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800628 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
629 mConfig.inputCfg.bufferProvider.cookie = NULL;
630 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
631 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
632 mConfig.outputCfg.bufferProvider.cookie = NULL;
633 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
634 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
635 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
636 // Insert effect:
637 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
638 // always overwrites output buffer: input buffer == output buffer
639 // - in other sessions:
640 // last effect in the chain accumulates in output buffer: input buffer != output buffer
641 // other effect: overwrites output buffer: input buffer == output buffer
642 // Auxiliary effect:
643 // accumulates in output buffer: input buffer != output buffer
644 // Therefore: accumulate <=> input buffer != output buffer
645 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
646 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
647 } else {
648 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
649 }
650 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
651 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
652 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
653 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
654
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700655 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800656 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
657
658 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700659 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700660 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800661 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700662 &mConfig,
663 &size,
664 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700665 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800666 status = cmdStatus;
667 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800668
669#ifdef MULTICHANNEL_EFFECT_CHAIN
670 if (status != NO_ERROR &&
Andy Hunge6a61a52018-04-06 18:55:26 -0700671 thread->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800672 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
673 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
674 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700675 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
676 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800677 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
678 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
679 }
680 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
681 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
682 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
683 }
684 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700685 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800686 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700687 &mConfig,
688 &size,
689 &cmdStatus);
690 if (status == NO_ERROR) {
691 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800692 }
693 }
694#endif
695
696#ifdef FLOAT_EFFECT_CHAIN
697 if (status == NO_ERROR) {
698 mSupportsFloat = true;
699 }
700
701 if (status != NO_ERROR) {
702 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
703 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
704 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
705 size = sizeof(int);
706 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
707 sizeof(mConfig),
708 &mConfig,
709 &size,
710 &cmdStatus);
711 if (status == NO_ERROR) {
712 status = cmdStatus;
713 }
714 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700715 mSupportsFloat = false;
716 ALOGVV("config worked with 16 bit");
717 } else {
718 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800719 }
rago94a1ee82017-07-21 15:11:02 -0700720 }
721#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800722
rago94a1ee82017-07-21 15:11:02 -0700723 if (status == NO_ERROR) {
724 // Establish Buffer strategy
725 setInBuffer(mInBuffer);
726 setOutBuffer(mOutBuffer);
727
728 // Update visualizer latency
729 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
730 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
731 effect_param_t *p = (effect_param_t *)buf32;
732
733 p->psize = sizeof(uint32_t);
734 p->vsize = sizeof(uint32_t);
735 size = sizeof(int);
736 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
737
738 uint32_t latency = 0;
739 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
740 if (pbt != NULL) {
741 latency = pbt->latency_l();
742 }
743
744 *((int32_t *)p->data + 1)= latency;
745 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
746 sizeof(effect_param_t) + 8,
747 &buf32,
748 &size,
749 &cmdStatus);
750 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800751 }
752
Andy Hung05083ac2017-12-14 15:00:28 -0800753 // mConfig.outputCfg.buffer.frameCount cannot be zero.
754 mMaxDisableWaitCnt = (uint32_t)std::max(
755 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
756 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
757 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800758
Eric Laurentd0ebb532013-04-02 16:41:41 -0700759exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800760 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700761 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700762 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800763 return status;
764}
765
766status_t AudioFlinger::EffectModule::init()
767{
768 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700769 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800770 return NO_INIT;
771 }
772 status_t cmdStatus;
773 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700774 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
775 0,
776 NULL,
777 &size,
778 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800779 if (status == 0) {
780 status = cmdStatus;
781 }
782 return status;
783}
784
Eric Laurent1b928682014-10-02 19:41:47 -0700785void AudioFlinger::EffectModule::addEffectToHal_l()
786{
787 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
788 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
789 sp<ThreadBase> thread = mThread.promote();
790 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700791 sp<StreamHalInterface> stream = thread->stream();
792 if (stream != 0) {
793 status_t result = stream->addEffect(mEffectInterface);
794 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700795 }
796 }
797 }
798}
799
Eric Laurentfa1e1232016-08-02 19:01:49 -0700800// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800801status_t AudioFlinger::EffectModule::start()
802{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700803 sp<EffectChain> chain;
804 status_t status;
805 {
806 Mutex::Autolock _l(mLock);
807 status = start_l();
808 if (status == NO_ERROR) {
809 chain = mChain.promote();
810 }
811 }
812 if (chain != 0) {
813 chain->resetVolume_l();
814 }
815 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800816}
817
818status_t AudioFlinger::EffectModule::start_l()
819{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700820 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800821 return NO_INIT;
822 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700823 if (mStatus != NO_ERROR) {
824 return mStatus;
825 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800826 status_t cmdStatus;
827 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700828 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
829 0,
830 NULL,
831 &size,
832 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800833 if (status == 0) {
834 status = cmdStatus;
835 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700836 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700837 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 }
839 return status;
840}
841
842status_t AudioFlinger::EffectModule::stop()
843{
844 Mutex::Autolock _l(mLock);
845 return stop_l();
846}
847
848status_t AudioFlinger::EffectModule::stop_l()
849{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700850 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800851 return NO_INIT;
852 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700853 if (mStatus != NO_ERROR) {
854 return mStatus;
855 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800856 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800857 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900858
859 if (isVolumeControl() && isOffloadedOrDirect()) {
860 sp<EffectChain>chain = mChain.promote();
861 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
862 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
863 mSetVolumeReentrantTid = gettid();
864 chain->resetVolume_l();
865 mSetVolumeReentrantTid = INVALID_PID;
866 }
867
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700868 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
869 0,
870 NULL,
871 &size,
872 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800873 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800874 status = cmdStatus;
875 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800876 if (status == NO_ERROR) {
877 status = remove_effect_from_hal_l();
878 }
879 return status;
880}
881
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800882// must be called with EffectChain::mLock held
883void AudioFlinger::EffectModule::release_l()
884{
885 if (mEffectInterface != 0) {
886 remove_effect_from_hal_l();
887 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800888 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800889 mEffectInterface.clear();
890 }
891}
892
Eric Laurentbfb1b832013-01-07 09:53:42 -0800893status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
894{
895 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
896 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800897 sp<ThreadBase> thread = mThread.promote();
898 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700899 sp<StreamHalInterface> stream = thread->stream();
900 if (stream != 0) {
901 status_t result = stream->removeEffect(mEffectInterface);
902 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800903 }
904 }
905 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800906 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800907}
908
Andy Hunge4a1d912016-08-17 14:11:13 -0700909// round up delta valid if value and divisor are positive.
910template <typename T>
911static T roundUpDelta(const T &value, const T &divisor) {
912 T remainder = value % divisor;
913 return remainder == 0 ? 0 : divisor - remainder;
914}
915
Eric Laurentca7cc822012-11-19 14:55:58 -0800916status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
917 uint32_t cmdSize,
918 void *pCmdData,
919 uint32_t *replySize,
920 void *pReplyData)
921{
922 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700923 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800924
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700925 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800926 return NO_INIT;
927 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700928 if (mStatus != NO_ERROR) {
929 return mStatus;
930 }
Andy Hung110bc952016-06-20 15:22:52 -0700931 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700932 (sizeof(effect_param_t) > cmdSize ||
933 ((effect_param_t *)pCmdData)->psize > cmdSize
934 - sizeof(effect_param_t))) {
935 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800936 android_errorWriteLog(0x534e4554, "33003822");
937 return -EINVAL;
938 }
939 if (cmdCode == EFFECT_CMD_GET_PARAM &&
940 (*replySize < sizeof(effect_param_t) ||
941 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
942 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700943 return -EINVAL;
944 }
ragoe2759072016-11-22 18:02:48 -0800945 if (cmdCode == EFFECT_CMD_GET_PARAM &&
946 (sizeof(effect_param_t) > *replySize
947 || ((effect_param_t *)pCmdData)->psize > *replySize
948 - sizeof(effect_param_t)
949 || ((effect_param_t *)pCmdData)->vsize > *replySize
950 - sizeof(effect_param_t)
951 - ((effect_param_t *)pCmdData)->psize
952 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
953 *replySize
954 - sizeof(effect_param_t)
955 - ((effect_param_t *)pCmdData)->psize
956 - ((effect_param_t *)pCmdData)->vsize)) {
957 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
958 android_errorWriteLog(0x534e4554, "32705438");
959 return -EINVAL;
960 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700961 if ((cmdCode == EFFECT_CMD_SET_PARAM
962 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
963 (sizeof(effect_param_t) > cmdSize
964 || ((effect_param_t *)pCmdData)->psize > cmdSize
965 - sizeof(effect_param_t)
966 || ((effect_param_t *)pCmdData)->vsize > cmdSize
967 - sizeof(effect_param_t)
968 - ((effect_param_t *)pCmdData)->psize
969 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
970 cmdSize
971 - sizeof(effect_param_t)
972 - ((effect_param_t *)pCmdData)->psize
973 - ((effect_param_t *)pCmdData)->vsize)) {
974 android_errorWriteLog(0x534e4554, "30204301");
975 return -EINVAL;
976 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700977 status_t status = mEffectInterface->command(cmdCode,
978 cmdSize,
979 pCmdData,
980 replySize,
981 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800982 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
983 uint32_t size = (replySize == NULL) ? 0 : *replySize;
984 for (size_t i = 1; i < mHandles.size(); i++) {
985 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800986 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800987 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
988 }
989 }
990 }
991 return status;
992}
993
994status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
995{
996 Mutex::Autolock _l(mLock);
997 return setEnabled_l(enabled);
998}
999
1000// must be called with EffectModule::mLock held
1001status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
1002{
1003
1004 ALOGV("setEnabled %p enabled %d", this, enabled);
1005
1006 if (enabled != isEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001007 switch (mState) {
1008 // going from disabled to enabled
1009 case IDLE:
1010 mState = STARTING;
1011 break;
1012 case STOPPED:
1013 mState = RESTART;
1014 break;
1015 case STOPPING:
1016 mState = ACTIVE;
1017 break;
1018
1019 // going from enabled to disabled
1020 case RESTART:
1021 mState = STOPPED;
1022 break;
1023 case STARTING:
1024 mState = IDLE;
1025 break;
1026 case ACTIVE:
1027 mState = STOPPING;
1028 break;
1029 case DESTROYED:
1030 return NO_ERROR; // simply ignore as we are being destroyed
1031 }
1032 for (size_t i = 1; i < mHandles.size(); i++) {
1033 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001034 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001035 h->setEnabled(enabled);
1036 }
1037 }
1038 }
1039 return NO_ERROR;
1040}
1041
1042bool AudioFlinger::EffectModule::isEnabled() const
1043{
1044 switch (mState) {
1045 case RESTART:
1046 case STARTING:
1047 case ACTIVE:
1048 return true;
1049 case IDLE:
1050 case STOPPING:
1051 case STOPPED:
1052 case DESTROYED:
1053 default:
1054 return false;
1055 }
1056}
1057
1058bool AudioFlinger::EffectModule::isProcessEnabled() const
1059{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001060 if (mStatus != NO_ERROR) {
1061 return false;
1062 }
1063
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 switch (mState) {
1065 case RESTART:
1066 case ACTIVE:
1067 case STOPPING:
1068 case STOPPED:
1069 return true;
1070 case IDLE:
1071 case STARTING:
1072 case DESTROYED:
1073 default:
1074 return false;
1075 }
1076}
1077
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001078bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1079{
1080 return (mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT);
1081}
1082
1083bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1084{
1085 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1086}
1087
Mikhail Naganov022b9952017-01-04 16:36:51 -08001088void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001089 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001090
1091 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001092 if (buffer != 0) {
1093 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1094 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1095 } else {
1096 mConfig.inputCfg.buffer.raw = NULL;
1097 }
1098 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001099 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001100
1101#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001102 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001103 // Theoretically insert effects can also do in-place conversions (destroying
1104 // the original buffer) when the output buffer is identical to the input buffer,
1105 // but we don't optimize for it here.
1106 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001107 const uint32_t inChannelCount =
1108 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1109 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1110 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001111 // we need to translate - create hidl shared buffer and intercept
1112 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001113 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1114 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1115 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001116
1117 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1118 __func__, inChannels, inFrameCount, size);
1119
Andy Hungbded9c82017-11-30 18:47:35 -08001120 if (size > 0 && (mInConversionBuffer.get() == nullptr
1121 || size > mInConversionBuffer->getSize())) {
1122 mInConversionBuffer.clear();
1123 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001124 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1125 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1126 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001127 }
Andy Hungbded9c82017-11-30 18:47:35 -08001128 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001129 mInConversionBuffer->setFrameCount(inFrameCount);
1130 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001131 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001132 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001133 }
1134 }
1135#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001136}
1137
1138void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001139 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001140
1141 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001142 if (buffer != 0) {
1143 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1144 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1145 } else {
1146 mConfig.outputCfg.buffer.raw = NULL;
1147 }
1148 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001149 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001150
1151#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001152 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001153 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001154 const uint32_t outChannelCount =
1155 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1156 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1157 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001158 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001159 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1160 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1161 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001162
1163 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1164 __func__, outChannels, outFrameCount, size);
1165
Andy Hungbded9c82017-11-30 18:47:35 -08001166 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1167 || size > mOutConversionBuffer->getSize())) {
1168 mOutConversionBuffer.clear();
1169 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001170 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1171 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1172 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001173 }
Andy Hungbded9c82017-11-30 18:47:35 -08001174 if (mOutConversionBuffer.get() != nullptr) {
1175 mOutConversionBuffer->setFrameCount(outFrameCount);
1176 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001177 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001178 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001179 }
1180 }
1181#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001182}
1183
Eric Laurentca7cc822012-11-19 14:55:58 -08001184status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1185{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001186 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001187 if (mStatus != NO_ERROR) {
1188 return mStatus;
1189 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001190 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001191 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1192 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1193 if (isProcessEnabled() &&
1194 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001195 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1196 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001197 uint32_t volume[2];
1198 uint32_t *pVolume = NULL;
1199 uint32_t size = sizeof(volume);
1200 volume[0] = *left;
1201 volume[1] = *right;
1202 if (controller) {
1203 pVolume = volume;
1204 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001205 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1206 size,
1207 volume,
1208 &size,
1209 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001210 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1211 *left = volume[0];
1212 *right = volume[1];
1213 }
1214 }
1215 return status;
1216}
1217
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001218void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1219{
1220 sp<ThreadBase> thread = mThread.promote();
1221 if (thread != 0 &&
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09001222 (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::DIRECT) &&
1223 !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001224 PlaybackThread *t = (PlaybackThread *)thread.get();
1225 float vol_l = (float)left / (1 << 24);
1226 float vol_r = (float)right / (1 << 24);
1227 t->setVolumeForOutput_l(vol_l, vol_r);
1228 }
1229}
1230
Eric Laurentca7cc822012-11-19 14:55:58 -08001231status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1232{
1233 if (device == AUDIO_DEVICE_NONE) {
1234 return NO_ERROR;
1235 }
1236
1237 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001238 if (mStatus != NO_ERROR) {
1239 return mStatus;
1240 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001241 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001242 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001243 status_t cmdStatus;
1244 uint32_t size = sizeof(status_t);
1245 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1246 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001247 status = mEffectInterface->command(cmd,
1248 sizeof(uint32_t),
1249 &device,
1250 &size,
1251 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001252 }
1253 return status;
1254}
1255
1256status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1257{
1258 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001259 if (mStatus != NO_ERROR) {
1260 return mStatus;
1261 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001262 status_t status = NO_ERROR;
1263 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1264 status_t cmdStatus;
1265 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001266 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1267 sizeof(audio_mode_t),
1268 &mode,
1269 &size,
1270 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001271 if (status == NO_ERROR) {
1272 status = cmdStatus;
1273 }
1274 }
1275 return status;
1276}
1277
1278status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1279{
1280 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001281 if (mStatus != NO_ERROR) {
1282 return mStatus;
1283 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001284 status_t status = NO_ERROR;
1285 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1286 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001287 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1288 sizeof(audio_source_t),
1289 &source,
1290 &size,
1291 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001292 }
1293 return status;
1294}
1295
1296void AudioFlinger::EffectModule::setSuspended(bool suspended)
1297{
1298 Mutex::Autolock _l(mLock);
1299 mSuspended = suspended;
1300}
1301
1302bool AudioFlinger::EffectModule::suspended() const
1303{
1304 Mutex::Autolock _l(mLock);
1305 return mSuspended;
1306}
1307
1308bool AudioFlinger::EffectModule::purgeHandles()
1309{
1310 bool enabled = false;
1311 Mutex::Autolock _l(mLock);
Eric Laurent6c796322019-04-09 14:13:17 -07001312 EffectHandle *handle = controlHandle_l();
1313 if (handle != NULL) {
1314 enabled = handle->enabled();
Eric Laurentca7cc822012-11-19 14:55:58 -08001315 }
Eric Laurent6c796322019-04-09 14:13:17 -07001316 mHandles.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001317 return enabled;
1318}
1319
Eric Laurent5baf2af2013-09-12 17:37:00 -07001320status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1321{
1322 Mutex::Autolock _l(mLock);
1323 if (mStatus != NO_ERROR) {
1324 return mStatus;
1325 }
1326 status_t status = NO_ERROR;
1327 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1328 status_t cmdStatus;
1329 uint32_t size = sizeof(status_t);
1330 effect_offload_param_t cmd;
1331
1332 cmd.isOffload = offloaded;
1333 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001334 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1335 sizeof(effect_offload_param_t),
1336 &cmd,
1337 &size,
1338 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001339 if (status == NO_ERROR) {
1340 status = cmdStatus;
1341 }
1342 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1343 } else {
1344 if (offloaded) {
1345 status = INVALID_OPERATION;
1346 }
1347 mOffloaded = false;
1348 }
1349 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1350 return status;
1351}
1352
1353bool AudioFlinger::EffectModule::isOffloaded() const
1354{
1355 Mutex::Autolock _l(mLock);
1356 return mOffloaded;
1357}
1358
Marco Nelissenb2208842014-02-07 14:00:50 -08001359String8 effectFlagsToString(uint32_t flags) {
1360 String8 s;
1361
1362 s.append("conn. mode: ");
1363 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1364 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1365 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1366 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1367 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1368 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1369 default: s.append("unknown/reserved"); break;
1370 }
1371 s.append(", ");
1372
1373 s.append("insert pref: ");
1374 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1375 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1376 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1377 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1378 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1379 default: s.append("unknown/reserved"); break;
1380 }
1381 s.append(", ");
1382
1383 s.append("volume mgmt: ");
1384 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1385 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1386 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1387 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001388 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
Marco Nelissenb2208842014-02-07 14:00:50 -08001389 default: s.append("unknown/reserved"); break;
1390 }
1391 s.append(", ");
1392
1393 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1394 if (devind) {
1395 s.append("device indication: ");
1396 switch (devind) {
1397 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1398 default: s.append("unknown/reserved"); break;
1399 }
1400 s.append(", ");
1401 }
1402
1403 s.append("input mode: ");
1404 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1405 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1406 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1407 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1408 default: s.append("not set"); break;
1409 }
1410 s.append(", ");
1411
1412 s.append("output mode: ");
1413 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1414 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1415 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1416 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1417 default: s.append("not set"); break;
1418 }
1419 s.append(", ");
1420
1421 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1422 if (accel) {
1423 s.append("hardware acceleration: ");
1424 switch (accel) {
1425 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1426 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1427 default: s.append("unknown/reserved"); break;
1428 }
1429 s.append(", ");
1430 }
1431
1432 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1433 if (modeind) {
1434 s.append("mode indication: ");
1435 switch (modeind) {
1436 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1437 default: s.append("unknown/reserved"); break;
1438 }
1439 s.append(", ");
1440 }
1441
1442 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1443 if (srcind) {
1444 s.append("source indication: ");
1445 switch (srcind) {
1446 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1447 default: s.append("unknown/reserved"); break;
1448 }
1449 s.append(", ");
1450 }
1451
1452 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1453 s.append("offloadable, ");
1454 }
1455
1456 int len = s.length();
1457 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001458 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001459 s.unlockBuffer(len - 2);
1460 }
1461 return s;
1462}
1463
Andy Hungbded9c82017-11-30 18:47:35 -08001464static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1465 std::stringstream ss;
1466
1467 if (buffer.get() == nullptr) {
1468 return "nullptr"; // make different than below
1469 } else if (buffer->externalData() != nullptr) {
1470 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1471 << " -> "
1472 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1473 } else {
1474 ss << buffer->audioBuffer()->raw;
1475 }
1476 return ss.str();
1477}
Marco Nelissenb2208842014-02-07 14:00:50 -08001478
Glenn Kasten0f11b512014-01-31 16:18:54 -08001479void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001480{
Eric Laurentca7cc822012-11-19 14:55:58 -08001481 String8 result;
1482
Andy Hung9718d662017-12-22 17:57:39 -08001483 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001484
1485 bool locked = AudioFlinger::dumpTryLock(mLock);
1486 // failed to lock - AudioFlinger is probably deadlocked
1487 if (!locked) {
1488 result.append("\t\tCould not lock Fx mutex:\n");
1489 }
1490
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001491 result.append("\t\tSession Status State Registered Enabled Suspended Engine:\n");
1492 result.appendFormat("\t\t%05d %03d %03d %s %s %s %p\n",
1493 mSessionId, mStatus, mState, mPolicyRegistered ? "y" : "n", mPolicyEnabled ? "y" : "n",
1494 mSuspended ? "y" : "n", mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001495
1496 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001497 char uuidStr[64];
1498 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001499 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001500 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001501 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1502 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001503 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001504 mDescriptor.flags,
1505 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001506 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001507 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001508
1509 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001510 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001511
1512 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001513
1514 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001515 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1516 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1517 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001518 mConfig.inputCfg.buffer.frameCount,
1519 mConfig.inputCfg.samplingRate,
1520 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001521 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001522 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001523
1524 result.append("\t\t- Output configuration:\n");
1525 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001526 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001527 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001528 mConfig.outputCfg.buffer.frameCount,
1529 mConfig.outputCfg.samplingRate,
1530 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001531 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001532 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001533
rago94a1ee82017-07-21 15:11:02 -07001534#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001535
Andy Hungbded9c82017-11-30 18:47:35 -08001536 result.appendFormat("\t\t- HAL buffers:\n"
1537 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1538 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1539 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1540 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1541 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001542#endif
1543
Andy Hung9718d662017-12-22 17:57:39 -08001544 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001545 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001546 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001547 for (size_t i = 0; i < mHandles.size(); ++i) {
1548 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001549 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001550 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001551 result.append(buffer);
1552 }
1553 }
1554
Eric Laurentca7cc822012-11-19 14:55:58 -08001555 write(fd, result.string(), result.length());
1556
Mikhail Naganov4d547672019-02-22 14:19:19 -08001557 if (mEffectInterface != 0) {
1558 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1559 (void)mEffectInterface->dump(fd);
1560 }
1561
Eric Laurentca7cc822012-11-19 14:55:58 -08001562 if (locked) {
1563 mLock.unlock();
1564 }
1565}
1566
1567// ----------------------------------------------------------------------------
1568// EffectHandle implementation
1569// ----------------------------------------------------------------------------
1570
1571#undef LOG_TAG
1572#define LOG_TAG "AudioFlinger::EffectHandle"
1573
1574AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1575 const sp<AudioFlinger::Client>& client,
1576 const sp<IEffectClient>& effectClient,
1577 int32_t priority)
1578 : BnEffect(),
1579 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001580 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001581{
1582 ALOGV("constructor %p", this);
1583
1584 if (client == 0) {
1585 return;
1586 }
1587 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1588 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001589 if (mCblkMemory == 0 ||
1590 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001591 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001592 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001593 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001594 return;
1595 }
Glenn Kastene75da402013-11-20 13:54:52 -08001596 new(mCblk) effect_param_cblk_t();
1597 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001598}
1599
1600AudioFlinger::EffectHandle::~EffectHandle()
1601{
1602 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001603 disconnect(false);
1604}
1605
Glenn Kastene75da402013-11-20 13:54:52 -08001606status_t AudioFlinger::EffectHandle::initCheck()
1607{
1608 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1609}
1610
Eric Laurentca7cc822012-11-19 14:55:58 -08001611status_t AudioFlinger::EffectHandle::enable()
1612{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001613 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001614 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001615 sp<EffectModule> effect = mEffect.promote();
1616 if (effect == 0 || mDisconnected) {
1617 return DEAD_OBJECT;
1618 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001619 if (!mHasControl) {
1620 return INVALID_OPERATION;
1621 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001622
1623 if (mEnabled) {
1624 return NO_ERROR;
1625 }
1626
1627 mEnabled = true;
1628
Eric Laurent6c796322019-04-09 14:13:17 -07001629 status_t status = effect->updatePolicyState();
1630 if (status != NO_ERROR) {
1631 mEnabled = false;
1632 return status;
1633 }
1634
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001635 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001636 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001637 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001638 }
1639
1640 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001641 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001642 return NO_ERROR;
1643 }
1644
Eric Laurent6c796322019-04-09 14:13:17 -07001645 status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001646 if (status != NO_ERROR) {
1647 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001648 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001649 }
1650 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001651 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001652 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001653 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1654 Mutex::Autolock _l(thread->mLock);
1655 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001656 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001657 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001658 if (thread->type() == ThreadBase::OFFLOAD) {
1659 PlaybackThread *t = (PlaybackThread *)thread.get();
1660 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1661 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001662 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001663 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1664 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001665 }
1666 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 }
1668 return status;
1669}
1670
1671status_t AudioFlinger::EffectHandle::disable()
1672{
1673 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001674 AutoMutex _l(mLock);
1675 sp<EffectModule> effect = mEffect.promote();
1676 if (effect == 0 || mDisconnected) {
1677 return DEAD_OBJECT;
1678 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001679 if (!mHasControl) {
1680 return INVALID_OPERATION;
1681 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001682
1683 if (!mEnabled) {
1684 return NO_ERROR;
1685 }
1686 mEnabled = false;
1687
Eric Laurent6c796322019-04-09 14:13:17 -07001688 effect->updatePolicyState();
1689
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001690 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001691 return NO_ERROR;
1692 }
1693
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001694 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001695
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001696 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001697 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001698 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001699 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1700 Mutex::Autolock _l(thread->mLock);
1701 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001702 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001703 }
1704
1705 return status;
1706}
1707
1708void AudioFlinger::EffectHandle::disconnect()
1709{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001710 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 disconnect(true);
1712}
1713
1714void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1715{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001716 AutoMutex _l(mLock);
1717 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1718 if (mDisconnected) {
1719 if (unpinIfLast) {
1720 android_errorWriteLog(0x534e4554, "32707507");
1721 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001722 return;
1723 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001724 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001725 {
1726 sp<EffectModule> effect = mEffect.promote();
1727 if (effect != 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001728 sp<ThreadBase> thread = effect->thread().promote();
1729 if (thread != 0) {
1730 thread->disconnectEffectHandle(this, unpinIfLast);
1731 } else if (effect->disconnectHandle(this, unpinIfLast) > 0) {
1732 ALOGW("%s Effect handle %p disconnected after thread destruction",
1733 __func__, this);
1734 }
1735 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001736 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001737 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001738
Eric Laurentca7cc822012-11-19 14:55:58 -08001739 if (mClient != 0) {
1740 if (mCblk != NULL) {
1741 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1742 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1743 }
1744 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001745 // Client destructor must run with AudioFlinger client mutex locked
1746 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001747 mClient.clear();
1748 }
1749}
1750
1751status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1752 uint32_t cmdSize,
1753 void *pCmdData,
1754 uint32_t *replySize,
1755 void *pReplyData)
1756{
1757 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001758 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001759
Eric Laurentc7ab3092017-06-15 18:43:46 -07001760 // reject commands reserved for internal use by audio framework if coming from outside
1761 // of audioserver
1762 switch(cmdCode) {
1763 case EFFECT_CMD_ENABLE:
1764 case EFFECT_CMD_DISABLE:
1765 case EFFECT_CMD_SET_PARAM:
1766 case EFFECT_CMD_SET_PARAM_DEFERRED:
1767 case EFFECT_CMD_SET_PARAM_COMMIT:
1768 case EFFECT_CMD_GET_PARAM:
1769 break;
1770 default:
1771 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1772 break;
1773 }
1774 android_errorWriteLog(0x534e4554, "62019992");
1775 return BAD_VALUE;
1776 }
1777
Eric Laurent1ffc5852016-12-15 14:46:09 -08001778 if (cmdCode == EFFECT_CMD_ENABLE) {
1779 if (*replySize < sizeof(int)) {
1780 android_errorWriteLog(0x534e4554, "32095713");
1781 return BAD_VALUE;
1782 }
1783 *(int *)pReplyData = NO_ERROR;
1784 *replySize = sizeof(int);
1785 return enable();
1786 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1787 if (*replySize < sizeof(int)) {
1788 android_errorWriteLog(0x534e4554, "32095713");
1789 return BAD_VALUE;
1790 }
1791 *(int *)pReplyData = NO_ERROR;
1792 *replySize = sizeof(int);
1793 return disable();
1794 }
1795
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001796 AutoMutex _l(mLock);
1797 sp<EffectModule> effect = mEffect.promote();
1798 if (effect == 0 || mDisconnected) {
1799 return DEAD_OBJECT;
1800 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001801 // only get parameter command is permitted for applications not controlling the effect
1802 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1803 return INVALID_OPERATION;
1804 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001805 if (mClient == 0) {
1806 return INVALID_OPERATION;
1807 }
1808
1809 // handle commands that are not forwarded transparently to effect engine
1810 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001811 if (*replySize < sizeof(int)) {
1812 android_errorWriteLog(0x534e4554, "32095713");
1813 return BAD_VALUE;
1814 }
1815 *(int *)pReplyData = NO_ERROR;
1816 *replySize = sizeof(int);
1817
Eric Laurentca7cc822012-11-19 14:55:58 -08001818 // No need to trylock() here as this function is executed in the binder thread serving a
1819 // particular client process: no risk to block the whole media server process or mixer
1820 // threads if we are stuck here
1821 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001822 // keep local copy of index in case of client corruption b/32220769
1823 const uint32_t clientIndex = mCblk->clientIndex;
1824 const uint32_t serverIndex = mCblk->serverIndex;
1825 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1826 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001827 mCblk->serverIndex = 0;
1828 mCblk->clientIndex = 0;
1829 return BAD_VALUE;
1830 }
1831 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001832 effect_param_t *param = NULL;
1833 for (uint32_t index = serverIndex; index < clientIndex;) {
1834 int *p = (int *)(mBuffer + index);
1835 const int size = *p++;
1836 if (size < 0
1837 || size > EFFECT_PARAM_BUFFER_SIZE
1838 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001839 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001840 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001841 break;
1842 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001843
1844 // copy to local memory in case of client corruption b/32220769
1845 param = (effect_param_t *)realloc(param, size);
1846 if (param == NULL) {
1847 ALOGW("command(): out of memory");
1848 status = NO_MEMORY;
1849 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001850 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001851 memcpy(param, p, size);
1852
1853 int reply = 0;
1854 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001855 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001856 size,
1857 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001858 &rsize,
1859 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001860
1861 // verify shared memory: server index shouldn't change; client index can't go back.
1862 if (serverIndex != mCblk->serverIndex
1863 || clientIndex > mCblk->clientIndex) {
1864 android_errorWriteLog(0x534e4554, "32220769");
1865 status = BAD_VALUE;
1866 break;
1867 }
1868
Eric Laurentca7cc822012-11-19 14:55:58 -08001869 // stop at first error encountered
1870 if (ret != NO_ERROR) {
1871 status = ret;
1872 *(int *)pReplyData = reply;
1873 break;
1874 } else if (reply != NO_ERROR) {
1875 *(int *)pReplyData = reply;
1876 break;
1877 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001878 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001879 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001880 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001881 mCblk->serverIndex = 0;
1882 mCblk->clientIndex = 0;
1883 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001884 }
1885
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001886 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001887}
1888
1889void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1890{
1891 ALOGV("setControl %p control %d", this, hasControl);
1892
1893 mHasControl = hasControl;
1894 mEnabled = enabled;
1895
1896 if (signal && mEffectClient != 0) {
1897 mEffectClient->controlStatusChanged(hasControl);
1898 }
1899}
1900
1901void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1902 uint32_t cmdSize,
1903 void *pCmdData,
1904 uint32_t replySize,
1905 void *pReplyData)
1906{
1907 if (mEffectClient != 0) {
1908 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1909 }
1910}
1911
1912
1913
1914void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1915{
1916 if (mEffectClient != 0) {
1917 mEffectClient->enableStatusChanged(enabled);
1918 }
1919}
1920
1921status_t AudioFlinger::EffectHandle::onTransact(
1922 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1923{
1924 return BnEffect::onTransact(code, data, reply, flags);
1925}
1926
1927
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001928void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001929{
1930 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1931
Marco Nelissenb2208842014-02-07 14:00:50 -08001932 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001933 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001934 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001935 mHasControl ? "yes" : "no",
1936 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001937 mCblk ? mCblk->clientIndex : 0,
1938 mCblk ? mCblk->serverIndex : 0
1939 );
1940
1941 if (locked) {
1942 mCblk->lock.unlock();
1943 }
1944}
1945
1946#undef LOG_TAG
1947#define LOG_TAG "AudioFlinger::EffectChain"
1948
1949AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001950 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001951 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001952 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001953 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001954{
1955 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1956 if (thread == NULL) {
1957 return;
1958 }
1959 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1960 thread->frameCount();
1961}
1962
1963AudioFlinger::EffectChain::~EffectChain()
1964{
Eric Laurentca7cc822012-11-19 14:55:58 -08001965}
1966
1967// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1968sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1969 effect_descriptor_t *descriptor)
1970{
1971 size_t size = mEffects.size();
1972
1973 for (size_t i = 0; i < size; i++) {
1974 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1975 return mEffects[i];
1976 }
1977 }
1978 return 0;
1979}
1980
1981// getEffectFromId_l() must be called with ThreadBase::mLock held
1982sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1983{
1984 size_t size = mEffects.size();
1985
1986 for (size_t i = 0; i < size; i++) {
1987 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1988 if (id == 0 || mEffects[i]->id() == id) {
1989 return mEffects[i];
1990 }
1991 }
1992 return 0;
1993}
1994
1995// getEffectFromType_l() must be called with ThreadBase::mLock held
1996sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1997 const effect_uuid_t *type)
1998{
1999 size_t size = mEffects.size();
2000
2001 for (size_t i = 0; i < size; i++) {
2002 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2003 return mEffects[i];
2004 }
2005 }
2006 return 0;
2007}
2008
Eric Laurent6c796322019-04-09 14:13:17 -07002009std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2010{
2011 std::vector<int> ids;
2012 Mutex::Autolock _l(mLock);
2013 for (size_t i = 0; i < mEffects.size(); i++) {
2014 ids.push_back(mEffects[i]->id());
2015 }
2016 return ids;
2017}
2018
Eric Laurentca7cc822012-11-19 14:55:58 -08002019void AudioFlinger::EffectChain::clearInputBuffer()
2020{
2021 Mutex::Autolock _l(mLock);
2022 sp<ThreadBase> thread = mThread.promote();
2023 if (thread == 0) {
2024 ALOGW("clearInputBuffer(): cannot promote mixer thread");
2025 return;
2026 }
2027 clearInputBuffer_l(thread);
2028}
2029
2030// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002031void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08002032{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002033 if (mInBuffer == NULL) {
2034 return;
2035 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002036 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08002037 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002038
Mikhail Naganov022b9952017-01-04 16:36:51 -08002039 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
2040 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002041}
2042
2043// Must be called with EffectChain::mLock locked
2044void AudioFlinger::EffectChain::process_l()
2045{
2046 sp<ThreadBase> thread = mThread.promote();
2047 if (thread == 0) {
2048 ALOGW("process_l(): cannot promote mixer thread");
2049 return;
2050 }
2051 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
2052 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002053 // never process effects when:
2054 // - on an OFFLOAD thread
2055 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08002056 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
2057 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08002058 if (!isGlobalSession) {
2059 bool tracksOnSession = (trackCnt() != 0);
2060
2061 if (!tracksOnSession && mTailBufferCount == 0) {
2062 doProcess = false;
2063 }
2064
2065 if (activeTrackCnt() == 0) {
2066 // if no track is active and the effect tail has not been rendered,
2067 // the input buffer must be cleared here as the mixer process will not do it
2068 if (tracksOnSession || mTailBufferCount > 0) {
2069 clearInputBuffer_l(thread);
2070 if (mTailBufferCount > 0) {
2071 mTailBufferCount--;
2072 }
2073 }
2074 }
2075 }
2076
2077 size_t size = mEffects.size();
2078 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002079 // Only the input and output buffers of the chain can be external,
2080 // and 'update' / 'commit' do nothing for allocated buffers, thus
2081 // it's not needed to consider any other buffers here.
2082 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002083 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2084 mOutBuffer->update();
2085 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002086 for (size_t i = 0; i < size; i++) {
2087 mEffects[i]->process();
2088 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002089 mInBuffer->commit();
2090 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2091 mOutBuffer->commit();
2092 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002093 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002094 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002095 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002096 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2097 }
2098 if (doResetVolume) {
2099 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002100 }
2101}
2102
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002103// createEffect_l() must be called with ThreadBase::mLock held
2104status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
2105 ThreadBase *thread,
2106 effect_descriptor_t *desc,
2107 int id,
2108 audio_session_t sessionId,
2109 bool pinned)
2110{
2111 Mutex::Autolock _l(mLock);
2112 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
2113 status_t lStatus = effect->status();
2114 if (lStatus == NO_ERROR) {
2115 lStatus = addEffect_ll(effect);
2116 }
2117 if (lStatus != NO_ERROR) {
2118 effect.clear();
2119 }
2120 return lStatus;
2121}
2122
2123// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002124status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2125{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002126 Mutex::Autolock _l(mLock);
2127 return addEffect_ll(effect);
2128}
2129// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2130status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2131{
Eric Laurentca7cc822012-11-19 14:55:58 -08002132 effect_descriptor_t desc = effect->desc();
2133 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2134
Eric Laurentca7cc822012-11-19 14:55:58 -08002135 effect->setChain(this);
2136 sp<ThreadBase> thread = mThread.promote();
2137 if (thread == 0) {
2138 return NO_INIT;
2139 }
2140 effect->setThread(thread);
2141
2142 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2143 // Auxiliary effects are inserted at the beginning of mEffects vector as
2144 // they are processed first and accumulated in chain input buffer
2145 mEffects.insertAt(effect, 0);
2146
2147 // the input buffer for auxiliary effect contains mono samples in
2148 // 32 bit format. This is to avoid saturation in AudoMixer
2149 // accumulation stage. Saturation is done in EffectModule::process() before
2150 // calling the process in effect engine
2151 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002152 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002153#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002154 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002155 numSamples * sizeof(float), &halBuffer);
2156#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002157 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002158 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002159#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002160 if (result != OK) return result;
2161 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002162 // auxiliary effects output samples to chain input buffer for further processing
2163 // by insert effects
2164 effect->setOutBuffer(mInBuffer);
2165 } else {
2166 // Insert effects are inserted at the end of mEffects vector as they are processed
2167 // after track and auxiliary effects.
2168 // Insert effect order as a function of indicated preference:
2169 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2170 // another effect is present
2171 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2172 // last effect claiming first position
2173 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2174 // first effect claiming last position
2175 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2176 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2177 // already present
2178
2179 size_t size = mEffects.size();
2180 size_t idx_insert = size;
2181 ssize_t idx_insert_first = -1;
2182 ssize_t idx_insert_last = -1;
2183
2184 for (size_t i = 0; i < size; i++) {
2185 effect_descriptor_t d = mEffects[i]->desc();
2186 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2187 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2188 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2189 // check invalid effect chaining combinations
2190 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2191 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2192 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2193 desc.name, d.name);
2194 return INVALID_OPERATION;
2195 }
2196 // remember position of first insert effect and by default
2197 // select this as insert position for new effect
2198 if (idx_insert == size) {
2199 idx_insert = i;
2200 }
2201 // remember position of last insert effect claiming
2202 // first position
2203 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2204 idx_insert_first = i;
2205 }
2206 // remember position of first insert effect claiming
2207 // last position
2208 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2209 idx_insert_last == -1) {
2210 idx_insert_last = i;
2211 }
2212 }
2213 }
2214
2215 // modify idx_insert from first position if needed
2216 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2217 if (idx_insert_last != -1) {
2218 idx_insert = idx_insert_last;
2219 } else {
2220 idx_insert = size;
2221 }
2222 } else {
2223 if (idx_insert_first != -1) {
2224 idx_insert = idx_insert_first + 1;
2225 }
2226 }
2227
2228 // always read samples from chain input buffer
2229 effect->setInBuffer(mInBuffer);
2230
2231 // if last effect in the chain, output samples to chain
2232 // output buffer, otherwise to chain input buffer
2233 if (idx_insert == size) {
2234 if (idx_insert != 0) {
2235 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2236 mEffects[idx_insert-1]->configure();
2237 }
2238 effect->setOutBuffer(mOutBuffer);
2239 } else {
2240 effect->setOutBuffer(mInBuffer);
2241 }
2242 mEffects.insertAt(effect, idx_insert);
2243
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002244 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002245 idx_insert);
2246 }
2247 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002248
Eric Laurentca7cc822012-11-19 14:55:58 -08002249 return NO_ERROR;
2250}
2251
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002252// removeEffect_l() must be called with ThreadBase::mLock held
2253size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2254 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002255{
2256 Mutex::Autolock _l(mLock);
2257 size_t size = mEffects.size();
2258 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2259
2260 for (size_t i = 0; i < size; i++) {
2261 if (effect == mEffects[i]) {
2262 // calling stop here will remove pre-processing effect from the audio HAL.
2263 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2264 // the middle of a read from audio HAL
2265 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2266 mEffects[i]->state() == EffectModule::STOPPING) {
2267 mEffects[i]->stop();
2268 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002269 if (release) {
2270 mEffects[i]->release_l();
2271 }
2272
Mikhail Naganov022b9952017-01-04 16:36:51 -08002273 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002274 if (i == size - 1 && i != 0) {
2275 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2276 mEffects[i - 1]->configure();
2277 }
2278 }
2279 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002280 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002281 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002282
Eric Laurentca7cc822012-11-19 14:55:58 -08002283 break;
2284 }
2285 }
2286
2287 return mEffects.size();
2288}
2289
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002290// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002291void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2292{
2293 size_t size = mEffects.size();
2294 for (size_t i = 0; i < size; i++) {
2295 mEffects[i]->setDevice(device);
2296 }
2297}
2298
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002299// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002300void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2301{
2302 size_t size = mEffects.size();
2303 for (size_t i = 0; i < size; i++) {
2304 mEffects[i]->setMode(mode);
2305 }
2306}
2307
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002308// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002309void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2310{
2311 size_t size = mEffects.size();
2312 for (size_t i = 0; i < size; i++) {
2313 mEffects[i]->setAudioSource(source);
2314 }
2315}
2316
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002317// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002318bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002319{
2320 uint32_t newLeft = *left;
2321 uint32_t newRight = *right;
2322 bool hasControl = false;
2323 int ctrlIdx = -1;
2324 size_t size = mEffects.size();
2325
2326 // first update volume controller
2327 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002328 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002329 ctrlIdx = i - 1;
2330 hasControl = true;
2331 break;
2332 }
2333 }
2334
Eric Laurentfa1e1232016-08-02 19:01:49 -07002335 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002336 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002337 if (hasControl) {
2338 *left = mNewLeftVolume;
2339 *right = mNewRightVolume;
2340 }
2341 return hasControl;
2342 }
2343
2344 mVolumeCtrlIdx = ctrlIdx;
2345 mLeftVolume = newLeft;
2346 mRightVolume = newRight;
2347
2348 // second get volume update from volume controller
2349 if (ctrlIdx >= 0) {
2350 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2351 mNewLeftVolume = newLeft;
2352 mNewRightVolume = newRight;
2353 }
2354 // then indicate volume to all other effects in chain.
2355 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002356 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002357 uint32_t lVol = newLeft;
2358 uint32_t rVol = newRight;
2359
2360 for (size_t i = 0; i < size; i++) {
2361 if ((int)i == ctrlIdx) {
2362 continue;
2363 }
2364 // this also works for ctrlIdx == -1 when there is no volume controller
2365 if ((int)i > ctrlIdx) {
2366 lVol = *left;
2367 rVol = *right;
2368 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002369 // Pass requested volume directly if this is volume monitor module
2370 if (mEffects[i]->isVolumeMonitor()) {
2371 mEffects[i]->setVolume(left, right, false);
2372 } else {
2373 mEffects[i]->setVolume(&lVol, &rVol, false);
2374 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002375 }
2376 *left = newLeft;
2377 *right = newRight;
2378
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002379 setVolumeForOutput_l(*left, *right);
2380
Eric Laurentca7cc822012-11-19 14:55:58 -08002381 return hasControl;
2382}
2383
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002384// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002385void AudioFlinger::EffectChain::resetVolume_l()
2386{
Eric Laurente7449bf2016-08-03 18:44:07 -07002387 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2388 uint32_t left = mLeftVolume;
2389 uint32_t right = mRightVolume;
2390 (void)setVolume_l(&left, &right, true);
2391 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002392}
2393
Eric Laurent1b928682014-10-02 19:41:47 -07002394void AudioFlinger::EffectChain::syncHalEffectsState()
2395{
2396 Mutex::Autolock _l(mLock);
2397 for (size_t i = 0; i < mEffects.size(); i++) {
2398 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2399 mEffects[i]->state() == EffectModule::STOPPING) {
2400 mEffects[i]->addEffectToHal_l();
2401 }
2402 }
2403}
2404
Eric Laurentca7cc822012-11-19 14:55:58 -08002405void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2406{
Eric Laurentca7cc822012-11-19 14:55:58 -08002407 String8 result;
2408
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002409 const size_t numEffects = mEffects.size();
2410 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002411
Marco Nelissenb2208842014-02-07 14:00:50 -08002412 if (numEffects) {
2413 bool locked = AudioFlinger::dumpTryLock(mLock);
2414 // failed to lock - AudioFlinger is probably deadlocked
2415 if (!locked) {
2416 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002417 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002418
Andy Hungbded9c82017-11-30 18:47:35 -08002419 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2420 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2421 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2422 (int)inBufferStr.size(), "In buffer ",
2423 (int)outBufferStr.size(), "Out buffer ");
2424 result.appendFormat("\t%s %s %d\n",
2425 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002426 write(fd, result.string(), result.size());
2427
2428 for (size_t i = 0; i < numEffects; ++i) {
2429 sp<EffectModule> effect = mEffects[i];
2430 if (effect != 0) {
2431 effect->dump(fd, args);
2432 }
2433 }
2434
2435 if (locked) {
2436 mLock.unlock();
2437 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002438 } else {
2439 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002440 }
2441}
2442
2443// must be called with ThreadBase::mLock held
2444void AudioFlinger::EffectChain::setEffectSuspended_l(
2445 const effect_uuid_t *type, bool suspend)
2446{
2447 sp<SuspendedEffectDesc> desc;
2448 // use effect type UUID timelow as key as there is no real risk of identical
2449 // timeLow fields among effect type UUIDs.
2450 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2451 if (suspend) {
2452 if (index >= 0) {
2453 desc = mSuspendedEffects.valueAt(index);
2454 } else {
2455 desc = new SuspendedEffectDesc();
2456 desc->mType = *type;
2457 mSuspendedEffects.add(type->timeLow, desc);
2458 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2459 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002460
Eric Laurentca7cc822012-11-19 14:55:58 -08002461 if (desc->mRefCount++ == 0) {
2462 sp<EffectModule> effect = getEffectIfEnabled(type);
2463 if (effect != 0) {
2464 desc->mEffect = effect;
2465 effect->setSuspended(true);
2466 effect->setEnabled(false);
2467 }
2468 }
2469 } else {
2470 if (index < 0) {
2471 return;
2472 }
2473 desc = mSuspendedEffects.valueAt(index);
2474 if (desc->mRefCount <= 0) {
2475 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002476 desc->mRefCount = 0;
2477 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002478 }
2479 if (--desc->mRefCount == 0) {
2480 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2481 if (desc->mEffect != 0) {
2482 sp<EffectModule> effect = desc->mEffect.promote();
2483 if (effect != 0) {
2484 effect->setSuspended(false);
2485 effect->lock();
2486 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002487 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002488 effect->setEnabled_l(handle->enabled());
2489 }
2490 effect->unlock();
2491 }
2492 desc->mEffect.clear();
2493 }
2494 mSuspendedEffects.removeItemsAt(index);
2495 }
2496 }
2497}
2498
2499// must be called with ThreadBase::mLock held
2500void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2501{
2502 sp<SuspendedEffectDesc> desc;
2503
2504 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2505 if (suspend) {
2506 if (index >= 0) {
2507 desc = mSuspendedEffects.valueAt(index);
2508 } else {
2509 desc = new SuspendedEffectDesc();
2510 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2511 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2512 }
2513 if (desc->mRefCount++ == 0) {
2514 Vector< sp<EffectModule> > effects;
2515 getSuspendEligibleEffects(effects);
2516 for (size_t i = 0; i < effects.size(); i++) {
2517 setEffectSuspended_l(&effects[i]->desc().type, true);
2518 }
2519 }
2520 } else {
2521 if (index < 0) {
2522 return;
2523 }
2524 desc = mSuspendedEffects.valueAt(index);
2525 if (desc->mRefCount <= 0) {
2526 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2527 desc->mRefCount = 1;
2528 }
2529 if (--desc->mRefCount == 0) {
2530 Vector<const effect_uuid_t *> types;
2531 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2532 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2533 continue;
2534 }
2535 types.add(&mSuspendedEffects.valueAt(i)->mType);
2536 }
2537 for (size_t i = 0; i < types.size(); i++) {
2538 setEffectSuspended_l(types[i], false);
2539 }
2540 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2541 mSuspendedEffects.keyAt(index));
2542 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2543 }
2544 }
2545}
2546
2547
2548// The volume effect is used for automated tests only
2549#ifndef OPENSL_ES_H_
2550static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2551 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2552const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2553#endif //OPENSL_ES_H_
2554
Eric Laurentd8365c52017-07-16 15:27:05 -07002555/* static */
2556bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2557{
2558 // Only NS and AEC are suspended when BtNRec is off
2559 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2560 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2561 return true;
2562 }
2563 return false;
2564}
2565
Eric Laurentca7cc822012-11-19 14:55:58 -08002566bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2567{
2568 // auxiliary effects and visualizer are never suspended on output mix
2569 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2570 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2571 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2572 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2573 return false;
2574 }
2575 return true;
2576}
2577
2578void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2579 Vector< sp<AudioFlinger::EffectModule> > &effects)
2580{
2581 effects.clear();
2582 for (size_t i = 0; i < mEffects.size(); i++) {
2583 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2584 effects.add(mEffects[i]);
2585 }
2586 }
2587}
2588
2589sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2590 const effect_uuid_t *type)
2591{
2592 sp<EffectModule> effect = getEffectFromType_l(type);
2593 return effect != 0 && effect->isEnabled() ? effect : 0;
2594}
2595
2596void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2597 bool enabled)
2598{
2599 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2600 if (enabled) {
2601 if (index < 0) {
2602 // if the effect is not suspend check if all effects are suspended
2603 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2604 if (index < 0) {
2605 return;
2606 }
2607 if (!isEffectEligibleForSuspend(effect->desc())) {
2608 return;
2609 }
2610 setEffectSuspended_l(&effect->desc().type, enabled);
2611 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2612 if (index < 0) {
2613 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2614 return;
2615 }
2616 }
2617 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2618 effect->desc().type.timeLow);
2619 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002620 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002621 if (desc->mEffect == 0) {
2622 desc->mEffect = effect;
2623 effect->setEnabled(false);
2624 effect->setSuspended(true);
2625 }
2626 } else {
2627 if (index < 0) {
2628 return;
2629 }
2630 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2631 effect->desc().type.timeLow);
2632 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2633 desc->mEffect.clear();
2634 effect->setSuspended(false);
2635 }
2636}
2637
Eric Laurent5baf2af2013-09-12 17:37:00 -07002638bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002639{
2640 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002641 return isNonOffloadableEnabled_l();
2642}
2643
2644bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2645{
Eric Laurent813e2a72013-08-31 12:59:48 -07002646 size_t size = mEffects.size();
2647 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002648 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002649 return true;
2650 }
2651 }
2652 return false;
2653}
2654
Eric Laurentaaa44472014-09-12 17:41:50 -07002655void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2656{
2657 Mutex::Autolock _l(mLock);
2658 mThread = thread;
2659 for (size_t i = 0; i < mEffects.size(); i++) {
2660 mEffects[i]->setThread(thread);
2661 }
2662}
2663
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002664void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2665{
2666 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2667 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2668 }
2669 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2670 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2671 }
2672}
2673
2674void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2675{
2676 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2677 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2678 }
2679 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2680 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2681 }
2682}
2683
2684bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002685{
2686 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002687 for (const auto &effect : mEffects) {
2688 if (effect->isProcessImplemented()) {
2689 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002690 }
2691 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002692 // Allow effects without processing.
2693 return true;
2694}
2695
2696bool AudioFlinger::EffectChain::isFastCompatible() const
2697{
2698 Mutex::Autolock _l(mLock);
2699 for (const auto &effect : mEffects) {
2700 if (effect->isProcessImplemented()
2701 && effect->isImplementationSoftware()) {
2702 return false;
2703 }
2704 }
2705 // Allow effects without processing or hw accelerated effects.
2706 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002707}
2708
2709// isCompatibleWithThread_l() must be called with thread->mLock held
2710bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2711{
2712 Mutex::Autolock _l(mLock);
2713 for (size_t i = 0; i < mEffects.size(); i++) {
2714 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2715 return false;
2716 }
2717 }
2718 return true;
2719}
2720
Glenn Kasten63238ef2015-03-02 15:50:29 -08002721} // namespace android