blob: 979290feaa437fd59360cb6ebdcb98e8bd445f0f [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 &&
594 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
595 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
596 // Older effects may require exact STEREO position mask.
597 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
598 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
599 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
600 }
601 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
602 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
603 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
604 }
605 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700606 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800607 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700608 &mConfig,
609 &size,
610 &cmdStatus);
611 if (status == NO_ERROR) {
612 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800613 }
614 }
615#endif
616
617#ifdef FLOAT_EFFECT_CHAIN
618 if (status == NO_ERROR) {
619 mSupportsFloat = true;
620 }
621
622 if (status != NO_ERROR) {
623 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
624 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
625 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
626 size = sizeof(int);
627 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
628 sizeof(mConfig),
629 &mConfig,
630 &size,
631 &cmdStatus);
632 if (status == NO_ERROR) {
633 status = cmdStatus;
634 }
635 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700636 mSupportsFloat = false;
637 ALOGVV("config worked with 16 bit");
638 } else {
639 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800640 }
rago94a1ee82017-07-21 15:11:02 -0700641 }
642#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800643
rago94a1ee82017-07-21 15:11:02 -0700644 if (status == NO_ERROR) {
645 // Establish Buffer strategy
646 setInBuffer(mInBuffer);
647 setOutBuffer(mOutBuffer);
648
649 // Update visualizer latency
650 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
651 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
652 effect_param_t *p = (effect_param_t *)buf32;
653
654 p->psize = sizeof(uint32_t);
655 p->vsize = sizeof(uint32_t);
656 size = sizeof(int);
657 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
658
659 uint32_t latency = 0;
660 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
661 if (pbt != NULL) {
662 latency = pbt->latency_l();
663 }
664
665 *((int32_t *)p->data + 1)= latency;
666 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
667 sizeof(effect_param_t) + 8,
668 &buf32,
669 &size,
670 &cmdStatus);
671 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800672 }
673
Andy Hung05083ac2017-12-14 15:00:28 -0800674 // mConfig.outputCfg.buffer.frameCount cannot be zero.
675 mMaxDisableWaitCnt = (uint32_t)std::max(
676 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
677 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
678 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800679
Eric Laurentd0ebb532013-04-02 16:41:41 -0700680exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800681 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700682 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700683 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800684 return status;
685}
686
687status_t AudioFlinger::EffectModule::init()
688{
689 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700690 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800691 return NO_INIT;
692 }
693 status_t cmdStatus;
694 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700695 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
696 0,
697 NULL,
698 &size,
699 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800700 if (status == 0) {
701 status = cmdStatus;
702 }
703 return status;
704}
705
Eric Laurent1b928682014-10-02 19:41:47 -0700706void AudioFlinger::EffectModule::addEffectToHal_l()
707{
708 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
709 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
710 sp<ThreadBase> thread = mThread.promote();
711 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700712 sp<StreamHalInterface> stream = thread->stream();
713 if (stream != 0) {
714 status_t result = stream->addEffect(mEffectInterface);
715 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700716 }
717 }
718 }
719}
720
Eric Laurentfa1e1232016-08-02 19:01:49 -0700721// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800722status_t AudioFlinger::EffectModule::start()
723{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700724 sp<EffectChain> chain;
725 status_t status;
726 {
727 Mutex::Autolock _l(mLock);
728 status = start_l();
729 if (status == NO_ERROR) {
730 chain = mChain.promote();
731 }
732 }
733 if (chain != 0) {
734 chain->resetVolume_l();
735 }
736 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800737}
738
739status_t AudioFlinger::EffectModule::start_l()
740{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700741 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800742 return NO_INIT;
743 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700744 if (mStatus != NO_ERROR) {
745 return mStatus;
746 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800747 status_t cmdStatus;
748 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700749 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
750 0,
751 NULL,
752 &size,
753 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800754 if (status == 0) {
755 status = cmdStatus;
756 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700757 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700758 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800759 }
760 return status;
761}
762
763status_t AudioFlinger::EffectModule::stop()
764{
765 Mutex::Autolock _l(mLock);
766 return stop_l();
767}
768
769status_t AudioFlinger::EffectModule::stop_l()
770{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700771 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800772 return NO_INIT;
773 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700774 if (mStatus != NO_ERROR) {
775 return mStatus;
776 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800777 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800778 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700779 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
780 0,
781 NULL,
782 &size,
783 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800784 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800785 status = cmdStatus;
786 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800787 if (status == NO_ERROR) {
788 status = remove_effect_from_hal_l();
789 }
790 return status;
791}
792
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800793// must be called with EffectChain::mLock held
794void AudioFlinger::EffectModule::release_l()
795{
796 if (mEffectInterface != 0) {
797 remove_effect_from_hal_l();
798 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800799 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800800 mEffectInterface.clear();
801 }
802}
803
Eric Laurentbfb1b832013-01-07 09:53:42 -0800804status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
805{
806 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
807 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800808 sp<ThreadBase> thread = mThread.promote();
809 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700810 sp<StreamHalInterface> stream = thread->stream();
811 if (stream != 0) {
812 status_t result = stream->removeEffect(mEffectInterface);
813 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800814 }
815 }
816 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800817 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800818}
819
Andy Hunge4a1d912016-08-17 14:11:13 -0700820// round up delta valid if value and divisor are positive.
821template <typename T>
822static T roundUpDelta(const T &value, const T &divisor) {
823 T remainder = value % divisor;
824 return remainder == 0 ? 0 : divisor - remainder;
825}
826
Eric Laurentca7cc822012-11-19 14:55:58 -0800827status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
828 uint32_t cmdSize,
829 void *pCmdData,
830 uint32_t *replySize,
831 void *pReplyData)
832{
833 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700834 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800835
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700836 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800837 return NO_INIT;
838 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700839 if (mStatus != NO_ERROR) {
840 return mStatus;
841 }
Andy Hung110bc952016-06-20 15:22:52 -0700842 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700843 (sizeof(effect_param_t) > cmdSize ||
844 ((effect_param_t *)pCmdData)->psize > cmdSize
845 - sizeof(effect_param_t))) {
846 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800847 android_errorWriteLog(0x534e4554, "33003822");
848 return -EINVAL;
849 }
850 if (cmdCode == EFFECT_CMD_GET_PARAM &&
851 (*replySize < sizeof(effect_param_t) ||
852 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
853 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700854 return -EINVAL;
855 }
ragoe2759072016-11-22 18:02:48 -0800856 if (cmdCode == EFFECT_CMD_GET_PARAM &&
857 (sizeof(effect_param_t) > *replySize
858 || ((effect_param_t *)pCmdData)->psize > *replySize
859 - sizeof(effect_param_t)
860 || ((effect_param_t *)pCmdData)->vsize > *replySize
861 - sizeof(effect_param_t)
862 - ((effect_param_t *)pCmdData)->psize
863 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
864 *replySize
865 - sizeof(effect_param_t)
866 - ((effect_param_t *)pCmdData)->psize
867 - ((effect_param_t *)pCmdData)->vsize)) {
868 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
869 android_errorWriteLog(0x534e4554, "32705438");
870 return -EINVAL;
871 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700872 if ((cmdCode == EFFECT_CMD_SET_PARAM
873 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
874 (sizeof(effect_param_t) > cmdSize
875 || ((effect_param_t *)pCmdData)->psize > cmdSize
876 - sizeof(effect_param_t)
877 || ((effect_param_t *)pCmdData)->vsize > cmdSize
878 - sizeof(effect_param_t)
879 - ((effect_param_t *)pCmdData)->psize
880 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
881 cmdSize
882 - sizeof(effect_param_t)
883 - ((effect_param_t *)pCmdData)->psize
884 - ((effect_param_t *)pCmdData)->vsize)) {
885 android_errorWriteLog(0x534e4554, "30204301");
886 return -EINVAL;
887 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700888 status_t status = mEffectInterface->command(cmdCode,
889 cmdSize,
890 pCmdData,
891 replySize,
892 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800893 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
894 uint32_t size = (replySize == NULL) ? 0 : *replySize;
895 for (size_t i = 1; i < mHandles.size(); i++) {
896 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800897 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800898 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
899 }
900 }
901 }
902 return status;
903}
904
905status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
906{
907 Mutex::Autolock _l(mLock);
908 return setEnabled_l(enabled);
909}
910
911// must be called with EffectModule::mLock held
912status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
913{
914
915 ALOGV("setEnabled %p enabled %d", this, enabled);
916
917 if (enabled != isEnabled()) {
918 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
919 if (enabled && status != NO_ERROR) {
920 return status;
921 }
922
923 switch (mState) {
924 // going from disabled to enabled
925 case IDLE:
926 mState = STARTING;
927 break;
928 case STOPPED:
929 mState = RESTART;
930 break;
931 case STOPPING:
932 mState = ACTIVE;
933 break;
934
935 // going from enabled to disabled
936 case RESTART:
937 mState = STOPPED;
938 break;
939 case STARTING:
940 mState = IDLE;
941 break;
942 case ACTIVE:
943 mState = STOPPING;
944 break;
945 case DESTROYED:
946 return NO_ERROR; // simply ignore as we are being destroyed
947 }
948 for (size_t i = 1; i < mHandles.size(); i++) {
949 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800950 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800951 h->setEnabled(enabled);
952 }
953 }
954 }
955 return NO_ERROR;
956}
957
958bool AudioFlinger::EffectModule::isEnabled() const
959{
960 switch (mState) {
961 case RESTART:
962 case STARTING:
963 case ACTIVE:
964 return true;
965 case IDLE:
966 case STOPPING:
967 case STOPPED:
968 case DESTROYED:
969 default:
970 return false;
971 }
972}
973
974bool AudioFlinger::EffectModule::isProcessEnabled() const
975{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700976 if (mStatus != NO_ERROR) {
977 return false;
978 }
979
Eric Laurentca7cc822012-11-19 14:55:58 -0800980 switch (mState) {
981 case RESTART:
982 case ACTIVE:
983 case STOPPING:
984 case STOPPED:
985 return true;
986 case IDLE:
987 case STARTING:
988 case DESTROYED:
989 default:
990 return false;
991 }
992}
993
Mikhail Naganov022b9952017-01-04 16:36:51 -0800994void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700995 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800996
997 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800998 if (buffer != 0) {
999 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1000 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1001 } else {
1002 mConfig.inputCfg.buffer.raw = NULL;
1003 }
1004 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001005 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001006
1007#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001008 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001009 // Theoretically insert effects can also do in-place conversions (destroying
1010 // the original buffer) when the output buffer is identical to the input buffer,
1011 // but we don't optimize for it here.
1012 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001013 const uint32_t inChannelCount =
1014 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1015 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1016 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001017 // we need to translate - create hidl shared buffer and intercept
1018 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001019 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1020 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1021 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001022
1023 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1024 __func__, inChannels, inFrameCount, size);
1025
Andy Hungbded9c82017-11-30 18:47:35 -08001026 if (size > 0 && (mInConversionBuffer.get() == nullptr
1027 || size > mInConversionBuffer->getSize())) {
1028 mInConversionBuffer.clear();
1029 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001030 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1031 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1032 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001033 }
Andy Hungbded9c82017-11-30 18:47:35 -08001034 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001035 mInConversionBuffer->setFrameCount(inFrameCount);
1036 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001037 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001038 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001039 }
1040 }
1041#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001042}
1043
1044void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001045 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001046
1047 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001048 if (buffer != 0) {
1049 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1050 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1051 } else {
1052 mConfig.outputCfg.buffer.raw = NULL;
1053 }
1054 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001055 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001056
1057#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001058 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001059 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001060 const uint32_t outChannelCount =
1061 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1062 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1063 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001064 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001065 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1066 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1067 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001068
1069 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1070 __func__, outChannels, outFrameCount, size);
1071
Andy Hungbded9c82017-11-30 18:47:35 -08001072 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1073 || size > mOutConversionBuffer->getSize())) {
1074 mOutConversionBuffer.clear();
1075 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001076 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1077 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1078 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001079 }
Andy Hungbded9c82017-11-30 18:47:35 -08001080 if (mOutConversionBuffer.get() != nullptr) {
1081 mOutConversionBuffer->setFrameCount(outFrameCount);
1082 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001083 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001084 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001085 }
1086 }
1087#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001088}
1089
Eric Laurentca7cc822012-11-19 14:55:58 -08001090status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1091{
1092 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001093 if (mStatus != NO_ERROR) {
1094 return mStatus;
1095 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001096 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001097 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1098 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1099 if (isProcessEnabled() &&
1100 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
1101 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001102 uint32_t volume[2];
1103 uint32_t *pVolume = NULL;
1104 uint32_t size = sizeof(volume);
1105 volume[0] = *left;
1106 volume[1] = *right;
1107 if (controller) {
1108 pVolume = volume;
1109 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001110 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1111 size,
1112 volume,
1113 &size,
1114 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001115 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1116 *left = volume[0];
1117 *right = volume[1];
1118 }
1119 }
1120 return status;
1121}
1122
1123status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1124{
1125 if (device == AUDIO_DEVICE_NONE) {
1126 return NO_ERROR;
1127 }
1128
1129 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001130 if (mStatus != NO_ERROR) {
1131 return mStatus;
1132 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001133 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001134 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001135 status_t cmdStatus;
1136 uint32_t size = sizeof(status_t);
1137 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1138 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001139 status = mEffectInterface->command(cmd,
1140 sizeof(uint32_t),
1141 &device,
1142 &size,
1143 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001144 }
1145 return status;
1146}
1147
1148status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1149{
1150 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001151 if (mStatus != NO_ERROR) {
1152 return mStatus;
1153 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001154 status_t status = NO_ERROR;
1155 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1156 status_t cmdStatus;
1157 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001158 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1159 sizeof(audio_mode_t),
1160 &mode,
1161 &size,
1162 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001163 if (status == NO_ERROR) {
1164 status = cmdStatus;
1165 }
1166 }
1167 return status;
1168}
1169
1170status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1171{
1172 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001173 if (mStatus != NO_ERROR) {
1174 return mStatus;
1175 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001176 status_t status = NO_ERROR;
1177 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1178 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001179 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1180 sizeof(audio_source_t),
1181 &source,
1182 &size,
1183 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001184 }
1185 return status;
1186}
1187
1188void AudioFlinger::EffectModule::setSuspended(bool suspended)
1189{
1190 Mutex::Autolock _l(mLock);
1191 mSuspended = suspended;
1192}
1193
1194bool AudioFlinger::EffectModule::suspended() const
1195{
1196 Mutex::Autolock _l(mLock);
1197 return mSuspended;
1198}
1199
1200bool AudioFlinger::EffectModule::purgeHandles()
1201{
1202 bool enabled = false;
1203 Mutex::Autolock _l(mLock);
1204 for (size_t i = 0; i < mHandles.size(); i++) {
1205 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001206 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001207 if (handle->hasControl()) {
1208 enabled = handle->enabled();
1209 }
1210 }
1211 }
1212 return enabled;
1213}
1214
Eric Laurent5baf2af2013-09-12 17:37:00 -07001215status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1216{
1217 Mutex::Autolock _l(mLock);
1218 if (mStatus != NO_ERROR) {
1219 return mStatus;
1220 }
1221 status_t status = NO_ERROR;
1222 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1223 status_t cmdStatus;
1224 uint32_t size = sizeof(status_t);
1225 effect_offload_param_t cmd;
1226
1227 cmd.isOffload = offloaded;
1228 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001229 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1230 sizeof(effect_offload_param_t),
1231 &cmd,
1232 &size,
1233 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001234 if (status == NO_ERROR) {
1235 status = cmdStatus;
1236 }
1237 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1238 } else {
1239 if (offloaded) {
1240 status = INVALID_OPERATION;
1241 }
1242 mOffloaded = false;
1243 }
1244 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1245 return status;
1246}
1247
1248bool AudioFlinger::EffectModule::isOffloaded() const
1249{
1250 Mutex::Autolock _l(mLock);
1251 return mOffloaded;
1252}
1253
Marco Nelissenb2208842014-02-07 14:00:50 -08001254String8 effectFlagsToString(uint32_t flags) {
1255 String8 s;
1256
1257 s.append("conn. mode: ");
1258 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1259 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1260 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1261 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1262 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1263 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1264 default: s.append("unknown/reserved"); break;
1265 }
1266 s.append(", ");
1267
1268 s.append("insert pref: ");
1269 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1270 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1271 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1272 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1273 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1274 default: s.append("unknown/reserved"); break;
1275 }
1276 s.append(", ");
1277
1278 s.append("volume mgmt: ");
1279 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1280 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1281 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1282 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1283 default: s.append("unknown/reserved"); break;
1284 }
1285 s.append(", ");
1286
1287 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1288 if (devind) {
1289 s.append("device indication: ");
1290 switch (devind) {
1291 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1292 default: s.append("unknown/reserved"); break;
1293 }
1294 s.append(", ");
1295 }
1296
1297 s.append("input mode: ");
1298 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1299 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1300 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1301 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1302 default: s.append("not set"); break;
1303 }
1304 s.append(", ");
1305
1306 s.append("output mode: ");
1307 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1308 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1309 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1310 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1311 default: s.append("not set"); break;
1312 }
1313 s.append(", ");
1314
1315 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1316 if (accel) {
1317 s.append("hardware acceleration: ");
1318 switch (accel) {
1319 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1320 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1321 default: s.append("unknown/reserved"); break;
1322 }
1323 s.append(", ");
1324 }
1325
1326 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1327 if (modeind) {
1328 s.append("mode indication: ");
1329 switch (modeind) {
1330 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1331 default: s.append("unknown/reserved"); break;
1332 }
1333 s.append(", ");
1334 }
1335
1336 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1337 if (srcind) {
1338 s.append("source indication: ");
1339 switch (srcind) {
1340 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1341 default: s.append("unknown/reserved"); break;
1342 }
1343 s.append(", ");
1344 }
1345
1346 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1347 s.append("offloadable, ");
1348 }
1349
1350 int len = s.length();
1351 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001352 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001353 s.unlockBuffer(len - 2);
1354 }
1355 return s;
1356}
1357
Andy Hungbded9c82017-11-30 18:47:35 -08001358static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1359 std::stringstream ss;
1360
1361 if (buffer.get() == nullptr) {
1362 return "nullptr"; // make different than below
1363 } else if (buffer->externalData() != nullptr) {
1364 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1365 << " -> "
1366 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1367 } else {
1368 ss << buffer->audioBuffer()->raw;
1369 }
1370 return ss.str();
1371}
Marco Nelissenb2208842014-02-07 14:00:50 -08001372
Glenn Kasten0f11b512014-01-31 16:18:54 -08001373void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001374{
Eric Laurentca7cc822012-11-19 14:55:58 -08001375 String8 result;
1376
Andy Hung9718d662017-12-22 17:57:39 -08001377 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001378
1379 bool locked = AudioFlinger::dumpTryLock(mLock);
1380 // failed to lock - AudioFlinger is probably deadlocked
1381 if (!locked) {
1382 result.append("\t\tCould not lock Fx mutex:\n");
1383 }
1384
1385 result.append("\t\tSession Status State Engine:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001386 result.appendFormat("\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001387 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001388
1389 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001390 char uuidStr[64];
1391 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001392 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001393 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001394 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1395 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001396 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001397 mDescriptor.flags,
1398 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001399 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001400 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001401
1402 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001403 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001404
1405 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001406
1407 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001408 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1409 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1410 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001411 mConfig.inputCfg.buffer.frameCount,
1412 mConfig.inputCfg.samplingRate,
1413 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001414 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001415 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001416
1417 result.append("\t\t- Output configuration:\n");
1418 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001419 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001420 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 mConfig.outputCfg.buffer.frameCount,
1422 mConfig.outputCfg.samplingRate,
1423 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001424 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001425 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001426
rago94a1ee82017-07-21 15:11:02 -07001427#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001428
Andy Hungbded9c82017-11-30 18:47:35 -08001429 result.appendFormat("\t\t- HAL buffers:\n"
1430 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1431 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1432 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1433 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1434 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001435#endif
1436
Andy Hung9718d662017-12-22 17:57:39 -08001437 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001438 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001439 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001440 for (size_t i = 0; i < mHandles.size(); ++i) {
1441 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001442 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001443 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001444 result.append(buffer);
1445 }
1446 }
1447
Eric Laurentca7cc822012-11-19 14:55:58 -08001448 write(fd, result.string(), result.length());
1449
1450 if (locked) {
1451 mLock.unlock();
1452 }
1453}
1454
1455// ----------------------------------------------------------------------------
1456// EffectHandle implementation
1457// ----------------------------------------------------------------------------
1458
1459#undef LOG_TAG
1460#define LOG_TAG "AudioFlinger::EffectHandle"
1461
1462AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1463 const sp<AudioFlinger::Client>& client,
1464 const sp<IEffectClient>& effectClient,
1465 int32_t priority)
1466 : BnEffect(),
1467 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001468 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001469{
1470 ALOGV("constructor %p", this);
1471
1472 if (client == 0) {
1473 return;
1474 }
1475 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1476 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001477 if (mCblkMemory == 0 ||
1478 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001479 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001480 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001481 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 return;
1483 }
Glenn Kastene75da402013-11-20 13:54:52 -08001484 new(mCblk) effect_param_cblk_t();
1485 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001486}
1487
1488AudioFlinger::EffectHandle::~EffectHandle()
1489{
1490 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001491 disconnect(false);
1492}
1493
Glenn Kastene75da402013-11-20 13:54:52 -08001494status_t AudioFlinger::EffectHandle::initCheck()
1495{
1496 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1497}
1498
Eric Laurentca7cc822012-11-19 14:55:58 -08001499status_t AudioFlinger::EffectHandle::enable()
1500{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001501 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001502 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001503 sp<EffectModule> effect = mEffect.promote();
1504 if (effect == 0 || mDisconnected) {
1505 return DEAD_OBJECT;
1506 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001507 if (!mHasControl) {
1508 return INVALID_OPERATION;
1509 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001510
1511 if (mEnabled) {
1512 return NO_ERROR;
1513 }
1514
1515 mEnabled = true;
1516
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001517 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001518 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001519 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001520 }
1521
1522 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001523 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001524 return NO_ERROR;
1525 }
1526
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001527 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001528 if (status != NO_ERROR) {
1529 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001530 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001531 }
1532 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001533 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001534 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001535 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1536 Mutex::Autolock _l(thread->mLock);
1537 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001538 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001539 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001540 if (thread->type() == ThreadBase::OFFLOAD) {
1541 PlaybackThread *t = (PlaybackThread *)thread.get();
1542 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1543 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001544 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001545 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1546 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001547 }
1548 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001549 }
1550 return status;
1551}
1552
1553status_t AudioFlinger::EffectHandle::disable()
1554{
1555 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001556 AutoMutex _l(mLock);
1557 sp<EffectModule> effect = mEffect.promote();
1558 if (effect == 0 || mDisconnected) {
1559 return DEAD_OBJECT;
1560 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001561 if (!mHasControl) {
1562 return INVALID_OPERATION;
1563 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001564
1565 if (!mEnabled) {
1566 return NO_ERROR;
1567 }
1568 mEnabled = false;
1569
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001570 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001571 return NO_ERROR;
1572 }
1573
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001574 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001575
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001576 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001577 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001578 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001579 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1580 Mutex::Autolock _l(thread->mLock);
1581 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001582 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001583 }
1584
1585 return status;
1586}
1587
1588void AudioFlinger::EffectHandle::disconnect()
1589{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001590 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001591 disconnect(true);
1592}
1593
1594void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1595{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001596 AutoMutex _l(mLock);
1597 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1598 if (mDisconnected) {
1599 if (unpinIfLast) {
1600 android_errorWriteLog(0x534e4554, "32707507");
1601 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001602 return;
1603 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001604 mDisconnected = true;
1605 sp<ThreadBase> thread;
1606 {
1607 sp<EffectModule> effect = mEffect.promote();
1608 if (effect != 0) {
1609 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001610 }
1611 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001612 if (thread != 0) {
1613 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001614 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001615 // try to cleanup as much as we can
1616 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001617 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1618 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001619 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001620 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001621
Eric Laurentca7cc822012-11-19 14:55:58 -08001622 if (mClient != 0) {
1623 if (mCblk != NULL) {
1624 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1625 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1626 }
1627 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001628 // Client destructor must run with AudioFlinger client mutex locked
1629 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001630 mClient.clear();
1631 }
1632}
1633
1634status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1635 uint32_t cmdSize,
1636 void *pCmdData,
1637 uint32_t *replySize,
1638 void *pReplyData)
1639{
1640 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001641 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001642
Eric Laurentc7ab3092017-06-15 18:43:46 -07001643 // reject commands reserved for internal use by audio framework if coming from outside
1644 // of audioserver
1645 switch(cmdCode) {
1646 case EFFECT_CMD_ENABLE:
1647 case EFFECT_CMD_DISABLE:
1648 case EFFECT_CMD_SET_PARAM:
1649 case EFFECT_CMD_SET_PARAM_DEFERRED:
1650 case EFFECT_CMD_SET_PARAM_COMMIT:
1651 case EFFECT_CMD_GET_PARAM:
1652 break;
1653 default:
1654 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1655 break;
1656 }
1657 android_errorWriteLog(0x534e4554, "62019992");
1658 return BAD_VALUE;
1659 }
1660
Eric Laurent1ffc5852016-12-15 14:46:09 -08001661 if (cmdCode == EFFECT_CMD_ENABLE) {
1662 if (*replySize < sizeof(int)) {
1663 android_errorWriteLog(0x534e4554, "32095713");
1664 return BAD_VALUE;
1665 }
1666 *(int *)pReplyData = NO_ERROR;
1667 *replySize = sizeof(int);
1668 return enable();
1669 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1670 if (*replySize < sizeof(int)) {
1671 android_errorWriteLog(0x534e4554, "32095713");
1672 return BAD_VALUE;
1673 }
1674 *(int *)pReplyData = NO_ERROR;
1675 *replySize = sizeof(int);
1676 return disable();
1677 }
1678
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001679 AutoMutex _l(mLock);
1680 sp<EffectModule> effect = mEffect.promote();
1681 if (effect == 0 || mDisconnected) {
1682 return DEAD_OBJECT;
1683 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001684 // only get parameter command is permitted for applications not controlling the effect
1685 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1686 return INVALID_OPERATION;
1687 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001688 if (mClient == 0) {
1689 return INVALID_OPERATION;
1690 }
1691
1692 // handle commands that are not forwarded transparently to effect engine
1693 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001694 if (*replySize < sizeof(int)) {
1695 android_errorWriteLog(0x534e4554, "32095713");
1696 return BAD_VALUE;
1697 }
1698 *(int *)pReplyData = NO_ERROR;
1699 *replySize = sizeof(int);
1700
Eric Laurentca7cc822012-11-19 14:55:58 -08001701 // No need to trylock() here as this function is executed in the binder thread serving a
1702 // particular client process: no risk to block the whole media server process or mixer
1703 // threads if we are stuck here
1704 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001705 // keep local copy of index in case of client corruption b/32220769
1706 const uint32_t clientIndex = mCblk->clientIndex;
1707 const uint32_t serverIndex = mCblk->serverIndex;
1708 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1709 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001710 mCblk->serverIndex = 0;
1711 mCblk->clientIndex = 0;
1712 return BAD_VALUE;
1713 }
1714 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001715 effect_param_t *param = NULL;
1716 for (uint32_t index = serverIndex; index < clientIndex;) {
1717 int *p = (int *)(mBuffer + index);
1718 const int size = *p++;
1719 if (size < 0
1720 || size > EFFECT_PARAM_BUFFER_SIZE
1721 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001722 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001723 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001724 break;
1725 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001726
1727 // copy to local memory in case of client corruption b/32220769
1728 param = (effect_param_t *)realloc(param, size);
1729 if (param == NULL) {
1730 ALOGW("command(): out of memory");
1731 status = NO_MEMORY;
1732 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001733 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001734 memcpy(param, p, size);
1735
1736 int reply = 0;
1737 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001738 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001739 size,
1740 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001741 &rsize,
1742 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001743
1744 // verify shared memory: server index shouldn't change; client index can't go back.
1745 if (serverIndex != mCblk->serverIndex
1746 || clientIndex > mCblk->clientIndex) {
1747 android_errorWriteLog(0x534e4554, "32220769");
1748 status = BAD_VALUE;
1749 break;
1750 }
1751
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 // stop at first error encountered
1753 if (ret != NO_ERROR) {
1754 status = ret;
1755 *(int *)pReplyData = reply;
1756 break;
1757 } else if (reply != NO_ERROR) {
1758 *(int *)pReplyData = reply;
1759 break;
1760 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001761 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001762 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001763 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001764 mCblk->serverIndex = 0;
1765 mCblk->clientIndex = 0;
1766 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001767 }
1768
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001769 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001770}
1771
1772void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1773{
1774 ALOGV("setControl %p control %d", this, hasControl);
1775
1776 mHasControl = hasControl;
1777 mEnabled = enabled;
1778
1779 if (signal && mEffectClient != 0) {
1780 mEffectClient->controlStatusChanged(hasControl);
1781 }
1782}
1783
1784void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1785 uint32_t cmdSize,
1786 void *pCmdData,
1787 uint32_t replySize,
1788 void *pReplyData)
1789{
1790 if (mEffectClient != 0) {
1791 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1792 }
1793}
1794
1795
1796
1797void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1798{
1799 if (mEffectClient != 0) {
1800 mEffectClient->enableStatusChanged(enabled);
1801 }
1802}
1803
1804status_t AudioFlinger::EffectHandle::onTransact(
1805 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1806{
1807 return BnEffect::onTransact(code, data, reply, flags);
1808}
1809
1810
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001811void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001812{
1813 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1814
Marco Nelissenb2208842014-02-07 14:00:50 -08001815 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 (mClient == 0) ? getpid_cached : mClient->pid(),
1817 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001818 mHasControl ? "yes" : "no",
1819 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001820 mCblk ? mCblk->clientIndex : 0,
1821 mCblk ? mCblk->serverIndex : 0
1822 );
1823
1824 if (locked) {
1825 mCblk->lock.unlock();
1826 }
1827}
1828
1829#undef LOG_TAG
1830#define LOG_TAG "AudioFlinger::EffectChain"
1831
1832AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001833 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001834 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001835 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001836 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001837{
1838 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1839 if (thread == NULL) {
1840 return;
1841 }
1842 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1843 thread->frameCount();
1844}
1845
1846AudioFlinger::EffectChain::~EffectChain()
1847{
Eric Laurentca7cc822012-11-19 14:55:58 -08001848}
1849
1850// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1851sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1852 effect_descriptor_t *descriptor)
1853{
1854 size_t size = mEffects.size();
1855
1856 for (size_t i = 0; i < size; i++) {
1857 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1858 return mEffects[i];
1859 }
1860 }
1861 return 0;
1862}
1863
1864// getEffectFromId_l() must be called with ThreadBase::mLock held
1865sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1866{
1867 size_t size = mEffects.size();
1868
1869 for (size_t i = 0; i < size; i++) {
1870 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1871 if (id == 0 || mEffects[i]->id() == id) {
1872 return mEffects[i];
1873 }
1874 }
1875 return 0;
1876}
1877
1878// getEffectFromType_l() must be called with ThreadBase::mLock held
1879sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1880 const effect_uuid_t *type)
1881{
1882 size_t size = mEffects.size();
1883
1884 for (size_t i = 0; i < size; i++) {
1885 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1886 return mEffects[i];
1887 }
1888 }
1889 return 0;
1890}
1891
1892void AudioFlinger::EffectChain::clearInputBuffer()
1893{
1894 Mutex::Autolock _l(mLock);
1895 sp<ThreadBase> thread = mThread.promote();
1896 if (thread == 0) {
1897 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1898 return;
1899 }
1900 clearInputBuffer_l(thread);
1901}
1902
1903// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001904void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001905{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001906 if (mInBuffer == NULL) {
1907 return;
1908 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001909 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08001910 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07001911
Mikhail Naganov022b9952017-01-04 16:36:51 -08001912 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1913 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001914}
1915
1916// Must be called with EffectChain::mLock locked
1917void AudioFlinger::EffectChain::process_l()
1918{
1919 sp<ThreadBase> thread = mThread.promote();
1920 if (thread == 0) {
1921 ALOGW("process_l(): cannot promote mixer thread");
1922 return;
1923 }
1924 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1925 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001926 // never process effects when:
1927 // - on an OFFLOAD thread
1928 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001929 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1930 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001931 if (!isGlobalSession) {
1932 bool tracksOnSession = (trackCnt() != 0);
1933
1934 if (!tracksOnSession && mTailBufferCount == 0) {
1935 doProcess = false;
1936 }
1937
1938 if (activeTrackCnt() == 0) {
1939 // if no track is active and the effect tail has not been rendered,
1940 // the input buffer must be cleared here as the mixer process will not do it
1941 if (tracksOnSession || mTailBufferCount > 0) {
1942 clearInputBuffer_l(thread);
1943 if (mTailBufferCount > 0) {
1944 mTailBufferCount--;
1945 }
1946 }
1947 }
1948 }
1949
1950 size_t size = mEffects.size();
1951 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001952 // Only the input and output buffers of the chain can be external,
1953 // and 'update' / 'commit' do nothing for allocated buffers, thus
1954 // it's not needed to consider any other buffers here.
1955 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001956 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1957 mOutBuffer->update();
1958 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001959 for (size_t i = 0; i < size; i++) {
1960 mEffects[i]->process();
1961 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001962 mInBuffer->commit();
1963 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1964 mOutBuffer->commit();
1965 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001966 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001967 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001968 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001969 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1970 }
1971 if (doResetVolume) {
1972 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001973 }
1974}
1975
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001976// createEffect_l() must be called with ThreadBase::mLock held
1977status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1978 ThreadBase *thread,
1979 effect_descriptor_t *desc,
1980 int id,
1981 audio_session_t sessionId,
1982 bool pinned)
1983{
1984 Mutex::Autolock _l(mLock);
1985 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1986 status_t lStatus = effect->status();
1987 if (lStatus == NO_ERROR) {
1988 lStatus = addEffect_ll(effect);
1989 }
1990 if (lStatus != NO_ERROR) {
1991 effect.clear();
1992 }
1993 return lStatus;
1994}
1995
1996// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001997status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1998{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001999 Mutex::Autolock _l(mLock);
2000 return addEffect_ll(effect);
2001}
2002// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2003status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2004{
Eric Laurentca7cc822012-11-19 14:55:58 -08002005 effect_descriptor_t desc = effect->desc();
2006 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2007
Eric Laurentca7cc822012-11-19 14:55:58 -08002008 effect->setChain(this);
2009 sp<ThreadBase> thread = mThread.promote();
2010 if (thread == 0) {
2011 return NO_INIT;
2012 }
2013 effect->setThread(thread);
2014
2015 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2016 // Auxiliary effects are inserted at the beginning of mEffects vector as
2017 // they are processed first and accumulated in chain input buffer
2018 mEffects.insertAt(effect, 0);
2019
2020 // the input buffer for auxiliary effect contains mono samples in
2021 // 32 bit format. This is to avoid saturation in AudoMixer
2022 // accumulation stage. Saturation is done in EffectModule::process() before
2023 // calling the process in effect engine
2024 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002025 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002026#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002027 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002028 numSamples * sizeof(float), &halBuffer);
2029#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002030 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002031 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002032#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002033 if (result != OK) return result;
2034 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002035 // auxiliary effects output samples to chain input buffer for further processing
2036 // by insert effects
2037 effect->setOutBuffer(mInBuffer);
2038 } else {
2039 // Insert effects are inserted at the end of mEffects vector as they are processed
2040 // after track and auxiliary effects.
2041 // Insert effect order as a function of indicated preference:
2042 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2043 // another effect is present
2044 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2045 // last effect claiming first position
2046 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2047 // first effect claiming last position
2048 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2049 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2050 // already present
2051
2052 size_t size = mEffects.size();
2053 size_t idx_insert = size;
2054 ssize_t idx_insert_first = -1;
2055 ssize_t idx_insert_last = -1;
2056
2057 for (size_t i = 0; i < size; i++) {
2058 effect_descriptor_t d = mEffects[i]->desc();
2059 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2060 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2061 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2062 // check invalid effect chaining combinations
2063 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2064 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2065 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2066 desc.name, d.name);
2067 return INVALID_OPERATION;
2068 }
2069 // remember position of first insert effect and by default
2070 // select this as insert position for new effect
2071 if (idx_insert == size) {
2072 idx_insert = i;
2073 }
2074 // remember position of last insert effect claiming
2075 // first position
2076 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2077 idx_insert_first = i;
2078 }
2079 // remember position of first insert effect claiming
2080 // last position
2081 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2082 idx_insert_last == -1) {
2083 idx_insert_last = i;
2084 }
2085 }
2086 }
2087
2088 // modify idx_insert from first position if needed
2089 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2090 if (idx_insert_last != -1) {
2091 idx_insert = idx_insert_last;
2092 } else {
2093 idx_insert = size;
2094 }
2095 } else {
2096 if (idx_insert_first != -1) {
2097 idx_insert = idx_insert_first + 1;
2098 }
2099 }
2100
2101 // always read samples from chain input buffer
2102 effect->setInBuffer(mInBuffer);
2103
2104 // if last effect in the chain, output samples to chain
2105 // output buffer, otherwise to chain input buffer
2106 if (idx_insert == size) {
2107 if (idx_insert != 0) {
2108 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2109 mEffects[idx_insert-1]->configure();
2110 }
2111 effect->setOutBuffer(mOutBuffer);
2112 } else {
2113 effect->setOutBuffer(mInBuffer);
2114 }
2115 mEffects.insertAt(effect, idx_insert);
2116
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002117 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002118 idx_insert);
2119 }
2120 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002121
Eric Laurentca7cc822012-11-19 14:55:58 -08002122 return NO_ERROR;
2123}
2124
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002125// removeEffect_l() must be called with ThreadBase::mLock held
2126size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2127 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002128{
2129 Mutex::Autolock _l(mLock);
2130 size_t size = mEffects.size();
2131 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2132
2133 for (size_t i = 0; i < size; i++) {
2134 if (effect == mEffects[i]) {
2135 // calling stop here will remove pre-processing effect from the audio HAL.
2136 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2137 // the middle of a read from audio HAL
2138 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2139 mEffects[i]->state() == EffectModule::STOPPING) {
2140 mEffects[i]->stop();
2141 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002142 if (release) {
2143 mEffects[i]->release_l();
2144 }
2145
Mikhail Naganov022b9952017-01-04 16:36:51 -08002146 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002147 if (i == size - 1 && i != 0) {
2148 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2149 mEffects[i - 1]->configure();
2150 }
2151 }
2152 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002153 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002154 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002155
Eric Laurentca7cc822012-11-19 14:55:58 -08002156 break;
2157 }
2158 }
2159
2160 return mEffects.size();
2161}
2162
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002163// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002164void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2165{
2166 size_t size = mEffects.size();
2167 for (size_t i = 0; i < size; i++) {
2168 mEffects[i]->setDevice(device);
2169 }
2170}
2171
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002172// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002173void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2174{
2175 size_t size = mEffects.size();
2176 for (size_t i = 0; i < size; i++) {
2177 mEffects[i]->setMode(mode);
2178 }
2179}
2180
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002181// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002182void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2183{
2184 size_t size = mEffects.size();
2185 for (size_t i = 0; i < size; i++) {
2186 mEffects[i]->setAudioSource(source);
2187 }
2188}
2189
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002190// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002191bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002192{
2193 uint32_t newLeft = *left;
2194 uint32_t newRight = *right;
2195 bool hasControl = false;
2196 int ctrlIdx = -1;
2197 size_t size = mEffects.size();
2198
2199 // first update volume controller
2200 for (size_t i = size; i > 0; i--) {
2201 if (mEffects[i - 1]->isProcessEnabled() &&
2202 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2203 ctrlIdx = i - 1;
2204 hasControl = true;
2205 break;
2206 }
2207 }
2208
Eric Laurentfa1e1232016-08-02 19:01:49 -07002209 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002210 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002211 if (hasControl) {
2212 *left = mNewLeftVolume;
2213 *right = mNewRightVolume;
2214 }
2215 return hasControl;
2216 }
2217
2218 mVolumeCtrlIdx = ctrlIdx;
2219 mLeftVolume = newLeft;
2220 mRightVolume = newRight;
2221
2222 // second get volume update from volume controller
2223 if (ctrlIdx >= 0) {
2224 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2225 mNewLeftVolume = newLeft;
2226 mNewRightVolume = newRight;
2227 }
2228 // then indicate volume to all other effects in chain.
2229 // Pass altered volume to effects before volume controller
2230 // and requested volume to effects after controller
2231 uint32_t lVol = newLeft;
2232 uint32_t rVol = newRight;
2233
2234 for (size_t i = 0; i < size; i++) {
2235 if ((int)i == ctrlIdx) {
2236 continue;
2237 }
2238 // this also works for ctrlIdx == -1 when there is no volume controller
2239 if ((int)i > ctrlIdx) {
2240 lVol = *left;
2241 rVol = *right;
2242 }
2243 mEffects[i]->setVolume(&lVol, &rVol, false);
2244 }
2245 *left = newLeft;
2246 *right = newRight;
2247
2248 return hasControl;
2249}
2250
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002251// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002252void AudioFlinger::EffectChain::resetVolume_l()
2253{
Eric Laurente7449bf2016-08-03 18:44:07 -07002254 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2255 uint32_t left = mLeftVolume;
2256 uint32_t right = mRightVolume;
2257 (void)setVolume_l(&left, &right, true);
2258 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002259}
2260
Eric Laurent1b928682014-10-02 19:41:47 -07002261void AudioFlinger::EffectChain::syncHalEffectsState()
2262{
2263 Mutex::Autolock _l(mLock);
2264 for (size_t i = 0; i < mEffects.size(); i++) {
2265 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2266 mEffects[i]->state() == EffectModule::STOPPING) {
2267 mEffects[i]->addEffectToHal_l();
2268 }
2269 }
2270}
2271
Eric Laurentca7cc822012-11-19 14:55:58 -08002272void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2273{
2274 const size_t SIZE = 256;
2275 char buffer[SIZE];
2276 String8 result;
2277
Marco Nelissenb2208842014-02-07 14:00:50 -08002278 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002279 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002280 result.append(buffer);
2281
Marco Nelissenb2208842014-02-07 14:00:50 -08002282 if (numEffects) {
2283 bool locked = AudioFlinger::dumpTryLock(mLock);
2284 // failed to lock - AudioFlinger is probably deadlocked
2285 if (!locked) {
2286 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002287 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002288
Andy Hungbded9c82017-11-30 18:47:35 -08002289 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2290 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2291 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2292 (int)inBufferStr.size(), "In buffer ",
2293 (int)outBufferStr.size(), "Out buffer ");
2294 result.appendFormat("\t%s %s %d\n",
2295 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002296 write(fd, result.string(), result.size());
2297
2298 for (size_t i = 0; i < numEffects; ++i) {
2299 sp<EffectModule> effect = mEffects[i];
2300 if (effect != 0) {
2301 effect->dump(fd, args);
2302 }
2303 }
2304
2305 if (locked) {
2306 mLock.unlock();
2307 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002308 }
2309}
2310
2311// must be called with ThreadBase::mLock held
2312void AudioFlinger::EffectChain::setEffectSuspended_l(
2313 const effect_uuid_t *type, bool suspend)
2314{
2315 sp<SuspendedEffectDesc> desc;
2316 // use effect type UUID timelow as key as there is no real risk of identical
2317 // timeLow fields among effect type UUIDs.
2318 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2319 if (suspend) {
2320 if (index >= 0) {
2321 desc = mSuspendedEffects.valueAt(index);
2322 } else {
2323 desc = new SuspendedEffectDesc();
2324 desc->mType = *type;
2325 mSuspendedEffects.add(type->timeLow, desc);
2326 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2327 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002328
Eric Laurentca7cc822012-11-19 14:55:58 -08002329 if (desc->mRefCount++ == 0) {
2330 sp<EffectModule> effect = getEffectIfEnabled(type);
2331 if (effect != 0) {
2332 desc->mEffect = effect;
2333 effect->setSuspended(true);
2334 effect->setEnabled(false);
2335 }
2336 }
2337 } else {
2338 if (index < 0) {
2339 return;
2340 }
2341 desc = mSuspendedEffects.valueAt(index);
2342 if (desc->mRefCount <= 0) {
2343 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002344 desc->mRefCount = 0;
2345 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002346 }
2347 if (--desc->mRefCount == 0) {
2348 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2349 if (desc->mEffect != 0) {
2350 sp<EffectModule> effect = desc->mEffect.promote();
2351 if (effect != 0) {
2352 effect->setSuspended(false);
2353 effect->lock();
2354 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002355 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002356 effect->setEnabled_l(handle->enabled());
2357 }
2358 effect->unlock();
2359 }
2360 desc->mEffect.clear();
2361 }
2362 mSuspendedEffects.removeItemsAt(index);
2363 }
2364 }
2365}
2366
2367// must be called with ThreadBase::mLock held
2368void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2369{
2370 sp<SuspendedEffectDesc> desc;
2371
2372 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2373 if (suspend) {
2374 if (index >= 0) {
2375 desc = mSuspendedEffects.valueAt(index);
2376 } else {
2377 desc = new SuspendedEffectDesc();
2378 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2379 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2380 }
2381 if (desc->mRefCount++ == 0) {
2382 Vector< sp<EffectModule> > effects;
2383 getSuspendEligibleEffects(effects);
2384 for (size_t i = 0; i < effects.size(); i++) {
2385 setEffectSuspended_l(&effects[i]->desc().type, true);
2386 }
2387 }
2388 } else {
2389 if (index < 0) {
2390 return;
2391 }
2392 desc = mSuspendedEffects.valueAt(index);
2393 if (desc->mRefCount <= 0) {
2394 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2395 desc->mRefCount = 1;
2396 }
2397 if (--desc->mRefCount == 0) {
2398 Vector<const effect_uuid_t *> types;
2399 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2400 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2401 continue;
2402 }
2403 types.add(&mSuspendedEffects.valueAt(i)->mType);
2404 }
2405 for (size_t i = 0; i < types.size(); i++) {
2406 setEffectSuspended_l(types[i], false);
2407 }
2408 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2409 mSuspendedEffects.keyAt(index));
2410 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2411 }
2412 }
2413}
2414
2415
2416// The volume effect is used for automated tests only
2417#ifndef OPENSL_ES_H_
2418static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2419 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2420const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2421#endif //OPENSL_ES_H_
2422
Eric Laurentd8365c52017-07-16 15:27:05 -07002423/* static */
2424bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2425{
2426 // Only NS and AEC are suspended when BtNRec is off
2427 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2428 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2429 return true;
2430 }
2431 return false;
2432}
2433
Eric Laurentca7cc822012-11-19 14:55:58 -08002434bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2435{
2436 // auxiliary effects and visualizer are never suspended on output mix
2437 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2438 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2439 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2440 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2441 return false;
2442 }
2443 return true;
2444}
2445
2446void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2447 Vector< sp<AudioFlinger::EffectModule> > &effects)
2448{
2449 effects.clear();
2450 for (size_t i = 0; i < mEffects.size(); i++) {
2451 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2452 effects.add(mEffects[i]);
2453 }
2454 }
2455}
2456
2457sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2458 const effect_uuid_t *type)
2459{
2460 sp<EffectModule> effect = getEffectFromType_l(type);
2461 return effect != 0 && effect->isEnabled() ? effect : 0;
2462}
2463
2464void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2465 bool enabled)
2466{
2467 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2468 if (enabled) {
2469 if (index < 0) {
2470 // if the effect is not suspend check if all effects are suspended
2471 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2472 if (index < 0) {
2473 return;
2474 }
2475 if (!isEffectEligibleForSuspend(effect->desc())) {
2476 return;
2477 }
2478 setEffectSuspended_l(&effect->desc().type, enabled);
2479 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2480 if (index < 0) {
2481 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2482 return;
2483 }
2484 }
2485 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2486 effect->desc().type.timeLow);
2487 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002488 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002489 if (desc->mEffect == 0) {
2490 desc->mEffect = effect;
2491 effect->setEnabled(false);
2492 effect->setSuspended(true);
2493 }
2494 } else {
2495 if (index < 0) {
2496 return;
2497 }
2498 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2499 effect->desc().type.timeLow);
2500 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2501 desc->mEffect.clear();
2502 effect->setSuspended(false);
2503 }
2504}
2505
Eric Laurent5baf2af2013-09-12 17:37:00 -07002506bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002507{
2508 Mutex::Autolock _l(mLock);
2509 size_t size = mEffects.size();
2510 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002511 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002512 return true;
2513 }
2514 }
2515 return false;
2516}
2517
Eric Laurentaaa44472014-09-12 17:41:50 -07002518void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2519{
2520 Mutex::Autolock _l(mLock);
2521 mThread = thread;
2522 for (size_t i = 0; i < mEffects.size(); i++) {
2523 mEffects[i]->setThread(thread);
2524 }
2525}
2526
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002527void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2528{
2529 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2530 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2531 }
2532 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2533 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2534 }
2535}
2536
2537void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2538{
2539 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2540 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2541 }
2542 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2543 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2544 }
2545}
2546
2547bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002548{
2549 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002550 for (const auto &effect : mEffects) {
2551 if (effect->isProcessImplemented()) {
2552 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002553 }
2554 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002555 // Allow effects without processing.
2556 return true;
2557}
2558
2559bool AudioFlinger::EffectChain::isFastCompatible() const
2560{
2561 Mutex::Autolock _l(mLock);
2562 for (const auto &effect : mEffects) {
2563 if (effect->isProcessImplemented()
2564 && effect->isImplementationSoftware()) {
2565 return false;
2566 }
2567 }
2568 // Allow effects without processing or hw accelerated effects.
2569 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002570}
2571
2572// isCompatibleWithThread_l() must be called with thread->mLock held
2573bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2574{
2575 Mutex::Autolock _l(mLock);
2576 for (size_t i = 0; i < mEffects.size(); i++) {
2577 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2578 return false;
2579 }
2580 }
2581 return true;
2582}
2583
Glenn Kasten63238ef2015-03-02 15:50:29 -08002584} // namespace android