blob: e9e4bb936bf26161cd5f8bdd26c7baba7dbbc345 [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);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001124
1125 // keep local copy of index in case of client corruption b/32220769
1126 const uint32_t clientIndex = mCblk->clientIndex;
1127 const uint32_t serverIndex = mCblk->serverIndex;
1128 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1129 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001130 mCblk->serverIndex = 0;
1131 mCblk->clientIndex = 0;
1132 return BAD_VALUE;
1133 }
1134 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001135 effect_param_t *param = NULL;
1136 for (uint32_t index = serverIndex; index < clientIndex;) {
1137 int *p = (int *)(mBuffer + index);
1138 const int size = *p++;
1139 if (size < 0
1140 || size > EFFECT_PARAM_BUFFER_SIZE
1141 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001142 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001143 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001144 break;
1145 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001146
1147 // copy to local memory in case of client corruption b/32220769
1148 param = (effect_param_t *)realloc(param, size);
1149 if (param == NULL) {
1150 ALOGW("command(): out of memory");
1151 status = NO_MEMORY;
1152 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001153 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001154 memcpy(param, p, size);
1155
1156 int reply = 0;
1157 uint32_t rsize = sizeof(reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001158 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001159 size,
1160 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001161 &rsize,
1162 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001163
1164 // verify shared memory: server index shouldn't change; client index can't go back.
1165 if (serverIndex != mCblk->serverIndex
1166 || clientIndex > mCblk->clientIndex) {
1167 android_errorWriteLog(0x534e4554, "32220769");
1168 status = BAD_VALUE;
1169 break;
1170 }
1171
Eric Laurentca7cc822012-11-19 14:55:58 -08001172 // stop at first error encountered
1173 if (ret != NO_ERROR) {
1174 status = ret;
1175 *(int *)pReplyData = reply;
1176 break;
1177 } else if (reply != NO_ERROR) {
1178 *(int *)pReplyData = reply;
1179 break;
1180 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001181 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001182 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001183 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001184 mCblk->serverIndex = 0;
1185 mCblk->clientIndex = 0;
1186 return status;
1187 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1188 *(int *)pReplyData = NO_ERROR;
1189 return enable();
1190 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1191 *(int *)pReplyData = NO_ERROR;
1192 return disable();
1193 }
1194
1195 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1196}
1197
1198void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1199{
1200 ALOGV("setControl %p control %d", this, hasControl);
1201
1202 mHasControl = hasControl;
1203 mEnabled = enabled;
1204
1205 if (signal && mEffectClient != 0) {
1206 mEffectClient->controlStatusChanged(hasControl);
1207 }
1208}
1209
1210void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1211 uint32_t cmdSize,
1212 void *pCmdData,
1213 uint32_t replySize,
1214 void *pReplyData)
1215{
1216 if (mEffectClient != 0) {
1217 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1218 }
1219}
1220
1221
1222
1223void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1224{
1225 if (mEffectClient != 0) {
1226 mEffectClient->enableStatusChanged(enabled);
1227 }
1228}
1229
1230status_t AudioFlinger::EffectHandle::onTransact(
1231 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1232{
1233 return BnEffect::onTransact(code, data, reply, flags);
1234}
1235
1236
1237void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
1238{
1239 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1240
1241 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
1242 (mClient == 0) ? getpid_cached : mClient->pid(),
1243 mPriority,
1244 mHasControl,
1245 !locked,
1246 mCblk ? mCblk->clientIndex : 0,
1247 mCblk ? mCblk->serverIndex : 0
1248 );
1249
1250 if (locked) {
1251 mCblk->lock.unlock();
1252 }
1253}
1254
1255#undef LOG_TAG
1256#define LOG_TAG "AudioFlinger::EffectChain"
1257
1258AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1259 int sessionId)
1260 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1261 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
1262 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
1263{
1264 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1265 if (thread == NULL) {
1266 return;
1267 }
1268 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1269 thread->frameCount();
1270}
1271
1272AudioFlinger::EffectChain::~EffectChain()
1273{
1274 if (mOwnInBuffer) {
1275 delete mInBuffer;
1276 }
1277
1278}
1279
1280// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1281sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1282 effect_descriptor_t *descriptor)
1283{
1284 size_t size = mEffects.size();
1285
1286 for (size_t i = 0; i < size; i++) {
1287 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1288 return mEffects[i];
1289 }
1290 }
1291 return 0;
1292}
1293
1294// getEffectFromId_l() must be called with ThreadBase::mLock held
1295sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1296{
1297 size_t size = mEffects.size();
1298
1299 for (size_t i = 0; i < size; i++) {
1300 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1301 if (id == 0 || mEffects[i]->id() == id) {
1302 return mEffects[i];
1303 }
1304 }
1305 return 0;
1306}
1307
1308// getEffectFromType_l() must be called with ThreadBase::mLock held
1309sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1310 const effect_uuid_t *type)
1311{
1312 size_t size = mEffects.size();
1313
1314 for (size_t i = 0; i < size; i++) {
1315 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1316 return mEffects[i];
1317 }
1318 }
1319 return 0;
1320}
1321
1322void AudioFlinger::EffectChain::clearInputBuffer()
1323{
1324 Mutex::Autolock _l(mLock);
1325 sp<ThreadBase> thread = mThread.promote();
1326 if (thread == 0) {
1327 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1328 return;
1329 }
1330 clearInputBuffer_l(thread);
1331}
1332
1333// Must be called with EffectChain::mLock locked
1334void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1335{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001336 memset(mInBuffer, 0, thread->frameCount() * thread->frameSize());
Eric Laurentca7cc822012-11-19 14:55:58 -08001337}
1338
1339// Must be called with EffectChain::mLock locked
1340void AudioFlinger::EffectChain::process_l()
1341{
1342 sp<ThreadBase> thread = mThread.promote();
1343 if (thread == 0) {
1344 ALOGW("process_l(): cannot promote mixer thread");
1345 return;
1346 }
1347 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1348 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001349 // never process effects when:
1350 // - on an OFFLOAD thread
1351 // - no more tracks are on the session and the effect tail has been rendered
1352 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001353 if (!isGlobalSession) {
1354 bool tracksOnSession = (trackCnt() != 0);
1355
1356 if (!tracksOnSession && mTailBufferCount == 0) {
1357 doProcess = false;
1358 }
1359
1360 if (activeTrackCnt() == 0) {
1361 // if no track is active and the effect tail has not been rendered,
1362 // the input buffer must be cleared here as the mixer process will not do it
1363 if (tracksOnSession || mTailBufferCount > 0) {
1364 clearInputBuffer_l(thread);
1365 if (mTailBufferCount > 0) {
1366 mTailBufferCount--;
1367 }
1368 }
1369 }
1370 }
1371
1372 size_t size = mEffects.size();
1373 if (doProcess) {
1374 for (size_t i = 0; i < size; i++) {
1375 mEffects[i]->process();
1376 }
1377 }
1378 for (size_t i = 0; i < size; i++) {
1379 mEffects[i]->updateState();
1380 }
1381}
1382
1383// addEffect_l() must be called with PlaybackThread::mLock held
1384status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1385{
1386 effect_descriptor_t desc = effect->desc();
1387 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1388
1389 Mutex::Autolock _l(mLock);
1390 effect->setChain(this);
1391 sp<ThreadBase> thread = mThread.promote();
1392 if (thread == 0) {
1393 return NO_INIT;
1394 }
1395 effect->setThread(thread);
1396
1397 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1398 // Auxiliary effects are inserted at the beginning of mEffects vector as
1399 // they are processed first and accumulated in chain input buffer
1400 mEffects.insertAt(effect, 0);
1401
1402 // the input buffer for auxiliary effect contains mono samples in
1403 // 32 bit format. This is to avoid saturation in AudoMixer
1404 // accumulation stage. Saturation is done in EffectModule::process() before
1405 // calling the process in effect engine
1406 size_t numSamples = thread->frameCount();
1407 int32_t *buffer = new int32_t[numSamples];
1408 memset(buffer, 0, numSamples * sizeof(int32_t));
1409 effect->setInBuffer((int16_t *)buffer);
1410 // auxiliary effects output samples to chain input buffer for further processing
1411 // by insert effects
1412 effect->setOutBuffer(mInBuffer);
1413 } else {
1414 // Insert effects are inserted at the end of mEffects vector as they are processed
1415 // after track and auxiliary effects.
1416 // Insert effect order as a function of indicated preference:
1417 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1418 // another effect is present
1419 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1420 // last effect claiming first position
1421 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1422 // first effect claiming last position
1423 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1424 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1425 // already present
1426
1427 size_t size = mEffects.size();
1428 size_t idx_insert = size;
1429 ssize_t idx_insert_first = -1;
1430 ssize_t idx_insert_last = -1;
1431
1432 for (size_t i = 0; i < size; i++) {
1433 effect_descriptor_t d = mEffects[i]->desc();
1434 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1435 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1436 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1437 // check invalid effect chaining combinations
1438 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1439 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1440 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1441 desc.name, d.name);
1442 return INVALID_OPERATION;
1443 }
1444 // remember position of first insert effect and by default
1445 // select this as insert position for new effect
1446 if (idx_insert == size) {
1447 idx_insert = i;
1448 }
1449 // remember position of last insert effect claiming
1450 // first position
1451 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1452 idx_insert_first = i;
1453 }
1454 // remember position of first insert effect claiming
1455 // last position
1456 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1457 idx_insert_last == -1) {
1458 idx_insert_last = i;
1459 }
1460 }
1461 }
1462
1463 // modify idx_insert from first position if needed
1464 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1465 if (idx_insert_last != -1) {
1466 idx_insert = idx_insert_last;
1467 } else {
1468 idx_insert = size;
1469 }
1470 } else {
1471 if (idx_insert_first != -1) {
1472 idx_insert = idx_insert_first + 1;
1473 }
1474 }
1475
1476 // always read samples from chain input buffer
1477 effect->setInBuffer(mInBuffer);
1478
1479 // if last effect in the chain, output samples to chain
1480 // output buffer, otherwise to chain input buffer
1481 if (idx_insert == size) {
1482 if (idx_insert != 0) {
1483 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1484 mEffects[idx_insert-1]->configure();
1485 }
1486 effect->setOutBuffer(mOutBuffer);
1487 } else {
1488 effect->setOutBuffer(mInBuffer);
1489 }
1490 mEffects.insertAt(effect, idx_insert);
1491
1492 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1493 idx_insert);
1494 }
1495 effect->configure();
1496 return NO_ERROR;
1497}
1498
1499// removeEffect_l() must be called with PlaybackThread::mLock held
1500size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1501{
1502 Mutex::Autolock _l(mLock);
1503 size_t size = mEffects.size();
1504 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1505
1506 for (size_t i = 0; i < size; i++) {
1507 if (effect == mEffects[i]) {
1508 // calling stop here will remove pre-processing effect from the audio HAL.
1509 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1510 // the middle of a read from audio HAL
1511 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1512 mEffects[i]->state() == EffectModule::STOPPING) {
1513 mEffects[i]->stop();
1514 }
1515 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1516 delete[] effect->inBuffer();
1517 } else {
1518 if (i == size - 1 && i != 0) {
1519 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1520 mEffects[i - 1]->configure();
1521 }
1522 }
1523 mEffects.removeAt(i);
1524 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1525 this, i);
1526 break;
1527 }
1528 }
1529
1530 return mEffects.size();
1531}
1532
1533// setDevice_l() must be called with PlaybackThread::mLock held
1534void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1535{
1536 size_t size = mEffects.size();
1537 for (size_t i = 0; i < size; i++) {
1538 mEffects[i]->setDevice(device);
1539 }
1540}
1541
1542// setMode_l() must be called with PlaybackThread::mLock held
1543void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1544{
1545 size_t size = mEffects.size();
1546 for (size_t i = 0; i < size; i++) {
1547 mEffects[i]->setMode(mode);
1548 }
1549}
1550
1551// setAudioSource_l() must be called with PlaybackThread::mLock held
1552void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1553{
1554 size_t size = mEffects.size();
1555 for (size_t i = 0; i < size; i++) {
1556 mEffects[i]->setAudioSource(source);
1557 }
1558}
1559
1560// setVolume_l() must be called with PlaybackThread::mLock held
1561bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1562{
1563 uint32_t newLeft = *left;
1564 uint32_t newRight = *right;
1565 bool hasControl = false;
1566 int ctrlIdx = -1;
1567 size_t size = mEffects.size();
1568
1569 // first update volume controller
1570 for (size_t i = size; i > 0; i--) {
1571 if (mEffects[i - 1]->isProcessEnabled() &&
1572 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1573 ctrlIdx = i - 1;
1574 hasControl = true;
1575 break;
1576 }
1577 }
1578
1579 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
1580 if (hasControl) {
1581 *left = mNewLeftVolume;
1582 *right = mNewRightVolume;
1583 }
1584 return hasControl;
1585 }
1586
1587 mVolumeCtrlIdx = ctrlIdx;
1588 mLeftVolume = newLeft;
1589 mRightVolume = newRight;
1590
1591 // second get volume update from volume controller
1592 if (ctrlIdx >= 0) {
1593 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1594 mNewLeftVolume = newLeft;
1595 mNewRightVolume = newRight;
1596 }
1597 // then indicate volume to all other effects in chain.
1598 // Pass altered volume to effects before volume controller
1599 // and requested volume to effects after controller
1600 uint32_t lVol = newLeft;
1601 uint32_t rVol = newRight;
1602
1603 for (size_t i = 0; i < size; i++) {
1604 if ((int)i == ctrlIdx) {
1605 continue;
1606 }
1607 // this also works for ctrlIdx == -1 when there is no volume controller
1608 if ((int)i > ctrlIdx) {
1609 lVol = *left;
1610 rVol = *right;
1611 }
1612 mEffects[i]->setVolume(&lVol, &rVol, false);
1613 }
1614 *left = newLeft;
1615 *right = newRight;
1616
1617 return hasControl;
1618}
1619
1620void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1621{
1622 const size_t SIZE = 256;
1623 char buffer[SIZE];
1624 String8 result;
1625
1626 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
1627 result.append(buffer);
1628
1629 bool locked = AudioFlinger::dumpTryLock(mLock);
1630 // failed to lock - AudioFlinger is probably deadlocked
1631 if (!locked) {
1632 result.append("\tCould not lock mutex:\n");
1633 }
1634
1635 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
1636 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
1637 mEffects.size(),
1638 (uint32_t)mInBuffer,
1639 (uint32_t)mOutBuffer,
1640 mActiveTrackCnt);
1641 result.append(buffer);
1642 write(fd, result.string(), result.size());
1643
1644 for (size_t i = 0; i < mEffects.size(); ++i) {
1645 sp<EffectModule> effect = mEffects[i];
1646 if (effect != 0) {
1647 effect->dump(fd, args);
1648 }
1649 }
1650
1651 if (locked) {
1652 mLock.unlock();
1653 }
1654}
1655
1656// must be called with ThreadBase::mLock held
1657void AudioFlinger::EffectChain::setEffectSuspended_l(
1658 const effect_uuid_t *type, bool suspend)
1659{
1660 sp<SuspendedEffectDesc> desc;
1661 // use effect type UUID timelow as key as there is no real risk of identical
1662 // timeLow fields among effect type UUIDs.
1663 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1664 if (suspend) {
1665 if (index >= 0) {
1666 desc = mSuspendedEffects.valueAt(index);
1667 } else {
1668 desc = new SuspendedEffectDesc();
1669 desc->mType = *type;
1670 mSuspendedEffects.add(type->timeLow, desc);
1671 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1672 }
1673 if (desc->mRefCount++ == 0) {
1674 sp<EffectModule> effect = getEffectIfEnabled(type);
1675 if (effect != 0) {
1676 desc->mEffect = effect;
1677 effect->setSuspended(true);
1678 effect->setEnabled(false);
1679 }
1680 }
1681 } else {
1682 if (index < 0) {
1683 return;
1684 }
1685 desc = mSuspendedEffects.valueAt(index);
1686 if (desc->mRefCount <= 0) {
1687 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1688 desc->mRefCount = 1;
1689 }
1690 if (--desc->mRefCount == 0) {
1691 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1692 if (desc->mEffect != 0) {
1693 sp<EffectModule> effect = desc->mEffect.promote();
1694 if (effect != 0) {
1695 effect->setSuspended(false);
1696 effect->lock();
1697 EffectHandle *handle = effect->controlHandle_l();
1698 if (handle != NULL && !handle->destroyed_l()) {
1699 effect->setEnabled_l(handle->enabled());
1700 }
1701 effect->unlock();
1702 }
1703 desc->mEffect.clear();
1704 }
1705 mSuspendedEffects.removeItemsAt(index);
1706 }
1707 }
1708}
1709
1710// must be called with ThreadBase::mLock held
1711void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1712{
1713 sp<SuspendedEffectDesc> desc;
1714
1715 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1716 if (suspend) {
1717 if (index >= 0) {
1718 desc = mSuspendedEffects.valueAt(index);
1719 } else {
1720 desc = new SuspendedEffectDesc();
1721 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1722 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1723 }
1724 if (desc->mRefCount++ == 0) {
1725 Vector< sp<EffectModule> > effects;
1726 getSuspendEligibleEffects(effects);
1727 for (size_t i = 0; i < effects.size(); i++) {
1728 setEffectSuspended_l(&effects[i]->desc().type, true);
1729 }
1730 }
1731 } else {
1732 if (index < 0) {
1733 return;
1734 }
1735 desc = mSuspendedEffects.valueAt(index);
1736 if (desc->mRefCount <= 0) {
1737 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1738 desc->mRefCount = 1;
1739 }
1740 if (--desc->mRefCount == 0) {
1741 Vector<const effect_uuid_t *> types;
1742 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1743 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1744 continue;
1745 }
1746 types.add(&mSuspendedEffects.valueAt(i)->mType);
1747 }
1748 for (size_t i = 0; i < types.size(); i++) {
1749 setEffectSuspended_l(types[i], false);
1750 }
1751 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1752 mSuspendedEffects.keyAt(index));
1753 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1754 }
1755 }
1756}
1757
1758
1759// The volume effect is used for automated tests only
1760#ifndef OPENSL_ES_H_
1761static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1762 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1763const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1764#endif //OPENSL_ES_H_
1765
1766bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1767{
1768 // auxiliary effects and visualizer are never suspended on output mix
1769 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1770 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1771 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1772 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1773 return false;
1774 }
1775 return true;
1776}
1777
1778void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1779 Vector< sp<AudioFlinger::EffectModule> > &effects)
1780{
1781 effects.clear();
1782 for (size_t i = 0; i < mEffects.size(); i++) {
1783 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1784 effects.add(mEffects[i]);
1785 }
1786 }
1787}
1788
1789sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1790 const effect_uuid_t *type)
1791{
1792 sp<EffectModule> effect = getEffectFromType_l(type);
1793 return effect != 0 && effect->isEnabled() ? effect : 0;
1794}
1795
1796void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1797 bool enabled)
1798{
1799 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1800 if (enabled) {
1801 if (index < 0) {
1802 // if the effect is not suspend check if all effects are suspended
1803 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1804 if (index < 0) {
1805 return;
1806 }
1807 if (!isEffectEligibleForSuspend(effect->desc())) {
1808 return;
1809 }
1810 setEffectSuspended_l(&effect->desc().type, enabled);
1811 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1812 if (index < 0) {
1813 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1814 return;
1815 }
1816 }
1817 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1818 effect->desc().type.timeLow);
1819 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1820 // if effect is requested to suspended but was not yet enabled, supend it now.
1821 if (desc->mEffect == 0) {
1822 desc->mEffect = effect;
1823 effect->setEnabled(false);
1824 effect->setSuspended(true);
1825 }
1826 } else {
1827 if (index < 0) {
1828 return;
1829 }
1830 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1831 effect->desc().type.timeLow);
1832 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1833 desc->mEffect.clear();
1834 effect->setSuspended(false);
1835 }
1836}
1837
Eric Laurent5baf2af2013-09-12 17:37:00 -07001838bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001839{
1840 Mutex::Autolock _l(mLock);
1841 size_t size = mEffects.size();
1842 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001843 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001844 return true;
1845 }
1846 }
1847 return false;
1848}
1849
Eric Laurentca7cc822012-11-19 14:55:58 -08001850}; // namespace android