blob: 15f1f23e39ea061023d52e398d246fec1917e144 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080023#include <utils/Log.h>
24#include <audio_effects/effect_visualizer.h>
25#include <audio_utils/primitives.h>
26#include <private/media/AudioEffectShared.h>
27#include <media/EffectsFactoryApi.h>
28
29#include "AudioFlinger.h"
30#include "ServiceUtilities.h"
31
32// ----------------------------------------------------------------------------
33
34// Note: the following macro is used for extremely verbose logging message. In
35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
36// 0; but one side effect of this is to turn all LOGV's as well. Some messages
37// are so verbose that we want to suppress them even when we have ALOG_ASSERT
38// turned on. Do not uncomment the #def below unless you really know what you
39// are doing and want to see all of the extremely verbose messages.
40//#define VERY_VERY_VERBOSE_LOGGING
41#ifdef VERY_VERY_VERBOSE_LOGGING
42#define ALOGVV ALOGV
43#else
44#define ALOGVV(a...) do { } while(0)
45#endif
46
Ricardo Garcia726b6a72014-08-11 12:04:54 -070047#define min(a, b) ((a) < (b) ? (a) : (b))
48
Eric Laurentca7cc822012-11-19 14:55:58 -080049namespace android {
50
51// ----------------------------------------------------------------------------
52// EffectModule implementation
53// ----------------------------------------------------------------------------
54
55#undef LOG_TAG
56#define LOG_TAG "AudioFlinger::EffectModule"
57
58AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
59 const wp<AudioFlinger::EffectChain>& chain,
60 effect_descriptor_t *desc,
61 int id,
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
Eric Laurentaaa44472014-09-12 17:41:50 -070071 mSuspended(false),
72 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080073{
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;
91Error:
92 EffectRelease(mEffectInterface);
93 mEffectInterface = NULL;
94 ALOGV("Constructor Error %d", mStatus);
95}
96
97AudioFlinger::EffectModule::~EffectModule()
98{
99 ALOGV("Destructor %p", this);
100 if (mEffectInterface != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800101 remove_effect_from_hal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800102 // release effect engine
103 EffectRelease(mEffectInterface);
104 }
105}
106
107status_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
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700122 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800123 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700124 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800125 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
146size_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
181AudioFlinger::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
194size_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 {
Eric Laurentaaa44472014-09-12 17:41:50 -0700201 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 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800214 }
215 }
216 return mHandles.size();
217}
218
219void 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 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700234 if (start_l() == NO_ERROR) {
235 mState = ACTIVE;
236 } else {
237 mState = IDLE;
238 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800239 break;
240 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700241 if (stop_l() == NO_ERROR) {
242 mDisableWaitCnt = mMaxDisableWaitCnt;
243 } else {
244 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
245 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800246 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
261void 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
310void AudioFlinger::EffectModule::reset_l()
311{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700312 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800313 return;
314 }
315 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
316}
317
318status_t AudioFlinger::EffectModule::configure()
319{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700320 status_t status;
321 sp<ThreadBase> thread;
322 uint32_t size;
323 audio_channel_mask_t channelMask;
324
Eric Laurentca7cc822012-11-19 14:55:58 -0800325 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700326 status = NO_INIT;
327 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800328 }
329
Eric Laurentd0ebb532013-04-02 16:41:41 -0700330 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800331 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700332 status = DEAD_OBJECT;
333 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800334 }
335
336 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700337 channelMask = thread->channelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800338
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;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700379 size = sizeof(int);
380 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800381 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
Eric Laurentd0ebb532013-04-02 16:41:41 -0700418exit:
419 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800420 return status;
421}
422
423status_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
443status_t AudioFlinger::EffectModule::start()
444{
445 Mutex::Autolock _l(mLock);
446 return start_l();
447}
448
449status_t AudioFlinger::EffectModule::start_l()
450{
451 if (mEffectInterface == NULL) {
452 return NO_INIT;
453 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700454 if (mStatus != NO_ERROR) {
455 return mStatus;
456 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800457 status_t cmdStatus;
458 uint32_t size = sizeof(status_t);
459 status_t status = (*mEffectInterface)->command(mEffectInterface,
460 EFFECT_CMD_ENABLE,
461 0,
462 NULL,
463 &size,
464 &cmdStatus);
465 if (status == 0) {
466 status = cmdStatus;
467 }
468 if (status == 0 &&
469 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
470 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
471 sp<ThreadBase> thread = mThread.promote();
472 if (thread != 0) {
473 audio_stream_t *stream = thread->stream();
474 if (stream != NULL) {
475 stream->add_audio_effect(stream, mEffectInterface);
476 }
477 }
478 }
479 return status;
480}
481
482status_t AudioFlinger::EffectModule::stop()
483{
484 Mutex::Autolock _l(mLock);
485 return stop_l();
486}
487
488status_t AudioFlinger::EffectModule::stop_l()
489{
490 if (mEffectInterface == NULL) {
491 return NO_INIT;
492 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700493 if (mStatus != NO_ERROR) {
494 return mStatus;
495 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800496 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800497 uint32_t size = sizeof(status_t);
498 status_t status = (*mEffectInterface)->command(mEffectInterface,
499 EFFECT_CMD_DISABLE,
500 0,
501 NULL,
502 &size,
503 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800504 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800505 status = cmdStatus;
506 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800507 if (status == NO_ERROR) {
508 status = remove_effect_from_hal_l();
509 }
510 return status;
511}
512
513status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
514{
515 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
516 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800517 sp<ThreadBase> thread = mThread.promote();
518 if (thread != 0) {
519 audio_stream_t *stream = thread->stream();
520 if (stream != NULL) {
521 stream->remove_audio_effect(stream, mEffectInterface);
522 }
523 }
524 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800525 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800526}
527
528status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
529 uint32_t cmdSize,
530 void *pCmdData,
531 uint32_t *replySize,
532 void *pReplyData)
533{
534 Mutex::Autolock _l(mLock);
535 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
536
537 if (mState == DESTROYED || mEffectInterface == NULL) {
538 return NO_INIT;
539 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700540 if (mStatus != NO_ERROR) {
541 return mStatus;
542 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800543 status_t status = (*mEffectInterface)->command(mEffectInterface,
544 cmdCode,
545 cmdSize,
546 pCmdData,
547 replySize,
548 pReplyData);
549 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
550 uint32_t size = (replySize == NULL) ? 0 : *replySize;
551 for (size_t i = 1; i < mHandles.size(); i++) {
552 EffectHandle *h = mHandles[i];
553 if (h != NULL && !h->destroyed_l()) {
554 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
555 }
556 }
557 }
558 return status;
559}
560
561status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
562{
563 Mutex::Autolock _l(mLock);
564 return setEnabled_l(enabled);
565}
566
567// must be called with EffectModule::mLock held
568status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
569{
570
571 ALOGV("setEnabled %p enabled %d", this, enabled);
572
573 if (enabled != isEnabled()) {
574 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
575 if (enabled && status != NO_ERROR) {
576 return status;
577 }
578
579 switch (mState) {
580 // going from disabled to enabled
581 case IDLE:
582 mState = STARTING;
583 break;
584 case STOPPED:
585 mState = RESTART;
586 break;
587 case STOPPING:
588 mState = ACTIVE;
589 break;
590
591 // going from enabled to disabled
592 case RESTART:
593 mState = STOPPED;
594 break;
595 case STARTING:
596 mState = IDLE;
597 break;
598 case ACTIVE:
599 mState = STOPPING;
600 break;
601 case DESTROYED:
602 return NO_ERROR; // simply ignore as we are being destroyed
603 }
604 for (size_t i = 1; i < mHandles.size(); i++) {
605 EffectHandle *h = mHandles[i];
606 if (h != NULL && !h->destroyed_l()) {
607 h->setEnabled(enabled);
608 }
609 }
610 }
611 return NO_ERROR;
612}
613
614bool AudioFlinger::EffectModule::isEnabled() const
615{
616 switch (mState) {
617 case RESTART:
618 case STARTING:
619 case ACTIVE:
620 return true;
621 case IDLE:
622 case STOPPING:
623 case STOPPED:
624 case DESTROYED:
625 default:
626 return false;
627 }
628}
629
630bool AudioFlinger::EffectModule::isProcessEnabled() const
631{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700632 if (mStatus != NO_ERROR) {
633 return false;
634 }
635
Eric Laurentca7cc822012-11-19 14:55:58 -0800636 switch (mState) {
637 case RESTART:
638 case ACTIVE:
639 case STOPPING:
640 case STOPPED:
641 return true;
642 case IDLE:
643 case STARTING:
644 case DESTROYED:
645 default:
646 return false;
647 }
648}
649
650status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
651{
652 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700653 if (mStatus != NO_ERROR) {
654 return mStatus;
655 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800656 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800657 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
658 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
659 if (isProcessEnabled() &&
660 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
661 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
662 status_t cmdStatus;
663 uint32_t volume[2];
664 uint32_t *pVolume = NULL;
665 uint32_t size = sizeof(volume);
666 volume[0] = *left;
667 volume[1] = *right;
668 if (controller) {
669 pVolume = volume;
670 }
671 status = (*mEffectInterface)->command(mEffectInterface,
672 EFFECT_CMD_SET_VOLUME,
673 size,
674 volume,
675 &size,
676 pVolume);
677 if (controller && status == NO_ERROR && size == sizeof(volume)) {
678 *left = volume[0];
679 *right = volume[1];
680 }
681 }
682 return status;
683}
684
685status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
686{
687 if (device == AUDIO_DEVICE_NONE) {
688 return NO_ERROR;
689 }
690
691 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700692 if (mStatus != NO_ERROR) {
693 return mStatus;
694 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800695 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700696 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800697 status_t cmdStatus;
698 uint32_t size = sizeof(status_t);
699 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
700 EFFECT_CMD_SET_INPUT_DEVICE;
701 status = (*mEffectInterface)->command(mEffectInterface,
702 cmd,
703 sizeof(uint32_t),
704 &device,
705 &size,
706 &cmdStatus);
707 }
708 return status;
709}
710
711status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
712{
713 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700714 if (mStatus != NO_ERROR) {
715 return mStatus;
716 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800717 status_t status = NO_ERROR;
718 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
719 status_t cmdStatus;
720 uint32_t size = sizeof(status_t);
721 status = (*mEffectInterface)->command(mEffectInterface,
722 EFFECT_CMD_SET_AUDIO_MODE,
723 sizeof(audio_mode_t),
724 &mode,
725 &size,
726 &cmdStatus);
727 if (status == NO_ERROR) {
728 status = cmdStatus;
729 }
730 }
731 return status;
732}
733
734status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
735{
736 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700737 if (mStatus != NO_ERROR) {
738 return mStatus;
739 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800740 status_t status = NO_ERROR;
741 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
742 uint32_t size = 0;
743 status = (*mEffectInterface)->command(mEffectInterface,
744 EFFECT_CMD_SET_AUDIO_SOURCE,
745 sizeof(audio_source_t),
746 &source,
747 &size,
748 NULL);
749 }
750 return status;
751}
752
753void AudioFlinger::EffectModule::setSuspended(bool suspended)
754{
755 Mutex::Autolock _l(mLock);
756 mSuspended = suspended;
757}
758
759bool AudioFlinger::EffectModule::suspended() const
760{
761 Mutex::Autolock _l(mLock);
762 return mSuspended;
763}
764
765bool AudioFlinger::EffectModule::purgeHandles()
766{
767 bool enabled = false;
768 Mutex::Autolock _l(mLock);
769 for (size_t i = 0; i < mHandles.size(); i++) {
770 EffectHandle *handle = mHandles[i];
771 if (handle != NULL && !handle->destroyed_l()) {
772 handle->effect().clear();
773 if (handle->hasControl()) {
774 enabled = handle->enabled();
775 }
776 }
777 }
778 return enabled;
779}
780
Eric Laurent5baf2af2013-09-12 17:37:00 -0700781status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
782{
783 Mutex::Autolock _l(mLock);
784 if (mStatus != NO_ERROR) {
785 return mStatus;
786 }
787 status_t status = NO_ERROR;
788 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
789 status_t cmdStatus;
790 uint32_t size = sizeof(status_t);
791 effect_offload_param_t cmd;
792
793 cmd.isOffload = offloaded;
794 cmd.ioHandle = io;
795 status = (*mEffectInterface)->command(mEffectInterface,
796 EFFECT_CMD_OFFLOAD,
797 sizeof(effect_offload_param_t),
798 &cmd,
799 &size,
800 &cmdStatus);
801 if (status == NO_ERROR) {
802 status = cmdStatus;
803 }
804 mOffloaded = (status == NO_ERROR) ? offloaded : false;
805 } else {
806 if (offloaded) {
807 status = INVALID_OPERATION;
808 }
809 mOffloaded = false;
810 }
811 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
812 return status;
813}
814
815bool AudioFlinger::EffectModule::isOffloaded() const
816{
817 Mutex::Autolock _l(mLock);
818 return mOffloaded;
819}
820
Marco Nelissenb2208842014-02-07 14:00:50 -0800821String8 effectFlagsToString(uint32_t flags) {
822 String8 s;
823
824 s.append("conn. mode: ");
825 switch (flags & EFFECT_FLAG_TYPE_MASK) {
826 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
827 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
828 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
829 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
830 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
831 default: s.append("unknown/reserved"); break;
832 }
833 s.append(", ");
834
835 s.append("insert pref: ");
836 switch (flags & EFFECT_FLAG_INSERT_MASK) {
837 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
838 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
839 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
840 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
841 default: s.append("unknown/reserved"); break;
842 }
843 s.append(", ");
844
845 s.append("volume mgmt: ");
846 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
847 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
848 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
849 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
850 default: s.append("unknown/reserved"); break;
851 }
852 s.append(", ");
853
854 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
855 if (devind) {
856 s.append("device indication: ");
857 switch (devind) {
858 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
859 default: s.append("unknown/reserved"); break;
860 }
861 s.append(", ");
862 }
863
864 s.append("input mode: ");
865 switch (flags & EFFECT_FLAG_INPUT_MASK) {
866 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
867 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
868 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
869 default: s.append("not set"); break;
870 }
871 s.append(", ");
872
873 s.append("output mode: ");
874 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
875 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
876 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
877 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
878 default: s.append("not set"); break;
879 }
880 s.append(", ");
881
882 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
883 if (accel) {
884 s.append("hardware acceleration: ");
885 switch (accel) {
886 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
887 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
888 default: s.append("unknown/reserved"); break;
889 }
890 s.append(", ");
891 }
892
893 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
894 if (modeind) {
895 s.append("mode indication: ");
896 switch (modeind) {
897 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
898 default: s.append("unknown/reserved"); break;
899 }
900 s.append(", ");
901 }
902
903 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
904 if (srcind) {
905 s.append("source indication: ");
906 switch (srcind) {
907 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
908 default: s.append("unknown/reserved"); break;
909 }
910 s.append(", ");
911 }
912
913 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
914 s.append("offloadable, ");
915 }
916
917 int len = s.length();
918 if (s.length() > 2) {
919 char *str = s.lockBuffer(len);
920 s.unlockBuffer(len - 2);
921 }
922 return s;
923}
924
925
Glenn Kasten0f11b512014-01-31 16:18:54 -0800926void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -0800927{
928 const size_t SIZE = 256;
929 char buffer[SIZE];
930 String8 result;
931
932 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
933 result.append(buffer);
934
935 bool locked = AudioFlinger::dumpTryLock(mLock);
936 // failed to lock - AudioFlinger is probably deadlocked
937 if (!locked) {
938 result.append("\t\tCould not lock Fx mutex:\n");
939 }
940
941 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000942 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
943 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -0800944 result.append(buffer);
945
946 result.append("\t\tDescriptor:\n");
947 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
948 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
949 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
950 mDescriptor.uuid.node[2],
951 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
952 result.append(buffer);
953 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
954 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
955 mDescriptor.type.timeHiAndVersion,
956 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
957 mDescriptor.type.node[2],
958 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
959 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -0800960 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -0800961 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -0800962 mDescriptor.flags,
963 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -0800964 result.append(buffer);
965 snprintf(buffer, SIZE, "\t\t- name: %s\n",
966 mDescriptor.name);
967 result.append(buffer);
968 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
969 mDescriptor.implementor);
970 result.append(buffer);
971
972 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000973 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000974 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -0800975 mConfig.inputCfg.buffer.frameCount,
976 mConfig.inputCfg.samplingRate,
977 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -0800978 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000979 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000980 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -0800981 result.append(buffer);
982
983 result.append("\t\t- Output configuration:\n");
984 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000985 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000986 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -0800987 mConfig.outputCfg.buffer.frameCount,
988 mConfig.outputCfg.samplingRate,
989 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -0800990 mConfig.outputCfg.format,
991 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -0800992 result.append(buffer);
993
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000994 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -0800995 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -0800996 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -0800997 for (size_t i = 0; i < mHandles.size(); ++i) {
998 EffectHandle *handle = mHandles[i];
999 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001000 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001001 result.append(buffer);
1002 }
1003 }
1004
Eric Laurentca7cc822012-11-19 14:55:58 -08001005 write(fd, result.string(), result.length());
1006
1007 if (locked) {
1008 mLock.unlock();
1009 }
1010}
1011
1012// ----------------------------------------------------------------------------
1013// EffectHandle implementation
1014// ----------------------------------------------------------------------------
1015
1016#undef LOG_TAG
1017#define LOG_TAG "AudioFlinger::EffectHandle"
1018
1019AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1020 const sp<AudioFlinger::Client>& client,
1021 const sp<IEffectClient>& effectClient,
1022 int32_t priority)
1023 : BnEffect(),
1024 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1025 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1026{
1027 ALOGV("constructor %p", this);
1028
1029 if (client == 0) {
1030 return;
1031 }
1032 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1033 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001034 if (mCblkMemory == 0 ||
1035 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE +
1037 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001038 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001039 return;
1040 }
Glenn Kastene75da402013-11-20 13:54:52 -08001041 new(mCblk) effect_param_cblk_t();
1042 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001043}
1044
1045AudioFlinger::EffectHandle::~EffectHandle()
1046{
1047 ALOGV("Destructor %p", this);
1048
1049 if (mEffect == 0) {
1050 mDestroyed = true;
1051 return;
1052 }
1053 mEffect->lock();
1054 mDestroyed = true;
1055 mEffect->unlock();
1056 disconnect(false);
1057}
1058
Glenn Kastene75da402013-11-20 13:54:52 -08001059status_t AudioFlinger::EffectHandle::initCheck()
1060{
1061 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1062}
1063
Eric Laurentca7cc822012-11-19 14:55:58 -08001064status_t AudioFlinger::EffectHandle::enable()
1065{
1066 ALOGV("enable %p", this);
1067 if (!mHasControl) {
1068 return INVALID_OPERATION;
1069 }
1070 if (mEffect == 0) {
1071 return DEAD_OBJECT;
1072 }
1073
1074 if (mEnabled) {
1075 return NO_ERROR;
1076 }
1077
1078 mEnabled = true;
1079
1080 sp<ThreadBase> thread = mEffect->thread().promote();
1081 if (thread != 0) {
1082 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1083 }
1084
1085 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1086 if (mEffect->suspended()) {
1087 return NO_ERROR;
1088 }
1089
1090 status_t status = mEffect->setEnabled(true);
1091 if (status != NO_ERROR) {
1092 if (thread != 0) {
1093 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1094 }
1095 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001096 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001097 if (thread != 0) {
1098 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001099 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001100 Mutex::Autolock _l(t->mLock);
1101 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001102 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001103 if (!mEffect->isOffloadable()) {
1104 if (thread->type() == ThreadBase::OFFLOAD) {
1105 PlaybackThread *t = (PlaybackThread *)thread.get();
1106 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1107 }
1108 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1109 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1110 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001111 }
1112 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001113 }
1114 return status;
1115}
1116
1117status_t AudioFlinger::EffectHandle::disable()
1118{
1119 ALOGV("disable %p", this);
1120 if (!mHasControl) {
1121 return INVALID_OPERATION;
1122 }
1123 if (mEffect == 0) {
1124 return DEAD_OBJECT;
1125 }
1126
1127 if (!mEnabled) {
1128 return NO_ERROR;
1129 }
1130 mEnabled = false;
1131
1132 if (mEffect->suspended()) {
1133 return NO_ERROR;
1134 }
1135
1136 status_t status = mEffect->setEnabled(false);
1137
1138 sp<ThreadBase> thread = mEffect->thread().promote();
1139 if (thread != 0) {
1140 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001141 if (thread->type() == ThreadBase::OFFLOAD) {
1142 PlaybackThread *t = (PlaybackThread *)thread.get();
1143 Mutex::Autolock _l(t->mLock);
1144 t->broadcast_l();
1145 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001146 }
1147
1148 return status;
1149}
1150
1151void AudioFlinger::EffectHandle::disconnect()
1152{
1153 disconnect(true);
1154}
1155
1156void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1157{
1158 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1159 if (mEffect == 0) {
1160 return;
1161 }
1162 // restore suspended effects if the disconnected handle was enabled and the last one.
1163 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1164 sp<ThreadBase> thread = mEffect->thread().promote();
1165 if (thread != 0) {
1166 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1167 }
1168 }
1169
1170 // release sp on module => module destructor can be called now
1171 mEffect.clear();
1172 if (mClient != 0) {
1173 if (mCblk != NULL) {
1174 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1175 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1176 }
1177 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001178 // Client destructor must run with AudioFlinger client mutex locked
1179 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001180 mClient.clear();
1181 }
1182}
1183
1184status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1185 uint32_t cmdSize,
1186 void *pCmdData,
1187 uint32_t *replySize,
1188 void *pReplyData)
1189{
1190 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1191 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1192
1193 // only get parameter command is permitted for applications not controlling the effect
1194 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1195 return INVALID_OPERATION;
1196 }
1197 if (mEffect == 0) {
1198 return DEAD_OBJECT;
1199 }
1200 if (mClient == 0) {
1201 return INVALID_OPERATION;
1202 }
1203
1204 // handle commands that are not forwarded transparently to effect engine
1205 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1206 // No need to trylock() here as this function is executed in the binder thread serving a
1207 // particular client process: no risk to block the whole media server process or mixer
1208 // threads if we are stuck here
1209 Mutex::Autolock _l(mCblk->lock);
1210 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1211 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1212 mCblk->serverIndex = 0;
1213 mCblk->clientIndex = 0;
1214 return BAD_VALUE;
1215 }
1216 status_t status = NO_ERROR;
1217 while (mCblk->serverIndex < mCblk->clientIndex) {
1218 int reply;
1219 uint32_t rsize = sizeof(int);
1220 int *p = (int *)(mBuffer + mCblk->serverIndex);
1221 int size = *p++;
1222 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1223 ALOGW("command(): invalid parameter block size");
1224 break;
1225 }
1226 effect_param_t *param = (effect_param_t *)p;
1227 if (param->psize == 0 || param->vsize == 0) {
1228 ALOGW("command(): null parameter or value size");
1229 mCblk->serverIndex += size;
1230 continue;
1231 }
1232 uint32_t psize = sizeof(effect_param_t) +
1233 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1234 param->vsize;
1235 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1236 psize,
1237 p,
1238 &rsize,
1239 &reply);
1240 // stop at first error encountered
1241 if (ret != NO_ERROR) {
1242 status = ret;
1243 *(int *)pReplyData = reply;
1244 break;
1245 } else if (reply != NO_ERROR) {
1246 *(int *)pReplyData = reply;
1247 break;
1248 }
1249 mCblk->serverIndex += size;
1250 }
1251 mCblk->serverIndex = 0;
1252 mCblk->clientIndex = 0;
1253 return status;
1254 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1255 *(int *)pReplyData = NO_ERROR;
1256 return enable();
1257 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1258 *(int *)pReplyData = NO_ERROR;
1259 return disable();
1260 }
1261
1262 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1263}
1264
1265void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1266{
1267 ALOGV("setControl %p control %d", this, hasControl);
1268
1269 mHasControl = hasControl;
1270 mEnabled = enabled;
1271
1272 if (signal && mEffectClient != 0) {
1273 mEffectClient->controlStatusChanged(hasControl);
1274 }
1275}
1276
1277void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1278 uint32_t cmdSize,
1279 void *pCmdData,
1280 uint32_t replySize,
1281 void *pReplyData)
1282{
1283 if (mEffectClient != 0) {
1284 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1285 }
1286}
1287
1288
1289
1290void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1291{
1292 if (mEffectClient != 0) {
1293 mEffectClient->enableStatusChanged(enabled);
1294 }
1295}
1296
1297status_t AudioFlinger::EffectHandle::onTransact(
1298 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1299{
1300 return BnEffect::onTransact(code, data, reply, flags);
1301}
1302
1303
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001304void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001305{
1306 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1307
Marco Nelissenb2208842014-02-07 14:00:50 -08001308 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001309 (mClient == 0) ? getpid_cached : mClient->pid(),
1310 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001311 mHasControl ? "yes" : "no",
1312 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001313 mCblk ? mCblk->clientIndex : 0,
1314 mCblk ? mCblk->serverIndex : 0
1315 );
1316
1317 if (locked) {
1318 mCblk->lock.unlock();
1319 }
1320}
1321
1322#undef LOG_TAG
1323#define LOG_TAG "AudioFlinger::EffectChain"
1324
1325AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1326 int sessionId)
1327 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1328 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
1329 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
1330{
1331 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1332 if (thread == NULL) {
1333 return;
1334 }
1335 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1336 thread->frameCount();
1337}
1338
1339AudioFlinger::EffectChain::~EffectChain()
1340{
1341 if (mOwnInBuffer) {
1342 delete mInBuffer;
1343 }
1344
1345}
1346
1347// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1348sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1349 effect_descriptor_t *descriptor)
1350{
1351 size_t size = mEffects.size();
1352
1353 for (size_t i = 0; i < size; i++) {
1354 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1355 return mEffects[i];
1356 }
1357 }
1358 return 0;
1359}
1360
1361// getEffectFromId_l() must be called with ThreadBase::mLock held
1362sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1363{
1364 size_t size = mEffects.size();
1365
1366 for (size_t i = 0; i < size; i++) {
1367 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1368 if (id == 0 || mEffects[i]->id() == id) {
1369 return mEffects[i];
1370 }
1371 }
1372 return 0;
1373}
1374
1375// getEffectFromType_l() must be called with ThreadBase::mLock held
1376sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1377 const effect_uuid_t *type)
1378{
1379 size_t size = mEffects.size();
1380
1381 for (size_t i = 0; i < size; i++) {
1382 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1383 return mEffects[i];
1384 }
1385 }
1386 return 0;
1387}
1388
1389void AudioFlinger::EffectChain::clearInputBuffer()
1390{
1391 Mutex::Autolock _l(mLock);
1392 sp<ThreadBase> thread = mThread.promote();
1393 if (thread == 0) {
1394 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1395 return;
1396 }
1397 clearInputBuffer_l(thread);
1398}
1399
1400// Must be called with EffectChain::mLock locked
1401void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1402{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001403 // TODO: This will change in the future, depending on multichannel
1404 // and sample format changes for effects.
1405 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1406 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001407 const size_t frameSize =
1408 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001409 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001410}
1411
1412// Must be called with EffectChain::mLock locked
1413void AudioFlinger::EffectChain::process_l()
1414{
1415 sp<ThreadBase> thread = mThread.promote();
1416 if (thread == 0) {
1417 ALOGW("process_l(): cannot promote mixer thread");
1418 return;
1419 }
1420 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1421 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001422 // never process effects when:
1423 // - on an OFFLOAD thread
1424 // - no more tracks are on the session and the effect tail has been rendered
1425 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001426 if (!isGlobalSession) {
1427 bool tracksOnSession = (trackCnt() != 0);
1428
1429 if (!tracksOnSession && mTailBufferCount == 0) {
1430 doProcess = false;
1431 }
1432
1433 if (activeTrackCnt() == 0) {
1434 // if no track is active and the effect tail has not been rendered,
1435 // the input buffer must be cleared here as the mixer process will not do it
1436 if (tracksOnSession || mTailBufferCount > 0) {
1437 clearInputBuffer_l(thread);
1438 if (mTailBufferCount > 0) {
1439 mTailBufferCount--;
1440 }
1441 }
1442 }
1443 }
1444
1445 size_t size = mEffects.size();
1446 if (doProcess) {
1447 for (size_t i = 0; i < size; i++) {
1448 mEffects[i]->process();
1449 }
1450 }
1451 for (size_t i = 0; i < size; i++) {
1452 mEffects[i]->updateState();
1453 }
1454}
1455
1456// addEffect_l() must be called with PlaybackThread::mLock held
1457status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1458{
1459 effect_descriptor_t desc = effect->desc();
1460 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1461
1462 Mutex::Autolock _l(mLock);
1463 effect->setChain(this);
1464 sp<ThreadBase> thread = mThread.promote();
1465 if (thread == 0) {
1466 return NO_INIT;
1467 }
1468 effect->setThread(thread);
1469
1470 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1471 // Auxiliary effects are inserted at the beginning of mEffects vector as
1472 // they are processed first and accumulated in chain input buffer
1473 mEffects.insertAt(effect, 0);
1474
1475 // the input buffer for auxiliary effect contains mono samples in
1476 // 32 bit format. This is to avoid saturation in AudoMixer
1477 // accumulation stage. Saturation is done in EffectModule::process() before
1478 // calling the process in effect engine
1479 size_t numSamples = thread->frameCount();
1480 int32_t *buffer = new int32_t[numSamples];
1481 memset(buffer, 0, numSamples * sizeof(int32_t));
1482 effect->setInBuffer((int16_t *)buffer);
1483 // auxiliary effects output samples to chain input buffer for further processing
1484 // by insert effects
1485 effect->setOutBuffer(mInBuffer);
1486 } else {
1487 // Insert effects are inserted at the end of mEffects vector as they are processed
1488 // after track and auxiliary effects.
1489 // Insert effect order as a function of indicated preference:
1490 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1491 // another effect is present
1492 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1493 // last effect claiming first position
1494 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1495 // first effect claiming last position
1496 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1497 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1498 // already present
1499
1500 size_t size = mEffects.size();
1501 size_t idx_insert = size;
1502 ssize_t idx_insert_first = -1;
1503 ssize_t idx_insert_last = -1;
1504
1505 for (size_t i = 0; i < size; i++) {
1506 effect_descriptor_t d = mEffects[i]->desc();
1507 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1508 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1509 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1510 // check invalid effect chaining combinations
1511 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1512 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1513 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1514 desc.name, d.name);
1515 return INVALID_OPERATION;
1516 }
1517 // remember position of first insert effect and by default
1518 // select this as insert position for new effect
1519 if (idx_insert == size) {
1520 idx_insert = i;
1521 }
1522 // remember position of last insert effect claiming
1523 // first position
1524 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1525 idx_insert_first = i;
1526 }
1527 // remember position of first insert effect claiming
1528 // last position
1529 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1530 idx_insert_last == -1) {
1531 idx_insert_last = i;
1532 }
1533 }
1534 }
1535
1536 // modify idx_insert from first position if needed
1537 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1538 if (idx_insert_last != -1) {
1539 idx_insert = idx_insert_last;
1540 } else {
1541 idx_insert = size;
1542 }
1543 } else {
1544 if (idx_insert_first != -1) {
1545 idx_insert = idx_insert_first + 1;
1546 }
1547 }
1548
1549 // always read samples from chain input buffer
1550 effect->setInBuffer(mInBuffer);
1551
1552 // if last effect in the chain, output samples to chain
1553 // output buffer, otherwise to chain input buffer
1554 if (idx_insert == size) {
1555 if (idx_insert != 0) {
1556 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1557 mEffects[idx_insert-1]->configure();
1558 }
1559 effect->setOutBuffer(mOutBuffer);
1560 } else {
1561 effect->setOutBuffer(mInBuffer);
1562 }
1563 mEffects.insertAt(effect, idx_insert);
1564
1565 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1566 idx_insert);
1567 }
1568 effect->configure();
1569 return NO_ERROR;
1570}
1571
1572// removeEffect_l() must be called with PlaybackThread::mLock held
1573size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1574{
1575 Mutex::Autolock _l(mLock);
1576 size_t size = mEffects.size();
1577 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1578
1579 for (size_t i = 0; i < size; i++) {
1580 if (effect == mEffects[i]) {
1581 // calling stop here will remove pre-processing effect from the audio HAL.
1582 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1583 // the middle of a read from audio HAL
1584 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1585 mEffects[i]->state() == EffectModule::STOPPING) {
1586 mEffects[i]->stop();
1587 }
1588 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1589 delete[] effect->inBuffer();
1590 } else {
1591 if (i == size - 1 && i != 0) {
1592 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1593 mEffects[i - 1]->configure();
1594 }
1595 }
1596 mEffects.removeAt(i);
1597 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1598 this, i);
1599 break;
1600 }
1601 }
1602
1603 return mEffects.size();
1604}
1605
1606// setDevice_l() must be called with PlaybackThread::mLock held
1607void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1608{
1609 size_t size = mEffects.size();
1610 for (size_t i = 0; i < size; i++) {
1611 mEffects[i]->setDevice(device);
1612 }
1613}
1614
1615// setMode_l() must be called with PlaybackThread::mLock held
1616void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1617{
1618 size_t size = mEffects.size();
1619 for (size_t i = 0; i < size; i++) {
1620 mEffects[i]->setMode(mode);
1621 }
1622}
1623
1624// setAudioSource_l() must be called with PlaybackThread::mLock held
1625void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1626{
1627 size_t size = mEffects.size();
1628 for (size_t i = 0; i < size; i++) {
1629 mEffects[i]->setAudioSource(source);
1630 }
1631}
1632
1633// setVolume_l() must be called with PlaybackThread::mLock held
1634bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1635{
1636 uint32_t newLeft = *left;
1637 uint32_t newRight = *right;
1638 bool hasControl = false;
1639 int ctrlIdx = -1;
1640 size_t size = mEffects.size();
1641
1642 // first update volume controller
1643 for (size_t i = size; i > 0; i--) {
1644 if (mEffects[i - 1]->isProcessEnabled() &&
1645 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1646 ctrlIdx = i - 1;
1647 hasControl = true;
1648 break;
1649 }
1650 }
1651
1652 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
1653 if (hasControl) {
1654 *left = mNewLeftVolume;
1655 *right = mNewRightVolume;
1656 }
1657 return hasControl;
1658 }
1659
1660 mVolumeCtrlIdx = ctrlIdx;
1661 mLeftVolume = newLeft;
1662 mRightVolume = newRight;
1663
1664 // second get volume update from volume controller
1665 if (ctrlIdx >= 0) {
1666 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1667 mNewLeftVolume = newLeft;
1668 mNewRightVolume = newRight;
1669 }
1670 // then indicate volume to all other effects in chain.
1671 // Pass altered volume to effects before volume controller
1672 // and requested volume to effects after controller
1673 uint32_t lVol = newLeft;
1674 uint32_t rVol = newRight;
1675
1676 for (size_t i = 0; i < size; i++) {
1677 if ((int)i == ctrlIdx) {
1678 continue;
1679 }
1680 // this also works for ctrlIdx == -1 when there is no volume controller
1681 if ((int)i > ctrlIdx) {
1682 lVol = *left;
1683 rVol = *right;
1684 }
1685 mEffects[i]->setVolume(&lVol, &rVol, false);
1686 }
1687 *left = newLeft;
1688 *right = newRight;
1689
1690 return hasControl;
1691}
1692
1693void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1694{
1695 const size_t SIZE = 256;
1696 char buffer[SIZE];
1697 String8 result;
1698
Marco Nelissenb2208842014-02-07 14:00:50 -08001699 size_t numEffects = mEffects.size();
1700 snprintf(buffer, SIZE, " %d effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001701 result.append(buffer);
1702
Marco Nelissenb2208842014-02-07 14:00:50 -08001703 if (numEffects) {
1704 bool locked = AudioFlinger::dumpTryLock(mLock);
1705 // failed to lock - AudioFlinger is probably deadlocked
1706 if (!locked) {
1707 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001708 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001709
Marco Nelissenb2208842014-02-07 14:00:50 -08001710 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001711 snprintf(buffer, SIZE, "\t%p %p %d\n",
1712 mInBuffer,
1713 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001714 mActiveTrackCnt);
1715 result.append(buffer);
1716 write(fd, result.string(), result.size());
1717
1718 for (size_t i = 0; i < numEffects; ++i) {
1719 sp<EffectModule> effect = mEffects[i];
1720 if (effect != 0) {
1721 effect->dump(fd, args);
1722 }
1723 }
1724
1725 if (locked) {
1726 mLock.unlock();
1727 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001728 }
1729}
1730
1731// must be called with ThreadBase::mLock held
1732void AudioFlinger::EffectChain::setEffectSuspended_l(
1733 const effect_uuid_t *type, bool suspend)
1734{
1735 sp<SuspendedEffectDesc> desc;
1736 // use effect type UUID timelow as key as there is no real risk of identical
1737 // timeLow fields among effect type UUIDs.
1738 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1739 if (suspend) {
1740 if (index >= 0) {
1741 desc = mSuspendedEffects.valueAt(index);
1742 } else {
1743 desc = new SuspendedEffectDesc();
1744 desc->mType = *type;
1745 mSuspendedEffects.add(type->timeLow, desc);
1746 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1747 }
1748 if (desc->mRefCount++ == 0) {
1749 sp<EffectModule> effect = getEffectIfEnabled(type);
1750 if (effect != 0) {
1751 desc->mEffect = effect;
1752 effect->setSuspended(true);
1753 effect->setEnabled(false);
1754 }
1755 }
1756 } else {
1757 if (index < 0) {
1758 return;
1759 }
1760 desc = mSuspendedEffects.valueAt(index);
1761 if (desc->mRefCount <= 0) {
1762 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1763 desc->mRefCount = 1;
1764 }
1765 if (--desc->mRefCount == 0) {
1766 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1767 if (desc->mEffect != 0) {
1768 sp<EffectModule> effect = desc->mEffect.promote();
1769 if (effect != 0) {
1770 effect->setSuspended(false);
1771 effect->lock();
1772 EffectHandle *handle = effect->controlHandle_l();
1773 if (handle != NULL && !handle->destroyed_l()) {
1774 effect->setEnabled_l(handle->enabled());
1775 }
1776 effect->unlock();
1777 }
1778 desc->mEffect.clear();
1779 }
1780 mSuspendedEffects.removeItemsAt(index);
1781 }
1782 }
1783}
1784
1785// must be called with ThreadBase::mLock held
1786void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1787{
1788 sp<SuspendedEffectDesc> desc;
1789
1790 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1791 if (suspend) {
1792 if (index >= 0) {
1793 desc = mSuspendedEffects.valueAt(index);
1794 } else {
1795 desc = new SuspendedEffectDesc();
1796 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1797 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1798 }
1799 if (desc->mRefCount++ == 0) {
1800 Vector< sp<EffectModule> > effects;
1801 getSuspendEligibleEffects(effects);
1802 for (size_t i = 0; i < effects.size(); i++) {
1803 setEffectSuspended_l(&effects[i]->desc().type, true);
1804 }
1805 }
1806 } else {
1807 if (index < 0) {
1808 return;
1809 }
1810 desc = mSuspendedEffects.valueAt(index);
1811 if (desc->mRefCount <= 0) {
1812 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1813 desc->mRefCount = 1;
1814 }
1815 if (--desc->mRefCount == 0) {
1816 Vector<const effect_uuid_t *> types;
1817 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1818 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1819 continue;
1820 }
1821 types.add(&mSuspendedEffects.valueAt(i)->mType);
1822 }
1823 for (size_t i = 0; i < types.size(); i++) {
1824 setEffectSuspended_l(types[i], false);
1825 }
1826 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1827 mSuspendedEffects.keyAt(index));
1828 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1829 }
1830 }
1831}
1832
1833
1834// The volume effect is used for automated tests only
1835#ifndef OPENSL_ES_H_
1836static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1837 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1838const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1839#endif //OPENSL_ES_H_
1840
1841bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1842{
1843 // auxiliary effects and visualizer are never suspended on output mix
1844 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1845 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1846 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1847 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1848 return false;
1849 }
1850 return true;
1851}
1852
1853void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1854 Vector< sp<AudioFlinger::EffectModule> > &effects)
1855{
1856 effects.clear();
1857 for (size_t i = 0; i < mEffects.size(); i++) {
1858 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1859 effects.add(mEffects[i]);
1860 }
1861 }
1862}
1863
1864sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1865 const effect_uuid_t *type)
1866{
1867 sp<EffectModule> effect = getEffectFromType_l(type);
1868 return effect != 0 && effect->isEnabled() ? effect : 0;
1869}
1870
1871void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1872 bool enabled)
1873{
1874 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1875 if (enabled) {
1876 if (index < 0) {
1877 // if the effect is not suspend check if all effects are suspended
1878 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1879 if (index < 0) {
1880 return;
1881 }
1882 if (!isEffectEligibleForSuspend(effect->desc())) {
1883 return;
1884 }
1885 setEffectSuspended_l(&effect->desc().type, enabled);
1886 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1887 if (index < 0) {
1888 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1889 return;
1890 }
1891 }
1892 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1893 effect->desc().type.timeLow);
1894 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1895 // if effect is requested to suspended but was not yet enabled, supend it now.
1896 if (desc->mEffect == 0) {
1897 desc->mEffect = effect;
1898 effect->setEnabled(false);
1899 effect->setSuspended(true);
1900 }
1901 } else {
1902 if (index < 0) {
1903 return;
1904 }
1905 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1906 effect->desc().type.timeLow);
1907 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1908 desc->mEffect.clear();
1909 effect->setSuspended(false);
1910 }
1911}
1912
Eric Laurent5baf2af2013-09-12 17:37:00 -07001913bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001914{
1915 Mutex::Autolock _l(mLock);
1916 size_t size = mEffects.size();
1917 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001918 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001919 return true;
1920 }
1921 }
1922 return false;
1923}
1924
Eric Laurentaaa44472014-09-12 17:41:50 -07001925void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
1926{
1927 Mutex::Autolock _l(mLock);
1928 mThread = thread;
1929 for (size_t i = 0; i < mEffects.size(); i++) {
1930 mEffects[i]->setThread(thread);
1931 }
1932}
1933
Eric Laurentca7cc822012-11-19 14:55:58 -08001934}; // namespace android