blob: 4296698bf8b3081420ef200fbfdcd788076411a8 [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
Eric Laurent5d885392019-12-13 10:56:31 -080067AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080068 effect_descriptor_t *desc,
69 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080070 audio_session_t sessionId,
71 bool pinned)
72 : mPinned(pinned),
Eric Laurent5d885392019-12-13 10:56:31 -080073 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurentca7cc822012-11-19 14:55:58 -080074 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080075 // clear mConfig to ensure consistent initial value of buffer framecount
76 // in case buffers are associated by setInBuffer() or setOutBuffer()
77 // prior to configure().
78 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080079 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080080 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
81 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070082 mSuspended(false),
Eric Laurent5d885392019-12-13 10:56:31 -080083 mOffloaded(false)
rago94a1ee82017-07-21 15:11:02 -070084#ifdef FLOAT_EFFECT_CHAIN
85 , mSupportsFloat(false)
86#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080087{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080088 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080089 int lStatus;
90
91 // create effect engine from effect factory
Eric Laurent5d885392019-12-13 10:56:31 -080092 mStatus = callback->createEffectHal(
93 &desc->uuid, sessionId, AUDIO_PORT_HANDLE_NONE, &mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -080094 if (mStatus != NO_ERROR) {
95 return;
96 }
97 lStatus = init();
98 if (lStatus < 0) {
99 mStatus = lStatus;
100 goto Error;
101 }
102
Eric Laurent5d885392019-12-13 10:56:31 -0800103 setOffloaded(callback->isOffload(), callback->io());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700104 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800105
Eric Laurentca7cc822012-11-19 14:55:58 -0800106 return;
107Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700108 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800109 ALOGV("Constructor Error %d", mStatus);
110}
111
112AudioFlinger::EffectModule::~EffectModule()
113{
114 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700115 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700116 char uuidStr[64];
117 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
118 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
119 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800120 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800121 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800122
Eric Laurentca7cc822012-11-19 14:55:58 -0800123}
124
125status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
126{
127 status_t status;
128
129 Mutex::Autolock _l(mLock);
130 int priority = handle->priority();
131 size_t size = mHandles.size();
132 EffectHandle *controlHandle = NULL;
133 size_t i;
134 for (i = 0; i < size; i++) {
135 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800136 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800137 continue;
138 }
139 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700140 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800141 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700142 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800143 if (h->priority() <= priority) {
144 break;
145 }
146 }
147 // if inserted in first place, move effect control from previous owner to this handle
148 if (i == 0) {
149 bool enabled = false;
150 if (controlHandle != NULL) {
151 enabled = controlHandle->enabled();
152 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
153 }
154 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
155 status = NO_ERROR;
156 } else {
157 status = ALREADY_EXISTS;
158 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700159 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800160 mHandles.insertAt(handle, i);
161 return status;
162}
163
Eric Laurent6c796322019-04-09 14:13:17 -0700164status_t AudioFlinger::EffectModule::updatePolicyState()
165{
166 status_t status = NO_ERROR;
167 bool doRegister = false;
168 bool registered = false;
169 bool doEnable = false;
170 bool enabled = false;
171 audio_io_handle_t io;
172 uint32_t strategy;
173
174 {
175 Mutex::Autolock _l(mLock);
176 // register effect when first handle is attached and unregister when last handle is removed
177 if (mPolicyRegistered != mHandles.size() > 0) {
178 doRegister = true;
179 mPolicyRegistered = mHandles.size() > 0;
180 if (mPolicyRegistered) {
Eric Laurent5d885392019-12-13 10:56:31 -0800181 io = mCallback->io();
182 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700183 }
184 }
185 // enable effect when registered according to enable state requested by controlling handle
186 if (mHandles.size() > 0) {
187 EffectHandle *handle = controlHandle_l();
188 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
189 doEnable = true;
190 mPolicyEnabled = handle->enabled();
191 }
192 }
193 registered = mPolicyRegistered;
194 enabled = mPolicyEnabled;
195 mPolicyLock.lock();
196 }
197 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
198 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
199 if (doRegister) {
200 if (registered) {
201 status = AudioSystem::registerEffect(
202 &mDescriptor,
203 io,
204 strategy,
205 mSessionId,
206 mId);
207 } else {
208 status = AudioSystem::unregisterEffect(mId);
209 }
210 }
211 if (registered && doEnable) {
212 status = AudioSystem::setEffectEnabled(mId, enabled);
213 }
214 mPolicyLock.unlock();
215
216 return status;
217}
218
219
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800220ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800221{
222 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800223 return removeHandle_l(handle);
224}
225
226ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
227{
Eric Laurentca7cc822012-11-19 14:55:58 -0800228 size_t size = mHandles.size();
229 size_t i;
230 for (i = 0; i < size; i++) {
231 if (mHandles[i] == handle) {
232 break;
233 }
234 }
235 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800236 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
237 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800238 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800239 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800240
241 mHandles.removeAt(i);
242 // if removed from first place, move effect control from this handle to next in line
243 if (i == 0) {
244 EffectHandle *h = controlHandle_l();
245 if (h != NULL) {
246 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
247 }
248 }
249
250 // Prevent calls to process() and other functions on effect interface from now on.
251 // The effect engine will be released by the destructor when the last strong reference on
252 // this object is released which can happen after next process is called.
253 if (mHandles.size() == 0 && !mPinned) {
254 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800255 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800256 }
257
258 return mHandles.size();
259}
260
261// must be called with EffectModule::mLock held
262AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
263{
264 // the first valid handle in the list has control over the module
265 for (size_t i = 0; i < mHandles.size(); i++) {
266 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800267 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800268 return h;
269 }
270 }
271
272 return NULL;
273}
274
Eric Laurentf10c7092016-12-06 17:09:56 -0800275// unsafe method called when the effect parent thread has been destroyed
276ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
277{
278 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent5d885392019-12-13 10:56:31 -0800279 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
280 return mHandles.size();
281 }
282
Eric Laurentf10c7092016-12-06 17:09:56 -0800283 Mutex::Autolock _l(mLock);
284 ssize_t numHandles = removeHandle_l(handle);
285 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent5d885392019-12-13 10:56:31 -0800286 mLock.unlock();
287 mCallback->updateOrphanEffectChains(this);
288 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800289 }
290 return numHandles;
291}
292
Eric Laurentfa1e1232016-08-02 19:01:49 -0700293bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800294 Mutex::Autolock _l(mLock);
295
Eric Laurentfa1e1232016-08-02 19:01:49 -0700296 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800297 switch (mState) {
298 case RESTART:
299 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700300 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800301
302 case STARTING:
303 // clear auxiliary effect input buffer for next accumulation
304 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
305 memset(mConfig.inputCfg.buffer.raw,
306 0,
307 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
308 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700309 if (start_l() == NO_ERROR) {
310 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700311 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700312 } else {
313 mState = IDLE;
314 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800315 break;
316 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900317 // volume control for offload and direct threads must take effect immediately.
318 if (stop_l() == NO_ERROR
319 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700320 mDisableWaitCnt = mMaxDisableWaitCnt;
321 } else {
322 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
323 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800324 mState = STOPPED;
325 break;
326 case STOPPED:
327 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
328 // turn off sequence.
329 if (--mDisableWaitCnt == 0) {
330 reset_l();
331 mState = IDLE;
332 }
333 break;
334 default: //IDLE , ACTIVE, DESTROYED
335 break;
336 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700337
338 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800339}
340
341void AudioFlinger::EffectModule::process()
342{
343 Mutex::Autolock _l(mLock);
344
Mikhail Naganov022b9952017-01-04 16:36:51 -0800345 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800346 return;
347 }
348
rago94a1ee82017-07-21 15:11:02 -0700349 const uint32_t inChannelCount =
350 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
351 const uint32_t outChannelCount =
352 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
353 const bool auxType =
354 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
355
Andy Hungfa69ca32017-11-30 10:07:53 -0800356 // safeInputOutputSampleCount is 0 if the channel count between input and output
357 // buffers do not match. This prevents automatic accumulation or copying between the
358 // input and output effect buffers without an intermediary effect process.
359 // TODO: consider implementing channel conversion.
360 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700361 mInChannelCountRequested != mOutChannelCountRequested ? 0
362 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800363 mConfig.inputCfg.buffer.frameCount,
364 mConfig.outputCfg.buffer.frameCount);
365 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
366#ifdef FLOAT_EFFECT_CHAIN
367 accumulate_float(
368 mConfig.outputCfg.buffer.f32,
369 mConfig.inputCfg.buffer.f32,
370 safeInputOutputSampleCount);
371#else
372 accumulate_i16(
373 mConfig.outputCfg.buffer.s16,
374 mConfig.inputCfg.buffer.s16,
375 safeInputOutputSampleCount);
376#endif
377 };
378 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
379#ifdef FLOAT_EFFECT_CHAIN
380 memcpy(
381 mConfig.outputCfg.buffer.f32,
382 mConfig.inputCfg.buffer.f32,
383 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
384
385#else
386 memcpy(
387 mConfig.outputCfg.buffer.s16,
388 mConfig.inputCfg.buffer.s16,
389 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
390#endif
391 };
392
Eric Laurentca7cc822012-11-19 14:55:58 -0800393 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700394 int ret;
395 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700396 if (auxType) {
397 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800398 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700399#ifdef FLOAT_EFFECT_CHAIN
400 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800401#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700402 // Do in-place float conversion for auxiliary effect input buffer.
403 static_assert(sizeof(float) <= sizeof(int32_t),
404 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
405
Andy Hungfa69ca32017-11-30 10:07:53 -0800406 memcpy_to_float_from_q4_27(
407 mConfig.inputCfg.buffer.f32,
408 mConfig.inputCfg.buffer.s32,
409 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800410#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800411 } else
Andy Hung116a4982017-11-30 10:15:08 -0800412#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800413 {
Andy Hung116a4982017-11-30 10:15:08 -0800414#ifdef FLOAT_AUX
415 memcpy_to_i16_from_float(
416 mConfig.inputCfg.buffer.s16,
417 mConfig.inputCfg.buffer.f32,
418 mConfig.inputCfg.buffer.frameCount);
419#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800420 memcpy_to_i16_from_q4_27(
421 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700422 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800423 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800424#endif
rago94a1ee82017-07-21 15:11:02 -0700425 }
rago94a1ee82017-07-21 15:11:02 -0700426 }
427#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800428 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
429 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
430
431 if (!auxType && mInChannelCountRequested != inChannelCount) {
432 adjust_channels(
433 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
434 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
435 sizeof(float),
436 sizeof(float)
437 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
438 inBuffer = mInConversionBuffer;
439 }
440 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
441 && mOutChannelCountRequested != outChannelCount) {
442 adjust_selected_channels(
443 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
444 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
445 sizeof(float),
446 sizeof(float)
447 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
448 outBuffer = mOutConversionBuffer;
449 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800450 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
451 if (!auxType) {
452 if (mInConversionBuffer.get() == nullptr) {
453 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
454 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700455 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800456 memcpy_to_i16_from_float(
457 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800458 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800459 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800460 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700461 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800462 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
463 if (mOutConversionBuffer.get() == nullptr) {
464 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
465 goto data_bypass;
466 }
467 memcpy_to_i16_from_float(
468 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800469 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800470 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800471 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700472 }
473 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800474#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800475 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800476#ifdef FLOAT_EFFECT_CHAIN
477 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800478 sp<EffectBufferHalInterface> target =
479 mOutChannelCountRequested != outChannelCount
480 ? mOutConversionBuffer : mOutBuffer;
481
Andy Hungfa69ca32017-11-30 10:07:53 -0800482 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800483 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800484 mOutConversionBuffer->audioBuffer()->s16,
485 outChannelCount * mConfig.outputCfg.buffer.frameCount);
486 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800487 if (mOutChannelCountRequested != outChannelCount) {
488 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
489 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
490 sizeof(float),
491 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
492 }
rago94a1ee82017-07-21 15:11:02 -0700493#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700494 } else {
rago94a1ee82017-07-21 15:11:02 -0700495#ifdef FLOAT_EFFECT_CHAIN
496 data_bypass:
497#endif
498 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800499 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700500 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800501 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700502 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800503 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700504 }
505 }
506 ret = -ENODATA;
507 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800508
Eric Laurentca7cc822012-11-19 14:55:58 -0800509 // force transition to IDLE state when engine is ready
510 if (mState == STOPPED && ret == -ENODATA) {
511 mDisableWaitCnt = 1;
512 }
513
514 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700515 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800516#ifdef FLOAT_AUX
517 const size_t size =
518 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
519#else
rago94a1ee82017-07-21 15:11:02 -0700520 const size_t size =
521 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800522#endif
rago94a1ee82017-07-21 15:11:02 -0700523 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800524 }
525 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700526 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800527 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
528 // If an insert effect is idle and input buffer is different from output buffer,
529 // accumulate input onto output
Eric Laurent5d885392019-12-13 10:56:31 -0800530 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700531 // similar handling with data_bypass above.
532 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
533 accumulateInputToOutput();
534 } else { // EFFECT_BUFFER_ACCESS_WRITE
535 copyInputToOutput();
536 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800537 }
538 }
539}
540
541void AudioFlinger::EffectModule::reset_l()
542{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700543 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800544 return;
545 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700546 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800547}
548
549status_t AudioFlinger::EffectModule::configure()
550{
rago94a1ee82017-07-21 15:11:02 -0700551 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700552 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700553 uint32_t size;
554 audio_channel_mask_t channelMask;
555
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700556 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700557 status = NO_INIT;
558 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800559 }
560
Eric Laurentca7cc822012-11-19 14:55:58 -0800561 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800562 // TODO: handle configuration of input (record) SW effects above the HAL,
563 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
564 // in which case input channel masks should be used here.
Eric Laurent5d885392019-12-13 10:56:31 -0800565 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800566 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700567 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800568
569 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800570 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
571 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
572 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
573 mConfig.inputCfg.channels);
574 }
575#ifndef MULTICHANNEL_EFFECT_CHAIN
576 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
577 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
578 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
579 mConfig.outputCfg.channels);
580 }
581#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800582 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800583#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700584 // TODO: Update this logic when multichannel effects are implemented.
585 // For offloaded tracks consider mono output as stereo for proper effect initialization
586 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
587 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
588 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
589 ALOGV("Overriding effect input and output as STEREO");
590 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800591#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800592 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800593 mInChannelCountRequested =
594 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
595 mOutChannelCountRequested =
596 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700597
rago94a1ee82017-07-21 15:11:02 -0700598 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
599 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900600
601 // Don't use sample rate for thread if effect isn't offloadable.
Eric Laurent5d885392019-12-13 10:56:31 -0800602 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900603 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
604 ALOGV("Overriding effect input as 48kHz");
605 } else {
Eric Laurent5d885392019-12-13 10:56:31 -0800606 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900607 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800608 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
609 mConfig.inputCfg.bufferProvider.cookie = NULL;
610 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
611 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
612 mConfig.outputCfg.bufferProvider.cookie = NULL;
613 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
614 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
615 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
616 // Insert effect:
Eric Laurenta20c4e92019-11-12 15:55:51 -0800617 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800618 // always overwrites output buffer: input buffer == output buffer
619 // - in other sessions:
620 // last effect in the chain accumulates in output buffer: input buffer != output buffer
621 // other effect: overwrites output buffer: input buffer == output buffer
622 // Auxiliary effect:
623 // accumulates in output buffer: input buffer != output buffer
624 // Therefore: accumulate <=> input buffer != output buffer
625 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
626 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
627 } else {
628 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
629 }
630 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
631 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent5d885392019-12-13 10:56:31 -0800632 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800633 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
634
Eric Laurent5d885392019-12-13 10:56:31 -0800635 ALOGV("configure() %p chain %p buffer %p framecount %zu",
636 this, mCallback->chain().promote() != nullptr ? mCallback->chain().promote().get() :
637 nullptr,
638 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800639
640 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700641 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700642 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800643 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700644 &mConfig,
645 &size,
646 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700647 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800648 status = cmdStatus;
649 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800650
651#ifdef MULTICHANNEL_EFFECT_CHAIN
652 if (status != NO_ERROR &&
Eric Laurent5d885392019-12-13 10:56:31 -0800653 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800654 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
655 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
656 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700657 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
658 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800659 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
660 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
661 }
662 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
663 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
664 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
665 }
666 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700667 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800668 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700669 &mConfig,
670 &size,
671 &cmdStatus);
672 if (status == NO_ERROR) {
673 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800674 }
675 }
676#endif
677
678#ifdef FLOAT_EFFECT_CHAIN
679 if (status == NO_ERROR) {
680 mSupportsFloat = true;
681 }
682
683 if (status != NO_ERROR) {
684 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
685 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
686 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
687 size = sizeof(int);
688 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
689 sizeof(mConfig),
690 &mConfig,
691 &size,
692 &cmdStatus);
693 if (status == NO_ERROR) {
694 status = cmdStatus;
695 }
696 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700697 mSupportsFloat = false;
698 ALOGVV("config worked with 16 bit");
699 } else {
700 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800701 }
rago94a1ee82017-07-21 15:11:02 -0700702 }
703#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800704
rago94a1ee82017-07-21 15:11:02 -0700705 if (status == NO_ERROR) {
706 // Establish Buffer strategy
707 setInBuffer(mInBuffer);
708 setOutBuffer(mOutBuffer);
709
710 // Update visualizer latency
711 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
712 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
713 effect_param_t *p = (effect_param_t *)buf32;
714
715 p->psize = sizeof(uint32_t);
716 p->vsize = sizeof(uint32_t);
717 size = sizeof(int);
718 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
719
Eric Laurent5d885392019-12-13 10:56:31 -0800720 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -0700721
722 *((int32_t *)p->data + 1)= latency;
723 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
724 sizeof(effect_param_t) + 8,
725 &buf32,
726 &size,
727 &cmdStatus);
728 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800729 }
730
Andy Hung05083ac2017-12-14 15:00:28 -0800731 // mConfig.outputCfg.buffer.frameCount cannot be zero.
732 mMaxDisableWaitCnt = (uint32_t)std::max(
733 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
734 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
735 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800736
Eric Laurentd0ebb532013-04-02 16:41:41 -0700737exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800738 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700739 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700740 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800741 return status;
742}
743
744status_t AudioFlinger::EffectModule::init()
745{
746 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700747 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800748 return NO_INIT;
749 }
750 status_t cmdStatus;
751 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700752 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
753 0,
754 NULL,
755 &size,
756 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800757 if (status == 0) {
758 status = cmdStatus;
759 }
760 return status;
761}
762
Eric Laurent1b928682014-10-02 19:41:47 -0700763void AudioFlinger::EffectModule::addEffectToHal_l()
764{
765 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
766 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent5d885392019-12-13 10:56:31 -0800767 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -0700768 }
769}
770
Eric Laurentfa1e1232016-08-02 19:01:49 -0700771// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800772status_t AudioFlinger::EffectModule::start()
773{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700774 status_t status;
775 {
776 Mutex::Autolock _l(mLock);
777 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -0700778 }
Eric Laurent5d885392019-12-13 10:56:31 -0800779 if (status == NO_ERROR) {
780 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -0700781 }
782 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800783}
784
785status_t AudioFlinger::EffectModule::start_l()
786{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700787 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800788 return NO_INIT;
789 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700790 if (mStatus != NO_ERROR) {
791 return mStatus;
792 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800793 status_t cmdStatus;
794 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700795 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
796 0,
797 NULL,
798 &size,
799 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800800 if (status == 0) {
801 status = cmdStatus;
802 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700803 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700804 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800805 }
806 return status;
807}
808
809status_t AudioFlinger::EffectModule::stop()
810{
811 Mutex::Autolock _l(mLock);
812 return stop_l();
813}
814
815status_t AudioFlinger::EffectModule::stop_l()
816{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700817 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800818 return NO_INIT;
819 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700820 if (mStatus != NO_ERROR) {
821 return mStatus;
822 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800823 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800824 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900825
826 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900827 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
828 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
829 mSetVolumeReentrantTid = gettid();
Eric Laurent5d885392019-12-13 10:56:31 -0800830 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900831 mSetVolumeReentrantTid = INVALID_PID;
832 }
833
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700834 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
835 0,
836 NULL,
837 &size,
838 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800839 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800840 status = cmdStatus;
841 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800842 if (status == NO_ERROR) {
Eric Laurent5d885392019-12-13 10:56:31 -0800843 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800844 }
845 return status;
846}
847
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800848// must be called with EffectChain::mLock held
849void AudioFlinger::EffectModule::release_l()
850{
851 if (mEffectInterface != 0) {
Eric Laurent5d885392019-12-13 10:56:31 -0800852 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800853 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800854 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800855 mEffectInterface.clear();
856 }
857}
858
Eric Laurent5d885392019-12-13 10:56:31 -0800859status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -0800860{
861 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
862 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent5d885392019-12-13 10:56:31 -0800863 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -0800864 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800865 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800866}
867
Andy Hunge4a1d912016-08-17 14:11:13 -0700868// round up delta valid if value and divisor are positive.
869template <typename T>
870static T roundUpDelta(const T &value, const T &divisor) {
871 T remainder = value % divisor;
872 return remainder == 0 ? 0 : divisor - remainder;
873}
874
Eric Laurentca7cc822012-11-19 14:55:58 -0800875status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
876 uint32_t cmdSize,
877 void *pCmdData,
878 uint32_t *replySize,
879 void *pReplyData)
880{
881 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700882 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800883
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700884 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800885 return NO_INIT;
886 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700887 if (mStatus != NO_ERROR) {
888 return mStatus;
889 }
Andy Hung110bc952016-06-20 15:22:52 -0700890 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700891 (sizeof(effect_param_t) > cmdSize ||
892 ((effect_param_t *)pCmdData)->psize > cmdSize
893 - sizeof(effect_param_t))) {
894 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800895 android_errorWriteLog(0x534e4554, "33003822");
896 return -EINVAL;
897 }
898 if (cmdCode == EFFECT_CMD_GET_PARAM &&
899 (*replySize < sizeof(effect_param_t) ||
900 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
901 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700902 return -EINVAL;
903 }
ragoe2759072016-11-22 18:02:48 -0800904 if (cmdCode == EFFECT_CMD_GET_PARAM &&
905 (sizeof(effect_param_t) > *replySize
906 || ((effect_param_t *)pCmdData)->psize > *replySize
907 - sizeof(effect_param_t)
908 || ((effect_param_t *)pCmdData)->vsize > *replySize
909 - sizeof(effect_param_t)
910 - ((effect_param_t *)pCmdData)->psize
911 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
912 *replySize
913 - sizeof(effect_param_t)
914 - ((effect_param_t *)pCmdData)->psize
915 - ((effect_param_t *)pCmdData)->vsize)) {
916 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
917 android_errorWriteLog(0x534e4554, "32705438");
918 return -EINVAL;
919 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700920 if ((cmdCode == EFFECT_CMD_SET_PARAM
921 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
922 (sizeof(effect_param_t) > cmdSize
923 || ((effect_param_t *)pCmdData)->psize > cmdSize
924 - sizeof(effect_param_t)
925 || ((effect_param_t *)pCmdData)->vsize > cmdSize
926 - sizeof(effect_param_t)
927 - ((effect_param_t *)pCmdData)->psize
928 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
929 cmdSize
930 - sizeof(effect_param_t)
931 - ((effect_param_t *)pCmdData)->psize
932 - ((effect_param_t *)pCmdData)->vsize)) {
933 android_errorWriteLog(0x534e4554, "30204301");
934 return -EINVAL;
935 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700936 status_t status = mEffectInterface->command(cmdCode,
937 cmdSize,
938 pCmdData,
939 replySize,
940 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800941 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
942 uint32_t size = (replySize == NULL) ? 0 : *replySize;
943 for (size_t i = 1; i < mHandles.size(); i++) {
944 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800945 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800946 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
947 }
948 }
949 }
950 return status;
951}
952
Eric Laurent5d885392019-12-13 10:56:31 -0800953void AudioFlinger::EffectModule::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
954 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
955}
956
957status_t AudioFlinger::EffectModule::setEnabled(bool enabled, bool fromHandle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800958{
Eric Laurent5d885392019-12-13 10:56:31 -0800959 status_t status;
960 {
961 Mutex::Autolock _l(mLock);
962 status = setEnabled_l(enabled);
963 }
964 if (fromHandle) {
965 if (enabled) {
966 if (status != NO_ERROR) {
967 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
968 } else {
969 mCallback->onEffectEnable(this);
970 }
971 } else {
972 mCallback->onEffectDisable(this);
973 }
974 }
975 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800976}
977
978// must be called with EffectModule::mLock held
979status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
980{
981
982 ALOGV("setEnabled %p enabled %d", this, enabled);
983
984 if (enabled != isEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800985 switch (mState) {
986 // going from disabled to enabled
987 case IDLE:
988 mState = STARTING;
989 break;
990 case STOPPED:
991 mState = RESTART;
992 break;
993 case STOPPING:
994 mState = ACTIVE;
995 break;
996
997 // going from enabled to disabled
998 case RESTART:
999 mState = STOPPED;
1000 break;
1001 case STARTING:
1002 mState = IDLE;
1003 break;
1004 case ACTIVE:
1005 mState = STOPPING;
1006 break;
1007 case DESTROYED:
1008 return NO_ERROR; // simply ignore as we are being destroyed
1009 }
1010 for (size_t i = 1; i < mHandles.size(); i++) {
1011 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001012 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001013 h->setEnabled(enabled);
1014 }
1015 }
1016 }
1017 return NO_ERROR;
1018}
1019
1020bool AudioFlinger::EffectModule::isEnabled() const
1021{
1022 switch (mState) {
1023 case RESTART:
1024 case STARTING:
1025 case ACTIVE:
1026 return true;
1027 case IDLE:
1028 case STOPPING:
1029 case STOPPED:
1030 case DESTROYED:
1031 default:
1032 return false;
1033 }
1034}
1035
1036bool AudioFlinger::EffectModule::isProcessEnabled() const
1037{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001038 if (mStatus != NO_ERROR) {
1039 return false;
1040 }
1041
Eric Laurentca7cc822012-11-19 14:55:58 -08001042 switch (mState) {
1043 case RESTART:
1044 case ACTIVE:
1045 case STOPPING:
1046 case STOPPED:
1047 return true;
1048 case IDLE:
1049 case STARTING:
1050 case DESTROYED:
1051 default:
1052 return false;
1053 }
1054}
1055
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001056bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1057{
Eric Laurent5d885392019-12-13 10:56:31 -08001058 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001059}
1060
1061bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1062{
1063 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1064}
1065
Mikhail Naganov022b9952017-01-04 16:36:51 -08001066void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001067 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001068
1069 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001070 if (buffer != 0) {
1071 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1072 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1073 } else {
1074 mConfig.inputCfg.buffer.raw = NULL;
1075 }
1076 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001077 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001078
1079#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001080 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001081 // Theoretically insert effects can also do in-place conversions (destroying
1082 // the original buffer) when the output buffer is identical to the input buffer,
1083 // but we don't optimize for it here.
1084 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001085 const uint32_t inChannelCount =
1086 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1087 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1088 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001089 // we need to translate - create hidl shared buffer and intercept
1090 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001091 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1092 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1093 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001094
1095 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1096 __func__, inChannels, inFrameCount, size);
1097
Andy Hungbded9c82017-11-30 18:47:35 -08001098 if (size > 0 && (mInConversionBuffer.get() == nullptr
1099 || size > mInConversionBuffer->getSize())) {
1100 mInConversionBuffer.clear();
1101 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent5d885392019-12-13 10:56:31 -08001102 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001103 }
Andy Hungbded9c82017-11-30 18:47:35 -08001104 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001105 mInConversionBuffer->setFrameCount(inFrameCount);
1106 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001107 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001108 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001109 }
1110 }
1111#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001112}
1113
1114void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001115 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001116
1117 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001118 if (buffer != 0) {
1119 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1120 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1121 } else {
1122 mConfig.outputCfg.buffer.raw = NULL;
1123 }
1124 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001125 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001126
1127#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001128 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001129 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001130 const uint32_t outChannelCount =
1131 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1132 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1133 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001134 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001135 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1136 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1137 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001138
1139 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1140 __func__, outChannels, outFrameCount, size);
1141
Andy Hungbded9c82017-11-30 18:47:35 -08001142 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1143 || size > mOutConversionBuffer->getSize())) {
1144 mOutConversionBuffer.clear();
1145 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent5d885392019-12-13 10:56:31 -08001146 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001147 }
Andy Hungbded9c82017-11-30 18:47:35 -08001148 if (mOutConversionBuffer.get() != nullptr) {
1149 mOutConversionBuffer->setFrameCount(outFrameCount);
1150 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001151 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001152 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001153 }
1154 }
1155#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001156}
1157
Eric Laurentca7cc822012-11-19 14:55:58 -08001158status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1159{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001160 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001161 if (mStatus != NO_ERROR) {
1162 return mStatus;
1163 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001164 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001165 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1166 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1167 if (isProcessEnabled() &&
1168 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001169 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1170 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001171 uint32_t volume[2];
1172 uint32_t *pVolume = NULL;
1173 uint32_t size = sizeof(volume);
1174 volume[0] = *left;
1175 volume[1] = *right;
1176 if (controller) {
1177 pVolume = volume;
1178 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001179 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1180 size,
1181 volume,
1182 &size,
1183 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001184 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1185 *left = volume[0];
1186 *right = volume[1];
1187 }
1188 }
1189 return status;
1190}
1191
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001192void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1193{
Eric Laurent5d885392019-12-13 10:56:31 -08001194 if (mEffectCallback->isOffloadOrDirect() && !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001195 float vol_l = (float)left / (1 << 24);
1196 float vol_r = (float)right / (1 << 24);
Eric Laurent5d885392019-12-13 10:56:31 -08001197 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001198 }
1199}
1200
jiabinb8269fd2019-11-11 12:16:27 -08001201status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1202 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001203{
jiabinb8269fd2019-11-11 12:16:27 -08001204 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1205 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001206 return NO_ERROR;
1207 }
1208
1209 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001210 if (mStatus != NO_ERROR) {
1211 return mStatus;
1212 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001213 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001214 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001215 status_t cmdStatus;
1216 uint32_t size = sizeof(status_t);
jiabinb8269fd2019-11-11 12:16:27 -08001217 // FIXME: use audio device types and addresses when the hal interface is ready.
1218 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001219 sizeof(uint32_t),
jiabinb8269fd2019-11-11 12:16:27 -08001220 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001221 &size,
1222 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001223 }
1224 return status;
1225}
1226
jiabinb8269fd2019-11-11 12:16:27 -08001227status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1228{
1229 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1230}
1231
1232status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1233{
1234 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1235}
1236
Eric Laurentca7cc822012-11-19 14:55:58 -08001237status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1238{
1239 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001240 if (mStatus != NO_ERROR) {
1241 return mStatus;
1242 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001243 status_t status = NO_ERROR;
1244 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1245 status_t cmdStatus;
1246 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001247 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1248 sizeof(audio_mode_t),
1249 &mode,
1250 &size,
1251 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001252 if (status == NO_ERROR) {
1253 status = cmdStatus;
1254 }
1255 }
1256 return status;
1257}
1258
1259status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1260{
1261 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001262 if (mStatus != NO_ERROR) {
1263 return mStatus;
1264 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001265 status_t status = NO_ERROR;
1266 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1267 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001268 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1269 sizeof(audio_source_t),
1270 &source,
1271 &size,
1272 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001273 }
1274 return status;
1275}
1276
1277void AudioFlinger::EffectModule::setSuspended(bool suspended)
1278{
1279 Mutex::Autolock _l(mLock);
1280 mSuspended = suspended;
1281}
1282
1283bool AudioFlinger::EffectModule::suspended() const
1284{
1285 Mutex::Autolock _l(mLock);
1286 return mSuspended;
1287}
1288
1289bool AudioFlinger::EffectModule::purgeHandles()
1290{
1291 bool enabled = false;
1292 Mutex::Autolock _l(mLock);
Eric Laurent6c796322019-04-09 14:13:17 -07001293 EffectHandle *handle = controlHandle_l();
1294 if (handle != NULL) {
1295 enabled = handle->enabled();
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 }
Eric Laurent6c796322019-04-09 14:13:17 -07001297 mHandles.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001298 return enabled;
1299}
1300
Eric Laurent5baf2af2013-09-12 17:37:00 -07001301status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1302{
1303 Mutex::Autolock _l(mLock);
1304 if (mStatus != NO_ERROR) {
1305 return mStatus;
1306 }
1307 status_t status = NO_ERROR;
1308 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1309 status_t cmdStatus;
1310 uint32_t size = sizeof(status_t);
1311 effect_offload_param_t cmd;
1312
1313 cmd.isOffload = offloaded;
1314 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001315 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1316 sizeof(effect_offload_param_t),
1317 &cmd,
1318 &size,
1319 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001320 if (status == NO_ERROR) {
1321 status = cmdStatus;
1322 }
1323 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1324 } else {
1325 if (offloaded) {
1326 status = INVALID_OPERATION;
1327 }
1328 mOffloaded = false;
1329 }
1330 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1331 return status;
1332}
1333
1334bool AudioFlinger::EffectModule::isOffloaded() const
1335{
1336 Mutex::Autolock _l(mLock);
1337 return mOffloaded;
1338}
1339
Marco Nelissenb2208842014-02-07 14:00:50 -08001340String8 effectFlagsToString(uint32_t flags) {
1341 String8 s;
1342
1343 s.append("conn. mode: ");
1344 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1345 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1346 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1347 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1348 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1349 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1350 default: s.append("unknown/reserved"); break;
1351 }
1352 s.append(", ");
1353
1354 s.append("insert pref: ");
1355 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1356 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1357 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1358 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1359 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1360 default: s.append("unknown/reserved"); break;
1361 }
1362 s.append(", ");
1363
1364 s.append("volume mgmt: ");
1365 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1366 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1367 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1368 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001369 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
Marco Nelissenb2208842014-02-07 14:00:50 -08001370 default: s.append("unknown/reserved"); break;
1371 }
1372 s.append(", ");
1373
1374 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1375 if (devind) {
1376 s.append("device indication: ");
1377 switch (devind) {
1378 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1379 default: s.append("unknown/reserved"); break;
1380 }
1381 s.append(", ");
1382 }
1383
1384 s.append("input mode: ");
1385 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1386 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1387 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1388 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1389 default: s.append("not set"); break;
1390 }
1391 s.append(", ");
1392
1393 s.append("output mode: ");
1394 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1395 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1396 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1397 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1398 default: s.append("not set"); break;
1399 }
1400 s.append(", ");
1401
1402 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1403 if (accel) {
1404 s.append("hardware acceleration: ");
1405 switch (accel) {
1406 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1407 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1408 default: s.append("unknown/reserved"); break;
1409 }
1410 s.append(", ");
1411 }
1412
1413 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1414 if (modeind) {
1415 s.append("mode indication: ");
1416 switch (modeind) {
1417 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1418 default: s.append("unknown/reserved"); break;
1419 }
1420 s.append(", ");
1421 }
1422
1423 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1424 if (srcind) {
1425 s.append("source indication: ");
1426 switch (srcind) {
1427 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1428 default: s.append("unknown/reserved"); break;
1429 }
1430 s.append(", ");
1431 }
1432
1433 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1434 s.append("offloadable, ");
1435 }
1436
1437 int len = s.length();
1438 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001439 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001440 s.unlockBuffer(len - 2);
1441 }
1442 return s;
1443}
1444
Andy Hungbded9c82017-11-30 18:47:35 -08001445static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1446 std::stringstream ss;
1447
1448 if (buffer.get() == nullptr) {
1449 return "nullptr"; // make different than below
1450 } else if (buffer->externalData() != nullptr) {
1451 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1452 << " -> "
1453 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1454 } else {
1455 ss << buffer->audioBuffer()->raw;
1456 }
1457 return ss.str();
1458}
Marco Nelissenb2208842014-02-07 14:00:50 -08001459
Glenn Kasten0f11b512014-01-31 16:18:54 -08001460void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001461{
Eric Laurentca7cc822012-11-19 14:55:58 -08001462 String8 result;
1463
Andy Hung9718d662017-12-22 17:57:39 -08001464 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001465
1466 bool locked = AudioFlinger::dumpTryLock(mLock);
1467 // failed to lock - AudioFlinger is probably deadlocked
1468 if (!locked) {
1469 result.append("\t\tCould not lock Fx mutex:\n");
1470 }
1471
Eric Laurentb20cf7d2019-04-05 19:37:34 -07001472 result.append("\t\tSession Status State Registered Enabled Suspended Engine:\n");
1473 result.appendFormat("\t\t%05d %03d %03d %s %s %s %p\n",
1474 mSessionId, mStatus, mState, mPolicyRegistered ? "y" : "n", mPolicyEnabled ? "y" : "n",
1475 mSuspended ? "y" : "n", mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001476
1477 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001478 char uuidStr[64];
1479 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001480 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001481 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001482 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1483 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001485 mDescriptor.flags,
1486 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001487 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001488 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001489
1490 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001491 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001492
1493 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001494
1495 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001496 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1497 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1498 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001499 mConfig.inputCfg.buffer.frameCount,
1500 mConfig.inputCfg.samplingRate,
1501 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001502 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001503 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001504
1505 result.append("\t\t- Output configuration:\n");
1506 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001507 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001508 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001509 mConfig.outputCfg.buffer.frameCount,
1510 mConfig.outputCfg.samplingRate,
1511 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001512 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001513 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001514
rago94a1ee82017-07-21 15:11:02 -07001515#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001516
Andy Hungbded9c82017-11-30 18:47:35 -08001517 result.appendFormat("\t\t- HAL buffers:\n"
1518 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1519 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1520 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1521 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1522 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001523#endif
1524
Andy Hung9718d662017-12-22 17:57:39 -08001525 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001526 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001527 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001528 for (size_t i = 0; i < mHandles.size(); ++i) {
1529 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001530 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001531 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001532 result.append(buffer);
1533 }
1534 }
1535
Eric Laurentca7cc822012-11-19 14:55:58 -08001536 write(fd, result.string(), result.length());
1537
Mikhail Naganov4d547672019-02-22 14:19:19 -08001538 if (mEffectInterface != 0) {
1539 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1540 (void)mEffectInterface->dump(fd);
1541 }
1542
Eric Laurentca7cc822012-11-19 14:55:58 -08001543 if (locked) {
1544 mLock.unlock();
1545 }
1546}
1547
1548// ----------------------------------------------------------------------------
1549// EffectHandle implementation
1550// ----------------------------------------------------------------------------
1551
1552#undef LOG_TAG
1553#define LOG_TAG "AudioFlinger::EffectHandle"
1554
1555AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1556 const sp<AudioFlinger::Client>& client,
1557 const sp<IEffectClient>& effectClient,
1558 int32_t priority)
1559 : BnEffect(),
1560 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001561 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001562{
1563 ALOGV("constructor %p", this);
1564
1565 if (client == 0) {
1566 return;
1567 }
1568 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1569 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001570 if (mCblkMemory == 0 ||
1571 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001572 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001573 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001574 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001575 return;
1576 }
Glenn Kastene75da402013-11-20 13:54:52 -08001577 new(mCblk) effect_param_cblk_t();
1578 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001579}
1580
1581AudioFlinger::EffectHandle::~EffectHandle()
1582{
1583 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001584 disconnect(false);
1585}
1586
Glenn Kastene75da402013-11-20 13:54:52 -08001587status_t AudioFlinger::EffectHandle::initCheck()
1588{
1589 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1590}
1591
Eric Laurentca7cc822012-11-19 14:55:58 -08001592status_t AudioFlinger::EffectHandle::enable()
1593{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001594 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001595 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001596 sp<EffectModule> effect = mEffect.promote();
1597 if (effect == 0 || mDisconnected) {
1598 return DEAD_OBJECT;
1599 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001600 if (!mHasControl) {
1601 return INVALID_OPERATION;
1602 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001603
1604 if (mEnabled) {
1605 return NO_ERROR;
1606 }
1607
1608 mEnabled = true;
1609
Eric Laurent6c796322019-04-09 14:13:17 -07001610 status_t status = effect->updatePolicyState();
1611 if (status != NO_ERROR) {
1612 mEnabled = false;
1613 return status;
1614 }
1615
Eric Laurent5d885392019-12-13 10:56:31 -08001616 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001617
1618 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001619 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001620 return NO_ERROR;
1621 }
1622
Eric Laurent5d885392019-12-13 10:56:31 -08001623 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001624 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001625 mEnabled = false;
1626 }
1627 return status;
1628}
1629
1630status_t AudioFlinger::EffectHandle::disable()
1631{
1632 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001633 AutoMutex _l(mLock);
1634 sp<EffectModule> effect = mEffect.promote();
1635 if (effect == 0 || mDisconnected) {
1636 return DEAD_OBJECT;
1637 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001638 if (!mHasControl) {
1639 return INVALID_OPERATION;
1640 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001641
1642 if (!mEnabled) {
1643 return NO_ERROR;
1644 }
1645 mEnabled = false;
1646
Eric Laurent6c796322019-04-09 14:13:17 -07001647 effect->updatePolicyState();
1648
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001649 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001650 return NO_ERROR;
1651 }
1652
Eric Laurent5d885392019-12-13 10:56:31 -08001653 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001654 return status;
1655}
1656
1657void AudioFlinger::EffectHandle::disconnect()
1658{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001659 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001660 disconnect(true);
1661}
1662
1663void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1664{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001665 AutoMutex _l(mLock);
1666 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1667 if (mDisconnected) {
1668 if (unpinIfLast) {
1669 android_errorWriteLog(0x534e4554, "32707507");
1670 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001671 return;
1672 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001673 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001674 {
1675 sp<EffectModule> effect = mEffect.promote();
1676 if (effect != 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08001677 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001678 ALOGW("%s Effect handle %p disconnected after thread destruction",
1679 __func__, this);
1680 }
1681 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001682 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001683 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001684
Eric Laurentca7cc822012-11-19 14:55:58 -08001685 if (mClient != 0) {
1686 if (mCblk != NULL) {
1687 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1688 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1689 }
1690 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001691 // Client destructor must run with AudioFlinger client mutex locked
1692 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001693 mClient.clear();
1694 }
1695}
1696
1697status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1698 uint32_t cmdSize,
1699 void *pCmdData,
1700 uint32_t *replySize,
1701 void *pReplyData)
1702{
1703 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001704 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001705
Eric Laurentc7ab3092017-06-15 18:43:46 -07001706 // reject commands reserved for internal use by audio framework if coming from outside
1707 // of audioserver
1708 switch(cmdCode) {
1709 case EFFECT_CMD_ENABLE:
1710 case EFFECT_CMD_DISABLE:
1711 case EFFECT_CMD_SET_PARAM:
1712 case EFFECT_CMD_SET_PARAM_DEFERRED:
1713 case EFFECT_CMD_SET_PARAM_COMMIT:
1714 case EFFECT_CMD_GET_PARAM:
1715 break;
1716 default:
1717 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1718 break;
1719 }
1720 android_errorWriteLog(0x534e4554, "62019992");
1721 return BAD_VALUE;
1722 }
1723
Eric Laurent1ffc5852016-12-15 14:46:09 -08001724 if (cmdCode == EFFECT_CMD_ENABLE) {
1725 if (*replySize < sizeof(int)) {
1726 android_errorWriteLog(0x534e4554, "32095713");
1727 return BAD_VALUE;
1728 }
1729 *(int *)pReplyData = NO_ERROR;
1730 *replySize = sizeof(int);
1731 return enable();
1732 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1733 if (*replySize < sizeof(int)) {
1734 android_errorWriteLog(0x534e4554, "32095713");
1735 return BAD_VALUE;
1736 }
1737 *(int *)pReplyData = NO_ERROR;
1738 *replySize = sizeof(int);
1739 return disable();
1740 }
1741
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001742 AutoMutex _l(mLock);
1743 sp<EffectModule> effect = mEffect.promote();
1744 if (effect == 0 || mDisconnected) {
1745 return DEAD_OBJECT;
1746 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001747 // only get parameter command is permitted for applications not controlling the effect
1748 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1749 return INVALID_OPERATION;
1750 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001751 if (mClient == 0) {
1752 return INVALID_OPERATION;
1753 }
1754
1755 // handle commands that are not forwarded transparently to effect engine
1756 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001757 if (*replySize < sizeof(int)) {
1758 android_errorWriteLog(0x534e4554, "32095713");
1759 return BAD_VALUE;
1760 }
1761 *(int *)pReplyData = NO_ERROR;
1762 *replySize = sizeof(int);
1763
Eric Laurentca7cc822012-11-19 14:55:58 -08001764 // No need to trylock() here as this function is executed in the binder thread serving a
1765 // particular client process: no risk to block the whole media server process or mixer
1766 // threads if we are stuck here
1767 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001768 // keep local copy of index in case of client corruption b/32220769
1769 const uint32_t clientIndex = mCblk->clientIndex;
1770 const uint32_t serverIndex = mCblk->serverIndex;
1771 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1772 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001773 mCblk->serverIndex = 0;
1774 mCblk->clientIndex = 0;
1775 return BAD_VALUE;
1776 }
1777 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001778 effect_param_t *param = NULL;
1779 for (uint32_t index = serverIndex; index < clientIndex;) {
1780 int *p = (int *)(mBuffer + index);
1781 const int size = *p++;
1782 if (size < 0
1783 || size > EFFECT_PARAM_BUFFER_SIZE
1784 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001785 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001786 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001787 break;
1788 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001789
1790 // copy to local memory in case of client corruption b/32220769
George Burgess IV80a22162020-01-05 20:06:15 -08001791 auto *newParam = (effect_param_t *)realloc(param, size);
1792 if (newParam == NULL) {
Andy Hunga447a0f2016-11-15 17:19:58 -08001793 ALOGW("command(): out of memory");
1794 status = NO_MEMORY;
1795 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001796 }
George Burgess IV80a22162020-01-05 20:06:15 -08001797 param = newParam;
Andy Hunga447a0f2016-11-15 17:19:58 -08001798 memcpy(param, p, size);
1799
1800 int reply = 0;
1801 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001802 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001803 size,
1804 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001805 &rsize,
1806 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001807
1808 // verify shared memory: server index shouldn't change; client index can't go back.
1809 if (serverIndex != mCblk->serverIndex
1810 || clientIndex > mCblk->clientIndex) {
1811 android_errorWriteLog(0x534e4554, "32220769");
1812 status = BAD_VALUE;
1813 break;
1814 }
1815
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 // stop at first error encountered
1817 if (ret != NO_ERROR) {
1818 status = ret;
1819 *(int *)pReplyData = reply;
1820 break;
1821 } else if (reply != NO_ERROR) {
1822 *(int *)pReplyData = reply;
1823 break;
1824 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001825 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001826 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001827 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001828 mCblk->serverIndex = 0;
1829 mCblk->clientIndex = 0;
1830 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 }
1832
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001833 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001834}
1835
1836void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1837{
1838 ALOGV("setControl %p control %d", this, hasControl);
1839
1840 mHasControl = hasControl;
1841 mEnabled = enabled;
1842
1843 if (signal && mEffectClient != 0) {
1844 mEffectClient->controlStatusChanged(hasControl);
1845 }
1846}
1847
1848void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1849 uint32_t cmdSize,
1850 void *pCmdData,
1851 uint32_t replySize,
1852 void *pReplyData)
1853{
1854 if (mEffectClient != 0) {
1855 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1856 }
1857}
1858
1859
1860
1861void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1862{
1863 if (mEffectClient != 0) {
1864 mEffectClient->enableStatusChanged(enabled);
1865 }
1866}
1867
1868status_t AudioFlinger::EffectHandle::onTransact(
1869 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1870{
1871 return BnEffect::onTransact(code, data, reply, flags);
1872}
1873
1874
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001875void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001876{
1877 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1878
Marco Nelissenb2208842014-02-07 14:00:50 -08001879 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001880 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001881 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001882 mHasControl ? "yes" : "no",
1883 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001884 mCblk ? mCblk->clientIndex : 0,
1885 mCblk ? mCblk->serverIndex : 0
1886 );
1887
1888 if (locked) {
1889 mCblk->lock.unlock();
1890 }
1891}
1892
1893#undef LOG_TAG
1894#define LOG_TAG "AudioFlinger::EffectChain"
1895
1896AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001897 audio_session_t sessionId)
Eric Laurent5d885392019-12-13 10:56:31 -08001898 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001899 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent5d885392019-12-13 10:56:31 -08001900 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
1901 mEffectCallback(new EffectCallback(this, thread, thread->mAudioFlinger.get()))
Eric Laurentca7cc822012-11-19 14:55:58 -08001902{
1903 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurent5d885392019-12-13 10:56:31 -08001904 if (thread == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001905 return;
1906 }
1907 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1908 thread->frameCount();
1909}
1910
1911AudioFlinger::EffectChain::~EffectChain()
1912{
Eric Laurentca7cc822012-11-19 14:55:58 -08001913}
1914
1915// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1916sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1917 effect_descriptor_t *descriptor)
1918{
1919 size_t size = mEffects.size();
1920
1921 for (size_t i = 0; i < size; i++) {
1922 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1923 return mEffects[i];
1924 }
1925 }
1926 return 0;
1927}
1928
1929// getEffectFromId_l() must be called with ThreadBase::mLock held
1930sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1931{
1932 size_t size = mEffects.size();
1933
1934 for (size_t i = 0; i < size; i++) {
1935 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1936 if (id == 0 || mEffects[i]->id() == id) {
1937 return mEffects[i];
1938 }
1939 }
1940 return 0;
1941}
1942
1943// getEffectFromType_l() must be called with ThreadBase::mLock held
1944sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1945 const effect_uuid_t *type)
1946{
1947 size_t size = mEffects.size();
1948
1949 for (size_t i = 0; i < size; i++) {
1950 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1951 return mEffects[i];
1952 }
1953 }
1954 return 0;
1955}
1956
Eric Laurent6c796322019-04-09 14:13:17 -07001957std::vector<int> AudioFlinger::EffectChain::getEffectIds()
1958{
1959 std::vector<int> ids;
1960 Mutex::Autolock _l(mLock);
1961 for (size_t i = 0; i < mEffects.size(); i++) {
1962 ids.push_back(mEffects[i]->id());
1963 }
1964 return ids;
1965}
1966
Eric Laurentca7cc822012-11-19 14:55:58 -08001967void AudioFlinger::EffectChain::clearInputBuffer()
1968{
1969 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08001970 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001971}
1972
1973// Must be called with EffectChain::mLock locked
Eric Laurent5d885392019-12-13 10:56:31 -08001974void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08001975{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001976 if (mInBuffer == NULL) {
1977 return;
1978 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001979 const size_t frameSize =
Eric Laurent5d885392019-12-13 10:56:31 -08001980 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07001981
Eric Laurent5d885392019-12-13 10:56:31 -08001982 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08001983 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001984}
1985
1986// Must be called with EffectChain::mLock locked
1987void AudioFlinger::EffectChain::process_l()
1988{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001989 // never process effects when:
1990 // - on an OFFLOAD thread
1991 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent5d885392019-12-13 10:56:31 -08001992 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurenta20c4e92019-11-12 15:55:51 -08001993 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001994 bool tracksOnSession = (trackCnt() != 0);
1995
1996 if (!tracksOnSession && mTailBufferCount == 0) {
1997 doProcess = false;
1998 }
1999
2000 if (activeTrackCnt() == 0) {
2001 // if no track is active and the effect tail has not been rendered,
2002 // the input buffer must be cleared here as the mixer process will not do it
2003 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08002004 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002005 if (mTailBufferCount > 0) {
2006 mTailBufferCount--;
2007 }
2008 }
2009 }
2010 }
2011
2012 size_t size = mEffects.size();
2013 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002014 // Only the input and output buffers of the chain can be external,
2015 // and 'update' / 'commit' do nothing for allocated buffers, thus
2016 // it's not needed to consider any other buffers here.
2017 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002018 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2019 mOutBuffer->update();
2020 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002021 for (size_t i = 0; i < size; i++) {
2022 mEffects[i]->process();
2023 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002024 mInBuffer->commit();
2025 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2026 mOutBuffer->commit();
2027 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002028 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002029 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002030 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002031 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2032 }
2033 if (doResetVolume) {
2034 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002035 }
2036}
2037
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002038// createEffect_l() must be called with ThreadBase::mLock held
2039status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002040 effect_descriptor_t *desc,
2041 int id,
2042 audio_session_t sessionId,
2043 bool pinned)
2044{
2045 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08002046 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002047 status_t lStatus = effect->status();
2048 if (lStatus == NO_ERROR) {
2049 lStatus = addEffect_ll(effect);
2050 }
2051 if (lStatus != NO_ERROR) {
2052 effect.clear();
2053 }
2054 return lStatus;
2055}
2056
2057// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002058status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2059{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002060 Mutex::Autolock _l(mLock);
2061 return addEffect_ll(effect);
2062}
2063// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2064status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2065{
Eric Laurentca7cc822012-11-19 14:55:58 -08002066 effect_descriptor_t desc = effect->desc();
2067 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2068
Eric Laurent5d885392019-12-13 10:56:31 -08002069 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002070
2071 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2072 // Auxiliary effects are inserted at the beginning of mEffects vector as
2073 // they are processed first and accumulated in chain input buffer
2074 mEffects.insertAt(effect, 0);
2075
2076 // the input buffer for auxiliary effect contains mono samples in
2077 // 32 bit format. This is to avoid saturation in AudoMixer
2078 // accumulation stage. Saturation is done in EffectModule::process() before
2079 // calling the process in effect engine
Eric Laurent5d885392019-12-13 10:56:31 -08002080 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002081 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002082#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent5d885392019-12-13 10:56:31 -08002083 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002084 numSamples * sizeof(float), &halBuffer);
2085#else
Eric Laurent5d885392019-12-13 10:56:31 -08002086 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002087 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002088#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002089 if (result != OK) return result;
2090 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002091 // auxiliary effects output samples to chain input buffer for further processing
2092 // by insert effects
2093 effect->setOutBuffer(mInBuffer);
2094 } else {
2095 // Insert effects are inserted at the end of mEffects vector as they are processed
2096 // after track and auxiliary effects.
2097 // Insert effect order as a function of indicated preference:
2098 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2099 // another effect is present
2100 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2101 // last effect claiming first position
2102 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2103 // first effect claiming last position
2104 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2105 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2106 // already present
2107
2108 size_t size = mEffects.size();
2109 size_t idx_insert = size;
2110 ssize_t idx_insert_first = -1;
2111 ssize_t idx_insert_last = -1;
2112
2113 for (size_t i = 0; i < size; i++) {
2114 effect_descriptor_t d = mEffects[i]->desc();
2115 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2116 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2117 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2118 // check invalid effect chaining combinations
2119 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2120 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2121 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2122 desc.name, d.name);
2123 return INVALID_OPERATION;
2124 }
2125 // remember position of first insert effect and by default
2126 // select this as insert position for new effect
2127 if (idx_insert == size) {
2128 idx_insert = i;
2129 }
2130 // remember position of last insert effect claiming
2131 // first position
2132 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2133 idx_insert_first = i;
2134 }
2135 // remember position of first insert effect claiming
2136 // last position
2137 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2138 idx_insert_last == -1) {
2139 idx_insert_last = i;
2140 }
2141 }
2142 }
2143
2144 // modify idx_insert from first position if needed
2145 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2146 if (idx_insert_last != -1) {
2147 idx_insert = idx_insert_last;
2148 } else {
2149 idx_insert = size;
2150 }
2151 } else {
2152 if (idx_insert_first != -1) {
2153 idx_insert = idx_insert_first + 1;
2154 }
2155 }
2156
2157 // always read samples from chain input buffer
2158 effect->setInBuffer(mInBuffer);
2159
2160 // if last effect in the chain, output samples to chain
2161 // output buffer, otherwise to chain input buffer
2162 if (idx_insert == size) {
2163 if (idx_insert != 0) {
2164 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2165 mEffects[idx_insert-1]->configure();
2166 }
2167 effect->setOutBuffer(mOutBuffer);
2168 } else {
2169 effect->setOutBuffer(mInBuffer);
2170 }
2171 mEffects.insertAt(effect, idx_insert);
2172
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002173 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002174 idx_insert);
2175 }
2176 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002177
Eric Laurentca7cc822012-11-19 14:55:58 -08002178 return NO_ERROR;
2179}
2180
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002181// removeEffect_l() must be called with ThreadBase::mLock held
2182size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2183 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002184{
2185 Mutex::Autolock _l(mLock);
2186 size_t size = mEffects.size();
2187 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2188
2189 for (size_t i = 0; i < size; i++) {
2190 if (effect == mEffects[i]) {
2191 // calling stop here will remove pre-processing effect from the audio HAL.
2192 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2193 // the middle of a read from audio HAL
2194 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2195 mEffects[i]->state() == EffectModule::STOPPING) {
2196 mEffects[i]->stop();
2197 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002198 if (release) {
2199 mEffects[i]->release_l();
2200 }
2201
Mikhail Naganov022b9952017-01-04 16:36:51 -08002202 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002203 if (i == size - 1 && i != 0) {
2204 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2205 mEffects[i - 1]->configure();
2206 }
2207 }
2208 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002209 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002210 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002211
Eric Laurentca7cc822012-11-19 14:55:58 -08002212 break;
2213 }
2214 }
2215
2216 return mEffects.size();
2217}
2218
jiabinb8269fd2019-11-11 12:16:27 -08002219// setDevices_l() must be called with ThreadBase::mLock held
2220void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002221{
2222 size_t size = mEffects.size();
2223 for (size_t i = 0; i < size; i++) {
jiabinb8269fd2019-11-11 12:16:27 -08002224 mEffects[i]->setDevices(devices);
2225 }
2226}
2227
2228// setInputDevice_l() must be called with ThreadBase::mLock held
2229void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2230{
2231 size_t size = mEffects.size();
2232 for (size_t i = 0; i < size; i++) {
2233 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002234 }
2235}
2236
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002237// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002238void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2239{
2240 size_t size = mEffects.size();
2241 for (size_t i = 0; i < size; i++) {
2242 mEffects[i]->setMode(mode);
2243 }
2244}
2245
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002246// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002247void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2248{
2249 size_t size = mEffects.size();
2250 for (size_t i = 0; i < size; i++) {
2251 mEffects[i]->setAudioSource(source);
2252 }
2253}
2254
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002255// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002256bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002257{
2258 uint32_t newLeft = *left;
2259 uint32_t newRight = *right;
2260 bool hasControl = false;
2261 int ctrlIdx = -1;
2262 size_t size = mEffects.size();
2263
2264 // first update volume controller
2265 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002266 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002267 ctrlIdx = i - 1;
2268 hasControl = true;
2269 break;
2270 }
2271 }
2272
Eric Laurentfa1e1232016-08-02 19:01:49 -07002273 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002274 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002275 if (hasControl) {
2276 *left = mNewLeftVolume;
2277 *right = mNewRightVolume;
2278 }
2279 return hasControl;
2280 }
2281
2282 mVolumeCtrlIdx = ctrlIdx;
2283 mLeftVolume = newLeft;
2284 mRightVolume = newRight;
2285
2286 // second get volume update from volume controller
2287 if (ctrlIdx >= 0) {
2288 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2289 mNewLeftVolume = newLeft;
2290 mNewRightVolume = newRight;
2291 }
2292 // then indicate volume to all other effects in chain.
2293 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002294 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002295 uint32_t lVol = newLeft;
2296 uint32_t rVol = newRight;
2297
2298 for (size_t i = 0; i < size; i++) {
2299 if ((int)i == ctrlIdx) {
2300 continue;
2301 }
2302 // this also works for ctrlIdx == -1 when there is no volume controller
2303 if ((int)i > ctrlIdx) {
2304 lVol = *left;
2305 rVol = *right;
2306 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002307 // Pass requested volume directly if this is volume monitor module
2308 if (mEffects[i]->isVolumeMonitor()) {
2309 mEffects[i]->setVolume(left, right, false);
2310 } else {
2311 mEffects[i]->setVolume(&lVol, &rVol, false);
2312 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002313 }
2314 *left = newLeft;
2315 *right = newRight;
2316
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002317 setVolumeForOutput_l(*left, *right);
2318
Eric Laurentca7cc822012-11-19 14:55:58 -08002319 return hasControl;
2320}
2321
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002322// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002323void AudioFlinger::EffectChain::resetVolume_l()
2324{
Eric Laurente7449bf2016-08-03 18:44:07 -07002325 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2326 uint32_t left = mLeftVolume;
2327 uint32_t right = mRightVolume;
2328 (void)setVolume_l(&left, &right, true);
2329 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002330}
2331
Eric Laurent1b928682014-10-02 19:41:47 -07002332void AudioFlinger::EffectChain::syncHalEffectsState()
2333{
2334 Mutex::Autolock _l(mLock);
2335 for (size_t i = 0; i < mEffects.size(); i++) {
2336 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2337 mEffects[i]->state() == EffectModule::STOPPING) {
2338 mEffects[i]->addEffectToHal_l();
2339 }
2340 }
2341}
2342
Eric Laurentca7cc822012-11-19 14:55:58 -08002343void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2344{
Eric Laurentca7cc822012-11-19 14:55:58 -08002345 String8 result;
2346
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002347 const size_t numEffects = mEffects.size();
2348 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002349
Marco Nelissenb2208842014-02-07 14:00:50 -08002350 if (numEffects) {
2351 bool locked = AudioFlinger::dumpTryLock(mLock);
2352 // failed to lock - AudioFlinger is probably deadlocked
2353 if (!locked) {
2354 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002355 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002356
Andy Hungbded9c82017-11-30 18:47:35 -08002357 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2358 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2359 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2360 (int)inBufferStr.size(), "In buffer ",
2361 (int)outBufferStr.size(), "Out buffer ");
2362 result.appendFormat("\t%s %s %d\n",
2363 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002364 write(fd, result.string(), result.size());
2365
2366 for (size_t i = 0; i < numEffects; ++i) {
2367 sp<EffectModule> effect = mEffects[i];
2368 if (effect != 0) {
2369 effect->dump(fd, args);
2370 }
2371 }
2372
2373 if (locked) {
2374 mLock.unlock();
2375 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002376 } else {
2377 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002378 }
2379}
2380
2381// must be called with ThreadBase::mLock held
2382void AudioFlinger::EffectChain::setEffectSuspended_l(
2383 const effect_uuid_t *type, bool suspend)
2384{
2385 sp<SuspendedEffectDesc> desc;
2386 // use effect type UUID timelow as key as there is no real risk of identical
2387 // timeLow fields among effect type UUIDs.
2388 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2389 if (suspend) {
2390 if (index >= 0) {
2391 desc = mSuspendedEffects.valueAt(index);
2392 } else {
2393 desc = new SuspendedEffectDesc();
2394 desc->mType = *type;
2395 mSuspendedEffects.add(type->timeLow, desc);
2396 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2397 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002398
Eric Laurentca7cc822012-11-19 14:55:58 -08002399 if (desc->mRefCount++ == 0) {
2400 sp<EffectModule> effect = getEffectIfEnabled(type);
2401 if (effect != 0) {
2402 desc->mEffect = effect;
2403 effect->setSuspended(true);
Eric Laurent5d885392019-12-13 10:56:31 -08002404 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002405 }
2406 }
2407 } else {
2408 if (index < 0) {
2409 return;
2410 }
2411 desc = mSuspendedEffects.valueAt(index);
2412 if (desc->mRefCount <= 0) {
2413 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002414 desc->mRefCount = 0;
2415 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002416 }
2417 if (--desc->mRefCount == 0) {
2418 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2419 if (desc->mEffect != 0) {
2420 sp<EffectModule> effect = desc->mEffect.promote();
2421 if (effect != 0) {
2422 effect->setSuspended(false);
2423 effect->lock();
2424 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002425 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002426 effect->setEnabled_l(handle->enabled());
2427 }
2428 effect->unlock();
2429 }
2430 desc->mEffect.clear();
2431 }
2432 mSuspendedEffects.removeItemsAt(index);
2433 }
2434 }
2435}
2436
2437// must be called with ThreadBase::mLock held
2438void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2439{
2440 sp<SuspendedEffectDesc> desc;
2441
2442 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2443 if (suspend) {
2444 if (index >= 0) {
2445 desc = mSuspendedEffects.valueAt(index);
2446 } else {
2447 desc = new SuspendedEffectDesc();
2448 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2449 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2450 }
2451 if (desc->mRefCount++ == 0) {
2452 Vector< sp<EffectModule> > effects;
2453 getSuspendEligibleEffects(effects);
2454 for (size_t i = 0; i < effects.size(); i++) {
2455 setEffectSuspended_l(&effects[i]->desc().type, true);
2456 }
2457 }
2458 } else {
2459 if (index < 0) {
2460 return;
2461 }
2462 desc = mSuspendedEffects.valueAt(index);
2463 if (desc->mRefCount <= 0) {
2464 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2465 desc->mRefCount = 1;
2466 }
2467 if (--desc->mRefCount == 0) {
2468 Vector<const effect_uuid_t *> types;
2469 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2470 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2471 continue;
2472 }
2473 types.add(&mSuspendedEffects.valueAt(i)->mType);
2474 }
2475 for (size_t i = 0; i < types.size(); i++) {
2476 setEffectSuspended_l(types[i], false);
2477 }
2478 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2479 mSuspendedEffects.keyAt(index));
2480 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2481 }
2482 }
2483}
2484
2485
2486// The volume effect is used for automated tests only
2487#ifndef OPENSL_ES_H_
2488static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2489 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2490const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2491#endif //OPENSL_ES_H_
2492
Eric Laurentd8365c52017-07-16 15:27:05 -07002493/* static */
2494bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2495{
2496 // Only NS and AEC are suspended when BtNRec is off
2497 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2498 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2499 return true;
2500 }
2501 return false;
2502}
2503
Eric Laurentca7cc822012-11-19 14:55:58 -08002504bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2505{
2506 // auxiliary effects and visualizer are never suspended on output mix
2507 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2508 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2509 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002510 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2511 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002512 return false;
2513 }
2514 return true;
2515}
2516
2517void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2518 Vector< sp<AudioFlinger::EffectModule> > &effects)
2519{
2520 effects.clear();
2521 for (size_t i = 0; i < mEffects.size(); i++) {
2522 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2523 effects.add(mEffects[i]);
2524 }
2525 }
2526}
2527
2528sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2529 const effect_uuid_t *type)
2530{
2531 sp<EffectModule> effect = getEffectFromType_l(type);
2532 return effect != 0 && effect->isEnabled() ? effect : 0;
2533}
2534
2535void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2536 bool enabled)
2537{
2538 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2539 if (enabled) {
2540 if (index < 0) {
2541 // if the effect is not suspend check if all effects are suspended
2542 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2543 if (index < 0) {
2544 return;
2545 }
2546 if (!isEffectEligibleForSuspend(effect->desc())) {
2547 return;
2548 }
2549 setEffectSuspended_l(&effect->desc().type, enabled);
2550 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2551 if (index < 0) {
2552 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2553 return;
2554 }
2555 }
2556 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2557 effect->desc().type.timeLow);
2558 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002559 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002560 if (desc->mEffect == 0) {
2561 desc->mEffect = effect;
Eric Laurent5d885392019-12-13 10:56:31 -08002562 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002563 effect->setSuspended(true);
2564 }
2565 } else {
2566 if (index < 0) {
2567 return;
2568 }
2569 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2570 effect->desc().type.timeLow);
2571 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2572 desc->mEffect.clear();
2573 effect->setSuspended(false);
2574 }
2575}
2576
Eric Laurent5baf2af2013-09-12 17:37:00 -07002577bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002578{
2579 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002580 return isNonOffloadableEnabled_l();
2581}
2582
2583bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2584{
Eric Laurent813e2a72013-08-31 12:59:48 -07002585 size_t size = mEffects.size();
2586 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002587 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002588 return true;
2589 }
2590 }
2591 return false;
2592}
2593
Eric Laurentaaa44472014-09-12 17:41:50 -07002594void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2595{
2596 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08002597 mEffectCallback->setThread(thread.get());
Eric Laurentaaa44472014-09-12 17:41:50 -07002598}
2599
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002600void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2601{
2602 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2603 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2604 }
2605 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2606 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2607 }
2608}
2609
2610void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2611{
2612 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2613 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2614 }
2615 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2616 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2617 }
2618}
2619
2620bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002621{
2622 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002623 for (const auto &effect : mEffects) {
2624 if (effect->isProcessImplemented()) {
2625 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002626 }
2627 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002628 // Allow effects without processing.
2629 return true;
2630}
2631
2632bool AudioFlinger::EffectChain::isFastCompatible() const
2633{
2634 Mutex::Autolock _l(mLock);
2635 for (const auto &effect : mEffects) {
2636 if (effect->isProcessImplemented()
2637 && effect->isImplementationSoftware()) {
2638 return false;
2639 }
2640 }
2641 // Allow effects without processing or hw accelerated effects.
2642 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002643}
2644
2645// isCompatibleWithThread_l() must be called with thread->mLock held
2646bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2647{
2648 Mutex::Autolock _l(mLock);
2649 for (size_t i = 0; i < mEffects.size(); i++) {
2650 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2651 return false;
2652 }
2653 }
2654 return true;
2655}
2656
Eric Laurent5d885392019-12-13 10:56:31 -08002657// EffectCallbackInterface implementation
2658status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2659 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2660 sp<EffectHalInterface> *effect) {
2661 status_t status = NO_INIT;
2662 sp<AudioFlinger> af = mAudioFlinger.promote();
2663 if (af == nullptr) {
2664 return status;
2665 }
2666 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2667 if (effectsFactory != 0) {
2668 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2669 }
2670 return status;
2671}
2672
2673bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
2674 const sp<AudioFlinger::EffectModule>& effect) {
2675 sp<AudioFlinger> af = mAudioFlinger.promote();
2676 if (af == nullptr) {
2677 return false;
2678 }
2679 return af->updateOrphanEffectChains(effect);
2680}
2681
2682status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2683 size_t size, sp<EffectBufferHalInterface>* buffer) {
2684 sp<AudioFlinger> af = mAudioFlinger.promote();
2685 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2686 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2687}
2688
2689status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2690 sp<EffectHalInterface> effect) {
2691 status_t result = NO_INIT;
2692 sp<ThreadBase> t = mThread.promote();
2693 if (t == nullptr) {
2694 return result;
2695 }
2696 sp <StreamHalInterface> st = t->stream();
2697 if (st == nullptr) {
2698 return result;
2699 }
2700 result = st->addEffect(effect);
2701 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2702 return result;
2703}
2704
2705status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2706 sp<EffectHalInterface> effect) {
2707 status_t result = NO_INIT;
2708 sp<ThreadBase> t = mThread.promote();
2709 if (t == nullptr) {
2710 return result;
2711 }
2712 sp <StreamHalInterface> st = t->stream();
2713 if (st == nullptr) {
2714 return result;
2715 }
2716 result = st->removeEffect(effect);
2717 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2718 return result;
2719}
2720
2721audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2722 sp<ThreadBase> t = mThread.promote();
2723 if (t == nullptr) {
2724 return AUDIO_IO_HANDLE_NONE;
2725 }
2726 return t->id();
2727}
2728
2729bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2730 sp<ThreadBase> t = mThread.promote();
2731 if (t == nullptr) {
2732 return true;
2733 }
2734 return t->isOutput();
2735}
2736
2737bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2738 sp<ThreadBase> t = mThread.promote();
2739 if (t == nullptr) {
2740 return false;
2741 }
2742 return t->type() == ThreadBase::OFFLOAD;
2743}
2744
2745bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2746 sp<ThreadBase> t = mThread.promote();
2747 if (t == nullptr) {
2748 return false;
2749 }
2750 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2751}
2752
2753bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2754 sp<ThreadBase> t = mThread.promote();
2755 if (t == nullptr) {
2756 return false;
2757 }
2758 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::MMAP;
2759}
2760
2761uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2762 sp<ThreadBase> t = mThread.promote();
2763 if (t == nullptr) {
2764 return 0;
2765 }
2766 return t->sampleRate();
2767}
2768
2769audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2770 sp<ThreadBase> t = mThread.promote();
2771 if (t == nullptr) {
2772 return AUDIO_CHANNEL_NONE;
2773 }
2774 return t->channelMask();
2775}
2776
2777uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2778 sp<ThreadBase> t = mThread.promote();
2779 if (t == nullptr) {
2780 return 0;
2781 }
2782 return t->channelCount();
2783}
2784
2785size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2786 sp<ThreadBase> t = mThread.promote();
2787 if (t == nullptr) {
2788 return 0;
2789 }
2790 return t->frameCount();
2791}
2792
2793uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2794 sp<ThreadBase> t = mThread.promote();
2795 if (t == nullptr) {
2796 return 0;
2797 }
2798 return t->latency_l();
2799}
2800
2801void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2802 sp<ThreadBase> t = mThread.promote();
2803 if (t == nullptr) {
2804 return;
2805 }
2806 t->setVolumeForOutput_l(left, right);
2807}
2808
2809void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
2810 const sp<EffectModule>& effect, bool enabled, bool threadLocked) {
2811 sp<ThreadBase> t = mThread.promote();
2812 if (t == nullptr) {
2813 return;
2814 }
2815 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2816
2817 sp<EffectChain> c = mChain.promote();
2818 if (c == nullptr) {
2819 return;
2820 }
2821 c->checkSuspendOnEffectEnabled(effect, enabled);
2822}
2823
2824void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectModule>& effect) {
2825 sp<ThreadBase> t = mThread.promote();
2826 if (t == nullptr) {
2827 return;
2828 }
2829 t->onEffectEnable(effect);
2830}
2831
2832void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectModule>& effect) {
2833 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2834
2835 sp<ThreadBase> t = mThread.promote();
2836 if (t == nullptr) {
2837 return;
2838 }
2839 t->onEffectDisable();
2840}
2841
2842bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2843 bool unpinIfLast) {
2844 sp<ThreadBase> t = mThread.promote();
2845 if (t == nullptr) {
2846 return false;
2847 }
2848 t->disconnectEffectHandle(handle, unpinIfLast);
2849 return true;
2850}
2851
2852void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2853 sp<EffectChain> c = mChain.promote();
2854 if (c == nullptr) {
2855 return;
2856 }
2857 c->resetVolume_l();
2858
2859}
2860
2861uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2862 sp<EffectChain> c = mChain.promote();
2863 if (c == nullptr) {
2864 return PRODUCT_STRATEGY_NONE;
2865 }
2866 return c->strategy();
2867}
2868
2869int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
2870 sp<EffectChain> c = mChain.promote();
2871 if (c == nullptr) {
2872 return 0;
2873 }
2874 return c->activeTrackCnt();
2875}
2876
Glenn Kasten63238ef2015-03-02 15:50:29 -08002877} // namespace android