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