blob: e0d0d7be6a73583de4d23f98f77d54ebda81889f [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;
Andy Hungc15aaee2017-11-27 17:02:40 -0800906 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700907
908#ifdef FLOAT_EFFECT_CHAIN
909 // aux effects do in place conversion to float - we don't allocate mInBuffer16 for them.
910 // Theoretically insert effects can also do in-place conversions (destroying
911 // the original buffer) when the output buffer is identical to the input buffer,
912 // but we don't optimize for it here.
913 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
914 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
915 // we need to translate - create hidl shared buffer and intercept
916 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
917 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
918 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
919
920 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
921 __func__, inChannels, inFrameCount, size);
922
923 if (size > 0 && (mInBuffer16.get() == nullptr || size > mInBuffer16->getSize())) {
924 mInBuffer16.clear();
925 ALOGV("%s: allocating mInBuffer16 %zu", __func__, size);
926 (void)EffectBufferHalInterface::allocate(size, &mInBuffer16);
927 }
928 if (mInBuffer16.get() != nullptr) {
929 // FIXME: confirm buffer has enough size.
930 mInBuffer16->setFrameCount(inFrameCount);
931 mEffectInterface->setInBuffer(mInBuffer16);
932 } else if (size > 0) {
933 ALOGE("%s cannot create mInBuffer16", __func__);
934 }
935 }
936#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800937}
938
939void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700940 ALOGVV("setOutBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800941 if (buffer != 0) {
942 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
943 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
944 } else {
945 mConfig.outputCfg.buffer.raw = NULL;
946 }
947 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800948 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700949
950#ifdef FLOAT_EFFECT_CHAIN
951 // Note: Any effect that does not accumulate does not need mOutBuffer16 and
952 // can do in-place conversion from int16_t to float. We don't optimize here.
953 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
954 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
955 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
956 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
957
958 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
959 __func__, outChannels, outFrameCount, size);
960
961 if (size > 0 && (mOutBuffer16.get() == nullptr || size > mOutBuffer16->getSize())) {
962 mOutBuffer16.clear();
963 ALOGV("%s: allocating mOutBuffer16 %zu", __func__, size);
964 (void)EffectBufferHalInterface::allocate(size, &mOutBuffer16);
965 }
966 if (mOutBuffer16.get() != nullptr) {
967 mOutBuffer16->setFrameCount(outFrameCount);
968 mEffectInterface->setOutBuffer(mOutBuffer16);
969 } else if (size > 0) {
970 ALOGE("%s cannot create mOutBuffer16", __func__);
971 }
972 }
973#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800974}
975
Eric Laurentca7cc822012-11-19 14:55:58 -0800976status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
977{
978 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700979 if (mStatus != NO_ERROR) {
980 return mStatus;
981 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800982 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800983 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
984 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
985 if (isProcessEnabled() &&
986 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
987 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800988 uint32_t volume[2];
989 uint32_t *pVolume = NULL;
990 uint32_t size = sizeof(volume);
991 volume[0] = *left;
992 volume[1] = *right;
993 if (controller) {
994 pVolume = volume;
995 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700996 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
997 size,
998 volume,
999 &size,
1000 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001001 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1002 *left = volume[0];
1003 *right = volume[1];
1004 }
1005 }
1006 return status;
1007}
1008
1009status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1010{
1011 if (device == AUDIO_DEVICE_NONE) {
1012 return NO_ERROR;
1013 }
1014
1015 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001016 if (mStatus != NO_ERROR) {
1017 return mStatus;
1018 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001019 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001020 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001021 status_t cmdStatus;
1022 uint32_t size = sizeof(status_t);
1023 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1024 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001025 status = mEffectInterface->command(cmd,
1026 sizeof(uint32_t),
1027 &device,
1028 &size,
1029 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001030 }
1031 return status;
1032}
1033
1034status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1035{
1036 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001037 if (mStatus != NO_ERROR) {
1038 return mStatus;
1039 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001040 status_t status = NO_ERROR;
1041 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1042 status_t cmdStatus;
1043 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001044 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1045 sizeof(audio_mode_t),
1046 &mode,
1047 &size,
1048 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001049 if (status == NO_ERROR) {
1050 status = cmdStatus;
1051 }
1052 }
1053 return status;
1054}
1055
1056status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1057{
1058 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001059 if (mStatus != NO_ERROR) {
1060 return mStatus;
1061 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001062 status_t status = NO_ERROR;
1063 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1064 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001065 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1066 sizeof(audio_source_t),
1067 &source,
1068 &size,
1069 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001070 }
1071 return status;
1072}
1073
1074void AudioFlinger::EffectModule::setSuspended(bool suspended)
1075{
1076 Mutex::Autolock _l(mLock);
1077 mSuspended = suspended;
1078}
1079
1080bool AudioFlinger::EffectModule::suspended() const
1081{
1082 Mutex::Autolock _l(mLock);
1083 return mSuspended;
1084}
1085
1086bool AudioFlinger::EffectModule::purgeHandles()
1087{
1088 bool enabled = false;
1089 Mutex::Autolock _l(mLock);
1090 for (size_t i = 0; i < mHandles.size(); i++) {
1091 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001092 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 if (handle->hasControl()) {
1094 enabled = handle->enabled();
1095 }
1096 }
1097 }
1098 return enabled;
1099}
1100
Eric Laurent5baf2af2013-09-12 17:37:00 -07001101status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1102{
1103 Mutex::Autolock _l(mLock);
1104 if (mStatus != NO_ERROR) {
1105 return mStatus;
1106 }
1107 status_t status = NO_ERROR;
1108 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1109 status_t cmdStatus;
1110 uint32_t size = sizeof(status_t);
1111 effect_offload_param_t cmd;
1112
1113 cmd.isOffload = offloaded;
1114 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001115 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1116 sizeof(effect_offload_param_t),
1117 &cmd,
1118 &size,
1119 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001120 if (status == NO_ERROR) {
1121 status = cmdStatus;
1122 }
1123 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1124 } else {
1125 if (offloaded) {
1126 status = INVALID_OPERATION;
1127 }
1128 mOffloaded = false;
1129 }
1130 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1131 return status;
1132}
1133
1134bool AudioFlinger::EffectModule::isOffloaded() const
1135{
1136 Mutex::Autolock _l(mLock);
1137 return mOffloaded;
1138}
1139
Marco Nelissenb2208842014-02-07 14:00:50 -08001140String8 effectFlagsToString(uint32_t flags) {
1141 String8 s;
1142
1143 s.append("conn. mode: ");
1144 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1145 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1146 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1147 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1148 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1149 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1150 default: s.append("unknown/reserved"); break;
1151 }
1152 s.append(", ");
1153
1154 s.append("insert pref: ");
1155 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1156 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1157 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1158 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1159 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1160 default: s.append("unknown/reserved"); break;
1161 }
1162 s.append(", ");
1163
1164 s.append("volume mgmt: ");
1165 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1166 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1167 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1168 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1169 default: s.append("unknown/reserved"); break;
1170 }
1171 s.append(", ");
1172
1173 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1174 if (devind) {
1175 s.append("device indication: ");
1176 switch (devind) {
1177 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1178 default: s.append("unknown/reserved"); break;
1179 }
1180 s.append(", ");
1181 }
1182
1183 s.append("input mode: ");
1184 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1185 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1186 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1187 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1188 default: s.append("not set"); break;
1189 }
1190 s.append(", ");
1191
1192 s.append("output mode: ");
1193 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1194 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1195 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1196 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1197 default: s.append("not set"); break;
1198 }
1199 s.append(", ");
1200
1201 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1202 if (accel) {
1203 s.append("hardware acceleration: ");
1204 switch (accel) {
1205 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1206 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1207 default: s.append("unknown/reserved"); break;
1208 }
1209 s.append(", ");
1210 }
1211
1212 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1213 if (modeind) {
1214 s.append("mode indication: ");
1215 switch (modeind) {
1216 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1217 default: s.append("unknown/reserved"); break;
1218 }
1219 s.append(", ");
1220 }
1221
1222 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1223 if (srcind) {
1224 s.append("source indication: ");
1225 switch (srcind) {
1226 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1227 default: s.append("unknown/reserved"); break;
1228 }
1229 s.append(", ");
1230 }
1231
1232 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1233 s.append("offloadable, ");
1234 }
1235
1236 int len = s.length();
1237 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001238 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001239 s.unlockBuffer(len - 2);
1240 }
1241 return s;
1242}
1243
1244
Glenn Kasten0f11b512014-01-31 16:18:54 -08001245void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001246{
1247 const size_t SIZE = 256;
1248 char buffer[SIZE];
1249 String8 result;
1250
1251 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1252 result.append(buffer);
1253
1254 bool locked = AudioFlinger::dumpTryLock(mLock);
1255 // failed to lock - AudioFlinger is probably deadlocked
1256 if (!locked) {
1257 result.append("\t\tCould not lock Fx mutex:\n");
1258 }
1259
1260 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001261 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001262 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001263 result.append(buffer);
1264
1265 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001266 char uuidStr[64];
1267 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1268 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001269 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001270 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1271 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001272 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001273 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001274 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001275 mDescriptor.flags,
1276 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001277 result.append(buffer);
1278 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1279 mDescriptor.name);
1280 result.append(buffer);
1281 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1282 mDescriptor.implementor);
1283 result.append(buffer);
1284
1285 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001286 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001287 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001288 mConfig.inputCfg.buffer.frameCount,
1289 mConfig.inputCfg.samplingRate,
1290 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001291 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001292 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001293 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001294 result.append(buffer);
1295
1296 result.append("\t\t- Output configuration:\n");
1297 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001298 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001299 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001300 mConfig.outputCfg.buffer.frameCount,
1301 mConfig.outputCfg.samplingRate,
1302 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001303 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001304 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001305 result.append(buffer);
1306
rago94a1ee82017-07-21 15:11:02 -07001307#ifdef FLOAT_EFFECT_CHAIN
1308 if (!mSupportsFloat) {
1309 int16_t* pIn16 = mInBuffer16 != 0 ? mInBuffer16->audioBuffer()->s16 : NULL;
1310 int16_t* pOut16 = mOutBuffer16 != 0 ? mOutBuffer16->audioBuffer()->s16 : NULL;
1311
1312 result.append("\t\t- Float and int16 buffers\n");
1313 result.append("\t\t\tIn_float In_int16 Out_float Out_int16\n");
1314 snprintf(buffer, SIZE,"\t\t\t%p %p %p %p\n",
1315 mConfig.inputCfg.buffer.raw,
1316 pIn16,
1317 pOut16,
1318 mConfig.outputCfg.buffer.raw);
1319 result.append(buffer);
1320 }
1321#endif
1322
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001323 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001324 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001325 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001326 for (size_t i = 0; i < mHandles.size(); ++i) {
1327 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001328 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001329 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001330 result.append(buffer);
1331 }
1332 }
1333
Eric Laurentca7cc822012-11-19 14:55:58 -08001334 write(fd, result.string(), result.length());
1335
1336 if (locked) {
1337 mLock.unlock();
1338 }
1339}
1340
1341// ----------------------------------------------------------------------------
1342// EffectHandle implementation
1343// ----------------------------------------------------------------------------
1344
1345#undef LOG_TAG
1346#define LOG_TAG "AudioFlinger::EffectHandle"
1347
1348AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1349 const sp<AudioFlinger::Client>& client,
1350 const sp<IEffectClient>& effectClient,
1351 int32_t priority)
1352 : BnEffect(),
1353 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001354 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001355{
1356 ALOGV("constructor %p", this);
1357
1358 if (client == 0) {
1359 return;
1360 }
1361 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1362 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001363 if (mCblkMemory == 0 ||
1364 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001365 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001366 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001367 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001368 return;
1369 }
Glenn Kastene75da402013-11-20 13:54:52 -08001370 new(mCblk) effect_param_cblk_t();
1371 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001372}
1373
1374AudioFlinger::EffectHandle::~EffectHandle()
1375{
1376 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001377 disconnect(false);
1378}
1379
Glenn Kastene75da402013-11-20 13:54:52 -08001380status_t AudioFlinger::EffectHandle::initCheck()
1381{
1382 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1383}
1384
Eric Laurentca7cc822012-11-19 14:55:58 -08001385status_t AudioFlinger::EffectHandle::enable()
1386{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001387 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001388 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001389 sp<EffectModule> effect = mEffect.promote();
1390 if (effect == 0 || mDisconnected) {
1391 return DEAD_OBJECT;
1392 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001393 if (!mHasControl) {
1394 return INVALID_OPERATION;
1395 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001396
1397 if (mEnabled) {
1398 return NO_ERROR;
1399 }
1400
1401 mEnabled = true;
1402
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001403 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001404 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001405 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001406 }
1407
1408 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001409 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 return NO_ERROR;
1411 }
1412
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001413 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 if (status != NO_ERROR) {
1415 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001416 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 }
1418 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001419 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001420 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001421 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1422 Mutex::Autolock _l(thread->mLock);
1423 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001424 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001425 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001426 if (thread->type() == ThreadBase::OFFLOAD) {
1427 PlaybackThread *t = (PlaybackThread *)thread.get();
1428 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1429 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001430 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001431 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1432 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001433 }
1434 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001435 }
1436 return status;
1437}
1438
1439status_t AudioFlinger::EffectHandle::disable()
1440{
1441 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001442 AutoMutex _l(mLock);
1443 sp<EffectModule> effect = mEffect.promote();
1444 if (effect == 0 || mDisconnected) {
1445 return DEAD_OBJECT;
1446 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001447 if (!mHasControl) {
1448 return INVALID_OPERATION;
1449 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001450
1451 if (!mEnabled) {
1452 return NO_ERROR;
1453 }
1454 mEnabled = false;
1455
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001456 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001457 return NO_ERROR;
1458 }
1459
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001460 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001461
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001462 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001463 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001464 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001465 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1466 Mutex::Autolock _l(thread->mLock);
1467 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001468 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001469 }
1470
1471 return status;
1472}
1473
1474void AudioFlinger::EffectHandle::disconnect()
1475{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001476 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001477 disconnect(true);
1478}
1479
1480void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1481{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001482 AutoMutex _l(mLock);
1483 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1484 if (mDisconnected) {
1485 if (unpinIfLast) {
1486 android_errorWriteLog(0x534e4554, "32707507");
1487 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001488 return;
1489 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001490 mDisconnected = true;
1491 sp<ThreadBase> thread;
1492 {
1493 sp<EffectModule> effect = mEffect.promote();
1494 if (effect != 0) {
1495 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001496 }
1497 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001498 if (thread != 0) {
1499 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001500 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001501 // try to cleanup as much as we can
1502 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001503 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1504 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001505 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001506 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001507
Eric Laurentca7cc822012-11-19 14:55:58 -08001508 if (mClient != 0) {
1509 if (mCblk != NULL) {
1510 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1511 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1512 }
1513 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001514 // Client destructor must run with AudioFlinger client mutex locked
1515 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001516 mClient.clear();
1517 }
1518}
1519
1520status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1521 uint32_t cmdSize,
1522 void *pCmdData,
1523 uint32_t *replySize,
1524 void *pReplyData)
1525{
1526 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001527 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001528
Eric Laurentc7ab3092017-06-15 18:43:46 -07001529 // reject commands reserved for internal use by audio framework if coming from outside
1530 // of audioserver
1531 switch(cmdCode) {
1532 case EFFECT_CMD_ENABLE:
1533 case EFFECT_CMD_DISABLE:
1534 case EFFECT_CMD_SET_PARAM:
1535 case EFFECT_CMD_SET_PARAM_DEFERRED:
1536 case EFFECT_CMD_SET_PARAM_COMMIT:
1537 case EFFECT_CMD_GET_PARAM:
1538 break;
1539 default:
1540 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1541 break;
1542 }
1543 android_errorWriteLog(0x534e4554, "62019992");
1544 return BAD_VALUE;
1545 }
1546
Eric Laurent1ffc5852016-12-15 14:46:09 -08001547 if (cmdCode == EFFECT_CMD_ENABLE) {
1548 if (*replySize < sizeof(int)) {
1549 android_errorWriteLog(0x534e4554, "32095713");
1550 return BAD_VALUE;
1551 }
1552 *(int *)pReplyData = NO_ERROR;
1553 *replySize = sizeof(int);
1554 return enable();
1555 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1556 if (*replySize < sizeof(int)) {
1557 android_errorWriteLog(0x534e4554, "32095713");
1558 return BAD_VALUE;
1559 }
1560 *(int *)pReplyData = NO_ERROR;
1561 *replySize = sizeof(int);
1562 return disable();
1563 }
1564
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001565 AutoMutex _l(mLock);
1566 sp<EffectModule> effect = mEffect.promote();
1567 if (effect == 0 || mDisconnected) {
1568 return DEAD_OBJECT;
1569 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001570 // only get parameter command is permitted for applications not controlling the effect
1571 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1572 return INVALID_OPERATION;
1573 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001574 if (mClient == 0) {
1575 return INVALID_OPERATION;
1576 }
1577
1578 // handle commands that are not forwarded transparently to effect engine
1579 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001580 if (*replySize < sizeof(int)) {
1581 android_errorWriteLog(0x534e4554, "32095713");
1582 return BAD_VALUE;
1583 }
1584 *(int *)pReplyData = NO_ERROR;
1585 *replySize = sizeof(int);
1586
Eric Laurentca7cc822012-11-19 14:55:58 -08001587 // No need to trylock() here as this function is executed in the binder thread serving a
1588 // particular client process: no risk to block the whole media server process or mixer
1589 // threads if we are stuck here
1590 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001591 // keep local copy of index in case of client corruption b/32220769
1592 const uint32_t clientIndex = mCblk->clientIndex;
1593 const uint32_t serverIndex = mCblk->serverIndex;
1594 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1595 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001596 mCblk->serverIndex = 0;
1597 mCblk->clientIndex = 0;
1598 return BAD_VALUE;
1599 }
1600 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001601 effect_param_t *param = NULL;
1602 for (uint32_t index = serverIndex; index < clientIndex;) {
1603 int *p = (int *)(mBuffer + index);
1604 const int size = *p++;
1605 if (size < 0
1606 || size > EFFECT_PARAM_BUFFER_SIZE
1607 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001608 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001609 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001610 break;
1611 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001612
1613 // copy to local memory in case of client corruption b/32220769
1614 param = (effect_param_t *)realloc(param, size);
1615 if (param == NULL) {
1616 ALOGW("command(): out of memory");
1617 status = NO_MEMORY;
1618 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001619 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001620 memcpy(param, p, size);
1621
1622 int reply = 0;
1623 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001624 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001625 size,
1626 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001627 &rsize,
1628 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001629
1630 // verify shared memory: server index shouldn't change; client index can't go back.
1631 if (serverIndex != mCblk->serverIndex
1632 || clientIndex > mCblk->clientIndex) {
1633 android_errorWriteLog(0x534e4554, "32220769");
1634 status = BAD_VALUE;
1635 break;
1636 }
1637
Eric Laurentca7cc822012-11-19 14:55:58 -08001638 // stop at first error encountered
1639 if (ret != NO_ERROR) {
1640 status = ret;
1641 *(int *)pReplyData = reply;
1642 break;
1643 } else if (reply != NO_ERROR) {
1644 *(int *)pReplyData = reply;
1645 break;
1646 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001647 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001648 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001649 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001650 mCblk->serverIndex = 0;
1651 mCblk->clientIndex = 0;
1652 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001653 }
1654
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001655 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001656}
1657
1658void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1659{
1660 ALOGV("setControl %p control %d", this, hasControl);
1661
1662 mHasControl = hasControl;
1663 mEnabled = enabled;
1664
1665 if (signal && mEffectClient != 0) {
1666 mEffectClient->controlStatusChanged(hasControl);
1667 }
1668}
1669
1670void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1671 uint32_t cmdSize,
1672 void *pCmdData,
1673 uint32_t replySize,
1674 void *pReplyData)
1675{
1676 if (mEffectClient != 0) {
1677 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1678 }
1679}
1680
1681
1682
1683void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1684{
1685 if (mEffectClient != 0) {
1686 mEffectClient->enableStatusChanged(enabled);
1687 }
1688}
1689
1690status_t AudioFlinger::EffectHandle::onTransact(
1691 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1692{
1693 return BnEffect::onTransact(code, data, reply, flags);
1694}
1695
1696
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001697void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001698{
1699 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1700
Marco Nelissenb2208842014-02-07 14:00:50 -08001701 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 (mClient == 0) ? getpid_cached : mClient->pid(),
1703 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001704 mHasControl ? "yes" : "no",
1705 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 mCblk ? mCblk->clientIndex : 0,
1707 mCblk ? mCblk->serverIndex : 0
1708 );
1709
1710 if (locked) {
1711 mCblk->lock.unlock();
1712 }
1713}
1714
1715#undef LOG_TAG
1716#define LOG_TAG "AudioFlinger::EffectChain"
1717
1718AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001719 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001720 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001721 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001722 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001723{
1724 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1725 if (thread == NULL) {
1726 return;
1727 }
1728 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1729 thread->frameCount();
1730}
1731
1732AudioFlinger::EffectChain::~EffectChain()
1733{
Eric Laurentca7cc822012-11-19 14:55:58 -08001734}
1735
1736// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1737sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1738 effect_descriptor_t *descriptor)
1739{
1740 size_t size = mEffects.size();
1741
1742 for (size_t i = 0; i < size; i++) {
1743 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1744 return mEffects[i];
1745 }
1746 }
1747 return 0;
1748}
1749
1750// getEffectFromId_l() must be called with ThreadBase::mLock held
1751sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1752{
1753 size_t size = mEffects.size();
1754
1755 for (size_t i = 0; i < size; i++) {
1756 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1757 if (id == 0 || mEffects[i]->id() == id) {
1758 return mEffects[i];
1759 }
1760 }
1761 return 0;
1762}
1763
1764// getEffectFromType_l() must be called with ThreadBase::mLock held
1765sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1766 const effect_uuid_t *type)
1767{
1768 size_t size = mEffects.size();
1769
1770 for (size_t i = 0; i < size; i++) {
1771 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1772 return mEffects[i];
1773 }
1774 }
1775 return 0;
1776}
1777
1778void AudioFlinger::EffectChain::clearInputBuffer()
1779{
1780 Mutex::Autolock _l(mLock);
1781 sp<ThreadBase> thread = mThread.promote();
1782 if (thread == 0) {
1783 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1784 return;
1785 }
1786 clearInputBuffer_l(thread);
1787}
1788
1789// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001790void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001791{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001792 if (mInBuffer == NULL) {
1793 return;
1794 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001795 // TODO: This will change in the future, depending on multichannel
1796 // and sample format changes for effects.
1797 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1798 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001799
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001800 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001801 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1802 * std::min((uint32_t)FCC_2, thread->channelCount());
1803
Mikhail Naganov022b9952017-01-04 16:36:51 -08001804 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1805 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001806}
1807
1808// Must be called with EffectChain::mLock locked
1809void AudioFlinger::EffectChain::process_l()
1810{
1811 sp<ThreadBase> thread = mThread.promote();
1812 if (thread == 0) {
1813 ALOGW("process_l(): cannot promote mixer thread");
1814 return;
1815 }
1816 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1817 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001818 // never process effects when:
1819 // - on an OFFLOAD thread
1820 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001821 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1822 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001823 if (!isGlobalSession) {
1824 bool tracksOnSession = (trackCnt() != 0);
1825
1826 if (!tracksOnSession && mTailBufferCount == 0) {
1827 doProcess = false;
1828 }
1829
1830 if (activeTrackCnt() == 0) {
1831 // if no track is active and the effect tail has not been rendered,
1832 // the input buffer must be cleared here as the mixer process will not do it
1833 if (tracksOnSession || mTailBufferCount > 0) {
1834 clearInputBuffer_l(thread);
1835 if (mTailBufferCount > 0) {
1836 mTailBufferCount--;
1837 }
1838 }
1839 }
1840 }
1841
1842 size_t size = mEffects.size();
1843 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001844 // Only the input and output buffers of the chain can be external,
1845 // and 'update' / 'commit' do nothing for allocated buffers, thus
1846 // it's not needed to consider any other buffers here.
1847 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001848 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1849 mOutBuffer->update();
1850 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 for (size_t i = 0; i < size; i++) {
1852 mEffects[i]->process();
1853 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001854 mInBuffer->commit();
1855 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1856 mOutBuffer->commit();
1857 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001858 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001859 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001860 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001861 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1862 }
1863 if (doResetVolume) {
1864 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 }
1866}
1867
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001868// createEffect_l() must be called with ThreadBase::mLock held
1869status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1870 ThreadBase *thread,
1871 effect_descriptor_t *desc,
1872 int id,
1873 audio_session_t sessionId,
1874 bool pinned)
1875{
1876 Mutex::Autolock _l(mLock);
1877 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1878 status_t lStatus = effect->status();
1879 if (lStatus == NO_ERROR) {
1880 lStatus = addEffect_ll(effect);
1881 }
1882 if (lStatus != NO_ERROR) {
1883 effect.clear();
1884 }
1885 return lStatus;
1886}
1887
1888// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001889status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1890{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001891 Mutex::Autolock _l(mLock);
1892 return addEffect_ll(effect);
1893}
1894// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1895status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1896{
Eric Laurentca7cc822012-11-19 14:55:58 -08001897 effect_descriptor_t desc = effect->desc();
1898 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1899
Eric Laurentca7cc822012-11-19 14:55:58 -08001900 effect->setChain(this);
1901 sp<ThreadBase> thread = mThread.promote();
1902 if (thread == 0) {
1903 return NO_INIT;
1904 }
1905 effect->setThread(thread);
1906
1907 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1908 // Auxiliary effects are inserted at the beginning of mEffects vector as
1909 // they are processed first and accumulated in chain input buffer
1910 mEffects.insertAt(effect, 0);
1911
1912 // the input buffer for auxiliary effect contains mono samples in
1913 // 32 bit format. This is to avoid saturation in AudoMixer
1914 // accumulation stage. Saturation is done in EffectModule::process() before
1915 // calling the process in effect engine
1916 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001917 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001918#ifdef FLOAT_EFFECT_CHAIN
1919 status_t result = EffectBufferHalInterface::allocate(
1920 numSamples * sizeof(float), &halBuffer);
1921#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001922 status_t result = EffectBufferHalInterface::allocate(
1923 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001924#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001925 if (result != OK) return result;
1926 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 // auxiliary effects output samples to chain input buffer for further processing
1928 // by insert effects
1929 effect->setOutBuffer(mInBuffer);
1930 } else {
1931 // Insert effects are inserted at the end of mEffects vector as they are processed
1932 // after track and auxiliary effects.
1933 // Insert effect order as a function of indicated preference:
1934 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1935 // another effect is present
1936 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1937 // last effect claiming first position
1938 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1939 // first effect claiming last position
1940 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1941 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1942 // already present
1943
1944 size_t size = mEffects.size();
1945 size_t idx_insert = size;
1946 ssize_t idx_insert_first = -1;
1947 ssize_t idx_insert_last = -1;
1948
1949 for (size_t i = 0; i < size; i++) {
1950 effect_descriptor_t d = mEffects[i]->desc();
1951 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1952 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1953 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1954 // check invalid effect chaining combinations
1955 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1956 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1957 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1958 desc.name, d.name);
1959 return INVALID_OPERATION;
1960 }
1961 // remember position of first insert effect and by default
1962 // select this as insert position for new effect
1963 if (idx_insert == size) {
1964 idx_insert = i;
1965 }
1966 // remember position of last insert effect claiming
1967 // first position
1968 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1969 idx_insert_first = i;
1970 }
1971 // remember position of first insert effect claiming
1972 // last position
1973 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1974 idx_insert_last == -1) {
1975 idx_insert_last = i;
1976 }
1977 }
1978 }
1979
1980 // modify idx_insert from first position if needed
1981 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1982 if (idx_insert_last != -1) {
1983 idx_insert = idx_insert_last;
1984 } else {
1985 idx_insert = size;
1986 }
1987 } else {
1988 if (idx_insert_first != -1) {
1989 idx_insert = idx_insert_first + 1;
1990 }
1991 }
1992
1993 // always read samples from chain input buffer
1994 effect->setInBuffer(mInBuffer);
1995
1996 // if last effect in the chain, output samples to chain
1997 // output buffer, otherwise to chain input buffer
1998 if (idx_insert == size) {
1999 if (idx_insert != 0) {
2000 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2001 mEffects[idx_insert-1]->configure();
2002 }
2003 effect->setOutBuffer(mOutBuffer);
2004 } else {
2005 effect->setOutBuffer(mInBuffer);
2006 }
2007 mEffects.insertAt(effect, idx_insert);
2008
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002009 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002010 idx_insert);
2011 }
2012 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002013
Eric Laurentca7cc822012-11-19 14:55:58 -08002014 return NO_ERROR;
2015}
2016
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002017// removeEffect_l() must be called with ThreadBase::mLock held
2018size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2019 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002020{
2021 Mutex::Autolock _l(mLock);
2022 size_t size = mEffects.size();
2023 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2024
2025 for (size_t i = 0; i < size; i++) {
2026 if (effect == mEffects[i]) {
2027 // calling stop here will remove pre-processing effect from the audio HAL.
2028 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2029 // the middle of a read from audio HAL
2030 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2031 mEffects[i]->state() == EffectModule::STOPPING) {
2032 mEffects[i]->stop();
2033 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002034 if (release) {
2035 mEffects[i]->release_l();
2036 }
2037
Mikhail Naganov022b9952017-01-04 16:36:51 -08002038 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002039 if (i == size - 1 && i != 0) {
2040 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2041 mEffects[i - 1]->configure();
2042 }
2043 }
2044 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002045 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002046 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002047
Eric Laurentca7cc822012-11-19 14:55:58 -08002048 break;
2049 }
2050 }
2051
2052 return mEffects.size();
2053}
2054
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002055// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002056void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2057{
2058 size_t size = mEffects.size();
2059 for (size_t i = 0; i < size; i++) {
2060 mEffects[i]->setDevice(device);
2061 }
2062}
2063
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002064// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002065void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2066{
2067 size_t size = mEffects.size();
2068 for (size_t i = 0; i < size; i++) {
2069 mEffects[i]->setMode(mode);
2070 }
2071}
2072
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002073// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002074void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2075{
2076 size_t size = mEffects.size();
2077 for (size_t i = 0; i < size; i++) {
2078 mEffects[i]->setAudioSource(source);
2079 }
2080}
2081
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002082// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002083bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002084{
2085 uint32_t newLeft = *left;
2086 uint32_t newRight = *right;
2087 bool hasControl = false;
2088 int ctrlIdx = -1;
2089 size_t size = mEffects.size();
2090
2091 // first update volume controller
2092 for (size_t i = size; i > 0; i--) {
2093 if (mEffects[i - 1]->isProcessEnabled() &&
2094 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2095 ctrlIdx = i - 1;
2096 hasControl = true;
2097 break;
2098 }
2099 }
2100
Eric Laurentfa1e1232016-08-02 19:01:49 -07002101 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002102 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002103 if (hasControl) {
2104 *left = mNewLeftVolume;
2105 *right = mNewRightVolume;
2106 }
2107 return hasControl;
2108 }
2109
2110 mVolumeCtrlIdx = ctrlIdx;
2111 mLeftVolume = newLeft;
2112 mRightVolume = newRight;
2113
2114 // second get volume update from volume controller
2115 if (ctrlIdx >= 0) {
2116 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2117 mNewLeftVolume = newLeft;
2118 mNewRightVolume = newRight;
2119 }
2120 // then indicate volume to all other effects in chain.
2121 // Pass altered volume to effects before volume controller
2122 // and requested volume to effects after controller
2123 uint32_t lVol = newLeft;
2124 uint32_t rVol = newRight;
2125
2126 for (size_t i = 0; i < size; i++) {
2127 if ((int)i == ctrlIdx) {
2128 continue;
2129 }
2130 // this also works for ctrlIdx == -1 when there is no volume controller
2131 if ((int)i > ctrlIdx) {
2132 lVol = *left;
2133 rVol = *right;
2134 }
2135 mEffects[i]->setVolume(&lVol, &rVol, false);
2136 }
2137 *left = newLeft;
2138 *right = newRight;
2139
2140 return hasControl;
2141}
2142
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002143// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002144void AudioFlinger::EffectChain::resetVolume_l()
2145{
Eric Laurente7449bf2016-08-03 18:44:07 -07002146 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2147 uint32_t left = mLeftVolume;
2148 uint32_t right = mRightVolume;
2149 (void)setVolume_l(&left, &right, true);
2150 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002151}
2152
Eric Laurent1b928682014-10-02 19:41:47 -07002153void AudioFlinger::EffectChain::syncHalEffectsState()
2154{
2155 Mutex::Autolock _l(mLock);
2156 for (size_t i = 0; i < mEffects.size(); i++) {
2157 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2158 mEffects[i]->state() == EffectModule::STOPPING) {
2159 mEffects[i]->addEffectToHal_l();
2160 }
2161 }
2162}
2163
Mikhail Naganov06888802017-01-19 12:47:55 -08002164static void dumpInOutBuffer(
2165 char *dump, size_t dumpSize, bool isInput, EffectBufferHalInterface *buffer) {
Mikhail Naganovc778e592017-01-25 10:35:30 -08002166 if (buffer == nullptr) {
2167 snprintf(dump, dumpSize, "%p", buffer);
2168 } else if (buffer->externalData() != nullptr) {
Mikhail Naganov06888802017-01-19 12:47:55 -08002169 snprintf(dump, dumpSize, "%p -> %p",
2170 isInput ? buffer->externalData() : buffer->audioBuffer()->raw,
2171 isInput ? buffer->audioBuffer()->raw : buffer->externalData());
2172 } else {
2173 snprintf(dump, dumpSize, "%p", buffer->audioBuffer()->raw);
2174 }
2175}
2176
Eric Laurentca7cc822012-11-19 14:55:58 -08002177void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2178{
2179 const size_t SIZE = 256;
2180 char buffer[SIZE];
2181 String8 result;
2182
Marco Nelissenb2208842014-02-07 14:00:50 -08002183 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002184 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002185 result.append(buffer);
2186
Marco Nelissenb2208842014-02-07 14:00:50 -08002187 if (numEffects) {
2188 bool locked = AudioFlinger::dumpTryLock(mLock);
2189 // failed to lock - AudioFlinger is probably deadlocked
2190 if (!locked) {
2191 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002192 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002193
Mikhail Naganov06888802017-01-19 12:47:55 -08002194 char inBufferStr[64], outBufferStr[64];
2195 dumpInOutBuffer(inBufferStr, sizeof(inBufferStr), true, mInBuffer.get());
2196 dumpInOutBuffer(outBufferStr, sizeof(outBufferStr), false, mOutBuffer.get());
2197 snprintf(buffer, SIZE, "\t%-*s%-*s Active tracks:\n",
2198 (int)strlen(inBufferStr), "In buffer ",
2199 (int)strlen(outBufferStr), "Out buffer ");
2200 result.append(buffer);
2201 snprintf(buffer, SIZE, "\t%s %s %d\n", inBufferStr, outBufferStr, mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002202 result.append(buffer);
2203 write(fd, result.string(), result.size());
2204
2205 for (size_t i = 0; i < numEffects; ++i) {
2206 sp<EffectModule> effect = mEffects[i];
2207 if (effect != 0) {
2208 effect->dump(fd, args);
2209 }
2210 }
2211
2212 if (locked) {
2213 mLock.unlock();
2214 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002215 }
2216}
2217
2218// must be called with ThreadBase::mLock held
2219void AudioFlinger::EffectChain::setEffectSuspended_l(
2220 const effect_uuid_t *type, bool suspend)
2221{
2222 sp<SuspendedEffectDesc> desc;
2223 // use effect type UUID timelow as key as there is no real risk of identical
2224 // timeLow fields among effect type UUIDs.
2225 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2226 if (suspend) {
2227 if (index >= 0) {
2228 desc = mSuspendedEffects.valueAt(index);
2229 } else {
2230 desc = new SuspendedEffectDesc();
2231 desc->mType = *type;
2232 mSuspendedEffects.add(type->timeLow, desc);
2233 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2234 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002235
Eric Laurentca7cc822012-11-19 14:55:58 -08002236 if (desc->mRefCount++ == 0) {
2237 sp<EffectModule> effect = getEffectIfEnabled(type);
2238 if (effect != 0) {
2239 desc->mEffect = effect;
2240 effect->setSuspended(true);
2241 effect->setEnabled(false);
2242 }
2243 }
2244 } else {
2245 if (index < 0) {
2246 return;
2247 }
2248 desc = mSuspendedEffects.valueAt(index);
2249 if (desc->mRefCount <= 0) {
2250 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002251 desc->mRefCount = 0;
2252 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002253 }
2254 if (--desc->mRefCount == 0) {
2255 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2256 if (desc->mEffect != 0) {
2257 sp<EffectModule> effect = desc->mEffect.promote();
2258 if (effect != 0) {
2259 effect->setSuspended(false);
2260 effect->lock();
2261 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002262 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002263 effect->setEnabled_l(handle->enabled());
2264 }
2265 effect->unlock();
2266 }
2267 desc->mEffect.clear();
2268 }
2269 mSuspendedEffects.removeItemsAt(index);
2270 }
2271 }
2272}
2273
2274// must be called with ThreadBase::mLock held
2275void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2276{
2277 sp<SuspendedEffectDesc> desc;
2278
2279 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2280 if (suspend) {
2281 if (index >= 0) {
2282 desc = mSuspendedEffects.valueAt(index);
2283 } else {
2284 desc = new SuspendedEffectDesc();
2285 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2286 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2287 }
2288 if (desc->mRefCount++ == 0) {
2289 Vector< sp<EffectModule> > effects;
2290 getSuspendEligibleEffects(effects);
2291 for (size_t i = 0; i < effects.size(); i++) {
2292 setEffectSuspended_l(&effects[i]->desc().type, true);
2293 }
2294 }
2295 } else {
2296 if (index < 0) {
2297 return;
2298 }
2299 desc = mSuspendedEffects.valueAt(index);
2300 if (desc->mRefCount <= 0) {
2301 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2302 desc->mRefCount = 1;
2303 }
2304 if (--desc->mRefCount == 0) {
2305 Vector<const effect_uuid_t *> types;
2306 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2307 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2308 continue;
2309 }
2310 types.add(&mSuspendedEffects.valueAt(i)->mType);
2311 }
2312 for (size_t i = 0; i < types.size(); i++) {
2313 setEffectSuspended_l(types[i], false);
2314 }
2315 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2316 mSuspendedEffects.keyAt(index));
2317 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2318 }
2319 }
2320}
2321
2322
2323// The volume effect is used for automated tests only
2324#ifndef OPENSL_ES_H_
2325static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2326 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2327const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2328#endif //OPENSL_ES_H_
2329
Eric Laurentd8365c52017-07-16 15:27:05 -07002330/* static */
2331bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2332{
2333 // Only NS and AEC are suspended when BtNRec is off
2334 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2335 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2336 return true;
2337 }
2338 return false;
2339}
2340
Eric Laurentca7cc822012-11-19 14:55:58 -08002341bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2342{
2343 // auxiliary effects and visualizer are never suspended on output mix
2344 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2345 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2346 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2347 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2348 return false;
2349 }
2350 return true;
2351}
2352
2353void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2354 Vector< sp<AudioFlinger::EffectModule> > &effects)
2355{
2356 effects.clear();
2357 for (size_t i = 0; i < mEffects.size(); i++) {
2358 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2359 effects.add(mEffects[i]);
2360 }
2361 }
2362}
2363
2364sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2365 const effect_uuid_t *type)
2366{
2367 sp<EffectModule> effect = getEffectFromType_l(type);
2368 return effect != 0 && effect->isEnabled() ? effect : 0;
2369}
2370
2371void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2372 bool enabled)
2373{
2374 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2375 if (enabled) {
2376 if (index < 0) {
2377 // if the effect is not suspend check if all effects are suspended
2378 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2379 if (index < 0) {
2380 return;
2381 }
2382 if (!isEffectEligibleForSuspend(effect->desc())) {
2383 return;
2384 }
2385 setEffectSuspended_l(&effect->desc().type, enabled);
2386 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2387 if (index < 0) {
2388 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2389 return;
2390 }
2391 }
2392 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2393 effect->desc().type.timeLow);
2394 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002395 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002396 if (desc->mEffect == 0) {
2397 desc->mEffect = effect;
2398 effect->setEnabled(false);
2399 effect->setSuspended(true);
2400 }
2401 } else {
2402 if (index < 0) {
2403 return;
2404 }
2405 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2406 effect->desc().type.timeLow);
2407 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2408 desc->mEffect.clear();
2409 effect->setSuspended(false);
2410 }
2411}
2412
Eric Laurent5baf2af2013-09-12 17:37:00 -07002413bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002414{
2415 Mutex::Autolock _l(mLock);
2416 size_t size = mEffects.size();
2417 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002418 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002419 return true;
2420 }
2421 }
2422 return false;
2423}
2424
Eric Laurentaaa44472014-09-12 17:41:50 -07002425void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2426{
2427 Mutex::Autolock _l(mLock);
2428 mThread = thread;
2429 for (size_t i = 0; i < mEffects.size(); i++) {
2430 mEffects[i]->setThread(thread);
2431 }
2432}
2433
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002434void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2435{
2436 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2437 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2438 }
2439 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2440 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2441 }
2442}
2443
2444void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2445{
2446 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2447 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2448 }
2449 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2450 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2451 }
2452}
2453
2454bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002455{
2456 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002457 for (const auto &effect : mEffects) {
2458 if (effect->isProcessImplemented()) {
2459 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002460 }
2461 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002462 // Allow effects without processing.
2463 return true;
2464}
2465
2466bool AudioFlinger::EffectChain::isFastCompatible() const
2467{
2468 Mutex::Autolock _l(mLock);
2469 for (const auto &effect : mEffects) {
2470 if (effect->isProcessImplemented()
2471 && effect->isImplementationSoftware()) {
2472 return false;
2473 }
2474 }
2475 // Allow effects without processing or hw accelerated effects.
2476 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002477}
2478
2479// isCompatibleWithThread_l() must be called with thread->mLock held
2480bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2481{
2482 Mutex::Autolock _l(mLock);
2483 for (size_t i = 0; i < mEffects.size(); i++) {
2484 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2485 return false;
2486 }
2487 }
2488 return true;
2489}
2490
Glenn Kasten63238ef2015-03-02 15:50:29 -08002491} // namespace android