blob: e3e518c28f2e151ebdb0dc97cc39b4bb92934a7b [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,
Glenn Kastend848eb42016-03-08 13:42:11 -080062 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -080063 : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX),
64 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
65 mDescriptor(*desc),
66 // mConfig is set by configure() and not used before then
67 mEffectInterface(NULL),
68 mStatus(NO_INIT), mState(IDLE),
69 // mMaxDisableWaitCnt is set by configure() and not used before then
70 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070071 mSuspended(false),
72 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080073{
74 ALOGV("Constructor %p", this);
75 int lStatus;
76
77 // create effect engine from effect factory
78 mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
79
80 if (mStatus != NO_ERROR) {
81 return;
82 }
83 lStatus = init();
84 if (lStatus < 0) {
85 mStatus = lStatus;
86 goto Error;
87 }
88
89 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
90 return;
91Error:
92 EffectRelease(mEffectInterface);
93 mEffectInterface = NULL;
94 ALOGV("Constructor Error %d", mStatus);
95}
96
97AudioFlinger::EffectModule::~EffectModule()
98{
99 ALOGV("Destructor %p", this);
100 if (mEffectInterface != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800101 remove_effect_from_hal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800102 // release effect engine
103 EffectRelease(mEffectInterface);
104 }
105}
106
107status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
108{
109 status_t status;
110
111 Mutex::Autolock _l(mLock);
112 int priority = handle->priority();
113 size_t size = mHandles.size();
114 EffectHandle *controlHandle = NULL;
115 size_t i;
116 for (i = 0; i < size; i++) {
117 EffectHandle *h = mHandles[i];
118 if (h == NULL || h->destroyed_l()) {
119 continue;
120 }
121 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700122 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800123 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700124 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800125 if (h->priority() <= priority) {
126 break;
127 }
128 }
129 // if inserted in first place, move effect control from previous owner to this handle
130 if (i == 0) {
131 bool enabled = false;
132 if (controlHandle != NULL) {
133 enabled = controlHandle->enabled();
134 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
135 }
136 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
137 status = NO_ERROR;
138 } else {
139 status = ALREADY_EXISTS;
140 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700141 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800142 mHandles.insertAt(handle, i);
143 return status;
144}
145
146size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
147{
148 Mutex::Autolock _l(mLock);
149 size_t size = mHandles.size();
150 size_t i;
151 for (i = 0; i < size; i++) {
152 if (mHandles[i] == handle) {
153 break;
154 }
155 }
156 if (i == size) {
157 return size;
158 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700159 ALOGV("removeHandle() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800160
161 mHandles.removeAt(i);
162 // if removed from first place, move effect control from this handle to next in line
163 if (i == 0) {
164 EffectHandle *h = controlHandle_l();
165 if (h != NULL) {
166 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
167 }
168 }
169
170 // Prevent calls to process() and other functions on effect interface from now on.
171 // The effect engine will be released by the destructor when the last strong reference on
172 // this object is released which can happen after next process is called.
173 if (mHandles.size() == 0 && !mPinned) {
174 mState = DESTROYED;
175 }
176
177 return mHandles.size();
178}
179
180// must be called with EffectModule::mLock held
181AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
182{
183 // the first valid handle in the list has control over the module
184 for (size_t i = 0; i < mHandles.size(); i++) {
185 EffectHandle *h = mHandles[i];
186 if (h != NULL && !h->destroyed_l()) {
187 return h;
188 }
189 }
190
191 return NULL;
192}
193
194size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast)
195{
196 ALOGV("disconnect() %p handle %p", this, handle);
197 // keep a strong reference on this EffectModule to avoid calling the
198 // destructor before we exit
199 sp<EffectModule> keep(this);
200 {
Eric Laurentaaa44472014-09-12 17:41:50 -0700201 if (removeHandle(handle) == 0) {
202 if (!isPinned() || unpinIfLast) {
203 sp<ThreadBase> thread = mThread.promote();
204 if (thread != 0) {
205 Mutex::Autolock _l(thread->mLock);
206 thread->removeEffect_l(this);
207 }
208 sp<AudioFlinger> af = mAudioFlinger.promote();
209 if (af != 0) {
210 af->updateOrphanEffectChains(this);
211 }
212 AudioSystem::unregisterEffect(mId);
213 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800214 }
215 }
216 return mHandles.size();
217}
218
Eric Laurentfa1e1232016-08-02 19:01:49 -0700219bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800220 Mutex::Autolock _l(mLock);
221
Eric Laurentfa1e1232016-08-02 19:01:49 -0700222 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800223 switch (mState) {
224 case RESTART:
225 reset_l();
226 // FALL THROUGH
227
228 case STARTING:
229 // clear auxiliary effect input buffer for next accumulation
230 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
231 memset(mConfig.inputCfg.buffer.raw,
232 0,
233 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
234 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700235 if (start_l() == NO_ERROR) {
236 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700237 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700238 } else {
239 mState = IDLE;
240 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800241 break;
242 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700243 if (stop_l() == NO_ERROR) {
244 mDisableWaitCnt = mMaxDisableWaitCnt;
245 } else {
246 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
247 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800248 mState = STOPPED;
249 break;
250 case STOPPED:
251 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
252 // turn off sequence.
253 if (--mDisableWaitCnt == 0) {
254 reset_l();
255 mState = IDLE;
256 }
257 break;
258 default: //IDLE , ACTIVE, DESTROYED
259 break;
260 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700261
262 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800263}
264
265void AudioFlinger::EffectModule::process()
266{
267 Mutex::Autolock _l(mLock);
268
269 if (mState == DESTROYED || mEffectInterface == NULL ||
270 mConfig.inputCfg.buffer.raw == NULL ||
271 mConfig.outputCfg.buffer.raw == NULL) {
272 return;
273 }
274
275 if (isProcessEnabled()) {
276 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
277 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
278 ditherAndClamp(mConfig.inputCfg.buffer.s32,
279 mConfig.inputCfg.buffer.s32,
280 mConfig.inputCfg.buffer.frameCount/2);
281 }
282
283 // do the actual processing in the effect engine
284 int ret = (*mEffectInterface)->process(mEffectInterface,
285 &mConfig.inputCfg.buffer,
286 &mConfig.outputCfg.buffer);
287
288 // force transition to IDLE state when engine is ready
289 if (mState == STOPPED && ret == -ENODATA) {
290 mDisableWaitCnt = 1;
291 }
292
293 // clear auxiliary effect input buffer for next accumulation
294 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
295 memset(mConfig.inputCfg.buffer.raw, 0,
296 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
297 }
298 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
299 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
300 // If an insert effect is idle and input buffer is different from output buffer,
301 // accumulate input onto output
302 sp<EffectChain> chain = mChain.promote();
303 if (chain != 0 && chain->activeTrackCnt() != 0) {
304 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
305 int16_t *in = mConfig.inputCfg.buffer.s16;
306 int16_t *out = mConfig.outputCfg.buffer.s16;
307 for (size_t i = 0; i < frameCnt; i++) {
308 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
309 }
310 }
311 }
312}
313
314void AudioFlinger::EffectModule::reset_l()
315{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700316 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800317 return;
318 }
319 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
320}
321
322status_t AudioFlinger::EffectModule::configure()
323{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700324 status_t status;
325 sp<ThreadBase> thread;
326 uint32_t size;
327 audio_channel_mask_t channelMask;
328
Eric Laurentca7cc822012-11-19 14:55:58 -0800329 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700330 status = NO_INIT;
331 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800332 }
333
Eric Laurentd0ebb532013-04-02 16:41:41 -0700334 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800335 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700336 status = DEAD_OBJECT;
337 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800338 }
339
340 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700341 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700342 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800343
344 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
345 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
346 } else {
347 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700348 // TODO: Update this logic when multichannel effects are implemented.
349 // For offloaded tracks consider mono output as stereo for proper effect initialization
350 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
351 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
352 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
353 ALOGV("Overriding effect input and output as STEREO");
354 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800355 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700356
Eric Laurentca7cc822012-11-19 14:55:58 -0800357 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
358 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
359 mConfig.inputCfg.samplingRate = thread->sampleRate();
360 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
361 mConfig.inputCfg.bufferProvider.cookie = NULL;
362 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
363 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
364 mConfig.outputCfg.bufferProvider.cookie = NULL;
365 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
366 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
367 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
368 // Insert effect:
369 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
370 // always overwrites output buffer: input buffer == output buffer
371 // - in other sessions:
372 // last effect in the chain accumulates in output buffer: input buffer != output buffer
373 // other effect: overwrites output buffer: input buffer == output buffer
374 // Auxiliary effect:
375 // accumulates in output buffer: input buffer != output buffer
376 // Therefore: accumulate <=> input buffer != output buffer
377 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
378 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
379 } else {
380 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
381 }
382 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
383 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
384 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
385 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
386
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700387 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800388 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
389
390 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700391 size = sizeof(int);
392 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800393 EFFECT_CMD_SET_CONFIG,
394 sizeof(effect_config_t),
395 &mConfig,
396 &size,
397 &cmdStatus);
398 if (status == 0) {
399 status = cmdStatus;
400 }
401
402 if (status == 0 &&
403 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
404 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
405 effect_param_t *p = (effect_param_t *)buf32;
406
407 p->psize = sizeof(uint32_t);
408 p->vsize = sizeof(uint32_t);
409 size = sizeof(int);
410 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
411
412 uint32_t latency = 0;
413 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
414 if (pbt != NULL) {
415 latency = pbt->latency_l();
416 }
417
418 *((int32_t *)p->data + 1)= latency;
419 (*mEffectInterface)->command(mEffectInterface,
420 EFFECT_CMD_SET_PARAM,
421 sizeof(effect_param_t) + 8,
422 &buf32,
423 &size,
424 &cmdStatus);
425 }
426
427 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
428 (1000 * mConfig.outputCfg.buffer.frameCount);
429
Eric Laurentd0ebb532013-04-02 16:41:41 -0700430exit:
431 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800432 return status;
433}
434
435status_t AudioFlinger::EffectModule::init()
436{
437 Mutex::Autolock _l(mLock);
438 if (mEffectInterface == NULL) {
439 return NO_INIT;
440 }
441 status_t cmdStatus;
442 uint32_t size = sizeof(status_t);
443 status_t status = (*mEffectInterface)->command(mEffectInterface,
444 EFFECT_CMD_INIT,
445 0,
446 NULL,
447 &size,
448 &cmdStatus);
449 if (status == 0) {
450 status = cmdStatus;
451 }
452 return status;
453}
454
Eric Laurent1b928682014-10-02 19:41:47 -0700455void AudioFlinger::EffectModule::addEffectToHal_l()
456{
457 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
458 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
459 sp<ThreadBase> thread = mThread.promote();
460 if (thread != 0) {
461 audio_stream_t *stream = thread->stream();
462 if (stream != NULL) {
463 stream->add_audio_effect(stream, mEffectInterface);
464 }
465 }
466 }
467}
468
Eric Laurentfa1e1232016-08-02 19:01:49 -0700469// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800470status_t AudioFlinger::EffectModule::start()
471{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700472 sp<EffectChain> chain;
473 status_t status;
474 {
475 Mutex::Autolock _l(mLock);
476 status = start_l();
477 if (status == NO_ERROR) {
478 chain = mChain.promote();
479 }
480 }
481 if (chain != 0) {
482 chain->resetVolume_l();
483 }
484 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800485}
486
487status_t AudioFlinger::EffectModule::start_l()
488{
489 if (mEffectInterface == NULL) {
490 return NO_INIT;
491 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700492 if (mStatus != NO_ERROR) {
493 return mStatus;
494 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800495 status_t cmdStatus;
496 uint32_t size = sizeof(status_t);
497 status_t status = (*mEffectInterface)->command(mEffectInterface,
498 EFFECT_CMD_ENABLE,
499 0,
500 NULL,
501 &size,
502 &cmdStatus);
503 if (status == 0) {
504 status = cmdStatus;
505 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700506 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700507 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800508 }
509 return status;
510}
511
512status_t AudioFlinger::EffectModule::stop()
513{
514 Mutex::Autolock _l(mLock);
515 return stop_l();
516}
517
518status_t AudioFlinger::EffectModule::stop_l()
519{
520 if (mEffectInterface == NULL) {
521 return NO_INIT;
522 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700523 if (mStatus != NO_ERROR) {
524 return mStatus;
525 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800526 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800527 uint32_t size = sizeof(status_t);
528 status_t status = (*mEffectInterface)->command(mEffectInterface,
529 EFFECT_CMD_DISABLE,
530 0,
531 NULL,
532 &size,
533 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800534 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800535 status = cmdStatus;
536 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800537 if (status == NO_ERROR) {
538 status = remove_effect_from_hal_l();
539 }
540 return status;
541}
542
543status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
544{
545 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
546 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800547 sp<ThreadBase> thread = mThread.promote();
548 if (thread != 0) {
549 audio_stream_t *stream = thread->stream();
550 if (stream != NULL) {
551 stream->remove_audio_effect(stream, mEffectInterface);
552 }
553 }
554 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800555 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800556}
557
558status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
559 uint32_t cmdSize,
560 void *pCmdData,
561 uint32_t *replySize,
562 void *pReplyData)
563{
564 Mutex::Autolock _l(mLock);
565 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
566
567 if (mState == DESTROYED || mEffectInterface == NULL) {
568 return NO_INIT;
569 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700570 if (mStatus != NO_ERROR) {
571 return mStatus;
572 }
Andy Hung110bc952016-06-20 15:22:52 -0700573 if (cmdCode == EFFECT_CMD_GET_PARAM &&
574 (*replySize < sizeof(effect_param_t) ||
575 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
576 android_errorWriteLog(0x534e4554, "29251553");
577 return -EINVAL;
578 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 status_t status = (*mEffectInterface)->command(mEffectInterface,
580 cmdCode,
581 cmdSize,
582 pCmdData,
583 replySize,
584 pReplyData);
585 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
586 uint32_t size = (replySize == NULL) ? 0 : *replySize;
587 for (size_t i = 1; i < mHandles.size(); i++) {
588 EffectHandle *h = mHandles[i];
589 if (h != NULL && !h->destroyed_l()) {
590 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
591 }
592 }
593 }
594 return status;
595}
596
597status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
598{
599 Mutex::Autolock _l(mLock);
600 return setEnabled_l(enabled);
601}
602
603// must be called with EffectModule::mLock held
604status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
605{
606
607 ALOGV("setEnabled %p enabled %d", this, enabled);
608
609 if (enabled != isEnabled()) {
610 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
611 if (enabled && status != NO_ERROR) {
612 return status;
613 }
614
615 switch (mState) {
616 // going from disabled to enabled
617 case IDLE:
618 mState = STARTING;
619 break;
620 case STOPPED:
621 mState = RESTART;
622 break;
623 case STOPPING:
624 mState = ACTIVE;
625 break;
626
627 // going from enabled to disabled
628 case RESTART:
629 mState = STOPPED;
630 break;
631 case STARTING:
632 mState = IDLE;
633 break;
634 case ACTIVE:
635 mState = STOPPING;
636 break;
637 case DESTROYED:
638 return NO_ERROR; // simply ignore as we are being destroyed
639 }
640 for (size_t i = 1; i < mHandles.size(); i++) {
641 EffectHandle *h = mHandles[i];
642 if (h != NULL && !h->destroyed_l()) {
643 h->setEnabled(enabled);
644 }
645 }
646 }
647 return NO_ERROR;
648}
649
650bool AudioFlinger::EffectModule::isEnabled() const
651{
652 switch (mState) {
653 case RESTART:
654 case STARTING:
655 case ACTIVE:
656 return true;
657 case IDLE:
658 case STOPPING:
659 case STOPPED:
660 case DESTROYED:
661 default:
662 return false;
663 }
664}
665
666bool AudioFlinger::EffectModule::isProcessEnabled() const
667{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700668 if (mStatus != NO_ERROR) {
669 return false;
670 }
671
Eric Laurentca7cc822012-11-19 14:55:58 -0800672 switch (mState) {
673 case RESTART:
674 case ACTIVE:
675 case STOPPING:
676 case STOPPED:
677 return true;
678 case IDLE:
679 case STARTING:
680 case DESTROYED:
681 default:
682 return false;
683 }
684}
685
686status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
687{
688 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700689 if (mStatus != NO_ERROR) {
690 return mStatus;
691 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800692 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800693 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
694 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
695 if (isProcessEnabled() &&
696 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
697 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800698 uint32_t volume[2];
699 uint32_t *pVolume = NULL;
700 uint32_t size = sizeof(volume);
701 volume[0] = *left;
702 volume[1] = *right;
703 if (controller) {
704 pVolume = volume;
705 }
706 status = (*mEffectInterface)->command(mEffectInterface,
707 EFFECT_CMD_SET_VOLUME,
708 size,
709 volume,
710 &size,
711 pVolume);
712 if (controller && status == NO_ERROR && size == sizeof(volume)) {
713 *left = volume[0];
714 *right = volume[1];
715 }
716 }
717 return status;
718}
719
720status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
721{
722 if (device == AUDIO_DEVICE_NONE) {
723 return NO_ERROR;
724 }
725
726 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700727 if (mStatus != NO_ERROR) {
728 return mStatus;
729 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800730 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700731 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800732 status_t cmdStatus;
733 uint32_t size = sizeof(status_t);
734 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
735 EFFECT_CMD_SET_INPUT_DEVICE;
736 status = (*mEffectInterface)->command(mEffectInterface,
737 cmd,
738 sizeof(uint32_t),
739 &device,
740 &size,
741 &cmdStatus);
742 }
743 return status;
744}
745
746status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
747{
748 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700749 if (mStatus != NO_ERROR) {
750 return mStatus;
751 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800752 status_t status = NO_ERROR;
753 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
754 status_t cmdStatus;
755 uint32_t size = sizeof(status_t);
756 status = (*mEffectInterface)->command(mEffectInterface,
757 EFFECT_CMD_SET_AUDIO_MODE,
758 sizeof(audio_mode_t),
759 &mode,
760 &size,
761 &cmdStatus);
762 if (status == NO_ERROR) {
763 status = cmdStatus;
764 }
765 }
766 return status;
767}
768
769status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
770{
771 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700772 if (mStatus != NO_ERROR) {
773 return mStatus;
774 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800775 status_t status = NO_ERROR;
776 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
777 uint32_t size = 0;
778 status = (*mEffectInterface)->command(mEffectInterface,
779 EFFECT_CMD_SET_AUDIO_SOURCE,
780 sizeof(audio_source_t),
781 &source,
782 &size,
783 NULL);
784 }
785 return status;
786}
787
788void AudioFlinger::EffectModule::setSuspended(bool suspended)
789{
790 Mutex::Autolock _l(mLock);
791 mSuspended = suspended;
792}
793
794bool AudioFlinger::EffectModule::suspended() const
795{
796 Mutex::Autolock _l(mLock);
797 return mSuspended;
798}
799
800bool AudioFlinger::EffectModule::purgeHandles()
801{
802 bool enabled = false;
803 Mutex::Autolock _l(mLock);
804 for (size_t i = 0; i < mHandles.size(); i++) {
805 EffectHandle *handle = mHandles[i];
806 if (handle != NULL && !handle->destroyed_l()) {
807 handle->effect().clear();
808 if (handle->hasControl()) {
809 enabled = handle->enabled();
810 }
811 }
812 }
813 return enabled;
814}
815
Eric Laurent5baf2af2013-09-12 17:37:00 -0700816status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
817{
818 Mutex::Autolock _l(mLock);
819 if (mStatus != NO_ERROR) {
820 return mStatus;
821 }
822 status_t status = NO_ERROR;
823 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
824 status_t cmdStatus;
825 uint32_t size = sizeof(status_t);
826 effect_offload_param_t cmd;
827
828 cmd.isOffload = offloaded;
829 cmd.ioHandle = io;
830 status = (*mEffectInterface)->command(mEffectInterface,
831 EFFECT_CMD_OFFLOAD,
832 sizeof(effect_offload_param_t),
833 &cmd,
834 &size,
835 &cmdStatus);
836 if (status == NO_ERROR) {
837 status = cmdStatus;
838 }
839 mOffloaded = (status == NO_ERROR) ? offloaded : false;
840 } else {
841 if (offloaded) {
842 status = INVALID_OPERATION;
843 }
844 mOffloaded = false;
845 }
846 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
847 return status;
848}
849
850bool AudioFlinger::EffectModule::isOffloaded() const
851{
852 Mutex::Autolock _l(mLock);
853 return mOffloaded;
854}
855
Marco Nelissenb2208842014-02-07 14:00:50 -0800856String8 effectFlagsToString(uint32_t flags) {
857 String8 s;
858
859 s.append("conn. mode: ");
860 switch (flags & EFFECT_FLAG_TYPE_MASK) {
861 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
862 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
863 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
864 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
865 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
866 default: s.append("unknown/reserved"); break;
867 }
868 s.append(", ");
869
870 s.append("insert pref: ");
871 switch (flags & EFFECT_FLAG_INSERT_MASK) {
872 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
873 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
874 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
875 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
876 default: s.append("unknown/reserved"); break;
877 }
878 s.append(", ");
879
880 s.append("volume mgmt: ");
881 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
882 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
883 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
884 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
885 default: s.append("unknown/reserved"); break;
886 }
887 s.append(", ");
888
889 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
890 if (devind) {
891 s.append("device indication: ");
892 switch (devind) {
893 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
894 default: s.append("unknown/reserved"); break;
895 }
896 s.append(", ");
897 }
898
899 s.append("input mode: ");
900 switch (flags & EFFECT_FLAG_INPUT_MASK) {
901 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
902 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
903 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
904 default: s.append("not set"); break;
905 }
906 s.append(", ");
907
908 s.append("output mode: ");
909 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
910 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
911 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
912 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
913 default: s.append("not set"); break;
914 }
915 s.append(", ");
916
917 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
918 if (accel) {
919 s.append("hardware acceleration: ");
920 switch (accel) {
921 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
922 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
923 default: s.append("unknown/reserved"); break;
924 }
925 s.append(", ");
926 }
927
928 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
929 if (modeind) {
930 s.append("mode indication: ");
931 switch (modeind) {
932 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
933 default: s.append("unknown/reserved"); break;
934 }
935 s.append(", ");
936 }
937
938 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
939 if (srcind) {
940 s.append("source indication: ");
941 switch (srcind) {
942 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
943 default: s.append("unknown/reserved"); break;
944 }
945 s.append(", ");
946 }
947
948 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
949 s.append("offloadable, ");
950 }
951
952 int len = s.length();
953 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -0700954 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -0800955 s.unlockBuffer(len - 2);
956 }
957 return s;
958}
959
960
Glenn Kasten0f11b512014-01-31 16:18:54 -0800961void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -0800962{
963 const size_t SIZE = 256;
964 char buffer[SIZE];
965 String8 result;
966
967 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
968 result.append(buffer);
969
970 bool locked = AudioFlinger::dumpTryLock(mLock);
971 // failed to lock - AudioFlinger is probably deadlocked
972 if (!locked) {
973 result.append("\t\tCould not lock Fx mutex:\n");
974 }
975
976 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000977 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
978 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -0800979 result.append(buffer);
980
981 result.append("\t\tDescriptor:\n");
982 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
983 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
984 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
985 mDescriptor.uuid.node[2],
986 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
987 result.append(buffer);
988 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
989 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
990 mDescriptor.type.timeHiAndVersion,
991 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
992 mDescriptor.type.node[2],
993 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
994 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -0800995 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -0800996 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -0800997 mDescriptor.flags,
998 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -0800999 result.append(buffer);
1000 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1001 mDescriptor.name);
1002 result.append(buffer);
1003 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1004 mDescriptor.implementor);
1005 result.append(buffer);
1006
1007 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001008 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001009 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001010 mConfig.inputCfg.buffer.frameCount,
1011 mConfig.inputCfg.samplingRate,
1012 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001013 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001014 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001015 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001016 result.append(buffer);
1017
1018 result.append("\t\t- Output configuration:\n");
1019 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001020 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001021 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001022 mConfig.outputCfg.buffer.frameCount,
1023 mConfig.outputCfg.samplingRate,
1024 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001025 mConfig.outputCfg.format,
1026 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001027 result.append(buffer);
1028
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001029 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001030 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001031 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001032 for (size_t i = 0; i < mHandles.size(); ++i) {
1033 EffectHandle *handle = mHandles[i];
1034 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001035 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 result.append(buffer);
1037 }
1038 }
1039
Eric Laurentca7cc822012-11-19 14:55:58 -08001040 write(fd, result.string(), result.length());
1041
1042 if (locked) {
1043 mLock.unlock();
1044 }
1045}
1046
1047// ----------------------------------------------------------------------------
1048// EffectHandle implementation
1049// ----------------------------------------------------------------------------
1050
1051#undef LOG_TAG
1052#define LOG_TAG "AudioFlinger::EffectHandle"
1053
1054AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1055 const sp<AudioFlinger::Client>& client,
1056 const sp<IEffectClient>& effectClient,
1057 int32_t priority)
1058 : BnEffect(),
1059 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1060 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1061{
1062 ALOGV("constructor %p", this);
1063
1064 if (client == 0) {
1065 return;
1066 }
1067 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1068 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001069 if (mCblkMemory == 0 ||
1070 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001071 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001072 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001073 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001074 return;
1075 }
Glenn Kastene75da402013-11-20 13:54:52 -08001076 new(mCblk) effect_param_cblk_t();
1077 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001078}
1079
1080AudioFlinger::EffectHandle::~EffectHandle()
1081{
1082 ALOGV("Destructor %p", this);
1083
1084 if (mEffect == 0) {
1085 mDestroyed = true;
1086 return;
1087 }
1088 mEffect->lock();
1089 mDestroyed = true;
1090 mEffect->unlock();
1091 disconnect(false);
1092}
1093
Glenn Kastene75da402013-11-20 13:54:52 -08001094status_t AudioFlinger::EffectHandle::initCheck()
1095{
1096 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1097}
1098
Eric Laurentca7cc822012-11-19 14:55:58 -08001099status_t AudioFlinger::EffectHandle::enable()
1100{
1101 ALOGV("enable %p", this);
1102 if (!mHasControl) {
1103 return INVALID_OPERATION;
1104 }
1105 if (mEffect == 0) {
1106 return DEAD_OBJECT;
1107 }
1108
1109 if (mEnabled) {
1110 return NO_ERROR;
1111 }
1112
1113 mEnabled = true;
1114
1115 sp<ThreadBase> thread = mEffect->thread().promote();
1116 if (thread != 0) {
1117 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1118 }
1119
1120 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1121 if (mEffect->suspended()) {
1122 return NO_ERROR;
1123 }
1124
1125 status_t status = mEffect->setEnabled(true);
1126 if (status != NO_ERROR) {
1127 if (thread != 0) {
1128 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1129 }
1130 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001131 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001132 if (thread != 0) {
1133 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001134 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001135 Mutex::Autolock _l(t->mLock);
1136 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001137 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001138 if (!mEffect->isOffloadable()) {
1139 if (thread->type() == ThreadBase::OFFLOAD) {
1140 PlaybackThread *t = (PlaybackThread *)thread.get();
1141 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1142 }
1143 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1144 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1145 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001146 }
1147 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001148 }
1149 return status;
1150}
1151
1152status_t AudioFlinger::EffectHandle::disable()
1153{
1154 ALOGV("disable %p", this);
1155 if (!mHasControl) {
1156 return INVALID_OPERATION;
1157 }
1158 if (mEffect == 0) {
1159 return DEAD_OBJECT;
1160 }
1161
1162 if (!mEnabled) {
1163 return NO_ERROR;
1164 }
1165 mEnabled = false;
1166
1167 if (mEffect->suspended()) {
1168 return NO_ERROR;
1169 }
1170
1171 status_t status = mEffect->setEnabled(false);
1172
1173 sp<ThreadBase> thread = mEffect->thread().promote();
1174 if (thread != 0) {
1175 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001176 if (thread->type() == ThreadBase::OFFLOAD) {
1177 PlaybackThread *t = (PlaybackThread *)thread.get();
1178 Mutex::Autolock _l(t->mLock);
1179 t->broadcast_l();
1180 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001181 }
1182
1183 return status;
1184}
1185
1186void AudioFlinger::EffectHandle::disconnect()
1187{
1188 disconnect(true);
1189}
1190
1191void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1192{
1193 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1194 if (mEffect == 0) {
1195 return;
1196 }
1197 // restore suspended effects if the disconnected handle was enabled and the last one.
1198 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1199 sp<ThreadBase> thread = mEffect->thread().promote();
1200 if (thread != 0) {
1201 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1202 }
1203 }
1204
1205 // release sp on module => module destructor can be called now
1206 mEffect.clear();
1207 if (mClient != 0) {
1208 if (mCblk != NULL) {
1209 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1210 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1211 }
1212 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001213 // Client destructor must run with AudioFlinger client mutex locked
1214 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001215 mClient.clear();
1216 }
1217}
1218
1219status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1220 uint32_t cmdSize,
1221 void *pCmdData,
1222 uint32_t *replySize,
1223 void *pReplyData)
1224{
1225 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1226 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1227
1228 // only get parameter command is permitted for applications not controlling the effect
1229 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1230 return INVALID_OPERATION;
1231 }
1232 if (mEffect == 0) {
1233 return DEAD_OBJECT;
1234 }
1235 if (mClient == 0) {
1236 return INVALID_OPERATION;
1237 }
1238
1239 // handle commands that are not forwarded transparently to effect engine
1240 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1241 // No need to trylock() here as this function is executed in the binder thread serving a
1242 // particular client process: no risk to block the whole media server process or mixer
1243 // threads if we are stuck here
1244 Mutex::Autolock _l(mCblk->lock);
1245 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1246 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1247 mCblk->serverIndex = 0;
1248 mCblk->clientIndex = 0;
1249 return BAD_VALUE;
1250 }
1251 status_t status = NO_ERROR;
1252 while (mCblk->serverIndex < mCblk->clientIndex) {
1253 int reply;
1254 uint32_t rsize = sizeof(int);
1255 int *p = (int *)(mBuffer + mCblk->serverIndex);
1256 int size = *p++;
1257 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1258 ALOGW("command(): invalid parameter block size");
1259 break;
1260 }
1261 effect_param_t *param = (effect_param_t *)p;
1262 if (param->psize == 0 || param->vsize == 0) {
1263 ALOGW("command(): null parameter or value size");
1264 mCblk->serverIndex += size;
1265 continue;
1266 }
1267 uint32_t psize = sizeof(effect_param_t) +
1268 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1269 param->vsize;
1270 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1271 psize,
1272 p,
1273 &rsize,
1274 &reply);
1275 // stop at first error encountered
1276 if (ret != NO_ERROR) {
1277 status = ret;
1278 *(int *)pReplyData = reply;
1279 break;
1280 } else if (reply != NO_ERROR) {
1281 *(int *)pReplyData = reply;
1282 break;
1283 }
1284 mCblk->serverIndex += size;
1285 }
1286 mCblk->serverIndex = 0;
1287 mCblk->clientIndex = 0;
1288 return status;
1289 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1290 *(int *)pReplyData = NO_ERROR;
1291 return enable();
1292 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1293 *(int *)pReplyData = NO_ERROR;
1294 return disable();
1295 }
1296
1297 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1298}
1299
1300void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1301{
1302 ALOGV("setControl %p control %d", this, hasControl);
1303
1304 mHasControl = hasControl;
1305 mEnabled = enabled;
1306
1307 if (signal && mEffectClient != 0) {
1308 mEffectClient->controlStatusChanged(hasControl);
1309 }
1310}
1311
1312void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1313 uint32_t cmdSize,
1314 void *pCmdData,
1315 uint32_t replySize,
1316 void *pReplyData)
1317{
1318 if (mEffectClient != 0) {
1319 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1320 }
1321}
1322
1323
1324
1325void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1326{
1327 if (mEffectClient != 0) {
1328 mEffectClient->enableStatusChanged(enabled);
1329 }
1330}
1331
1332status_t AudioFlinger::EffectHandle::onTransact(
1333 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1334{
1335 return BnEffect::onTransact(code, data, reply, flags);
1336}
1337
1338
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001339void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001340{
1341 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1342
Marco Nelissenb2208842014-02-07 14:00:50 -08001343 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001344 (mClient == 0) ? getpid_cached : mClient->pid(),
1345 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001346 mHasControl ? "yes" : "no",
1347 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001348 mCblk ? mCblk->clientIndex : 0,
1349 mCblk ? mCblk->serverIndex : 0
1350 );
1351
1352 if (locked) {
1353 mCblk->lock.unlock();
1354 }
1355}
1356
1357#undef LOG_TAG
1358#define LOG_TAG "AudioFlinger::EffectChain"
1359
1360AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001361 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001362 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1363 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001364 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001365{
1366 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1367 if (thread == NULL) {
1368 return;
1369 }
1370 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1371 thread->frameCount();
1372}
1373
1374AudioFlinger::EffectChain::~EffectChain()
1375{
1376 if (mOwnInBuffer) {
1377 delete mInBuffer;
1378 }
1379
1380}
1381
1382// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1383sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1384 effect_descriptor_t *descriptor)
1385{
1386 size_t size = mEffects.size();
1387
1388 for (size_t i = 0; i < size; i++) {
1389 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1390 return mEffects[i];
1391 }
1392 }
1393 return 0;
1394}
1395
1396// getEffectFromId_l() must be called with ThreadBase::mLock held
1397sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1398{
1399 size_t size = mEffects.size();
1400
1401 for (size_t i = 0; i < size; i++) {
1402 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1403 if (id == 0 || mEffects[i]->id() == id) {
1404 return mEffects[i];
1405 }
1406 }
1407 return 0;
1408}
1409
1410// getEffectFromType_l() must be called with ThreadBase::mLock held
1411sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1412 const effect_uuid_t *type)
1413{
1414 size_t size = mEffects.size();
1415
1416 for (size_t i = 0; i < size; i++) {
1417 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1418 return mEffects[i];
1419 }
1420 }
1421 return 0;
1422}
1423
1424void AudioFlinger::EffectChain::clearInputBuffer()
1425{
1426 Mutex::Autolock _l(mLock);
1427 sp<ThreadBase> thread = mThread.promote();
1428 if (thread == 0) {
1429 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1430 return;
1431 }
1432 clearInputBuffer_l(thread);
1433}
1434
1435// Must be called with EffectChain::mLock locked
1436void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1437{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001438 // TODO: This will change in the future, depending on multichannel
1439 // and sample format changes for effects.
1440 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1441 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001442 const size_t frameSize =
1443 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001444 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001445}
1446
1447// Must be called with EffectChain::mLock locked
1448void AudioFlinger::EffectChain::process_l()
1449{
1450 sp<ThreadBase> thread = mThread.promote();
1451 if (thread == 0) {
1452 ALOGW("process_l(): cannot promote mixer thread");
1453 return;
1454 }
1455 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1456 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001457 // never process effects when:
1458 // - on an OFFLOAD thread
1459 // - no more tracks are on the session and the effect tail has been rendered
1460 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001461 if (!isGlobalSession) {
1462 bool tracksOnSession = (trackCnt() != 0);
1463
1464 if (!tracksOnSession && mTailBufferCount == 0) {
1465 doProcess = false;
1466 }
1467
1468 if (activeTrackCnt() == 0) {
1469 // if no track is active and the effect tail has not been rendered,
1470 // the input buffer must be cleared here as the mixer process will not do it
1471 if (tracksOnSession || mTailBufferCount > 0) {
1472 clearInputBuffer_l(thread);
1473 if (mTailBufferCount > 0) {
1474 mTailBufferCount--;
1475 }
1476 }
1477 }
1478 }
1479
1480 size_t size = mEffects.size();
1481 if (doProcess) {
1482 for (size_t i = 0; i < size; i++) {
1483 mEffects[i]->process();
1484 }
1485 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001486 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001487 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001488 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1489 }
1490 if (doResetVolume) {
1491 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001492 }
1493}
1494
1495// addEffect_l() must be called with PlaybackThread::mLock held
1496status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1497{
1498 effect_descriptor_t desc = effect->desc();
1499 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1500
1501 Mutex::Autolock _l(mLock);
1502 effect->setChain(this);
1503 sp<ThreadBase> thread = mThread.promote();
1504 if (thread == 0) {
1505 return NO_INIT;
1506 }
1507 effect->setThread(thread);
1508
1509 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1510 // Auxiliary effects are inserted at the beginning of mEffects vector as
1511 // they are processed first and accumulated in chain input buffer
1512 mEffects.insertAt(effect, 0);
1513
1514 // the input buffer for auxiliary effect contains mono samples in
1515 // 32 bit format. This is to avoid saturation in AudoMixer
1516 // accumulation stage. Saturation is done in EffectModule::process() before
1517 // calling the process in effect engine
1518 size_t numSamples = thread->frameCount();
1519 int32_t *buffer = new int32_t[numSamples];
1520 memset(buffer, 0, numSamples * sizeof(int32_t));
1521 effect->setInBuffer((int16_t *)buffer);
1522 // auxiliary effects output samples to chain input buffer for further processing
1523 // by insert effects
1524 effect->setOutBuffer(mInBuffer);
1525 } else {
1526 // Insert effects are inserted at the end of mEffects vector as they are processed
1527 // after track and auxiliary effects.
1528 // Insert effect order as a function of indicated preference:
1529 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1530 // another effect is present
1531 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1532 // last effect claiming first position
1533 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1534 // first effect claiming last position
1535 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1536 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1537 // already present
1538
1539 size_t size = mEffects.size();
1540 size_t idx_insert = size;
1541 ssize_t idx_insert_first = -1;
1542 ssize_t idx_insert_last = -1;
1543
1544 for (size_t i = 0; i < size; i++) {
1545 effect_descriptor_t d = mEffects[i]->desc();
1546 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1547 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1548 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1549 // check invalid effect chaining combinations
1550 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1551 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1552 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1553 desc.name, d.name);
1554 return INVALID_OPERATION;
1555 }
1556 // remember position of first insert effect and by default
1557 // select this as insert position for new effect
1558 if (idx_insert == size) {
1559 idx_insert = i;
1560 }
1561 // remember position of last insert effect claiming
1562 // first position
1563 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1564 idx_insert_first = i;
1565 }
1566 // remember position of first insert effect claiming
1567 // last position
1568 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1569 idx_insert_last == -1) {
1570 idx_insert_last = i;
1571 }
1572 }
1573 }
1574
1575 // modify idx_insert from first position if needed
1576 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1577 if (idx_insert_last != -1) {
1578 idx_insert = idx_insert_last;
1579 } else {
1580 idx_insert = size;
1581 }
1582 } else {
1583 if (idx_insert_first != -1) {
1584 idx_insert = idx_insert_first + 1;
1585 }
1586 }
1587
1588 // always read samples from chain input buffer
1589 effect->setInBuffer(mInBuffer);
1590
1591 // if last effect in the chain, output samples to chain
1592 // output buffer, otherwise to chain input buffer
1593 if (idx_insert == size) {
1594 if (idx_insert != 0) {
1595 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1596 mEffects[idx_insert-1]->configure();
1597 }
1598 effect->setOutBuffer(mOutBuffer);
1599 } else {
1600 effect->setOutBuffer(mInBuffer);
1601 }
1602 mEffects.insertAt(effect, idx_insert);
1603
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001604 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001605 idx_insert);
1606 }
1607 effect->configure();
1608 return NO_ERROR;
1609}
1610
1611// removeEffect_l() must be called with PlaybackThread::mLock held
1612size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1613{
1614 Mutex::Autolock _l(mLock);
1615 size_t size = mEffects.size();
1616 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1617
1618 for (size_t i = 0; i < size; i++) {
1619 if (effect == mEffects[i]) {
1620 // calling stop here will remove pre-processing effect from the audio HAL.
1621 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1622 // the middle of a read from audio HAL
1623 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1624 mEffects[i]->state() == EffectModule::STOPPING) {
1625 mEffects[i]->stop();
1626 }
1627 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1628 delete[] effect->inBuffer();
1629 } else {
1630 if (i == size - 1 && i != 0) {
1631 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1632 mEffects[i - 1]->configure();
1633 }
1634 }
1635 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001636 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001637 this, i);
1638 break;
1639 }
1640 }
1641
1642 return mEffects.size();
1643}
1644
1645// setDevice_l() must be called with PlaybackThread::mLock held
1646void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1647{
1648 size_t size = mEffects.size();
1649 for (size_t i = 0; i < size; i++) {
1650 mEffects[i]->setDevice(device);
1651 }
1652}
1653
1654// setMode_l() must be called with PlaybackThread::mLock held
1655void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1656{
1657 size_t size = mEffects.size();
1658 for (size_t i = 0; i < size; i++) {
1659 mEffects[i]->setMode(mode);
1660 }
1661}
1662
1663// setAudioSource_l() must be called with PlaybackThread::mLock held
1664void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1665{
1666 size_t size = mEffects.size();
1667 for (size_t i = 0; i < size; i++) {
1668 mEffects[i]->setAudioSource(source);
1669 }
1670}
1671
Eric Laurentfa1e1232016-08-02 19:01:49 -07001672// setVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1673bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001674{
1675 uint32_t newLeft = *left;
1676 uint32_t newRight = *right;
1677 bool hasControl = false;
1678 int ctrlIdx = -1;
1679 size_t size = mEffects.size();
1680
1681 // first update volume controller
1682 for (size_t i = size; i > 0; i--) {
1683 if (mEffects[i - 1]->isProcessEnabled() &&
1684 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1685 ctrlIdx = i - 1;
1686 hasControl = true;
1687 break;
1688 }
1689 }
1690
Eric Laurentfa1e1232016-08-02 19:01:49 -07001691 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001692 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001693 if (hasControl) {
1694 *left = mNewLeftVolume;
1695 *right = mNewRightVolume;
1696 }
1697 return hasControl;
1698 }
1699
1700 mVolumeCtrlIdx = ctrlIdx;
1701 mLeftVolume = newLeft;
1702 mRightVolume = newRight;
1703
1704 // second get volume update from volume controller
1705 if (ctrlIdx >= 0) {
1706 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1707 mNewLeftVolume = newLeft;
1708 mNewRightVolume = newRight;
1709 }
1710 // then indicate volume to all other effects in chain.
1711 // Pass altered volume to effects before volume controller
1712 // and requested volume to effects after controller
1713 uint32_t lVol = newLeft;
1714 uint32_t rVol = newRight;
1715
1716 for (size_t i = 0; i < size; i++) {
1717 if ((int)i == ctrlIdx) {
1718 continue;
1719 }
1720 // this also works for ctrlIdx == -1 when there is no volume controller
1721 if ((int)i > ctrlIdx) {
1722 lVol = *left;
1723 rVol = *right;
1724 }
1725 mEffects[i]->setVolume(&lVol, &rVol, false);
1726 }
1727 *left = newLeft;
1728 *right = newRight;
1729
1730 return hasControl;
1731}
1732
Eric Laurentfa1e1232016-08-02 19:01:49 -07001733// resetVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1734void AudioFlinger::EffectChain::resetVolume_l()
1735{
Eric Laurente7449bf2016-08-03 18:44:07 -07001736 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1737 uint32_t left = mLeftVolume;
1738 uint32_t right = mRightVolume;
1739 (void)setVolume_l(&left, &right, true);
1740 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001741}
1742
Eric Laurent1b928682014-10-02 19:41:47 -07001743void AudioFlinger::EffectChain::syncHalEffectsState()
1744{
1745 Mutex::Autolock _l(mLock);
1746 for (size_t i = 0; i < mEffects.size(); i++) {
1747 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1748 mEffects[i]->state() == EffectModule::STOPPING) {
1749 mEffects[i]->addEffectToHal_l();
1750 }
1751 }
1752}
1753
Eric Laurentca7cc822012-11-19 14:55:58 -08001754void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1755{
1756 const size_t SIZE = 256;
1757 char buffer[SIZE];
1758 String8 result;
1759
Marco Nelissenb2208842014-02-07 14:00:50 -08001760 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001761 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001762 result.append(buffer);
1763
Marco Nelissenb2208842014-02-07 14:00:50 -08001764 if (numEffects) {
1765 bool locked = AudioFlinger::dumpTryLock(mLock);
1766 // failed to lock - AudioFlinger is probably deadlocked
1767 if (!locked) {
1768 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001769 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001770
Marco Nelissenb2208842014-02-07 14:00:50 -08001771 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001772 snprintf(buffer, SIZE, "\t%p %p %d\n",
1773 mInBuffer,
1774 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001775 mActiveTrackCnt);
1776 result.append(buffer);
1777 write(fd, result.string(), result.size());
1778
1779 for (size_t i = 0; i < numEffects; ++i) {
1780 sp<EffectModule> effect = mEffects[i];
1781 if (effect != 0) {
1782 effect->dump(fd, args);
1783 }
1784 }
1785
1786 if (locked) {
1787 mLock.unlock();
1788 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001789 }
1790}
1791
1792// must be called with ThreadBase::mLock held
1793void AudioFlinger::EffectChain::setEffectSuspended_l(
1794 const effect_uuid_t *type, bool suspend)
1795{
1796 sp<SuspendedEffectDesc> desc;
1797 // use effect type UUID timelow as key as there is no real risk of identical
1798 // timeLow fields among effect type UUIDs.
1799 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1800 if (suspend) {
1801 if (index >= 0) {
1802 desc = mSuspendedEffects.valueAt(index);
1803 } else {
1804 desc = new SuspendedEffectDesc();
1805 desc->mType = *type;
1806 mSuspendedEffects.add(type->timeLow, desc);
1807 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1808 }
1809 if (desc->mRefCount++ == 0) {
1810 sp<EffectModule> effect = getEffectIfEnabled(type);
1811 if (effect != 0) {
1812 desc->mEffect = effect;
1813 effect->setSuspended(true);
1814 effect->setEnabled(false);
1815 }
1816 }
1817 } else {
1818 if (index < 0) {
1819 return;
1820 }
1821 desc = mSuspendedEffects.valueAt(index);
1822 if (desc->mRefCount <= 0) {
1823 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1824 desc->mRefCount = 1;
1825 }
1826 if (--desc->mRefCount == 0) {
1827 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1828 if (desc->mEffect != 0) {
1829 sp<EffectModule> effect = desc->mEffect.promote();
1830 if (effect != 0) {
1831 effect->setSuspended(false);
1832 effect->lock();
1833 EffectHandle *handle = effect->controlHandle_l();
1834 if (handle != NULL && !handle->destroyed_l()) {
1835 effect->setEnabled_l(handle->enabled());
1836 }
1837 effect->unlock();
1838 }
1839 desc->mEffect.clear();
1840 }
1841 mSuspendedEffects.removeItemsAt(index);
1842 }
1843 }
1844}
1845
1846// must be called with ThreadBase::mLock held
1847void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1848{
1849 sp<SuspendedEffectDesc> desc;
1850
1851 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1852 if (suspend) {
1853 if (index >= 0) {
1854 desc = mSuspendedEffects.valueAt(index);
1855 } else {
1856 desc = new SuspendedEffectDesc();
1857 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1858 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1859 }
1860 if (desc->mRefCount++ == 0) {
1861 Vector< sp<EffectModule> > effects;
1862 getSuspendEligibleEffects(effects);
1863 for (size_t i = 0; i < effects.size(); i++) {
1864 setEffectSuspended_l(&effects[i]->desc().type, true);
1865 }
1866 }
1867 } else {
1868 if (index < 0) {
1869 return;
1870 }
1871 desc = mSuspendedEffects.valueAt(index);
1872 if (desc->mRefCount <= 0) {
1873 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1874 desc->mRefCount = 1;
1875 }
1876 if (--desc->mRefCount == 0) {
1877 Vector<const effect_uuid_t *> types;
1878 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1879 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1880 continue;
1881 }
1882 types.add(&mSuspendedEffects.valueAt(i)->mType);
1883 }
1884 for (size_t i = 0; i < types.size(); i++) {
1885 setEffectSuspended_l(types[i], false);
1886 }
1887 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1888 mSuspendedEffects.keyAt(index));
1889 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1890 }
1891 }
1892}
1893
1894
1895// The volume effect is used for automated tests only
1896#ifndef OPENSL_ES_H_
1897static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1898 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1899const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1900#endif //OPENSL_ES_H_
1901
1902bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1903{
1904 // auxiliary effects and visualizer are never suspended on output mix
1905 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1906 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1907 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1908 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1909 return false;
1910 }
1911 return true;
1912}
1913
1914void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1915 Vector< sp<AudioFlinger::EffectModule> > &effects)
1916{
1917 effects.clear();
1918 for (size_t i = 0; i < mEffects.size(); i++) {
1919 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1920 effects.add(mEffects[i]);
1921 }
1922 }
1923}
1924
1925sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1926 const effect_uuid_t *type)
1927{
1928 sp<EffectModule> effect = getEffectFromType_l(type);
1929 return effect != 0 && effect->isEnabled() ? effect : 0;
1930}
1931
1932void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1933 bool enabled)
1934{
1935 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1936 if (enabled) {
1937 if (index < 0) {
1938 // if the effect is not suspend check if all effects are suspended
1939 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1940 if (index < 0) {
1941 return;
1942 }
1943 if (!isEffectEligibleForSuspend(effect->desc())) {
1944 return;
1945 }
1946 setEffectSuspended_l(&effect->desc().type, enabled);
1947 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1948 if (index < 0) {
1949 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1950 return;
1951 }
1952 }
1953 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1954 effect->desc().type.timeLow);
1955 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1956 // if effect is requested to suspended but was not yet enabled, supend it now.
1957 if (desc->mEffect == 0) {
1958 desc->mEffect = effect;
1959 effect->setEnabled(false);
1960 effect->setSuspended(true);
1961 }
1962 } else {
1963 if (index < 0) {
1964 return;
1965 }
1966 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1967 effect->desc().type.timeLow);
1968 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1969 desc->mEffect.clear();
1970 effect->setSuspended(false);
1971 }
1972}
1973
Eric Laurent5baf2af2013-09-12 17:37:00 -07001974bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001975{
1976 Mutex::Autolock _l(mLock);
1977 size_t size = mEffects.size();
1978 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001979 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001980 return true;
1981 }
1982 }
1983 return false;
1984}
1985
Eric Laurentaaa44472014-09-12 17:41:50 -07001986void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
1987{
1988 Mutex::Autolock _l(mLock);
1989 mThread = thread;
1990 for (size_t i = 0; i < mEffects.size(); i++) {
1991 mEffects[i]->setThread(thread);
1992 }
1993}
1994
Eric Laurent4c415062016-06-17 16:14:16 -07001995bool AudioFlinger::EffectChain::hasSoftwareEffect() const
1996{
1997 Mutex::Autolock _l(mLock);
1998 for (size_t i = 0; i < mEffects.size(); i++) {
1999 if (mEffects[i]->isImplementationSoftware()) {
2000 return true;
2001 }
2002 }
2003 return false;
2004}
2005
2006// isCompatibleWithThread_l() must be called with thread->mLock held
2007bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2008{
2009 Mutex::Autolock _l(mLock);
2010 for (size_t i = 0; i < mEffects.size(); i++) {
2011 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2012 return false;
2013 }
2014 }
2015 return true;
2016}
2017
Glenn Kasten63238ef2015-03-02 15:50:29 -08002018} // namespace android