blob: 2047dfd2afa83756c284befb00dfb050fe531a05 [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>
Eric Laurentca7cc822012-11-19 14:55:58 -080034
35#include "AudioFlinger.h"
36#include "ServiceUtilities.h"
37
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
53namespace android {
54
55// ----------------------------------------------------------------------------
56// EffectModule implementation
57// ----------------------------------------------------------------------------
58
59#undef LOG_TAG
60#define LOG_TAG "AudioFlinger::EffectModule"
61
62AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
63 const wp<AudioFlinger::EffectChain>& chain,
64 effect_descriptor_t *desc,
65 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080066 audio_session_t sessionId,
67 bool pinned)
68 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080069 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
70 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080071 // clear mConfig to ensure consistent initial value of buffer framecount
72 // in case buffers are associated by setInBuffer() or setOutBuffer()
73 // prior to configure().
74 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080075 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080076 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
77 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070078 mSuspended(false),
Andy Hung62aef7d2017-12-14 14:50:40 -080079 mOffloaded(false),
Eric Laurentaaa44472014-09-12 17:41:50 -070080 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070081#ifdef FLOAT_EFFECT_CHAIN
82 , mSupportsFloat(false)
83#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080084{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080085 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080086 int lStatus;
87
88 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070089 mStatus = -ENODEV;
90 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070091 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070092 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070093 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070094 mStatus = effectsFactory->createEffect(
95 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
96 }
97 }
Eric Laurentca7cc822012-11-19 14:55:58 -080098
99 if (mStatus != NO_ERROR) {
100 return;
101 }
102 lStatus = init();
103 if (lStatus < 0) {
104 mStatus = lStatus;
105 goto Error;
106 }
107
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800108 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700109 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110
Eric Laurentca7cc822012-11-19 14:55:58 -0800111 return;
112Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700113 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800114 ALOGV("Constructor Error %d", mStatus);
115}
116
117AudioFlinger::EffectModule::~EffectModule()
118{
119 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700120 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700121 char uuidStr[64];
122 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
123 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
124 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800125 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800126 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800127
Eric Laurentca7cc822012-11-19 14:55:58 -0800128}
129
130status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
131{
132 status_t status;
133
134 Mutex::Autolock _l(mLock);
135 int priority = handle->priority();
136 size_t size = mHandles.size();
137 EffectHandle *controlHandle = NULL;
138 size_t i;
139 for (i = 0; i < size; i++) {
140 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800141 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800142 continue;
143 }
144 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700145 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800146 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700147 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800148 if (h->priority() <= priority) {
149 break;
150 }
151 }
152 // if inserted in first place, move effect control from previous owner to this handle
153 if (i == 0) {
154 bool enabled = false;
155 if (controlHandle != NULL) {
156 enabled = controlHandle->enabled();
157 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
158 }
159 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
160 status = NO_ERROR;
161 } else {
162 status = ALREADY_EXISTS;
163 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700164 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800165 mHandles.insertAt(handle, i);
166 return status;
167}
168
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800169ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800170{
171 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800172 return removeHandle_l(handle);
173}
174
175ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
176{
Eric Laurentca7cc822012-11-19 14:55:58 -0800177 size_t size = mHandles.size();
178 size_t i;
179 for (i = 0; i < size; i++) {
180 if (mHandles[i] == handle) {
181 break;
182 }
183 }
184 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800185 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
186 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800187 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800188 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800189
190 mHandles.removeAt(i);
191 // if removed from first place, move effect control from this handle to next in line
192 if (i == 0) {
193 EffectHandle *h = controlHandle_l();
194 if (h != NULL) {
195 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
196 }
197 }
198
199 // Prevent calls to process() and other functions on effect interface from now on.
200 // The effect engine will be released by the destructor when the last strong reference on
201 // this object is released which can happen after next process is called.
202 if (mHandles.size() == 0 && !mPinned) {
203 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800204 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800205 }
206
207 return mHandles.size();
208}
209
210// must be called with EffectModule::mLock held
211AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
212{
213 // the first valid handle in the list has control over the module
214 for (size_t i = 0; i < mHandles.size(); i++) {
215 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800216 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800217 return h;
218 }
219 }
220
221 return NULL;
222}
223
Eric Laurentf10c7092016-12-06 17:09:56 -0800224// unsafe method called when the effect parent thread has been destroyed
225ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
226{
227 ALOGV("disconnect() %p handle %p", this, handle);
228 Mutex::Autolock _l(mLock);
229 ssize_t numHandles = removeHandle_l(handle);
230 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
231 AudioSystem::unregisterEffect(mId);
232 sp<AudioFlinger> af = mAudioFlinger.promote();
233 if (af != 0) {
234 mLock.unlock();
235 af->updateOrphanEffectChains(this);
236 mLock.lock();
237 }
238 }
239 return numHandles;
240}
241
Eric Laurentfa1e1232016-08-02 19:01:49 -0700242bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800243 Mutex::Autolock _l(mLock);
244
Eric Laurentfa1e1232016-08-02 19:01:49 -0700245 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800246 switch (mState) {
247 case RESTART:
248 reset_l();
249 // FALL THROUGH
250
251 case STARTING:
252 // clear auxiliary effect input buffer for next accumulation
253 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
254 memset(mConfig.inputCfg.buffer.raw,
255 0,
256 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
257 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700258 if (start_l() == NO_ERROR) {
259 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700260 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700261 } else {
262 mState = IDLE;
263 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800264 break;
265 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700266 if (stop_l() == NO_ERROR) {
267 mDisableWaitCnt = mMaxDisableWaitCnt;
268 } else {
269 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
270 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800271 mState = STOPPED;
272 break;
273 case STOPPED:
274 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
275 // turn off sequence.
276 if (--mDisableWaitCnt == 0) {
277 reset_l();
278 mState = IDLE;
279 }
280 break;
281 default: //IDLE , ACTIVE, DESTROYED
282 break;
283 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700284
285 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800286}
287
288void AudioFlinger::EffectModule::process()
289{
290 Mutex::Autolock _l(mLock);
291
Mikhail Naganov022b9952017-01-04 16:36:51 -0800292 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800293 return;
294 }
295
rago94a1ee82017-07-21 15:11:02 -0700296 const uint32_t inChannelCount =
297 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
298 const uint32_t outChannelCount =
299 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
300 const bool auxType =
301 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
302
Andy Hungfa69ca32017-11-30 10:07:53 -0800303 // safeInputOutputSampleCount is 0 if the channel count between input and output
304 // buffers do not match. This prevents automatic accumulation or copying between the
305 // input and output effect buffers without an intermediary effect process.
306 // TODO: consider implementing channel conversion.
307 const size_t safeInputOutputSampleCount =
308 inChannelCount != outChannelCount ? 0
309 : outChannelCount * std::min(
310 mConfig.inputCfg.buffer.frameCount,
311 mConfig.outputCfg.buffer.frameCount);
312 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
313#ifdef FLOAT_EFFECT_CHAIN
314 accumulate_float(
315 mConfig.outputCfg.buffer.f32,
316 mConfig.inputCfg.buffer.f32,
317 safeInputOutputSampleCount);
318#else
319 accumulate_i16(
320 mConfig.outputCfg.buffer.s16,
321 mConfig.inputCfg.buffer.s16,
322 safeInputOutputSampleCount);
323#endif
324 };
325 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
326#ifdef FLOAT_EFFECT_CHAIN
327 memcpy(
328 mConfig.outputCfg.buffer.f32,
329 mConfig.inputCfg.buffer.f32,
330 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
331
332#else
333 memcpy(
334 mConfig.outputCfg.buffer.s16,
335 mConfig.inputCfg.buffer.s16,
336 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
337#endif
338 };
339
Eric Laurentca7cc822012-11-19 14:55:58 -0800340 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700341 int ret;
342 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700343 if (auxType) {
344 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800345 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700346#ifdef FLOAT_EFFECT_CHAIN
347 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800348#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700349 // Do in-place float conversion for auxiliary effect input buffer.
350 static_assert(sizeof(float) <= sizeof(int32_t),
351 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
352
Andy Hungfa69ca32017-11-30 10:07:53 -0800353 memcpy_to_float_from_q4_27(
354 mConfig.inputCfg.buffer.f32,
355 mConfig.inputCfg.buffer.s32,
356 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800357#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800358 } else
Andy Hung116a4982017-11-30 10:15:08 -0800359#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800360 {
Andy Hung116a4982017-11-30 10:15:08 -0800361#ifdef FLOAT_AUX
362 memcpy_to_i16_from_float(
363 mConfig.inputCfg.buffer.s16,
364 mConfig.inputCfg.buffer.f32,
365 mConfig.inputCfg.buffer.frameCount);
366#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800367 memcpy_to_i16_from_q4_27(
368 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700369 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800370 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800371#endif
rago94a1ee82017-07-21 15:11:02 -0700372 }
rago94a1ee82017-07-21 15:11:02 -0700373 }
374#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800375 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
376 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
377
378 if (!auxType && mInChannelCountRequested != inChannelCount) {
379 adjust_channels(
380 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
381 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
382 sizeof(float),
383 sizeof(float)
384 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
385 inBuffer = mInConversionBuffer;
386 }
387 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
388 && mOutChannelCountRequested != outChannelCount) {
389 adjust_selected_channels(
390 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
391 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
392 sizeof(float),
393 sizeof(float)
394 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
395 outBuffer = mOutConversionBuffer;
396 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800397 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
398 if (!auxType) {
399 if (mInConversionBuffer.get() == nullptr) {
400 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
401 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700402 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800403 memcpy_to_i16_from_float(
404 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800405 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800406 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800407 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700408 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800409 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
410 if (mOutConversionBuffer.get() == nullptr) {
411 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
412 goto data_bypass;
413 }
414 memcpy_to_i16_from_float(
415 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800416 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800417 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800418 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700419 }
420 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800421#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800422 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800423#ifdef FLOAT_EFFECT_CHAIN
424 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800425 sp<EffectBufferHalInterface> target =
426 mOutChannelCountRequested != outChannelCount
427 ? mOutConversionBuffer : mOutBuffer;
428
Andy Hungfa69ca32017-11-30 10:07:53 -0800429 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800430 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800431 mOutConversionBuffer->audioBuffer()->s16,
432 outChannelCount * mConfig.outputCfg.buffer.frameCount);
433 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800434 if (mOutChannelCountRequested != outChannelCount) {
435 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
436 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
437 sizeof(float),
438 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
439 }
rago94a1ee82017-07-21 15:11:02 -0700440#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700441 } else {
rago94a1ee82017-07-21 15:11:02 -0700442#ifdef FLOAT_EFFECT_CHAIN
443 data_bypass:
444#endif
445 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800446 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700447 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800448 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700449 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800450 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700451 }
452 }
453 ret = -ENODATA;
454 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800455
Eric Laurentca7cc822012-11-19 14:55:58 -0800456 // force transition to IDLE state when engine is ready
457 if (mState == STOPPED && ret == -ENODATA) {
458 mDisableWaitCnt = 1;
459 }
460
461 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700462 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800463#ifdef FLOAT_AUX
464 const size_t size =
465 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
466#else
rago94a1ee82017-07-21 15:11:02 -0700467 const size_t size =
468 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800469#endif
rago94a1ee82017-07-21 15:11:02 -0700470 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800471 }
472 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700473 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800474 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
475 // If an insert effect is idle and input buffer is different from output buffer,
476 // accumulate input onto output
477 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800478 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
479 accumulateInputToOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800480 }
481 }
482}
483
484void AudioFlinger::EffectModule::reset_l()
485{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700486 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800487 return;
488 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700489 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800490}
491
492status_t AudioFlinger::EffectModule::configure()
493{
rago94a1ee82017-07-21 15:11:02 -0700494 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700495 status_t status;
496 sp<ThreadBase> thread;
497 uint32_t size;
498 audio_channel_mask_t channelMask;
499
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700500 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700501 status = NO_INIT;
502 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800503 }
504
Eric Laurentd0ebb532013-04-02 16:41:41 -0700505 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800506 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700507 status = DEAD_OBJECT;
508 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800509 }
510
511 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800512 // TODO: handle configuration of input (record) SW effects above the HAL,
513 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
514 // in which case input channel masks should be used here.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700515 channelMask = thread->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800516 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700517 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800518
519 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800520 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
521 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
522 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
523 mConfig.inputCfg.channels);
524 }
525#ifndef MULTICHANNEL_EFFECT_CHAIN
526 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
527 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
528 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
529 mConfig.outputCfg.channels);
530 }
531#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800532 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800533#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700534 // TODO: Update this logic when multichannel effects are implemented.
535 // For offloaded tracks consider mono output as stereo for proper effect initialization
536 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
537 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
538 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
539 ALOGV("Overriding effect input and output as STEREO");
540 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800541#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800542 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800543 mInChannelCountRequested =
544 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
545 mOutChannelCountRequested =
546 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700547
rago94a1ee82017-07-21 15:11:02 -0700548 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
549 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800550 mConfig.inputCfg.samplingRate = thread->sampleRate();
551 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
552 mConfig.inputCfg.bufferProvider.cookie = NULL;
553 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
554 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
555 mConfig.outputCfg.bufferProvider.cookie = NULL;
556 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
557 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
558 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
559 // Insert effect:
560 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
561 // always overwrites output buffer: input buffer == output buffer
562 // - in other sessions:
563 // last effect in the chain accumulates in output buffer: input buffer != output buffer
564 // other effect: overwrites output buffer: input buffer == output buffer
565 // Auxiliary effect:
566 // accumulates in output buffer: input buffer != output buffer
567 // Therefore: accumulate <=> input buffer != output buffer
568 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
569 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
570 } else {
571 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
572 }
573 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
574 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
575 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
576 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
577
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700578 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
580
581 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700582 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700583 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800584 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700585 &mConfig,
586 &size,
587 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700588 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800589 status = cmdStatus;
590 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800591
592#ifdef MULTICHANNEL_EFFECT_CHAIN
593 if (status != NO_ERROR &&
Andy Hunge6a61a52018-04-06 18:55:26 -0700594 thread->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800595 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
596 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
597 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700598 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
599 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800600 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
601 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
602 }
603 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
604 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
605 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
606 }
607 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700608 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800609 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700610 &mConfig,
611 &size,
612 &cmdStatus);
613 if (status == NO_ERROR) {
614 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800615 }
616 }
617#endif
618
619#ifdef FLOAT_EFFECT_CHAIN
620 if (status == NO_ERROR) {
621 mSupportsFloat = true;
622 }
623
624 if (status != NO_ERROR) {
625 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
626 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
627 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
628 size = sizeof(int);
629 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
630 sizeof(mConfig),
631 &mConfig,
632 &size,
633 &cmdStatus);
634 if (status == NO_ERROR) {
635 status = cmdStatus;
636 }
637 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700638 mSupportsFloat = false;
639 ALOGVV("config worked with 16 bit");
640 } else {
641 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800642 }
rago94a1ee82017-07-21 15:11:02 -0700643 }
644#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800645
rago94a1ee82017-07-21 15:11:02 -0700646 if (status == NO_ERROR) {
647 // Establish Buffer strategy
648 setInBuffer(mInBuffer);
649 setOutBuffer(mOutBuffer);
650
651 // Update visualizer latency
652 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
653 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
654 effect_param_t *p = (effect_param_t *)buf32;
655
656 p->psize = sizeof(uint32_t);
657 p->vsize = sizeof(uint32_t);
658 size = sizeof(int);
659 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
660
661 uint32_t latency = 0;
662 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
663 if (pbt != NULL) {
664 latency = pbt->latency_l();
665 }
666
667 *((int32_t *)p->data + 1)= latency;
668 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
669 sizeof(effect_param_t) + 8,
670 &buf32,
671 &size,
672 &cmdStatus);
673 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800674 }
675
Andy Hung05083ac2017-12-14 15:00:28 -0800676 // mConfig.outputCfg.buffer.frameCount cannot be zero.
677 mMaxDisableWaitCnt = (uint32_t)std::max(
678 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
679 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
680 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800681
Eric Laurentd0ebb532013-04-02 16:41:41 -0700682exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800683 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700684 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700685 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800686 return status;
687}
688
689status_t AudioFlinger::EffectModule::init()
690{
691 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700692 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800693 return NO_INIT;
694 }
695 status_t cmdStatus;
696 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700697 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
698 0,
699 NULL,
700 &size,
701 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800702 if (status == 0) {
703 status = cmdStatus;
704 }
705 return status;
706}
707
Eric Laurent1b928682014-10-02 19:41:47 -0700708void AudioFlinger::EffectModule::addEffectToHal_l()
709{
710 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
711 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
712 sp<ThreadBase> thread = mThread.promote();
713 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700714 sp<StreamHalInterface> stream = thread->stream();
715 if (stream != 0) {
716 status_t result = stream->addEffect(mEffectInterface);
717 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700718 }
719 }
720 }
721}
722
Eric Laurentfa1e1232016-08-02 19:01:49 -0700723// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800724status_t AudioFlinger::EffectModule::start()
725{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700726 sp<EffectChain> chain;
727 status_t status;
728 {
729 Mutex::Autolock _l(mLock);
730 status = start_l();
731 if (status == NO_ERROR) {
732 chain = mChain.promote();
733 }
734 }
735 if (chain != 0) {
736 chain->resetVolume_l();
737 }
738 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800739}
740
741status_t AudioFlinger::EffectModule::start_l()
742{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700743 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800744 return NO_INIT;
745 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700746 if (mStatus != NO_ERROR) {
747 return mStatus;
748 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800749 status_t cmdStatus;
750 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700751 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
752 0,
753 NULL,
754 &size,
755 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800756 if (status == 0) {
757 status = cmdStatus;
758 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700759 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700760 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800761 }
762 return status;
763}
764
765status_t AudioFlinger::EffectModule::stop()
766{
767 Mutex::Autolock _l(mLock);
768 return stop_l();
769}
770
771status_t AudioFlinger::EffectModule::stop_l()
772{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700773 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800774 return NO_INIT;
775 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700776 if (mStatus != NO_ERROR) {
777 return mStatus;
778 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800779 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800780 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700781 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
782 0,
783 NULL,
784 &size,
785 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800786 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800787 status = cmdStatus;
788 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800789 if (status == NO_ERROR) {
790 status = remove_effect_from_hal_l();
791 }
792 return status;
793}
794
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800795// must be called with EffectChain::mLock held
796void AudioFlinger::EffectModule::release_l()
797{
798 if (mEffectInterface != 0) {
799 remove_effect_from_hal_l();
800 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800801 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800802 mEffectInterface.clear();
803 }
804}
805
Eric Laurentbfb1b832013-01-07 09:53:42 -0800806status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
807{
808 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
809 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800810 sp<ThreadBase> thread = mThread.promote();
811 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700812 sp<StreamHalInterface> stream = thread->stream();
813 if (stream != 0) {
814 status_t result = stream->removeEffect(mEffectInterface);
815 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800816 }
817 }
818 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800819 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800820}
821
Andy Hunge4a1d912016-08-17 14:11:13 -0700822// round up delta valid if value and divisor are positive.
823template <typename T>
824static T roundUpDelta(const T &value, const T &divisor) {
825 T remainder = value % divisor;
826 return remainder == 0 ? 0 : divisor - remainder;
827}
828
Eric Laurentca7cc822012-11-19 14:55:58 -0800829status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
830 uint32_t cmdSize,
831 void *pCmdData,
832 uint32_t *replySize,
833 void *pReplyData)
834{
835 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700836 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800837
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700838 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 return NO_INIT;
840 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700841 if (mStatus != NO_ERROR) {
842 return mStatus;
843 }
Andy Hung110bc952016-06-20 15:22:52 -0700844 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700845 (sizeof(effect_param_t) > cmdSize ||
846 ((effect_param_t *)pCmdData)->psize > cmdSize
847 - sizeof(effect_param_t))) {
848 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800849 android_errorWriteLog(0x534e4554, "33003822");
850 return -EINVAL;
851 }
852 if (cmdCode == EFFECT_CMD_GET_PARAM &&
853 (*replySize < sizeof(effect_param_t) ||
854 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
855 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700856 return -EINVAL;
857 }
ragoe2759072016-11-22 18:02:48 -0800858 if (cmdCode == EFFECT_CMD_GET_PARAM &&
859 (sizeof(effect_param_t) > *replySize
860 || ((effect_param_t *)pCmdData)->psize > *replySize
861 - sizeof(effect_param_t)
862 || ((effect_param_t *)pCmdData)->vsize > *replySize
863 - sizeof(effect_param_t)
864 - ((effect_param_t *)pCmdData)->psize
865 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
866 *replySize
867 - sizeof(effect_param_t)
868 - ((effect_param_t *)pCmdData)->psize
869 - ((effect_param_t *)pCmdData)->vsize)) {
870 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
871 android_errorWriteLog(0x534e4554, "32705438");
872 return -EINVAL;
873 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700874 if ((cmdCode == EFFECT_CMD_SET_PARAM
875 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
876 (sizeof(effect_param_t) > cmdSize
877 || ((effect_param_t *)pCmdData)->psize > cmdSize
878 - sizeof(effect_param_t)
879 || ((effect_param_t *)pCmdData)->vsize > cmdSize
880 - sizeof(effect_param_t)
881 - ((effect_param_t *)pCmdData)->psize
882 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
883 cmdSize
884 - sizeof(effect_param_t)
885 - ((effect_param_t *)pCmdData)->psize
886 - ((effect_param_t *)pCmdData)->vsize)) {
887 android_errorWriteLog(0x534e4554, "30204301");
888 return -EINVAL;
889 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700890 status_t status = mEffectInterface->command(cmdCode,
891 cmdSize,
892 pCmdData,
893 replySize,
894 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800895 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
896 uint32_t size = (replySize == NULL) ? 0 : *replySize;
897 for (size_t i = 1; i < mHandles.size(); i++) {
898 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800899 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800900 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
901 }
902 }
903 }
904 return status;
905}
906
907status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
908{
909 Mutex::Autolock _l(mLock);
910 return setEnabled_l(enabled);
911}
912
913// must be called with EffectModule::mLock held
914status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
915{
916
917 ALOGV("setEnabled %p enabled %d", this, enabled);
918
919 if (enabled != isEnabled()) {
920 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
921 if (enabled && status != NO_ERROR) {
922 return status;
923 }
924
925 switch (mState) {
926 // going from disabled to enabled
927 case IDLE:
928 mState = STARTING;
929 break;
930 case STOPPED:
931 mState = RESTART;
932 break;
933 case STOPPING:
934 mState = ACTIVE;
935 break;
936
937 // going from enabled to disabled
938 case RESTART:
939 mState = STOPPED;
940 break;
941 case STARTING:
942 mState = IDLE;
943 break;
944 case ACTIVE:
945 mState = STOPPING;
946 break;
947 case DESTROYED:
948 return NO_ERROR; // simply ignore as we are being destroyed
949 }
950 for (size_t i = 1; i < mHandles.size(); i++) {
951 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800952 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800953 h->setEnabled(enabled);
954 }
955 }
956 }
957 return NO_ERROR;
958}
959
960bool AudioFlinger::EffectModule::isEnabled() const
961{
962 switch (mState) {
963 case RESTART:
964 case STARTING:
965 case ACTIVE:
966 return true;
967 case IDLE:
968 case STOPPING:
969 case STOPPED:
970 case DESTROYED:
971 default:
972 return false;
973 }
974}
975
976bool AudioFlinger::EffectModule::isProcessEnabled() const
977{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700978 if (mStatus != NO_ERROR) {
979 return false;
980 }
981
Eric Laurentca7cc822012-11-19 14:55:58 -0800982 switch (mState) {
983 case RESTART:
984 case ACTIVE:
985 case STOPPING:
986 case STOPPED:
987 return true;
988 case IDLE:
989 case STARTING:
990 case DESTROYED:
991 default:
992 return false;
993 }
994}
995
Mikhail Naganov022b9952017-01-04 16:36:51 -0800996void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700997 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800998
999 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001000 if (buffer != 0) {
1001 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1002 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1003 } else {
1004 mConfig.inputCfg.buffer.raw = NULL;
1005 }
1006 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001007 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001008
1009#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001010 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001011 // Theoretically insert effects can also do in-place conversions (destroying
1012 // the original buffer) when the output buffer is identical to the input buffer,
1013 // but we don't optimize for it here.
1014 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001015 const uint32_t inChannelCount =
1016 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1017 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1018 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001019 // we need to translate - create hidl shared buffer and intercept
1020 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001021 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1022 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1023 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001024
1025 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1026 __func__, inChannels, inFrameCount, size);
1027
Andy Hungbded9c82017-11-30 18:47:35 -08001028 if (size > 0 && (mInConversionBuffer.get() == nullptr
1029 || size > mInConversionBuffer->getSize())) {
1030 mInConversionBuffer.clear();
1031 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001032 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1033 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1034 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001035 }
Andy Hungbded9c82017-11-30 18:47:35 -08001036 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001037 mInConversionBuffer->setFrameCount(inFrameCount);
1038 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001039 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001040 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001041 }
1042 }
1043#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001044}
1045
1046void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001047 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001048
1049 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001050 if (buffer != 0) {
1051 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1052 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1053 } else {
1054 mConfig.outputCfg.buffer.raw = NULL;
1055 }
1056 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001057 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001058
1059#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001060 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001061 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001062 const uint32_t outChannelCount =
1063 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1064 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1065 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001066 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001067 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1068 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1069 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001070
1071 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1072 __func__, outChannels, outFrameCount, size);
1073
Andy Hungbded9c82017-11-30 18:47:35 -08001074 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1075 || size > mOutConversionBuffer->getSize())) {
1076 mOutConversionBuffer.clear();
1077 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001078 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1079 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1080 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001081 }
Andy Hungbded9c82017-11-30 18:47:35 -08001082 if (mOutConversionBuffer.get() != nullptr) {
1083 mOutConversionBuffer->setFrameCount(outFrameCount);
1084 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001085 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001086 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001087 }
1088 }
1089#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001090}
1091
Eric Laurentca7cc822012-11-19 14:55:58 -08001092status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1093{
1094 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001095 if (mStatus != NO_ERROR) {
1096 return mStatus;
1097 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001098 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001099 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1100 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1101 if (isProcessEnabled() &&
1102 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
1103 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001104 uint32_t volume[2];
1105 uint32_t *pVolume = NULL;
1106 uint32_t size = sizeof(volume);
1107 volume[0] = *left;
1108 volume[1] = *right;
1109 if (controller) {
1110 pVolume = volume;
1111 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001112 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1113 size,
1114 volume,
1115 &size,
1116 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001117 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1118 *left = volume[0];
1119 *right = volume[1];
1120 }
1121 }
1122 return status;
1123}
1124
1125status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1126{
1127 if (device == AUDIO_DEVICE_NONE) {
1128 return NO_ERROR;
1129 }
1130
1131 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001132 if (mStatus != NO_ERROR) {
1133 return mStatus;
1134 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001135 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001136 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001137 status_t cmdStatus;
1138 uint32_t size = sizeof(status_t);
1139 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1140 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001141 status = mEffectInterface->command(cmd,
1142 sizeof(uint32_t),
1143 &device,
1144 &size,
1145 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001146 }
1147 return status;
1148}
1149
1150status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1151{
1152 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001153 if (mStatus != NO_ERROR) {
1154 return mStatus;
1155 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001156 status_t status = NO_ERROR;
1157 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1158 status_t cmdStatus;
1159 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001160 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1161 sizeof(audio_mode_t),
1162 &mode,
1163 &size,
1164 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001165 if (status == NO_ERROR) {
1166 status = cmdStatus;
1167 }
1168 }
1169 return status;
1170}
1171
1172status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1173{
1174 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001175 if (mStatus != NO_ERROR) {
1176 return mStatus;
1177 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001178 status_t status = NO_ERROR;
1179 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1180 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001181 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1182 sizeof(audio_source_t),
1183 &source,
1184 &size,
1185 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001186 }
1187 return status;
1188}
1189
1190void AudioFlinger::EffectModule::setSuspended(bool suspended)
1191{
1192 Mutex::Autolock _l(mLock);
1193 mSuspended = suspended;
1194}
1195
1196bool AudioFlinger::EffectModule::suspended() const
1197{
1198 Mutex::Autolock _l(mLock);
1199 return mSuspended;
1200}
1201
1202bool AudioFlinger::EffectModule::purgeHandles()
1203{
1204 bool enabled = false;
1205 Mutex::Autolock _l(mLock);
1206 for (size_t i = 0; i < mHandles.size(); i++) {
1207 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001208 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001209 if (handle->hasControl()) {
1210 enabled = handle->enabled();
1211 }
1212 }
1213 }
1214 return enabled;
1215}
1216
Eric Laurent5baf2af2013-09-12 17:37:00 -07001217status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1218{
1219 Mutex::Autolock _l(mLock);
1220 if (mStatus != NO_ERROR) {
1221 return mStatus;
1222 }
1223 status_t status = NO_ERROR;
1224 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1225 status_t cmdStatus;
1226 uint32_t size = sizeof(status_t);
1227 effect_offload_param_t cmd;
1228
1229 cmd.isOffload = offloaded;
1230 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001231 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1232 sizeof(effect_offload_param_t),
1233 &cmd,
1234 &size,
1235 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001236 if (status == NO_ERROR) {
1237 status = cmdStatus;
1238 }
1239 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1240 } else {
1241 if (offloaded) {
1242 status = INVALID_OPERATION;
1243 }
1244 mOffloaded = false;
1245 }
1246 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1247 return status;
1248}
1249
1250bool AudioFlinger::EffectModule::isOffloaded() const
1251{
1252 Mutex::Autolock _l(mLock);
1253 return mOffloaded;
1254}
1255
Marco Nelissenb2208842014-02-07 14:00:50 -08001256String8 effectFlagsToString(uint32_t flags) {
1257 String8 s;
1258
1259 s.append("conn. mode: ");
1260 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1261 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1262 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1263 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1264 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1265 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1266 default: s.append("unknown/reserved"); break;
1267 }
1268 s.append(", ");
1269
1270 s.append("insert pref: ");
1271 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1272 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1273 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1274 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1275 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1276 default: s.append("unknown/reserved"); break;
1277 }
1278 s.append(", ");
1279
1280 s.append("volume mgmt: ");
1281 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1282 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1283 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1284 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1285 default: s.append("unknown/reserved"); break;
1286 }
1287 s.append(", ");
1288
1289 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1290 if (devind) {
1291 s.append("device indication: ");
1292 switch (devind) {
1293 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1294 default: s.append("unknown/reserved"); break;
1295 }
1296 s.append(", ");
1297 }
1298
1299 s.append("input mode: ");
1300 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1301 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1302 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1303 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1304 default: s.append("not set"); break;
1305 }
1306 s.append(", ");
1307
1308 s.append("output mode: ");
1309 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1310 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1311 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1312 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1313 default: s.append("not set"); break;
1314 }
1315 s.append(", ");
1316
1317 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1318 if (accel) {
1319 s.append("hardware acceleration: ");
1320 switch (accel) {
1321 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1322 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1323 default: s.append("unknown/reserved"); break;
1324 }
1325 s.append(", ");
1326 }
1327
1328 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1329 if (modeind) {
1330 s.append("mode indication: ");
1331 switch (modeind) {
1332 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1333 default: s.append("unknown/reserved"); break;
1334 }
1335 s.append(", ");
1336 }
1337
1338 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1339 if (srcind) {
1340 s.append("source indication: ");
1341 switch (srcind) {
1342 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1343 default: s.append("unknown/reserved"); break;
1344 }
1345 s.append(", ");
1346 }
1347
1348 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1349 s.append("offloadable, ");
1350 }
1351
1352 int len = s.length();
1353 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001354 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001355 s.unlockBuffer(len - 2);
1356 }
1357 return s;
1358}
1359
Andy Hungbded9c82017-11-30 18:47:35 -08001360static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1361 std::stringstream ss;
1362
1363 if (buffer.get() == nullptr) {
1364 return "nullptr"; // make different than below
1365 } else if (buffer->externalData() != nullptr) {
1366 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1367 << " -> "
1368 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1369 } else {
1370 ss << buffer->audioBuffer()->raw;
1371 }
1372 return ss.str();
1373}
Marco Nelissenb2208842014-02-07 14:00:50 -08001374
Glenn Kasten0f11b512014-01-31 16:18:54 -08001375void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001376{
Eric Laurentca7cc822012-11-19 14:55:58 -08001377 String8 result;
1378
Andy Hung9718d662017-12-22 17:57:39 -08001379 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001380
1381 bool locked = AudioFlinger::dumpTryLock(mLock);
1382 // failed to lock - AudioFlinger is probably deadlocked
1383 if (!locked) {
1384 result.append("\t\tCould not lock Fx mutex:\n");
1385 }
1386
1387 result.append("\t\tSession Status State Engine:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001388 result.appendFormat("\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001389 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001390
1391 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001392 char uuidStr[64];
1393 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001394 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001395 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001396 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1397 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001398 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001399 mDescriptor.flags,
1400 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001401 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001403
1404 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001405 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001406
1407 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001408
1409 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001410 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1411 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1412 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001413 mConfig.inputCfg.buffer.frameCount,
1414 mConfig.inputCfg.samplingRate,
1415 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001416 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001417 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001418
1419 result.append("\t\t- Output configuration:\n");
1420 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001421 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001422 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001423 mConfig.outputCfg.buffer.frameCount,
1424 mConfig.outputCfg.samplingRate,
1425 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001426 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001427 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001428
rago94a1ee82017-07-21 15:11:02 -07001429#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001430
Andy Hungbded9c82017-11-30 18:47:35 -08001431 result.appendFormat("\t\t- HAL buffers:\n"
1432 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1433 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1434 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1435 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1436 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001437#endif
1438
Andy Hung9718d662017-12-22 17:57:39 -08001439 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001440 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001441 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001442 for (size_t i = 0; i < mHandles.size(); ++i) {
1443 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001444 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001445 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001446 result.append(buffer);
1447 }
1448 }
1449
Eric Laurentca7cc822012-11-19 14:55:58 -08001450 write(fd, result.string(), result.length());
1451
1452 if (locked) {
1453 mLock.unlock();
1454 }
1455}
1456
1457// ----------------------------------------------------------------------------
1458// EffectHandle implementation
1459// ----------------------------------------------------------------------------
1460
1461#undef LOG_TAG
1462#define LOG_TAG "AudioFlinger::EffectHandle"
1463
1464AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1465 const sp<AudioFlinger::Client>& client,
1466 const sp<IEffectClient>& effectClient,
1467 int32_t priority)
1468 : BnEffect(),
1469 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001470 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001471{
1472 ALOGV("constructor %p", this);
1473
1474 if (client == 0) {
1475 return;
1476 }
1477 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1478 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001479 if (mCblkMemory == 0 ||
1480 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001481 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001483 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 return;
1485 }
Glenn Kastene75da402013-11-20 13:54:52 -08001486 new(mCblk) effect_param_cblk_t();
1487 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001488}
1489
1490AudioFlinger::EffectHandle::~EffectHandle()
1491{
1492 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001493 disconnect(false);
1494}
1495
Glenn Kastene75da402013-11-20 13:54:52 -08001496status_t AudioFlinger::EffectHandle::initCheck()
1497{
1498 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1499}
1500
Eric Laurentca7cc822012-11-19 14:55:58 -08001501status_t AudioFlinger::EffectHandle::enable()
1502{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001503 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001504 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001505 sp<EffectModule> effect = mEffect.promote();
1506 if (effect == 0 || mDisconnected) {
1507 return DEAD_OBJECT;
1508 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001509 if (!mHasControl) {
1510 return INVALID_OPERATION;
1511 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001512
1513 if (mEnabled) {
1514 return NO_ERROR;
1515 }
1516
1517 mEnabled = true;
1518
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001519 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001520 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001521 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001522 }
1523
1524 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001525 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001526 return NO_ERROR;
1527 }
1528
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001529 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001530 if (status != NO_ERROR) {
1531 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001532 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001533 }
1534 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001535 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001536 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001537 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1538 Mutex::Autolock _l(thread->mLock);
1539 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001540 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001541 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001542 if (thread->type() == ThreadBase::OFFLOAD) {
1543 PlaybackThread *t = (PlaybackThread *)thread.get();
1544 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1545 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001546 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001547 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1548 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001549 }
1550 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001551 }
1552 return status;
1553}
1554
1555status_t AudioFlinger::EffectHandle::disable()
1556{
1557 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001558 AutoMutex _l(mLock);
1559 sp<EffectModule> effect = mEffect.promote();
1560 if (effect == 0 || mDisconnected) {
1561 return DEAD_OBJECT;
1562 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001563 if (!mHasControl) {
1564 return INVALID_OPERATION;
1565 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001566
1567 if (!mEnabled) {
1568 return NO_ERROR;
1569 }
1570 mEnabled = false;
1571
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001572 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001573 return NO_ERROR;
1574 }
1575
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001576 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001577
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001578 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001579 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001580 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001581 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1582 Mutex::Autolock _l(thread->mLock);
1583 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001584 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001585 }
1586
1587 return status;
1588}
1589
1590void AudioFlinger::EffectHandle::disconnect()
1591{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001592 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001593 disconnect(true);
1594}
1595
1596void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1597{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001598 AutoMutex _l(mLock);
1599 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1600 if (mDisconnected) {
1601 if (unpinIfLast) {
1602 android_errorWriteLog(0x534e4554, "32707507");
1603 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001604 return;
1605 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001606 mDisconnected = true;
1607 sp<ThreadBase> thread;
1608 {
1609 sp<EffectModule> effect = mEffect.promote();
1610 if (effect != 0) {
1611 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001612 }
1613 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001614 if (thread != 0) {
1615 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001616 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001617 // try to cleanup as much as we can
1618 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001619 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1620 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001621 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001622 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001623
Eric Laurentca7cc822012-11-19 14:55:58 -08001624 if (mClient != 0) {
1625 if (mCblk != NULL) {
1626 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1627 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1628 }
1629 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001630 // Client destructor must run with AudioFlinger client mutex locked
1631 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001632 mClient.clear();
1633 }
1634}
1635
1636status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1637 uint32_t cmdSize,
1638 void *pCmdData,
1639 uint32_t *replySize,
1640 void *pReplyData)
1641{
1642 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001643 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001644
Eric Laurentc7ab3092017-06-15 18:43:46 -07001645 // reject commands reserved for internal use by audio framework if coming from outside
1646 // of audioserver
1647 switch(cmdCode) {
1648 case EFFECT_CMD_ENABLE:
1649 case EFFECT_CMD_DISABLE:
1650 case EFFECT_CMD_SET_PARAM:
1651 case EFFECT_CMD_SET_PARAM_DEFERRED:
1652 case EFFECT_CMD_SET_PARAM_COMMIT:
1653 case EFFECT_CMD_GET_PARAM:
1654 break;
1655 default:
1656 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1657 break;
1658 }
1659 android_errorWriteLog(0x534e4554, "62019992");
1660 return BAD_VALUE;
1661 }
1662
Eric Laurent1ffc5852016-12-15 14:46:09 -08001663 if (cmdCode == EFFECT_CMD_ENABLE) {
1664 if (*replySize < sizeof(int)) {
1665 android_errorWriteLog(0x534e4554, "32095713");
1666 return BAD_VALUE;
1667 }
1668 *(int *)pReplyData = NO_ERROR;
1669 *replySize = sizeof(int);
1670 return enable();
1671 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1672 if (*replySize < sizeof(int)) {
1673 android_errorWriteLog(0x534e4554, "32095713");
1674 return BAD_VALUE;
1675 }
1676 *(int *)pReplyData = NO_ERROR;
1677 *replySize = sizeof(int);
1678 return disable();
1679 }
1680
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001681 AutoMutex _l(mLock);
1682 sp<EffectModule> effect = mEffect.promote();
1683 if (effect == 0 || mDisconnected) {
1684 return DEAD_OBJECT;
1685 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001686 // only get parameter command is permitted for applications not controlling the effect
1687 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1688 return INVALID_OPERATION;
1689 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001690 if (mClient == 0) {
1691 return INVALID_OPERATION;
1692 }
1693
1694 // handle commands that are not forwarded transparently to effect engine
1695 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001696 if (*replySize < sizeof(int)) {
1697 android_errorWriteLog(0x534e4554, "32095713");
1698 return BAD_VALUE;
1699 }
1700 *(int *)pReplyData = NO_ERROR;
1701 *replySize = sizeof(int);
1702
Eric Laurentca7cc822012-11-19 14:55:58 -08001703 // No need to trylock() here as this function is executed in the binder thread serving a
1704 // particular client process: no risk to block the whole media server process or mixer
1705 // threads if we are stuck here
1706 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001707 // keep local copy of index in case of client corruption b/32220769
1708 const uint32_t clientIndex = mCblk->clientIndex;
1709 const uint32_t serverIndex = mCblk->serverIndex;
1710 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1711 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001712 mCblk->serverIndex = 0;
1713 mCblk->clientIndex = 0;
1714 return BAD_VALUE;
1715 }
1716 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001717 effect_param_t *param = NULL;
1718 for (uint32_t index = serverIndex; index < clientIndex;) {
1719 int *p = (int *)(mBuffer + index);
1720 const int size = *p++;
1721 if (size < 0
1722 || size > EFFECT_PARAM_BUFFER_SIZE
1723 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001724 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001725 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001726 break;
1727 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001728
1729 // copy to local memory in case of client corruption b/32220769
1730 param = (effect_param_t *)realloc(param, size);
1731 if (param == NULL) {
1732 ALOGW("command(): out of memory");
1733 status = NO_MEMORY;
1734 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001735 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001736 memcpy(param, p, size);
1737
1738 int reply = 0;
1739 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001740 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001741 size,
1742 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001743 &rsize,
1744 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001745
1746 // verify shared memory: server index shouldn't change; client index can't go back.
1747 if (serverIndex != mCblk->serverIndex
1748 || clientIndex > mCblk->clientIndex) {
1749 android_errorWriteLog(0x534e4554, "32220769");
1750 status = BAD_VALUE;
1751 break;
1752 }
1753
Eric Laurentca7cc822012-11-19 14:55:58 -08001754 // stop at first error encountered
1755 if (ret != NO_ERROR) {
1756 status = ret;
1757 *(int *)pReplyData = reply;
1758 break;
1759 } else if (reply != NO_ERROR) {
1760 *(int *)pReplyData = reply;
1761 break;
1762 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001763 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001764 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001765 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001766 mCblk->serverIndex = 0;
1767 mCblk->clientIndex = 0;
1768 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001769 }
1770
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001771 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001772}
1773
1774void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1775{
1776 ALOGV("setControl %p control %d", this, hasControl);
1777
1778 mHasControl = hasControl;
1779 mEnabled = enabled;
1780
1781 if (signal && mEffectClient != 0) {
1782 mEffectClient->controlStatusChanged(hasControl);
1783 }
1784}
1785
1786void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1787 uint32_t cmdSize,
1788 void *pCmdData,
1789 uint32_t replySize,
1790 void *pReplyData)
1791{
1792 if (mEffectClient != 0) {
1793 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1794 }
1795}
1796
1797
1798
1799void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1800{
1801 if (mEffectClient != 0) {
1802 mEffectClient->enableStatusChanged(enabled);
1803 }
1804}
1805
1806status_t AudioFlinger::EffectHandle::onTransact(
1807 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1808{
1809 return BnEffect::onTransact(code, data, reply, flags);
1810}
1811
1812
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001813void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001814{
1815 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1816
Marco Nelissenb2208842014-02-07 14:00:50 -08001817 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001818 (mClient == 0) ? getpid_cached : mClient->pid(),
1819 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001820 mHasControl ? "yes" : "no",
1821 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001822 mCblk ? mCblk->clientIndex : 0,
1823 mCblk ? mCblk->serverIndex : 0
1824 );
1825
1826 if (locked) {
1827 mCblk->lock.unlock();
1828 }
1829}
1830
1831#undef LOG_TAG
1832#define LOG_TAG "AudioFlinger::EffectChain"
1833
1834AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001835 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001836 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001837 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001838 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001839{
1840 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1841 if (thread == NULL) {
1842 return;
1843 }
1844 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1845 thread->frameCount();
1846}
1847
1848AudioFlinger::EffectChain::~EffectChain()
1849{
Eric Laurentca7cc822012-11-19 14:55:58 -08001850}
1851
1852// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1853sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1854 effect_descriptor_t *descriptor)
1855{
1856 size_t size = mEffects.size();
1857
1858 for (size_t i = 0; i < size; i++) {
1859 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1860 return mEffects[i];
1861 }
1862 }
1863 return 0;
1864}
1865
1866// getEffectFromId_l() must be called with ThreadBase::mLock held
1867sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1868{
1869 size_t size = mEffects.size();
1870
1871 for (size_t i = 0; i < size; i++) {
1872 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1873 if (id == 0 || mEffects[i]->id() == id) {
1874 return mEffects[i];
1875 }
1876 }
1877 return 0;
1878}
1879
1880// getEffectFromType_l() must be called with ThreadBase::mLock held
1881sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1882 const effect_uuid_t *type)
1883{
1884 size_t size = mEffects.size();
1885
1886 for (size_t i = 0; i < size; i++) {
1887 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1888 return mEffects[i];
1889 }
1890 }
1891 return 0;
1892}
1893
1894void AudioFlinger::EffectChain::clearInputBuffer()
1895{
1896 Mutex::Autolock _l(mLock);
1897 sp<ThreadBase> thread = mThread.promote();
1898 if (thread == 0) {
1899 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1900 return;
1901 }
1902 clearInputBuffer_l(thread);
1903}
1904
1905// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001906void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001907{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001908 if (mInBuffer == NULL) {
1909 return;
1910 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001911 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08001912 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07001913
Mikhail Naganov022b9952017-01-04 16:36:51 -08001914 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1915 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001916}
1917
1918// Must be called with EffectChain::mLock locked
1919void AudioFlinger::EffectChain::process_l()
1920{
1921 sp<ThreadBase> thread = mThread.promote();
1922 if (thread == 0) {
1923 ALOGW("process_l(): cannot promote mixer thread");
1924 return;
1925 }
1926 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1927 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001928 // never process effects when:
1929 // - on an OFFLOAD thread
1930 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001931 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1932 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001933 if (!isGlobalSession) {
1934 bool tracksOnSession = (trackCnt() != 0);
1935
1936 if (!tracksOnSession && mTailBufferCount == 0) {
1937 doProcess = false;
1938 }
1939
1940 if (activeTrackCnt() == 0) {
1941 // if no track is active and the effect tail has not been rendered,
1942 // the input buffer must be cleared here as the mixer process will not do it
1943 if (tracksOnSession || mTailBufferCount > 0) {
1944 clearInputBuffer_l(thread);
1945 if (mTailBufferCount > 0) {
1946 mTailBufferCount--;
1947 }
1948 }
1949 }
1950 }
1951
1952 size_t size = mEffects.size();
1953 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001954 // Only the input and output buffers of the chain can be external,
1955 // and 'update' / 'commit' do nothing for allocated buffers, thus
1956 // it's not needed to consider any other buffers here.
1957 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001958 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1959 mOutBuffer->update();
1960 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001961 for (size_t i = 0; i < size; i++) {
1962 mEffects[i]->process();
1963 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001964 mInBuffer->commit();
1965 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1966 mOutBuffer->commit();
1967 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001968 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001969 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001970 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001971 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1972 }
1973 if (doResetVolume) {
1974 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001975 }
1976}
1977
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001978// createEffect_l() must be called with ThreadBase::mLock held
1979status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1980 ThreadBase *thread,
1981 effect_descriptor_t *desc,
1982 int id,
1983 audio_session_t sessionId,
1984 bool pinned)
1985{
1986 Mutex::Autolock _l(mLock);
1987 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1988 status_t lStatus = effect->status();
1989 if (lStatus == NO_ERROR) {
1990 lStatus = addEffect_ll(effect);
1991 }
1992 if (lStatus != NO_ERROR) {
1993 effect.clear();
1994 }
1995 return lStatus;
1996}
1997
1998// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001999status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2000{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002001 Mutex::Autolock _l(mLock);
2002 return addEffect_ll(effect);
2003}
2004// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2005status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2006{
Eric Laurentca7cc822012-11-19 14:55:58 -08002007 effect_descriptor_t desc = effect->desc();
2008 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2009
Eric Laurentca7cc822012-11-19 14:55:58 -08002010 effect->setChain(this);
2011 sp<ThreadBase> thread = mThread.promote();
2012 if (thread == 0) {
2013 return NO_INIT;
2014 }
2015 effect->setThread(thread);
2016
2017 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2018 // Auxiliary effects are inserted at the beginning of mEffects vector as
2019 // they are processed first and accumulated in chain input buffer
2020 mEffects.insertAt(effect, 0);
2021
2022 // the input buffer for auxiliary effect contains mono samples in
2023 // 32 bit format. This is to avoid saturation in AudoMixer
2024 // accumulation stage. Saturation is done in EffectModule::process() before
2025 // calling the process in effect engine
2026 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002027 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002028#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002029 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002030 numSamples * sizeof(float), &halBuffer);
2031#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002032 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002033 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002034#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002035 if (result != OK) return result;
2036 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002037 // auxiliary effects output samples to chain input buffer for further processing
2038 // by insert effects
2039 effect->setOutBuffer(mInBuffer);
2040 } else {
2041 // Insert effects are inserted at the end of mEffects vector as they are processed
2042 // after track and auxiliary effects.
2043 // Insert effect order as a function of indicated preference:
2044 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2045 // another effect is present
2046 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2047 // last effect claiming first position
2048 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2049 // first effect claiming last position
2050 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2051 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2052 // already present
2053
2054 size_t size = mEffects.size();
2055 size_t idx_insert = size;
2056 ssize_t idx_insert_first = -1;
2057 ssize_t idx_insert_last = -1;
2058
2059 for (size_t i = 0; i < size; i++) {
2060 effect_descriptor_t d = mEffects[i]->desc();
2061 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2062 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2063 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2064 // check invalid effect chaining combinations
2065 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2066 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2067 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2068 desc.name, d.name);
2069 return INVALID_OPERATION;
2070 }
2071 // remember position of first insert effect and by default
2072 // select this as insert position for new effect
2073 if (idx_insert == size) {
2074 idx_insert = i;
2075 }
2076 // remember position of last insert effect claiming
2077 // first position
2078 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2079 idx_insert_first = i;
2080 }
2081 // remember position of first insert effect claiming
2082 // last position
2083 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2084 idx_insert_last == -1) {
2085 idx_insert_last = i;
2086 }
2087 }
2088 }
2089
2090 // modify idx_insert from first position if needed
2091 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2092 if (idx_insert_last != -1) {
2093 idx_insert = idx_insert_last;
2094 } else {
2095 idx_insert = size;
2096 }
2097 } else {
2098 if (idx_insert_first != -1) {
2099 idx_insert = idx_insert_first + 1;
2100 }
2101 }
2102
2103 // always read samples from chain input buffer
2104 effect->setInBuffer(mInBuffer);
2105
2106 // if last effect in the chain, output samples to chain
2107 // output buffer, otherwise to chain input buffer
2108 if (idx_insert == size) {
2109 if (idx_insert != 0) {
2110 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2111 mEffects[idx_insert-1]->configure();
2112 }
2113 effect->setOutBuffer(mOutBuffer);
2114 } else {
2115 effect->setOutBuffer(mInBuffer);
2116 }
2117 mEffects.insertAt(effect, idx_insert);
2118
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002119 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002120 idx_insert);
2121 }
2122 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002123
Eric Laurentca7cc822012-11-19 14:55:58 -08002124 return NO_ERROR;
2125}
2126
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002127// removeEffect_l() must be called with ThreadBase::mLock held
2128size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2129 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002130{
2131 Mutex::Autolock _l(mLock);
2132 size_t size = mEffects.size();
2133 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2134
2135 for (size_t i = 0; i < size; i++) {
2136 if (effect == mEffects[i]) {
2137 // calling stop here will remove pre-processing effect from the audio HAL.
2138 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2139 // the middle of a read from audio HAL
2140 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2141 mEffects[i]->state() == EffectModule::STOPPING) {
2142 mEffects[i]->stop();
2143 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002144 if (release) {
2145 mEffects[i]->release_l();
2146 }
2147
Mikhail Naganov022b9952017-01-04 16:36:51 -08002148 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002149 if (i == size - 1 && i != 0) {
2150 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2151 mEffects[i - 1]->configure();
2152 }
2153 }
2154 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002155 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002156 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002157
Eric Laurentca7cc822012-11-19 14:55:58 -08002158 break;
2159 }
2160 }
2161
2162 return mEffects.size();
2163}
2164
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002165// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002166void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2167{
2168 size_t size = mEffects.size();
2169 for (size_t i = 0; i < size; i++) {
2170 mEffects[i]->setDevice(device);
2171 }
2172}
2173
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002174// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002175void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2176{
2177 size_t size = mEffects.size();
2178 for (size_t i = 0; i < size; i++) {
2179 mEffects[i]->setMode(mode);
2180 }
2181}
2182
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002183// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002184void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2185{
2186 size_t size = mEffects.size();
2187 for (size_t i = 0; i < size; i++) {
2188 mEffects[i]->setAudioSource(source);
2189 }
2190}
2191
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002192// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002193bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002194{
2195 uint32_t newLeft = *left;
2196 uint32_t newRight = *right;
2197 bool hasControl = false;
2198 int ctrlIdx = -1;
2199 size_t size = mEffects.size();
2200
2201 // first update volume controller
2202 for (size_t i = size; i > 0; i--) {
2203 if (mEffects[i - 1]->isProcessEnabled() &&
2204 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2205 ctrlIdx = i - 1;
2206 hasControl = true;
2207 break;
2208 }
2209 }
2210
Eric Laurentfa1e1232016-08-02 19:01:49 -07002211 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002212 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002213 if (hasControl) {
2214 *left = mNewLeftVolume;
2215 *right = mNewRightVolume;
2216 }
2217 return hasControl;
2218 }
2219
2220 mVolumeCtrlIdx = ctrlIdx;
2221 mLeftVolume = newLeft;
2222 mRightVolume = newRight;
2223
2224 // second get volume update from volume controller
2225 if (ctrlIdx >= 0) {
2226 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2227 mNewLeftVolume = newLeft;
2228 mNewRightVolume = newRight;
2229 }
2230 // then indicate volume to all other effects in chain.
2231 // Pass altered volume to effects before volume controller
2232 // and requested volume to effects after controller
2233 uint32_t lVol = newLeft;
2234 uint32_t rVol = newRight;
2235
2236 for (size_t i = 0; i < size; i++) {
2237 if ((int)i == ctrlIdx) {
2238 continue;
2239 }
2240 // this also works for ctrlIdx == -1 when there is no volume controller
2241 if ((int)i > ctrlIdx) {
2242 lVol = *left;
2243 rVol = *right;
2244 }
2245 mEffects[i]->setVolume(&lVol, &rVol, false);
2246 }
2247 *left = newLeft;
2248 *right = newRight;
2249
2250 return hasControl;
2251}
2252
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002253// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002254void AudioFlinger::EffectChain::resetVolume_l()
2255{
Eric Laurente7449bf2016-08-03 18:44:07 -07002256 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2257 uint32_t left = mLeftVolume;
2258 uint32_t right = mRightVolume;
2259 (void)setVolume_l(&left, &right, true);
2260 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002261}
2262
Eric Laurent1b928682014-10-02 19:41:47 -07002263void AudioFlinger::EffectChain::syncHalEffectsState()
2264{
2265 Mutex::Autolock _l(mLock);
2266 for (size_t i = 0; i < mEffects.size(); i++) {
2267 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2268 mEffects[i]->state() == EffectModule::STOPPING) {
2269 mEffects[i]->addEffectToHal_l();
2270 }
2271 }
2272}
2273
Eric Laurentca7cc822012-11-19 14:55:58 -08002274void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2275{
2276 const size_t SIZE = 256;
2277 char buffer[SIZE];
2278 String8 result;
2279
Marco Nelissenb2208842014-02-07 14:00:50 -08002280 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002281 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002282 result.append(buffer);
2283
Marco Nelissenb2208842014-02-07 14:00:50 -08002284 if (numEffects) {
2285 bool locked = AudioFlinger::dumpTryLock(mLock);
2286 // failed to lock - AudioFlinger is probably deadlocked
2287 if (!locked) {
2288 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002289 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002290
Andy Hungbded9c82017-11-30 18:47:35 -08002291 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2292 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2293 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2294 (int)inBufferStr.size(), "In buffer ",
2295 (int)outBufferStr.size(), "Out buffer ");
2296 result.appendFormat("\t%s %s %d\n",
2297 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002298 write(fd, result.string(), result.size());
2299
2300 for (size_t i = 0; i < numEffects; ++i) {
2301 sp<EffectModule> effect = mEffects[i];
2302 if (effect != 0) {
2303 effect->dump(fd, args);
2304 }
2305 }
2306
2307 if (locked) {
2308 mLock.unlock();
2309 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002310 }
2311}
2312
2313// must be called with ThreadBase::mLock held
2314void AudioFlinger::EffectChain::setEffectSuspended_l(
2315 const effect_uuid_t *type, bool suspend)
2316{
2317 sp<SuspendedEffectDesc> desc;
2318 // use effect type UUID timelow as key as there is no real risk of identical
2319 // timeLow fields among effect type UUIDs.
2320 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2321 if (suspend) {
2322 if (index >= 0) {
2323 desc = mSuspendedEffects.valueAt(index);
2324 } else {
2325 desc = new SuspendedEffectDesc();
2326 desc->mType = *type;
2327 mSuspendedEffects.add(type->timeLow, desc);
2328 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2329 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002330
Eric Laurentca7cc822012-11-19 14:55:58 -08002331 if (desc->mRefCount++ == 0) {
2332 sp<EffectModule> effect = getEffectIfEnabled(type);
2333 if (effect != 0) {
2334 desc->mEffect = effect;
2335 effect->setSuspended(true);
2336 effect->setEnabled(false);
2337 }
2338 }
2339 } else {
2340 if (index < 0) {
2341 return;
2342 }
2343 desc = mSuspendedEffects.valueAt(index);
2344 if (desc->mRefCount <= 0) {
2345 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002346 desc->mRefCount = 0;
2347 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002348 }
2349 if (--desc->mRefCount == 0) {
2350 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2351 if (desc->mEffect != 0) {
2352 sp<EffectModule> effect = desc->mEffect.promote();
2353 if (effect != 0) {
2354 effect->setSuspended(false);
2355 effect->lock();
2356 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002357 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002358 effect->setEnabled_l(handle->enabled());
2359 }
2360 effect->unlock();
2361 }
2362 desc->mEffect.clear();
2363 }
2364 mSuspendedEffects.removeItemsAt(index);
2365 }
2366 }
2367}
2368
2369// must be called with ThreadBase::mLock held
2370void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2371{
2372 sp<SuspendedEffectDesc> desc;
2373
2374 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2375 if (suspend) {
2376 if (index >= 0) {
2377 desc = mSuspendedEffects.valueAt(index);
2378 } else {
2379 desc = new SuspendedEffectDesc();
2380 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2381 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2382 }
2383 if (desc->mRefCount++ == 0) {
2384 Vector< sp<EffectModule> > effects;
2385 getSuspendEligibleEffects(effects);
2386 for (size_t i = 0; i < effects.size(); i++) {
2387 setEffectSuspended_l(&effects[i]->desc().type, true);
2388 }
2389 }
2390 } else {
2391 if (index < 0) {
2392 return;
2393 }
2394 desc = mSuspendedEffects.valueAt(index);
2395 if (desc->mRefCount <= 0) {
2396 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2397 desc->mRefCount = 1;
2398 }
2399 if (--desc->mRefCount == 0) {
2400 Vector<const effect_uuid_t *> types;
2401 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2402 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2403 continue;
2404 }
2405 types.add(&mSuspendedEffects.valueAt(i)->mType);
2406 }
2407 for (size_t i = 0; i < types.size(); i++) {
2408 setEffectSuspended_l(types[i], false);
2409 }
2410 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2411 mSuspendedEffects.keyAt(index));
2412 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2413 }
2414 }
2415}
2416
2417
2418// The volume effect is used for automated tests only
2419#ifndef OPENSL_ES_H_
2420static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2421 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2422const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2423#endif //OPENSL_ES_H_
2424
Eric Laurentd8365c52017-07-16 15:27:05 -07002425/* static */
2426bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2427{
2428 // Only NS and AEC are suspended when BtNRec is off
2429 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2430 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2431 return true;
2432 }
2433 return false;
2434}
2435
Eric Laurentca7cc822012-11-19 14:55:58 -08002436bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2437{
2438 // auxiliary effects and visualizer are never suspended on output mix
2439 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2440 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2441 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2442 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2443 return false;
2444 }
2445 return true;
2446}
2447
2448void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2449 Vector< sp<AudioFlinger::EffectModule> > &effects)
2450{
2451 effects.clear();
2452 for (size_t i = 0; i < mEffects.size(); i++) {
2453 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2454 effects.add(mEffects[i]);
2455 }
2456 }
2457}
2458
2459sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2460 const effect_uuid_t *type)
2461{
2462 sp<EffectModule> effect = getEffectFromType_l(type);
2463 return effect != 0 && effect->isEnabled() ? effect : 0;
2464}
2465
2466void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2467 bool enabled)
2468{
2469 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2470 if (enabled) {
2471 if (index < 0) {
2472 // if the effect is not suspend check if all effects are suspended
2473 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2474 if (index < 0) {
2475 return;
2476 }
2477 if (!isEffectEligibleForSuspend(effect->desc())) {
2478 return;
2479 }
2480 setEffectSuspended_l(&effect->desc().type, enabled);
2481 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2482 if (index < 0) {
2483 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2484 return;
2485 }
2486 }
2487 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2488 effect->desc().type.timeLow);
2489 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002490 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002491 if (desc->mEffect == 0) {
2492 desc->mEffect = effect;
2493 effect->setEnabled(false);
2494 effect->setSuspended(true);
2495 }
2496 } else {
2497 if (index < 0) {
2498 return;
2499 }
2500 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2501 effect->desc().type.timeLow);
2502 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2503 desc->mEffect.clear();
2504 effect->setSuspended(false);
2505 }
2506}
2507
Eric Laurent5baf2af2013-09-12 17:37:00 -07002508bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002509{
2510 Mutex::Autolock _l(mLock);
2511 size_t size = mEffects.size();
2512 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002513 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002514 return true;
2515 }
2516 }
2517 return false;
2518}
2519
Eric Laurentaaa44472014-09-12 17:41:50 -07002520void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2521{
2522 Mutex::Autolock _l(mLock);
2523 mThread = thread;
2524 for (size_t i = 0; i < mEffects.size(); i++) {
2525 mEffects[i]->setThread(thread);
2526 }
2527}
2528
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002529void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2530{
2531 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2532 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2533 }
2534 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2535 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2536 }
2537}
2538
2539void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2540{
2541 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2542 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2543 }
2544 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2545 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2546 }
2547}
2548
2549bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002550{
2551 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002552 for (const auto &effect : mEffects) {
2553 if (effect->isProcessImplemented()) {
2554 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002555 }
2556 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002557 // Allow effects without processing.
2558 return true;
2559}
2560
2561bool AudioFlinger::EffectChain::isFastCompatible() const
2562{
2563 Mutex::Autolock _l(mLock);
2564 for (const auto &effect : mEffects) {
2565 if (effect->isProcessImplemented()
2566 && effect->isImplementationSoftware()) {
2567 return false;
2568 }
2569 }
2570 // Allow effects without processing or hw accelerated effects.
2571 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002572}
2573
2574// isCompatibleWithThread_l() must be called with thread->mLock held
2575bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2576{
2577 Mutex::Autolock _l(mLock);
2578 for (size_t i = 0; i < mEffects.size(); i++) {
2579 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2580 return false;
2581 }
2582 }
2583 return true;
2584}
2585
Glenn Kasten63238ef2015-03-02 15:50:29 -08002586} // namespace android