blob: 9e9116b9ac2bc9287b130a68b81a706315448692 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080023#include <utils/Log.h>
24#include <audio_effects/effect_visualizer.h>
25#include <audio_utils/primitives.h>
26#include <private/media/AudioEffectShared.h>
27#include <media/EffectsFactoryApi.h>
28
29#include "AudioFlinger.h"
30#include "ServiceUtilities.h"
31
32// ----------------------------------------------------------------------------
33
34// Note: the following macro is used for extremely verbose logging message. In
35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
36// 0; but one side effect of this is to turn all LOGV's as well. Some messages
37// are so verbose that we want to suppress them even when we have ALOG_ASSERT
38// turned on. Do not uncomment the #def below unless you really know what you
39// are doing and want to see all of the extremely verbose messages.
40//#define VERY_VERY_VERBOSE_LOGGING
41#ifdef VERY_VERY_VERBOSE_LOGGING
42#define ALOGVV ALOGV
43#else
44#define ALOGVV(a...) do { } while(0)
45#endif
46
47namespace android {
48
49// ----------------------------------------------------------------------------
50// EffectModule implementation
51// ----------------------------------------------------------------------------
52
53#undef LOG_TAG
54#define LOG_TAG "AudioFlinger::EffectModule"
55
56AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
57 const wp<AudioFlinger::EffectChain>& chain,
58 effect_descriptor_t *desc,
59 int id,
Eric Laurentb47a5ab2016-12-01 15:28:29 -080060 int sessionId,
61 bool pinned)
62 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080063 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
64 mDescriptor(*desc),
65 // mConfig is set by configure() and not used before then
66 mEffectInterface(NULL),
67 mStatus(NO_INIT), mState(IDLE),
68 // mMaxDisableWaitCnt is set by configure() and not used before then
69 // mDisableWaitCnt is set by process() and updateState() and not used before then
70 mSuspended(false)
71{
Eric Laurentb47a5ab2016-12-01 15:28:29 -080072 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080073 int lStatus;
74
75 // create effect engine from effect factory
76 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
77
78 if (mStatus != NO_ERROR) {
79 return;
80 }
81 lStatus = init();
82 if (lStatus < 0) {
83 mStatus = lStatus;
84 goto Error;
85 }
86
Eric Laurentb47a5ab2016-12-01 15:28:29 -080087 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
88
Eric Laurentca7cc822012-11-19 14:55:58 -080089 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 Laurentb47a5ab2016-12-01 15:28:29 -0800101 ALOGW("EffectModule %p destructor called with unreleased interface", this);
102 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800103 }
104}
105
106status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
107{
108 status_t status;
109
110 Mutex::Autolock _l(mLock);
111 int priority = handle->priority();
112 size_t size = mHandles.size();
113 EffectHandle *controlHandle = NULL;
114 size_t i;
115 for (i = 0; i < size; i++) {
116 EffectHandle *h = mHandles[i];
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800117 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800118 continue;
119 }
120 // first non destroyed handle is considered in control
121 if (controlHandle == NULL)
122 controlHandle = h;
123 if (h->priority() <= priority) {
124 break;
125 }
126 }
127 // if inserted in first place, move effect control from previous owner to this handle
128 if (i == 0) {
129 bool enabled = false;
130 if (controlHandle != NULL) {
131 enabled = controlHandle->enabled();
132 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
133 }
134 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
135 status = NO_ERROR;
136 } else {
137 status = ALREADY_EXISTS;
138 }
139 ALOGV("addHandle() %p added handle %p in position %d", this, handle, i);
140 mHandles.insertAt(handle, i);
141 return status;
142}
143
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800144ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800145{
146 Mutex::Autolock _l(mLock);
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800147 return removeHandle_l(handle);
148}
149
150ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
151{
Eric Laurentca7cc822012-11-19 14:55:58 -0800152 size_t size = mHandles.size();
153 size_t i;
154 for (i = 0; i < size; i++) {
155 if (mHandles[i] == handle) {
156 break;
157 }
158 }
159 if (i == size) {
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800160 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
161 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800162 }
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800163 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800164
165 mHandles.removeAt(i);
166 // if removed from first place, move effect control from this handle to next in line
167 if (i == 0) {
168 EffectHandle *h = controlHandle_l();
169 if (h != NULL) {
170 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
171 }
172 }
173
174 // Prevent calls to process() and other functions on effect interface from now on.
175 // The effect engine will be released by the destructor when the last strong reference on
176 // this object is released which can happen after next process is called.
177 if (mHandles.size() == 0 && !mPinned) {
178 mState = DESTROYED;
179 }
180
181 return mHandles.size();
182}
183
184// must be called with EffectModule::mLock held
185AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
186{
187 // the first valid handle in the list has control over the module
188 for (size_t i = 0; i < mHandles.size(); i++) {
189 EffectHandle *h = mHandles[i];
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800190 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800191 return h;
192 }
193 }
194
195 return NULL;
196}
197
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800198// unsafe method called when the effect parent thread has been destroyed
199ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentca7cc822012-11-19 14:55:58 -0800200{
201 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800202 Mutex::Autolock _l(mLock);
203 ssize_t numHandles = removeHandle_l(handle);
204 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
205 AudioSystem::unregisterEffect(mId);
Eric Laurentca7cc822012-11-19 14:55:58 -0800206 }
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800207 return numHandles;
Eric Laurentca7cc822012-11-19 14:55:58 -0800208}
209
210void AudioFlinger::EffectModule::updateState() {
211 Mutex::Autolock _l(mLock);
212
213 switch (mState) {
214 case RESTART:
215 reset_l();
216 // FALL THROUGH
217
218 case STARTING:
219 // clear auxiliary effect input buffer for next accumulation
220 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
221 memset(mConfig.inputCfg.buffer.raw,
222 0,
223 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
224 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700225 if (start_l() == NO_ERROR) {
226 mState = ACTIVE;
227 } else {
228 mState = IDLE;
229 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800230 break;
231 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700232 if (stop_l() == NO_ERROR) {
233 mDisableWaitCnt = mMaxDisableWaitCnt;
234 } else {
235 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
236 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800237 mState = STOPPED;
238 break;
239 case STOPPED:
240 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
241 // turn off sequence.
242 if (--mDisableWaitCnt == 0) {
243 reset_l();
244 mState = IDLE;
245 }
246 break;
247 default: //IDLE , ACTIVE, DESTROYED
248 break;
249 }
250}
251
252void AudioFlinger::EffectModule::process()
253{
254 Mutex::Autolock _l(mLock);
255
256 if (mState == DESTROYED || mEffectInterface == NULL ||
257 mConfig.inputCfg.buffer.raw == NULL ||
258 mConfig.outputCfg.buffer.raw == NULL) {
259 return;
260 }
261
262 if (isProcessEnabled()) {
263 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
264 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
265 ditherAndClamp(mConfig.inputCfg.buffer.s32,
266 mConfig.inputCfg.buffer.s32,
267 mConfig.inputCfg.buffer.frameCount/2);
268 }
269
270 // do the actual processing in the effect engine
271 int ret = (*mEffectInterface)->process(mEffectInterface,
272 &mConfig.inputCfg.buffer,
273 &mConfig.outputCfg.buffer);
274
275 // force transition to IDLE state when engine is ready
276 if (mState == STOPPED && ret == -ENODATA) {
277 mDisableWaitCnt = 1;
278 }
279
280 // clear auxiliary effect input buffer for next accumulation
281 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
282 memset(mConfig.inputCfg.buffer.raw, 0,
283 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
284 }
285 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
286 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
287 // If an insert effect is idle and input buffer is different from output buffer,
288 // accumulate input onto output
289 sp<EffectChain> chain = mChain.promote();
290 if (chain != 0 && chain->activeTrackCnt() != 0) {
291 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
292 int16_t *in = mConfig.inputCfg.buffer.s16;
293 int16_t *out = mConfig.outputCfg.buffer.s16;
294 for (size_t i = 0; i < frameCnt; i++) {
295 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
296 }
297 }
298 }
299}
300
301void AudioFlinger::EffectModule::reset_l()
302{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700303 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800304 return;
305 }
306 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
307}
308
309status_t AudioFlinger::EffectModule::configure()
310{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700311 status_t status;
312 sp<ThreadBase> thread;
313 uint32_t size;
314 audio_channel_mask_t channelMask;
315
Eric Laurentca7cc822012-11-19 14:55:58 -0800316 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700317 status = NO_INIT;
318 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800319 }
320
Eric Laurentd0ebb532013-04-02 16:41:41 -0700321 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800322 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700323 status = DEAD_OBJECT;
324 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800325 }
326
327 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700328 channelMask = thread->channelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800329
330 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
331 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
332 } else {
333 mConfig.inputCfg.channels = channelMask;
334 }
335 mConfig.outputCfg.channels = channelMask;
336 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
337 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
338 mConfig.inputCfg.samplingRate = thread->sampleRate();
339 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
340 mConfig.inputCfg.bufferProvider.cookie = NULL;
341 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
342 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
343 mConfig.outputCfg.bufferProvider.cookie = NULL;
344 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
345 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
346 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
347 // Insert effect:
348 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
349 // always overwrites output buffer: input buffer == output buffer
350 // - in other sessions:
351 // last effect in the chain accumulates in output buffer: input buffer != output buffer
352 // other effect: overwrites output buffer: input buffer == output buffer
353 // Auxiliary effect:
354 // accumulates in output buffer: input buffer != output buffer
355 // Therefore: accumulate <=> input buffer != output buffer
356 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
357 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
358 } else {
359 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
360 }
361 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
362 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
363 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
364 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
365
366 ALOGV("configure() %p thread %p buffer %p framecount %d",
367 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
368
369 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700370 size = sizeof(int);
371 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800372 EFFECT_CMD_SET_CONFIG,
373 sizeof(effect_config_t),
374 &mConfig,
375 &size,
376 &cmdStatus);
377 if (status == 0) {
378 status = cmdStatus;
379 }
380
381 if (status == 0 &&
382 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
383 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
384 effect_param_t *p = (effect_param_t *)buf32;
385
386 p->psize = sizeof(uint32_t);
387 p->vsize = sizeof(uint32_t);
388 size = sizeof(int);
389 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
390
391 uint32_t latency = 0;
392 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
393 if (pbt != NULL) {
394 latency = pbt->latency_l();
395 }
396
397 *((int32_t *)p->data + 1)= latency;
398 (*mEffectInterface)->command(mEffectInterface,
399 EFFECT_CMD_SET_PARAM,
400 sizeof(effect_param_t) + 8,
401 &buf32,
402 &size,
403 &cmdStatus);
404 }
405
406 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
407 (1000 * mConfig.outputCfg.buffer.frameCount);
408
Eric Laurentd0ebb532013-04-02 16:41:41 -0700409exit:
410 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800411 return status;
412}
413
414status_t AudioFlinger::EffectModule::init()
415{
416 Mutex::Autolock _l(mLock);
417 if (mEffectInterface == NULL) {
418 return NO_INIT;
419 }
420 status_t cmdStatus;
421 uint32_t size = sizeof(status_t);
422 status_t status = (*mEffectInterface)->command(mEffectInterface,
423 EFFECT_CMD_INIT,
424 0,
425 NULL,
426 &size,
427 &cmdStatus);
428 if (status == 0) {
429 status = cmdStatus;
430 }
431 return status;
432}
433
434status_t AudioFlinger::EffectModule::start()
435{
436 Mutex::Autolock _l(mLock);
437 return start_l();
438}
439
440status_t AudioFlinger::EffectModule::start_l()
441{
442 if (mEffectInterface == NULL) {
443 return NO_INIT;
444 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700445 if (mStatus != NO_ERROR) {
446 return mStatus;
447 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800448 status_t cmdStatus;
449 uint32_t size = sizeof(status_t);
450 status_t status = (*mEffectInterface)->command(mEffectInterface,
451 EFFECT_CMD_ENABLE,
452 0,
453 NULL,
454 &size,
455 &cmdStatus);
456 if (status == 0) {
457 status = cmdStatus;
458 }
459 if (status == 0 &&
460 ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
461 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
462 sp<ThreadBase> thread = mThread.promote();
463 if (thread != 0) {
464 audio_stream_t *stream = thread->stream();
465 if (stream != NULL) {
466 stream->add_audio_effect(stream, mEffectInterface);
467 }
468 }
469 }
470 return status;
471}
472
473status_t AudioFlinger::EffectModule::stop()
474{
475 Mutex::Autolock _l(mLock);
476 return stop_l();
477}
478
479status_t AudioFlinger::EffectModule::stop_l()
480{
481 if (mEffectInterface == NULL) {
482 return NO_INIT;
483 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700484 if (mStatus != NO_ERROR) {
485 return mStatus;
486 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800487 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800488 uint32_t size = sizeof(status_t);
489 status_t status = (*mEffectInterface)->command(mEffectInterface,
490 EFFECT_CMD_DISABLE,
491 0,
492 NULL,
493 &size,
494 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800495 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800496 status = cmdStatus;
497 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800498 if (status == NO_ERROR) {
499 status = remove_effect_from_hal_l();
500 }
501 return status;
502}
503
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800504// must be called with EffectChain::mLock held
505void AudioFlinger::EffectModule::release_l()
506{
507 if (mEffectInterface != NULL) {
508 remove_effect_from_hal_l();
509 // release effect engine
510 EffectRelease(mEffectInterface);
511 mEffectInterface = NULL;
512 }
513}
514
Eric Laurentbfb1b832013-01-07 09:53:42 -0800515status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
516{
517 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
518 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800519 sp<ThreadBase> thread = mThread.promote();
520 if (thread != 0) {
521 audio_stream_t *stream = thread->stream();
522 if (stream != NULL) {
523 stream->remove_audio_effect(stream, mEffectInterface);
524 }
525 }
526 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800527 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800528}
529
Andy Hunge4a1d912016-08-17 14:11:13 -0700530// round up delta valid if value and divisor are positive.
531template <typename T>
532static T roundUpDelta(const T &value, const T &divisor) {
533 T remainder = value % divisor;
534 return remainder == 0 ? 0 : divisor - remainder;
535}
536
Eric Laurentca7cc822012-11-19 14:55:58 -0800537status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
538 uint32_t cmdSize,
539 void *pCmdData,
540 uint32_t *replySize,
541 void *pReplyData)
542{
543 Mutex::Autolock _l(mLock);
544 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
545
546 if (mState == DESTROYED || mEffectInterface == NULL) {
547 return NO_INIT;
548 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700549 if (mStatus != NO_ERROR) {
550 return mStatus;
551 }
Andy Hung110bc952016-06-20 15:22:52 -0700552 if (cmdCode == EFFECT_CMD_GET_PARAM &&
553 (*replySize < sizeof(effect_param_t) ||
554 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
555 android_errorWriteLog(0x534e4554, "29251553");
556 return -EINVAL;
557 }
Andy Hung3d34cc72016-11-04 19:40:53 -0700558 if (cmdCode == EFFECT_CMD_GET_PARAM &&
559 (sizeof(effect_param_t) > cmdSize ||
560 ((effect_param_t *)pCmdData)->psize > cmdSize
561 - sizeof(effect_param_t))) {
562 android_errorWriteLog(0x534e4554, "32438594");
563 return -EINVAL;
564 }
ragoe2759072016-11-22 18:02:48 -0800565 if (cmdCode == EFFECT_CMD_GET_PARAM &&
566 (sizeof(effect_param_t) > *replySize
567 || ((effect_param_t *)pCmdData)->psize > *replySize
568 - sizeof(effect_param_t)
569 || ((effect_param_t *)pCmdData)->vsize > *replySize
570 - sizeof(effect_param_t)
571 - ((effect_param_t *)pCmdData)->psize
572 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
573 *replySize
574 - sizeof(effect_param_t)
575 - ((effect_param_t *)pCmdData)->psize
576 - ((effect_param_t *)pCmdData)->vsize)) {
577 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
578 android_errorWriteLog(0x534e4554, "32705438");
579 return -EINVAL;
580 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700581 if ((cmdCode == EFFECT_CMD_SET_PARAM
582 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
583 (sizeof(effect_param_t) > cmdSize
584 || ((effect_param_t *)pCmdData)->psize > cmdSize
585 - sizeof(effect_param_t)
586 || ((effect_param_t *)pCmdData)->vsize > cmdSize
587 - sizeof(effect_param_t)
588 - ((effect_param_t *)pCmdData)->psize
589 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
590 cmdSize
591 - sizeof(effect_param_t)
592 - ((effect_param_t *)pCmdData)->psize
593 - ((effect_param_t *)pCmdData)->vsize)) {
594 android_errorWriteLog(0x534e4554, "30204301");
595 return -EINVAL;
596 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800597 status_t status = (*mEffectInterface)->command(mEffectInterface,
598 cmdCode,
599 cmdSize,
600 pCmdData,
601 replySize,
602 pReplyData);
603 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
604 uint32_t size = (replySize == NULL) ? 0 : *replySize;
605 for (size_t i = 1; i < mHandles.size(); i++) {
606 EffectHandle *h = mHandles[i];
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800607 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800608 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
609 }
610 }
611 }
612 return status;
613}
614
615status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
616{
617 Mutex::Autolock _l(mLock);
618 return setEnabled_l(enabled);
619}
620
621// must be called with EffectModule::mLock held
622status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
623{
624
625 ALOGV("setEnabled %p enabled %d", this, enabled);
626
627 if (enabled != isEnabled()) {
628 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
629 if (enabled && status != NO_ERROR) {
630 return status;
631 }
632
633 switch (mState) {
634 // going from disabled to enabled
635 case IDLE:
636 mState = STARTING;
637 break;
638 case STOPPED:
639 mState = RESTART;
640 break;
641 case STOPPING:
642 mState = ACTIVE;
643 break;
644
645 // going from enabled to disabled
646 case RESTART:
647 mState = STOPPED;
648 break;
649 case STARTING:
650 mState = IDLE;
651 break;
652 case ACTIVE:
653 mState = STOPPING;
654 break;
655 case DESTROYED:
656 return NO_ERROR; // simply ignore as we are being destroyed
657 }
658 for (size_t i = 1; i < mHandles.size(); i++) {
659 EffectHandle *h = mHandles[i];
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800660 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800661 h->setEnabled(enabled);
662 }
663 }
664 }
665 return NO_ERROR;
666}
667
668bool AudioFlinger::EffectModule::isEnabled() const
669{
670 switch (mState) {
671 case RESTART:
672 case STARTING:
673 case ACTIVE:
674 return true;
675 case IDLE:
676 case STOPPING:
677 case STOPPED:
678 case DESTROYED:
679 default:
680 return false;
681 }
682}
683
684bool AudioFlinger::EffectModule::isProcessEnabled() const
685{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700686 if (mStatus != NO_ERROR) {
687 return false;
688 }
689
Eric Laurentca7cc822012-11-19 14:55:58 -0800690 switch (mState) {
691 case RESTART:
692 case ACTIVE:
693 case STOPPING:
694 case STOPPED:
695 return true;
696 case IDLE:
697 case STARTING:
698 case DESTROYED:
699 default:
700 return false;
701 }
702}
703
704status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
705{
706 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700707 if (mStatus != NO_ERROR) {
708 return mStatus;
709 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800710 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800711 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
712 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
713 if (isProcessEnabled() &&
714 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
715 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
716 status_t cmdStatus;
717 uint32_t volume[2];
718 uint32_t *pVolume = NULL;
719 uint32_t size = sizeof(volume);
720 volume[0] = *left;
721 volume[1] = *right;
722 if (controller) {
723 pVolume = volume;
724 }
725 status = (*mEffectInterface)->command(mEffectInterface,
726 EFFECT_CMD_SET_VOLUME,
727 size,
728 volume,
729 &size,
730 pVolume);
731 if (controller && status == NO_ERROR && size == sizeof(volume)) {
732 *left = volume[0];
733 *right = volume[1];
734 }
735 }
736 return status;
737}
738
739status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
740{
741 if (device == AUDIO_DEVICE_NONE) {
742 return NO_ERROR;
743 }
744
745 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700746 if (mStatus != NO_ERROR) {
747 return mStatus;
748 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800749 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700750 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800751 status_t cmdStatus;
752 uint32_t size = sizeof(status_t);
753 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
754 EFFECT_CMD_SET_INPUT_DEVICE;
755 status = (*mEffectInterface)->command(mEffectInterface,
756 cmd,
757 sizeof(uint32_t),
758 &device,
759 &size,
760 &cmdStatus);
761 }
762 return status;
763}
764
765status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
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_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
773 status_t cmdStatus;
774 uint32_t size = sizeof(status_t);
775 status = (*mEffectInterface)->command(mEffectInterface,
776 EFFECT_CMD_SET_AUDIO_MODE,
777 sizeof(audio_mode_t),
778 &mode,
779 &size,
780 &cmdStatus);
781 if (status == NO_ERROR) {
782 status = cmdStatus;
783 }
784 }
785 return status;
786}
787
788status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
789{
790 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700791 if (mStatus != NO_ERROR) {
792 return mStatus;
793 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800794 status_t status = NO_ERROR;
795 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
796 uint32_t size = 0;
797 status = (*mEffectInterface)->command(mEffectInterface,
798 EFFECT_CMD_SET_AUDIO_SOURCE,
799 sizeof(audio_source_t),
800 &source,
801 &size,
802 NULL);
803 }
804 return status;
805}
806
807void AudioFlinger::EffectModule::setSuspended(bool suspended)
808{
809 Mutex::Autolock _l(mLock);
810 mSuspended = suspended;
811}
812
813bool AudioFlinger::EffectModule::suspended() const
814{
815 Mutex::Autolock _l(mLock);
816 return mSuspended;
817}
818
819bool AudioFlinger::EffectModule::purgeHandles()
820{
821 bool enabled = false;
822 Mutex::Autolock _l(mLock);
823 for (size_t i = 0; i < mHandles.size(); i++) {
824 EffectHandle *handle = mHandles[i];
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800825 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800826 if (handle->hasControl()) {
827 enabled = handle->enabled();
828 }
829 }
830 }
831 return enabled;
832}
833
Eric Laurent5baf2af2013-09-12 17:37:00 -0700834status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
835{
836 Mutex::Autolock _l(mLock);
837 if (mStatus != NO_ERROR) {
838 return mStatus;
839 }
840 status_t status = NO_ERROR;
841 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
842 status_t cmdStatus;
843 uint32_t size = sizeof(status_t);
844 effect_offload_param_t cmd;
845
846 cmd.isOffload = offloaded;
847 cmd.ioHandle = io;
848 status = (*mEffectInterface)->command(mEffectInterface,
849 EFFECT_CMD_OFFLOAD,
850 sizeof(effect_offload_param_t),
851 &cmd,
852 &size,
853 &cmdStatus);
854 if (status == NO_ERROR) {
855 status = cmdStatus;
856 }
857 mOffloaded = (status == NO_ERROR) ? offloaded : false;
858 } else {
859 if (offloaded) {
860 status = INVALID_OPERATION;
861 }
862 mOffloaded = false;
863 }
864 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
865 return status;
866}
867
868bool AudioFlinger::EffectModule::isOffloaded() const
869{
870 Mutex::Autolock _l(mLock);
871 return mOffloaded;
872}
873
Eric Laurentca7cc822012-11-19 14:55:58 -0800874void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
875{
876 const size_t SIZE = 256;
877 char buffer[SIZE];
878 String8 result;
879
880 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
881 result.append(buffer);
882
883 bool locked = AudioFlinger::dumpTryLock(mLock);
884 // failed to lock - AudioFlinger is probably deadlocked
885 if (!locked) {
886 result.append("\t\tCould not lock Fx mutex:\n");
887 }
888
889 result.append("\t\tSession Status State Engine:\n");
890 snprintf(buffer, SIZE, "\t\t%05d %03d %03d 0x%08x\n",
891 mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
892 result.append(buffer);
893
894 result.append("\t\tDescriptor:\n");
895 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
896 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
897 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
898 mDescriptor.uuid.node[2],
899 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
900 result.append(buffer);
901 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
902 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
903 mDescriptor.type.timeHiAndVersion,
904 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
905 mDescriptor.type.node[2],
906 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
907 result.append(buffer);
908 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
909 mDescriptor.apiVersion,
910 mDescriptor.flags);
911 result.append(buffer);
912 snprintf(buffer, SIZE, "\t\t- name: %s\n",
913 mDescriptor.name);
914 result.append(buffer);
915 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
916 mDescriptor.implementor);
917 result.append(buffer);
918
919 result.append("\t\t- Input configuration:\n");
920 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
921 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
922 (uint32_t)mConfig.inputCfg.buffer.raw,
923 mConfig.inputCfg.buffer.frameCount,
924 mConfig.inputCfg.samplingRate,
925 mConfig.inputCfg.channels,
926 mConfig.inputCfg.format);
927 result.append(buffer);
928
929 result.append("\t\t- Output configuration:\n");
930 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
931 snprintf(buffer, SIZE, "\t\t\t0x%08x %05d %05d %08x %d\n",
932 (uint32_t)mConfig.outputCfg.buffer.raw,
933 mConfig.outputCfg.buffer.frameCount,
934 mConfig.outputCfg.samplingRate,
935 mConfig.outputCfg.channels,
936 mConfig.outputCfg.format);
937 result.append(buffer);
938
939 snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
940 result.append(buffer);
941 result.append("\t\t\tPid Priority Ctrl Locked client server\n");
942 for (size_t i = 0; i < mHandles.size(); ++i) {
943 EffectHandle *handle = mHandles[i];
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800944 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800945 handle->dump(buffer, SIZE);
946 result.append(buffer);
947 }
948 }
949
950 result.append("\n");
951
952 write(fd, result.string(), result.length());
953
954 if (locked) {
955 mLock.unlock();
956 }
957}
958
959// ----------------------------------------------------------------------------
960// EffectHandle implementation
961// ----------------------------------------------------------------------------
962
963#undef LOG_TAG
964#define LOG_TAG "AudioFlinger::EffectHandle"
965
966AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
967 const sp<AudioFlinger::Client>& client,
968 const sp<IEffectClient>& effectClient,
969 int32_t priority)
970 : BnEffect(),
971 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800972 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -0800973{
974 ALOGV("constructor %p", this);
975
976 if (client == 0) {
977 return;
978 }
979 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
980 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
981 if (mCblkMemory != 0) {
982 mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
983
984 if (mCblk != NULL) {
985 new(mCblk) effect_param_cblk_t();
986 mBuffer = (uint8_t *)mCblk + bufOffset;
987 }
988 } else {
989 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE +
990 sizeof(effect_param_cblk_t));
991 return;
992 }
993}
994
995AudioFlinger::EffectHandle::~EffectHandle()
996{
997 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -0800998 disconnect(false);
999}
1000
1001status_t AudioFlinger::EffectHandle::enable()
1002{
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001003 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001004 ALOGV("enable %p", this);
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001005 sp<EffectModule> effect = mEffect.promote();
1006 if (effect == 0 || mDisconnected) {
1007 return DEAD_OBJECT;
1008 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001009 if (!mHasControl) {
1010 return INVALID_OPERATION;
1011 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001012
1013 if (mEnabled) {
1014 return NO_ERROR;
1015 }
1016
1017 mEnabled = true;
1018
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001019 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001020 if (thread != 0) {
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001021 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001022 }
1023
1024 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001025 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001026 return NO_ERROR;
1027 }
1028
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001029 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001030 if (status != NO_ERROR) {
1031 if (thread != 0) {
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001032 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001033 }
1034 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001035 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001036 if (thread != 0) {
1037 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001038 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001039 Mutex::Autolock _l(t->mLock);
1040 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001041 }
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001042 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001043 if (thread->type() == ThreadBase::OFFLOAD) {
1044 PlaybackThread *t = (PlaybackThread *)thread.get();
1045 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1046 }
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001047 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001048 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1049 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001050 }
1051 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001052 }
1053 return status;
1054}
1055
1056status_t AudioFlinger::EffectHandle::disable()
1057{
1058 ALOGV("disable %p", this);
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001059 AutoMutex _l(mLock);
1060 sp<EffectModule> effect = mEffect.promote();
1061 if (effect == 0 || mDisconnected) {
1062 return DEAD_OBJECT;
1063 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 if (!mHasControl) {
1065 return INVALID_OPERATION;
1066 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001067
1068 if (!mEnabled) {
1069 return NO_ERROR;
1070 }
1071 mEnabled = false;
1072
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001073 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001074 return NO_ERROR;
1075 }
1076
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001077 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001078
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001079 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 if (thread != 0) {
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001081 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001082 if (thread->type() == ThreadBase::OFFLOAD) {
1083 PlaybackThread *t = (PlaybackThread *)thread.get();
1084 Mutex::Autolock _l(t->mLock);
1085 t->broadcast_l();
1086 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 }
1088
1089 return status;
1090}
1091
1092void AudioFlinger::EffectHandle::disconnect()
1093{
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001094 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 disconnect(true);
1096}
1097
1098void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1099{
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001100 AutoMutex _l(mLock);
1101 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1102 if (mDisconnected) {
1103 if (unpinIfLast) {
1104 android_errorWriteLog(0x534e4554, "32707507");
1105 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001106 return;
1107 }
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001108 mDisconnected = true;
1109 sp<ThreadBase> thread;
1110 {
1111 sp<EffectModule> effect = mEffect.promote();
1112 if (effect != 0) {
1113 thread = effect->thread().promote();
1114 }
1115 }
1116 if (thread != 0) {
1117 thread->disconnectEffectHandle(this, unpinIfLast);
1118 } else {
1119 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1120 // try to cleanup as much as we can
1121 sp<EffectModule> effect = mEffect.promote();
1122 if (effect != 0) {
1123 effect->disconnectHandle(this, unpinIfLast);
Eric Laurentca7cc822012-11-19 14:55:58 -08001124 }
1125 }
1126
Eric Laurentca7cc822012-11-19 14:55:58 -08001127 if (mClient != 0) {
1128 if (mCblk != NULL) {
1129 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1130 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1131 }
1132 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
1133 // Client destructor must run with AudioFlinger mutex locked
1134 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
1135 mClient.clear();
1136 }
1137}
1138
1139status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1140 uint32_t cmdSize,
1141 void *pCmdData,
1142 uint32_t *replySize,
1143 void *pReplyData)
1144{
1145 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001146 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001147
Eric Laurent08824142017-06-15 18:43:46 -07001148 // reject commands reserved for internal use by audio framework if coming from outside
1149 // of audioserver
1150 switch(cmdCode) {
1151 case EFFECT_CMD_ENABLE:
1152 case EFFECT_CMD_DISABLE:
1153 case EFFECT_CMD_SET_PARAM:
1154 case EFFECT_CMD_SET_PARAM_DEFERRED:
1155 case EFFECT_CMD_SET_PARAM_COMMIT:
1156 case EFFECT_CMD_GET_PARAM:
1157 break;
1158 default:
1159 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1160 break;
1161 }
1162 android_errorWriteLog(0x534e4554, "62019992");
1163 return BAD_VALUE;
1164 }
1165
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001166 if (cmdCode == EFFECT_CMD_ENABLE) {
1167 if (*replySize < sizeof(int)) {
1168 android_errorWriteLog(0x534e4554, "32095713");
1169 return BAD_VALUE;
1170 }
1171 *(int *)pReplyData = NO_ERROR;
1172 *replySize = sizeof(int);
1173 return enable();
1174 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1175 if (*replySize < sizeof(int)) {
1176 android_errorWriteLog(0x534e4554, "32095713");
1177 return BAD_VALUE;
1178 }
1179 *(int *)pReplyData = NO_ERROR;
1180 *replySize = sizeof(int);
1181 return disable();
1182 }
1183
1184 AutoMutex _l(mLock);
1185 sp<EffectModule> effect = mEffect.promote();
1186 if (effect == 0 || mDisconnected) {
1187 return DEAD_OBJECT;
1188 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001189 // only get parameter command is permitted for applications not controlling the effect
1190 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1191 return INVALID_OPERATION;
1192 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001193 if (mClient == 0) {
1194 return INVALID_OPERATION;
1195 }
1196
1197 // handle commands that are not forwarded transparently to effect engine
1198 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001199 if (*replySize < sizeof(int)) {
1200 android_errorWriteLog(0x534e4554, "32095713");
1201 return BAD_VALUE;
1202 }
1203 *(int *)pReplyData = NO_ERROR;
1204 *replySize = sizeof(int);
1205
Eric Laurentca7cc822012-11-19 14:55:58 -08001206 // No need to trylock() here as this function is executed in the binder thread serving a
1207 // particular client process: no risk to block the whole media server process or mixer
1208 // threads if we are stuck here
1209 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001210 // keep local copy of index in case of client corruption b/32220769
1211 const uint32_t clientIndex = mCblk->clientIndex;
1212 const uint32_t serverIndex = mCblk->serverIndex;
1213 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1214 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001215 mCblk->serverIndex = 0;
1216 mCblk->clientIndex = 0;
1217 return BAD_VALUE;
1218 }
1219 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001220 effect_param_t *param = NULL;
1221 for (uint32_t index = serverIndex; index < clientIndex;) {
1222 int *p = (int *)(mBuffer + index);
1223 const int size = *p++;
1224 if (size < 0
1225 || size > EFFECT_PARAM_BUFFER_SIZE
1226 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001227 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001228 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001229 break;
1230 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001231
1232 // copy to local memory in case of client corruption b/32220769
1233 param = (effect_param_t *)realloc(param, size);
1234 if (param == NULL) {
1235 ALOGW("command(): out of memory");
1236 status = NO_MEMORY;
1237 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001238 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001239 memcpy(param, p, size);
1240
1241 int reply = 0;
1242 uint32_t rsize = sizeof(reply);
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001243 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001244 size,
1245 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001246 &rsize,
1247 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001248
1249 // verify shared memory: server index shouldn't change; client index can't go back.
1250 if (serverIndex != mCblk->serverIndex
1251 || clientIndex > mCblk->clientIndex) {
1252 android_errorWriteLog(0x534e4554, "32220769");
1253 status = BAD_VALUE;
1254 break;
1255 }
1256
Eric Laurentca7cc822012-11-19 14:55:58 -08001257 // stop at first error encountered
1258 if (ret != NO_ERROR) {
1259 status = ret;
1260 *(int *)pReplyData = reply;
1261 break;
1262 } else if (reply != NO_ERROR) {
1263 *(int *)pReplyData = reply;
1264 break;
1265 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001266 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001267 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001268 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001269 mCblk->serverIndex = 0;
1270 mCblk->clientIndex = 0;
1271 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001272 }
1273
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001274 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001275}
1276
1277void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1278{
1279 ALOGV("setControl %p control %d", this, hasControl);
1280
1281 mHasControl = hasControl;
1282 mEnabled = enabled;
1283
1284 if (signal && mEffectClient != 0) {
1285 mEffectClient->controlStatusChanged(hasControl);
1286 }
1287}
1288
1289void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1290 uint32_t cmdSize,
1291 void *pCmdData,
1292 uint32_t replySize,
1293 void *pReplyData)
1294{
1295 if (mEffectClient != 0) {
1296 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1297 }
1298}
1299
1300
1301
1302void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1303{
1304 if (mEffectClient != 0) {
1305 mEffectClient->enableStatusChanged(enabled);
1306 }
1307}
1308
1309status_t AudioFlinger::EffectHandle::onTransact(
1310 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1311{
1312 return BnEffect::onTransact(code, data, reply, flags);
1313}
1314
1315
1316void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
1317{
1318 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1319
1320 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
1321 (mClient == 0) ? getpid_cached : mClient->pid(),
1322 mPriority,
1323 mHasControl,
1324 !locked,
1325 mCblk ? mCblk->clientIndex : 0,
1326 mCblk ? mCblk->serverIndex : 0
1327 );
1328
1329 if (locked) {
1330 mCblk->lock.unlock();
1331 }
1332}
1333
1334#undef LOG_TAG
1335#define LOG_TAG "AudioFlinger::EffectChain"
1336
1337AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1338 int sessionId)
1339 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1340 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
1341 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
1342{
1343 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1344 if (thread == NULL) {
1345 return;
1346 }
1347 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1348 thread->frameCount();
1349}
1350
1351AudioFlinger::EffectChain::~EffectChain()
1352{
1353 if (mOwnInBuffer) {
1354 delete mInBuffer;
1355 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001356}
1357
1358// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1359sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1360 effect_descriptor_t *descriptor)
1361{
1362 size_t size = mEffects.size();
1363
1364 for (size_t i = 0; i < size; i++) {
1365 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1366 return mEffects[i];
1367 }
1368 }
1369 return 0;
1370}
1371
1372// getEffectFromId_l() must be called with ThreadBase::mLock held
1373sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1374{
1375 size_t size = mEffects.size();
1376
1377 for (size_t i = 0; i < size; i++) {
1378 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1379 if (id == 0 || mEffects[i]->id() == id) {
1380 return mEffects[i];
1381 }
1382 }
1383 return 0;
1384}
1385
1386// getEffectFromType_l() must be called with ThreadBase::mLock held
1387sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1388 const effect_uuid_t *type)
1389{
1390 size_t size = mEffects.size();
1391
1392 for (size_t i = 0; i < size; i++) {
1393 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1394 return mEffects[i];
1395 }
1396 }
1397 return 0;
1398}
1399
1400void AudioFlinger::EffectChain::clearInputBuffer()
1401{
1402 Mutex::Autolock _l(mLock);
1403 sp<ThreadBase> thread = mThread.promote();
1404 if (thread == 0) {
1405 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1406 return;
1407 }
1408 clearInputBuffer_l(thread);
1409}
1410
1411// Must be called with EffectChain::mLock locked
1412void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1413{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001414 memset(mInBuffer, 0, thread->frameCount() * thread->frameSize());
Eric Laurentca7cc822012-11-19 14:55:58 -08001415}
1416
1417// Must be called with EffectChain::mLock locked
1418void AudioFlinger::EffectChain::process_l()
1419{
1420 sp<ThreadBase> thread = mThread.promote();
1421 if (thread == 0) {
1422 ALOGW("process_l(): cannot promote mixer thread");
1423 return;
1424 }
1425 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1426 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001427 // never process effects when:
1428 // - on an OFFLOAD thread
1429 // - no more tracks are on the session and the effect tail has been rendered
1430 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 if (!isGlobalSession) {
1432 bool tracksOnSession = (trackCnt() != 0);
1433
1434 if (!tracksOnSession && mTailBufferCount == 0) {
1435 doProcess = false;
1436 }
1437
1438 if (activeTrackCnt() == 0) {
1439 // if no track is active and the effect tail has not been rendered,
1440 // the input buffer must be cleared here as the mixer process will not do it
1441 if (tracksOnSession || mTailBufferCount > 0) {
1442 clearInputBuffer_l(thread);
1443 if (mTailBufferCount > 0) {
1444 mTailBufferCount--;
1445 }
1446 }
1447 }
1448 }
1449
1450 size_t size = mEffects.size();
1451 if (doProcess) {
1452 for (size_t i = 0; i < size; i++) {
1453 mEffects[i]->process();
1454 }
1455 }
1456 for (size_t i = 0; i < size; i++) {
1457 mEffects[i]->updateState();
1458 }
1459}
1460
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001461// createEffect_l() must be called with ThreadBase::mLock held
1462status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1463 ThreadBase *thread,
1464 effect_descriptor_t *desc,
1465 int id,
1466 int sessionId,
1467 bool pinned)
1468{
1469 Mutex::Autolock _l(mLock);
1470 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1471 status_t lStatus = effect->status();
1472 if (lStatus == NO_ERROR) {
1473 lStatus = addEffect_ll(effect);
1474 }
1475 if (lStatus != NO_ERROR) {
1476 effect.clear();
1477 }
1478 return lStatus;
1479}
1480
1481// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001482status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1483{
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001484 Mutex::Autolock _l(mLock);
1485 return addEffect_ll(effect);
1486}
1487// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1488status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1489{
Eric Laurentca7cc822012-11-19 14:55:58 -08001490 effect_descriptor_t desc = effect->desc();
1491 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1492
Eric Laurentca7cc822012-11-19 14:55:58 -08001493 effect->setChain(this);
1494 sp<ThreadBase> thread = mThread.promote();
1495 if (thread == 0) {
1496 return NO_INIT;
1497 }
1498 effect->setThread(thread);
1499
1500 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1501 // Auxiliary effects are inserted at the beginning of mEffects vector as
1502 // they are processed first and accumulated in chain input buffer
1503 mEffects.insertAt(effect, 0);
1504
1505 // the input buffer for auxiliary effect contains mono samples in
1506 // 32 bit format. This is to avoid saturation in AudoMixer
1507 // accumulation stage. Saturation is done in EffectModule::process() before
1508 // calling the process in effect engine
1509 size_t numSamples = thread->frameCount();
1510 int32_t *buffer = new int32_t[numSamples];
1511 memset(buffer, 0, numSamples * sizeof(int32_t));
1512 effect->setInBuffer((int16_t *)buffer);
1513 // auxiliary effects output samples to chain input buffer for further processing
1514 // by insert effects
1515 effect->setOutBuffer(mInBuffer);
1516 } else {
1517 // Insert effects are inserted at the end of mEffects vector as they are processed
1518 // after track and auxiliary effects.
1519 // Insert effect order as a function of indicated preference:
1520 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1521 // another effect is present
1522 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1523 // last effect claiming first position
1524 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1525 // first effect claiming last position
1526 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1527 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1528 // already present
1529
1530 size_t size = mEffects.size();
1531 size_t idx_insert = size;
1532 ssize_t idx_insert_first = -1;
1533 ssize_t idx_insert_last = -1;
1534
1535 for (size_t i = 0; i < size; i++) {
1536 effect_descriptor_t d = mEffects[i]->desc();
1537 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1538 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1539 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1540 // check invalid effect chaining combinations
1541 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1542 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1543 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1544 desc.name, d.name);
1545 return INVALID_OPERATION;
1546 }
1547 // remember position of first insert effect and by default
1548 // select this as insert position for new effect
1549 if (idx_insert == size) {
1550 idx_insert = i;
1551 }
1552 // remember position of last insert effect claiming
1553 // first position
1554 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1555 idx_insert_first = i;
1556 }
1557 // remember position of first insert effect claiming
1558 // last position
1559 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1560 idx_insert_last == -1) {
1561 idx_insert_last = i;
1562 }
1563 }
1564 }
1565
1566 // modify idx_insert from first position if needed
1567 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1568 if (idx_insert_last != -1) {
1569 idx_insert = idx_insert_last;
1570 } else {
1571 idx_insert = size;
1572 }
1573 } else {
1574 if (idx_insert_first != -1) {
1575 idx_insert = idx_insert_first + 1;
1576 }
1577 }
1578
1579 // always read samples from chain input buffer
1580 effect->setInBuffer(mInBuffer);
1581
1582 // if last effect in the chain, output samples to chain
1583 // output buffer, otherwise to chain input buffer
1584 if (idx_insert == size) {
1585 if (idx_insert != 0) {
1586 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1587 mEffects[idx_insert-1]->configure();
1588 }
1589 effect->setOutBuffer(mOutBuffer);
1590 } else {
1591 effect->setOutBuffer(mInBuffer);
1592 }
1593 mEffects.insertAt(effect, idx_insert);
1594
1595 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1596 idx_insert);
1597 }
1598 effect->configure();
1599 return NO_ERROR;
1600}
1601
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001602// removeEffect_l() must be called with ThreadBase::mLock held
1603size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1604 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001605{
1606 Mutex::Autolock _l(mLock);
1607 size_t size = mEffects.size();
1608 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1609
1610 for (size_t i = 0; i < size; i++) {
1611 if (effect == mEffects[i]) {
1612 // calling stop here will remove pre-processing effect from the audio HAL.
1613 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1614 // the middle of a read from audio HAL
1615 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1616 mEffects[i]->state() == EffectModule::STOPPING) {
1617 mEffects[i]->stop();
1618 }
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001619 if (release) {
1620 mEffects[i]->release_l();
1621 }
1622
Eric Laurentca7cc822012-11-19 14:55:58 -08001623 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1624 delete[] effect->inBuffer();
1625 } else {
1626 if (i == size - 1 && i != 0) {
1627 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1628 mEffects[i - 1]->configure();
1629 }
1630 }
1631 mEffects.removeAt(i);
1632 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1633 this, i);
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001634
Eric Laurentca7cc822012-11-19 14:55:58 -08001635 break;
1636 }
1637 }
1638
1639 return mEffects.size();
1640}
1641
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001642// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001643void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1644{
1645 size_t size = mEffects.size();
1646 for (size_t i = 0; i < size; i++) {
1647 mEffects[i]->setDevice(device);
1648 }
1649}
1650
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001651// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001652void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1653{
1654 size_t size = mEffects.size();
1655 for (size_t i = 0; i < size; i++) {
1656 mEffects[i]->setMode(mode);
1657 }
1658}
1659
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001660// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001661void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1662{
1663 size_t size = mEffects.size();
1664 for (size_t i = 0; i < size; i++) {
1665 mEffects[i]->setAudioSource(source);
1666 }
1667}
1668
1669// setVolume_l() must be called with PlaybackThread::mLock held
1670bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1671{
1672 uint32_t newLeft = *left;
1673 uint32_t newRight = *right;
1674 bool hasControl = false;
1675 int ctrlIdx = -1;
1676 size_t size = mEffects.size();
1677
1678 // first update volume controller
1679 for (size_t i = size; i > 0; i--) {
1680 if (mEffects[i - 1]->isProcessEnabled() &&
1681 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1682 ctrlIdx = i - 1;
1683 hasControl = true;
1684 break;
1685 }
1686 }
1687
1688 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
1689 if (hasControl) {
1690 *left = mNewLeftVolume;
1691 *right = mNewRightVolume;
1692 }
1693 return hasControl;
1694 }
1695
1696 mVolumeCtrlIdx = ctrlIdx;
1697 mLeftVolume = newLeft;
1698 mRightVolume = newRight;
1699
1700 // second get volume update from volume controller
1701 if (ctrlIdx >= 0) {
1702 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1703 mNewLeftVolume = newLeft;
1704 mNewRightVolume = newRight;
1705 }
1706 // then indicate volume to all other effects in chain.
1707 // Pass altered volume to effects before volume controller
1708 // and requested volume to effects after controller
1709 uint32_t lVol = newLeft;
1710 uint32_t rVol = newRight;
1711
1712 for (size_t i = 0; i < size; i++) {
1713 if ((int)i == ctrlIdx) {
1714 continue;
1715 }
1716 // this also works for ctrlIdx == -1 when there is no volume controller
1717 if ((int)i > ctrlIdx) {
1718 lVol = *left;
1719 rVol = *right;
1720 }
1721 mEffects[i]->setVolume(&lVol, &rVol, false);
1722 }
1723 *left = newLeft;
1724 *right = newRight;
1725
1726 return hasControl;
1727}
1728
1729void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1730{
1731 const size_t SIZE = 256;
1732 char buffer[SIZE];
1733 String8 result;
1734
1735 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
1736 result.append(buffer);
1737
1738 bool locked = AudioFlinger::dumpTryLock(mLock);
1739 // failed to lock - AudioFlinger is probably deadlocked
1740 if (!locked) {
1741 result.append("\tCould not lock mutex:\n");
1742 }
1743
1744 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
1745 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
1746 mEffects.size(),
1747 (uint32_t)mInBuffer,
1748 (uint32_t)mOutBuffer,
1749 mActiveTrackCnt);
1750 result.append(buffer);
1751 write(fd, result.string(), result.size());
1752
1753 for (size_t i = 0; i < mEffects.size(); ++i) {
1754 sp<EffectModule> effect = mEffects[i];
1755 if (effect != 0) {
1756 effect->dump(fd, args);
1757 }
1758 }
1759
1760 if (locked) {
1761 mLock.unlock();
1762 }
1763}
1764
1765// must be called with ThreadBase::mLock held
1766void AudioFlinger::EffectChain::setEffectSuspended_l(
1767 const effect_uuid_t *type, bool suspend)
1768{
1769 sp<SuspendedEffectDesc> desc;
1770 // use effect type UUID timelow as key as there is no real risk of identical
1771 // timeLow fields among effect type UUIDs.
1772 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1773 if (suspend) {
1774 if (index >= 0) {
1775 desc = mSuspendedEffects.valueAt(index);
1776 } else {
1777 desc = new SuspendedEffectDesc();
1778 desc->mType = *type;
1779 mSuspendedEffects.add(type->timeLow, desc);
1780 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1781 }
1782 if (desc->mRefCount++ == 0) {
1783 sp<EffectModule> effect = getEffectIfEnabled(type);
1784 if (effect != 0) {
1785 desc->mEffect = effect;
1786 effect->setSuspended(true);
1787 effect->setEnabled(false);
1788 }
1789 }
1790 } else {
1791 if (index < 0) {
1792 return;
1793 }
1794 desc = mSuspendedEffects.valueAt(index);
1795 if (desc->mRefCount <= 0) {
1796 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1797 desc->mRefCount = 1;
1798 }
1799 if (--desc->mRefCount == 0) {
1800 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1801 if (desc->mEffect != 0) {
1802 sp<EffectModule> effect = desc->mEffect.promote();
1803 if (effect != 0) {
1804 effect->setSuspended(false);
1805 effect->lock();
1806 EffectHandle *handle = effect->controlHandle_l();
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001807 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001808 effect->setEnabled_l(handle->enabled());
1809 }
1810 effect->unlock();
1811 }
1812 desc->mEffect.clear();
1813 }
1814 mSuspendedEffects.removeItemsAt(index);
1815 }
1816 }
1817}
1818
1819// must be called with ThreadBase::mLock held
1820void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1821{
1822 sp<SuspendedEffectDesc> desc;
1823
1824 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1825 if (suspend) {
1826 if (index >= 0) {
1827 desc = mSuspendedEffects.valueAt(index);
1828 } else {
1829 desc = new SuspendedEffectDesc();
1830 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1831 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1832 }
1833 if (desc->mRefCount++ == 0) {
1834 Vector< sp<EffectModule> > effects;
1835 getSuspendEligibleEffects(effects);
1836 for (size_t i = 0; i < effects.size(); i++) {
1837 setEffectSuspended_l(&effects[i]->desc().type, true);
1838 }
1839 }
1840 } else {
1841 if (index < 0) {
1842 return;
1843 }
1844 desc = mSuspendedEffects.valueAt(index);
1845 if (desc->mRefCount <= 0) {
1846 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1847 desc->mRefCount = 1;
1848 }
1849 if (--desc->mRefCount == 0) {
1850 Vector<const effect_uuid_t *> types;
1851 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1852 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1853 continue;
1854 }
1855 types.add(&mSuspendedEffects.valueAt(i)->mType);
1856 }
1857 for (size_t i = 0; i < types.size(); i++) {
1858 setEffectSuspended_l(types[i], false);
1859 }
1860 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1861 mSuspendedEffects.keyAt(index));
1862 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1863 }
1864 }
1865}
1866
1867
1868// The volume effect is used for automated tests only
1869#ifndef OPENSL_ES_H_
1870static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1871 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1872const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1873#endif //OPENSL_ES_H_
1874
1875bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1876{
1877 // auxiliary effects and visualizer are never suspended on output mix
1878 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1879 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1880 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1881 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1882 return false;
1883 }
1884 return true;
1885}
1886
1887void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1888 Vector< sp<AudioFlinger::EffectModule> > &effects)
1889{
1890 effects.clear();
1891 for (size_t i = 0; i < mEffects.size(); i++) {
1892 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1893 effects.add(mEffects[i]);
1894 }
1895 }
1896}
1897
1898sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1899 const effect_uuid_t *type)
1900{
1901 sp<EffectModule> effect = getEffectFromType_l(type);
1902 return effect != 0 && effect->isEnabled() ? effect : 0;
1903}
1904
1905void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1906 bool enabled)
1907{
1908 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1909 if (enabled) {
1910 if (index < 0) {
1911 // if the effect is not suspend check if all effects are suspended
1912 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1913 if (index < 0) {
1914 return;
1915 }
1916 if (!isEffectEligibleForSuspend(effect->desc())) {
1917 return;
1918 }
1919 setEffectSuspended_l(&effect->desc().type, enabled);
1920 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1921 if (index < 0) {
1922 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1923 return;
1924 }
1925 }
1926 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1927 effect->desc().type.timeLow);
1928 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1929 // if effect is requested to suspended but was not yet enabled, supend it now.
1930 if (desc->mEffect == 0) {
1931 desc->mEffect = effect;
1932 effect->setEnabled(false);
1933 effect->setSuspended(true);
1934 }
1935 } else {
1936 if (index < 0) {
1937 return;
1938 }
1939 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1940 effect->desc().type.timeLow);
1941 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1942 desc->mEffect.clear();
1943 effect->setSuspended(false);
1944 }
1945}
1946
Eric Laurent5baf2af2013-09-12 17:37:00 -07001947bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001948{
1949 Mutex::Autolock _l(mLock);
1950 size_t size = mEffects.size();
1951 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001952 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001953 return true;
1954 }
1955 }
1956 return false;
1957}
1958
Eric Laurentca7cc822012-11-19 14:55:58 -08001959}; // namespace android