blob: 2b34267bf1fdc14c26c558245ca9457be3552636 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
27#include <system/audio_effects/effect_ns.h>
28#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080029#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080030#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070031#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070032#include <media/audiohal/EffectHalInterface.h>
33#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070034#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080035
36#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080037
38// ----------------------------------------------------------------------------
39
40// Note: the following macro is used for extremely verbose logging message. In
41// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
42// 0; but one side effect of this is to turn all LOGV's as well. Some messages
43// are so verbose that we want to suppress them even when we have ALOG_ASSERT
44// turned on. Do not uncomment the #def below unless you really know what you
45// are doing and want to see all of the extremely verbose messages.
46//#define VERY_VERY_VERBOSE_LOGGING
47#ifdef VERY_VERY_VERBOSE_LOGGING
48#define ALOGVV ALOGV
49#else
50#define ALOGVV(a...) do { } while(0)
51#endif
52
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090053#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
54
Eric Laurentca7cc822012-11-19 14:55:58 -080055namespace android {
56
57// ----------------------------------------------------------------------------
58// EffectModule implementation
59// ----------------------------------------------------------------------------
60
61#undef LOG_TAG
62#define LOG_TAG "AudioFlinger::EffectModule"
63
64AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
65 const wp<AudioFlinger::EffectChain>& chain,
66 effect_descriptor_t *desc,
67 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080068 audio_session_t sessionId,
69 bool pinned)
70 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080071 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
72 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080073 // clear mConfig to ensure consistent initial value of buffer framecount
74 // in case buffers are associated by setInBuffer() or setOutBuffer()
75 // prior to configure().
76 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080077 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080078 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
79 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070080 mSuspended(false),
Andy Hung62aef7d2017-12-14 14:50:40 -080081 mOffloaded(false),
Eric Laurentaaa44472014-09-12 17:41:50 -070082 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070083#ifdef FLOAT_EFFECT_CHAIN
84 , mSupportsFloat(false)
85#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080086{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080087 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080088 int lStatus;
89
90 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070091 mStatus = -ENODEV;
92 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070093 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070094 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070095 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070096 mStatus = effectsFactory->createEffect(
97 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
98 }
99 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800100
101 if (mStatus != NO_ERROR) {
102 return;
103 }
104 lStatus = init();
105 if (lStatus < 0) {
106 mStatus = lStatus;
107 goto Error;
108 }
109
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700111 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800112
Eric Laurentca7cc822012-11-19 14:55:58 -0800113 return;
114Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700115 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800116 ALOGV("Constructor Error %d", mStatus);
117}
118
119AudioFlinger::EffectModule::~EffectModule()
120{
121 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700122 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700123 char uuidStr[64];
124 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
125 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
126 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800127 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800128 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800129
Eric Laurentca7cc822012-11-19 14:55:58 -0800130}
131
132status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
133{
134 status_t status;
135
136 Mutex::Autolock _l(mLock);
137 int priority = handle->priority();
138 size_t size = mHandles.size();
139 EffectHandle *controlHandle = NULL;
140 size_t i;
141 for (i = 0; i < size; i++) {
142 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800143 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800144 continue;
145 }
146 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700147 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800148 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700149 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800150 if (h->priority() <= priority) {
151 break;
152 }
153 }
154 // if inserted in first place, move effect control from previous owner to this handle
155 if (i == 0) {
156 bool enabled = false;
157 if (controlHandle != NULL) {
158 enabled = controlHandle->enabled();
159 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
160 }
161 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
162 status = NO_ERROR;
163 } else {
164 status = ALREADY_EXISTS;
165 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700166 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800167 mHandles.insertAt(handle, i);
168 return status;
169}
170
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800171ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800172{
173 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800174 return removeHandle_l(handle);
175}
176
177ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
178{
Eric Laurentca7cc822012-11-19 14:55:58 -0800179 size_t size = mHandles.size();
180 size_t i;
181 for (i = 0; i < size; i++) {
182 if (mHandles[i] == handle) {
183 break;
184 }
185 }
186 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800187 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
188 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800189 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800190 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800191
192 mHandles.removeAt(i);
193 // if removed from first place, move effect control from this handle to next in line
194 if (i == 0) {
195 EffectHandle *h = controlHandle_l();
196 if (h != NULL) {
197 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
198 }
199 }
200
201 // Prevent calls to process() and other functions on effect interface from now on.
202 // The effect engine will be released by the destructor when the last strong reference on
203 // this object is released which can happen after next process is called.
204 if (mHandles.size() == 0 && !mPinned) {
205 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800206 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800207 }
208
209 return mHandles.size();
210}
211
212// must be called with EffectModule::mLock held
213AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
214{
215 // the first valid handle in the list has control over the module
216 for (size_t i = 0; i < mHandles.size(); i++) {
217 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800218 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800219 return h;
220 }
221 }
222
223 return NULL;
224}
225
Eric Laurentf10c7092016-12-06 17:09:56 -0800226// unsafe method called when the effect parent thread has been destroyed
227ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
228{
229 ALOGV("disconnect() %p handle %p", this, handle);
230 Mutex::Autolock _l(mLock);
231 ssize_t numHandles = removeHandle_l(handle);
232 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
233 AudioSystem::unregisterEffect(mId);
234 sp<AudioFlinger> af = mAudioFlinger.promote();
235 if (af != 0) {
236 mLock.unlock();
237 af->updateOrphanEffectChains(this);
238 mLock.lock();
239 }
240 }
241 return numHandles;
242}
243
Eric Laurentfa1e1232016-08-02 19:01:49 -0700244bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800245 Mutex::Autolock _l(mLock);
246
Eric Laurentfa1e1232016-08-02 19:01:49 -0700247 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800248 switch (mState) {
249 case RESTART:
250 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700251 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800252
253 case STARTING:
254 // clear auxiliary effect input buffer for next accumulation
255 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
256 memset(mConfig.inputCfg.buffer.raw,
257 0,
258 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
259 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700260 if (start_l() == NO_ERROR) {
261 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700262 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700263 } else {
264 mState = IDLE;
265 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800266 break;
267 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900268 // volume control for offload and direct threads must take effect immediately.
269 if (stop_l() == NO_ERROR
270 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700271 mDisableWaitCnt = mMaxDisableWaitCnt;
272 } else {
273 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
274 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800275 mState = STOPPED;
276 break;
277 case STOPPED:
278 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
279 // turn off sequence.
280 if (--mDisableWaitCnt == 0) {
281 reset_l();
282 mState = IDLE;
283 }
284 break;
285 default: //IDLE , ACTIVE, DESTROYED
286 break;
287 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700288
289 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800290}
291
292void AudioFlinger::EffectModule::process()
293{
294 Mutex::Autolock _l(mLock);
295
Mikhail Naganov022b9952017-01-04 16:36:51 -0800296 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800297 return;
298 }
299
rago94a1ee82017-07-21 15:11:02 -0700300 const uint32_t inChannelCount =
301 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
302 const uint32_t outChannelCount =
303 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
304 const bool auxType =
305 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
306
Andy Hungfa69ca32017-11-30 10:07:53 -0800307 // safeInputOutputSampleCount is 0 if the channel count between input and output
308 // buffers do not match. This prevents automatic accumulation or copying between the
309 // input and output effect buffers without an intermediary effect process.
310 // TODO: consider implementing channel conversion.
311 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700312 mInChannelCountRequested != mOutChannelCountRequested ? 0
313 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800314 mConfig.inputCfg.buffer.frameCount,
315 mConfig.outputCfg.buffer.frameCount);
316 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
317#ifdef FLOAT_EFFECT_CHAIN
318 accumulate_float(
319 mConfig.outputCfg.buffer.f32,
320 mConfig.inputCfg.buffer.f32,
321 safeInputOutputSampleCount);
322#else
323 accumulate_i16(
324 mConfig.outputCfg.buffer.s16,
325 mConfig.inputCfg.buffer.s16,
326 safeInputOutputSampleCount);
327#endif
328 };
329 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
330#ifdef FLOAT_EFFECT_CHAIN
331 memcpy(
332 mConfig.outputCfg.buffer.f32,
333 mConfig.inputCfg.buffer.f32,
334 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
335
336#else
337 memcpy(
338 mConfig.outputCfg.buffer.s16,
339 mConfig.inputCfg.buffer.s16,
340 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
341#endif
342 };
343
Eric Laurentca7cc822012-11-19 14:55:58 -0800344 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700345 int ret;
346 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700347 if (auxType) {
348 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800349 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700350#ifdef FLOAT_EFFECT_CHAIN
351 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800352#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700353 // Do in-place float conversion for auxiliary effect input buffer.
354 static_assert(sizeof(float) <= sizeof(int32_t),
355 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
356
Andy Hungfa69ca32017-11-30 10:07:53 -0800357 memcpy_to_float_from_q4_27(
358 mConfig.inputCfg.buffer.f32,
359 mConfig.inputCfg.buffer.s32,
360 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800361#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800362 } else
Andy Hung116a4982017-11-30 10:15:08 -0800363#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800364 {
Andy Hung116a4982017-11-30 10:15:08 -0800365#ifdef FLOAT_AUX
366 memcpy_to_i16_from_float(
367 mConfig.inputCfg.buffer.s16,
368 mConfig.inputCfg.buffer.f32,
369 mConfig.inputCfg.buffer.frameCount);
370#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800371 memcpy_to_i16_from_q4_27(
372 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700373 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800374 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800375#endif
rago94a1ee82017-07-21 15:11:02 -0700376 }
rago94a1ee82017-07-21 15:11:02 -0700377 }
378#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800379 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
380 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
381
382 if (!auxType && mInChannelCountRequested != inChannelCount) {
383 adjust_channels(
384 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
385 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
386 sizeof(float),
387 sizeof(float)
388 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
389 inBuffer = mInConversionBuffer;
390 }
391 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
392 && mOutChannelCountRequested != outChannelCount) {
393 adjust_selected_channels(
394 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
395 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
396 sizeof(float),
397 sizeof(float)
398 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
399 outBuffer = mOutConversionBuffer;
400 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800401 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
402 if (!auxType) {
403 if (mInConversionBuffer.get() == nullptr) {
404 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
405 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700406 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800407 memcpy_to_i16_from_float(
408 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800409 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800410 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800411 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700412 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800413 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
414 if (mOutConversionBuffer.get() == nullptr) {
415 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
416 goto data_bypass;
417 }
418 memcpy_to_i16_from_float(
419 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800420 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800421 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800422 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700423 }
424 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800425#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800426 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800427#ifdef FLOAT_EFFECT_CHAIN
428 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800429 sp<EffectBufferHalInterface> target =
430 mOutChannelCountRequested != outChannelCount
431 ? mOutConversionBuffer : mOutBuffer;
432
Andy Hungfa69ca32017-11-30 10:07:53 -0800433 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800434 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800435 mOutConversionBuffer->audioBuffer()->s16,
436 outChannelCount * mConfig.outputCfg.buffer.frameCount);
437 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800438 if (mOutChannelCountRequested != outChannelCount) {
439 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
440 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
441 sizeof(float),
442 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
443 }
rago94a1ee82017-07-21 15:11:02 -0700444#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700445 } else {
rago94a1ee82017-07-21 15:11:02 -0700446#ifdef FLOAT_EFFECT_CHAIN
447 data_bypass:
448#endif
449 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800450 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700451 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800452 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700453 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800454 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700455 }
456 }
457 ret = -ENODATA;
458 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800459
Eric Laurentca7cc822012-11-19 14:55:58 -0800460 // force transition to IDLE state when engine is ready
461 if (mState == STOPPED && ret == -ENODATA) {
462 mDisableWaitCnt = 1;
463 }
464
465 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700466 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800467#ifdef FLOAT_AUX
468 const size_t size =
469 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
470#else
rago94a1ee82017-07-21 15:11:02 -0700471 const size_t size =
472 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800473#endif
rago94a1ee82017-07-21 15:11:02 -0700474 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800475 }
476 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700477 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800478 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
479 // If an insert effect is idle and input buffer is different from output buffer,
480 // accumulate input onto output
481 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800482 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700483 // similar handling with data_bypass above.
484 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
485 accumulateInputToOutput();
486 } else { // EFFECT_BUFFER_ACCESS_WRITE
487 copyInputToOutput();
488 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800489 }
490 }
491}
492
493void AudioFlinger::EffectModule::reset_l()
494{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700495 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800496 return;
497 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700498 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800499}
500
501status_t AudioFlinger::EffectModule::configure()
502{
rago94a1ee82017-07-21 15:11:02 -0700503 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700504 status_t status;
505 sp<ThreadBase> thread;
506 uint32_t size;
507 audio_channel_mask_t channelMask;
508
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700509 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700510 status = NO_INIT;
511 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800512 }
513
Eric Laurentd0ebb532013-04-02 16:41:41 -0700514 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800515 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700516 status = DEAD_OBJECT;
517 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800518 }
519
520 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800521 // TODO: handle configuration of input (record) SW effects above the HAL,
522 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
523 // in which case input channel masks should be used here.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700524 channelMask = thread->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800525 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700526 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800527
528 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800529 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
530 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
531 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
532 mConfig.inputCfg.channels);
533 }
534#ifndef MULTICHANNEL_EFFECT_CHAIN
535 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
536 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
537 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
538 mConfig.outputCfg.channels);
539 }
540#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800541 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800542#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700543 // TODO: Update this logic when multichannel effects are implemented.
544 // For offloaded tracks consider mono output as stereo for proper effect initialization
545 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
546 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
547 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
548 ALOGV("Overriding effect input and output as STEREO");
549 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800550#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800551 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800552 mInChannelCountRequested =
553 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
554 mOutChannelCountRequested =
555 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700556
rago94a1ee82017-07-21 15:11:02 -0700557 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
558 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900559
560 // Don't use sample rate for thread if effect isn't offloadable.
561 if ((thread->type() == ThreadBase::OFFLOAD) && !isOffloaded()) {
562 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
563 ALOGV("Overriding effect input as 48kHz");
564 } else {
565 mConfig.inputCfg.samplingRate = thread->sampleRate();
566 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800567 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
568 mConfig.inputCfg.bufferProvider.cookie = NULL;
569 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
570 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
571 mConfig.outputCfg.bufferProvider.cookie = NULL;
572 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
573 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
574 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
575 // Insert effect:
576 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
577 // always overwrites output buffer: input buffer == output buffer
578 // - in other sessions:
579 // last effect in the chain accumulates in output buffer: input buffer != output buffer
580 // other effect: overwrites output buffer: input buffer == output buffer
581 // Auxiliary effect:
582 // accumulates in output buffer: input buffer != output buffer
583 // Therefore: accumulate <=> input buffer != output buffer
584 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
585 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
586 } else {
587 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
588 }
589 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
590 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
591 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
592 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
593
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700594 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800595 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
596
597 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700598 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700599 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800600 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700601 &mConfig,
602 &size,
603 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700604 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800605 status = cmdStatus;
606 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800607
608#ifdef MULTICHANNEL_EFFECT_CHAIN
609 if (status != NO_ERROR &&
Andy Hunge6a61a52018-04-06 18:55:26 -0700610 thread->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800611 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
612 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
613 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700614 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
615 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800616 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
617 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
618 }
619 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
620 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
621 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
622 }
623 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700624 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800625 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700626 &mConfig,
627 &size,
628 &cmdStatus);
629 if (status == NO_ERROR) {
630 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800631 }
632 }
633#endif
634
635#ifdef FLOAT_EFFECT_CHAIN
636 if (status == NO_ERROR) {
637 mSupportsFloat = true;
638 }
639
640 if (status != NO_ERROR) {
641 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
642 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
643 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
644 size = sizeof(int);
645 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
646 sizeof(mConfig),
647 &mConfig,
648 &size,
649 &cmdStatus);
650 if (status == NO_ERROR) {
651 status = cmdStatus;
652 }
653 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700654 mSupportsFloat = false;
655 ALOGVV("config worked with 16 bit");
656 } else {
657 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800658 }
rago94a1ee82017-07-21 15:11:02 -0700659 }
660#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800661
rago94a1ee82017-07-21 15:11:02 -0700662 if (status == NO_ERROR) {
663 // Establish Buffer strategy
664 setInBuffer(mInBuffer);
665 setOutBuffer(mOutBuffer);
666
667 // Update visualizer latency
668 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
669 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
670 effect_param_t *p = (effect_param_t *)buf32;
671
672 p->psize = sizeof(uint32_t);
673 p->vsize = sizeof(uint32_t);
674 size = sizeof(int);
675 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
676
677 uint32_t latency = 0;
678 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
679 if (pbt != NULL) {
680 latency = pbt->latency_l();
681 }
682
683 *((int32_t *)p->data + 1)= latency;
684 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
685 sizeof(effect_param_t) + 8,
686 &buf32,
687 &size,
688 &cmdStatus);
689 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800690 }
691
Andy Hung05083ac2017-12-14 15:00:28 -0800692 // mConfig.outputCfg.buffer.frameCount cannot be zero.
693 mMaxDisableWaitCnt = (uint32_t)std::max(
694 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
695 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
696 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800697
Eric Laurentd0ebb532013-04-02 16:41:41 -0700698exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800699 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700700 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700701 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800702 return status;
703}
704
705status_t AudioFlinger::EffectModule::init()
706{
707 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700708 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800709 return NO_INIT;
710 }
711 status_t cmdStatus;
712 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700713 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
714 0,
715 NULL,
716 &size,
717 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800718 if (status == 0) {
719 status = cmdStatus;
720 }
721 return status;
722}
723
Eric Laurent1b928682014-10-02 19:41:47 -0700724void AudioFlinger::EffectModule::addEffectToHal_l()
725{
726 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
727 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
728 sp<ThreadBase> thread = mThread.promote();
729 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700730 sp<StreamHalInterface> stream = thread->stream();
731 if (stream != 0) {
732 status_t result = stream->addEffect(mEffectInterface);
733 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700734 }
735 }
736 }
737}
738
Eric Laurentfa1e1232016-08-02 19:01:49 -0700739// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800740status_t AudioFlinger::EffectModule::start()
741{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700742 sp<EffectChain> chain;
743 status_t status;
744 {
745 Mutex::Autolock _l(mLock);
746 status = start_l();
747 if (status == NO_ERROR) {
748 chain = mChain.promote();
749 }
750 }
751 if (chain != 0) {
752 chain->resetVolume_l();
753 }
754 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800755}
756
757status_t AudioFlinger::EffectModule::start_l()
758{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700759 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800760 return NO_INIT;
761 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700762 if (mStatus != NO_ERROR) {
763 return mStatus;
764 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800765 status_t cmdStatus;
766 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700767 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
768 0,
769 NULL,
770 &size,
771 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800772 if (status == 0) {
773 status = cmdStatus;
774 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700775 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700776 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800777 }
778 return status;
779}
780
781status_t AudioFlinger::EffectModule::stop()
782{
783 Mutex::Autolock _l(mLock);
784 return stop_l();
785}
786
787status_t AudioFlinger::EffectModule::stop_l()
788{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700789 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800790 return NO_INIT;
791 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700792 if (mStatus != NO_ERROR) {
793 return mStatus;
794 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800795 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800796 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900797
798 if (isVolumeControl() && isOffloadedOrDirect()) {
799 sp<EffectChain>chain = mChain.promote();
800 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
801 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
802 mSetVolumeReentrantTid = gettid();
803 chain->resetVolume_l();
804 mSetVolumeReentrantTid = INVALID_PID;
805 }
806
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700807 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
808 0,
809 NULL,
810 &size,
811 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800812 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800813 status = cmdStatus;
814 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800815 if (status == NO_ERROR) {
816 status = remove_effect_from_hal_l();
817 }
818 return status;
819}
820
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800821// must be called with EffectChain::mLock held
822void AudioFlinger::EffectModule::release_l()
823{
824 if (mEffectInterface != 0) {
825 remove_effect_from_hal_l();
826 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800827 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800828 mEffectInterface.clear();
829 }
830}
831
Eric Laurentbfb1b832013-01-07 09:53:42 -0800832status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
833{
834 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
835 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800836 sp<ThreadBase> thread = mThread.promote();
837 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700838 sp<StreamHalInterface> stream = thread->stream();
839 if (stream != 0) {
840 status_t result = stream->removeEffect(mEffectInterface);
841 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800842 }
843 }
844 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800845 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800846}
847
Andy Hunge4a1d912016-08-17 14:11:13 -0700848// round up delta valid if value and divisor are positive.
849template <typename T>
850static T roundUpDelta(const T &value, const T &divisor) {
851 T remainder = value % divisor;
852 return remainder == 0 ? 0 : divisor - remainder;
853}
854
Eric Laurentca7cc822012-11-19 14:55:58 -0800855status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
856 uint32_t cmdSize,
857 void *pCmdData,
858 uint32_t *replySize,
859 void *pReplyData)
860{
861 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700862 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800863
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700864 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800865 return NO_INIT;
866 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700867 if (mStatus != NO_ERROR) {
868 return mStatus;
869 }
Andy Hung110bc952016-06-20 15:22:52 -0700870 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700871 (sizeof(effect_param_t) > cmdSize ||
872 ((effect_param_t *)pCmdData)->psize > cmdSize
873 - sizeof(effect_param_t))) {
874 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800875 android_errorWriteLog(0x534e4554, "33003822");
876 return -EINVAL;
877 }
878 if (cmdCode == EFFECT_CMD_GET_PARAM &&
879 (*replySize < sizeof(effect_param_t) ||
880 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
881 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700882 return -EINVAL;
883 }
ragoe2759072016-11-22 18:02:48 -0800884 if (cmdCode == EFFECT_CMD_GET_PARAM &&
885 (sizeof(effect_param_t) > *replySize
886 || ((effect_param_t *)pCmdData)->psize > *replySize
887 - sizeof(effect_param_t)
888 || ((effect_param_t *)pCmdData)->vsize > *replySize
889 - sizeof(effect_param_t)
890 - ((effect_param_t *)pCmdData)->psize
891 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
892 *replySize
893 - sizeof(effect_param_t)
894 - ((effect_param_t *)pCmdData)->psize
895 - ((effect_param_t *)pCmdData)->vsize)) {
896 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
897 android_errorWriteLog(0x534e4554, "32705438");
898 return -EINVAL;
899 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700900 if ((cmdCode == EFFECT_CMD_SET_PARAM
901 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
902 (sizeof(effect_param_t) > cmdSize
903 || ((effect_param_t *)pCmdData)->psize > cmdSize
904 - sizeof(effect_param_t)
905 || ((effect_param_t *)pCmdData)->vsize > cmdSize
906 - sizeof(effect_param_t)
907 - ((effect_param_t *)pCmdData)->psize
908 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
909 cmdSize
910 - sizeof(effect_param_t)
911 - ((effect_param_t *)pCmdData)->psize
912 - ((effect_param_t *)pCmdData)->vsize)) {
913 android_errorWriteLog(0x534e4554, "30204301");
914 return -EINVAL;
915 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700916 status_t status = mEffectInterface->command(cmdCode,
917 cmdSize,
918 pCmdData,
919 replySize,
920 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800921 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
922 uint32_t size = (replySize == NULL) ? 0 : *replySize;
923 for (size_t i = 1; i < mHandles.size(); i++) {
924 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800925 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800926 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
927 }
928 }
929 }
930 return status;
931}
932
933status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
934{
935 Mutex::Autolock _l(mLock);
936 return setEnabled_l(enabled);
937}
938
939// must be called with EffectModule::mLock held
940status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
941{
942
943 ALOGV("setEnabled %p enabled %d", this, enabled);
944
945 if (enabled != isEnabled()) {
946 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
947 if (enabled && status != NO_ERROR) {
948 return status;
949 }
950
951 switch (mState) {
952 // going from disabled to enabled
953 case IDLE:
954 mState = STARTING;
955 break;
956 case STOPPED:
957 mState = RESTART;
958 break;
959 case STOPPING:
960 mState = ACTIVE;
961 break;
962
963 // going from enabled to disabled
964 case RESTART:
965 mState = STOPPED;
966 break;
967 case STARTING:
968 mState = IDLE;
969 break;
970 case ACTIVE:
971 mState = STOPPING;
972 break;
973 case DESTROYED:
974 return NO_ERROR; // simply ignore as we are being destroyed
975 }
976 for (size_t i = 1; i < mHandles.size(); i++) {
977 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800978 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800979 h->setEnabled(enabled);
980 }
981 }
982 }
983 return NO_ERROR;
984}
985
986bool AudioFlinger::EffectModule::isEnabled() const
987{
988 switch (mState) {
989 case RESTART:
990 case STARTING:
991 case ACTIVE:
992 return true;
993 case IDLE:
994 case STOPPING:
995 case STOPPED:
996 case DESTROYED:
997 default:
998 return false;
999 }
1000}
1001
1002bool AudioFlinger::EffectModule::isProcessEnabled() const
1003{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001004 if (mStatus != NO_ERROR) {
1005 return false;
1006 }
1007
Eric Laurentca7cc822012-11-19 14:55:58 -08001008 switch (mState) {
1009 case RESTART:
1010 case ACTIVE:
1011 case STOPPING:
1012 case STOPPED:
1013 return true;
1014 case IDLE:
1015 case STARTING:
1016 case DESTROYED:
1017 default:
1018 return false;
1019 }
1020}
1021
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001022bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1023{
1024 return (mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT);
1025}
1026
1027bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1028{
1029 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1030}
1031
Mikhail Naganov022b9952017-01-04 16:36:51 -08001032void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001033 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001034
1035 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001036 if (buffer != 0) {
1037 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1038 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1039 } else {
1040 mConfig.inputCfg.buffer.raw = NULL;
1041 }
1042 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001043 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001044
1045#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001046 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001047 // Theoretically insert effects can also do in-place conversions (destroying
1048 // the original buffer) when the output buffer is identical to the input buffer,
1049 // but we don't optimize for it here.
1050 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001051 const uint32_t inChannelCount =
1052 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1053 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1054 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001055 // we need to translate - create hidl shared buffer and intercept
1056 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001057 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1058 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1059 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001060
1061 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1062 __func__, inChannels, inFrameCount, size);
1063
Andy Hungbded9c82017-11-30 18:47:35 -08001064 if (size > 0 && (mInConversionBuffer.get() == nullptr
1065 || size > mInConversionBuffer->getSize())) {
1066 mInConversionBuffer.clear();
1067 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001068 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1069 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1070 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001071 }
Andy Hungbded9c82017-11-30 18:47:35 -08001072 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001073 mInConversionBuffer->setFrameCount(inFrameCount);
1074 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001075 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001076 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001077 }
1078 }
1079#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001080}
1081
1082void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001083 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001084
1085 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001086 if (buffer != 0) {
1087 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1088 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1089 } else {
1090 mConfig.outputCfg.buffer.raw = NULL;
1091 }
1092 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001093 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001094
1095#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001096 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001097 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001098 const uint32_t outChannelCount =
1099 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1100 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1101 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001102 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001103 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1104 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1105 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001106
1107 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1108 __func__, outChannels, outFrameCount, size);
1109
Andy Hungbded9c82017-11-30 18:47:35 -08001110 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1111 || size > mOutConversionBuffer->getSize())) {
1112 mOutConversionBuffer.clear();
1113 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001114 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1115 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1116 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001117 }
Andy Hungbded9c82017-11-30 18:47:35 -08001118 if (mOutConversionBuffer.get() != nullptr) {
1119 mOutConversionBuffer->setFrameCount(outFrameCount);
1120 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001121 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001122 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001123 }
1124 }
1125#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001126}
1127
Eric Laurentca7cc822012-11-19 14:55:58 -08001128status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1129{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001130 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001131 if (mStatus != NO_ERROR) {
1132 return mStatus;
1133 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001134 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001135 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1136 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1137 if (isProcessEnabled() &&
1138 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001139 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1140 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001141 uint32_t volume[2];
1142 uint32_t *pVolume = NULL;
1143 uint32_t size = sizeof(volume);
1144 volume[0] = *left;
1145 volume[1] = *right;
1146 if (controller) {
1147 pVolume = volume;
1148 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001149 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1150 size,
1151 volume,
1152 &size,
1153 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001154 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1155 *left = volume[0];
1156 *right = volume[1];
1157 }
1158 }
1159 return status;
1160}
1161
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001162void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1163{
1164 sp<ThreadBase> thread = mThread.promote();
1165 if (thread != 0 &&
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09001166 (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::DIRECT) &&
1167 !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001168 PlaybackThread *t = (PlaybackThread *)thread.get();
1169 float vol_l = (float)left / (1 << 24);
1170 float vol_r = (float)right / (1 << 24);
1171 t->setVolumeForOutput_l(vol_l, vol_r);
1172 }
1173}
1174
Eric Laurentca7cc822012-11-19 14:55:58 -08001175status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1176{
1177 if (device == AUDIO_DEVICE_NONE) {
1178 return NO_ERROR;
1179 }
1180
1181 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001182 if (mStatus != NO_ERROR) {
1183 return mStatus;
1184 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001185 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001186 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001187 status_t cmdStatus;
1188 uint32_t size = sizeof(status_t);
1189 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1190 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001191 status = mEffectInterface->command(cmd,
1192 sizeof(uint32_t),
1193 &device,
1194 &size,
1195 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001196 }
1197 return status;
1198}
1199
1200status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1201{
1202 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001203 if (mStatus != NO_ERROR) {
1204 return mStatus;
1205 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001206 status_t status = NO_ERROR;
1207 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1208 status_t cmdStatus;
1209 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001210 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1211 sizeof(audio_mode_t),
1212 &mode,
1213 &size,
1214 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001215 if (status == NO_ERROR) {
1216 status = cmdStatus;
1217 }
1218 }
1219 return status;
1220}
1221
1222status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1223{
1224 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001225 if (mStatus != NO_ERROR) {
1226 return mStatus;
1227 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001228 status_t status = NO_ERROR;
1229 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1230 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001231 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1232 sizeof(audio_source_t),
1233 &source,
1234 &size,
1235 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001236 }
1237 return status;
1238}
1239
1240void AudioFlinger::EffectModule::setSuspended(bool suspended)
1241{
1242 Mutex::Autolock _l(mLock);
1243 mSuspended = suspended;
1244}
1245
1246bool AudioFlinger::EffectModule::suspended() const
1247{
1248 Mutex::Autolock _l(mLock);
1249 return mSuspended;
1250}
1251
1252bool AudioFlinger::EffectModule::purgeHandles()
1253{
1254 bool enabled = false;
1255 Mutex::Autolock _l(mLock);
1256 for (size_t i = 0; i < mHandles.size(); i++) {
1257 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001258 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001259 if (handle->hasControl()) {
1260 enabled = handle->enabled();
1261 }
1262 }
1263 }
1264 return enabled;
1265}
1266
Eric Laurent5baf2af2013-09-12 17:37:00 -07001267status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1268{
1269 Mutex::Autolock _l(mLock);
1270 if (mStatus != NO_ERROR) {
1271 return mStatus;
1272 }
1273 status_t status = NO_ERROR;
1274 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1275 status_t cmdStatus;
1276 uint32_t size = sizeof(status_t);
1277 effect_offload_param_t cmd;
1278
1279 cmd.isOffload = offloaded;
1280 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001281 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1282 sizeof(effect_offload_param_t),
1283 &cmd,
1284 &size,
1285 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001286 if (status == NO_ERROR) {
1287 status = cmdStatus;
1288 }
1289 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1290 } else {
1291 if (offloaded) {
1292 status = INVALID_OPERATION;
1293 }
1294 mOffloaded = false;
1295 }
1296 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1297 return status;
1298}
1299
1300bool AudioFlinger::EffectModule::isOffloaded() const
1301{
1302 Mutex::Autolock _l(mLock);
1303 return mOffloaded;
1304}
1305
Marco Nelissenb2208842014-02-07 14:00:50 -08001306String8 effectFlagsToString(uint32_t flags) {
1307 String8 s;
1308
1309 s.append("conn. mode: ");
1310 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1311 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1312 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1313 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1314 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1315 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1316 default: s.append("unknown/reserved"); break;
1317 }
1318 s.append(", ");
1319
1320 s.append("insert pref: ");
1321 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1322 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1323 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1324 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1325 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1326 default: s.append("unknown/reserved"); break;
1327 }
1328 s.append(", ");
1329
1330 s.append("volume mgmt: ");
1331 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1332 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1333 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1334 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001335 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
Marco Nelissenb2208842014-02-07 14:00:50 -08001336 default: s.append("unknown/reserved"); break;
1337 }
1338 s.append(", ");
1339
1340 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1341 if (devind) {
1342 s.append("device indication: ");
1343 switch (devind) {
1344 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1345 default: s.append("unknown/reserved"); break;
1346 }
1347 s.append(", ");
1348 }
1349
1350 s.append("input mode: ");
1351 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1352 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1353 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1354 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1355 default: s.append("not set"); break;
1356 }
1357 s.append(", ");
1358
1359 s.append("output mode: ");
1360 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1361 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1362 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1363 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1364 default: s.append("not set"); break;
1365 }
1366 s.append(", ");
1367
1368 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1369 if (accel) {
1370 s.append("hardware acceleration: ");
1371 switch (accel) {
1372 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1373 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1374 default: s.append("unknown/reserved"); break;
1375 }
1376 s.append(", ");
1377 }
1378
1379 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1380 if (modeind) {
1381 s.append("mode indication: ");
1382 switch (modeind) {
1383 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1384 default: s.append("unknown/reserved"); break;
1385 }
1386 s.append(", ");
1387 }
1388
1389 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1390 if (srcind) {
1391 s.append("source indication: ");
1392 switch (srcind) {
1393 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1394 default: s.append("unknown/reserved"); break;
1395 }
1396 s.append(", ");
1397 }
1398
1399 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1400 s.append("offloadable, ");
1401 }
1402
1403 int len = s.length();
1404 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001405 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001406 s.unlockBuffer(len - 2);
1407 }
1408 return s;
1409}
1410
Andy Hungbded9c82017-11-30 18:47:35 -08001411static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1412 std::stringstream ss;
1413
1414 if (buffer.get() == nullptr) {
1415 return "nullptr"; // make different than below
1416 } else if (buffer->externalData() != nullptr) {
1417 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1418 << " -> "
1419 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1420 } else {
1421 ss << buffer->audioBuffer()->raw;
1422 }
1423 return ss.str();
1424}
Marco Nelissenb2208842014-02-07 14:00:50 -08001425
Glenn Kasten0f11b512014-01-31 16:18:54 -08001426void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001427{
Eric Laurentca7cc822012-11-19 14:55:58 -08001428 String8 result;
1429
Andy Hung9718d662017-12-22 17:57:39 -08001430 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001431
1432 bool locked = AudioFlinger::dumpTryLock(mLock);
1433 // failed to lock - AudioFlinger is probably deadlocked
1434 if (!locked) {
1435 result.append("\t\tCould not lock Fx mutex:\n");
1436 }
1437
1438 result.append("\t\tSession Status State Engine:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001439 result.appendFormat("\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001440 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001441
1442 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001443 char uuidStr[64];
1444 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001445 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001446 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001447 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1448 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001449 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001450 mDescriptor.flags,
1451 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001452 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001453 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001454
1455 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001456 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001457
1458 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001459
1460 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001461 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1462 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1463 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001464 mConfig.inputCfg.buffer.frameCount,
1465 mConfig.inputCfg.samplingRate,
1466 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001467 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001468 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001469
1470 result.append("\t\t- Output configuration:\n");
1471 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001472 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001473 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001474 mConfig.outputCfg.buffer.frameCount,
1475 mConfig.outputCfg.samplingRate,
1476 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001477 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001478 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001479
rago94a1ee82017-07-21 15:11:02 -07001480#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001481
Andy Hungbded9c82017-11-30 18:47:35 -08001482 result.appendFormat("\t\t- HAL buffers:\n"
1483 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1484 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1485 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1486 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1487 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001488#endif
1489
Andy Hung9718d662017-12-22 17:57:39 -08001490 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001491 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001492 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001493 for (size_t i = 0; i < mHandles.size(); ++i) {
1494 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001495 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001496 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001497 result.append(buffer);
1498 }
1499 }
1500
Eric Laurentca7cc822012-11-19 14:55:58 -08001501 write(fd, result.string(), result.length());
1502
Mikhail Naganov4d547672019-02-22 14:19:19 -08001503 if (mEffectInterface != 0) {
1504 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1505 (void)mEffectInterface->dump(fd);
1506 }
1507
Eric Laurentca7cc822012-11-19 14:55:58 -08001508 if (locked) {
1509 mLock.unlock();
1510 }
1511}
1512
1513// ----------------------------------------------------------------------------
1514// EffectHandle implementation
1515// ----------------------------------------------------------------------------
1516
1517#undef LOG_TAG
1518#define LOG_TAG "AudioFlinger::EffectHandle"
1519
1520AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1521 const sp<AudioFlinger::Client>& client,
1522 const sp<IEffectClient>& effectClient,
1523 int32_t priority)
1524 : BnEffect(),
1525 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001526 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001527{
1528 ALOGV("constructor %p", this);
1529
1530 if (client == 0) {
1531 return;
1532 }
1533 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1534 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001535 if (mCblkMemory == 0 ||
1536 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001537 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001538 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001539 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001540 return;
1541 }
Glenn Kastene75da402013-11-20 13:54:52 -08001542 new(mCblk) effect_param_cblk_t();
1543 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001544}
1545
1546AudioFlinger::EffectHandle::~EffectHandle()
1547{
1548 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001549 disconnect(false);
1550}
1551
Glenn Kastene75da402013-11-20 13:54:52 -08001552status_t AudioFlinger::EffectHandle::initCheck()
1553{
1554 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1555}
1556
Eric Laurentca7cc822012-11-19 14:55:58 -08001557status_t AudioFlinger::EffectHandle::enable()
1558{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001559 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001560 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001561 sp<EffectModule> effect = mEffect.promote();
1562 if (effect == 0 || mDisconnected) {
1563 return DEAD_OBJECT;
1564 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001565 if (!mHasControl) {
1566 return INVALID_OPERATION;
1567 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001568
1569 if (mEnabled) {
1570 return NO_ERROR;
1571 }
1572
1573 mEnabled = true;
1574
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001575 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001576 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001577 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001578 }
1579
1580 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001581 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001582 return NO_ERROR;
1583 }
1584
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001585 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001586 if (status != NO_ERROR) {
1587 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001588 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001589 }
1590 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001591 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001592 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001593 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1594 Mutex::Autolock _l(thread->mLock);
1595 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001596 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001597 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001598 if (thread->type() == ThreadBase::OFFLOAD) {
1599 PlaybackThread *t = (PlaybackThread *)thread.get();
1600 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1601 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001602 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001603 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1604 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001605 }
1606 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001607 }
1608 return status;
1609}
1610
1611status_t AudioFlinger::EffectHandle::disable()
1612{
1613 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001614 AutoMutex _l(mLock);
1615 sp<EffectModule> effect = mEffect.promote();
1616 if (effect == 0 || mDisconnected) {
1617 return DEAD_OBJECT;
1618 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001619 if (!mHasControl) {
1620 return INVALID_OPERATION;
1621 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001622
1623 if (!mEnabled) {
1624 return NO_ERROR;
1625 }
1626 mEnabled = false;
1627
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001628 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001629 return NO_ERROR;
1630 }
1631
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001632 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001633
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001634 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001635 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001636 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001637 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1638 Mutex::Autolock _l(thread->mLock);
1639 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001640 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001641 }
1642
1643 return status;
1644}
1645
1646void AudioFlinger::EffectHandle::disconnect()
1647{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001648 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001649 disconnect(true);
1650}
1651
1652void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1653{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001654 AutoMutex _l(mLock);
1655 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1656 if (mDisconnected) {
1657 if (unpinIfLast) {
1658 android_errorWriteLog(0x534e4554, "32707507");
1659 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001660 return;
1661 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001662 mDisconnected = true;
1663 sp<ThreadBase> thread;
1664 {
1665 sp<EffectModule> effect = mEffect.promote();
1666 if (effect != 0) {
1667 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001668 }
1669 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001670 if (thread != 0) {
1671 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001672 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001673 // try to cleanup as much as we can
1674 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001675 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1676 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001677 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001678 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001679
Eric Laurentca7cc822012-11-19 14:55:58 -08001680 if (mClient != 0) {
1681 if (mCblk != NULL) {
1682 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1683 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1684 }
1685 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001686 // Client destructor must run with AudioFlinger client mutex locked
1687 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001688 mClient.clear();
1689 }
1690}
1691
1692status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1693 uint32_t cmdSize,
1694 void *pCmdData,
1695 uint32_t *replySize,
1696 void *pReplyData)
1697{
1698 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001699 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001700
Eric Laurentc7ab3092017-06-15 18:43:46 -07001701 // reject commands reserved for internal use by audio framework if coming from outside
1702 // of audioserver
1703 switch(cmdCode) {
1704 case EFFECT_CMD_ENABLE:
1705 case EFFECT_CMD_DISABLE:
1706 case EFFECT_CMD_SET_PARAM:
1707 case EFFECT_CMD_SET_PARAM_DEFERRED:
1708 case EFFECT_CMD_SET_PARAM_COMMIT:
1709 case EFFECT_CMD_GET_PARAM:
1710 break;
1711 default:
1712 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1713 break;
1714 }
1715 android_errorWriteLog(0x534e4554, "62019992");
1716 return BAD_VALUE;
1717 }
1718
Eric Laurent1ffc5852016-12-15 14:46:09 -08001719 if (cmdCode == EFFECT_CMD_ENABLE) {
1720 if (*replySize < sizeof(int)) {
1721 android_errorWriteLog(0x534e4554, "32095713");
1722 return BAD_VALUE;
1723 }
1724 *(int *)pReplyData = NO_ERROR;
1725 *replySize = sizeof(int);
1726 return enable();
1727 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1728 if (*replySize < sizeof(int)) {
1729 android_errorWriteLog(0x534e4554, "32095713");
1730 return BAD_VALUE;
1731 }
1732 *(int *)pReplyData = NO_ERROR;
1733 *replySize = sizeof(int);
1734 return disable();
1735 }
1736
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001737 AutoMutex _l(mLock);
1738 sp<EffectModule> effect = mEffect.promote();
1739 if (effect == 0 || mDisconnected) {
1740 return DEAD_OBJECT;
1741 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001742 // only get parameter command is permitted for applications not controlling the effect
1743 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1744 return INVALID_OPERATION;
1745 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001746 if (mClient == 0) {
1747 return INVALID_OPERATION;
1748 }
1749
1750 // handle commands that are not forwarded transparently to effect engine
1751 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001752 if (*replySize < sizeof(int)) {
1753 android_errorWriteLog(0x534e4554, "32095713");
1754 return BAD_VALUE;
1755 }
1756 *(int *)pReplyData = NO_ERROR;
1757 *replySize = sizeof(int);
1758
Eric Laurentca7cc822012-11-19 14:55:58 -08001759 // No need to trylock() here as this function is executed in the binder thread serving a
1760 // particular client process: no risk to block the whole media server process or mixer
1761 // threads if we are stuck here
1762 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001763 // keep local copy of index in case of client corruption b/32220769
1764 const uint32_t clientIndex = mCblk->clientIndex;
1765 const uint32_t serverIndex = mCblk->serverIndex;
1766 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1767 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001768 mCblk->serverIndex = 0;
1769 mCblk->clientIndex = 0;
1770 return BAD_VALUE;
1771 }
1772 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001773 effect_param_t *param = NULL;
1774 for (uint32_t index = serverIndex; index < clientIndex;) {
1775 int *p = (int *)(mBuffer + index);
1776 const int size = *p++;
1777 if (size < 0
1778 || size > EFFECT_PARAM_BUFFER_SIZE
1779 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001780 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001781 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001782 break;
1783 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001784
1785 // copy to local memory in case of client corruption b/32220769
1786 param = (effect_param_t *)realloc(param, size);
1787 if (param == NULL) {
1788 ALOGW("command(): out of memory");
1789 status = NO_MEMORY;
1790 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001791 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001792 memcpy(param, p, size);
1793
1794 int reply = 0;
1795 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001796 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001797 size,
1798 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001799 &rsize,
1800 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001801
1802 // verify shared memory: server index shouldn't change; client index can't go back.
1803 if (serverIndex != mCblk->serverIndex
1804 || clientIndex > mCblk->clientIndex) {
1805 android_errorWriteLog(0x534e4554, "32220769");
1806 status = BAD_VALUE;
1807 break;
1808 }
1809
Eric Laurentca7cc822012-11-19 14:55:58 -08001810 // stop at first error encountered
1811 if (ret != NO_ERROR) {
1812 status = ret;
1813 *(int *)pReplyData = reply;
1814 break;
1815 } else if (reply != NO_ERROR) {
1816 *(int *)pReplyData = reply;
1817 break;
1818 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001819 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001820 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001821 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001822 mCblk->serverIndex = 0;
1823 mCblk->clientIndex = 0;
1824 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001825 }
1826
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001827 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001828}
1829
1830void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1831{
1832 ALOGV("setControl %p control %d", this, hasControl);
1833
1834 mHasControl = hasControl;
1835 mEnabled = enabled;
1836
1837 if (signal && mEffectClient != 0) {
1838 mEffectClient->controlStatusChanged(hasControl);
1839 }
1840}
1841
1842void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1843 uint32_t cmdSize,
1844 void *pCmdData,
1845 uint32_t replySize,
1846 void *pReplyData)
1847{
1848 if (mEffectClient != 0) {
1849 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1850 }
1851}
1852
1853
1854
1855void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1856{
1857 if (mEffectClient != 0) {
1858 mEffectClient->enableStatusChanged(enabled);
1859 }
1860}
1861
1862status_t AudioFlinger::EffectHandle::onTransact(
1863 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1864{
1865 return BnEffect::onTransact(code, data, reply, flags);
1866}
1867
1868
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001869void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001870{
1871 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1872
Marco Nelissenb2208842014-02-07 14:00:50 -08001873 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001874 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001875 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001876 mHasControl ? "yes" : "no",
1877 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001878 mCblk ? mCblk->clientIndex : 0,
1879 mCblk ? mCblk->serverIndex : 0
1880 );
1881
1882 if (locked) {
1883 mCblk->lock.unlock();
1884 }
1885}
1886
1887#undef LOG_TAG
1888#define LOG_TAG "AudioFlinger::EffectChain"
1889
1890AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001891 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001892 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001893 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001894 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001895{
1896 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1897 if (thread == NULL) {
1898 return;
1899 }
1900 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1901 thread->frameCount();
1902}
1903
1904AudioFlinger::EffectChain::~EffectChain()
1905{
Eric Laurentca7cc822012-11-19 14:55:58 -08001906}
1907
1908// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1909sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1910 effect_descriptor_t *descriptor)
1911{
1912 size_t size = mEffects.size();
1913
1914 for (size_t i = 0; i < size; i++) {
1915 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1916 return mEffects[i];
1917 }
1918 }
1919 return 0;
1920}
1921
1922// getEffectFromId_l() must be called with ThreadBase::mLock held
1923sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1924{
1925 size_t size = mEffects.size();
1926
1927 for (size_t i = 0; i < size; i++) {
1928 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1929 if (id == 0 || mEffects[i]->id() == id) {
1930 return mEffects[i];
1931 }
1932 }
1933 return 0;
1934}
1935
1936// getEffectFromType_l() must be called with ThreadBase::mLock held
1937sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1938 const effect_uuid_t *type)
1939{
1940 size_t size = mEffects.size();
1941
1942 for (size_t i = 0; i < size; i++) {
1943 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1944 return mEffects[i];
1945 }
1946 }
1947 return 0;
1948}
1949
1950void AudioFlinger::EffectChain::clearInputBuffer()
1951{
1952 Mutex::Autolock _l(mLock);
1953 sp<ThreadBase> thread = mThread.promote();
1954 if (thread == 0) {
1955 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1956 return;
1957 }
1958 clearInputBuffer_l(thread);
1959}
1960
1961// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001962void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001963{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001964 if (mInBuffer == NULL) {
1965 return;
1966 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001967 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08001968 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07001969
Mikhail Naganov022b9952017-01-04 16:36:51 -08001970 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1971 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001972}
1973
1974// Must be called with EffectChain::mLock locked
1975void AudioFlinger::EffectChain::process_l()
1976{
1977 sp<ThreadBase> thread = mThread.promote();
1978 if (thread == 0) {
1979 ALOGW("process_l(): cannot promote mixer thread");
1980 return;
1981 }
1982 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1983 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001984 // never process effects when:
1985 // - on an OFFLOAD thread
1986 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001987 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1988 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001989 if (!isGlobalSession) {
1990 bool tracksOnSession = (trackCnt() != 0);
1991
1992 if (!tracksOnSession && mTailBufferCount == 0) {
1993 doProcess = false;
1994 }
1995
1996 if (activeTrackCnt() == 0) {
1997 // if no track is active and the effect tail has not been rendered,
1998 // the input buffer must be cleared here as the mixer process will not do it
1999 if (tracksOnSession || mTailBufferCount > 0) {
2000 clearInputBuffer_l(thread);
2001 if (mTailBufferCount > 0) {
2002 mTailBufferCount--;
2003 }
2004 }
2005 }
2006 }
2007
2008 size_t size = mEffects.size();
2009 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002010 // Only the input and output buffers of the chain can be external,
2011 // and 'update' / 'commit' do nothing for allocated buffers, thus
2012 // it's not needed to consider any other buffers here.
2013 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002014 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2015 mOutBuffer->update();
2016 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002017 for (size_t i = 0; i < size; i++) {
2018 mEffects[i]->process();
2019 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002020 mInBuffer->commit();
2021 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2022 mOutBuffer->commit();
2023 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002024 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002025 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002026 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002027 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2028 }
2029 if (doResetVolume) {
2030 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002031 }
2032}
2033
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002034// createEffect_l() must be called with ThreadBase::mLock held
2035status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
2036 ThreadBase *thread,
2037 effect_descriptor_t *desc,
2038 int id,
2039 audio_session_t sessionId,
2040 bool pinned)
2041{
2042 Mutex::Autolock _l(mLock);
2043 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
2044 status_t lStatus = effect->status();
2045 if (lStatus == NO_ERROR) {
2046 lStatus = addEffect_ll(effect);
2047 }
2048 if (lStatus != NO_ERROR) {
2049 effect.clear();
2050 }
2051 return lStatus;
2052}
2053
2054// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002055status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2056{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002057 Mutex::Autolock _l(mLock);
2058 return addEffect_ll(effect);
2059}
2060// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2061status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2062{
Eric Laurentca7cc822012-11-19 14:55:58 -08002063 effect_descriptor_t desc = effect->desc();
2064 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2065
Eric Laurentca7cc822012-11-19 14:55:58 -08002066 effect->setChain(this);
2067 sp<ThreadBase> thread = mThread.promote();
2068 if (thread == 0) {
2069 return NO_INIT;
2070 }
2071 effect->setThread(thread);
2072
2073 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2074 // Auxiliary effects are inserted at the beginning of mEffects vector as
2075 // they are processed first and accumulated in chain input buffer
2076 mEffects.insertAt(effect, 0);
2077
2078 // the input buffer for auxiliary effect contains mono samples in
2079 // 32 bit format. This is to avoid saturation in AudoMixer
2080 // accumulation stage. Saturation is done in EffectModule::process() before
2081 // calling the process in effect engine
2082 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002083 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002084#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002085 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002086 numSamples * sizeof(float), &halBuffer);
2087#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002088 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002089 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002090#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002091 if (result != OK) return result;
2092 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002093 // auxiliary effects output samples to chain input buffer for further processing
2094 // by insert effects
2095 effect->setOutBuffer(mInBuffer);
2096 } else {
2097 // Insert effects are inserted at the end of mEffects vector as they are processed
2098 // after track and auxiliary effects.
2099 // Insert effect order as a function of indicated preference:
2100 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2101 // another effect is present
2102 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2103 // last effect claiming first position
2104 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2105 // first effect claiming last position
2106 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2107 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2108 // already present
2109
2110 size_t size = mEffects.size();
2111 size_t idx_insert = size;
2112 ssize_t idx_insert_first = -1;
2113 ssize_t idx_insert_last = -1;
2114
2115 for (size_t i = 0; i < size; i++) {
2116 effect_descriptor_t d = mEffects[i]->desc();
2117 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2118 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2119 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2120 // check invalid effect chaining combinations
2121 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2122 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2123 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2124 desc.name, d.name);
2125 return INVALID_OPERATION;
2126 }
2127 // remember position of first insert effect and by default
2128 // select this as insert position for new effect
2129 if (idx_insert == size) {
2130 idx_insert = i;
2131 }
2132 // remember position of last insert effect claiming
2133 // first position
2134 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2135 idx_insert_first = i;
2136 }
2137 // remember position of first insert effect claiming
2138 // last position
2139 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2140 idx_insert_last == -1) {
2141 idx_insert_last = i;
2142 }
2143 }
2144 }
2145
2146 // modify idx_insert from first position if needed
2147 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2148 if (idx_insert_last != -1) {
2149 idx_insert = idx_insert_last;
2150 } else {
2151 idx_insert = size;
2152 }
2153 } else {
2154 if (idx_insert_first != -1) {
2155 idx_insert = idx_insert_first + 1;
2156 }
2157 }
2158
2159 // always read samples from chain input buffer
2160 effect->setInBuffer(mInBuffer);
2161
2162 // if last effect in the chain, output samples to chain
2163 // output buffer, otherwise to chain input buffer
2164 if (idx_insert == size) {
2165 if (idx_insert != 0) {
2166 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2167 mEffects[idx_insert-1]->configure();
2168 }
2169 effect->setOutBuffer(mOutBuffer);
2170 } else {
2171 effect->setOutBuffer(mInBuffer);
2172 }
2173 mEffects.insertAt(effect, idx_insert);
2174
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002175 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002176 idx_insert);
2177 }
2178 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002179
Eric Laurentca7cc822012-11-19 14:55:58 -08002180 return NO_ERROR;
2181}
2182
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002183// removeEffect_l() must be called with ThreadBase::mLock held
2184size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2185 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002186{
2187 Mutex::Autolock _l(mLock);
2188 size_t size = mEffects.size();
2189 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2190
2191 for (size_t i = 0; i < size; i++) {
2192 if (effect == mEffects[i]) {
2193 // calling stop here will remove pre-processing effect from the audio HAL.
2194 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2195 // the middle of a read from audio HAL
2196 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2197 mEffects[i]->state() == EffectModule::STOPPING) {
2198 mEffects[i]->stop();
2199 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002200 if (release) {
2201 mEffects[i]->release_l();
2202 }
2203
Mikhail Naganov022b9952017-01-04 16:36:51 -08002204 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002205 if (i == size - 1 && i != 0) {
2206 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2207 mEffects[i - 1]->configure();
2208 }
2209 }
2210 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002211 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002212 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002213
Eric Laurentca7cc822012-11-19 14:55:58 -08002214 break;
2215 }
2216 }
2217
2218 return mEffects.size();
2219}
2220
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002221// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002222void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2223{
2224 size_t size = mEffects.size();
2225 for (size_t i = 0; i < size; i++) {
2226 mEffects[i]->setDevice(device);
2227 }
2228}
2229
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002230// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002231void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2232{
2233 size_t size = mEffects.size();
2234 for (size_t i = 0; i < size; i++) {
2235 mEffects[i]->setMode(mode);
2236 }
2237}
2238
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002239// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002240void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2241{
2242 size_t size = mEffects.size();
2243 for (size_t i = 0; i < size; i++) {
2244 mEffects[i]->setAudioSource(source);
2245 }
2246}
2247
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002248// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002249bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002250{
2251 uint32_t newLeft = *left;
2252 uint32_t newRight = *right;
2253 bool hasControl = false;
2254 int ctrlIdx = -1;
2255 size_t size = mEffects.size();
2256
2257 // first update volume controller
2258 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002259 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002260 ctrlIdx = i - 1;
2261 hasControl = true;
2262 break;
2263 }
2264 }
2265
Eric Laurentfa1e1232016-08-02 19:01:49 -07002266 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002267 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002268 if (hasControl) {
2269 *left = mNewLeftVolume;
2270 *right = mNewRightVolume;
2271 }
2272 return hasControl;
2273 }
2274
2275 mVolumeCtrlIdx = ctrlIdx;
2276 mLeftVolume = newLeft;
2277 mRightVolume = newRight;
2278
2279 // second get volume update from volume controller
2280 if (ctrlIdx >= 0) {
2281 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2282 mNewLeftVolume = newLeft;
2283 mNewRightVolume = newRight;
2284 }
2285 // then indicate volume to all other effects in chain.
2286 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002287 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002288 uint32_t lVol = newLeft;
2289 uint32_t rVol = newRight;
2290
2291 for (size_t i = 0; i < size; i++) {
2292 if ((int)i == ctrlIdx) {
2293 continue;
2294 }
2295 // this also works for ctrlIdx == -1 when there is no volume controller
2296 if ((int)i > ctrlIdx) {
2297 lVol = *left;
2298 rVol = *right;
2299 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002300 // Pass requested volume directly if this is volume monitor module
2301 if (mEffects[i]->isVolumeMonitor()) {
2302 mEffects[i]->setVolume(left, right, false);
2303 } else {
2304 mEffects[i]->setVolume(&lVol, &rVol, false);
2305 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002306 }
2307 *left = newLeft;
2308 *right = newRight;
2309
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002310 setVolumeForOutput_l(*left, *right);
2311
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 return hasControl;
2313}
2314
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002315// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002316void AudioFlinger::EffectChain::resetVolume_l()
2317{
Eric Laurente7449bf2016-08-03 18:44:07 -07002318 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2319 uint32_t left = mLeftVolume;
2320 uint32_t right = mRightVolume;
2321 (void)setVolume_l(&left, &right, true);
2322 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002323}
2324
Eric Laurent1b928682014-10-02 19:41:47 -07002325void AudioFlinger::EffectChain::syncHalEffectsState()
2326{
2327 Mutex::Autolock _l(mLock);
2328 for (size_t i = 0; i < mEffects.size(); i++) {
2329 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2330 mEffects[i]->state() == EffectModule::STOPPING) {
2331 mEffects[i]->addEffectToHal_l();
2332 }
2333 }
2334}
2335
Eric Laurentca7cc822012-11-19 14:55:58 -08002336void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2337{
Eric Laurentca7cc822012-11-19 14:55:58 -08002338 String8 result;
2339
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002340 const size_t numEffects = mEffects.size();
2341 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002342
Marco Nelissenb2208842014-02-07 14:00:50 -08002343 if (numEffects) {
2344 bool locked = AudioFlinger::dumpTryLock(mLock);
2345 // failed to lock - AudioFlinger is probably deadlocked
2346 if (!locked) {
2347 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002348 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002349
Andy Hungbded9c82017-11-30 18:47:35 -08002350 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2351 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2352 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2353 (int)inBufferStr.size(), "In buffer ",
2354 (int)outBufferStr.size(), "Out buffer ");
2355 result.appendFormat("\t%s %s %d\n",
2356 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002357 write(fd, result.string(), result.size());
2358
2359 for (size_t i = 0; i < numEffects; ++i) {
2360 sp<EffectModule> effect = mEffects[i];
2361 if (effect != 0) {
2362 effect->dump(fd, args);
2363 }
2364 }
2365
2366 if (locked) {
2367 mLock.unlock();
2368 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002369 } else {
2370 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002371 }
2372}
2373
2374// must be called with ThreadBase::mLock held
2375void AudioFlinger::EffectChain::setEffectSuspended_l(
2376 const effect_uuid_t *type, bool suspend)
2377{
2378 sp<SuspendedEffectDesc> desc;
2379 // use effect type UUID timelow as key as there is no real risk of identical
2380 // timeLow fields among effect type UUIDs.
2381 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2382 if (suspend) {
2383 if (index >= 0) {
2384 desc = mSuspendedEffects.valueAt(index);
2385 } else {
2386 desc = new SuspendedEffectDesc();
2387 desc->mType = *type;
2388 mSuspendedEffects.add(type->timeLow, desc);
2389 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2390 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002391
Eric Laurentca7cc822012-11-19 14:55:58 -08002392 if (desc->mRefCount++ == 0) {
2393 sp<EffectModule> effect = getEffectIfEnabled(type);
2394 if (effect != 0) {
2395 desc->mEffect = effect;
2396 effect->setSuspended(true);
2397 effect->setEnabled(false);
2398 }
2399 }
2400 } else {
2401 if (index < 0) {
2402 return;
2403 }
2404 desc = mSuspendedEffects.valueAt(index);
2405 if (desc->mRefCount <= 0) {
2406 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002407 desc->mRefCount = 0;
2408 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002409 }
2410 if (--desc->mRefCount == 0) {
2411 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2412 if (desc->mEffect != 0) {
2413 sp<EffectModule> effect = desc->mEffect.promote();
2414 if (effect != 0) {
2415 effect->setSuspended(false);
2416 effect->lock();
2417 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002418 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002419 effect->setEnabled_l(handle->enabled());
2420 }
2421 effect->unlock();
2422 }
2423 desc->mEffect.clear();
2424 }
2425 mSuspendedEffects.removeItemsAt(index);
2426 }
2427 }
2428}
2429
2430// must be called with ThreadBase::mLock held
2431void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2432{
2433 sp<SuspendedEffectDesc> desc;
2434
2435 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2436 if (suspend) {
2437 if (index >= 0) {
2438 desc = mSuspendedEffects.valueAt(index);
2439 } else {
2440 desc = new SuspendedEffectDesc();
2441 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2442 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2443 }
2444 if (desc->mRefCount++ == 0) {
2445 Vector< sp<EffectModule> > effects;
2446 getSuspendEligibleEffects(effects);
2447 for (size_t i = 0; i < effects.size(); i++) {
2448 setEffectSuspended_l(&effects[i]->desc().type, true);
2449 }
2450 }
2451 } else {
2452 if (index < 0) {
2453 return;
2454 }
2455 desc = mSuspendedEffects.valueAt(index);
2456 if (desc->mRefCount <= 0) {
2457 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2458 desc->mRefCount = 1;
2459 }
2460 if (--desc->mRefCount == 0) {
2461 Vector<const effect_uuid_t *> types;
2462 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2463 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2464 continue;
2465 }
2466 types.add(&mSuspendedEffects.valueAt(i)->mType);
2467 }
2468 for (size_t i = 0; i < types.size(); i++) {
2469 setEffectSuspended_l(types[i], false);
2470 }
2471 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2472 mSuspendedEffects.keyAt(index));
2473 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2474 }
2475 }
2476}
2477
2478
2479// The volume effect is used for automated tests only
2480#ifndef OPENSL_ES_H_
2481static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2482 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2483const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2484#endif //OPENSL_ES_H_
2485
Eric Laurentd8365c52017-07-16 15:27:05 -07002486/* static */
2487bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2488{
2489 // Only NS and AEC are suspended when BtNRec is off
2490 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2491 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2492 return true;
2493 }
2494 return false;
2495}
2496
Eric Laurentca7cc822012-11-19 14:55:58 -08002497bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2498{
2499 // auxiliary effects and visualizer are never suspended on output mix
2500 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2501 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2502 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2503 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2504 return false;
2505 }
2506 return true;
2507}
2508
2509void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2510 Vector< sp<AudioFlinger::EffectModule> > &effects)
2511{
2512 effects.clear();
2513 for (size_t i = 0; i < mEffects.size(); i++) {
2514 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2515 effects.add(mEffects[i]);
2516 }
2517 }
2518}
2519
2520sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2521 const effect_uuid_t *type)
2522{
2523 sp<EffectModule> effect = getEffectFromType_l(type);
2524 return effect != 0 && effect->isEnabled() ? effect : 0;
2525}
2526
2527void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2528 bool enabled)
2529{
2530 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2531 if (enabled) {
2532 if (index < 0) {
2533 // if the effect is not suspend check if all effects are suspended
2534 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2535 if (index < 0) {
2536 return;
2537 }
2538 if (!isEffectEligibleForSuspend(effect->desc())) {
2539 return;
2540 }
2541 setEffectSuspended_l(&effect->desc().type, enabled);
2542 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2543 if (index < 0) {
2544 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2545 return;
2546 }
2547 }
2548 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2549 effect->desc().type.timeLow);
2550 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002551 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002552 if (desc->mEffect == 0) {
2553 desc->mEffect = effect;
2554 effect->setEnabled(false);
2555 effect->setSuspended(true);
2556 }
2557 } else {
2558 if (index < 0) {
2559 return;
2560 }
2561 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2562 effect->desc().type.timeLow);
2563 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2564 desc->mEffect.clear();
2565 effect->setSuspended(false);
2566 }
2567}
2568
Eric Laurent5baf2af2013-09-12 17:37:00 -07002569bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002570{
2571 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002572 return isNonOffloadableEnabled_l();
2573}
2574
2575bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2576{
Eric Laurent813e2a72013-08-31 12:59:48 -07002577 size_t size = mEffects.size();
2578 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002579 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002580 return true;
2581 }
2582 }
2583 return false;
2584}
2585
Eric Laurentaaa44472014-09-12 17:41:50 -07002586void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2587{
2588 Mutex::Autolock _l(mLock);
2589 mThread = thread;
2590 for (size_t i = 0; i < mEffects.size(); i++) {
2591 mEffects[i]->setThread(thread);
2592 }
2593}
2594
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002595void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2596{
2597 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2598 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2599 }
2600 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2601 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2602 }
2603}
2604
2605void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2606{
2607 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2608 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2609 }
2610 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2611 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2612 }
2613}
2614
2615bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002616{
2617 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002618 for (const auto &effect : mEffects) {
2619 if (effect->isProcessImplemented()) {
2620 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002621 }
2622 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002623 // Allow effects without processing.
2624 return true;
2625}
2626
2627bool AudioFlinger::EffectChain::isFastCompatible() const
2628{
2629 Mutex::Autolock _l(mLock);
2630 for (const auto &effect : mEffects) {
2631 if (effect->isProcessImplemented()
2632 && effect->isImplementationSoftware()) {
2633 return false;
2634 }
2635 }
2636 // Allow effects without processing or hw accelerated effects.
2637 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002638}
2639
2640// isCompatibleWithThread_l() must be called with thread->mLock held
2641bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2642{
2643 Mutex::Autolock _l(mLock);
2644 for (size_t i = 0; i < mEffects.size(); i++) {
2645 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2646 return false;
2647 }
2648 }
2649 return true;
2650}
2651
Glenn Kasten63238ef2015-03-02 15:50:29 -08002652} // namespace android