blob: b58868598d073ba13f6f824056e60c7588b84abe [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 Laurentc7ab3092017-06-15 18:43:46 -07001312 // reject commands reserved for internal use by audio framework if coming from outside
1313 // of audioserver
1314 switch(cmdCode) {
1315 case EFFECT_CMD_ENABLE:
1316 case EFFECT_CMD_DISABLE:
1317 case EFFECT_CMD_SET_PARAM:
1318 case EFFECT_CMD_SET_PARAM_DEFERRED:
1319 case EFFECT_CMD_SET_PARAM_COMMIT:
1320 case EFFECT_CMD_GET_PARAM:
1321 break;
1322 default:
1323 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1324 break;
1325 }
1326 android_errorWriteLog(0x534e4554, "62019992");
1327 return BAD_VALUE;
1328 }
1329
Eric Laurent31a45982016-12-15 14:46:09 -08001330 if (cmdCode == EFFECT_CMD_ENABLE) {
1331 if (*replySize < sizeof(int)) {
1332 android_errorWriteLog(0x534e4554, "32095713");
1333 return BAD_VALUE;
1334 }
1335 *(int *)pReplyData = NO_ERROR;
1336 *replySize = sizeof(int);
1337 return enable();
1338 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1339 if (*replySize < sizeof(int)) {
1340 android_errorWriteLog(0x534e4554, "32095713");
1341 return BAD_VALUE;
1342 }
1343 *(int *)pReplyData = NO_ERROR;
1344 *replySize = sizeof(int);
1345 return disable();
1346 }
1347
Eric Laurentb378b732016-12-01 15:28:29 -08001348 AutoMutex _l(mLock);
1349 sp<EffectModule> effect = mEffect.promote();
1350 if (effect == 0 || mDisconnected) {
1351 return DEAD_OBJECT;
1352 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001353 // only get parameter command is permitted for applications not controlling the effect
1354 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1355 return INVALID_OPERATION;
1356 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001357 if (mClient == 0) {
1358 return INVALID_OPERATION;
1359 }
1360
1361 // handle commands that are not forwarded transparently to effect engine
1362 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent31a45982016-12-15 14:46:09 -08001363 if (*replySize < sizeof(int)) {
1364 android_errorWriteLog(0x534e4554, "32095713");
1365 return BAD_VALUE;
1366 }
1367 *(int *)pReplyData = NO_ERROR;
1368 *replySize = sizeof(int);
1369
Eric Laurentca7cc822012-11-19 14:55:58 -08001370 // No need to trylock() here as this function is executed in the binder thread serving a
1371 // particular client process: no risk to block the whole media server process or mixer
1372 // threads if we are stuck here
1373 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001374 // keep local copy of index in case of client corruption b/32220769
1375 const uint32_t clientIndex = mCblk->clientIndex;
1376 const uint32_t serverIndex = mCblk->serverIndex;
1377 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1378 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001379 mCblk->serverIndex = 0;
1380 mCblk->clientIndex = 0;
1381 return BAD_VALUE;
1382 }
1383 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001384 effect_param_t *param = NULL;
1385 for (uint32_t index = serverIndex; index < clientIndex;) {
1386 int *p = (int *)(mBuffer + index);
1387 const int size = *p++;
1388 if (size < 0
1389 || size > EFFECT_PARAM_BUFFER_SIZE
1390 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001391 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001392 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001393 break;
1394 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001395
1396 // copy to local memory in case of client corruption b/32220769
1397 param = (effect_param_t *)realloc(param, size);
1398 if (param == NULL) {
1399 ALOGW("command(): out of memory");
1400 status = NO_MEMORY;
1401 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001403 memcpy(param, p, size);
1404
1405 int reply = 0;
1406 uint32_t rsize = sizeof(reply);
Eric Laurentb378b732016-12-01 15:28:29 -08001407 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001408 size,
1409 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 &rsize,
1411 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001412
1413 // verify shared memory: server index shouldn't change; client index can't go back.
1414 if (serverIndex != mCblk->serverIndex
1415 || clientIndex > mCblk->clientIndex) {
1416 android_errorWriteLog(0x534e4554, "32220769");
1417 status = BAD_VALUE;
1418 break;
1419 }
1420
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 // stop at first error encountered
1422 if (ret != NO_ERROR) {
1423 status = ret;
1424 *(int *)pReplyData = reply;
1425 break;
1426 } else if (reply != NO_ERROR) {
1427 *(int *)pReplyData = reply;
1428 break;
1429 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001430 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001432 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001433 mCblk->serverIndex = 0;
1434 mCblk->clientIndex = 0;
1435 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001436 }
1437
Eric Laurentb378b732016-12-01 15:28:29 -08001438 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001439}
1440
1441void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1442{
1443 ALOGV("setControl %p control %d", this, hasControl);
1444
1445 mHasControl = hasControl;
1446 mEnabled = enabled;
1447
1448 if (signal && mEffectClient != 0) {
1449 mEffectClient->controlStatusChanged(hasControl);
1450 }
1451}
1452
1453void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1454 uint32_t cmdSize,
1455 void *pCmdData,
1456 uint32_t replySize,
1457 void *pReplyData)
1458{
1459 if (mEffectClient != 0) {
1460 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1461 }
1462}
1463
1464
1465
1466void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1467{
1468 if (mEffectClient != 0) {
1469 mEffectClient->enableStatusChanged(enabled);
1470 }
1471}
1472
1473status_t AudioFlinger::EffectHandle::onTransact(
1474 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1475{
1476 return BnEffect::onTransact(code, data, reply, flags);
1477}
1478
1479
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001480void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001481{
1482 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1483
Marco Nelissenb2208842014-02-07 14:00:50 -08001484 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001485 (mClient == 0) ? getpid_cached : mClient->pid(),
1486 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001487 mHasControl ? "yes" : "no",
1488 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001489 mCblk ? mCblk->clientIndex : 0,
1490 mCblk ? mCblk->serverIndex : 0
1491 );
1492
1493 if (locked) {
1494 mCblk->lock.unlock();
1495 }
1496}
1497
1498#undef LOG_TAG
1499#define LOG_TAG "AudioFlinger::EffectChain"
1500
1501AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001502 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001503 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1504 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001505 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001506{
1507 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1508 if (thread == NULL) {
1509 return;
1510 }
1511 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1512 thread->frameCount();
1513}
1514
1515AudioFlinger::EffectChain::~EffectChain()
1516{
1517 if (mOwnInBuffer) {
1518 delete mInBuffer;
1519 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001520}
1521
1522// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1523sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1524 effect_descriptor_t *descriptor)
1525{
1526 size_t size = mEffects.size();
1527
1528 for (size_t i = 0; i < size; i++) {
1529 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1530 return mEffects[i];
1531 }
1532 }
1533 return 0;
1534}
1535
1536// getEffectFromId_l() must be called with ThreadBase::mLock held
1537sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1538{
1539 size_t size = mEffects.size();
1540
1541 for (size_t i = 0; i < size; i++) {
1542 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1543 if (id == 0 || mEffects[i]->id() == id) {
1544 return mEffects[i];
1545 }
1546 }
1547 return 0;
1548}
1549
1550// getEffectFromType_l() must be called with ThreadBase::mLock held
1551sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1552 const effect_uuid_t *type)
1553{
1554 size_t size = mEffects.size();
1555
1556 for (size_t i = 0; i < size; i++) {
1557 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1558 return mEffects[i];
1559 }
1560 }
1561 return 0;
1562}
1563
1564void AudioFlinger::EffectChain::clearInputBuffer()
1565{
1566 Mutex::Autolock _l(mLock);
1567 sp<ThreadBase> thread = mThread.promote();
1568 if (thread == 0) {
1569 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1570 return;
1571 }
1572 clearInputBuffer_l(thread);
1573}
1574
1575// Must be called with EffectChain::mLock locked
1576void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1577{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001578 // TODO: This will change in the future, depending on multichannel
1579 // and sample format changes for effects.
1580 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1581 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001582 const size_t frameSize =
1583 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001584 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001585}
1586
1587// Must be called with EffectChain::mLock locked
1588void AudioFlinger::EffectChain::process_l()
1589{
1590 sp<ThreadBase> thread = mThread.promote();
1591 if (thread == 0) {
1592 ALOGW("process_l(): cannot promote mixer thread");
1593 return;
1594 }
1595 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1596 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001597 // never process effects when:
1598 // - on an OFFLOAD thread
1599 // - no more tracks are on the session and the effect tail has been rendered
1600 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001601 if (!isGlobalSession) {
1602 bool tracksOnSession = (trackCnt() != 0);
1603
1604 if (!tracksOnSession && mTailBufferCount == 0) {
1605 doProcess = false;
1606 }
1607
1608 if (activeTrackCnt() == 0) {
1609 // if no track is active and the effect tail has not been rendered,
1610 // the input buffer must be cleared here as the mixer process will not do it
1611 if (tracksOnSession || mTailBufferCount > 0) {
1612 clearInputBuffer_l(thread);
1613 if (mTailBufferCount > 0) {
1614 mTailBufferCount--;
1615 }
1616 }
1617 }
1618 }
1619
1620 size_t size = mEffects.size();
1621 if (doProcess) {
1622 for (size_t i = 0; i < size; i++) {
1623 mEffects[i]->process();
1624 }
1625 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001626 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001627 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001628 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1629 }
1630 if (doResetVolume) {
1631 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001632 }
1633}
1634
Eric Laurentb378b732016-12-01 15:28:29 -08001635// createEffect_l() must be called with ThreadBase::mLock held
1636status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1637 ThreadBase *thread,
1638 effect_descriptor_t *desc,
1639 int id,
1640 audio_session_t sessionId,
1641 bool pinned)
1642{
1643 Mutex::Autolock _l(mLock);
1644 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1645 status_t lStatus = effect->status();
1646 if (lStatus == NO_ERROR) {
1647 lStatus = addEffect_ll(effect);
1648 }
1649 if (lStatus != NO_ERROR) {
1650 effect.clear();
1651 }
1652 return lStatus;
1653}
1654
1655// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001656status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1657{
Eric Laurentb378b732016-12-01 15:28:29 -08001658 Mutex::Autolock _l(mLock);
1659 return addEffect_ll(effect);
1660}
1661// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1662status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1663{
Eric Laurentca7cc822012-11-19 14:55:58 -08001664 effect_descriptor_t desc = effect->desc();
1665 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1666
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 effect->setChain(this);
1668 sp<ThreadBase> thread = mThread.promote();
1669 if (thread == 0) {
1670 return NO_INIT;
1671 }
1672 effect->setThread(thread);
1673
1674 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1675 // Auxiliary effects are inserted at the beginning of mEffects vector as
1676 // they are processed first and accumulated in chain input buffer
1677 mEffects.insertAt(effect, 0);
1678
1679 // the input buffer for auxiliary effect contains mono samples in
1680 // 32 bit format. This is to avoid saturation in AudoMixer
1681 // accumulation stage. Saturation is done in EffectModule::process() before
1682 // calling the process in effect engine
1683 size_t numSamples = thread->frameCount();
1684 int32_t *buffer = new int32_t[numSamples];
1685 memset(buffer, 0, numSamples * sizeof(int32_t));
1686 effect->setInBuffer((int16_t *)buffer);
1687 // auxiliary effects output samples to chain input buffer for further processing
1688 // by insert effects
1689 effect->setOutBuffer(mInBuffer);
1690 } else {
1691 // Insert effects are inserted at the end of mEffects vector as they are processed
1692 // after track and auxiliary effects.
1693 // Insert effect order as a function of indicated preference:
1694 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1695 // another effect is present
1696 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1697 // last effect claiming first position
1698 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1699 // first effect claiming last position
1700 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1701 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1702 // already present
1703
1704 size_t size = mEffects.size();
1705 size_t idx_insert = size;
1706 ssize_t idx_insert_first = -1;
1707 ssize_t idx_insert_last = -1;
1708
1709 for (size_t i = 0; i < size; i++) {
1710 effect_descriptor_t d = mEffects[i]->desc();
1711 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1712 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1713 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1714 // check invalid effect chaining combinations
1715 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1716 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1717 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1718 desc.name, d.name);
1719 return INVALID_OPERATION;
1720 }
1721 // remember position of first insert effect and by default
1722 // select this as insert position for new effect
1723 if (idx_insert == size) {
1724 idx_insert = i;
1725 }
1726 // remember position of last insert effect claiming
1727 // first position
1728 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1729 idx_insert_first = i;
1730 }
1731 // remember position of first insert effect claiming
1732 // last position
1733 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1734 idx_insert_last == -1) {
1735 idx_insert_last = i;
1736 }
1737 }
1738 }
1739
1740 // modify idx_insert from first position if needed
1741 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1742 if (idx_insert_last != -1) {
1743 idx_insert = idx_insert_last;
1744 } else {
1745 idx_insert = size;
1746 }
1747 } else {
1748 if (idx_insert_first != -1) {
1749 idx_insert = idx_insert_first + 1;
1750 }
1751 }
1752
1753 // always read samples from chain input buffer
1754 effect->setInBuffer(mInBuffer);
1755
1756 // if last effect in the chain, output samples to chain
1757 // output buffer, otherwise to chain input buffer
1758 if (idx_insert == size) {
1759 if (idx_insert != 0) {
1760 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1761 mEffects[idx_insert-1]->configure();
1762 }
1763 effect->setOutBuffer(mOutBuffer);
1764 } else {
1765 effect->setOutBuffer(mInBuffer);
1766 }
1767 mEffects.insertAt(effect, idx_insert);
1768
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001769 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001770 idx_insert);
1771 }
1772 effect->configure();
1773 return NO_ERROR;
1774}
1775
Eric Laurentb378b732016-12-01 15:28:29 -08001776// removeEffect_l() must be called with ThreadBase::mLock held
1777size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1778 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001779{
1780 Mutex::Autolock _l(mLock);
1781 size_t size = mEffects.size();
1782 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1783
1784 for (size_t i = 0; i < size; i++) {
1785 if (effect == mEffects[i]) {
1786 // calling stop here will remove pre-processing effect from the audio HAL.
1787 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1788 // the middle of a read from audio HAL
1789 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1790 mEffects[i]->state() == EffectModule::STOPPING) {
1791 mEffects[i]->stop();
1792 }
Eric Laurentb378b732016-12-01 15:28:29 -08001793 if (release) {
1794 mEffects[i]->release_l();
1795 }
1796
Eric Laurentca7cc822012-11-19 14:55:58 -08001797 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1798 delete[] effect->inBuffer();
1799 } else {
1800 if (i == size - 1 && i != 0) {
1801 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1802 mEffects[i - 1]->configure();
1803 }
1804 }
1805 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001806 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001807 this, i);
Eric Laurentb378b732016-12-01 15:28:29 -08001808
Eric Laurentca7cc822012-11-19 14:55:58 -08001809 break;
1810 }
1811 }
1812
1813 return mEffects.size();
1814}
1815
Eric Laurentb378b732016-12-01 15:28:29 -08001816// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001817void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1818{
1819 size_t size = mEffects.size();
1820 for (size_t i = 0; i < size; i++) {
1821 mEffects[i]->setDevice(device);
1822 }
1823}
1824
Eric Laurentb378b732016-12-01 15:28:29 -08001825// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001826void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1827{
1828 size_t size = mEffects.size();
1829 for (size_t i = 0; i < size; i++) {
1830 mEffects[i]->setMode(mode);
1831 }
1832}
1833
Eric Laurentb378b732016-12-01 15:28:29 -08001834// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001835void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1836{
1837 size_t size = mEffects.size();
1838 for (size_t i = 0; i < size; i++) {
1839 mEffects[i]->setAudioSource(source);
1840 }
1841}
1842
Eric Laurentb378b732016-12-01 15:28:29 -08001843// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001844bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001845{
1846 uint32_t newLeft = *left;
1847 uint32_t newRight = *right;
1848 bool hasControl = false;
1849 int ctrlIdx = -1;
1850 size_t size = mEffects.size();
1851
1852 // first update volume controller
1853 for (size_t i = size; i > 0; i--) {
1854 if (mEffects[i - 1]->isProcessEnabled() &&
1855 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1856 ctrlIdx = i - 1;
1857 hasControl = true;
1858 break;
1859 }
1860 }
1861
Eric Laurentfa1e1232016-08-02 19:01:49 -07001862 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001863 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 if (hasControl) {
1865 *left = mNewLeftVolume;
1866 *right = mNewRightVolume;
1867 }
1868 return hasControl;
1869 }
1870
1871 mVolumeCtrlIdx = ctrlIdx;
1872 mLeftVolume = newLeft;
1873 mRightVolume = newRight;
1874
1875 // second get volume update from volume controller
1876 if (ctrlIdx >= 0) {
1877 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1878 mNewLeftVolume = newLeft;
1879 mNewRightVolume = newRight;
1880 }
1881 // then indicate volume to all other effects in chain.
1882 // Pass altered volume to effects before volume controller
1883 // and requested volume to effects after controller
1884 uint32_t lVol = newLeft;
1885 uint32_t rVol = newRight;
1886
1887 for (size_t i = 0; i < size; i++) {
1888 if ((int)i == ctrlIdx) {
1889 continue;
1890 }
1891 // this also works for ctrlIdx == -1 when there is no volume controller
1892 if ((int)i > ctrlIdx) {
1893 lVol = *left;
1894 rVol = *right;
1895 }
1896 mEffects[i]->setVolume(&lVol, &rVol, false);
1897 }
1898 *left = newLeft;
1899 *right = newRight;
1900
1901 return hasControl;
1902}
1903
Eric Laurentb378b732016-12-01 15:28:29 -08001904// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001905void AudioFlinger::EffectChain::resetVolume_l()
1906{
Eric Laurente7449bf2016-08-03 18:44:07 -07001907 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1908 uint32_t left = mLeftVolume;
1909 uint32_t right = mRightVolume;
1910 (void)setVolume_l(&left, &right, true);
1911 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001912}
1913
Eric Laurent1b928682014-10-02 19:41:47 -07001914void AudioFlinger::EffectChain::syncHalEffectsState()
1915{
1916 Mutex::Autolock _l(mLock);
1917 for (size_t i = 0; i < mEffects.size(); i++) {
1918 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1919 mEffects[i]->state() == EffectModule::STOPPING) {
1920 mEffects[i]->addEffectToHal_l();
1921 }
1922 }
1923}
1924
Eric Laurentca7cc822012-11-19 14:55:58 -08001925void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1926{
1927 const size_t SIZE = 256;
1928 char buffer[SIZE];
1929 String8 result;
1930
Marco Nelissenb2208842014-02-07 14:00:50 -08001931 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001932 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001933 result.append(buffer);
1934
Marco Nelissenb2208842014-02-07 14:00:50 -08001935 if (numEffects) {
1936 bool locked = AudioFlinger::dumpTryLock(mLock);
1937 // failed to lock - AudioFlinger is probably deadlocked
1938 if (!locked) {
1939 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001940 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001941
Marco Nelissenb2208842014-02-07 14:00:50 -08001942 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001943 snprintf(buffer, SIZE, "\t%p %p %d\n",
1944 mInBuffer,
1945 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001946 mActiveTrackCnt);
1947 result.append(buffer);
1948 write(fd, result.string(), result.size());
1949
1950 for (size_t i = 0; i < numEffects; ++i) {
1951 sp<EffectModule> effect = mEffects[i];
1952 if (effect != 0) {
1953 effect->dump(fd, args);
1954 }
1955 }
1956
1957 if (locked) {
1958 mLock.unlock();
1959 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001960 }
1961}
1962
1963// must be called with ThreadBase::mLock held
1964void AudioFlinger::EffectChain::setEffectSuspended_l(
1965 const effect_uuid_t *type, bool suspend)
1966{
1967 sp<SuspendedEffectDesc> desc;
1968 // use effect type UUID timelow as key as there is no real risk of identical
1969 // timeLow fields among effect type UUIDs.
1970 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1971 if (suspend) {
1972 if (index >= 0) {
1973 desc = mSuspendedEffects.valueAt(index);
1974 } else {
1975 desc = new SuspendedEffectDesc();
1976 desc->mType = *type;
1977 mSuspendedEffects.add(type->timeLow, desc);
1978 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1979 }
1980 if (desc->mRefCount++ == 0) {
1981 sp<EffectModule> effect = getEffectIfEnabled(type);
1982 if (effect != 0) {
1983 desc->mEffect = effect;
1984 effect->setSuspended(true);
1985 effect->setEnabled(false);
1986 }
1987 }
1988 } else {
1989 if (index < 0) {
1990 return;
1991 }
1992 desc = mSuspendedEffects.valueAt(index);
1993 if (desc->mRefCount <= 0) {
1994 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1995 desc->mRefCount = 1;
1996 }
1997 if (--desc->mRefCount == 0) {
1998 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1999 if (desc->mEffect != 0) {
2000 sp<EffectModule> effect = desc->mEffect.promote();
2001 if (effect != 0) {
2002 effect->setSuspended(false);
2003 effect->lock();
2004 EffectHandle *handle = effect->controlHandle_l();
Eric Laurentb378b732016-12-01 15:28:29 -08002005 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002006 effect->setEnabled_l(handle->enabled());
2007 }
2008 effect->unlock();
2009 }
2010 desc->mEffect.clear();
2011 }
2012 mSuspendedEffects.removeItemsAt(index);
2013 }
2014 }
2015}
2016
2017// must be called with ThreadBase::mLock held
2018void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2019{
2020 sp<SuspendedEffectDesc> desc;
2021
2022 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2023 if (suspend) {
2024 if (index >= 0) {
2025 desc = mSuspendedEffects.valueAt(index);
2026 } else {
2027 desc = new SuspendedEffectDesc();
2028 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2029 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2030 }
2031 if (desc->mRefCount++ == 0) {
2032 Vector< sp<EffectModule> > effects;
2033 getSuspendEligibleEffects(effects);
2034 for (size_t i = 0; i < effects.size(); i++) {
2035 setEffectSuspended_l(&effects[i]->desc().type, true);
2036 }
2037 }
2038 } else {
2039 if (index < 0) {
2040 return;
2041 }
2042 desc = mSuspendedEffects.valueAt(index);
2043 if (desc->mRefCount <= 0) {
2044 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2045 desc->mRefCount = 1;
2046 }
2047 if (--desc->mRefCount == 0) {
2048 Vector<const effect_uuid_t *> types;
2049 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2050 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2051 continue;
2052 }
2053 types.add(&mSuspendedEffects.valueAt(i)->mType);
2054 }
2055 for (size_t i = 0; i < types.size(); i++) {
2056 setEffectSuspended_l(types[i], false);
2057 }
2058 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2059 mSuspendedEffects.keyAt(index));
2060 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2061 }
2062 }
2063}
2064
2065
2066// The volume effect is used for automated tests only
2067#ifndef OPENSL_ES_H_
2068static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2069 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2070const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2071#endif //OPENSL_ES_H_
2072
2073bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2074{
2075 // auxiliary effects and visualizer are never suspended on output mix
2076 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2077 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2078 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2079 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2080 return false;
2081 }
2082 return true;
2083}
2084
2085void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2086 Vector< sp<AudioFlinger::EffectModule> > &effects)
2087{
2088 effects.clear();
2089 for (size_t i = 0; i < mEffects.size(); i++) {
2090 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2091 effects.add(mEffects[i]);
2092 }
2093 }
2094}
2095
2096sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2097 const effect_uuid_t *type)
2098{
2099 sp<EffectModule> effect = getEffectFromType_l(type);
2100 return effect != 0 && effect->isEnabled() ? effect : 0;
2101}
2102
2103void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2104 bool enabled)
2105{
2106 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2107 if (enabled) {
2108 if (index < 0) {
2109 // if the effect is not suspend check if all effects are suspended
2110 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2111 if (index < 0) {
2112 return;
2113 }
2114 if (!isEffectEligibleForSuspend(effect->desc())) {
2115 return;
2116 }
2117 setEffectSuspended_l(&effect->desc().type, enabled);
2118 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2119 if (index < 0) {
2120 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2121 return;
2122 }
2123 }
2124 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2125 effect->desc().type.timeLow);
2126 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2127 // if effect is requested to suspended but was not yet enabled, supend it now.
2128 if (desc->mEffect == 0) {
2129 desc->mEffect = effect;
2130 effect->setEnabled(false);
2131 effect->setSuspended(true);
2132 }
2133 } else {
2134 if (index < 0) {
2135 return;
2136 }
2137 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2138 effect->desc().type.timeLow);
2139 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2140 desc->mEffect.clear();
2141 effect->setSuspended(false);
2142 }
2143}
2144
Eric Laurent5baf2af2013-09-12 17:37:00 -07002145bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002146{
2147 Mutex::Autolock _l(mLock);
2148 size_t size = mEffects.size();
2149 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002150 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002151 return true;
2152 }
2153 }
2154 return false;
2155}
2156
Eric Laurentaaa44472014-09-12 17:41:50 -07002157void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2158{
2159 Mutex::Autolock _l(mLock);
2160 mThread = thread;
2161 for (size_t i = 0; i < mEffects.size(); i++) {
2162 mEffects[i]->setThread(thread);
2163 }
2164}
2165
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002166void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2167{
2168 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2169 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2170 }
2171 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2172 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2173 }
2174}
2175
2176void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2177{
2178 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2179 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2180 }
2181 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2182 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2183 }
2184}
2185
2186bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002187{
2188 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002189 for (const auto &effect : mEffects) {
2190 if (effect->isProcessImplemented()) {
2191 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002192 }
2193 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002194 // Allow effects without processing.
2195 return true;
2196}
2197
2198bool AudioFlinger::EffectChain::isFastCompatible() const
2199{
2200 Mutex::Autolock _l(mLock);
2201 for (const auto &effect : mEffects) {
2202 if (effect->isProcessImplemented()
2203 && effect->isImplementationSoftware()) {
2204 return false;
2205 }
2206 }
2207 // Allow effects without processing or hw accelerated effects.
2208 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002209}
2210
2211// isCompatibleWithThread_l() must be called with thread->mLock held
2212bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2213{
2214 Mutex::Autolock _l(mLock);
2215 for (size_t i = 0; i < mEffects.size(); i++) {
2216 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2217 return false;
2218 }
2219 }
2220 return true;
2221}
2222
Glenn Kasten63238ef2015-03-02 15:50:29 -08002223} // namespace android