blob: 2193f47ac960e313035da2b3297c9f5753192d0b [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
47namespace android {
48
49// ----------------------------------------------------------------------------
50// EffectModule implementation
51// ----------------------------------------------------------------------------
52
53#undef LOG_TAG
54#define LOG_TAG "AudioFlinger::EffectModule"
55
56AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
57 const wp<AudioFlinger::EffectChain>& chain,
58 effect_descriptor_t *desc,
59 int id,
60 int sessionId)
61 : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX),
62 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
63 mDescriptor(*desc),
64 // mConfig is set by configure() and not used before then
65 mEffectInterface(NULL),
66 mStatus(NO_INIT), mState(IDLE),
67 // mMaxDisableWaitCnt is set by configure() and not used before then
68 // mDisableWaitCnt is set by process() and updateState() and not used before then
69 mSuspended(false)
70{
71 ALOGV("Constructor %p", this);
72 int lStatus;
73
74 // create effect engine from effect factory
75 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
76
77 if (mStatus != NO_ERROR) {
78 return;
79 }
80 lStatus = init();
81 if (lStatus < 0) {
82 mStatus = lStatus;
83 goto Error;
84 }
85
86 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
87 return;
88Error:
89 EffectRelease(mEffectInterface);
90 mEffectInterface = NULL;
91 ALOGV("Constructor Error %d", mStatus);
92}
93
94AudioFlinger::EffectModule::~EffectModule()
95{
96 ALOGV("Destructor %p", this);
97 if (mEffectInterface != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -080098 remove_effect_from_hal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -080099 // release effect engine
100 EffectRelease(mEffectInterface);
101 }
102}
103
104status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
105{
106 status_t status;
107
108 Mutex::Autolock _l(mLock);
109 int priority = handle->priority();
110 size_t size = mHandles.size();
111 EffectHandle *controlHandle = NULL;
112 size_t i;
113 for (i = 0; i < size; i++) {
114 EffectHandle *h = mHandles[i];
115 if (h == NULL || h->destroyed_l()) {
116 continue;
117 }
118 // first non destroyed handle is considered in control
119 if (controlHandle == NULL)
120 controlHandle = h;
121 if (h->priority() <= priority) {
122 break;
123 }
124 }
125 // if inserted in first place, move effect control from previous owner to this handle
126 if (i == 0) {
127 bool enabled = false;
128 if (controlHandle != NULL) {
129 enabled = controlHandle->enabled();
130 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
131 }
132 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
133 status = NO_ERROR;
134 } else {
135 status = ALREADY_EXISTS;
136 }
137 ALOGV("addHandle() %p added handle %p in position %d", this, handle, i);
138 mHandles.insertAt(handle, i);
139 return status;
140}
141
142size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
143{
144 Mutex::Autolock _l(mLock);
145 size_t size = mHandles.size();
146 size_t i;
147 for (i = 0; i < size; i++) {
148 if (mHandles[i] == handle) {
149 break;
150 }
151 }
152 if (i == size) {
153 return size;
154 }
155 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle, i);
156
157 mHandles.removeAt(i);
158 // if removed from first place, move effect control from this handle to next in line
159 if (i == 0) {
160 EffectHandle *h = controlHandle_l();
161 if (h != NULL) {
162 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
163 }
164 }
165
166 // Prevent calls to process() and other functions on effect interface from now on.
167 // The effect engine will be released by the destructor when the last strong reference on
168 // this object is released which can happen after next process is called.
169 if (mHandles.size() == 0 && !mPinned) {
170 mState = DESTROYED;
171 }
172
173 return mHandles.size();
174}
175
176// must be called with EffectModule::mLock held
177AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
178{
179 // the first valid handle in the list has control over the module
180 for (size_t i = 0; i < mHandles.size(); i++) {
181 EffectHandle *h = mHandles[i];
182 if (h != NULL && !h->destroyed_l()) {
183 return h;
184 }
185 }
186
187 return NULL;
188}
189
190size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast)
191{
192 ALOGV("disconnect() %p handle %p", this, handle);
193 // keep a strong reference on this EffectModule to avoid calling the
194 // destructor before we exit
195 sp<EffectModule> keep(this);
196 {
197 sp<ThreadBase> thread = mThread.promote();
198 if (thread != 0) {
199 thread->disconnectEffect(keep, handle, unpinIfLast);
200 }
201 }
202 return mHandles.size();
203}
204
205void AudioFlinger::EffectModule::updateState() {
206 Mutex::Autolock _l(mLock);
207
208 switch (mState) {
209 case RESTART:
210 reset_l();
211 // FALL THROUGH
212
213 case STARTING:
214 // clear auxiliary effect input buffer for next accumulation
215 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
216 memset(mConfig.inputCfg.buffer.raw,
217 0,
218 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
219 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700220 if (start_l() == NO_ERROR) {
221 mState = ACTIVE;
222 } else {
223 mState = IDLE;
224 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800225 break;
226 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700227 if (stop_l() == NO_ERROR) {
228 mDisableWaitCnt = mMaxDisableWaitCnt;
229 } else {
230 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
231 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800232 mState = STOPPED;
233 break;
234 case STOPPED:
235 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
236 // turn off sequence.
237 if (--mDisableWaitCnt == 0) {
238 reset_l();
239 mState = IDLE;
240 }
241 break;
242 default: //IDLE , ACTIVE, DESTROYED
243 break;
244 }
245}
246
247void AudioFlinger::EffectModule::process()
248{
249 Mutex::Autolock _l(mLock);
250
251 if (mState == DESTROYED || mEffectInterface == NULL ||
252 mConfig.inputCfg.buffer.raw == NULL ||
253 mConfig.outputCfg.buffer.raw == NULL) {
254 return;
255 }
256
257 if (isProcessEnabled()) {
258 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
259 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
260 ditherAndClamp(mConfig.inputCfg.buffer.s32,
261 mConfig.inputCfg.buffer.s32,
262 mConfig.inputCfg.buffer.frameCount/2);
263 }
264
265 // do the actual processing in the effect engine
266 int ret = (*mEffectInterface)->process(mEffectInterface,
267 &mConfig.inputCfg.buffer,
268 &mConfig.outputCfg.buffer);
269
270 // force transition to IDLE state when engine is ready
271 if (mState == STOPPED && ret == -ENODATA) {
272 mDisableWaitCnt = 1;
273 }
274
275 // clear auxiliary effect input buffer for next accumulation
276 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
277 memset(mConfig.inputCfg.buffer.raw, 0,
278 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
279 }
280 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
281 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
282 // If an insert effect is idle and input buffer is different from output buffer,
283 // accumulate input onto output
284 sp<EffectChain> chain = mChain.promote();
285 if (chain != 0 && chain->activeTrackCnt() != 0) {
286 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
287 int16_t *in = mConfig.inputCfg.buffer.s16;
288 int16_t *out = mConfig.outputCfg.buffer.s16;
289 for (size_t i = 0; i < frameCnt; i++) {
290 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
291 }
292 }
293 }
294}
295
296void AudioFlinger::EffectModule::reset_l()
297{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700298 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800299 return;
300 }
301 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
302}
303
304status_t AudioFlinger::EffectModule::configure()
305{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700306 status_t status;
307 sp<ThreadBase> thread;
308 uint32_t size;
309 audio_channel_mask_t channelMask;
310
Eric Laurentca7cc822012-11-19 14:55:58 -0800311 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700312 status = NO_INIT;
313 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 }
315
Eric Laurentd0ebb532013-04-02 16:41:41 -0700316 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800317 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700318 status = DEAD_OBJECT;
319 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800320 }
321
322 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700323 channelMask = thread->channelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800324
325 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
326 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
327 } else {
328 mConfig.inputCfg.channels = channelMask;
329 }
330 mConfig.outputCfg.channels = channelMask;
331 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
332 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
333 mConfig.inputCfg.samplingRate = thread->sampleRate();
334 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
335 mConfig.inputCfg.bufferProvider.cookie = NULL;
336 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
337 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
338 mConfig.outputCfg.bufferProvider.cookie = NULL;
339 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
340 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
341 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
342 // Insert effect:
343 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
344 // always overwrites output buffer: input buffer == output buffer
345 // - in other sessions:
346 // last effect in the chain accumulates in output buffer: input buffer != output buffer
347 // other effect: overwrites output buffer: input buffer == output buffer
348 // Auxiliary effect:
349 // accumulates in output buffer: input buffer != output buffer
350 // Therefore: accumulate <=> input buffer != output buffer
351 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
352 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
353 } else {
354 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
355 }
356 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
357 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
358 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
359 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
360
361 ALOGV("configure() %p thread %p buffer %p framecount %d",
362 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
363
364 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700365 size = sizeof(int);
366 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800367 EFFECT_CMD_SET_CONFIG,
368 sizeof(effect_config_t),
369 &mConfig,
370 &size,
371 &cmdStatus);
372 if (status == 0) {
373 status = cmdStatus;
374 }
375
376 if (status == 0 &&
377 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
378 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
379 effect_param_t *p = (effect_param_t *)buf32;
380
381 p->psize = sizeof(uint32_t);
382 p->vsize = sizeof(uint32_t);
383 size = sizeof(int);
384 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
385
386 uint32_t latency = 0;
387 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
388 if (pbt != NULL) {
389 latency = pbt->latency_l();
390 }
391
392 *((int32_t *)p->data + 1)= latency;
393 (*mEffectInterface)->command(mEffectInterface,
394 EFFECT_CMD_SET_PARAM,
395 sizeof(effect_param_t) + 8,
396 &buf32,
397 &size,
398 &cmdStatus);
399 }
400
401 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
402 (1000 * mConfig.outputCfg.buffer.frameCount);
403
Eric Laurentd0ebb532013-04-02 16:41:41 -0700404exit:
405 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800406 return status;
407}
408
409status_t AudioFlinger::EffectModule::init()
410{
411 Mutex::Autolock _l(mLock);
412 if (mEffectInterface == NULL) {
413 return NO_INIT;
414 }
415 status_t cmdStatus;
416 uint32_t size = sizeof(status_t);
417 status_t status = (*mEffectInterface)->command(mEffectInterface,
418 EFFECT_CMD_INIT,
419 0,
420 NULL,
421 &size,
422 &cmdStatus);
423 if (status == 0) {
424 status = cmdStatus;
425 }
426 return status;
427}
428
429status_t AudioFlinger::EffectModule::start()
430{
431 Mutex::Autolock _l(mLock);
432 return start_l();
433}
434
435status_t AudioFlinger::EffectModule::start_l()
436{
437 if (mEffectInterface == NULL) {
438 return NO_INIT;
439 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700440 if (mStatus != NO_ERROR) {
441 return mStatus;
442 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800443 status_t cmdStatus;
444 uint32_t size = sizeof(status_t);
445 status_t status = (*mEffectInterface)->command(mEffectInterface,
446 EFFECT_CMD_ENABLE,
447 0,
448 NULL,
449 &size,
450 &cmdStatus);
451 if (status == 0) {
452 status = cmdStatus;
453 }
454 if (status == 0 &&
455 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
456 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
457 sp<ThreadBase> thread = mThread.promote();
458 if (thread != 0) {
459 audio_stream_t *stream = thread->stream();
460 if (stream != NULL) {
461 stream->add_audio_effect(stream, mEffectInterface);
462 }
463 }
464 }
465 return status;
466}
467
468status_t AudioFlinger::EffectModule::stop()
469{
470 Mutex::Autolock _l(mLock);
471 return stop_l();
472}
473
474status_t AudioFlinger::EffectModule::stop_l()
475{
476 if (mEffectInterface == NULL) {
477 return NO_INIT;
478 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700479 if (mStatus != NO_ERROR) {
480 return mStatus;
481 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800482 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800483 uint32_t size = sizeof(status_t);
484 status_t status = (*mEffectInterface)->command(mEffectInterface,
485 EFFECT_CMD_DISABLE,
486 0,
487 NULL,
488 &size,
489 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800490 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800491 status = cmdStatus;
492 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800493 if (status == NO_ERROR) {
494 status = remove_effect_from_hal_l();
495 }
496 return status;
497}
498
499status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
500{
501 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
502 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800503 sp<ThreadBase> thread = mThread.promote();
504 if (thread != 0) {
505 audio_stream_t *stream = thread->stream();
506 if (stream != NULL) {
507 stream->remove_audio_effect(stream, mEffectInterface);
508 }
509 }
510 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800511 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800512}
513
Andy Hunge4a1d912016-08-17 14:11:13 -0700514// round up delta valid if value and divisor are positive.
515template <typename T>
516static T roundUpDelta(const T &value, const T &divisor) {
517 T remainder = value % divisor;
518 return remainder == 0 ? 0 : divisor - remainder;
519}
520
Eric Laurentca7cc822012-11-19 14:55:58 -0800521status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
522 uint32_t cmdSize,
523 void *pCmdData,
524 uint32_t *replySize,
525 void *pReplyData)
526{
527 Mutex::Autolock _l(mLock);
528 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
529
530 if (mState == DESTROYED || mEffectInterface == NULL) {
531 return NO_INIT;
532 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700533 if (mStatus != NO_ERROR) {
534 return mStatus;
535 }
Andy Hung110bc952016-06-20 15:22:52 -0700536 if (cmdCode == EFFECT_CMD_GET_PARAM &&
537 (*replySize < sizeof(effect_param_t) ||
538 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
539 android_errorWriteLog(0x534e4554, "29251553");
540 return -EINVAL;
541 }
Andy Hung3d34cc72016-11-04 19:40:53 -0700542 if (cmdCode == EFFECT_CMD_GET_PARAM &&
543 (sizeof(effect_param_t) > cmdSize ||
544 ((effect_param_t *)pCmdData)->psize > cmdSize
545 - sizeof(effect_param_t))) {
546 android_errorWriteLog(0x534e4554, "32438594");
547 return -EINVAL;
548 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700549 if ((cmdCode == EFFECT_CMD_SET_PARAM
550 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
551 (sizeof(effect_param_t) > cmdSize
552 || ((effect_param_t *)pCmdData)->psize > cmdSize
553 - sizeof(effect_param_t)
554 || ((effect_param_t *)pCmdData)->vsize > cmdSize
555 - sizeof(effect_param_t)
556 - ((effect_param_t *)pCmdData)->psize
557 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
558 cmdSize
559 - sizeof(effect_param_t)
560 - ((effect_param_t *)pCmdData)->psize
561 - ((effect_param_t *)pCmdData)->vsize)) {
562 android_errorWriteLog(0x534e4554, "30204301");
563 return -EINVAL;
564 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800565 status_t status = (*mEffectInterface)->command(mEffectInterface,
566 cmdCode,
567 cmdSize,
568 pCmdData,
569 replySize,
570 pReplyData);
571 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
572 uint32_t size = (replySize == NULL) ? 0 : *replySize;
573 for (size_t i = 1; i < mHandles.size(); i++) {
574 EffectHandle *h = mHandles[i];
575 if (h != NULL && !h->destroyed_l()) {
576 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
577 }
578 }
579 }
580 return status;
581}
582
583status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
584{
585 Mutex::Autolock _l(mLock);
586 return setEnabled_l(enabled);
587}
588
589// must be called with EffectModule::mLock held
590status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
591{
592
593 ALOGV("setEnabled %p enabled %d", this, enabled);
594
595 if (enabled != isEnabled()) {
596 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
597 if (enabled && status != NO_ERROR) {
598 return status;
599 }
600
601 switch (mState) {
602 // going from disabled to enabled
603 case IDLE:
604 mState = STARTING;
605 break;
606 case STOPPED:
607 mState = RESTART;
608 break;
609 case STOPPING:
610 mState = ACTIVE;
611 break;
612
613 // going from enabled to disabled
614 case RESTART:
615 mState = STOPPED;
616 break;
617 case STARTING:
618 mState = IDLE;
619 break;
620 case ACTIVE:
621 mState = STOPPING;
622 break;
623 case DESTROYED:
624 return NO_ERROR; // simply ignore as we are being destroyed
625 }
626 for (size_t i = 1; i < mHandles.size(); i++) {
627 EffectHandle *h = mHandles[i];
628 if (h != NULL && !h->destroyed_l()) {
629 h->setEnabled(enabled);
630 }
631 }
632 }
633 return NO_ERROR;
634}
635
636bool AudioFlinger::EffectModule::isEnabled() const
637{
638 switch (mState) {
639 case RESTART:
640 case STARTING:
641 case ACTIVE:
642 return true;
643 case IDLE:
644 case STOPPING:
645 case STOPPED:
646 case DESTROYED:
647 default:
648 return false;
649 }
650}
651
652bool AudioFlinger::EffectModule::isProcessEnabled() const
653{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700654 if (mStatus != NO_ERROR) {
655 return false;
656 }
657
Eric Laurentca7cc822012-11-19 14:55:58 -0800658 switch (mState) {
659 case RESTART:
660 case ACTIVE:
661 case STOPPING:
662 case STOPPED:
663 return true;
664 case IDLE:
665 case STARTING:
666 case DESTROYED:
667 default:
668 return false;
669 }
670}
671
672status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
673{
674 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700675 if (mStatus != NO_ERROR) {
676 return mStatus;
677 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800678 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800679 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
680 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
681 if (isProcessEnabled() &&
682 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
683 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
684 status_t cmdStatus;
685 uint32_t volume[2];
686 uint32_t *pVolume = NULL;
687 uint32_t size = sizeof(volume);
688 volume[0] = *left;
689 volume[1] = *right;
690 if (controller) {
691 pVolume = volume;
692 }
693 status = (*mEffectInterface)->command(mEffectInterface,
694 EFFECT_CMD_SET_VOLUME,
695 size,
696 volume,
697 &size,
698 pVolume);
699 if (controller && status == NO_ERROR && size == sizeof(volume)) {
700 *left = volume[0];
701 *right = volume[1];
702 }
703 }
704 return status;
705}
706
707status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
708{
709 if (device == AUDIO_DEVICE_NONE) {
710 return NO_ERROR;
711 }
712
713 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700714 if (mStatus != NO_ERROR) {
715 return mStatus;
716 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800717 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700718 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800719 status_t cmdStatus;
720 uint32_t size = sizeof(status_t);
721 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
722 EFFECT_CMD_SET_INPUT_DEVICE;
723 status = (*mEffectInterface)->command(mEffectInterface,
724 cmd,
725 sizeof(uint32_t),
726 &device,
727 &size,
728 &cmdStatus);
729 }
730 return status;
731}
732
733status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
734{
735 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700736 if (mStatus != NO_ERROR) {
737 return mStatus;
738 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800739 status_t status = NO_ERROR;
740 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
741 status_t cmdStatus;
742 uint32_t size = sizeof(status_t);
743 status = (*mEffectInterface)->command(mEffectInterface,
744 EFFECT_CMD_SET_AUDIO_MODE,
745 sizeof(audio_mode_t),
746 &mode,
747 &size,
748 &cmdStatus);
749 if (status == NO_ERROR) {
750 status = cmdStatus;
751 }
752 }
753 return status;
754}
755
756status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
757{
758 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700759 if (mStatus != NO_ERROR) {
760 return mStatus;
761 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800762 status_t status = NO_ERROR;
763 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
764 uint32_t size = 0;
765 status = (*mEffectInterface)->command(mEffectInterface,
766 EFFECT_CMD_SET_AUDIO_SOURCE,
767 sizeof(audio_source_t),
768 &source,
769 &size,
770 NULL);
771 }
772 return status;
773}
774
775void AudioFlinger::EffectModule::setSuspended(bool suspended)
776{
777 Mutex::Autolock _l(mLock);
778 mSuspended = suspended;
779}
780
781bool AudioFlinger::EffectModule::suspended() const
782{
783 Mutex::Autolock _l(mLock);
784 return mSuspended;
785}
786
787bool AudioFlinger::EffectModule::purgeHandles()
788{
789 bool enabled = false;
790 Mutex::Autolock _l(mLock);
791 for (size_t i = 0; i < mHandles.size(); i++) {
792 EffectHandle *handle = mHandles[i];
793 if (handle != NULL && !handle->destroyed_l()) {
794 handle->effect().clear();
795 if (handle->hasControl()) {
796 enabled = handle->enabled();
797 }
798 }
799 }
800 return enabled;
801}
802
Eric Laurent5baf2af2013-09-12 17:37:00 -0700803status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
804{
805 Mutex::Autolock _l(mLock);
806 if (mStatus != NO_ERROR) {
807 return mStatus;
808 }
809 status_t status = NO_ERROR;
810 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
811 status_t cmdStatus;
812 uint32_t size = sizeof(status_t);
813 effect_offload_param_t cmd;
814
815 cmd.isOffload = offloaded;
816 cmd.ioHandle = io;
817 status = (*mEffectInterface)->command(mEffectInterface,
818 EFFECT_CMD_OFFLOAD,
819 sizeof(effect_offload_param_t),
820 &cmd,
821 &size,
822 &cmdStatus);
823 if (status == NO_ERROR) {
824 status = cmdStatus;
825 }
826 mOffloaded = (status == NO_ERROR) ? offloaded : false;
827 } else {
828 if (offloaded) {
829 status = INVALID_OPERATION;
830 }
831 mOffloaded = false;
832 }
833 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
834 return status;
835}
836
837bool AudioFlinger::EffectModule::isOffloaded() const
838{
839 Mutex::Autolock _l(mLock);
840 return mOffloaded;
841}
842
Eric Laurentca7cc822012-11-19 14:55:58 -0800843void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
844{
845 const size_t SIZE = 256;
846 char buffer[SIZE];
847 String8 result;
848
849 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
850 result.append(buffer);
851
852 bool locked = AudioFlinger::dumpTryLock(mLock);
853 // failed to lock - AudioFlinger is probably deadlocked
854 if (!locked) {
855 result.append("\t\tCould not lock Fx mutex:\n");
856 }
857
858 result.append("\t\tSession Status State Engine:\n");
859 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n",
860 mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
861 result.append(buffer);
862
863 result.append("\t\tDescriptor:\n");
864 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
865 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
866 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
867 mDescriptor.uuid.node[2],
868 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
869 result.append(buffer);
870 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
871 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
872 mDescriptor.type.timeHiAndVersion,
873 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
874 mDescriptor.type.node[2],
875 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
876 result.append(buffer);
877 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
878 mDescriptor.apiVersion,
879 mDescriptor.flags);
880 result.append(buffer);
881 snprintf(buffer, SIZE, "\t\t- name: %s\n",
882 mDescriptor.name);
883 result.append(buffer);
884 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
885 mDescriptor.implementor);
886 result.append(buffer);
887
888 result.append("\t\t- Input configuration:\n");
889 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
890 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
891 (uint32_t)mConfig.inputCfg.buffer.raw,
892 mConfig.inputCfg.buffer.frameCount,
893 mConfig.inputCfg.samplingRate,
894 mConfig.inputCfg.channels,
895 mConfig.inputCfg.format);
896 result.append(buffer);
897
898 result.append("\t\t- Output configuration:\n");
899 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
900 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
901 (uint32_t)mConfig.outputCfg.buffer.raw,
902 mConfig.outputCfg.buffer.frameCount,
903 mConfig.outputCfg.samplingRate,
904 mConfig.outputCfg.channels,
905 mConfig.outputCfg.format);
906 result.append(buffer);
907
908 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
909 result.append(buffer);
910 result.append("\t\t\tPid Priority Ctrl Locked client server\n");
911 for (size_t i = 0; i < mHandles.size(); ++i) {
912 EffectHandle *handle = mHandles[i];
913 if (handle != NULL && !handle->destroyed_l()) {
914 handle->dump(buffer, SIZE);
915 result.append(buffer);
916 }
917 }
918
919 result.append("\n");
920
921 write(fd, result.string(), result.length());
922
923 if (locked) {
924 mLock.unlock();
925 }
926}
927
928// ----------------------------------------------------------------------------
929// EffectHandle implementation
930// ----------------------------------------------------------------------------
931
932#undef LOG_TAG
933#define LOG_TAG "AudioFlinger::EffectHandle"
934
935AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
936 const sp<AudioFlinger::Client>& client,
937 const sp<IEffectClient>& effectClient,
938 int32_t priority)
939 : BnEffect(),
940 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
941 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
942{
943 ALOGV("constructor %p", this);
944
945 if (client == 0) {
946 return;
947 }
948 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
949 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
950 if (mCblkMemory != 0) {
951 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
952
953 if (mCblk != NULL) {
954 new(mCblk) effect_param_cblk_t();
955 mBuffer = (uint8_t *)mCblk + bufOffset;
956 }
957 } else {
958 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE +
959 sizeof(effect_param_cblk_t));
960 return;
961 }
962}
963
964AudioFlinger::EffectHandle::~EffectHandle()
965{
966 ALOGV("Destructor %p", this);
967
968 if (mEffect == 0) {
969 mDestroyed = true;
970 return;
971 }
972 mEffect->lock();
973 mDestroyed = true;
974 mEffect->unlock();
975 disconnect(false);
976}
977
978status_t AudioFlinger::EffectHandle::enable()
979{
980 ALOGV("enable %p", this);
981 if (!mHasControl) {
982 return INVALID_OPERATION;
983 }
984 if (mEffect == 0) {
985 return DEAD_OBJECT;
986 }
987
988 if (mEnabled) {
989 return NO_ERROR;
990 }
991
992 mEnabled = true;
993
994 sp<ThreadBase> thread = mEffect->thread().promote();
995 if (thread != 0) {
996 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
997 }
998
999 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1000 if (mEffect->suspended()) {
1001 return NO_ERROR;
1002 }
1003
1004 status_t status = mEffect->setEnabled(true);
1005 if (status != NO_ERROR) {
1006 if (thread != 0) {
1007 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1008 }
1009 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001010 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001011 if (thread != 0) {
1012 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001013 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001014 Mutex::Autolock _l(t->mLock);
1015 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001016 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001017 if (!mEffect->isOffloadable()) {
1018 if (thread->type() == ThreadBase::OFFLOAD) {
1019 PlaybackThread *t = (PlaybackThread *)thread.get();
1020 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1021 }
1022 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1023 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1024 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001025 }
1026 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001027 }
1028 return status;
1029}
1030
1031status_t AudioFlinger::EffectHandle::disable()
1032{
1033 ALOGV("disable %p", this);
1034 if (!mHasControl) {
1035 return INVALID_OPERATION;
1036 }
1037 if (mEffect == 0) {
1038 return DEAD_OBJECT;
1039 }
1040
1041 if (!mEnabled) {
1042 return NO_ERROR;
1043 }
1044 mEnabled = false;
1045
1046 if (mEffect->suspended()) {
1047 return NO_ERROR;
1048 }
1049
1050 status_t status = mEffect->setEnabled(false);
1051
1052 sp<ThreadBase> thread = mEffect->thread().promote();
1053 if (thread != 0) {
1054 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001055 if (thread->type() == ThreadBase::OFFLOAD) {
1056 PlaybackThread *t = (PlaybackThread *)thread.get();
1057 Mutex::Autolock _l(t->mLock);
1058 t->broadcast_l();
1059 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001060 }
1061
1062 return status;
1063}
1064
1065void AudioFlinger::EffectHandle::disconnect()
1066{
1067 disconnect(true);
1068}
1069
1070void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1071{
1072 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1073 if (mEffect == 0) {
1074 return;
1075 }
1076 // restore suspended effects if the disconnected handle was enabled and the last one.
1077 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1078 sp<ThreadBase> thread = mEffect->thread().promote();
1079 if (thread != 0) {
1080 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1081 }
1082 }
1083
1084 // release sp on module => module destructor can be called now
1085 mEffect.clear();
1086 if (mClient != 0) {
1087 if (mCblk != NULL) {
1088 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1089 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1090 }
1091 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
1092 // Client destructor must run with AudioFlinger mutex locked
1093 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
1094 mClient.clear();
1095 }
1096}
1097
1098status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1099 uint32_t cmdSize,
1100 void *pCmdData,
1101 uint32_t *replySize,
1102 void *pReplyData)
1103{
1104 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1105 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1106
1107 // only get parameter command is permitted for applications not controlling the effect
1108 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1109 return INVALID_OPERATION;
1110 }
1111 if (mEffect == 0) {
1112 return DEAD_OBJECT;
1113 }
1114 if (mClient == 0) {
1115 return INVALID_OPERATION;
1116 }
1117
1118 // handle commands that are not forwarded transparently to effect engine
1119 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1120 // No need to trylock() here as this function is executed in the binder thread serving a
1121 // particular client process: no risk to block the whole media server process or mixer
1122 // threads if we are stuck here
1123 Mutex::Autolock _l(mCblk->lock);
1124 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1125 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1126 mCblk->serverIndex = 0;
1127 mCblk->clientIndex = 0;
1128 return BAD_VALUE;
1129 }
1130 status_t status = NO_ERROR;
1131 while (mCblk->serverIndex < mCblk->clientIndex) {
1132 int reply;
1133 uint32_t rsize = sizeof(int);
1134 int *p = (int *)(mBuffer + mCblk->serverIndex);
1135 int size = *p++;
1136 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1137 ALOGW("command(): invalid parameter block size");
1138 break;
1139 }
1140 effect_param_t *param = (effect_param_t *)p;
1141 if (param->psize == 0 || param->vsize == 0) {
1142 ALOGW("command(): null parameter or value size");
1143 mCblk->serverIndex += size;
1144 continue;
1145 }
1146 uint32_t psize = sizeof(effect_param_t) +
1147 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1148 param->vsize;
1149 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1150 psize,
1151 p,
1152 &rsize,
1153 &reply);
1154 // stop at first error encountered
1155 if (ret != NO_ERROR) {
1156 status = ret;
1157 *(int *)pReplyData = reply;
1158 break;
1159 } else if (reply != NO_ERROR) {
1160 *(int *)pReplyData = reply;
1161 break;
1162 }
1163 mCblk->serverIndex += size;
1164 }
1165 mCblk->serverIndex = 0;
1166 mCblk->clientIndex = 0;
1167 return status;
1168 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1169 *(int *)pReplyData = NO_ERROR;
1170 return enable();
1171 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1172 *(int *)pReplyData = NO_ERROR;
1173 return disable();
1174 }
1175
1176 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1177}
1178
1179void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1180{
1181 ALOGV("setControl %p control %d", this, hasControl);
1182
1183 mHasControl = hasControl;
1184 mEnabled = enabled;
1185
1186 if (signal && mEffectClient != 0) {
1187 mEffectClient->controlStatusChanged(hasControl);
1188 }
1189}
1190
1191void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1192 uint32_t cmdSize,
1193 void *pCmdData,
1194 uint32_t replySize,
1195 void *pReplyData)
1196{
1197 if (mEffectClient != 0) {
1198 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1199 }
1200}
1201
1202
1203
1204void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1205{
1206 if (mEffectClient != 0) {
1207 mEffectClient->enableStatusChanged(enabled);
1208 }
1209}
1210
1211status_t AudioFlinger::EffectHandle::onTransact(
1212 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1213{
1214 return BnEffect::onTransact(code, data, reply, flags);
1215}
1216
1217
1218void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
1219{
1220 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1221
1222 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
1223 (mClient == 0) ? getpid_cached : mClient->pid(),
1224 mPriority,
1225 mHasControl,
1226 !locked,
1227 mCblk ? mCblk->clientIndex : 0,
1228 mCblk ? mCblk->serverIndex : 0
1229 );
1230
1231 if (locked) {
1232 mCblk->lock.unlock();
1233 }
1234}
1235
1236#undef LOG_TAG
1237#define LOG_TAG "AudioFlinger::EffectChain"
1238
1239AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1240 int sessionId)
1241 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1242 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
1243 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
1244{
1245 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1246 if (thread == NULL) {
1247 return;
1248 }
1249 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1250 thread->frameCount();
1251}
1252
1253AudioFlinger::EffectChain::~EffectChain()
1254{
1255 if (mOwnInBuffer) {
1256 delete mInBuffer;
1257 }
1258
1259}
1260
1261// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1262sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1263 effect_descriptor_t *descriptor)
1264{
1265 size_t size = mEffects.size();
1266
1267 for (size_t i = 0; i < size; i++) {
1268 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1269 return mEffects[i];
1270 }
1271 }
1272 return 0;
1273}
1274
1275// getEffectFromId_l() must be called with ThreadBase::mLock held
1276sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1277{
1278 size_t size = mEffects.size();
1279
1280 for (size_t i = 0; i < size; i++) {
1281 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1282 if (id == 0 || mEffects[i]->id() == id) {
1283 return mEffects[i];
1284 }
1285 }
1286 return 0;
1287}
1288
1289// getEffectFromType_l() must be called with ThreadBase::mLock held
1290sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1291 const effect_uuid_t *type)
1292{
1293 size_t size = mEffects.size();
1294
1295 for (size_t i = 0; i < size; i++) {
1296 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1297 return mEffects[i];
1298 }
1299 }
1300 return 0;
1301}
1302
1303void AudioFlinger::EffectChain::clearInputBuffer()
1304{
1305 Mutex::Autolock _l(mLock);
1306 sp<ThreadBase> thread = mThread.promote();
1307 if (thread == 0) {
1308 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1309 return;
1310 }
1311 clearInputBuffer_l(thread);
1312}
1313
1314// Must be called with EffectChain::mLock locked
1315void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1316{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001317 memset(mInBuffer, 0, thread->frameCount() * thread->frameSize());
Eric Laurentca7cc822012-11-19 14:55:58 -08001318}
1319
1320// Must be called with EffectChain::mLock locked
1321void AudioFlinger::EffectChain::process_l()
1322{
1323 sp<ThreadBase> thread = mThread.promote();
1324 if (thread == 0) {
1325 ALOGW("process_l(): cannot promote mixer thread");
1326 return;
1327 }
1328 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1329 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001330 // never process effects when:
1331 // - on an OFFLOAD thread
1332 // - no more tracks are on the session and the effect tail has been rendered
1333 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001334 if (!isGlobalSession) {
1335 bool tracksOnSession = (trackCnt() != 0);
1336
1337 if (!tracksOnSession && mTailBufferCount == 0) {
1338 doProcess = false;
1339 }
1340
1341 if (activeTrackCnt() == 0) {
1342 // if no track is active and the effect tail has not been rendered,
1343 // the input buffer must be cleared here as the mixer process will not do it
1344 if (tracksOnSession || mTailBufferCount > 0) {
1345 clearInputBuffer_l(thread);
1346 if (mTailBufferCount > 0) {
1347 mTailBufferCount--;
1348 }
1349 }
1350 }
1351 }
1352
1353 size_t size = mEffects.size();
1354 if (doProcess) {
1355 for (size_t i = 0; i < size; i++) {
1356 mEffects[i]->process();
1357 }
1358 }
1359 for (size_t i = 0; i < size; i++) {
1360 mEffects[i]->updateState();
1361 }
1362}
1363
1364// addEffect_l() must be called with PlaybackThread::mLock held
1365status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1366{
1367 effect_descriptor_t desc = effect->desc();
1368 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1369
1370 Mutex::Autolock _l(mLock);
1371 effect->setChain(this);
1372 sp<ThreadBase> thread = mThread.promote();
1373 if (thread == 0) {
1374 return NO_INIT;
1375 }
1376 effect->setThread(thread);
1377
1378 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1379 // Auxiliary effects are inserted at the beginning of mEffects vector as
1380 // they are processed first and accumulated in chain input buffer
1381 mEffects.insertAt(effect, 0);
1382
1383 // the input buffer for auxiliary effect contains mono samples in
1384 // 32 bit format. This is to avoid saturation in AudoMixer
1385 // accumulation stage. Saturation is done in EffectModule::process() before
1386 // calling the process in effect engine
1387 size_t numSamples = thread->frameCount();
1388 int32_t *buffer = new int32_t[numSamples];
1389 memset(buffer, 0, numSamples * sizeof(int32_t));
1390 effect->setInBuffer((int16_t *)buffer);
1391 // auxiliary effects output samples to chain input buffer for further processing
1392 // by insert effects
1393 effect->setOutBuffer(mInBuffer);
1394 } else {
1395 // Insert effects are inserted at the end of mEffects vector as they are processed
1396 // after track and auxiliary effects.
1397 // Insert effect order as a function of indicated preference:
1398 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1399 // another effect is present
1400 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1401 // last effect claiming first position
1402 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1403 // first effect claiming last position
1404 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1405 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1406 // already present
1407
1408 size_t size = mEffects.size();
1409 size_t idx_insert = size;
1410 ssize_t idx_insert_first = -1;
1411 ssize_t idx_insert_last = -1;
1412
1413 for (size_t i = 0; i < size; i++) {
1414 effect_descriptor_t d = mEffects[i]->desc();
1415 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1416 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1417 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1418 // check invalid effect chaining combinations
1419 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1420 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1421 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1422 desc.name, d.name);
1423 return INVALID_OPERATION;
1424 }
1425 // remember position of first insert effect and by default
1426 // select this as insert position for new effect
1427 if (idx_insert == size) {
1428 idx_insert = i;
1429 }
1430 // remember position of last insert effect claiming
1431 // first position
1432 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1433 idx_insert_first = i;
1434 }
1435 // remember position of first insert effect claiming
1436 // last position
1437 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1438 idx_insert_last == -1) {
1439 idx_insert_last = i;
1440 }
1441 }
1442 }
1443
1444 // modify idx_insert from first position if needed
1445 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1446 if (idx_insert_last != -1) {
1447 idx_insert = idx_insert_last;
1448 } else {
1449 idx_insert = size;
1450 }
1451 } else {
1452 if (idx_insert_first != -1) {
1453 idx_insert = idx_insert_first + 1;
1454 }
1455 }
1456
1457 // always read samples from chain input buffer
1458 effect->setInBuffer(mInBuffer);
1459
1460 // if last effect in the chain, output samples to chain
1461 // output buffer, otherwise to chain input buffer
1462 if (idx_insert == size) {
1463 if (idx_insert != 0) {
1464 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1465 mEffects[idx_insert-1]->configure();
1466 }
1467 effect->setOutBuffer(mOutBuffer);
1468 } else {
1469 effect->setOutBuffer(mInBuffer);
1470 }
1471 mEffects.insertAt(effect, idx_insert);
1472
1473 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1474 idx_insert);
1475 }
1476 effect->configure();
1477 return NO_ERROR;
1478}
1479
1480// removeEffect_l() must be called with PlaybackThread::mLock held
1481size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1482{
1483 Mutex::Autolock _l(mLock);
1484 size_t size = mEffects.size();
1485 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1486
1487 for (size_t i = 0; i < size; i++) {
1488 if (effect == mEffects[i]) {
1489 // calling stop here will remove pre-processing effect from the audio HAL.
1490 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1491 // the middle of a read from audio HAL
1492 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1493 mEffects[i]->state() == EffectModule::STOPPING) {
1494 mEffects[i]->stop();
1495 }
1496 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1497 delete[] effect->inBuffer();
1498 } else {
1499 if (i == size - 1 && i != 0) {
1500 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1501 mEffects[i - 1]->configure();
1502 }
1503 }
1504 mEffects.removeAt(i);
1505 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1506 this, i);
1507 break;
1508 }
1509 }
1510
1511 return mEffects.size();
1512}
1513
1514// setDevice_l() must be called with PlaybackThread::mLock held
1515void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1516{
1517 size_t size = mEffects.size();
1518 for (size_t i = 0; i < size; i++) {
1519 mEffects[i]->setDevice(device);
1520 }
1521}
1522
1523// setMode_l() must be called with PlaybackThread::mLock held
1524void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1525{
1526 size_t size = mEffects.size();
1527 for (size_t i = 0; i < size; i++) {
1528 mEffects[i]->setMode(mode);
1529 }
1530}
1531
1532// setAudioSource_l() must be called with PlaybackThread::mLock held
1533void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1534{
1535 size_t size = mEffects.size();
1536 for (size_t i = 0; i < size; i++) {
1537 mEffects[i]->setAudioSource(source);
1538 }
1539}
1540
1541// setVolume_l() must be called with PlaybackThread::mLock held
1542bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1543{
1544 uint32_t newLeft = *left;
1545 uint32_t newRight = *right;
1546 bool hasControl = false;
1547 int ctrlIdx = -1;
1548 size_t size = mEffects.size();
1549
1550 // first update volume controller
1551 for (size_t i = size; i > 0; i--) {
1552 if (mEffects[i - 1]->isProcessEnabled() &&
1553 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1554 ctrlIdx = i - 1;
1555 hasControl = true;
1556 break;
1557 }
1558 }
1559
1560 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
1561 if (hasControl) {
1562 *left = mNewLeftVolume;
1563 *right = mNewRightVolume;
1564 }
1565 return hasControl;
1566 }
1567
1568 mVolumeCtrlIdx = ctrlIdx;
1569 mLeftVolume = newLeft;
1570 mRightVolume = newRight;
1571
1572 // second get volume update from volume controller
1573 if (ctrlIdx >= 0) {
1574 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1575 mNewLeftVolume = newLeft;
1576 mNewRightVolume = newRight;
1577 }
1578 // then indicate volume to all other effects in chain.
1579 // Pass altered volume to effects before volume controller
1580 // and requested volume to effects after controller
1581 uint32_t lVol = newLeft;
1582 uint32_t rVol = newRight;
1583
1584 for (size_t i = 0; i < size; i++) {
1585 if ((int)i == ctrlIdx) {
1586 continue;
1587 }
1588 // this also works for ctrlIdx == -1 when there is no volume controller
1589 if ((int)i > ctrlIdx) {
1590 lVol = *left;
1591 rVol = *right;
1592 }
1593 mEffects[i]->setVolume(&lVol, &rVol, false);
1594 }
1595 *left = newLeft;
1596 *right = newRight;
1597
1598 return hasControl;
1599}
1600
1601void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1602{
1603 const size_t SIZE = 256;
1604 char buffer[SIZE];
1605 String8 result;
1606
1607 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
1608 result.append(buffer);
1609
1610 bool locked = AudioFlinger::dumpTryLock(mLock);
1611 // failed to lock - AudioFlinger is probably deadlocked
1612 if (!locked) {
1613 result.append("\tCould not lock mutex:\n");
1614 }
1615
1616 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
1617 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
1618 mEffects.size(),
1619 (uint32_t)mInBuffer,
1620 (uint32_t)mOutBuffer,
1621 mActiveTrackCnt);
1622 result.append(buffer);
1623 write(fd, result.string(), result.size());
1624
1625 for (size_t i = 0; i < mEffects.size(); ++i) {
1626 sp<EffectModule> effect = mEffects[i];
1627 if (effect != 0) {
1628 effect->dump(fd, args);
1629 }
1630 }
1631
1632 if (locked) {
1633 mLock.unlock();
1634 }
1635}
1636
1637// must be called with ThreadBase::mLock held
1638void AudioFlinger::EffectChain::setEffectSuspended_l(
1639 const effect_uuid_t *type, bool suspend)
1640{
1641 sp<SuspendedEffectDesc> desc;
1642 // use effect type UUID timelow as key as there is no real risk of identical
1643 // timeLow fields among effect type UUIDs.
1644 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1645 if (suspend) {
1646 if (index >= 0) {
1647 desc = mSuspendedEffects.valueAt(index);
1648 } else {
1649 desc = new SuspendedEffectDesc();
1650 desc->mType = *type;
1651 mSuspendedEffects.add(type->timeLow, desc);
1652 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1653 }
1654 if (desc->mRefCount++ == 0) {
1655 sp<EffectModule> effect = getEffectIfEnabled(type);
1656 if (effect != 0) {
1657 desc->mEffect = effect;
1658 effect->setSuspended(true);
1659 effect->setEnabled(false);
1660 }
1661 }
1662 } else {
1663 if (index < 0) {
1664 return;
1665 }
1666 desc = mSuspendedEffects.valueAt(index);
1667 if (desc->mRefCount <= 0) {
1668 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1669 desc->mRefCount = 1;
1670 }
1671 if (--desc->mRefCount == 0) {
1672 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1673 if (desc->mEffect != 0) {
1674 sp<EffectModule> effect = desc->mEffect.promote();
1675 if (effect != 0) {
1676 effect->setSuspended(false);
1677 effect->lock();
1678 EffectHandle *handle = effect->controlHandle_l();
1679 if (handle != NULL && !handle->destroyed_l()) {
1680 effect->setEnabled_l(handle->enabled());
1681 }
1682 effect->unlock();
1683 }
1684 desc->mEffect.clear();
1685 }
1686 mSuspendedEffects.removeItemsAt(index);
1687 }
1688 }
1689}
1690
1691// must be called with ThreadBase::mLock held
1692void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1693{
1694 sp<SuspendedEffectDesc> desc;
1695
1696 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1697 if (suspend) {
1698 if (index >= 0) {
1699 desc = mSuspendedEffects.valueAt(index);
1700 } else {
1701 desc = new SuspendedEffectDesc();
1702 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1703 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1704 }
1705 if (desc->mRefCount++ == 0) {
1706 Vector< sp<EffectModule> > effects;
1707 getSuspendEligibleEffects(effects);
1708 for (size_t i = 0; i < effects.size(); i++) {
1709 setEffectSuspended_l(&effects[i]->desc().type, true);
1710 }
1711 }
1712 } else {
1713 if (index < 0) {
1714 return;
1715 }
1716 desc = mSuspendedEffects.valueAt(index);
1717 if (desc->mRefCount <= 0) {
1718 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1719 desc->mRefCount = 1;
1720 }
1721 if (--desc->mRefCount == 0) {
1722 Vector<const effect_uuid_t *> types;
1723 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1724 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1725 continue;
1726 }
1727 types.add(&mSuspendedEffects.valueAt(i)->mType);
1728 }
1729 for (size_t i = 0; i < types.size(); i++) {
1730 setEffectSuspended_l(types[i], false);
1731 }
1732 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1733 mSuspendedEffects.keyAt(index));
1734 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1735 }
1736 }
1737}
1738
1739
1740// The volume effect is used for automated tests only
1741#ifndef OPENSL_ES_H_
1742static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1743 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1744const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1745#endif //OPENSL_ES_H_
1746
1747bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1748{
1749 // auxiliary effects and visualizer are never suspended on output mix
1750 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1751 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1752 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1753 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1754 return false;
1755 }
1756 return true;
1757}
1758
1759void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1760 Vector< sp<AudioFlinger::EffectModule> > &effects)
1761{
1762 effects.clear();
1763 for (size_t i = 0; i < mEffects.size(); i++) {
1764 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1765 effects.add(mEffects[i]);
1766 }
1767 }
1768}
1769
1770sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1771 const effect_uuid_t *type)
1772{
1773 sp<EffectModule> effect = getEffectFromType_l(type);
1774 return effect != 0 && effect->isEnabled() ? effect : 0;
1775}
1776
1777void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1778 bool enabled)
1779{
1780 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1781 if (enabled) {
1782 if (index < 0) {
1783 // if the effect is not suspend check if all effects are suspended
1784 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1785 if (index < 0) {
1786 return;
1787 }
1788 if (!isEffectEligibleForSuspend(effect->desc())) {
1789 return;
1790 }
1791 setEffectSuspended_l(&effect->desc().type, enabled);
1792 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1793 if (index < 0) {
1794 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1795 return;
1796 }
1797 }
1798 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1799 effect->desc().type.timeLow);
1800 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1801 // if effect is requested to suspended but was not yet enabled, supend it now.
1802 if (desc->mEffect == 0) {
1803 desc->mEffect = effect;
1804 effect->setEnabled(false);
1805 effect->setSuspended(true);
1806 }
1807 } else {
1808 if (index < 0) {
1809 return;
1810 }
1811 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1812 effect->desc().type.timeLow);
1813 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1814 desc->mEffect.clear();
1815 effect->setSuspended(false);
1816 }
1817}
1818
Eric Laurent5baf2af2013-09-12 17:37:00 -07001819bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001820{
1821 Mutex::Autolock _l(mLock);
1822 size_t size = mEffects.size();
1823 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001824 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001825 return true;
1826 }
1827 }
1828 return false;
1829}
1830
Eric Laurentca7cc822012-11-19 14:55:58 -08001831}; // namespace android