blob: 9e06358fa9a03b627dede029a24cd459b6033342 [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 Laurentb47a5ab2016-12-01 15:28:29 -08001148 if (cmdCode == EFFECT_CMD_ENABLE) {
1149 if (*replySize < sizeof(int)) {
1150 android_errorWriteLog(0x534e4554, "32095713");
1151 return BAD_VALUE;
1152 }
1153 *(int *)pReplyData = NO_ERROR;
1154 *replySize = sizeof(int);
1155 return enable();
1156 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1157 if (*replySize < sizeof(int)) {
1158 android_errorWriteLog(0x534e4554, "32095713");
1159 return BAD_VALUE;
1160 }
1161 *(int *)pReplyData = NO_ERROR;
1162 *replySize = sizeof(int);
1163 return disable();
1164 }
1165
1166 AutoMutex _l(mLock);
1167 sp<EffectModule> effect = mEffect.promote();
1168 if (effect == 0 || mDisconnected) {
1169 return DEAD_OBJECT;
1170 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001171 // only get parameter command is permitted for applications not controlling the effect
1172 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1173 return INVALID_OPERATION;
1174 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001175 if (mClient == 0) {
1176 return INVALID_OPERATION;
1177 }
1178
1179 // handle commands that are not forwarded transparently to effect engine
1180 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001181 if (*replySize < sizeof(int)) {
1182 android_errorWriteLog(0x534e4554, "32095713");
1183 return BAD_VALUE;
1184 }
1185 *(int *)pReplyData = NO_ERROR;
1186 *replySize = sizeof(int);
1187
Eric Laurentca7cc822012-11-19 14:55:58 -08001188 // No need to trylock() here as this function is executed in the binder thread serving a
1189 // particular client process: no risk to block the whole media server process or mixer
1190 // threads if we are stuck here
1191 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001192 // keep local copy of index in case of client corruption b/32220769
1193 const uint32_t clientIndex = mCblk->clientIndex;
1194 const uint32_t serverIndex = mCblk->serverIndex;
1195 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1196 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001197 mCblk->serverIndex = 0;
1198 mCblk->clientIndex = 0;
1199 return BAD_VALUE;
1200 }
1201 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001202 effect_param_t *param = NULL;
1203 for (uint32_t index = serverIndex; index < clientIndex;) {
1204 int *p = (int *)(mBuffer + index);
1205 const int size = *p++;
1206 if (size < 0
1207 || size > EFFECT_PARAM_BUFFER_SIZE
1208 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001209 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001210 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001211 break;
1212 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001213
1214 // copy to local memory in case of client corruption b/32220769
1215 param = (effect_param_t *)realloc(param, size);
1216 if (param == NULL) {
1217 ALOGW("command(): out of memory");
1218 status = NO_MEMORY;
1219 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001220 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001221 memcpy(param, p, size);
1222
1223 int reply = 0;
1224 uint32_t rsize = sizeof(reply);
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001225 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001226 size,
1227 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001228 &rsize,
1229 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001230
1231 // verify shared memory: server index shouldn't change; client index can't go back.
1232 if (serverIndex != mCblk->serverIndex
1233 || clientIndex > mCblk->clientIndex) {
1234 android_errorWriteLog(0x534e4554, "32220769");
1235 status = BAD_VALUE;
1236 break;
1237 }
1238
Eric Laurentca7cc822012-11-19 14:55:58 -08001239 // stop at first error encountered
1240 if (ret != NO_ERROR) {
1241 status = ret;
1242 *(int *)pReplyData = reply;
1243 break;
1244 } else if (reply != NO_ERROR) {
1245 *(int *)pReplyData = reply;
1246 break;
1247 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001248 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001249 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001250 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001251 mCblk->serverIndex = 0;
1252 mCblk->clientIndex = 0;
1253 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001254 }
1255
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001256 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001257}
1258
1259void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1260{
1261 ALOGV("setControl %p control %d", this, hasControl);
1262
1263 mHasControl = hasControl;
1264 mEnabled = enabled;
1265
1266 if (signal && mEffectClient != 0) {
1267 mEffectClient->controlStatusChanged(hasControl);
1268 }
1269}
1270
1271void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1272 uint32_t cmdSize,
1273 void *pCmdData,
1274 uint32_t replySize,
1275 void *pReplyData)
1276{
1277 if (mEffectClient != 0) {
1278 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1279 }
1280}
1281
1282
1283
1284void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1285{
1286 if (mEffectClient != 0) {
1287 mEffectClient->enableStatusChanged(enabled);
1288 }
1289}
1290
1291status_t AudioFlinger::EffectHandle::onTransact(
1292 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1293{
1294 return BnEffect::onTransact(code, data, reply, flags);
1295}
1296
1297
1298void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
1299{
1300 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1301
1302 snprintf(buffer, size, "\t\t\t%05d %05d %01u %01u %05u %05u\n",
1303 (mClient == 0) ? getpid_cached : mClient->pid(),
1304 mPriority,
1305 mHasControl,
1306 !locked,
1307 mCblk ? mCblk->clientIndex : 0,
1308 mCblk ? mCblk->serverIndex : 0
1309 );
1310
1311 if (locked) {
1312 mCblk->lock.unlock();
1313 }
1314}
1315
1316#undef LOG_TAG
1317#define LOG_TAG "AudioFlinger::EffectChain"
1318
1319AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1320 int sessionId)
1321 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1322 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
1323 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
1324{
1325 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1326 if (thread == NULL) {
1327 return;
1328 }
1329 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1330 thread->frameCount();
1331}
1332
1333AudioFlinger::EffectChain::~EffectChain()
1334{
1335 if (mOwnInBuffer) {
1336 delete mInBuffer;
1337 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001338}
1339
1340// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1341sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1342 effect_descriptor_t *descriptor)
1343{
1344 size_t size = mEffects.size();
1345
1346 for (size_t i = 0; i < size; i++) {
1347 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1348 return mEffects[i];
1349 }
1350 }
1351 return 0;
1352}
1353
1354// getEffectFromId_l() must be called with ThreadBase::mLock held
1355sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1356{
1357 size_t size = mEffects.size();
1358
1359 for (size_t i = 0; i < size; i++) {
1360 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1361 if (id == 0 || mEffects[i]->id() == id) {
1362 return mEffects[i];
1363 }
1364 }
1365 return 0;
1366}
1367
1368// getEffectFromType_l() must be called with ThreadBase::mLock held
1369sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1370 const effect_uuid_t *type)
1371{
1372 size_t size = mEffects.size();
1373
1374 for (size_t i = 0; i < size; i++) {
1375 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1376 return mEffects[i];
1377 }
1378 }
1379 return 0;
1380}
1381
1382void AudioFlinger::EffectChain::clearInputBuffer()
1383{
1384 Mutex::Autolock _l(mLock);
1385 sp<ThreadBase> thread = mThread.promote();
1386 if (thread == 0) {
1387 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1388 return;
1389 }
1390 clearInputBuffer_l(thread);
1391}
1392
1393// Must be called with EffectChain::mLock locked
1394void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1395{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001396 memset(mInBuffer, 0, thread->frameCount() * thread->frameSize());
Eric Laurentca7cc822012-11-19 14:55:58 -08001397}
1398
1399// Must be called with EffectChain::mLock locked
1400void AudioFlinger::EffectChain::process_l()
1401{
1402 sp<ThreadBase> thread = mThread.promote();
1403 if (thread == 0) {
1404 ALOGW("process_l(): cannot promote mixer thread");
1405 return;
1406 }
1407 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1408 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001409 // never process effects when:
1410 // - on an OFFLOAD thread
1411 // - no more tracks are on the session and the effect tail has been rendered
1412 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001413 if (!isGlobalSession) {
1414 bool tracksOnSession = (trackCnt() != 0);
1415
1416 if (!tracksOnSession && mTailBufferCount == 0) {
1417 doProcess = false;
1418 }
1419
1420 if (activeTrackCnt() == 0) {
1421 // if no track is active and the effect tail has not been rendered,
1422 // the input buffer must be cleared here as the mixer process will not do it
1423 if (tracksOnSession || mTailBufferCount > 0) {
1424 clearInputBuffer_l(thread);
1425 if (mTailBufferCount > 0) {
1426 mTailBufferCount--;
1427 }
1428 }
1429 }
1430 }
1431
1432 size_t size = mEffects.size();
1433 if (doProcess) {
1434 for (size_t i = 0; i < size; i++) {
1435 mEffects[i]->process();
1436 }
1437 }
1438 for (size_t i = 0; i < size; i++) {
1439 mEffects[i]->updateState();
1440 }
1441}
1442
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001443// createEffect_l() must be called with ThreadBase::mLock held
1444status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1445 ThreadBase *thread,
1446 effect_descriptor_t *desc,
1447 int id,
1448 int sessionId,
1449 bool pinned)
1450{
1451 Mutex::Autolock _l(mLock);
1452 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1453 status_t lStatus = effect->status();
1454 if (lStatus == NO_ERROR) {
1455 lStatus = addEffect_ll(effect);
1456 }
1457 if (lStatus != NO_ERROR) {
1458 effect.clear();
1459 }
1460 return lStatus;
1461}
1462
1463// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001464status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1465{
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001466 Mutex::Autolock _l(mLock);
1467 return addEffect_ll(effect);
1468}
1469// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1470status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1471{
Eric Laurentca7cc822012-11-19 14:55:58 -08001472 effect_descriptor_t desc = effect->desc();
1473 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1474
Eric Laurentca7cc822012-11-19 14:55:58 -08001475 effect->setChain(this);
1476 sp<ThreadBase> thread = mThread.promote();
1477 if (thread == 0) {
1478 return NO_INIT;
1479 }
1480 effect->setThread(thread);
1481
1482 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1483 // Auxiliary effects are inserted at the beginning of mEffects vector as
1484 // they are processed first and accumulated in chain input buffer
1485 mEffects.insertAt(effect, 0);
1486
1487 // the input buffer for auxiliary effect contains mono samples in
1488 // 32 bit format. This is to avoid saturation in AudoMixer
1489 // accumulation stage. Saturation is done in EffectModule::process() before
1490 // calling the process in effect engine
1491 size_t numSamples = thread->frameCount();
1492 int32_t *buffer = new int32_t[numSamples];
1493 memset(buffer, 0, numSamples * sizeof(int32_t));
1494 effect->setInBuffer((int16_t *)buffer);
1495 // auxiliary effects output samples to chain input buffer for further processing
1496 // by insert effects
1497 effect->setOutBuffer(mInBuffer);
1498 } else {
1499 // Insert effects are inserted at the end of mEffects vector as they are processed
1500 // after track and auxiliary effects.
1501 // Insert effect order as a function of indicated preference:
1502 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1503 // another effect is present
1504 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1505 // last effect claiming first position
1506 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1507 // first effect claiming last position
1508 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1509 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1510 // already present
1511
1512 size_t size = mEffects.size();
1513 size_t idx_insert = size;
1514 ssize_t idx_insert_first = -1;
1515 ssize_t idx_insert_last = -1;
1516
1517 for (size_t i = 0; i < size; i++) {
1518 effect_descriptor_t d = mEffects[i]->desc();
1519 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1520 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1521 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1522 // check invalid effect chaining combinations
1523 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1524 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1525 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1526 desc.name, d.name);
1527 return INVALID_OPERATION;
1528 }
1529 // remember position of first insert effect and by default
1530 // select this as insert position for new effect
1531 if (idx_insert == size) {
1532 idx_insert = i;
1533 }
1534 // remember position of last insert effect claiming
1535 // first position
1536 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1537 idx_insert_first = i;
1538 }
1539 // remember position of first insert effect claiming
1540 // last position
1541 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1542 idx_insert_last == -1) {
1543 idx_insert_last = i;
1544 }
1545 }
1546 }
1547
1548 // modify idx_insert from first position if needed
1549 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1550 if (idx_insert_last != -1) {
1551 idx_insert = idx_insert_last;
1552 } else {
1553 idx_insert = size;
1554 }
1555 } else {
1556 if (idx_insert_first != -1) {
1557 idx_insert = idx_insert_first + 1;
1558 }
1559 }
1560
1561 // always read samples from chain input buffer
1562 effect->setInBuffer(mInBuffer);
1563
1564 // if last effect in the chain, output samples to chain
1565 // output buffer, otherwise to chain input buffer
1566 if (idx_insert == size) {
1567 if (idx_insert != 0) {
1568 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1569 mEffects[idx_insert-1]->configure();
1570 }
1571 effect->setOutBuffer(mOutBuffer);
1572 } else {
1573 effect->setOutBuffer(mInBuffer);
1574 }
1575 mEffects.insertAt(effect, idx_insert);
1576
1577 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1578 idx_insert);
1579 }
1580 effect->configure();
1581 return NO_ERROR;
1582}
1583
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001584// removeEffect_l() must be called with ThreadBase::mLock held
1585size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1586 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001587{
1588 Mutex::Autolock _l(mLock);
1589 size_t size = mEffects.size();
1590 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1591
1592 for (size_t i = 0; i < size; i++) {
1593 if (effect == mEffects[i]) {
1594 // calling stop here will remove pre-processing effect from the audio HAL.
1595 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1596 // the middle of a read from audio HAL
1597 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1598 mEffects[i]->state() == EffectModule::STOPPING) {
1599 mEffects[i]->stop();
1600 }
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001601 if (release) {
1602 mEffects[i]->release_l();
1603 }
1604
Eric Laurentca7cc822012-11-19 14:55:58 -08001605 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1606 delete[] effect->inBuffer();
1607 } else {
1608 if (i == size - 1 && i != 0) {
1609 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1610 mEffects[i - 1]->configure();
1611 }
1612 }
1613 mEffects.removeAt(i);
1614 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1615 this, i);
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001616
Eric Laurentca7cc822012-11-19 14:55:58 -08001617 break;
1618 }
1619 }
1620
1621 return mEffects.size();
1622}
1623
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001624// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001625void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1626{
1627 size_t size = mEffects.size();
1628 for (size_t i = 0; i < size; i++) {
1629 mEffects[i]->setDevice(device);
1630 }
1631}
1632
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001633// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001634void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1635{
1636 size_t size = mEffects.size();
1637 for (size_t i = 0; i < size; i++) {
1638 mEffects[i]->setMode(mode);
1639 }
1640}
1641
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001642// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001643void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1644{
1645 size_t size = mEffects.size();
1646 for (size_t i = 0; i < size; i++) {
1647 mEffects[i]->setAudioSource(source);
1648 }
1649}
1650
1651// setVolume_l() must be called with PlaybackThread::mLock held
1652bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1653{
1654 uint32_t newLeft = *left;
1655 uint32_t newRight = *right;
1656 bool hasControl = false;
1657 int ctrlIdx = -1;
1658 size_t size = mEffects.size();
1659
1660 // first update volume controller
1661 for (size_t i = size; i > 0; i--) {
1662 if (mEffects[i - 1]->isProcessEnabled() &&
1663 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1664 ctrlIdx = i - 1;
1665 hasControl = true;
1666 break;
1667 }
1668 }
1669
1670 if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
1671 if (hasControl) {
1672 *left = mNewLeftVolume;
1673 *right = mNewRightVolume;
1674 }
1675 return hasControl;
1676 }
1677
1678 mVolumeCtrlIdx = ctrlIdx;
1679 mLeftVolume = newLeft;
1680 mRightVolume = newRight;
1681
1682 // second get volume update from volume controller
1683 if (ctrlIdx >= 0) {
1684 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1685 mNewLeftVolume = newLeft;
1686 mNewRightVolume = newRight;
1687 }
1688 // then indicate volume to all other effects in chain.
1689 // Pass altered volume to effects before volume controller
1690 // and requested volume to effects after controller
1691 uint32_t lVol = newLeft;
1692 uint32_t rVol = newRight;
1693
1694 for (size_t i = 0; i < size; i++) {
1695 if ((int)i == ctrlIdx) {
1696 continue;
1697 }
1698 // this also works for ctrlIdx == -1 when there is no volume controller
1699 if ((int)i > ctrlIdx) {
1700 lVol = *left;
1701 rVol = *right;
1702 }
1703 mEffects[i]->setVolume(&lVol, &rVol, false);
1704 }
1705 *left = newLeft;
1706 *right = newRight;
1707
1708 return hasControl;
1709}
1710
1711void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1712{
1713 const size_t SIZE = 256;
1714 char buffer[SIZE];
1715 String8 result;
1716
1717 snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
1718 result.append(buffer);
1719
1720 bool locked = AudioFlinger::dumpTryLock(mLock);
1721 // failed to lock - AudioFlinger is probably deadlocked
1722 if (!locked) {
1723 result.append("\tCould not lock mutex:\n");
1724 }
1725
1726 result.append("\tNum fx In buffer Out buffer Active tracks:\n");
1727 snprintf(buffer, SIZE, "\t%02d 0x%08x 0x%08x %d\n",
1728 mEffects.size(),
1729 (uint32_t)mInBuffer,
1730 (uint32_t)mOutBuffer,
1731 mActiveTrackCnt);
1732 result.append(buffer);
1733 write(fd, result.string(), result.size());
1734
1735 for (size_t i = 0; i < mEffects.size(); ++i) {
1736 sp<EffectModule> effect = mEffects[i];
1737 if (effect != 0) {
1738 effect->dump(fd, args);
1739 }
1740 }
1741
1742 if (locked) {
1743 mLock.unlock();
1744 }
1745}
1746
1747// must be called with ThreadBase::mLock held
1748void AudioFlinger::EffectChain::setEffectSuspended_l(
1749 const effect_uuid_t *type, bool suspend)
1750{
1751 sp<SuspendedEffectDesc> desc;
1752 // use effect type UUID timelow as key as there is no real risk of identical
1753 // timeLow fields among effect type UUIDs.
1754 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1755 if (suspend) {
1756 if (index >= 0) {
1757 desc = mSuspendedEffects.valueAt(index);
1758 } else {
1759 desc = new SuspendedEffectDesc();
1760 desc->mType = *type;
1761 mSuspendedEffects.add(type->timeLow, desc);
1762 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1763 }
1764 if (desc->mRefCount++ == 0) {
1765 sp<EffectModule> effect = getEffectIfEnabled(type);
1766 if (effect != 0) {
1767 desc->mEffect = effect;
1768 effect->setSuspended(true);
1769 effect->setEnabled(false);
1770 }
1771 }
1772 } else {
1773 if (index < 0) {
1774 return;
1775 }
1776 desc = mSuspendedEffects.valueAt(index);
1777 if (desc->mRefCount <= 0) {
1778 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1779 desc->mRefCount = 1;
1780 }
1781 if (--desc->mRefCount == 0) {
1782 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1783 if (desc->mEffect != 0) {
1784 sp<EffectModule> effect = desc->mEffect.promote();
1785 if (effect != 0) {
1786 effect->setSuspended(false);
1787 effect->lock();
1788 EffectHandle *handle = effect->controlHandle_l();
Eric Laurentb47a5ab2016-12-01 15:28:29 -08001789 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001790 effect->setEnabled_l(handle->enabled());
1791 }
1792 effect->unlock();
1793 }
1794 desc->mEffect.clear();
1795 }
1796 mSuspendedEffects.removeItemsAt(index);
1797 }
1798 }
1799}
1800
1801// must be called with ThreadBase::mLock held
1802void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1803{
1804 sp<SuspendedEffectDesc> desc;
1805
1806 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1807 if (suspend) {
1808 if (index >= 0) {
1809 desc = mSuspendedEffects.valueAt(index);
1810 } else {
1811 desc = new SuspendedEffectDesc();
1812 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1813 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1814 }
1815 if (desc->mRefCount++ == 0) {
1816 Vector< sp<EffectModule> > effects;
1817 getSuspendEligibleEffects(effects);
1818 for (size_t i = 0; i < effects.size(); i++) {
1819 setEffectSuspended_l(&effects[i]->desc().type, true);
1820 }
1821 }
1822 } else {
1823 if (index < 0) {
1824 return;
1825 }
1826 desc = mSuspendedEffects.valueAt(index);
1827 if (desc->mRefCount <= 0) {
1828 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1829 desc->mRefCount = 1;
1830 }
1831 if (--desc->mRefCount == 0) {
1832 Vector<const effect_uuid_t *> types;
1833 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1834 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1835 continue;
1836 }
1837 types.add(&mSuspendedEffects.valueAt(i)->mType);
1838 }
1839 for (size_t i = 0; i < types.size(); i++) {
1840 setEffectSuspended_l(types[i], false);
1841 }
1842 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1843 mSuspendedEffects.keyAt(index));
1844 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1845 }
1846 }
1847}
1848
1849
1850// The volume effect is used for automated tests only
1851#ifndef OPENSL_ES_H_
1852static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1853 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1854const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1855#endif //OPENSL_ES_H_
1856
1857bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1858{
1859 // auxiliary effects and visualizer are never suspended on output mix
1860 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1861 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1862 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1863 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1864 return false;
1865 }
1866 return true;
1867}
1868
1869void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1870 Vector< sp<AudioFlinger::EffectModule> > &effects)
1871{
1872 effects.clear();
1873 for (size_t i = 0; i < mEffects.size(); i++) {
1874 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1875 effects.add(mEffects[i]);
1876 }
1877 }
1878}
1879
1880sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1881 const effect_uuid_t *type)
1882{
1883 sp<EffectModule> effect = getEffectFromType_l(type);
1884 return effect != 0 && effect->isEnabled() ? effect : 0;
1885}
1886
1887void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1888 bool enabled)
1889{
1890 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1891 if (enabled) {
1892 if (index < 0) {
1893 // if the effect is not suspend check if all effects are suspended
1894 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1895 if (index < 0) {
1896 return;
1897 }
1898 if (!isEffectEligibleForSuspend(effect->desc())) {
1899 return;
1900 }
1901 setEffectSuspended_l(&effect->desc().type, enabled);
1902 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1903 if (index < 0) {
1904 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1905 return;
1906 }
1907 }
1908 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1909 effect->desc().type.timeLow);
1910 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1911 // if effect is requested to suspended but was not yet enabled, supend it now.
1912 if (desc->mEffect == 0) {
1913 desc->mEffect = effect;
1914 effect->setEnabled(false);
1915 effect->setSuspended(true);
1916 }
1917 } else {
1918 if (index < 0) {
1919 return;
1920 }
1921 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1922 effect->desc().type.timeLow);
1923 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1924 desc->mEffect.clear();
1925 effect->setSuspended(false);
1926 }
1927}
1928
Eric Laurent5baf2af2013-09-12 17:37:00 -07001929bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001930{
1931 Mutex::Autolock _l(mLock);
1932 size_t size = mEffects.size();
1933 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001934 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001935 return true;
1936 }
1937 }
1938 return false;
1939}
1940
Eric Laurentca7cc822012-11-19 14:55:58 -08001941}; // namespace android