blob: f916dfc23eef47b5a31f07414e70f0f48b0b6c6a [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
Alok Kediya601e9b52013-09-27 11:55:10 +05302** Copyright (c) 2013, The Linux Foundation. All rights reserved.
3** Not a Contribution.
Eric Laurentca7cc822012-11-19 14:55:58 -08004** Copyright 2012, The Android Open Source Project
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19
20#define LOG_TAG "AudioFlinger"
21//#define LOG_NDEBUG 0
22
Glenn Kasten153b9fe2013-07-15 11:23:36 -070023#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080024#include <utils/Log.h>
25#include <audio_effects/effect_visualizer.h>
26#include <audio_utils/primitives.h>
27#include <private/media/AudioEffectShared.h>
28#include <media/EffectsFactoryApi.h>
29
30#include "AudioFlinger.h"
31#include "ServiceUtilities.h"
32
Alok Kediya601e9b52013-09-27 11:55:10 +053033#ifdef SRS_PROCESSING
34#include "srs_processing.h"
35#include "postpro_patch_ics.h"
36#endif
37
Eric Laurentca7cc822012-11-19 14:55:58 -080038// ----------------------------------------------------------------------------
39
40// Note: the following macro is used for extremely verbose logging message. In
41// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
42// 0; but one side effect of this is to turn all LOGV's as well. Some messages
43// are so verbose that we want to suppress them even when we have ALOG_ASSERT
44// turned on. Do not uncomment the #def below unless you really know what you
45// are doing and want to see all of the extremely verbose messages.
46//#define VERY_VERY_VERBOSE_LOGGING
47#ifdef VERY_VERY_VERBOSE_LOGGING
48#define ALOGVV ALOGV
49#else
50#define ALOGVV(a...) do { } while(0)
51#endif
52
53namespace android {
54
55// ----------------------------------------------------------------------------
56// EffectModule implementation
57// ----------------------------------------------------------------------------
58
59#undef LOG_TAG
60#define LOG_TAG "AudioFlinger::EffectModule"
61
62AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
63 const wp<AudioFlinger::EffectChain>& chain,
64 effect_descriptor_t *desc,
65 int id,
66 int sessionId)
67 : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX),
68 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69 mDescriptor(*desc),
70 // mConfig is set by configure() and not used before then
71 mEffectInterface(NULL),
72 mStatus(NO_INIT), mState(IDLE),
73 // mMaxDisableWaitCnt is set by configure() and not used before then
74 // mDisableWaitCnt is set by process() and updateState() and not used before then
Alok Kediya601e9b52013-09-27 11:55:10 +053075 mSuspended(false), mIsForLPA(false)
Eric Laurentca7cc822012-11-19 14:55:58 -080076{
77 ALOGV("Constructor %p", this);
78 int lStatus;
79
80 // create effect engine from effect factory
81 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
82
83 if (mStatus != NO_ERROR) {
84 return;
85 }
86 lStatus = init();
87 if (lStatus < 0) {
88 mStatus = lStatus;
89 goto Error;
90 }
91
92 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
93 return;
94Error:
95 EffectRelease(mEffectInterface);
96 mEffectInterface = NULL;
97 ALOGV("Constructor Error %d", mStatus);
98}
99
100AudioFlinger::EffectModule::~EffectModule()
101{
102 ALOGV("Destructor %p", this);
103 if (mEffectInterface != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800104 remove_effect_from_hal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800105 // release effect engine
106 EffectRelease(mEffectInterface);
107 }
108}
109
110status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
111{
112 status_t status;
113
114 Mutex::Autolock _l(mLock);
115 int priority = handle->priority();
116 size_t size = mHandles.size();
117 EffectHandle *controlHandle = NULL;
118 size_t i;
119 for (i = 0; i < size; i++) {
120 EffectHandle *h = mHandles[i];
121 if (h == NULL || h->destroyed_l()) {
122 continue;
123 }
124 // first non destroyed handle is considered in control
125 if (controlHandle == NULL)
126 controlHandle = h;
127 if (h->priority() <= priority) {
128 break;
129 }
130 }
131 // if inserted in first place, move effect control from previous owner to this handle
132 if (i == 0) {
133 bool enabled = false;
134 if (controlHandle != NULL) {
135 enabled = controlHandle->enabled();
136 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
137 }
138 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
139 status = NO_ERROR;
140 } else {
141 status = ALREADY_EXISTS;
142 }
143 ALOGV("addHandle() %p added handle %p in position %d", this, handle, i);
144 mHandles.insertAt(handle, i);
145 return status;
146}
147
148size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
149{
150 Mutex::Autolock _l(mLock);
151 size_t size = mHandles.size();
152 size_t i;
153 for (i = 0; i < size; i++) {
154 if (mHandles[i] == handle) {
155 break;
156 }
157 }
158 if (i == size) {
159 return size;
160 }
161 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle, i);
162
163 mHandles.removeAt(i);
164 // if removed from first place, move effect control from this handle to next in line
165 if (i == 0) {
166 EffectHandle *h = controlHandle_l();
167 if (h != NULL) {
168 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
169 }
170 }
171
172 // Prevent calls to process() and other functions on effect interface from now on.
173 // The effect engine will be released by the destructor when the last strong reference on
174 // this object is released which can happen after next process is called.
175 if (mHandles.size() == 0 && !mPinned) {
176 mState = DESTROYED;
177 }
178
179 return mHandles.size();
180}
181
182// must be called with EffectModule::mLock held
183AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
184{
185 // the first valid handle in the list has control over the module
186 for (size_t i = 0; i < mHandles.size(); i++) {
187 EffectHandle *h = mHandles[i];
188 if (h != NULL && !h->destroyed_l()) {
189 return h;
190 }
191 }
192
193 return NULL;
194}
195
196size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast)
197{
198 ALOGV("disconnect() %p handle %p", this, handle);
199 // keep a strong reference on this EffectModule to avoid calling the
200 // destructor before we exit
201 sp<EffectModule> keep(this);
202 {
203 sp<ThreadBase> thread = mThread.promote();
204 if (thread != 0) {
205 thread->disconnectEffect(keep, handle, unpinIfLast);
206 }
207 }
208 return mHandles.size();
209}
210
211void AudioFlinger::EffectModule::updateState() {
212 Mutex::Autolock _l(mLock);
213
214 switch (mState) {
215 case RESTART:
216 reset_l();
217 // FALL THROUGH
218
219 case STARTING:
220 // clear auxiliary effect input buffer for next accumulation
221 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
222 memset(mConfig.inputCfg.buffer.raw,
223 0,
224 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
225 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700226 if (start_l() == NO_ERROR) {
227 mState = ACTIVE;
228 } else {
229 mState = IDLE;
230 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800231 break;
232 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700233 if (stop_l() == NO_ERROR) {
234 mDisableWaitCnt = mMaxDisableWaitCnt;
235 } else {
236 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
237 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800238 mState = STOPPED;
239 break;
240 case STOPPED:
241 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
242 // turn off sequence.
243 if (--mDisableWaitCnt == 0) {
244 reset_l();
245 mState = IDLE;
246 }
247 break;
248 default: //IDLE , ACTIVE, DESTROYED
249 break;
250 }
251}
252
253void AudioFlinger::EffectModule::process()
254{
255 Mutex::Autolock _l(mLock);
256
257 if (mState == DESTROYED || mEffectInterface == NULL ||
258 mConfig.inputCfg.buffer.raw == NULL ||
259 mConfig.outputCfg.buffer.raw == NULL) {
260 return;
261 }
262
263 if (isProcessEnabled()) {
264 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
265 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
266 ditherAndClamp(mConfig.inputCfg.buffer.s32,
267 mConfig.inputCfg.buffer.s32,
268 mConfig.inputCfg.buffer.frameCount/2);
269 }
270
271 // do the actual processing in the effect engine
272 int ret = (*mEffectInterface)->process(mEffectInterface,
273 &mConfig.inputCfg.buffer,
274 &mConfig.outputCfg.buffer);
275
276 // force transition to IDLE state when engine is ready
277 if (mState == STOPPED && ret == -ENODATA) {
278 mDisableWaitCnt = 1;
279 }
280
281 // clear auxiliary effect input buffer for next accumulation
282 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
283 memset(mConfig.inputCfg.buffer.raw, 0,
284 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
285 }
286 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
287 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
288 // If an insert effect is idle and input buffer is different from output buffer,
289 // accumulate input onto output
290 sp<EffectChain> chain = mChain.promote();
291 if (chain != 0 && chain->activeTrackCnt() != 0) {
292 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
293 int16_t *in = mConfig.inputCfg.buffer.s16;
294 int16_t *out = mConfig.outputCfg.buffer.s16;
295 for (size_t i = 0; i < frameCnt; i++) {
296 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
297 }
298 }
299 }
300}
301
302void AudioFlinger::EffectModule::reset_l()
303{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700304 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800305 return;
306 }
307 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
308}
309
Alok Kediya601e9b52013-09-27 11:55:10 +0530310status_t AudioFlinger::EffectModule::configure(bool isForLPA, int sampleRate, int channelCount, int frameCount)
Eric Laurentca7cc822012-11-19 14:55:58 -0800311{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700312 status_t status;
Alok Kediya601e9b52013-09-27 11:55:10 +0530313 status_t cmdStatus = 0;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700314 sp<ThreadBase> thread;
315 uint32_t size;
316 audio_channel_mask_t channelMask;
Alok Kediya601e9b52013-09-27 11:55:10 +0530317 uint32_t channels;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700318
Eric Laurentca7cc822012-11-19 14:55:58 -0800319 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700320 status = NO_INIT;
321 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800322 }
323
Eric Laurentd0ebb532013-04-02 16:41:41 -0700324 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800325 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700326 status = DEAD_OBJECT;
327 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800328 }
329
330 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700331 channelMask = thread->channelMask();
Alok Kediya601e9b52013-09-27 11:55:10 +0530332 if(popcount(channelMask) > 2) {
333 ALOGE("Error: Trying to apply effect on %d channel content",popcount(channelMask));
334 return INVALID_OPERATION;
335 }
336 mIsForLPA = isForLPA;
337 if(isForLPA) {
338 if (channelCount == 1) {
339 channels = AUDIO_CHANNEL_OUT_MONO;
340 } else {
341 channels = AUDIO_CHANNEL_OUT_STEREO;
342 }
343 } else {
344 if (thread->channelCount() == 1) {
345 channels = AUDIO_CHANNEL_OUT_MONO;
346 } else {
347 channels = AUDIO_CHANNEL_OUT_STEREO;
348 }
349 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800350
351 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
352 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
353 } else {
354 mConfig.inputCfg.channels = channelMask;
355 }
356 mConfig.outputCfg.channels = channelMask;
357 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
358 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
Alok Kediya601e9b52013-09-27 11:55:10 +0530359 if(isForLPA){
360 mConfig.inputCfg.samplingRate = sampleRate;
361 } else {
362 mConfig.inputCfg.samplingRate = thread->sampleRate();
363 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800364 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
365 mConfig.inputCfg.bufferProvider.cookie = NULL;
366 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
367 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
368 mConfig.outputCfg.bufferProvider.cookie = NULL;
369 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
370 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
371 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
372 // Insert effect:
373 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
374 // always overwrites output buffer: input buffer == output buffer
375 // - in other sessions:
376 // last effect in the chain accumulates in output buffer: input buffer != output buffer
377 // other effect: overwrites output buffer: input buffer == output buffer
378 // Auxiliary effect:
379 // accumulates in output buffer: input buffer != output buffer
380 // Therefore: accumulate <=> input buffer != output buffer
381 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
382 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
383 } else {
384 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
385 }
386 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
387 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Alok Kediya601e9b52013-09-27 11:55:10 +0530388 if(isForLPA) {
389 mConfig.inputCfg.buffer.frameCount = frameCount;
390 } else {
391 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
392 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800393 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
394
395 ALOGV("configure() %p thread %p buffer %p framecount %d",
396 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
397
Eric Laurentd0ebb532013-04-02 16:41:41 -0700398 size = sizeof(int);
399 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800400 EFFECT_CMD_SET_CONFIG,
401 sizeof(effect_config_t),
402 &mConfig,
403 &size,
404 &cmdStatus);
405 if (status == 0) {
406 status = cmdStatus;
407 }
408
409 if (status == 0 &&
410 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
411 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
412 effect_param_t *p = (effect_param_t *)buf32;
413
414 p->psize = sizeof(uint32_t);
415 p->vsize = sizeof(uint32_t);
416 size = sizeof(int);
417 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
418
419 uint32_t latency = 0;
420 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
421 if (pbt != NULL) {
422 latency = pbt->latency_l();
423 }
424
425 *((int32_t *)p->data + 1)= latency;
426 (*mEffectInterface)->command(mEffectInterface,
427 EFFECT_CMD_SET_PARAM,
428 sizeof(effect_param_t) + 8,
429 &buf32,
430 &size,
431 &cmdStatus);
432 }
433
434 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
435 (1000 * mConfig.outputCfg.buffer.frameCount);
436
Eric Laurentd0ebb532013-04-02 16:41:41 -0700437exit:
438 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800439 return status;
440}
441
442status_t AudioFlinger::EffectModule::init()
443{
444 Mutex::Autolock _l(mLock);
445 if (mEffectInterface == NULL) {
446 return NO_INIT;
447 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530448 status_t cmdStatus = 0;
Eric Laurentca7cc822012-11-19 14:55:58 -0800449 uint32_t size = sizeof(status_t);
450 status_t status = (*mEffectInterface)->command(mEffectInterface,
451 EFFECT_CMD_INIT,
452 0,
453 NULL,
454 &size,
455 &cmdStatus);
456 if (status == 0) {
457 status = cmdStatus;
458 }
459 return status;
460}
461
462status_t AudioFlinger::EffectModule::start()
463{
464 Mutex::Autolock _l(mLock);
465 return start_l();
466}
467
468status_t AudioFlinger::EffectModule::start_l()
469{
470 if (mEffectInterface == NULL) {
471 return NO_INIT;
472 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700473 if (mStatus != NO_ERROR) {
474 return mStatus;
475 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530476 status_t cmdStatus = 0;
Eric Laurentca7cc822012-11-19 14:55:58 -0800477 uint32_t size = sizeof(status_t);
478 status_t status = (*mEffectInterface)->command(mEffectInterface,
479 EFFECT_CMD_ENABLE,
480 0,
481 NULL,
482 &size,
483 &cmdStatus);
484 if (status == 0) {
485 status = cmdStatus;
486 }
487 if (status == 0 &&
488 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
489 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
490 sp<ThreadBase> thread = mThread.promote();
491 if (thread != 0) {
492 audio_stream_t *stream = thread->stream();
493 if (stream != NULL) {
494 stream->add_audio_effect(stream, mEffectInterface);
495 }
496 }
497 }
498 return status;
499}
500
501status_t AudioFlinger::EffectModule::stop()
502{
503 Mutex::Autolock _l(mLock);
504 return stop_l();
505}
506
507status_t AudioFlinger::EffectModule::stop_l()
508{
509 if (mEffectInterface == NULL) {
510 return NO_INIT;
511 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700512 if (mStatus != NO_ERROR) {
513 return mStatus;
514 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800515 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800516 uint32_t size = sizeof(status_t);
517 status_t status = (*mEffectInterface)->command(mEffectInterface,
518 EFFECT_CMD_DISABLE,
519 0,
520 NULL,
521 &size,
522 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800523 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800524 status = cmdStatus;
525 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800526 if (status == NO_ERROR) {
527 status = remove_effect_from_hal_l();
528 }
529 return status;
530}
531
532status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
533{
534 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
535 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800536 sp<ThreadBase> thread = mThread.promote();
537 if (thread != 0) {
538 audio_stream_t *stream = thread->stream();
539 if (stream != NULL) {
540 stream->remove_audio_effect(stream, mEffectInterface);
541 }
542 }
543 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800544 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800545}
546
547status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
548 uint32_t cmdSize,
549 void *pCmdData,
550 uint32_t *replySize,
551 void *pReplyData)
552{
553 Mutex::Autolock _l(mLock);
554 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
555
556 if (mState == DESTROYED || mEffectInterface == NULL) {
557 return NO_INIT;
558 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700559 if (mStatus != NO_ERROR) {
560 return mStatus;
561 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800562 status_t status = (*mEffectInterface)->command(mEffectInterface,
563 cmdCode,
564 cmdSize,
565 pCmdData,
566 replySize,
567 pReplyData);
568 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
569 uint32_t size = (replySize == NULL) ? 0 : *replySize;
570 for (size_t i = 1; i < mHandles.size(); i++) {
571 EffectHandle *h = mHandles[i];
572 if (h != NULL && !h->destroyed_l()) {
573 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
574 }
575 }
576 }
577 return status;
578}
579
580status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
581{
582 Mutex::Autolock _l(mLock);
583 return setEnabled_l(enabled);
584}
585
586// must be called with EffectModule::mLock held
587status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
588{
Alok Kediya601e9b52013-09-27 11:55:10 +0530589 bool effectStateChanged = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800590 ALOGV("setEnabled %p enabled %d", this, enabled);
591
592 if (enabled != isEnabled()) {
Alok Kediya601e9b52013-09-27 11:55:10 +0530593 effectStateChanged = true;
Eric Laurentca7cc822012-11-19 14:55:58 -0800594 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
595 if (enabled && status != NO_ERROR) {
596 return status;
597 }
598
599 switch (mState) {
600 // going from disabled to enabled
601 case IDLE:
602 mState = STARTING;
603 break;
604 case STOPPED:
605 mState = RESTART;
606 break;
607 case STOPPING:
608 mState = ACTIVE;
609 break;
610
611 // going from enabled to disabled
612 case RESTART:
613 mState = STOPPED;
614 break;
615 case STARTING:
616 mState = IDLE;
617 break;
618 case ACTIVE:
619 mState = STOPPING;
620 break;
621 case DESTROYED:
622 return NO_ERROR; // simply ignore as we are being destroyed
623 }
624 for (size_t i = 1; i < mHandles.size(); i++) {
625 EffectHandle *h = mHandles[i];
626 if (h != NULL && !h->destroyed_l()) {
627 h->setEnabled(enabled);
628 }
629 }
630 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530631 /*
632 Send notification event to LPA Player when an effect for
633 LPA output is enabled or disabled.
634 */
635 sp<EffectChain> chain = mChain.promote();
636 if (effectStateChanged && chain->isForLPATrack()) {
637 sp<ThreadBase> thread = mThread.promote();
638 unlock();//Acquire locks in certain sequence to avoid deadlock
639 thread->effectConfigChanged();
640 lock();
641 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800642 return NO_ERROR;
643}
644
645bool AudioFlinger::EffectModule::isEnabled() const
646{
647 switch (mState) {
648 case RESTART:
649 case STARTING:
650 case ACTIVE:
651 return true;
652 case IDLE:
653 case STOPPING:
654 case STOPPED:
655 case DESTROYED:
656 default:
657 return false;
658 }
659}
660
661bool AudioFlinger::EffectModule::isProcessEnabled() const
662{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700663 if (mStatus != NO_ERROR) {
664 return false;
665 }
666
Eric Laurentca7cc822012-11-19 14:55:58 -0800667 switch (mState) {
668 case RESTART:
669 case ACTIVE:
670 case STOPPING:
671 case STOPPED:
672 return true;
673 case IDLE:
674 case STARTING:
675 case DESTROYED:
676 default:
677 return false;
678 }
679}
680
681status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
682{
683 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700684 if (mStatus != NO_ERROR) {
685 return mStatus;
686 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800687 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800688 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
689 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
690 if (isProcessEnabled() &&
691 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
692 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Alok Kediya601e9b52013-09-27 11:55:10 +0530693 status_t cmdStatus = 0;
Eric Laurentca7cc822012-11-19 14:55:58 -0800694 uint32_t volume[2];
695 uint32_t *pVolume = NULL;
696 uint32_t size = sizeof(volume);
697 volume[0] = *left;
698 volume[1] = *right;
699 if (controller) {
700 pVolume = volume;
701 }
702 status = (*mEffectInterface)->command(mEffectInterface,
703 EFFECT_CMD_SET_VOLUME,
704 size,
705 volume,
706 &size,
707 pVolume);
708 if (controller && status == NO_ERROR && size == sizeof(volume)) {
709 *left = volume[0];
710 *right = volume[1];
711 }
712 }
713 return status;
714}
715
716status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
717{
718 if (device == AUDIO_DEVICE_NONE) {
719 return NO_ERROR;
720 }
721
722 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700723 if (mStatus != NO_ERROR) {
724 return mStatus;
725 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800726 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700727 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Alok Kediya601e9b52013-09-27 11:55:10 +0530728 status_t cmdStatus = 0;
Eric Laurentca7cc822012-11-19 14:55:58 -0800729 uint32_t size = sizeof(status_t);
730 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
731 EFFECT_CMD_SET_INPUT_DEVICE;
732 status = (*mEffectInterface)->command(mEffectInterface,
733 cmd,
734 sizeof(uint32_t),
735 &device,
736 &size,
737 &cmdStatus);
738 }
739 return status;
740}
741
742status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
743{
744 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700745 if (mStatus != NO_ERROR) {
746 return mStatus;
747 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800748 status_t status = NO_ERROR;
749 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
Alok Kediya601e9b52013-09-27 11:55:10 +0530750 status_t cmdStatus = 0;
Eric Laurentca7cc822012-11-19 14:55:58 -0800751 uint32_t size = sizeof(status_t);
752 status = (*mEffectInterface)->command(mEffectInterface,
753 EFFECT_CMD_SET_AUDIO_MODE,
754 sizeof(audio_mode_t),
755 &mode,
756 &size,
757 &cmdStatus);
758 if (status == NO_ERROR) {
759 status = cmdStatus;
760 }
761 }
762 return status;
763}
764
765status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
766{
767 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700768 if (mStatus != NO_ERROR) {
769 return mStatus;
770 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800771 status_t status = NO_ERROR;
772 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
773 uint32_t size = 0;
774 status = (*mEffectInterface)->command(mEffectInterface,
775 EFFECT_CMD_SET_AUDIO_SOURCE,
776 sizeof(audio_source_t),
777 &source,
778 &size,
779 NULL);
780 }
781 return status;
782}
783
784void AudioFlinger::EffectModule::setSuspended(bool suspended)
785{
786 Mutex::Autolock _l(mLock);
787 mSuspended = suspended;
788}
789
790bool AudioFlinger::EffectModule::suspended() const
791{
792 Mutex::Autolock _l(mLock);
793 return mSuspended;
794}
795
796bool AudioFlinger::EffectModule::purgeHandles()
797{
798 bool enabled = false;
799 Mutex::Autolock _l(mLock);
800 for (size_t i = 0; i < mHandles.size(); i++) {
801 EffectHandle *handle = mHandles[i];
802 if (handle != NULL && !handle->destroyed_l()) {
803 handle->effect().clear();
804 if (handle->hasControl()) {
805 enabled = handle->enabled();
806 }
807 }
808 }
809 return enabled;
810}
811
Eric Laurent5baf2af2013-09-12 17:37:00 -0700812status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
813{
814 Mutex::Autolock _l(mLock);
815 if (mStatus != NO_ERROR) {
816 return mStatus;
817 }
818 status_t status = NO_ERROR;
819 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
820 status_t cmdStatus;
821 uint32_t size = sizeof(status_t);
822 effect_offload_param_t cmd;
823
824 cmd.isOffload = offloaded;
825 cmd.ioHandle = io;
826 status = (*mEffectInterface)->command(mEffectInterface,
827 EFFECT_CMD_OFFLOAD,
828 sizeof(effect_offload_param_t),
829 &cmd,
830 &size,
831 &cmdStatus);
832 if (status == NO_ERROR) {
833 status = cmdStatus;
834 }
835 mOffloaded = (status == NO_ERROR) ? offloaded : false;
836 } else {
837 if (offloaded) {
838 status = INVALID_OPERATION;
839 }
840 mOffloaded = false;
841 }
842 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
843 return status;
844}
845
846bool AudioFlinger::EffectModule::isOffloaded() const
847{
848 Mutex::Autolock _l(mLock);
849 return mOffloaded;
850}
851
Eric Laurentca7cc822012-11-19 14:55:58 -0800852void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
853{
854 const size_t SIZE = 256;
855 char buffer[SIZE];
856 String8 result;
857
858 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
859 result.append(buffer);
860
861 bool locked = AudioFlinger::dumpTryLock(mLock);
862 // failed to lock - AudioFlinger is probably deadlocked
863 if (!locked) {
864 result.append("\t\tCould not lock Fx mutex:\n");
865 }
866
867 result.append("\t\tSession Status State Engine:\n");
868 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n",
869 mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
870 result.append(buffer);
871
872 result.append("\t\tDescriptor:\n");
873 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
874 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
875 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
876 mDescriptor.uuid.node[2],
877 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
878 result.append(buffer);
879 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
880 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
881 mDescriptor.type.timeHiAndVersion,
882 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
883 mDescriptor.type.node[2],
884 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
885 result.append(buffer);
886 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
887 mDescriptor.apiVersion,
888 mDescriptor.flags);
889 result.append(buffer);
890 snprintf(buffer, SIZE, "\t\t- name: %s\n",
891 mDescriptor.name);
892 result.append(buffer);
893 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
894 mDescriptor.implementor);
895 result.append(buffer);
896
897 result.append("\t\t- Input configuration:\n");
898 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
899 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
900 (uint32_t)mConfig.inputCfg.buffer.raw,
901 mConfig.inputCfg.buffer.frameCount,
902 mConfig.inputCfg.samplingRate,
903 mConfig.inputCfg.channels,
904 mConfig.inputCfg.format);
905 result.append(buffer);
906
907 result.append("\t\t- Output configuration:\n");
908 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
909 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
910 (uint32_t)mConfig.outputCfg.buffer.raw,
911 mConfig.outputCfg.buffer.frameCount,
912 mConfig.outputCfg.samplingRate,
913 mConfig.outputCfg.channels,
914 mConfig.outputCfg.format);
915 result.append(buffer);
916
917 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
918 result.append(buffer);
919 result.append("\t\t\tPid Priority Ctrl Locked client server\n");
920 for (size_t i = 0; i < mHandles.size(); ++i) {
921 EffectHandle *handle = mHandles[i];
922 if (handle != NULL && !handle->destroyed_l()) {
923 handle->dump(buffer, SIZE);
924 result.append(buffer);
925 }
926 }
927
928 result.append("\n");
929
930 write(fd, result.string(), result.length());
931
932 if (locked) {
933 mLock.unlock();
934 }
935}
936
937// ----------------------------------------------------------------------------
938// EffectHandle implementation
939// ----------------------------------------------------------------------------
940
941#undef LOG_TAG
942#define LOG_TAG "AudioFlinger::EffectHandle"
943
944AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
945 const sp<AudioFlinger::Client>& client,
946 const sp<IEffectClient>& effectClient,
947 int32_t priority)
948 : BnEffect(),
949 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
950 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
951{
952 ALOGV("constructor %p", this);
953
954 if (client == 0) {
955 return;
956 }
957 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
958 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
959 if (mCblkMemory != 0) {
960 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
961
962 if (mCblk != NULL) {
963 new(mCblk) effect_param_cblk_t();
964 mBuffer = (uint8_t *)mCblk + bufOffset;
965 }
966 } else {
967 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE +
968 sizeof(effect_param_cblk_t));
969 return;
970 }
971}
972
973AudioFlinger::EffectHandle::~EffectHandle()
974{
975 ALOGV("Destructor %p", this);
976
977 if (mEffect == 0) {
978 mDestroyed = true;
979 return;
980 }
981 mEffect->lock();
982 mDestroyed = true;
983 mEffect->unlock();
984 disconnect(false);
985}
986
987status_t AudioFlinger::EffectHandle::enable()
988{
989 ALOGV("enable %p", this);
990 if (!mHasControl) {
991 return INVALID_OPERATION;
992 }
993 if (mEffect == 0) {
994 return DEAD_OBJECT;
995 }
996
997 if (mEnabled) {
998 return NO_ERROR;
999 }
1000
1001 mEnabled = true;
1002
1003 sp<ThreadBase> thread = mEffect->thread().promote();
1004 if (thread != 0) {
1005 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1006 }
1007
1008 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1009 if (mEffect->suspended()) {
1010 return NO_ERROR;
1011 }
1012
1013 status_t status = mEffect->setEnabled(true);
1014 if (status != NO_ERROR) {
1015 if (thread != 0) {
1016 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1017 }
1018 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001019 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001020 if (thread != 0) {
1021 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001022 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001023 Mutex::Autolock _l(t->mLock);
1024 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001025 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001026 if (!mEffect->isOffloadable()) {
1027 if (thread->type() == ThreadBase::OFFLOAD) {
1028 PlaybackThread *t = (PlaybackThread *)thread.get();
1029 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1030 }
1031 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1032 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1033 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001034 }
1035 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 }
1037 return status;
1038}
1039
1040status_t AudioFlinger::EffectHandle::disable()
1041{
1042 ALOGV("disable %p", this);
1043 if (!mHasControl) {
1044 return INVALID_OPERATION;
1045 }
1046 if (mEffect == 0) {
1047 return DEAD_OBJECT;
1048 }
1049
1050 if (!mEnabled) {
1051 return NO_ERROR;
1052 }
1053 mEnabled = false;
1054
1055 if (mEffect->suspended()) {
1056 return NO_ERROR;
1057 }
1058
1059 status_t status = mEffect->setEnabled(false);
1060
1061 sp<ThreadBase> thread = mEffect->thread().promote();
1062 if (thread != 0) {
1063 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001064 if (thread->type() == ThreadBase::OFFLOAD) {
1065 PlaybackThread *t = (PlaybackThread *)thread.get();
1066 Mutex::Autolock _l(t->mLock);
1067 t->broadcast_l();
1068 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 }
1070
1071 return status;
1072}
1073
1074void AudioFlinger::EffectHandle::disconnect()
1075{
1076 disconnect(true);
1077}
1078
1079void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1080{
1081 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1082 if (mEffect == 0) {
1083 return;
1084 }
1085 // restore suspended effects if the disconnected handle was enabled and the last one.
1086 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1087 sp<ThreadBase> thread = mEffect->thread().promote();
1088 if (thread != 0) {
1089 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1090 }
1091 }
1092
1093 // release sp on module => module destructor can be called now
1094 mEffect.clear();
1095 if (mClient != 0) {
1096 if (mCblk != NULL) {
1097 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1098 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1099 }
1100 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
1101 // Client destructor must run with AudioFlinger mutex locked
1102 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
1103 mClient.clear();
1104 }
1105}
1106
1107status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1108 uint32_t cmdSize,
1109 void *pCmdData,
1110 uint32_t *replySize,
1111 void *pReplyData)
1112{
1113 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1114 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1115
1116 // only get parameter command is permitted for applications not controlling the effect
1117 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1118 return INVALID_OPERATION;
1119 }
1120 if (mEffect == 0) {
1121 return DEAD_OBJECT;
1122 }
1123 if (mClient == 0) {
1124 return INVALID_OPERATION;
1125 }
1126
1127 // handle commands that are not forwarded transparently to effect engine
1128 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1129 // No need to trylock() here as this function is executed in the binder thread serving a
1130 // particular client process: no risk to block the whole media server process or mixer
1131 // threads if we are stuck here
1132 Mutex::Autolock _l(mCblk->lock);
1133 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1134 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1135 mCblk->serverIndex = 0;
1136 mCblk->clientIndex = 0;
1137 return BAD_VALUE;
1138 }
1139 status_t status = NO_ERROR;
1140 while (mCblk->serverIndex < mCblk->clientIndex) {
1141 int reply;
1142 uint32_t rsize = sizeof(int);
1143 int *p = (int *)(mBuffer + mCblk->serverIndex);
1144 int size = *p++;
1145 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1146 ALOGW("command(): invalid parameter block size");
1147 break;
1148 }
1149 effect_param_t *param = (effect_param_t *)p;
1150 if (param->psize == 0 || param->vsize == 0) {
1151 ALOGW("command(): null parameter or value size");
1152 mCblk->serverIndex += size;
1153 continue;
1154 }
1155 uint32_t psize = sizeof(effect_param_t) +
1156 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1157 param->vsize;
1158 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1159 psize,
1160 p,
1161 &rsize,
1162 &reply);
1163 // stop at first error encountered
1164 if (ret != NO_ERROR) {
1165 status = ret;
1166 *(int *)pReplyData = reply;
1167 break;
1168 } else if (reply != NO_ERROR) {
1169 *(int *)pReplyData = reply;
1170 break;
1171 }
1172 mCblk->serverIndex += size;
1173 }
1174 mCblk->serverIndex = 0;
1175 mCblk->clientIndex = 0;
1176 return status;
1177 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1178 *(int *)pReplyData = NO_ERROR;
1179 return enable();
1180 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1181 *(int *)pReplyData = NO_ERROR;
1182 return disable();
1183 }
1184
Alok Kediya601e9b52013-09-27 11:55:10 +05301185 if(mEffect->isOnLPA() &&
1186 ((cmdCode == EFFECT_CMD_SET_PARAM) || (cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) ||
1187 (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) || (cmdCode == EFFECT_CMD_SET_DEVICE) ||
1188 (cmdCode == EFFECT_CMD_SET_VOLUME) || (cmdCode == EFFECT_CMD_SET_AUDIO_MODE)) ) {
1189 // Notify Direct track for the change in Effect module
1190 // TODO: check if it is required to send mLPAHandle
1191 ALOGV("Notifying Direct Track for the change in effect config %d", cmdCode);
1192 mClient->audioFlinger()->audioConfigChanged_l(AudioSystem::EFFECT_CONFIG_CHANGED, 0, NULL);
1193 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001194 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1195}
1196
1197void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1198{
1199 ALOGV("setControl %p control %d", this, hasControl);
1200
1201 mHasControl = hasControl;
1202 mEnabled = enabled;
1203
1204 if (signal && mEffectClient != 0) {
1205 mEffectClient->controlStatusChanged(hasControl);
1206 }
1207}
1208
1209void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1210 uint32_t cmdSize,
1211 void *pCmdData,
1212 uint32_t replySize,
1213 void *pReplyData)
1214{
1215 if (mEffectClient != 0) {
1216 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1217 }
1218}
1219
1220
1221
1222void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1223{
1224 if (mEffectClient != 0) {
1225 mEffectClient->enableStatusChanged(enabled);
1226 }
1227}
1228
1229status_t AudioFlinger::EffectHandle::onTransact(
1230 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1231{
1232 return BnEffect::onTransact(code, data, reply, flags);
1233}
1234
1235
1236void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
1237{
1238 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1239
1240 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
1241 (mClient == 0) ? getpid_cached : mClient->pid(),
1242 mPriority,
1243 mHasControl,
1244 !locked,
1245 mCblk ? mCblk->clientIndex : 0,
1246 mCblk ? mCblk->serverIndex : 0
1247 );
1248
1249 if (locked) {
1250 mCblk->lock.unlock();
1251 }
1252}
1253
1254#undef LOG_TAG
1255#define LOG_TAG "AudioFlinger::EffectChain"
1256
1257AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1258 int sessionId)
1259 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1260 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Alok Kediya601e9b52013-09-27 11:55:10 +05301261 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), mIsForLPATrack(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001262{
1263 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1264 if (thread == NULL) {
1265 return;
1266 }
1267 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1268 thread->frameCount();
1269}
1270
1271AudioFlinger::EffectChain::~EffectChain()
1272{
1273 if (mOwnInBuffer) {
1274 delete mInBuffer;
1275 }
1276
1277}
1278
1279// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1280sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1281 effect_descriptor_t *descriptor)
1282{
1283 size_t size = mEffects.size();
1284
1285 for (size_t i = 0; i < size; i++) {
1286 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1287 return mEffects[i];
1288 }
1289 }
1290 return 0;
1291}
1292
1293// getEffectFromId_l() must be called with ThreadBase::mLock held
1294sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1295{
1296 size_t size = mEffects.size();
1297
1298 for (size_t i = 0; i < size; i++) {
1299 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1300 if (id == 0 || mEffects[i]->id() == id) {
1301 return mEffects[i];
1302 }
1303 }
1304 return 0;
1305}
1306
Alok Kediya601e9b52013-09-27 11:55:10 +05301307sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromIndex_l(int idx)
1308{
1309 sp<EffectModule> effect = NULL;
1310 if(idx < 0 || idx >= mEffects.size()) {
1311 ALOGE("EffectChain::getEffectFromIndex_l: invalid index %d", idx);
1312 }
1313 if(mEffects.size() > 0){
1314 effect = mEffects[idx];
1315 }
1316 return effect;
1317}
1318
Eric Laurentca7cc822012-11-19 14:55:58 -08001319// getEffectFromType_l() must be called with ThreadBase::mLock held
1320sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1321 const effect_uuid_t *type)
1322{
1323 size_t size = mEffects.size();
1324
1325 for (size_t i = 0; i < size; i++) {
1326 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1327 return mEffects[i];
1328 }
1329 }
1330 return 0;
1331}
1332
1333void AudioFlinger::EffectChain::clearInputBuffer()
1334{
1335 Mutex::Autolock _l(mLock);
1336 sp<ThreadBase> thread = mThread.promote();
1337 if (thread == 0) {
1338 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1339 return;
1340 }
1341 clearInputBuffer_l(thread);
1342}
1343
1344// Must be called with EffectChain::mLock locked
1345void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1346{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001347 memset(mInBuffer, 0, thread->frameCount() * thread->frameSize());
Eric Laurentca7cc822012-11-19 14:55:58 -08001348}
1349
1350// Must be called with EffectChain::mLock locked
1351void AudioFlinger::EffectChain::process_l()
1352{
1353 sp<ThreadBase> thread = mThread.promote();
1354 if (thread == 0) {
1355 ALOGW("process_l(): cannot promote mixer thread");
1356 return;
1357 }
1358 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1359 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001360 // never process effects when:
1361 // - on an OFFLOAD thread
1362 // - no more tracks are on the session and the effect tail has been rendered
1363 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001364 if (!isGlobalSession) {
1365 bool tracksOnSession = (trackCnt() != 0);
1366
1367 if (!tracksOnSession && mTailBufferCount == 0) {
1368 doProcess = false;
1369 }
1370
1371 if (activeTrackCnt() == 0) {
1372 // if no track is active and the effect tail has not been rendered,
1373 // the input buffer must be cleared here as the mixer process will not do it
1374 if (tracksOnSession || mTailBufferCount > 0) {
1375 clearInputBuffer_l(thread);
1376 if (mTailBufferCount > 0) {
1377 mTailBufferCount--;
1378 }
1379 }
1380 }
1381 }
1382
1383 size_t size = mEffects.size();
Alok Kediya601e9b52013-09-27 11:55:10 +05301384 if (doProcess || isForLPATrack()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001385 for (size_t i = 0; i < size; i++) {
1386 mEffects[i]->process();
1387 }
1388 }
1389 for (size_t i = 0; i < size; i++) {
1390 mEffects[i]->updateState();
1391 }
1392}
1393
1394// addEffect_l() must be called with PlaybackThread::mLock held
1395status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1396{
1397 effect_descriptor_t desc = effect->desc();
1398 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1399
1400 Mutex::Autolock _l(mLock);
1401 effect->setChain(this);
1402 sp<ThreadBase> thread = mThread.promote();
1403 if (thread == 0) {
1404 return NO_INIT;
1405 }
1406 effect->setThread(thread);
1407
1408 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1409 // Auxiliary effects are inserted at the beginning of mEffects vector as
1410 // they are processed first and accumulated in chain input buffer
1411 mEffects.insertAt(effect, 0);
1412
1413 // the input buffer for auxiliary effect contains mono samples in
1414 // 32 bit format. This is to avoid saturation in AudoMixer
1415 // accumulation stage. Saturation is done in EffectModule::process() before
1416 // calling the process in effect engine
1417 size_t numSamples = thread->frameCount();
1418 int32_t *buffer = new int32_t[numSamples];
1419 memset(buffer, 0, numSamples * sizeof(int32_t));
1420 effect->setInBuffer((int16_t *)buffer);
1421 // auxiliary effects output samples to chain input buffer for further processing
1422 // by insert effects
1423 effect->setOutBuffer(mInBuffer);
1424 } else {
1425 // Insert effects are inserted at the end of mEffects vector as they are processed
1426 // after track and auxiliary effects.
1427 // Insert effect order as a function of indicated preference:
1428 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1429 // another effect is present
1430 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1431 // last effect claiming first position
1432 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1433 // first effect claiming last position
1434 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1435 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1436 // already present
1437
1438 size_t size = mEffects.size();
1439 size_t idx_insert = size;
1440 ssize_t idx_insert_first = -1;
1441 ssize_t idx_insert_last = -1;
1442
1443 for (size_t i = 0; i < size; i++) {
1444 effect_descriptor_t d = mEffects[i]->desc();
1445 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1446 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1447 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1448 // check invalid effect chaining combinations
1449 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1450 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1451 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1452 desc.name, d.name);
1453 return INVALID_OPERATION;
1454 }
1455 // remember position of first insert effect and by default
1456 // select this as insert position for new effect
1457 if (idx_insert == size) {
1458 idx_insert = i;
1459 }
1460 // remember position of last insert effect claiming
1461 // first position
1462 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1463 idx_insert_first = i;
1464 }
1465 // remember position of first insert effect claiming
1466 // last position
1467 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1468 idx_insert_last == -1) {
1469 idx_insert_last = i;
1470 }
1471 }
1472 }
1473
1474 // modify idx_insert from first position if needed
1475 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1476 if (idx_insert_last != -1) {
1477 idx_insert = idx_insert_last;
1478 } else {
1479 idx_insert = size;
1480 }
1481 } else {
1482 if (idx_insert_first != -1) {
1483 idx_insert = idx_insert_first + 1;
1484 }
1485 }
1486
1487 // always read samples from chain input buffer
1488 effect->setInBuffer(mInBuffer);
1489
1490 // if last effect in the chain, output samples to chain
1491 // output buffer, otherwise to chain input buffer
1492 if (idx_insert == size) {
1493 if (idx_insert != 0) {
1494 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1495 mEffects[idx_insert-1]->configure();
1496 }
1497 effect->setOutBuffer(mOutBuffer);
1498 } else {
1499 effect->setOutBuffer(mInBuffer);
1500 }
1501 mEffects.insertAt(effect, idx_insert);
1502
1503 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1504 idx_insert);
1505 }
1506 effect->configure();
1507 return NO_ERROR;
1508}
1509
1510// removeEffect_l() must be called with PlaybackThread::mLock held
1511size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1512{
1513 Mutex::Autolock _l(mLock);
1514 size_t size = mEffects.size();
1515 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1516
1517 for (size_t i = 0; i < size; i++) {
1518 if (effect == mEffects[i]) {
1519 // calling stop here will remove pre-processing effect from the audio HAL.
1520 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1521 // the middle of a read from audio HAL
1522 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1523 mEffects[i]->state() == EffectModule::STOPPING) {
1524 mEffects[i]->stop();
1525 }
1526 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1527 delete[] effect->inBuffer();
1528 } else {
1529 if (i == size - 1 && i != 0) {
1530 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1531 mEffects[i - 1]->configure();
1532 }
1533 }
1534 mEffects.removeAt(i);
1535 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1536 this, i);
1537 break;
1538 }
1539 }
1540
1541 return mEffects.size();
1542}
1543
1544// setDevice_l() must be called with PlaybackThread::mLock held
1545void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1546{
1547 size_t size = mEffects.size();
1548 for (size_t i = 0; i < size; i++) {
1549 mEffects[i]->setDevice(device);
1550 }
1551}
1552
1553// setMode_l() must be called with PlaybackThread::mLock held
1554void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1555{
1556 size_t size = mEffects.size();
1557 for (size_t i = 0; i < size; i++) {
1558 mEffects[i]->setMode(mode);
1559 }
1560}
1561
1562// setAudioSource_l() must be called with PlaybackThread::mLock held
1563void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1564{
1565 size_t size = mEffects.size();
1566 for (size_t i = 0; i < size; i++) {
1567 mEffects[i]->setAudioSource(source);
1568 }
1569}
1570
1571// setVolume_l() must be called with PlaybackThread::mLock held
1572bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1573{
1574 uint32_t newLeft = *left;
1575 uint32_t newRight = *right;
1576 bool hasControl = false;
1577 int ctrlIdx = -1;
1578 size_t size = mEffects.size();
1579
1580 // first update volume controller
1581 for (size_t i = size; i > 0; i--) {
1582 if (mEffects[i - 1]->isProcessEnabled() &&
1583 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1584 ctrlIdx = i - 1;
1585 hasControl = true;
1586 break;
1587 }
1588 }
1589
1590 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
1591 if (hasControl) {
1592 *left = mNewLeftVolume;
1593 *right = mNewRightVolume;
1594 }
1595 return hasControl;
1596 }
1597
1598 mVolumeCtrlIdx = ctrlIdx;
1599 mLeftVolume = newLeft;
1600 mRightVolume = newRight;
1601
1602 // second get volume update from volume controller
1603 if (ctrlIdx >= 0) {
1604 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1605 mNewLeftVolume = newLeft;
1606 mNewRightVolume = newRight;
1607 }
1608 // then indicate volume to all other effects in chain.
1609 // Pass altered volume to effects before volume controller
1610 // and requested volume to effects after controller
1611 uint32_t lVol = newLeft;
1612 uint32_t rVol = newRight;
1613
1614 for (size_t i = 0; i < size; i++) {
1615 if ((int)i == ctrlIdx) {
1616 continue;
1617 }
1618 // this also works for ctrlIdx == -1 when there is no volume controller
1619 if ((int)i > ctrlIdx) {
1620 lVol = *left;
1621 rVol = *right;
1622 }
1623 mEffects[i]->setVolume(&lVol, &rVol, false);
1624 }
1625 *left = newLeft;
1626 *right = newRight;
1627
1628 return hasControl;
1629}
1630
1631void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1632{
1633 const size_t SIZE = 256;
1634 char buffer[SIZE];
1635 String8 result;
1636
1637 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
1638 result.append(buffer);
1639
1640 bool locked = AudioFlinger::dumpTryLock(mLock);
1641 // failed to lock - AudioFlinger is probably deadlocked
1642 if (!locked) {
1643 result.append("\tCould not lock mutex:\n");
1644 }
1645
1646 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
1647 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
1648 mEffects.size(),
1649 (uint32_t)mInBuffer,
1650 (uint32_t)mOutBuffer,
1651 mActiveTrackCnt);
1652 result.append(buffer);
1653 write(fd, result.string(), result.size());
1654
1655 for (size_t i = 0; i < mEffects.size(); ++i) {
1656 sp<EffectModule> effect = mEffects[i];
1657 if (effect != 0) {
1658 effect->dump(fd, args);
1659 }
1660 }
1661
1662 if (locked) {
1663 mLock.unlock();
1664 }
1665}
1666
1667// must be called with ThreadBase::mLock held
1668void AudioFlinger::EffectChain::setEffectSuspended_l(
1669 const effect_uuid_t *type, bool suspend)
1670{
1671 sp<SuspendedEffectDesc> desc;
1672 // use effect type UUID timelow as key as there is no real risk of identical
1673 // timeLow fields among effect type UUIDs.
1674 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1675 if (suspend) {
1676 if (index >= 0) {
1677 desc = mSuspendedEffects.valueAt(index);
1678 } else {
1679 desc = new SuspendedEffectDesc();
1680 desc->mType = *type;
1681 mSuspendedEffects.add(type->timeLow, desc);
1682 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1683 }
1684 if (desc->mRefCount++ == 0) {
1685 sp<EffectModule> effect = getEffectIfEnabled(type);
1686 if (effect != 0) {
1687 desc->mEffect = effect;
1688 effect->setSuspended(true);
1689 effect->setEnabled(false);
1690 }
1691 }
1692 } else {
1693 if (index < 0) {
1694 return;
1695 }
1696 desc = mSuspendedEffects.valueAt(index);
1697 if (desc->mRefCount <= 0) {
1698 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1699 desc->mRefCount = 1;
1700 }
1701 if (--desc->mRefCount == 0) {
1702 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1703 if (desc->mEffect != 0) {
1704 sp<EffectModule> effect = desc->mEffect.promote();
1705 if (effect != 0) {
1706 effect->setSuspended(false);
1707 effect->lock();
1708 EffectHandle *handle = effect->controlHandle_l();
1709 if (handle != NULL && !handle->destroyed_l()) {
1710 effect->setEnabled_l(handle->enabled());
1711 }
1712 effect->unlock();
1713 }
1714 desc->mEffect.clear();
1715 }
1716 mSuspendedEffects.removeItemsAt(index);
1717 }
1718 }
1719}
1720
1721// must be called with ThreadBase::mLock held
1722void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1723{
1724 sp<SuspendedEffectDesc> desc;
1725
1726 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1727 if (suspend) {
1728 if (index >= 0) {
1729 desc = mSuspendedEffects.valueAt(index);
1730 } else {
1731 desc = new SuspendedEffectDesc();
1732 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1733 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1734 }
1735 if (desc->mRefCount++ == 0) {
1736 Vector< sp<EffectModule> > effects;
1737 getSuspendEligibleEffects(effects);
1738 for (size_t i = 0; i < effects.size(); i++) {
1739 setEffectSuspended_l(&effects[i]->desc().type, true);
1740 }
1741 }
1742 } else {
1743 if (index < 0) {
1744 return;
1745 }
1746 desc = mSuspendedEffects.valueAt(index);
1747 if (desc->mRefCount <= 0) {
1748 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1749 desc->mRefCount = 1;
1750 }
1751 if (--desc->mRefCount == 0) {
1752 Vector<const effect_uuid_t *> types;
1753 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1754 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1755 continue;
1756 }
1757 types.add(&mSuspendedEffects.valueAt(i)->mType);
1758 }
1759 for (size_t i = 0; i < types.size(); i++) {
1760 setEffectSuspended_l(types[i], false);
1761 }
1762 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1763 mSuspendedEffects.keyAt(index));
1764 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1765 }
1766 }
1767}
1768
1769
1770// The volume effect is used for automated tests only
1771#ifndef OPENSL_ES_H_
1772static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1773 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1774const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1775#endif //OPENSL_ES_H_
1776
1777bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1778{
1779 // auxiliary effects and visualizer are never suspended on output mix
1780 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1781 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1782 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1783 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1784 return false;
1785 }
1786 return true;
1787}
1788
1789void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1790 Vector< sp<AudioFlinger::EffectModule> > &effects)
1791{
1792 effects.clear();
1793 for (size_t i = 0; i < mEffects.size(); i++) {
1794 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1795 effects.add(mEffects[i]);
1796 }
1797 }
1798}
1799
1800sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1801 const effect_uuid_t *type)
1802{
1803 sp<EffectModule> effect = getEffectFromType_l(type);
1804 return effect != 0 && effect->isEnabled() ? effect : 0;
1805}
1806
1807void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1808 bool enabled)
1809{
1810 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1811 if (enabled) {
1812 if (index < 0) {
1813 // if the effect is not suspend check if all effects are suspended
1814 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1815 if (index < 0) {
1816 return;
1817 }
1818 if (!isEffectEligibleForSuspend(effect->desc())) {
1819 return;
1820 }
1821 setEffectSuspended_l(&effect->desc().type, enabled);
1822 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1823 if (index < 0) {
1824 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1825 return;
1826 }
1827 }
1828 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1829 effect->desc().type.timeLow);
1830 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1831 // if effect is requested to suspended but was not yet enabled, supend it now.
1832 if (desc->mEffect == 0) {
1833 desc->mEffect = effect;
1834 effect->setEnabled(false);
1835 effect->setSuspended(true);
1836 }
1837 } else {
1838 if (index < 0) {
1839 return;
1840 }
1841 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1842 effect->desc().type.timeLow);
1843 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1844 desc->mEffect.clear();
1845 effect->setSuspended(false);
1846 }
1847}
1848
Alok Kediya601e9b52013-09-27 11:55:10 +05301849
Eric Laurent5baf2af2013-09-12 17:37:00 -07001850bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001851{
1852 Mutex::Autolock _l(mLock);
1853 size_t size = mEffects.size();
1854 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001855 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001856 return true;
1857 }
1858 }
1859 return false;
1860}
1861
Alok Kediya601e9b52013-09-27 11:55:10 +05301862#define DEAFULT_FRAME_COUNT 1200
1863bool AudioFlinger::applyEffectsOn(void *token, int16_t *inBuffer,
1864 int16_t *outBuffer, int size, bool force)
1865{
1866 ALOGV("applyEffectsOn: inBuf %p outBuf %p size %d token %p", inBuffer, outBuffer, size, token);
1867 // This might be the first buffer to apply effects after effect config change
1868 // should not skip effects processing
1869 mIsEffectConfigChanged = false;
1870
1871 volatile size_t numEffects = 0;
1872#ifdef SRS_PROCESSING
1873 POSTPRO_PATCH_ICS_OUTPROC_DIRECT_SAMPLES(token, AUDIO_FORMAT_PCM_16_BIT, outBuffer, size, mLPASampleRate, mLPANumChannels);
1874#endif
1875
1876 if(mLPAEffectChain != NULL) {
1877 numEffects = mLPAEffectChain->getNumEffects();
1878 }
1879
1880 if( numEffects > 0) {
1881 size_t i = 0;
1882 int16_t *pIn = inBuffer;
1883 int16_t *pOut = outBuffer;
1884
1885 int frameCount = size / (sizeof(int16_t) * mLPANumChannels);
1886
1887 while(frameCount > 0) {
1888 if(mLPAEffectChain == NULL) {
1889 ALOGV("LPA Effect Chain is removed - No effects processing !!");
1890 numEffects = 0;
1891 break;
1892 }
1893 mLPAEffectChain->lock();
1894
1895 numEffects = mLPAEffectChain->getNumEffects();
1896 if(!numEffects) {
1897 ALOGV("applyEffectsOn: All the effects are removed - nothing to process");
1898 mLPAEffectChain->unlock();
1899 break;
1900 }
1901
1902 int outFrameCount = (frameCount > DEAFULT_FRAME_COUNT ? DEAFULT_FRAME_COUNT: frameCount);
1903 bool isEffectEnabled = false;
1904 for(i = 0; i < numEffects; i++) {
1905 // If effect configuration is changed while applying effects do not process further
1906 if(mIsEffectConfigChanged && !force) {
1907 mLPAEffectChain->unlock();
1908 ALOGV("applyEffectsOn: mIsEffectConfigChanged is set - no further processing %d",frameCount);
1909 return false;
1910 }
1911 sp<EffectModule> effect = mLPAEffectChain->getEffectFromIndex_l(i);
1912 if(effect == NULL) {
1913 ALOGE("getEffectFromIndex_l(%d) returned NULL ptr", i);
1914 mLPAEffectChain->unlock();
1915 return false;
1916 }
1917 if(i == 0) {
1918 // For the first set input and output buffers different
1919 isEffectEnabled = effect->isProcessEnabled();
1920 effect->setInBuffer(pIn);
1921 effect->setOutBuffer(pOut);
1922 } else {
1923 // For the remaining use previous effect's output buffer as input buffer
1924 effect->setInBuffer(pOut);
1925 effect->setOutBuffer(pOut);
1926 }
1927 // true indicates that it is being applied on LPA output
1928 effect->configure(true, mLPASampleRate, mLPANumChannels, outFrameCount);
1929 }
1930
1931 if(isEffectEnabled) {
1932 // Clear the output buffer
1933 memset(pOut, 0, (outFrameCount * mLPANumChannels * sizeof(int16_t)));
1934 } else {
1935 // Copy input buffer content to the output buffer
1936 memcpy(pOut, pIn, (outFrameCount * mLPANumChannels * sizeof(int16_t)));
1937 }
1938
1939 mLPAEffectChain->process_l();
1940
1941 mLPAEffectChain->unlock();
1942
1943 // Update input and output buffer pointers
1944 pIn += (outFrameCount * mLPANumChannels);
1945 pOut += (outFrameCount * mLPANumChannels);
1946 frameCount -= outFrameCount;
1947 }
1948 }
1949
1950 if (!numEffects && !force) {
1951 ALOGV("applyEffectsOn: There are no effects to be applied");
1952 if(inBuffer != outBuffer) {
1953 // No effect applied so just copy input buffer to output buffer
1954 memcpy(outBuffer, inBuffer, size);
1955 }
1956 }
1957 return true;
1958}
1959
1960void *AudioFlinger::DirectAudioTrack::EffectsThreadWrapper(void *me) {
1961 static_cast<DirectAudioTrack *>(me)->EffectsThreadEntry();
1962 return NULL;
1963}
1964
1965void AudioFlinger::DirectAudioTrack::EffectsThreadEntry() {
1966 while(1) {
1967 mEffectLock.lock();
1968 if (!mEffectConfigChanged && !mKillEffectsThread) {
1969 mEffectCv.wait(mEffectLock);
1970 }
1971 if(mKillEffectsThread) {
1972 mEffectLock.unlock();
1973 break;
1974 }
1975 if (mEffectConfigChanged) {
1976 mEffectConfigChanged = false;
1977 if (mFlag & AUDIO_OUTPUT_FLAG_LPA) {
1978 for ( List<BufferInfo>::iterator it = mEffectsPool.begin();
1979 it != mEffectsPool.end(); it++) {
1980 ALOGV("ete: calling applyEffectsOn buff %x",it->localBuf);
1981 bool isEffectsApplied = mAudioFlinger->applyEffectsOn(
1982 static_cast<void *>(this),
1983 (int16_t *)it->localBuf,
1984 (int16_t *)mEffectsThreadScratchBuffer,
1985 it->bytesToWrite,
1986 false);
1987 if (isEffectsApplied == true){
1988 ALOGV("ete:dsp updated for local buf %x",it->localBuf);
1989 memcpy(it->dspBuf, mEffectsThreadScratchBuffer, it->bytesToWrite);
1990 }
1991 else
1992 ALOGV("ete:dsp updated for local buf %x SKIPPED",it->localBuf);
1993
1994 if (mEffectConfigChanged) {
1995 ALOGE("ete:effects changed, abort effects application");
1996 break;
1997 }
1998 }
1999#ifdef SRS_PROCESSING
2000 } else if (mFlag & AUDIO_OUTPUT_FLAG_TUNNEL) {
2001 ALOGV("applying effects for TUNNEL");
2002 char buffer[2];
2003 //dummy buffer to ensure the SRS processing takes place
2004 // The API mandates Sample rate and channel mode. Hence
2005 // defaulted the sample rate channel mode to 48000 and 2 respectively
2006 POSTPRO_PATCH_ICS_OUTPROC_DIRECT_SAMPLES(static_cast<void *>(this),
2007 AUDIO_FORMAT_PCM_16_BIT,
2008 (int16_t*)buffer, 2, 48000, 2);
2009#endif
2010 }
2011 }
2012 mEffectLock.unlock();
2013 }
2014 ALOGV("Effects thread is dead");
2015 mEffectsThreadAlive = false;
2016}
2017
2018void AudioFlinger::DirectAudioTrack::requestAndWaitForEffectsThreadExit() {
2019 if (!mEffectsThreadAlive)
2020 return;
2021 mKillEffectsThread = true;
2022 mEffectCv.signal();
2023 pthread_join(mEffectsThread,NULL);
2024 ALOGV("effects thread killed");
2025}
2026
2027void AudioFlinger::DirectAudioTrack::createEffectThread() {
2028 //Create the effects thread
2029 pthread_attr_t attr;
2030 pthread_attr_init(&attr);
2031 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
2032 mEffectsThreadAlive = true;
2033 ALOGV("Creating Effects Thread");
2034 pthread_create(&mEffectsThread, &attr, EffectsThreadWrapper, this);
2035}
2036
Eric Laurentca7cc822012-11-19 14:55:58 -08002037}; // namespace android