blob: 024e7ebc13c62b593d186f622659ca5641c007db [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:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900266 // volume control for offload and direct threads must take effect immediately.
267 if (stop_l() == NO_ERROR
268 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700269 mDisableWaitCnt = mMaxDisableWaitCnt;
270 } else {
271 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
272 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800273 mState = STOPPED;
274 break;
275 case STOPPED:
276 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
277 // turn off sequence.
278 if (--mDisableWaitCnt == 0) {
279 reset_l();
280 mState = IDLE;
281 }
282 break;
283 default: //IDLE , ACTIVE, DESTROYED
284 break;
285 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700286
287 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800288}
289
290void AudioFlinger::EffectModule::process()
291{
292 Mutex::Autolock _l(mLock);
293
Mikhail Naganov022b9952017-01-04 16:36:51 -0800294 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800295 return;
296 }
297
rago94a1ee82017-07-21 15:11:02 -0700298 const uint32_t inChannelCount =
299 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
300 const uint32_t outChannelCount =
301 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
302 const bool auxType =
303 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
304
Andy Hungfa69ca32017-11-30 10:07:53 -0800305 // safeInputOutputSampleCount is 0 if the channel count between input and output
306 // buffers do not match. This prevents automatic accumulation or copying between the
307 // input and output effect buffers without an intermediary effect process.
308 // TODO: consider implementing channel conversion.
309 const size_t safeInputOutputSampleCount =
310 inChannelCount != outChannelCount ? 0
311 : outChannelCount * std::min(
312 mConfig.inputCfg.buffer.frameCount,
313 mConfig.outputCfg.buffer.frameCount);
314 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
315#ifdef FLOAT_EFFECT_CHAIN
316 accumulate_float(
317 mConfig.outputCfg.buffer.f32,
318 mConfig.inputCfg.buffer.f32,
319 safeInputOutputSampleCount);
320#else
321 accumulate_i16(
322 mConfig.outputCfg.buffer.s16,
323 mConfig.inputCfg.buffer.s16,
324 safeInputOutputSampleCount);
325#endif
326 };
327 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
328#ifdef FLOAT_EFFECT_CHAIN
329 memcpy(
330 mConfig.outputCfg.buffer.f32,
331 mConfig.inputCfg.buffer.f32,
332 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
333
334#else
335 memcpy(
336 mConfig.outputCfg.buffer.s16,
337 mConfig.inputCfg.buffer.s16,
338 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
339#endif
340 };
341
Eric Laurentca7cc822012-11-19 14:55:58 -0800342 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700343 int ret;
344 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700345 if (auxType) {
346 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800347 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700348#ifdef FLOAT_EFFECT_CHAIN
349 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800350#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700351 // Do in-place float conversion for auxiliary effect input buffer.
352 static_assert(sizeof(float) <= sizeof(int32_t),
353 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
354
Andy Hungfa69ca32017-11-30 10:07:53 -0800355 memcpy_to_float_from_q4_27(
356 mConfig.inputCfg.buffer.f32,
357 mConfig.inputCfg.buffer.s32,
358 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800359#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800360 } else
Andy Hung116a4982017-11-30 10:15:08 -0800361#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800362 {
Andy Hung116a4982017-11-30 10:15:08 -0800363#ifdef FLOAT_AUX
364 memcpy_to_i16_from_float(
365 mConfig.inputCfg.buffer.s16,
366 mConfig.inputCfg.buffer.f32,
367 mConfig.inputCfg.buffer.frameCount);
368#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800369 memcpy_to_i16_from_q4_27(
370 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700371 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800372 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800373#endif
rago94a1ee82017-07-21 15:11:02 -0700374 }
rago94a1ee82017-07-21 15:11:02 -0700375 }
376#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800377 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
378 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
379
380 if (!auxType && mInChannelCountRequested != inChannelCount) {
381 adjust_channels(
382 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
383 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
384 sizeof(float),
385 sizeof(float)
386 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
387 inBuffer = mInConversionBuffer;
388 }
389 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
390 && mOutChannelCountRequested != outChannelCount) {
391 adjust_selected_channels(
392 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
393 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
394 sizeof(float),
395 sizeof(float)
396 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
397 outBuffer = mOutConversionBuffer;
398 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800399 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
400 if (!auxType) {
401 if (mInConversionBuffer.get() == nullptr) {
402 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
403 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700404 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800405 memcpy_to_i16_from_float(
406 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800407 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800408 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800409 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700410 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800411 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
412 if (mOutConversionBuffer.get() == nullptr) {
413 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
414 goto data_bypass;
415 }
416 memcpy_to_i16_from_float(
417 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800418 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800419 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800420 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700421 }
422 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800423#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800424 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800425#ifdef FLOAT_EFFECT_CHAIN
426 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800427 sp<EffectBufferHalInterface> target =
428 mOutChannelCountRequested != outChannelCount
429 ? mOutConversionBuffer : mOutBuffer;
430
Andy Hungfa69ca32017-11-30 10:07:53 -0800431 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800432 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800433 mOutConversionBuffer->audioBuffer()->s16,
434 outChannelCount * mConfig.outputCfg.buffer.frameCount);
435 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800436 if (mOutChannelCountRequested != outChannelCount) {
437 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
438 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
439 sizeof(float),
440 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
441 }
rago94a1ee82017-07-21 15:11:02 -0700442#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700443 } else {
rago94a1ee82017-07-21 15:11:02 -0700444#ifdef FLOAT_EFFECT_CHAIN
445 data_bypass:
446#endif
447 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800448 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700449 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800450 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700451 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800452 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700453 }
454 }
455 ret = -ENODATA;
456 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800457
Eric Laurentca7cc822012-11-19 14:55:58 -0800458 // force transition to IDLE state when engine is ready
459 if (mState == STOPPED && ret == -ENODATA) {
460 mDisableWaitCnt = 1;
461 }
462
463 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700464 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800465#ifdef FLOAT_AUX
466 const size_t size =
467 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
468#else
rago94a1ee82017-07-21 15:11:02 -0700469 const size_t size =
470 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800471#endif
rago94a1ee82017-07-21 15:11:02 -0700472 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800473 }
474 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700475 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800476 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
477 // If an insert effect is idle and input buffer is different from output buffer,
478 // accumulate input onto output
479 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800480 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
481 accumulateInputToOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800482 }
483 }
484}
485
486void AudioFlinger::EffectModule::reset_l()
487{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700488 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800489 return;
490 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700491 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800492}
493
494status_t AudioFlinger::EffectModule::configure()
495{
rago94a1ee82017-07-21 15:11:02 -0700496 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700497 status_t status;
498 sp<ThreadBase> thread;
499 uint32_t size;
500 audio_channel_mask_t channelMask;
501
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700502 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700503 status = NO_INIT;
504 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800505 }
506
Eric Laurentd0ebb532013-04-02 16:41:41 -0700507 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800508 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700509 status = DEAD_OBJECT;
510 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800511 }
512
513 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800514 // TODO: handle configuration of input (record) SW effects above the HAL,
515 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
516 // in which case input channel masks should be used here.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700517 channelMask = thread->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800518 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700519 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800520
521 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800522 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
523 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
524 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
525 mConfig.inputCfg.channels);
526 }
527#ifndef MULTICHANNEL_EFFECT_CHAIN
528 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
529 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
530 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
531 mConfig.outputCfg.channels);
532 }
533#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800534 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800535#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700536 // TODO: Update this logic when multichannel effects are implemented.
537 // For offloaded tracks consider mono output as stereo for proper effect initialization
538 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
539 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
540 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
541 ALOGV("Overriding effect input and output as STEREO");
542 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800543#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800544 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800545 mInChannelCountRequested =
546 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
547 mOutChannelCountRequested =
548 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700549
rago94a1ee82017-07-21 15:11:02 -0700550 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
551 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800552 mConfig.inputCfg.samplingRate = thread->sampleRate();
553 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
554 mConfig.inputCfg.bufferProvider.cookie = NULL;
555 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
556 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
557 mConfig.outputCfg.bufferProvider.cookie = NULL;
558 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
559 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
560 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
561 // Insert effect:
562 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
563 // always overwrites output buffer: input buffer == output buffer
564 // - in other sessions:
565 // last effect in the chain accumulates in output buffer: input buffer != output buffer
566 // other effect: overwrites output buffer: input buffer == output buffer
567 // Auxiliary effect:
568 // accumulates in output buffer: input buffer != output buffer
569 // Therefore: accumulate <=> input buffer != output buffer
570 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
571 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
572 } else {
573 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
574 }
575 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
576 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
577 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
578 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
579
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700580 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800581 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
582
583 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700584 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700585 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800586 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700587 &mConfig,
588 &size,
589 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700590 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800591 status = cmdStatus;
592 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800593
594#ifdef MULTICHANNEL_EFFECT_CHAIN
595 if (status != NO_ERROR &&
Andy Hunge6a61a52018-04-06 18:55:26 -0700596 thread->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800597 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
598 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
599 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700600 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
601 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800602 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
603 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
604 }
605 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
606 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
607 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
608 }
609 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700610 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800611 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700612 &mConfig,
613 &size,
614 &cmdStatus);
615 if (status == NO_ERROR) {
616 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800617 }
618 }
619#endif
620
621#ifdef FLOAT_EFFECT_CHAIN
622 if (status == NO_ERROR) {
623 mSupportsFloat = true;
624 }
625
626 if (status != NO_ERROR) {
627 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
628 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
629 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
630 size = sizeof(int);
631 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
632 sizeof(mConfig),
633 &mConfig,
634 &size,
635 &cmdStatus);
636 if (status == NO_ERROR) {
637 status = cmdStatus;
638 }
639 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700640 mSupportsFloat = false;
641 ALOGVV("config worked with 16 bit");
642 } else {
643 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800644 }
rago94a1ee82017-07-21 15:11:02 -0700645 }
646#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800647
rago94a1ee82017-07-21 15:11:02 -0700648 if (status == NO_ERROR) {
649 // Establish Buffer strategy
650 setInBuffer(mInBuffer);
651 setOutBuffer(mOutBuffer);
652
653 // Update visualizer latency
654 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
655 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
656 effect_param_t *p = (effect_param_t *)buf32;
657
658 p->psize = sizeof(uint32_t);
659 p->vsize = sizeof(uint32_t);
660 size = sizeof(int);
661 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
662
663 uint32_t latency = 0;
664 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
665 if (pbt != NULL) {
666 latency = pbt->latency_l();
667 }
668
669 *((int32_t *)p->data + 1)= latency;
670 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
671 sizeof(effect_param_t) + 8,
672 &buf32,
673 &size,
674 &cmdStatus);
675 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800676 }
677
Andy Hung05083ac2017-12-14 15:00:28 -0800678 // mConfig.outputCfg.buffer.frameCount cannot be zero.
679 mMaxDisableWaitCnt = (uint32_t)std::max(
680 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
681 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
682 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800683
Eric Laurentd0ebb532013-04-02 16:41:41 -0700684exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800685 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700686 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700687 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800688 return status;
689}
690
691status_t AudioFlinger::EffectModule::init()
692{
693 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700694 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800695 return NO_INIT;
696 }
697 status_t cmdStatus;
698 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700699 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
700 0,
701 NULL,
702 &size,
703 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800704 if (status == 0) {
705 status = cmdStatus;
706 }
707 return status;
708}
709
Eric Laurent1b928682014-10-02 19:41:47 -0700710void AudioFlinger::EffectModule::addEffectToHal_l()
711{
712 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
713 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
714 sp<ThreadBase> thread = mThread.promote();
715 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700716 sp<StreamHalInterface> stream = thread->stream();
717 if (stream != 0) {
718 status_t result = stream->addEffect(mEffectInterface);
719 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700720 }
721 }
722 }
723}
724
Eric Laurentfa1e1232016-08-02 19:01:49 -0700725// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800726status_t AudioFlinger::EffectModule::start()
727{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700728 sp<EffectChain> chain;
729 status_t status;
730 {
731 Mutex::Autolock _l(mLock);
732 status = start_l();
733 if (status == NO_ERROR) {
734 chain = mChain.promote();
735 }
736 }
737 if (chain != 0) {
738 chain->resetVolume_l();
739 }
740 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800741}
742
743status_t AudioFlinger::EffectModule::start_l()
744{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700745 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800746 return NO_INIT;
747 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700748 if (mStatus != NO_ERROR) {
749 return mStatus;
750 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800751 status_t cmdStatus;
752 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700753 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
754 0,
755 NULL,
756 &size,
757 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800758 if (status == 0) {
759 status = cmdStatus;
760 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700761 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700762 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800763 }
764 return status;
765}
766
767status_t AudioFlinger::EffectModule::stop()
768{
769 Mutex::Autolock _l(mLock);
770 return stop_l();
771}
772
773status_t AudioFlinger::EffectModule::stop_l()
774{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700775 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800776 return NO_INIT;
777 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700778 if (mStatus != NO_ERROR) {
779 return mStatus;
780 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800781 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800782 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900783
784 if (isVolumeControl() && isOffloadedOrDirect()) {
785 sp<EffectChain>chain = mChain.promote();
786 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
787 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
788 mSetVolumeReentrantTid = gettid();
789 chain->resetVolume_l();
790 mSetVolumeReentrantTid = INVALID_PID;
791 }
792
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700793 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
794 0,
795 NULL,
796 &size,
797 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800798 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800799 status = cmdStatus;
800 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800801 if (status == NO_ERROR) {
802 status = remove_effect_from_hal_l();
803 }
804 return status;
805}
806
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800807// must be called with EffectChain::mLock held
808void AudioFlinger::EffectModule::release_l()
809{
810 if (mEffectInterface != 0) {
811 remove_effect_from_hal_l();
812 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800813 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800814 mEffectInterface.clear();
815 }
816}
817
Eric Laurentbfb1b832013-01-07 09:53:42 -0800818status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
819{
820 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
821 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800822 sp<ThreadBase> thread = mThread.promote();
823 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700824 sp<StreamHalInterface> stream = thread->stream();
825 if (stream != 0) {
826 status_t result = stream->removeEffect(mEffectInterface);
827 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800828 }
829 }
830 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800831 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800832}
833
Andy Hunge4a1d912016-08-17 14:11:13 -0700834// round up delta valid if value and divisor are positive.
835template <typename T>
836static T roundUpDelta(const T &value, const T &divisor) {
837 T remainder = value % divisor;
838 return remainder == 0 ? 0 : divisor - remainder;
839}
840
Eric Laurentca7cc822012-11-19 14:55:58 -0800841status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
842 uint32_t cmdSize,
843 void *pCmdData,
844 uint32_t *replySize,
845 void *pReplyData)
846{
847 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700848 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800849
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700850 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800851 return NO_INIT;
852 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700853 if (mStatus != NO_ERROR) {
854 return mStatus;
855 }
Andy Hung110bc952016-06-20 15:22:52 -0700856 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700857 (sizeof(effect_param_t) > cmdSize ||
858 ((effect_param_t *)pCmdData)->psize > cmdSize
859 - sizeof(effect_param_t))) {
860 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800861 android_errorWriteLog(0x534e4554, "33003822");
862 return -EINVAL;
863 }
864 if (cmdCode == EFFECT_CMD_GET_PARAM &&
865 (*replySize < sizeof(effect_param_t) ||
866 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
867 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700868 return -EINVAL;
869 }
ragoe2759072016-11-22 18:02:48 -0800870 if (cmdCode == EFFECT_CMD_GET_PARAM &&
871 (sizeof(effect_param_t) > *replySize
872 || ((effect_param_t *)pCmdData)->psize > *replySize
873 - sizeof(effect_param_t)
874 || ((effect_param_t *)pCmdData)->vsize > *replySize
875 - sizeof(effect_param_t)
876 - ((effect_param_t *)pCmdData)->psize
877 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
878 *replySize
879 - sizeof(effect_param_t)
880 - ((effect_param_t *)pCmdData)->psize
881 - ((effect_param_t *)pCmdData)->vsize)) {
882 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
883 android_errorWriteLog(0x534e4554, "32705438");
884 return -EINVAL;
885 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700886 if ((cmdCode == EFFECT_CMD_SET_PARAM
887 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
888 (sizeof(effect_param_t) > cmdSize
889 || ((effect_param_t *)pCmdData)->psize > cmdSize
890 - sizeof(effect_param_t)
891 || ((effect_param_t *)pCmdData)->vsize > cmdSize
892 - sizeof(effect_param_t)
893 - ((effect_param_t *)pCmdData)->psize
894 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
895 cmdSize
896 - sizeof(effect_param_t)
897 - ((effect_param_t *)pCmdData)->psize
898 - ((effect_param_t *)pCmdData)->vsize)) {
899 android_errorWriteLog(0x534e4554, "30204301");
900 return -EINVAL;
901 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700902 status_t status = mEffectInterface->command(cmdCode,
903 cmdSize,
904 pCmdData,
905 replySize,
906 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800907 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
908 uint32_t size = (replySize == NULL) ? 0 : *replySize;
909 for (size_t i = 1; i < mHandles.size(); i++) {
910 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800911 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800912 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
913 }
914 }
915 }
916 return status;
917}
918
919status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
920{
921 Mutex::Autolock _l(mLock);
922 return setEnabled_l(enabled);
923}
924
925// must be called with EffectModule::mLock held
926status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
927{
928
929 ALOGV("setEnabled %p enabled %d", this, enabled);
930
931 if (enabled != isEnabled()) {
932 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
933 if (enabled && status != NO_ERROR) {
934 return status;
935 }
936
937 switch (mState) {
938 // going from disabled to enabled
939 case IDLE:
940 mState = STARTING;
941 break;
942 case STOPPED:
943 mState = RESTART;
944 break;
945 case STOPPING:
946 mState = ACTIVE;
947 break;
948
949 // going from enabled to disabled
950 case RESTART:
951 mState = STOPPED;
952 break;
953 case STARTING:
954 mState = IDLE;
955 break;
956 case ACTIVE:
957 mState = STOPPING;
958 break;
959 case DESTROYED:
960 return NO_ERROR; // simply ignore as we are being destroyed
961 }
962 for (size_t i = 1; i < mHandles.size(); i++) {
963 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800964 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800965 h->setEnabled(enabled);
966 }
967 }
968 }
969 return NO_ERROR;
970}
971
972bool AudioFlinger::EffectModule::isEnabled() const
973{
974 switch (mState) {
975 case RESTART:
976 case STARTING:
977 case ACTIVE:
978 return true;
979 case IDLE:
980 case STOPPING:
981 case STOPPED:
982 case DESTROYED:
983 default:
984 return false;
985 }
986}
987
988bool AudioFlinger::EffectModule::isProcessEnabled() const
989{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700990 if (mStatus != NO_ERROR) {
991 return false;
992 }
993
Eric Laurentca7cc822012-11-19 14:55:58 -0800994 switch (mState) {
995 case RESTART:
996 case ACTIVE:
997 case STOPPING:
998 case STOPPED:
999 return true;
1000 case IDLE:
1001 case STARTING:
1002 case DESTROYED:
1003 default:
1004 return false;
1005 }
1006}
1007
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001008bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1009{
1010 return (mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT);
1011}
1012
1013bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1014{
1015 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1016}
1017
Mikhail Naganov022b9952017-01-04 16:36:51 -08001018void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001019 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001020
1021 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001022 if (buffer != 0) {
1023 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1024 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1025 } else {
1026 mConfig.inputCfg.buffer.raw = NULL;
1027 }
1028 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001029 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001030
1031#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001032 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001033 // Theoretically insert effects can also do in-place conversions (destroying
1034 // the original buffer) when the output buffer is identical to the input buffer,
1035 // but we don't optimize for it here.
1036 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001037 const uint32_t inChannelCount =
1038 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1039 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1040 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001041 // we need to translate - create hidl shared buffer and intercept
1042 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001043 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1044 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1045 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001046
1047 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1048 __func__, inChannels, inFrameCount, size);
1049
Andy Hungbded9c82017-11-30 18:47:35 -08001050 if (size > 0 && (mInConversionBuffer.get() == nullptr
1051 || size > mInConversionBuffer->getSize())) {
1052 mInConversionBuffer.clear();
1053 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001054 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1055 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1056 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001057 }
Andy Hungbded9c82017-11-30 18:47:35 -08001058 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001059 mInConversionBuffer->setFrameCount(inFrameCount);
1060 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001061 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001062 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001063 }
1064 }
1065#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001066}
1067
1068void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001069 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001070
1071 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001072 if (buffer != 0) {
1073 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1074 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1075 } else {
1076 mConfig.outputCfg.buffer.raw = NULL;
1077 }
1078 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001079 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001080
1081#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001082 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001083 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001084 const uint32_t outChannelCount =
1085 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1086 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1087 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001088 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001089 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1090 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1091 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001092
1093 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1094 __func__, outChannels, outFrameCount, size);
1095
Andy Hungbded9c82017-11-30 18:47:35 -08001096 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1097 || size > mOutConversionBuffer->getSize())) {
1098 mOutConversionBuffer.clear();
1099 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001100 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1101 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1102 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001103 }
Andy Hungbded9c82017-11-30 18:47:35 -08001104 if (mOutConversionBuffer.get() != nullptr) {
1105 mOutConversionBuffer->setFrameCount(outFrameCount);
1106 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001107 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001108 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001109 }
1110 }
1111#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001112}
1113
Eric Laurentca7cc822012-11-19 14:55:58 -08001114status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1115{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001116 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001117 if (mStatus != NO_ERROR) {
1118 return mStatus;
1119 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001120 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001121 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1122 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1123 if (isProcessEnabled() &&
1124 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
1125 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001126 uint32_t volume[2];
1127 uint32_t *pVolume = NULL;
1128 uint32_t size = sizeof(volume);
1129 volume[0] = *left;
1130 volume[1] = *right;
1131 if (controller) {
1132 pVolume = volume;
1133 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001134 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1135 size,
1136 volume,
1137 &size,
1138 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001139 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1140 *left = volume[0];
1141 *right = volume[1];
1142 }
1143 }
1144 return status;
1145}
1146
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001147void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1148{
1149 sp<ThreadBase> thread = mThread.promote();
1150 if (thread != 0 &&
1151 (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::DIRECT)) {
1152 PlaybackThread *t = (PlaybackThread *)thread.get();
1153 float vol_l = (float)left / (1 << 24);
1154 float vol_r = (float)right / (1 << 24);
1155 t->setVolumeForOutput_l(vol_l, vol_r);
1156 }
1157}
1158
Eric Laurentca7cc822012-11-19 14:55:58 -08001159status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1160{
1161 if (device == AUDIO_DEVICE_NONE) {
1162 return NO_ERROR;
1163 }
1164
1165 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001166 if (mStatus != NO_ERROR) {
1167 return mStatus;
1168 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001169 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001170 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001171 status_t cmdStatus;
1172 uint32_t size = sizeof(status_t);
1173 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1174 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001175 status = mEffectInterface->command(cmd,
1176 sizeof(uint32_t),
1177 &device,
1178 &size,
1179 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001180 }
1181 return status;
1182}
1183
1184status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1185{
1186 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001187 if (mStatus != NO_ERROR) {
1188 return mStatus;
1189 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001190 status_t status = NO_ERROR;
1191 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1192 status_t cmdStatus;
1193 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001194 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1195 sizeof(audio_mode_t),
1196 &mode,
1197 &size,
1198 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001199 if (status == NO_ERROR) {
1200 status = cmdStatus;
1201 }
1202 }
1203 return status;
1204}
1205
1206status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1207{
1208 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001209 if (mStatus != NO_ERROR) {
1210 return mStatus;
1211 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001212 status_t status = NO_ERROR;
1213 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1214 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001215 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1216 sizeof(audio_source_t),
1217 &source,
1218 &size,
1219 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001220 }
1221 return status;
1222}
1223
1224void AudioFlinger::EffectModule::setSuspended(bool suspended)
1225{
1226 Mutex::Autolock _l(mLock);
1227 mSuspended = suspended;
1228}
1229
1230bool AudioFlinger::EffectModule::suspended() const
1231{
1232 Mutex::Autolock _l(mLock);
1233 return mSuspended;
1234}
1235
1236bool AudioFlinger::EffectModule::purgeHandles()
1237{
1238 bool enabled = false;
1239 Mutex::Autolock _l(mLock);
1240 for (size_t i = 0; i < mHandles.size(); i++) {
1241 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001242 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001243 if (handle->hasControl()) {
1244 enabled = handle->enabled();
1245 }
1246 }
1247 }
1248 return enabled;
1249}
1250
Eric Laurent5baf2af2013-09-12 17:37:00 -07001251status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1252{
1253 Mutex::Autolock _l(mLock);
1254 if (mStatus != NO_ERROR) {
1255 return mStatus;
1256 }
1257 status_t status = NO_ERROR;
1258 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1259 status_t cmdStatus;
1260 uint32_t size = sizeof(status_t);
1261 effect_offload_param_t cmd;
1262
1263 cmd.isOffload = offloaded;
1264 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001265 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1266 sizeof(effect_offload_param_t),
1267 &cmd,
1268 &size,
1269 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001270 if (status == NO_ERROR) {
1271 status = cmdStatus;
1272 }
1273 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1274 } else {
1275 if (offloaded) {
1276 status = INVALID_OPERATION;
1277 }
1278 mOffloaded = false;
1279 }
1280 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1281 return status;
1282}
1283
1284bool AudioFlinger::EffectModule::isOffloaded() const
1285{
1286 Mutex::Autolock _l(mLock);
1287 return mOffloaded;
1288}
1289
Marco Nelissenb2208842014-02-07 14:00:50 -08001290String8 effectFlagsToString(uint32_t flags) {
1291 String8 s;
1292
1293 s.append("conn. mode: ");
1294 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1295 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1296 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1297 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1298 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1299 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1300 default: s.append("unknown/reserved"); break;
1301 }
1302 s.append(", ");
1303
1304 s.append("insert pref: ");
1305 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1306 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1307 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1308 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1309 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1310 default: s.append("unknown/reserved"); break;
1311 }
1312 s.append(", ");
1313
1314 s.append("volume mgmt: ");
1315 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1316 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1317 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1318 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1319 default: s.append("unknown/reserved"); break;
1320 }
1321 s.append(", ");
1322
1323 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1324 if (devind) {
1325 s.append("device indication: ");
1326 switch (devind) {
1327 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1328 default: s.append("unknown/reserved"); break;
1329 }
1330 s.append(", ");
1331 }
1332
1333 s.append("input mode: ");
1334 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1335 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1336 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1337 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1338 default: s.append("not set"); break;
1339 }
1340 s.append(", ");
1341
1342 s.append("output mode: ");
1343 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1344 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1345 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1346 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1347 default: s.append("not set"); break;
1348 }
1349 s.append(", ");
1350
1351 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1352 if (accel) {
1353 s.append("hardware acceleration: ");
1354 switch (accel) {
1355 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1356 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1357 default: s.append("unknown/reserved"); break;
1358 }
1359 s.append(", ");
1360 }
1361
1362 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1363 if (modeind) {
1364 s.append("mode indication: ");
1365 switch (modeind) {
1366 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1367 default: s.append("unknown/reserved"); break;
1368 }
1369 s.append(", ");
1370 }
1371
1372 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1373 if (srcind) {
1374 s.append("source indication: ");
1375 switch (srcind) {
1376 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1377 default: s.append("unknown/reserved"); break;
1378 }
1379 s.append(", ");
1380 }
1381
1382 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1383 s.append("offloadable, ");
1384 }
1385
1386 int len = s.length();
1387 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001388 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001389 s.unlockBuffer(len - 2);
1390 }
1391 return s;
1392}
1393
Andy Hungbded9c82017-11-30 18:47:35 -08001394static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1395 std::stringstream ss;
1396
1397 if (buffer.get() == nullptr) {
1398 return "nullptr"; // make different than below
1399 } else if (buffer->externalData() != nullptr) {
1400 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1401 << " -> "
1402 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1403 } else {
1404 ss << buffer->audioBuffer()->raw;
1405 }
1406 return ss.str();
1407}
Marco Nelissenb2208842014-02-07 14:00:50 -08001408
Glenn Kasten0f11b512014-01-31 16:18:54 -08001409void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001410{
Eric Laurentca7cc822012-11-19 14:55:58 -08001411 String8 result;
1412
Andy Hung9718d662017-12-22 17:57:39 -08001413 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001414
1415 bool locked = AudioFlinger::dumpTryLock(mLock);
1416 // failed to lock - AudioFlinger is probably deadlocked
1417 if (!locked) {
1418 result.append("\t\tCould not lock Fx mutex:\n");
1419 }
1420
1421 result.append("\t\tSession Status State Engine:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001422 result.appendFormat("\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001423 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001424
1425 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001426 char uuidStr[64];
1427 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001428 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001429 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001430 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1431 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001432 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001433 mDescriptor.flags,
1434 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001435 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001436 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001437
1438 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001439 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001440
1441 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001442
1443 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001444 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1445 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1446 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001447 mConfig.inputCfg.buffer.frameCount,
1448 mConfig.inputCfg.samplingRate,
1449 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001450 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001451 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001452
1453 result.append("\t\t- Output configuration:\n");
1454 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001455 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001456 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001457 mConfig.outputCfg.buffer.frameCount,
1458 mConfig.outputCfg.samplingRate,
1459 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001460 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001461 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001462
rago94a1ee82017-07-21 15:11:02 -07001463#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001464
Andy Hungbded9c82017-11-30 18:47:35 -08001465 result.appendFormat("\t\t- HAL buffers:\n"
1466 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1467 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1468 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1469 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1470 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001471#endif
1472
Andy Hung9718d662017-12-22 17:57:39 -08001473 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001474 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001475 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001476 for (size_t i = 0; i < mHandles.size(); ++i) {
1477 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001478 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001479 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001480 result.append(buffer);
1481 }
1482 }
1483
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 write(fd, result.string(), result.length());
1485
1486 if (locked) {
1487 mLock.unlock();
1488 }
1489}
1490
1491// ----------------------------------------------------------------------------
1492// EffectHandle implementation
1493// ----------------------------------------------------------------------------
1494
1495#undef LOG_TAG
1496#define LOG_TAG "AudioFlinger::EffectHandle"
1497
1498AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1499 const sp<AudioFlinger::Client>& client,
1500 const sp<IEffectClient>& effectClient,
1501 int32_t priority)
1502 : BnEffect(),
1503 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001504 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001505{
1506 ALOGV("constructor %p", this);
1507
1508 if (client == 0) {
1509 return;
1510 }
1511 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1512 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001513 if (mCblkMemory == 0 ||
1514 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001515 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001516 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001517 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001518 return;
1519 }
Glenn Kastene75da402013-11-20 13:54:52 -08001520 new(mCblk) effect_param_cblk_t();
1521 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001522}
1523
1524AudioFlinger::EffectHandle::~EffectHandle()
1525{
1526 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001527 disconnect(false);
1528}
1529
Glenn Kastene75da402013-11-20 13:54:52 -08001530status_t AudioFlinger::EffectHandle::initCheck()
1531{
1532 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1533}
1534
Eric Laurentca7cc822012-11-19 14:55:58 -08001535status_t AudioFlinger::EffectHandle::enable()
1536{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001537 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001538 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001539 sp<EffectModule> effect = mEffect.promote();
1540 if (effect == 0 || mDisconnected) {
1541 return DEAD_OBJECT;
1542 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001543 if (!mHasControl) {
1544 return INVALID_OPERATION;
1545 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001546
1547 if (mEnabled) {
1548 return NO_ERROR;
1549 }
1550
1551 mEnabled = true;
1552
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001553 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001554 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001555 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001556 }
1557
1558 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001559 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001560 return NO_ERROR;
1561 }
1562
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001563 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001564 if (status != NO_ERROR) {
1565 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001566 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001567 }
1568 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001569 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001570 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001571 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1572 Mutex::Autolock _l(thread->mLock);
1573 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001574 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001575 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001576 if (thread->type() == ThreadBase::OFFLOAD) {
1577 PlaybackThread *t = (PlaybackThread *)thread.get();
1578 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1579 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001580 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001581 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1582 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001583 }
1584 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001585 }
1586 return status;
1587}
1588
1589status_t AudioFlinger::EffectHandle::disable()
1590{
1591 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001592 AutoMutex _l(mLock);
1593 sp<EffectModule> effect = mEffect.promote();
1594 if (effect == 0 || mDisconnected) {
1595 return DEAD_OBJECT;
1596 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001597 if (!mHasControl) {
1598 return INVALID_OPERATION;
1599 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001600
1601 if (!mEnabled) {
1602 return NO_ERROR;
1603 }
1604 mEnabled = false;
1605
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001606 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001607 return NO_ERROR;
1608 }
1609
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001610 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001611
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001612 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001613 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001614 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001615 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1616 Mutex::Autolock _l(thread->mLock);
1617 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001618 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001619 }
1620
1621 return status;
1622}
1623
1624void AudioFlinger::EffectHandle::disconnect()
1625{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001626 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001627 disconnect(true);
1628}
1629
1630void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1631{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001632 AutoMutex _l(mLock);
1633 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1634 if (mDisconnected) {
1635 if (unpinIfLast) {
1636 android_errorWriteLog(0x534e4554, "32707507");
1637 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001638 return;
1639 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001640 mDisconnected = true;
1641 sp<ThreadBase> thread;
1642 {
1643 sp<EffectModule> effect = mEffect.promote();
1644 if (effect != 0) {
1645 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001646 }
1647 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001648 if (thread != 0) {
1649 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001650 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001651 // try to cleanup as much as we can
1652 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001653 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1654 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001655 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001656 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001657
Eric Laurentca7cc822012-11-19 14:55:58 -08001658 if (mClient != 0) {
1659 if (mCblk != NULL) {
1660 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1661 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1662 }
1663 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001664 // Client destructor must run with AudioFlinger client mutex locked
1665 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 mClient.clear();
1667 }
1668}
1669
1670status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1671 uint32_t cmdSize,
1672 void *pCmdData,
1673 uint32_t *replySize,
1674 void *pReplyData)
1675{
1676 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001677 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001678
Eric Laurentc7ab3092017-06-15 18:43:46 -07001679 // reject commands reserved for internal use by audio framework if coming from outside
1680 // of audioserver
1681 switch(cmdCode) {
1682 case EFFECT_CMD_ENABLE:
1683 case EFFECT_CMD_DISABLE:
1684 case EFFECT_CMD_SET_PARAM:
1685 case EFFECT_CMD_SET_PARAM_DEFERRED:
1686 case EFFECT_CMD_SET_PARAM_COMMIT:
1687 case EFFECT_CMD_GET_PARAM:
1688 break;
1689 default:
1690 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1691 break;
1692 }
1693 android_errorWriteLog(0x534e4554, "62019992");
1694 return BAD_VALUE;
1695 }
1696
Eric Laurent1ffc5852016-12-15 14:46:09 -08001697 if (cmdCode == EFFECT_CMD_ENABLE) {
1698 if (*replySize < sizeof(int)) {
1699 android_errorWriteLog(0x534e4554, "32095713");
1700 return BAD_VALUE;
1701 }
1702 *(int *)pReplyData = NO_ERROR;
1703 *replySize = sizeof(int);
1704 return enable();
1705 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1706 if (*replySize < sizeof(int)) {
1707 android_errorWriteLog(0x534e4554, "32095713");
1708 return BAD_VALUE;
1709 }
1710 *(int *)pReplyData = NO_ERROR;
1711 *replySize = sizeof(int);
1712 return disable();
1713 }
1714
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001715 AutoMutex _l(mLock);
1716 sp<EffectModule> effect = mEffect.promote();
1717 if (effect == 0 || mDisconnected) {
1718 return DEAD_OBJECT;
1719 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001720 // only get parameter command is permitted for applications not controlling the effect
1721 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1722 return INVALID_OPERATION;
1723 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001724 if (mClient == 0) {
1725 return INVALID_OPERATION;
1726 }
1727
1728 // handle commands that are not forwarded transparently to effect engine
1729 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001730 if (*replySize < sizeof(int)) {
1731 android_errorWriteLog(0x534e4554, "32095713");
1732 return BAD_VALUE;
1733 }
1734 *(int *)pReplyData = NO_ERROR;
1735 *replySize = sizeof(int);
1736
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 // No need to trylock() here as this function is executed in the binder thread serving a
1738 // particular client process: no risk to block the whole media server process or mixer
1739 // threads if we are stuck here
1740 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001741 // keep local copy of index in case of client corruption b/32220769
1742 const uint32_t clientIndex = mCblk->clientIndex;
1743 const uint32_t serverIndex = mCblk->serverIndex;
1744 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1745 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001746 mCblk->serverIndex = 0;
1747 mCblk->clientIndex = 0;
1748 return BAD_VALUE;
1749 }
1750 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001751 effect_param_t *param = NULL;
1752 for (uint32_t index = serverIndex; index < clientIndex;) {
1753 int *p = (int *)(mBuffer + index);
1754 const int size = *p++;
1755 if (size < 0
1756 || size > EFFECT_PARAM_BUFFER_SIZE
1757 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001758 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001759 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001760 break;
1761 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001762
1763 // copy to local memory in case of client corruption b/32220769
1764 param = (effect_param_t *)realloc(param, size);
1765 if (param == NULL) {
1766 ALOGW("command(): out of memory");
1767 status = NO_MEMORY;
1768 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001769 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001770 memcpy(param, p, size);
1771
1772 int reply = 0;
1773 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001774 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001775 size,
1776 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001777 &rsize,
1778 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001779
1780 // verify shared memory: server index shouldn't change; client index can't go back.
1781 if (serverIndex != mCblk->serverIndex
1782 || clientIndex > mCblk->clientIndex) {
1783 android_errorWriteLog(0x534e4554, "32220769");
1784 status = BAD_VALUE;
1785 break;
1786 }
1787
Eric Laurentca7cc822012-11-19 14:55:58 -08001788 // stop at first error encountered
1789 if (ret != NO_ERROR) {
1790 status = ret;
1791 *(int *)pReplyData = reply;
1792 break;
1793 } else if (reply != NO_ERROR) {
1794 *(int *)pReplyData = reply;
1795 break;
1796 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001797 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001798 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001799 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001800 mCblk->serverIndex = 0;
1801 mCblk->clientIndex = 0;
1802 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001803 }
1804
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001805 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001806}
1807
1808void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1809{
1810 ALOGV("setControl %p control %d", this, hasControl);
1811
1812 mHasControl = hasControl;
1813 mEnabled = enabled;
1814
1815 if (signal && mEffectClient != 0) {
1816 mEffectClient->controlStatusChanged(hasControl);
1817 }
1818}
1819
1820void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1821 uint32_t cmdSize,
1822 void *pCmdData,
1823 uint32_t replySize,
1824 void *pReplyData)
1825{
1826 if (mEffectClient != 0) {
1827 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1828 }
1829}
1830
1831
1832
1833void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1834{
1835 if (mEffectClient != 0) {
1836 mEffectClient->enableStatusChanged(enabled);
1837 }
1838}
1839
1840status_t AudioFlinger::EffectHandle::onTransact(
1841 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1842{
1843 return BnEffect::onTransact(code, data, reply, flags);
1844}
1845
1846
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001847void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001848{
1849 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1850
Marco Nelissenb2208842014-02-07 14:00:50 -08001851 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001852 (mClient == 0) ? getpid_cached : mClient->pid(),
1853 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001854 mHasControl ? "yes" : "no",
1855 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001856 mCblk ? mCblk->clientIndex : 0,
1857 mCblk ? mCblk->serverIndex : 0
1858 );
1859
1860 if (locked) {
1861 mCblk->lock.unlock();
1862 }
1863}
1864
1865#undef LOG_TAG
1866#define LOG_TAG "AudioFlinger::EffectChain"
1867
1868AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001869 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001870 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001871 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001872 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001873{
1874 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1875 if (thread == NULL) {
1876 return;
1877 }
1878 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1879 thread->frameCount();
1880}
1881
1882AudioFlinger::EffectChain::~EffectChain()
1883{
Eric Laurentca7cc822012-11-19 14:55:58 -08001884}
1885
1886// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1887sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1888 effect_descriptor_t *descriptor)
1889{
1890 size_t size = mEffects.size();
1891
1892 for (size_t i = 0; i < size; i++) {
1893 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1894 return mEffects[i];
1895 }
1896 }
1897 return 0;
1898}
1899
1900// getEffectFromId_l() must be called with ThreadBase::mLock held
1901sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1902{
1903 size_t size = mEffects.size();
1904
1905 for (size_t i = 0; i < size; i++) {
1906 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1907 if (id == 0 || mEffects[i]->id() == id) {
1908 return mEffects[i];
1909 }
1910 }
1911 return 0;
1912}
1913
1914// getEffectFromType_l() must be called with ThreadBase::mLock held
1915sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1916 const effect_uuid_t *type)
1917{
1918 size_t size = mEffects.size();
1919
1920 for (size_t i = 0; i < size; i++) {
1921 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1922 return mEffects[i];
1923 }
1924 }
1925 return 0;
1926}
1927
1928void AudioFlinger::EffectChain::clearInputBuffer()
1929{
1930 Mutex::Autolock _l(mLock);
1931 sp<ThreadBase> thread = mThread.promote();
1932 if (thread == 0) {
1933 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1934 return;
1935 }
1936 clearInputBuffer_l(thread);
1937}
1938
1939// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001940void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001941{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001942 if (mInBuffer == NULL) {
1943 return;
1944 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001945 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08001946 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07001947
Mikhail Naganov022b9952017-01-04 16:36:51 -08001948 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1949 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001950}
1951
1952// Must be called with EffectChain::mLock locked
1953void AudioFlinger::EffectChain::process_l()
1954{
1955 sp<ThreadBase> thread = mThread.promote();
1956 if (thread == 0) {
1957 ALOGW("process_l(): cannot promote mixer thread");
1958 return;
1959 }
1960 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1961 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001962 // never process effects when:
1963 // - on an OFFLOAD thread
1964 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001965 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1966 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001967 if (!isGlobalSession) {
1968 bool tracksOnSession = (trackCnt() != 0);
1969
1970 if (!tracksOnSession && mTailBufferCount == 0) {
1971 doProcess = false;
1972 }
1973
1974 if (activeTrackCnt() == 0) {
1975 // if no track is active and the effect tail has not been rendered,
1976 // the input buffer must be cleared here as the mixer process will not do it
1977 if (tracksOnSession || mTailBufferCount > 0) {
1978 clearInputBuffer_l(thread);
1979 if (mTailBufferCount > 0) {
1980 mTailBufferCount--;
1981 }
1982 }
1983 }
1984 }
1985
1986 size_t size = mEffects.size();
1987 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001988 // Only the input and output buffers of the chain can be external,
1989 // and 'update' / 'commit' do nothing for allocated buffers, thus
1990 // it's not needed to consider any other buffers here.
1991 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001992 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1993 mOutBuffer->update();
1994 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001995 for (size_t i = 0; i < size; i++) {
1996 mEffects[i]->process();
1997 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001998 mInBuffer->commit();
1999 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2000 mOutBuffer->commit();
2001 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002002 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002003 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002004 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002005 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2006 }
2007 if (doResetVolume) {
2008 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002009 }
2010}
2011
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002012// createEffect_l() must be called with ThreadBase::mLock held
2013status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
2014 ThreadBase *thread,
2015 effect_descriptor_t *desc,
2016 int id,
2017 audio_session_t sessionId,
2018 bool pinned)
2019{
2020 Mutex::Autolock _l(mLock);
2021 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
2022 status_t lStatus = effect->status();
2023 if (lStatus == NO_ERROR) {
2024 lStatus = addEffect_ll(effect);
2025 }
2026 if (lStatus != NO_ERROR) {
2027 effect.clear();
2028 }
2029 return lStatus;
2030}
2031
2032// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002033status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2034{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002035 Mutex::Autolock _l(mLock);
2036 return addEffect_ll(effect);
2037}
2038// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2039status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2040{
Eric Laurentca7cc822012-11-19 14:55:58 -08002041 effect_descriptor_t desc = effect->desc();
2042 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2043
Eric Laurentca7cc822012-11-19 14:55:58 -08002044 effect->setChain(this);
2045 sp<ThreadBase> thread = mThread.promote();
2046 if (thread == 0) {
2047 return NO_INIT;
2048 }
2049 effect->setThread(thread);
2050
2051 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2052 // Auxiliary effects are inserted at the beginning of mEffects vector as
2053 // they are processed first and accumulated in chain input buffer
2054 mEffects.insertAt(effect, 0);
2055
2056 // the input buffer for auxiliary effect contains mono samples in
2057 // 32 bit format. This is to avoid saturation in AudoMixer
2058 // accumulation stage. Saturation is done in EffectModule::process() before
2059 // calling the process in effect engine
2060 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002061 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002062#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002063 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002064 numSamples * sizeof(float), &halBuffer);
2065#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002066 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002067 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002068#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002069 if (result != OK) return result;
2070 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002071 // auxiliary effects output samples to chain input buffer for further processing
2072 // by insert effects
2073 effect->setOutBuffer(mInBuffer);
2074 } else {
2075 // Insert effects are inserted at the end of mEffects vector as they are processed
2076 // after track and auxiliary effects.
2077 // Insert effect order as a function of indicated preference:
2078 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2079 // another effect is present
2080 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2081 // last effect claiming first position
2082 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2083 // first effect claiming last position
2084 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2085 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2086 // already present
2087
2088 size_t size = mEffects.size();
2089 size_t idx_insert = size;
2090 ssize_t idx_insert_first = -1;
2091 ssize_t idx_insert_last = -1;
2092
2093 for (size_t i = 0; i < size; i++) {
2094 effect_descriptor_t d = mEffects[i]->desc();
2095 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2096 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2097 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2098 // check invalid effect chaining combinations
2099 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2100 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2101 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2102 desc.name, d.name);
2103 return INVALID_OPERATION;
2104 }
2105 // remember position of first insert effect and by default
2106 // select this as insert position for new effect
2107 if (idx_insert == size) {
2108 idx_insert = i;
2109 }
2110 // remember position of last insert effect claiming
2111 // first position
2112 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2113 idx_insert_first = i;
2114 }
2115 // remember position of first insert effect claiming
2116 // last position
2117 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2118 idx_insert_last == -1) {
2119 idx_insert_last = i;
2120 }
2121 }
2122 }
2123
2124 // modify idx_insert from first position if needed
2125 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2126 if (idx_insert_last != -1) {
2127 idx_insert = idx_insert_last;
2128 } else {
2129 idx_insert = size;
2130 }
2131 } else {
2132 if (idx_insert_first != -1) {
2133 idx_insert = idx_insert_first + 1;
2134 }
2135 }
2136
2137 // always read samples from chain input buffer
2138 effect->setInBuffer(mInBuffer);
2139
2140 // if last effect in the chain, output samples to chain
2141 // output buffer, otherwise to chain input buffer
2142 if (idx_insert == size) {
2143 if (idx_insert != 0) {
2144 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2145 mEffects[idx_insert-1]->configure();
2146 }
2147 effect->setOutBuffer(mOutBuffer);
2148 } else {
2149 effect->setOutBuffer(mInBuffer);
2150 }
2151 mEffects.insertAt(effect, idx_insert);
2152
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002153 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002154 idx_insert);
2155 }
2156 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002157
Eric Laurentca7cc822012-11-19 14:55:58 -08002158 return NO_ERROR;
2159}
2160
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002161// removeEffect_l() must be called with ThreadBase::mLock held
2162size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2163 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002164{
2165 Mutex::Autolock _l(mLock);
2166 size_t size = mEffects.size();
2167 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2168
2169 for (size_t i = 0; i < size; i++) {
2170 if (effect == mEffects[i]) {
2171 // calling stop here will remove pre-processing effect from the audio HAL.
2172 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2173 // the middle of a read from audio HAL
2174 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2175 mEffects[i]->state() == EffectModule::STOPPING) {
2176 mEffects[i]->stop();
2177 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002178 if (release) {
2179 mEffects[i]->release_l();
2180 }
2181
Mikhail Naganov022b9952017-01-04 16:36:51 -08002182 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002183 if (i == size - 1 && i != 0) {
2184 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2185 mEffects[i - 1]->configure();
2186 }
2187 }
2188 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002189 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002190 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002191
Eric Laurentca7cc822012-11-19 14:55:58 -08002192 break;
2193 }
2194 }
2195
2196 return mEffects.size();
2197}
2198
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002199// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002200void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2201{
2202 size_t size = mEffects.size();
2203 for (size_t i = 0; i < size; i++) {
2204 mEffects[i]->setDevice(device);
2205 }
2206}
2207
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002208// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002209void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2210{
2211 size_t size = mEffects.size();
2212 for (size_t i = 0; i < size; i++) {
2213 mEffects[i]->setMode(mode);
2214 }
2215}
2216
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002217// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002218void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2219{
2220 size_t size = mEffects.size();
2221 for (size_t i = 0; i < size; i++) {
2222 mEffects[i]->setAudioSource(source);
2223 }
2224}
2225
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002226// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002227bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002228{
2229 uint32_t newLeft = *left;
2230 uint32_t newRight = *right;
2231 bool hasControl = false;
2232 int ctrlIdx = -1;
2233 size_t size = mEffects.size();
2234
2235 // first update volume controller
2236 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002237 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002238 ctrlIdx = i - 1;
2239 hasControl = true;
2240 break;
2241 }
2242 }
2243
Eric Laurentfa1e1232016-08-02 19:01:49 -07002244 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002245 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002246 if (hasControl) {
2247 *left = mNewLeftVolume;
2248 *right = mNewRightVolume;
2249 }
2250 return hasControl;
2251 }
2252
2253 mVolumeCtrlIdx = ctrlIdx;
2254 mLeftVolume = newLeft;
2255 mRightVolume = newRight;
2256
2257 // second get volume update from volume controller
2258 if (ctrlIdx >= 0) {
2259 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2260 mNewLeftVolume = newLeft;
2261 mNewRightVolume = newRight;
2262 }
2263 // then indicate volume to all other effects in chain.
2264 // Pass altered volume to effects before volume controller
2265 // and requested volume to effects after controller
2266 uint32_t lVol = newLeft;
2267 uint32_t rVol = newRight;
2268
2269 for (size_t i = 0; i < size; i++) {
2270 if ((int)i == ctrlIdx) {
2271 continue;
2272 }
2273 // this also works for ctrlIdx == -1 when there is no volume controller
2274 if ((int)i > ctrlIdx) {
2275 lVol = *left;
2276 rVol = *right;
2277 }
2278 mEffects[i]->setVolume(&lVol, &rVol, false);
2279 }
2280 *left = newLeft;
2281 *right = newRight;
2282
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002283 setVolumeForOutput_l(*left, *right);
2284
Eric Laurentca7cc822012-11-19 14:55:58 -08002285 return hasControl;
2286}
2287
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002288// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002289void AudioFlinger::EffectChain::resetVolume_l()
2290{
Eric Laurente7449bf2016-08-03 18:44:07 -07002291 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2292 uint32_t left = mLeftVolume;
2293 uint32_t right = mRightVolume;
2294 (void)setVolume_l(&left, &right, true);
2295 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002296}
2297
Eric Laurent1b928682014-10-02 19:41:47 -07002298void AudioFlinger::EffectChain::syncHalEffectsState()
2299{
2300 Mutex::Autolock _l(mLock);
2301 for (size_t i = 0; i < mEffects.size(); i++) {
2302 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2303 mEffects[i]->state() == EffectModule::STOPPING) {
2304 mEffects[i]->addEffectToHal_l();
2305 }
2306 }
2307}
2308
Eric Laurentca7cc822012-11-19 14:55:58 -08002309void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2310{
2311 const size_t SIZE = 256;
2312 char buffer[SIZE];
2313 String8 result;
2314
Marco Nelissenb2208842014-02-07 14:00:50 -08002315 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002316 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002317 result.append(buffer);
2318
Marco Nelissenb2208842014-02-07 14:00:50 -08002319 if (numEffects) {
2320 bool locked = AudioFlinger::dumpTryLock(mLock);
2321 // failed to lock - AudioFlinger is probably deadlocked
2322 if (!locked) {
2323 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002324 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002325
Andy Hungbded9c82017-11-30 18:47:35 -08002326 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2327 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2328 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2329 (int)inBufferStr.size(), "In buffer ",
2330 (int)outBufferStr.size(), "Out buffer ");
2331 result.appendFormat("\t%s %s %d\n",
2332 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002333 write(fd, result.string(), result.size());
2334
2335 for (size_t i = 0; i < numEffects; ++i) {
2336 sp<EffectModule> effect = mEffects[i];
2337 if (effect != 0) {
2338 effect->dump(fd, args);
2339 }
2340 }
2341
2342 if (locked) {
2343 mLock.unlock();
2344 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002345 }
2346}
2347
2348// must be called with ThreadBase::mLock held
2349void AudioFlinger::EffectChain::setEffectSuspended_l(
2350 const effect_uuid_t *type, bool suspend)
2351{
2352 sp<SuspendedEffectDesc> desc;
2353 // use effect type UUID timelow as key as there is no real risk of identical
2354 // timeLow fields among effect type UUIDs.
2355 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2356 if (suspend) {
2357 if (index >= 0) {
2358 desc = mSuspendedEffects.valueAt(index);
2359 } else {
2360 desc = new SuspendedEffectDesc();
2361 desc->mType = *type;
2362 mSuspendedEffects.add(type->timeLow, desc);
2363 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2364 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002365
Eric Laurentca7cc822012-11-19 14:55:58 -08002366 if (desc->mRefCount++ == 0) {
2367 sp<EffectModule> effect = getEffectIfEnabled(type);
2368 if (effect != 0) {
2369 desc->mEffect = effect;
2370 effect->setSuspended(true);
2371 effect->setEnabled(false);
2372 }
2373 }
2374 } else {
2375 if (index < 0) {
2376 return;
2377 }
2378 desc = mSuspendedEffects.valueAt(index);
2379 if (desc->mRefCount <= 0) {
2380 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002381 desc->mRefCount = 0;
2382 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002383 }
2384 if (--desc->mRefCount == 0) {
2385 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2386 if (desc->mEffect != 0) {
2387 sp<EffectModule> effect = desc->mEffect.promote();
2388 if (effect != 0) {
2389 effect->setSuspended(false);
2390 effect->lock();
2391 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002392 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002393 effect->setEnabled_l(handle->enabled());
2394 }
2395 effect->unlock();
2396 }
2397 desc->mEffect.clear();
2398 }
2399 mSuspendedEffects.removeItemsAt(index);
2400 }
2401 }
2402}
2403
2404// must be called with ThreadBase::mLock held
2405void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2406{
2407 sp<SuspendedEffectDesc> desc;
2408
2409 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2410 if (suspend) {
2411 if (index >= 0) {
2412 desc = mSuspendedEffects.valueAt(index);
2413 } else {
2414 desc = new SuspendedEffectDesc();
2415 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2416 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2417 }
2418 if (desc->mRefCount++ == 0) {
2419 Vector< sp<EffectModule> > effects;
2420 getSuspendEligibleEffects(effects);
2421 for (size_t i = 0; i < effects.size(); i++) {
2422 setEffectSuspended_l(&effects[i]->desc().type, true);
2423 }
2424 }
2425 } else {
2426 if (index < 0) {
2427 return;
2428 }
2429 desc = mSuspendedEffects.valueAt(index);
2430 if (desc->mRefCount <= 0) {
2431 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2432 desc->mRefCount = 1;
2433 }
2434 if (--desc->mRefCount == 0) {
2435 Vector<const effect_uuid_t *> types;
2436 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2437 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2438 continue;
2439 }
2440 types.add(&mSuspendedEffects.valueAt(i)->mType);
2441 }
2442 for (size_t i = 0; i < types.size(); i++) {
2443 setEffectSuspended_l(types[i], false);
2444 }
2445 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2446 mSuspendedEffects.keyAt(index));
2447 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2448 }
2449 }
2450}
2451
2452
2453// The volume effect is used for automated tests only
2454#ifndef OPENSL_ES_H_
2455static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2456 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2457const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2458#endif //OPENSL_ES_H_
2459
Eric Laurentd8365c52017-07-16 15:27:05 -07002460/* static */
2461bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2462{
2463 // Only NS and AEC are suspended when BtNRec is off
2464 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2465 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2466 return true;
2467 }
2468 return false;
2469}
2470
Eric Laurentca7cc822012-11-19 14:55:58 -08002471bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2472{
2473 // auxiliary effects and visualizer are never suspended on output mix
2474 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2475 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2476 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2477 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2478 return false;
2479 }
2480 return true;
2481}
2482
2483void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2484 Vector< sp<AudioFlinger::EffectModule> > &effects)
2485{
2486 effects.clear();
2487 for (size_t i = 0; i < mEffects.size(); i++) {
2488 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2489 effects.add(mEffects[i]);
2490 }
2491 }
2492}
2493
2494sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2495 const effect_uuid_t *type)
2496{
2497 sp<EffectModule> effect = getEffectFromType_l(type);
2498 return effect != 0 && effect->isEnabled() ? effect : 0;
2499}
2500
2501void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2502 bool enabled)
2503{
2504 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2505 if (enabled) {
2506 if (index < 0) {
2507 // if the effect is not suspend check if all effects are suspended
2508 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2509 if (index < 0) {
2510 return;
2511 }
2512 if (!isEffectEligibleForSuspend(effect->desc())) {
2513 return;
2514 }
2515 setEffectSuspended_l(&effect->desc().type, enabled);
2516 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2517 if (index < 0) {
2518 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2519 return;
2520 }
2521 }
2522 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2523 effect->desc().type.timeLow);
2524 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002525 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002526 if (desc->mEffect == 0) {
2527 desc->mEffect = effect;
2528 effect->setEnabled(false);
2529 effect->setSuspended(true);
2530 }
2531 } else {
2532 if (index < 0) {
2533 return;
2534 }
2535 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2536 effect->desc().type.timeLow);
2537 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2538 desc->mEffect.clear();
2539 effect->setSuspended(false);
2540 }
2541}
2542
Eric Laurent5baf2af2013-09-12 17:37:00 -07002543bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002544{
2545 Mutex::Autolock _l(mLock);
2546 size_t size = mEffects.size();
2547 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002548 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002549 return true;
2550 }
2551 }
2552 return false;
2553}
2554
Eric Laurentaaa44472014-09-12 17:41:50 -07002555void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2556{
2557 Mutex::Autolock _l(mLock);
2558 mThread = thread;
2559 for (size_t i = 0; i < mEffects.size(); i++) {
2560 mEffects[i]->setThread(thread);
2561 }
2562}
2563
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002564void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2565{
2566 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2567 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2568 }
2569 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2570 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2571 }
2572}
2573
2574void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2575{
2576 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2577 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2578 }
2579 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2580 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2581 }
2582}
2583
2584bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002585{
2586 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002587 for (const auto &effect : mEffects) {
2588 if (effect->isProcessImplemented()) {
2589 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002590 }
2591 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002592 // Allow effects without processing.
2593 return true;
2594}
2595
2596bool AudioFlinger::EffectChain::isFastCompatible() const
2597{
2598 Mutex::Autolock _l(mLock);
2599 for (const auto &effect : mEffects) {
2600 if (effect->isProcessImplemented()
2601 && effect->isImplementationSoftware()) {
2602 return false;
2603 }
2604 }
2605 // Allow effects without processing or hw accelerated effects.
2606 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002607}
2608
2609// isCompatibleWithThread_l() must be called with thread->mLock held
2610bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2611{
2612 Mutex::Autolock _l(mLock);
2613 for (size_t i = 0; i < mEffects.size(); i++) {
2614 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2615 return false;
2616 }
2617 }
2618 return true;
2619}
2620
Glenn Kasten63238ef2015-03-02 15:50:29 -08002621} // namespace android