blob: f908d6df3a127dd77c69ff4224436ce439a0d446 [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 Laurent31a45982016-12-15 14:46:09 -08001312 if (cmdCode == EFFECT_CMD_ENABLE) {
1313 if (*replySize < sizeof(int)) {
1314 android_errorWriteLog(0x534e4554, "32095713");
1315 return BAD_VALUE;
1316 }
1317 *(int *)pReplyData = NO_ERROR;
1318 *replySize = sizeof(int);
1319 return enable();
1320 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1321 if (*replySize < sizeof(int)) {
1322 android_errorWriteLog(0x534e4554, "32095713");
1323 return BAD_VALUE;
1324 }
1325 *(int *)pReplyData = NO_ERROR;
1326 *replySize = sizeof(int);
1327 return disable();
1328 }
1329
Eric Laurentb378b732016-12-01 15:28:29 -08001330 AutoMutex _l(mLock);
1331 sp<EffectModule> effect = mEffect.promote();
1332 if (effect == 0 || mDisconnected) {
1333 return DEAD_OBJECT;
1334 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001335 // only get parameter command is permitted for applications not controlling the effect
1336 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1337 return INVALID_OPERATION;
1338 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001339 if (mClient == 0) {
1340 return INVALID_OPERATION;
1341 }
1342
1343 // handle commands that are not forwarded transparently to effect engine
1344 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent31a45982016-12-15 14:46:09 -08001345 if (*replySize < sizeof(int)) {
1346 android_errorWriteLog(0x534e4554, "32095713");
1347 return BAD_VALUE;
1348 }
1349 *(int *)pReplyData = NO_ERROR;
1350 *replySize = sizeof(int);
1351
Eric Laurentca7cc822012-11-19 14:55:58 -08001352 // No need to trylock() here as this function is executed in the binder thread serving a
1353 // particular client process: no risk to block the whole media server process or mixer
1354 // threads if we are stuck here
1355 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001356 // keep local copy of index in case of client corruption b/32220769
1357 const uint32_t clientIndex = mCblk->clientIndex;
1358 const uint32_t serverIndex = mCblk->serverIndex;
1359 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1360 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001361 mCblk->serverIndex = 0;
1362 mCblk->clientIndex = 0;
1363 return BAD_VALUE;
1364 }
1365 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001366 effect_param_t *param = NULL;
1367 for (uint32_t index = serverIndex; index < clientIndex;) {
1368 int *p = (int *)(mBuffer + index);
1369 const int size = *p++;
1370 if (size < 0
1371 || size > EFFECT_PARAM_BUFFER_SIZE
1372 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001373 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001374 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001375 break;
1376 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001377
1378 // copy to local memory in case of client corruption b/32220769
1379 param = (effect_param_t *)realloc(param, size);
1380 if (param == NULL) {
1381 ALOGW("command(): out of memory");
1382 status = NO_MEMORY;
1383 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001384 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001385 memcpy(param, p, size);
1386
1387 int reply = 0;
1388 uint32_t rsize = sizeof(reply);
Eric Laurentb378b732016-12-01 15:28:29 -08001389 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001390 size,
1391 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001392 &rsize,
1393 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001394
1395 // verify shared memory: server index shouldn't change; client index can't go back.
1396 if (serverIndex != mCblk->serverIndex
1397 || clientIndex > mCblk->clientIndex) {
1398 android_errorWriteLog(0x534e4554, "32220769");
1399 status = BAD_VALUE;
1400 break;
1401 }
1402
Eric Laurentca7cc822012-11-19 14:55:58 -08001403 // stop at first error encountered
1404 if (ret != NO_ERROR) {
1405 status = ret;
1406 *(int *)pReplyData = reply;
1407 break;
1408 } else if (reply != NO_ERROR) {
1409 *(int *)pReplyData = reply;
1410 break;
1411 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001412 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001413 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001414 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001415 mCblk->serverIndex = 0;
1416 mCblk->clientIndex = 0;
1417 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001418 }
1419
Eric Laurentb378b732016-12-01 15:28:29 -08001420 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001421}
1422
1423void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1424{
1425 ALOGV("setControl %p control %d", this, hasControl);
1426
1427 mHasControl = hasControl;
1428 mEnabled = enabled;
1429
1430 if (signal && mEffectClient != 0) {
1431 mEffectClient->controlStatusChanged(hasControl);
1432 }
1433}
1434
1435void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1436 uint32_t cmdSize,
1437 void *pCmdData,
1438 uint32_t replySize,
1439 void *pReplyData)
1440{
1441 if (mEffectClient != 0) {
1442 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1443 }
1444}
1445
1446
1447
1448void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1449{
1450 if (mEffectClient != 0) {
1451 mEffectClient->enableStatusChanged(enabled);
1452 }
1453}
1454
1455status_t AudioFlinger::EffectHandle::onTransact(
1456 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1457{
1458 return BnEffect::onTransact(code, data, reply, flags);
1459}
1460
1461
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001462void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001463{
1464 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1465
Marco Nelissenb2208842014-02-07 14:00:50 -08001466 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 (mClient == 0) ? getpid_cached : mClient->pid(),
1468 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001469 mHasControl ? "yes" : "no",
1470 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001471 mCblk ? mCblk->clientIndex : 0,
1472 mCblk ? mCblk->serverIndex : 0
1473 );
1474
1475 if (locked) {
1476 mCblk->lock.unlock();
1477 }
1478}
1479
1480#undef LOG_TAG
1481#define LOG_TAG "AudioFlinger::EffectChain"
1482
1483AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001484 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001485 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1486 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001487 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001488{
1489 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1490 if (thread == NULL) {
1491 return;
1492 }
1493 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1494 thread->frameCount();
1495}
1496
1497AudioFlinger::EffectChain::~EffectChain()
1498{
1499 if (mOwnInBuffer) {
1500 delete mInBuffer;
1501 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001502}
1503
1504// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1505sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1506 effect_descriptor_t *descriptor)
1507{
1508 size_t size = mEffects.size();
1509
1510 for (size_t i = 0; i < size; i++) {
1511 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1512 return mEffects[i];
1513 }
1514 }
1515 return 0;
1516}
1517
1518// getEffectFromId_l() must be called with ThreadBase::mLock held
1519sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1520{
1521 size_t size = mEffects.size();
1522
1523 for (size_t i = 0; i < size; i++) {
1524 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1525 if (id == 0 || mEffects[i]->id() == id) {
1526 return mEffects[i];
1527 }
1528 }
1529 return 0;
1530}
1531
1532// getEffectFromType_l() must be called with ThreadBase::mLock held
1533sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1534 const effect_uuid_t *type)
1535{
1536 size_t size = mEffects.size();
1537
1538 for (size_t i = 0; i < size; i++) {
1539 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1540 return mEffects[i];
1541 }
1542 }
1543 return 0;
1544}
1545
1546void AudioFlinger::EffectChain::clearInputBuffer()
1547{
1548 Mutex::Autolock _l(mLock);
1549 sp<ThreadBase> thread = mThread.promote();
1550 if (thread == 0) {
1551 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1552 return;
1553 }
1554 clearInputBuffer_l(thread);
1555}
1556
1557// Must be called with EffectChain::mLock locked
1558void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1559{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001560 // TODO: This will change in the future, depending on multichannel
1561 // and sample format changes for effects.
1562 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1563 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001564 const size_t frameSize =
1565 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001566 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001567}
1568
1569// Must be called with EffectChain::mLock locked
1570void AudioFlinger::EffectChain::process_l()
1571{
1572 sp<ThreadBase> thread = mThread.promote();
1573 if (thread == 0) {
1574 ALOGW("process_l(): cannot promote mixer thread");
1575 return;
1576 }
1577 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1578 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001579 // never process effects when:
1580 // - on an OFFLOAD thread
1581 // - no more tracks are on the session and the effect tail has been rendered
1582 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001583 if (!isGlobalSession) {
1584 bool tracksOnSession = (trackCnt() != 0);
1585
1586 if (!tracksOnSession && mTailBufferCount == 0) {
1587 doProcess = false;
1588 }
1589
1590 if (activeTrackCnt() == 0) {
1591 // if no track is active and the effect tail has not been rendered,
1592 // the input buffer must be cleared here as the mixer process will not do it
1593 if (tracksOnSession || mTailBufferCount > 0) {
1594 clearInputBuffer_l(thread);
1595 if (mTailBufferCount > 0) {
1596 mTailBufferCount--;
1597 }
1598 }
1599 }
1600 }
1601
1602 size_t size = mEffects.size();
1603 if (doProcess) {
1604 for (size_t i = 0; i < size; i++) {
1605 mEffects[i]->process();
1606 }
1607 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001608 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001609 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001610 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1611 }
1612 if (doResetVolume) {
1613 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001614 }
1615}
1616
Eric Laurentb378b732016-12-01 15:28:29 -08001617// createEffect_l() must be called with ThreadBase::mLock held
1618status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1619 ThreadBase *thread,
1620 effect_descriptor_t *desc,
1621 int id,
1622 audio_session_t sessionId,
1623 bool pinned)
1624{
1625 Mutex::Autolock _l(mLock);
1626 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1627 status_t lStatus = effect->status();
1628 if (lStatus == NO_ERROR) {
1629 lStatus = addEffect_ll(effect);
1630 }
1631 if (lStatus != NO_ERROR) {
1632 effect.clear();
1633 }
1634 return lStatus;
1635}
1636
1637// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001638status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1639{
Eric Laurentb378b732016-12-01 15:28:29 -08001640 Mutex::Autolock _l(mLock);
1641 return addEffect_ll(effect);
1642}
1643// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1644status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1645{
Eric Laurentca7cc822012-11-19 14:55:58 -08001646 effect_descriptor_t desc = effect->desc();
1647 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1648
Eric Laurentca7cc822012-11-19 14:55:58 -08001649 effect->setChain(this);
1650 sp<ThreadBase> thread = mThread.promote();
1651 if (thread == 0) {
1652 return NO_INIT;
1653 }
1654 effect->setThread(thread);
1655
1656 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1657 // Auxiliary effects are inserted at the beginning of mEffects vector as
1658 // they are processed first and accumulated in chain input buffer
1659 mEffects.insertAt(effect, 0);
1660
1661 // the input buffer for auxiliary effect contains mono samples in
1662 // 32 bit format. This is to avoid saturation in AudoMixer
1663 // accumulation stage. Saturation is done in EffectModule::process() before
1664 // calling the process in effect engine
1665 size_t numSamples = thread->frameCount();
1666 int32_t *buffer = new int32_t[numSamples];
1667 memset(buffer, 0, numSamples * sizeof(int32_t));
1668 effect->setInBuffer((int16_t *)buffer);
1669 // auxiliary effects output samples to chain input buffer for further processing
1670 // by insert effects
1671 effect->setOutBuffer(mInBuffer);
1672 } else {
1673 // Insert effects are inserted at the end of mEffects vector as they are processed
1674 // after track and auxiliary effects.
1675 // Insert effect order as a function of indicated preference:
1676 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1677 // another effect is present
1678 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1679 // last effect claiming first position
1680 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1681 // first effect claiming last position
1682 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1683 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1684 // already present
1685
1686 size_t size = mEffects.size();
1687 size_t idx_insert = size;
1688 ssize_t idx_insert_first = -1;
1689 ssize_t idx_insert_last = -1;
1690
1691 for (size_t i = 0; i < size; i++) {
1692 effect_descriptor_t d = mEffects[i]->desc();
1693 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1694 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1695 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1696 // check invalid effect chaining combinations
1697 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1698 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1699 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1700 desc.name, d.name);
1701 return INVALID_OPERATION;
1702 }
1703 // remember position of first insert effect and by default
1704 // select this as insert position for new effect
1705 if (idx_insert == size) {
1706 idx_insert = i;
1707 }
1708 // remember position of last insert effect claiming
1709 // first position
1710 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1711 idx_insert_first = i;
1712 }
1713 // remember position of first insert effect claiming
1714 // last position
1715 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1716 idx_insert_last == -1) {
1717 idx_insert_last = i;
1718 }
1719 }
1720 }
1721
1722 // modify idx_insert from first position if needed
1723 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1724 if (idx_insert_last != -1) {
1725 idx_insert = idx_insert_last;
1726 } else {
1727 idx_insert = size;
1728 }
1729 } else {
1730 if (idx_insert_first != -1) {
1731 idx_insert = idx_insert_first + 1;
1732 }
1733 }
1734
1735 // always read samples from chain input buffer
1736 effect->setInBuffer(mInBuffer);
1737
1738 // if last effect in the chain, output samples to chain
1739 // output buffer, otherwise to chain input buffer
1740 if (idx_insert == size) {
1741 if (idx_insert != 0) {
1742 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1743 mEffects[idx_insert-1]->configure();
1744 }
1745 effect->setOutBuffer(mOutBuffer);
1746 } else {
1747 effect->setOutBuffer(mInBuffer);
1748 }
1749 mEffects.insertAt(effect, idx_insert);
1750
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001751 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 idx_insert);
1753 }
1754 effect->configure();
1755 return NO_ERROR;
1756}
1757
Eric Laurentb378b732016-12-01 15:28:29 -08001758// removeEffect_l() must be called with ThreadBase::mLock held
1759size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1760 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001761{
1762 Mutex::Autolock _l(mLock);
1763 size_t size = mEffects.size();
1764 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1765
1766 for (size_t i = 0; i < size; i++) {
1767 if (effect == mEffects[i]) {
1768 // calling stop here will remove pre-processing effect from the audio HAL.
1769 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1770 // the middle of a read from audio HAL
1771 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1772 mEffects[i]->state() == EffectModule::STOPPING) {
1773 mEffects[i]->stop();
1774 }
Eric Laurentb378b732016-12-01 15:28:29 -08001775 if (release) {
1776 mEffects[i]->release_l();
1777 }
1778
Eric Laurentca7cc822012-11-19 14:55:58 -08001779 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1780 delete[] effect->inBuffer();
1781 } else {
1782 if (i == size - 1 && i != 0) {
1783 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1784 mEffects[i - 1]->configure();
1785 }
1786 }
1787 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001788 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001789 this, i);
Eric Laurentb378b732016-12-01 15:28:29 -08001790
Eric Laurentca7cc822012-11-19 14:55:58 -08001791 break;
1792 }
1793 }
1794
1795 return mEffects.size();
1796}
1797
Eric Laurentb378b732016-12-01 15:28:29 -08001798// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001799void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1800{
1801 size_t size = mEffects.size();
1802 for (size_t i = 0; i < size; i++) {
1803 mEffects[i]->setDevice(device);
1804 }
1805}
1806
Eric Laurentb378b732016-12-01 15:28:29 -08001807// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001808void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1809{
1810 size_t size = mEffects.size();
1811 for (size_t i = 0; i < size; i++) {
1812 mEffects[i]->setMode(mode);
1813 }
1814}
1815
Eric Laurentb378b732016-12-01 15:28:29 -08001816// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001817void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1818{
1819 size_t size = mEffects.size();
1820 for (size_t i = 0; i < size; i++) {
1821 mEffects[i]->setAudioSource(source);
1822 }
1823}
1824
Eric Laurentb378b732016-12-01 15:28:29 -08001825// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001826bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001827{
1828 uint32_t newLeft = *left;
1829 uint32_t newRight = *right;
1830 bool hasControl = false;
1831 int ctrlIdx = -1;
1832 size_t size = mEffects.size();
1833
1834 // first update volume controller
1835 for (size_t i = size; i > 0; i--) {
1836 if (mEffects[i - 1]->isProcessEnabled() &&
1837 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1838 ctrlIdx = i - 1;
1839 hasControl = true;
1840 break;
1841 }
1842 }
1843
Eric Laurentfa1e1232016-08-02 19:01:49 -07001844 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001845 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001846 if (hasControl) {
1847 *left = mNewLeftVolume;
1848 *right = mNewRightVolume;
1849 }
1850 return hasControl;
1851 }
1852
1853 mVolumeCtrlIdx = ctrlIdx;
1854 mLeftVolume = newLeft;
1855 mRightVolume = newRight;
1856
1857 // second get volume update from volume controller
1858 if (ctrlIdx >= 0) {
1859 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1860 mNewLeftVolume = newLeft;
1861 mNewRightVolume = newRight;
1862 }
1863 // then indicate volume to all other effects in chain.
1864 // Pass altered volume to effects before volume controller
1865 // and requested volume to effects after controller
1866 uint32_t lVol = newLeft;
1867 uint32_t rVol = newRight;
1868
1869 for (size_t i = 0; i < size; i++) {
1870 if ((int)i == ctrlIdx) {
1871 continue;
1872 }
1873 // this also works for ctrlIdx == -1 when there is no volume controller
1874 if ((int)i > ctrlIdx) {
1875 lVol = *left;
1876 rVol = *right;
1877 }
1878 mEffects[i]->setVolume(&lVol, &rVol, false);
1879 }
1880 *left = newLeft;
1881 *right = newRight;
1882
1883 return hasControl;
1884}
1885
Eric Laurentb378b732016-12-01 15:28:29 -08001886// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001887void AudioFlinger::EffectChain::resetVolume_l()
1888{
Eric Laurente7449bf2016-08-03 18:44:07 -07001889 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1890 uint32_t left = mLeftVolume;
1891 uint32_t right = mRightVolume;
1892 (void)setVolume_l(&left, &right, true);
1893 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001894}
1895
Eric Laurent1b928682014-10-02 19:41:47 -07001896void AudioFlinger::EffectChain::syncHalEffectsState()
1897{
1898 Mutex::Autolock _l(mLock);
1899 for (size_t i = 0; i < mEffects.size(); i++) {
1900 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1901 mEffects[i]->state() == EffectModule::STOPPING) {
1902 mEffects[i]->addEffectToHal_l();
1903 }
1904 }
1905}
1906
Eric Laurentca7cc822012-11-19 14:55:58 -08001907void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1908{
1909 const size_t SIZE = 256;
1910 char buffer[SIZE];
1911 String8 result;
1912
Marco Nelissenb2208842014-02-07 14:00:50 -08001913 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001914 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001915 result.append(buffer);
1916
Marco Nelissenb2208842014-02-07 14:00:50 -08001917 if (numEffects) {
1918 bool locked = AudioFlinger::dumpTryLock(mLock);
1919 // failed to lock - AudioFlinger is probably deadlocked
1920 if (!locked) {
1921 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001922 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001923
Marco Nelissenb2208842014-02-07 14:00:50 -08001924 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001925 snprintf(buffer, SIZE, "\t%p %p %d\n",
1926 mInBuffer,
1927 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001928 mActiveTrackCnt);
1929 result.append(buffer);
1930 write(fd, result.string(), result.size());
1931
1932 for (size_t i = 0; i < numEffects; ++i) {
1933 sp<EffectModule> effect = mEffects[i];
1934 if (effect != 0) {
1935 effect->dump(fd, args);
1936 }
1937 }
1938
1939 if (locked) {
1940 mLock.unlock();
1941 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001942 }
1943}
1944
1945// must be called with ThreadBase::mLock held
1946void AudioFlinger::EffectChain::setEffectSuspended_l(
1947 const effect_uuid_t *type, bool suspend)
1948{
1949 sp<SuspendedEffectDesc> desc;
1950 // use effect type UUID timelow as key as there is no real risk of identical
1951 // timeLow fields among effect type UUIDs.
1952 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1953 if (suspend) {
1954 if (index >= 0) {
1955 desc = mSuspendedEffects.valueAt(index);
1956 } else {
1957 desc = new SuspendedEffectDesc();
1958 desc->mType = *type;
1959 mSuspendedEffects.add(type->timeLow, desc);
1960 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1961 }
1962 if (desc->mRefCount++ == 0) {
1963 sp<EffectModule> effect = getEffectIfEnabled(type);
1964 if (effect != 0) {
1965 desc->mEffect = effect;
1966 effect->setSuspended(true);
1967 effect->setEnabled(false);
1968 }
1969 }
1970 } else {
1971 if (index < 0) {
1972 return;
1973 }
1974 desc = mSuspendedEffects.valueAt(index);
1975 if (desc->mRefCount <= 0) {
1976 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1977 desc->mRefCount = 1;
1978 }
1979 if (--desc->mRefCount == 0) {
1980 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1981 if (desc->mEffect != 0) {
1982 sp<EffectModule> effect = desc->mEffect.promote();
1983 if (effect != 0) {
1984 effect->setSuspended(false);
1985 effect->lock();
1986 EffectHandle *handle = effect->controlHandle_l();
Eric Laurentb378b732016-12-01 15:28:29 -08001987 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001988 effect->setEnabled_l(handle->enabled());
1989 }
1990 effect->unlock();
1991 }
1992 desc->mEffect.clear();
1993 }
1994 mSuspendedEffects.removeItemsAt(index);
1995 }
1996 }
1997}
1998
1999// must be called with ThreadBase::mLock held
2000void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2001{
2002 sp<SuspendedEffectDesc> desc;
2003
2004 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2005 if (suspend) {
2006 if (index >= 0) {
2007 desc = mSuspendedEffects.valueAt(index);
2008 } else {
2009 desc = new SuspendedEffectDesc();
2010 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2011 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2012 }
2013 if (desc->mRefCount++ == 0) {
2014 Vector< sp<EffectModule> > effects;
2015 getSuspendEligibleEffects(effects);
2016 for (size_t i = 0; i < effects.size(); i++) {
2017 setEffectSuspended_l(&effects[i]->desc().type, true);
2018 }
2019 }
2020 } else {
2021 if (index < 0) {
2022 return;
2023 }
2024 desc = mSuspendedEffects.valueAt(index);
2025 if (desc->mRefCount <= 0) {
2026 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2027 desc->mRefCount = 1;
2028 }
2029 if (--desc->mRefCount == 0) {
2030 Vector<const effect_uuid_t *> types;
2031 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2032 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2033 continue;
2034 }
2035 types.add(&mSuspendedEffects.valueAt(i)->mType);
2036 }
2037 for (size_t i = 0; i < types.size(); i++) {
2038 setEffectSuspended_l(types[i], false);
2039 }
2040 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2041 mSuspendedEffects.keyAt(index));
2042 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2043 }
2044 }
2045}
2046
2047
2048// The volume effect is used for automated tests only
2049#ifndef OPENSL_ES_H_
2050static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2051 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2052const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2053#endif //OPENSL_ES_H_
2054
2055bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2056{
2057 // auxiliary effects and visualizer are never suspended on output mix
2058 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2059 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2060 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2061 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2062 return false;
2063 }
2064 return true;
2065}
2066
2067void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2068 Vector< sp<AudioFlinger::EffectModule> > &effects)
2069{
2070 effects.clear();
2071 for (size_t i = 0; i < mEffects.size(); i++) {
2072 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2073 effects.add(mEffects[i]);
2074 }
2075 }
2076}
2077
2078sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2079 const effect_uuid_t *type)
2080{
2081 sp<EffectModule> effect = getEffectFromType_l(type);
2082 return effect != 0 && effect->isEnabled() ? effect : 0;
2083}
2084
2085void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2086 bool enabled)
2087{
2088 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2089 if (enabled) {
2090 if (index < 0) {
2091 // if the effect is not suspend check if all effects are suspended
2092 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2093 if (index < 0) {
2094 return;
2095 }
2096 if (!isEffectEligibleForSuspend(effect->desc())) {
2097 return;
2098 }
2099 setEffectSuspended_l(&effect->desc().type, enabled);
2100 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2101 if (index < 0) {
2102 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2103 return;
2104 }
2105 }
2106 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2107 effect->desc().type.timeLow);
2108 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2109 // if effect is requested to suspended but was not yet enabled, supend it now.
2110 if (desc->mEffect == 0) {
2111 desc->mEffect = effect;
2112 effect->setEnabled(false);
2113 effect->setSuspended(true);
2114 }
2115 } else {
2116 if (index < 0) {
2117 return;
2118 }
2119 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2120 effect->desc().type.timeLow);
2121 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2122 desc->mEffect.clear();
2123 effect->setSuspended(false);
2124 }
2125}
2126
Eric Laurent5baf2af2013-09-12 17:37:00 -07002127bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002128{
2129 Mutex::Autolock _l(mLock);
2130 size_t size = mEffects.size();
2131 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002132 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002133 return true;
2134 }
2135 }
2136 return false;
2137}
2138
Eric Laurentaaa44472014-09-12 17:41:50 -07002139void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2140{
2141 Mutex::Autolock _l(mLock);
2142 mThread = thread;
2143 for (size_t i = 0; i < mEffects.size(); i++) {
2144 mEffects[i]->setThread(thread);
2145 }
2146}
2147
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002148void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2149{
2150 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2151 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2152 }
2153 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2154 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2155 }
2156}
2157
2158void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2159{
2160 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2161 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2162 }
2163 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2164 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2165 }
2166}
2167
2168bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002169{
2170 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002171 for (const auto &effect : mEffects) {
2172 if (effect->isProcessImplemented()) {
2173 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002174 }
2175 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002176 // Allow effects without processing.
2177 return true;
2178}
2179
2180bool AudioFlinger::EffectChain::isFastCompatible() const
2181{
2182 Mutex::Autolock _l(mLock);
2183 for (const auto &effect : mEffects) {
2184 if (effect->isProcessImplemented()
2185 && effect->isImplementationSoftware()) {
2186 return false;
2187 }
2188 }
2189 // Allow effects without processing or hw accelerated effects.
2190 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002191}
2192
2193// isCompatibleWithThread_l() must be called with thread->mLock held
2194bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2195{
2196 Mutex::Autolock _l(mLock);
2197 for (size_t i = 0; i < mEffects.size(); i++) {
2198 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2199 return false;
2200 }
2201 }
2202 return true;
2203}
2204
Glenn Kasten63238ef2015-03-02 15:50:29 -08002205} // namespace android