blob: dcf223ce3df869969608159a2b851394f8e619bf [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
27#include <system/audio_effects/effect_ns.h>
28#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080029#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080030#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070031#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070032#include <media/audiohal/EffectHalInterface.h>
33#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080034
35#include "AudioFlinger.h"
36#include "ServiceUtilities.h"
37
38// ----------------------------------------------------------------------------
39
40// Note: the following macro is used for extremely verbose logging message. In
41// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
42// 0; but one side effect of this is to turn all LOGV's as well. Some messages
43// are so verbose that we want to suppress them even when we have ALOG_ASSERT
44// turned on. Do not uncomment the #def below unless you really know what you
45// are doing and want to see all of the extremely verbose messages.
46//#define VERY_VERY_VERBOSE_LOGGING
47#ifdef VERY_VERY_VERBOSE_LOGGING
48#define ALOGVV ALOGV
49#else
50#define ALOGVV(a...) do { } while(0)
51#endif
52
53namespace android {
54
55// ----------------------------------------------------------------------------
56// EffectModule implementation
57// ----------------------------------------------------------------------------
58
59#undef LOG_TAG
60#define LOG_TAG "AudioFlinger::EffectModule"
61
62AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
63 const wp<AudioFlinger::EffectChain>& chain,
64 effect_descriptor_t *desc,
65 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080066 audio_session_t sessionId,
67 bool pinned)
68 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080069 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
70 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080071 // clear mConfig to ensure consistent initial value of buffer framecount
72 // in case buffers are associated by setInBuffer() or setOutBuffer()
73 // prior to configure().
74 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080075 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080076 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
77 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070078 mSuspended(false),
Andy Hung62aef7d2017-12-14 14:50:40 -080079 mOffloaded(false),
Eric Laurentaaa44472014-09-12 17:41:50 -070080 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070081#ifdef FLOAT_EFFECT_CHAIN
82 , mSupportsFloat(false)
83#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080084{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080085 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080086 int lStatus;
87
88 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070089 mStatus = -ENODEV;
90 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070091 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070092 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070093 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070094 mStatus = effectsFactory->createEffect(
95 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
96 }
97 }
Eric Laurentca7cc822012-11-19 14:55:58 -080098
99 if (mStatus != NO_ERROR) {
100 return;
101 }
102 lStatus = init();
103 if (lStatus < 0) {
104 mStatus = lStatus;
105 goto Error;
106 }
107
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800108 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700109 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110
Eric Laurentca7cc822012-11-19 14:55:58 -0800111 return;
112Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700113 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800114 ALOGV("Constructor Error %d", mStatus);
115}
116
117AudioFlinger::EffectModule::~EffectModule()
118{
119 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700120 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700121 char uuidStr[64];
122 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
123 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
124 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800125 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800126 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800127
Eric Laurentca7cc822012-11-19 14:55:58 -0800128}
129
130status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
131{
132 status_t status;
133
134 Mutex::Autolock _l(mLock);
135 int priority = handle->priority();
136 size_t size = mHandles.size();
137 EffectHandle *controlHandle = NULL;
138 size_t i;
139 for (i = 0; i < size; i++) {
140 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800141 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800142 continue;
143 }
144 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700145 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800146 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700147 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800148 if (h->priority() <= priority) {
149 break;
150 }
151 }
152 // if inserted in first place, move effect control from previous owner to this handle
153 if (i == 0) {
154 bool enabled = false;
155 if (controlHandle != NULL) {
156 enabled = controlHandle->enabled();
157 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
158 }
159 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
160 status = NO_ERROR;
161 } else {
162 status = ALREADY_EXISTS;
163 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700164 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800165 mHandles.insertAt(handle, i);
166 return status;
167}
168
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800169ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800170{
171 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800172 return removeHandle_l(handle);
173}
174
175ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
176{
Eric Laurentca7cc822012-11-19 14:55:58 -0800177 size_t size = mHandles.size();
178 size_t i;
179 for (i = 0; i < size; i++) {
180 if (mHandles[i] == handle) {
181 break;
182 }
183 }
184 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800185 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
186 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800187 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800188 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800189
190 mHandles.removeAt(i);
191 // if removed from first place, move effect control from this handle to next in line
192 if (i == 0) {
193 EffectHandle *h = controlHandle_l();
194 if (h != NULL) {
195 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
196 }
197 }
198
199 // Prevent calls to process() and other functions on effect interface from now on.
200 // The effect engine will be released by the destructor when the last strong reference on
201 // this object is released which can happen after next process is called.
202 if (mHandles.size() == 0 && !mPinned) {
203 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800204 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800205 }
206
207 return mHandles.size();
208}
209
210// must be called with EffectModule::mLock held
211AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
212{
213 // the first valid handle in the list has control over the module
214 for (size_t i = 0; i < mHandles.size(); i++) {
215 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800216 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800217 return h;
218 }
219 }
220
221 return NULL;
222}
223
Eric Laurentf10c7092016-12-06 17:09:56 -0800224// unsafe method called when the effect parent thread has been destroyed
225ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
226{
227 ALOGV("disconnect() %p handle %p", this, handle);
228 Mutex::Autolock _l(mLock);
229 ssize_t numHandles = removeHandle_l(handle);
230 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
231 AudioSystem::unregisterEffect(mId);
232 sp<AudioFlinger> af = mAudioFlinger.promote();
233 if (af != 0) {
234 mLock.unlock();
235 af->updateOrphanEffectChains(this);
236 mLock.lock();
237 }
238 }
239 return numHandles;
240}
241
Eric Laurentfa1e1232016-08-02 19:01:49 -0700242bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800243 Mutex::Autolock _l(mLock);
244
Eric Laurentfa1e1232016-08-02 19:01:49 -0700245 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800246 switch (mState) {
247 case RESTART:
248 reset_l();
249 // FALL THROUGH
250
251 case STARTING:
252 // clear auxiliary effect input buffer for next accumulation
253 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
254 memset(mConfig.inputCfg.buffer.raw,
255 0,
256 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
257 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700258 if (start_l() == NO_ERROR) {
259 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700260 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700261 } else {
262 mState = IDLE;
263 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800264 break;
265 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700266 if (stop_l() == NO_ERROR) {
267 mDisableWaitCnt = mMaxDisableWaitCnt;
268 } else {
269 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
270 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800271 mState = STOPPED;
272 break;
273 case STOPPED:
274 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
275 // turn off sequence.
276 if (--mDisableWaitCnt == 0) {
277 reset_l();
278 mState = IDLE;
279 }
280 break;
281 default: //IDLE , ACTIVE, DESTROYED
282 break;
283 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700284
285 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800286}
287
288void AudioFlinger::EffectModule::process()
289{
290 Mutex::Autolock _l(mLock);
291
Mikhail Naganov022b9952017-01-04 16:36:51 -0800292 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800293 return;
294 }
295
rago94a1ee82017-07-21 15:11:02 -0700296 const uint32_t inChannelCount =
297 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
298 const uint32_t outChannelCount =
299 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
300 const bool auxType =
301 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
302
Andy Hungfa69ca32017-11-30 10:07:53 -0800303 // safeInputOutputSampleCount is 0 if the channel count between input and output
304 // buffers do not match. This prevents automatic accumulation or copying between the
305 // input and output effect buffers without an intermediary effect process.
306 // TODO: consider implementing channel conversion.
307 const size_t safeInputOutputSampleCount =
308 inChannelCount != outChannelCount ? 0
309 : outChannelCount * std::min(
310 mConfig.inputCfg.buffer.frameCount,
311 mConfig.outputCfg.buffer.frameCount);
312 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
313#ifdef FLOAT_EFFECT_CHAIN
314 accumulate_float(
315 mConfig.outputCfg.buffer.f32,
316 mConfig.inputCfg.buffer.f32,
317 safeInputOutputSampleCount);
318#else
319 accumulate_i16(
320 mConfig.outputCfg.buffer.s16,
321 mConfig.inputCfg.buffer.s16,
322 safeInputOutputSampleCount);
323#endif
324 };
325 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
326#ifdef FLOAT_EFFECT_CHAIN
327 memcpy(
328 mConfig.outputCfg.buffer.f32,
329 mConfig.inputCfg.buffer.f32,
330 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
331
332#else
333 memcpy(
334 mConfig.outputCfg.buffer.s16,
335 mConfig.inputCfg.buffer.s16,
336 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
337#endif
338 };
339
Eric Laurentca7cc822012-11-19 14:55:58 -0800340 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700341 int ret;
342 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700343 if (auxType) {
344 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800345 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700346#ifdef FLOAT_EFFECT_CHAIN
347 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800348#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700349 // Do in-place float conversion for auxiliary effect input buffer.
350 static_assert(sizeof(float) <= sizeof(int32_t),
351 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
352
Andy Hungfa69ca32017-11-30 10:07:53 -0800353 memcpy_to_float_from_q4_27(
354 mConfig.inputCfg.buffer.f32,
355 mConfig.inputCfg.buffer.s32,
356 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800357#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800358 } else
Andy Hung116a4982017-11-30 10:15:08 -0800359#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800360 {
Andy Hung116a4982017-11-30 10:15:08 -0800361#ifdef FLOAT_AUX
362 memcpy_to_i16_from_float(
363 mConfig.inputCfg.buffer.s16,
364 mConfig.inputCfg.buffer.f32,
365 mConfig.inputCfg.buffer.frameCount);
366#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800367 memcpy_to_i16_from_q4_27(
368 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700369 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800370 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800371#endif
rago94a1ee82017-07-21 15:11:02 -0700372 }
rago94a1ee82017-07-21 15:11:02 -0700373 }
374#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800375 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
376 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
377
378 if (!auxType && mInChannelCountRequested != inChannelCount) {
379 adjust_channels(
380 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
381 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
382 sizeof(float),
383 sizeof(float)
384 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
385 inBuffer = mInConversionBuffer;
386 }
387 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
388 && mOutChannelCountRequested != outChannelCount) {
389 adjust_selected_channels(
390 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
391 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
392 sizeof(float),
393 sizeof(float)
394 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
395 outBuffer = mOutConversionBuffer;
396 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800397 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
398 if (!auxType) {
399 if (mInConversionBuffer.get() == nullptr) {
400 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
401 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700402 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800403 memcpy_to_i16_from_float(
404 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800405 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800406 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800407 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700408 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800409 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
410 if (mOutConversionBuffer.get() == nullptr) {
411 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
412 goto data_bypass;
413 }
414 memcpy_to_i16_from_float(
415 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800416 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800417 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800418 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700419 }
420 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800421#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800422 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800423#ifdef FLOAT_EFFECT_CHAIN
424 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800425 sp<EffectBufferHalInterface> target =
426 mOutChannelCountRequested != outChannelCount
427 ? mOutConversionBuffer : mOutBuffer;
428
Andy Hungfa69ca32017-11-30 10:07:53 -0800429 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800430 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800431 mOutConversionBuffer->audioBuffer()->s16,
432 outChannelCount * mConfig.outputCfg.buffer.frameCount);
433 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800434 if (mOutChannelCountRequested != outChannelCount) {
435 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
436 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
437 sizeof(float),
438 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
439 }
rago94a1ee82017-07-21 15:11:02 -0700440#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700441 } else {
rago94a1ee82017-07-21 15:11:02 -0700442#ifdef FLOAT_EFFECT_CHAIN
443 data_bypass:
444#endif
445 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800446 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700447 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800448 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700449 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800450 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700451 }
452 }
453 ret = -ENODATA;
454 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800455
Eric Laurentca7cc822012-11-19 14:55:58 -0800456 // force transition to IDLE state when engine is ready
457 if (mState == STOPPED && ret == -ENODATA) {
458 mDisableWaitCnt = 1;
459 }
460
461 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700462 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800463#ifdef FLOAT_AUX
464 const size_t size =
465 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
466#else
rago94a1ee82017-07-21 15:11:02 -0700467 const size_t size =
468 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800469#endif
rago94a1ee82017-07-21 15:11:02 -0700470 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800471 }
472 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700473 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800474 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
475 // If an insert effect is idle and input buffer is different from output buffer,
476 // accumulate input onto output
477 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800478 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
479 accumulateInputToOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800480 }
481 }
482}
483
484void AudioFlinger::EffectModule::reset_l()
485{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700486 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800487 return;
488 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700489 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800490}
491
492status_t AudioFlinger::EffectModule::configure()
493{
rago94a1ee82017-07-21 15:11:02 -0700494 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700495 status_t status;
496 sp<ThreadBase> thread;
497 uint32_t size;
498 audio_channel_mask_t channelMask;
499
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700500 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700501 status = NO_INIT;
502 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800503 }
504
Eric Laurentd0ebb532013-04-02 16:41:41 -0700505 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800506 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700507 status = DEAD_OBJECT;
508 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800509 }
510
511 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800512 // TODO: handle configuration of input (record) SW effects above the HAL,
513 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
514 // in which case input channel masks should be used here.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700515 channelMask = thread->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800516 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700517 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800518
519 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800520 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
521 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
522 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
523 mConfig.inputCfg.channels);
524 }
525#ifndef MULTICHANNEL_EFFECT_CHAIN
526 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
527 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
528 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
529 mConfig.outputCfg.channels);
530 }
531#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800532 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800533#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700534 // TODO: Update this logic when multichannel effects are implemented.
535 // For offloaded tracks consider mono output as stereo for proper effect initialization
536 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
537 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
538 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
539 ALOGV("Overriding effect input and output as STEREO");
540 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800541#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800542 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800543 mInChannelCountRequested =
544 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
545 mOutChannelCountRequested =
546 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700547
rago94a1ee82017-07-21 15:11:02 -0700548 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
549 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800550 mConfig.inputCfg.samplingRate = thread->sampleRate();
551 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
552 mConfig.inputCfg.bufferProvider.cookie = NULL;
553 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
554 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
555 mConfig.outputCfg.bufferProvider.cookie = NULL;
556 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
557 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
558 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
559 // Insert effect:
560 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
561 // always overwrites output buffer: input buffer == output buffer
562 // - in other sessions:
563 // last effect in the chain accumulates in output buffer: input buffer != output buffer
564 // other effect: overwrites output buffer: input buffer == output buffer
565 // Auxiliary effect:
566 // accumulates in output buffer: input buffer != output buffer
567 // Therefore: accumulate <=> input buffer != output buffer
568 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
569 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
570 } else {
571 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
572 }
573 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
574 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
575 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
576 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
577
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700578 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
580
581 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700582 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700583 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800584 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700585 &mConfig,
586 &size,
587 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700588 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800589 status = cmdStatus;
590 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800591
592#ifdef MULTICHANNEL_EFFECT_CHAIN
593 if (status != NO_ERROR &&
Andy Hunge6a61a52018-04-06 18:55:26 -0700594 thread->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800595 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
596 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
597 // Older effects may require exact STEREO position mask.
598 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
599 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
600 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
601 }
602 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
603 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
604 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
605 }
606 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700607 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800608 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700609 &mConfig,
610 &size,
611 &cmdStatus);
612 if (status == NO_ERROR) {
613 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800614 }
615 }
616#endif
617
618#ifdef FLOAT_EFFECT_CHAIN
619 if (status == NO_ERROR) {
620 mSupportsFloat = true;
621 }
622
623 if (status != NO_ERROR) {
624 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
625 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
626 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
627 size = sizeof(int);
628 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
629 sizeof(mConfig),
630 &mConfig,
631 &size,
632 &cmdStatus);
633 if (status == NO_ERROR) {
634 status = cmdStatus;
635 }
636 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700637 mSupportsFloat = false;
638 ALOGVV("config worked with 16 bit");
639 } else {
640 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800641 }
rago94a1ee82017-07-21 15:11:02 -0700642 }
643#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800644
rago94a1ee82017-07-21 15:11:02 -0700645 if (status == NO_ERROR) {
646 // Establish Buffer strategy
647 setInBuffer(mInBuffer);
648 setOutBuffer(mOutBuffer);
649
650 // Update visualizer latency
651 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
652 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
653 effect_param_t *p = (effect_param_t *)buf32;
654
655 p->psize = sizeof(uint32_t);
656 p->vsize = sizeof(uint32_t);
657 size = sizeof(int);
658 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
659
660 uint32_t latency = 0;
661 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
662 if (pbt != NULL) {
663 latency = pbt->latency_l();
664 }
665
666 *((int32_t *)p->data + 1)= latency;
667 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
668 sizeof(effect_param_t) + 8,
669 &buf32,
670 &size,
671 &cmdStatus);
672 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800673 }
674
Andy Hung05083ac2017-12-14 15:00:28 -0800675 // mConfig.outputCfg.buffer.frameCount cannot be zero.
676 mMaxDisableWaitCnt = (uint32_t)std::max(
677 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
678 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
679 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800680
Eric Laurentd0ebb532013-04-02 16:41:41 -0700681exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800682 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700683 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700684 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800685 return status;
686}
687
688status_t AudioFlinger::EffectModule::init()
689{
690 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700691 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800692 return NO_INIT;
693 }
694 status_t cmdStatus;
695 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700696 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
697 0,
698 NULL,
699 &size,
700 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800701 if (status == 0) {
702 status = cmdStatus;
703 }
704 return status;
705}
706
Eric Laurent1b928682014-10-02 19:41:47 -0700707void AudioFlinger::EffectModule::addEffectToHal_l()
708{
709 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
710 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
711 sp<ThreadBase> thread = mThread.promote();
712 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700713 sp<StreamHalInterface> stream = thread->stream();
714 if (stream != 0) {
715 status_t result = stream->addEffect(mEffectInterface);
716 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700717 }
718 }
719 }
720}
721
Eric Laurentfa1e1232016-08-02 19:01:49 -0700722// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800723status_t AudioFlinger::EffectModule::start()
724{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700725 sp<EffectChain> chain;
726 status_t status;
727 {
728 Mutex::Autolock _l(mLock);
729 status = start_l();
730 if (status == NO_ERROR) {
731 chain = mChain.promote();
732 }
733 }
734 if (chain != 0) {
735 chain->resetVolume_l();
736 }
737 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800738}
739
740status_t AudioFlinger::EffectModule::start_l()
741{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700742 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800743 return NO_INIT;
744 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700745 if (mStatus != NO_ERROR) {
746 return mStatus;
747 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800748 status_t cmdStatus;
749 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700750 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
751 0,
752 NULL,
753 &size,
754 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800755 if (status == 0) {
756 status = cmdStatus;
757 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700758 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700759 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800760 }
761 return status;
762}
763
764status_t AudioFlinger::EffectModule::stop()
765{
766 Mutex::Autolock _l(mLock);
767 return stop_l();
768}
769
770status_t AudioFlinger::EffectModule::stop_l()
771{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700772 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800773 return NO_INIT;
774 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700775 if (mStatus != NO_ERROR) {
776 return mStatus;
777 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800778 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800779 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700780 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
781 0,
782 NULL,
783 &size,
784 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800785 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800786 status = cmdStatus;
787 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800788 if (status == NO_ERROR) {
789 status = remove_effect_from_hal_l();
790 }
791 return status;
792}
793
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800794// must be called with EffectChain::mLock held
795void AudioFlinger::EffectModule::release_l()
796{
797 if (mEffectInterface != 0) {
798 remove_effect_from_hal_l();
799 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800800 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800801 mEffectInterface.clear();
802 }
803}
804
Eric Laurentbfb1b832013-01-07 09:53:42 -0800805status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
806{
807 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
808 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800809 sp<ThreadBase> thread = mThread.promote();
810 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700811 sp<StreamHalInterface> stream = thread->stream();
812 if (stream != 0) {
813 status_t result = stream->removeEffect(mEffectInterface);
814 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800815 }
816 }
817 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800818 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800819}
820
Andy Hunge4a1d912016-08-17 14:11:13 -0700821// round up delta valid if value and divisor are positive.
822template <typename T>
823static T roundUpDelta(const T &value, const T &divisor) {
824 T remainder = value % divisor;
825 return remainder == 0 ? 0 : divisor - remainder;
826}
827
Eric Laurentca7cc822012-11-19 14:55:58 -0800828status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
829 uint32_t cmdSize,
830 void *pCmdData,
831 uint32_t *replySize,
832 void *pReplyData)
833{
834 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700835 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800836
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700837 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 return NO_INIT;
839 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700840 if (mStatus != NO_ERROR) {
841 return mStatus;
842 }
Andy Hung110bc952016-06-20 15:22:52 -0700843 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700844 (sizeof(effect_param_t) > cmdSize ||
845 ((effect_param_t *)pCmdData)->psize > cmdSize
846 - sizeof(effect_param_t))) {
847 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800848 android_errorWriteLog(0x534e4554, "33003822");
849 return -EINVAL;
850 }
851 if (cmdCode == EFFECT_CMD_GET_PARAM &&
852 (*replySize < sizeof(effect_param_t) ||
853 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
854 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700855 return -EINVAL;
856 }
ragoe2759072016-11-22 18:02:48 -0800857 if (cmdCode == EFFECT_CMD_GET_PARAM &&
858 (sizeof(effect_param_t) > *replySize
859 || ((effect_param_t *)pCmdData)->psize > *replySize
860 - sizeof(effect_param_t)
861 || ((effect_param_t *)pCmdData)->vsize > *replySize
862 - sizeof(effect_param_t)
863 - ((effect_param_t *)pCmdData)->psize
864 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
865 *replySize
866 - sizeof(effect_param_t)
867 - ((effect_param_t *)pCmdData)->psize
868 - ((effect_param_t *)pCmdData)->vsize)) {
869 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
870 android_errorWriteLog(0x534e4554, "32705438");
871 return -EINVAL;
872 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700873 if ((cmdCode == EFFECT_CMD_SET_PARAM
874 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
875 (sizeof(effect_param_t) > cmdSize
876 || ((effect_param_t *)pCmdData)->psize > cmdSize
877 - sizeof(effect_param_t)
878 || ((effect_param_t *)pCmdData)->vsize > cmdSize
879 - sizeof(effect_param_t)
880 - ((effect_param_t *)pCmdData)->psize
881 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
882 cmdSize
883 - sizeof(effect_param_t)
884 - ((effect_param_t *)pCmdData)->psize
885 - ((effect_param_t *)pCmdData)->vsize)) {
886 android_errorWriteLog(0x534e4554, "30204301");
887 return -EINVAL;
888 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700889 status_t status = mEffectInterface->command(cmdCode,
890 cmdSize,
891 pCmdData,
892 replySize,
893 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800894 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
895 uint32_t size = (replySize == NULL) ? 0 : *replySize;
896 for (size_t i = 1; i < mHandles.size(); i++) {
897 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800898 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800899 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
900 }
901 }
902 }
903 return status;
904}
905
906status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
907{
908 Mutex::Autolock _l(mLock);
909 return setEnabled_l(enabled);
910}
911
912// must be called with EffectModule::mLock held
913status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
914{
915
916 ALOGV("setEnabled %p enabled %d", this, enabled);
917
918 if (enabled != isEnabled()) {
919 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
920 if (enabled && status != NO_ERROR) {
921 return status;
922 }
923
924 switch (mState) {
925 // going from disabled to enabled
926 case IDLE:
927 mState = STARTING;
928 break;
929 case STOPPED:
930 mState = RESTART;
931 break;
932 case STOPPING:
933 mState = ACTIVE;
934 break;
935
936 // going from enabled to disabled
937 case RESTART:
938 mState = STOPPED;
939 break;
940 case STARTING:
941 mState = IDLE;
942 break;
943 case ACTIVE:
944 mState = STOPPING;
945 break;
946 case DESTROYED:
947 return NO_ERROR; // simply ignore as we are being destroyed
948 }
949 for (size_t i = 1; i < mHandles.size(); i++) {
950 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800951 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800952 h->setEnabled(enabled);
953 }
954 }
955 }
956 return NO_ERROR;
957}
958
959bool AudioFlinger::EffectModule::isEnabled() const
960{
961 switch (mState) {
962 case RESTART:
963 case STARTING:
964 case ACTIVE:
965 return true;
966 case IDLE:
967 case STOPPING:
968 case STOPPED:
969 case DESTROYED:
970 default:
971 return false;
972 }
973}
974
975bool AudioFlinger::EffectModule::isProcessEnabled() const
976{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700977 if (mStatus != NO_ERROR) {
978 return false;
979 }
980
Eric Laurentca7cc822012-11-19 14:55:58 -0800981 switch (mState) {
982 case RESTART:
983 case ACTIVE:
984 case STOPPING:
985 case STOPPED:
986 return true;
987 case IDLE:
988 case STARTING:
989 case DESTROYED:
990 default:
991 return false;
992 }
993}
994
Mikhail Naganov022b9952017-01-04 16:36:51 -0800995void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700996 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800997
998 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800999 if (buffer != 0) {
1000 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1001 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1002 } else {
1003 mConfig.inputCfg.buffer.raw = NULL;
1004 }
1005 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001006 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001007
1008#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001009 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001010 // Theoretically insert effects can also do in-place conversions (destroying
1011 // the original buffer) when the output buffer is identical to the input buffer,
1012 // but we don't optimize for it here.
1013 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001014 const uint32_t inChannelCount =
1015 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1016 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1017 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001018 // we need to translate - create hidl shared buffer and intercept
1019 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001020 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1021 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1022 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001023
1024 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1025 __func__, inChannels, inFrameCount, size);
1026
Andy Hungbded9c82017-11-30 18:47:35 -08001027 if (size > 0 && (mInConversionBuffer.get() == nullptr
1028 || size > mInConversionBuffer->getSize())) {
1029 mInConversionBuffer.clear();
1030 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001031 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1032 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1033 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001034 }
Andy Hungbded9c82017-11-30 18:47:35 -08001035 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001036 mInConversionBuffer->setFrameCount(inFrameCount);
1037 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001038 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001039 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001040 }
1041 }
1042#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001043}
1044
1045void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001046 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001047
1048 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001049 if (buffer != 0) {
1050 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1051 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1052 } else {
1053 mConfig.outputCfg.buffer.raw = NULL;
1054 }
1055 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001056 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001057
1058#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001059 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001060 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001061 const uint32_t outChannelCount =
1062 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1063 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1064 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001065 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001066 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1067 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1068 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001069
1070 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1071 __func__, outChannels, outFrameCount, size);
1072
Andy Hungbded9c82017-11-30 18:47:35 -08001073 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1074 || size > mOutConversionBuffer->getSize())) {
1075 mOutConversionBuffer.clear();
1076 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Kevin Rocard7588ff42018-01-08 11:11:30 -08001077 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1078 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1079 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001080 }
Andy Hungbded9c82017-11-30 18:47:35 -08001081 if (mOutConversionBuffer.get() != nullptr) {
1082 mOutConversionBuffer->setFrameCount(outFrameCount);
1083 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001084 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001085 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001086 }
1087 }
1088#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001089}
1090
Eric Laurentca7cc822012-11-19 14:55:58 -08001091status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1092{
1093 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001094 if (mStatus != NO_ERROR) {
1095 return mStatus;
1096 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001097 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001098 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1099 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1100 if (isProcessEnabled() &&
1101 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
1102 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001103 uint32_t volume[2];
1104 uint32_t *pVolume = NULL;
1105 uint32_t size = sizeof(volume);
1106 volume[0] = *left;
1107 volume[1] = *right;
1108 if (controller) {
1109 pVolume = volume;
1110 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001111 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1112 size,
1113 volume,
1114 &size,
1115 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001116 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1117 *left = volume[0];
1118 *right = volume[1];
1119 }
1120 }
1121 return status;
1122}
1123
1124status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1125{
1126 if (device == AUDIO_DEVICE_NONE) {
1127 return NO_ERROR;
1128 }
1129
1130 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001131 if (mStatus != NO_ERROR) {
1132 return mStatus;
1133 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001134 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001135 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001136 status_t cmdStatus;
1137 uint32_t size = sizeof(status_t);
1138 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1139 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001140 status = mEffectInterface->command(cmd,
1141 sizeof(uint32_t),
1142 &device,
1143 &size,
1144 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001145 }
1146 return status;
1147}
1148
1149status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1150{
1151 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001152 if (mStatus != NO_ERROR) {
1153 return mStatus;
1154 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001155 status_t status = NO_ERROR;
1156 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1157 status_t cmdStatus;
1158 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001159 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1160 sizeof(audio_mode_t),
1161 &mode,
1162 &size,
1163 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001164 if (status == NO_ERROR) {
1165 status = cmdStatus;
1166 }
1167 }
1168 return status;
1169}
1170
1171status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1172{
1173 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001174 if (mStatus != NO_ERROR) {
1175 return mStatus;
1176 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001177 status_t status = NO_ERROR;
1178 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1179 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001180 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1181 sizeof(audio_source_t),
1182 &source,
1183 &size,
1184 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001185 }
1186 return status;
1187}
1188
1189void AudioFlinger::EffectModule::setSuspended(bool suspended)
1190{
1191 Mutex::Autolock _l(mLock);
1192 mSuspended = suspended;
1193}
1194
1195bool AudioFlinger::EffectModule::suspended() const
1196{
1197 Mutex::Autolock _l(mLock);
1198 return mSuspended;
1199}
1200
1201bool AudioFlinger::EffectModule::purgeHandles()
1202{
1203 bool enabled = false;
1204 Mutex::Autolock _l(mLock);
1205 for (size_t i = 0; i < mHandles.size(); i++) {
1206 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001207 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001208 if (handle->hasControl()) {
1209 enabled = handle->enabled();
1210 }
1211 }
1212 }
1213 return enabled;
1214}
1215
Eric Laurent5baf2af2013-09-12 17:37:00 -07001216status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1217{
1218 Mutex::Autolock _l(mLock);
1219 if (mStatus != NO_ERROR) {
1220 return mStatus;
1221 }
1222 status_t status = NO_ERROR;
1223 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1224 status_t cmdStatus;
1225 uint32_t size = sizeof(status_t);
1226 effect_offload_param_t cmd;
1227
1228 cmd.isOffload = offloaded;
1229 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001230 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1231 sizeof(effect_offload_param_t),
1232 &cmd,
1233 &size,
1234 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001235 if (status == NO_ERROR) {
1236 status = cmdStatus;
1237 }
1238 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1239 } else {
1240 if (offloaded) {
1241 status = INVALID_OPERATION;
1242 }
1243 mOffloaded = false;
1244 }
1245 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1246 return status;
1247}
1248
1249bool AudioFlinger::EffectModule::isOffloaded() const
1250{
1251 Mutex::Autolock _l(mLock);
1252 return mOffloaded;
1253}
1254
Marco Nelissenb2208842014-02-07 14:00:50 -08001255String8 effectFlagsToString(uint32_t flags) {
1256 String8 s;
1257
1258 s.append("conn. mode: ");
1259 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1260 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1261 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1262 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1263 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1264 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1265 default: s.append("unknown/reserved"); break;
1266 }
1267 s.append(", ");
1268
1269 s.append("insert pref: ");
1270 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1271 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1272 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1273 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1274 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1275 default: s.append("unknown/reserved"); break;
1276 }
1277 s.append(", ");
1278
1279 s.append("volume mgmt: ");
1280 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1281 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1282 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1283 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1284 default: s.append("unknown/reserved"); break;
1285 }
1286 s.append(", ");
1287
1288 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1289 if (devind) {
1290 s.append("device indication: ");
1291 switch (devind) {
1292 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1293 default: s.append("unknown/reserved"); break;
1294 }
1295 s.append(", ");
1296 }
1297
1298 s.append("input mode: ");
1299 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1300 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1301 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1302 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1303 default: s.append("not set"); break;
1304 }
1305 s.append(", ");
1306
1307 s.append("output mode: ");
1308 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1309 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1310 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1311 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1312 default: s.append("not set"); break;
1313 }
1314 s.append(", ");
1315
1316 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1317 if (accel) {
1318 s.append("hardware acceleration: ");
1319 switch (accel) {
1320 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1321 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1322 default: s.append("unknown/reserved"); break;
1323 }
1324 s.append(", ");
1325 }
1326
1327 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1328 if (modeind) {
1329 s.append("mode indication: ");
1330 switch (modeind) {
1331 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1332 default: s.append("unknown/reserved"); break;
1333 }
1334 s.append(", ");
1335 }
1336
1337 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1338 if (srcind) {
1339 s.append("source indication: ");
1340 switch (srcind) {
1341 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1342 default: s.append("unknown/reserved"); break;
1343 }
1344 s.append(", ");
1345 }
1346
1347 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1348 s.append("offloadable, ");
1349 }
1350
1351 int len = s.length();
1352 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001353 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001354 s.unlockBuffer(len - 2);
1355 }
1356 return s;
1357}
1358
Andy Hungbded9c82017-11-30 18:47:35 -08001359static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1360 std::stringstream ss;
1361
1362 if (buffer.get() == nullptr) {
1363 return "nullptr"; // make different than below
1364 } else if (buffer->externalData() != nullptr) {
1365 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1366 << " -> "
1367 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1368 } else {
1369 ss << buffer->audioBuffer()->raw;
1370 }
1371 return ss.str();
1372}
Marco Nelissenb2208842014-02-07 14:00:50 -08001373
Glenn Kasten0f11b512014-01-31 16:18:54 -08001374void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001375{
Eric Laurentca7cc822012-11-19 14:55:58 -08001376 String8 result;
1377
Andy Hung9718d662017-12-22 17:57:39 -08001378 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001379
1380 bool locked = AudioFlinger::dumpTryLock(mLock);
1381 // failed to lock - AudioFlinger is probably deadlocked
1382 if (!locked) {
1383 result.append("\t\tCould not lock Fx mutex:\n");
1384 }
1385
1386 result.append("\t\tSession Status State Engine:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001387 result.appendFormat("\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001388 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001389
1390 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001391 char uuidStr[64];
1392 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001393 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001394 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001395 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1396 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001397 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001398 mDescriptor.flags,
1399 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001400 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001401 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001402
1403 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001404 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001405
1406 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001407
1408 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001409 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1410 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1411 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001412 mConfig.inputCfg.buffer.frameCount,
1413 mConfig.inputCfg.samplingRate,
1414 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001415 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001416 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001417
1418 result.append("\t\t- Output configuration:\n");
1419 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001420 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001421 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001422 mConfig.outputCfg.buffer.frameCount,
1423 mConfig.outputCfg.samplingRate,
1424 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001425 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001426 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001427
rago94a1ee82017-07-21 15:11:02 -07001428#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001429
Andy Hungbded9c82017-11-30 18:47:35 -08001430 result.appendFormat("\t\t- HAL buffers:\n"
1431 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1432 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1433 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1434 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1435 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001436#endif
1437
Andy Hung9718d662017-12-22 17:57:39 -08001438 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001439 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001440 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001441 for (size_t i = 0; i < mHandles.size(); ++i) {
1442 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001443 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001444 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001445 result.append(buffer);
1446 }
1447 }
1448
Eric Laurentca7cc822012-11-19 14:55:58 -08001449 write(fd, result.string(), result.length());
1450
1451 if (locked) {
1452 mLock.unlock();
1453 }
1454}
1455
1456// ----------------------------------------------------------------------------
1457// EffectHandle implementation
1458// ----------------------------------------------------------------------------
1459
1460#undef LOG_TAG
1461#define LOG_TAG "AudioFlinger::EffectHandle"
1462
1463AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1464 const sp<AudioFlinger::Client>& client,
1465 const sp<IEffectClient>& effectClient,
1466 int32_t priority)
1467 : BnEffect(),
1468 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001469 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001470{
1471 ALOGV("constructor %p", this);
1472
1473 if (client == 0) {
1474 return;
1475 }
1476 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1477 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001478 if (mCblkMemory == 0 ||
1479 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001480 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001481 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001482 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001483 return;
1484 }
Glenn Kastene75da402013-11-20 13:54:52 -08001485 new(mCblk) effect_param_cblk_t();
1486 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001487}
1488
1489AudioFlinger::EffectHandle::~EffectHandle()
1490{
1491 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001492 disconnect(false);
1493}
1494
Glenn Kastene75da402013-11-20 13:54:52 -08001495status_t AudioFlinger::EffectHandle::initCheck()
1496{
1497 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1498}
1499
Eric Laurentca7cc822012-11-19 14:55:58 -08001500status_t AudioFlinger::EffectHandle::enable()
1501{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001502 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001503 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001504 sp<EffectModule> effect = mEffect.promote();
1505 if (effect == 0 || mDisconnected) {
1506 return DEAD_OBJECT;
1507 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001508 if (!mHasControl) {
1509 return INVALID_OPERATION;
1510 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001511
1512 if (mEnabled) {
1513 return NO_ERROR;
1514 }
1515
1516 mEnabled = true;
1517
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001518 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001519 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001520 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001521 }
1522
1523 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001524 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001525 return NO_ERROR;
1526 }
1527
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001528 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001529 if (status != NO_ERROR) {
1530 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001531 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001532 }
1533 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001534 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001535 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001536 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1537 Mutex::Autolock _l(thread->mLock);
1538 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001539 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001540 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001541 if (thread->type() == ThreadBase::OFFLOAD) {
1542 PlaybackThread *t = (PlaybackThread *)thread.get();
1543 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1544 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001545 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001546 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1547 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001548 }
1549 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001550 }
1551 return status;
1552}
1553
1554status_t AudioFlinger::EffectHandle::disable()
1555{
1556 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001557 AutoMutex _l(mLock);
1558 sp<EffectModule> effect = mEffect.promote();
1559 if (effect == 0 || mDisconnected) {
1560 return DEAD_OBJECT;
1561 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001562 if (!mHasControl) {
1563 return INVALID_OPERATION;
1564 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001565
1566 if (!mEnabled) {
1567 return NO_ERROR;
1568 }
1569 mEnabled = false;
1570
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001571 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001572 return NO_ERROR;
1573 }
1574
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001575 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001576
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001577 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001578 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001579 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001580 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1581 Mutex::Autolock _l(thread->mLock);
1582 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001583 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001584 }
1585
1586 return status;
1587}
1588
1589void AudioFlinger::EffectHandle::disconnect()
1590{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001591 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001592 disconnect(true);
1593}
1594
1595void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1596{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001597 AutoMutex _l(mLock);
1598 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1599 if (mDisconnected) {
1600 if (unpinIfLast) {
1601 android_errorWriteLog(0x534e4554, "32707507");
1602 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001603 return;
1604 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001605 mDisconnected = true;
1606 sp<ThreadBase> thread;
1607 {
1608 sp<EffectModule> effect = mEffect.promote();
1609 if (effect != 0) {
1610 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001611 }
1612 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001613 if (thread != 0) {
1614 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001615 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001616 // try to cleanup as much as we can
1617 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001618 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1619 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001620 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001621 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001622
Eric Laurentca7cc822012-11-19 14:55:58 -08001623 if (mClient != 0) {
1624 if (mCblk != NULL) {
1625 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1626 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1627 }
1628 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001629 // Client destructor must run with AudioFlinger client mutex locked
1630 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 mClient.clear();
1632 }
1633}
1634
1635status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1636 uint32_t cmdSize,
1637 void *pCmdData,
1638 uint32_t *replySize,
1639 void *pReplyData)
1640{
1641 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001642 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001643
Eric Laurentc7ab3092017-06-15 18:43:46 -07001644 // reject commands reserved for internal use by audio framework if coming from outside
1645 // of audioserver
1646 switch(cmdCode) {
1647 case EFFECT_CMD_ENABLE:
1648 case EFFECT_CMD_DISABLE:
1649 case EFFECT_CMD_SET_PARAM:
1650 case EFFECT_CMD_SET_PARAM_DEFERRED:
1651 case EFFECT_CMD_SET_PARAM_COMMIT:
1652 case EFFECT_CMD_GET_PARAM:
1653 break;
1654 default:
1655 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1656 break;
1657 }
1658 android_errorWriteLog(0x534e4554, "62019992");
1659 return BAD_VALUE;
1660 }
1661
Eric Laurent1ffc5852016-12-15 14:46:09 -08001662 if (cmdCode == EFFECT_CMD_ENABLE) {
1663 if (*replySize < sizeof(int)) {
1664 android_errorWriteLog(0x534e4554, "32095713");
1665 return BAD_VALUE;
1666 }
1667 *(int *)pReplyData = NO_ERROR;
1668 *replySize = sizeof(int);
1669 return enable();
1670 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1671 if (*replySize < sizeof(int)) {
1672 android_errorWriteLog(0x534e4554, "32095713");
1673 return BAD_VALUE;
1674 }
1675 *(int *)pReplyData = NO_ERROR;
1676 *replySize = sizeof(int);
1677 return disable();
1678 }
1679
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001680 AutoMutex _l(mLock);
1681 sp<EffectModule> effect = mEffect.promote();
1682 if (effect == 0 || mDisconnected) {
1683 return DEAD_OBJECT;
1684 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001685 // only get parameter command is permitted for applications not controlling the effect
1686 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1687 return INVALID_OPERATION;
1688 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001689 if (mClient == 0) {
1690 return INVALID_OPERATION;
1691 }
1692
1693 // handle commands that are not forwarded transparently to effect engine
1694 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001695 if (*replySize < sizeof(int)) {
1696 android_errorWriteLog(0x534e4554, "32095713");
1697 return BAD_VALUE;
1698 }
1699 *(int *)pReplyData = NO_ERROR;
1700 *replySize = sizeof(int);
1701
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 // No need to trylock() here as this function is executed in the binder thread serving a
1703 // particular client process: no risk to block the whole media server process or mixer
1704 // threads if we are stuck here
1705 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001706 // keep local copy of index in case of client corruption b/32220769
1707 const uint32_t clientIndex = mCblk->clientIndex;
1708 const uint32_t serverIndex = mCblk->serverIndex;
1709 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1710 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 mCblk->serverIndex = 0;
1712 mCblk->clientIndex = 0;
1713 return BAD_VALUE;
1714 }
1715 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001716 effect_param_t *param = NULL;
1717 for (uint32_t index = serverIndex; index < clientIndex;) {
1718 int *p = (int *)(mBuffer + index);
1719 const int size = *p++;
1720 if (size < 0
1721 || size > EFFECT_PARAM_BUFFER_SIZE
1722 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001723 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001724 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001725 break;
1726 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001727
1728 // copy to local memory in case of client corruption b/32220769
1729 param = (effect_param_t *)realloc(param, size);
1730 if (param == NULL) {
1731 ALOGW("command(): out of memory");
1732 status = NO_MEMORY;
1733 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001734 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001735 memcpy(param, p, size);
1736
1737 int reply = 0;
1738 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001739 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001740 size,
1741 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001742 &rsize,
1743 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001744
1745 // verify shared memory: server index shouldn't change; client index can't go back.
1746 if (serverIndex != mCblk->serverIndex
1747 || clientIndex > mCblk->clientIndex) {
1748 android_errorWriteLog(0x534e4554, "32220769");
1749 status = BAD_VALUE;
1750 break;
1751 }
1752
Eric Laurentca7cc822012-11-19 14:55:58 -08001753 // stop at first error encountered
1754 if (ret != NO_ERROR) {
1755 status = ret;
1756 *(int *)pReplyData = reply;
1757 break;
1758 } else if (reply != NO_ERROR) {
1759 *(int *)pReplyData = reply;
1760 break;
1761 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001762 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001763 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001764 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001765 mCblk->serverIndex = 0;
1766 mCblk->clientIndex = 0;
1767 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001768 }
1769
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001770 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001771}
1772
1773void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1774{
1775 ALOGV("setControl %p control %d", this, hasControl);
1776
1777 mHasControl = hasControl;
1778 mEnabled = enabled;
1779
1780 if (signal && mEffectClient != 0) {
1781 mEffectClient->controlStatusChanged(hasControl);
1782 }
1783}
1784
1785void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1786 uint32_t cmdSize,
1787 void *pCmdData,
1788 uint32_t replySize,
1789 void *pReplyData)
1790{
1791 if (mEffectClient != 0) {
1792 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1793 }
1794}
1795
1796
1797
1798void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1799{
1800 if (mEffectClient != 0) {
1801 mEffectClient->enableStatusChanged(enabled);
1802 }
1803}
1804
1805status_t AudioFlinger::EffectHandle::onTransact(
1806 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1807{
1808 return BnEffect::onTransact(code, data, reply, flags);
1809}
1810
1811
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001812void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001813{
1814 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1815
Marco Nelissenb2208842014-02-07 14:00:50 -08001816 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001817 (mClient == 0) ? getpid_cached : mClient->pid(),
1818 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001819 mHasControl ? "yes" : "no",
1820 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001821 mCblk ? mCblk->clientIndex : 0,
1822 mCblk ? mCblk->serverIndex : 0
1823 );
1824
1825 if (locked) {
1826 mCblk->lock.unlock();
1827 }
1828}
1829
1830#undef LOG_TAG
1831#define LOG_TAG "AudioFlinger::EffectChain"
1832
1833AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001834 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001835 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001836 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001837 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001838{
1839 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1840 if (thread == NULL) {
1841 return;
1842 }
1843 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1844 thread->frameCount();
1845}
1846
1847AudioFlinger::EffectChain::~EffectChain()
1848{
Eric Laurentca7cc822012-11-19 14:55:58 -08001849}
1850
1851// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1852sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1853 effect_descriptor_t *descriptor)
1854{
1855 size_t size = mEffects.size();
1856
1857 for (size_t i = 0; i < size; i++) {
1858 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1859 return mEffects[i];
1860 }
1861 }
1862 return 0;
1863}
1864
1865// getEffectFromId_l() must be called with ThreadBase::mLock held
1866sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1867{
1868 size_t size = mEffects.size();
1869
1870 for (size_t i = 0; i < size; i++) {
1871 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1872 if (id == 0 || mEffects[i]->id() == id) {
1873 return mEffects[i];
1874 }
1875 }
1876 return 0;
1877}
1878
1879// getEffectFromType_l() must be called with ThreadBase::mLock held
1880sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1881 const effect_uuid_t *type)
1882{
1883 size_t size = mEffects.size();
1884
1885 for (size_t i = 0; i < size; i++) {
1886 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1887 return mEffects[i];
1888 }
1889 }
1890 return 0;
1891}
1892
1893void AudioFlinger::EffectChain::clearInputBuffer()
1894{
1895 Mutex::Autolock _l(mLock);
1896 sp<ThreadBase> thread = mThread.promote();
1897 if (thread == 0) {
1898 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1899 return;
1900 }
1901 clearInputBuffer_l(thread);
1902}
1903
1904// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001905void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001906{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001907 if (mInBuffer == NULL) {
1908 return;
1909 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001910 const size_t frameSize =
Andy Hung9aad48c2017-11-29 10:29:19 -08001911 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
rago94a1ee82017-07-21 15:11:02 -07001912
Mikhail Naganov022b9952017-01-04 16:36:51 -08001913 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1914 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001915}
1916
1917// Must be called with EffectChain::mLock locked
1918void AudioFlinger::EffectChain::process_l()
1919{
1920 sp<ThreadBase> thread = mThread.promote();
1921 if (thread == 0) {
1922 ALOGW("process_l(): cannot promote mixer thread");
1923 return;
1924 }
1925 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1926 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001927 // never process effects when:
1928 // - on an OFFLOAD thread
1929 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001930 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1931 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001932 if (!isGlobalSession) {
1933 bool tracksOnSession = (trackCnt() != 0);
1934
1935 if (!tracksOnSession && mTailBufferCount == 0) {
1936 doProcess = false;
1937 }
1938
1939 if (activeTrackCnt() == 0) {
1940 // if no track is active and the effect tail has not been rendered,
1941 // the input buffer must be cleared here as the mixer process will not do it
1942 if (tracksOnSession || mTailBufferCount > 0) {
1943 clearInputBuffer_l(thread);
1944 if (mTailBufferCount > 0) {
1945 mTailBufferCount--;
1946 }
1947 }
1948 }
1949 }
1950
1951 size_t size = mEffects.size();
1952 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001953 // Only the input and output buffers of the chain can be external,
1954 // and 'update' / 'commit' do nothing for allocated buffers, thus
1955 // it's not needed to consider any other buffers here.
1956 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001957 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1958 mOutBuffer->update();
1959 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001960 for (size_t i = 0; i < size; i++) {
1961 mEffects[i]->process();
1962 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001963 mInBuffer->commit();
1964 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1965 mOutBuffer->commit();
1966 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001967 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001968 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001969 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001970 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1971 }
1972 if (doResetVolume) {
1973 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001974 }
1975}
1976
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001977// createEffect_l() must be called with ThreadBase::mLock held
1978status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1979 ThreadBase *thread,
1980 effect_descriptor_t *desc,
1981 int id,
1982 audio_session_t sessionId,
1983 bool pinned)
1984{
1985 Mutex::Autolock _l(mLock);
1986 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1987 status_t lStatus = effect->status();
1988 if (lStatus == NO_ERROR) {
1989 lStatus = addEffect_ll(effect);
1990 }
1991 if (lStatus != NO_ERROR) {
1992 effect.clear();
1993 }
1994 return lStatus;
1995}
1996
1997// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001998status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1999{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002000 Mutex::Autolock _l(mLock);
2001 return addEffect_ll(effect);
2002}
2003// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2004status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2005{
Eric Laurentca7cc822012-11-19 14:55:58 -08002006 effect_descriptor_t desc = effect->desc();
2007 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2008
Eric Laurentca7cc822012-11-19 14:55:58 -08002009 effect->setChain(this);
2010 sp<ThreadBase> thread = mThread.promote();
2011 if (thread == 0) {
2012 return NO_INIT;
2013 }
2014 effect->setThread(thread);
2015
2016 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2017 // Auxiliary effects are inserted at the beginning of mEffects vector as
2018 // they are processed first and accumulated in chain input buffer
2019 mEffects.insertAt(effect, 0);
2020
2021 // the input buffer for auxiliary effect contains mono samples in
2022 // 32 bit format. This is to avoid saturation in AudoMixer
2023 // accumulation stage. Saturation is done in EffectModule::process() before
2024 // calling the process in effect engine
2025 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002026 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002027#ifdef FLOAT_EFFECT_CHAIN
Kevin Rocard7588ff42018-01-08 11:11:30 -08002028 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
rago94a1ee82017-07-21 15:11:02 -07002029 numSamples * sizeof(float), &halBuffer);
2030#else
Kevin Rocard7588ff42018-01-08 11:11:30 -08002031 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002032 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002033#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002034 if (result != OK) return result;
2035 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002036 // auxiliary effects output samples to chain input buffer for further processing
2037 // by insert effects
2038 effect->setOutBuffer(mInBuffer);
2039 } else {
2040 // Insert effects are inserted at the end of mEffects vector as they are processed
2041 // after track and auxiliary effects.
2042 // Insert effect order as a function of indicated preference:
2043 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2044 // another effect is present
2045 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2046 // last effect claiming first position
2047 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2048 // first effect claiming last position
2049 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2050 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2051 // already present
2052
2053 size_t size = mEffects.size();
2054 size_t idx_insert = size;
2055 ssize_t idx_insert_first = -1;
2056 ssize_t idx_insert_last = -1;
2057
2058 for (size_t i = 0; i < size; i++) {
2059 effect_descriptor_t d = mEffects[i]->desc();
2060 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2061 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2062 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2063 // check invalid effect chaining combinations
2064 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2065 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2066 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2067 desc.name, d.name);
2068 return INVALID_OPERATION;
2069 }
2070 // remember position of first insert effect and by default
2071 // select this as insert position for new effect
2072 if (idx_insert == size) {
2073 idx_insert = i;
2074 }
2075 // remember position of last insert effect claiming
2076 // first position
2077 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2078 idx_insert_first = i;
2079 }
2080 // remember position of first insert effect claiming
2081 // last position
2082 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2083 idx_insert_last == -1) {
2084 idx_insert_last = i;
2085 }
2086 }
2087 }
2088
2089 // modify idx_insert from first position if needed
2090 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2091 if (idx_insert_last != -1) {
2092 idx_insert = idx_insert_last;
2093 } else {
2094 idx_insert = size;
2095 }
2096 } else {
2097 if (idx_insert_first != -1) {
2098 idx_insert = idx_insert_first + 1;
2099 }
2100 }
2101
2102 // always read samples from chain input buffer
2103 effect->setInBuffer(mInBuffer);
2104
2105 // if last effect in the chain, output samples to chain
2106 // output buffer, otherwise to chain input buffer
2107 if (idx_insert == size) {
2108 if (idx_insert != 0) {
2109 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2110 mEffects[idx_insert-1]->configure();
2111 }
2112 effect->setOutBuffer(mOutBuffer);
2113 } else {
2114 effect->setOutBuffer(mInBuffer);
2115 }
2116 mEffects.insertAt(effect, idx_insert);
2117
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002118 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002119 idx_insert);
2120 }
2121 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002122
Eric Laurentca7cc822012-11-19 14:55:58 -08002123 return NO_ERROR;
2124}
2125
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002126// removeEffect_l() must be called with ThreadBase::mLock held
2127size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2128 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002129{
2130 Mutex::Autolock _l(mLock);
2131 size_t size = mEffects.size();
2132 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2133
2134 for (size_t i = 0; i < size; i++) {
2135 if (effect == mEffects[i]) {
2136 // calling stop here will remove pre-processing effect from the audio HAL.
2137 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2138 // the middle of a read from audio HAL
2139 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2140 mEffects[i]->state() == EffectModule::STOPPING) {
2141 mEffects[i]->stop();
2142 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002143 if (release) {
2144 mEffects[i]->release_l();
2145 }
2146
Mikhail Naganov022b9952017-01-04 16:36:51 -08002147 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002148 if (i == size - 1 && i != 0) {
2149 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2150 mEffects[i - 1]->configure();
2151 }
2152 }
2153 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002154 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002155 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002156
Eric Laurentca7cc822012-11-19 14:55:58 -08002157 break;
2158 }
2159 }
2160
2161 return mEffects.size();
2162}
2163
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002164// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002165void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2166{
2167 size_t size = mEffects.size();
2168 for (size_t i = 0; i < size; i++) {
2169 mEffects[i]->setDevice(device);
2170 }
2171}
2172
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002173// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002174void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2175{
2176 size_t size = mEffects.size();
2177 for (size_t i = 0; i < size; i++) {
2178 mEffects[i]->setMode(mode);
2179 }
2180}
2181
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002182// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002183void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2184{
2185 size_t size = mEffects.size();
2186 for (size_t i = 0; i < size; i++) {
2187 mEffects[i]->setAudioSource(source);
2188 }
2189}
2190
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002191// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002192bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002193{
2194 uint32_t newLeft = *left;
2195 uint32_t newRight = *right;
2196 bool hasControl = false;
2197 int ctrlIdx = -1;
2198 size_t size = mEffects.size();
2199
2200 // first update volume controller
2201 for (size_t i = size; i > 0; i--) {
2202 if (mEffects[i - 1]->isProcessEnabled() &&
2203 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2204 ctrlIdx = i - 1;
2205 hasControl = true;
2206 break;
2207 }
2208 }
2209
Eric Laurentfa1e1232016-08-02 19:01:49 -07002210 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002211 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002212 if (hasControl) {
2213 *left = mNewLeftVolume;
2214 *right = mNewRightVolume;
2215 }
2216 return hasControl;
2217 }
2218
2219 mVolumeCtrlIdx = ctrlIdx;
2220 mLeftVolume = newLeft;
2221 mRightVolume = newRight;
2222
2223 // second get volume update from volume controller
2224 if (ctrlIdx >= 0) {
2225 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2226 mNewLeftVolume = newLeft;
2227 mNewRightVolume = newRight;
2228 }
2229 // then indicate volume to all other effects in chain.
2230 // Pass altered volume to effects before volume controller
2231 // and requested volume to effects after controller
2232 uint32_t lVol = newLeft;
2233 uint32_t rVol = newRight;
2234
2235 for (size_t i = 0; i < size; i++) {
2236 if ((int)i == ctrlIdx) {
2237 continue;
2238 }
2239 // this also works for ctrlIdx == -1 when there is no volume controller
2240 if ((int)i > ctrlIdx) {
2241 lVol = *left;
2242 rVol = *right;
2243 }
2244 mEffects[i]->setVolume(&lVol, &rVol, false);
2245 }
2246 *left = newLeft;
2247 *right = newRight;
2248
2249 return hasControl;
2250}
2251
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002252// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002253void AudioFlinger::EffectChain::resetVolume_l()
2254{
Eric Laurente7449bf2016-08-03 18:44:07 -07002255 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2256 uint32_t left = mLeftVolume;
2257 uint32_t right = mRightVolume;
2258 (void)setVolume_l(&left, &right, true);
2259 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002260}
2261
Eric Laurent1b928682014-10-02 19:41:47 -07002262void AudioFlinger::EffectChain::syncHalEffectsState()
2263{
2264 Mutex::Autolock _l(mLock);
2265 for (size_t i = 0; i < mEffects.size(); i++) {
2266 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2267 mEffects[i]->state() == EffectModule::STOPPING) {
2268 mEffects[i]->addEffectToHal_l();
2269 }
2270 }
2271}
2272
Eric Laurentca7cc822012-11-19 14:55:58 -08002273void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2274{
2275 const size_t SIZE = 256;
2276 char buffer[SIZE];
2277 String8 result;
2278
Marco Nelissenb2208842014-02-07 14:00:50 -08002279 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002280 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002281 result.append(buffer);
2282
Marco Nelissenb2208842014-02-07 14:00:50 -08002283 if (numEffects) {
2284 bool locked = AudioFlinger::dumpTryLock(mLock);
2285 // failed to lock - AudioFlinger is probably deadlocked
2286 if (!locked) {
2287 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002288 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002289
Andy Hungbded9c82017-11-30 18:47:35 -08002290 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2291 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2292 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2293 (int)inBufferStr.size(), "In buffer ",
2294 (int)outBufferStr.size(), "Out buffer ");
2295 result.appendFormat("\t%s %s %d\n",
2296 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002297 write(fd, result.string(), result.size());
2298
2299 for (size_t i = 0; i < numEffects; ++i) {
2300 sp<EffectModule> effect = mEffects[i];
2301 if (effect != 0) {
2302 effect->dump(fd, args);
2303 }
2304 }
2305
2306 if (locked) {
2307 mLock.unlock();
2308 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002309 }
2310}
2311
2312// must be called with ThreadBase::mLock held
2313void AudioFlinger::EffectChain::setEffectSuspended_l(
2314 const effect_uuid_t *type, bool suspend)
2315{
2316 sp<SuspendedEffectDesc> desc;
2317 // use effect type UUID timelow as key as there is no real risk of identical
2318 // timeLow fields among effect type UUIDs.
2319 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2320 if (suspend) {
2321 if (index >= 0) {
2322 desc = mSuspendedEffects.valueAt(index);
2323 } else {
2324 desc = new SuspendedEffectDesc();
2325 desc->mType = *type;
2326 mSuspendedEffects.add(type->timeLow, desc);
2327 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2328 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002329
Eric Laurentca7cc822012-11-19 14:55:58 -08002330 if (desc->mRefCount++ == 0) {
2331 sp<EffectModule> effect = getEffectIfEnabled(type);
2332 if (effect != 0) {
2333 desc->mEffect = effect;
2334 effect->setSuspended(true);
2335 effect->setEnabled(false);
2336 }
2337 }
2338 } else {
2339 if (index < 0) {
2340 return;
2341 }
2342 desc = mSuspendedEffects.valueAt(index);
2343 if (desc->mRefCount <= 0) {
2344 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002345 desc->mRefCount = 0;
2346 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002347 }
2348 if (--desc->mRefCount == 0) {
2349 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2350 if (desc->mEffect != 0) {
2351 sp<EffectModule> effect = desc->mEffect.promote();
2352 if (effect != 0) {
2353 effect->setSuspended(false);
2354 effect->lock();
2355 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002356 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002357 effect->setEnabled_l(handle->enabled());
2358 }
2359 effect->unlock();
2360 }
2361 desc->mEffect.clear();
2362 }
2363 mSuspendedEffects.removeItemsAt(index);
2364 }
2365 }
2366}
2367
2368// must be called with ThreadBase::mLock held
2369void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2370{
2371 sp<SuspendedEffectDesc> desc;
2372
2373 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2374 if (suspend) {
2375 if (index >= 0) {
2376 desc = mSuspendedEffects.valueAt(index);
2377 } else {
2378 desc = new SuspendedEffectDesc();
2379 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2380 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2381 }
2382 if (desc->mRefCount++ == 0) {
2383 Vector< sp<EffectModule> > effects;
2384 getSuspendEligibleEffects(effects);
2385 for (size_t i = 0; i < effects.size(); i++) {
2386 setEffectSuspended_l(&effects[i]->desc().type, true);
2387 }
2388 }
2389 } else {
2390 if (index < 0) {
2391 return;
2392 }
2393 desc = mSuspendedEffects.valueAt(index);
2394 if (desc->mRefCount <= 0) {
2395 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2396 desc->mRefCount = 1;
2397 }
2398 if (--desc->mRefCount == 0) {
2399 Vector<const effect_uuid_t *> types;
2400 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2401 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2402 continue;
2403 }
2404 types.add(&mSuspendedEffects.valueAt(i)->mType);
2405 }
2406 for (size_t i = 0; i < types.size(); i++) {
2407 setEffectSuspended_l(types[i], false);
2408 }
2409 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2410 mSuspendedEffects.keyAt(index));
2411 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2412 }
2413 }
2414}
2415
2416
2417// The volume effect is used for automated tests only
2418#ifndef OPENSL_ES_H_
2419static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2420 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2421const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2422#endif //OPENSL_ES_H_
2423
Eric Laurentd8365c52017-07-16 15:27:05 -07002424/* static */
2425bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2426{
2427 // Only NS and AEC are suspended when BtNRec is off
2428 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2429 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2430 return true;
2431 }
2432 return false;
2433}
2434
Eric Laurentca7cc822012-11-19 14:55:58 -08002435bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2436{
2437 // auxiliary effects and visualizer are never suspended on output mix
2438 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2439 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2440 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2441 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2442 return false;
2443 }
2444 return true;
2445}
2446
2447void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2448 Vector< sp<AudioFlinger::EffectModule> > &effects)
2449{
2450 effects.clear();
2451 for (size_t i = 0; i < mEffects.size(); i++) {
2452 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2453 effects.add(mEffects[i]);
2454 }
2455 }
2456}
2457
2458sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2459 const effect_uuid_t *type)
2460{
2461 sp<EffectModule> effect = getEffectFromType_l(type);
2462 return effect != 0 && effect->isEnabled() ? effect : 0;
2463}
2464
2465void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2466 bool enabled)
2467{
2468 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2469 if (enabled) {
2470 if (index < 0) {
2471 // if the effect is not suspend check if all effects are suspended
2472 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2473 if (index < 0) {
2474 return;
2475 }
2476 if (!isEffectEligibleForSuspend(effect->desc())) {
2477 return;
2478 }
2479 setEffectSuspended_l(&effect->desc().type, enabled);
2480 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2481 if (index < 0) {
2482 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2483 return;
2484 }
2485 }
2486 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2487 effect->desc().type.timeLow);
2488 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002489 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002490 if (desc->mEffect == 0) {
2491 desc->mEffect = effect;
2492 effect->setEnabled(false);
2493 effect->setSuspended(true);
2494 }
2495 } else {
2496 if (index < 0) {
2497 return;
2498 }
2499 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2500 effect->desc().type.timeLow);
2501 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2502 desc->mEffect.clear();
2503 effect->setSuspended(false);
2504 }
2505}
2506
Eric Laurent5baf2af2013-09-12 17:37:00 -07002507bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002508{
2509 Mutex::Autolock _l(mLock);
2510 size_t size = mEffects.size();
2511 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002512 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002513 return true;
2514 }
2515 }
2516 return false;
2517}
2518
Eric Laurentaaa44472014-09-12 17:41:50 -07002519void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2520{
2521 Mutex::Autolock _l(mLock);
2522 mThread = thread;
2523 for (size_t i = 0; i < mEffects.size(); i++) {
2524 mEffects[i]->setThread(thread);
2525 }
2526}
2527
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002528void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2529{
2530 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2531 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2532 }
2533 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2534 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2535 }
2536}
2537
2538void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2539{
2540 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2541 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2542 }
2543 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2544 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2545 }
2546}
2547
2548bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002549{
2550 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002551 for (const auto &effect : mEffects) {
2552 if (effect->isProcessImplemented()) {
2553 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002554 }
2555 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002556 // Allow effects without processing.
2557 return true;
2558}
2559
2560bool AudioFlinger::EffectChain::isFastCompatible() const
2561{
2562 Mutex::Autolock _l(mLock);
2563 for (const auto &effect : mEffects) {
2564 if (effect->isProcessImplemented()
2565 && effect->isImplementationSoftware()) {
2566 return false;
2567 }
2568 }
2569 // Allow effects without processing or hw accelerated effects.
2570 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002571}
2572
2573// isCompatibleWithThread_l() must be called with thread->mLock held
2574bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2575{
2576 Mutex::Autolock _l(mLock);
2577 for (size_t i = 0; i < mEffects.size(); i++) {
2578 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2579 return false;
2580 }
2581 }
2582 return true;
2583}
2584
Glenn Kasten63238ef2015-03-02 15:50:29 -08002585} // namespace android