blob: e77907ad15cef6f9b609c913e57d789e38f0d230 [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
Eric Laurentca7cc822012-11-19 14:55:58 -0800299 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700300 int ret;
301 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700302 if (auxType) {
303 // We overwrite the aux input buffer here and clear after processing.
304 // Note that aux input buffers are format q4_27.
305#ifdef FLOAT_EFFECT_CHAIN
306 if (mSupportsFloat) {
307 // Do in-place float conversion for auxiliary effect input buffer.
308 static_assert(sizeof(float) <= sizeof(int32_t),
309 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
310
311 const int32_t * const p32 = mConfig.inputCfg.buffer.s32;
312 float * const pFloat = mConfig.inputCfg.buffer.f32;
313 memcpy_to_float_from_q4_27(pFloat, p32, mConfig.inputCfg.buffer.frameCount);
314 } else {
Andy Hung5effdf62017-11-27 13:51:40 -0800315 memcpy_to_i16_from_q4_27(mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700316 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800317 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700318 }
319#else
Andy Hung5effdf62017-11-27 13:51:40 -0800320 memcpy_to_i16_from_q4_27(mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700321 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800322 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700323#endif
324 }
325#ifdef FLOAT_EFFECT_CHAIN
326 if (mSupportsFloat) {
327 ret = mEffectInterface->process();
328 } else {
329 { // convert input to int16_t as effect doesn't support float.
330 if (!auxType) {
331 if (mInBuffer16.get() == nullptr) {
332 ALOGW("%s: mInBuffer16 is null, bypassing", __func__);
333 goto data_bypass;
334 }
335 const float * const pIn = mInBuffer->audioBuffer()->f32;
336 int16_t * const pIn16 = mInBuffer16->audioBuffer()->s16;
337 memcpy_to_i16_from_float(
338 pIn16, pIn, inChannelCount * mConfig.inputCfg.buffer.frameCount);
339 }
340 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
341 if (mOutBuffer16.get() == nullptr) {
342 ALOGW("%s: mOutBuffer16 is null, bypassing", __func__);
343 goto data_bypass;
344 }
345 int16_t * const pOut16 = mOutBuffer16->audioBuffer()->s16;
346 const float * const pOut = mOutBuffer->audioBuffer()->f32;
347 memcpy_to_i16_from_float(
348 pOut16,
349 pOut,
350 outChannelCount * mConfig.outputCfg.buffer.frameCount);
351 }
352 }
353
354 ret = mEffectInterface->process();
355
356 { // convert output back to float.
357 const int16_t * const pOut16 = mOutBuffer16->audioBuffer()->s16;
358 float * const pOut = mOutBuffer->audioBuffer()->f32;
359 memcpy_to_float_from_i16(
360 pOut, pOut16, outChannelCount * mConfig.outputCfg.buffer.frameCount);
361 }
362 }
363#else
Mikhail Naganov022b9952017-01-04 16:36:51 -0800364 ret = mEffectInterface->process();
rago94a1ee82017-07-21 15:11:02 -0700365#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700366 } else {
rago94a1ee82017-07-21 15:11:02 -0700367#ifdef FLOAT_EFFECT_CHAIN
368 data_bypass:
369#endif
370 if (!auxType /* aux effects do not require data bypass */
371 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw
372 && inChannelCount == outChannelCount) {
373 const size_t sampleCount = std::min(
374 mConfig.inputCfg.buffer.frameCount,
375 mConfig.outputCfg.buffer.frameCount) * outChannelCount;
376
377#ifdef FLOAT_EFFECT_CHAIN
378 const float * const in = mConfig.inputCfg.buffer.f32;
379 float * const out = mConfig.outputCfg.buffer.f32;
Eric Laurentca7cc822012-11-19 14:55:58 -0800380
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700381 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
rago94a1ee82017-07-21 15:11:02 -0700382 accumulate_float(out, in, sampleCount);
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700383 } else {
rago94a1ee82017-07-21 15:11:02 -0700384 memcpy(mConfig.outputCfg.buffer.f32, mConfig.inputCfg.buffer.f32,
385 sampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700386 }
rago94a1ee82017-07-21 15:11:02 -0700387
388#else
389 const int16_t * const in = mConfig.inputCfg.buffer.s16;
390 int16_t * const out = mConfig.outputCfg.buffer.s16;
391
392 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
393 accumulate_i16(out, in, sampleCount);
394 } else {
395 memcpy(mConfig.outputCfg.buffer.s16, mConfig.inputCfg.buffer.s16,
396 sampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
397 }
398#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700399 }
400 ret = -ENODATA;
401 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800402 // force transition to IDLE state when engine is ready
403 if (mState == STOPPED && ret == -ENODATA) {
404 mDisableWaitCnt = 1;
405 }
406
407 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700408 if (auxType) {
409 // input always q4_27 regardless of FLOAT_EFFECT_CHAIN.
410 const size_t size =
411 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
412 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800413 }
414 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700415 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800416 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
417 // If an insert effect is idle and input buffer is different from output buffer,
418 // accumulate input onto output
419 sp<EffectChain> chain = mChain.promote();
rago94a1ee82017-07-21 15:11:02 -0700420 if (chain != 0
421 && chain->activeTrackCnt() != 0
422 && inChannelCount == outChannelCount) {
423 const size_t sampleCount = std::min(
424 mConfig.inputCfg.buffer.frameCount,
425 mConfig.outputCfg.buffer.frameCount) * outChannelCount;
426#ifdef FLOAT_EFFECT_CHAIN
427 const float * const in = mConfig.inputCfg.buffer.f32;
428 float * const out = mConfig.outputCfg.buffer.f32;
429 accumulate_float(out, in, sampleCount);
430#else
431 const int16_t * const in = mConfig.inputCfg.buffer.s16;
432 int16_t * const out = mConfig.outputCfg.buffer.s16;
433 accumulate_i16(out, in, sampleCount);
434#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800435 }
436 }
437}
438
439void AudioFlinger::EffectModule::reset_l()
440{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700441 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800442 return;
443 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700444 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800445}
446
447status_t AudioFlinger::EffectModule::configure()
448{
rago94a1ee82017-07-21 15:11:02 -0700449 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700450 status_t status;
451 sp<ThreadBase> thread;
452 uint32_t size;
453 audio_channel_mask_t channelMask;
454
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700455 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700456 status = NO_INIT;
457 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800458 }
459
Eric Laurentd0ebb532013-04-02 16:41:41 -0700460 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800461 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700462 status = DEAD_OBJECT;
463 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800464 }
465
466 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700467 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700468 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800469
470 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
471 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900472 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
473 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800474 } else {
475 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700476 // TODO: Update this logic when multichannel effects are implemented.
477 // For offloaded tracks consider mono output as stereo for proper effect initialization
478 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
479 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
480 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
481 ALOGV("Overriding effect input and output as STEREO");
482 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800483 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700484
rago94a1ee82017-07-21 15:11:02 -0700485 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
486 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800487 mConfig.inputCfg.samplingRate = thread->sampleRate();
488 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
489 mConfig.inputCfg.bufferProvider.cookie = NULL;
490 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
491 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
492 mConfig.outputCfg.bufferProvider.cookie = NULL;
493 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
494 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
495 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
496 // Insert effect:
497 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
498 // always overwrites output buffer: input buffer == output buffer
499 // - in other sessions:
500 // last effect in the chain accumulates in output buffer: input buffer != output buffer
501 // other effect: overwrites output buffer: input buffer == output buffer
502 // Auxiliary effect:
503 // accumulates in output buffer: input buffer != output buffer
504 // Therefore: accumulate <=> input buffer != output buffer
505 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
506 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
507 } else {
508 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
509 }
510 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
511 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
512 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
513 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
514
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700515 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800516 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
517
518 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700519 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700520 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
521 sizeof(effect_config_t),
522 &mConfig,
523 &size,
524 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700525 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800526 status = cmdStatus;
rago94a1ee82017-07-21 15:11:02 -0700527#ifdef FLOAT_EFFECT_CHAIN
528 mSupportsFloat = true;
529#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800530 }
rago94a1ee82017-07-21 15:11:02 -0700531#ifdef FLOAT_EFFECT_CHAIN
532 else {
533 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
534 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
535 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
536 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
537 sizeof(effect_config_t),
538 &mConfig,
539 &size,
540 &cmdStatus);
541 if (status == NO_ERROR) {
542 status = cmdStatus;
543 mSupportsFloat = false;
544 ALOGVV("config worked with 16 bit");
545 } else {
546 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800547 }
rago94a1ee82017-07-21 15:11:02 -0700548 }
549#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800550
rago94a1ee82017-07-21 15:11:02 -0700551 if (status == NO_ERROR) {
552 // Establish Buffer strategy
553 setInBuffer(mInBuffer);
554 setOutBuffer(mOutBuffer);
555
556 // Update visualizer latency
557 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
558 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
559 effect_param_t *p = (effect_param_t *)buf32;
560
561 p->psize = sizeof(uint32_t);
562 p->vsize = sizeof(uint32_t);
563 size = sizeof(int);
564 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
565
566 uint32_t latency = 0;
567 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
568 if (pbt != NULL) {
569 latency = pbt->latency_l();
570 }
571
572 *((int32_t *)p->data + 1)= latency;
573 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
574 sizeof(effect_param_t) + 8,
575 &buf32,
576 &size,
577 &cmdStatus);
578 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 }
580
581 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
582 (1000 * mConfig.outputCfg.buffer.frameCount);
583
Eric Laurentd0ebb532013-04-02 16:41:41 -0700584exit:
585 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700586 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800587 return status;
588}
589
590status_t AudioFlinger::EffectModule::init()
591{
592 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700593 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800594 return NO_INIT;
595 }
596 status_t cmdStatus;
597 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700598 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
599 0,
600 NULL,
601 &size,
602 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800603 if (status == 0) {
604 status = cmdStatus;
605 }
606 return status;
607}
608
Eric Laurent1b928682014-10-02 19:41:47 -0700609void AudioFlinger::EffectModule::addEffectToHal_l()
610{
611 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
612 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
613 sp<ThreadBase> thread = mThread.promote();
614 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700615 sp<StreamHalInterface> stream = thread->stream();
616 if (stream != 0) {
617 status_t result = stream->addEffect(mEffectInterface);
618 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700619 }
620 }
621 }
622}
623
Eric Laurentfa1e1232016-08-02 19:01:49 -0700624// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800625status_t AudioFlinger::EffectModule::start()
626{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700627 sp<EffectChain> chain;
628 status_t status;
629 {
630 Mutex::Autolock _l(mLock);
631 status = start_l();
632 if (status == NO_ERROR) {
633 chain = mChain.promote();
634 }
635 }
636 if (chain != 0) {
637 chain->resetVolume_l();
638 }
639 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800640}
641
642status_t AudioFlinger::EffectModule::start_l()
643{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700644 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800645 return NO_INIT;
646 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700647 if (mStatus != NO_ERROR) {
648 return mStatus;
649 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800650 status_t cmdStatus;
651 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700652 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
653 0,
654 NULL,
655 &size,
656 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800657 if (status == 0) {
658 status = cmdStatus;
659 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700660 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700661 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800662 }
663 return status;
664}
665
666status_t AudioFlinger::EffectModule::stop()
667{
668 Mutex::Autolock _l(mLock);
669 return stop_l();
670}
671
672status_t AudioFlinger::EffectModule::stop_l()
673{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700674 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800675 return NO_INIT;
676 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700677 if (mStatus != NO_ERROR) {
678 return mStatus;
679 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800680 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800681 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700682 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
683 0,
684 NULL,
685 &size,
686 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800687 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800688 status = cmdStatus;
689 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800690 if (status == NO_ERROR) {
691 status = remove_effect_from_hal_l();
692 }
693 return status;
694}
695
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800696// must be called with EffectChain::mLock held
697void AudioFlinger::EffectModule::release_l()
698{
699 if (mEffectInterface != 0) {
700 remove_effect_from_hal_l();
701 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800702 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800703 mEffectInterface.clear();
704 }
705}
706
Eric Laurentbfb1b832013-01-07 09:53:42 -0800707status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
708{
709 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
710 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800711 sp<ThreadBase> thread = mThread.promote();
712 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700713 sp<StreamHalInterface> stream = thread->stream();
714 if (stream != 0) {
715 status_t result = stream->removeEffect(mEffectInterface);
716 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800717 }
718 }
719 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800720 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800721}
722
Andy Hunge4a1d912016-08-17 14:11:13 -0700723// round up delta valid if value and divisor are positive.
724template <typename T>
725static T roundUpDelta(const T &value, const T &divisor) {
726 T remainder = value % divisor;
727 return remainder == 0 ? 0 : divisor - remainder;
728}
729
Eric Laurentca7cc822012-11-19 14:55:58 -0800730status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
731 uint32_t cmdSize,
732 void *pCmdData,
733 uint32_t *replySize,
734 void *pReplyData)
735{
736 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700737 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800738
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700739 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800740 return NO_INIT;
741 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700742 if (mStatus != NO_ERROR) {
743 return mStatus;
744 }
Andy Hung110bc952016-06-20 15:22:52 -0700745 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700746 (sizeof(effect_param_t) > cmdSize ||
747 ((effect_param_t *)pCmdData)->psize > cmdSize
748 - sizeof(effect_param_t))) {
749 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800750 android_errorWriteLog(0x534e4554, "33003822");
751 return -EINVAL;
752 }
753 if (cmdCode == EFFECT_CMD_GET_PARAM &&
754 (*replySize < sizeof(effect_param_t) ||
755 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
756 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700757 return -EINVAL;
758 }
ragoe2759072016-11-22 18:02:48 -0800759 if (cmdCode == EFFECT_CMD_GET_PARAM &&
760 (sizeof(effect_param_t) > *replySize
761 || ((effect_param_t *)pCmdData)->psize > *replySize
762 - sizeof(effect_param_t)
763 || ((effect_param_t *)pCmdData)->vsize > *replySize
764 - sizeof(effect_param_t)
765 - ((effect_param_t *)pCmdData)->psize
766 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
767 *replySize
768 - sizeof(effect_param_t)
769 - ((effect_param_t *)pCmdData)->psize
770 - ((effect_param_t *)pCmdData)->vsize)) {
771 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
772 android_errorWriteLog(0x534e4554, "32705438");
773 return -EINVAL;
774 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700775 if ((cmdCode == EFFECT_CMD_SET_PARAM
776 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
777 (sizeof(effect_param_t) > cmdSize
778 || ((effect_param_t *)pCmdData)->psize > cmdSize
779 - sizeof(effect_param_t)
780 || ((effect_param_t *)pCmdData)->vsize > cmdSize
781 - sizeof(effect_param_t)
782 - ((effect_param_t *)pCmdData)->psize
783 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
784 cmdSize
785 - sizeof(effect_param_t)
786 - ((effect_param_t *)pCmdData)->psize
787 - ((effect_param_t *)pCmdData)->vsize)) {
788 android_errorWriteLog(0x534e4554, "30204301");
789 return -EINVAL;
790 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700791 status_t status = mEffectInterface->command(cmdCode,
792 cmdSize,
793 pCmdData,
794 replySize,
795 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800796 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
797 uint32_t size = (replySize == NULL) ? 0 : *replySize;
798 for (size_t i = 1; i < mHandles.size(); i++) {
799 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800800 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800801 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
802 }
803 }
804 }
805 return status;
806}
807
808status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
809{
810 Mutex::Autolock _l(mLock);
811 return setEnabled_l(enabled);
812}
813
814// must be called with EffectModule::mLock held
815status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
816{
817
818 ALOGV("setEnabled %p enabled %d", this, enabled);
819
820 if (enabled != isEnabled()) {
821 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
822 if (enabled && status != NO_ERROR) {
823 return status;
824 }
825
826 switch (mState) {
827 // going from disabled to enabled
828 case IDLE:
829 mState = STARTING;
830 break;
831 case STOPPED:
832 mState = RESTART;
833 break;
834 case STOPPING:
835 mState = ACTIVE;
836 break;
837
838 // going from enabled to disabled
839 case RESTART:
840 mState = STOPPED;
841 break;
842 case STARTING:
843 mState = IDLE;
844 break;
845 case ACTIVE:
846 mState = STOPPING;
847 break;
848 case DESTROYED:
849 return NO_ERROR; // simply ignore as we are being destroyed
850 }
851 for (size_t i = 1; i < mHandles.size(); i++) {
852 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800853 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800854 h->setEnabled(enabled);
855 }
856 }
857 }
858 return NO_ERROR;
859}
860
861bool AudioFlinger::EffectModule::isEnabled() const
862{
863 switch (mState) {
864 case RESTART:
865 case STARTING:
866 case ACTIVE:
867 return true;
868 case IDLE:
869 case STOPPING:
870 case STOPPED:
871 case DESTROYED:
872 default:
873 return false;
874 }
875}
876
877bool AudioFlinger::EffectModule::isProcessEnabled() const
878{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700879 if (mStatus != NO_ERROR) {
880 return false;
881 }
882
Eric Laurentca7cc822012-11-19 14:55:58 -0800883 switch (mState) {
884 case RESTART:
885 case ACTIVE:
886 case STOPPING:
887 case STOPPED:
888 return true;
889 case IDLE:
890 case STARTING:
891 case DESTROYED:
892 default:
893 return false;
894 }
895}
896
Mikhail Naganov022b9952017-01-04 16:36:51 -0800897void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700898 ALOGVV("setInBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800899 if (buffer != 0) {
900 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
901 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
902 } else {
903 mConfig.inputCfg.buffer.raw = NULL;
904 }
905 mInBuffer = buffer;
rago94a1ee82017-07-21 15:11:02 -0700906 if (buffer != nullptr) { // FIXME: EffectHalHidl::setInBuffer should accept null input.
907 mEffectInterface->setInBuffer(buffer);
908 }
909
910#ifdef FLOAT_EFFECT_CHAIN
911 // aux effects do in place conversion to float - we don't allocate mInBuffer16 for them.
912 // Theoretically insert effects can also do in-place conversions (destroying
913 // the original buffer) when the output buffer is identical to the input buffer,
914 // but we don't optimize for it here.
915 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
916 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
917 // we need to translate - create hidl shared buffer and intercept
918 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
919 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
920 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
921
922 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
923 __func__, inChannels, inFrameCount, size);
924
925 if (size > 0 && (mInBuffer16.get() == nullptr || size > mInBuffer16->getSize())) {
926 mInBuffer16.clear();
927 ALOGV("%s: allocating mInBuffer16 %zu", __func__, size);
928 (void)EffectBufferHalInterface::allocate(size, &mInBuffer16);
929 }
930 if (mInBuffer16.get() != nullptr) {
931 // FIXME: confirm buffer has enough size.
932 mInBuffer16->setFrameCount(inFrameCount);
933 mEffectInterface->setInBuffer(mInBuffer16);
934 } else if (size > 0) {
935 ALOGE("%s cannot create mInBuffer16", __func__);
936 }
937 }
938#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800939}
940
941void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700942 ALOGVV("setOutBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800943 if (buffer != 0) {
944 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
945 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
946 } else {
947 mConfig.outputCfg.buffer.raw = NULL;
948 }
949 mOutBuffer = buffer;
rago94a1ee82017-07-21 15:11:02 -0700950 if (buffer != nullptr) {
951 mEffectInterface->setOutBuffer(buffer);
952 }
953
954#ifdef FLOAT_EFFECT_CHAIN
955 // Note: Any effect that does not accumulate does not need mOutBuffer16 and
956 // can do in-place conversion from int16_t to float. We don't optimize here.
957 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
958 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
959 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
960 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
961
962 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
963 __func__, outChannels, outFrameCount, size);
964
965 if (size > 0 && (mOutBuffer16.get() == nullptr || size > mOutBuffer16->getSize())) {
966 mOutBuffer16.clear();
967 ALOGV("%s: allocating mOutBuffer16 %zu", __func__, size);
968 (void)EffectBufferHalInterface::allocate(size, &mOutBuffer16);
969 }
970 if (mOutBuffer16.get() != nullptr) {
971 mOutBuffer16->setFrameCount(outFrameCount);
972 mEffectInterface->setOutBuffer(mOutBuffer16);
973 } else if (size > 0) {
974 ALOGE("%s cannot create mOutBuffer16", __func__);
975 }
976 }
977#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800978}
979
Eric Laurentca7cc822012-11-19 14:55:58 -0800980status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
981{
982 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700983 if (mStatus != NO_ERROR) {
984 return mStatus;
985 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800986 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800987 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
988 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
989 if (isProcessEnabled() &&
990 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
991 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800992 uint32_t volume[2];
993 uint32_t *pVolume = NULL;
994 uint32_t size = sizeof(volume);
995 volume[0] = *left;
996 volume[1] = *right;
997 if (controller) {
998 pVolume = volume;
999 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001000 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1001 size,
1002 volume,
1003 &size,
1004 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001005 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1006 *left = volume[0];
1007 *right = volume[1];
1008 }
1009 }
1010 return status;
1011}
1012
1013status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1014{
1015 if (device == AUDIO_DEVICE_NONE) {
1016 return NO_ERROR;
1017 }
1018
1019 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001020 if (mStatus != NO_ERROR) {
1021 return mStatus;
1022 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001023 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001024 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001025 status_t cmdStatus;
1026 uint32_t size = sizeof(status_t);
1027 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1028 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001029 status = mEffectInterface->command(cmd,
1030 sizeof(uint32_t),
1031 &device,
1032 &size,
1033 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 }
1035 return status;
1036}
1037
1038status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1039{
1040 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001041 if (mStatus != NO_ERROR) {
1042 return mStatus;
1043 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001044 status_t status = NO_ERROR;
1045 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1046 status_t cmdStatus;
1047 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001048 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1049 sizeof(audio_mode_t),
1050 &mode,
1051 &size,
1052 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001053 if (status == NO_ERROR) {
1054 status = cmdStatus;
1055 }
1056 }
1057 return status;
1058}
1059
1060status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1061{
1062 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001063 if (mStatus != NO_ERROR) {
1064 return mStatus;
1065 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001066 status_t status = NO_ERROR;
1067 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1068 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001069 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1070 sizeof(audio_source_t),
1071 &source,
1072 &size,
1073 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001074 }
1075 return status;
1076}
1077
1078void AudioFlinger::EffectModule::setSuspended(bool suspended)
1079{
1080 Mutex::Autolock _l(mLock);
1081 mSuspended = suspended;
1082}
1083
1084bool AudioFlinger::EffectModule::suspended() const
1085{
1086 Mutex::Autolock _l(mLock);
1087 return mSuspended;
1088}
1089
1090bool AudioFlinger::EffectModule::purgeHandles()
1091{
1092 bool enabled = false;
1093 Mutex::Autolock _l(mLock);
1094 for (size_t i = 0; i < mHandles.size(); i++) {
1095 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001096 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001097 if (handle->hasControl()) {
1098 enabled = handle->enabled();
1099 }
1100 }
1101 }
1102 return enabled;
1103}
1104
Eric Laurent5baf2af2013-09-12 17:37:00 -07001105status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1106{
1107 Mutex::Autolock _l(mLock);
1108 if (mStatus != NO_ERROR) {
1109 return mStatus;
1110 }
1111 status_t status = NO_ERROR;
1112 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1113 status_t cmdStatus;
1114 uint32_t size = sizeof(status_t);
1115 effect_offload_param_t cmd;
1116
1117 cmd.isOffload = offloaded;
1118 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001119 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1120 sizeof(effect_offload_param_t),
1121 &cmd,
1122 &size,
1123 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001124 if (status == NO_ERROR) {
1125 status = cmdStatus;
1126 }
1127 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1128 } else {
1129 if (offloaded) {
1130 status = INVALID_OPERATION;
1131 }
1132 mOffloaded = false;
1133 }
1134 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1135 return status;
1136}
1137
1138bool AudioFlinger::EffectModule::isOffloaded() const
1139{
1140 Mutex::Autolock _l(mLock);
1141 return mOffloaded;
1142}
1143
Marco Nelissenb2208842014-02-07 14:00:50 -08001144String8 effectFlagsToString(uint32_t flags) {
1145 String8 s;
1146
1147 s.append("conn. mode: ");
1148 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1149 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1150 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1151 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1152 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1153 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1154 default: s.append("unknown/reserved"); break;
1155 }
1156 s.append(", ");
1157
1158 s.append("insert pref: ");
1159 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1160 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1161 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1162 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1163 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1164 default: s.append("unknown/reserved"); break;
1165 }
1166 s.append(", ");
1167
1168 s.append("volume mgmt: ");
1169 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1170 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1171 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1172 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1173 default: s.append("unknown/reserved"); break;
1174 }
1175 s.append(", ");
1176
1177 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1178 if (devind) {
1179 s.append("device indication: ");
1180 switch (devind) {
1181 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1182 default: s.append("unknown/reserved"); break;
1183 }
1184 s.append(", ");
1185 }
1186
1187 s.append("input mode: ");
1188 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1189 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1190 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1191 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1192 default: s.append("not set"); break;
1193 }
1194 s.append(", ");
1195
1196 s.append("output mode: ");
1197 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1198 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1199 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1200 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1201 default: s.append("not set"); break;
1202 }
1203 s.append(", ");
1204
1205 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1206 if (accel) {
1207 s.append("hardware acceleration: ");
1208 switch (accel) {
1209 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1210 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1211 default: s.append("unknown/reserved"); break;
1212 }
1213 s.append(", ");
1214 }
1215
1216 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1217 if (modeind) {
1218 s.append("mode indication: ");
1219 switch (modeind) {
1220 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1221 default: s.append("unknown/reserved"); break;
1222 }
1223 s.append(", ");
1224 }
1225
1226 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1227 if (srcind) {
1228 s.append("source indication: ");
1229 switch (srcind) {
1230 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1231 default: s.append("unknown/reserved"); break;
1232 }
1233 s.append(", ");
1234 }
1235
1236 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1237 s.append("offloadable, ");
1238 }
1239
1240 int len = s.length();
1241 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001242 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001243 s.unlockBuffer(len - 2);
1244 }
1245 return s;
1246}
1247
1248
Glenn Kasten0f11b512014-01-31 16:18:54 -08001249void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001250{
1251 const size_t SIZE = 256;
1252 char buffer[SIZE];
1253 String8 result;
1254
1255 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1256 result.append(buffer);
1257
1258 bool locked = AudioFlinger::dumpTryLock(mLock);
1259 // failed to lock - AudioFlinger is probably deadlocked
1260 if (!locked) {
1261 result.append("\t\tCould not lock Fx mutex:\n");
1262 }
1263
1264 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001265 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001266 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001267 result.append(buffer);
1268
1269 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001270 char uuidStr[64];
1271 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1272 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001273 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001274 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1275 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001276 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001277 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001278 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001279 mDescriptor.flags,
1280 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001281 result.append(buffer);
1282 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1283 mDescriptor.name);
1284 result.append(buffer);
1285 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1286 mDescriptor.implementor);
1287 result.append(buffer);
1288
1289 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001290 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001291 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001292 mConfig.inputCfg.buffer.frameCount,
1293 mConfig.inputCfg.samplingRate,
1294 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001295 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001296 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001297 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001298 result.append(buffer);
1299
1300 result.append("\t\t- Output configuration:\n");
1301 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001302 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001303 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001304 mConfig.outputCfg.buffer.frameCount,
1305 mConfig.outputCfg.samplingRate,
1306 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001307 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001308 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001309 result.append(buffer);
1310
rago94a1ee82017-07-21 15:11:02 -07001311#ifdef FLOAT_EFFECT_CHAIN
1312 if (!mSupportsFloat) {
1313 int16_t* pIn16 = mInBuffer16 != 0 ? mInBuffer16->audioBuffer()->s16 : NULL;
1314 int16_t* pOut16 = mOutBuffer16 != 0 ? mOutBuffer16->audioBuffer()->s16 : NULL;
1315
1316 result.append("\t\t- Float and int16 buffers\n");
1317 result.append("\t\t\tIn_float In_int16 Out_float Out_int16\n");
1318 snprintf(buffer, SIZE,"\t\t\t%p %p %p %p\n",
1319 mConfig.inputCfg.buffer.raw,
1320 pIn16,
1321 pOut16,
1322 mConfig.outputCfg.buffer.raw);
1323 result.append(buffer);
1324 }
1325#endif
1326
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001327 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001328 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001329 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001330 for (size_t i = 0; i < mHandles.size(); ++i) {
1331 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001332 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001333 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001334 result.append(buffer);
1335 }
1336 }
1337
Eric Laurentca7cc822012-11-19 14:55:58 -08001338 write(fd, result.string(), result.length());
1339
1340 if (locked) {
1341 mLock.unlock();
1342 }
1343}
1344
1345// ----------------------------------------------------------------------------
1346// EffectHandle implementation
1347// ----------------------------------------------------------------------------
1348
1349#undef LOG_TAG
1350#define LOG_TAG "AudioFlinger::EffectHandle"
1351
1352AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1353 const sp<AudioFlinger::Client>& client,
1354 const sp<IEffectClient>& effectClient,
1355 int32_t priority)
1356 : BnEffect(),
1357 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001358 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001359{
1360 ALOGV("constructor %p", this);
1361
1362 if (client == 0) {
1363 return;
1364 }
1365 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1366 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001367 if (mCblkMemory == 0 ||
1368 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001369 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001370 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001371 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001372 return;
1373 }
Glenn Kastene75da402013-11-20 13:54:52 -08001374 new(mCblk) effect_param_cblk_t();
1375 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001376}
1377
1378AudioFlinger::EffectHandle::~EffectHandle()
1379{
1380 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001381 disconnect(false);
1382}
1383
Glenn Kastene75da402013-11-20 13:54:52 -08001384status_t AudioFlinger::EffectHandle::initCheck()
1385{
1386 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1387}
1388
Eric Laurentca7cc822012-11-19 14:55:58 -08001389status_t AudioFlinger::EffectHandle::enable()
1390{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001391 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001392 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001393 sp<EffectModule> effect = mEffect.promote();
1394 if (effect == 0 || mDisconnected) {
1395 return DEAD_OBJECT;
1396 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001397 if (!mHasControl) {
1398 return INVALID_OPERATION;
1399 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001400
1401 if (mEnabled) {
1402 return NO_ERROR;
1403 }
1404
1405 mEnabled = true;
1406
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001407 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001408 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001409 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 }
1411
1412 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001413 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 return NO_ERROR;
1415 }
1416
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001417 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001418 if (status != NO_ERROR) {
1419 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001420 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 }
1422 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001423 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001424 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001425 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1426 Mutex::Autolock _l(thread->mLock);
1427 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001428 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001429 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001430 if (thread->type() == ThreadBase::OFFLOAD) {
1431 PlaybackThread *t = (PlaybackThread *)thread.get();
1432 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1433 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001434 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001435 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1436 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001437 }
1438 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001439 }
1440 return status;
1441}
1442
1443status_t AudioFlinger::EffectHandle::disable()
1444{
1445 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001446 AutoMutex _l(mLock);
1447 sp<EffectModule> effect = mEffect.promote();
1448 if (effect == 0 || mDisconnected) {
1449 return DEAD_OBJECT;
1450 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001451 if (!mHasControl) {
1452 return INVALID_OPERATION;
1453 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001454
1455 if (!mEnabled) {
1456 return NO_ERROR;
1457 }
1458 mEnabled = false;
1459
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001460 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001461 return NO_ERROR;
1462 }
1463
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001464 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001465
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001466 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001468 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001469 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1470 Mutex::Autolock _l(thread->mLock);
1471 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001472 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001473 }
1474
1475 return status;
1476}
1477
1478void AudioFlinger::EffectHandle::disconnect()
1479{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001480 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001481 disconnect(true);
1482}
1483
1484void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1485{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001486 AutoMutex _l(mLock);
1487 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1488 if (mDisconnected) {
1489 if (unpinIfLast) {
1490 android_errorWriteLog(0x534e4554, "32707507");
1491 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001492 return;
1493 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001494 mDisconnected = true;
1495 sp<ThreadBase> thread;
1496 {
1497 sp<EffectModule> effect = mEffect.promote();
1498 if (effect != 0) {
1499 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001500 }
1501 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001502 if (thread != 0) {
1503 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001504 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001505 // try to cleanup as much as we can
1506 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001507 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1508 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001509 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001510 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001511
Eric Laurentca7cc822012-11-19 14:55:58 -08001512 if (mClient != 0) {
1513 if (mCblk != NULL) {
1514 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1515 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1516 }
1517 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001518 // Client destructor must run with AudioFlinger client mutex locked
1519 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001520 mClient.clear();
1521 }
1522}
1523
1524status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1525 uint32_t cmdSize,
1526 void *pCmdData,
1527 uint32_t *replySize,
1528 void *pReplyData)
1529{
1530 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001531 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001532
Eric Laurentc7ab3092017-06-15 18:43:46 -07001533 // reject commands reserved for internal use by audio framework if coming from outside
1534 // of audioserver
1535 switch(cmdCode) {
1536 case EFFECT_CMD_ENABLE:
1537 case EFFECT_CMD_DISABLE:
1538 case EFFECT_CMD_SET_PARAM:
1539 case EFFECT_CMD_SET_PARAM_DEFERRED:
1540 case EFFECT_CMD_SET_PARAM_COMMIT:
1541 case EFFECT_CMD_GET_PARAM:
1542 break;
1543 default:
1544 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1545 break;
1546 }
1547 android_errorWriteLog(0x534e4554, "62019992");
1548 return BAD_VALUE;
1549 }
1550
Eric Laurent1ffc5852016-12-15 14:46:09 -08001551 if (cmdCode == EFFECT_CMD_ENABLE) {
1552 if (*replySize < sizeof(int)) {
1553 android_errorWriteLog(0x534e4554, "32095713");
1554 return BAD_VALUE;
1555 }
1556 *(int *)pReplyData = NO_ERROR;
1557 *replySize = sizeof(int);
1558 return enable();
1559 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1560 if (*replySize < sizeof(int)) {
1561 android_errorWriteLog(0x534e4554, "32095713");
1562 return BAD_VALUE;
1563 }
1564 *(int *)pReplyData = NO_ERROR;
1565 *replySize = sizeof(int);
1566 return disable();
1567 }
1568
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001569 AutoMutex _l(mLock);
1570 sp<EffectModule> effect = mEffect.promote();
1571 if (effect == 0 || mDisconnected) {
1572 return DEAD_OBJECT;
1573 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001574 // only get parameter command is permitted for applications not controlling the effect
1575 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1576 return INVALID_OPERATION;
1577 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001578 if (mClient == 0) {
1579 return INVALID_OPERATION;
1580 }
1581
1582 // handle commands that are not forwarded transparently to effect engine
1583 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001584 if (*replySize < sizeof(int)) {
1585 android_errorWriteLog(0x534e4554, "32095713");
1586 return BAD_VALUE;
1587 }
1588 *(int *)pReplyData = NO_ERROR;
1589 *replySize = sizeof(int);
1590
Eric Laurentca7cc822012-11-19 14:55:58 -08001591 // No need to trylock() here as this function is executed in the binder thread serving a
1592 // particular client process: no risk to block the whole media server process or mixer
1593 // threads if we are stuck here
1594 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001595 // keep local copy of index in case of client corruption b/32220769
1596 const uint32_t clientIndex = mCblk->clientIndex;
1597 const uint32_t serverIndex = mCblk->serverIndex;
1598 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1599 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001600 mCblk->serverIndex = 0;
1601 mCblk->clientIndex = 0;
1602 return BAD_VALUE;
1603 }
1604 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001605 effect_param_t *param = NULL;
1606 for (uint32_t index = serverIndex; index < clientIndex;) {
1607 int *p = (int *)(mBuffer + index);
1608 const int size = *p++;
1609 if (size < 0
1610 || size > EFFECT_PARAM_BUFFER_SIZE
1611 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001612 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001613 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001614 break;
1615 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001616
1617 // copy to local memory in case of client corruption b/32220769
1618 param = (effect_param_t *)realloc(param, size);
1619 if (param == NULL) {
1620 ALOGW("command(): out of memory");
1621 status = NO_MEMORY;
1622 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001623 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001624 memcpy(param, p, size);
1625
1626 int reply = 0;
1627 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001628 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001629 size,
1630 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 &rsize,
1632 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001633
1634 // verify shared memory: server index shouldn't change; client index can't go back.
1635 if (serverIndex != mCblk->serverIndex
1636 || clientIndex > mCblk->clientIndex) {
1637 android_errorWriteLog(0x534e4554, "32220769");
1638 status = BAD_VALUE;
1639 break;
1640 }
1641
Eric Laurentca7cc822012-11-19 14:55:58 -08001642 // stop at first error encountered
1643 if (ret != NO_ERROR) {
1644 status = ret;
1645 *(int *)pReplyData = reply;
1646 break;
1647 } else if (reply != NO_ERROR) {
1648 *(int *)pReplyData = reply;
1649 break;
1650 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001651 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001652 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001653 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001654 mCblk->serverIndex = 0;
1655 mCblk->clientIndex = 0;
1656 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001657 }
1658
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001659 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001660}
1661
1662void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1663{
1664 ALOGV("setControl %p control %d", this, hasControl);
1665
1666 mHasControl = hasControl;
1667 mEnabled = enabled;
1668
1669 if (signal && mEffectClient != 0) {
1670 mEffectClient->controlStatusChanged(hasControl);
1671 }
1672}
1673
1674void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1675 uint32_t cmdSize,
1676 void *pCmdData,
1677 uint32_t replySize,
1678 void *pReplyData)
1679{
1680 if (mEffectClient != 0) {
1681 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1682 }
1683}
1684
1685
1686
1687void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1688{
1689 if (mEffectClient != 0) {
1690 mEffectClient->enableStatusChanged(enabled);
1691 }
1692}
1693
1694status_t AudioFlinger::EffectHandle::onTransact(
1695 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1696{
1697 return BnEffect::onTransact(code, data, reply, flags);
1698}
1699
1700
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001701void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001702{
1703 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1704
Marco Nelissenb2208842014-02-07 14:00:50 -08001705 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 (mClient == 0) ? getpid_cached : mClient->pid(),
1707 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001708 mHasControl ? "yes" : "no",
1709 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001710 mCblk ? mCblk->clientIndex : 0,
1711 mCblk ? mCblk->serverIndex : 0
1712 );
1713
1714 if (locked) {
1715 mCblk->lock.unlock();
1716 }
1717}
1718
1719#undef LOG_TAG
1720#define LOG_TAG "AudioFlinger::EffectChain"
1721
1722AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001723 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001724 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001725 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001726 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001727{
1728 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1729 if (thread == NULL) {
1730 return;
1731 }
1732 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1733 thread->frameCount();
1734}
1735
1736AudioFlinger::EffectChain::~EffectChain()
1737{
Eric Laurentca7cc822012-11-19 14:55:58 -08001738}
1739
1740// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1741sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1742 effect_descriptor_t *descriptor)
1743{
1744 size_t size = mEffects.size();
1745
1746 for (size_t i = 0; i < size; i++) {
1747 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1748 return mEffects[i];
1749 }
1750 }
1751 return 0;
1752}
1753
1754// getEffectFromId_l() must be called with ThreadBase::mLock held
1755sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1756{
1757 size_t size = mEffects.size();
1758
1759 for (size_t i = 0; i < size; i++) {
1760 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1761 if (id == 0 || mEffects[i]->id() == id) {
1762 return mEffects[i];
1763 }
1764 }
1765 return 0;
1766}
1767
1768// getEffectFromType_l() must be called with ThreadBase::mLock held
1769sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1770 const effect_uuid_t *type)
1771{
1772 size_t size = mEffects.size();
1773
1774 for (size_t i = 0; i < size; i++) {
1775 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1776 return mEffects[i];
1777 }
1778 }
1779 return 0;
1780}
1781
1782void AudioFlinger::EffectChain::clearInputBuffer()
1783{
1784 Mutex::Autolock _l(mLock);
1785 sp<ThreadBase> thread = mThread.promote();
1786 if (thread == 0) {
1787 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1788 return;
1789 }
1790 clearInputBuffer_l(thread);
1791}
1792
1793// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001794void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001795{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001796 if (mInBuffer == NULL) {
1797 return;
1798 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001799 // TODO: This will change in the future, depending on multichannel
1800 // and sample format changes for effects.
1801 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1802 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001803
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001804 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001805 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1806 * std::min((uint32_t)FCC_2, thread->channelCount());
1807
Mikhail Naganov022b9952017-01-04 16:36:51 -08001808 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1809 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001810}
1811
1812// Must be called with EffectChain::mLock locked
1813void AudioFlinger::EffectChain::process_l()
1814{
1815 sp<ThreadBase> thread = mThread.promote();
1816 if (thread == 0) {
1817 ALOGW("process_l(): cannot promote mixer thread");
1818 return;
1819 }
1820 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1821 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001822 // never process effects when:
1823 // - on an OFFLOAD thread
1824 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001825 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1826 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001827 if (!isGlobalSession) {
1828 bool tracksOnSession = (trackCnt() != 0);
1829
1830 if (!tracksOnSession && mTailBufferCount == 0) {
1831 doProcess = false;
1832 }
1833
1834 if (activeTrackCnt() == 0) {
1835 // if no track is active and the effect tail has not been rendered,
1836 // the input buffer must be cleared here as the mixer process will not do it
1837 if (tracksOnSession || mTailBufferCount > 0) {
1838 clearInputBuffer_l(thread);
1839 if (mTailBufferCount > 0) {
1840 mTailBufferCount--;
1841 }
1842 }
1843 }
1844 }
1845
1846 size_t size = mEffects.size();
1847 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001848 // Only the input and output buffers of the chain can be external,
1849 // and 'update' / 'commit' do nothing for allocated buffers, thus
1850 // it's not needed to consider any other buffers here.
1851 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001852 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1853 mOutBuffer->update();
1854 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001855 for (size_t i = 0; i < size; i++) {
1856 mEffects[i]->process();
1857 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001858 mInBuffer->commit();
1859 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1860 mOutBuffer->commit();
1861 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001862 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001863 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001865 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1866 }
1867 if (doResetVolume) {
1868 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001869 }
1870}
1871
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001872// createEffect_l() must be called with ThreadBase::mLock held
1873status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1874 ThreadBase *thread,
1875 effect_descriptor_t *desc,
1876 int id,
1877 audio_session_t sessionId,
1878 bool pinned)
1879{
1880 Mutex::Autolock _l(mLock);
1881 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1882 status_t lStatus = effect->status();
1883 if (lStatus == NO_ERROR) {
1884 lStatus = addEffect_ll(effect);
1885 }
1886 if (lStatus != NO_ERROR) {
1887 effect.clear();
1888 }
1889 return lStatus;
1890}
1891
1892// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001893status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1894{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001895 Mutex::Autolock _l(mLock);
1896 return addEffect_ll(effect);
1897}
1898// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1899status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1900{
Eric Laurentca7cc822012-11-19 14:55:58 -08001901 effect_descriptor_t desc = effect->desc();
1902 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1903
Eric Laurentca7cc822012-11-19 14:55:58 -08001904 effect->setChain(this);
1905 sp<ThreadBase> thread = mThread.promote();
1906 if (thread == 0) {
1907 return NO_INIT;
1908 }
1909 effect->setThread(thread);
1910
1911 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1912 // Auxiliary effects are inserted at the beginning of mEffects vector as
1913 // they are processed first and accumulated in chain input buffer
1914 mEffects.insertAt(effect, 0);
1915
1916 // the input buffer for auxiliary effect contains mono samples in
1917 // 32 bit format. This is to avoid saturation in AudoMixer
1918 // accumulation stage. Saturation is done in EffectModule::process() before
1919 // calling the process in effect engine
1920 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001921 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001922#ifdef FLOAT_EFFECT_CHAIN
1923 status_t result = EffectBufferHalInterface::allocate(
1924 numSamples * sizeof(float), &halBuffer);
1925#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001926 status_t result = EffectBufferHalInterface::allocate(
1927 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001928#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001929 if (result != OK) return result;
1930 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001931 // auxiliary effects output samples to chain input buffer for further processing
1932 // by insert effects
1933 effect->setOutBuffer(mInBuffer);
1934 } else {
1935 // Insert effects are inserted at the end of mEffects vector as they are processed
1936 // after track and auxiliary effects.
1937 // Insert effect order as a function of indicated preference:
1938 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1939 // another effect is present
1940 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1941 // last effect claiming first position
1942 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1943 // first effect claiming last position
1944 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1945 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1946 // already present
1947
1948 size_t size = mEffects.size();
1949 size_t idx_insert = size;
1950 ssize_t idx_insert_first = -1;
1951 ssize_t idx_insert_last = -1;
1952
1953 for (size_t i = 0; i < size; i++) {
1954 effect_descriptor_t d = mEffects[i]->desc();
1955 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1956 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1957 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1958 // check invalid effect chaining combinations
1959 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1960 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1961 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1962 desc.name, d.name);
1963 return INVALID_OPERATION;
1964 }
1965 // remember position of first insert effect and by default
1966 // select this as insert position for new effect
1967 if (idx_insert == size) {
1968 idx_insert = i;
1969 }
1970 // remember position of last insert effect claiming
1971 // first position
1972 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1973 idx_insert_first = i;
1974 }
1975 // remember position of first insert effect claiming
1976 // last position
1977 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1978 idx_insert_last == -1) {
1979 idx_insert_last = i;
1980 }
1981 }
1982 }
1983
1984 // modify idx_insert from first position if needed
1985 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1986 if (idx_insert_last != -1) {
1987 idx_insert = idx_insert_last;
1988 } else {
1989 idx_insert = size;
1990 }
1991 } else {
1992 if (idx_insert_first != -1) {
1993 idx_insert = idx_insert_first + 1;
1994 }
1995 }
1996
1997 // always read samples from chain input buffer
1998 effect->setInBuffer(mInBuffer);
1999
2000 // if last effect in the chain, output samples to chain
2001 // output buffer, otherwise to chain input buffer
2002 if (idx_insert == size) {
2003 if (idx_insert != 0) {
2004 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2005 mEffects[idx_insert-1]->configure();
2006 }
2007 effect->setOutBuffer(mOutBuffer);
2008 } else {
2009 effect->setOutBuffer(mInBuffer);
2010 }
2011 mEffects.insertAt(effect, idx_insert);
2012
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002013 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002014 idx_insert);
2015 }
2016 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002017
Eric Laurentca7cc822012-11-19 14:55:58 -08002018 return NO_ERROR;
2019}
2020
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002021// removeEffect_l() must be called with ThreadBase::mLock held
2022size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2023 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002024{
2025 Mutex::Autolock _l(mLock);
2026 size_t size = mEffects.size();
2027 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2028
2029 for (size_t i = 0; i < size; i++) {
2030 if (effect == mEffects[i]) {
2031 // calling stop here will remove pre-processing effect from the audio HAL.
2032 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2033 // the middle of a read from audio HAL
2034 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2035 mEffects[i]->state() == EffectModule::STOPPING) {
2036 mEffects[i]->stop();
2037 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002038 if (release) {
2039 mEffects[i]->release_l();
2040 }
2041
Mikhail Naganov022b9952017-01-04 16:36:51 -08002042 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002043 if (i == size - 1 && i != 0) {
2044 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2045 mEffects[i - 1]->configure();
2046 }
2047 }
2048 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002049 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002050 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002051
Eric Laurentca7cc822012-11-19 14:55:58 -08002052 break;
2053 }
2054 }
2055
2056 return mEffects.size();
2057}
2058
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002059// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002060void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2061{
2062 size_t size = mEffects.size();
2063 for (size_t i = 0; i < size; i++) {
2064 mEffects[i]->setDevice(device);
2065 }
2066}
2067
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002068// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002069void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2070{
2071 size_t size = mEffects.size();
2072 for (size_t i = 0; i < size; i++) {
2073 mEffects[i]->setMode(mode);
2074 }
2075}
2076
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002077// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002078void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2079{
2080 size_t size = mEffects.size();
2081 for (size_t i = 0; i < size; i++) {
2082 mEffects[i]->setAudioSource(source);
2083 }
2084}
2085
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002086// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002087bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002088{
2089 uint32_t newLeft = *left;
2090 uint32_t newRight = *right;
2091 bool hasControl = false;
2092 int ctrlIdx = -1;
2093 size_t size = mEffects.size();
2094
2095 // first update volume controller
2096 for (size_t i = size; i > 0; i--) {
2097 if (mEffects[i - 1]->isProcessEnabled() &&
2098 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2099 ctrlIdx = i - 1;
2100 hasControl = true;
2101 break;
2102 }
2103 }
2104
Eric Laurentfa1e1232016-08-02 19:01:49 -07002105 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002106 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002107 if (hasControl) {
2108 *left = mNewLeftVolume;
2109 *right = mNewRightVolume;
2110 }
2111 return hasControl;
2112 }
2113
2114 mVolumeCtrlIdx = ctrlIdx;
2115 mLeftVolume = newLeft;
2116 mRightVolume = newRight;
2117
2118 // second get volume update from volume controller
2119 if (ctrlIdx >= 0) {
2120 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2121 mNewLeftVolume = newLeft;
2122 mNewRightVolume = newRight;
2123 }
2124 // then indicate volume to all other effects in chain.
2125 // Pass altered volume to effects before volume controller
2126 // and requested volume to effects after controller
2127 uint32_t lVol = newLeft;
2128 uint32_t rVol = newRight;
2129
2130 for (size_t i = 0; i < size; i++) {
2131 if ((int)i == ctrlIdx) {
2132 continue;
2133 }
2134 // this also works for ctrlIdx == -1 when there is no volume controller
2135 if ((int)i > ctrlIdx) {
2136 lVol = *left;
2137 rVol = *right;
2138 }
2139 mEffects[i]->setVolume(&lVol, &rVol, false);
2140 }
2141 *left = newLeft;
2142 *right = newRight;
2143
2144 return hasControl;
2145}
2146
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002147// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002148void AudioFlinger::EffectChain::resetVolume_l()
2149{
Eric Laurente7449bf2016-08-03 18:44:07 -07002150 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2151 uint32_t left = mLeftVolume;
2152 uint32_t right = mRightVolume;
2153 (void)setVolume_l(&left, &right, true);
2154 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002155}
2156
Eric Laurent1b928682014-10-02 19:41:47 -07002157void AudioFlinger::EffectChain::syncHalEffectsState()
2158{
2159 Mutex::Autolock _l(mLock);
2160 for (size_t i = 0; i < mEffects.size(); i++) {
2161 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2162 mEffects[i]->state() == EffectModule::STOPPING) {
2163 mEffects[i]->addEffectToHal_l();
2164 }
2165 }
2166}
2167
Mikhail Naganov06888802017-01-19 12:47:55 -08002168static void dumpInOutBuffer(
2169 char *dump, size_t dumpSize, bool isInput, EffectBufferHalInterface *buffer) {
Mikhail Naganovc778e592017-01-25 10:35:30 -08002170 if (buffer == nullptr) {
2171 snprintf(dump, dumpSize, "%p", buffer);
2172 } else if (buffer->externalData() != nullptr) {
Mikhail Naganov06888802017-01-19 12:47:55 -08002173 snprintf(dump, dumpSize, "%p -> %p",
2174 isInput ? buffer->externalData() : buffer->audioBuffer()->raw,
2175 isInput ? buffer->audioBuffer()->raw : buffer->externalData());
2176 } else {
2177 snprintf(dump, dumpSize, "%p", buffer->audioBuffer()->raw);
2178 }
2179}
2180
Eric Laurentca7cc822012-11-19 14:55:58 -08002181void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2182{
2183 const size_t SIZE = 256;
2184 char buffer[SIZE];
2185 String8 result;
2186
Marco Nelissenb2208842014-02-07 14:00:50 -08002187 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002188 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002189 result.append(buffer);
2190
Marco Nelissenb2208842014-02-07 14:00:50 -08002191 if (numEffects) {
2192 bool locked = AudioFlinger::dumpTryLock(mLock);
2193 // failed to lock - AudioFlinger is probably deadlocked
2194 if (!locked) {
2195 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002196 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002197
Mikhail Naganov06888802017-01-19 12:47:55 -08002198 char inBufferStr[64], outBufferStr[64];
2199 dumpInOutBuffer(inBufferStr, sizeof(inBufferStr), true, mInBuffer.get());
2200 dumpInOutBuffer(outBufferStr, sizeof(outBufferStr), false, mOutBuffer.get());
2201 snprintf(buffer, SIZE, "\t%-*s%-*s Active tracks:\n",
2202 (int)strlen(inBufferStr), "In buffer ",
2203 (int)strlen(outBufferStr), "Out buffer ");
2204 result.append(buffer);
2205 snprintf(buffer, SIZE, "\t%s %s %d\n", inBufferStr, outBufferStr, mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002206 result.append(buffer);
2207 write(fd, result.string(), result.size());
2208
2209 for (size_t i = 0; i < numEffects; ++i) {
2210 sp<EffectModule> effect = mEffects[i];
2211 if (effect != 0) {
2212 effect->dump(fd, args);
2213 }
2214 }
2215
2216 if (locked) {
2217 mLock.unlock();
2218 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002219 }
2220}
2221
2222// must be called with ThreadBase::mLock held
2223void AudioFlinger::EffectChain::setEffectSuspended_l(
2224 const effect_uuid_t *type, bool suspend)
2225{
2226 sp<SuspendedEffectDesc> desc;
2227 // use effect type UUID timelow as key as there is no real risk of identical
2228 // timeLow fields among effect type UUIDs.
2229 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2230 if (suspend) {
2231 if (index >= 0) {
2232 desc = mSuspendedEffects.valueAt(index);
2233 } else {
2234 desc = new SuspendedEffectDesc();
2235 desc->mType = *type;
2236 mSuspendedEffects.add(type->timeLow, desc);
2237 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2238 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002239
Eric Laurentca7cc822012-11-19 14:55:58 -08002240 if (desc->mRefCount++ == 0) {
2241 sp<EffectModule> effect = getEffectIfEnabled(type);
2242 if (effect != 0) {
2243 desc->mEffect = effect;
2244 effect->setSuspended(true);
2245 effect->setEnabled(false);
2246 }
2247 }
2248 } else {
2249 if (index < 0) {
2250 return;
2251 }
2252 desc = mSuspendedEffects.valueAt(index);
2253 if (desc->mRefCount <= 0) {
2254 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002255 desc->mRefCount = 0;
2256 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002257 }
2258 if (--desc->mRefCount == 0) {
2259 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2260 if (desc->mEffect != 0) {
2261 sp<EffectModule> effect = desc->mEffect.promote();
2262 if (effect != 0) {
2263 effect->setSuspended(false);
2264 effect->lock();
2265 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002266 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002267 effect->setEnabled_l(handle->enabled());
2268 }
2269 effect->unlock();
2270 }
2271 desc->mEffect.clear();
2272 }
2273 mSuspendedEffects.removeItemsAt(index);
2274 }
2275 }
2276}
2277
2278// must be called with ThreadBase::mLock held
2279void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2280{
2281 sp<SuspendedEffectDesc> desc;
2282
2283 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2284 if (suspend) {
2285 if (index >= 0) {
2286 desc = mSuspendedEffects.valueAt(index);
2287 } else {
2288 desc = new SuspendedEffectDesc();
2289 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2290 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2291 }
2292 if (desc->mRefCount++ == 0) {
2293 Vector< sp<EffectModule> > effects;
2294 getSuspendEligibleEffects(effects);
2295 for (size_t i = 0; i < effects.size(); i++) {
2296 setEffectSuspended_l(&effects[i]->desc().type, true);
2297 }
2298 }
2299 } else {
2300 if (index < 0) {
2301 return;
2302 }
2303 desc = mSuspendedEffects.valueAt(index);
2304 if (desc->mRefCount <= 0) {
2305 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2306 desc->mRefCount = 1;
2307 }
2308 if (--desc->mRefCount == 0) {
2309 Vector<const effect_uuid_t *> types;
2310 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2311 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2312 continue;
2313 }
2314 types.add(&mSuspendedEffects.valueAt(i)->mType);
2315 }
2316 for (size_t i = 0; i < types.size(); i++) {
2317 setEffectSuspended_l(types[i], false);
2318 }
2319 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2320 mSuspendedEffects.keyAt(index));
2321 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2322 }
2323 }
2324}
2325
2326
2327// The volume effect is used for automated tests only
2328#ifndef OPENSL_ES_H_
2329static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2330 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2331const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2332#endif //OPENSL_ES_H_
2333
Eric Laurentd8365c52017-07-16 15:27:05 -07002334/* static */
2335bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2336{
2337 // Only NS and AEC are suspended when BtNRec is off
2338 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2339 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2340 return true;
2341 }
2342 return false;
2343}
2344
Eric Laurentca7cc822012-11-19 14:55:58 -08002345bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2346{
2347 // auxiliary effects and visualizer are never suspended on output mix
2348 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2349 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2350 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2351 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2352 return false;
2353 }
2354 return true;
2355}
2356
2357void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2358 Vector< sp<AudioFlinger::EffectModule> > &effects)
2359{
2360 effects.clear();
2361 for (size_t i = 0; i < mEffects.size(); i++) {
2362 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2363 effects.add(mEffects[i]);
2364 }
2365 }
2366}
2367
2368sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2369 const effect_uuid_t *type)
2370{
2371 sp<EffectModule> effect = getEffectFromType_l(type);
2372 return effect != 0 && effect->isEnabled() ? effect : 0;
2373}
2374
2375void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2376 bool enabled)
2377{
2378 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2379 if (enabled) {
2380 if (index < 0) {
2381 // if the effect is not suspend check if all effects are suspended
2382 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2383 if (index < 0) {
2384 return;
2385 }
2386 if (!isEffectEligibleForSuspend(effect->desc())) {
2387 return;
2388 }
2389 setEffectSuspended_l(&effect->desc().type, enabled);
2390 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2391 if (index < 0) {
2392 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2393 return;
2394 }
2395 }
2396 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2397 effect->desc().type.timeLow);
2398 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002399 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002400 if (desc->mEffect == 0) {
2401 desc->mEffect = effect;
2402 effect->setEnabled(false);
2403 effect->setSuspended(true);
2404 }
2405 } else {
2406 if (index < 0) {
2407 return;
2408 }
2409 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2410 effect->desc().type.timeLow);
2411 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2412 desc->mEffect.clear();
2413 effect->setSuspended(false);
2414 }
2415}
2416
Eric Laurent5baf2af2013-09-12 17:37:00 -07002417bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002418{
2419 Mutex::Autolock _l(mLock);
2420 size_t size = mEffects.size();
2421 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002422 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002423 return true;
2424 }
2425 }
2426 return false;
2427}
2428
Eric Laurentaaa44472014-09-12 17:41:50 -07002429void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2430{
2431 Mutex::Autolock _l(mLock);
2432 mThread = thread;
2433 for (size_t i = 0; i < mEffects.size(); i++) {
2434 mEffects[i]->setThread(thread);
2435 }
2436}
2437
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002438void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2439{
2440 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2441 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2442 }
2443 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2444 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2445 }
2446}
2447
2448void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2449{
2450 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2451 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2452 }
2453 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2454 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2455 }
2456}
2457
2458bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002459{
2460 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002461 for (const auto &effect : mEffects) {
2462 if (effect->isProcessImplemented()) {
2463 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002464 }
2465 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002466 // Allow effects without processing.
2467 return true;
2468}
2469
2470bool AudioFlinger::EffectChain::isFastCompatible() const
2471{
2472 Mutex::Autolock _l(mLock);
2473 for (const auto &effect : mEffects) {
2474 if (effect->isProcessImplemented()
2475 && effect->isImplementationSoftware()) {
2476 return false;
2477 }
2478 }
2479 // Allow effects without processing or hw accelerated effects.
2480 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002481}
2482
2483// isCompatibleWithThread_l() must be called with thread->mLock held
2484bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2485{
2486 Mutex::Autolock _l(mLock);
2487 for (size_t i = 0; i < mEffects.size(); i++) {
2488 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2489 return false;
2490 }
2491 }
2492 return true;
2493}
2494
Glenn Kasten63238ef2015-03-02 15:50:29 -08002495} // namespace android