blob: 2dd6ff57cec4dfca3a6dc3d90ca4de4710fcb301 [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
Eric Laurent1b928682014-10-02 19:41:47 -0700443void AudioFlinger::EffectModule::addEffectToHal_l()
444{
445 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
446 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
447 sp<ThreadBase> thread = mThread.promote();
448 if (thread != 0) {
449 audio_stream_t *stream = thread->stream();
450 if (stream != NULL) {
451 stream->add_audio_effect(stream, mEffectInterface);
452 }
453 }
454 }
455}
456
Eric Laurentca7cc822012-11-19 14:55:58 -0800457status_t AudioFlinger::EffectModule::start()
458{
459 Mutex::Autolock _l(mLock);
460 return start_l();
461}
462
463status_t AudioFlinger::EffectModule::start_l()
464{
465 if (mEffectInterface == NULL) {
466 return NO_INIT;
467 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700468 if (mStatus != NO_ERROR) {
469 return mStatus;
470 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800471 status_t cmdStatus;
472 uint32_t size = sizeof(status_t);
473 status_t status = (*mEffectInterface)->command(mEffectInterface,
474 EFFECT_CMD_ENABLE,
475 0,
476 NULL,
477 &size,
478 &cmdStatus);
479 if (status == 0) {
480 status = cmdStatus;
481 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700482 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700483 addEffectToHal_l();
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700484 sp<EffectChain> chain = mChain.promote();
485 if (chain != 0) {
486 chain->forceVolume();
487 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800488 }
489 return status;
490}
491
492status_t AudioFlinger::EffectModule::stop()
493{
494 Mutex::Autolock _l(mLock);
495 return stop_l();
496}
497
498status_t AudioFlinger::EffectModule::stop_l()
499{
500 if (mEffectInterface == NULL) {
501 return NO_INIT;
502 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700503 if (mStatus != NO_ERROR) {
504 return mStatus;
505 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800506 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800507 uint32_t size = sizeof(status_t);
508 status_t status = (*mEffectInterface)->command(mEffectInterface,
509 EFFECT_CMD_DISABLE,
510 0,
511 NULL,
512 &size,
513 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800514 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800515 status = cmdStatus;
516 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800517 if (status == NO_ERROR) {
518 status = remove_effect_from_hal_l();
519 }
520 return status;
521}
522
523status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
524{
525 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
526 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800527 sp<ThreadBase> thread = mThread.promote();
528 if (thread != 0) {
529 audio_stream_t *stream = thread->stream();
530 if (stream != NULL) {
531 stream->remove_audio_effect(stream, mEffectInterface);
532 }
533 }
534 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800535 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800536}
537
Andy Hunge4a1d912016-08-17 14:11:13 -0700538// round up delta valid if value and divisor are positive.
539template <typename T>
540static T roundUpDelta(const T &value, const T &divisor) {
541 T remainder = value % divisor;
542 return remainder == 0 ? 0 : divisor - remainder;
543}
544
Eric Laurentca7cc822012-11-19 14:55:58 -0800545status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
546 uint32_t cmdSize,
547 void *pCmdData,
548 uint32_t *replySize,
549 void *pReplyData)
550{
551 Mutex::Autolock _l(mLock);
552 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
553
554 if (mState == DESTROYED || mEffectInterface == NULL) {
555 return NO_INIT;
556 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700557 if (mStatus != NO_ERROR) {
558 return mStatus;
559 }
Andy Hung110bc952016-06-20 15:22:52 -0700560 if (cmdCode == EFFECT_CMD_GET_PARAM &&
561 (*replySize < sizeof(effect_param_t) ||
562 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
563 android_errorWriteLog(0x534e4554, "29251553");
564 return -EINVAL;
565 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700566 if ((cmdCode == EFFECT_CMD_SET_PARAM
567 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
568 (sizeof(effect_param_t) > cmdSize
569 || ((effect_param_t *)pCmdData)->psize > cmdSize
570 - sizeof(effect_param_t)
571 || ((effect_param_t *)pCmdData)->vsize > cmdSize
572 - sizeof(effect_param_t)
573 - ((effect_param_t *)pCmdData)->psize
574 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
575 cmdSize
576 - sizeof(effect_param_t)
577 - ((effect_param_t *)pCmdData)->psize
578 - ((effect_param_t *)pCmdData)->vsize)) {
579 android_errorWriteLog(0x534e4554, "30204301");
580 return -EINVAL;
581 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800582 status_t status = (*mEffectInterface)->command(mEffectInterface,
583 cmdCode,
584 cmdSize,
585 pCmdData,
586 replySize,
587 pReplyData);
588 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
589 uint32_t size = (replySize == NULL) ? 0 : *replySize;
590 for (size_t i = 1; i < mHandles.size(); i++) {
591 EffectHandle *h = mHandles[i];
592 if (h != NULL && !h->destroyed_l()) {
593 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
594 }
595 }
596 }
597 return status;
598}
599
600status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
601{
602 Mutex::Autolock _l(mLock);
603 return setEnabled_l(enabled);
604}
605
606// must be called with EffectModule::mLock held
607status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
608{
609
610 ALOGV("setEnabled %p enabled %d", this, enabled);
611
612 if (enabled != isEnabled()) {
613 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
614 if (enabled && status != NO_ERROR) {
615 return status;
616 }
617
618 switch (mState) {
619 // going from disabled to enabled
620 case IDLE:
621 mState = STARTING;
622 break;
623 case STOPPED:
624 mState = RESTART;
625 break;
626 case STOPPING:
627 mState = ACTIVE;
628 break;
629
630 // going from enabled to disabled
631 case RESTART:
632 mState = STOPPED;
633 break;
634 case STARTING:
635 mState = IDLE;
636 break;
637 case ACTIVE:
638 mState = STOPPING;
639 break;
640 case DESTROYED:
641 return NO_ERROR; // simply ignore as we are being destroyed
642 }
643 for (size_t i = 1; i < mHandles.size(); i++) {
644 EffectHandle *h = mHandles[i];
645 if (h != NULL && !h->destroyed_l()) {
646 h->setEnabled(enabled);
647 }
648 }
649 }
650 return NO_ERROR;
651}
652
653bool AudioFlinger::EffectModule::isEnabled() const
654{
655 switch (mState) {
656 case RESTART:
657 case STARTING:
658 case ACTIVE:
659 return true;
660 case IDLE:
661 case STOPPING:
662 case STOPPED:
663 case DESTROYED:
664 default:
665 return false;
666 }
667}
668
669bool AudioFlinger::EffectModule::isProcessEnabled() const
670{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700671 if (mStatus != NO_ERROR) {
672 return false;
673 }
674
Eric Laurentca7cc822012-11-19 14:55:58 -0800675 switch (mState) {
676 case RESTART:
677 case ACTIVE:
678 case STOPPING:
679 case STOPPED:
680 return true;
681 case IDLE:
682 case STARTING:
683 case DESTROYED:
684 default:
685 return false;
686 }
687}
688
689status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
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 Laurentca7cc822012-11-19 14:55:58 -0800696 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
697 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
698 if (isProcessEnabled() &&
699 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
700 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
701 status_t cmdStatus;
702 uint32_t volume[2];
703 uint32_t *pVolume = NULL;
704 uint32_t size = sizeof(volume);
705 volume[0] = *left;
706 volume[1] = *right;
707 if (controller) {
708 pVolume = volume;
709 }
710 status = (*mEffectInterface)->command(mEffectInterface,
711 EFFECT_CMD_SET_VOLUME,
712 size,
713 volume,
714 &size,
715 pVolume);
716 if (controller && status == NO_ERROR && size == sizeof(volume)) {
717 *left = volume[0];
718 *right = volume[1];
719 }
720 }
721 return status;
722}
723
724status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
725{
726 if (device == AUDIO_DEVICE_NONE) {
727 return NO_ERROR;
728 }
729
730 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700731 if (mStatus != NO_ERROR) {
732 return mStatus;
733 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800734 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700735 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800736 status_t cmdStatus;
737 uint32_t size = sizeof(status_t);
738 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
739 EFFECT_CMD_SET_INPUT_DEVICE;
740 status = (*mEffectInterface)->command(mEffectInterface,
741 cmd,
742 sizeof(uint32_t),
743 &device,
744 &size,
745 &cmdStatus);
746 }
747 return status;
748}
749
750status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
751{
752 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700753 if (mStatus != NO_ERROR) {
754 return mStatus;
755 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800756 status_t status = NO_ERROR;
757 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
758 status_t cmdStatus;
759 uint32_t size = sizeof(status_t);
760 status = (*mEffectInterface)->command(mEffectInterface,
761 EFFECT_CMD_SET_AUDIO_MODE,
762 sizeof(audio_mode_t),
763 &mode,
764 &size,
765 &cmdStatus);
766 if (status == NO_ERROR) {
767 status = cmdStatus;
768 }
769 }
770 return status;
771}
772
773status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
774{
775 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700776 if (mStatus != NO_ERROR) {
777 return mStatus;
778 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800779 status_t status = NO_ERROR;
780 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
781 uint32_t size = 0;
782 status = (*mEffectInterface)->command(mEffectInterface,
783 EFFECT_CMD_SET_AUDIO_SOURCE,
784 sizeof(audio_source_t),
785 &source,
786 &size,
787 NULL);
788 }
789 return status;
790}
791
792void AudioFlinger::EffectModule::setSuspended(bool suspended)
793{
794 Mutex::Autolock _l(mLock);
795 mSuspended = suspended;
796}
797
798bool AudioFlinger::EffectModule::suspended() const
799{
800 Mutex::Autolock _l(mLock);
801 return mSuspended;
802}
803
804bool AudioFlinger::EffectModule::purgeHandles()
805{
806 bool enabled = false;
807 Mutex::Autolock _l(mLock);
808 for (size_t i = 0; i < mHandles.size(); i++) {
809 EffectHandle *handle = mHandles[i];
810 if (handle != NULL && !handle->destroyed_l()) {
811 handle->effect().clear();
812 if (handle->hasControl()) {
813 enabled = handle->enabled();
814 }
815 }
816 }
817 return enabled;
818}
819
Eric Laurent5baf2af2013-09-12 17:37:00 -0700820status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
821{
822 Mutex::Autolock _l(mLock);
823 if (mStatus != NO_ERROR) {
824 return mStatus;
825 }
826 status_t status = NO_ERROR;
827 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
828 status_t cmdStatus;
829 uint32_t size = sizeof(status_t);
830 effect_offload_param_t cmd;
831
832 cmd.isOffload = offloaded;
833 cmd.ioHandle = io;
834 status = (*mEffectInterface)->command(mEffectInterface,
835 EFFECT_CMD_OFFLOAD,
836 sizeof(effect_offload_param_t),
837 &cmd,
838 &size,
839 &cmdStatus);
840 if (status == NO_ERROR) {
841 status = cmdStatus;
842 }
843 mOffloaded = (status == NO_ERROR) ? offloaded : false;
844 } else {
845 if (offloaded) {
846 status = INVALID_OPERATION;
847 }
848 mOffloaded = false;
849 }
850 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
851 return status;
852}
853
854bool AudioFlinger::EffectModule::isOffloaded() const
855{
856 Mutex::Autolock _l(mLock);
857 return mOffloaded;
858}
859
Marco Nelissenb2208842014-02-07 14:00:50 -0800860String8 effectFlagsToString(uint32_t flags) {
861 String8 s;
862
863 s.append("conn. mode: ");
864 switch (flags & EFFECT_FLAG_TYPE_MASK) {
865 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
866 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
867 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
868 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
869 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
870 default: s.append("unknown/reserved"); break;
871 }
872 s.append(", ");
873
874 s.append("insert pref: ");
875 switch (flags & EFFECT_FLAG_INSERT_MASK) {
876 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
877 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
878 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
879 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
880 default: s.append("unknown/reserved"); break;
881 }
882 s.append(", ");
883
884 s.append("volume mgmt: ");
885 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
886 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
887 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
888 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
889 default: s.append("unknown/reserved"); break;
890 }
891 s.append(", ");
892
893 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
894 if (devind) {
895 s.append("device indication: ");
896 switch (devind) {
897 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
898 default: s.append("unknown/reserved"); break;
899 }
900 s.append(", ");
901 }
902
903 s.append("input mode: ");
904 switch (flags & EFFECT_FLAG_INPUT_MASK) {
905 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
906 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
907 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
908 default: s.append("not set"); break;
909 }
910 s.append(", ");
911
912 s.append("output mode: ");
913 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
914 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
915 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
916 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
917 default: s.append("not set"); break;
918 }
919 s.append(", ");
920
921 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
922 if (accel) {
923 s.append("hardware acceleration: ");
924 switch (accel) {
925 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
926 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
927 default: s.append("unknown/reserved"); break;
928 }
929 s.append(", ");
930 }
931
932 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
933 if (modeind) {
934 s.append("mode indication: ");
935 switch (modeind) {
936 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
937 default: s.append("unknown/reserved"); break;
938 }
939 s.append(", ");
940 }
941
942 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
943 if (srcind) {
944 s.append("source indication: ");
945 switch (srcind) {
946 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
947 default: s.append("unknown/reserved"); break;
948 }
949 s.append(", ");
950 }
951
952 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
953 s.append("offloadable, ");
954 }
955
956 int len = s.length();
957 if (s.length() > 2) {
958 char *str = s.lockBuffer(len);
959 s.unlockBuffer(len - 2);
960 }
961 return s;
962}
963
964
Glenn Kasten0f11b512014-01-31 16:18:54 -0800965void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -0800966{
967 const size_t SIZE = 256;
968 char buffer[SIZE];
969 String8 result;
970
971 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
972 result.append(buffer);
973
974 bool locked = AudioFlinger::dumpTryLock(mLock);
975 // failed to lock - AudioFlinger is probably deadlocked
976 if (!locked) {
977 result.append("\t\tCould not lock Fx mutex:\n");
978 }
979
980 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000981 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
982 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -0800983 result.append(buffer);
984
985 result.append("\t\tDescriptor:\n");
986 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
987 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
988 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
989 mDescriptor.uuid.node[2],
990 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
991 result.append(buffer);
992 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
993 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
994 mDescriptor.type.timeHiAndVersion,
995 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
996 mDescriptor.type.node[2],
997 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
998 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -0800999 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001000 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001001 mDescriptor.flags,
1002 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001003 result.append(buffer);
1004 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1005 mDescriptor.name);
1006 result.append(buffer);
1007 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1008 mDescriptor.implementor);
1009 result.append(buffer);
1010
1011 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001012 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001013 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001014 mConfig.inputCfg.buffer.frameCount,
1015 mConfig.inputCfg.samplingRate,
1016 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001017 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001018 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001019 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001020 result.append(buffer);
1021
1022 result.append("\t\t- Output configuration:\n");
1023 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001024 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001025 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001026 mConfig.outputCfg.buffer.frameCount,
1027 mConfig.outputCfg.samplingRate,
1028 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001029 mConfig.outputCfg.format,
1030 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001031 result.append(buffer);
1032
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001033 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001035 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 for (size_t i = 0; i < mHandles.size(); ++i) {
1037 EffectHandle *handle = mHandles[i];
1038 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001039 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001040 result.append(buffer);
1041 }
1042 }
1043
Eric Laurentca7cc822012-11-19 14:55:58 -08001044 write(fd, result.string(), result.length());
1045
1046 if (locked) {
1047 mLock.unlock();
1048 }
1049}
1050
1051// ----------------------------------------------------------------------------
1052// EffectHandle implementation
1053// ----------------------------------------------------------------------------
1054
1055#undef LOG_TAG
1056#define LOG_TAG "AudioFlinger::EffectHandle"
1057
1058AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1059 const sp<AudioFlinger::Client>& client,
1060 const sp<IEffectClient>& effectClient,
1061 int32_t priority)
1062 : BnEffect(),
1063 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1064 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1065{
1066 ALOGV("constructor %p", this);
1067
1068 if (client == 0) {
1069 return;
1070 }
1071 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1072 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001073 if (mCblkMemory == 0 ||
1074 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE +
1076 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001077 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001078 return;
1079 }
Glenn Kastene75da402013-11-20 13:54:52 -08001080 new(mCblk) effect_param_cblk_t();
1081 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001082}
1083
1084AudioFlinger::EffectHandle::~EffectHandle()
1085{
1086 ALOGV("Destructor %p", this);
1087
1088 if (mEffect == 0) {
1089 mDestroyed = true;
1090 return;
1091 }
1092 mEffect->lock();
1093 mDestroyed = true;
1094 mEffect->unlock();
1095 disconnect(false);
1096}
1097
Glenn Kastene75da402013-11-20 13:54:52 -08001098status_t AudioFlinger::EffectHandle::initCheck()
1099{
1100 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1101}
1102
Eric Laurentca7cc822012-11-19 14:55:58 -08001103status_t AudioFlinger::EffectHandle::enable()
1104{
1105 ALOGV("enable %p", this);
1106 if (!mHasControl) {
1107 return INVALID_OPERATION;
1108 }
1109 if (mEffect == 0) {
1110 return DEAD_OBJECT;
1111 }
1112
1113 if (mEnabled) {
1114 return NO_ERROR;
1115 }
1116
1117 mEnabled = true;
1118
1119 sp<ThreadBase> thread = mEffect->thread().promote();
1120 if (thread != 0) {
1121 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1122 }
1123
1124 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1125 if (mEffect->suspended()) {
1126 return NO_ERROR;
1127 }
1128
1129 status_t status = mEffect->setEnabled(true);
1130 if (status != NO_ERROR) {
1131 if (thread != 0) {
1132 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1133 }
1134 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001135 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001136 if (thread != 0) {
1137 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001138 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001139 Mutex::Autolock _l(t->mLock);
1140 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001141 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001142 if (!mEffect->isOffloadable()) {
1143 if (thread->type() == ThreadBase::OFFLOAD) {
1144 PlaybackThread *t = (PlaybackThread *)thread.get();
1145 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1146 }
1147 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1148 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1149 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001150 }
1151 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001152 }
1153 return status;
1154}
1155
1156status_t AudioFlinger::EffectHandle::disable()
1157{
1158 ALOGV("disable %p", this);
1159 if (!mHasControl) {
1160 return INVALID_OPERATION;
1161 }
1162 if (mEffect == 0) {
1163 return DEAD_OBJECT;
1164 }
1165
1166 if (!mEnabled) {
1167 return NO_ERROR;
1168 }
1169 mEnabled = false;
1170
1171 if (mEffect->suspended()) {
1172 return NO_ERROR;
1173 }
1174
1175 status_t status = mEffect->setEnabled(false);
1176
1177 sp<ThreadBase> thread = mEffect->thread().promote();
1178 if (thread != 0) {
1179 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001180 if (thread->type() == ThreadBase::OFFLOAD) {
1181 PlaybackThread *t = (PlaybackThread *)thread.get();
1182 Mutex::Autolock _l(t->mLock);
1183 t->broadcast_l();
1184 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001185 }
1186
1187 return status;
1188}
1189
1190void AudioFlinger::EffectHandle::disconnect()
1191{
1192 disconnect(true);
1193}
1194
1195void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1196{
1197 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1198 if (mEffect == 0) {
1199 return;
1200 }
1201 // restore suspended effects if the disconnected handle was enabled and the last one.
1202 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1203 sp<ThreadBase> thread = mEffect->thread().promote();
1204 if (thread != 0) {
1205 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1206 }
1207 }
1208
1209 // release sp on module => module destructor can be called now
1210 mEffect.clear();
1211 if (mClient != 0) {
1212 if (mCblk != NULL) {
1213 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1214 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1215 }
1216 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001217 // Client destructor must run with AudioFlinger client mutex locked
1218 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001219 mClient.clear();
1220 }
1221}
1222
1223status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1224 uint32_t cmdSize,
1225 void *pCmdData,
1226 uint32_t *replySize,
1227 void *pReplyData)
1228{
1229 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1230 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1231
1232 // only get parameter command is permitted for applications not controlling the effect
1233 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1234 return INVALID_OPERATION;
1235 }
1236 if (mEffect == 0) {
1237 return DEAD_OBJECT;
1238 }
1239 if (mClient == 0) {
1240 return INVALID_OPERATION;
1241 }
1242
1243 // handle commands that are not forwarded transparently to effect engine
1244 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1245 // No need to trylock() here as this function is executed in the binder thread serving a
1246 // particular client process: no risk to block the whole media server process or mixer
1247 // threads if we are stuck here
1248 Mutex::Autolock _l(mCblk->lock);
1249 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1250 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1251 mCblk->serverIndex = 0;
1252 mCblk->clientIndex = 0;
1253 return BAD_VALUE;
1254 }
1255 status_t status = NO_ERROR;
1256 while (mCblk->serverIndex < mCblk->clientIndex) {
1257 int reply;
1258 uint32_t rsize = sizeof(int);
1259 int *p = (int *)(mBuffer + mCblk->serverIndex);
1260 int size = *p++;
1261 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1262 ALOGW("command(): invalid parameter block size");
1263 break;
1264 }
1265 effect_param_t *param = (effect_param_t *)p;
1266 if (param->psize == 0 || param->vsize == 0) {
1267 ALOGW("command(): null parameter or value size");
1268 mCblk->serverIndex += size;
1269 continue;
1270 }
1271 uint32_t psize = sizeof(effect_param_t) +
1272 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1273 param->vsize;
1274 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1275 psize,
1276 p,
1277 &rsize,
1278 &reply);
1279 // stop at first error encountered
1280 if (ret != NO_ERROR) {
1281 status = ret;
1282 *(int *)pReplyData = reply;
1283 break;
1284 } else if (reply != NO_ERROR) {
1285 *(int *)pReplyData = reply;
1286 break;
1287 }
1288 mCblk->serverIndex += size;
1289 }
1290 mCblk->serverIndex = 0;
1291 mCblk->clientIndex = 0;
1292 return status;
1293 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1294 *(int *)pReplyData = NO_ERROR;
1295 return enable();
1296 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1297 *(int *)pReplyData = NO_ERROR;
1298 return disable();
1299 }
1300
1301 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1302}
1303
1304void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1305{
1306 ALOGV("setControl %p control %d", this, hasControl);
1307
1308 mHasControl = hasControl;
1309 mEnabled = enabled;
1310
1311 if (signal && mEffectClient != 0) {
1312 mEffectClient->controlStatusChanged(hasControl);
1313 }
1314}
1315
1316void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1317 uint32_t cmdSize,
1318 void *pCmdData,
1319 uint32_t replySize,
1320 void *pReplyData)
1321{
1322 if (mEffectClient != 0) {
1323 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1324 }
1325}
1326
1327
1328
1329void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1330{
1331 if (mEffectClient != 0) {
1332 mEffectClient->enableStatusChanged(enabled);
1333 }
1334}
1335
1336status_t AudioFlinger::EffectHandle::onTransact(
1337 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1338{
1339 return BnEffect::onTransact(code, data, reply, flags);
1340}
1341
1342
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001343void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001344{
1345 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1346
Marco Nelissenb2208842014-02-07 14:00:50 -08001347 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001348 (mClient == 0) ? getpid_cached : mClient->pid(),
1349 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001350 mHasControl ? "yes" : "no",
1351 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001352 mCblk ? mCblk->clientIndex : 0,
1353 mCblk ? mCblk->serverIndex : 0
1354 );
1355
1356 if (locked) {
1357 mCblk->lock.unlock();
1358 }
1359}
1360
1361#undef LOG_TAG
1362#define LOG_TAG "AudioFlinger::EffectChain"
1363
1364AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1365 int sessionId)
1366 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1367 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001368 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), mForceVolume(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001369{
1370 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1371 if (thread == NULL) {
1372 return;
1373 }
1374 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1375 thread->frameCount();
1376}
1377
1378AudioFlinger::EffectChain::~EffectChain()
1379{
1380 if (mOwnInBuffer) {
1381 delete mInBuffer;
1382 }
1383
1384}
1385
1386// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1387sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1388 effect_descriptor_t *descriptor)
1389{
1390 size_t size = mEffects.size();
1391
1392 for (size_t i = 0; i < size; i++) {
1393 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1394 return mEffects[i];
1395 }
1396 }
1397 return 0;
1398}
1399
1400// getEffectFromId_l() must be called with ThreadBase::mLock held
1401sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1402{
1403 size_t size = mEffects.size();
1404
1405 for (size_t i = 0; i < size; i++) {
1406 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1407 if (id == 0 || mEffects[i]->id() == id) {
1408 return mEffects[i];
1409 }
1410 }
1411 return 0;
1412}
1413
1414// getEffectFromType_l() must be called with ThreadBase::mLock held
1415sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1416 const effect_uuid_t *type)
1417{
1418 size_t size = mEffects.size();
1419
1420 for (size_t i = 0; i < size; i++) {
1421 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1422 return mEffects[i];
1423 }
1424 }
1425 return 0;
1426}
1427
1428void AudioFlinger::EffectChain::clearInputBuffer()
1429{
1430 Mutex::Autolock _l(mLock);
1431 sp<ThreadBase> thread = mThread.promote();
1432 if (thread == 0) {
1433 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1434 return;
1435 }
1436 clearInputBuffer_l(thread);
1437}
1438
1439// Must be called with EffectChain::mLock locked
1440void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1441{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001442 // TODO: This will change in the future, depending on multichannel
1443 // and sample format changes for effects.
1444 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1445 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001446 const size_t frameSize =
1447 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001448 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001449}
1450
1451// Must be called with EffectChain::mLock locked
1452void AudioFlinger::EffectChain::process_l()
1453{
1454 sp<ThreadBase> thread = mThread.promote();
1455 if (thread == 0) {
1456 ALOGW("process_l(): cannot promote mixer thread");
1457 return;
1458 }
1459 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1460 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001461 // never process effects when:
1462 // - on an OFFLOAD thread
1463 // - no more tracks are on the session and the effect tail has been rendered
1464 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001465 if (!isGlobalSession) {
1466 bool tracksOnSession = (trackCnt() != 0);
1467
1468 if (!tracksOnSession && mTailBufferCount == 0) {
1469 doProcess = false;
1470 }
1471
1472 if (activeTrackCnt() == 0) {
1473 // if no track is active and the effect tail has not been rendered,
1474 // the input buffer must be cleared here as the mixer process will not do it
1475 if (tracksOnSession || mTailBufferCount > 0) {
1476 clearInputBuffer_l(thread);
1477 if (mTailBufferCount > 0) {
1478 mTailBufferCount--;
1479 }
1480 }
1481 }
1482 }
1483
1484 size_t size = mEffects.size();
1485 if (doProcess) {
1486 for (size_t i = 0; i < size; i++) {
1487 mEffects[i]->process();
1488 }
1489 }
1490 for (size_t i = 0; i < size; i++) {
1491 mEffects[i]->updateState();
1492 }
1493}
1494
1495// addEffect_l() must be called with PlaybackThread::mLock held
1496status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1497{
1498 effect_descriptor_t desc = effect->desc();
1499 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1500
1501 Mutex::Autolock _l(mLock);
1502 effect->setChain(this);
1503 sp<ThreadBase> thread = mThread.promote();
1504 if (thread == 0) {
1505 return NO_INIT;
1506 }
1507 effect->setThread(thread);
1508
1509 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1510 // Auxiliary effects are inserted at the beginning of mEffects vector as
1511 // they are processed first and accumulated in chain input buffer
1512 mEffects.insertAt(effect, 0);
1513
1514 // the input buffer for auxiliary effect contains mono samples in
1515 // 32 bit format. This is to avoid saturation in AudoMixer
1516 // accumulation stage. Saturation is done in EffectModule::process() before
1517 // calling the process in effect engine
1518 size_t numSamples = thread->frameCount();
1519 int32_t *buffer = new int32_t[numSamples];
1520 memset(buffer, 0, numSamples * sizeof(int32_t));
1521 effect->setInBuffer((int16_t *)buffer);
1522 // auxiliary effects output samples to chain input buffer for further processing
1523 // by insert effects
1524 effect->setOutBuffer(mInBuffer);
1525 } else {
1526 // Insert effects are inserted at the end of mEffects vector as they are processed
1527 // after track and auxiliary effects.
1528 // Insert effect order as a function of indicated preference:
1529 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1530 // another effect is present
1531 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1532 // last effect claiming first position
1533 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1534 // first effect claiming last position
1535 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1536 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1537 // already present
1538
1539 size_t size = mEffects.size();
1540 size_t idx_insert = size;
1541 ssize_t idx_insert_first = -1;
1542 ssize_t idx_insert_last = -1;
1543
1544 for (size_t i = 0; i < size; i++) {
1545 effect_descriptor_t d = mEffects[i]->desc();
1546 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1547 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1548 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1549 // check invalid effect chaining combinations
1550 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1551 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1552 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1553 desc.name, d.name);
1554 return INVALID_OPERATION;
1555 }
1556 // remember position of first insert effect and by default
1557 // select this as insert position for new effect
1558 if (idx_insert == size) {
1559 idx_insert = i;
1560 }
1561 // remember position of last insert effect claiming
1562 // first position
1563 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1564 idx_insert_first = i;
1565 }
1566 // remember position of first insert effect claiming
1567 // last position
1568 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1569 idx_insert_last == -1) {
1570 idx_insert_last = i;
1571 }
1572 }
1573 }
1574
1575 // modify idx_insert from first position if needed
1576 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1577 if (idx_insert_last != -1) {
1578 idx_insert = idx_insert_last;
1579 } else {
1580 idx_insert = size;
1581 }
1582 } else {
1583 if (idx_insert_first != -1) {
1584 idx_insert = idx_insert_first + 1;
1585 }
1586 }
1587
1588 // always read samples from chain input buffer
1589 effect->setInBuffer(mInBuffer);
1590
1591 // if last effect in the chain, output samples to chain
1592 // output buffer, otherwise to chain input buffer
1593 if (idx_insert == size) {
1594 if (idx_insert != 0) {
1595 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1596 mEffects[idx_insert-1]->configure();
1597 }
1598 effect->setOutBuffer(mOutBuffer);
1599 } else {
1600 effect->setOutBuffer(mInBuffer);
1601 }
1602 mEffects.insertAt(effect, idx_insert);
1603
1604 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1605 idx_insert);
1606 }
1607 effect->configure();
1608 return NO_ERROR;
1609}
1610
1611// removeEffect_l() must be called with PlaybackThread::mLock held
1612size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1613{
1614 Mutex::Autolock _l(mLock);
1615 size_t size = mEffects.size();
1616 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1617
1618 for (size_t i = 0; i < size; i++) {
1619 if (effect == mEffects[i]) {
1620 // calling stop here will remove pre-processing effect from the audio HAL.
1621 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1622 // the middle of a read from audio HAL
1623 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1624 mEffects[i]->state() == EffectModule::STOPPING) {
1625 mEffects[i]->stop();
1626 }
1627 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1628 delete[] effect->inBuffer();
1629 } else {
1630 if (i == size - 1 && i != 0) {
1631 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1632 mEffects[i - 1]->configure();
1633 }
1634 }
1635 mEffects.removeAt(i);
1636 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1637 this, i);
1638 break;
1639 }
1640 }
1641
1642 return mEffects.size();
1643}
1644
1645// setDevice_l() must be called with PlaybackThread::mLock held
1646void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1647{
1648 size_t size = mEffects.size();
1649 for (size_t i = 0; i < size; i++) {
1650 mEffects[i]->setDevice(device);
1651 }
1652}
1653
1654// setMode_l() must be called with PlaybackThread::mLock held
1655void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1656{
1657 size_t size = mEffects.size();
1658 for (size_t i = 0; i < size; i++) {
1659 mEffects[i]->setMode(mode);
1660 }
1661}
1662
1663// setAudioSource_l() must be called with PlaybackThread::mLock held
1664void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1665{
1666 size_t size = mEffects.size();
1667 for (size_t i = 0; i < size; i++) {
1668 mEffects[i]->setAudioSource(source);
1669 }
1670}
1671
1672// setVolume_l() must be called with PlaybackThread::mLock held
1673bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1674{
1675 uint32_t newLeft = *left;
1676 uint32_t newRight = *right;
1677 bool hasControl = false;
1678 int ctrlIdx = -1;
1679 size_t size = mEffects.size();
1680
1681 // first update volume controller
1682 for (size_t i = size; i > 0; i--) {
1683 if (mEffects[i - 1]->isProcessEnabled() &&
1684 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1685 ctrlIdx = i - 1;
1686 hasControl = true;
1687 break;
1688 }
1689 }
1690
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001691 if (!isVolumeForced() && ctrlIdx == mVolumeCtrlIdx &&
1692 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001693 if (hasControl) {
1694 *left = mNewLeftVolume;
1695 *right = mNewRightVolume;
1696 }
1697 return hasControl;
1698 }
1699
1700 mVolumeCtrlIdx = ctrlIdx;
1701 mLeftVolume = newLeft;
1702 mRightVolume = newRight;
1703
1704 // second get volume update from volume controller
1705 if (ctrlIdx >= 0) {
1706 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1707 mNewLeftVolume = newLeft;
1708 mNewRightVolume = newRight;
1709 }
1710 // then indicate volume to all other effects in chain.
1711 // Pass altered volume to effects before volume controller
1712 // and requested volume to effects after controller
1713 uint32_t lVol = newLeft;
1714 uint32_t rVol = newRight;
1715
1716 for (size_t i = 0; i < size; i++) {
1717 if ((int)i == ctrlIdx) {
1718 continue;
1719 }
1720 // this also works for ctrlIdx == -1 when there is no volume controller
1721 if ((int)i > ctrlIdx) {
1722 lVol = *left;
1723 rVol = *right;
1724 }
1725 mEffects[i]->setVolume(&lVol, &rVol, false);
1726 }
1727 *left = newLeft;
1728 *right = newRight;
1729
1730 return hasControl;
1731}
1732
Eric Laurent1b928682014-10-02 19:41:47 -07001733void AudioFlinger::EffectChain::syncHalEffectsState()
1734{
1735 Mutex::Autolock _l(mLock);
1736 for (size_t i = 0; i < mEffects.size(); i++) {
1737 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1738 mEffects[i]->state() == EffectModule::STOPPING) {
1739 mEffects[i]->addEffectToHal_l();
1740 }
1741 }
1742}
1743
Eric Laurentca7cc822012-11-19 14:55:58 -08001744void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1745{
1746 const size_t SIZE = 256;
1747 char buffer[SIZE];
1748 String8 result;
1749
Marco Nelissenb2208842014-02-07 14:00:50 -08001750 size_t numEffects = mEffects.size();
1751 snprintf(buffer, SIZE, " %d effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 result.append(buffer);
1753
Marco Nelissenb2208842014-02-07 14:00:50 -08001754 if (numEffects) {
1755 bool locked = AudioFlinger::dumpTryLock(mLock);
1756 // failed to lock - AudioFlinger is probably deadlocked
1757 if (!locked) {
1758 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001759 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001760
Marco Nelissenb2208842014-02-07 14:00:50 -08001761 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001762 snprintf(buffer, SIZE, "\t%p %p %d\n",
1763 mInBuffer,
1764 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001765 mActiveTrackCnt);
1766 result.append(buffer);
1767 write(fd, result.string(), result.size());
1768
1769 for (size_t i = 0; i < numEffects; ++i) {
1770 sp<EffectModule> effect = mEffects[i];
1771 if (effect != 0) {
1772 effect->dump(fd, args);
1773 }
1774 }
1775
1776 if (locked) {
1777 mLock.unlock();
1778 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001779 }
1780}
1781
1782// must be called with ThreadBase::mLock held
1783void AudioFlinger::EffectChain::setEffectSuspended_l(
1784 const effect_uuid_t *type, bool suspend)
1785{
1786 sp<SuspendedEffectDesc> desc;
1787 // use effect type UUID timelow as key as there is no real risk of identical
1788 // timeLow fields among effect type UUIDs.
1789 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1790 if (suspend) {
1791 if (index >= 0) {
1792 desc = mSuspendedEffects.valueAt(index);
1793 } else {
1794 desc = new SuspendedEffectDesc();
1795 desc->mType = *type;
1796 mSuspendedEffects.add(type->timeLow, desc);
1797 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1798 }
1799 if (desc->mRefCount++ == 0) {
1800 sp<EffectModule> effect = getEffectIfEnabled(type);
1801 if (effect != 0) {
1802 desc->mEffect = effect;
1803 effect->setSuspended(true);
1804 effect->setEnabled(false);
1805 }
1806 }
1807 } else {
1808 if (index < 0) {
1809 return;
1810 }
1811 desc = mSuspendedEffects.valueAt(index);
1812 if (desc->mRefCount <= 0) {
1813 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1814 desc->mRefCount = 1;
1815 }
1816 if (--desc->mRefCount == 0) {
1817 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1818 if (desc->mEffect != 0) {
1819 sp<EffectModule> effect = desc->mEffect.promote();
1820 if (effect != 0) {
1821 effect->setSuspended(false);
1822 effect->lock();
1823 EffectHandle *handle = effect->controlHandle_l();
1824 if (handle != NULL && !handle->destroyed_l()) {
1825 effect->setEnabled_l(handle->enabled());
1826 }
1827 effect->unlock();
1828 }
1829 desc->mEffect.clear();
1830 }
1831 mSuspendedEffects.removeItemsAt(index);
1832 }
1833 }
1834}
1835
1836// must be called with ThreadBase::mLock held
1837void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1838{
1839 sp<SuspendedEffectDesc> desc;
1840
1841 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1842 if (suspend) {
1843 if (index >= 0) {
1844 desc = mSuspendedEffects.valueAt(index);
1845 } else {
1846 desc = new SuspendedEffectDesc();
1847 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1848 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1849 }
1850 if (desc->mRefCount++ == 0) {
1851 Vector< sp<EffectModule> > effects;
1852 getSuspendEligibleEffects(effects);
1853 for (size_t i = 0; i < effects.size(); i++) {
1854 setEffectSuspended_l(&effects[i]->desc().type, true);
1855 }
1856 }
1857 } else {
1858 if (index < 0) {
1859 return;
1860 }
1861 desc = mSuspendedEffects.valueAt(index);
1862 if (desc->mRefCount <= 0) {
1863 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1864 desc->mRefCount = 1;
1865 }
1866 if (--desc->mRefCount == 0) {
1867 Vector<const effect_uuid_t *> types;
1868 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1869 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1870 continue;
1871 }
1872 types.add(&mSuspendedEffects.valueAt(i)->mType);
1873 }
1874 for (size_t i = 0; i < types.size(); i++) {
1875 setEffectSuspended_l(types[i], false);
1876 }
1877 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1878 mSuspendedEffects.keyAt(index));
1879 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1880 }
1881 }
1882}
1883
1884
1885// The volume effect is used for automated tests only
1886#ifndef OPENSL_ES_H_
1887static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1888 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1889const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1890#endif //OPENSL_ES_H_
1891
1892bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1893{
1894 // auxiliary effects and visualizer are never suspended on output mix
1895 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1896 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1897 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1898 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1899 return false;
1900 }
1901 return true;
1902}
1903
1904void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1905 Vector< sp<AudioFlinger::EffectModule> > &effects)
1906{
1907 effects.clear();
1908 for (size_t i = 0; i < mEffects.size(); i++) {
1909 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1910 effects.add(mEffects[i]);
1911 }
1912 }
1913}
1914
1915sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1916 const effect_uuid_t *type)
1917{
1918 sp<EffectModule> effect = getEffectFromType_l(type);
1919 return effect != 0 && effect->isEnabled() ? effect : 0;
1920}
1921
1922void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1923 bool enabled)
1924{
1925 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1926 if (enabled) {
1927 if (index < 0) {
1928 // if the effect is not suspend check if all effects are suspended
1929 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1930 if (index < 0) {
1931 return;
1932 }
1933 if (!isEffectEligibleForSuspend(effect->desc())) {
1934 return;
1935 }
1936 setEffectSuspended_l(&effect->desc().type, enabled);
1937 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1938 if (index < 0) {
1939 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1940 return;
1941 }
1942 }
1943 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1944 effect->desc().type.timeLow);
1945 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1946 // if effect is requested to suspended but was not yet enabled, supend it now.
1947 if (desc->mEffect == 0) {
1948 desc->mEffect = effect;
1949 effect->setEnabled(false);
1950 effect->setSuspended(true);
1951 }
1952 } else {
1953 if (index < 0) {
1954 return;
1955 }
1956 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1957 effect->desc().type.timeLow);
1958 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1959 desc->mEffect.clear();
1960 effect->setSuspended(false);
1961 }
1962}
1963
Eric Laurent5baf2af2013-09-12 17:37:00 -07001964bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001965{
1966 Mutex::Autolock _l(mLock);
1967 size_t size = mEffects.size();
1968 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001969 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001970 return true;
1971 }
1972 }
1973 return false;
1974}
1975
Eric Laurentaaa44472014-09-12 17:41:50 -07001976void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
1977{
1978 Mutex::Autolock _l(mLock);
1979 mThread = thread;
1980 for (size_t i = 0; i < mEffects.size(); i++) {
1981 mEffects[i]->setThread(thread);
1982 }
1983}
1984
Eric Laurentca7cc822012-11-19 14:55:58 -08001985}; // namespace android