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