blob: d669841a1f9c4d209e027d35067493f3609a4cbd [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 Laurentb378b732016-12-01 15:28:29 -080062 audio_session_t 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 Laurentb378b732016-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 Laurentb378b732016-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);
103 if (mEffectInterface != NULL) {
Eric Laurentb378b732016-12-01 15:28:29 -0800104 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 Laurentb378b732016-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 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700143 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800144 mHandles.insertAt(handle, i);
145 return status;
146}
147
Eric Laurentb378b732016-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 Laurentb378b732016-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 Laurentb378b732016-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 Laurentb378b732016-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 Laurentb378b732016-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 Laurentb378b732016-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 Laurentb378b732016-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 Laurentb378b732016-12-01 15:28:29 -0800217 return numHandles;
Eric Laurentca7cc822012-11-19 14:55:58 -0800218}
219
Eric Laurentfa1e1232016-08-02 19:01:49 -0700220bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800221 Mutex::Autolock _l(mLock);
222
Eric Laurentfa1e1232016-08-02 19:01:49 -0700223 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800224 switch (mState) {
225 case RESTART:
226 reset_l();
227 // FALL THROUGH
228
229 case STARTING:
230 // clear auxiliary effect input buffer for next accumulation
231 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
232 memset(mConfig.inputCfg.buffer.raw,
233 0,
234 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
235 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700236 if (start_l() == NO_ERROR) {
237 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700238 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700239 } else {
240 mState = IDLE;
241 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800242 break;
243 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700244 if (stop_l() == NO_ERROR) {
245 mDisableWaitCnt = mMaxDisableWaitCnt;
246 } else {
247 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
248 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800249 mState = STOPPED;
250 break;
251 case STOPPED:
252 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
253 // turn off sequence.
254 if (--mDisableWaitCnt == 0) {
255 reset_l();
256 mState = IDLE;
257 }
258 break;
259 default: //IDLE , ACTIVE, DESTROYED
260 break;
261 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700262
263 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800264}
265
266void AudioFlinger::EffectModule::process()
267{
268 Mutex::Autolock _l(mLock);
269
270 if (mState == DESTROYED || mEffectInterface == NULL ||
271 mConfig.inputCfg.buffer.raw == NULL ||
272 mConfig.outputCfg.buffer.raw == NULL) {
273 return;
274 }
275
276 if (isProcessEnabled()) {
277 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
278 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
279 ditherAndClamp(mConfig.inputCfg.buffer.s32,
280 mConfig.inputCfg.buffer.s32,
281 mConfig.inputCfg.buffer.frameCount/2);
282 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700283 int ret;
284 if (isProcessImplemented()) {
285 // do the actual processing in the effect engine
286 ret = (*mEffectInterface)->process(mEffectInterface,
287 &mConfig.inputCfg.buffer,
288 &mConfig.outputCfg.buffer);
289 } else {
290 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
291 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
292 int16_t *in = mConfig.inputCfg.buffer.s16;
293 int16_t *out = mConfig.outputCfg.buffer.s16;
Eric Laurentca7cc822012-11-19 14:55:58 -0800294
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700295 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
296 for (size_t i = 0; i < frameCnt; i++) {
297 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
298 }
299 } else {
300 memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
301 frameCnt * sizeof(int16_t));
302 }
303 }
304 ret = -ENODATA;
305 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800306 // force transition to IDLE state when engine is ready
307 if (mState == STOPPED && ret == -ENODATA) {
308 mDisableWaitCnt = 1;
309 }
310
311 // clear auxiliary effect input buffer for next accumulation
312 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
313 memset(mConfig.inputCfg.buffer.raw, 0,
314 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
315 }
316 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
317 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
318 // If an insert effect is idle and input buffer is different from output buffer,
319 // accumulate input onto output
320 sp<EffectChain> chain = mChain.promote();
321 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700322 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
Eric Laurentca7cc822012-11-19 14:55:58 -0800323 int16_t *in = mConfig.inputCfg.buffer.s16;
324 int16_t *out = mConfig.outputCfg.buffer.s16;
325 for (size_t i = 0; i < frameCnt; i++) {
326 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
327 }
328 }
329 }
330}
331
332void AudioFlinger::EffectModule::reset_l()
333{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700334 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800335 return;
336 }
337 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
338}
339
340status_t AudioFlinger::EffectModule::configure()
341{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700342 status_t status;
343 sp<ThreadBase> thread;
344 uint32_t size;
345 audio_channel_mask_t channelMask;
346
Eric Laurentca7cc822012-11-19 14:55:58 -0800347 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700348 status = NO_INIT;
349 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800350 }
351
Eric Laurentd0ebb532013-04-02 16:41:41 -0700352 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800353 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700354 status = DEAD_OBJECT;
355 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800356 }
357
358 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700359 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700360 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800361
362 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
363 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
364 } else {
365 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700366 // TODO: Update this logic when multichannel effects are implemented.
367 // For offloaded tracks consider mono output as stereo for proper effect initialization
368 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
369 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
370 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
371 ALOGV("Overriding effect input and output as STEREO");
372 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800373 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700374
Eric Laurentca7cc822012-11-19 14:55:58 -0800375 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
376 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
377 mConfig.inputCfg.samplingRate = thread->sampleRate();
378 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
379 mConfig.inputCfg.bufferProvider.cookie = NULL;
380 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
381 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
382 mConfig.outputCfg.bufferProvider.cookie = NULL;
383 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
384 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
385 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
386 // Insert effect:
387 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
388 // always overwrites output buffer: input buffer == output buffer
389 // - in other sessions:
390 // last effect in the chain accumulates in output buffer: input buffer != output buffer
391 // other effect: overwrites output buffer: input buffer == output buffer
392 // Auxiliary effect:
393 // accumulates in output buffer: input buffer != output buffer
394 // Therefore: accumulate <=> input buffer != output buffer
395 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
396 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
397 } else {
398 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
399 }
400 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
401 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
402 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
403 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
404
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700405 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800406 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
407
408 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700409 size = sizeof(int);
410 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800411 EFFECT_CMD_SET_CONFIG,
412 sizeof(effect_config_t),
413 &mConfig,
414 &size,
415 &cmdStatus);
416 if (status == 0) {
417 status = cmdStatus;
418 }
419
420 if (status == 0 &&
421 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
422 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
423 effect_param_t *p = (effect_param_t *)buf32;
424
425 p->psize = sizeof(uint32_t);
426 p->vsize = sizeof(uint32_t);
427 size = sizeof(int);
428 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
429
430 uint32_t latency = 0;
431 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
432 if (pbt != NULL) {
433 latency = pbt->latency_l();
434 }
435
436 *((int32_t *)p->data + 1)= latency;
437 (*mEffectInterface)->command(mEffectInterface,
438 EFFECT_CMD_SET_PARAM,
439 sizeof(effect_param_t) + 8,
440 &buf32,
441 &size,
442 &cmdStatus);
443 }
444
445 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
446 (1000 * mConfig.outputCfg.buffer.frameCount);
447
Eric Laurentd0ebb532013-04-02 16:41:41 -0700448exit:
449 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800450 return status;
451}
452
453status_t AudioFlinger::EffectModule::init()
454{
455 Mutex::Autolock _l(mLock);
456 if (mEffectInterface == NULL) {
457 return NO_INIT;
458 }
459 status_t cmdStatus;
460 uint32_t size = sizeof(status_t);
461 status_t status = (*mEffectInterface)->command(mEffectInterface,
462 EFFECT_CMD_INIT,
463 0,
464 NULL,
465 &size,
466 &cmdStatus);
467 if (status == 0) {
468 status = cmdStatus;
469 }
470 return status;
471}
472
Eric Laurent1b928682014-10-02 19:41:47 -0700473void AudioFlinger::EffectModule::addEffectToHal_l()
474{
475 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
476 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
477 sp<ThreadBase> thread = mThread.promote();
478 if (thread != 0) {
479 audio_stream_t *stream = thread->stream();
480 if (stream != NULL) {
481 stream->add_audio_effect(stream, mEffectInterface);
482 }
483 }
484 }
485}
486
Eric Laurentfa1e1232016-08-02 19:01:49 -0700487// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800488status_t AudioFlinger::EffectModule::start()
489{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700490 sp<EffectChain> chain;
491 status_t status;
492 {
493 Mutex::Autolock _l(mLock);
494 status = start_l();
495 if (status == NO_ERROR) {
496 chain = mChain.promote();
497 }
498 }
499 if (chain != 0) {
500 chain->resetVolume_l();
501 }
502 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800503}
504
505status_t AudioFlinger::EffectModule::start_l()
506{
507 if (mEffectInterface == NULL) {
508 return NO_INIT;
509 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700510 if (mStatus != NO_ERROR) {
511 return mStatus;
512 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800513 status_t cmdStatus;
514 uint32_t size = sizeof(status_t);
515 status_t status = (*mEffectInterface)->command(mEffectInterface,
516 EFFECT_CMD_ENABLE,
517 0,
518 NULL,
519 &size,
520 &cmdStatus);
521 if (status == 0) {
522 status = cmdStatus;
523 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700524 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700525 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800526 }
527 return status;
528}
529
530status_t AudioFlinger::EffectModule::stop()
531{
532 Mutex::Autolock _l(mLock);
533 return stop_l();
534}
535
536status_t AudioFlinger::EffectModule::stop_l()
537{
538 if (mEffectInterface == NULL) {
539 return NO_INIT;
540 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700541 if (mStatus != NO_ERROR) {
542 return mStatus;
543 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800544 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800545 uint32_t size = sizeof(status_t);
546 status_t status = (*mEffectInterface)->command(mEffectInterface,
547 EFFECT_CMD_DISABLE,
548 0,
549 NULL,
550 &size,
551 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800552 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800553 status = cmdStatus;
554 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800555 if (status == NO_ERROR) {
556 status = remove_effect_from_hal_l();
557 }
558 return status;
559}
560
Eric Laurentb378b732016-12-01 15:28:29 -0800561// must be called with EffectChain::mLock held
562void AudioFlinger::EffectModule::release_l()
563{
564 if (mEffectInterface != NULL) {
565 remove_effect_from_hal_l();
566 // release effect engine
567 EffectRelease(mEffectInterface);
568 mEffectInterface = NULL;
569 }
570}
571
Eric Laurentbfb1b832013-01-07 09:53:42 -0800572status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
573{
574 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
575 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800576 sp<ThreadBase> thread = mThread.promote();
577 if (thread != 0) {
578 audio_stream_t *stream = thread->stream();
579 if (stream != NULL) {
580 stream->remove_audio_effect(stream, mEffectInterface);
581 }
582 }
583 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800584 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800585}
586
Andy Hunge4a1d912016-08-17 14:11:13 -0700587// round up delta valid if value and divisor are positive.
588template <typename T>
589static T roundUpDelta(const T &value, const T &divisor) {
590 T remainder = value % divisor;
591 return remainder == 0 ? 0 : divisor - remainder;
592}
593
Eric Laurentca7cc822012-11-19 14:55:58 -0800594status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
595 uint32_t cmdSize,
596 void *pCmdData,
597 uint32_t *replySize,
598 void *pReplyData)
599{
600 Mutex::Autolock _l(mLock);
601 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
602
603 if (mState == DESTROYED || mEffectInterface == NULL) {
604 return NO_INIT;
605 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700606 if (mStatus != NO_ERROR) {
607 return mStatus;
608 }
Andy Hung110bc952016-06-20 15:22:52 -0700609 if (cmdCode == EFFECT_CMD_GET_PARAM &&
610 (*replySize < sizeof(effect_param_t) ||
611 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
612 android_errorWriteLog(0x534e4554, "29251553");
613 return -EINVAL;
614 }
Andy Hung3d34cc72016-11-04 19:40:53 -0700615 if (cmdCode == EFFECT_CMD_GET_PARAM &&
616 (sizeof(effect_param_t) > cmdSize ||
617 ((effect_param_t *)pCmdData)->psize > cmdSize
618 - sizeof(effect_param_t))) {
619 android_errorWriteLog(0x534e4554, "32438594");
620 return -EINVAL;
621 }
ragoe2759072016-11-22 18:02:48 -0800622 if (cmdCode == EFFECT_CMD_GET_PARAM &&
623 (sizeof(effect_param_t) > *replySize
624 || ((effect_param_t *)pCmdData)->psize > *replySize
625 - sizeof(effect_param_t)
626 || ((effect_param_t *)pCmdData)->vsize > *replySize
627 - sizeof(effect_param_t)
628 - ((effect_param_t *)pCmdData)->psize
629 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
630 *replySize
631 - sizeof(effect_param_t)
632 - ((effect_param_t *)pCmdData)->psize
633 - ((effect_param_t *)pCmdData)->vsize)) {
634 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
635 android_errorWriteLog(0x534e4554, "32705438");
636 return -EINVAL;
637 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700638 if ((cmdCode == EFFECT_CMD_SET_PARAM
639 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
640 (sizeof(effect_param_t) > cmdSize
641 || ((effect_param_t *)pCmdData)->psize > cmdSize
642 - sizeof(effect_param_t)
643 || ((effect_param_t *)pCmdData)->vsize > cmdSize
644 - sizeof(effect_param_t)
645 - ((effect_param_t *)pCmdData)->psize
646 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
647 cmdSize
648 - sizeof(effect_param_t)
649 - ((effect_param_t *)pCmdData)->psize
650 - ((effect_param_t *)pCmdData)->vsize)) {
651 android_errorWriteLog(0x534e4554, "30204301");
652 return -EINVAL;
653 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800654 status_t status = (*mEffectInterface)->command(mEffectInterface,
655 cmdCode,
656 cmdSize,
657 pCmdData,
658 replySize,
659 pReplyData);
660 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
661 uint32_t size = (replySize == NULL) ? 0 : *replySize;
662 for (size_t i = 1; i < mHandles.size(); i++) {
663 EffectHandle *h = mHandles[i];
Eric Laurentb378b732016-12-01 15:28:29 -0800664 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800665 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
666 }
667 }
668 }
669 return status;
670}
671
672status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
673{
674 Mutex::Autolock _l(mLock);
675 return setEnabled_l(enabled);
676}
677
678// must be called with EffectModule::mLock held
679status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
680{
681
682 ALOGV("setEnabled %p enabled %d", this, enabled);
683
684 if (enabled != isEnabled()) {
685 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
686 if (enabled && status != NO_ERROR) {
687 return status;
688 }
689
690 switch (mState) {
691 // going from disabled to enabled
692 case IDLE:
693 mState = STARTING;
694 break;
695 case STOPPED:
696 mState = RESTART;
697 break;
698 case STOPPING:
699 mState = ACTIVE;
700 break;
701
702 // going from enabled to disabled
703 case RESTART:
704 mState = STOPPED;
705 break;
706 case STARTING:
707 mState = IDLE;
708 break;
709 case ACTIVE:
710 mState = STOPPING;
711 break;
712 case DESTROYED:
713 return NO_ERROR; // simply ignore as we are being destroyed
714 }
715 for (size_t i = 1; i < mHandles.size(); i++) {
716 EffectHandle *h = mHandles[i];
Eric Laurentb378b732016-12-01 15:28:29 -0800717 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800718 h->setEnabled(enabled);
719 }
720 }
721 }
722 return NO_ERROR;
723}
724
725bool AudioFlinger::EffectModule::isEnabled() const
726{
727 switch (mState) {
728 case RESTART:
729 case STARTING:
730 case ACTIVE:
731 return true;
732 case IDLE:
733 case STOPPING:
734 case STOPPED:
735 case DESTROYED:
736 default:
737 return false;
738 }
739}
740
741bool AudioFlinger::EffectModule::isProcessEnabled() const
742{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700743 if (mStatus != NO_ERROR) {
744 return false;
745 }
746
Eric Laurentca7cc822012-11-19 14:55:58 -0800747 switch (mState) {
748 case RESTART:
749 case ACTIVE:
750 case STOPPING:
751 case STOPPED:
752 return true;
753 case IDLE:
754 case STARTING:
755 case DESTROYED:
756 default:
757 return false;
758 }
759}
760
761status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
762{
763 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700764 if (mStatus != NO_ERROR) {
765 return mStatus;
766 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800767 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800768 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
769 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
770 if (isProcessEnabled() &&
771 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
772 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800773 uint32_t volume[2];
774 uint32_t *pVolume = NULL;
775 uint32_t size = sizeof(volume);
776 volume[0] = *left;
777 volume[1] = *right;
778 if (controller) {
779 pVolume = volume;
780 }
781 status = (*mEffectInterface)->command(mEffectInterface,
782 EFFECT_CMD_SET_VOLUME,
783 size,
784 volume,
785 &size,
786 pVolume);
787 if (controller && status == NO_ERROR && size == sizeof(volume)) {
788 *left = volume[0];
789 *right = volume[1];
790 }
791 }
792 return status;
793}
794
795status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
796{
797 if (device == AUDIO_DEVICE_NONE) {
798 return NO_ERROR;
799 }
800
801 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700802 if (mStatus != NO_ERROR) {
803 return mStatus;
804 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800805 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700806 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800807 status_t cmdStatus;
808 uint32_t size = sizeof(status_t);
809 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
810 EFFECT_CMD_SET_INPUT_DEVICE;
811 status = (*mEffectInterface)->command(mEffectInterface,
812 cmd,
813 sizeof(uint32_t),
814 &device,
815 &size,
816 &cmdStatus);
817 }
818 return status;
819}
820
821status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
822{
823 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700824 if (mStatus != NO_ERROR) {
825 return mStatus;
826 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800827 status_t status = NO_ERROR;
828 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
829 status_t cmdStatus;
830 uint32_t size = sizeof(status_t);
831 status = (*mEffectInterface)->command(mEffectInterface,
832 EFFECT_CMD_SET_AUDIO_MODE,
833 sizeof(audio_mode_t),
834 &mode,
835 &size,
836 &cmdStatus);
837 if (status == NO_ERROR) {
838 status = cmdStatus;
839 }
840 }
841 return status;
842}
843
844status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
845{
846 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700847 if (mStatus != NO_ERROR) {
848 return mStatus;
849 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800850 status_t status = NO_ERROR;
851 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
852 uint32_t size = 0;
853 status = (*mEffectInterface)->command(mEffectInterface,
854 EFFECT_CMD_SET_AUDIO_SOURCE,
855 sizeof(audio_source_t),
856 &source,
857 &size,
858 NULL);
859 }
860 return status;
861}
862
863void AudioFlinger::EffectModule::setSuspended(bool suspended)
864{
865 Mutex::Autolock _l(mLock);
866 mSuspended = suspended;
867}
868
869bool AudioFlinger::EffectModule::suspended() const
870{
871 Mutex::Autolock _l(mLock);
872 return mSuspended;
873}
874
875bool AudioFlinger::EffectModule::purgeHandles()
876{
877 bool enabled = false;
878 Mutex::Autolock _l(mLock);
879 for (size_t i = 0; i < mHandles.size(); i++) {
880 EffectHandle *handle = mHandles[i];
Eric Laurentb378b732016-12-01 15:28:29 -0800881 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800882 if (handle->hasControl()) {
883 enabled = handle->enabled();
884 }
885 }
886 }
887 return enabled;
888}
889
Eric Laurent5baf2af2013-09-12 17:37:00 -0700890status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
891{
892 Mutex::Autolock _l(mLock);
893 if (mStatus != NO_ERROR) {
894 return mStatus;
895 }
896 status_t status = NO_ERROR;
897 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
898 status_t cmdStatus;
899 uint32_t size = sizeof(status_t);
900 effect_offload_param_t cmd;
901
902 cmd.isOffload = offloaded;
903 cmd.ioHandle = io;
904 status = (*mEffectInterface)->command(mEffectInterface,
905 EFFECT_CMD_OFFLOAD,
906 sizeof(effect_offload_param_t),
907 &cmd,
908 &size,
909 &cmdStatus);
910 if (status == NO_ERROR) {
911 status = cmdStatus;
912 }
913 mOffloaded = (status == NO_ERROR) ? offloaded : false;
914 } else {
915 if (offloaded) {
916 status = INVALID_OPERATION;
917 }
918 mOffloaded = false;
919 }
920 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
921 return status;
922}
923
924bool AudioFlinger::EffectModule::isOffloaded() const
925{
926 Mutex::Autolock _l(mLock);
927 return mOffloaded;
928}
929
Marco Nelissenb2208842014-02-07 14:00:50 -0800930String8 effectFlagsToString(uint32_t flags) {
931 String8 s;
932
933 s.append("conn. mode: ");
934 switch (flags & EFFECT_FLAG_TYPE_MASK) {
935 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
936 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
937 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
938 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
939 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
940 default: s.append("unknown/reserved"); break;
941 }
942 s.append(", ");
943
944 s.append("insert pref: ");
945 switch (flags & EFFECT_FLAG_INSERT_MASK) {
946 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
947 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
948 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
949 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
950 default: s.append("unknown/reserved"); break;
951 }
952 s.append(", ");
953
954 s.append("volume mgmt: ");
955 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
956 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
957 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
958 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
959 default: s.append("unknown/reserved"); break;
960 }
961 s.append(", ");
962
963 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
964 if (devind) {
965 s.append("device indication: ");
966 switch (devind) {
967 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
968 default: s.append("unknown/reserved"); break;
969 }
970 s.append(", ");
971 }
972
973 s.append("input mode: ");
974 switch (flags & EFFECT_FLAG_INPUT_MASK) {
975 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
976 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
977 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
978 default: s.append("not set"); break;
979 }
980 s.append(", ");
981
982 s.append("output mode: ");
983 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
984 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
985 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
986 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
987 default: s.append("not set"); break;
988 }
989 s.append(", ");
990
991 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
992 if (accel) {
993 s.append("hardware acceleration: ");
994 switch (accel) {
995 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
996 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
997 default: s.append("unknown/reserved"); break;
998 }
999 s.append(", ");
1000 }
1001
1002 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1003 if (modeind) {
1004 s.append("mode indication: ");
1005 switch (modeind) {
1006 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1007 default: s.append("unknown/reserved"); break;
1008 }
1009 s.append(", ");
1010 }
1011
1012 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1013 if (srcind) {
1014 s.append("source indication: ");
1015 switch (srcind) {
1016 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1017 default: s.append("unknown/reserved"); break;
1018 }
1019 s.append(", ");
1020 }
1021
1022 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1023 s.append("offloadable, ");
1024 }
1025
1026 int len = s.length();
1027 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001028 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001029 s.unlockBuffer(len - 2);
1030 }
1031 return s;
1032}
1033
1034
Glenn Kasten0f11b512014-01-31 16:18:54 -08001035void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001036{
1037 const size_t SIZE = 256;
1038 char buffer[SIZE];
1039 String8 result;
1040
1041 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1042 result.append(buffer);
1043
1044 bool locked = AudioFlinger::dumpTryLock(mLock);
1045 // failed to lock - AudioFlinger is probably deadlocked
1046 if (!locked) {
1047 result.append("\t\tCould not lock Fx mutex:\n");
1048 }
1049
1050 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001051 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
1052 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001053 result.append(buffer);
1054
1055 result.append("\t\tDescriptor:\n");
1056 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1057 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1058 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1059 mDescriptor.uuid.node[2],
1060 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1061 result.append(buffer);
1062 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1063 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1064 mDescriptor.type.timeHiAndVersion,
1065 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1066 mDescriptor.type.node[2],
1067 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1068 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001069 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001070 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001071 mDescriptor.flags,
1072 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001073 result.append(buffer);
1074 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1075 mDescriptor.name);
1076 result.append(buffer);
1077 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1078 mDescriptor.implementor);
1079 result.append(buffer);
1080
1081 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001082 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001083 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001084 mConfig.inputCfg.buffer.frameCount,
1085 mConfig.inputCfg.samplingRate,
1086 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001087 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001088 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001089 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001090 result.append(buffer);
1091
1092 result.append("\t\t- Output configuration:\n");
1093 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001094 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001095 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001096 mConfig.outputCfg.buffer.frameCount,
1097 mConfig.outputCfg.samplingRate,
1098 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001099 mConfig.outputCfg.format,
1100 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001101 result.append(buffer);
1102
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001103 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001104 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001105 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001106 for (size_t i = 0; i < mHandles.size(); ++i) {
1107 EffectHandle *handle = mHandles[i];
Eric Laurentb378b732016-12-01 15:28:29 -08001108 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001109 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001110 result.append(buffer);
1111 }
1112 }
1113
Eric Laurentca7cc822012-11-19 14:55:58 -08001114 write(fd, result.string(), result.length());
1115
1116 if (locked) {
1117 mLock.unlock();
1118 }
1119}
1120
1121// ----------------------------------------------------------------------------
1122// EffectHandle implementation
1123// ----------------------------------------------------------------------------
1124
1125#undef LOG_TAG
1126#define LOG_TAG "AudioFlinger::EffectHandle"
1127
1128AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1129 const sp<AudioFlinger::Client>& client,
1130 const sp<IEffectClient>& effectClient,
1131 int32_t priority)
1132 : BnEffect(),
1133 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentb378b732016-12-01 15:28:29 -08001134 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001135{
1136 ALOGV("constructor %p", this);
1137
1138 if (client == 0) {
1139 return;
1140 }
1141 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1142 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001143 if (mCblkMemory == 0 ||
1144 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001145 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001146 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001147 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001148 return;
1149 }
Glenn Kastene75da402013-11-20 13:54:52 -08001150 new(mCblk) effect_param_cblk_t();
1151 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001152}
1153
1154AudioFlinger::EffectHandle::~EffectHandle()
1155{
1156 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001157 disconnect(false);
1158}
1159
Glenn Kastene75da402013-11-20 13:54:52 -08001160status_t AudioFlinger::EffectHandle::initCheck()
1161{
1162 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1163}
1164
Eric Laurentca7cc822012-11-19 14:55:58 -08001165status_t AudioFlinger::EffectHandle::enable()
1166{
Eric Laurentb378b732016-12-01 15:28:29 -08001167 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001168 ALOGV("enable %p", this);
Eric Laurentb378b732016-12-01 15:28:29 -08001169 sp<EffectModule> effect = mEffect.promote();
1170 if (effect == 0 || mDisconnected) {
1171 return DEAD_OBJECT;
1172 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001173 if (!mHasControl) {
1174 return INVALID_OPERATION;
1175 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001176
1177 if (mEnabled) {
1178 return NO_ERROR;
1179 }
1180
1181 mEnabled = true;
1182
Eric Laurentb378b732016-12-01 15:28:29 -08001183 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001184 if (thread != 0) {
Eric Laurentb378b732016-12-01 15:28:29 -08001185 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001186 }
1187
1188 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurentb378b732016-12-01 15:28:29 -08001189 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001190 return NO_ERROR;
1191 }
1192
Eric Laurentb378b732016-12-01 15:28:29 -08001193 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001194 if (status != NO_ERROR) {
1195 if (thread != 0) {
Eric Laurentb378b732016-12-01 15:28:29 -08001196 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001197 }
1198 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001199 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001200 if (thread != 0) {
1201 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001202 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001203 Mutex::Autolock _l(t->mLock);
1204 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001205 }
Eric Laurentb378b732016-12-01 15:28:29 -08001206 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001207 if (thread->type() == ThreadBase::OFFLOAD) {
1208 PlaybackThread *t = (PlaybackThread *)thread.get();
1209 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1210 }
Eric Laurentb378b732016-12-01 15:28:29 -08001211 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001212 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1213 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001214 }
1215 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001216 }
1217 return status;
1218}
1219
1220status_t AudioFlinger::EffectHandle::disable()
1221{
1222 ALOGV("disable %p", this);
Eric Laurentb378b732016-12-01 15:28:29 -08001223 AutoMutex _l(mLock);
1224 sp<EffectModule> effect = mEffect.promote();
1225 if (effect == 0 || mDisconnected) {
1226 return DEAD_OBJECT;
1227 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001228 if (!mHasControl) {
1229 return INVALID_OPERATION;
1230 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001231
1232 if (!mEnabled) {
1233 return NO_ERROR;
1234 }
1235 mEnabled = false;
1236
Eric Laurentb378b732016-12-01 15:28:29 -08001237 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001238 return NO_ERROR;
1239 }
1240
Eric Laurentb378b732016-12-01 15:28:29 -08001241 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001242
Eric Laurentb378b732016-12-01 15:28:29 -08001243 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001244 if (thread != 0) {
Eric Laurentb378b732016-12-01 15:28:29 -08001245 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001246 if (thread->type() == ThreadBase::OFFLOAD) {
1247 PlaybackThread *t = (PlaybackThread *)thread.get();
1248 Mutex::Autolock _l(t->mLock);
1249 t->broadcast_l();
1250 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001251 }
1252
1253 return status;
1254}
1255
1256void AudioFlinger::EffectHandle::disconnect()
1257{
Eric Laurentb378b732016-12-01 15:28:29 -08001258 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001259 disconnect(true);
1260}
1261
1262void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1263{
Eric Laurentb378b732016-12-01 15:28:29 -08001264 AutoMutex _l(mLock);
1265 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1266 if (mDisconnected) {
1267 if (unpinIfLast) {
1268 android_errorWriteLog(0x534e4554, "32707507");
1269 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001270 return;
1271 }
Eric Laurentb378b732016-12-01 15:28:29 -08001272 mDisconnected = true;
1273 sp<ThreadBase> thread;
1274 {
1275 sp<EffectModule> effect = mEffect.promote();
1276 if (effect != 0) {
1277 thread = effect->thread().promote();
1278 }
1279 }
1280 if (thread != 0) {
1281 thread->disconnectEffectHandle(this, unpinIfLast);
1282 } else {
1283 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1284 // try to cleanup as much as we can
1285 sp<EffectModule> effect = mEffect.promote();
1286 if (effect != 0) {
1287 effect->disconnectHandle(this, unpinIfLast);
Eric Laurentca7cc822012-11-19 14:55:58 -08001288 }
1289 }
1290
Eric Laurentca7cc822012-11-19 14:55:58 -08001291 if (mClient != 0) {
1292 if (mCblk != NULL) {
1293 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1294 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1295 }
1296 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001297 // Client destructor must run with AudioFlinger client mutex locked
1298 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001299 mClient.clear();
1300 }
1301}
1302
1303status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1304 uint32_t cmdSize,
1305 void *pCmdData,
1306 uint32_t *replySize,
1307 void *pReplyData)
1308{
1309 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurentb378b732016-12-01 15:28:29 -08001310 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001311
Eric Laurentb378b732016-12-01 15:28:29 -08001312 AutoMutex _l(mLock);
1313 sp<EffectModule> effect = mEffect.promote();
1314 if (effect == 0 || mDisconnected) {
1315 return DEAD_OBJECT;
1316 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001317 // only get parameter command is permitted for applications not controlling the effect
1318 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1319 return INVALID_OPERATION;
1320 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001321 if (mClient == 0) {
1322 return INVALID_OPERATION;
1323 }
1324
1325 // handle commands that are not forwarded transparently to effect engine
1326 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1327 // No need to trylock() here as this function is executed in the binder thread serving a
1328 // particular client process: no risk to block the whole media server process or mixer
1329 // threads if we are stuck here
1330 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001331
1332 // keep local copy of index in case of client corruption b/32220769
1333 const uint32_t clientIndex = mCblk->clientIndex;
1334 const uint32_t serverIndex = mCblk->serverIndex;
1335 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1336 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001337 mCblk->serverIndex = 0;
1338 mCblk->clientIndex = 0;
1339 return BAD_VALUE;
1340 }
1341 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001342 effect_param_t *param = NULL;
1343 for (uint32_t index = serverIndex; index < clientIndex;) {
1344 int *p = (int *)(mBuffer + index);
1345 const int size = *p++;
1346 if (size < 0
1347 || size > EFFECT_PARAM_BUFFER_SIZE
1348 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001349 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001350 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001351 break;
1352 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001353
1354 // copy to local memory in case of client corruption b/32220769
1355 param = (effect_param_t *)realloc(param, size);
1356 if (param == NULL) {
1357 ALOGW("command(): out of memory");
1358 status = NO_MEMORY;
1359 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001360 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001361 memcpy(param, p, size);
1362
1363 int reply = 0;
1364 uint32_t rsize = sizeof(reply);
Eric Laurentb378b732016-12-01 15:28:29 -08001365 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001366 size,
1367 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001368 &rsize,
1369 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001370
1371 // verify shared memory: server index shouldn't change; client index can't go back.
1372 if (serverIndex != mCblk->serverIndex
1373 || clientIndex > mCblk->clientIndex) {
1374 android_errorWriteLog(0x534e4554, "32220769");
1375 status = BAD_VALUE;
1376 break;
1377 }
1378
Eric Laurentca7cc822012-11-19 14:55:58 -08001379 // stop at first error encountered
1380 if (ret != NO_ERROR) {
1381 status = ret;
1382 *(int *)pReplyData = reply;
1383 break;
1384 } else if (reply != NO_ERROR) {
1385 *(int *)pReplyData = reply;
1386 break;
1387 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001388 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001389 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001390 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001391 mCblk->serverIndex = 0;
1392 mCblk->clientIndex = 0;
1393 return status;
1394 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1395 *(int *)pReplyData = NO_ERROR;
1396 return enable();
1397 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1398 *(int *)pReplyData = NO_ERROR;
1399 return disable();
1400 }
1401
Eric Laurentb378b732016-12-01 15:28:29 -08001402 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001403}
1404
1405void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1406{
1407 ALOGV("setControl %p control %d", this, hasControl);
1408
1409 mHasControl = hasControl;
1410 mEnabled = enabled;
1411
1412 if (signal && mEffectClient != 0) {
1413 mEffectClient->controlStatusChanged(hasControl);
1414 }
1415}
1416
1417void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1418 uint32_t cmdSize,
1419 void *pCmdData,
1420 uint32_t replySize,
1421 void *pReplyData)
1422{
1423 if (mEffectClient != 0) {
1424 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1425 }
1426}
1427
1428
1429
1430void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1431{
1432 if (mEffectClient != 0) {
1433 mEffectClient->enableStatusChanged(enabled);
1434 }
1435}
1436
1437status_t AudioFlinger::EffectHandle::onTransact(
1438 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1439{
1440 return BnEffect::onTransact(code, data, reply, flags);
1441}
1442
1443
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001444void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001445{
1446 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1447
Marco Nelissenb2208842014-02-07 14:00:50 -08001448 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001449 (mClient == 0) ? getpid_cached : mClient->pid(),
1450 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001451 mHasControl ? "yes" : "no",
1452 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001453 mCblk ? mCblk->clientIndex : 0,
1454 mCblk ? mCblk->serverIndex : 0
1455 );
1456
1457 if (locked) {
1458 mCblk->lock.unlock();
1459 }
1460}
1461
1462#undef LOG_TAG
1463#define LOG_TAG "AudioFlinger::EffectChain"
1464
1465AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001466 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1468 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001469 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001470{
1471 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1472 if (thread == NULL) {
1473 return;
1474 }
1475 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1476 thread->frameCount();
1477}
1478
1479AudioFlinger::EffectChain::~EffectChain()
1480{
1481 if (mOwnInBuffer) {
1482 delete mInBuffer;
1483 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001484}
1485
1486// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1487sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1488 effect_descriptor_t *descriptor)
1489{
1490 size_t size = mEffects.size();
1491
1492 for (size_t i = 0; i < size; i++) {
1493 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1494 return mEffects[i];
1495 }
1496 }
1497 return 0;
1498}
1499
1500// getEffectFromId_l() must be called with ThreadBase::mLock held
1501sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1502{
1503 size_t size = mEffects.size();
1504
1505 for (size_t i = 0; i < size; i++) {
1506 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1507 if (id == 0 || mEffects[i]->id() == id) {
1508 return mEffects[i];
1509 }
1510 }
1511 return 0;
1512}
1513
1514// getEffectFromType_l() must be called with ThreadBase::mLock held
1515sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1516 const effect_uuid_t *type)
1517{
1518 size_t size = mEffects.size();
1519
1520 for (size_t i = 0; i < size; i++) {
1521 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1522 return mEffects[i];
1523 }
1524 }
1525 return 0;
1526}
1527
1528void AudioFlinger::EffectChain::clearInputBuffer()
1529{
1530 Mutex::Autolock _l(mLock);
1531 sp<ThreadBase> thread = mThread.promote();
1532 if (thread == 0) {
1533 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1534 return;
1535 }
1536 clearInputBuffer_l(thread);
1537}
1538
1539// Must be called with EffectChain::mLock locked
1540void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1541{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001542 // TODO: This will change in the future, depending on multichannel
1543 // and sample format changes for effects.
1544 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1545 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001546 const size_t frameSize =
1547 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001548 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001549}
1550
1551// Must be called with EffectChain::mLock locked
1552void AudioFlinger::EffectChain::process_l()
1553{
1554 sp<ThreadBase> thread = mThread.promote();
1555 if (thread == 0) {
1556 ALOGW("process_l(): cannot promote mixer thread");
1557 return;
1558 }
1559 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1560 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001561 // never process effects when:
1562 // - on an OFFLOAD thread
1563 // - no more tracks are on the session and the effect tail has been rendered
1564 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001565 if (!isGlobalSession) {
1566 bool tracksOnSession = (trackCnt() != 0);
1567
1568 if (!tracksOnSession && mTailBufferCount == 0) {
1569 doProcess = false;
1570 }
1571
1572 if (activeTrackCnt() == 0) {
1573 // if no track is active and the effect tail has not been rendered,
1574 // the input buffer must be cleared here as the mixer process will not do it
1575 if (tracksOnSession || mTailBufferCount > 0) {
1576 clearInputBuffer_l(thread);
1577 if (mTailBufferCount > 0) {
1578 mTailBufferCount--;
1579 }
1580 }
1581 }
1582 }
1583
1584 size_t size = mEffects.size();
1585 if (doProcess) {
1586 for (size_t i = 0; i < size; i++) {
1587 mEffects[i]->process();
1588 }
1589 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001590 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001591 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001592 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1593 }
1594 if (doResetVolume) {
1595 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001596 }
1597}
1598
Eric Laurentb378b732016-12-01 15:28:29 -08001599// createEffect_l() must be called with ThreadBase::mLock held
1600status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1601 ThreadBase *thread,
1602 effect_descriptor_t *desc,
1603 int id,
1604 audio_session_t sessionId,
1605 bool pinned)
1606{
1607 Mutex::Autolock _l(mLock);
1608 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1609 status_t lStatus = effect->status();
1610 if (lStatus == NO_ERROR) {
1611 lStatus = addEffect_ll(effect);
1612 }
1613 if (lStatus != NO_ERROR) {
1614 effect.clear();
1615 }
1616 return lStatus;
1617}
1618
1619// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001620status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1621{
Eric Laurentb378b732016-12-01 15:28:29 -08001622 Mutex::Autolock _l(mLock);
1623 return addEffect_ll(effect);
1624}
1625// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1626status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1627{
Eric Laurentca7cc822012-11-19 14:55:58 -08001628 effect_descriptor_t desc = effect->desc();
1629 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1630
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 effect->setChain(this);
1632 sp<ThreadBase> thread = mThread.promote();
1633 if (thread == 0) {
1634 return NO_INIT;
1635 }
1636 effect->setThread(thread);
1637
1638 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1639 // Auxiliary effects are inserted at the beginning of mEffects vector as
1640 // they are processed first and accumulated in chain input buffer
1641 mEffects.insertAt(effect, 0);
1642
1643 // the input buffer for auxiliary effect contains mono samples in
1644 // 32 bit format. This is to avoid saturation in AudoMixer
1645 // accumulation stage. Saturation is done in EffectModule::process() before
1646 // calling the process in effect engine
1647 size_t numSamples = thread->frameCount();
1648 int32_t *buffer = new int32_t[numSamples];
1649 memset(buffer, 0, numSamples * sizeof(int32_t));
1650 effect->setInBuffer((int16_t *)buffer);
1651 // auxiliary effects output samples to chain input buffer for further processing
1652 // by insert effects
1653 effect->setOutBuffer(mInBuffer);
1654 } else {
1655 // Insert effects are inserted at the end of mEffects vector as they are processed
1656 // after track and auxiliary effects.
1657 // Insert effect order as a function of indicated preference:
1658 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1659 // another effect is present
1660 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1661 // last effect claiming first position
1662 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1663 // first effect claiming last position
1664 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1665 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1666 // already present
1667
1668 size_t size = mEffects.size();
1669 size_t idx_insert = size;
1670 ssize_t idx_insert_first = -1;
1671 ssize_t idx_insert_last = -1;
1672
1673 for (size_t i = 0; i < size; i++) {
1674 effect_descriptor_t d = mEffects[i]->desc();
1675 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1676 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1677 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1678 // check invalid effect chaining combinations
1679 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1680 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1681 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1682 desc.name, d.name);
1683 return INVALID_OPERATION;
1684 }
1685 // remember position of first insert effect and by default
1686 // select this as insert position for new effect
1687 if (idx_insert == size) {
1688 idx_insert = i;
1689 }
1690 // remember position of last insert effect claiming
1691 // first position
1692 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1693 idx_insert_first = i;
1694 }
1695 // remember position of first insert effect claiming
1696 // last position
1697 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1698 idx_insert_last == -1) {
1699 idx_insert_last = i;
1700 }
1701 }
1702 }
1703
1704 // modify idx_insert from first position if needed
1705 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1706 if (idx_insert_last != -1) {
1707 idx_insert = idx_insert_last;
1708 } else {
1709 idx_insert = size;
1710 }
1711 } else {
1712 if (idx_insert_first != -1) {
1713 idx_insert = idx_insert_first + 1;
1714 }
1715 }
1716
1717 // always read samples from chain input buffer
1718 effect->setInBuffer(mInBuffer);
1719
1720 // if last effect in the chain, output samples to chain
1721 // output buffer, otherwise to chain input buffer
1722 if (idx_insert == size) {
1723 if (idx_insert != 0) {
1724 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1725 mEffects[idx_insert-1]->configure();
1726 }
1727 effect->setOutBuffer(mOutBuffer);
1728 } else {
1729 effect->setOutBuffer(mInBuffer);
1730 }
1731 mEffects.insertAt(effect, idx_insert);
1732
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001733 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001734 idx_insert);
1735 }
1736 effect->configure();
1737 return NO_ERROR;
1738}
1739
Eric Laurentb378b732016-12-01 15:28:29 -08001740// removeEffect_l() must be called with ThreadBase::mLock held
1741size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1742 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001743{
1744 Mutex::Autolock _l(mLock);
1745 size_t size = mEffects.size();
1746 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1747
1748 for (size_t i = 0; i < size; i++) {
1749 if (effect == mEffects[i]) {
1750 // calling stop here will remove pre-processing effect from the audio HAL.
1751 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1752 // the middle of a read from audio HAL
1753 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1754 mEffects[i]->state() == EffectModule::STOPPING) {
1755 mEffects[i]->stop();
1756 }
Eric Laurentb378b732016-12-01 15:28:29 -08001757 if (release) {
1758 mEffects[i]->release_l();
1759 }
1760
Eric Laurentca7cc822012-11-19 14:55:58 -08001761 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1762 delete[] effect->inBuffer();
1763 } else {
1764 if (i == size - 1 && i != 0) {
1765 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1766 mEffects[i - 1]->configure();
1767 }
1768 }
1769 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001770 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001771 this, i);
Eric Laurentb378b732016-12-01 15:28:29 -08001772
Eric Laurentca7cc822012-11-19 14:55:58 -08001773 break;
1774 }
1775 }
1776
1777 return mEffects.size();
1778}
1779
Eric Laurentb378b732016-12-01 15:28:29 -08001780// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001781void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1782{
1783 size_t size = mEffects.size();
1784 for (size_t i = 0; i < size; i++) {
1785 mEffects[i]->setDevice(device);
1786 }
1787}
1788
Eric Laurentb378b732016-12-01 15:28:29 -08001789// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001790void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1791{
1792 size_t size = mEffects.size();
1793 for (size_t i = 0; i < size; i++) {
1794 mEffects[i]->setMode(mode);
1795 }
1796}
1797
Eric Laurentb378b732016-12-01 15:28:29 -08001798// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001799void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1800{
1801 size_t size = mEffects.size();
1802 for (size_t i = 0; i < size; i++) {
1803 mEffects[i]->setAudioSource(source);
1804 }
1805}
1806
Eric Laurentb378b732016-12-01 15:28:29 -08001807// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001808bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001809{
1810 uint32_t newLeft = *left;
1811 uint32_t newRight = *right;
1812 bool hasControl = false;
1813 int ctrlIdx = -1;
1814 size_t size = mEffects.size();
1815
1816 // first update volume controller
1817 for (size_t i = size; i > 0; i--) {
1818 if (mEffects[i - 1]->isProcessEnabled() &&
1819 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1820 ctrlIdx = i - 1;
1821 hasControl = true;
1822 break;
1823 }
1824 }
1825
Eric Laurentfa1e1232016-08-02 19:01:49 -07001826 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001827 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001828 if (hasControl) {
1829 *left = mNewLeftVolume;
1830 *right = mNewRightVolume;
1831 }
1832 return hasControl;
1833 }
1834
1835 mVolumeCtrlIdx = ctrlIdx;
1836 mLeftVolume = newLeft;
1837 mRightVolume = newRight;
1838
1839 // second get volume update from volume controller
1840 if (ctrlIdx >= 0) {
1841 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1842 mNewLeftVolume = newLeft;
1843 mNewRightVolume = newRight;
1844 }
1845 // then indicate volume to all other effects in chain.
1846 // Pass altered volume to effects before volume controller
1847 // and requested volume to effects after controller
1848 uint32_t lVol = newLeft;
1849 uint32_t rVol = newRight;
1850
1851 for (size_t i = 0; i < size; i++) {
1852 if ((int)i == ctrlIdx) {
1853 continue;
1854 }
1855 // this also works for ctrlIdx == -1 when there is no volume controller
1856 if ((int)i > ctrlIdx) {
1857 lVol = *left;
1858 rVol = *right;
1859 }
1860 mEffects[i]->setVolume(&lVol, &rVol, false);
1861 }
1862 *left = newLeft;
1863 *right = newRight;
1864
1865 return hasControl;
1866}
1867
Eric Laurentb378b732016-12-01 15:28:29 -08001868// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001869void AudioFlinger::EffectChain::resetVolume_l()
1870{
Eric Laurente7449bf2016-08-03 18:44:07 -07001871 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1872 uint32_t left = mLeftVolume;
1873 uint32_t right = mRightVolume;
1874 (void)setVolume_l(&left, &right, true);
1875 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001876}
1877
Eric Laurent1b928682014-10-02 19:41:47 -07001878void AudioFlinger::EffectChain::syncHalEffectsState()
1879{
1880 Mutex::Autolock _l(mLock);
1881 for (size_t i = 0; i < mEffects.size(); i++) {
1882 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1883 mEffects[i]->state() == EffectModule::STOPPING) {
1884 mEffects[i]->addEffectToHal_l();
1885 }
1886 }
1887}
1888
Eric Laurentca7cc822012-11-19 14:55:58 -08001889void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1890{
1891 const size_t SIZE = 256;
1892 char buffer[SIZE];
1893 String8 result;
1894
Marco Nelissenb2208842014-02-07 14:00:50 -08001895 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001896 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001897 result.append(buffer);
1898
Marco Nelissenb2208842014-02-07 14:00:50 -08001899 if (numEffects) {
1900 bool locked = AudioFlinger::dumpTryLock(mLock);
1901 // failed to lock - AudioFlinger is probably deadlocked
1902 if (!locked) {
1903 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001904 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001905
Marco Nelissenb2208842014-02-07 14:00:50 -08001906 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001907 snprintf(buffer, SIZE, "\t%p %p %d\n",
1908 mInBuffer,
1909 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001910 mActiveTrackCnt);
1911 result.append(buffer);
1912 write(fd, result.string(), result.size());
1913
1914 for (size_t i = 0; i < numEffects; ++i) {
1915 sp<EffectModule> effect = mEffects[i];
1916 if (effect != 0) {
1917 effect->dump(fd, args);
1918 }
1919 }
1920
1921 if (locked) {
1922 mLock.unlock();
1923 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001924 }
1925}
1926
1927// must be called with ThreadBase::mLock held
1928void AudioFlinger::EffectChain::setEffectSuspended_l(
1929 const effect_uuid_t *type, bool suspend)
1930{
1931 sp<SuspendedEffectDesc> desc;
1932 // use effect type UUID timelow as key as there is no real risk of identical
1933 // timeLow fields among effect type UUIDs.
1934 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1935 if (suspend) {
1936 if (index >= 0) {
1937 desc = mSuspendedEffects.valueAt(index);
1938 } else {
1939 desc = new SuspendedEffectDesc();
1940 desc->mType = *type;
1941 mSuspendedEffects.add(type->timeLow, desc);
1942 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1943 }
1944 if (desc->mRefCount++ == 0) {
1945 sp<EffectModule> effect = getEffectIfEnabled(type);
1946 if (effect != 0) {
1947 desc->mEffect = effect;
1948 effect->setSuspended(true);
1949 effect->setEnabled(false);
1950 }
1951 }
1952 } else {
1953 if (index < 0) {
1954 return;
1955 }
1956 desc = mSuspendedEffects.valueAt(index);
1957 if (desc->mRefCount <= 0) {
1958 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1959 desc->mRefCount = 1;
1960 }
1961 if (--desc->mRefCount == 0) {
1962 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1963 if (desc->mEffect != 0) {
1964 sp<EffectModule> effect = desc->mEffect.promote();
1965 if (effect != 0) {
1966 effect->setSuspended(false);
1967 effect->lock();
1968 EffectHandle *handle = effect->controlHandle_l();
Eric Laurentb378b732016-12-01 15:28:29 -08001969 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001970 effect->setEnabled_l(handle->enabled());
1971 }
1972 effect->unlock();
1973 }
1974 desc->mEffect.clear();
1975 }
1976 mSuspendedEffects.removeItemsAt(index);
1977 }
1978 }
1979}
1980
1981// must be called with ThreadBase::mLock held
1982void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1983{
1984 sp<SuspendedEffectDesc> desc;
1985
1986 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1987 if (suspend) {
1988 if (index >= 0) {
1989 desc = mSuspendedEffects.valueAt(index);
1990 } else {
1991 desc = new SuspendedEffectDesc();
1992 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1993 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1994 }
1995 if (desc->mRefCount++ == 0) {
1996 Vector< sp<EffectModule> > effects;
1997 getSuspendEligibleEffects(effects);
1998 for (size_t i = 0; i < effects.size(); i++) {
1999 setEffectSuspended_l(&effects[i]->desc().type, true);
2000 }
2001 }
2002 } else {
2003 if (index < 0) {
2004 return;
2005 }
2006 desc = mSuspendedEffects.valueAt(index);
2007 if (desc->mRefCount <= 0) {
2008 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2009 desc->mRefCount = 1;
2010 }
2011 if (--desc->mRefCount == 0) {
2012 Vector<const effect_uuid_t *> types;
2013 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2014 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2015 continue;
2016 }
2017 types.add(&mSuspendedEffects.valueAt(i)->mType);
2018 }
2019 for (size_t i = 0; i < types.size(); i++) {
2020 setEffectSuspended_l(types[i], false);
2021 }
2022 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2023 mSuspendedEffects.keyAt(index));
2024 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2025 }
2026 }
2027}
2028
2029
2030// The volume effect is used for automated tests only
2031#ifndef OPENSL_ES_H_
2032static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2033 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2034const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2035#endif //OPENSL_ES_H_
2036
2037bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2038{
2039 // auxiliary effects and visualizer are never suspended on output mix
2040 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2041 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2042 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2043 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2044 return false;
2045 }
2046 return true;
2047}
2048
2049void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2050 Vector< sp<AudioFlinger::EffectModule> > &effects)
2051{
2052 effects.clear();
2053 for (size_t i = 0; i < mEffects.size(); i++) {
2054 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2055 effects.add(mEffects[i]);
2056 }
2057 }
2058}
2059
2060sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2061 const effect_uuid_t *type)
2062{
2063 sp<EffectModule> effect = getEffectFromType_l(type);
2064 return effect != 0 && effect->isEnabled() ? effect : 0;
2065}
2066
2067void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2068 bool enabled)
2069{
2070 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2071 if (enabled) {
2072 if (index < 0) {
2073 // if the effect is not suspend check if all effects are suspended
2074 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2075 if (index < 0) {
2076 return;
2077 }
2078 if (!isEffectEligibleForSuspend(effect->desc())) {
2079 return;
2080 }
2081 setEffectSuspended_l(&effect->desc().type, enabled);
2082 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2083 if (index < 0) {
2084 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2085 return;
2086 }
2087 }
2088 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2089 effect->desc().type.timeLow);
2090 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2091 // if effect is requested to suspended but was not yet enabled, supend it now.
2092 if (desc->mEffect == 0) {
2093 desc->mEffect = effect;
2094 effect->setEnabled(false);
2095 effect->setSuspended(true);
2096 }
2097 } else {
2098 if (index < 0) {
2099 return;
2100 }
2101 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2102 effect->desc().type.timeLow);
2103 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2104 desc->mEffect.clear();
2105 effect->setSuspended(false);
2106 }
2107}
2108
Eric Laurent5baf2af2013-09-12 17:37:00 -07002109bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002110{
2111 Mutex::Autolock _l(mLock);
2112 size_t size = mEffects.size();
2113 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002114 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002115 return true;
2116 }
2117 }
2118 return false;
2119}
2120
Eric Laurentaaa44472014-09-12 17:41:50 -07002121void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2122{
2123 Mutex::Autolock _l(mLock);
2124 mThread = thread;
2125 for (size_t i = 0; i < mEffects.size(); i++) {
2126 mEffects[i]->setThread(thread);
2127 }
2128}
2129
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002130void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2131{
2132 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2133 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2134 }
2135 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2136 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2137 }
2138}
2139
2140void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2141{
2142 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2143 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2144 }
2145 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2146 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2147 }
2148}
2149
2150bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002151{
2152 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002153 for (const auto &effect : mEffects) {
2154 if (effect->isProcessImplemented()) {
2155 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002156 }
2157 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002158 // Allow effects without processing.
2159 return true;
2160}
2161
2162bool AudioFlinger::EffectChain::isFastCompatible() const
2163{
2164 Mutex::Autolock _l(mLock);
2165 for (const auto &effect : mEffects) {
2166 if (effect->isProcessImplemented()
2167 && effect->isImplementationSoftware()) {
2168 return false;
2169 }
2170 }
2171 // Allow effects without processing or hw accelerated effects.
2172 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002173}
2174
2175// isCompatibleWithThread_l() must be called with thread->mLock held
2176bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2177{
2178 Mutex::Autolock _l(mLock);
2179 for (size_t i = 0; i < mEffects.size(); i++) {
2180 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2181 return false;
2182 }
2183 }
2184 return true;
2185}
2186
Glenn Kasten63238ef2015-03-02 15:50:29 -08002187} // namespace android