blob: bbd0d2c6b9dc10120202ede2ca33de295e5e054f [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070027#include <system/audio_effects/effect_dynamicsprocessing.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070028#include <system/audio_effects/effect_ns.h>
29#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080030#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080031#include <audio_utils/primitives.h>
jiabinb8269fd2019-11-11 12:16:27 -080032#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070033#include <media/AudioEffect.h>
jiabinb8269fd2019-11-11 12:16:27 -080034#include <media/AudioDeviceTypeAddr.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070035#include <media/audiohal/EffectHalInterface.h>
36#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070037#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080038
39#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080040
41// ----------------------------------------------------------------------------
42
43// Note: the following macro is used for extremely verbose logging message. In
44// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
45// 0; but one side effect of this is to turn all LOGV's as well. Some messages
46// are so verbose that we want to suppress them even when we have ALOG_ASSERT
47// turned on. Do not uncomment the #def below unless you really know what you
48// are doing and want to see all of the extremely verbose messages.
49//#define VERY_VERY_VERBOSE_LOGGING
50#ifdef VERY_VERY_VERBOSE_LOGGING
51#define ALOGVV ALOGV
52#else
53#define ALOGVV(a...) do { } while(0)
54#endif
55
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090056#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
57
Eric Laurentca7cc822012-11-19 14:55:58 -080058namespace android {
59
60// ----------------------------------------------------------------------------
61// EffectModule implementation
62// ----------------------------------------------------------------------------
63
64#undef LOG_TAG
65#define LOG_TAG "AudioFlinger::EffectModule"
66
67AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
68 const wp<AudioFlinger::EffectChain>& chain,
69 effect_descriptor_t *desc,
70 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080071 audio_session_t sessionId,
72 bool pinned)
73 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080074 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
75 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080076 // clear mConfig to ensure consistent initial value of buffer framecount
77 // in case buffers are associated by setInBuffer() or setOutBuffer()
78 // prior to configure().
79 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080080 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080081 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
82 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070083 mSuspended(false),
Andy Hung62aef7d2017-12-14 14:50:40 -080084 mOffloaded(false),
Eric Laurentaaa44472014-09-12 17:41:50 -070085 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070086#ifdef FLOAT_EFFECT_CHAIN
87 , mSupportsFloat(false)
88#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080089{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080090 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080091 int lStatus;
92
93 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070094 mStatus = -ENODEV;
95 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070096 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070097 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070098 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070099 mStatus = effectsFactory->createEffect(
100 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
101 }
102 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800103
104 if (mStatus != NO_ERROR) {
105 return;
106 }
107 lStatus = init();
108 if (lStatus < 0) {
109 mStatus = lStatus;
110 goto Error;
111 }
112
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800113 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700114 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800115
Eric Laurentca7cc822012-11-19 14:55:58 -0800116 return;
117Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700118 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800119 ALOGV("Constructor Error %d", mStatus);
120}
121
122AudioFlinger::EffectModule::~EffectModule()
123{
124 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700125 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700126 char uuidStr[64];
127 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
128 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
129 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800130 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800131 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800132
Eric Laurentca7cc822012-11-19 14:55:58 -0800133}
134
135status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
136{
137 status_t status;
138
139 Mutex::Autolock _l(mLock);
140 int priority = handle->priority();
141 size_t size = mHandles.size();
142 EffectHandle *controlHandle = NULL;
143 size_t i;
144 for (i = 0; i < size; i++) {
145 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800146 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800147 continue;
148 }
149 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700150 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800151 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700152 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800153 if (h->priority() <= priority) {
154 break;
155 }
156 }
157 // if inserted in first place, move effect control from previous owner to this handle
158 if (i == 0) {
159 bool enabled = false;
160 if (controlHandle != NULL) {
161 enabled = controlHandle->enabled();
162 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
163 }
164 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
165 status = NO_ERROR;
166 } else {
167 status = ALREADY_EXISTS;
168 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700169 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800170 mHandles.insertAt(handle, i);
171 return status;
172}
173
Eric Laurent6c796322019-04-09 14:13:17 -0700174status_t AudioFlinger::EffectModule::updatePolicyState()
175{
176 status_t status = NO_ERROR;
177 bool doRegister = false;
178 bool registered = false;
179 bool doEnable = false;
180 bool enabled = false;
181 audio_io_handle_t io;
182 uint32_t strategy;
183
184 {
185 Mutex::Autolock _l(mLock);
186 // register effect when first handle is attached and unregister when last handle is removed
187 if (mPolicyRegistered != mHandles.size() > 0) {
188 doRegister = true;
189 mPolicyRegistered = mHandles.size() > 0;
190 if (mPolicyRegistered) {
191 sp <EffectChain> chain = mChain.promote();
192 sp <ThreadBase> thread = mThread.promote();
193
194 if (thread == nullptr || chain == nullptr) {
195 return INVALID_OPERATION;
196 }
197 io = thread->id();
198 strategy = chain->strategy();
199 }
200 }
201 // enable effect when registered according to enable state requested by controlling handle
202 if (mHandles.size() > 0) {
203 EffectHandle *handle = controlHandle_l();
204 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
205 doEnable = true;
206 mPolicyEnabled = handle->enabled();
207 }
208 }
209 registered = mPolicyRegistered;
210 enabled = mPolicyEnabled;
211 mPolicyLock.lock();
212 }
213 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
214 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
215 if (doRegister) {
216 if (registered) {
217 status = AudioSystem::registerEffect(
218 &mDescriptor,
219 io,
220 strategy,
221 mSessionId,
222 mId);
223 } else {
224 status = AudioSystem::unregisterEffect(mId);
225 }
226 }
227 if (registered && doEnable) {
228 status = AudioSystem::setEffectEnabled(mId, enabled);
229 }
230 mPolicyLock.unlock();
231
232 return status;
233}
234
235
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800236ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800237{
238 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800239 return removeHandle_l(handle);
240}
241
242ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
243{
Eric Laurentca7cc822012-11-19 14:55:58 -0800244 size_t size = mHandles.size();
245 size_t i;
246 for (i = 0; i < size; i++) {
247 if (mHandles[i] == handle) {
248 break;
249 }
250 }
251 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800252 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
253 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800254 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800255 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800256
257 mHandles.removeAt(i);
258 // if removed from first place, move effect control from this handle to next in line
259 if (i == 0) {
260 EffectHandle *h = controlHandle_l();
261 if (h != NULL) {
262 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
263 }
264 }
265
266 // Prevent calls to process() and other functions on effect interface from now on.
267 // The effect engine will be released by the destructor when the last strong reference on
268 // this object is released which can happen after next process is called.
269 if (mHandles.size() == 0 && !mPinned) {
270 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800271 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800272 }
273
274 return mHandles.size();
275}
276
277// must be called with EffectModule::mLock held
278AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
279{
280 // the first valid handle in the list has control over the module
281 for (size_t i = 0; i < mHandles.size(); i++) {
282 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800283 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800284 return h;
285 }
286 }
287
288 return NULL;
289}
290
Eric Laurentf10c7092016-12-06 17:09:56 -0800291// unsafe method called when the effect parent thread has been destroyed
292ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
293{
294 ALOGV("disconnect() %p handle %p", this, handle);
295 Mutex::Autolock _l(mLock);
296 ssize_t numHandles = removeHandle_l(handle);
297 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurentf10c7092016-12-06 17:09:56 -0800298 sp<AudioFlinger> af = mAudioFlinger.promote();
299 if (af != 0) {
300 mLock.unlock();
301 af->updateOrphanEffectChains(this);
302 mLock.lock();
303 }
304 }
305 return numHandles;
306}
307
Eric Laurentfa1e1232016-08-02 19:01:49 -0700308bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800309 Mutex::Autolock _l(mLock);
310
Eric Laurentfa1e1232016-08-02 19:01:49 -0700311 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800312 switch (mState) {
313 case RESTART:
314 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700315 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800316
317 case STARTING:
318 // clear auxiliary effect input buffer for next accumulation
319 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
320 memset(mConfig.inputCfg.buffer.raw,
321 0,
322 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
323 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700324 if (start_l() == NO_ERROR) {
325 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700326 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700327 } else {
328 mState = IDLE;
329 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800330 break;
331 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900332 // volume control for offload and direct threads must take effect immediately.
333 if (stop_l() == NO_ERROR
334 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700335 mDisableWaitCnt = mMaxDisableWaitCnt;
336 } else {
337 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
338 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800339 mState = STOPPED;
340 break;
341 case STOPPED:
342 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
343 // turn off sequence.
344 if (--mDisableWaitCnt == 0) {
345 reset_l();
346 mState = IDLE;
347 }
348 break;
349 default: //IDLE , ACTIVE, DESTROYED
350 break;
351 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700352
353 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800354}
355
356void AudioFlinger::EffectModule::process()
357{
358 Mutex::Autolock _l(mLock);
359
Mikhail Naganov022b9952017-01-04 16:36:51 -0800360 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800361 return;
362 }
363
rago94a1ee82017-07-21 15:11:02 -0700364 const uint32_t inChannelCount =
365 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
366 const uint32_t outChannelCount =
367 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
368 const bool auxType =
369 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
370
Andy Hungfa69ca32017-11-30 10:07:53 -0800371 // safeInputOutputSampleCount is 0 if the channel count between input and output
372 // buffers do not match. This prevents automatic accumulation or copying between the
373 // input and output effect buffers without an intermediary effect process.
374 // TODO: consider implementing channel conversion.
375 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700376 mInChannelCountRequested != mOutChannelCountRequested ? 0
377 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800378 mConfig.inputCfg.buffer.frameCount,
379 mConfig.outputCfg.buffer.frameCount);
380 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
381#ifdef FLOAT_EFFECT_CHAIN
382 accumulate_float(
383 mConfig.outputCfg.buffer.f32,
384 mConfig.inputCfg.buffer.f32,
385 safeInputOutputSampleCount);
386#else
387 accumulate_i16(
388 mConfig.outputCfg.buffer.s16,
389 mConfig.inputCfg.buffer.s16,
390 safeInputOutputSampleCount);
391#endif
392 };
393 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
394#ifdef FLOAT_EFFECT_CHAIN
395 memcpy(
396 mConfig.outputCfg.buffer.f32,
397 mConfig.inputCfg.buffer.f32,
398 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
399
400#else
401 memcpy(
402 mConfig.outputCfg.buffer.s16,
403 mConfig.inputCfg.buffer.s16,
404 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
405#endif
406 };
407
Eric Laurentca7cc822012-11-19 14:55:58 -0800408 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700409 int ret;
410 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700411 if (auxType) {
412 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800413 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700414#ifdef FLOAT_EFFECT_CHAIN
415 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800416#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700417 // Do in-place float conversion for auxiliary effect input buffer.
418 static_assert(sizeof(float) <= sizeof(int32_t),
419 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
420
Andy Hungfa69ca32017-11-30 10:07:53 -0800421 memcpy_to_float_from_q4_27(
422 mConfig.inputCfg.buffer.f32,
423 mConfig.inputCfg.buffer.s32,
424 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800425#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800426 } else
Andy Hung116a4982017-11-30 10:15:08 -0800427#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800428 {
Andy Hung116a4982017-11-30 10:15:08 -0800429#ifdef FLOAT_AUX
430 memcpy_to_i16_from_float(
431 mConfig.inputCfg.buffer.s16,
432 mConfig.inputCfg.buffer.f32,
433 mConfig.inputCfg.buffer.frameCount);
434#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800435 memcpy_to_i16_from_q4_27(
436 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700437 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800438 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800439#endif
rago94a1ee82017-07-21 15:11:02 -0700440 }
rago94a1ee82017-07-21 15:11:02 -0700441 }
442#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800443 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
444 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
445
446 if (!auxType && mInChannelCountRequested != inChannelCount) {
447 adjust_channels(
448 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
449 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
450 sizeof(float),
451 sizeof(float)
452 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
453 inBuffer = mInConversionBuffer;
454 }
455 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
456 && mOutChannelCountRequested != outChannelCount) {
457 adjust_selected_channels(
458 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
459 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
460 sizeof(float),
461 sizeof(float)
462 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
463 outBuffer = mOutConversionBuffer;
464 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800465 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
466 if (!auxType) {
467 if (mInConversionBuffer.get() == nullptr) {
468 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
469 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700470 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800471 memcpy_to_i16_from_float(
472 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800473 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800474 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800475 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700476 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800477 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
478 if (mOutConversionBuffer.get() == nullptr) {
479 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
480 goto data_bypass;
481 }
482 memcpy_to_i16_from_float(
483 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800484 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800485 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800486 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700487 }
488 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800489#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800490 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800491#ifdef FLOAT_EFFECT_CHAIN
492 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800493 sp<EffectBufferHalInterface> target =
494 mOutChannelCountRequested != outChannelCount
495 ? mOutConversionBuffer : mOutBuffer;
496
Andy Hungfa69ca32017-11-30 10:07:53 -0800497 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800498 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800499 mOutConversionBuffer->audioBuffer()->s16,
500 outChannelCount * mConfig.outputCfg.buffer.frameCount);
501 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800502 if (mOutChannelCountRequested != outChannelCount) {
503 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
504 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
505 sizeof(float),
506 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
507 }
rago94a1ee82017-07-21 15:11:02 -0700508#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700509 } else {
rago94a1ee82017-07-21 15:11:02 -0700510#ifdef FLOAT_EFFECT_CHAIN
511 data_bypass:
512#endif
513 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800514 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700515 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800516 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700517 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800518 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700519 }
520 }
521 ret = -ENODATA;
522 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800523
Eric Laurentca7cc822012-11-19 14:55:58 -0800524 // force transition to IDLE state when engine is ready
525 if (mState == STOPPED && ret == -ENODATA) {
526 mDisableWaitCnt = 1;
527 }
528
529 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700530 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800531#ifdef FLOAT_AUX
532 const size_t size =
533 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
534#else
rago94a1ee82017-07-21 15:11:02 -0700535 const size_t size =
536 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800537#endif
rago94a1ee82017-07-21 15:11:02 -0700538 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800539 }
540 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700541 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800542 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
543 // If an insert effect is idle and input buffer is different from output buffer,
544 // accumulate input onto output
545 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800546 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700547 // similar handling with data_bypass above.
548 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
549 accumulateInputToOutput();
550 } else { // EFFECT_BUFFER_ACCESS_WRITE
551 copyInputToOutput();
552 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800553 }
554 }
555}
556
557void AudioFlinger::EffectModule::reset_l()
558{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700559 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800560 return;
561 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700562 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800563}
564
565status_t AudioFlinger::EffectModule::configure()
566{
rago94a1ee82017-07-21 15:11:02 -0700567 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700568 status_t status;
569 sp<ThreadBase> thread;
570 uint32_t size;
571 audio_channel_mask_t channelMask;
572
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700573 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700574 status = NO_INIT;
575 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800576 }
577
Eric Laurentd0ebb532013-04-02 16:41:41 -0700578 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700580 status = DEAD_OBJECT;
581 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800582 }
583
584 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800585 // TODO: handle configuration of input (record) SW effects above the HAL,
586 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
587 // in which case input channel masks should be used here.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700588 channelMask = thread->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800589 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700590 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800591
592 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800593 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
594 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
595 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
596 mConfig.inputCfg.channels);
597 }
598#ifndef MULTICHANNEL_EFFECT_CHAIN
599 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
600 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
601 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
602 mConfig.outputCfg.channels);
603 }
604#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800605 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800606#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700607 // TODO: Update this logic when multichannel effects are implemented.
608 // For offloaded tracks consider mono output as stereo for proper effect initialization
609 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
610 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
611 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
612 ALOGV("Overriding effect input and output as STEREO");
613 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800614#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800615 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800616 mInChannelCountRequested =
617 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
618 mOutChannelCountRequested =
619 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700620
rago94a1ee82017-07-21 15:11:02 -0700621 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
622 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900623
624 // Don't use sample rate for thread if effect isn't offloadable.
Daniel Bonnevier46850e02019-12-06 09:14:56 +0100625 if (isOffloadedOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900626 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
627 ALOGV("Overriding effect input as 48kHz");
628 } else {
629 mConfig.inputCfg.samplingRate = thread->sampleRate();
630 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800631 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
632 mConfig.inputCfg.bufferProvider.cookie = NULL;
633 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
634 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
635 mConfig.outputCfg.bufferProvider.cookie = NULL;
636 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
637 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
638 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
639 // Insert effect:
640 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
641 // always overwrites output buffer: input buffer == output buffer
642 // - in other sessions:
643 // last effect in the chain accumulates in output buffer: input buffer != output buffer
644 // other effect: overwrites output buffer: input buffer == output buffer
645 // Auxiliary effect:
646 // accumulates in output buffer: input buffer != output buffer
647 // Therefore: accumulate <=> input buffer != output buffer
648 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
649 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
650 } else {
651 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
652 }
653 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
654 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
655 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
656 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
657
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700658 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800659 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
660
661 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700662 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700663 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800664 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700665 &mConfig,
666 &size,
667 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700668 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800669 status = cmdStatus;
670 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800671
672#ifdef MULTICHANNEL_EFFECT_CHAIN
673 if (status != NO_ERROR &&
Andy Hunge6a61a52018-04-06 18:55:26 -0700674 thread->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800675 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
676 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
677 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700678 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
679 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800680 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
681 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
682 }
683 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
684 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
685 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
686 }
687 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700688 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800689 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700690 &mConfig,
691 &size,
692 &cmdStatus);
693 if (status == NO_ERROR) {
694 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800695 }
696 }
697#endif
698
699#ifdef FLOAT_EFFECT_CHAIN
700 if (status == NO_ERROR) {
701 mSupportsFloat = true;
702 }
703
704 if (status != NO_ERROR) {
705 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
706 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
707 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
708 size = sizeof(int);
709 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
710 sizeof(mConfig),
711 &mConfig,
712 &size,
713 &cmdStatus);
714 if (status == NO_ERROR) {
715 status = cmdStatus;
716 }
717 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700718 mSupportsFloat = false;
719 ALOGVV("config worked with 16 bit");
720 } else {
721 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800722 }
rago94a1ee82017-07-21 15:11:02 -0700723 }
724#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800725
rago94a1ee82017-07-21 15:11:02 -0700726 if (status == NO_ERROR) {
727 // Establish Buffer strategy
728 setInBuffer(mInBuffer);
729 setOutBuffer(mOutBuffer);
730
731 // Update visualizer latency
732 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
733 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
734 effect_param_t *p = (effect_param_t *)buf32;
735
736 p->psize = sizeof(uint32_t);
737 p->vsize = sizeof(uint32_t);
738 size = sizeof(int);
739 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
740
741 uint32_t latency = 0;
742 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
743 if (pbt != NULL) {
744 latency = pbt->latency_l();
745 }
746
747 *((int32_t *)p->data + 1)= latency;
748 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
749 sizeof(effect_param_t) + 8,
750 &buf32,
751 &size,
752 &cmdStatus);
753 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800754 }
755
Andy Hung05083ac2017-12-14 15:00:28 -0800756 // mConfig.outputCfg.buffer.frameCount cannot be zero.
757 mMaxDisableWaitCnt = (uint32_t)std::max(
758 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
759 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
760 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800761
Eric Laurentd0ebb532013-04-02 16:41:41 -0700762exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800763 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700764 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700765 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800766 return status;
767}
768
769status_t AudioFlinger::EffectModule::init()
770{
771 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700772 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800773 return NO_INIT;
774 }
775 status_t cmdStatus;
776 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700777 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
778 0,
779 NULL,
780 &size,
781 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800782 if (status == 0) {
783 status = cmdStatus;
784 }
785 return status;
786}
787
Eric Laurent1b928682014-10-02 19:41:47 -0700788void AudioFlinger::EffectModule::addEffectToHal_l()
789{
790 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
791 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
792 sp<ThreadBase> thread = mThread.promote();
793 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700794 sp<StreamHalInterface> stream = thread->stream();
795 if (stream != 0) {
796 status_t result = stream->addEffect(mEffectInterface);
797 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700798 }
799 }
800 }
801}
802
Eric Laurentfa1e1232016-08-02 19:01:49 -0700803// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800804status_t AudioFlinger::EffectModule::start()
805{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700806 sp<EffectChain> chain;
807 status_t status;
808 {
809 Mutex::Autolock _l(mLock);
810 status = start_l();
811 if (status == NO_ERROR) {
812 chain = mChain.promote();
813 }
814 }
815 if (chain != 0) {
816 chain->resetVolume_l();
817 }
818 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800819}
820
821status_t AudioFlinger::EffectModule::start_l()
822{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700823 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800824 return NO_INIT;
825 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700826 if (mStatus != NO_ERROR) {
827 return mStatus;
828 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800829 status_t cmdStatus;
830 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700831 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
832 0,
833 NULL,
834 &size,
835 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800836 if (status == 0) {
837 status = cmdStatus;
838 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700839 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700840 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800841 }
842 return status;
843}
844
845status_t AudioFlinger::EffectModule::stop()
846{
847 Mutex::Autolock _l(mLock);
848 return stop_l();
849}
850
851status_t AudioFlinger::EffectModule::stop_l()
852{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700853 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800854 return NO_INIT;
855 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700856 if (mStatus != NO_ERROR) {
857 return mStatus;
858 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800859 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800860 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900861
862 if (isVolumeControl() && isOffloadedOrDirect()) {
863 sp<EffectChain>chain = mChain.promote();
864 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
865 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
866 mSetVolumeReentrantTid = gettid();
867 chain->resetVolume_l();
868 mSetVolumeReentrantTid = INVALID_PID;
869 }
870
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700871 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
872 0,
873 NULL,
874 &size,
875 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800876 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800877 status = cmdStatus;
878 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800879 if (status == NO_ERROR) {
880 status = remove_effect_from_hal_l();
881 }
882 return status;
883}
884
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800885// must be called with EffectChain::mLock held
886void AudioFlinger::EffectModule::release_l()
887{
888 if (mEffectInterface != 0) {
889 remove_effect_from_hal_l();
890 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800891 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800892 mEffectInterface.clear();
893 }
894}
895
Eric Laurentbfb1b832013-01-07 09:53:42 -0800896status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
897{
898 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
899 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800900 sp<ThreadBase> thread = mThread.promote();
901 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700902 sp<StreamHalInterface> stream = thread->stream();
903 if (stream != 0) {
904 status_t result = stream->removeEffect(mEffectInterface);
905 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800906 }
907 }
908 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800909 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800910}
911
Andy Hunge4a1d912016-08-17 14:11:13 -0700912// round up delta valid if value and divisor are positive.
913template <typename T>
914static T roundUpDelta(const T &value, const T &divisor) {
915 T remainder = value % divisor;
916 return remainder == 0 ? 0 : divisor - remainder;
917}
918
Eric Laurentca7cc822012-11-19 14:55:58 -0800919status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
920 uint32_t cmdSize,
921 void *pCmdData,
922 uint32_t *replySize,
923 void *pReplyData)
924{
925 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700926 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800927
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700928 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800929 return NO_INIT;
930 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700931 if (mStatus != NO_ERROR) {
932 return mStatus;
933 }
Andy Hung110bc952016-06-20 15:22:52 -0700934 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700935 (sizeof(effect_param_t) > cmdSize ||
936 ((effect_param_t *)pCmdData)->psize > cmdSize
937 - sizeof(effect_param_t))) {
938 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800939 android_errorWriteLog(0x534e4554, "33003822");
940 return -EINVAL;
941 }
942 if (cmdCode == EFFECT_CMD_GET_PARAM &&
943 (*replySize < sizeof(effect_param_t) ||
944 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
945 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700946 return -EINVAL;
947 }
ragoe2759072016-11-22 18:02:48 -0800948 if (cmdCode == EFFECT_CMD_GET_PARAM &&
949 (sizeof(effect_param_t) > *replySize
950 || ((effect_param_t *)pCmdData)->psize > *replySize
951 - sizeof(effect_param_t)
952 || ((effect_param_t *)pCmdData)->vsize > *replySize
953 - sizeof(effect_param_t)
954 - ((effect_param_t *)pCmdData)->psize
955 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
956 *replySize
957 - sizeof(effect_param_t)
958 - ((effect_param_t *)pCmdData)->psize
959 - ((effect_param_t *)pCmdData)->vsize)) {
960 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
961 android_errorWriteLog(0x534e4554, "32705438");
962 return -EINVAL;
963 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700964 if ((cmdCode == EFFECT_CMD_SET_PARAM
965 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
966 (sizeof(effect_param_t) > cmdSize
967 || ((effect_param_t *)pCmdData)->psize > cmdSize
968 - sizeof(effect_param_t)
969 || ((effect_param_t *)pCmdData)->vsize > cmdSize
970 - sizeof(effect_param_t)
971 - ((effect_param_t *)pCmdData)->psize
972 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
973 cmdSize
974 - sizeof(effect_param_t)
975 - ((effect_param_t *)pCmdData)->psize
976 - ((effect_param_t *)pCmdData)->vsize)) {
977 android_errorWriteLog(0x534e4554, "30204301");
978 return -EINVAL;
979 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700980 status_t status = mEffectInterface->command(cmdCode,
981 cmdSize,
982 pCmdData,
983 replySize,
984 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800985 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
986 uint32_t size = (replySize == NULL) ? 0 : *replySize;
987 for (size_t i = 1; i < mHandles.size(); i++) {
988 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800989 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800990 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
991 }
992 }
993 }
994 return status;
995}
996
997status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
998{
999 Mutex::Autolock _l(mLock);
1000 return setEnabled_l(enabled);
1001}
1002
1003// must be called with EffectModule::mLock held
1004status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
1005{
1006
1007 ALOGV("setEnabled %p enabled %d", this, enabled);
1008
1009 if (enabled != isEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001010 switch (mState) {
1011 // going from disabled to enabled
1012 case IDLE:
1013 mState = STARTING;
1014 break;
1015 case STOPPED:
1016 mState = RESTART;
1017 break;
1018 case STOPPING:
1019 mState = ACTIVE;
1020 break;
1021
1022 // going from enabled to disabled
1023 case RESTART:
1024 mState = STOPPED;
1025 break;
1026 case STARTING:
1027 mState = IDLE;
1028 break;
1029 case ACTIVE:
1030 mState = STOPPING;
1031 break;
1032 case DESTROYED:
1033 return NO_ERROR; // simply ignore as we are being destroyed
1034 }
1035 for (size_t i = 1; i < mHandles.size(); i++) {
1036 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001037 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001038 h->setEnabled(enabled);
1039 }
1040 }
1041 }
1042 return NO_ERROR;
1043}
1044
1045bool AudioFlinger::EffectModule::isEnabled() const
1046{
1047 switch (mState) {
1048 case RESTART:
1049 case STARTING:
1050 case ACTIVE:
1051 return true;
1052 case IDLE:
1053 case STOPPING:
1054 case STOPPED:
1055 case DESTROYED:
1056 default:
1057 return false;
1058 }
1059}
1060
1061bool AudioFlinger::EffectModule::isProcessEnabled() const
1062{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001063 if (mStatus != NO_ERROR) {
1064 return false;
1065 }
1066
Eric Laurentca7cc822012-11-19 14:55:58 -08001067 switch (mState) {
1068 case RESTART:
1069 case ACTIVE:
1070 case STOPPING:
1071 case STOPPED:
1072 return true;
1073 case IDLE:
1074 case STARTING:
1075 case DESTROYED:
1076 default:
1077 return false;
1078 }
1079}
1080
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001081bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1082{
1083 return (mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT);
1084}
1085
1086bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1087{
1088 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1089}
1090
Mikhail Naganov022b9952017-01-04 16:36:51 -08001091void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001092 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001093
1094 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001095 if (buffer != 0) {
1096 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1097 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1098 } else {
1099 mConfig.inputCfg.buffer.raw = NULL;
1100 }
1101 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001102 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001103
1104#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001105 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001106 // Theoretically insert effects can also do in-place conversions (destroying
1107 // the original buffer) when the output buffer is identical to the input buffer,
1108 // but we don't optimize for it here.
1109 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001110 const uint32_t inChannelCount =
1111 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1112 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1113 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001114 // we need to translate - create hidl shared buffer and intercept
1115 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001116 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1117 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1118 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001119
1120 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1121 __func__, inChannels, inFrameCount, size);
1122
Andy Hungbded9c82017-11-30 18:47:35 -08001123 if (size > 0 && (mInConversionBuffer.get() == nullptr
1124 || size > mInConversionBuffer->getSize())) {
1125 mInConversionBuffer.clear();
1126 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001127 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1128 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1129 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001130 }
Andy Hungbded9c82017-11-30 18:47:35 -08001131 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001132 mInConversionBuffer->setFrameCount(inFrameCount);
1133 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001134 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001135 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001136 }
1137 }
1138#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001139}
1140
1141void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001142 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001143
1144 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001145 if (buffer != 0) {
1146 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1147 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1148 } else {
1149 mConfig.outputCfg.buffer.raw = NULL;
1150 }
1151 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001152 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001153
1154#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001155 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001156 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001157 const uint32_t outChannelCount =
1158 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1159 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1160 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001161 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001162 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1163 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1164 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001165
1166 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1167 __func__, outChannels, outFrameCount, size);
1168
Andy Hungbded9c82017-11-30 18:47:35 -08001169 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1170 || size > mOutConversionBuffer->getSize())) {
1171 mOutConversionBuffer.clear();
1172 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001173 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1174 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1175 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001176 }
Andy Hungbded9c82017-11-30 18:47:35 -08001177 if (mOutConversionBuffer.get() != nullptr) {
1178 mOutConversionBuffer->setFrameCount(outFrameCount);
1179 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001180 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001181 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001182 }
1183 }
1184#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001185}
1186
Eric Laurentca7cc822012-11-19 14:55:58 -08001187status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1188{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001189 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001190 if (mStatus != NO_ERROR) {
1191 return mStatus;
1192 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001193 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001194 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1195 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1196 if (isProcessEnabled() &&
1197 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001198 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1199 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001200 uint32_t volume[2];
1201 uint32_t *pVolume = NULL;
1202 uint32_t size = sizeof(volume);
1203 volume[0] = *left;
1204 volume[1] = *right;
1205 if (controller) {
1206 pVolume = volume;
1207 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001208 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1209 size,
1210 volume,
1211 &size,
1212 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001213 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1214 *left = volume[0];
1215 *right = volume[1];
1216 }
1217 }
1218 return status;
1219}
1220
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001221void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1222{
1223 sp<ThreadBase> thread = mThread.promote();
1224 if (thread != 0 &&
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09001225 (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::DIRECT) &&
1226 !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001227 PlaybackThread *t = (PlaybackThread *)thread.get();
1228 float vol_l = (float)left / (1 << 24);
1229 float vol_r = (float)right / (1 << 24);
1230 t->setVolumeForOutput_l(vol_l, vol_r);
1231 }
1232}
1233
jiabinb8269fd2019-11-11 12:16:27 -08001234status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1235 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001236{
jiabinb8269fd2019-11-11 12:16:27 -08001237 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1238 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001239 return NO_ERROR;
1240 }
1241
1242 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001243 if (mStatus != NO_ERROR) {
1244 return mStatus;
1245 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001246 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001247 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001248 status_t cmdStatus;
1249 uint32_t size = sizeof(status_t);
jiabinb8269fd2019-11-11 12:16:27 -08001250 // FIXME: use audio device types and addresses when the hal interface is ready.
1251 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001252 sizeof(uint32_t),
jiabinb8269fd2019-11-11 12:16:27 -08001253 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001254 &size,
1255 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001256 }
1257 return status;
1258}
1259
jiabinb8269fd2019-11-11 12:16:27 -08001260status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1261{
1262 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1263}
1264
1265status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1266{
1267 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1268}
1269
Eric Laurentca7cc822012-11-19 14:55:58 -08001270status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1271{
1272 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001273 if (mStatus != NO_ERROR) {
1274 return mStatus;
1275 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001276 status_t status = NO_ERROR;
1277 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1278 status_t cmdStatus;
1279 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001280 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1281 sizeof(audio_mode_t),
1282 &mode,
1283 &size,
1284 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001285 if (status == NO_ERROR) {
1286 status = cmdStatus;
1287 }
1288 }
1289 return status;
1290}
1291
1292status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1293{
1294 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001295 if (mStatus != NO_ERROR) {
1296 return mStatus;
1297 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001298 status_t status = NO_ERROR;
1299 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1300 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001301 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1302 sizeof(audio_source_t),
1303 &source,
1304 &size,
1305 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001306 }
1307 return status;
1308}
1309
1310void AudioFlinger::EffectModule::setSuspended(bool suspended)
1311{
1312 Mutex::Autolock _l(mLock);
1313 mSuspended = suspended;
1314}
1315
1316bool AudioFlinger::EffectModule::suspended() const
1317{
1318 Mutex::Autolock _l(mLock);
1319 return mSuspended;
1320}
1321
1322bool AudioFlinger::EffectModule::purgeHandles()
1323{
1324 bool enabled = false;
1325 Mutex::Autolock _l(mLock);
Eric Laurent6c796322019-04-09 14:13:17 -07001326 EffectHandle *handle = controlHandle_l();
1327 if (handle != NULL) {
1328 enabled = handle->enabled();
Eric Laurentca7cc822012-11-19 14:55:58 -08001329 }
Eric Laurent6c796322019-04-09 14:13:17 -07001330 mHandles.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001331 return enabled;
1332}
1333
Eric Laurent5baf2af2013-09-12 17:37:00 -07001334status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1335{
1336 Mutex::Autolock _l(mLock);
1337 if (mStatus != NO_ERROR) {
1338 return mStatus;
1339 }
1340 status_t status = NO_ERROR;
1341 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1342 status_t cmdStatus;
1343 uint32_t size = sizeof(status_t);
1344 effect_offload_param_t cmd;
1345
1346 cmd.isOffload = offloaded;
1347 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001348 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1349 sizeof(effect_offload_param_t),
1350 &cmd,
1351 &size,
1352 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001353 if (status == NO_ERROR) {
1354 status = cmdStatus;
1355 }
1356 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1357 } else {
1358 if (offloaded) {
1359 status = INVALID_OPERATION;
1360 }
1361 mOffloaded = false;
1362 }
1363 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1364 return status;
1365}
1366
1367bool AudioFlinger::EffectModule::isOffloaded() const
1368{
1369 Mutex::Autolock _l(mLock);
1370 return mOffloaded;
1371}
1372
Marco Nelissenb2208842014-02-07 14:00:50 -08001373String8 effectFlagsToString(uint32_t flags) {
1374 String8 s;
1375
1376 s.append("conn. mode: ");
1377 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1378 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1379 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1380 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1381 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1382 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1383 default: s.append("unknown/reserved"); break;
1384 }
1385 s.append(", ");
1386
1387 s.append("insert pref: ");
1388 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1389 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1390 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1391 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1392 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1393 default: s.append("unknown/reserved"); break;
1394 }
1395 s.append(", ");
1396
1397 s.append("volume mgmt: ");
1398 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1399 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1400 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1401 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001402 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
Marco Nelissenb2208842014-02-07 14:00:50 -08001403 default: s.append("unknown/reserved"); break;
1404 }
1405 s.append(", ");
1406
1407 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1408 if (devind) {
1409 s.append("device indication: ");
1410 switch (devind) {
1411 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1412 default: s.append("unknown/reserved"); break;
1413 }
1414 s.append(", ");
1415 }
1416
1417 s.append("input mode: ");
1418 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1419 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1420 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1421 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1422 default: s.append("not set"); break;
1423 }
1424 s.append(", ");
1425
1426 s.append("output mode: ");
1427 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1428 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1429 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1430 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1431 default: s.append("not set"); break;
1432 }
1433 s.append(", ");
1434
1435 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1436 if (accel) {
1437 s.append("hardware acceleration: ");
1438 switch (accel) {
1439 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1440 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1441 default: s.append("unknown/reserved"); break;
1442 }
1443 s.append(", ");
1444 }
1445
1446 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1447 if (modeind) {
1448 s.append("mode indication: ");
1449 switch (modeind) {
1450 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1451 default: s.append("unknown/reserved"); break;
1452 }
1453 s.append(", ");
1454 }
1455
1456 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1457 if (srcind) {
1458 s.append("source indication: ");
1459 switch (srcind) {
1460 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1461 default: s.append("unknown/reserved"); break;
1462 }
1463 s.append(", ");
1464 }
1465
1466 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1467 s.append("offloadable, ");
1468 }
1469
1470 int len = s.length();
1471 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001472 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001473 s.unlockBuffer(len - 2);
1474 }
1475 return s;
1476}
1477
Andy Hungbded9c82017-11-30 18:47:35 -08001478static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1479 std::stringstream ss;
1480
1481 if (buffer.get() == nullptr) {
1482 return "nullptr"; // make different than below
1483 } else if (buffer->externalData() != nullptr) {
1484 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1485 << " -> "
1486 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1487 } else {
1488 ss << buffer->audioBuffer()->raw;
1489 }
1490 return ss.str();
1491}
Marco Nelissenb2208842014-02-07 14:00:50 -08001492
Glenn Kasten0f11b512014-01-31 16:18:54 -08001493void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001494{
Eric Laurentca7cc822012-11-19 14:55:58 -08001495 String8 result;
1496
Andy Hung9718d662017-12-22 17:57:39 -08001497 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001498
1499 bool locked = AudioFlinger::dumpTryLock(mLock);
1500 // failed to lock - AudioFlinger is probably deadlocked
1501 if (!locked) {
1502 result.append("\t\tCould not lock Fx mutex:\n");
1503 }
1504
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001505 result.append("\t\tSession Status State Registered Enabled Suspended Engine:\n");
1506 result.appendFormat("\t\t%05d %03d %03d %s %s %s %p\n",
1507 mSessionId, mStatus, mState, mPolicyRegistered ? "y" : "n", mPolicyEnabled ? "y" : "n",
1508 mSuspended ? "y" : "n", mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001509
1510 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001511 char uuidStr[64];
1512 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001513 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001514 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001515 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1516 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001518 mDescriptor.flags,
1519 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001520 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001521 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001522
1523 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001524 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001525
1526 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001527
1528 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001529 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1530 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1531 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001532 mConfig.inputCfg.buffer.frameCount,
1533 mConfig.inputCfg.samplingRate,
1534 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001535 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001536 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001537
1538 result.append("\t\t- Output configuration:\n");
1539 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001540 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001541 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001542 mConfig.outputCfg.buffer.frameCount,
1543 mConfig.outputCfg.samplingRate,
1544 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001545 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001546 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001547
rago94a1ee82017-07-21 15:11:02 -07001548#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001549
Andy Hungbded9c82017-11-30 18:47:35 -08001550 result.appendFormat("\t\t- HAL buffers:\n"
1551 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1552 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1553 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1554 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1555 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001556#endif
1557
Andy Hung9718d662017-12-22 17:57:39 -08001558 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001559 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001560 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001561 for (size_t i = 0; i < mHandles.size(); ++i) {
1562 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001563 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001564 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001565 result.append(buffer);
1566 }
1567 }
1568
Eric Laurentca7cc822012-11-19 14:55:58 -08001569 write(fd, result.string(), result.length());
1570
Mikhail Naganov4d547672019-02-22 14:19:19 -08001571 if (mEffectInterface != 0) {
1572 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1573 (void)mEffectInterface->dump(fd);
1574 }
1575
Eric Laurentca7cc822012-11-19 14:55:58 -08001576 if (locked) {
1577 mLock.unlock();
1578 }
1579}
1580
1581// ----------------------------------------------------------------------------
1582// EffectHandle implementation
1583// ----------------------------------------------------------------------------
1584
1585#undef LOG_TAG
1586#define LOG_TAG "AudioFlinger::EffectHandle"
1587
1588AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1589 const sp<AudioFlinger::Client>& client,
1590 const sp<IEffectClient>& effectClient,
1591 int32_t priority)
1592 : BnEffect(),
1593 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001594 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001595{
1596 ALOGV("constructor %p", this);
1597
1598 if (client == 0) {
1599 return;
1600 }
1601 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1602 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001603 if (mCblkMemory == 0 ||
1604 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001605 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001606 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001607 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001608 return;
1609 }
Glenn Kastene75da402013-11-20 13:54:52 -08001610 new(mCblk) effect_param_cblk_t();
1611 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001612}
1613
1614AudioFlinger::EffectHandle::~EffectHandle()
1615{
1616 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001617 disconnect(false);
1618}
1619
Glenn Kastene75da402013-11-20 13:54:52 -08001620status_t AudioFlinger::EffectHandle::initCheck()
1621{
1622 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1623}
1624
Eric Laurentca7cc822012-11-19 14:55:58 -08001625status_t AudioFlinger::EffectHandle::enable()
1626{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001627 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001628 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001629 sp<EffectModule> effect = mEffect.promote();
1630 if (effect == 0 || mDisconnected) {
1631 return DEAD_OBJECT;
1632 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001633 if (!mHasControl) {
1634 return INVALID_OPERATION;
1635 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001636
1637 if (mEnabled) {
1638 return NO_ERROR;
1639 }
1640
1641 mEnabled = true;
1642
Eric Laurent6c796322019-04-09 14:13:17 -07001643 status_t status = effect->updatePolicyState();
1644 if (status != NO_ERROR) {
1645 mEnabled = false;
1646 return status;
1647 }
1648
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001649 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001650 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001651 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001652 }
1653
1654 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001655 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001656 return NO_ERROR;
1657 }
1658
Eric Laurent6c796322019-04-09 14:13:17 -07001659 status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001660 if (status != NO_ERROR) {
1661 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001662 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 }
1664 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001665 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001666 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001667 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1668 Mutex::Autolock _l(thread->mLock);
1669 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001670 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001671 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001672 if (thread->type() == ThreadBase::OFFLOAD) {
1673 PlaybackThread *t = (PlaybackThread *)thread.get();
1674 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1675 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001676 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001677 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1678 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001679 }
1680 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001681 }
1682 return status;
1683}
1684
1685status_t AudioFlinger::EffectHandle::disable()
1686{
1687 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001688 AutoMutex _l(mLock);
1689 sp<EffectModule> effect = mEffect.promote();
1690 if (effect == 0 || mDisconnected) {
1691 return DEAD_OBJECT;
1692 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001693 if (!mHasControl) {
1694 return INVALID_OPERATION;
1695 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001696
1697 if (!mEnabled) {
1698 return NO_ERROR;
1699 }
1700 mEnabled = false;
1701
Eric Laurent6c796322019-04-09 14:13:17 -07001702 effect->updatePolicyState();
1703
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001704 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001705 return NO_ERROR;
1706 }
1707
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001708 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001709
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001710 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001712 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001713 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1714 Mutex::Autolock _l(thread->mLock);
1715 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001716 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001717 }
1718
1719 return status;
1720}
1721
1722void AudioFlinger::EffectHandle::disconnect()
1723{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001724 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001725 disconnect(true);
1726}
1727
1728void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1729{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001730 AutoMutex _l(mLock);
1731 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1732 if (mDisconnected) {
1733 if (unpinIfLast) {
1734 android_errorWriteLog(0x534e4554, "32707507");
1735 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001736 return;
1737 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001738 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001739 {
1740 sp<EffectModule> effect = mEffect.promote();
1741 if (effect != 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001742 sp<ThreadBase> thread = effect->thread().promote();
1743 if (thread != 0) {
1744 thread->disconnectEffectHandle(this, unpinIfLast);
1745 } else if (effect->disconnectHandle(this, unpinIfLast) > 0) {
1746 ALOGW("%s Effect handle %p disconnected after thread destruction",
1747 __func__, this);
1748 }
1749 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001750 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001751 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001752
Eric Laurentca7cc822012-11-19 14:55:58 -08001753 if (mClient != 0) {
1754 if (mCblk != NULL) {
1755 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1756 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1757 }
1758 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001759 // Client destructor must run with AudioFlinger client mutex locked
1760 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001761 mClient.clear();
1762 }
1763}
1764
1765status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1766 uint32_t cmdSize,
1767 void *pCmdData,
1768 uint32_t *replySize,
1769 void *pReplyData)
1770{
1771 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001772 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001773
Eric Laurentc7ab3092017-06-15 18:43:46 -07001774 // reject commands reserved for internal use by audio framework if coming from outside
1775 // of audioserver
1776 switch(cmdCode) {
1777 case EFFECT_CMD_ENABLE:
1778 case EFFECT_CMD_DISABLE:
1779 case EFFECT_CMD_SET_PARAM:
1780 case EFFECT_CMD_SET_PARAM_DEFERRED:
1781 case EFFECT_CMD_SET_PARAM_COMMIT:
1782 case EFFECT_CMD_GET_PARAM:
1783 break;
1784 default:
1785 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1786 break;
1787 }
1788 android_errorWriteLog(0x534e4554, "62019992");
1789 return BAD_VALUE;
1790 }
1791
Eric Laurent1ffc5852016-12-15 14:46:09 -08001792 if (cmdCode == EFFECT_CMD_ENABLE) {
1793 if (*replySize < sizeof(int)) {
1794 android_errorWriteLog(0x534e4554, "32095713");
1795 return BAD_VALUE;
1796 }
1797 *(int *)pReplyData = NO_ERROR;
1798 *replySize = sizeof(int);
1799 return enable();
1800 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1801 if (*replySize < sizeof(int)) {
1802 android_errorWriteLog(0x534e4554, "32095713");
1803 return BAD_VALUE;
1804 }
1805 *(int *)pReplyData = NO_ERROR;
1806 *replySize = sizeof(int);
1807 return disable();
1808 }
1809
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001810 AutoMutex _l(mLock);
1811 sp<EffectModule> effect = mEffect.promote();
1812 if (effect == 0 || mDisconnected) {
1813 return DEAD_OBJECT;
1814 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001815 // only get parameter command is permitted for applications not controlling the effect
1816 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1817 return INVALID_OPERATION;
1818 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001819 if (mClient == 0) {
1820 return INVALID_OPERATION;
1821 }
1822
1823 // handle commands that are not forwarded transparently to effect engine
1824 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001825 if (*replySize < sizeof(int)) {
1826 android_errorWriteLog(0x534e4554, "32095713");
1827 return BAD_VALUE;
1828 }
1829 *(int *)pReplyData = NO_ERROR;
1830 *replySize = sizeof(int);
1831
Eric Laurentca7cc822012-11-19 14:55:58 -08001832 // No need to trylock() here as this function is executed in the binder thread serving a
1833 // particular client process: no risk to block the whole media server process or mixer
1834 // threads if we are stuck here
1835 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001836 // keep local copy of index in case of client corruption b/32220769
1837 const uint32_t clientIndex = mCblk->clientIndex;
1838 const uint32_t serverIndex = mCblk->serverIndex;
1839 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1840 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001841 mCblk->serverIndex = 0;
1842 mCblk->clientIndex = 0;
1843 return BAD_VALUE;
1844 }
1845 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001846 effect_param_t *param = NULL;
1847 for (uint32_t index = serverIndex; index < clientIndex;) {
1848 int *p = (int *)(mBuffer + index);
1849 const int size = *p++;
1850 if (size < 0
1851 || size > EFFECT_PARAM_BUFFER_SIZE
1852 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001853 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001854 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001855 break;
1856 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001857
1858 // copy to local memory in case of client corruption b/32220769
George Burgess IV80a22162020-01-05 20:06:15 -08001859 auto *newParam = (effect_param_t *)realloc(param, size);
1860 if (newParam == NULL) {
Andy Hunga447a0f2016-11-15 17:19:58 -08001861 ALOGW("command(): out of memory");
1862 status = NO_MEMORY;
1863 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 }
George Burgess IV80a22162020-01-05 20:06:15 -08001865 param = newParam;
Andy Hunga447a0f2016-11-15 17:19:58 -08001866 memcpy(param, p, size);
1867
1868 int reply = 0;
1869 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001870 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001871 size,
1872 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001873 &rsize,
1874 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001875
1876 // verify shared memory: server index shouldn't change; client index can't go back.
1877 if (serverIndex != mCblk->serverIndex
1878 || clientIndex > mCblk->clientIndex) {
1879 android_errorWriteLog(0x534e4554, "32220769");
1880 status = BAD_VALUE;
1881 break;
1882 }
1883
Eric Laurentca7cc822012-11-19 14:55:58 -08001884 // stop at first error encountered
1885 if (ret != NO_ERROR) {
1886 status = ret;
1887 *(int *)pReplyData = reply;
1888 break;
1889 } else if (reply != NO_ERROR) {
1890 *(int *)pReplyData = reply;
1891 break;
1892 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001893 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001894 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001895 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001896 mCblk->serverIndex = 0;
1897 mCblk->clientIndex = 0;
1898 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001899 }
1900
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001901 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001902}
1903
1904void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1905{
1906 ALOGV("setControl %p control %d", this, hasControl);
1907
1908 mHasControl = hasControl;
1909 mEnabled = enabled;
1910
1911 if (signal && mEffectClient != 0) {
1912 mEffectClient->controlStatusChanged(hasControl);
1913 }
1914}
1915
1916void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1917 uint32_t cmdSize,
1918 void *pCmdData,
1919 uint32_t replySize,
1920 void *pReplyData)
1921{
1922 if (mEffectClient != 0) {
1923 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1924 }
1925}
1926
1927
1928
1929void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1930{
1931 if (mEffectClient != 0) {
1932 mEffectClient->enableStatusChanged(enabled);
1933 }
1934}
1935
1936status_t AudioFlinger::EffectHandle::onTransact(
1937 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1938{
1939 return BnEffect::onTransact(code, data, reply, flags);
1940}
1941
1942
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001943void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001944{
1945 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1946
Marco Nelissenb2208842014-02-07 14:00:50 -08001947 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001948 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001949 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001950 mHasControl ? "yes" : "no",
1951 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001952 mCblk ? mCblk->clientIndex : 0,
1953 mCblk ? mCblk->serverIndex : 0
1954 );
1955
1956 if (locked) {
1957 mCblk->lock.unlock();
1958 }
1959}
1960
1961#undef LOG_TAG
1962#define LOG_TAG "AudioFlinger::EffectChain"
1963
1964AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001965 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001966 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001967 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001968 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001969{
1970 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1971 if (thread == NULL) {
1972 return;
1973 }
1974 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1975 thread->frameCount();
1976}
1977
1978AudioFlinger::EffectChain::~EffectChain()
1979{
Eric Laurentca7cc822012-11-19 14:55:58 -08001980}
1981
1982// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1983sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1984 effect_descriptor_t *descriptor)
1985{
1986 size_t size = mEffects.size();
1987
1988 for (size_t i = 0; i < size; i++) {
1989 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1990 return mEffects[i];
1991 }
1992 }
1993 return 0;
1994}
1995
1996// getEffectFromId_l() must be called with ThreadBase::mLock held
1997sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1998{
1999 size_t size = mEffects.size();
2000
2001 for (size_t i = 0; i < size; i++) {
2002 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2003 if (id == 0 || mEffects[i]->id() == id) {
2004 return mEffects[i];
2005 }
2006 }
2007 return 0;
2008}
2009
2010// getEffectFromType_l() must be called with ThreadBase::mLock held
2011sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2012 const effect_uuid_t *type)
2013{
2014 size_t size = mEffects.size();
2015
2016 for (size_t i = 0; i < size; i++) {
2017 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2018 return mEffects[i];
2019 }
2020 }
2021 return 0;
2022}
2023
Eric Laurent6c796322019-04-09 14:13:17 -07002024std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2025{
2026 std::vector<int> ids;
2027 Mutex::Autolock _l(mLock);
2028 for (size_t i = 0; i < mEffects.size(); i++) {
2029 ids.push_back(mEffects[i]->id());
2030 }
2031 return ids;
2032}
2033
Eric Laurentca7cc822012-11-19 14:55:58 -08002034void AudioFlinger::EffectChain::clearInputBuffer()
2035{
2036 Mutex::Autolock _l(mLock);
2037 sp<ThreadBase> thread = mThread.promote();
2038 if (thread == 0) {
2039 ALOGW("clearInputBuffer(): cannot promote mixer thread");
2040 return;
2041 }
2042 clearInputBuffer_l(thread);
2043}
2044
2045// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07002046void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08002047{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002048 if (mInBuffer == NULL) {
2049 return;
2050 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002051 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08002052 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002053
Mikhail Naganov022b9952017-01-04 16:36:51 -08002054 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
2055 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002056}
2057
2058// Must be called with EffectChain::mLock locked
2059void AudioFlinger::EffectChain::process_l()
2060{
2061 sp<ThreadBase> thread = mThread.promote();
2062 if (thread == 0) {
2063 ALOGW("process_l(): cannot promote mixer thread");
2064 return;
2065 }
2066 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
2067 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002068 // never process effects when:
2069 // - on an OFFLOAD thread
2070 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08002071 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
2072 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08002073 if (!isGlobalSession) {
2074 bool tracksOnSession = (trackCnt() != 0);
2075
2076 if (!tracksOnSession && mTailBufferCount == 0) {
2077 doProcess = false;
2078 }
2079
2080 if (activeTrackCnt() == 0) {
2081 // if no track is active and the effect tail has not been rendered,
2082 // the input buffer must be cleared here as the mixer process will not do it
2083 if (tracksOnSession || mTailBufferCount > 0) {
2084 clearInputBuffer_l(thread);
2085 if (mTailBufferCount > 0) {
2086 mTailBufferCount--;
2087 }
2088 }
2089 }
2090 }
2091
2092 size_t size = mEffects.size();
2093 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002094 // Only the input and output buffers of the chain can be external,
2095 // and 'update' / 'commit' do nothing for allocated buffers, thus
2096 // it's not needed to consider any other buffers here.
2097 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002098 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2099 mOutBuffer->update();
2100 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002101 for (size_t i = 0; i < size; i++) {
2102 mEffects[i]->process();
2103 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002104 mInBuffer->commit();
2105 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2106 mOutBuffer->commit();
2107 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002108 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002109 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002110 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002111 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2112 }
2113 if (doResetVolume) {
2114 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002115 }
2116}
2117
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002118// createEffect_l() must be called with ThreadBase::mLock held
2119status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
2120 ThreadBase *thread,
2121 effect_descriptor_t *desc,
2122 int id,
2123 audio_session_t sessionId,
2124 bool pinned)
2125{
2126 Mutex::Autolock _l(mLock);
2127 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
2128 status_t lStatus = effect->status();
2129 if (lStatus == NO_ERROR) {
2130 lStatus = addEffect_ll(effect);
2131 }
2132 if (lStatus != NO_ERROR) {
2133 effect.clear();
2134 }
2135 return lStatus;
2136}
2137
2138// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002139status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2140{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002141 Mutex::Autolock _l(mLock);
2142 return addEffect_ll(effect);
2143}
2144// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2145status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2146{
Eric Laurentca7cc822012-11-19 14:55:58 -08002147 effect_descriptor_t desc = effect->desc();
2148 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2149
Eric Laurentca7cc822012-11-19 14:55:58 -08002150 effect->setChain(this);
2151 sp<ThreadBase> thread = mThread.promote();
2152 if (thread == 0) {
2153 return NO_INIT;
2154 }
2155 effect->setThread(thread);
2156
2157 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2158 // Auxiliary effects are inserted at the beginning of mEffects vector as
2159 // they are processed first and accumulated in chain input buffer
2160 mEffects.insertAt(effect, 0);
2161
2162 // the input buffer for auxiliary effect contains mono samples in
2163 // 32 bit format. This is to avoid saturation in AudoMixer
2164 // accumulation stage. Saturation is done in EffectModule::process() before
2165 // calling the process in effect engine
2166 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002167 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002168#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002169 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002170 numSamples * sizeof(float), &halBuffer);
2171#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002172 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002173 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002174#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002175 if (result != OK) return result;
2176 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002177 // auxiliary effects output samples to chain input buffer for further processing
2178 // by insert effects
2179 effect->setOutBuffer(mInBuffer);
2180 } else {
2181 // Insert effects are inserted at the end of mEffects vector as they are processed
2182 // after track and auxiliary effects.
2183 // Insert effect order as a function of indicated preference:
2184 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2185 // another effect is present
2186 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2187 // last effect claiming first position
2188 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2189 // first effect claiming last position
2190 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2191 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2192 // already present
2193
2194 size_t size = mEffects.size();
2195 size_t idx_insert = size;
2196 ssize_t idx_insert_first = -1;
2197 ssize_t idx_insert_last = -1;
2198
2199 for (size_t i = 0; i < size; i++) {
2200 effect_descriptor_t d = mEffects[i]->desc();
2201 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2202 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2203 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2204 // check invalid effect chaining combinations
2205 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2206 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2207 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2208 desc.name, d.name);
2209 return INVALID_OPERATION;
2210 }
2211 // remember position of first insert effect and by default
2212 // select this as insert position for new effect
2213 if (idx_insert == size) {
2214 idx_insert = i;
2215 }
2216 // remember position of last insert effect claiming
2217 // first position
2218 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2219 idx_insert_first = i;
2220 }
2221 // remember position of first insert effect claiming
2222 // last position
2223 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2224 idx_insert_last == -1) {
2225 idx_insert_last = i;
2226 }
2227 }
2228 }
2229
2230 // modify idx_insert from first position if needed
2231 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2232 if (idx_insert_last != -1) {
2233 idx_insert = idx_insert_last;
2234 } else {
2235 idx_insert = size;
2236 }
2237 } else {
2238 if (idx_insert_first != -1) {
2239 idx_insert = idx_insert_first + 1;
2240 }
2241 }
2242
2243 // always read samples from chain input buffer
2244 effect->setInBuffer(mInBuffer);
2245
2246 // if last effect in the chain, output samples to chain
2247 // output buffer, otherwise to chain input buffer
2248 if (idx_insert == size) {
2249 if (idx_insert != 0) {
2250 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2251 mEffects[idx_insert-1]->configure();
2252 }
2253 effect->setOutBuffer(mOutBuffer);
2254 } else {
2255 effect->setOutBuffer(mInBuffer);
2256 }
2257 mEffects.insertAt(effect, idx_insert);
2258
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002259 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002260 idx_insert);
2261 }
2262 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002263
Eric Laurentca7cc822012-11-19 14:55:58 -08002264 return NO_ERROR;
2265}
2266
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002267// removeEffect_l() must be called with ThreadBase::mLock held
2268size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2269 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002270{
2271 Mutex::Autolock _l(mLock);
2272 size_t size = mEffects.size();
2273 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2274
2275 for (size_t i = 0; i < size; i++) {
2276 if (effect == mEffects[i]) {
2277 // calling stop here will remove pre-processing effect from the audio HAL.
2278 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2279 // the middle of a read from audio HAL
2280 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2281 mEffects[i]->state() == EffectModule::STOPPING) {
2282 mEffects[i]->stop();
2283 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002284 if (release) {
2285 mEffects[i]->release_l();
2286 }
2287
Mikhail Naganov022b9952017-01-04 16:36:51 -08002288 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002289 if (i == size - 1 && i != 0) {
2290 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2291 mEffects[i - 1]->configure();
2292 }
2293 }
2294 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002295 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002296 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002297
Eric Laurentca7cc822012-11-19 14:55:58 -08002298 break;
2299 }
2300 }
2301
2302 return mEffects.size();
2303}
2304
jiabinb8269fd2019-11-11 12:16:27 -08002305// setDevices_l() must be called with ThreadBase::mLock held
2306void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002307{
2308 size_t size = mEffects.size();
2309 for (size_t i = 0; i < size; i++) {
jiabinb8269fd2019-11-11 12:16:27 -08002310 mEffects[i]->setDevices(devices);
2311 }
2312}
2313
2314// setInputDevice_l() must be called with ThreadBase::mLock held
2315void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2316{
2317 size_t size = mEffects.size();
2318 for (size_t i = 0; i < size; i++) {
2319 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002320 }
2321}
2322
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002323// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002324void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2325{
2326 size_t size = mEffects.size();
2327 for (size_t i = 0; i < size; i++) {
2328 mEffects[i]->setMode(mode);
2329 }
2330}
2331
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002332// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002333void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2334{
2335 size_t size = mEffects.size();
2336 for (size_t i = 0; i < size; i++) {
2337 mEffects[i]->setAudioSource(source);
2338 }
2339}
2340
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002341// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002342bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002343{
2344 uint32_t newLeft = *left;
2345 uint32_t newRight = *right;
2346 bool hasControl = false;
2347 int ctrlIdx = -1;
2348 size_t size = mEffects.size();
2349
2350 // first update volume controller
2351 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002352 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002353 ctrlIdx = i - 1;
2354 hasControl = true;
2355 break;
2356 }
2357 }
2358
Eric Laurentfa1e1232016-08-02 19:01:49 -07002359 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002360 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002361 if (hasControl) {
2362 *left = mNewLeftVolume;
2363 *right = mNewRightVolume;
2364 }
2365 return hasControl;
2366 }
2367
2368 mVolumeCtrlIdx = ctrlIdx;
2369 mLeftVolume = newLeft;
2370 mRightVolume = newRight;
2371
2372 // second get volume update from volume controller
2373 if (ctrlIdx >= 0) {
2374 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2375 mNewLeftVolume = newLeft;
2376 mNewRightVolume = newRight;
2377 }
2378 // then indicate volume to all other effects in chain.
2379 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002380 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002381 uint32_t lVol = newLeft;
2382 uint32_t rVol = newRight;
2383
2384 for (size_t i = 0; i < size; i++) {
2385 if ((int)i == ctrlIdx) {
2386 continue;
2387 }
2388 // this also works for ctrlIdx == -1 when there is no volume controller
2389 if ((int)i > ctrlIdx) {
2390 lVol = *left;
2391 rVol = *right;
2392 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002393 // Pass requested volume directly if this is volume monitor module
2394 if (mEffects[i]->isVolumeMonitor()) {
2395 mEffects[i]->setVolume(left, right, false);
2396 } else {
2397 mEffects[i]->setVolume(&lVol, &rVol, false);
2398 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002399 }
2400 *left = newLeft;
2401 *right = newRight;
2402
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002403 setVolumeForOutput_l(*left, *right);
2404
Eric Laurentca7cc822012-11-19 14:55:58 -08002405 return hasControl;
2406}
2407
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002408// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002409void AudioFlinger::EffectChain::resetVolume_l()
2410{
Eric Laurente7449bf2016-08-03 18:44:07 -07002411 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2412 uint32_t left = mLeftVolume;
2413 uint32_t right = mRightVolume;
2414 (void)setVolume_l(&left, &right, true);
2415 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002416}
2417
Eric Laurent1b928682014-10-02 19:41:47 -07002418void AudioFlinger::EffectChain::syncHalEffectsState()
2419{
2420 Mutex::Autolock _l(mLock);
2421 for (size_t i = 0; i < mEffects.size(); i++) {
2422 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2423 mEffects[i]->state() == EffectModule::STOPPING) {
2424 mEffects[i]->addEffectToHal_l();
2425 }
2426 }
2427}
2428
Eric Laurentca7cc822012-11-19 14:55:58 -08002429void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2430{
Eric Laurentca7cc822012-11-19 14:55:58 -08002431 String8 result;
2432
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002433 const size_t numEffects = mEffects.size();
2434 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002435
Marco Nelissenb2208842014-02-07 14:00:50 -08002436 if (numEffects) {
2437 bool locked = AudioFlinger::dumpTryLock(mLock);
2438 // failed to lock - AudioFlinger is probably deadlocked
2439 if (!locked) {
2440 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002441 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002442
Andy Hungbded9c82017-11-30 18:47:35 -08002443 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2444 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2445 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2446 (int)inBufferStr.size(), "In buffer ",
2447 (int)outBufferStr.size(), "Out buffer ");
2448 result.appendFormat("\t%s %s %d\n",
2449 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002450 write(fd, result.string(), result.size());
2451
2452 for (size_t i = 0; i < numEffects; ++i) {
2453 sp<EffectModule> effect = mEffects[i];
2454 if (effect != 0) {
2455 effect->dump(fd, args);
2456 }
2457 }
2458
2459 if (locked) {
2460 mLock.unlock();
2461 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002462 } else {
2463 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002464 }
2465}
2466
2467// must be called with ThreadBase::mLock held
2468void AudioFlinger::EffectChain::setEffectSuspended_l(
2469 const effect_uuid_t *type, bool suspend)
2470{
2471 sp<SuspendedEffectDesc> desc;
2472 // use effect type UUID timelow as key as there is no real risk of identical
2473 // timeLow fields among effect type UUIDs.
2474 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2475 if (suspend) {
2476 if (index >= 0) {
2477 desc = mSuspendedEffects.valueAt(index);
2478 } else {
2479 desc = new SuspendedEffectDesc();
2480 desc->mType = *type;
2481 mSuspendedEffects.add(type->timeLow, desc);
2482 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2483 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002484
Eric Laurentca7cc822012-11-19 14:55:58 -08002485 if (desc->mRefCount++ == 0) {
2486 sp<EffectModule> effect = getEffectIfEnabled(type);
2487 if (effect != 0) {
2488 desc->mEffect = effect;
2489 effect->setSuspended(true);
2490 effect->setEnabled(false);
2491 }
2492 }
2493 } else {
2494 if (index < 0) {
2495 return;
2496 }
2497 desc = mSuspendedEffects.valueAt(index);
2498 if (desc->mRefCount <= 0) {
2499 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002500 desc->mRefCount = 0;
2501 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002502 }
2503 if (--desc->mRefCount == 0) {
2504 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2505 if (desc->mEffect != 0) {
2506 sp<EffectModule> effect = desc->mEffect.promote();
2507 if (effect != 0) {
2508 effect->setSuspended(false);
2509 effect->lock();
2510 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002511 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002512 effect->setEnabled_l(handle->enabled());
2513 }
2514 effect->unlock();
2515 }
2516 desc->mEffect.clear();
2517 }
2518 mSuspendedEffects.removeItemsAt(index);
2519 }
2520 }
2521}
2522
2523// must be called with ThreadBase::mLock held
2524void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2525{
2526 sp<SuspendedEffectDesc> desc;
2527
2528 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2529 if (suspend) {
2530 if (index >= 0) {
2531 desc = mSuspendedEffects.valueAt(index);
2532 } else {
2533 desc = new SuspendedEffectDesc();
2534 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2535 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2536 }
2537 if (desc->mRefCount++ == 0) {
2538 Vector< sp<EffectModule> > effects;
2539 getSuspendEligibleEffects(effects);
2540 for (size_t i = 0; i < effects.size(); i++) {
2541 setEffectSuspended_l(&effects[i]->desc().type, true);
2542 }
2543 }
2544 } else {
2545 if (index < 0) {
2546 return;
2547 }
2548 desc = mSuspendedEffects.valueAt(index);
2549 if (desc->mRefCount <= 0) {
2550 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2551 desc->mRefCount = 1;
2552 }
2553 if (--desc->mRefCount == 0) {
2554 Vector<const effect_uuid_t *> types;
2555 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2556 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2557 continue;
2558 }
2559 types.add(&mSuspendedEffects.valueAt(i)->mType);
2560 }
2561 for (size_t i = 0; i < types.size(); i++) {
2562 setEffectSuspended_l(types[i], false);
2563 }
2564 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2565 mSuspendedEffects.keyAt(index));
2566 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2567 }
2568 }
2569}
2570
2571
2572// The volume effect is used for automated tests only
2573#ifndef OPENSL_ES_H_
2574static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2575 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2576const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2577#endif //OPENSL_ES_H_
2578
Eric Laurentd8365c52017-07-16 15:27:05 -07002579/* static */
2580bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2581{
2582 // Only NS and AEC are suspended when BtNRec is off
2583 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2584 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2585 return true;
2586 }
2587 return false;
2588}
2589
Eric Laurentca7cc822012-11-19 14:55:58 -08002590bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2591{
2592 // auxiliary effects and visualizer are never suspended on output mix
2593 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2594 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2595 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002596 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2597 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002598 return false;
2599 }
2600 return true;
2601}
2602
2603void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2604 Vector< sp<AudioFlinger::EffectModule> > &effects)
2605{
2606 effects.clear();
2607 for (size_t i = 0; i < mEffects.size(); i++) {
2608 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2609 effects.add(mEffects[i]);
2610 }
2611 }
2612}
2613
2614sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2615 const effect_uuid_t *type)
2616{
2617 sp<EffectModule> effect = getEffectFromType_l(type);
2618 return effect != 0 && effect->isEnabled() ? effect : 0;
2619}
2620
2621void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2622 bool enabled)
2623{
2624 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2625 if (enabled) {
2626 if (index < 0) {
2627 // if the effect is not suspend check if all effects are suspended
2628 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2629 if (index < 0) {
2630 return;
2631 }
2632 if (!isEffectEligibleForSuspend(effect->desc())) {
2633 return;
2634 }
2635 setEffectSuspended_l(&effect->desc().type, enabled);
2636 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2637 if (index < 0) {
2638 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2639 return;
2640 }
2641 }
2642 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2643 effect->desc().type.timeLow);
2644 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002645 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002646 if (desc->mEffect == 0) {
2647 desc->mEffect = effect;
2648 effect->setEnabled(false);
2649 effect->setSuspended(true);
2650 }
2651 } else {
2652 if (index < 0) {
2653 return;
2654 }
2655 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2656 effect->desc().type.timeLow);
2657 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2658 desc->mEffect.clear();
2659 effect->setSuspended(false);
2660 }
2661}
2662
Eric Laurent5baf2af2013-09-12 17:37:00 -07002663bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002664{
2665 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002666 return isNonOffloadableEnabled_l();
2667}
2668
2669bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2670{
Eric Laurent813e2a72013-08-31 12:59:48 -07002671 size_t size = mEffects.size();
2672 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002673 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002674 return true;
2675 }
2676 }
2677 return false;
2678}
2679
Eric Laurentaaa44472014-09-12 17:41:50 -07002680void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2681{
2682 Mutex::Autolock _l(mLock);
2683 mThread = thread;
2684 for (size_t i = 0; i < mEffects.size(); i++) {
2685 mEffects[i]->setThread(thread);
2686 }
2687}
2688
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002689void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2690{
2691 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2692 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2693 }
2694 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2695 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2696 }
2697}
2698
2699void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2700{
2701 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2702 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2703 }
2704 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2705 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2706 }
2707}
2708
2709bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002710{
2711 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002712 for (const auto &effect : mEffects) {
2713 if (effect->isProcessImplemented()) {
2714 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002715 }
2716 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002717 // Allow effects without processing.
2718 return true;
2719}
2720
2721bool AudioFlinger::EffectChain::isFastCompatible() const
2722{
2723 Mutex::Autolock _l(mLock);
2724 for (const auto &effect : mEffects) {
2725 if (effect->isProcessImplemented()
2726 && effect->isImplementationSoftware()) {
2727 return false;
2728 }
2729 }
2730 // Allow effects without processing or hw accelerated effects.
2731 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002732}
2733
2734// isCompatibleWithThread_l() must be called with thread->mLock held
2735bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2736{
2737 Mutex::Autolock _l(mLock);
2738 for (size_t i = 0; i < mEffects.size(); i++) {
2739 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2740 return false;
2741 }
2742 }
2743 return true;
2744}
2745
Glenn Kasten63238ef2015-03-02 15:50:29 -08002746} // namespace android