blob: bde32e730ea1cbba2300b229b186b5a763b7a15e [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
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080023#include <utils/Log.h>
24#include <audio_effects/effect_visualizer.h>
25#include <audio_utils/primitives.h>
26#include <private/media/AudioEffectShared.h>
27#include <media/EffectsFactoryApi.h>
28
29#include "AudioFlinger.h"
30#include "ServiceUtilities.h"
31
32// ----------------------------------------------------------------------------
33
34// Note: the following macro is used for extremely verbose logging message. In
35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
36// 0; but one side effect of this is to turn all LOGV's as well. Some messages
37// are so verbose that we want to suppress them even when we have ALOG_ASSERT
38// turned on. Do not uncomment the #def below unless you really know what you
39// are doing and want to see all of the extremely verbose messages.
40//#define VERY_VERY_VERBOSE_LOGGING
41#ifdef VERY_VERY_VERBOSE_LOGGING
42#define ALOGVV ALOGV
43#else
44#define ALOGVV(a...) do { } while(0)
45#endif
46
Ricardo Garcia726b6a72014-08-11 12:04:54 -070047#define min(a, b) ((a) < (b) ? (a) : (b))
48
Eric Laurentca7cc822012-11-19 14:55:58 -080049namespace android {
50
51// ----------------------------------------------------------------------------
52// EffectModule implementation
53// ----------------------------------------------------------------------------
54
55#undef LOG_TAG
56#define LOG_TAG "AudioFlinger::EffectModule"
57
58AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
59 const wp<AudioFlinger::EffectChain>& chain,
60 effect_descriptor_t *desc,
61 int id,
Eric Laurentb37f28a2016-12-01 15:28:29 -080062 audio_session_t sessionId,
63 bool pinned)
64 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080065 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
66 mDescriptor(*desc),
67 // mConfig is set by configure() and not used before then
68 mEffectInterface(NULL),
69 mStatus(NO_INIT), mState(IDLE),
70 // mMaxDisableWaitCnt is set by configure() and not used before then
71 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070072 mSuspended(false),
73 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080074{
Eric Laurentb37f28a2016-12-01 15:28:29 -080075 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080076 int lStatus;
77
78 // create effect engine from effect factory
79 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
80
81 if (mStatus != NO_ERROR) {
82 return;
83 }
84 lStatus = init();
85 if (lStatus < 0) {
86 mStatus = lStatus;
87 goto Error;
88 }
89
Eric Laurentb37f28a2016-12-01 15:28:29 -080090 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
91
Eric Laurentca7cc822012-11-19 14:55:58 -080092 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
93 return;
94Error:
95 EffectRelease(mEffectInterface);
96 mEffectInterface = NULL;
97 ALOGV("Constructor Error %d", mStatus);
98}
99
100AudioFlinger::EffectModule::~EffectModule()
101{
102 ALOGV("Destructor %p", this);
103 if (mEffectInterface != NULL) {
Eric Laurentb37f28a2016-12-01 15:28:29 -0800104 ALOGW("EffectModule %p destructor called with unreleased interface", this);
105 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800106 }
107}
108
109status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
110{
111 status_t status;
112
113 Mutex::Autolock _l(mLock);
114 int priority = handle->priority();
115 size_t size = mHandles.size();
116 EffectHandle *controlHandle = NULL;
117 size_t i;
118 for (i = 0; i < size; i++) {
119 EffectHandle *h = mHandles[i];
Eric Laurentb37f28a2016-12-01 15:28:29 -0800120 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800121 continue;
122 }
123 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700124 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800125 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700126 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800127 if (h->priority() <= priority) {
128 break;
129 }
130 }
131 // if inserted in first place, move effect control from previous owner to this handle
132 if (i == 0) {
133 bool enabled = false;
134 if (controlHandle != NULL) {
135 enabled = controlHandle->enabled();
136 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
137 }
138 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
139 status = NO_ERROR;
140 } else {
141 status = ALREADY_EXISTS;
142 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700143 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800144 mHandles.insertAt(handle, i);
145 return status;
146}
147
Eric Laurentb37f28a2016-12-01 15:28:29 -0800148ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800149{
150 Mutex::Autolock _l(mLock);
Eric Laurentb37f28a2016-12-01 15:28:29 -0800151 return removeHandle_l(handle);
152}
153
154ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
155{
Eric Laurentca7cc822012-11-19 14:55:58 -0800156 size_t size = mHandles.size();
157 size_t i;
158 for (i = 0; i < size; i++) {
159 if (mHandles[i] == handle) {
160 break;
161 }
162 }
163 if (i == size) {
Eric Laurentb37f28a2016-12-01 15:28:29 -0800164 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
165 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800166 }
Eric Laurentb37f28a2016-12-01 15:28:29 -0800167 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800168
169 mHandles.removeAt(i);
170 // if removed from first place, move effect control from this handle to next in line
171 if (i == 0) {
172 EffectHandle *h = controlHandle_l();
173 if (h != NULL) {
174 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
175 }
176 }
177
178 // Prevent calls to process() and other functions on effect interface from now on.
179 // The effect engine will be released by the destructor when the last strong reference on
180 // this object is released which can happen after next process is called.
181 if (mHandles.size() == 0 && !mPinned) {
182 mState = DESTROYED;
183 }
184
185 return mHandles.size();
186}
187
188// must be called with EffectModule::mLock held
189AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
190{
191 // the first valid handle in the list has control over the module
192 for (size_t i = 0; i < mHandles.size(); i++) {
193 EffectHandle *h = mHandles[i];
Eric Laurentb37f28a2016-12-01 15:28:29 -0800194 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800195 return h;
196 }
197 }
198
199 return NULL;
200}
201
Eric Laurentb37f28a2016-12-01 15:28:29 -0800202// unsafe method called when the effect parent thread has been destroyed
203ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentca7cc822012-11-19 14:55:58 -0800204{
205 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurentb37f28a2016-12-01 15:28:29 -0800206 Mutex::Autolock _l(mLock);
207 ssize_t numHandles = removeHandle_l(handle);
208 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
209 AudioSystem::unregisterEffect(mId);
210 sp<AudioFlinger> af = mAudioFlinger.promote();
211 if (af != 0) {
212 mLock.unlock();
213 af->updateOrphanEffectChains(this);
214 mLock.lock();
Eric Laurentca7cc822012-11-19 14:55:58 -0800215 }
216 }
Eric Laurentb37f28a2016-12-01 15:28:29 -0800217 return numHandles;
Eric Laurentca7cc822012-11-19 14:55:58 -0800218}
219
220void AudioFlinger::EffectModule::updateState() {
221 Mutex::Autolock _l(mLock);
222
223 switch (mState) {
224 case RESTART:
225 reset_l();
226 // FALL THROUGH
227
228 case STARTING:
229 // clear auxiliary effect input buffer for next accumulation
230 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
231 memset(mConfig.inputCfg.buffer.raw,
232 0,
233 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
234 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700235 if (start_l() == NO_ERROR) {
236 mState = ACTIVE;
237 } else {
238 mState = IDLE;
239 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800240 break;
241 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700242 if (stop_l() == NO_ERROR) {
243 mDisableWaitCnt = mMaxDisableWaitCnt;
244 } else {
245 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
246 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800247 mState = STOPPED;
248 break;
249 case STOPPED:
250 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
251 // turn off sequence.
252 if (--mDisableWaitCnt == 0) {
253 reset_l();
254 mState = IDLE;
255 }
256 break;
257 default: //IDLE , ACTIVE, DESTROYED
258 break;
259 }
260}
261
262void AudioFlinger::EffectModule::process()
263{
264 Mutex::Autolock _l(mLock);
265
266 if (mState == DESTROYED || mEffectInterface == NULL ||
267 mConfig.inputCfg.buffer.raw == NULL ||
268 mConfig.outputCfg.buffer.raw == NULL) {
269 return;
270 }
271
272 if (isProcessEnabled()) {
273 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
274 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
275 ditherAndClamp(mConfig.inputCfg.buffer.s32,
276 mConfig.inputCfg.buffer.s32,
277 mConfig.inputCfg.buffer.frameCount/2);
278 }
279
280 // do the actual processing in the effect engine
281 int ret = (*mEffectInterface)->process(mEffectInterface,
282 &mConfig.inputCfg.buffer,
283 &mConfig.outputCfg.buffer);
284
285 // force transition to IDLE state when engine is ready
286 if (mState == STOPPED && ret == -ENODATA) {
287 mDisableWaitCnt = 1;
288 }
289
290 // clear auxiliary effect input buffer for next accumulation
291 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
292 memset(mConfig.inputCfg.buffer.raw, 0,
293 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
294 }
295 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
296 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
297 // If an insert effect is idle and input buffer is different from output buffer,
298 // accumulate input onto output
299 sp<EffectChain> chain = mChain.promote();
300 if (chain != 0 && chain->activeTrackCnt() != 0) {
301 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
302 int16_t *in = mConfig.inputCfg.buffer.s16;
303 int16_t *out = mConfig.outputCfg.buffer.s16;
304 for (size_t i = 0; i < frameCnt; i++) {
305 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
306 }
307 }
308 }
309}
310
311void AudioFlinger::EffectModule::reset_l()
312{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700313 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 return;
315 }
316 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
317}
318
319status_t AudioFlinger::EffectModule::configure()
320{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700321 status_t status;
322 sp<ThreadBase> thread;
323 uint32_t size;
324 audio_channel_mask_t channelMask;
325
Eric Laurentca7cc822012-11-19 14:55:58 -0800326 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700327 status = NO_INIT;
328 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800329 }
330
Eric Laurentd0ebb532013-04-02 16:41:41 -0700331 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800332 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700333 status = DEAD_OBJECT;
334 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800335 }
336
337 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700338 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700339 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800340
341 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
342 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
343 } else {
344 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700345 // TODO: Update this logic when multichannel effects are implemented.
346 // For offloaded tracks consider mono output as stereo for proper effect initialization
347 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
348 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
349 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
350 ALOGV("Overriding effect input and output as STEREO");
351 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800352 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700353
Eric Laurentca7cc822012-11-19 14:55:58 -0800354 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
355 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
356 mConfig.inputCfg.samplingRate = thread->sampleRate();
357 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
358 mConfig.inputCfg.bufferProvider.cookie = NULL;
359 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
360 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
361 mConfig.outputCfg.bufferProvider.cookie = NULL;
362 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
363 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
364 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
365 // Insert effect:
366 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
367 // always overwrites output buffer: input buffer == output buffer
368 // - in other sessions:
369 // last effect in the chain accumulates in output buffer: input buffer != output buffer
370 // other effect: overwrites output buffer: input buffer == output buffer
371 // Auxiliary effect:
372 // accumulates in output buffer: input buffer != output buffer
373 // Therefore: accumulate <=> input buffer != output buffer
374 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
375 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
376 } else {
377 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
378 }
379 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
380 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
381 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
382 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
383
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700384 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800385 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
386
387 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700388 size = sizeof(int);
389 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800390 EFFECT_CMD_SET_CONFIG,
391 sizeof(effect_config_t),
392 &mConfig,
393 &size,
394 &cmdStatus);
395 if (status == 0) {
396 status = cmdStatus;
397 }
398
399 if (status == 0 &&
400 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
401 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
402 effect_param_t *p = (effect_param_t *)buf32;
403
404 p->psize = sizeof(uint32_t);
405 p->vsize = sizeof(uint32_t);
406 size = sizeof(int);
407 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
408
409 uint32_t latency = 0;
410 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
411 if (pbt != NULL) {
412 latency = pbt->latency_l();
413 }
414
415 *((int32_t *)p->data + 1)= latency;
416 (*mEffectInterface)->command(mEffectInterface,
417 EFFECT_CMD_SET_PARAM,
418 sizeof(effect_param_t) + 8,
419 &buf32,
420 &size,
421 &cmdStatus);
422 }
423
424 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
425 (1000 * mConfig.outputCfg.buffer.frameCount);
426
Eric Laurentd0ebb532013-04-02 16:41:41 -0700427exit:
428 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800429 return status;
430}
431
432status_t AudioFlinger::EffectModule::init()
433{
434 Mutex::Autolock _l(mLock);
435 if (mEffectInterface == NULL) {
436 return NO_INIT;
437 }
438 status_t cmdStatus;
439 uint32_t size = sizeof(status_t);
440 status_t status = (*mEffectInterface)->command(mEffectInterface,
441 EFFECT_CMD_INIT,
442 0,
443 NULL,
444 &size,
445 &cmdStatus);
446 if (status == 0) {
447 status = cmdStatus;
448 }
449 return status;
450}
451
Eric Laurent1b928682014-10-02 19:41:47 -0700452void AudioFlinger::EffectModule::addEffectToHal_l()
453{
454 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
455 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
456 sp<ThreadBase> thread = mThread.promote();
457 if (thread != 0) {
458 audio_stream_t *stream = thread->stream();
459 if (stream != NULL) {
460 stream->add_audio_effect(stream, mEffectInterface);
461 }
462 }
463 }
464}
465
Eric Laurentca7cc822012-11-19 14:55:58 -0800466status_t AudioFlinger::EffectModule::start()
467{
468 Mutex::Autolock _l(mLock);
469 return start_l();
470}
471
472status_t AudioFlinger::EffectModule::start_l()
473{
474 if (mEffectInterface == NULL) {
475 return NO_INIT;
476 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700477 if (mStatus != NO_ERROR) {
478 return mStatus;
479 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800480 status_t cmdStatus;
481 uint32_t size = sizeof(status_t);
482 status_t status = (*mEffectInterface)->command(mEffectInterface,
483 EFFECT_CMD_ENABLE,
484 0,
485 NULL,
486 &size,
487 &cmdStatus);
488 if (status == 0) {
489 status = cmdStatus;
490 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700491 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700492 addEffectToHal_l();
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700493 sp<EffectChain> chain = mChain.promote();
494 if (chain != 0) {
495 chain->forceVolume();
496 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800497 }
498 return status;
499}
500
501status_t AudioFlinger::EffectModule::stop()
502{
503 Mutex::Autolock _l(mLock);
504 return stop_l();
505}
506
507status_t AudioFlinger::EffectModule::stop_l()
508{
509 if (mEffectInterface == NULL) {
510 return NO_INIT;
511 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700512 if (mStatus != NO_ERROR) {
513 return mStatus;
514 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800515 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800516 uint32_t size = sizeof(status_t);
517 status_t status = (*mEffectInterface)->command(mEffectInterface,
518 EFFECT_CMD_DISABLE,
519 0,
520 NULL,
521 &size,
522 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800523 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800524 status = cmdStatus;
525 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800526 if (status == NO_ERROR) {
527 status = remove_effect_from_hal_l();
528 }
529 return status;
530}
531
Eric Laurentb37f28a2016-12-01 15:28:29 -0800532// must be called with EffectChain::mLock held
533void AudioFlinger::EffectModule::release_l()
534{
535 if (mEffectInterface != NULL) {
536 remove_effect_from_hal_l();
537 // release effect engine
538 EffectRelease(mEffectInterface);
539 mEffectInterface = NULL;
540 }
541}
542
Eric Laurentbfb1b832013-01-07 09:53:42 -0800543status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
544{
545 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
546 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800547 sp<ThreadBase> thread = mThread.promote();
548 if (thread != 0) {
549 audio_stream_t *stream = thread->stream();
550 if (stream != NULL) {
551 stream->remove_audio_effect(stream, mEffectInterface);
552 }
553 }
554 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800555 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800556}
557
Andy Hunge4a1d912016-08-17 14:11:13 -0700558// round up delta valid if value and divisor are positive.
559template <typename T>
560static T roundUpDelta(const T &value, const T &divisor) {
561 T remainder = value % divisor;
562 return remainder == 0 ? 0 : divisor - remainder;
563}
564
Eric Laurentca7cc822012-11-19 14:55:58 -0800565status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
566 uint32_t cmdSize,
567 void *pCmdData,
568 uint32_t *replySize,
569 void *pReplyData)
570{
571 Mutex::Autolock _l(mLock);
572 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
573
574 if (mState == DESTROYED || mEffectInterface == NULL) {
575 return NO_INIT;
576 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700577 if (mStatus != NO_ERROR) {
578 return mStatus;
579 }
Andy Hung110bc952016-06-20 15:22:52 -0700580 if (cmdCode == EFFECT_CMD_GET_PARAM &&
581 (*replySize < sizeof(effect_param_t) ||
582 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
583 android_errorWriteLog(0x534e4554, "29251553");
584 return -EINVAL;
585 }
Andy Hung3d34cc72016-11-04 19:40:53 -0700586 if (cmdCode == EFFECT_CMD_GET_PARAM &&
587 (sizeof(effect_param_t) > cmdSize ||
588 ((effect_param_t *)pCmdData)->psize > cmdSize
589 - sizeof(effect_param_t))) {
590 android_errorWriteLog(0x534e4554, "32438594");
591 return -EINVAL;
592 }
ragoe2759072016-11-22 18:02:48 -0800593 if (cmdCode == EFFECT_CMD_GET_PARAM &&
594 (sizeof(effect_param_t) > *replySize
595 || ((effect_param_t *)pCmdData)->psize > *replySize
596 - sizeof(effect_param_t)
597 || ((effect_param_t *)pCmdData)->vsize > *replySize
598 - sizeof(effect_param_t)
599 - ((effect_param_t *)pCmdData)->psize
600 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
601 *replySize
602 - sizeof(effect_param_t)
603 - ((effect_param_t *)pCmdData)->psize
604 - ((effect_param_t *)pCmdData)->vsize)) {
605 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
606 android_errorWriteLog(0x534e4554, "32705438");
607 return -EINVAL;
608 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700609 if ((cmdCode == EFFECT_CMD_SET_PARAM
610 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
611 (sizeof(effect_param_t) > cmdSize
612 || ((effect_param_t *)pCmdData)->psize > cmdSize
613 - sizeof(effect_param_t)
614 || ((effect_param_t *)pCmdData)->vsize > cmdSize
615 - sizeof(effect_param_t)
616 - ((effect_param_t *)pCmdData)->psize
617 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
618 cmdSize
619 - sizeof(effect_param_t)
620 - ((effect_param_t *)pCmdData)->psize
621 - ((effect_param_t *)pCmdData)->vsize)) {
622 android_errorWriteLog(0x534e4554, "30204301");
623 return -EINVAL;
624 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800625 status_t status = (*mEffectInterface)->command(mEffectInterface,
626 cmdCode,
627 cmdSize,
628 pCmdData,
629 replySize,
630 pReplyData);
631 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
632 uint32_t size = (replySize == NULL) ? 0 : *replySize;
633 for (size_t i = 1; i < mHandles.size(); i++) {
634 EffectHandle *h = mHandles[i];
Eric Laurentb37f28a2016-12-01 15:28:29 -0800635 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800636 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
637 }
638 }
639 }
640 return status;
641}
642
643status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
644{
645 Mutex::Autolock _l(mLock);
646 return setEnabled_l(enabled);
647}
648
649// must be called with EffectModule::mLock held
650status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
651{
652
653 ALOGV("setEnabled %p enabled %d", this, enabled);
654
655 if (enabled != isEnabled()) {
656 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
657 if (enabled && status != NO_ERROR) {
658 return status;
659 }
660
661 switch (mState) {
662 // going from disabled to enabled
663 case IDLE:
664 mState = STARTING;
665 break;
666 case STOPPED:
667 mState = RESTART;
668 break;
669 case STOPPING:
670 mState = ACTIVE;
671 break;
672
673 // going from enabled to disabled
674 case RESTART:
675 mState = STOPPED;
676 break;
677 case STARTING:
678 mState = IDLE;
679 break;
680 case ACTIVE:
681 mState = STOPPING;
682 break;
683 case DESTROYED:
684 return NO_ERROR; // simply ignore as we are being destroyed
685 }
686 for (size_t i = 1; i < mHandles.size(); i++) {
687 EffectHandle *h = mHandles[i];
Eric Laurentb37f28a2016-12-01 15:28:29 -0800688 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800689 h->setEnabled(enabled);
690 }
691 }
692 }
693 return NO_ERROR;
694}
695
696bool AudioFlinger::EffectModule::isEnabled() const
697{
698 switch (mState) {
699 case RESTART:
700 case STARTING:
701 case ACTIVE:
702 return true;
703 case IDLE:
704 case STOPPING:
705 case STOPPED:
706 case DESTROYED:
707 default:
708 return false;
709 }
710}
711
712bool AudioFlinger::EffectModule::isProcessEnabled() const
713{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700714 if (mStatus != NO_ERROR) {
715 return false;
716 }
717
Eric Laurentca7cc822012-11-19 14:55:58 -0800718 switch (mState) {
719 case RESTART:
720 case ACTIVE:
721 case STOPPING:
722 case STOPPED:
723 return true;
724 case IDLE:
725 case STARTING:
726 case DESTROYED:
727 default:
728 return false;
729 }
730}
731
732status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
733{
734 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700735 if (mStatus != NO_ERROR) {
736 return mStatus;
737 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800738 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800739 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
740 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
741 if (isProcessEnabled() &&
742 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
743 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800744 uint32_t volume[2];
745 uint32_t *pVolume = NULL;
746 uint32_t size = sizeof(volume);
747 volume[0] = *left;
748 volume[1] = *right;
749 if (controller) {
750 pVolume = volume;
751 }
752 status = (*mEffectInterface)->command(mEffectInterface,
753 EFFECT_CMD_SET_VOLUME,
754 size,
755 volume,
756 &size,
757 pVolume);
758 if (controller && status == NO_ERROR && size == sizeof(volume)) {
759 *left = volume[0];
760 *right = volume[1];
761 }
762 }
763 return status;
764}
765
766status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
767{
768 if (device == AUDIO_DEVICE_NONE) {
769 return NO_ERROR;
770 }
771
772 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700773 if (mStatus != NO_ERROR) {
774 return mStatus;
775 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800776 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700777 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800778 status_t cmdStatus;
779 uint32_t size = sizeof(status_t);
780 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
781 EFFECT_CMD_SET_INPUT_DEVICE;
782 status = (*mEffectInterface)->command(mEffectInterface,
783 cmd,
784 sizeof(uint32_t),
785 &device,
786 &size,
787 &cmdStatus);
788 }
789 return status;
790}
791
792status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
793{
794 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700795 if (mStatus != NO_ERROR) {
796 return mStatus;
797 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800798 status_t status = NO_ERROR;
799 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
800 status_t cmdStatus;
801 uint32_t size = sizeof(status_t);
802 status = (*mEffectInterface)->command(mEffectInterface,
803 EFFECT_CMD_SET_AUDIO_MODE,
804 sizeof(audio_mode_t),
805 &mode,
806 &size,
807 &cmdStatus);
808 if (status == NO_ERROR) {
809 status = cmdStatus;
810 }
811 }
812 return status;
813}
814
815status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
816{
817 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700818 if (mStatus != NO_ERROR) {
819 return mStatus;
820 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800821 status_t status = NO_ERROR;
822 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
823 uint32_t size = 0;
824 status = (*mEffectInterface)->command(mEffectInterface,
825 EFFECT_CMD_SET_AUDIO_SOURCE,
826 sizeof(audio_source_t),
827 &source,
828 &size,
829 NULL);
830 }
831 return status;
832}
833
834void AudioFlinger::EffectModule::setSuspended(bool suspended)
835{
836 Mutex::Autolock _l(mLock);
837 mSuspended = suspended;
838}
839
840bool AudioFlinger::EffectModule::suspended() const
841{
842 Mutex::Autolock _l(mLock);
843 return mSuspended;
844}
845
846bool AudioFlinger::EffectModule::purgeHandles()
847{
848 bool enabled = false;
849 Mutex::Autolock _l(mLock);
850 for (size_t i = 0; i < mHandles.size(); i++) {
851 EffectHandle *handle = mHandles[i];
Eric Laurentb37f28a2016-12-01 15:28:29 -0800852 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800853 if (handle->hasControl()) {
854 enabled = handle->enabled();
855 }
856 }
857 }
858 return enabled;
859}
860
Eric Laurent5baf2af2013-09-12 17:37:00 -0700861status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
862{
863 Mutex::Autolock _l(mLock);
864 if (mStatus != NO_ERROR) {
865 return mStatus;
866 }
867 status_t status = NO_ERROR;
868 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
869 status_t cmdStatus;
870 uint32_t size = sizeof(status_t);
871 effect_offload_param_t cmd;
872
873 cmd.isOffload = offloaded;
874 cmd.ioHandle = io;
875 status = (*mEffectInterface)->command(mEffectInterface,
876 EFFECT_CMD_OFFLOAD,
877 sizeof(effect_offload_param_t),
878 &cmd,
879 &size,
880 &cmdStatus);
881 if (status == NO_ERROR) {
882 status = cmdStatus;
883 }
884 mOffloaded = (status == NO_ERROR) ? offloaded : false;
885 } else {
886 if (offloaded) {
887 status = INVALID_OPERATION;
888 }
889 mOffloaded = false;
890 }
891 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
892 return status;
893}
894
895bool AudioFlinger::EffectModule::isOffloaded() const
896{
897 Mutex::Autolock _l(mLock);
898 return mOffloaded;
899}
900
Marco Nelissenb2208842014-02-07 14:00:50 -0800901String8 effectFlagsToString(uint32_t flags) {
902 String8 s;
903
904 s.append("conn. mode: ");
905 switch (flags & EFFECT_FLAG_TYPE_MASK) {
906 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
907 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
908 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
909 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
910 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
911 default: s.append("unknown/reserved"); break;
912 }
913 s.append(", ");
914
915 s.append("insert pref: ");
916 switch (flags & EFFECT_FLAG_INSERT_MASK) {
917 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
918 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
919 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
920 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
921 default: s.append("unknown/reserved"); break;
922 }
923 s.append(", ");
924
925 s.append("volume mgmt: ");
926 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
927 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
928 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
929 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
930 default: s.append("unknown/reserved"); break;
931 }
932 s.append(", ");
933
934 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
935 if (devind) {
936 s.append("device indication: ");
937 switch (devind) {
938 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
939 default: s.append("unknown/reserved"); break;
940 }
941 s.append(", ");
942 }
943
944 s.append("input mode: ");
945 switch (flags & EFFECT_FLAG_INPUT_MASK) {
946 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
947 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
948 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
949 default: s.append("not set"); break;
950 }
951 s.append(", ");
952
953 s.append("output mode: ");
954 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
955 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
956 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
957 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
958 default: s.append("not set"); break;
959 }
960 s.append(", ");
961
962 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
963 if (accel) {
964 s.append("hardware acceleration: ");
965 switch (accel) {
966 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
967 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
968 default: s.append("unknown/reserved"); break;
969 }
970 s.append(", ");
971 }
972
973 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
974 if (modeind) {
975 s.append("mode indication: ");
976 switch (modeind) {
977 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
978 default: s.append("unknown/reserved"); break;
979 }
980 s.append(", ");
981 }
982
983 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
984 if (srcind) {
985 s.append("source indication: ");
986 switch (srcind) {
987 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
988 default: s.append("unknown/reserved"); break;
989 }
990 s.append(", ");
991 }
992
993 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
994 s.append("offloadable, ");
995 }
996
997 int len = s.length();
998 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -0700999 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001000 s.unlockBuffer(len - 2);
1001 }
1002 return s;
1003}
1004
1005
Glenn Kasten0f11b512014-01-31 16:18:54 -08001006void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001007{
1008 const size_t SIZE = 256;
1009 char buffer[SIZE];
1010 String8 result;
1011
1012 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1013 result.append(buffer);
1014
1015 bool locked = AudioFlinger::dumpTryLock(mLock);
1016 // failed to lock - AudioFlinger is probably deadlocked
1017 if (!locked) {
1018 result.append("\t\tCould not lock Fx mutex:\n");
1019 }
1020
1021 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001022 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
1023 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001024 result.append(buffer);
1025
1026 result.append("\t\tDescriptor:\n");
1027 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1028 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1029 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1030 mDescriptor.uuid.node[2],
1031 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1032 result.append(buffer);
1033 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1034 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1035 mDescriptor.type.timeHiAndVersion,
1036 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1037 mDescriptor.type.node[2],
1038 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1039 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001040 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001041 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001042 mDescriptor.flags,
1043 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001044 result.append(buffer);
1045 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1046 mDescriptor.name);
1047 result.append(buffer);
1048 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1049 mDescriptor.implementor);
1050 result.append(buffer);
1051
1052 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001053 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001054 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001055 mConfig.inputCfg.buffer.frameCount,
1056 mConfig.inputCfg.samplingRate,
1057 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001058 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001059 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001060 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001061 result.append(buffer);
1062
1063 result.append("\t\t- Output configuration:\n");
1064 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001065 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001066 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001067 mConfig.outputCfg.buffer.frameCount,
1068 mConfig.outputCfg.samplingRate,
1069 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001070 mConfig.outputCfg.format,
1071 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001072 result.append(buffer);
1073
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001074 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001076 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001077 for (size_t i = 0; i < mHandles.size(); ++i) {
1078 EffectHandle *handle = mHandles[i];
Eric Laurentb37f28a2016-12-01 15:28:29 -08001079 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001080 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 result.append(buffer);
1082 }
1083 }
1084
Eric Laurentca7cc822012-11-19 14:55:58 -08001085 write(fd, result.string(), result.length());
1086
1087 if (locked) {
1088 mLock.unlock();
1089 }
1090}
1091
1092// ----------------------------------------------------------------------------
1093// EffectHandle implementation
1094// ----------------------------------------------------------------------------
1095
1096#undef LOG_TAG
1097#define LOG_TAG "AudioFlinger::EffectHandle"
1098
1099AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1100 const sp<AudioFlinger::Client>& client,
1101 const sp<IEffectClient>& effectClient,
1102 int32_t priority)
1103 : BnEffect(),
1104 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentb37f28a2016-12-01 15:28:29 -08001105 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001106{
1107 ALOGV("constructor %p", this);
1108
1109 if (client == 0) {
1110 return;
1111 }
1112 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1113 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001114 if (mCblkMemory == 0 ||
1115 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001116 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001117 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001118 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001119 return;
1120 }
Glenn Kastene75da402013-11-20 13:54:52 -08001121 new(mCblk) effect_param_cblk_t();
1122 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001123}
1124
1125AudioFlinger::EffectHandle::~EffectHandle()
1126{
1127 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001128 disconnect(false);
1129}
1130
Glenn Kastene75da402013-11-20 13:54:52 -08001131status_t AudioFlinger::EffectHandle::initCheck()
1132{
1133 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1134}
1135
Eric Laurentca7cc822012-11-19 14:55:58 -08001136status_t AudioFlinger::EffectHandle::enable()
1137{
Eric Laurentb37f28a2016-12-01 15:28:29 -08001138 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001139 ALOGV("enable %p", this);
Eric Laurentb37f28a2016-12-01 15:28:29 -08001140 sp<EffectModule> effect = mEffect.promote();
1141 if (effect == 0 || mDisconnected) {
1142 return DEAD_OBJECT;
1143 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001144 if (!mHasControl) {
1145 return INVALID_OPERATION;
1146 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001147
1148 if (mEnabled) {
1149 return NO_ERROR;
1150 }
1151
1152 mEnabled = true;
1153
Eric Laurentb37f28a2016-12-01 15:28:29 -08001154 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001155 if (thread != 0) {
Eric Laurentb37f28a2016-12-01 15:28:29 -08001156 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001157 }
1158
1159 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurentb37f28a2016-12-01 15:28:29 -08001160 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001161 return NO_ERROR;
1162 }
1163
Eric Laurentb37f28a2016-12-01 15:28:29 -08001164 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001165 if (status != NO_ERROR) {
1166 if (thread != 0) {
Eric Laurentb37f28a2016-12-01 15:28:29 -08001167 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001168 }
1169 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001170 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001171 if (thread != 0) {
1172 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001173 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001174 Mutex::Autolock _l(t->mLock);
1175 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001176 }
Eric Laurentb37f28a2016-12-01 15:28:29 -08001177 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001178 if (thread->type() == ThreadBase::OFFLOAD) {
1179 PlaybackThread *t = (PlaybackThread *)thread.get();
1180 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1181 }
Eric Laurentb37f28a2016-12-01 15:28:29 -08001182 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001183 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1184 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001185 }
1186 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001187 }
1188 return status;
1189}
1190
1191status_t AudioFlinger::EffectHandle::disable()
1192{
1193 ALOGV("disable %p", this);
Eric Laurentb37f28a2016-12-01 15:28:29 -08001194 AutoMutex _l(mLock);
1195 sp<EffectModule> effect = mEffect.promote();
1196 if (effect == 0 || mDisconnected) {
1197 return DEAD_OBJECT;
1198 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001199 if (!mHasControl) {
1200 return INVALID_OPERATION;
1201 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001202
1203 if (!mEnabled) {
1204 return NO_ERROR;
1205 }
1206 mEnabled = false;
1207
Eric Laurentb37f28a2016-12-01 15:28:29 -08001208 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001209 return NO_ERROR;
1210 }
1211
Eric Laurentb37f28a2016-12-01 15:28:29 -08001212 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001213
Eric Laurentb37f28a2016-12-01 15:28:29 -08001214 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001215 if (thread != 0) {
Eric Laurentb37f28a2016-12-01 15:28:29 -08001216 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001217 if (thread->type() == ThreadBase::OFFLOAD) {
1218 PlaybackThread *t = (PlaybackThread *)thread.get();
1219 Mutex::Autolock _l(t->mLock);
1220 t->broadcast_l();
1221 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001222 }
1223
1224 return status;
1225}
1226
1227void AudioFlinger::EffectHandle::disconnect()
1228{
Eric Laurentb37f28a2016-12-01 15:28:29 -08001229 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001230 disconnect(true);
1231}
1232
1233void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1234{
Eric Laurentb37f28a2016-12-01 15:28:29 -08001235 AutoMutex _l(mLock);
1236 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1237 if (mDisconnected) {
1238 if (unpinIfLast) {
1239 android_errorWriteLog(0x534e4554, "32707507");
1240 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001241 return;
1242 }
Eric Laurentb37f28a2016-12-01 15:28:29 -08001243 mDisconnected = true;
1244 sp<ThreadBase> thread;
1245 {
1246 sp<EffectModule> effect = mEffect.promote();
1247 if (effect != 0) {
1248 thread = effect->thread().promote();
1249 }
1250 }
1251 if (thread != 0) {
1252 thread->disconnectEffectHandle(this, unpinIfLast);
1253 } else {
1254 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1255 // try to cleanup as much as we can
1256 sp<EffectModule> effect = mEffect.promote();
1257 if (effect != 0) {
1258 effect->disconnectHandle(this, unpinIfLast);
Eric Laurentca7cc822012-11-19 14:55:58 -08001259 }
1260 }
1261
Eric Laurentca7cc822012-11-19 14:55:58 -08001262 if (mClient != 0) {
1263 if (mCblk != NULL) {
1264 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1265 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1266 }
1267 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001268 // Client destructor must run with AudioFlinger client mutex locked
1269 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001270 mClient.clear();
1271 }
1272}
1273
1274status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1275 uint32_t cmdSize,
1276 void *pCmdData,
1277 uint32_t *replySize,
1278 void *pReplyData)
1279{
1280 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurentb37f28a2016-12-01 15:28:29 -08001281 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001282
Eric Laurent08824142017-06-15 18:43:46 -07001283 // reject commands reserved for internal use by audio framework if coming from outside
1284 // of audioserver
1285 switch(cmdCode) {
1286 case EFFECT_CMD_ENABLE:
1287 case EFFECT_CMD_DISABLE:
1288 case EFFECT_CMD_SET_PARAM:
1289 case EFFECT_CMD_SET_PARAM_DEFERRED:
1290 case EFFECT_CMD_SET_PARAM_COMMIT:
1291 case EFFECT_CMD_GET_PARAM:
1292 break;
1293 default:
1294 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1295 break;
1296 }
1297 android_errorWriteLog(0x534e4554, "62019992");
1298 return BAD_VALUE;
1299 }
1300
Eric Laurentb37f28a2016-12-01 15:28:29 -08001301 if (cmdCode == EFFECT_CMD_ENABLE) {
1302 if (*replySize < sizeof(int)) {
1303 android_errorWriteLog(0x534e4554, "32095713");
1304 return BAD_VALUE;
1305 }
1306 *(int *)pReplyData = NO_ERROR;
1307 *replySize = sizeof(int);
1308 return enable();
1309 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1310 if (*replySize < sizeof(int)) {
1311 android_errorWriteLog(0x534e4554, "32095713");
1312 return BAD_VALUE;
1313 }
1314 *(int *)pReplyData = NO_ERROR;
1315 *replySize = sizeof(int);
1316 return disable();
1317 }
1318
1319 AutoMutex _l(mLock);
1320 sp<EffectModule> effect = mEffect.promote();
1321 if (effect == 0 || mDisconnected) {
1322 return DEAD_OBJECT;
1323 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001324 // only get parameter command is permitted for applications not controlling the effect
1325 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1326 return INVALID_OPERATION;
1327 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001328 if (mClient == 0) {
1329 return INVALID_OPERATION;
1330 }
1331
1332 // handle commands that are not forwarded transparently to effect engine
1333 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb37f28a2016-12-01 15:28:29 -08001334 if (*replySize < sizeof(int)) {
1335 android_errorWriteLog(0x534e4554, "32095713");
1336 return BAD_VALUE;
1337 }
1338 *(int *)pReplyData = NO_ERROR;
1339 *replySize = sizeof(int);
1340
Eric Laurentca7cc822012-11-19 14:55:58 -08001341 // No need to trylock() here as this function is executed in the binder thread serving a
1342 // particular client process: no risk to block the whole media server process or mixer
1343 // threads if we are stuck here
1344 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001345 // keep local copy of index in case of client corruption b/32220769
1346 const uint32_t clientIndex = mCblk->clientIndex;
1347 const uint32_t serverIndex = mCblk->serverIndex;
1348 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1349 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001350 mCblk->serverIndex = 0;
1351 mCblk->clientIndex = 0;
1352 return BAD_VALUE;
1353 }
1354 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001355 effect_param_t *param = NULL;
1356 for (uint32_t index = serverIndex; index < clientIndex;) {
1357 int *p = (int *)(mBuffer + index);
1358 const int size = *p++;
1359 if (size < 0
1360 || size > EFFECT_PARAM_BUFFER_SIZE
1361 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001362 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001363 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001364 break;
1365 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001366
1367 // copy to local memory in case of client corruption b/32220769
1368 param = (effect_param_t *)realloc(param, size);
1369 if (param == NULL) {
1370 ALOGW("command(): out of memory");
1371 status = NO_MEMORY;
1372 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001373 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001374 memcpy(param, p, size);
1375
1376 int reply = 0;
1377 uint32_t rsize = sizeof(reply);
Eric Laurentb37f28a2016-12-01 15:28:29 -08001378 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001379 size,
1380 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001381 &rsize,
1382 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001383
1384 // verify shared memory: server index shouldn't change; client index can't go back.
1385 if (serverIndex != mCblk->serverIndex
1386 || clientIndex > mCblk->clientIndex) {
1387 android_errorWriteLog(0x534e4554, "32220769");
1388 status = BAD_VALUE;
1389 break;
1390 }
1391
Eric Laurentca7cc822012-11-19 14:55:58 -08001392 // stop at first error encountered
1393 if (ret != NO_ERROR) {
1394 status = ret;
1395 *(int *)pReplyData = reply;
1396 break;
1397 } else if (reply != NO_ERROR) {
1398 *(int *)pReplyData = reply;
1399 break;
1400 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001401 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001403 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001404 mCblk->serverIndex = 0;
1405 mCblk->clientIndex = 0;
1406 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001407 }
1408
Eric Laurentb37f28a2016-12-01 15:28:29 -08001409 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001410}
1411
1412void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1413{
1414 ALOGV("setControl %p control %d", this, hasControl);
1415
1416 mHasControl = hasControl;
1417 mEnabled = enabled;
1418
1419 if (signal && mEffectClient != 0) {
1420 mEffectClient->controlStatusChanged(hasControl);
1421 }
1422}
1423
1424void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1425 uint32_t cmdSize,
1426 void *pCmdData,
1427 uint32_t replySize,
1428 void *pReplyData)
1429{
1430 if (mEffectClient != 0) {
1431 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1432 }
1433}
1434
1435
1436
1437void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1438{
1439 if (mEffectClient != 0) {
1440 mEffectClient->enableStatusChanged(enabled);
1441 }
1442}
1443
1444status_t AudioFlinger::EffectHandle::onTransact(
1445 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1446{
1447 return BnEffect::onTransact(code, data, reply, flags);
1448}
1449
1450
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001451void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001452{
1453 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1454
Marco Nelissenb2208842014-02-07 14:00:50 -08001455 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001456 (mClient == 0) ? getpid_cached : mClient->pid(),
1457 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001458 mHasControl ? "yes" : "no",
1459 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001460 mCblk ? mCblk->clientIndex : 0,
1461 mCblk ? mCblk->serverIndex : 0
1462 );
1463
1464 if (locked) {
1465 mCblk->lock.unlock();
1466 }
1467}
1468
1469#undef LOG_TAG
1470#define LOG_TAG "AudioFlinger::EffectChain"
1471
1472AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001473 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001474 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1475 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001476 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), mForceVolume(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001477{
1478 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1479 if (thread == NULL) {
1480 return;
1481 }
1482 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1483 thread->frameCount();
1484}
1485
1486AudioFlinger::EffectChain::~EffectChain()
1487{
1488 if (mOwnInBuffer) {
1489 delete mInBuffer;
1490 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001491}
1492
1493// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1494sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1495 effect_descriptor_t *descriptor)
1496{
1497 size_t size = mEffects.size();
1498
1499 for (size_t i = 0; i < size; i++) {
1500 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1501 return mEffects[i];
1502 }
1503 }
1504 return 0;
1505}
1506
1507// getEffectFromId_l() must be called with ThreadBase::mLock held
1508sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1509{
1510 size_t size = mEffects.size();
1511
1512 for (size_t i = 0; i < size; i++) {
1513 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1514 if (id == 0 || mEffects[i]->id() == id) {
1515 return mEffects[i];
1516 }
1517 }
1518 return 0;
1519}
1520
1521// getEffectFromType_l() must be called with ThreadBase::mLock held
1522sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1523 const effect_uuid_t *type)
1524{
1525 size_t size = mEffects.size();
1526
1527 for (size_t i = 0; i < size; i++) {
1528 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1529 return mEffects[i];
1530 }
1531 }
1532 return 0;
1533}
1534
1535void AudioFlinger::EffectChain::clearInputBuffer()
1536{
1537 Mutex::Autolock _l(mLock);
1538 sp<ThreadBase> thread = mThread.promote();
1539 if (thread == 0) {
1540 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1541 return;
1542 }
1543 clearInputBuffer_l(thread);
1544}
1545
1546// Must be called with EffectChain::mLock locked
1547void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1548{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001549 // TODO: This will change in the future, depending on multichannel
1550 // and sample format changes for effects.
1551 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1552 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001553 const size_t frameSize =
1554 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001555 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001556}
1557
1558// Must be called with EffectChain::mLock locked
1559void AudioFlinger::EffectChain::process_l()
1560{
1561 sp<ThreadBase> thread = mThread.promote();
1562 if (thread == 0) {
1563 ALOGW("process_l(): cannot promote mixer thread");
1564 return;
1565 }
1566 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1567 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001568 // never process effects when:
1569 // - on an OFFLOAD thread
1570 // - no more tracks are on the session and the effect tail has been rendered
1571 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001572 if (!isGlobalSession) {
1573 bool tracksOnSession = (trackCnt() != 0);
1574
1575 if (!tracksOnSession && mTailBufferCount == 0) {
1576 doProcess = false;
1577 }
1578
1579 if (activeTrackCnt() == 0) {
1580 // if no track is active and the effect tail has not been rendered,
1581 // the input buffer must be cleared here as the mixer process will not do it
1582 if (tracksOnSession || mTailBufferCount > 0) {
1583 clearInputBuffer_l(thread);
1584 if (mTailBufferCount > 0) {
1585 mTailBufferCount--;
1586 }
1587 }
1588 }
1589 }
1590
1591 size_t size = mEffects.size();
1592 if (doProcess) {
1593 for (size_t i = 0; i < size; i++) {
1594 mEffects[i]->process();
1595 }
1596 }
1597 for (size_t i = 0; i < size; i++) {
1598 mEffects[i]->updateState();
1599 }
1600}
1601
Eric Laurentb37f28a2016-12-01 15:28:29 -08001602// createEffect_l() must be called with ThreadBase::mLock held
1603status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1604 ThreadBase *thread,
1605 effect_descriptor_t *desc,
1606 int id,
1607 audio_session_t sessionId,
1608 bool pinned)
1609{
1610 Mutex::Autolock _l(mLock);
1611 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1612 status_t lStatus = effect->status();
1613 if (lStatus == NO_ERROR) {
1614 lStatus = addEffect_ll(effect);
1615 }
1616 if (lStatus != NO_ERROR) {
1617 effect.clear();
1618 }
1619 return lStatus;
1620}
1621
1622// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001623status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1624{
Eric Laurentb37f28a2016-12-01 15:28:29 -08001625 Mutex::Autolock _l(mLock);
1626 return addEffect_ll(effect);
1627}
1628// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1629status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1630{
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 effect_descriptor_t desc = effect->desc();
1632 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1633
Eric Laurentca7cc822012-11-19 14:55:58 -08001634 effect->setChain(this);
1635 sp<ThreadBase> thread = mThread.promote();
1636 if (thread == 0) {
1637 return NO_INIT;
1638 }
1639 effect->setThread(thread);
1640
1641 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1642 // Auxiliary effects are inserted at the beginning of mEffects vector as
1643 // they are processed first and accumulated in chain input buffer
1644 mEffects.insertAt(effect, 0);
1645
1646 // the input buffer for auxiliary effect contains mono samples in
1647 // 32 bit format. This is to avoid saturation in AudoMixer
1648 // accumulation stage. Saturation is done in EffectModule::process() before
1649 // calling the process in effect engine
1650 size_t numSamples = thread->frameCount();
1651 int32_t *buffer = new int32_t[numSamples];
1652 memset(buffer, 0, numSamples * sizeof(int32_t));
1653 effect->setInBuffer((int16_t *)buffer);
1654 // auxiliary effects output samples to chain input buffer for further processing
1655 // by insert effects
1656 effect->setOutBuffer(mInBuffer);
1657 } else {
1658 // Insert effects are inserted at the end of mEffects vector as they are processed
1659 // after track and auxiliary effects.
1660 // Insert effect order as a function of indicated preference:
1661 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1662 // another effect is present
1663 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1664 // last effect claiming first position
1665 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1666 // first effect claiming last position
1667 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1668 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1669 // already present
1670
1671 size_t size = mEffects.size();
1672 size_t idx_insert = size;
1673 ssize_t idx_insert_first = -1;
1674 ssize_t idx_insert_last = -1;
1675
1676 for (size_t i = 0; i < size; i++) {
1677 effect_descriptor_t d = mEffects[i]->desc();
1678 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1679 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1680 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1681 // check invalid effect chaining combinations
1682 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1683 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1684 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1685 desc.name, d.name);
1686 return INVALID_OPERATION;
1687 }
1688 // remember position of first insert effect and by default
1689 // select this as insert position for new effect
1690 if (idx_insert == size) {
1691 idx_insert = i;
1692 }
1693 // remember position of last insert effect claiming
1694 // first position
1695 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1696 idx_insert_first = i;
1697 }
1698 // remember position of first insert effect claiming
1699 // last position
1700 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1701 idx_insert_last == -1) {
1702 idx_insert_last = i;
1703 }
1704 }
1705 }
1706
1707 // modify idx_insert from first position if needed
1708 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1709 if (idx_insert_last != -1) {
1710 idx_insert = idx_insert_last;
1711 } else {
1712 idx_insert = size;
1713 }
1714 } else {
1715 if (idx_insert_first != -1) {
1716 idx_insert = idx_insert_first + 1;
1717 }
1718 }
1719
1720 // always read samples from chain input buffer
1721 effect->setInBuffer(mInBuffer);
1722
1723 // if last effect in the chain, output samples to chain
1724 // output buffer, otherwise to chain input buffer
1725 if (idx_insert == size) {
1726 if (idx_insert != 0) {
1727 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1728 mEffects[idx_insert-1]->configure();
1729 }
1730 effect->setOutBuffer(mOutBuffer);
1731 } else {
1732 effect->setOutBuffer(mInBuffer);
1733 }
1734 mEffects.insertAt(effect, idx_insert);
1735
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001736 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 idx_insert);
1738 }
1739 effect->configure();
1740 return NO_ERROR;
1741}
1742
Eric Laurentb37f28a2016-12-01 15:28:29 -08001743// removeEffect_l() must be called with ThreadBase::mLock held
1744size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1745 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001746{
1747 Mutex::Autolock _l(mLock);
1748 size_t size = mEffects.size();
1749 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1750
1751 for (size_t i = 0; i < size; i++) {
1752 if (effect == mEffects[i]) {
1753 // calling stop here will remove pre-processing effect from the audio HAL.
1754 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1755 // the middle of a read from audio HAL
1756 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1757 mEffects[i]->state() == EffectModule::STOPPING) {
1758 mEffects[i]->stop();
1759 }
Eric Laurentb37f28a2016-12-01 15:28:29 -08001760 if (release) {
1761 mEffects[i]->release_l();
1762 }
1763
Eric Laurentca7cc822012-11-19 14:55:58 -08001764 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1765 delete[] effect->inBuffer();
1766 } else {
1767 if (i == size - 1 && i != 0) {
1768 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1769 mEffects[i - 1]->configure();
1770 }
1771 }
1772 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001773 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001774 this, i);
Eric Laurentb37f28a2016-12-01 15:28:29 -08001775
Eric Laurentca7cc822012-11-19 14:55:58 -08001776 break;
1777 }
1778 }
1779
1780 return mEffects.size();
1781}
1782
Eric Laurentb37f28a2016-12-01 15:28:29 -08001783// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001784void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1785{
1786 size_t size = mEffects.size();
1787 for (size_t i = 0; i < size; i++) {
1788 mEffects[i]->setDevice(device);
1789 }
1790}
1791
Eric Laurentb37f28a2016-12-01 15:28:29 -08001792// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001793void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1794{
1795 size_t size = mEffects.size();
1796 for (size_t i = 0; i < size; i++) {
1797 mEffects[i]->setMode(mode);
1798 }
1799}
1800
Eric Laurentb37f28a2016-12-01 15:28:29 -08001801// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001802void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1803{
1804 size_t size = mEffects.size();
1805 for (size_t i = 0; i < size; i++) {
1806 mEffects[i]->setAudioSource(source);
1807 }
1808}
1809
1810// setVolume_l() must be called with PlaybackThread::mLock held
1811bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1812{
1813 uint32_t newLeft = *left;
1814 uint32_t newRight = *right;
1815 bool hasControl = false;
1816 int ctrlIdx = -1;
1817 size_t size = mEffects.size();
1818
1819 // first update volume controller
1820 for (size_t i = size; i > 0; i--) {
1821 if (mEffects[i - 1]->isProcessEnabled() &&
1822 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1823 ctrlIdx = i - 1;
1824 hasControl = true;
1825 break;
1826 }
1827 }
1828
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001829 if (!isVolumeForced() && ctrlIdx == mVolumeCtrlIdx &&
1830 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 if (hasControl) {
1832 *left = mNewLeftVolume;
1833 *right = mNewRightVolume;
1834 }
1835 return hasControl;
1836 }
1837
1838 mVolumeCtrlIdx = ctrlIdx;
1839 mLeftVolume = newLeft;
1840 mRightVolume = newRight;
1841
1842 // second get volume update from volume controller
1843 if (ctrlIdx >= 0) {
1844 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1845 mNewLeftVolume = newLeft;
1846 mNewRightVolume = newRight;
1847 }
1848 // then indicate volume to all other effects in chain.
1849 // Pass altered volume to effects before volume controller
1850 // and requested volume to effects after controller
1851 uint32_t lVol = newLeft;
1852 uint32_t rVol = newRight;
1853
1854 for (size_t i = 0; i < size; i++) {
1855 if ((int)i == ctrlIdx) {
1856 continue;
1857 }
1858 // this also works for ctrlIdx == -1 when there is no volume controller
1859 if ((int)i > ctrlIdx) {
1860 lVol = *left;
1861 rVol = *right;
1862 }
1863 mEffects[i]->setVolume(&lVol, &rVol, false);
1864 }
1865 *left = newLeft;
1866 *right = newRight;
1867
1868 return hasControl;
1869}
1870
Eric Laurent1b928682014-10-02 19:41:47 -07001871void AudioFlinger::EffectChain::syncHalEffectsState()
1872{
1873 Mutex::Autolock _l(mLock);
1874 for (size_t i = 0; i < mEffects.size(); i++) {
1875 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1876 mEffects[i]->state() == EffectModule::STOPPING) {
1877 mEffects[i]->addEffectToHal_l();
1878 }
1879 }
1880}
1881
Eric Laurentca7cc822012-11-19 14:55:58 -08001882void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1883{
1884 const size_t SIZE = 256;
1885 char buffer[SIZE];
1886 String8 result;
1887
Marco Nelissenb2208842014-02-07 14:00:50 -08001888 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001889 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001890 result.append(buffer);
1891
Marco Nelissenb2208842014-02-07 14:00:50 -08001892 if (numEffects) {
1893 bool locked = AudioFlinger::dumpTryLock(mLock);
1894 // failed to lock - AudioFlinger is probably deadlocked
1895 if (!locked) {
1896 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001897 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001898
Marco Nelissenb2208842014-02-07 14:00:50 -08001899 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001900 snprintf(buffer, SIZE, "\t%p %p %d\n",
1901 mInBuffer,
1902 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001903 mActiveTrackCnt);
1904 result.append(buffer);
1905 write(fd, result.string(), result.size());
1906
1907 for (size_t i = 0; i < numEffects; ++i) {
1908 sp<EffectModule> effect = mEffects[i];
1909 if (effect != 0) {
1910 effect->dump(fd, args);
1911 }
1912 }
1913
1914 if (locked) {
1915 mLock.unlock();
1916 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001917 }
1918}
1919
1920// must be called with ThreadBase::mLock held
1921void AudioFlinger::EffectChain::setEffectSuspended_l(
1922 const effect_uuid_t *type, bool suspend)
1923{
1924 sp<SuspendedEffectDesc> desc;
1925 // use effect type UUID timelow as key as there is no real risk of identical
1926 // timeLow fields among effect type UUIDs.
1927 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1928 if (suspend) {
1929 if (index >= 0) {
1930 desc = mSuspendedEffects.valueAt(index);
1931 } else {
1932 desc = new SuspendedEffectDesc();
1933 desc->mType = *type;
1934 mSuspendedEffects.add(type->timeLow, desc);
1935 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1936 }
1937 if (desc->mRefCount++ == 0) {
1938 sp<EffectModule> effect = getEffectIfEnabled(type);
1939 if (effect != 0) {
1940 desc->mEffect = effect;
1941 effect->setSuspended(true);
1942 effect->setEnabled(false);
1943 }
1944 }
1945 } else {
1946 if (index < 0) {
1947 return;
1948 }
1949 desc = mSuspendedEffects.valueAt(index);
1950 if (desc->mRefCount <= 0) {
1951 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1952 desc->mRefCount = 1;
1953 }
1954 if (--desc->mRefCount == 0) {
1955 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1956 if (desc->mEffect != 0) {
1957 sp<EffectModule> effect = desc->mEffect.promote();
1958 if (effect != 0) {
1959 effect->setSuspended(false);
1960 effect->lock();
1961 EffectHandle *handle = effect->controlHandle_l();
Eric Laurentb37f28a2016-12-01 15:28:29 -08001962 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001963 effect->setEnabled_l(handle->enabled());
1964 }
1965 effect->unlock();
1966 }
1967 desc->mEffect.clear();
1968 }
1969 mSuspendedEffects.removeItemsAt(index);
1970 }
1971 }
1972}
1973
1974// must be called with ThreadBase::mLock held
1975void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1976{
1977 sp<SuspendedEffectDesc> desc;
1978
1979 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1980 if (suspend) {
1981 if (index >= 0) {
1982 desc = mSuspendedEffects.valueAt(index);
1983 } else {
1984 desc = new SuspendedEffectDesc();
1985 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1986 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1987 }
1988 if (desc->mRefCount++ == 0) {
1989 Vector< sp<EffectModule> > effects;
1990 getSuspendEligibleEffects(effects);
1991 for (size_t i = 0; i < effects.size(); i++) {
1992 setEffectSuspended_l(&effects[i]->desc().type, true);
1993 }
1994 }
1995 } else {
1996 if (index < 0) {
1997 return;
1998 }
1999 desc = mSuspendedEffects.valueAt(index);
2000 if (desc->mRefCount <= 0) {
2001 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2002 desc->mRefCount = 1;
2003 }
2004 if (--desc->mRefCount == 0) {
2005 Vector<const effect_uuid_t *> types;
2006 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2007 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2008 continue;
2009 }
2010 types.add(&mSuspendedEffects.valueAt(i)->mType);
2011 }
2012 for (size_t i = 0; i < types.size(); i++) {
2013 setEffectSuspended_l(types[i], false);
2014 }
2015 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2016 mSuspendedEffects.keyAt(index));
2017 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2018 }
2019 }
2020}
2021
2022
2023// The volume effect is used for automated tests only
2024#ifndef OPENSL_ES_H_
2025static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2026 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2027const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2028#endif //OPENSL_ES_H_
2029
2030bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2031{
2032 // auxiliary effects and visualizer are never suspended on output mix
2033 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2034 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2035 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2036 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2037 return false;
2038 }
2039 return true;
2040}
2041
2042void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2043 Vector< sp<AudioFlinger::EffectModule> > &effects)
2044{
2045 effects.clear();
2046 for (size_t i = 0; i < mEffects.size(); i++) {
2047 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2048 effects.add(mEffects[i]);
2049 }
2050 }
2051}
2052
2053sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2054 const effect_uuid_t *type)
2055{
2056 sp<EffectModule> effect = getEffectFromType_l(type);
2057 return effect != 0 && effect->isEnabled() ? effect : 0;
2058}
2059
2060void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2061 bool enabled)
2062{
2063 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2064 if (enabled) {
2065 if (index < 0) {
2066 // if the effect is not suspend check if all effects are suspended
2067 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2068 if (index < 0) {
2069 return;
2070 }
2071 if (!isEffectEligibleForSuspend(effect->desc())) {
2072 return;
2073 }
2074 setEffectSuspended_l(&effect->desc().type, enabled);
2075 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2076 if (index < 0) {
2077 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2078 return;
2079 }
2080 }
2081 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2082 effect->desc().type.timeLow);
2083 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2084 // if effect is requested to suspended but was not yet enabled, supend it now.
2085 if (desc->mEffect == 0) {
2086 desc->mEffect = effect;
2087 effect->setEnabled(false);
2088 effect->setSuspended(true);
2089 }
2090 } else {
2091 if (index < 0) {
2092 return;
2093 }
2094 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2095 effect->desc().type.timeLow);
2096 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2097 desc->mEffect.clear();
2098 effect->setSuspended(false);
2099 }
2100}
2101
Eric Laurent5baf2af2013-09-12 17:37:00 -07002102bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002103{
2104 Mutex::Autolock _l(mLock);
2105 size_t size = mEffects.size();
2106 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002107 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002108 return true;
2109 }
2110 }
2111 return false;
2112}
2113
Eric Laurentaaa44472014-09-12 17:41:50 -07002114void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2115{
2116 Mutex::Autolock _l(mLock);
2117 mThread = thread;
2118 for (size_t i = 0; i < mEffects.size(); i++) {
2119 mEffects[i]->setThread(thread);
2120 }
2121}
2122
Glenn Kasten63238ef2015-03-02 15:50:29 -08002123} // namespace android