blob: 2e433448df544e6d5feba2ed96b8476766cce166 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080023#include <utils/Log.h>
24#include <audio_effects/effect_visualizer.h>
25#include <audio_utils/primitives.h>
26#include <private/media/AudioEffectShared.h>
27#include <media/EffectsFactoryApi.h>
28
29#include "AudioFlinger.h"
30#include "ServiceUtilities.h"
31
32// ----------------------------------------------------------------------------
33
34// Note: the following macro is used for extremely verbose logging message. In
35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
36// 0; but one side effect of this is to turn all LOGV's as well. Some messages
37// are so verbose that we want to suppress them even when we have ALOG_ASSERT
38// turned on. Do not uncomment the #def below unless you really know what you
39// are doing and want to see all of the extremely verbose messages.
40//#define VERY_VERY_VERBOSE_LOGGING
41#ifdef VERY_VERY_VERBOSE_LOGGING
42#define ALOGVV ALOGV
43#else
44#define ALOGVV(a...) do { } while(0)
45#endif
46
Ricardo Garcia726b6a72014-08-11 12:04:54 -070047#define min(a, b) ((a) < (b) ? (a) : (b))
48
Eric Laurentca7cc822012-11-19 14:55:58 -080049namespace android {
50
51// ----------------------------------------------------------------------------
52// EffectModule implementation
53// ----------------------------------------------------------------------------
54
55#undef LOG_TAG
56#define LOG_TAG "AudioFlinger::EffectModule"
57
58AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
59 const wp<AudioFlinger::EffectChain>& chain,
60 effect_descriptor_t *desc,
61 int id,
Eric Laurent84c39212016-12-01 15:28:29 -080062 int sessionId,
63 bool pinned)
64 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080065 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
66 mDescriptor(*desc),
67 // mConfig is set by configure() and not used before then
68 mEffectInterface(NULL),
69 mStatus(NO_INIT), mState(IDLE),
70 // mMaxDisableWaitCnt is set by configure() and not used before then
71 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070072 mSuspended(false),
73 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080074{
Eric Laurent84c39212016-12-01 15:28:29 -080075 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080076 int lStatus;
77
78 // create effect engine from effect factory
79 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
80
81 if (mStatus != NO_ERROR) {
82 return;
83 }
84 lStatus = init();
85 if (lStatus < 0) {
86 mStatus = lStatus;
87 goto Error;
88 }
89
Eric Laurent84c39212016-12-01 15:28:29 -080090 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
91
Eric Laurentca7cc822012-11-19 14:55:58 -080092 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
93 return;
94Error:
95 EffectRelease(mEffectInterface);
96 mEffectInterface = NULL;
97 ALOGV("Constructor Error %d", mStatus);
98}
99
100AudioFlinger::EffectModule::~EffectModule()
101{
102 ALOGV("Destructor %p", this);
Eric Laurent84c39212016-12-01 15:28:29 -0800103 if (mEffectInterface != 0) {
104 ALOGW("EffectModule %p destructor called with unreleased interface", this);
105 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800106 }
107}
108
109status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
110{
111 status_t status;
112
113 Mutex::Autolock _l(mLock);
114 int priority = handle->priority();
115 size_t size = mHandles.size();
116 EffectHandle *controlHandle = NULL;
117 size_t i;
118 for (i = 0; i < size; i++) {
119 EffectHandle *h = mHandles[i];
Eric Laurent84c39212016-12-01 15:28:29 -0800120 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800121 continue;
122 }
123 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700124 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800125 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700126 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800127 if (h->priority() <= priority) {
128 break;
129 }
130 }
131 // if inserted in first place, move effect control from previous owner to this handle
132 if (i == 0) {
133 bool enabled = false;
134 if (controlHandle != NULL) {
135 enabled = controlHandle->enabled();
136 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
137 }
138 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
139 status = NO_ERROR;
140 } else {
141 status = ALREADY_EXISTS;
142 }
143 ALOGV("addHandle() %p added handle %p in position %d", this, handle, i);
144 mHandles.insertAt(handle, i);
145 return status;
146}
147
Eric Laurent84c39212016-12-01 15:28:29 -0800148ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800149{
150 Mutex::Autolock _l(mLock);
Eric Laurent84c39212016-12-01 15:28:29 -0800151 return removeHandle_l(handle);
152}
153
154ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
155{
Eric Laurentca7cc822012-11-19 14:55:58 -0800156 size_t size = mHandles.size();
157 size_t i;
158 for (i = 0; i < size; i++) {
159 if (mHandles[i] == handle) {
160 break;
161 }
162 }
163 if (i == size) {
Eric Laurent84c39212016-12-01 15:28:29 -0800164 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
165 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800166 }
Eric Laurent84c39212016-12-01 15:28:29 -0800167 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800168
169 mHandles.removeAt(i);
170 // if removed from first place, move effect control from this handle to next in line
171 if (i == 0) {
172 EffectHandle *h = controlHandle_l();
173 if (h != NULL) {
174 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
175 }
176 }
177
178 // Prevent calls to process() and other functions on effect interface from now on.
179 // The effect engine will be released by the destructor when the last strong reference on
180 // this object is released which can happen after next process is called.
181 if (mHandles.size() == 0 && !mPinned) {
182 mState = DESTROYED;
183 }
184
185 return mHandles.size();
186}
187
188// must be called with EffectModule::mLock held
189AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
190{
191 // the first valid handle in the list has control over the module
192 for (size_t i = 0; i < mHandles.size(); i++) {
193 EffectHandle *h = mHandles[i];
Eric Laurent84c39212016-12-01 15:28:29 -0800194 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800195 return h;
196 }
197 }
198
199 return NULL;
200}
201
Eric Laurent84c39212016-12-01 15:28:29 -0800202// unsafe method called when the effect parent thread has been destroyed
203ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentca7cc822012-11-19 14:55:58 -0800204{
205 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent84c39212016-12-01 15:28:29 -0800206 Mutex::Autolock _l(mLock);
207 ssize_t numHandles = removeHandle_l(handle);
208 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
209 AudioSystem::unregisterEffect(mId);
210 sp<AudioFlinger> af = mAudioFlinger.promote();
211 if (af != 0) {
212 mLock.unlock();
213 af->updateOrphanEffectChains(this);
214 mLock.lock();
Eric Laurentca7cc822012-11-19 14:55:58 -0800215 }
216 }
Eric Laurent84c39212016-12-01 15:28:29 -0800217 return numHandles;
Eric Laurentca7cc822012-11-19 14:55:58 -0800218}
219
220void AudioFlinger::EffectModule::updateState() {
221 Mutex::Autolock _l(mLock);
222
223 switch (mState) {
224 case RESTART:
225 reset_l();
226 // FALL THROUGH
227
228 case STARTING:
229 // clear auxiliary effect input buffer for next accumulation
230 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
231 memset(mConfig.inputCfg.buffer.raw,
232 0,
233 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
234 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700235 if (start_l() == NO_ERROR) {
236 mState = ACTIVE;
237 } else {
238 mState = IDLE;
239 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800240 break;
241 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700242 if (stop_l() == NO_ERROR) {
243 mDisableWaitCnt = mMaxDisableWaitCnt;
244 } else {
245 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
246 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800247 mState = STOPPED;
248 break;
249 case STOPPED:
250 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
251 // turn off sequence.
252 if (--mDisableWaitCnt == 0) {
253 reset_l();
254 mState = IDLE;
255 }
256 break;
257 default: //IDLE , ACTIVE, DESTROYED
258 break;
259 }
260}
261
262void AudioFlinger::EffectModule::process()
263{
264 Mutex::Autolock _l(mLock);
265
266 if (mState == DESTROYED || mEffectInterface == NULL ||
267 mConfig.inputCfg.buffer.raw == NULL ||
268 mConfig.outputCfg.buffer.raw == NULL) {
269 return;
270 }
271
272 if (isProcessEnabled()) {
273 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
274 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
275 ditherAndClamp(mConfig.inputCfg.buffer.s32,
276 mConfig.inputCfg.buffer.s32,
277 mConfig.inputCfg.buffer.frameCount/2);
278 }
279
280 // do the actual processing in the effect engine
281 int ret = (*mEffectInterface)->process(mEffectInterface,
282 &mConfig.inputCfg.buffer,
283 &mConfig.outputCfg.buffer);
284
285 // force transition to IDLE state when engine is ready
286 if (mState == STOPPED && ret == -ENODATA) {
287 mDisableWaitCnt = 1;
288 }
289
290 // clear auxiliary effect input buffer for next accumulation
291 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
292 memset(mConfig.inputCfg.buffer.raw, 0,
293 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
294 }
295 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
296 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
297 // If an insert effect is idle and input buffer is different from output buffer,
298 // accumulate input onto output
299 sp<EffectChain> chain = mChain.promote();
300 if (chain != 0 && chain->activeTrackCnt() != 0) {
301 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
302 int16_t *in = mConfig.inputCfg.buffer.s16;
303 int16_t *out = mConfig.outputCfg.buffer.s16;
304 for (size_t i = 0; i < frameCnt; i++) {
305 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
306 }
307 }
308 }
309}
310
311void AudioFlinger::EffectModule::reset_l()
312{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700313 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 return;
315 }
316 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
317}
318
319status_t AudioFlinger::EffectModule::configure()
320{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700321 status_t status;
322 sp<ThreadBase> thread;
323 uint32_t size;
324 audio_channel_mask_t channelMask;
325
Eric Laurentca7cc822012-11-19 14:55:58 -0800326 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700327 status = NO_INIT;
328 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800329 }
330
Eric Laurentd0ebb532013-04-02 16:41:41 -0700331 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800332 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700333 status = DEAD_OBJECT;
334 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800335 }
336
337 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700338 channelMask = thread->channelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800339
340 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
341 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
342 } else {
343 mConfig.inputCfg.channels = channelMask;
344 }
345 mConfig.outputCfg.channels = channelMask;
346 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
347 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
348 mConfig.inputCfg.samplingRate = thread->sampleRate();
349 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
350 mConfig.inputCfg.bufferProvider.cookie = NULL;
351 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
352 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
353 mConfig.outputCfg.bufferProvider.cookie = NULL;
354 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
355 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
356 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
357 // Insert effect:
358 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
359 // always overwrites output buffer: input buffer == output buffer
360 // - in other sessions:
361 // last effect in the chain accumulates in output buffer: input buffer != output buffer
362 // other effect: overwrites output buffer: input buffer == output buffer
363 // Auxiliary effect:
364 // accumulates in output buffer: input buffer != output buffer
365 // Therefore: accumulate <=> input buffer != output buffer
366 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
367 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
368 } else {
369 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
370 }
371 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
372 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
373 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
374 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
375
376 ALOGV("configure() %p thread %p buffer %p framecount %d",
377 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
378
379 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700380 size = sizeof(int);
381 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800382 EFFECT_CMD_SET_CONFIG,
383 sizeof(effect_config_t),
384 &mConfig,
385 &size,
386 &cmdStatus);
387 if (status == 0) {
388 status = cmdStatus;
389 }
390
391 if (status == 0 &&
392 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
393 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
394 effect_param_t *p = (effect_param_t *)buf32;
395
396 p->psize = sizeof(uint32_t);
397 p->vsize = sizeof(uint32_t);
398 size = sizeof(int);
399 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
400
401 uint32_t latency = 0;
402 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
403 if (pbt != NULL) {
404 latency = pbt->latency_l();
405 }
406
407 *((int32_t *)p->data + 1)= latency;
408 (*mEffectInterface)->command(mEffectInterface,
409 EFFECT_CMD_SET_PARAM,
410 sizeof(effect_param_t) + 8,
411 &buf32,
412 &size,
413 &cmdStatus);
414 }
415
416 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
417 (1000 * mConfig.outputCfg.buffer.frameCount);
418
Eric Laurentd0ebb532013-04-02 16:41:41 -0700419exit:
420 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800421 return status;
422}
423
424status_t AudioFlinger::EffectModule::init()
425{
426 Mutex::Autolock _l(mLock);
427 if (mEffectInterface == NULL) {
428 return NO_INIT;
429 }
430 status_t cmdStatus;
431 uint32_t size = sizeof(status_t);
432 status_t status = (*mEffectInterface)->command(mEffectInterface,
433 EFFECT_CMD_INIT,
434 0,
435 NULL,
436 &size,
437 &cmdStatus);
438 if (status == 0) {
439 status = cmdStatus;
440 }
441 return status;
442}
443
Eric Laurent1b928682014-10-02 19:41:47 -0700444void AudioFlinger::EffectModule::addEffectToHal_l()
445{
446 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
447 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
448 sp<ThreadBase> thread = mThread.promote();
449 if (thread != 0) {
450 audio_stream_t *stream = thread->stream();
451 if (stream != NULL) {
452 stream->add_audio_effect(stream, mEffectInterface);
453 }
454 }
455 }
456}
457
Eric Laurentca7cc822012-11-19 14:55:58 -0800458status_t AudioFlinger::EffectModule::start()
459{
460 Mutex::Autolock _l(mLock);
461 return start_l();
462}
463
464status_t AudioFlinger::EffectModule::start_l()
465{
466 if (mEffectInterface == NULL) {
467 return NO_INIT;
468 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700469 if (mStatus != NO_ERROR) {
470 return mStatus;
471 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800472 status_t cmdStatus;
473 uint32_t size = sizeof(status_t);
474 status_t status = (*mEffectInterface)->command(mEffectInterface,
475 EFFECT_CMD_ENABLE,
476 0,
477 NULL,
478 &size,
479 &cmdStatus);
480 if (status == 0) {
481 status = cmdStatus;
482 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700483 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700484 addEffectToHal_l();
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700485 sp<EffectChain> chain = mChain.promote();
486 if (chain != 0) {
487 chain->forceVolume();
488 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800489 }
490 return status;
491}
492
493status_t AudioFlinger::EffectModule::stop()
494{
495 Mutex::Autolock _l(mLock);
496 return stop_l();
497}
498
499status_t AudioFlinger::EffectModule::stop_l()
500{
501 if (mEffectInterface == NULL) {
502 return NO_INIT;
503 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700504 if (mStatus != NO_ERROR) {
505 return mStatus;
506 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800507 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800508 uint32_t size = sizeof(status_t);
509 status_t status = (*mEffectInterface)->command(mEffectInterface,
510 EFFECT_CMD_DISABLE,
511 0,
512 NULL,
513 &size,
514 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800515 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800516 status = cmdStatus;
517 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800518 if (status == NO_ERROR) {
519 status = remove_effect_from_hal_l();
520 }
521 return status;
522}
523
Eric Laurent84c39212016-12-01 15:28:29 -0800524// must be called with EffectChain::mLock held
525void AudioFlinger::EffectModule::release_l()
526{
527 if (mEffectInterface != NULL) {
528 remove_effect_from_hal_l();
529 // release effect engine
530 EffectRelease(mEffectInterface);
531 mEffectInterface = NULL;
532 }
533}
534
Eric Laurentbfb1b832013-01-07 09:53:42 -0800535status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
536{
537 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
538 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800539 sp<ThreadBase> thread = mThread.promote();
540 if (thread != 0) {
541 audio_stream_t *stream = thread->stream();
542 if (stream != NULL) {
543 stream->remove_audio_effect(stream, mEffectInterface);
544 }
545 }
546 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800547 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800548}
549
Andy Hunge4a1d912016-08-17 14:11:13 -0700550// round up delta valid if value and divisor are positive.
551template <typename T>
552static T roundUpDelta(const T &value, const T &divisor) {
553 T remainder = value % divisor;
554 return remainder == 0 ? 0 : divisor - remainder;
555}
556
Eric Laurentca7cc822012-11-19 14:55:58 -0800557status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
558 uint32_t cmdSize,
559 void *pCmdData,
560 uint32_t *replySize,
561 void *pReplyData)
562{
563 Mutex::Autolock _l(mLock);
564 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
565
566 if (mState == DESTROYED || mEffectInterface == NULL) {
567 return NO_INIT;
568 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700569 if (mStatus != NO_ERROR) {
570 return mStatus;
571 }
Andy Hung110bc952016-06-20 15:22:52 -0700572 if (cmdCode == EFFECT_CMD_GET_PARAM &&
573 (*replySize < sizeof(effect_param_t) ||
574 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
575 android_errorWriteLog(0x534e4554, "29251553");
576 return -EINVAL;
577 }
Andy Hung3d34cc72016-11-04 19:40:53 -0700578 if (cmdCode == EFFECT_CMD_GET_PARAM &&
579 (sizeof(effect_param_t) > cmdSize ||
580 ((effect_param_t *)pCmdData)->psize > cmdSize
581 - sizeof(effect_param_t))) {
582 android_errorWriteLog(0x534e4554, "32438594");
583 return -EINVAL;
584 }
ragoe2759072016-11-22 18:02:48 -0800585 if (cmdCode == EFFECT_CMD_GET_PARAM &&
586 (sizeof(effect_param_t) > *replySize
587 || ((effect_param_t *)pCmdData)->psize > *replySize
588 - sizeof(effect_param_t)
589 || ((effect_param_t *)pCmdData)->vsize > *replySize
590 - sizeof(effect_param_t)
591 - ((effect_param_t *)pCmdData)->psize
592 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
593 *replySize
594 - sizeof(effect_param_t)
595 - ((effect_param_t *)pCmdData)->psize
596 - ((effect_param_t *)pCmdData)->vsize)) {
597 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
598 android_errorWriteLog(0x534e4554, "32705438");
599 return -EINVAL;
600 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700601 if ((cmdCode == EFFECT_CMD_SET_PARAM
602 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
603 (sizeof(effect_param_t) > cmdSize
604 || ((effect_param_t *)pCmdData)->psize > cmdSize
605 - sizeof(effect_param_t)
606 || ((effect_param_t *)pCmdData)->vsize > cmdSize
607 - sizeof(effect_param_t)
608 - ((effect_param_t *)pCmdData)->psize
609 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
610 cmdSize
611 - sizeof(effect_param_t)
612 - ((effect_param_t *)pCmdData)->psize
613 - ((effect_param_t *)pCmdData)->vsize)) {
614 android_errorWriteLog(0x534e4554, "30204301");
615 return -EINVAL;
616 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800617 status_t status = (*mEffectInterface)->command(mEffectInterface,
618 cmdCode,
619 cmdSize,
620 pCmdData,
621 replySize,
622 pReplyData);
623 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
624 uint32_t size = (replySize == NULL) ? 0 : *replySize;
625 for (size_t i = 1; i < mHandles.size(); i++) {
626 EffectHandle *h = mHandles[i];
Eric Laurent84c39212016-12-01 15:28:29 -0800627 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800628 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
629 }
630 }
631 }
632 return status;
633}
634
635status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
636{
637 Mutex::Autolock _l(mLock);
638 return setEnabled_l(enabled);
639}
640
641// must be called with EffectModule::mLock held
642status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
643{
644
645 ALOGV("setEnabled %p enabled %d", this, enabled);
646
647 if (enabled != isEnabled()) {
648 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
649 if (enabled && status != NO_ERROR) {
650 return status;
651 }
652
653 switch (mState) {
654 // going from disabled to enabled
655 case IDLE:
656 mState = STARTING;
657 break;
658 case STOPPED:
659 mState = RESTART;
660 break;
661 case STOPPING:
662 mState = ACTIVE;
663 break;
664
665 // going from enabled to disabled
666 case RESTART:
667 mState = STOPPED;
668 break;
669 case STARTING:
670 mState = IDLE;
671 break;
672 case ACTIVE:
673 mState = STOPPING;
674 break;
675 case DESTROYED:
676 return NO_ERROR; // simply ignore as we are being destroyed
677 }
678 for (size_t i = 1; i < mHandles.size(); i++) {
679 EffectHandle *h = mHandles[i];
Eric Laurent84c39212016-12-01 15:28:29 -0800680 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800681 h->setEnabled(enabled);
682 }
683 }
684 }
685 return NO_ERROR;
686}
687
688bool AudioFlinger::EffectModule::isEnabled() const
689{
690 switch (mState) {
691 case RESTART:
692 case STARTING:
693 case ACTIVE:
694 return true;
695 case IDLE:
696 case STOPPING:
697 case STOPPED:
698 case DESTROYED:
699 default:
700 return false;
701 }
702}
703
704bool AudioFlinger::EffectModule::isProcessEnabled() const
705{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700706 if (mStatus != NO_ERROR) {
707 return false;
708 }
709
Eric Laurentca7cc822012-11-19 14:55:58 -0800710 switch (mState) {
711 case RESTART:
712 case ACTIVE:
713 case STOPPING:
714 case STOPPED:
715 return true;
716 case IDLE:
717 case STARTING:
718 case DESTROYED:
719 default:
720 return false;
721 }
722}
723
724status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
725{
726 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700727 if (mStatus != NO_ERROR) {
728 return mStatus;
729 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800730 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800731 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
732 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
733 if (isProcessEnabled() &&
734 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
735 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
736 status_t cmdStatus;
737 uint32_t volume[2];
738 uint32_t *pVolume = NULL;
739 uint32_t size = sizeof(volume);
740 volume[0] = *left;
741 volume[1] = *right;
742 if (controller) {
743 pVolume = volume;
744 }
745 status = (*mEffectInterface)->command(mEffectInterface,
746 EFFECT_CMD_SET_VOLUME,
747 size,
748 volume,
749 &size,
750 pVolume);
751 if (controller && status == NO_ERROR && size == sizeof(volume)) {
752 *left = volume[0];
753 *right = volume[1];
754 }
755 }
756 return status;
757}
758
759status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
760{
761 if (device == AUDIO_DEVICE_NONE) {
762 return NO_ERROR;
763 }
764
765 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700766 if (mStatus != NO_ERROR) {
767 return mStatus;
768 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800769 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700770 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800771 status_t cmdStatus;
772 uint32_t size = sizeof(status_t);
773 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
774 EFFECT_CMD_SET_INPUT_DEVICE;
775 status = (*mEffectInterface)->command(mEffectInterface,
776 cmd,
777 sizeof(uint32_t),
778 &device,
779 &size,
780 &cmdStatus);
781 }
782 return status;
783}
784
785status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
786{
787 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700788 if (mStatus != NO_ERROR) {
789 return mStatus;
790 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800791 status_t status = NO_ERROR;
792 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
793 status_t cmdStatus;
794 uint32_t size = sizeof(status_t);
795 status = (*mEffectInterface)->command(mEffectInterface,
796 EFFECT_CMD_SET_AUDIO_MODE,
797 sizeof(audio_mode_t),
798 &mode,
799 &size,
800 &cmdStatus);
801 if (status == NO_ERROR) {
802 status = cmdStatus;
803 }
804 }
805 return status;
806}
807
808status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
809{
810 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700811 if (mStatus != NO_ERROR) {
812 return mStatus;
813 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800814 status_t status = NO_ERROR;
815 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
816 uint32_t size = 0;
817 status = (*mEffectInterface)->command(mEffectInterface,
818 EFFECT_CMD_SET_AUDIO_SOURCE,
819 sizeof(audio_source_t),
820 &source,
821 &size,
822 NULL);
823 }
824 return status;
825}
826
827void AudioFlinger::EffectModule::setSuspended(bool suspended)
828{
829 Mutex::Autolock _l(mLock);
830 mSuspended = suspended;
831}
832
833bool AudioFlinger::EffectModule::suspended() const
834{
835 Mutex::Autolock _l(mLock);
836 return mSuspended;
837}
838
839bool AudioFlinger::EffectModule::purgeHandles()
840{
841 bool enabled = false;
842 Mutex::Autolock _l(mLock);
843 for (size_t i = 0; i < mHandles.size(); i++) {
844 EffectHandle *handle = mHandles[i];
Eric Laurent84c39212016-12-01 15:28:29 -0800845 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800846 if (handle->hasControl()) {
847 enabled = handle->enabled();
848 }
849 }
850 }
851 return enabled;
852}
853
Eric Laurent5baf2af2013-09-12 17:37:00 -0700854status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
855{
856 Mutex::Autolock _l(mLock);
857 if (mStatus != NO_ERROR) {
858 return mStatus;
859 }
860 status_t status = NO_ERROR;
861 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
862 status_t cmdStatus;
863 uint32_t size = sizeof(status_t);
864 effect_offload_param_t cmd;
865
866 cmd.isOffload = offloaded;
867 cmd.ioHandle = io;
868 status = (*mEffectInterface)->command(mEffectInterface,
869 EFFECT_CMD_OFFLOAD,
870 sizeof(effect_offload_param_t),
871 &cmd,
872 &size,
873 &cmdStatus);
874 if (status == NO_ERROR) {
875 status = cmdStatus;
876 }
877 mOffloaded = (status == NO_ERROR) ? offloaded : false;
878 } else {
879 if (offloaded) {
880 status = INVALID_OPERATION;
881 }
882 mOffloaded = false;
883 }
884 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
885 return status;
886}
887
888bool AudioFlinger::EffectModule::isOffloaded() const
889{
890 Mutex::Autolock _l(mLock);
891 return mOffloaded;
892}
893
Marco Nelissenb2208842014-02-07 14:00:50 -0800894String8 effectFlagsToString(uint32_t flags) {
895 String8 s;
896
897 s.append("conn. mode: ");
898 switch (flags & EFFECT_FLAG_TYPE_MASK) {
899 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
900 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
901 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
902 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
903 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
904 default: s.append("unknown/reserved"); break;
905 }
906 s.append(", ");
907
908 s.append("insert pref: ");
909 switch (flags & EFFECT_FLAG_INSERT_MASK) {
910 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
911 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
912 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
913 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
914 default: s.append("unknown/reserved"); break;
915 }
916 s.append(", ");
917
918 s.append("volume mgmt: ");
919 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
920 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
921 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
922 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
923 default: s.append("unknown/reserved"); break;
924 }
925 s.append(", ");
926
927 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
928 if (devind) {
929 s.append("device indication: ");
930 switch (devind) {
931 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
932 default: s.append("unknown/reserved"); break;
933 }
934 s.append(", ");
935 }
936
937 s.append("input mode: ");
938 switch (flags & EFFECT_FLAG_INPUT_MASK) {
939 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
940 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
941 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
942 default: s.append("not set"); break;
943 }
944 s.append(", ");
945
946 s.append("output mode: ");
947 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
948 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
949 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
950 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
951 default: s.append("not set"); break;
952 }
953 s.append(", ");
954
955 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
956 if (accel) {
957 s.append("hardware acceleration: ");
958 switch (accel) {
959 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
960 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
961 default: s.append("unknown/reserved"); break;
962 }
963 s.append(", ");
964 }
965
966 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
967 if (modeind) {
968 s.append("mode indication: ");
969 switch (modeind) {
970 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
971 default: s.append("unknown/reserved"); break;
972 }
973 s.append(", ");
974 }
975
976 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
977 if (srcind) {
978 s.append("source indication: ");
979 switch (srcind) {
980 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
981 default: s.append("unknown/reserved"); break;
982 }
983 s.append(", ");
984 }
985
986 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
987 s.append("offloadable, ");
988 }
989
990 int len = s.length();
991 if (s.length() > 2) {
992 char *str = s.lockBuffer(len);
993 s.unlockBuffer(len - 2);
994 }
995 return s;
996}
997
998
Glenn Kasten0f11b512014-01-31 16:18:54 -0800999void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001000{
1001 const size_t SIZE = 256;
1002 char buffer[SIZE];
1003 String8 result;
1004
1005 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1006 result.append(buffer);
1007
1008 bool locked = AudioFlinger::dumpTryLock(mLock);
1009 // failed to lock - AudioFlinger is probably deadlocked
1010 if (!locked) {
1011 result.append("\t\tCould not lock Fx mutex:\n");
1012 }
1013
1014 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001015 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
1016 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001017 result.append(buffer);
1018
1019 result.append("\t\tDescriptor:\n");
1020 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1021 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1022 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1023 mDescriptor.uuid.node[2],
1024 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1025 result.append(buffer);
1026 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1027 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1028 mDescriptor.type.timeHiAndVersion,
1029 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1030 mDescriptor.type.node[2],
1031 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1032 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001033 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001035 mDescriptor.flags,
1036 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001037 result.append(buffer);
1038 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1039 mDescriptor.name);
1040 result.append(buffer);
1041 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1042 mDescriptor.implementor);
1043 result.append(buffer);
1044
1045 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001046 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001047 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001048 mConfig.inputCfg.buffer.frameCount,
1049 mConfig.inputCfg.samplingRate,
1050 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001051 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001052 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001053 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001054 result.append(buffer);
1055
1056 result.append("\t\t- Output configuration:\n");
1057 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001058 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001059 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001060 mConfig.outputCfg.buffer.frameCount,
1061 mConfig.outputCfg.samplingRate,
1062 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001063 mConfig.outputCfg.format,
1064 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001065 result.append(buffer);
1066
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001067 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001068 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001069 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001070 for (size_t i = 0; i < mHandles.size(); ++i) {
1071 EffectHandle *handle = mHandles[i];
Eric Laurent84c39212016-12-01 15:28:29 -08001072 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001073 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001074 result.append(buffer);
1075 }
1076 }
1077
Eric Laurentca7cc822012-11-19 14:55:58 -08001078 write(fd, result.string(), result.length());
1079
1080 if (locked) {
1081 mLock.unlock();
1082 }
1083}
1084
1085// ----------------------------------------------------------------------------
1086// EffectHandle implementation
1087// ----------------------------------------------------------------------------
1088
1089#undef LOG_TAG
1090#define LOG_TAG "AudioFlinger::EffectHandle"
1091
1092AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1093 const sp<AudioFlinger::Client>& client,
1094 const sp<IEffectClient>& effectClient,
1095 int32_t priority)
1096 : BnEffect(),
1097 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent84c39212016-12-01 15:28:29 -08001098 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001099{
1100 ALOGV("constructor %p", this);
1101
1102 if (client == 0) {
1103 return;
1104 }
1105 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1106 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001107 if (mCblkMemory == 0 ||
1108 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001109 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE +
1110 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001111 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001112 return;
1113 }
Glenn Kastene75da402013-11-20 13:54:52 -08001114 new(mCblk) effect_param_cblk_t();
1115 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001116}
1117
1118AudioFlinger::EffectHandle::~EffectHandle()
1119{
1120 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001121 disconnect(false);
1122}
1123
Glenn Kastene75da402013-11-20 13:54:52 -08001124status_t AudioFlinger::EffectHandle::initCheck()
1125{
1126 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1127}
1128
Eric Laurentca7cc822012-11-19 14:55:58 -08001129status_t AudioFlinger::EffectHandle::enable()
1130{
Eric Laurent84c39212016-12-01 15:28:29 -08001131 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001132 ALOGV("enable %p", this);
Eric Laurent84c39212016-12-01 15:28:29 -08001133 sp<EffectModule> effect = mEffect.promote();
1134 if (effect == 0 || mDisconnected) {
1135 return DEAD_OBJECT;
1136 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001137 if (!mHasControl) {
1138 return INVALID_OPERATION;
1139 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001140
1141 if (mEnabled) {
1142 return NO_ERROR;
1143 }
1144
1145 mEnabled = true;
1146
Eric Laurent84c39212016-12-01 15:28:29 -08001147 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001148 if (thread != 0) {
Eric Laurent84c39212016-12-01 15:28:29 -08001149 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001150 }
1151
1152 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent84c39212016-12-01 15:28:29 -08001153 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001154 return NO_ERROR;
1155 }
1156
Eric Laurent84c39212016-12-01 15:28:29 -08001157 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001158 if (status != NO_ERROR) {
1159 if (thread != 0) {
Eric Laurent84c39212016-12-01 15:28:29 -08001160 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001161 }
1162 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001163 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001164 if (thread != 0) {
1165 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001166 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001167 Mutex::Autolock _l(t->mLock);
1168 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001169 }
Eric Laurent84c39212016-12-01 15:28:29 -08001170 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001171 if (thread->type() == ThreadBase::OFFLOAD) {
1172 PlaybackThread *t = (PlaybackThread *)thread.get();
1173 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1174 }
Eric Laurent84c39212016-12-01 15:28:29 -08001175 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001176 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1177 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001178 }
1179 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001180 }
1181 return status;
1182}
1183
1184status_t AudioFlinger::EffectHandle::disable()
1185{
1186 ALOGV("disable %p", this);
Eric Laurent84c39212016-12-01 15:28:29 -08001187 AutoMutex _l(mLock);
1188 sp<EffectModule> effect = mEffect.promote();
1189 if (effect == 0 || mDisconnected) {
1190 return DEAD_OBJECT;
1191 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001192 if (!mHasControl) {
1193 return INVALID_OPERATION;
1194 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001195
1196 if (!mEnabled) {
1197 return NO_ERROR;
1198 }
1199 mEnabled = false;
1200
Eric Laurent84c39212016-12-01 15:28:29 -08001201 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001202 return NO_ERROR;
1203 }
1204
Eric Laurent84c39212016-12-01 15:28:29 -08001205 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001206
Eric Laurent84c39212016-12-01 15:28:29 -08001207 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001208 if (thread != 0) {
Eric Laurent84c39212016-12-01 15:28:29 -08001209 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001210 if (thread->type() == ThreadBase::OFFLOAD) {
1211 PlaybackThread *t = (PlaybackThread *)thread.get();
1212 Mutex::Autolock _l(t->mLock);
1213 t->broadcast_l();
1214 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001215 }
1216
1217 return status;
1218}
1219
1220void AudioFlinger::EffectHandle::disconnect()
1221{
Eric Laurent84c39212016-12-01 15:28:29 -08001222 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001223 disconnect(true);
1224}
1225
1226void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1227{
Eric Laurent84c39212016-12-01 15:28:29 -08001228 AutoMutex _l(mLock);
1229 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1230 if (mDisconnected) {
1231 if (unpinIfLast) {
1232 android_errorWriteLog(0x534e4554, "32707507");
1233 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001234 return;
1235 }
Eric Laurent84c39212016-12-01 15:28:29 -08001236 mDisconnected = true;
1237 sp<ThreadBase> thread;
1238 {
1239 sp<EffectModule> effect = mEffect.promote();
1240 if (effect != 0) {
1241 thread = effect->thread().promote();
1242 }
1243 }
1244 if (thread != 0) {
1245 thread->disconnectEffectHandle(this, unpinIfLast);
1246 } else {
1247 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1248 // try to cleanup as much as we can
1249 sp<EffectModule> effect = mEffect.promote();
1250 if (effect != 0) {
1251 effect->disconnectHandle(this, unpinIfLast);
Eric Laurentca7cc822012-11-19 14:55:58 -08001252 }
1253 }
1254
Eric Laurentca7cc822012-11-19 14:55:58 -08001255 if (mClient != 0) {
1256 if (mCblk != NULL) {
1257 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1258 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1259 }
1260 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001261 // Client destructor must run with AudioFlinger client mutex locked
1262 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001263 mClient.clear();
1264 }
1265}
1266
1267status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1268 uint32_t cmdSize,
1269 void *pCmdData,
1270 uint32_t *replySize,
1271 void *pReplyData)
1272{
1273 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent84c39212016-12-01 15:28:29 -08001274 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001275
Eric Laurent84c39212016-12-01 15:28:29 -08001276 if (cmdCode == EFFECT_CMD_ENABLE) {
1277 if (*replySize < sizeof(int)) {
1278 android_errorWriteLog(0x534e4554, "32095713");
1279 return BAD_VALUE;
1280 }
1281 *(int *)pReplyData = NO_ERROR;
1282 *replySize = sizeof(int);
1283 return enable();
1284 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1285 if (*replySize < sizeof(int)) {
1286 android_errorWriteLog(0x534e4554, "32095713");
1287 return BAD_VALUE;
1288 }
1289 *(int *)pReplyData = NO_ERROR;
1290 *replySize = sizeof(int);
1291 return disable();
1292 }
1293
1294 AutoMutex _l(mLock);
1295 sp<EffectModule> effect = mEffect.promote();
1296 if (effect == 0 || mDisconnected) {
1297 return DEAD_OBJECT;
1298 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001299 // only get parameter command is permitted for applications not controlling the effect
1300 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1301 return INVALID_OPERATION;
1302 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001303 if (mClient == 0) {
1304 return INVALID_OPERATION;
1305 }
1306
1307 // handle commands that are not forwarded transparently to effect engine
1308 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent84c39212016-12-01 15:28:29 -08001309 if (*replySize < sizeof(int)) {
1310 android_errorWriteLog(0x534e4554, "32095713");
1311 return BAD_VALUE;
1312 }
1313 *(int *)pReplyData = NO_ERROR;
1314 *replySize = sizeof(int);
1315
Eric Laurentca7cc822012-11-19 14:55:58 -08001316 // No need to trylock() here as this function is executed in the binder thread serving a
1317 // particular client process: no risk to block the whole media server process or mixer
1318 // threads if we are stuck here
1319 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001320 // keep local copy of index in case of client corruption b/32220769
1321 const uint32_t clientIndex = mCblk->clientIndex;
1322 const uint32_t serverIndex = mCblk->serverIndex;
1323 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1324 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001325 mCblk->serverIndex = 0;
1326 mCblk->clientIndex = 0;
1327 return BAD_VALUE;
1328 }
1329 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001330 effect_param_t *param = NULL;
1331 for (uint32_t index = serverIndex; index < clientIndex;) {
1332 int *p = (int *)(mBuffer + index);
1333 const int size = *p++;
1334 if (size < 0
1335 || size > EFFECT_PARAM_BUFFER_SIZE
1336 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001337 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001338 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001339 break;
1340 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001341
1342 // copy to local memory in case of client corruption b/32220769
1343 param = (effect_param_t *)realloc(param, size);
1344 if (param == NULL) {
1345 ALOGW("command(): out of memory");
1346 status = NO_MEMORY;
1347 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001348 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001349 memcpy(param, p, size);
1350
1351 int reply = 0;
1352 uint32_t rsize = sizeof(reply);
Eric Laurent84c39212016-12-01 15:28:29 -08001353 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001354 size,
1355 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001356 &rsize,
1357 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001358
1359 // verify shared memory: server index shouldn't change; client index can't go back.
1360 if (serverIndex != mCblk->serverIndex
1361 || clientIndex > mCblk->clientIndex) {
1362 android_errorWriteLog(0x534e4554, "32220769");
1363 status = BAD_VALUE;
1364 break;
1365 }
1366
Eric Laurentca7cc822012-11-19 14:55:58 -08001367 // stop at first error encountered
1368 if (ret != NO_ERROR) {
1369 status = ret;
1370 *(int *)pReplyData = reply;
1371 break;
1372 } else if (reply != NO_ERROR) {
1373 *(int *)pReplyData = reply;
1374 break;
1375 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001376 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001377 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001378 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001379 mCblk->serverIndex = 0;
1380 mCblk->clientIndex = 0;
1381 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001382 }
1383
Eric Laurent84c39212016-12-01 15:28:29 -08001384 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001385}
1386
1387void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1388{
1389 ALOGV("setControl %p control %d", this, hasControl);
1390
1391 mHasControl = hasControl;
1392 mEnabled = enabled;
1393
1394 if (signal && mEffectClient != 0) {
1395 mEffectClient->controlStatusChanged(hasControl);
1396 }
1397}
1398
1399void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1400 uint32_t cmdSize,
1401 void *pCmdData,
1402 uint32_t replySize,
1403 void *pReplyData)
1404{
1405 if (mEffectClient != 0) {
1406 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1407 }
1408}
1409
1410
1411
1412void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1413{
1414 if (mEffectClient != 0) {
1415 mEffectClient->enableStatusChanged(enabled);
1416 }
1417}
1418
1419status_t AudioFlinger::EffectHandle::onTransact(
1420 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1421{
1422 return BnEffect::onTransact(code, data, reply, flags);
1423}
1424
1425
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001426void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001427{
1428 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1429
Marco Nelissenb2208842014-02-07 14:00:50 -08001430 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 (mClient == 0) ? getpid_cached : mClient->pid(),
1432 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001433 mHasControl ? "yes" : "no",
1434 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001435 mCblk ? mCblk->clientIndex : 0,
1436 mCblk ? mCblk->serverIndex : 0
1437 );
1438
1439 if (locked) {
1440 mCblk->lock.unlock();
1441 }
1442}
1443
1444#undef LOG_TAG
1445#define LOG_TAG "AudioFlinger::EffectChain"
1446
1447AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1448 int sessionId)
1449 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1450 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001451 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), mForceVolume(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001452{
1453 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1454 if (thread == NULL) {
1455 return;
1456 }
1457 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1458 thread->frameCount();
1459}
1460
1461AudioFlinger::EffectChain::~EffectChain()
1462{
1463 if (mOwnInBuffer) {
1464 delete mInBuffer;
1465 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001466}
1467
1468// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1469sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1470 effect_descriptor_t *descriptor)
1471{
1472 size_t size = mEffects.size();
1473
1474 for (size_t i = 0; i < size; i++) {
1475 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1476 return mEffects[i];
1477 }
1478 }
1479 return 0;
1480}
1481
1482// getEffectFromId_l() must be called with ThreadBase::mLock held
1483sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1484{
1485 size_t size = mEffects.size();
1486
1487 for (size_t i = 0; i < size; i++) {
1488 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1489 if (id == 0 || mEffects[i]->id() == id) {
1490 return mEffects[i];
1491 }
1492 }
1493 return 0;
1494}
1495
1496// getEffectFromType_l() must be called with ThreadBase::mLock held
1497sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1498 const effect_uuid_t *type)
1499{
1500 size_t size = mEffects.size();
1501
1502 for (size_t i = 0; i < size; i++) {
1503 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1504 return mEffects[i];
1505 }
1506 }
1507 return 0;
1508}
1509
1510void AudioFlinger::EffectChain::clearInputBuffer()
1511{
1512 Mutex::Autolock _l(mLock);
1513 sp<ThreadBase> thread = mThread.promote();
1514 if (thread == 0) {
1515 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1516 return;
1517 }
1518 clearInputBuffer_l(thread);
1519}
1520
1521// Must be called with EffectChain::mLock locked
1522void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1523{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001524 // TODO: This will change in the future, depending on multichannel
1525 // and sample format changes for effects.
1526 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1527 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001528 const size_t frameSize =
1529 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001530 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001531}
1532
1533// Must be called with EffectChain::mLock locked
1534void AudioFlinger::EffectChain::process_l()
1535{
1536 sp<ThreadBase> thread = mThread.promote();
1537 if (thread == 0) {
1538 ALOGW("process_l(): cannot promote mixer thread");
1539 return;
1540 }
1541 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1542 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001543 // never process effects when:
1544 // - on an OFFLOAD thread
1545 // - no more tracks are on the session and the effect tail has been rendered
1546 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001547 if (!isGlobalSession) {
1548 bool tracksOnSession = (trackCnt() != 0);
1549
1550 if (!tracksOnSession && mTailBufferCount == 0) {
1551 doProcess = false;
1552 }
1553
1554 if (activeTrackCnt() == 0) {
1555 // if no track is active and the effect tail has not been rendered,
1556 // the input buffer must be cleared here as the mixer process will not do it
1557 if (tracksOnSession || mTailBufferCount > 0) {
1558 clearInputBuffer_l(thread);
1559 if (mTailBufferCount > 0) {
1560 mTailBufferCount--;
1561 }
1562 }
1563 }
1564 }
1565
1566 size_t size = mEffects.size();
1567 if (doProcess) {
1568 for (size_t i = 0; i < size; i++) {
1569 mEffects[i]->process();
1570 }
1571 }
1572 for (size_t i = 0; i < size; i++) {
1573 mEffects[i]->updateState();
1574 }
1575}
1576
Eric Laurent84c39212016-12-01 15:28:29 -08001577// createEffect_l() must be called with ThreadBase::mLock held
1578status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1579 ThreadBase *thread,
1580 effect_descriptor_t *desc,
1581 int id,
1582 int sessionId,
1583 bool pinned)
1584{
1585 Mutex::Autolock _l(mLock);
1586 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1587 status_t lStatus = effect->status();
1588 if (lStatus == NO_ERROR) {
1589 lStatus = addEffect_ll(effect);
1590 }
1591 if (lStatus != NO_ERROR) {
1592 effect.clear();
1593 }
1594 return lStatus;
1595}
1596
1597// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001598status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1599{
Eric Laurent84c39212016-12-01 15:28:29 -08001600 Mutex::Autolock _l(mLock);
1601 return addEffect_ll(effect);
1602}
1603// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1604status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1605{
Eric Laurentca7cc822012-11-19 14:55:58 -08001606 effect_descriptor_t desc = effect->desc();
1607 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1608
Eric Laurentca7cc822012-11-19 14:55:58 -08001609 effect->setChain(this);
1610 sp<ThreadBase> thread = mThread.promote();
1611 if (thread == 0) {
1612 return NO_INIT;
1613 }
1614 effect->setThread(thread);
1615
1616 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1617 // Auxiliary effects are inserted at the beginning of mEffects vector as
1618 // they are processed first and accumulated in chain input buffer
1619 mEffects.insertAt(effect, 0);
1620
1621 // the input buffer for auxiliary effect contains mono samples in
1622 // 32 bit format. This is to avoid saturation in AudoMixer
1623 // accumulation stage. Saturation is done in EffectModule::process() before
1624 // calling the process in effect engine
1625 size_t numSamples = thread->frameCount();
1626 int32_t *buffer = new int32_t[numSamples];
1627 memset(buffer, 0, numSamples * sizeof(int32_t));
1628 effect->setInBuffer((int16_t *)buffer);
1629 // auxiliary effects output samples to chain input buffer for further processing
1630 // by insert effects
1631 effect->setOutBuffer(mInBuffer);
1632 } else {
1633 // Insert effects are inserted at the end of mEffects vector as they are processed
1634 // after track and auxiliary effects.
1635 // Insert effect order as a function of indicated preference:
1636 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1637 // another effect is present
1638 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1639 // last effect claiming first position
1640 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1641 // first effect claiming last position
1642 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1643 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1644 // already present
1645
1646 size_t size = mEffects.size();
1647 size_t idx_insert = size;
1648 ssize_t idx_insert_first = -1;
1649 ssize_t idx_insert_last = -1;
1650
1651 for (size_t i = 0; i < size; i++) {
1652 effect_descriptor_t d = mEffects[i]->desc();
1653 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1654 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1655 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1656 // check invalid effect chaining combinations
1657 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1658 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1659 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1660 desc.name, d.name);
1661 return INVALID_OPERATION;
1662 }
1663 // remember position of first insert effect and by default
1664 // select this as insert position for new effect
1665 if (idx_insert == size) {
1666 idx_insert = i;
1667 }
1668 // remember position of last insert effect claiming
1669 // first position
1670 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1671 idx_insert_first = i;
1672 }
1673 // remember position of first insert effect claiming
1674 // last position
1675 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1676 idx_insert_last == -1) {
1677 idx_insert_last = i;
1678 }
1679 }
1680 }
1681
1682 // modify idx_insert from first position if needed
1683 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1684 if (idx_insert_last != -1) {
1685 idx_insert = idx_insert_last;
1686 } else {
1687 idx_insert = size;
1688 }
1689 } else {
1690 if (idx_insert_first != -1) {
1691 idx_insert = idx_insert_first + 1;
1692 }
1693 }
1694
1695 // always read samples from chain input buffer
1696 effect->setInBuffer(mInBuffer);
1697
1698 // if last effect in the chain, output samples to chain
1699 // output buffer, otherwise to chain input buffer
1700 if (idx_insert == size) {
1701 if (idx_insert != 0) {
1702 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1703 mEffects[idx_insert-1]->configure();
1704 }
1705 effect->setOutBuffer(mOutBuffer);
1706 } else {
1707 effect->setOutBuffer(mInBuffer);
1708 }
1709 mEffects.insertAt(effect, idx_insert);
1710
1711 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1712 idx_insert);
1713 }
1714 effect->configure();
1715 return NO_ERROR;
1716}
1717
Eric Laurent84c39212016-12-01 15:28:29 -08001718// removeEffect_l() must be called with ThreadBase::mLock held
1719size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1720 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001721{
1722 Mutex::Autolock _l(mLock);
1723 size_t size = mEffects.size();
1724 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1725
1726 for (size_t i = 0; i < size; i++) {
1727 if (effect == mEffects[i]) {
1728 // calling stop here will remove pre-processing effect from the audio HAL.
1729 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1730 // the middle of a read from audio HAL
1731 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1732 mEffects[i]->state() == EffectModule::STOPPING) {
1733 mEffects[i]->stop();
1734 }
Eric Laurent84c39212016-12-01 15:28:29 -08001735 if (release) {
1736 mEffects[i]->release_l();
1737 }
1738
Eric Laurentca7cc822012-11-19 14:55:58 -08001739 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1740 delete[] effect->inBuffer();
1741 } else {
1742 if (i == size - 1 && i != 0) {
1743 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1744 mEffects[i - 1]->configure();
1745 }
1746 }
1747 mEffects.removeAt(i);
1748 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1749 this, i);
Eric Laurent84c39212016-12-01 15:28:29 -08001750
Eric Laurentca7cc822012-11-19 14:55:58 -08001751 break;
1752 }
1753 }
1754
1755 return mEffects.size();
1756}
1757
Eric Laurent84c39212016-12-01 15:28:29 -08001758// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001759void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1760{
1761 size_t size = mEffects.size();
1762 for (size_t i = 0; i < size; i++) {
1763 mEffects[i]->setDevice(device);
1764 }
1765}
1766
Eric Laurent84c39212016-12-01 15:28:29 -08001767// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001768void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1769{
1770 size_t size = mEffects.size();
1771 for (size_t i = 0; i < size; i++) {
1772 mEffects[i]->setMode(mode);
1773 }
1774}
1775
Eric Laurent84c39212016-12-01 15:28:29 -08001776// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001777void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1778{
1779 size_t size = mEffects.size();
1780 for (size_t i = 0; i < size; i++) {
1781 mEffects[i]->setAudioSource(source);
1782 }
1783}
1784
1785// setVolume_l() must be called with PlaybackThread::mLock held
1786bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1787{
1788 uint32_t newLeft = *left;
1789 uint32_t newRight = *right;
1790 bool hasControl = false;
1791 int ctrlIdx = -1;
1792 size_t size = mEffects.size();
1793
1794 // first update volume controller
1795 for (size_t i = size; i > 0; i--) {
1796 if (mEffects[i - 1]->isProcessEnabled() &&
1797 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1798 ctrlIdx = i - 1;
1799 hasControl = true;
1800 break;
1801 }
1802 }
1803
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001804 if (!isVolumeForced() && ctrlIdx == mVolumeCtrlIdx &&
1805 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001806 if (hasControl) {
1807 *left = mNewLeftVolume;
1808 *right = mNewRightVolume;
1809 }
1810 return hasControl;
1811 }
1812
1813 mVolumeCtrlIdx = ctrlIdx;
1814 mLeftVolume = newLeft;
1815 mRightVolume = newRight;
1816
1817 // second get volume update from volume controller
1818 if (ctrlIdx >= 0) {
1819 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1820 mNewLeftVolume = newLeft;
1821 mNewRightVolume = newRight;
1822 }
1823 // then indicate volume to all other effects in chain.
1824 // Pass altered volume to effects before volume controller
1825 // and requested volume to effects after controller
1826 uint32_t lVol = newLeft;
1827 uint32_t rVol = newRight;
1828
1829 for (size_t i = 0; i < size; i++) {
1830 if ((int)i == ctrlIdx) {
1831 continue;
1832 }
1833 // this also works for ctrlIdx == -1 when there is no volume controller
1834 if ((int)i > ctrlIdx) {
1835 lVol = *left;
1836 rVol = *right;
1837 }
1838 mEffects[i]->setVolume(&lVol, &rVol, false);
1839 }
1840 *left = newLeft;
1841 *right = newRight;
1842
1843 return hasControl;
1844}
1845
Eric Laurent1b928682014-10-02 19:41:47 -07001846void AudioFlinger::EffectChain::syncHalEffectsState()
1847{
1848 Mutex::Autolock _l(mLock);
1849 for (size_t i = 0; i < mEffects.size(); i++) {
1850 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1851 mEffects[i]->state() == EffectModule::STOPPING) {
1852 mEffects[i]->addEffectToHal_l();
1853 }
1854 }
1855}
1856
Eric Laurentca7cc822012-11-19 14:55:58 -08001857void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1858{
1859 const size_t SIZE = 256;
1860 char buffer[SIZE];
1861 String8 result;
1862
Marco Nelissenb2208842014-02-07 14:00:50 -08001863 size_t numEffects = mEffects.size();
1864 snprintf(buffer, SIZE, " %d effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 result.append(buffer);
1866
Marco Nelissenb2208842014-02-07 14:00:50 -08001867 if (numEffects) {
1868 bool locked = AudioFlinger::dumpTryLock(mLock);
1869 // failed to lock - AudioFlinger is probably deadlocked
1870 if (!locked) {
1871 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001872 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001873
Marco Nelissenb2208842014-02-07 14:00:50 -08001874 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001875 snprintf(buffer, SIZE, "\t%p %p %d\n",
1876 mInBuffer,
1877 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001878 mActiveTrackCnt);
1879 result.append(buffer);
1880 write(fd, result.string(), result.size());
1881
1882 for (size_t i = 0; i < numEffects; ++i) {
1883 sp<EffectModule> effect = mEffects[i];
1884 if (effect != 0) {
1885 effect->dump(fd, args);
1886 }
1887 }
1888
1889 if (locked) {
1890 mLock.unlock();
1891 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001892 }
1893}
1894
1895// must be called with ThreadBase::mLock held
1896void AudioFlinger::EffectChain::setEffectSuspended_l(
1897 const effect_uuid_t *type, bool suspend)
1898{
1899 sp<SuspendedEffectDesc> desc;
1900 // use effect type UUID timelow as key as there is no real risk of identical
1901 // timeLow fields among effect type UUIDs.
1902 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1903 if (suspend) {
1904 if (index >= 0) {
1905 desc = mSuspendedEffects.valueAt(index);
1906 } else {
1907 desc = new SuspendedEffectDesc();
1908 desc->mType = *type;
1909 mSuspendedEffects.add(type->timeLow, desc);
1910 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1911 }
1912 if (desc->mRefCount++ == 0) {
1913 sp<EffectModule> effect = getEffectIfEnabled(type);
1914 if (effect != 0) {
1915 desc->mEffect = effect;
1916 effect->setSuspended(true);
1917 effect->setEnabled(false);
1918 }
1919 }
1920 } else {
1921 if (index < 0) {
1922 return;
1923 }
1924 desc = mSuspendedEffects.valueAt(index);
1925 if (desc->mRefCount <= 0) {
1926 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1927 desc->mRefCount = 1;
1928 }
1929 if (--desc->mRefCount == 0) {
1930 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1931 if (desc->mEffect != 0) {
1932 sp<EffectModule> effect = desc->mEffect.promote();
1933 if (effect != 0) {
1934 effect->setSuspended(false);
1935 effect->lock();
1936 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent84c39212016-12-01 15:28:29 -08001937 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001938 effect->setEnabled_l(handle->enabled());
1939 }
1940 effect->unlock();
1941 }
1942 desc->mEffect.clear();
1943 }
1944 mSuspendedEffects.removeItemsAt(index);
1945 }
1946 }
1947}
1948
1949// must be called with ThreadBase::mLock held
1950void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1951{
1952 sp<SuspendedEffectDesc> desc;
1953
1954 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1955 if (suspend) {
1956 if (index >= 0) {
1957 desc = mSuspendedEffects.valueAt(index);
1958 } else {
1959 desc = new SuspendedEffectDesc();
1960 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1961 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1962 }
1963 if (desc->mRefCount++ == 0) {
1964 Vector< sp<EffectModule> > effects;
1965 getSuspendEligibleEffects(effects);
1966 for (size_t i = 0; i < effects.size(); i++) {
1967 setEffectSuspended_l(&effects[i]->desc().type, true);
1968 }
1969 }
1970 } else {
1971 if (index < 0) {
1972 return;
1973 }
1974 desc = mSuspendedEffects.valueAt(index);
1975 if (desc->mRefCount <= 0) {
1976 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1977 desc->mRefCount = 1;
1978 }
1979 if (--desc->mRefCount == 0) {
1980 Vector<const effect_uuid_t *> types;
1981 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1982 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1983 continue;
1984 }
1985 types.add(&mSuspendedEffects.valueAt(i)->mType);
1986 }
1987 for (size_t i = 0; i < types.size(); i++) {
1988 setEffectSuspended_l(types[i], false);
1989 }
1990 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1991 mSuspendedEffects.keyAt(index));
1992 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1993 }
1994 }
1995}
1996
1997
1998// The volume effect is used for automated tests only
1999#ifndef OPENSL_ES_H_
2000static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2001 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2002const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2003#endif //OPENSL_ES_H_
2004
2005bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2006{
2007 // auxiliary effects and visualizer are never suspended on output mix
2008 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2009 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2010 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2011 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2012 return false;
2013 }
2014 return true;
2015}
2016
2017void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2018 Vector< sp<AudioFlinger::EffectModule> > &effects)
2019{
2020 effects.clear();
2021 for (size_t i = 0; i < mEffects.size(); i++) {
2022 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2023 effects.add(mEffects[i]);
2024 }
2025 }
2026}
2027
2028sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2029 const effect_uuid_t *type)
2030{
2031 sp<EffectModule> effect = getEffectFromType_l(type);
2032 return effect != 0 && effect->isEnabled() ? effect : 0;
2033}
2034
2035void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2036 bool enabled)
2037{
2038 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2039 if (enabled) {
2040 if (index < 0) {
2041 // if the effect is not suspend check if all effects are suspended
2042 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2043 if (index < 0) {
2044 return;
2045 }
2046 if (!isEffectEligibleForSuspend(effect->desc())) {
2047 return;
2048 }
2049 setEffectSuspended_l(&effect->desc().type, enabled);
2050 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2051 if (index < 0) {
2052 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2053 return;
2054 }
2055 }
2056 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2057 effect->desc().type.timeLow);
2058 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2059 // if effect is requested to suspended but was not yet enabled, supend it now.
2060 if (desc->mEffect == 0) {
2061 desc->mEffect = effect;
2062 effect->setEnabled(false);
2063 effect->setSuspended(true);
2064 }
2065 } else {
2066 if (index < 0) {
2067 return;
2068 }
2069 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2070 effect->desc().type.timeLow);
2071 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2072 desc->mEffect.clear();
2073 effect->setSuspended(false);
2074 }
2075}
2076
Eric Laurent5baf2af2013-09-12 17:37:00 -07002077bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002078{
2079 Mutex::Autolock _l(mLock);
2080 size_t size = mEffects.size();
2081 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002082 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002083 return true;
2084 }
2085 }
2086 return false;
2087}
2088
Eric Laurentaaa44472014-09-12 17:41:50 -07002089void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2090{
2091 Mutex::Autolock _l(mLock);
2092 mThread = thread;
2093 for (size_t i = 0; i < mEffects.size(); i++) {
2094 mEffects[i]->setThread(thread);
2095 }
2096}
2097
Eric Laurentca7cc822012-11-19 14:55:58 -08002098}; // namespace android