blob: bfb0fe22fdc57bba943be6c99eb0c437afc945ae [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>
Eric Laurentca7cc822012-11-19 14:55:58 -080029#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070030#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070031#include <media/audiohal/EffectHalInterface.h>
32#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080033
34#include "AudioFlinger.h"
35#include "ServiceUtilities.h"
36
37// ----------------------------------------------------------------------------
38
39// Note: the following macro is used for extremely verbose logging message. In
40// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
41// 0; but one side effect of this is to turn all LOGV's as well. Some messages
42// are so verbose that we want to suppress them even when we have ALOG_ASSERT
43// turned on. Do not uncomment the #def below unless you really know what you
44// are doing and want to see all of the extremely verbose messages.
45//#define VERY_VERY_VERBOSE_LOGGING
46#ifdef VERY_VERY_VERBOSE_LOGGING
47#define ALOGVV ALOGV
48#else
49#define ALOGVV(a...) do { } while(0)
50#endif
51
52namespace android {
53
54// ----------------------------------------------------------------------------
55// EffectModule implementation
56// ----------------------------------------------------------------------------
57
58#undef LOG_TAG
59#define LOG_TAG "AudioFlinger::EffectModule"
60
61AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
62 const wp<AudioFlinger::EffectChain>& chain,
63 effect_descriptor_t *desc,
64 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080065 audio_session_t sessionId,
66 bool pinned)
67 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080068 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69 mDescriptor(*desc),
70 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080071 mStatus(NO_INIT), mState(IDLE),
72 // mMaxDisableWaitCnt is set by configure() and not used before then
73 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070074 mSuspended(false),
75 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070076#ifdef FLOAT_EFFECT_CHAIN
77 , mSupportsFloat(false)
78#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080079{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080080 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080081 int lStatus;
82
83 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070084 mStatus = -ENODEV;
85 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070086 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070087 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070088 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070089 mStatus = effectsFactory->createEffect(
90 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
91 }
92 }
Eric Laurentca7cc822012-11-19 14:55:58 -080093
94 if (mStatus != NO_ERROR) {
95 return;
96 }
97 lStatus = init();
98 if (lStatus < 0) {
99 mStatus = lStatus;
100 goto Error;
101 }
102
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800103 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700104 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800105
Eric Laurentca7cc822012-11-19 14:55:58 -0800106 return;
107Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700108 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800109 ALOGV("Constructor Error %d", mStatus);
110}
111
112AudioFlinger::EffectModule::~EffectModule()
113{
114 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700115 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700116 char uuidStr[64];
117 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
118 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
119 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800120 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800121 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800122
Eric Laurentca7cc822012-11-19 14:55:58 -0800123}
124
125status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
126{
127 status_t status;
128
129 Mutex::Autolock _l(mLock);
130 int priority = handle->priority();
131 size_t size = mHandles.size();
132 EffectHandle *controlHandle = NULL;
133 size_t i;
134 for (i = 0; i < size; i++) {
135 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800136 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800137 continue;
138 }
139 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700140 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800141 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700142 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800143 if (h->priority() <= priority) {
144 break;
145 }
146 }
147 // if inserted in first place, move effect control from previous owner to this handle
148 if (i == 0) {
149 bool enabled = false;
150 if (controlHandle != NULL) {
151 enabled = controlHandle->enabled();
152 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
153 }
154 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
155 status = NO_ERROR;
156 } else {
157 status = ALREADY_EXISTS;
158 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700159 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800160 mHandles.insertAt(handle, i);
161 return status;
162}
163
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800164ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800165{
166 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800167 return removeHandle_l(handle);
168}
169
170ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
171{
Eric Laurentca7cc822012-11-19 14:55:58 -0800172 size_t size = mHandles.size();
173 size_t i;
174 for (i = 0; i < size; i++) {
175 if (mHandles[i] == handle) {
176 break;
177 }
178 }
179 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800180 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
181 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800182 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800183 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800184
185 mHandles.removeAt(i);
186 // if removed from first place, move effect control from this handle to next in line
187 if (i == 0) {
188 EffectHandle *h = controlHandle_l();
189 if (h != NULL) {
190 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
191 }
192 }
193
194 // Prevent calls to process() and other functions on effect interface from now on.
195 // The effect engine will be released by the destructor when the last strong reference on
196 // this object is released which can happen after next process is called.
197 if (mHandles.size() == 0 && !mPinned) {
198 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800199 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800200 }
201
202 return mHandles.size();
203}
204
205// must be called with EffectModule::mLock held
206AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
207{
208 // the first valid handle in the list has control over the module
209 for (size_t i = 0; i < mHandles.size(); i++) {
210 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800211 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 return h;
213 }
214 }
215
216 return NULL;
217}
218
Eric Laurentf10c7092016-12-06 17:09:56 -0800219// unsafe method called when the effect parent thread has been destroyed
220ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
221{
222 ALOGV("disconnect() %p handle %p", this, handle);
223 Mutex::Autolock _l(mLock);
224 ssize_t numHandles = removeHandle_l(handle);
225 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
226 AudioSystem::unregisterEffect(mId);
227 sp<AudioFlinger> af = mAudioFlinger.promote();
228 if (af != 0) {
229 mLock.unlock();
230 af->updateOrphanEffectChains(this);
231 mLock.lock();
232 }
233 }
234 return numHandles;
235}
236
Eric Laurentfa1e1232016-08-02 19:01:49 -0700237bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800238 Mutex::Autolock _l(mLock);
239
Eric Laurentfa1e1232016-08-02 19:01:49 -0700240 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800241 switch (mState) {
242 case RESTART:
243 reset_l();
244 // FALL THROUGH
245
246 case STARTING:
247 // clear auxiliary effect input buffer for next accumulation
248 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
249 memset(mConfig.inputCfg.buffer.raw,
250 0,
251 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
252 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700253 if (start_l() == NO_ERROR) {
254 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700255 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700256 } else {
257 mState = IDLE;
258 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800259 break;
260 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700261 if (stop_l() == NO_ERROR) {
262 mDisableWaitCnt = mMaxDisableWaitCnt;
263 } else {
264 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
265 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800266 mState = STOPPED;
267 break;
268 case STOPPED:
269 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
270 // turn off sequence.
271 if (--mDisableWaitCnt == 0) {
272 reset_l();
273 mState = IDLE;
274 }
275 break;
276 default: //IDLE , ACTIVE, DESTROYED
277 break;
278 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700279
280 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800281}
282
283void AudioFlinger::EffectModule::process()
284{
285 Mutex::Autolock _l(mLock);
286
Mikhail Naganov022b9952017-01-04 16:36:51 -0800287 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800288 return;
289 }
290
rago94a1ee82017-07-21 15:11:02 -0700291 // TODO: Implement multichannel effects; here outChannelCount == FCC_2 == 2
292 const uint32_t inChannelCount =
293 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
294 const uint32_t outChannelCount =
295 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
296 const bool auxType =
297 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
298
Andy Hungfa69ca32017-11-30 10:07:53 -0800299 // safeInputOutputSampleCount is 0 if the channel count between input and output
300 // buffers do not match. This prevents automatic accumulation or copying between the
301 // input and output effect buffers without an intermediary effect process.
302 // TODO: consider implementing channel conversion.
303 const size_t safeInputOutputSampleCount =
304 inChannelCount != outChannelCount ? 0
305 : outChannelCount * std::min(
306 mConfig.inputCfg.buffer.frameCount,
307 mConfig.outputCfg.buffer.frameCount);
308 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
309#ifdef FLOAT_EFFECT_CHAIN
310 accumulate_float(
311 mConfig.outputCfg.buffer.f32,
312 mConfig.inputCfg.buffer.f32,
313 safeInputOutputSampleCount);
314#else
315 accumulate_i16(
316 mConfig.outputCfg.buffer.s16,
317 mConfig.inputCfg.buffer.s16,
318 safeInputOutputSampleCount);
319#endif
320 };
321 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
322#ifdef FLOAT_EFFECT_CHAIN
323 memcpy(
324 mConfig.outputCfg.buffer.f32,
325 mConfig.inputCfg.buffer.f32,
326 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
327
328#else
329 memcpy(
330 mConfig.outputCfg.buffer.s16,
331 mConfig.inputCfg.buffer.s16,
332 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
333#endif
334 };
335
Eric Laurentca7cc822012-11-19 14:55:58 -0800336 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700337 int ret;
338 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700339 if (auxType) {
340 // We overwrite the aux input buffer here and clear after processing.
341 // Note that aux input buffers are format q4_27.
342#ifdef FLOAT_EFFECT_CHAIN
343 if (mSupportsFloat) {
344 // Do in-place float conversion for auxiliary effect input buffer.
345 static_assert(sizeof(float) <= sizeof(int32_t),
346 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
347
Andy Hungfa69ca32017-11-30 10:07:53 -0800348 memcpy_to_float_from_q4_27(
349 mConfig.inputCfg.buffer.f32,
350 mConfig.inputCfg.buffer.s32,
351 mConfig.inputCfg.buffer.frameCount);
352 } else
353#endif
354 {
355 memcpy_to_i16_from_q4_27(
356 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700357 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800358 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700359 }
rago94a1ee82017-07-21 15:11:02 -0700360 }
361#ifdef FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800362 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
363 if (!auxType) {
364 if (mInConversionBuffer.get() == nullptr) {
365 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
366 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700367 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800368 memcpy_to_i16_from_float(
369 mInConversionBuffer->audioBuffer()->s16,
370 mInBuffer->audioBuffer()->f32,
371 inChannelCount * mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700372 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800373 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
374 if (mOutConversionBuffer.get() == nullptr) {
375 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
376 goto data_bypass;
377 }
378 memcpy_to_i16_from_float(
379 mOutConversionBuffer->audioBuffer()->s16,
380 mOutBuffer->audioBuffer()->f32,
381 outChannelCount * mConfig.outputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700382 }
383 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800384#endif
385
Mikhail Naganov022b9952017-01-04 16:36:51 -0800386 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800387
388#ifdef FLOAT_EFFECT_CHAIN
389 if (!mSupportsFloat) { // convert output int16_t back to float.
390 memcpy_to_float_from_i16(
391 mOutBuffer->audioBuffer()->f32,
392 mOutConversionBuffer->audioBuffer()->s16,
393 outChannelCount * mConfig.outputCfg.buffer.frameCount);
394 }
rago94a1ee82017-07-21 15:11:02 -0700395#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700396 } else {
rago94a1ee82017-07-21 15:11:02 -0700397#ifdef FLOAT_EFFECT_CHAIN
398 data_bypass:
399#endif
400 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800401 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700402 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800403 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700404 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800405 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700406 }
407 }
408 ret = -ENODATA;
409 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800410
Eric Laurentca7cc822012-11-19 14:55:58 -0800411 // force transition to IDLE state when engine is ready
412 if (mState == STOPPED && ret == -ENODATA) {
413 mDisableWaitCnt = 1;
414 }
415
416 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700417 if (auxType) {
418 // input always q4_27 regardless of FLOAT_EFFECT_CHAIN.
419 const size_t size =
420 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
421 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800422 }
423 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700424 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800425 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
426 // If an insert effect is idle and input buffer is different from output buffer,
427 // accumulate input onto output
428 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800429 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
430 accumulateInputToOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800431 }
432 }
433}
434
435void AudioFlinger::EffectModule::reset_l()
436{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700437 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800438 return;
439 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700440 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800441}
442
443status_t AudioFlinger::EffectModule::configure()
444{
rago94a1ee82017-07-21 15:11:02 -0700445 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700446 status_t status;
447 sp<ThreadBase> thread;
448 uint32_t size;
449 audio_channel_mask_t channelMask;
450
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700451 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700452 status = NO_INIT;
453 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800454 }
455
Eric Laurentd0ebb532013-04-02 16:41:41 -0700456 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800457 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700458 status = DEAD_OBJECT;
459 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800460 }
461
462 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700463 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700464 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800465
466 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
467 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900468 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
469 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800470 } else {
471 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700472 // TODO: Update this logic when multichannel effects are implemented.
473 // For offloaded tracks consider mono output as stereo for proper effect initialization
474 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
475 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
476 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
477 ALOGV("Overriding effect input and output as STEREO");
478 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800479 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700480
rago94a1ee82017-07-21 15:11:02 -0700481 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
482 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800483 mConfig.inputCfg.samplingRate = thread->sampleRate();
484 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
485 mConfig.inputCfg.bufferProvider.cookie = NULL;
486 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
487 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
488 mConfig.outputCfg.bufferProvider.cookie = NULL;
489 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
490 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
491 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
492 // Insert effect:
493 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
494 // always overwrites output buffer: input buffer == output buffer
495 // - in other sessions:
496 // last effect in the chain accumulates in output buffer: input buffer != output buffer
497 // other effect: overwrites output buffer: input buffer == output buffer
498 // Auxiliary effect:
499 // accumulates in output buffer: input buffer != output buffer
500 // Therefore: accumulate <=> input buffer != output buffer
501 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
502 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
503 } else {
504 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
505 }
506 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
507 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
508 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
509 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
510
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700511 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800512 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
513
514 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700515 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700516 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
517 sizeof(effect_config_t),
518 &mConfig,
519 &size,
520 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700521 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800522 status = cmdStatus;
rago94a1ee82017-07-21 15:11:02 -0700523#ifdef FLOAT_EFFECT_CHAIN
524 mSupportsFloat = true;
525#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800526 }
rago94a1ee82017-07-21 15:11:02 -0700527#ifdef FLOAT_EFFECT_CHAIN
528 else {
529 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
530 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
531 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
532 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
533 sizeof(effect_config_t),
534 &mConfig,
535 &size,
536 &cmdStatus);
537 if (status == NO_ERROR) {
538 status = cmdStatus;
539 mSupportsFloat = false;
540 ALOGVV("config worked with 16 bit");
541 } else {
542 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800543 }
rago94a1ee82017-07-21 15:11:02 -0700544 }
545#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800546
rago94a1ee82017-07-21 15:11:02 -0700547 if (status == NO_ERROR) {
548 // Establish Buffer strategy
549 setInBuffer(mInBuffer);
550 setOutBuffer(mOutBuffer);
551
552 // Update visualizer latency
553 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
554 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
555 effect_param_t *p = (effect_param_t *)buf32;
556
557 p->psize = sizeof(uint32_t);
558 p->vsize = sizeof(uint32_t);
559 size = sizeof(int);
560 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
561
562 uint32_t latency = 0;
563 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
564 if (pbt != NULL) {
565 latency = pbt->latency_l();
566 }
567
568 *((int32_t *)p->data + 1)= latency;
569 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
570 sizeof(effect_param_t) + 8,
571 &buf32,
572 &size,
573 &cmdStatus);
574 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800575 }
576
577 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
578 (1000 * mConfig.outputCfg.buffer.frameCount);
579
Eric Laurentd0ebb532013-04-02 16:41:41 -0700580exit:
581 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700582 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800583 return status;
584}
585
586status_t AudioFlinger::EffectModule::init()
587{
588 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700589 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800590 return NO_INIT;
591 }
592 status_t cmdStatus;
593 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700594 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
595 0,
596 NULL,
597 &size,
598 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800599 if (status == 0) {
600 status = cmdStatus;
601 }
602 return status;
603}
604
Eric Laurent1b928682014-10-02 19:41:47 -0700605void AudioFlinger::EffectModule::addEffectToHal_l()
606{
607 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
608 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
609 sp<ThreadBase> thread = mThread.promote();
610 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700611 sp<StreamHalInterface> stream = thread->stream();
612 if (stream != 0) {
613 status_t result = stream->addEffect(mEffectInterface);
614 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700615 }
616 }
617 }
618}
619
Eric Laurentfa1e1232016-08-02 19:01:49 -0700620// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800621status_t AudioFlinger::EffectModule::start()
622{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700623 sp<EffectChain> chain;
624 status_t status;
625 {
626 Mutex::Autolock _l(mLock);
627 status = start_l();
628 if (status == NO_ERROR) {
629 chain = mChain.promote();
630 }
631 }
632 if (chain != 0) {
633 chain->resetVolume_l();
634 }
635 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800636}
637
638status_t AudioFlinger::EffectModule::start_l()
639{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700640 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800641 return NO_INIT;
642 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700643 if (mStatus != NO_ERROR) {
644 return mStatus;
645 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800646 status_t cmdStatus;
647 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700648 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
649 0,
650 NULL,
651 &size,
652 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800653 if (status == 0) {
654 status = cmdStatus;
655 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700656 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700657 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800658 }
659 return status;
660}
661
662status_t AudioFlinger::EffectModule::stop()
663{
664 Mutex::Autolock _l(mLock);
665 return stop_l();
666}
667
668status_t AudioFlinger::EffectModule::stop_l()
669{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700670 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800671 return NO_INIT;
672 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700673 if (mStatus != NO_ERROR) {
674 return mStatus;
675 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800676 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800677 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700678 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
679 0,
680 NULL,
681 &size,
682 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800683 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800684 status = cmdStatus;
685 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800686 if (status == NO_ERROR) {
687 status = remove_effect_from_hal_l();
688 }
689 return status;
690}
691
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800692// must be called with EffectChain::mLock held
693void AudioFlinger::EffectModule::release_l()
694{
695 if (mEffectInterface != 0) {
696 remove_effect_from_hal_l();
697 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800698 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800699 mEffectInterface.clear();
700 }
701}
702
Eric Laurentbfb1b832013-01-07 09:53:42 -0800703status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
704{
705 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
706 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800707 sp<ThreadBase> thread = mThread.promote();
708 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700709 sp<StreamHalInterface> stream = thread->stream();
710 if (stream != 0) {
711 status_t result = stream->removeEffect(mEffectInterface);
712 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800713 }
714 }
715 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800716 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800717}
718
Andy Hunge4a1d912016-08-17 14:11:13 -0700719// round up delta valid if value and divisor are positive.
720template <typename T>
721static T roundUpDelta(const T &value, const T &divisor) {
722 T remainder = value % divisor;
723 return remainder == 0 ? 0 : divisor - remainder;
724}
725
Eric Laurentca7cc822012-11-19 14:55:58 -0800726status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
727 uint32_t cmdSize,
728 void *pCmdData,
729 uint32_t *replySize,
730 void *pReplyData)
731{
732 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700733 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800734
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700735 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800736 return NO_INIT;
737 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700738 if (mStatus != NO_ERROR) {
739 return mStatus;
740 }
Andy Hung110bc952016-06-20 15:22:52 -0700741 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700742 (sizeof(effect_param_t) > cmdSize ||
743 ((effect_param_t *)pCmdData)->psize > cmdSize
744 - sizeof(effect_param_t))) {
745 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800746 android_errorWriteLog(0x534e4554, "33003822");
747 return -EINVAL;
748 }
749 if (cmdCode == EFFECT_CMD_GET_PARAM &&
750 (*replySize < sizeof(effect_param_t) ||
751 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
752 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700753 return -EINVAL;
754 }
ragoe2759072016-11-22 18:02:48 -0800755 if (cmdCode == EFFECT_CMD_GET_PARAM &&
756 (sizeof(effect_param_t) > *replySize
757 || ((effect_param_t *)pCmdData)->psize > *replySize
758 - sizeof(effect_param_t)
759 || ((effect_param_t *)pCmdData)->vsize > *replySize
760 - sizeof(effect_param_t)
761 - ((effect_param_t *)pCmdData)->psize
762 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
763 *replySize
764 - sizeof(effect_param_t)
765 - ((effect_param_t *)pCmdData)->psize
766 - ((effect_param_t *)pCmdData)->vsize)) {
767 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
768 android_errorWriteLog(0x534e4554, "32705438");
769 return -EINVAL;
770 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700771 if ((cmdCode == EFFECT_CMD_SET_PARAM
772 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
773 (sizeof(effect_param_t) > cmdSize
774 || ((effect_param_t *)pCmdData)->psize > cmdSize
775 - sizeof(effect_param_t)
776 || ((effect_param_t *)pCmdData)->vsize > cmdSize
777 - sizeof(effect_param_t)
778 - ((effect_param_t *)pCmdData)->psize
779 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
780 cmdSize
781 - sizeof(effect_param_t)
782 - ((effect_param_t *)pCmdData)->psize
783 - ((effect_param_t *)pCmdData)->vsize)) {
784 android_errorWriteLog(0x534e4554, "30204301");
785 return -EINVAL;
786 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700787 status_t status = mEffectInterface->command(cmdCode,
788 cmdSize,
789 pCmdData,
790 replySize,
791 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800792 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
793 uint32_t size = (replySize == NULL) ? 0 : *replySize;
794 for (size_t i = 1; i < mHandles.size(); i++) {
795 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800796 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800797 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
798 }
799 }
800 }
801 return status;
802}
803
804status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
805{
806 Mutex::Autolock _l(mLock);
807 return setEnabled_l(enabled);
808}
809
810// must be called with EffectModule::mLock held
811status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
812{
813
814 ALOGV("setEnabled %p enabled %d", this, enabled);
815
816 if (enabled != isEnabled()) {
817 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
818 if (enabled && status != NO_ERROR) {
819 return status;
820 }
821
822 switch (mState) {
823 // going from disabled to enabled
824 case IDLE:
825 mState = STARTING;
826 break;
827 case STOPPED:
828 mState = RESTART;
829 break;
830 case STOPPING:
831 mState = ACTIVE;
832 break;
833
834 // going from enabled to disabled
835 case RESTART:
836 mState = STOPPED;
837 break;
838 case STARTING:
839 mState = IDLE;
840 break;
841 case ACTIVE:
842 mState = STOPPING;
843 break;
844 case DESTROYED:
845 return NO_ERROR; // simply ignore as we are being destroyed
846 }
847 for (size_t i = 1; i < mHandles.size(); i++) {
848 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800849 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800850 h->setEnabled(enabled);
851 }
852 }
853 }
854 return NO_ERROR;
855}
856
857bool AudioFlinger::EffectModule::isEnabled() const
858{
859 switch (mState) {
860 case RESTART:
861 case STARTING:
862 case ACTIVE:
863 return true;
864 case IDLE:
865 case STOPPING:
866 case STOPPED:
867 case DESTROYED:
868 default:
869 return false;
870 }
871}
872
873bool AudioFlinger::EffectModule::isProcessEnabled() const
874{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700875 if (mStatus != NO_ERROR) {
876 return false;
877 }
878
Eric Laurentca7cc822012-11-19 14:55:58 -0800879 switch (mState) {
880 case RESTART:
881 case ACTIVE:
882 case STOPPING:
883 case STOPPED:
884 return true;
885 case IDLE:
886 case STARTING:
887 case DESTROYED:
888 default:
889 return false;
890 }
891}
892
Mikhail Naganov022b9952017-01-04 16:36:51 -0800893void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700894 ALOGVV("setInBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800895 if (buffer != 0) {
896 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
897 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
898 } else {
899 mConfig.inputCfg.buffer.raw = NULL;
900 }
901 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800902 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700903
904#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800905 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -0700906 // Theoretically insert effects can also do in-place conversions (destroying
907 // the original buffer) when the output buffer is identical to the input buffer,
908 // but we don't optimize for it here.
909 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
910 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
911 // we need to translate - create hidl shared buffer and intercept
912 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
913 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
914 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
915
916 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
917 __func__, inChannels, inFrameCount, size);
918
Andy Hungbded9c82017-11-30 18:47:35 -0800919 if (size > 0 && (mInConversionBuffer.get() == nullptr
920 || size > mInConversionBuffer->getSize())) {
921 mInConversionBuffer.clear();
922 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
923 (void)EffectBufferHalInterface::allocate(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700924 }
Andy Hungbded9c82017-11-30 18:47:35 -0800925 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -0800926 mInConversionBuffer->setFrameCount(inFrameCount);
927 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700928 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800929 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700930 }
931 }
932#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800933}
934
935void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700936 ALOGVV("setOutBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800937 if (buffer != 0) {
938 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
939 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
940 } else {
941 mConfig.outputCfg.buffer.raw = NULL;
942 }
943 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800944 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700945
946#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800947 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -0700948 // can do in-place conversion from int16_t to float. We don't optimize here.
949 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
950 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
951 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
952 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
953
954 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
955 __func__, outChannels, outFrameCount, size);
956
Andy Hungbded9c82017-11-30 18:47:35 -0800957 if (size > 0 && (mOutConversionBuffer.get() == nullptr
958 || size > mOutConversionBuffer->getSize())) {
959 mOutConversionBuffer.clear();
960 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
961 (void)EffectBufferHalInterface::allocate(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700962 }
Andy Hungbded9c82017-11-30 18:47:35 -0800963 if (mOutConversionBuffer.get() != nullptr) {
964 mOutConversionBuffer->setFrameCount(outFrameCount);
965 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700966 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800967 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700968 }
969 }
970#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800971}
972
Eric Laurentca7cc822012-11-19 14:55:58 -0800973status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
974{
975 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700976 if (mStatus != NO_ERROR) {
977 return mStatus;
978 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800979 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800980 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
981 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
982 if (isProcessEnabled() &&
983 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
984 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800985 uint32_t volume[2];
986 uint32_t *pVolume = NULL;
987 uint32_t size = sizeof(volume);
988 volume[0] = *left;
989 volume[1] = *right;
990 if (controller) {
991 pVolume = volume;
992 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700993 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
994 size,
995 volume,
996 &size,
997 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -0800998 if (controller && status == NO_ERROR && size == sizeof(volume)) {
999 *left = volume[0];
1000 *right = volume[1];
1001 }
1002 }
1003 return status;
1004}
1005
1006status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1007{
1008 if (device == AUDIO_DEVICE_NONE) {
1009 return NO_ERROR;
1010 }
1011
1012 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001013 if (mStatus != NO_ERROR) {
1014 return mStatus;
1015 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001016 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001017 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001018 status_t cmdStatus;
1019 uint32_t size = sizeof(status_t);
1020 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1021 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001022 status = mEffectInterface->command(cmd,
1023 sizeof(uint32_t),
1024 &device,
1025 &size,
1026 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001027 }
1028 return status;
1029}
1030
1031status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1032{
1033 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001034 if (mStatus != NO_ERROR) {
1035 return mStatus;
1036 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001037 status_t status = NO_ERROR;
1038 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1039 status_t cmdStatus;
1040 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001041 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1042 sizeof(audio_mode_t),
1043 &mode,
1044 &size,
1045 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001046 if (status == NO_ERROR) {
1047 status = cmdStatus;
1048 }
1049 }
1050 return status;
1051}
1052
1053status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1054{
1055 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001056 if (mStatus != NO_ERROR) {
1057 return mStatus;
1058 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001059 status_t status = NO_ERROR;
1060 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1061 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001062 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1063 sizeof(audio_source_t),
1064 &source,
1065 &size,
1066 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001067 }
1068 return status;
1069}
1070
1071void AudioFlinger::EffectModule::setSuspended(bool suspended)
1072{
1073 Mutex::Autolock _l(mLock);
1074 mSuspended = suspended;
1075}
1076
1077bool AudioFlinger::EffectModule::suspended() const
1078{
1079 Mutex::Autolock _l(mLock);
1080 return mSuspended;
1081}
1082
1083bool AudioFlinger::EffectModule::purgeHandles()
1084{
1085 bool enabled = false;
1086 Mutex::Autolock _l(mLock);
1087 for (size_t i = 0; i < mHandles.size(); i++) {
1088 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001089 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001090 if (handle->hasControl()) {
1091 enabled = handle->enabled();
1092 }
1093 }
1094 }
1095 return enabled;
1096}
1097
Eric Laurent5baf2af2013-09-12 17:37:00 -07001098status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1099{
1100 Mutex::Autolock _l(mLock);
1101 if (mStatus != NO_ERROR) {
1102 return mStatus;
1103 }
1104 status_t status = NO_ERROR;
1105 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1106 status_t cmdStatus;
1107 uint32_t size = sizeof(status_t);
1108 effect_offload_param_t cmd;
1109
1110 cmd.isOffload = offloaded;
1111 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001112 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1113 sizeof(effect_offload_param_t),
1114 &cmd,
1115 &size,
1116 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001117 if (status == NO_ERROR) {
1118 status = cmdStatus;
1119 }
1120 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1121 } else {
1122 if (offloaded) {
1123 status = INVALID_OPERATION;
1124 }
1125 mOffloaded = false;
1126 }
1127 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1128 return status;
1129}
1130
1131bool AudioFlinger::EffectModule::isOffloaded() const
1132{
1133 Mutex::Autolock _l(mLock);
1134 return mOffloaded;
1135}
1136
Marco Nelissenb2208842014-02-07 14:00:50 -08001137String8 effectFlagsToString(uint32_t flags) {
1138 String8 s;
1139
1140 s.append("conn. mode: ");
1141 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1142 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1143 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1144 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1145 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1146 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1147 default: s.append("unknown/reserved"); break;
1148 }
1149 s.append(", ");
1150
1151 s.append("insert pref: ");
1152 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1153 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1154 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1155 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1156 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1157 default: s.append("unknown/reserved"); break;
1158 }
1159 s.append(", ");
1160
1161 s.append("volume mgmt: ");
1162 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1163 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1164 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1165 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1166 default: s.append("unknown/reserved"); break;
1167 }
1168 s.append(", ");
1169
1170 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1171 if (devind) {
1172 s.append("device indication: ");
1173 switch (devind) {
1174 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1175 default: s.append("unknown/reserved"); break;
1176 }
1177 s.append(", ");
1178 }
1179
1180 s.append("input mode: ");
1181 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1182 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1183 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1184 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1185 default: s.append("not set"); break;
1186 }
1187 s.append(", ");
1188
1189 s.append("output mode: ");
1190 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1191 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1192 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1193 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1194 default: s.append("not set"); break;
1195 }
1196 s.append(", ");
1197
1198 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1199 if (accel) {
1200 s.append("hardware acceleration: ");
1201 switch (accel) {
1202 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1203 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1204 default: s.append("unknown/reserved"); break;
1205 }
1206 s.append(", ");
1207 }
1208
1209 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1210 if (modeind) {
1211 s.append("mode indication: ");
1212 switch (modeind) {
1213 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1214 default: s.append("unknown/reserved"); break;
1215 }
1216 s.append(", ");
1217 }
1218
1219 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1220 if (srcind) {
1221 s.append("source indication: ");
1222 switch (srcind) {
1223 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1224 default: s.append("unknown/reserved"); break;
1225 }
1226 s.append(", ");
1227 }
1228
1229 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1230 s.append("offloadable, ");
1231 }
1232
1233 int len = s.length();
1234 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001235 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001236 s.unlockBuffer(len - 2);
1237 }
1238 return s;
1239}
1240
Andy Hungbded9c82017-11-30 18:47:35 -08001241static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1242 std::stringstream ss;
1243
1244 if (buffer.get() == nullptr) {
1245 return "nullptr"; // make different than below
1246 } else if (buffer->externalData() != nullptr) {
1247 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1248 << " -> "
1249 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1250 } else {
1251 ss << buffer->audioBuffer()->raw;
1252 }
1253 return ss.str();
1254}
Marco Nelissenb2208842014-02-07 14:00:50 -08001255
Glenn Kasten0f11b512014-01-31 16:18:54 -08001256void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001257{
1258 const size_t SIZE = 256;
1259 char buffer[SIZE];
1260 String8 result;
1261
1262 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1263 result.append(buffer);
1264
1265 bool locked = AudioFlinger::dumpTryLock(mLock);
1266 // failed to lock - AudioFlinger is probably deadlocked
1267 if (!locked) {
1268 result.append("\t\tCould not lock Fx mutex:\n");
1269 }
1270
1271 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001272 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001273 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001274 result.append(buffer);
1275
1276 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001277 char uuidStr[64];
1278 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1279 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001280 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001281 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1282 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001283 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001284 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001285 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001286 mDescriptor.flags,
1287 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001288 result.append(buffer);
1289 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1290 mDescriptor.name);
1291 result.append(buffer);
1292 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1293 mDescriptor.implementor);
1294 result.append(buffer);
1295
1296 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001297 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001298 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001299 mConfig.inputCfg.buffer.frameCount,
1300 mConfig.inputCfg.samplingRate,
1301 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001302 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001303 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001304 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001305 result.append(buffer);
1306
1307 result.append("\t\t- Output configuration:\n");
1308 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001309 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001310 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001311 mConfig.outputCfg.buffer.frameCount,
1312 mConfig.outputCfg.samplingRate,
1313 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001314 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001315 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001316 result.append(buffer);
1317
rago94a1ee82017-07-21 15:11:02 -07001318#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001319
Andy Hungbded9c82017-11-30 18:47:35 -08001320 result.appendFormat("\t\t- HAL buffers:\n"
1321 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1322 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1323 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1324 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1325 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001326#endif
1327
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001328 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001329 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001330 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001331 for (size_t i = 0; i < mHandles.size(); ++i) {
1332 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001333 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001334 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001335 result.append(buffer);
1336 }
1337 }
1338
Eric Laurentca7cc822012-11-19 14:55:58 -08001339 write(fd, result.string(), result.length());
1340
1341 if (locked) {
1342 mLock.unlock();
1343 }
1344}
1345
1346// ----------------------------------------------------------------------------
1347// EffectHandle implementation
1348// ----------------------------------------------------------------------------
1349
1350#undef LOG_TAG
1351#define LOG_TAG "AudioFlinger::EffectHandle"
1352
1353AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1354 const sp<AudioFlinger::Client>& client,
1355 const sp<IEffectClient>& effectClient,
1356 int32_t priority)
1357 : BnEffect(),
1358 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001359 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001360{
1361 ALOGV("constructor %p", this);
1362
1363 if (client == 0) {
1364 return;
1365 }
1366 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1367 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001368 if (mCblkMemory == 0 ||
1369 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001370 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001371 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001372 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001373 return;
1374 }
Glenn Kastene75da402013-11-20 13:54:52 -08001375 new(mCblk) effect_param_cblk_t();
1376 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001377}
1378
1379AudioFlinger::EffectHandle::~EffectHandle()
1380{
1381 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001382 disconnect(false);
1383}
1384
Glenn Kastene75da402013-11-20 13:54:52 -08001385status_t AudioFlinger::EffectHandle::initCheck()
1386{
1387 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1388}
1389
Eric Laurentca7cc822012-11-19 14:55:58 -08001390status_t AudioFlinger::EffectHandle::enable()
1391{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001392 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001393 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001394 sp<EffectModule> effect = mEffect.promote();
1395 if (effect == 0 || mDisconnected) {
1396 return DEAD_OBJECT;
1397 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001398 if (!mHasControl) {
1399 return INVALID_OPERATION;
1400 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001401
1402 if (mEnabled) {
1403 return NO_ERROR;
1404 }
1405
1406 mEnabled = true;
1407
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001408 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001409 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001410 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001411 }
1412
1413 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001414 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001415 return NO_ERROR;
1416 }
1417
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001418 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 if (status != NO_ERROR) {
1420 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001421 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001422 }
1423 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001424 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001425 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001426 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1427 Mutex::Autolock _l(thread->mLock);
1428 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001429 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001430 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001431 if (thread->type() == ThreadBase::OFFLOAD) {
1432 PlaybackThread *t = (PlaybackThread *)thread.get();
1433 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1434 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001435 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001436 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1437 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001438 }
1439 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001440 }
1441 return status;
1442}
1443
1444status_t AudioFlinger::EffectHandle::disable()
1445{
1446 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001447 AutoMutex _l(mLock);
1448 sp<EffectModule> effect = mEffect.promote();
1449 if (effect == 0 || mDisconnected) {
1450 return DEAD_OBJECT;
1451 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001452 if (!mHasControl) {
1453 return INVALID_OPERATION;
1454 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001455
1456 if (!mEnabled) {
1457 return NO_ERROR;
1458 }
1459 mEnabled = false;
1460
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001461 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001462 return NO_ERROR;
1463 }
1464
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001465 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001466
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001467 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001468 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001469 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001470 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1471 Mutex::Autolock _l(thread->mLock);
1472 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001473 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001474 }
1475
1476 return status;
1477}
1478
1479void AudioFlinger::EffectHandle::disconnect()
1480{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001481 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 disconnect(true);
1483}
1484
1485void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1486{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001487 AutoMutex _l(mLock);
1488 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1489 if (mDisconnected) {
1490 if (unpinIfLast) {
1491 android_errorWriteLog(0x534e4554, "32707507");
1492 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001493 return;
1494 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001495 mDisconnected = true;
1496 sp<ThreadBase> thread;
1497 {
1498 sp<EffectModule> effect = mEffect.promote();
1499 if (effect != 0) {
1500 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001501 }
1502 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001503 if (thread != 0) {
1504 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001505 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001506 // try to cleanup as much as we can
1507 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001508 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1509 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001510 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001511 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001512
Eric Laurentca7cc822012-11-19 14:55:58 -08001513 if (mClient != 0) {
1514 if (mCblk != NULL) {
1515 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1516 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1517 }
1518 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001519 // Client destructor must run with AudioFlinger client mutex locked
1520 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001521 mClient.clear();
1522 }
1523}
1524
1525status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1526 uint32_t cmdSize,
1527 void *pCmdData,
1528 uint32_t *replySize,
1529 void *pReplyData)
1530{
1531 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001532 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001533
Eric Laurentc7ab3092017-06-15 18:43:46 -07001534 // reject commands reserved for internal use by audio framework if coming from outside
1535 // of audioserver
1536 switch(cmdCode) {
1537 case EFFECT_CMD_ENABLE:
1538 case EFFECT_CMD_DISABLE:
1539 case EFFECT_CMD_SET_PARAM:
1540 case EFFECT_CMD_SET_PARAM_DEFERRED:
1541 case EFFECT_CMD_SET_PARAM_COMMIT:
1542 case EFFECT_CMD_GET_PARAM:
1543 break;
1544 default:
1545 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1546 break;
1547 }
1548 android_errorWriteLog(0x534e4554, "62019992");
1549 return BAD_VALUE;
1550 }
1551
Eric Laurent1ffc5852016-12-15 14:46:09 -08001552 if (cmdCode == EFFECT_CMD_ENABLE) {
1553 if (*replySize < sizeof(int)) {
1554 android_errorWriteLog(0x534e4554, "32095713");
1555 return BAD_VALUE;
1556 }
1557 *(int *)pReplyData = NO_ERROR;
1558 *replySize = sizeof(int);
1559 return enable();
1560 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1561 if (*replySize < sizeof(int)) {
1562 android_errorWriteLog(0x534e4554, "32095713");
1563 return BAD_VALUE;
1564 }
1565 *(int *)pReplyData = NO_ERROR;
1566 *replySize = sizeof(int);
1567 return disable();
1568 }
1569
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001570 AutoMutex _l(mLock);
1571 sp<EffectModule> effect = mEffect.promote();
1572 if (effect == 0 || mDisconnected) {
1573 return DEAD_OBJECT;
1574 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001575 // only get parameter command is permitted for applications not controlling the effect
1576 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1577 return INVALID_OPERATION;
1578 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001579 if (mClient == 0) {
1580 return INVALID_OPERATION;
1581 }
1582
1583 // handle commands that are not forwarded transparently to effect engine
1584 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001585 if (*replySize < sizeof(int)) {
1586 android_errorWriteLog(0x534e4554, "32095713");
1587 return BAD_VALUE;
1588 }
1589 *(int *)pReplyData = NO_ERROR;
1590 *replySize = sizeof(int);
1591
Eric Laurentca7cc822012-11-19 14:55:58 -08001592 // No need to trylock() here as this function is executed in the binder thread serving a
1593 // particular client process: no risk to block the whole media server process or mixer
1594 // threads if we are stuck here
1595 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001596 // keep local copy of index in case of client corruption b/32220769
1597 const uint32_t clientIndex = mCblk->clientIndex;
1598 const uint32_t serverIndex = mCblk->serverIndex;
1599 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1600 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001601 mCblk->serverIndex = 0;
1602 mCblk->clientIndex = 0;
1603 return BAD_VALUE;
1604 }
1605 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001606 effect_param_t *param = NULL;
1607 for (uint32_t index = serverIndex; index < clientIndex;) {
1608 int *p = (int *)(mBuffer + index);
1609 const int size = *p++;
1610 if (size < 0
1611 || size > EFFECT_PARAM_BUFFER_SIZE
1612 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001613 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001614 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001615 break;
1616 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001617
1618 // copy to local memory in case of client corruption b/32220769
1619 param = (effect_param_t *)realloc(param, size);
1620 if (param == NULL) {
1621 ALOGW("command(): out of memory");
1622 status = NO_MEMORY;
1623 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001624 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001625 memcpy(param, p, size);
1626
1627 int reply = 0;
1628 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001629 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001630 size,
1631 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001632 &rsize,
1633 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001634
1635 // verify shared memory: server index shouldn't change; client index can't go back.
1636 if (serverIndex != mCblk->serverIndex
1637 || clientIndex > mCblk->clientIndex) {
1638 android_errorWriteLog(0x534e4554, "32220769");
1639 status = BAD_VALUE;
1640 break;
1641 }
1642
Eric Laurentca7cc822012-11-19 14:55:58 -08001643 // stop at first error encountered
1644 if (ret != NO_ERROR) {
1645 status = ret;
1646 *(int *)pReplyData = reply;
1647 break;
1648 } else if (reply != NO_ERROR) {
1649 *(int *)pReplyData = reply;
1650 break;
1651 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001652 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001653 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001654 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001655 mCblk->serverIndex = 0;
1656 mCblk->clientIndex = 0;
1657 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001658 }
1659
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001660 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001661}
1662
1663void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1664{
1665 ALOGV("setControl %p control %d", this, hasControl);
1666
1667 mHasControl = hasControl;
1668 mEnabled = enabled;
1669
1670 if (signal && mEffectClient != 0) {
1671 mEffectClient->controlStatusChanged(hasControl);
1672 }
1673}
1674
1675void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1676 uint32_t cmdSize,
1677 void *pCmdData,
1678 uint32_t replySize,
1679 void *pReplyData)
1680{
1681 if (mEffectClient != 0) {
1682 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1683 }
1684}
1685
1686
1687
1688void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1689{
1690 if (mEffectClient != 0) {
1691 mEffectClient->enableStatusChanged(enabled);
1692 }
1693}
1694
1695status_t AudioFlinger::EffectHandle::onTransact(
1696 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1697{
1698 return BnEffect::onTransact(code, data, reply, flags);
1699}
1700
1701
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001702void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001703{
1704 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1705
Marco Nelissenb2208842014-02-07 14:00:50 -08001706 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001707 (mClient == 0) ? getpid_cached : mClient->pid(),
1708 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001709 mHasControl ? "yes" : "no",
1710 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 mCblk ? mCblk->clientIndex : 0,
1712 mCblk ? mCblk->serverIndex : 0
1713 );
1714
1715 if (locked) {
1716 mCblk->lock.unlock();
1717 }
1718}
1719
1720#undef LOG_TAG
1721#define LOG_TAG "AudioFlinger::EffectChain"
1722
1723AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001724 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001725 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001726 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001727 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001728{
1729 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1730 if (thread == NULL) {
1731 return;
1732 }
1733 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1734 thread->frameCount();
1735}
1736
1737AudioFlinger::EffectChain::~EffectChain()
1738{
Eric Laurentca7cc822012-11-19 14:55:58 -08001739}
1740
1741// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1742sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1743 effect_descriptor_t *descriptor)
1744{
1745 size_t size = mEffects.size();
1746
1747 for (size_t i = 0; i < size; i++) {
1748 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1749 return mEffects[i];
1750 }
1751 }
1752 return 0;
1753}
1754
1755// getEffectFromId_l() must be called with ThreadBase::mLock held
1756sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1757{
1758 size_t size = mEffects.size();
1759
1760 for (size_t i = 0; i < size; i++) {
1761 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1762 if (id == 0 || mEffects[i]->id() == id) {
1763 return mEffects[i];
1764 }
1765 }
1766 return 0;
1767}
1768
1769// getEffectFromType_l() must be called with ThreadBase::mLock held
1770sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1771 const effect_uuid_t *type)
1772{
1773 size_t size = mEffects.size();
1774
1775 for (size_t i = 0; i < size; i++) {
1776 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1777 return mEffects[i];
1778 }
1779 }
1780 return 0;
1781}
1782
1783void AudioFlinger::EffectChain::clearInputBuffer()
1784{
1785 Mutex::Autolock _l(mLock);
1786 sp<ThreadBase> thread = mThread.promote();
1787 if (thread == 0) {
1788 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1789 return;
1790 }
1791 clearInputBuffer_l(thread);
1792}
1793
1794// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001795void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001796{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001797 if (mInBuffer == NULL) {
1798 return;
1799 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001800 // TODO: This will change in the future, depending on multichannel
1801 // and sample format changes for effects.
1802 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1803 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001804
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001805 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001806 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1807 * std::min((uint32_t)FCC_2, thread->channelCount());
1808
Mikhail Naganov022b9952017-01-04 16:36:51 -08001809 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1810 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001811}
1812
1813// Must be called with EffectChain::mLock locked
1814void AudioFlinger::EffectChain::process_l()
1815{
1816 sp<ThreadBase> thread = mThread.promote();
1817 if (thread == 0) {
1818 ALOGW("process_l(): cannot promote mixer thread");
1819 return;
1820 }
1821 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1822 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001823 // never process effects when:
1824 // - on an OFFLOAD thread
1825 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001826 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1827 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001828 if (!isGlobalSession) {
1829 bool tracksOnSession = (trackCnt() != 0);
1830
1831 if (!tracksOnSession && mTailBufferCount == 0) {
1832 doProcess = false;
1833 }
1834
1835 if (activeTrackCnt() == 0) {
1836 // if no track is active and the effect tail has not been rendered,
1837 // the input buffer must be cleared here as the mixer process will not do it
1838 if (tracksOnSession || mTailBufferCount > 0) {
1839 clearInputBuffer_l(thread);
1840 if (mTailBufferCount > 0) {
1841 mTailBufferCount--;
1842 }
1843 }
1844 }
1845 }
1846
1847 size_t size = mEffects.size();
1848 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001849 // Only the input and output buffers of the chain can be external,
1850 // and 'update' / 'commit' do nothing for allocated buffers, thus
1851 // it's not needed to consider any other buffers here.
1852 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001853 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1854 mOutBuffer->update();
1855 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001856 for (size_t i = 0; i < size; i++) {
1857 mEffects[i]->process();
1858 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001859 mInBuffer->commit();
1860 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1861 mOutBuffer->commit();
1862 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001863 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001864 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001866 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1867 }
1868 if (doResetVolume) {
1869 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001870 }
1871}
1872
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001873// createEffect_l() must be called with ThreadBase::mLock held
1874status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1875 ThreadBase *thread,
1876 effect_descriptor_t *desc,
1877 int id,
1878 audio_session_t sessionId,
1879 bool pinned)
1880{
1881 Mutex::Autolock _l(mLock);
1882 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1883 status_t lStatus = effect->status();
1884 if (lStatus == NO_ERROR) {
1885 lStatus = addEffect_ll(effect);
1886 }
1887 if (lStatus != NO_ERROR) {
1888 effect.clear();
1889 }
1890 return lStatus;
1891}
1892
1893// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001894status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1895{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001896 Mutex::Autolock _l(mLock);
1897 return addEffect_ll(effect);
1898}
1899// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1900status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1901{
Eric Laurentca7cc822012-11-19 14:55:58 -08001902 effect_descriptor_t desc = effect->desc();
1903 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1904
Eric Laurentca7cc822012-11-19 14:55:58 -08001905 effect->setChain(this);
1906 sp<ThreadBase> thread = mThread.promote();
1907 if (thread == 0) {
1908 return NO_INIT;
1909 }
1910 effect->setThread(thread);
1911
1912 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1913 // Auxiliary effects are inserted at the beginning of mEffects vector as
1914 // they are processed first and accumulated in chain input buffer
1915 mEffects.insertAt(effect, 0);
1916
1917 // the input buffer for auxiliary effect contains mono samples in
1918 // 32 bit format. This is to avoid saturation in AudoMixer
1919 // accumulation stage. Saturation is done in EffectModule::process() before
1920 // calling the process in effect engine
1921 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001922 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001923#ifdef FLOAT_EFFECT_CHAIN
1924 status_t result = EffectBufferHalInterface::allocate(
1925 numSamples * sizeof(float), &halBuffer);
1926#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001927 status_t result = EffectBufferHalInterface::allocate(
1928 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001929#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001930 if (result != OK) return result;
1931 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001932 // auxiliary effects output samples to chain input buffer for further processing
1933 // by insert effects
1934 effect->setOutBuffer(mInBuffer);
1935 } else {
1936 // Insert effects are inserted at the end of mEffects vector as they are processed
1937 // after track and auxiliary effects.
1938 // Insert effect order as a function of indicated preference:
1939 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1940 // another effect is present
1941 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1942 // last effect claiming first position
1943 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1944 // first effect claiming last position
1945 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1946 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1947 // already present
1948
1949 size_t size = mEffects.size();
1950 size_t idx_insert = size;
1951 ssize_t idx_insert_first = -1;
1952 ssize_t idx_insert_last = -1;
1953
1954 for (size_t i = 0; i < size; i++) {
1955 effect_descriptor_t d = mEffects[i]->desc();
1956 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1957 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1958 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1959 // check invalid effect chaining combinations
1960 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1961 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1962 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1963 desc.name, d.name);
1964 return INVALID_OPERATION;
1965 }
1966 // remember position of first insert effect and by default
1967 // select this as insert position for new effect
1968 if (idx_insert == size) {
1969 idx_insert = i;
1970 }
1971 // remember position of last insert effect claiming
1972 // first position
1973 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1974 idx_insert_first = i;
1975 }
1976 // remember position of first insert effect claiming
1977 // last position
1978 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1979 idx_insert_last == -1) {
1980 idx_insert_last = i;
1981 }
1982 }
1983 }
1984
1985 // modify idx_insert from first position if needed
1986 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1987 if (idx_insert_last != -1) {
1988 idx_insert = idx_insert_last;
1989 } else {
1990 idx_insert = size;
1991 }
1992 } else {
1993 if (idx_insert_first != -1) {
1994 idx_insert = idx_insert_first + 1;
1995 }
1996 }
1997
1998 // always read samples from chain input buffer
1999 effect->setInBuffer(mInBuffer);
2000
2001 // if last effect in the chain, output samples to chain
2002 // output buffer, otherwise to chain input buffer
2003 if (idx_insert == size) {
2004 if (idx_insert != 0) {
2005 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2006 mEffects[idx_insert-1]->configure();
2007 }
2008 effect->setOutBuffer(mOutBuffer);
2009 } else {
2010 effect->setOutBuffer(mInBuffer);
2011 }
2012 mEffects.insertAt(effect, idx_insert);
2013
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002014 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002015 idx_insert);
2016 }
2017 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002018
Eric Laurentca7cc822012-11-19 14:55:58 -08002019 return NO_ERROR;
2020}
2021
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002022// removeEffect_l() must be called with ThreadBase::mLock held
2023size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2024 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002025{
2026 Mutex::Autolock _l(mLock);
2027 size_t size = mEffects.size();
2028 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2029
2030 for (size_t i = 0; i < size; i++) {
2031 if (effect == mEffects[i]) {
2032 // calling stop here will remove pre-processing effect from the audio HAL.
2033 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2034 // the middle of a read from audio HAL
2035 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2036 mEffects[i]->state() == EffectModule::STOPPING) {
2037 mEffects[i]->stop();
2038 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002039 if (release) {
2040 mEffects[i]->release_l();
2041 }
2042
Mikhail Naganov022b9952017-01-04 16:36:51 -08002043 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002044 if (i == size - 1 && i != 0) {
2045 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2046 mEffects[i - 1]->configure();
2047 }
2048 }
2049 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002050 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002051 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002052
Eric Laurentca7cc822012-11-19 14:55:58 -08002053 break;
2054 }
2055 }
2056
2057 return mEffects.size();
2058}
2059
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002060// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002061void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2062{
2063 size_t size = mEffects.size();
2064 for (size_t i = 0; i < size; i++) {
2065 mEffects[i]->setDevice(device);
2066 }
2067}
2068
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002069// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002070void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2071{
2072 size_t size = mEffects.size();
2073 for (size_t i = 0; i < size; i++) {
2074 mEffects[i]->setMode(mode);
2075 }
2076}
2077
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002078// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002079void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2080{
2081 size_t size = mEffects.size();
2082 for (size_t i = 0; i < size; i++) {
2083 mEffects[i]->setAudioSource(source);
2084 }
2085}
2086
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002087// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002088bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002089{
2090 uint32_t newLeft = *left;
2091 uint32_t newRight = *right;
2092 bool hasControl = false;
2093 int ctrlIdx = -1;
2094 size_t size = mEffects.size();
2095
2096 // first update volume controller
2097 for (size_t i = size; i > 0; i--) {
2098 if (mEffects[i - 1]->isProcessEnabled() &&
2099 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2100 ctrlIdx = i - 1;
2101 hasControl = true;
2102 break;
2103 }
2104 }
2105
Eric Laurentfa1e1232016-08-02 19:01:49 -07002106 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002107 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002108 if (hasControl) {
2109 *left = mNewLeftVolume;
2110 *right = mNewRightVolume;
2111 }
2112 return hasControl;
2113 }
2114
2115 mVolumeCtrlIdx = ctrlIdx;
2116 mLeftVolume = newLeft;
2117 mRightVolume = newRight;
2118
2119 // second get volume update from volume controller
2120 if (ctrlIdx >= 0) {
2121 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2122 mNewLeftVolume = newLeft;
2123 mNewRightVolume = newRight;
2124 }
2125 // then indicate volume to all other effects in chain.
2126 // Pass altered volume to effects before volume controller
2127 // and requested volume to effects after controller
2128 uint32_t lVol = newLeft;
2129 uint32_t rVol = newRight;
2130
2131 for (size_t i = 0; i < size; i++) {
2132 if ((int)i == ctrlIdx) {
2133 continue;
2134 }
2135 // this also works for ctrlIdx == -1 when there is no volume controller
2136 if ((int)i > ctrlIdx) {
2137 lVol = *left;
2138 rVol = *right;
2139 }
2140 mEffects[i]->setVolume(&lVol, &rVol, false);
2141 }
2142 *left = newLeft;
2143 *right = newRight;
2144
2145 return hasControl;
2146}
2147
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002148// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002149void AudioFlinger::EffectChain::resetVolume_l()
2150{
Eric Laurente7449bf2016-08-03 18:44:07 -07002151 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2152 uint32_t left = mLeftVolume;
2153 uint32_t right = mRightVolume;
2154 (void)setVolume_l(&left, &right, true);
2155 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002156}
2157
Eric Laurent1b928682014-10-02 19:41:47 -07002158void AudioFlinger::EffectChain::syncHalEffectsState()
2159{
2160 Mutex::Autolock _l(mLock);
2161 for (size_t i = 0; i < mEffects.size(); i++) {
2162 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2163 mEffects[i]->state() == EffectModule::STOPPING) {
2164 mEffects[i]->addEffectToHal_l();
2165 }
2166 }
2167}
2168
Eric Laurentca7cc822012-11-19 14:55:58 -08002169void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2170{
2171 const size_t SIZE = 256;
2172 char buffer[SIZE];
2173 String8 result;
2174
Marco Nelissenb2208842014-02-07 14:00:50 -08002175 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002176 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002177 result.append(buffer);
2178
Marco Nelissenb2208842014-02-07 14:00:50 -08002179 if (numEffects) {
2180 bool locked = AudioFlinger::dumpTryLock(mLock);
2181 // failed to lock - AudioFlinger is probably deadlocked
2182 if (!locked) {
2183 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002184 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002185
Andy Hungbded9c82017-11-30 18:47:35 -08002186 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2187 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2188 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2189 (int)inBufferStr.size(), "In buffer ",
2190 (int)outBufferStr.size(), "Out buffer ");
2191 result.appendFormat("\t%s %s %d\n",
2192 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002193 write(fd, result.string(), result.size());
2194
2195 for (size_t i = 0; i < numEffects; ++i) {
2196 sp<EffectModule> effect = mEffects[i];
2197 if (effect != 0) {
2198 effect->dump(fd, args);
2199 }
2200 }
2201
2202 if (locked) {
2203 mLock.unlock();
2204 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002205 }
2206}
2207
2208// must be called with ThreadBase::mLock held
2209void AudioFlinger::EffectChain::setEffectSuspended_l(
2210 const effect_uuid_t *type, bool suspend)
2211{
2212 sp<SuspendedEffectDesc> desc;
2213 // use effect type UUID timelow as key as there is no real risk of identical
2214 // timeLow fields among effect type UUIDs.
2215 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2216 if (suspend) {
2217 if (index >= 0) {
2218 desc = mSuspendedEffects.valueAt(index);
2219 } else {
2220 desc = new SuspendedEffectDesc();
2221 desc->mType = *type;
2222 mSuspendedEffects.add(type->timeLow, desc);
2223 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2224 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002225
Eric Laurentca7cc822012-11-19 14:55:58 -08002226 if (desc->mRefCount++ == 0) {
2227 sp<EffectModule> effect = getEffectIfEnabled(type);
2228 if (effect != 0) {
2229 desc->mEffect = effect;
2230 effect->setSuspended(true);
2231 effect->setEnabled(false);
2232 }
2233 }
2234 } else {
2235 if (index < 0) {
2236 return;
2237 }
2238 desc = mSuspendedEffects.valueAt(index);
2239 if (desc->mRefCount <= 0) {
2240 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002241 desc->mRefCount = 0;
2242 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002243 }
2244 if (--desc->mRefCount == 0) {
2245 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2246 if (desc->mEffect != 0) {
2247 sp<EffectModule> effect = desc->mEffect.promote();
2248 if (effect != 0) {
2249 effect->setSuspended(false);
2250 effect->lock();
2251 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002252 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002253 effect->setEnabled_l(handle->enabled());
2254 }
2255 effect->unlock();
2256 }
2257 desc->mEffect.clear();
2258 }
2259 mSuspendedEffects.removeItemsAt(index);
2260 }
2261 }
2262}
2263
2264// must be called with ThreadBase::mLock held
2265void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2266{
2267 sp<SuspendedEffectDesc> desc;
2268
2269 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2270 if (suspend) {
2271 if (index >= 0) {
2272 desc = mSuspendedEffects.valueAt(index);
2273 } else {
2274 desc = new SuspendedEffectDesc();
2275 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2276 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2277 }
2278 if (desc->mRefCount++ == 0) {
2279 Vector< sp<EffectModule> > effects;
2280 getSuspendEligibleEffects(effects);
2281 for (size_t i = 0; i < effects.size(); i++) {
2282 setEffectSuspended_l(&effects[i]->desc().type, true);
2283 }
2284 }
2285 } else {
2286 if (index < 0) {
2287 return;
2288 }
2289 desc = mSuspendedEffects.valueAt(index);
2290 if (desc->mRefCount <= 0) {
2291 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2292 desc->mRefCount = 1;
2293 }
2294 if (--desc->mRefCount == 0) {
2295 Vector<const effect_uuid_t *> types;
2296 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2297 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2298 continue;
2299 }
2300 types.add(&mSuspendedEffects.valueAt(i)->mType);
2301 }
2302 for (size_t i = 0; i < types.size(); i++) {
2303 setEffectSuspended_l(types[i], false);
2304 }
2305 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2306 mSuspendedEffects.keyAt(index));
2307 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2308 }
2309 }
2310}
2311
2312
2313// The volume effect is used for automated tests only
2314#ifndef OPENSL_ES_H_
2315static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2316 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2317const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2318#endif //OPENSL_ES_H_
2319
Eric Laurentd8365c52017-07-16 15:27:05 -07002320/* static */
2321bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2322{
2323 // Only NS and AEC are suspended when BtNRec is off
2324 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2325 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2326 return true;
2327 }
2328 return false;
2329}
2330
Eric Laurentca7cc822012-11-19 14:55:58 -08002331bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2332{
2333 // auxiliary effects and visualizer are never suspended on output mix
2334 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2335 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2336 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2337 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2338 return false;
2339 }
2340 return true;
2341}
2342
2343void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2344 Vector< sp<AudioFlinger::EffectModule> > &effects)
2345{
2346 effects.clear();
2347 for (size_t i = 0; i < mEffects.size(); i++) {
2348 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2349 effects.add(mEffects[i]);
2350 }
2351 }
2352}
2353
2354sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2355 const effect_uuid_t *type)
2356{
2357 sp<EffectModule> effect = getEffectFromType_l(type);
2358 return effect != 0 && effect->isEnabled() ? effect : 0;
2359}
2360
2361void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2362 bool enabled)
2363{
2364 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2365 if (enabled) {
2366 if (index < 0) {
2367 // if the effect is not suspend check if all effects are suspended
2368 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2369 if (index < 0) {
2370 return;
2371 }
2372 if (!isEffectEligibleForSuspend(effect->desc())) {
2373 return;
2374 }
2375 setEffectSuspended_l(&effect->desc().type, enabled);
2376 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2377 if (index < 0) {
2378 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2379 return;
2380 }
2381 }
2382 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2383 effect->desc().type.timeLow);
2384 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002385 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002386 if (desc->mEffect == 0) {
2387 desc->mEffect = effect;
2388 effect->setEnabled(false);
2389 effect->setSuspended(true);
2390 }
2391 } else {
2392 if (index < 0) {
2393 return;
2394 }
2395 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2396 effect->desc().type.timeLow);
2397 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2398 desc->mEffect.clear();
2399 effect->setSuspended(false);
2400 }
2401}
2402
Eric Laurent5baf2af2013-09-12 17:37:00 -07002403bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002404{
2405 Mutex::Autolock _l(mLock);
2406 size_t size = mEffects.size();
2407 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002408 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002409 return true;
2410 }
2411 }
2412 return false;
2413}
2414
Eric Laurentaaa44472014-09-12 17:41:50 -07002415void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2416{
2417 Mutex::Autolock _l(mLock);
2418 mThread = thread;
2419 for (size_t i = 0; i < mEffects.size(); i++) {
2420 mEffects[i]->setThread(thread);
2421 }
2422}
2423
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002424void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2425{
2426 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2427 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2428 }
2429 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2430 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2431 }
2432}
2433
2434void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2435{
2436 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2437 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2438 }
2439 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2440 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2441 }
2442}
2443
2444bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002445{
2446 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002447 for (const auto &effect : mEffects) {
2448 if (effect->isProcessImplemented()) {
2449 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002450 }
2451 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002452 // Allow effects without processing.
2453 return true;
2454}
2455
2456bool AudioFlinger::EffectChain::isFastCompatible() const
2457{
2458 Mutex::Autolock _l(mLock);
2459 for (const auto &effect : mEffects) {
2460 if (effect->isProcessImplemented()
2461 && effect->isImplementationSoftware()) {
2462 return false;
2463 }
2464 }
2465 // Allow effects without processing or hw accelerated effects.
2466 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002467}
2468
2469// isCompatibleWithThread_l() must be called with thread->mLock held
2470bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2471{
2472 Mutex::Autolock _l(mLock);
2473 for (size_t i = 0; i < mEffects.size(); i++) {
2474 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2475 return false;
2476 }
2477 }
2478 return true;
2479}
2480
Glenn Kasten63238ef2015-03-02 15:50:29 -08002481} // namespace android