blob: 50ab6349607aa373cf2a1d274b291fe1564ccf37 [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
1491 result.append("\t\tSession Status State Engine:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001492 result.appendFormat("\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001493 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001494
1495 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001496 char uuidStr[64];
1497 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001498 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001499 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001500 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1501 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001502 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001503 mDescriptor.flags,
1504 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001505 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001506 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001507
1508 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001509 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001510
1511 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001512
1513 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001514 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1515 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1516 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 mConfig.inputCfg.buffer.frameCount,
1518 mConfig.inputCfg.samplingRate,
1519 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001520 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001521 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001522
1523 result.append("\t\t- Output configuration:\n");
1524 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001525 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001526 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001527 mConfig.outputCfg.buffer.frameCount,
1528 mConfig.outputCfg.samplingRate,
1529 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001530 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001531 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001532
rago94a1ee82017-07-21 15:11:02 -07001533#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001534
Andy Hungbded9c82017-11-30 18:47:35 -08001535 result.appendFormat("\t\t- HAL buffers:\n"
1536 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1537 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1538 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1539 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1540 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001541#endif
1542
Andy Hung9718d662017-12-22 17:57:39 -08001543 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001544 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001545 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001546 for (size_t i = 0; i < mHandles.size(); ++i) {
1547 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001548 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001549 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001550 result.append(buffer);
1551 }
1552 }
1553
Eric Laurentca7cc822012-11-19 14:55:58 -08001554 write(fd, result.string(), result.length());
1555
Mikhail Naganov4d547672019-02-22 14:19:19 -08001556 if (mEffectInterface != 0) {
1557 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1558 (void)mEffectInterface->dump(fd);
1559 }
1560
Eric Laurentca7cc822012-11-19 14:55:58 -08001561 if (locked) {
1562 mLock.unlock();
1563 }
1564}
1565
1566// ----------------------------------------------------------------------------
1567// EffectHandle implementation
1568// ----------------------------------------------------------------------------
1569
1570#undef LOG_TAG
1571#define LOG_TAG "AudioFlinger::EffectHandle"
1572
1573AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1574 const sp<AudioFlinger::Client>& client,
1575 const sp<IEffectClient>& effectClient,
1576 int32_t priority)
1577 : BnEffect(),
1578 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001579 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001580{
1581 ALOGV("constructor %p", this);
1582
1583 if (client == 0) {
1584 return;
1585 }
1586 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1587 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001588 if (mCblkMemory == 0 ||
1589 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001590 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001591 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001592 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001593 return;
1594 }
Glenn Kastene75da402013-11-20 13:54:52 -08001595 new(mCblk) effect_param_cblk_t();
1596 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001597}
1598
1599AudioFlinger::EffectHandle::~EffectHandle()
1600{
1601 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001602 disconnect(false);
1603}
1604
Glenn Kastene75da402013-11-20 13:54:52 -08001605status_t AudioFlinger::EffectHandle::initCheck()
1606{
1607 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1608}
1609
Eric Laurentca7cc822012-11-19 14:55:58 -08001610status_t AudioFlinger::EffectHandle::enable()
1611{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001612 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001613 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001614 sp<EffectModule> effect = mEffect.promote();
1615 if (effect == 0 || mDisconnected) {
1616 return DEAD_OBJECT;
1617 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001618 if (!mHasControl) {
1619 return INVALID_OPERATION;
1620 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001621
1622 if (mEnabled) {
1623 return NO_ERROR;
1624 }
1625
1626 mEnabled = true;
1627
Eric Laurent6c796322019-04-09 14:13:17 -07001628 status_t status = effect->updatePolicyState();
1629 if (status != NO_ERROR) {
1630 mEnabled = false;
1631 return status;
1632 }
1633
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001634 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001635 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001636 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001637 }
1638
1639 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001640 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001641 return NO_ERROR;
1642 }
1643
Eric Laurent6c796322019-04-09 14:13:17 -07001644 status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001645 if (status != NO_ERROR) {
1646 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001647 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001648 }
1649 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001650 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001651 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001652 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1653 Mutex::Autolock _l(thread->mLock);
1654 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001655 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001656 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001657 if (thread->type() == ThreadBase::OFFLOAD) {
1658 PlaybackThread *t = (PlaybackThread *)thread.get();
1659 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1660 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001661 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001662 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1663 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001664 }
1665 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 }
1667 return status;
1668}
1669
1670status_t AudioFlinger::EffectHandle::disable()
1671{
1672 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001673 AutoMutex _l(mLock);
1674 sp<EffectModule> effect = mEffect.promote();
1675 if (effect == 0 || mDisconnected) {
1676 return DEAD_OBJECT;
1677 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001678 if (!mHasControl) {
1679 return INVALID_OPERATION;
1680 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001681
1682 if (!mEnabled) {
1683 return NO_ERROR;
1684 }
1685 mEnabled = false;
1686
Eric Laurent6c796322019-04-09 14:13:17 -07001687 effect->updatePolicyState();
1688
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001689 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001690 return NO_ERROR;
1691 }
1692
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001693 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001694
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001695 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001696 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001697 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001698 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1699 Mutex::Autolock _l(thread->mLock);
1700 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001701 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 }
1703
1704 return status;
1705}
1706
1707void AudioFlinger::EffectHandle::disconnect()
1708{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001709 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001710 disconnect(true);
1711}
1712
1713void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1714{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001715 AutoMutex _l(mLock);
1716 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1717 if (mDisconnected) {
1718 if (unpinIfLast) {
1719 android_errorWriteLog(0x534e4554, "32707507");
1720 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001721 return;
1722 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001723 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001724 {
1725 sp<EffectModule> effect = mEffect.promote();
1726 if (effect != 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001727 sp<ThreadBase> thread = effect->thread().promote();
1728 if (thread != 0) {
1729 thread->disconnectEffectHandle(this, unpinIfLast);
1730 } else if (effect->disconnectHandle(this, unpinIfLast) > 0) {
1731 ALOGW("%s Effect handle %p disconnected after thread destruction",
1732 __func__, this);
1733 }
1734 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001735 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001736 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001737
Eric Laurentca7cc822012-11-19 14:55:58 -08001738 if (mClient != 0) {
1739 if (mCblk != NULL) {
1740 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1741 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1742 }
1743 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001744 // Client destructor must run with AudioFlinger client mutex locked
1745 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001746 mClient.clear();
1747 }
1748}
1749
1750status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1751 uint32_t cmdSize,
1752 void *pCmdData,
1753 uint32_t *replySize,
1754 void *pReplyData)
1755{
1756 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001757 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001758
Eric Laurentc7ab3092017-06-15 18:43:46 -07001759 // reject commands reserved for internal use by audio framework if coming from outside
1760 // of audioserver
1761 switch(cmdCode) {
1762 case EFFECT_CMD_ENABLE:
1763 case EFFECT_CMD_DISABLE:
1764 case EFFECT_CMD_SET_PARAM:
1765 case EFFECT_CMD_SET_PARAM_DEFERRED:
1766 case EFFECT_CMD_SET_PARAM_COMMIT:
1767 case EFFECT_CMD_GET_PARAM:
1768 break;
1769 default:
1770 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1771 break;
1772 }
1773 android_errorWriteLog(0x534e4554, "62019992");
1774 return BAD_VALUE;
1775 }
1776
Eric Laurent1ffc5852016-12-15 14:46:09 -08001777 if (cmdCode == EFFECT_CMD_ENABLE) {
1778 if (*replySize < sizeof(int)) {
1779 android_errorWriteLog(0x534e4554, "32095713");
1780 return BAD_VALUE;
1781 }
1782 *(int *)pReplyData = NO_ERROR;
1783 *replySize = sizeof(int);
1784 return enable();
1785 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1786 if (*replySize < sizeof(int)) {
1787 android_errorWriteLog(0x534e4554, "32095713");
1788 return BAD_VALUE;
1789 }
1790 *(int *)pReplyData = NO_ERROR;
1791 *replySize = sizeof(int);
1792 return disable();
1793 }
1794
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001795 AutoMutex _l(mLock);
1796 sp<EffectModule> effect = mEffect.promote();
1797 if (effect == 0 || mDisconnected) {
1798 return DEAD_OBJECT;
1799 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001800 // only get parameter command is permitted for applications not controlling the effect
1801 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1802 return INVALID_OPERATION;
1803 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001804 if (mClient == 0) {
1805 return INVALID_OPERATION;
1806 }
1807
1808 // handle commands that are not forwarded transparently to effect engine
1809 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001810 if (*replySize < sizeof(int)) {
1811 android_errorWriteLog(0x534e4554, "32095713");
1812 return BAD_VALUE;
1813 }
1814 *(int *)pReplyData = NO_ERROR;
1815 *replySize = sizeof(int);
1816
Eric Laurentca7cc822012-11-19 14:55:58 -08001817 // No need to trylock() here as this function is executed in the binder thread serving a
1818 // particular client process: no risk to block the whole media server process or mixer
1819 // threads if we are stuck here
1820 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001821 // keep local copy of index in case of client corruption b/32220769
1822 const uint32_t clientIndex = mCblk->clientIndex;
1823 const uint32_t serverIndex = mCblk->serverIndex;
1824 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1825 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001826 mCblk->serverIndex = 0;
1827 mCblk->clientIndex = 0;
1828 return BAD_VALUE;
1829 }
1830 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001831 effect_param_t *param = NULL;
1832 for (uint32_t index = serverIndex; index < clientIndex;) {
1833 int *p = (int *)(mBuffer + index);
1834 const int size = *p++;
1835 if (size < 0
1836 || size > EFFECT_PARAM_BUFFER_SIZE
1837 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001838 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001839 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 break;
1841 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001842
1843 // copy to local memory in case of client corruption b/32220769
1844 param = (effect_param_t *)realloc(param, size);
1845 if (param == NULL) {
1846 ALOGW("command(): out of memory");
1847 status = NO_MEMORY;
1848 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001849 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001850 memcpy(param, p, size);
1851
1852 int reply = 0;
1853 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001854 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001855 size,
1856 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001857 &rsize,
1858 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001859
1860 // verify shared memory: server index shouldn't change; client index can't go back.
1861 if (serverIndex != mCblk->serverIndex
1862 || clientIndex > mCblk->clientIndex) {
1863 android_errorWriteLog(0x534e4554, "32220769");
1864 status = BAD_VALUE;
1865 break;
1866 }
1867
Eric Laurentca7cc822012-11-19 14:55:58 -08001868 // stop at first error encountered
1869 if (ret != NO_ERROR) {
1870 status = ret;
1871 *(int *)pReplyData = reply;
1872 break;
1873 } else if (reply != NO_ERROR) {
1874 *(int *)pReplyData = reply;
1875 break;
1876 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001877 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001878 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001879 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001880 mCblk->serverIndex = 0;
1881 mCblk->clientIndex = 0;
1882 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001883 }
1884
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001885 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001886}
1887
1888void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1889{
1890 ALOGV("setControl %p control %d", this, hasControl);
1891
1892 mHasControl = hasControl;
1893 mEnabled = enabled;
1894
1895 if (signal && mEffectClient != 0) {
1896 mEffectClient->controlStatusChanged(hasControl);
1897 }
1898}
1899
1900void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1901 uint32_t cmdSize,
1902 void *pCmdData,
1903 uint32_t replySize,
1904 void *pReplyData)
1905{
1906 if (mEffectClient != 0) {
1907 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1908 }
1909}
1910
1911
1912
1913void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1914{
1915 if (mEffectClient != 0) {
1916 mEffectClient->enableStatusChanged(enabled);
1917 }
1918}
1919
1920status_t AudioFlinger::EffectHandle::onTransact(
1921 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1922{
1923 return BnEffect::onTransact(code, data, reply, flags);
1924}
1925
1926
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001927void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001928{
1929 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1930
Marco Nelissenb2208842014-02-07 14:00:50 -08001931 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001932 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001933 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001934 mHasControl ? "yes" : "no",
1935 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001936 mCblk ? mCblk->clientIndex : 0,
1937 mCblk ? mCblk->serverIndex : 0
1938 );
1939
1940 if (locked) {
1941 mCblk->lock.unlock();
1942 }
1943}
1944
1945#undef LOG_TAG
1946#define LOG_TAG "AudioFlinger::EffectChain"
1947
1948AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001949 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001950 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001951 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001952 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001953{
1954 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1955 if (thread == NULL) {
1956 return;
1957 }
1958 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1959 thread->frameCount();
1960}
1961
1962AudioFlinger::EffectChain::~EffectChain()
1963{
Eric Laurentca7cc822012-11-19 14:55:58 -08001964}
1965
1966// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1967sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1968 effect_descriptor_t *descriptor)
1969{
1970 size_t size = mEffects.size();
1971
1972 for (size_t i = 0; i < size; i++) {
1973 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1974 return mEffects[i];
1975 }
1976 }
1977 return 0;
1978}
1979
1980// getEffectFromId_l() must be called with ThreadBase::mLock held
1981sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1982{
1983 size_t size = mEffects.size();
1984
1985 for (size_t i = 0; i < size; i++) {
1986 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1987 if (id == 0 || mEffects[i]->id() == id) {
1988 return mEffects[i];
1989 }
1990 }
1991 return 0;
1992}
1993
1994// getEffectFromType_l() must be called with ThreadBase::mLock held
1995sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1996 const effect_uuid_t *type)
1997{
1998 size_t size = mEffects.size();
1999
2000 for (size_t i = 0; i < size; i++) {
2001 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2002 return mEffects[i];
2003 }
2004 }
2005 return 0;
2006}
2007
Eric Laurent6c796322019-04-09 14:13:17 -07002008std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2009{
2010 std::vector<int> ids;
2011 Mutex::Autolock _l(mLock);
2012 for (size_t i = 0; i < mEffects.size(); i++) {
2013 ids.push_back(mEffects[i]->id());
2014 }
2015 return ids;
2016}
2017
Eric Laurentca7cc822012-11-19 14:55:58 -08002018void AudioFlinger::EffectChain::clearInputBuffer()
2019{
2020 Mutex::Autolock _l(mLock);
2021 sp<ThreadBase> thread = mThread.promote();
2022 if (thread == 0) {
2023 ALOGW("clearInputBuffer(): cannot promote mixer thread");
2024 return;
2025 }
2026 clearInputBuffer_l(thread);
2027}
2028
2029// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002030void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08002031{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002032 if (mInBuffer == NULL) {
2033 return;
2034 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002035 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08002036 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002037
Mikhail Naganov022b9952017-01-04 16:36:51 -08002038 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
2039 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002040}
2041
2042// Must be called with EffectChain::mLock locked
2043void AudioFlinger::EffectChain::process_l()
2044{
2045 sp<ThreadBase> thread = mThread.promote();
2046 if (thread == 0) {
2047 ALOGW("process_l(): cannot promote mixer thread");
2048 return;
2049 }
2050 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
2051 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002052 // never process effects when:
2053 // - on an OFFLOAD thread
2054 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08002055 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
2056 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08002057 if (!isGlobalSession) {
2058 bool tracksOnSession = (trackCnt() != 0);
2059
2060 if (!tracksOnSession && mTailBufferCount == 0) {
2061 doProcess = false;
2062 }
2063
2064 if (activeTrackCnt() == 0) {
2065 // if no track is active and the effect tail has not been rendered,
2066 // the input buffer must be cleared here as the mixer process will not do it
2067 if (tracksOnSession || mTailBufferCount > 0) {
2068 clearInputBuffer_l(thread);
2069 if (mTailBufferCount > 0) {
2070 mTailBufferCount--;
2071 }
2072 }
2073 }
2074 }
2075
2076 size_t size = mEffects.size();
2077 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002078 // Only the input and output buffers of the chain can be external,
2079 // and 'update' / 'commit' do nothing for allocated buffers, thus
2080 // it's not needed to consider any other buffers here.
2081 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002082 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2083 mOutBuffer->update();
2084 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002085 for (size_t i = 0; i < size; i++) {
2086 mEffects[i]->process();
2087 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002088 mInBuffer->commit();
2089 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2090 mOutBuffer->commit();
2091 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002092 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002093 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002094 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002095 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2096 }
2097 if (doResetVolume) {
2098 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002099 }
2100}
2101
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002102// createEffect_l() must be called with ThreadBase::mLock held
2103status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
2104 ThreadBase *thread,
2105 effect_descriptor_t *desc,
2106 int id,
2107 audio_session_t sessionId,
2108 bool pinned)
2109{
2110 Mutex::Autolock _l(mLock);
2111 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
2112 status_t lStatus = effect->status();
2113 if (lStatus == NO_ERROR) {
2114 lStatus = addEffect_ll(effect);
2115 }
2116 if (lStatus != NO_ERROR) {
2117 effect.clear();
2118 }
2119 return lStatus;
2120}
2121
2122// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002123status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2124{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002125 Mutex::Autolock _l(mLock);
2126 return addEffect_ll(effect);
2127}
2128// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2129status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2130{
Eric Laurentca7cc822012-11-19 14:55:58 -08002131 effect_descriptor_t desc = effect->desc();
2132 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2133
Eric Laurentca7cc822012-11-19 14:55:58 -08002134 effect->setChain(this);
2135 sp<ThreadBase> thread = mThread.promote();
2136 if (thread == 0) {
2137 return NO_INIT;
2138 }
2139 effect->setThread(thread);
2140
2141 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2142 // Auxiliary effects are inserted at the beginning of mEffects vector as
2143 // they are processed first and accumulated in chain input buffer
2144 mEffects.insertAt(effect, 0);
2145
2146 // the input buffer for auxiliary effect contains mono samples in
2147 // 32 bit format. This is to avoid saturation in AudoMixer
2148 // accumulation stage. Saturation is done in EffectModule::process() before
2149 // calling the process in effect engine
2150 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002151 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002152#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002153 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002154 numSamples * sizeof(float), &halBuffer);
2155#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002156 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002157 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002158#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002159 if (result != OK) return result;
2160 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002161 // auxiliary effects output samples to chain input buffer for further processing
2162 // by insert effects
2163 effect->setOutBuffer(mInBuffer);
2164 } else {
2165 // Insert effects are inserted at the end of mEffects vector as they are processed
2166 // after track and auxiliary effects.
2167 // Insert effect order as a function of indicated preference:
2168 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2169 // another effect is present
2170 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2171 // last effect claiming first position
2172 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2173 // first effect claiming last position
2174 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2175 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2176 // already present
2177
2178 size_t size = mEffects.size();
2179 size_t idx_insert = size;
2180 ssize_t idx_insert_first = -1;
2181 ssize_t idx_insert_last = -1;
2182
2183 for (size_t i = 0; i < size; i++) {
2184 effect_descriptor_t d = mEffects[i]->desc();
2185 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2186 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2187 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2188 // check invalid effect chaining combinations
2189 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2190 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2191 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2192 desc.name, d.name);
2193 return INVALID_OPERATION;
2194 }
2195 // remember position of first insert effect and by default
2196 // select this as insert position for new effect
2197 if (idx_insert == size) {
2198 idx_insert = i;
2199 }
2200 // remember position of last insert effect claiming
2201 // first position
2202 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2203 idx_insert_first = i;
2204 }
2205 // remember position of first insert effect claiming
2206 // last position
2207 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2208 idx_insert_last == -1) {
2209 idx_insert_last = i;
2210 }
2211 }
2212 }
2213
2214 // modify idx_insert from first position if needed
2215 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2216 if (idx_insert_last != -1) {
2217 idx_insert = idx_insert_last;
2218 } else {
2219 idx_insert = size;
2220 }
2221 } else {
2222 if (idx_insert_first != -1) {
2223 idx_insert = idx_insert_first + 1;
2224 }
2225 }
2226
2227 // always read samples from chain input buffer
2228 effect->setInBuffer(mInBuffer);
2229
2230 // if last effect in the chain, output samples to chain
2231 // output buffer, otherwise to chain input buffer
2232 if (idx_insert == size) {
2233 if (idx_insert != 0) {
2234 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2235 mEffects[idx_insert-1]->configure();
2236 }
2237 effect->setOutBuffer(mOutBuffer);
2238 } else {
2239 effect->setOutBuffer(mInBuffer);
2240 }
2241 mEffects.insertAt(effect, idx_insert);
2242
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002243 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002244 idx_insert);
2245 }
2246 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002247
Eric Laurentca7cc822012-11-19 14:55:58 -08002248 return NO_ERROR;
2249}
2250
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002251// removeEffect_l() must be called with ThreadBase::mLock held
2252size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2253 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002254{
2255 Mutex::Autolock _l(mLock);
2256 size_t size = mEffects.size();
2257 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2258
2259 for (size_t i = 0; i < size; i++) {
2260 if (effect == mEffects[i]) {
2261 // calling stop here will remove pre-processing effect from the audio HAL.
2262 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2263 // the middle of a read from audio HAL
2264 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2265 mEffects[i]->state() == EffectModule::STOPPING) {
2266 mEffects[i]->stop();
2267 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002268 if (release) {
2269 mEffects[i]->release_l();
2270 }
2271
Mikhail Naganov022b9952017-01-04 16:36:51 -08002272 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002273 if (i == size - 1 && i != 0) {
2274 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2275 mEffects[i - 1]->configure();
2276 }
2277 }
2278 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002279 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002280 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002281
Eric Laurentca7cc822012-11-19 14:55:58 -08002282 break;
2283 }
2284 }
2285
2286 return mEffects.size();
2287}
2288
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002289// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002290void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2291{
2292 size_t size = mEffects.size();
2293 for (size_t i = 0; i < size; i++) {
2294 mEffects[i]->setDevice(device);
2295 }
2296}
2297
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002298// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002299void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2300{
2301 size_t size = mEffects.size();
2302 for (size_t i = 0; i < size; i++) {
2303 mEffects[i]->setMode(mode);
2304 }
2305}
2306
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002307// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002308void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2309{
2310 size_t size = mEffects.size();
2311 for (size_t i = 0; i < size; i++) {
2312 mEffects[i]->setAudioSource(source);
2313 }
2314}
2315
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002316// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002317bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002318{
2319 uint32_t newLeft = *left;
2320 uint32_t newRight = *right;
2321 bool hasControl = false;
2322 int ctrlIdx = -1;
2323 size_t size = mEffects.size();
2324
2325 // first update volume controller
2326 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002327 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002328 ctrlIdx = i - 1;
2329 hasControl = true;
2330 break;
2331 }
2332 }
2333
Eric Laurentfa1e1232016-08-02 19:01:49 -07002334 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002335 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002336 if (hasControl) {
2337 *left = mNewLeftVolume;
2338 *right = mNewRightVolume;
2339 }
2340 return hasControl;
2341 }
2342
2343 mVolumeCtrlIdx = ctrlIdx;
2344 mLeftVolume = newLeft;
2345 mRightVolume = newRight;
2346
2347 // second get volume update from volume controller
2348 if (ctrlIdx >= 0) {
2349 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2350 mNewLeftVolume = newLeft;
2351 mNewRightVolume = newRight;
2352 }
2353 // then indicate volume to all other effects in chain.
2354 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002355 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002356 uint32_t lVol = newLeft;
2357 uint32_t rVol = newRight;
2358
2359 for (size_t i = 0; i < size; i++) {
2360 if ((int)i == ctrlIdx) {
2361 continue;
2362 }
2363 // this also works for ctrlIdx == -1 when there is no volume controller
2364 if ((int)i > ctrlIdx) {
2365 lVol = *left;
2366 rVol = *right;
2367 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002368 // Pass requested volume directly if this is volume monitor module
2369 if (mEffects[i]->isVolumeMonitor()) {
2370 mEffects[i]->setVolume(left, right, false);
2371 } else {
2372 mEffects[i]->setVolume(&lVol, &rVol, false);
2373 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002374 }
2375 *left = newLeft;
2376 *right = newRight;
2377
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002378 setVolumeForOutput_l(*left, *right);
2379
Eric Laurentca7cc822012-11-19 14:55:58 -08002380 return hasControl;
2381}
2382
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002383// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002384void AudioFlinger::EffectChain::resetVolume_l()
2385{
Eric Laurente7449bf2016-08-03 18:44:07 -07002386 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2387 uint32_t left = mLeftVolume;
2388 uint32_t right = mRightVolume;
2389 (void)setVolume_l(&left, &right, true);
2390 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002391}
2392
Eric Laurent1b928682014-10-02 19:41:47 -07002393void AudioFlinger::EffectChain::syncHalEffectsState()
2394{
2395 Mutex::Autolock _l(mLock);
2396 for (size_t i = 0; i < mEffects.size(); i++) {
2397 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2398 mEffects[i]->state() == EffectModule::STOPPING) {
2399 mEffects[i]->addEffectToHal_l();
2400 }
2401 }
2402}
2403
Eric Laurentca7cc822012-11-19 14:55:58 -08002404void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2405{
Eric Laurentca7cc822012-11-19 14:55:58 -08002406 String8 result;
2407
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002408 const size_t numEffects = mEffects.size();
2409 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002410
Marco Nelissenb2208842014-02-07 14:00:50 -08002411 if (numEffects) {
2412 bool locked = AudioFlinger::dumpTryLock(mLock);
2413 // failed to lock - AudioFlinger is probably deadlocked
2414 if (!locked) {
2415 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002416 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002417
Andy Hungbded9c82017-11-30 18:47:35 -08002418 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2419 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2420 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2421 (int)inBufferStr.size(), "In buffer ",
2422 (int)outBufferStr.size(), "Out buffer ");
2423 result.appendFormat("\t%s %s %d\n",
2424 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002425 write(fd, result.string(), result.size());
2426
2427 for (size_t i = 0; i < numEffects; ++i) {
2428 sp<EffectModule> effect = mEffects[i];
2429 if (effect != 0) {
2430 effect->dump(fd, args);
2431 }
2432 }
2433
2434 if (locked) {
2435 mLock.unlock();
2436 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002437 } else {
2438 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002439 }
2440}
2441
2442// must be called with ThreadBase::mLock held
2443void AudioFlinger::EffectChain::setEffectSuspended_l(
2444 const effect_uuid_t *type, bool suspend)
2445{
2446 sp<SuspendedEffectDesc> desc;
2447 // use effect type UUID timelow as key as there is no real risk of identical
2448 // timeLow fields among effect type UUIDs.
2449 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2450 if (suspend) {
2451 if (index >= 0) {
2452 desc = mSuspendedEffects.valueAt(index);
2453 } else {
2454 desc = new SuspendedEffectDesc();
2455 desc->mType = *type;
2456 mSuspendedEffects.add(type->timeLow, desc);
2457 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2458 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002459
Eric Laurentca7cc822012-11-19 14:55:58 -08002460 if (desc->mRefCount++ == 0) {
2461 sp<EffectModule> effect = getEffectIfEnabled(type);
2462 if (effect != 0) {
2463 desc->mEffect = effect;
2464 effect->setSuspended(true);
2465 effect->setEnabled(false);
2466 }
2467 }
2468 } else {
2469 if (index < 0) {
2470 return;
2471 }
2472 desc = mSuspendedEffects.valueAt(index);
2473 if (desc->mRefCount <= 0) {
2474 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002475 desc->mRefCount = 0;
2476 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002477 }
2478 if (--desc->mRefCount == 0) {
2479 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2480 if (desc->mEffect != 0) {
2481 sp<EffectModule> effect = desc->mEffect.promote();
2482 if (effect != 0) {
2483 effect->setSuspended(false);
2484 effect->lock();
2485 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002486 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002487 effect->setEnabled_l(handle->enabled());
2488 }
2489 effect->unlock();
2490 }
2491 desc->mEffect.clear();
2492 }
2493 mSuspendedEffects.removeItemsAt(index);
2494 }
2495 }
2496}
2497
2498// must be called with ThreadBase::mLock held
2499void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2500{
2501 sp<SuspendedEffectDesc> desc;
2502
2503 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2504 if (suspend) {
2505 if (index >= 0) {
2506 desc = mSuspendedEffects.valueAt(index);
2507 } else {
2508 desc = new SuspendedEffectDesc();
2509 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2510 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2511 }
2512 if (desc->mRefCount++ == 0) {
2513 Vector< sp<EffectModule> > effects;
2514 getSuspendEligibleEffects(effects);
2515 for (size_t i = 0; i < effects.size(); i++) {
2516 setEffectSuspended_l(&effects[i]->desc().type, true);
2517 }
2518 }
2519 } else {
2520 if (index < 0) {
2521 return;
2522 }
2523 desc = mSuspendedEffects.valueAt(index);
2524 if (desc->mRefCount <= 0) {
2525 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2526 desc->mRefCount = 1;
2527 }
2528 if (--desc->mRefCount == 0) {
2529 Vector<const effect_uuid_t *> types;
2530 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2531 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2532 continue;
2533 }
2534 types.add(&mSuspendedEffects.valueAt(i)->mType);
2535 }
2536 for (size_t i = 0; i < types.size(); i++) {
2537 setEffectSuspended_l(types[i], false);
2538 }
2539 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2540 mSuspendedEffects.keyAt(index));
2541 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2542 }
2543 }
2544}
2545
2546
2547// The volume effect is used for automated tests only
2548#ifndef OPENSL_ES_H_
2549static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2550 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2551const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2552#endif //OPENSL_ES_H_
2553
Eric Laurentd8365c52017-07-16 15:27:05 -07002554/* static */
2555bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2556{
2557 // Only NS and AEC are suspended when BtNRec is off
2558 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2559 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2560 return true;
2561 }
2562 return false;
2563}
2564
Eric Laurentca7cc822012-11-19 14:55:58 -08002565bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2566{
2567 // auxiliary effects and visualizer are never suspended on output mix
2568 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2569 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2570 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2571 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2572 return false;
2573 }
2574 return true;
2575}
2576
2577void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2578 Vector< sp<AudioFlinger::EffectModule> > &effects)
2579{
2580 effects.clear();
2581 for (size_t i = 0; i < mEffects.size(); i++) {
2582 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2583 effects.add(mEffects[i]);
2584 }
2585 }
2586}
2587
2588sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2589 const effect_uuid_t *type)
2590{
2591 sp<EffectModule> effect = getEffectFromType_l(type);
2592 return effect != 0 && effect->isEnabled() ? effect : 0;
2593}
2594
2595void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2596 bool enabled)
2597{
2598 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2599 if (enabled) {
2600 if (index < 0) {
2601 // if the effect is not suspend check if all effects are suspended
2602 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2603 if (index < 0) {
2604 return;
2605 }
2606 if (!isEffectEligibleForSuspend(effect->desc())) {
2607 return;
2608 }
2609 setEffectSuspended_l(&effect->desc().type, enabled);
2610 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2611 if (index < 0) {
2612 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2613 return;
2614 }
2615 }
2616 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2617 effect->desc().type.timeLow);
2618 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002619 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002620 if (desc->mEffect == 0) {
2621 desc->mEffect = effect;
2622 effect->setEnabled(false);
2623 effect->setSuspended(true);
2624 }
2625 } else {
2626 if (index < 0) {
2627 return;
2628 }
2629 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2630 effect->desc().type.timeLow);
2631 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2632 desc->mEffect.clear();
2633 effect->setSuspended(false);
2634 }
2635}
2636
Eric Laurent5baf2af2013-09-12 17:37:00 -07002637bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002638{
2639 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002640 return isNonOffloadableEnabled_l();
2641}
2642
2643bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2644{
Eric Laurent813e2a72013-08-31 12:59:48 -07002645 size_t size = mEffects.size();
2646 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002647 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002648 return true;
2649 }
2650 }
2651 return false;
2652}
2653
Eric Laurentaaa44472014-09-12 17:41:50 -07002654void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2655{
2656 Mutex::Autolock _l(mLock);
2657 mThread = thread;
2658 for (size_t i = 0; i < mEffects.size(); i++) {
2659 mEffects[i]->setThread(thread);
2660 }
2661}
2662
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002663void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2664{
2665 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2666 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2667 }
2668 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2669 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2670 }
2671}
2672
2673void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2674{
2675 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2676 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2677 }
2678 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2679 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2680 }
2681}
2682
2683bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002684{
2685 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002686 for (const auto &effect : mEffects) {
2687 if (effect->isProcessImplemented()) {
2688 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002689 }
2690 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002691 // Allow effects without processing.
2692 return true;
2693}
2694
2695bool AudioFlinger::EffectChain::isFastCompatible() const
2696{
2697 Mutex::Autolock _l(mLock);
2698 for (const auto &effect : mEffects) {
2699 if (effect->isProcessImplemented()
2700 && effect->isImplementationSoftware()) {
2701 return false;
2702 }
2703 }
2704 // Allow effects without processing or hw accelerated effects.
2705 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002706}
2707
2708// isCompatibleWithThread_l() must be called with thread->mLock held
2709bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2710{
2711 Mutex::Autolock _l(mLock);
2712 for (size_t i = 0; i < mEffects.size(); i++) {
2713 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2714 return false;
2715 }
2716 }
2717 return true;
2718}
2719
Glenn Kasten63238ef2015-03-02 15:50:29 -08002720} // namespace android