blob: 9711f2decf60946cbe9f6cefaf063f8bfe5d7728 [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 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700282 int ret;
283 if (isProcessImplemented()) {
284 // do the actual processing in the effect engine
285 ret = (*mEffectInterface)->process(mEffectInterface,
286 &mConfig.inputCfg.buffer,
287 &mConfig.outputCfg.buffer);
288 } else {
289 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
290 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
291 int16_t *in = mConfig.inputCfg.buffer.s16;
292 int16_t *out = mConfig.outputCfg.buffer.s16;
Eric Laurentca7cc822012-11-19 14:55:58 -0800293
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700294 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
295 for (size_t i = 0; i < frameCnt; i++) {
296 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
297 }
298 } else {
299 memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
300 frameCnt * sizeof(int16_t));
301 }
302 }
303 ret = -ENODATA;
304 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800305 // force transition to IDLE state when engine is ready
306 if (mState == STOPPED && ret == -ENODATA) {
307 mDisableWaitCnt = 1;
308 }
309
310 // clear auxiliary effect input buffer for next accumulation
311 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
312 memset(mConfig.inputCfg.buffer.raw, 0,
313 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
314 }
315 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
316 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
317 // If an insert effect is idle and input buffer is different from output buffer,
318 // accumulate input onto output
319 sp<EffectChain> chain = mChain.promote();
320 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700321 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
Eric Laurentca7cc822012-11-19 14:55:58 -0800322 int16_t *in = mConfig.inputCfg.buffer.s16;
323 int16_t *out = mConfig.outputCfg.buffer.s16;
324 for (size_t i = 0; i < frameCnt; i++) {
325 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
326 }
327 }
328 }
329}
330
331void AudioFlinger::EffectModule::reset_l()
332{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700333 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800334 return;
335 }
336 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
337}
338
339status_t AudioFlinger::EffectModule::configure()
340{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700341 status_t status;
342 sp<ThreadBase> thread;
343 uint32_t size;
344 audio_channel_mask_t channelMask;
345
Eric Laurentca7cc822012-11-19 14:55:58 -0800346 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700347 status = NO_INIT;
348 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800349 }
350
Eric Laurentd0ebb532013-04-02 16:41:41 -0700351 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800352 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700353 status = DEAD_OBJECT;
354 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800355 }
356
357 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700358 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700359 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800360
361 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
362 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
363 } else {
364 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700365 // TODO: Update this logic when multichannel effects are implemented.
366 // For offloaded tracks consider mono output as stereo for proper effect initialization
367 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
368 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
369 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
370 ALOGV("Overriding effect input and output as STEREO");
371 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800372 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700373
Eric Laurentca7cc822012-11-19 14:55:58 -0800374 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
375 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
376 mConfig.inputCfg.samplingRate = thread->sampleRate();
377 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
378 mConfig.inputCfg.bufferProvider.cookie = NULL;
379 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
380 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
381 mConfig.outputCfg.bufferProvider.cookie = NULL;
382 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
383 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
384 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
385 // Insert effect:
386 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
387 // always overwrites output buffer: input buffer == output buffer
388 // - in other sessions:
389 // last effect in the chain accumulates in output buffer: input buffer != output buffer
390 // other effect: overwrites output buffer: input buffer == output buffer
391 // Auxiliary effect:
392 // accumulates in output buffer: input buffer != output buffer
393 // Therefore: accumulate <=> input buffer != output buffer
394 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
395 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
396 } else {
397 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
398 }
399 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
400 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
401 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
402 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
403
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700404 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800405 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
406
407 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700408 size = sizeof(int);
409 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800410 EFFECT_CMD_SET_CONFIG,
411 sizeof(effect_config_t),
412 &mConfig,
413 &size,
414 &cmdStatus);
415 if (status == 0) {
416 status = cmdStatus;
417 }
418
419 if (status == 0 &&
420 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
421 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
422 effect_param_t *p = (effect_param_t *)buf32;
423
424 p->psize = sizeof(uint32_t);
425 p->vsize = sizeof(uint32_t);
426 size = sizeof(int);
427 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
428
429 uint32_t latency = 0;
430 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
431 if (pbt != NULL) {
432 latency = pbt->latency_l();
433 }
434
435 *((int32_t *)p->data + 1)= latency;
436 (*mEffectInterface)->command(mEffectInterface,
437 EFFECT_CMD_SET_PARAM,
438 sizeof(effect_param_t) + 8,
439 &buf32,
440 &size,
441 &cmdStatus);
442 }
443
444 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
445 (1000 * mConfig.outputCfg.buffer.frameCount);
446
Eric Laurentd0ebb532013-04-02 16:41:41 -0700447exit:
448 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800449 return status;
450}
451
452status_t AudioFlinger::EffectModule::init()
453{
454 Mutex::Autolock _l(mLock);
455 if (mEffectInterface == NULL) {
456 return NO_INIT;
457 }
458 status_t cmdStatus;
459 uint32_t size = sizeof(status_t);
460 status_t status = (*mEffectInterface)->command(mEffectInterface,
461 EFFECT_CMD_INIT,
462 0,
463 NULL,
464 &size,
465 &cmdStatus);
466 if (status == 0) {
467 status = cmdStatus;
468 }
469 return status;
470}
471
Eric Laurent1b928682014-10-02 19:41:47 -0700472void AudioFlinger::EffectModule::addEffectToHal_l()
473{
474 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
475 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
476 sp<ThreadBase> thread = mThread.promote();
477 if (thread != 0) {
478 audio_stream_t *stream = thread->stream();
479 if (stream != NULL) {
480 stream->add_audio_effect(stream, mEffectInterface);
481 }
482 }
483 }
484}
485
Eric Laurentfa1e1232016-08-02 19:01:49 -0700486// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800487status_t AudioFlinger::EffectModule::start()
488{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700489 sp<EffectChain> chain;
490 status_t status;
491 {
492 Mutex::Autolock _l(mLock);
493 status = start_l();
494 if (status == NO_ERROR) {
495 chain = mChain.promote();
496 }
497 }
498 if (chain != 0) {
499 chain->resetVolume_l();
500 }
501 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800502}
503
504status_t AudioFlinger::EffectModule::start_l()
505{
506 if (mEffectInterface == NULL) {
507 return NO_INIT;
508 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700509 if (mStatus != NO_ERROR) {
510 return mStatus;
511 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800512 status_t cmdStatus;
513 uint32_t size = sizeof(status_t);
514 status_t status = (*mEffectInterface)->command(mEffectInterface,
515 EFFECT_CMD_ENABLE,
516 0,
517 NULL,
518 &size,
519 &cmdStatus);
520 if (status == 0) {
521 status = cmdStatus;
522 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700523 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700524 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800525 }
526 return status;
527}
528
529status_t AudioFlinger::EffectModule::stop()
530{
531 Mutex::Autolock _l(mLock);
532 return stop_l();
533}
534
535status_t AudioFlinger::EffectModule::stop_l()
536{
537 if (mEffectInterface == NULL) {
538 return NO_INIT;
539 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700540 if (mStatus != NO_ERROR) {
541 return mStatus;
542 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800543 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800544 uint32_t size = sizeof(status_t);
545 status_t status = (*mEffectInterface)->command(mEffectInterface,
546 EFFECT_CMD_DISABLE,
547 0,
548 NULL,
549 &size,
550 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800551 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800552 status = cmdStatus;
553 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800554 if (status == NO_ERROR) {
555 status = remove_effect_from_hal_l();
556 }
557 return status;
558}
559
560status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
561{
562 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
563 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800564 sp<ThreadBase> thread = mThread.promote();
565 if (thread != 0) {
566 audio_stream_t *stream = thread->stream();
567 if (stream != NULL) {
568 stream->remove_audio_effect(stream, mEffectInterface);
569 }
570 }
571 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800572 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800573}
574
Andy Hunge4a1d912016-08-17 14:11:13 -0700575// round up delta valid if value and divisor are positive.
576template <typename T>
577static T roundUpDelta(const T &value, const T &divisor) {
578 T remainder = value % divisor;
579 return remainder == 0 ? 0 : divisor - remainder;
580}
581
Eric Laurentca7cc822012-11-19 14:55:58 -0800582status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
583 uint32_t cmdSize,
584 void *pCmdData,
585 uint32_t *replySize,
586 void *pReplyData)
587{
588 Mutex::Autolock _l(mLock);
589 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
590
591 if (mState == DESTROYED || mEffectInterface == NULL) {
592 return NO_INIT;
593 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700594 if (mStatus != NO_ERROR) {
595 return mStatus;
596 }
Andy Hung110bc952016-06-20 15:22:52 -0700597 if (cmdCode == EFFECT_CMD_GET_PARAM &&
598 (*replySize < sizeof(effect_param_t) ||
599 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
600 android_errorWriteLog(0x534e4554, "29251553");
601 return -EINVAL;
602 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700603 if ((cmdCode == EFFECT_CMD_SET_PARAM
604 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
605 (sizeof(effect_param_t) > cmdSize
606 || ((effect_param_t *)pCmdData)->psize > cmdSize
607 - sizeof(effect_param_t)
608 || ((effect_param_t *)pCmdData)->vsize > cmdSize
609 - sizeof(effect_param_t)
610 - ((effect_param_t *)pCmdData)->psize
611 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
612 cmdSize
613 - sizeof(effect_param_t)
614 - ((effect_param_t *)pCmdData)->psize
615 - ((effect_param_t *)pCmdData)->vsize)) {
616 android_errorWriteLog(0x534e4554, "30204301");
617 return -EINVAL;
618 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800619 status_t status = (*mEffectInterface)->command(mEffectInterface,
620 cmdCode,
621 cmdSize,
622 pCmdData,
623 replySize,
624 pReplyData);
625 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
626 uint32_t size = (replySize == NULL) ? 0 : *replySize;
627 for (size_t i = 1; i < mHandles.size(); i++) {
628 EffectHandle *h = mHandles[i];
629 if (h != NULL && !h->destroyed_l()) {
630 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
631 }
632 }
633 }
634 return status;
635}
636
637status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
638{
639 Mutex::Autolock _l(mLock);
640 return setEnabled_l(enabled);
641}
642
643// must be called with EffectModule::mLock held
644status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
645{
646
647 ALOGV("setEnabled %p enabled %d", this, enabled);
648
649 if (enabled != isEnabled()) {
650 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
651 if (enabled && status != NO_ERROR) {
652 return status;
653 }
654
655 switch (mState) {
656 // going from disabled to enabled
657 case IDLE:
658 mState = STARTING;
659 break;
660 case STOPPED:
661 mState = RESTART;
662 break;
663 case STOPPING:
664 mState = ACTIVE;
665 break;
666
667 // going from enabled to disabled
668 case RESTART:
669 mState = STOPPED;
670 break;
671 case STARTING:
672 mState = IDLE;
673 break;
674 case ACTIVE:
675 mState = STOPPING;
676 break;
677 case DESTROYED:
678 return NO_ERROR; // simply ignore as we are being destroyed
679 }
680 for (size_t i = 1; i < mHandles.size(); i++) {
681 EffectHandle *h = mHandles[i];
682 if (h != NULL && !h->destroyed_l()) {
683 h->setEnabled(enabled);
684 }
685 }
686 }
687 return NO_ERROR;
688}
689
690bool AudioFlinger::EffectModule::isEnabled() const
691{
692 switch (mState) {
693 case RESTART:
694 case STARTING:
695 case ACTIVE:
696 return true;
697 case IDLE:
698 case STOPPING:
699 case STOPPED:
700 case DESTROYED:
701 default:
702 return false;
703 }
704}
705
706bool AudioFlinger::EffectModule::isProcessEnabled() const
707{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700708 if (mStatus != NO_ERROR) {
709 return false;
710 }
711
Eric Laurentca7cc822012-11-19 14:55:58 -0800712 switch (mState) {
713 case RESTART:
714 case ACTIVE:
715 case STOPPING:
716 case STOPPED:
717 return true;
718 case IDLE:
719 case STARTING:
720 case DESTROYED:
721 default:
722 return false;
723 }
724}
725
726status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
727{
728 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700729 if (mStatus != NO_ERROR) {
730 return mStatus;
731 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800732 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800733 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
734 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
735 if (isProcessEnabled() &&
736 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
737 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800738 uint32_t volume[2];
739 uint32_t *pVolume = NULL;
740 uint32_t size = sizeof(volume);
741 volume[0] = *left;
742 volume[1] = *right;
743 if (controller) {
744 pVolume = volume;
745 }
746 status = (*mEffectInterface)->command(mEffectInterface,
747 EFFECT_CMD_SET_VOLUME,
748 size,
749 volume,
750 &size,
751 pVolume);
752 if (controller && status == NO_ERROR && size == sizeof(volume)) {
753 *left = volume[0];
754 *right = volume[1];
755 }
756 }
757 return status;
758}
759
760status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
761{
762 if (device == AUDIO_DEVICE_NONE) {
763 return NO_ERROR;
764 }
765
766 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700767 if (mStatus != NO_ERROR) {
768 return mStatus;
769 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800770 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700771 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800772 status_t cmdStatus;
773 uint32_t size = sizeof(status_t);
774 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
775 EFFECT_CMD_SET_INPUT_DEVICE;
776 status = (*mEffectInterface)->command(mEffectInterface,
777 cmd,
778 sizeof(uint32_t),
779 &device,
780 &size,
781 &cmdStatus);
782 }
783 return status;
784}
785
786status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
787{
788 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700789 if (mStatus != NO_ERROR) {
790 return mStatus;
791 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800792 status_t status = NO_ERROR;
793 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
794 status_t cmdStatus;
795 uint32_t size = sizeof(status_t);
796 status = (*mEffectInterface)->command(mEffectInterface,
797 EFFECT_CMD_SET_AUDIO_MODE,
798 sizeof(audio_mode_t),
799 &mode,
800 &size,
801 &cmdStatus);
802 if (status == NO_ERROR) {
803 status = cmdStatus;
804 }
805 }
806 return status;
807}
808
809status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
810{
811 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700812 if (mStatus != NO_ERROR) {
813 return mStatus;
814 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800815 status_t status = NO_ERROR;
816 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
817 uint32_t size = 0;
818 status = (*mEffectInterface)->command(mEffectInterface,
819 EFFECT_CMD_SET_AUDIO_SOURCE,
820 sizeof(audio_source_t),
821 &source,
822 &size,
823 NULL);
824 }
825 return status;
826}
827
828void AudioFlinger::EffectModule::setSuspended(bool suspended)
829{
830 Mutex::Autolock _l(mLock);
831 mSuspended = suspended;
832}
833
834bool AudioFlinger::EffectModule::suspended() const
835{
836 Mutex::Autolock _l(mLock);
837 return mSuspended;
838}
839
840bool AudioFlinger::EffectModule::purgeHandles()
841{
842 bool enabled = false;
843 Mutex::Autolock _l(mLock);
844 for (size_t i = 0; i < mHandles.size(); i++) {
845 EffectHandle *handle = mHandles[i];
846 if (handle != NULL && !handle->destroyed_l()) {
847 handle->effect().clear();
848 if (handle->hasControl()) {
849 enabled = handle->enabled();
850 }
851 }
852 }
853 return enabled;
854}
855
Eric Laurent5baf2af2013-09-12 17:37:00 -0700856status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
857{
858 Mutex::Autolock _l(mLock);
859 if (mStatus != NO_ERROR) {
860 return mStatus;
861 }
862 status_t status = NO_ERROR;
863 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
864 status_t cmdStatus;
865 uint32_t size = sizeof(status_t);
866 effect_offload_param_t cmd;
867
868 cmd.isOffload = offloaded;
869 cmd.ioHandle = io;
870 status = (*mEffectInterface)->command(mEffectInterface,
871 EFFECT_CMD_OFFLOAD,
872 sizeof(effect_offload_param_t),
873 &cmd,
874 &size,
875 &cmdStatus);
876 if (status == NO_ERROR) {
877 status = cmdStatus;
878 }
879 mOffloaded = (status == NO_ERROR) ? offloaded : false;
880 } else {
881 if (offloaded) {
882 status = INVALID_OPERATION;
883 }
884 mOffloaded = false;
885 }
886 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
887 return status;
888}
889
890bool AudioFlinger::EffectModule::isOffloaded() const
891{
892 Mutex::Autolock _l(mLock);
893 return mOffloaded;
894}
895
Marco Nelissenb2208842014-02-07 14:00:50 -0800896String8 effectFlagsToString(uint32_t flags) {
897 String8 s;
898
899 s.append("conn. mode: ");
900 switch (flags & EFFECT_FLAG_TYPE_MASK) {
901 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
902 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
903 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
904 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
905 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
906 default: s.append("unknown/reserved"); break;
907 }
908 s.append(", ");
909
910 s.append("insert pref: ");
911 switch (flags & EFFECT_FLAG_INSERT_MASK) {
912 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
913 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
914 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
915 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
916 default: s.append("unknown/reserved"); break;
917 }
918 s.append(", ");
919
920 s.append("volume mgmt: ");
921 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
922 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
923 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
924 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
925 default: s.append("unknown/reserved"); break;
926 }
927 s.append(", ");
928
929 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
930 if (devind) {
931 s.append("device indication: ");
932 switch (devind) {
933 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
934 default: s.append("unknown/reserved"); break;
935 }
936 s.append(", ");
937 }
938
939 s.append("input mode: ");
940 switch (flags & EFFECT_FLAG_INPUT_MASK) {
941 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
942 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
943 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
944 default: s.append("not set"); break;
945 }
946 s.append(", ");
947
948 s.append("output mode: ");
949 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
950 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
951 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
952 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
953 default: s.append("not set"); break;
954 }
955 s.append(", ");
956
957 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
958 if (accel) {
959 s.append("hardware acceleration: ");
960 switch (accel) {
961 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
962 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
963 default: s.append("unknown/reserved"); break;
964 }
965 s.append(", ");
966 }
967
968 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
969 if (modeind) {
970 s.append("mode indication: ");
971 switch (modeind) {
972 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
973 default: s.append("unknown/reserved"); break;
974 }
975 s.append(", ");
976 }
977
978 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
979 if (srcind) {
980 s.append("source indication: ");
981 switch (srcind) {
982 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
983 default: s.append("unknown/reserved"); break;
984 }
985 s.append(", ");
986 }
987
988 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
989 s.append("offloadable, ");
990 }
991
992 int len = s.length();
993 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -0700994 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -0800995 s.unlockBuffer(len - 2);
996 }
997 return s;
998}
999
1000
Glenn Kasten0f11b512014-01-31 16:18:54 -08001001void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001002{
1003 const size_t SIZE = 256;
1004 char buffer[SIZE];
1005 String8 result;
1006
1007 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1008 result.append(buffer);
1009
1010 bool locked = AudioFlinger::dumpTryLock(mLock);
1011 // failed to lock - AudioFlinger is probably deadlocked
1012 if (!locked) {
1013 result.append("\t\tCould not lock Fx mutex:\n");
1014 }
1015
1016 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001017 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
1018 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001019 result.append(buffer);
1020
1021 result.append("\t\tDescriptor:\n");
1022 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1023 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1024 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1025 mDescriptor.uuid.node[2],
1026 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1027 result.append(buffer);
1028 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1029 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1030 mDescriptor.type.timeHiAndVersion,
1031 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1032 mDescriptor.type.node[2],
1033 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1034 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001035 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001037 mDescriptor.flags,
1038 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001039 result.append(buffer);
1040 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1041 mDescriptor.name);
1042 result.append(buffer);
1043 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1044 mDescriptor.implementor);
1045 result.append(buffer);
1046
1047 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001048 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001049 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001050 mConfig.inputCfg.buffer.frameCount,
1051 mConfig.inputCfg.samplingRate,
1052 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001053 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001054 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001055 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001056 result.append(buffer);
1057
1058 result.append("\t\t- Output configuration:\n");
1059 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001060 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001061 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001062 mConfig.outputCfg.buffer.frameCount,
1063 mConfig.outputCfg.samplingRate,
1064 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001065 mConfig.outputCfg.format,
1066 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001067 result.append(buffer);
1068
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001069 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001070 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001071 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001072 for (size_t i = 0; i < mHandles.size(); ++i) {
1073 EffectHandle *handle = mHandles[i];
1074 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001075 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 result.append(buffer);
1077 }
1078 }
1079
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 write(fd, result.string(), result.length());
1081
1082 if (locked) {
1083 mLock.unlock();
1084 }
1085}
1086
1087// ----------------------------------------------------------------------------
1088// EffectHandle implementation
1089// ----------------------------------------------------------------------------
1090
1091#undef LOG_TAG
1092#define LOG_TAG "AudioFlinger::EffectHandle"
1093
1094AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1095 const sp<AudioFlinger::Client>& client,
1096 const sp<IEffectClient>& effectClient,
1097 int32_t priority)
1098 : BnEffect(),
1099 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1100 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1101{
1102 ALOGV("constructor %p", this);
1103
1104 if (client == 0) {
1105 return;
1106 }
1107 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1108 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001109 if (mCblkMemory == 0 ||
1110 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001111 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001112 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001113 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001114 return;
1115 }
Glenn Kastene75da402013-11-20 13:54:52 -08001116 new(mCblk) effect_param_cblk_t();
1117 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001118}
1119
1120AudioFlinger::EffectHandle::~EffectHandle()
1121{
1122 ALOGV("Destructor %p", this);
1123
1124 if (mEffect == 0) {
1125 mDestroyed = true;
1126 return;
1127 }
1128 mEffect->lock();
1129 mDestroyed = true;
1130 mEffect->unlock();
1131 disconnect(false);
1132}
1133
Glenn Kastene75da402013-11-20 13:54:52 -08001134status_t AudioFlinger::EffectHandle::initCheck()
1135{
1136 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1137}
1138
Eric Laurentca7cc822012-11-19 14:55:58 -08001139status_t AudioFlinger::EffectHandle::enable()
1140{
1141 ALOGV("enable %p", this);
1142 if (!mHasControl) {
1143 return INVALID_OPERATION;
1144 }
1145 if (mEffect == 0) {
1146 return DEAD_OBJECT;
1147 }
1148
1149 if (mEnabled) {
1150 return NO_ERROR;
1151 }
1152
1153 mEnabled = true;
1154
1155 sp<ThreadBase> thread = mEffect->thread().promote();
1156 if (thread != 0) {
1157 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1158 }
1159
1160 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1161 if (mEffect->suspended()) {
1162 return NO_ERROR;
1163 }
1164
1165 status_t status = mEffect->setEnabled(true);
1166 if (status != NO_ERROR) {
1167 if (thread != 0) {
1168 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1169 }
1170 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001171 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001172 if (thread != 0) {
1173 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001174 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001175 Mutex::Autolock _l(t->mLock);
1176 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001177 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001178 if (!mEffect->isOffloadable()) {
1179 if (thread->type() == ThreadBase::OFFLOAD) {
1180 PlaybackThread *t = (PlaybackThread *)thread.get();
1181 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1182 }
1183 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1184 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1185 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001186 }
1187 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001188 }
1189 return status;
1190}
1191
1192status_t AudioFlinger::EffectHandle::disable()
1193{
1194 ALOGV("disable %p", this);
1195 if (!mHasControl) {
1196 return INVALID_OPERATION;
1197 }
1198 if (mEffect == 0) {
1199 return DEAD_OBJECT;
1200 }
1201
1202 if (!mEnabled) {
1203 return NO_ERROR;
1204 }
1205 mEnabled = false;
1206
1207 if (mEffect->suspended()) {
1208 return NO_ERROR;
1209 }
1210
1211 status_t status = mEffect->setEnabled(false);
1212
1213 sp<ThreadBase> thread = mEffect->thread().promote();
1214 if (thread != 0) {
1215 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001216 if (thread->type() == ThreadBase::OFFLOAD) {
1217 PlaybackThread *t = (PlaybackThread *)thread.get();
1218 Mutex::Autolock _l(t->mLock);
1219 t->broadcast_l();
1220 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001221 }
1222
1223 return status;
1224}
1225
1226void AudioFlinger::EffectHandle::disconnect()
1227{
1228 disconnect(true);
1229}
1230
1231void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1232{
1233 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1234 if (mEffect == 0) {
1235 return;
1236 }
1237 // restore suspended effects if the disconnected handle was enabled and the last one.
1238 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1239 sp<ThreadBase> thread = mEffect->thread().promote();
1240 if (thread != 0) {
1241 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1242 }
1243 }
1244
1245 // release sp on module => module destructor can be called now
1246 mEffect.clear();
1247 if (mClient != 0) {
1248 if (mCblk != NULL) {
1249 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1250 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1251 }
1252 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001253 // Client destructor must run with AudioFlinger client mutex locked
1254 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001255 mClient.clear();
1256 }
1257}
1258
1259status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1260 uint32_t cmdSize,
1261 void *pCmdData,
1262 uint32_t *replySize,
1263 void *pReplyData)
1264{
1265 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1266 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1267
1268 // only get parameter command is permitted for applications not controlling the effect
1269 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1270 return INVALID_OPERATION;
1271 }
1272 if (mEffect == 0) {
1273 return DEAD_OBJECT;
1274 }
1275 if (mClient == 0) {
1276 return INVALID_OPERATION;
1277 }
1278
1279 // handle commands that are not forwarded transparently to effect engine
1280 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1281 // No need to trylock() here as this function is executed in the binder thread serving a
1282 // particular client process: no risk to block the whole media server process or mixer
1283 // threads if we are stuck here
1284 Mutex::Autolock _l(mCblk->lock);
1285 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1286 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1287 mCblk->serverIndex = 0;
1288 mCblk->clientIndex = 0;
1289 return BAD_VALUE;
1290 }
1291 status_t status = NO_ERROR;
1292 while (mCblk->serverIndex < mCblk->clientIndex) {
1293 int reply;
1294 uint32_t rsize = sizeof(int);
1295 int *p = (int *)(mBuffer + mCblk->serverIndex);
1296 int size = *p++;
1297 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1298 ALOGW("command(): invalid parameter block size");
1299 break;
1300 }
1301 effect_param_t *param = (effect_param_t *)p;
1302 if (param->psize == 0 || param->vsize == 0) {
1303 ALOGW("command(): null parameter or value size");
1304 mCblk->serverIndex += size;
1305 continue;
1306 }
1307 uint32_t psize = sizeof(effect_param_t) +
1308 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1309 param->vsize;
1310 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1311 psize,
1312 p,
1313 &rsize,
1314 &reply);
1315 // stop at first error encountered
1316 if (ret != NO_ERROR) {
1317 status = ret;
1318 *(int *)pReplyData = reply;
1319 break;
1320 } else if (reply != NO_ERROR) {
1321 *(int *)pReplyData = reply;
1322 break;
1323 }
1324 mCblk->serverIndex += size;
1325 }
1326 mCblk->serverIndex = 0;
1327 mCblk->clientIndex = 0;
1328 return status;
1329 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1330 *(int *)pReplyData = NO_ERROR;
1331 return enable();
1332 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1333 *(int *)pReplyData = NO_ERROR;
1334 return disable();
1335 }
1336
1337 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1338}
1339
1340void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1341{
1342 ALOGV("setControl %p control %d", this, hasControl);
1343
1344 mHasControl = hasControl;
1345 mEnabled = enabled;
1346
1347 if (signal && mEffectClient != 0) {
1348 mEffectClient->controlStatusChanged(hasControl);
1349 }
1350}
1351
1352void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1353 uint32_t cmdSize,
1354 void *pCmdData,
1355 uint32_t replySize,
1356 void *pReplyData)
1357{
1358 if (mEffectClient != 0) {
1359 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1360 }
1361}
1362
1363
1364
1365void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1366{
1367 if (mEffectClient != 0) {
1368 mEffectClient->enableStatusChanged(enabled);
1369 }
1370}
1371
1372status_t AudioFlinger::EffectHandle::onTransact(
1373 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1374{
1375 return BnEffect::onTransact(code, data, reply, flags);
1376}
1377
1378
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001379void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001380{
1381 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1382
Marco Nelissenb2208842014-02-07 14:00:50 -08001383 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001384 (mClient == 0) ? getpid_cached : mClient->pid(),
1385 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001386 mHasControl ? "yes" : "no",
1387 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001388 mCblk ? mCblk->clientIndex : 0,
1389 mCblk ? mCblk->serverIndex : 0
1390 );
1391
1392 if (locked) {
1393 mCblk->lock.unlock();
1394 }
1395}
1396
1397#undef LOG_TAG
1398#define LOG_TAG "AudioFlinger::EffectChain"
1399
1400AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001401 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1403 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001404 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001405{
1406 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1407 if (thread == NULL) {
1408 return;
1409 }
1410 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1411 thread->frameCount();
1412}
1413
1414AudioFlinger::EffectChain::~EffectChain()
1415{
1416 if (mOwnInBuffer) {
1417 delete mInBuffer;
1418 }
1419
1420}
1421
1422// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1423sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1424 effect_descriptor_t *descriptor)
1425{
1426 size_t size = mEffects.size();
1427
1428 for (size_t i = 0; i < size; i++) {
1429 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1430 return mEffects[i];
1431 }
1432 }
1433 return 0;
1434}
1435
1436// getEffectFromId_l() must be called with ThreadBase::mLock held
1437sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1438{
1439 size_t size = mEffects.size();
1440
1441 for (size_t i = 0; i < size; i++) {
1442 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1443 if (id == 0 || mEffects[i]->id() == id) {
1444 return mEffects[i];
1445 }
1446 }
1447 return 0;
1448}
1449
1450// getEffectFromType_l() must be called with ThreadBase::mLock held
1451sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1452 const effect_uuid_t *type)
1453{
1454 size_t size = mEffects.size();
1455
1456 for (size_t i = 0; i < size; i++) {
1457 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1458 return mEffects[i];
1459 }
1460 }
1461 return 0;
1462}
1463
1464void AudioFlinger::EffectChain::clearInputBuffer()
1465{
1466 Mutex::Autolock _l(mLock);
1467 sp<ThreadBase> thread = mThread.promote();
1468 if (thread == 0) {
1469 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1470 return;
1471 }
1472 clearInputBuffer_l(thread);
1473}
1474
1475// Must be called with EffectChain::mLock locked
1476void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1477{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001478 // TODO: This will change in the future, depending on multichannel
1479 // and sample format changes for effects.
1480 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1481 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001482 const size_t frameSize =
1483 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001484 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001485}
1486
1487// Must be called with EffectChain::mLock locked
1488void AudioFlinger::EffectChain::process_l()
1489{
1490 sp<ThreadBase> thread = mThread.promote();
1491 if (thread == 0) {
1492 ALOGW("process_l(): cannot promote mixer thread");
1493 return;
1494 }
1495 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1496 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001497 // never process effects when:
1498 // - on an OFFLOAD thread
1499 // - no more tracks are on the session and the effect tail has been rendered
1500 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001501 if (!isGlobalSession) {
1502 bool tracksOnSession = (trackCnt() != 0);
1503
1504 if (!tracksOnSession && mTailBufferCount == 0) {
1505 doProcess = false;
1506 }
1507
1508 if (activeTrackCnt() == 0) {
1509 // if no track is active and the effect tail has not been rendered,
1510 // the input buffer must be cleared here as the mixer process will not do it
1511 if (tracksOnSession || mTailBufferCount > 0) {
1512 clearInputBuffer_l(thread);
1513 if (mTailBufferCount > 0) {
1514 mTailBufferCount--;
1515 }
1516 }
1517 }
1518 }
1519
1520 size_t size = mEffects.size();
1521 if (doProcess) {
1522 for (size_t i = 0; i < size; i++) {
1523 mEffects[i]->process();
1524 }
1525 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001526 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001527 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001528 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1529 }
1530 if (doResetVolume) {
1531 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001532 }
1533}
1534
1535// addEffect_l() must be called with PlaybackThread::mLock held
1536status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1537{
1538 effect_descriptor_t desc = effect->desc();
1539 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1540
1541 Mutex::Autolock _l(mLock);
1542 effect->setChain(this);
1543 sp<ThreadBase> thread = mThread.promote();
1544 if (thread == 0) {
1545 return NO_INIT;
1546 }
1547 effect->setThread(thread);
1548
1549 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1550 // Auxiliary effects are inserted at the beginning of mEffects vector as
1551 // they are processed first and accumulated in chain input buffer
1552 mEffects.insertAt(effect, 0);
1553
1554 // the input buffer for auxiliary effect contains mono samples in
1555 // 32 bit format. This is to avoid saturation in AudoMixer
1556 // accumulation stage. Saturation is done in EffectModule::process() before
1557 // calling the process in effect engine
1558 size_t numSamples = thread->frameCount();
1559 int32_t *buffer = new int32_t[numSamples];
1560 memset(buffer, 0, numSamples * sizeof(int32_t));
1561 effect->setInBuffer((int16_t *)buffer);
1562 // auxiliary effects output samples to chain input buffer for further processing
1563 // by insert effects
1564 effect->setOutBuffer(mInBuffer);
1565 } else {
1566 // Insert effects are inserted at the end of mEffects vector as they are processed
1567 // after track and auxiliary effects.
1568 // Insert effect order as a function of indicated preference:
1569 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1570 // another effect is present
1571 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1572 // last effect claiming first position
1573 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1574 // first effect claiming last position
1575 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1576 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1577 // already present
1578
1579 size_t size = mEffects.size();
1580 size_t idx_insert = size;
1581 ssize_t idx_insert_first = -1;
1582 ssize_t idx_insert_last = -1;
1583
1584 for (size_t i = 0; i < size; i++) {
1585 effect_descriptor_t d = mEffects[i]->desc();
1586 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1587 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1588 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1589 // check invalid effect chaining combinations
1590 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1591 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1592 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1593 desc.name, d.name);
1594 return INVALID_OPERATION;
1595 }
1596 // remember position of first insert effect and by default
1597 // select this as insert position for new effect
1598 if (idx_insert == size) {
1599 idx_insert = i;
1600 }
1601 // remember position of last insert effect claiming
1602 // first position
1603 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1604 idx_insert_first = i;
1605 }
1606 // remember position of first insert effect claiming
1607 // last position
1608 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1609 idx_insert_last == -1) {
1610 idx_insert_last = i;
1611 }
1612 }
1613 }
1614
1615 // modify idx_insert from first position if needed
1616 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1617 if (idx_insert_last != -1) {
1618 idx_insert = idx_insert_last;
1619 } else {
1620 idx_insert = size;
1621 }
1622 } else {
1623 if (idx_insert_first != -1) {
1624 idx_insert = idx_insert_first + 1;
1625 }
1626 }
1627
1628 // always read samples from chain input buffer
1629 effect->setInBuffer(mInBuffer);
1630
1631 // if last effect in the chain, output samples to chain
1632 // output buffer, otherwise to chain input buffer
1633 if (idx_insert == size) {
1634 if (idx_insert != 0) {
1635 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1636 mEffects[idx_insert-1]->configure();
1637 }
1638 effect->setOutBuffer(mOutBuffer);
1639 } else {
1640 effect->setOutBuffer(mInBuffer);
1641 }
1642 mEffects.insertAt(effect, idx_insert);
1643
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001644 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001645 idx_insert);
1646 }
1647 effect->configure();
1648 return NO_ERROR;
1649}
1650
1651// removeEffect_l() must be called with PlaybackThread::mLock held
1652size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1653{
1654 Mutex::Autolock _l(mLock);
1655 size_t size = mEffects.size();
1656 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1657
1658 for (size_t i = 0; i < size; i++) {
1659 if (effect == mEffects[i]) {
1660 // calling stop here will remove pre-processing effect from the audio HAL.
1661 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1662 // the middle of a read from audio HAL
1663 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1664 mEffects[i]->state() == EffectModule::STOPPING) {
1665 mEffects[i]->stop();
1666 }
1667 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1668 delete[] effect->inBuffer();
1669 } else {
1670 if (i == size - 1 && i != 0) {
1671 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1672 mEffects[i - 1]->configure();
1673 }
1674 }
1675 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001676 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001677 this, i);
1678 break;
1679 }
1680 }
1681
1682 return mEffects.size();
1683}
1684
1685// setDevice_l() must be called with PlaybackThread::mLock held
1686void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1687{
1688 size_t size = mEffects.size();
1689 for (size_t i = 0; i < size; i++) {
1690 mEffects[i]->setDevice(device);
1691 }
1692}
1693
1694// setMode_l() must be called with PlaybackThread::mLock held
1695void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1696{
1697 size_t size = mEffects.size();
1698 for (size_t i = 0; i < size; i++) {
1699 mEffects[i]->setMode(mode);
1700 }
1701}
1702
1703// setAudioSource_l() must be called with PlaybackThread::mLock held
1704void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1705{
1706 size_t size = mEffects.size();
1707 for (size_t i = 0; i < size; i++) {
1708 mEffects[i]->setAudioSource(source);
1709 }
1710}
1711
Eric Laurentfa1e1232016-08-02 19:01:49 -07001712// setVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1713bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001714{
1715 uint32_t newLeft = *left;
1716 uint32_t newRight = *right;
1717 bool hasControl = false;
1718 int ctrlIdx = -1;
1719 size_t size = mEffects.size();
1720
1721 // first update volume controller
1722 for (size_t i = size; i > 0; i--) {
1723 if (mEffects[i - 1]->isProcessEnabled() &&
1724 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1725 ctrlIdx = i - 1;
1726 hasControl = true;
1727 break;
1728 }
1729 }
1730
Eric Laurentfa1e1232016-08-02 19:01:49 -07001731 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001732 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001733 if (hasControl) {
1734 *left = mNewLeftVolume;
1735 *right = mNewRightVolume;
1736 }
1737 return hasControl;
1738 }
1739
1740 mVolumeCtrlIdx = ctrlIdx;
1741 mLeftVolume = newLeft;
1742 mRightVolume = newRight;
1743
1744 // second get volume update from volume controller
1745 if (ctrlIdx >= 0) {
1746 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1747 mNewLeftVolume = newLeft;
1748 mNewRightVolume = newRight;
1749 }
1750 // then indicate volume to all other effects in chain.
1751 // Pass altered volume to effects before volume controller
1752 // and requested volume to effects after controller
1753 uint32_t lVol = newLeft;
1754 uint32_t rVol = newRight;
1755
1756 for (size_t i = 0; i < size; i++) {
1757 if ((int)i == ctrlIdx) {
1758 continue;
1759 }
1760 // this also works for ctrlIdx == -1 when there is no volume controller
1761 if ((int)i > ctrlIdx) {
1762 lVol = *left;
1763 rVol = *right;
1764 }
1765 mEffects[i]->setVolume(&lVol, &rVol, false);
1766 }
1767 *left = newLeft;
1768 *right = newRight;
1769
1770 return hasControl;
1771}
1772
Eric Laurentfa1e1232016-08-02 19:01:49 -07001773// resetVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1774void AudioFlinger::EffectChain::resetVolume_l()
1775{
Eric Laurente7449bf2016-08-03 18:44:07 -07001776 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1777 uint32_t left = mLeftVolume;
1778 uint32_t right = mRightVolume;
1779 (void)setVolume_l(&left, &right, true);
1780 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001781}
1782
Eric Laurent1b928682014-10-02 19:41:47 -07001783void AudioFlinger::EffectChain::syncHalEffectsState()
1784{
1785 Mutex::Autolock _l(mLock);
1786 for (size_t i = 0; i < mEffects.size(); i++) {
1787 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1788 mEffects[i]->state() == EffectModule::STOPPING) {
1789 mEffects[i]->addEffectToHal_l();
1790 }
1791 }
1792}
1793
Eric Laurentca7cc822012-11-19 14:55:58 -08001794void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1795{
1796 const size_t SIZE = 256;
1797 char buffer[SIZE];
1798 String8 result;
1799
Marco Nelissenb2208842014-02-07 14:00:50 -08001800 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001801 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001802 result.append(buffer);
1803
Marco Nelissenb2208842014-02-07 14:00:50 -08001804 if (numEffects) {
1805 bool locked = AudioFlinger::dumpTryLock(mLock);
1806 // failed to lock - AudioFlinger is probably deadlocked
1807 if (!locked) {
1808 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001809 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001810
Marco Nelissenb2208842014-02-07 14:00:50 -08001811 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001812 snprintf(buffer, SIZE, "\t%p %p %d\n",
1813 mInBuffer,
1814 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001815 mActiveTrackCnt);
1816 result.append(buffer);
1817 write(fd, result.string(), result.size());
1818
1819 for (size_t i = 0; i < numEffects; ++i) {
1820 sp<EffectModule> effect = mEffects[i];
1821 if (effect != 0) {
1822 effect->dump(fd, args);
1823 }
1824 }
1825
1826 if (locked) {
1827 mLock.unlock();
1828 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001829 }
1830}
1831
1832// must be called with ThreadBase::mLock held
1833void AudioFlinger::EffectChain::setEffectSuspended_l(
1834 const effect_uuid_t *type, bool suspend)
1835{
1836 sp<SuspendedEffectDesc> desc;
1837 // use effect type UUID timelow as key as there is no real risk of identical
1838 // timeLow fields among effect type UUIDs.
1839 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1840 if (suspend) {
1841 if (index >= 0) {
1842 desc = mSuspendedEffects.valueAt(index);
1843 } else {
1844 desc = new SuspendedEffectDesc();
1845 desc->mType = *type;
1846 mSuspendedEffects.add(type->timeLow, desc);
1847 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1848 }
1849 if (desc->mRefCount++ == 0) {
1850 sp<EffectModule> effect = getEffectIfEnabled(type);
1851 if (effect != 0) {
1852 desc->mEffect = effect;
1853 effect->setSuspended(true);
1854 effect->setEnabled(false);
1855 }
1856 }
1857 } else {
1858 if (index < 0) {
1859 return;
1860 }
1861 desc = mSuspendedEffects.valueAt(index);
1862 if (desc->mRefCount <= 0) {
1863 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1864 desc->mRefCount = 1;
1865 }
1866 if (--desc->mRefCount == 0) {
1867 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1868 if (desc->mEffect != 0) {
1869 sp<EffectModule> effect = desc->mEffect.promote();
1870 if (effect != 0) {
1871 effect->setSuspended(false);
1872 effect->lock();
1873 EffectHandle *handle = effect->controlHandle_l();
1874 if (handle != NULL && !handle->destroyed_l()) {
1875 effect->setEnabled_l(handle->enabled());
1876 }
1877 effect->unlock();
1878 }
1879 desc->mEffect.clear();
1880 }
1881 mSuspendedEffects.removeItemsAt(index);
1882 }
1883 }
1884}
1885
1886// must be called with ThreadBase::mLock held
1887void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1888{
1889 sp<SuspendedEffectDesc> desc;
1890
1891 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1892 if (suspend) {
1893 if (index >= 0) {
1894 desc = mSuspendedEffects.valueAt(index);
1895 } else {
1896 desc = new SuspendedEffectDesc();
1897 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1898 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1899 }
1900 if (desc->mRefCount++ == 0) {
1901 Vector< sp<EffectModule> > effects;
1902 getSuspendEligibleEffects(effects);
1903 for (size_t i = 0; i < effects.size(); i++) {
1904 setEffectSuspended_l(&effects[i]->desc().type, true);
1905 }
1906 }
1907 } else {
1908 if (index < 0) {
1909 return;
1910 }
1911 desc = mSuspendedEffects.valueAt(index);
1912 if (desc->mRefCount <= 0) {
1913 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1914 desc->mRefCount = 1;
1915 }
1916 if (--desc->mRefCount == 0) {
1917 Vector<const effect_uuid_t *> types;
1918 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1919 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1920 continue;
1921 }
1922 types.add(&mSuspendedEffects.valueAt(i)->mType);
1923 }
1924 for (size_t i = 0; i < types.size(); i++) {
1925 setEffectSuspended_l(types[i], false);
1926 }
1927 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1928 mSuspendedEffects.keyAt(index));
1929 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1930 }
1931 }
1932}
1933
1934
1935// The volume effect is used for automated tests only
1936#ifndef OPENSL_ES_H_
1937static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1938 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1939const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1940#endif //OPENSL_ES_H_
1941
1942bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1943{
1944 // auxiliary effects and visualizer are never suspended on output mix
1945 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1946 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1947 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1948 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1949 return false;
1950 }
1951 return true;
1952}
1953
1954void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1955 Vector< sp<AudioFlinger::EffectModule> > &effects)
1956{
1957 effects.clear();
1958 for (size_t i = 0; i < mEffects.size(); i++) {
1959 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1960 effects.add(mEffects[i]);
1961 }
1962 }
1963}
1964
1965sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1966 const effect_uuid_t *type)
1967{
1968 sp<EffectModule> effect = getEffectFromType_l(type);
1969 return effect != 0 && effect->isEnabled() ? effect : 0;
1970}
1971
1972void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1973 bool enabled)
1974{
1975 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1976 if (enabled) {
1977 if (index < 0) {
1978 // if the effect is not suspend check if all effects are suspended
1979 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1980 if (index < 0) {
1981 return;
1982 }
1983 if (!isEffectEligibleForSuspend(effect->desc())) {
1984 return;
1985 }
1986 setEffectSuspended_l(&effect->desc().type, enabled);
1987 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1988 if (index < 0) {
1989 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1990 return;
1991 }
1992 }
1993 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1994 effect->desc().type.timeLow);
1995 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1996 // if effect is requested to suspended but was not yet enabled, supend it now.
1997 if (desc->mEffect == 0) {
1998 desc->mEffect = effect;
1999 effect->setEnabled(false);
2000 effect->setSuspended(true);
2001 }
2002 } else {
2003 if (index < 0) {
2004 return;
2005 }
2006 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2007 effect->desc().type.timeLow);
2008 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2009 desc->mEffect.clear();
2010 effect->setSuspended(false);
2011 }
2012}
2013
Eric Laurent5baf2af2013-09-12 17:37:00 -07002014bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002015{
2016 Mutex::Autolock _l(mLock);
2017 size_t size = mEffects.size();
2018 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002019 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002020 return true;
2021 }
2022 }
2023 return false;
2024}
2025
Eric Laurentaaa44472014-09-12 17:41:50 -07002026void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2027{
2028 Mutex::Autolock _l(mLock);
2029 mThread = thread;
2030 for (size_t i = 0; i < mEffects.size(); i++) {
2031 mEffects[i]->setThread(thread);
2032 }
2033}
2034
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002035void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2036{
2037 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2038 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2039 }
2040 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2041 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2042 }
2043}
2044
2045void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2046{
2047 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2048 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2049 }
2050 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2051 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2052 }
2053}
2054
2055bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002056{
2057 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002058 for (const auto &effect : mEffects) {
2059 if (effect->isProcessImplemented()) {
2060 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002061 }
2062 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002063 // Allow effects without processing.
2064 return true;
2065}
2066
2067bool AudioFlinger::EffectChain::isFastCompatible() const
2068{
2069 Mutex::Autolock _l(mLock);
2070 for (const auto &effect : mEffects) {
2071 if (effect->isProcessImplemented()
2072 && effect->isImplementationSoftware()) {
2073 return false;
2074 }
2075 }
2076 // Allow effects without processing or hw accelerated effects.
2077 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002078}
2079
2080// isCompatibleWithThread_l() must be called with thread->mLock held
2081bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2082{
2083 Mutex::Autolock _l(mLock);
2084 for (size_t i = 0; i < mEffects.size(); i++) {
2085 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2086 return false;
2087 }
2088 }
2089 return true;
2090}
2091
Glenn Kasten63238ef2015-03-02 15:50:29 -08002092} // namespace android