blob: 09e7fd85429c33c7d9d9f16d81b9d72ff8276a28 [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;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900364 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
365 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800366 } else {
367 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700368 // TODO: Update this logic when multichannel effects are implemented.
369 // For offloaded tracks consider mono output as stereo for proper effect initialization
370 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
371 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
372 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
373 ALOGV("Overriding effect input and output as STEREO");
374 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800375 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700376
Eric Laurentca7cc822012-11-19 14:55:58 -0800377 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
378 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
379 mConfig.inputCfg.samplingRate = thread->sampleRate();
380 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
381 mConfig.inputCfg.bufferProvider.cookie = NULL;
382 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
383 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
384 mConfig.outputCfg.bufferProvider.cookie = NULL;
385 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
386 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
387 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
388 // Insert effect:
389 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
390 // always overwrites output buffer: input buffer == output buffer
391 // - in other sessions:
392 // last effect in the chain accumulates in output buffer: input buffer != output buffer
393 // other effect: overwrites output buffer: input buffer == output buffer
394 // Auxiliary effect:
395 // accumulates in output buffer: input buffer != output buffer
396 // Therefore: accumulate <=> input buffer != output buffer
397 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
398 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
399 } else {
400 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
401 }
402 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
403 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
404 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
405 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
406
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700407 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800408 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
409
410 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700411 size = sizeof(int);
412 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800413 EFFECT_CMD_SET_CONFIG,
414 sizeof(effect_config_t),
415 &mConfig,
416 &size,
417 &cmdStatus);
418 if (status == 0) {
419 status = cmdStatus;
420 }
421
422 if (status == 0 &&
423 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
424 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
425 effect_param_t *p = (effect_param_t *)buf32;
426
427 p->psize = sizeof(uint32_t);
428 p->vsize = sizeof(uint32_t);
429 size = sizeof(int);
430 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
431
432 uint32_t latency = 0;
433 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
434 if (pbt != NULL) {
435 latency = pbt->latency_l();
436 }
437
438 *((int32_t *)p->data + 1)= latency;
439 (*mEffectInterface)->command(mEffectInterface,
440 EFFECT_CMD_SET_PARAM,
441 sizeof(effect_param_t) + 8,
442 &buf32,
443 &size,
444 &cmdStatus);
445 }
446
447 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
448 (1000 * mConfig.outputCfg.buffer.frameCount);
449
Eric Laurentd0ebb532013-04-02 16:41:41 -0700450exit:
451 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800452 return status;
453}
454
455status_t AudioFlinger::EffectModule::init()
456{
457 Mutex::Autolock _l(mLock);
458 if (mEffectInterface == NULL) {
459 return NO_INIT;
460 }
461 status_t cmdStatus;
462 uint32_t size = sizeof(status_t);
463 status_t status = (*mEffectInterface)->command(mEffectInterface,
464 EFFECT_CMD_INIT,
465 0,
466 NULL,
467 &size,
468 &cmdStatus);
469 if (status == 0) {
470 status = cmdStatus;
471 }
472 return status;
473}
474
Eric Laurent1b928682014-10-02 19:41:47 -0700475void AudioFlinger::EffectModule::addEffectToHal_l()
476{
477 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
478 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
479 sp<ThreadBase> thread = mThread.promote();
480 if (thread != 0) {
481 audio_stream_t *stream = thread->stream();
482 if (stream != NULL) {
483 stream->add_audio_effect(stream, mEffectInterface);
484 }
485 }
486 }
487}
488
Eric Laurentfa1e1232016-08-02 19:01:49 -0700489// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800490status_t AudioFlinger::EffectModule::start()
491{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700492 sp<EffectChain> chain;
493 status_t status;
494 {
495 Mutex::Autolock _l(mLock);
496 status = start_l();
497 if (status == NO_ERROR) {
498 chain = mChain.promote();
499 }
500 }
501 if (chain != 0) {
502 chain->resetVolume_l();
503 }
504 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800505}
506
507status_t AudioFlinger::EffectModule::start_l()
508{
509 if (mEffectInterface == NULL) {
510 return NO_INIT;
511 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700512 if (mStatus != NO_ERROR) {
513 return mStatus;
514 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800515 status_t cmdStatus;
516 uint32_t size = sizeof(status_t);
517 status_t status = (*mEffectInterface)->command(mEffectInterface,
518 EFFECT_CMD_ENABLE,
519 0,
520 NULL,
521 &size,
522 &cmdStatus);
523 if (status == 0) {
524 status = cmdStatus;
525 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700526 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700527 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800528 }
529 return status;
530}
531
532status_t AudioFlinger::EffectModule::stop()
533{
534 Mutex::Autolock _l(mLock);
535 return stop_l();
536}
537
538status_t AudioFlinger::EffectModule::stop_l()
539{
540 if (mEffectInterface == NULL) {
541 return NO_INIT;
542 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700543 if (mStatus != NO_ERROR) {
544 return mStatus;
545 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800546 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800547 uint32_t size = sizeof(status_t);
548 status_t status = (*mEffectInterface)->command(mEffectInterface,
549 EFFECT_CMD_DISABLE,
550 0,
551 NULL,
552 &size,
553 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800554 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800555 status = cmdStatus;
556 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800557 if (status == NO_ERROR) {
558 status = remove_effect_from_hal_l();
559 }
560 return status;
561}
562
Eric Laurentb378b732016-12-01 15:28:29 -0800563// must be called with EffectChain::mLock held
564void AudioFlinger::EffectModule::release_l()
565{
566 if (mEffectInterface != NULL) {
567 remove_effect_from_hal_l();
568 // release effect engine
569 EffectRelease(mEffectInterface);
570 mEffectInterface = NULL;
571 }
572}
573
Eric Laurentbfb1b832013-01-07 09:53:42 -0800574status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
575{
576 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
577 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800578 sp<ThreadBase> thread = mThread.promote();
579 if (thread != 0) {
580 audio_stream_t *stream = thread->stream();
581 if (stream != NULL) {
582 stream->remove_audio_effect(stream, mEffectInterface);
583 }
584 }
585 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800586 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800587}
588
Andy Hunge4a1d912016-08-17 14:11:13 -0700589// round up delta valid if value and divisor are positive.
590template <typename T>
591static T roundUpDelta(const T &value, const T &divisor) {
592 T remainder = value % divisor;
593 return remainder == 0 ? 0 : divisor - remainder;
594}
595
Eric Laurentca7cc822012-11-19 14:55:58 -0800596status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
597 uint32_t cmdSize,
598 void *pCmdData,
599 uint32_t *replySize,
600 void *pReplyData)
601{
602 Mutex::Autolock _l(mLock);
603 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
604
605 if (mState == DESTROYED || mEffectInterface == NULL) {
606 return NO_INIT;
607 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700608 if (mStatus != NO_ERROR) {
609 return mStatus;
610 }
Andy Hung110bc952016-06-20 15:22:52 -0700611 if (cmdCode == EFFECT_CMD_GET_PARAM &&
612 (*replySize < sizeof(effect_param_t) ||
613 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
614 android_errorWriteLog(0x534e4554, "29251553");
615 return -EINVAL;
616 }
Andy Hung3d34cc72016-11-04 19:40:53 -0700617 if (cmdCode == EFFECT_CMD_GET_PARAM &&
618 (sizeof(effect_param_t) > cmdSize ||
619 ((effect_param_t *)pCmdData)->psize > cmdSize
620 - sizeof(effect_param_t))) {
621 android_errorWriteLog(0x534e4554, "32438594");
622 return -EINVAL;
623 }
ragoe2759072016-11-22 18:02:48 -0800624 if (cmdCode == EFFECT_CMD_GET_PARAM &&
625 (sizeof(effect_param_t) > *replySize
626 || ((effect_param_t *)pCmdData)->psize > *replySize
627 - sizeof(effect_param_t)
628 || ((effect_param_t *)pCmdData)->vsize > *replySize
629 - sizeof(effect_param_t)
630 - ((effect_param_t *)pCmdData)->psize
631 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
632 *replySize
633 - sizeof(effect_param_t)
634 - ((effect_param_t *)pCmdData)->psize
635 - ((effect_param_t *)pCmdData)->vsize)) {
636 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
637 android_errorWriteLog(0x534e4554, "32705438");
638 return -EINVAL;
639 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700640 if ((cmdCode == EFFECT_CMD_SET_PARAM
641 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
642 (sizeof(effect_param_t) > cmdSize
643 || ((effect_param_t *)pCmdData)->psize > cmdSize
644 - sizeof(effect_param_t)
645 || ((effect_param_t *)pCmdData)->vsize > cmdSize
646 - sizeof(effect_param_t)
647 - ((effect_param_t *)pCmdData)->psize
648 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
649 cmdSize
650 - sizeof(effect_param_t)
651 - ((effect_param_t *)pCmdData)->psize
652 - ((effect_param_t *)pCmdData)->vsize)) {
653 android_errorWriteLog(0x534e4554, "30204301");
654 return -EINVAL;
655 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800656 status_t status = (*mEffectInterface)->command(mEffectInterface,
657 cmdCode,
658 cmdSize,
659 pCmdData,
660 replySize,
661 pReplyData);
662 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
663 uint32_t size = (replySize == NULL) ? 0 : *replySize;
664 for (size_t i = 1; i < mHandles.size(); i++) {
665 EffectHandle *h = mHandles[i];
Eric Laurentb378b732016-12-01 15:28:29 -0800666 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800667 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
668 }
669 }
670 }
671 return status;
672}
673
674status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
675{
676 Mutex::Autolock _l(mLock);
677 return setEnabled_l(enabled);
678}
679
680// must be called with EffectModule::mLock held
681status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
682{
683
684 ALOGV("setEnabled %p enabled %d", this, enabled);
685
686 if (enabled != isEnabled()) {
687 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
688 if (enabled && status != NO_ERROR) {
689 return status;
690 }
691
692 switch (mState) {
693 // going from disabled to enabled
694 case IDLE:
695 mState = STARTING;
696 break;
697 case STOPPED:
698 mState = RESTART;
699 break;
700 case STOPPING:
701 mState = ACTIVE;
702 break;
703
704 // going from enabled to disabled
705 case RESTART:
706 mState = STOPPED;
707 break;
708 case STARTING:
709 mState = IDLE;
710 break;
711 case ACTIVE:
712 mState = STOPPING;
713 break;
714 case DESTROYED:
715 return NO_ERROR; // simply ignore as we are being destroyed
716 }
717 for (size_t i = 1; i < mHandles.size(); i++) {
718 EffectHandle *h = mHandles[i];
Eric Laurentb378b732016-12-01 15:28:29 -0800719 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800720 h->setEnabled(enabled);
721 }
722 }
723 }
724 return NO_ERROR;
725}
726
727bool AudioFlinger::EffectModule::isEnabled() const
728{
729 switch (mState) {
730 case RESTART:
731 case STARTING:
732 case ACTIVE:
733 return true;
734 case IDLE:
735 case STOPPING:
736 case STOPPED:
737 case DESTROYED:
738 default:
739 return false;
740 }
741}
742
743bool AudioFlinger::EffectModule::isProcessEnabled() const
744{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700745 if (mStatus != NO_ERROR) {
746 return false;
747 }
748
Eric Laurentca7cc822012-11-19 14:55:58 -0800749 switch (mState) {
750 case RESTART:
751 case ACTIVE:
752 case STOPPING:
753 case STOPPED:
754 return true;
755 case IDLE:
756 case STARTING:
757 case DESTROYED:
758 default:
759 return false;
760 }
761}
762
763status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
764{
765 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700766 if (mStatus != NO_ERROR) {
767 return mStatus;
768 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800769 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800770 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
771 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
772 if (isProcessEnabled() &&
773 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
774 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800775 uint32_t volume[2];
776 uint32_t *pVolume = NULL;
777 uint32_t size = sizeof(volume);
778 volume[0] = *left;
779 volume[1] = *right;
780 if (controller) {
781 pVolume = volume;
782 }
783 status = (*mEffectInterface)->command(mEffectInterface,
784 EFFECT_CMD_SET_VOLUME,
785 size,
786 volume,
787 &size,
788 pVolume);
789 if (controller && status == NO_ERROR && size == sizeof(volume)) {
790 *left = volume[0];
791 *right = volume[1];
792 }
793 }
794 return status;
795}
796
797status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
798{
799 if (device == AUDIO_DEVICE_NONE) {
800 return NO_ERROR;
801 }
802
803 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700804 if (mStatus != NO_ERROR) {
805 return mStatus;
806 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800807 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700808 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800809 status_t cmdStatus;
810 uint32_t size = sizeof(status_t);
811 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
812 EFFECT_CMD_SET_INPUT_DEVICE;
813 status = (*mEffectInterface)->command(mEffectInterface,
814 cmd,
815 sizeof(uint32_t),
816 &device,
817 &size,
818 &cmdStatus);
819 }
820 return status;
821}
822
823status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
824{
825 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700826 if (mStatus != NO_ERROR) {
827 return mStatus;
828 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800829 status_t status = NO_ERROR;
830 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
831 status_t cmdStatus;
832 uint32_t size = sizeof(status_t);
833 status = (*mEffectInterface)->command(mEffectInterface,
834 EFFECT_CMD_SET_AUDIO_MODE,
835 sizeof(audio_mode_t),
836 &mode,
837 &size,
838 &cmdStatus);
839 if (status == NO_ERROR) {
840 status = cmdStatus;
841 }
842 }
843 return status;
844}
845
846status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
847{
848 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700849 if (mStatus != NO_ERROR) {
850 return mStatus;
851 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800852 status_t status = NO_ERROR;
853 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
854 uint32_t size = 0;
855 status = (*mEffectInterface)->command(mEffectInterface,
856 EFFECT_CMD_SET_AUDIO_SOURCE,
857 sizeof(audio_source_t),
858 &source,
859 &size,
860 NULL);
861 }
862 return status;
863}
864
865void AudioFlinger::EffectModule::setSuspended(bool suspended)
866{
867 Mutex::Autolock _l(mLock);
868 mSuspended = suspended;
869}
870
871bool AudioFlinger::EffectModule::suspended() const
872{
873 Mutex::Autolock _l(mLock);
874 return mSuspended;
875}
876
877bool AudioFlinger::EffectModule::purgeHandles()
878{
879 bool enabled = false;
880 Mutex::Autolock _l(mLock);
881 for (size_t i = 0; i < mHandles.size(); i++) {
882 EffectHandle *handle = mHandles[i];
Eric Laurentb378b732016-12-01 15:28:29 -0800883 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800884 if (handle->hasControl()) {
885 enabled = handle->enabled();
886 }
887 }
888 }
889 return enabled;
890}
891
Eric Laurent5baf2af2013-09-12 17:37:00 -0700892status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
893{
894 Mutex::Autolock _l(mLock);
895 if (mStatus != NO_ERROR) {
896 return mStatus;
897 }
898 status_t status = NO_ERROR;
899 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
900 status_t cmdStatus;
901 uint32_t size = sizeof(status_t);
902 effect_offload_param_t cmd;
903
904 cmd.isOffload = offloaded;
905 cmd.ioHandle = io;
906 status = (*mEffectInterface)->command(mEffectInterface,
907 EFFECT_CMD_OFFLOAD,
908 sizeof(effect_offload_param_t),
909 &cmd,
910 &size,
911 &cmdStatus);
912 if (status == NO_ERROR) {
913 status = cmdStatus;
914 }
915 mOffloaded = (status == NO_ERROR) ? offloaded : false;
916 } else {
917 if (offloaded) {
918 status = INVALID_OPERATION;
919 }
920 mOffloaded = false;
921 }
922 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
923 return status;
924}
925
926bool AudioFlinger::EffectModule::isOffloaded() const
927{
928 Mutex::Autolock _l(mLock);
929 return mOffloaded;
930}
931
Marco Nelissenb2208842014-02-07 14:00:50 -0800932String8 effectFlagsToString(uint32_t flags) {
933 String8 s;
934
935 s.append("conn. mode: ");
936 switch (flags & EFFECT_FLAG_TYPE_MASK) {
937 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
938 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
939 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
940 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
941 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
942 default: s.append("unknown/reserved"); break;
943 }
944 s.append(", ");
945
946 s.append("insert pref: ");
947 switch (flags & EFFECT_FLAG_INSERT_MASK) {
948 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
949 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
950 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
951 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
952 default: s.append("unknown/reserved"); break;
953 }
954 s.append(", ");
955
956 s.append("volume mgmt: ");
957 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
958 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
959 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
960 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
961 default: s.append("unknown/reserved"); break;
962 }
963 s.append(", ");
964
965 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
966 if (devind) {
967 s.append("device indication: ");
968 switch (devind) {
969 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
970 default: s.append("unknown/reserved"); break;
971 }
972 s.append(", ");
973 }
974
975 s.append("input mode: ");
976 switch (flags & EFFECT_FLAG_INPUT_MASK) {
977 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
978 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
979 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
980 default: s.append("not set"); break;
981 }
982 s.append(", ");
983
984 s.append("output mode: ");
985 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
986 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
987 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
988 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
989 default: s.append("not set"); break;
990 }
991 s.append(", ");
992
993 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
994 if (accel) {
995 s.append("hardware acceleration: ");
996 switch (accel) {
997 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
998 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
999 default: s.append("unknown/reserved"); break;
1000 }
1001 s.append(", ");
1002 }
1003
1004 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1005 if (modeind) {
1006 s.append("mode indication: ");
1007 switch (modeind) {
1008 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1009 default: s.append("unknown/reserved"); break;
1010 }
1011 s.append(", ");
1012 }
1013
1014 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1015 if (srcind) {
1016 s.append("source indication: ");
1017 switch (srcind) {
1018 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1019 default: s.append("unknown/reserved"); break;
1020 }
1021 s.append(", ");
1022 }
1023
1024 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1025 s.append("offloadable, ");
1026 }
1027
1028 int len = s.length();
1029 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001030 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001031 s.unlockBuffer(len - 2);
1032 }
1033 return s;
1034}
1035
1036
Glenn Kasten0f11b512014-01-31 16:18:54 -08001037void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001038{
1039 const size_t SIZE = 256;
1040 char buffer[SIZE];
1041 String8 result;
1042
1043 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1044 result.append(buffer);
1045
1046 bool locked = AudioFlinger::dumpTryLock(mLock);
1047 // failed to lock - AudioFlinger is probably deadlocked
1048 if (!locked) {
1049 result.append("\t\tCould not lock Fx mutex:\n");
1050 }
1051
1052 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001053 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
1054 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001055 result.append(buffer);
1056
1057 result.append("\t\tDescriptor:\n");
1058 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1059 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1060 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1061 mDescriptor.uuid.node[2],
1062 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1063 result.append(buffer);
1064 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1065 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1066 mDescriptor.type.timeHiAndVersion,
1067 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1068 mDescriptor.type.node[2],
1069 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1070 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001071 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001072 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001073 mDescriptor.flags,
1074 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 result.append(buffer);
1076 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1077 mDescriptor.name);
1078 result.append(buffer);
1079 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1080 mDescriptor.implementor);
1081 result.append(buffer);
1082
1083 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001084 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001085 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001086 mConfig.inputCfg.buffer.frameCount,
1087 mConfig.inputCfg.samplingRate,
1088 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001089 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001090 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001091 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001092 result.append(buffer);
1093
1094 result.append("\t\t- Output configuration:\n");
1095 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001096 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001097 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001098 mConfig.outputCfg.buffer.frameCount,
1099 mConfig.outputCfg.samplingRate,
1100 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001101 mConfig.outputCfg.format,
1102 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001103 result.append(buffer);
1104
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001105 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001106 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001107 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001108 for (size_t i = 0; i < mHandles.size(); ++i) {
1109 EffectHandle *handle = mHandles[i];
Eric Laurentb378b732016-12-01 15:28:29 -08001110 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001111 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001112 result.append(buffer);
1113 }
1114 }
1115
Eric Laurentca7cc822012-11-19 14:55:58 -08001116 write(fd, result.string(), result.length());
1117
1118 if (locked) {
1119 mLock.unlock();
1120 }
1121}
1122
1123// ----------------------------------------------------------------------------
1124// EffectHandle implementation
1125// ----------------------------------------------------------------------------
1126
1127#undef LOG_TAG
1128#define LOG_TAG "AudioFlinger::EffectHandle"
1129
1130AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1131 const sp<AudioFlinger::Client>& client,
1132 const sp<IEffectClient>& effectClient,
1133 int32_t priority)
1134 : BnEffect(),
1135 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentb378b732016-12-01 15:28:29 -08001136 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001137{
1138 ALOGV("constructor %p", this);
1139
1140 if (client == 0) {
1141 return;
1142 }
1143 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1144 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001145 if (mCblkMemory == 0 ||
1146 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001147 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001148 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001149 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001150 return;
1151 }
Glenn Kastene75da402013-11-20 13:54:52 -08001152 new(mCblk) effect_param_cblk_t();
1153 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001154}
1155
1156AudioFlinger::EffectHandle::~EffectHandle()
1157{
1158 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001159 disconnect(false);
1160}
1161
Glenn Kastene75da402013-11-20 13:54:52 -08001162status_t AudioFlinger::EffectHandle::initCheck()
1163{
1164 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1165}
1166
Eric Laurentca7cc822012-11-19 14:55:58 -08001167status_t AudioFlinger::EffectHandle::enable()
1168{
Eric Laurentb378b732016-12-01 15:28:29 -08001169 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001170 ALOGV("enable %p", this);
Eric Laurentb378b732016-12-01 15:28:29 -08001171 sp<EffectModule> effect = mEffect.promote();
1172 if (effect == 0 || mDisconnected) {
1173 return DEAD_OBJECT;
1174 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001175 if (!mHasControl) {
1176 return INVALID_OPERATION;
1177 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001178
1179 if (mEnabled) {
1180 return NO_ERROR;
1181 }
1182
1183 mEnabled = true;
1184
Eric Laurentb378b732016-12-01 15:28:29 -08001185 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001186 if (thread != 0) {
Eric Laurentb378b732016-12-01 15:28:29 -08001187 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001188 }
1189
1190 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurentb378b732016-12-01 15:28:29 -08001191 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001192 return NO_ERROR;
1193 }
1194
Eric Laurentb378b732016-12-01 15:28:29 -08001195 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001196 if (status != NO_ERROR) {
1197 if (thread != 0) {
Eric Laurentb378b732016-12-01 15:28:29 -08001198 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001199 }
1200 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001201 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001202 if (thread != 0) {
1203 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001204 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001205 Mutex::Autolock _l(t->mLock);
1206 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001207 }
Eric Laurentb378b732016-12-01 15:28:29 -08001208 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001209 if (thread->type() == ThreadBase::OFFLOAD) {
1210 PlaybackThread *t = (PlaybackThread *)thread.get();
1211 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1212 }
Eric Laurentb378b732016-12-01 15:28:29 -08001213 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001214 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1215 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001216 }
1217 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001218 }
1219 return status;
1220}
1221
1222status_t AudioFlinger::EffectHandle::disable()
1223{
1224 ALOGV("disable %p", this);
Eric Laurentb378b732016-12-01 15:28:29 -08001225 AutoMutex _l(mLock);
1226 sp<EffectModule> effect = mEffect.promote();
1227 if (effect == 0 || mDisconnected) {
1228 return DEAD_OBJECT;
1229 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001230 if (!mHasControl) {
1231 return INVALID_OPERATION;
1232 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001233
1234 if (!mEnabled) {
1235 return NO_ERROR;
1236 }
1237 mEnabled = false;
1238
Eric Laurentb378b732016-12-01 15:28:29 -08001239 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001240 return NO_ERROR;
1241 }
1242
Eric Laurentb378b732016-12-01 15:28:29 -08001243 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001244
Eric Laurentb378b732016-12-01 15:28:29 -08001245 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001246 if (thread != 0) {
Eric Laurentb378b732016-12-01 15:28:29 -08001247 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001248 if (thread->type() == ThreadBase::OFFLOAD) {
1249 PlaybackThread *t = (PlaybackThread *)thread.get();
1250 Mutex::Autolock _l(t->mLock);
1251 t->broadcast_l();
1252 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001253 }
1254
1255 return status;
1256}
1257
1258void AudioFlinger::EffectHandle::disconnect()
1259{
Eric Laurentb378b732016-12-01 15:28:29 -08001260 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001261 disconnect(true);
1262}
1263
1264void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1265{
Eric Laurentb378b732016-12-01 15:28:29 -08001266 AutoMutex _l(mLock);
1267 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1268 if (mDisconnected) {
1269 if (unpinIfLast) {
1270 android_errorWriteLog(0x534e4554, "32707507");
1271 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001272 return;
1273 }
Eric Laurentb378b732016-12-01 15:28:29 -08001274 mDisconnected = true;
1275 sp<ThreadBase> thread;
1276 {
1277 sp<EffectModule> effect = mEffect.promote();
1278 if (effect != 0) {
1279 thread = effect->thread().promote();
1280 }
1281 }
1282 if (thread != 0) {
1283 thread->disconnectEffectHandle(this, unpinIfLast);
1284 } else {
1285 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1286 // try to cleanup as much as we can
1287 sp<EffectModule> effect = mEffect.promote();
1288 if (effect != 0) {
1289 effect->disconnectHandle(this, unpinIfLast);
Eric Laurentca7cc822012-11-19 14:55:58 -08001290 }
1291 }
1292
Eric Laurentca7cc822012-11-19 14:55:58 -08001293 if (mClient != 0) {
1294 if (mCblk != NULL) {
1295 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1296 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1297 }
1298 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001299 // Client destructor must run with AudioFlinger client mutex locked
1300 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001301 mClient.clear();
1302 }
1303}
1304
1305status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1306 uint32_t cmdSize,
1307 void *pCmdData,
1308 uint32_t *replySize,
1309 void *pReplyData)
1310{
1311 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurentb378b732016-12-01 15:28:29 -08001312 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001313
Eric Laurent31a45982016-12-15 14:46:09 -08001314 if (cmdCode == EFFECT_CMD_ENABLE) {
1315 if (*replySize < sizeof(int)) {
1316 android_errorWriteLog(0x534e4554, "32095713");
1317 return BAD_VALUE;
1318 }
1319 *(int *)pReplyData = NO_ERROR;
1320 *replySize = sizeof(int);
1321 return enable();
1322 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1323 if (*replySize < sizeof(int)) {
1324 android_errorWriteLog(0x534e4554, "32095713");
1325 return BAD_VALUE;
1326 }
1327 *(int *)pReplyData = NO_ERROR;
1328 *replySize = sizeof(int);
1329 return disable();
1330 }
1331
Eric Laurentb378b732016-12-01 15:28:29 -08001332 AutoMutex _l(mLock);
1333 sp<EffectModule> effect = mEffect.promote();
1334 if (effect == 0 || mDisconnected) {
1335 return DEAD_OBJECT;
1336 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001337 // only get parameter command is permitted for applications not controlling the effect
1338 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1339 return INVALID_OPERATION;
1340 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001341 if (mClient == 0) {
1342 return INVALID_OPERATION;
1343 }
1344
1345 // handle commands that are not forwarded transparently to effect engine
1346 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent31a45982016-12-15 14:46:09 -08001347 if (*replySize < sizeof(int)) {
1348 android_errorWriteLog(0x534e4554, "32095713");
1349 return BAD_VALUE;
1350 }
1351 *(int *)pReplyData = NO_ERROR;
1352 *replySize = sizeof(int);
1353
Eric Laurentca7cc822012-11-19 14:55:58 -08001354 // No need to trylock() here as this function is executed in the binder thread serving a
1355 // particular client process: no risk to block the whole media server process or mixer
1356 // threads if we are stuck here
1357 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001358 // keep local copy of index in case of client corruption b/32220769
1359 const uint32_t clientIndex = mCblk->clientIndex;
1360 const uint32_t serverIndex = mCblk->serverIndex;
1361 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1362 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001363 mCblk->serverIndex = 0;
1364 mCblk->clientIndex = 0;
1365 return BAD_VALUE;
1366 }
1367 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001368 effect_param_t *param = NULL;
1369 for (uint32_t index = serverIndex; index < clientIndex;) {
1370 int *p = (int *)(mBuffer + index);
1371 const int size = *p++;
1372 if (size < 0
1373 || size > EFFECT_PARAM_BUFFER_SIZE
1374 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001375 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001376 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001377 break;
1378 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001379
1380 // copy to local memory in case of client corruption b/32220769
1381 param = (effect_param_t *)realloc(param, size);
1382 if (param == NULL) {
1383 ALOGW("command(): out of memory");
1384 status = NO_MEMORY;
1385 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001386 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001387 memcpy(param, p, size);
1388
1389 int reply = 0;
1390 uint32_t rsize = sizeof(reply);
Eric Laurentb378b732016-12-01 15:28:29 -08001391 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001392 size,
1393 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001394 &rsize,
1395 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001396
1397 // verify shared memory: server index shouldn't change; client index can't go back.
1398 if (serverIndex != mCblk->serverIndex
1399 || clientIndex > mCblk->clientIndex) {
1400 android_errorWriteLog(0x534e4554, "32220769");
1401 status = BAD_VALUE;
1402 break;
1403 }
1404
Eric Laurentca7cc822012-11-19 14:55:58 -08001405 // stop at first error encountered
1406 if (ret != NO_ERROR) {
1407 status = ret;
1408 *(int *)pReplyData = reply;
1409 break;
1410 } else if (reply != NO_ERROR) {
1411 *(int *)pReplyData = reply;
1412 break;
1413 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001414 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001415 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001416 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 mCblk->serverIndex = 0;
1418 mCblk->clientIndex = 0;
1419 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001420 }
1421
Eric Laurentb378b732016-12-01 15:28:29 -08001422 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001423}
1424
1425void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1426{
1427 ALOGV("setControl %p control %d", this, hasControl);
1428
1429 mHasControl = hasControl;
1430 mEnabled = enabled;
1431
1432 if (signal && mEffectClient != 0) {
1433 mEffectClient->controlStatusChanged(hasControl);
1434 }
1435}
1436
1437void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1438 uint32_t cmdSize,
1439 void *pCmdData,
1440 uint32_t replySize,
1441 void *pReplyData)
1442{
1443 if (mEffectClient != 0) {
1444 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1445 }
1446}
1447
1448
1449
1450void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1451{
1452 if (mEffectClient != 0) {
1453 mEffectClient->enableStatusChanged(enabled);
1454 }
1455}
1456
1457status_t AudioFlinger::EffectHandle::onTransact(
1458 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1459{
1460 return BnEffect::onTransact(code, data, reply, flags);
1461}
1462
1463
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001464void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001465{
1466 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1467
Marco Nelissenb2208842014-02-07 14:00:50 -08001468 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001469 (mClient == 0) ? getpid_cached : mClient->pid(),
1470 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001471 mHasControl ? "yes" : "no",
1472 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001473 mCblk ? mCblk->clientIndex : 0,
1474 mCblk ? mCblk->serverIndex : 0
1475 );
1476
1477 if (locked) {
1478 mCblk->lock.unlock();
1479 }
1480}
1481
1482#undef LOG_TAG
1483#define LOG_TAG "AudioFlinger::EffectChain"
1484
1485AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001486 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001487 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1488 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001489 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001490{
1491 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1492 if (thread == NULL) {
1493 return;
1494 }
1495 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1496 thread->frameCount();
1497}
1498
1499AudioFlinger::EffectChain::~EffectChain()
1500{
1501 if (mOwnInBuffer) {
1502 delete mInBuffer;
1503 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001504}
1505
1506// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1507sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1508 effect_descriptor_t *descriptor)
1509{
1510 size_t size = mEffects.size();
1511
1512 for (size_t i = 0; i < size; i++) {
1513 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1514 return mEffects[i];
1515 }
1516 }
1517 return 0;
1518}
1519
1520// getEffectFromId_l() must be called with ThreadBase::mLock held
1521sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1522{
1523 size_t size = mEffects.size();
1524
1525 for (size_t i = 0; i < size; i++) {
1526 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1527 if (id == 0 || mEffects[i]->id() == id) {
1528 return mEffects[i];
1529 }
1530 }
1531 return 0;
1532}
1533
1534// getEffectFromType_l() must be called with ThreadBase::mLock held
1535sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1536 const effect_uuid_t *type)
1537{
1538 size_t size = mEffects.size();
1539
1540 for (size_t i = 0; i < size; i++) {
1541 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1542 return mEffects[i];
1543 }
1544 }
1545 return 0;
1546}
1547
1548void AudioFlinger::EffectChain::clearInputBuffer()
1549{
1550 Mutex::Autolock _l(mLock);
1551 sp<ThreadBase> thread = mThread.promote();
1552 if (thread == 0) {
1553 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1554 return;
1555 }
1556 clearInputBuffer_l(thread);
1557}
1558
1559// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001560void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001561{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001562 // TODO: This will change in the future, depending on multichannel
1563 // and sample format changes for effects.
1564 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1565 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001566 const size_t frameSize =
1567 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001568 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001569}
1570
1571// Must be called with EffectChain::mLock locked
1572void AudioFlinger::EffectChain::process_l()
1573{
1574 sp<ThreadBase> thread = mThread.promote();
1575 if (thread == 0) {
1576 ALOGW("process_l(): cannot promote mixer thread");
1577 return;
1578 }
1579 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1580 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001581 // never process effects when:
1582 // - on an OFFLOAD thread
1583 // - no more tracks are on the session and the effect tail has been rendered
1584 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001585 if (!isGlobalSession) {
1586 bool tracksOnSession = (trackCnt() != 0);
1587
1588 if (!tracksOnSession && mTailBufferCount == 0) {
1589 doProcess = false;
1590 }
1591
1592 if (activeTrackCnt() == 0) {
1593 // if no track is active and the effect tail has not been rendered,
1594 // the input buffer must be cleared here as the mixer process will not do it
1595 if (tracksOnSession || mTailBufferCount > 0) {
1596 clearInputBuffer_l(thread);
1597 if (mTailBufferCount > 0) {
1598 mTailBufferCount--;
1599 }
1600 }
1601 }
1602 }
1603
1604 size_t size = mEffects.size();
1605 if (doProcess) {
1606 for (size_t i = 0; i < size; i++) {
1607 mEffects[i]->process();
1608 }
1609 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001610 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001611 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001612 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1613 }
1614 if (doResetVolume) {
1615 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001616 }
1617}
1618
Eric Laurentb378b732016-12-01 15:28:29 -08001619// createEffect_l() must be called with ThreadBase::mLock held
1620status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1621 ThreadBase *thread,
1622 effect_descriptor_t *desc,
1623 int id,
1624 audio_session_t sessionId,
1625 bool pinned)
1626{
1627 Mutex::Autolock _l(mLock);
1628 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1629 status_t lStatus = effect->status();
1630 if (lStatus == NO_ERROR) {
1631 lStatus = addEffect_ll(effect);
1632 }
1633 if (lStatus != NO_ERROR) {
1634 effect.clear();
1635 }
1636 return lStatus;
1637}
1638
1639// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001640status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1641{
Eric Laurentb378b732016-12-01 15:28:29 -08001642 Mutex::Autolock _l(mLock);
1643 return addEffect_ll(effect);
1644}
1645// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1646status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1647{
Eric Laurentca7cc822012-11-19 14:55:58 -08001648 effect_descriptor_t desc = effect->desc();
1649 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1650
Eric Laurentca7cc822012-11-19 14:55:58 -08001651 effect->setChain(this);
1652 sp<ThreadBase> thread = mThread.promote();
1653 if (thread == 0) {
1654 return NO_INIT;
1655 }
1656 effect->setThread(thread);
1657
1658 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1659 // Auxiliary effects are inserted at the beginning of mEffects vector as
1660 // they are processed first and accumulated in chain input buffer
1661 mEffects.insertAt(effect, 0);
1662
1663 // the input buffer for auxiliary effect contains mono samples in
1664 // 32 bit format. This is to avoid saturation in AudoMixer
1665 // accumulation stage. Saturation is done in EffectModule::process() before
1666 // calling the process in effect engine
1667 size_t numSamples = thread->frameCount();
1668 int32_t *buffer = new int32_t[numSamples];
1669 memset(buffer, 0, numSamples * sizeof(int32_t));
1670 effect->setInBuffer((int16_t *)buffer);
1671 // auxiliary effects output samples to chain input buffer for further processing
1672 // by insert effects
1673 effect->setOutBuffer(mInBuffer);
1674 } else {
1675 // Insert effects are inserted at the end of mEffects vector as they are processed
1676 // after track and auxiliary effects.
1677 // Insert effect order as a function of indicated preference:
1678 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1679 // another effect is present
1680 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1681 // last effect claiming first position
1682 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1683 // first effect claiming last position
1684 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1685 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1686 // already present
1687
1688 size_t size = mEffects.size();
1689 size_t idx_insert = size;
1690 ssize_t idx_insert_first = -1;
1691 ssize_t idx_insert_last = -1;
1692
1693 for (size_t i = 0; i < size; i++) {
1694 effect_descriptor_t d = mEffects[i]->desc();
1695 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1696 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1697 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1698 // check invalid effect chaining combinations
1699 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1700 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1701 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1702 desc.name, d.name);
1703 return INVALID_OPERATION;
1704 }
1705 // remember position of first insert effect and by default
1706 // select this as insert position for new effect
1707 if (idx_insert == size) {
1708 idx_insert = i;
1709 }
1710 // remember position of last insert effect claiming
1711 // first position
1712 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1713 idx_insert_first = i;
1714 }
1715 // remember position of first insert effect claiming
1716 // last position
1717 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1718 idx_insert_last == -1) {
1719 idx_insert_last = i;
1720 }
1721 }
1722 }
1723
1724 // modify idx_insert from first position if needed
1725 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1726 if (idx_insert_last != -1) {
1727 idx_insert = idx_insert_last;
1728 } else {
1729 idx_insert = size;
1730 }
1731 } else {
1732 if (idx_insert_first != -1) {
1733 idx_insert = idx_insert_first + 1;
1734 }
1735 }
1736
1737 // always read samples from chain input buffer
1738 effect->setInBuffer(mInBuffer);
1739
1740 // if last effect in the chain, output samples to chain
1741 // output buffer, otherwise to chain input buffer
1742 if (idx_insert == size) {
1743 if (idx_insert != 0) {
1744 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1745 mEffects[idx_insert-1]->configure();
1746 }
1747 effect->setOutBuffer(mOutBuffer);
1748 } else {
1749 effect->setOutBuffer(mInBuffer);
1750 }
1751 mEffects.insertAt(effect, idx_insert);
1752
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001753 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001754 idx_insert);
1755 }
1756 effect->configure();
1757 return NO_ERROR;
1758}
1759
Eric Laurentb378b732016-12-01 15:28:29 -08001760// removeEffect_l() must be called with ThreadBase::mLock held
1761size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1762 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001763{
1764 Mutex::Autolock _l(mLock);
1765 size_t size = mEffects.size();
1766 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1767
1768 for (size_t i = 0; i < size; i++) {
1769 if (effect == mEffects[i]) {
1770 // calling stop here will remove pre-processing effect from the audio HAL.
1771 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1772 // the middle of a read from audio HAL
1773 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1774 mEffects[i]->state() == EffectModule::STOPPING) {
1775 mEffects[i]->stop();
1776 }
Eric Laurentb378b732016-12-01 15:28:29 -08001777 if (release) {
1778 mEffects[i]->release_l();
1779 }
1780
Eric Laurentca7cc822012-11-19 14:55:58 -08001781 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1782 delete[] effect->inBuffer();
1783 } else {
1784 if (i == size - 1 && i != 0) {
1785 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1786 mEffects[i - 1]->configure();
1787 }
1788 }
1789 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001790 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001791 this, i);
Eric Laurentb378b732016-12-01 15:28:29 -08001792
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 break;
1794 }
1795 }
1796
1797 return mEffects.size();
1798}
1799
Eric Laurentb378b732016-12-01 15:28:29 -08001800// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001801void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1802{
1803 size_t size = mEffects.size();
1804 for (size_t i = 0; i < size; i++) {
1805 mEffects[i]->setDevice(device);
1806 }
1807}
1808
Eric Laurentb378b732016-12-01 15:28:29 -08001809// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001810void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1811{
1812 size_t size = mEffects.size();
1813 for (size_t i = 0; i < size; i++) {
1814 mEffects[i]->setMode(mode);
1815 }
1816}
1817
Eric Laurentb378b732016-12-01 15:28:29 -08001818// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001819void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1820{
1821 size_t size = mEffects.size();
1822 for (size_t i = 0; i < size; i++) {
1823 mEffects[i]->setAudioSource(source);
1824 }
1825}
1826
Eric Laurentb378b732016-12-01 15:28:29 -08001827// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001828bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001829{
1830 uint32_t newLeft = *left;
1831 uint32_t newRight = *right;
1832 bool hasControl = false;
1833 int ctrlIdx = -1;
1834 size_t size = mEffects.size();
1835
1836 // first update volume controller
1837 for (size_t i = size; i > 0; i--) {
1838 if (mEffects[i - 1]->isProcessEnabled() &&
1839 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1840 ctrlIdx = i - 1;
1841 hasControl = true;
1842 break;
1843 }
1844 }
1845
Eric Laurentfa1e1232016-08-02 19:01:49 -07001846 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001847 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001848 if (hasControl) {
1849 *left = mNewLeftVolume;
1850 *right = mNewRightVolume;
1851 }
1852 return hasControl;
1853 }
1854
1855 mVolumeCtrlIdx = ctrlIdx;
1856 mLeftVolume = newLeft;
1857 mRightVolume = newRight;
1858
1859 // second get volume update from volume controller
1860 if (ctrlIdx >= 0) {
1861 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1862 mNewLeftVolume = newLeft;
1863 mNewRightVolume = newRight;
1864 }
1865 // then indicate volume to all other effects in chain.
1866 // Pass altered volume to effects before volume controller
1867 // and requested volume to effects after controller
1868 uint32_t lVol = newLeft;
1869 uint32_t rVol = newRight;
1870
1871 for (size_t i = 0; i < size; i++) {
1872 if ((int)i == ctrlIdx) {
1873 continue;
1874 }
1875 // this also works for ctrlIdx == -1 when there is no volume controller
1876 if ((int)i > ctrlIdx) {
1877 lVol = *left;
1878 rVol = *right;
1879 }
1880 mEffects[i]->setVolume(&lVol, &rVol, false);
1881 }
1882 *left = newLeft;
1883 *right = newRight;
1884
1885 return hasControl;
1886}
1887
Eric Laurentb378b732016-12-01 15:28:29 -08001888// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001889void AudioFlinger::EffectChain::resetVolume_l()
1890{
Eric Laurente7449bf2016-08-03 18:44:07 -07001891 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1892 uint32_t left = mLeftVolume;
1893 uint32_t right = mRightVolume;
1894 (void)setVolume_l(&left, &right, true);
1895 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001896}
1897
Eric Laurent1b928682014-10-02 19:41:47 -07001898void AudioFlinger::EffectChain::syncHalEffectsState()
1899{
1900 Mutex::Autolock _l(mLock);
1901 for (size_t i = 0; i < mEffects.size(); i++) {
1902 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1903 mEffects[i]->state() == EffectModule::STOPPING) {
1904 mEffects[i]->addEffectToHal_l();
1905 }
1906 }
1907}
1908
Eric Laurentca7cc822012-11-19 14:55:58 -08001909void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1910{
1911 const size_t SIZE = 256;
1912 char buffer[SIZE];
1913 String8 result;
1914
Marco Nelissenb2208842014-02-07 14:00:50 -08001915 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001916 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001917 result.append(buffer);
1918
Marco Nelissenb2208842014-02-07 14:00:50 -08001919 if (numEffects) {
1920 bool locked = AudioFlinger::dumpTryLock(mLock);
1921 // failed to lock - AudioFlinger is probably deadlocked
1922 if (!locked) {
1923 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001924 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001925
Marco Nelissenb2208842014-02-07 14:00:50 -08001926 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001927 snprintf(buffer, SIZE, "\t%p %p %d\n",
1928 mInBuffer,
1929 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001930 mActiveTrackCnt);
1931 result.append(buffer);
1932 write(fd, result.string(), result.size());
1933
1934 for (size_t i = 0; i < numEffects; ++i) {
1935 sp<EffectModule> effect = mEffects[i];
1936 if (effect != 0) {
1937 effect->dump(fd, args);
1938 }
1939 }
1940
1941 if (locked) {
1942 mLock.unlock();
1943 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001944 }
1945}
1946
1947// must be called with ThreadBase::mLock held
1948void AudioFlinger::EffectChain::setEffectSuspended_l(
1949 const effect_uuid_t *type, bool suspend)
1950{
1951 sp<SuspendedEffectDesc> desc;
1952 // use effect type UUID timelow as key as there is no real risk of identical
1953 // timeLow fields among effect type UUIDs.
1954 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1955 if (suspend) {
1956 if (index >= 0) {
1957 desc = mSuspendedEffects.valueAt(index);
1958 } else {
1959 desc = new SuspendedEffectDesc();
1960 desc->mType = *type;
1961 mSuspendedEffects.add(type->timeLow, desc);
1962 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1963 }
1964 if (desc->mRefCount++ == 0) {
1965 sp<EffectModule> effect = getEffectIfEnabled(type);
1966 if (effect != 0) {
1967 desc->mEffect = effect;
1968 effect->setSuspended(true);
1969 effect->setEnabled(false);
1970 }
1971 }
1972 } else {
1973 if (index < 0) {
1974 return;
1975 }
1976 desc = mSuspendedEffects.valueAt(index);
1977 if (desc->mRefCount <= 0) {
1978 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1979 desc->mRefCount = 1;
1980 }
1981 if (--desc->mRefCount == 0) {
1982 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1983 if (desc->mEffect != 0) {
1984 sp<EffectModule> effect = desc->mEffect.promote();
1985 if (effect != 0) {
1986 effect->setSuspended(false);
1987 effect->lock();
1988 EffectHandle *handle = effect->controlHandle_l();
Eric Laurentb378b732016-12-01 15:28:29 -08001989 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001990 effect->setEnabled_l(handle->enabled());
1991 }
1992 effect->unlock();
1993 }
1994 desc->mEffect.clear();
1995 }
1996 mSuspendedEffects.removeItemsAt(index);
1997 }
1998 }
1999}
2000
2001// must be called with ThreadBase::mLock held
2002void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2003{
2004 sp<SuspendedEffectDesc> desc;
2005
2006 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2007 if (suspend) {
2008 if (index >= 0) {
2009 desc = mSuspendedEffects.valueAt(index);
2010 } else {
2011 desc = new SuspendedEffectDesc();
2012 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2013 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2014 }
2015 if (desc->mRefCount++ == 0) {
2016 Vector< sp<EffectModule> > effects;
2017 getSuspendEligibleEffects(effects);
2018 for (size_t i = 0; i < effects.size(); i++) {
2019 setEffectSuspended_l(&effects[i]->desc().type, true);
2020 }
2021 }
2022 } else {
2023 if (index < 0) {
2024 return;
2025 }
2026 desc = mSuspendedEffects.valueAt(index);
2027 if (desc->mRefCount <= 0) {
2028 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2029 desc->mRefCount = 1;
2030 }
2031 if (--desc->mRefCount == 0) {
2032 Vector<const effect_uuid_t *> types;
2033 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2034 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2035 continue;
2036 }
2037 types.add(&mSuspendedEffects.valueAt(i)->mType);
2038 }
2039 for (size_t i = 0; i < types.size(); i++) {
2040 setEffectSuspended_l(types[i], false);
2041 }
2042 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2043 mSuspendedEffects.keyAt(index));
2044 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2045 }
2046 }
2047}
2048
2049
2050// The volume effect is used for automated tests only
2051#ifndef OPENSL_ES_H_
2052static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2053 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2054const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2055#endif //OPENSL_ES_H_
2056
2057bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2058{
2059 // auxiliary effects and visualizer are never suspended on output mix
2060 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2061 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2062 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2063 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2064 return false;
2065 }
2066 return true;
2067}
2068
2069void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2070 Vector< sp<AudioFlinger::EffectModule> > &effects)
2071{
2072 effects.clear();
2073 for (size_t i = 0; i < mEffects.size(); i++) {
2074 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2075 effects.add(mEffects[i]);
2076 }
2077 }
2078}
2079
2080sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2081 const effect_uuid_t *type)
2082{
2083 sp<EffectModule> effect = getEffectFromType_l(type);
2084 return effect != 0 && effect->isEnabled() ? effect : 0;
2085}
2086
2087void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2088 bool enabled)
2089{
2090 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2091 if (enabled) {
2092 if (index < 0) {
2093 // if the effect is not suspend check if all effects are suspended
2094 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2095 if (index < 0) {
2096 return;
2097 }
2098 if (!isEffectEligibleForSuspend(effect->desc())) {
2099 return;
2100 }
2101 setEffectSuspended_l(&effect->desc().type, enabled);
2102 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2103 if (index < 0) {
2104 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2105 return;
2106 }
2107 }
2108 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2109 effect->desc().type.timeLow);
2110 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2111 // if effect is requested to suspended but was not yet enabled, supend it now.
2112 if (desc->mEffect == 0) {
2113 desc->mEffect = effect;
2114 effect->setEnabled(false);
2115 effect->setSuspended(true);
2116 }
2117 } else {
2118 if (index < 0) {
2119 return;
2120 }
2121 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2122 effect->desc().type.timeLow);
2123 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2124 desc->mEffect.clear();
2125 effect->setSuspended(false);
2126 }
2127}
2128
Eric Laurent5baf2af2013-09-12 17:37:00 -07002129bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002130{
2131 Mutex::Autolock _l(mLock);
2132 size_t size = mEffects.size();
2133 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002134 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002135 return true;
2136 }
2137 }
2138 return false;
2139}
2140
Eric Laurentaaa44472014-09-12 17:41:50 -07002141void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2142{
2143 Mutex::Autolock _l(mLock);
2144 mThread = thread;
2145 for (size_t i = 0; i < mEffects.size(); i++) {
2146 mEffects[i]->setThread(thread);
2147 }
2148}
2149
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002150void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2151{
2152 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2153 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2154 }
2155 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2156 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2157 }
2158}
2159
2160void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2161{
2162 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2163 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2164 }
2165 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2166 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2167 }
2168}
2169
2170bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002171{
2172 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002173 for (const auto &effect : mEffects) {
2174 if (effect->isProcessImplemented()) {
2175 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002176 }
2177 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002178 // Allow effects without processing.
2179 return true;
2180}
2181
2182bool AudioFlinger::EffectChain::isFastCompatible() const
2183{
2184 Mutex::Autolock _l(mLock);
2185 for (const auto &effect : mEffects) {
2186 if (effect->isProcessImplemented()
2187 && effect->isImplementationSoftware()) {
2188 return false;
2189 }
2190 }
2191 // Allow effects without processing or hw accelerated effects.
2192 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002193}
2194
2195// isCompatibleWithThread_l() must be called with thread->mLock held
2196bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2197{
2198 Mutex::Autolock _l(mLock);
2199 for (size_t i = 0; i < mEffects.size(); i++) {
2200 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2201 return false;
2202 }
2203 }
2204 return true;
2205}
2206
Glenn Kasten63238ef2015-03-02 15:50:29 -08002207} // namespace android