blob: b13e551bf3a793266231e3fa2354b98ff5385a05 [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);
1030 (void)EffectBufferHalInterface::allocate(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001031 }
Andy Hungbded9c82017-11-30 18:47:35 -08001032 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001033 mInConversionBuffer->setFrameCount(inFrameCount);
1034 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001035 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001036 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001037 }
1038 }
1039#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001040}
1041
1042void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001043 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001044
1045 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001046 if (buffer != 0) {
1047 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1048 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1049 } else {
1050 mConfig.outputCfg.buffer.raw = NULL;
1051 }
1052 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001053 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001054
1055#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001056 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001057 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001058 const uint32_t outChannelCount =
1059 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1060 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1061 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001062 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001063 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1064 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1065 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001066
1067 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1068 __func__, outChannels, outFrameCount, size);
1069
Andy Hungbded9c82017-11-30 18:47:35 -08001070 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1071 || size > mOutConversionBuffer->getSize())) {
1072 mOutConversionBuffer.clear();
1073 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
1074 (void)EffectBufferHalInterface::allocate(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001075 }
Andy Hungbded9c82017-11-30 18:47:35 -08001076 if (mOutConversionBuffer.get() != nullptr) {
1077 mOutConversionBuffer->setFrameCount(outFrameCount);
1078 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001079 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001080 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001081 }
1082 }
1083#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001084}
1085
Eric Laurentca7cc822012-11-19 14:55:58 -08001086status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1087{
1088 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001089 if (mStatus != NO_ERROR) {
1090 return mStatus;
1091 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001092 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1094 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1095 if (isProcessEnabled() &&
1096 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
1097 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001098 uint32_t volume[2];
1099 uint32_t *pVolume = NULL;
1100 uint32_t size = sizeof(volume);
1101 volume[0] = *left;
1102 volume[1] = *right;
1103 if (controller) {
1104 pVolume = volume;
1105 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001106 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1107 size,
1108 volume,
1109 &size,
1110 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001111 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1112 *left = volume[0];
1113 *right = volume[1];
1114 }
1115 }
1116 return status;
1117}
1118
1119status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1120{
1121 if (device == AUDIO_DEVICE_NONE) {
1122 return NO_ERROR;
1123 }
1124
1125 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001126 if (mStatus != NO_ERROR) {
1127 return mStatus;
1128 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001129 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001130 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001131 status_t cmdStatus;
1132 uint32_t size = sizeof(status_t);
1133 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1134 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001135 status = mEffectInterface->command(cmd,
1136 sizeof(uint32_t),
1137 &device,
1138 &size,
1139 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 }
1141 return status;
1142}
1143
1144status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1145{
1146 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001147 if (mStatus != NO_ERROR) {
1148 return mStatus;
1149 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001150 status_t status = NO_ERROR;
1151 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1152 status_t cmdStatus;
1153 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001154 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1155 sizeof(audio_mode_t),
1156 &mode,
1157 &size,
1158 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001159 if (status == NO_ERROR) {
1160 status = cmdStatus;
1161 }
1162 }
1163 return status;
1164}
1165
1166status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1167{
1168 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001169 if (mStatus != NO_ERROR) {
1170 return mStatus;
1171 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001172 status_t status = NO_ERROR;
1173 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1174 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001175 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1176 sizeof(audio_source_t),
1177 &source,
1178 &size,
1179 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001180 }
1181 return status;
1182}
1183
1184void AudioFlinger::EffectModule::setSuspended(bool suspended)
1185{
1186 Mutex::Autolock _l(mLock);
1187 mSuspended = suspended;
1188}
1189
1190bool AudioFlinger::EffectModule::suspended() const
1191{
1192 Mutex::Autolock _l(mLock);
1193 return mSuspended;
1194}
1195
1196bool AudioFlinger::EffectModule::purgeHandles()
1197{
1198 bool enabled = false;
1199 Mutex::Autolock _l(mLock);
1200 for (size_t i = 0; i < mHandles.size(); i++) {
1201 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001202 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001203 if (handle->hasControl()) {
1204 enabled = handle->enabled();
1205 }
1206 }
1207 }
1208 return enabled;
1209}
1210
Eric Laurent5baf2af2013-09-12 17:37:00 -07001211status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1212{
1213 Mutex::Autolock _l(mLock);
1214 if (mStatus != NO_ERROR) {
1215 return mStatus;
1216 }
1217 status_t status = NO_ERROR;
1218 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1219 status_t cmdStatus;
1220 uint32_t size = sizeof(status_t);
1221 effect_offload_param_t cmd;
1222
1223 cmd.isOffload = offloaded;
1224 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001225 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1226 sizeof(effect_offload_param_t),
1227 &cmd,
1228 &size,
1229 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001230 if (status == NO_ERROR) {
1231 status = cmdStatus;
1232 }
1233 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1234 } else {
1235 if (offloaded) {
1236 status = INVALID_OPERATION;
1237 }
1238 mOffloaded = false;
1239 }
1240 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1241 return status;
1242}
1243
1244bool AudioFlinger::EffectModule::isOffloaded() const
1245{
1246 Mutex::Autolock _l(mLock);
1247 return mOffloaded;
1248}
1249
Marco Nelissenb2208842014-02-07 14:00:50 -08001250String8 effectFlagsToString(uint32_t flags) {
1251 String8 s;
1252
1253 s.append("conn. mode: ");
1254 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1255 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1256 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1257 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1258 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1259 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1260 default: s.append("unknown/reserved"); break;
1261 }
1262 s.append(", ");
1263
1264 s.append("insert pref: ");
1265 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1266 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1267 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1268 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1269 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1270 default: s.append("unknown/reserved"); break;
1271 }
1272 s.append(", ");
1273
1274 s.append("volume mgmt: ");
1275 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1276 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1277 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1278 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1279 default: s.append("unknown/reserved"); break;
1280 }
1281 s.append(", ");
1282
1283 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1284 if (devind) {
1285 s.append("device indication: ");
1286 switch (devind) {
1287 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1288 default: s.append("unknown/reserved"); break;
1289 }
1290 s.append(", ");
1291 }
1292
1293 s.append("input mode: ");
1294 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1295 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1296 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1297 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1298 default: s.append("not set"); break;
1299 }
1300 s.append(", ");
1301
1302 s.append("output mode: ");
1303 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1304 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1305 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1306 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1307 default: s.append("not set"); break;
1308 }
1309 s.append(", ");
1310
1311 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1312 if (accel) {
1313 s.append("hardware acceleration: ");
1314 switch (accel) {
1315 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1316 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1317 default: s.append("unknown/reserved"); break;
1318 }
1319 s.append(", ");
1320 }
1321
1322 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1323 if (modeind) {
1324 s.append("mode indication: ");
1325 switch (modeind) {
1326 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1327 default: s.append("unknown/reserved"); break;
1328 }
1329 s.append(", ");
1330 }
1331
1332 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1333 if (srcind) {
1334 s.append("source indication: ");
1335 switch (srcind) {
1336 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1337 default: s.append("unknown/reserved"); break;
1338 }
1339 s.append(", ");
1340 }
1341
1342 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1343 s.append("offloadable, ");
1344 }
1345
1346 int len = s.length();
1347 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001348 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001349 s.unlockBuffer(len - 2);
1350 }
1351 return s;
1352}
1353
Andy Hungbded9c82017-11-30 18:47:35 -08001354static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1355 std::stringstream ss;
1356
1357 if (buffer.get() == nullptr) {
1358 return "nullptr"; // make different than below
1359 } else if (buffer->externalData() != nullptr) {
1360 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1361 << " -> "
1362 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1363 } else {
1364 ss << buffer->audioBuffer()->raw;
1365 }
1366 return ss.str();
1367}
Marco Nelissenb2208842014-02-07 14:00:50 -08001368
Glenn Kasten0f11b512014-01-31 16:18:54 -08001369void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001370{
Eric Laurentca7cc822012-11-19 14:55:58 -08001371 String8 result;
1372
Andy Hung9718d662017-12-22 17:57:39 -08001373 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001374
1375 bool locked = AudioFlinger::dumpTryLock(mLock);
1376 // failed to lock - AudioFlinger is probably deadlocked
1377 if (!locked) {
1378 result.append("\t\tCould not lock Fx mutex:\n");
1379 }
1380
1381 result.append("\t\tSession Status State Engine:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001382 result.appendFormat("\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001383 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001384
1385 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001386 char uuidStr[64];
1387 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001388 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001389 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001390 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1391 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001392 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001393 mDescriptor.flags,
1394 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001395 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001396 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001397
1398 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001399 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001400
1401 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001402
1403 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001404 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1405 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1406 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001407 mConfig.inputCfg.buffer.frameCount,
1408 mConfig.inputCfg.samplingRate,
1409 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001410 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001411 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001412
1413 result.append("\t\t- Output configuration:\n");
1414 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001415 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001416 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 mConfig.outputCfg.buffer.frameCount,
1418 mConfig.outputCfg.samplingRate,
1419 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001420 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001421 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001422
rago94a1ee82017-07-21 15:11:02 -07001423#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001424
Andy Hungbded9c82017-11-30 18:47:35 -08001425 result.appendFormat("\t\t- HAL buffers:\n"
1426 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1427 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1428 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1429 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1430 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001431#endif
1432
Andy Hung9718d662017-12-22 17:57:39 -08001433 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001434 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001435 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001436 for (size_t i = 0; i < mHandles.size(); ++i) {
1437 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001438 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001439 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001440 result.append(buffer);
1441 }
1442 }
1443
Eric Laurentca7cc822012-11-19 14:55:58 -08001444 write(fd, result.string(), result.length());
1445
1446 if (locked) {
1447 mLock.unlock();
1448 }
1449}
1450
1451// ----------------------------------------------------------------------------
1452// EffectHandle implementation
1453// ----------------------------------------------------------------------------
1454
1455#undef LOG_TAG
1456#define LOG_TAG "AudioFlinger::EffectHandle"
1457
1458AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1459 const sp<AudioFlinger::Client>& client,
1460 const sp<IEffectClient>& effectClient,
1461 int32_t priority)
1462 : BnEffect(),
1463 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001464 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001465{
1466 ALOGV("constructor %p", this);
1467
1468 if (client == 0) {
1469 return;
1470 }
1471 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1472 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001473 if (mCblkMemory == 0 ||
1474 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001475 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001476 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001477 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001478 return;
1479 }
Glenn Kastene75da402013-11-20 13:54:52 -08001480 new(mCblk) effect_param_cblk_t();
1481 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001482}
1483
1484AudioFlinger::EffectHandle::~EffectHandle()
1485{
1486 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001487 disconnect(false);
1488}
1489
Glenn Kastene75da402013-11-20 13:54:52 -08001490status_t AudioFlinger::EffectHandle::initCheck()
1491{
1492 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1493}
1494
Eric Laurentca7cc822012-11-19 14:55:58 -08001495status_t AudioFlinger::EffectHandle::enable()
1496{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001497 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001498 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001499 sp<EffectModule> effect = mEffect.promote();
1500 if (effect == 0 || mDisconnected) {
1501 return DEAD_OBJECT;
1502 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001503 if (!mHasControl) {
1504 return INVALID_OPERATION;
1505 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001506
1507 if (mEnabled) {
1508 return NO_ERROR;
1509 }
1510
1511 mEnabled = true;
1512
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001513 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001514 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001515 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001516 }
1517
1518 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001519 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001520 return NO_ERROR;
1521 }
1522
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001523 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001524 if (status != NO_ERROR) {
1525 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001526 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001527 }
1528 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001529 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001530 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001531 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1532 Mutex::Autolock _l(thread->mLock);
1533 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001534 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001535 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001536 if (thread->type() == ThreadBase::OFFLOAD) {
1537 PlaybackThread *t = (PlaybackThread *)thread.get();
1538 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1539 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001540 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001541 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1542 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001543 }
1544 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001545 }
1546 return status;
1547}
1548
1549status_t AudioFlinger::EffectHandle::disable()
1550{
1551 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001552 AutoMutex _l(mLock);
1553 sp<EffectModule> effect = mEffect.promote();
1554 if (effect == 0 || mDisconnected) {
1555 return DEAD_OBJECT;
1556 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001557 if (!mHasControl) {
1558 return INVALID_OPERATION;
1559 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001560
1561 if (!mEnabled) {
1562 return NO_ERROR;
1563 }
1564 mEnabled = false;
1565
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001566 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001567 return NO_ERROR;
1568 }
1569
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001570 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001571
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001572 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001573 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001574 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001575 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1576 Mutex::Autolock _l(thread->mLock);
1577 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001578 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001579 }
1580
1581 return status;
1582}
1583
1584void AudioFlinger::EffectHandle::disconnect()
1585{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001586 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001587 disconnect(true);
1588}
1589
1590void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1591{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001592 AutoMutex _l(mLock);
1593 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1594 if (mDisconnected) {
1595 if (unpinIfLast) {
1596 android_errorWriteLog(0x534e4554, "32707507");
1597 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001598 return;
1599 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001600 mDisconnected = true;
1601 sp<ThreadBase> thread;
1602 {
1603 sp<EffectModule> effect = mEffect.promote();
1604 if (effect != 0) {
1605 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001606 }
1607 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001608 if (thread != 0) {
1609 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001610 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001611 // try to cleanup as much as we can
1612 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001613 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1614 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001615 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001616 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001617
Eric Laurentca7cc822012-11-19 14:55:58 -08001618 if (mClient != 0) {
1619 if (mCblk != NULL) {
1620 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1621 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1622 }
1623 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001624 // Client destructor must run with AudioFlinger client mutex locked
1625 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001626 mClient.clear();
1627 }
1628}
1629
1630status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1631 uint32_t cmdSize,
1632 void *pCmdData,
1633 uint32_t *replySize,
1634 void *pReplyData)
1635{
1636 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001637 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001638
Eric Laurentc7ab3092017-06-15 18:43:46 -07001639 // reject commands reserved for internal use by audio framework if coming from outside
1640 // of audioserver
1641 switch(cmdCode) {
1642 case EFFECT_CMD_ENABLE:
1643 case EFFECT_CMD_DISABLE:
1644 case EFFECT_CMD_SET_PARAM:
1645 case EFFECT_CMD_SET_PARAM_DEFERRED:
1646 case EFFECT_CMD_SET_PARAM_COMMIT:
1647 case EFFECT_CMD_GET_PARAM:
1648 break;
1649 default:
1650 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1651 break;
1652 }
1653 android_errorWriteLog(0x534e4554, "62019992");
1654 return BAD_VALUE;
1655 }
1656
Eric Laurent1ffc5852016-12-15 14:46:09 -08001657 if (cmdCode == EFFECT_CMD_ENABLE) {
1658 if (*replySize < sizeof(int)) {
1659 android_errorWriteLog(0x534e4554, "32095713");
1660 return BAD_VALUE;
1661 }
1662 *(int *)pReplyData = NO_ERROR;
1663 *replySize = sizeof(int);
1664 return enable();
1665 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1666 if (*replySize < sizeof(int)) {
1667 android_errorWriteLog(0x534e4554, "32095713");
1668 return BAD_VALUE;
1669 }
1670 *(int *)pReplyData = NO_ERROR;
1671 *replySize = sizeof(int);
1672 return disable();
1673 }
1674
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001675 AutoMutex _l(mLock);
1676 sp<EffectModule> effect = mEffect.promote();
1677 if (effect == 0 || mDisconnected) {
1678 return DEAD_OBJECT;
1679 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001680 // only get parameter command is permitted for applications not controlling the effect
1681 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1682 return INVALID_OPERATION;
1683 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001684 if (mClient == 0) {
1685 return INVALID_OPERATION;
1686 }
1687
1688 // handle commands that are not forwarded transparently to effect engine
1689 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001690 if (*replySize < sizeof(int)) {
1691 android_errorWriteLog(0x534e4554, "32095713");
1692 return BAD_VALUE;
1693 }
1694 *(int *)pReplyData = NO_ERROR;
1695 *replySize = sizeof(int);
1696
Eric Laurentca7cc822012-11-19 14:55:58 -08001697 // No need to trylock() here as this function is executed in the binder thread serving a
1698 // particular client process: no risk to block the whole media server process or mixer
1699 // threads if we are stuck here
1700 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001701 // keep local copy of index in case of client corruption b/32220769
1702 const uint32_t clientIndex = mCblk->clientIndex;
1703 const uint32_t serverIndex = mCblk->serverIndex;
1704 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1705 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 mCblk->serverIndex = 0;
1707 mCblk->clientIndex = 0;
1708 return BAD_VALUE;
1709 }
1710 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001711 effect_param_t *param = NULL;
1712 for (uint32_t index = serverIndex; index < clientIndex;) {
1713 int *p = (int *)(mBuffer + index);
1714 const int size = *p++;
1715 if (size < 0
1716 || size > EFFECT_PARAM_BUFFER_SIZE
1717 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001718 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001719 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001720 break;
1721 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001722
1723 // copy to local memory in case of client corruption b/32220769
1724 param = (effect_param_t *)realloc(param, size);
1725 if (param == NULL) {
1726 ALOGW("command(): out of memory");
1727 status = NO_MEMORY;
1728 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001729 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001730 memcpy(param, p, size);
1731
1732 int reply = 0;
1733 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001734 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001735 size,
1736 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 &rsize,
1738 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001739
1740 // verify shared memory: server index shouldn't change; client index can't go back.
1741 if (serverIndex != mCblk->serverIndex
1742 || clientIndex > mCblk->clientIndex) {
1743 android_errorWriteLog(0x534e4554, "32220769");
1744 status = BAD_VALUE;
1745 break;
1746 }
1747
Eric Laurentca7cc822012-11-19 14:55:58 -08001748 // stop at first error encountered
1749 if (ret != NO_ERROR) {
1750 status = ret;
1751 *(int *)pReplyData = reply;
1752 break;
1753 } else if (reply != NO_ERROR) {
1754 *(int *)pReplyData = reply;
1755 break;
1756 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001757 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001758 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001759 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001760 mCblk->serverIndex = 0;
1761 mCblk->clientIndex = 0;
1762 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001763 }
1764
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001765 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001766}
1767
1768void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1769{
1770 ALOGV("setControl %p control %d", this, hasControl);
1771
1772 mHasControl = hasControl;
1773 mEnabled = enabled;
1774
1775 if (signal && mEffectClient != 0) {
1776 mEffectClient->controlStatusChanged(hasControl);
1777 }
1778}
1779
1780void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1781 uint32_t cmdSize,
1782 void *pCmdData,
1783 uint32_t replySize,
1784 void *pReplyData)
1785{
1786 if (mEffectClient != 0) {
1787 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1788 }
1789}
1790
1791
1792
1793void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1794{
1795 if (mEffectClient != 0) {
1796 mEffectClient->enableStatusChanged(enabled);
1797 }
1798}
1799
1800status_t AudioFlinger::EffectHandle::onTransact(
1801 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1802{
1803 return BnEffect::onTransact(code, data, reply, flags);
1804}
1805
1806
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001807void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001808{
1809 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1810
Marco Nelissenb2208842014-02-07 14:00:50 -08001811 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001812 (mClient == 0) ? getpid_cached : mClient->pid(),
1813 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001814 mHasControl ? "yes" : "no",
1815 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 mCblk ? mCblk->clientIndex : 0,
1817 mCblk ? mCblk->serverIndex : 0
1818 );
1819
1820 if (locked) {
1821 mCblk->lock.unlock();
1822 }
1823}
1824
1825#undef LOG_TAG
1826#define LOG_TAG "AudioFlinger::EffectChain"
1827
1828AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001829 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001830 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001831 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001832 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001833{
1834 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1835 if (thread == NULL) {
1836 return;
1837 }
1838 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1839 thread->frameCount();
1840}
1841
1842AudioFlinger::EffectChain::~EffectChain()
1843{
Eric Laurentca7cc822012-11-19 14:55:58 -08001844}
1845
1846// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1847sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1848 effect_descriptor_t *descriptor)
1849{
1850 size_t size = mEffects.size();
1851
1852 for (size_t i = 0; i < size; i++) {
1853 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1854 return mEffects[i];
1855 }
1856 }
1857 return 0;
1858}
1859
1860// getEffectFromId_l() must be called with ThreadBase::mLock held
1861sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1862{
1863 size_t size = mEffects.size();
1864
1865 for (size_t i = 0; i < size; i++) {
1866 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1867 if (id == 0 || mEffects[i]->id() == id) {
1868 return mEffects[i];
1869 }
1870 }
1871 return 0;
1872}
1873
1874// getEffectFromType_l() must be called with ThreadBase::mLock held
1875sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1876 const effect_uuid_t *type)
1877{
1878 size_t size = mEffects.size();
1879
1880 for (size_t i = 0; i < size; i++) {
1881 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1882 return mEffects[i];
1883 }
1884 }
1885 return 0;
1886}
1887
1888void AudioFlinger::EffectChain::clearInputBuffer()
1889{
1890 Mutex::Autolock _l(mLock);
1891 sp<ThreadBase> thread = mThread.promote();
1892 if (thread == 0) {
1893 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1894 return;
1895 }
1896 clearInputBuffer_l(thread);
1897}
1898
1899// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001900void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001901{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001902 if (mInBuffer == NULL) {
1903 return;
1904 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001905 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08001906 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07001907
Mikhail Naganov022b9952017-01-04 16:36:51 -08001908 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1909 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001910}
1911
1912// Must be called with EffectChain::mLock locked
1913void AudioFlinger::EffectChain::process_l()
1914{
1915 sp<ThreadBase> thread = mThread.promote();
1916 if (thread == 0) {
1917 ALOGW("process_l(): cannot promote mixer thread");
1918 return;
1919 }
1920 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1921 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001922 // never process effects when:
1923 // - on an OFFLOAD thread
1924 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001925 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1926 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 if (!isGlobalSession) {
1928 bool tracksOnSession = (trackCnt() != 0);
1929
1930 if (!tracksOnSession && mTailBufferCount == 0) {
1931 doProcess = false;
1932 }
1933
1934 if (activeTrackCnt() == 0) {
1935 // if no track is active and the effect tail has not been rendered,
1936 // the input buffer must be cleared here as the mixer process will not do it
1937 if (tracksOnSession || mTailBufferCount > 0) {
1938 clearInputBuffer_l(thread);
1939 if (mTailBufferCount > 0) {
1940 mTailBufferCount--;
1941 }
1942 }
1943 }
1944 }
1945
1946 size_t size = mEffects.size();
1947 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001948 // Only the input and output buffers of the chain can be external,
1949 // and 'update' / 'commit' do nothing for allocated buffers, thus
1950 // it's not needed to consider any other buffers here.
1951 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001952 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1953 mOutBuffer->update();
1954 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001955 for (size_t i = 0; i < size; i++) {
1956 mEffects[i]->process();
1957 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001958 mInBuffer->commit();
1959 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1960 mOutBuffer->commit();
1961 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001962 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001963 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001964 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001965 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1966 }
1967 if (doResetVolume) {
1968 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001969 }
1970}
1971
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001972// createEffect_l() must be called with ThreadBase::mLock held
1973status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1974 ThreadBase *thread,
1975 effect_descriptor_t *desc,
1976 int id,
1977 audio_session_t sessionId,
1978 bool pinned)
1979{
1980 Mutex::Autolock _l(mLock);
1981 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1982 status_t lStatus = effect->status();
1983 if (lStatus == NO_ERROR) {
1984 lStatus = addEffect_ll(effect);
1985 }
1986 if (lStatus != NO_ERROR) {
1987 effect.clear();
1988 }
1989 return lStatus;
1990}
1991
1992// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001993status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1994{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001995 Mutex::Autolock _l(mLock);
1996 return addEffect_ll(effect);
1997}
1998// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1999status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2000{
Eric Laurentca7cc822012-11-19 14:55:58 -08002001 effect_descriptor_t desc = effect->desc();
2002 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2003
Eric Laurentca7cc822012-11-19 14:55:58 -08002004 effect->setChain(this);
2005 sp<ThreadBase> thread = mThread.promote();
2006 if (thread == 0) {
2007 return NO_INIT;
2008 }
2009 effect->setThread(thread);
2010
2011 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2012 // Auxiliary effects are inserted at the beginning of mEffects vector as
2013 // they are processed first and accumulated in chain input buffer
2014 mEffects.insertAt(effect, 0);
2015
2016 // the input buffer for auxiliary effect contains mono samples in
2017 // 32 bit format. This is to avoid saturation in AudoMixer
2018 // accumulation stage. Saturation is done in EffectModule::process() before
2019 // calling the process in effect engine
2020 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002021 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002022#ifdef FLOAT_EFFECT_CHAIN
2023 status_t result = EffectBufferHalInterface::allocate(
2024 numSamples * sizeof(float), &halBuffer);
2025#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08002026 status_t result = EffectBufferHalInterface::allocate(
2027 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002028#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002029 if (result != OK) return result;
2030 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002031 // auxiliary effects output samples to chain input buffer for further processing
2032 // by insert effects
2033 effect->setOutBuffer(mInBuffer);
2034 } else {
2035 // Insert effects are inserted at the end of mEffects vector as they are processed
2036 // after track and auxiliary effects.
2037 // Insert effect order as a function of indicated preference:
2038 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2039 // another effect is present
2040 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2041 // last effect claiming first position
2042 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2043 // first effect claiming last position
2044 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2045 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2046 // already present
2047
2048 size_t size = mEffects.size();
2049 size_t idx_insert = size;
2050 ssize_t idx_insert_first = -1;
2051 ssize_t idx_insert_last = -1;
2052
2053 for (size_t i = 0; i < size; i++) {
2054 effect_descriptor_t d = mEffects[i]->desc();
2055 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2056 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2057 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2058 // check invalid effect chaining combinations
2059 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2060 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2061 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2062 desc.name, d.name);
2063 return INVALID_OPERATION;
2064 }
2065 // remember position of first insert effect and by default
2066 // select this as insert position for new effect
2067 if (idx_insert == size) {
2068 idx_insert = i;
2069 }
2070 // remember position of last insert effect claiming
2071 // first position
2072 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2073 idx_insert_first = i;
2074 }
2075 // remember position of first insert effect claiming
2076 // last position
2077 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2078 idx_insert_last == -1) {
2079 idx_insert_last = i;
2080 }
2081 }
2082 }
2083
2084 // modify idx_insert from first position if needed
2085 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2086 if (idx_insert_last != -1) {
2087 idx_insert = idx_insert_last;
2088 } else {
2089 idx_insert = size;
2090 }
2091 } else {
2092 if (idx_insert_first != -1) {
2093 idx_insert = idx_insert_first + 1;
2094 }
2095 }
2096
2097 // always read samples from chain input buffer
2098 effect->setInBuffer(mInBuffer);
2099
2100 // if last effect in the chain, output samples to chain
2101 // output buffer, otherwise to chain input buffer
2102 if (idx_insert == size) {
2103 if (idx_insert != 0) {
2104 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2105 mEffects[idx_insert-1]->configure();
2106 }
2107 effect->setOutBuffer(mOutBuffer);
2108 } else {
2109 effect->setOutBuffer(mInBuffer);
2110 }
2111 mEffects.insertAt(effect, idx_insert);
2112
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002113 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002114 idx_insert);
2115 }
2116 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002117
Eric Laurentca7cc822012-11-19 14:55:58 -08002118 return NO_ERROR;
2119}
2120
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002121// removeEffect_l() must be called with ThreadBase::mLock held
2122size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2123 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002124{
2125 Mutex::Autolock _l(mLock);
2126 size_t size = mEffects.size();
2127 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2128
2129 for (size_t i = 0; i < size; i++) {
2130 if (effect == mEffects[i]) {
2131 // calling stop here will remove pre-processing effect from the audio HAL.
2132 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2133 // the middle of a read from audio HAL
2134 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2135 mEffects[i]->state() == EffectModule::STOPPING) {
2136 mEffects[i]->stop();
2137 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002138 if (release) {
2139 mEffects[i]->release_l();
2140 }
2141
Mikhail Naganov022b9952017-01-04 16:36:51 -08002142 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002143 if (i == size - 1 && i != 0) {
2144 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2145 mEffects[i - 1]->configure();
2146 }
2147 }
2148 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002149 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002150 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002151
Eric Laurentca7cc822012-11-19 14:55:58 -08002152 break;
2153 }
2154 }
2155
2156 return mEffects.size();
2157}
2158
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002159// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002160void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2161{
2162 size_t size = mEffects.size();
2163 for (size_t i = 0; i < size; i++) {
2164 mEffects[i]->setDevice(device);
2165 }
2166}
2167
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002168// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002169void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2170{
2171 size_t size = mEffects.size();
2172 for (size_t i = 0; i < size; i++) {
2173 mEffects[i]->setMode(mode);
2174 }
2175}
2176
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002177// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002178void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2179{
2180 size_t size = mEffects.size();
2181 for (size_t i = 0; i < size; i++) {
2182 mEffects[i]->setAudioSource(source);
2183 }
2184}
2185
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002186// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002187bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002188{
2189 uint32_t newLeft = *left;
2190 uint32_t newRight = *right;
2191 bool hasControl = false;
2192 int ctrlIdx = -1;
2193 size_t size = mEffects.size();
2194
2195 // first update volume controller
2196 for (size_t i = size; i > 0; i--) {
2197 if (mEffects[i - 1]->isProcessEnabled() &&
2198 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2199 ctrlIdx = i - 1;
2200 hasControl = true;
2201 break;
2202 }
2203 }
2204
Eric Laurentfa1e1232016-08-02 19:01:49 -07002205 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002206 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002207 if (hasControl) {
2208 *left = mNewLeftVolume;
2209 *right = mNewRightVolume;
2210 }
2211 return hasControl;
2212 }
2213
2214 mVolumeCtrlIdx = ctrlIdx;
2215 mLeftVolume = newLeft;
2216 mRightVolume = newRight;
2217
2218 // second get volume update from volume controller
2219 if (ctrlIdx >= 0) {
2220 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2221 mNewLeftVolume = newLeft;
2222 mNewRightVolume = newRight;
2223 }
2224 // then indicate volume to all other effects in chain.
2225 // Pass altered volume to effects before volume controller
2226 // and requested volume to effects after controller
2227 uint32_t lVol = newLeft;
2228 uint32_t rVol = newRight;
2229
2230 for (size_t i = 0; i < size; i++) {
2231 if ((int)i == ctrlIdx) {
2232 continue;
2233 }
2234 // this also works for ctrlIdx == -1 when there is no volume controller
2235 if ((int)i > ctrlIdx) {
2236 lVol = *left;
2237 rVol = *right;
2238 }
2239 mEffects[i]->setVolume(&lVol, &rVol, false);
2240 }
2241 *left = newLeft;
2242 *right = newRight;
2243
2244 return hasControl;
2245}
2246
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002247// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002248void AudioFlinger::EffectChain::resetVolume_l()
2249{
Eric Laurente7449bf2016-08-03 18:44:07 -07002250 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2251 uint32_t left = mLeftVolume;
2252 uint32_t right = mRightVolume;
2253 (void)setVolume_l(&left, &right, true);
2254 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002255}
2256
Eric Laurent1b928682014-10-02 19:41:47 -07002257void AudioFlinger::EffectChain::syncHalEffectsState()
2258{
2259 Mutex::Autolock _l(mLock);
2260 for (size_t i = 0; i < mEffects.size(); i++) {
2261 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2262 mEffects[i]->state() == EffectModule::STOPPING) {
2263 mEffects[i]->addEffectToHal_l();
2264 }
2265 }
2266}
2267
Eric Laurentca7cc822012-11-19 14:55:58 -08002268void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2269{
2270 const size_t SIZE = 256;
2271 char buffer[SIZE];
2272 String8 result;
2273
Marco Nelissenb2208842014-02-07 14:00:50 -08002274 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002275 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002276 result.append(buffer);
2277
Marco Nelissenb2208842014-02-07 14:00:50 -08002278 if (numEffects) {
2279 bool locked = AudioFlinger::dumpTryLock(mLock);
2280 // failed to lock - AudioFlinger is probably deadlocked
2281 if (!locked) {
2282 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002283 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002284
Andy Hungbded9c82017-11-30 18:47:35 -08002285 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2286 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2287 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2288 (int)inBufferStr.size(), "In buffer ",
2289 (int)outBufferStr.size(), "Out buffer ");
2290 result.appendFormat("\t%s %s %d\n",
2291 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002292 write(fd, result.string(), result.size());
2293
2294 for (size_t i = 0; i < numEffects; ++i) {
2295 sp<EffectModule> effect = mEffects[i];
2296 if (effect != 0) {
2297 effect->dump(fd, args);
2298 }
2299 }
2300
2301 if (locked) {
2302 mLock.unlock();
2303 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002304 }
2305}
2306
2307// must be called with ThreadBase::mLock held
2308void AudioFlinger::EffectChain::setEffectSuspended_l(
2309 const effect_uuid_t *type, bool suspend)
2310{
2311 sp<SuspendedEffectDesc> desc;
2312 // use effect type UUID timelow as key as there is no real risk of identical
2313 // timeLow fields among effect type UUIDs.
2314 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2315 if (suspend) {
2316 if (index >= 0) {
2317 desc = mSuspendedEffects.valueAt(index);
2318 } else {
2319 desc = new SuspendedEffectDesc();
2320 desc->mType = *type;
2321 mSuspendedEffects.add(type->timeLow, desc);
2322 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2323 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002324
Eric Laurentca7cc822012-11-19 14:55:58 -08002325 if (desc->mRefCount++ == 0) {
2326 sp<EffectModule> effect = getEffectIfEnabled(type);
2327 if (effect != 0) {
2328 desc->mEffect = effect;
2329 effect->setSuspended(true);
2330 effect->setEnabled(false);
2331 }
2332 }
2333 } else {
2334 if (index < 0) {
2335 return;
2336 }
2337 desc = mSuspendedEffects.valueAt(index);
2338 if (desc->mRefCount <= 0) {
2339 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002340 desc->mRefCount = 0;
2341 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002342 }
2343 if (--desc->mRefCount == 0) {
2344 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2345 if (desc->mEffect != 0) {
2346 sp<EffectModule> effect = desc->mEffect.promote();
2347 if (effect != 0) {
2348 effect->setSuspended(false);
2349 effect->lock();
2350 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002351 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002352 effect->setEnabled_l(handle->enabled());
2353 }
2354 effect->unlock();
2355 }
2356 desc->mEffect.clear();
2357 }
2358 mSuspendedEffects.removeItemsAt(index);
2359 }
2360 }
2361}
2362
2363// must be called with ThreadBase::mLock held
2364void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2365{
2366 sp<SuspendedEffectDesc> desc;
2367
2368 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2369 if (suspend) {
2370 if (index >= 0) {
2371 desc = mSuspendedEffects.valueAt(index);
2372 } else {
2373 desc = new SuspendedEffectDesc();
2374 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2375 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2376 }
2377 if (desc->mRefCount++ == 0) {
2378 Vector< sp<EffectModule> > effects;
2379 getSuspendEligibleEffects(effects);
2380 for (size_t i = 0; i < effects.size(); i++) {
2381 setEffectSuspended_l(&effects[i]->desc().type, true);
2382 }
2383 }
2384 } else {
2385 if (index < 0) {
2386 return;
2387 }
2388 desc = mSuspendedEffects.valueAt(index);
2389 if (desc->mRefCount <= 0) {
2390 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2391 desc->mRefCount = 1;
2392 }
2393 if (--desc->mRefCount == 0) {
2394 Vector<const effect_uuid_t *> types;
2395 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2396 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2397 continue;
2398 }
2399 types.add(&mSuspendedEffects.valueAt(i)->mType);
2400 }
2401 for (size_t i = 0; i < types.size(); i++) {
2402 setEffectSuspended_l(types[i], false);
2403 }
2404 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2405 mSuspendedEffects.keyAt(index));
2406 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2407 }
2408 }
2409}
2410
2411
2412// The volume effect is used for automated tests only
2413#ifndef OPENSL_ES_H_
2414static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2415 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2416const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2417#endif //OPENSL_ES_H_
2418
Eric Laurentd8365c52017-07-16 15:27:05 -07002419/* static */
2420bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2421{
2422 // Only NS and AEC are suspended when BtNRec is off
2423 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2424 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2425 return true;
2426 }
2427 return false;
2428}
2429
Eric Laurentca7cc822012-11-19 14:55:58 -08002430bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2431{
2432 // auxiliary effects and visualizer are never suspended on output mix
2433 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2434 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2435 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2436 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2437 return false;
2438 }
2439 return true;
2440}
2441
2442void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2443 Vector< sp<AudioFlinger::EffectModule> > &effects)
2444{
2445 effects.clear();
2446 for (size_t i = 0; i < mEffects.size(); i++) {
2447 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2448 effects.add(mEffects[i]);
2449 }
2450 }
2451}
2452
2453sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2454 const effect_uuid_t *type)
2455{
2456 sp<EffectModule> effect = getEffectFromType_l(type);
2457 return effect != 0 && effect->isEnabled() ? effect : 0;
2458}
2459
2460void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2461 bool enabled)
2462{
2463 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2464 if (enabled) {
2465 if (index < 0) {
2466 // if the effect is not suspend check if all effects are suspended
2467 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2468 if (index < 0) {
2469 return;
2470 }
2471 if (!isEffectEligibleForSuspend(effect->desc())) {
2472 return;
2473 }
2474 setEffectSuspended_l(&effect->desc().type, enabled);
2475 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2476 if (index < 0) {
2477 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2478 return;
2479 }
2480 }
2481 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2482 effect->desc().type.timeLow);
2483 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002484 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002485 if (desc->mEffect == 0) {
2486 desc->mEffect = effect;
2487 effect->setEnabled(false);
2488 effect->setSuspended(true);
2489 }
2490 } else {
2491 if (index < 0) {
2492 return;
2493 }
2494 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2495 effect->desc().type.timeLow);
2496 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2497 desc->mEffect.clear();
2498 effect->setSuspended(false);
2499 }
2500}
2501
Eric Laurent5baf2af2013-09-12 17:37:00 -07002502bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002503{
2504 Mutex::Autolock _l(mLock);
2505 size_t size = mEffects.size();
2506 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002507 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002508 return true;
2509 }
2510 }
2511 return false;
2512}
2513
Eric Laurentaaa44472014-09-12 17:41:50 -07002514void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2515{
2516 Mutex::Autolock _l(mLock);
2517 mThread = thread;
2518 for (size_t i = 0; i < mEffects.size(); i++) {
2519 mEffects[i]->setThread(thread);
2520 }
2521}
2522
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002523void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2524{
2525 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2526 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2527 }
2528 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2529 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2530 }
2531}
2532
2533void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2534{
2535 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2536 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2537 }
2538 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2539 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2540 }
2541}
2542
2543bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002544{
2545 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002546 for (const auto &effect : mEffects) {
2547 if (effect->isProcessImplemented()) {
2548 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002549 }
2550 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002551 // Allow effects without processing.
2552 return true;
2553}
2554
2555bool AudioFlinger::EffectChain::isFastCompatible() const
2556{
2557 Mutex::Autolock _l(mLock);
2558 for (const auto &effect : mEffects) {
2559 if (effect->isProcessImplemented()
2560 && effect->isImplementationSoftware()) {
2561 return false;
2562 }
2563 }
2564 // Allow effects without processing or hw accelerated effects.
2565 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002566}
2567
2568// isCompatibleWithThread_l() must be called with thread->mLock held
2569bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2570{
2571 Mutex::Autolock _l(mLock);
2572 for (size_t i = 0; i < mEffects.size(); i++) {
2573 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2574 return false;
2575 }
2576 }
2577 return true;
2578}
2579
Glenn Kasten63238ef2015-03-02 15:50:29 -08002580} // namespace android