blob: 0d245cb0498b54ad7815cd4d258ad2d1d8ea316c [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 Hung3d34cc72016-11-04 19:40:53 -0700603 if (cmdCode == EFFECT_CMD_GET_PARAM &&
604 (sizeof(effect_param_t) > cmdSize ||
605 ((effect_param_t *)pCmdData)->psize > cmdSize
606 - sizeof(effect_param_t))) {
607 android_errorWriteLog(0x534e4554, "32438594");
608 return -EINVAL;
609 }
ragoe2759072016-11-22 18:02:48 -0800610 if (cmdCode == EFFECT_CMD_GET_PARAM &&
611 (sizeof(effect_param_t) > *replySize
612 || ((effect_param_t *)pCmdData)->psize > *replySize
613 - sizeof(effect_param_t)
614 || ((effect_param_t *)pCmdData)->vsize > *replySize
615 - sizeof(effect_param_t)
616 - ((effect_param_t *)pCmdData)->psize
617 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
618 *replySize
619 - sizeof(effect_param_t)
620 - ((effect_param_t *)pCmdData)->psize
621 - ((effect_param_t *)pCmdData)->vsize)) {
622 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
623 android_errorWriteLog(0x534e4554, "32705438");
624 return -EINVAL;
625 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700626 if ((cmdCode == EFFECT_CMD_SET_PARAM
627 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
628 (sizeof(effect_param_t) > cmdSize
629 || ((effect_param_t *)pCmdData)->psize > cmdSize
630 - sizeof(effect_param_t)
631 || ((effect_param_t *)pCmdData)->vsize > cmdSize
632 - sizeof(effect_param_t)
633 - ((effect_param_t *)pCmdData)->psize
634 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
635 cmdSize
636 - sizeof(effect_param_t)
637 - ((effect_param_t *)pCmdData)->psize
638 - ((effect_param_t *)pCmdData)->vsize)) {
639 android_errorWriteLog(0x534e4554, "30204301");
640 return -EINVAL;
641 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800642 status_t status = (*mEffectInterface)->command(mEffectInterface,
643 cmdCode,
644 cmdSize,
645 pCmdData,
646 replySize,
647 pReplyData);
648 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
649 uint32_t size = (replySize == NULL) ? 0 : *replySize;
650 for (size_t i = 1; i < mHandles.size(); i++) {
651 EffectHandle *h = mHandles[i];
652 if (h != NULL && !h->destroyed_l()) {
653 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
654 }
655 }
656 }
657 return status;
658}
659
660status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
661{
662 Mutex::Autolock _l(mLock);
663 return setEnabled_l(enabled);
664}
665
666// must be called with EffectModule::mLock held
667status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
668{
669
670 ALOGV("setEnabled %p enabled %d", this, enabled);
671
672 if (enabled != isEnabled()) {
673 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
674 if (enabled && status != NO_ERROR) {
675 return status;
676 }
677
678 switch (mState) {
679 // going from disabled to enabled
680 case IDLE:
681 mState = STARTING;
682 break;
683 case STOPPED:
684 mState = RESTART;
685 break;
686 case STOPPING:
687 mState = ACTIVE;
688 break;
689
690 // going from enabled to disabled
691 case RESTART:
692 mState = STOPPED;
693 break;
694 case STARTING:
695 mState = IDLE;
696 break;
697 case ACTIVE:
698 mState = STOPPING;
699 break;
700 case DESTROYED:
701 return NO_ERROR; // simply ignore as we are being destroyed
702 }
703 for (size_t i = 1; i < mHandles.size(); i++) {
704 EffectHandle *h = mHandles[i];
705 if (h != NULL && !h->destroyed_l()) {
706 h->setEnabled(enabled);
707 }
708 }
709 }
710 return NO_ERROR;
711}
712
713bool AudioFlinger::EffectModule::isEnabled() const
714{
715 switch (mState) {
716 case RESTART:
717 case STARTING:
718 case ACTIVE:
719 return true;
720 case IDLE:
721 case STOPPING:
722 case STOPPED:
723 case DESTROYED:
724 default:
725 return false;
726 }
727}
728
729bool AudioFlinger::EffectModule::isProcessEnabled() const
730{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700731 if (mStatus != NO_ERROR) {
732 return false;
733 }
734
Eric Laurentca7cc822012-11-19 14:55:58 -0800735 switch (mState) {
736 case RESTART:
737 case ACTIVE:
738 case STOPPING:
739 case STOPPED:
740 return true;
741 case IDLE:
742 case STARTING:
743 case DESTROYED:
744 default:
745 return false;
746 }
747}
748
749status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
750{
751 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700752 if (mStatus != NO_ERROR) {
753 return mStatus;
754 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800755 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800756 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
757 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
758 if (isProcessEnabled() &&
759 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
760 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800761 uint32_t volume[2];
762 uint32_t *pVolume = NULL;
763 uint32_t size = sizeof(volume);
764 volume[0] = *left;
765 volume[1] = *right;
766 if (controller) {
767 pVolume = volume;
768 }
769 status = (*mEffectInterface)->command(mEffectInterface,
770 EFFECT_CMD_SET_VOLUME,
771 size,
772 volume,
773 &size,
774 pVolume);
775 if (controller && status == NO_ERROR && size == sizeof(volume)) {
776 *left = volume[0];
777 *right = volume[1];
778 }
779 }
780 return status;
781}
782
783status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
784{
785 if (device == AUDIO_DEVICE_NONE) {
786 return NO_ERROR;
787 }
788
789 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700790 if (mStatus != NO_ERROR) {
791 return mStatus;
792 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800793 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700794 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800795 status_t cmdStatus;
796 uint32_t size = sizeof(status_t);
797 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
798 EFFECT_CMD_SET_INPUT_DEVICE;
799 status = (*mEffectInterface)->command(mEffectInterface,
800 cmd,
801 sizeof(uint32_t),
802 &device,
803 &size,
804 &cmdStatus);
805 }
806 return status;
807}
808
809status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
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_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
817 status_t cmdStatus;
818 uint32_t size = sizeof(status_t);
819 status = (*mEffectInterface)->command(mEffectInterface,
820 EFFECT_CMD_SET_AUDIO_MODE,
821 sizeof(audio_mode_t),
822 &mode,
823 &size,
824 &cmdStatus);
825 if (status == NO_ERROR) {
826 status = cmdStatus;
827 }
828 }
829 return status;
830}
831
832status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
833{
834 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700835 if (mStatus != NO_ERROR) {
836 return mStatus;
837 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 status_t status = NO_ERROR;
839 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
840 uint32_t size = 0;
841 status = (*mEffectInterface)->command(mEffectInterface,
842 EFFECT_CMD_SET_AUDIO_SOURCE,
843 sizeof(audio_source_t),
844 &source,
845 &size,
846 NULL);
847 }
848 return status;
849}
850
851void AudioFlinger::EffectModule::setSuspended(bool suspended)
852{
853 Mutex::Autolock _l(mLock);
854 mSuspended = suspended;
855}
856
857bool AudioFlinger::EffectModule::suspended() const
858{
859 Mutex::Autolock _l(mLock);
860 return mSuspended;
861}
862
863bool AudioFlinger::EffectModule::purgeHandles()
864{
865 bool enabled = false;
866 Mutex::Autolock _l(mLock);
867 for (size_t i = 0; i < mHandles.size(); i++) {
868 EffectHandle *handle = mHandles[i];
869 if (handle != NULL && !handle->destroyed_l()) {
870 handle->effect().clear();
871 if (handle->hasControl()) {
872 enabled = handle->enabled();
873 }
874 }
875 }
876 return enabled;
877}
878
Eric Laurent5baf2af2013-09-12 17:37:00 -0700879status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
880{
881 Mutex::Autolock _l(mLock);
882 if (mStatus != NO_ERROR) {
883 return mStatus;
884 }
885 status_t status = NO_ERROR;
886 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
887 status_t cmdStatus;
888 uint32_t size = sizeof(status_t);
889 effect_offload_param_t cmd;
890
891 cmd.isOffload = offloaded;
892 cmd.ioHandle = io;
893 status = (*mEffectInterface)->command(mEffectInterface,
894 EFFECT_CMD_OFFLOAD,
895 sizeof(effect_offload_param_t),
896 &cmd,
897 &size,
898 &cmdStatus);
899 if (status == NO_ERROR) {
900 status = cmdStatus;
901 }
902 mOffloaded = (status == NO_ERROR) ? offloaded : false;
903 } else {
904 if (offloaded) {
905 status = INVALID_OPERATION;
906 }
907 mOffloaded = false;
908 }
909 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
910 return status;
911}
912
913bool AudioFlinger::EffectModule::isOffloaded() const
914{
915 Mutex::Autolock _l(mLock);
916 return mOffloaded;
917}
918
Marco Nelissenb2208842014-02-07 14:00:50 -0800919String8 effectFlagsToString(uint32_t flags) {
920 String8 s;
921
922 s.append("conn. mode: ");
923 switch (flags & EFFECT_FLAG_TYPE_MASK) {
924 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
925 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
926 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
927 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
928 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
929 default: s.append("unknown/reserved"); break;
930 }
931 s.append(", ");
932
933 s.append("insert pref: ");
934 switch (flags & EFFECT_FLAG_INSERT_MASK) {
935 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
936 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
937 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
938 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
939 default: s.append("unknown/reserved"); break;
940 }
941 s.append(", ");
942
943 s.append("volume mgmt: ");
944 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
945 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
946 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
947 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
948 default: s.append("unknown/reserved"); break;
949 }
950 s.append(", ");
951
952 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
953 if (devind) {
954 s.append("device indication: ");
955 switch (devind) {
956 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
957 default: s.append("unknown/reserved"); break;
958 }
959 s.append(", ");
960 }
961
962 s.append("input mode: ");
963 switch (flags & EFFECT_FLAG_INPUT_MASK) {
964 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
965 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
966 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
967 default: s.append("not set"); break;
968 }
969 s.append(", ");
970
971 s.append("output mode: ");
972 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
973 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
974 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
975 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
976 default: s.append("not set"); break;
977 }
978 s.append(", ");
979
980 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
981 if (accel) {
982 s.append("hardware acceleration: ");
983 switch (accel) {
984 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
985 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
986 default: s.append("unknown/reserved"); break;
987 }
988 s.append(", ");
989 }
990
991 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
992 if (modeind) {
993 s.append("mode indication: ");
994 switch (modeind) {
995 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
996 default: s.append("unknown/reserved"); break;
997 }
998 s.append(", ");
999 }
1000
1001 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1002 if (srcind) {
1003 s.append("source indication: ");
1004 switch (srcind) {
1005 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1006 default: s.append("unknown/reserved"); break;
1007 }
1008 s.append(", ");
1009 }
1010
1011 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1012 s.append("offloadable, ");
1013 }
1014
1015 int len = s.length();
1016 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001017 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001018 s.unlockBuffer(len - 2);
1019 }
1020 return s;
1021}
1022
1023
Glenn Kasten0f11b512014-01-31 16:18:54 -08001024void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001025{
1026 const size_t SIZE = 256;
1027 char buffer[SIZE];
1028 String8 result;
1029
1030 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1031 result.append(buffer);
1032
1033 bool locked = AudioFlinger::dumpTryLock(mLock);
1034 // failed to lock - AudioFlinger is probably deadlocked
1035 if (!locked) {
1036 result.append("\t\tCould not lock Fx mutex:\n");
1037 }
1038
1039 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001040 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
1041 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001042 result.append(buffer);
1043
1044 result.append("\t\tDescriptor:\n");
1045 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1046 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1047 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1048 mDescriptor.uuid.node[2],
1049 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1050 result.append(buffer);
1051 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1052 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1053 mDescriptor.type.timeHiAndVersion,
1054 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1055 mDescriptor.type.node[2],
1056 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1057 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001058 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001059 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001060 mDescriptor.flags,
1061 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001062 result.append(buffer);
1063 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1064 mDescriptor.name);
1065 result.append(buffer);
1066 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1067 mDescriptor.implementor);
1068 result.append(buffer);
1069
1070 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001071 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001072 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001073 mConfig.inputCfg.buffer.frameCount,
1074 mConfig.inputCfg.samplingRate,
1075 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001076 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001077 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001078 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001079 result.append(buffer);
1080
1081 result.append("\t\t- Output configuration:\n");
1082 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001083 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001084 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001085 mConfig.outputCfg.buffer.frameCount,
1086 mConfig.outputCfg.samplingRate,
1087 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001088 mConfig.outputCfg.format,
1089 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001090 result.append(buffer);
1091
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001092 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001094 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 for (size_t i = 0; i < mHandles.size(); ++i) {
1096 EffectHandle *handle = mHandles[i];
1097 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001098 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001099 result.append(buffer);
1100 }
1101 }
1102
Eric Laurentca7cc822012-11-19 14:55:58 -08001103 write(fd, result.string(), result.length());
1104
1105 if (locked) {
1106 mLock.unlock();
1107 }
1108}
1109
1110// ----------------------------------------------------------------------------
1111// EffectHandle implementation
1112// ----------------------------------------------------------------------------
1113
1114#undef LOG_TAG
1115#define LOG_TAG "AudioFlinger::EffectHandle"
1116
1117AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1118 const sp<AudioFlinger::Client>& client,
1119 const sp<IEffectClient>& effectClient,
1120 int32_t priority)
1121 : BnEffect(),
1122 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1123 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1124{
1125 ALOGV("constructor %p", this);
1126
1127 if (client == 0) {
1128 return;
1129 }
1130 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1131 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001132 if (mCblkMemory == 0 ||
1133 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001134 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001135 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001136 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001137 return;
1138 }
Glenn Kastene75da402013-11-20 13:54:52 -08001139 new(mCblk) effect_param_cblk_t();
1140 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001141}
1142
1143AudioFlinger::EffectHandle::~EffectHandle()
1144{
1145 ALOGV("Destructor %p", this);
1146
1147 if (mEffect == 0) {
1148 mDestroyed = true;
1149 return;
1150 }
1151 mEffect->lock();
1152 mDestroyed = true;
1153 mEffect->unlock();
1154 disconnect(false);
1155}
1156
Glenn Kastene75da402013-11-20 13:54:52 -08001157status_t AudioFlinger::EffectHandle::initCheck()
1158{
1159 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1160}
1161
Eric Laurentca7cc822012-11-19 14:55:58 -08001162status_t AudioFlinger::EffectHandle::enable()
1163{
1164 ALOGV("enable %p", this);
1165 if (!mHasControl) {
1166 return INVALID_OPERATION;
1167 }
1168 if (mEffect == 0) {
1169 return DEAD_OBJECT;
1170 }
1171
1172 if (mEnabled) {
1173 return NO_ERROR;
1174 }
1175
1176 mEnabled = true;
1177
1178 sp<ThreadBase> thread = mEffect->thread().promote();
1179 if (thread != 0) {
1180 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1181 }
1182
1183 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1184 if (mEffect->suspended()) {
1185 return NO_ERROR;
1186 }
1187
1188 status_t status = mEffect->setEnabled(true);
1189 if (status != NO_ERROR) {
1190 if (thread != 0) {
1191 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1192 }
1193 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001194 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001195 if (thread != 0) {
1196 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001197 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001198 Mutex::Autolock _l(t->mLock);
1199 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001200 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001201 if (!mEffect->isOffloadable()) {
1202 if (thread->type() == ThreadBase::OFFLOAD) {
1203 PlaybackThread *t = (PlaybackThread *)thread.get();
1204 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1205 }
1206 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1207 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1208 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001209 }
1210 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001211 }
1212 return status;
1213}
1214
1215status_t AudioFlinger::EffectHandle::disable()
1216{
1217 ALOGV("disable %p", this);
1218 if (!mHasControl) {
1219 return INVALID_OPERATION;
1220 }
1221 if (mEffect == 0) {
1222 return DEAD_OBJECT;
1223 }
1224
1225 if (!mEnabled) {
1226 return NO_ERROR;
1227 }
1228 mEnabled = false;
1229
1230 if (mEffect->suspended()) {
1231 return NO_ERROR;
1232 }
1233
1234 status_t status = mEffect->setEnabled(false);
1235
1236 sp<ThreadBase> thread = mEffect->thread().promote();
1237 if (thread != 0) {
1238 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001239 if (thread->type() == ThreadBase::OFFLOAD) {
1240 PlaybackThread *t = (PlaybackThread *)thread.get();
1241 Mutex::Autolock _l(t->mLock);
1242 t->broadcast_l();
1243 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001244 }
1245
1246 return status;
1247}
1248
1249void AudioFlinger::EffectHandle::disconnect()
1250{
1251 disconnect(true);
1252}
1253
1254void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1255{
1256 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1257 if (mEffect == 0) {
1258 return;
1259 }
1260 // restore suspended effects if the disconnected handle was enabled and the last one.
1261 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1262 sp<ThreadBase> thread = mEffect->thread().promote();
1263 if (thread != 0) {
1264 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1265 }
1266 }
1267
1268 // release sp on module => module destructor can be called now
1269 mEffect.clear();
1270 if (mClient != 0) {
1271 if (mCblk != NULL) {
1272 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1273 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1274 }
1275 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001276 // Client destructor must run with AudioFlinger client mutex locked
1277 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001278 mClient.clear();
1279 }
1280}
1281
1282status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1283 uint32_t cmdSize,
1284 void *pCmdData,
1285 uint32_t *replySize,
1286 void *pReplyData)
1287{
1288 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1289 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1290
1291 // only get parameter command is permitted for applications not controlling the effect
1292 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1293 return INVALID_OPERATION;
1294 }
1295 if (mEffect == 0) {
1296 return DEAD_OBJECT;
1297 }
1298 if (mClient == 0) {
1299 return INVALID_OPERATION;
1300 }
1301
1302 // handle commands that are not forwarded transparently to effect engine
1303 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1304 // No need to trylock() here as this function is executed in the binder thread serving a
1305 // particular client process: no risk to block the whole media server process or mixer
1306 // threads if we are stuck here
1307 Mutex::Autolock _l(mCblk->lock);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001308
1309 // keep local copy of index in case of client corruption b/32220769
1310 const uint32_t clientIndex = mCblk->clientIndex;
1311 const uint32_t serverIndex = mCblk->serverIndex;
1312 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1313 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001314 mCblk->serverIndex = 0;
1315 mCblk->clientIndex = 0;
1316 return BAD_VALUE;
1317 }
1318 status_t status = NO_ERROR;
Andy Hungdd79ccd2016-11-15 17:19:58 -08001319 effect_param_t *param = NULL;
1320 for (uint32_t index = serverIndex; index < clientIndex;) {
1321 int *p = (int *)(mBuffer + index);
1322 const int size = *p++;
1323 if (size < 0
1324 || size > EFFECT_PARAM_BUFFER_SIZE
1325 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001326 ALOGW("command(): invalid parameter block size");
Andy Hungdd79ccd2016-11-15 17:19:58 -08001327 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001328 break;
1329 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001330
1331 // copy to local memory in case of client corruption b/32220769
1332 param = (effect_param_t *)realloc(param, size);
1333 if (param == NULL) {
1334 ALOGW("command(): out of memory");
1335 status = NO_MEMORY;
1336 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001337 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001338 memcpy(param, p, size);
1339
1340 int reply = 0;
1341 uint32_t rsize = sizeof(reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001342 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
Andy Hungdd79ccd2016-11-15 17:19:58 -08001343 size,
1344 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001345 &rsize,
1346 &reply);
Andy Hungdd79ccd2016-11-15 17:19:58 -08001347
1348 // verify shared memory: server index shouldn't change; client index can't go back.
1349 if (serverIndex != mCblk->serverIndex
1350 || clientIndex > mCblk->clientIndex) {
1351 android_errorWriteLog(0x534e4554, "32220769");
1352 status = BAD_VALUE;
1353 break;
1354 }
1355
Eric Laurentca7cc822012-11-19 14:55:58 -08001356 // stop at first error encountered
1357 if (ret != NO_ERROR) {
1358 status = ret;
1359 *(int *)pReplyData = reply;
1360 break;
1361 } else if (reply != NO_ERROR) {
1362 *(int *)pReplyData = reply;
1363 break;
1364 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001365 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001366 }
Andy Hungdd79ccd2016-11-15 17:19:58 -08001367 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001368 mCblk->serverIndex = 0;
1369 mCblk->clientIndex = 0;
1370 return status;
1371 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1372 *(int *)pReplyData = NO_ERROR;
1373 return enable();
1374 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1375 *(int *)pReplyData = NO_ERROR;
1376 return disable();
1377 }
1378
1379 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1380}
1381
1382void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1383{
1384 ALOGV("setControl %p control %d", this, hasControl);
1385
1386 mHasControl = hasControl;
1387 mEnabled = enabled;
1388
1389 if (signal && mEffectClient != 0) {
1390 mEffectClient->controlStatusChanged(hasControl);
1391 }
1392}
1393
1394void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1395 uint32_t cmdSize,
1396 void *pCmdData,
1397 uint32_t replySize,
1398 void *pReplyData)
1399{
1400 if (mEffectClient != 0) {
1401 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1402 }
1403}
1404
1405
1406
1407void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1408{
1409 if (mEffectClient != 0) {
1410 mEffectClient->enableStatusChanged(enabled);
1411 }
1412}
1413
1414status_t AudioFlinger::EffectHandle::onTransact(
1415 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1416{
1417 return BnEffect::onTransact(code, data, reply, flags);
1418}
1419
1420
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001421void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001422{
1423 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1424
Marco Nelissenb2208842014-02-07 14:00:50 -08001425 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001426 (mClient == 0) ? getpid_cached : mClient->pid(),
1427 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001428 mHasControl ? "yes" : "no",
1429 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001430 mCblk ? mCblk->clientIndex : 0,
1431 mCblk ? mCblk->serverIndex : 0
1432 );
1433
1434 if (locked) {
1435 mCblk->lock.unlock();
1436 }
1437}
1438
1439#undef LOG_TAG
1440#define LOG_TAG "AudioFlinger::EffectChain"
1441
1442AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001443 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001444 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1445 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001446 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001447{
1448 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1449 if (thread == NULL) {
1450 return;
1451 }
1452 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1453 thread->frameCount();
1454}
1455
1456AudioFlinger::EffectChain::~EffectChain()
1457{
1458 if (mOwnInBuffer) {
1459 delete mInBuffer;
1460 }
1461
1462}
1463
1464// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1465sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1466 effect_descriptor_t *descriptor)
1467{
1468 size_t size = mEffects.size();
1469
1470 for (size_t i = 0; i < size; i++) {
1471 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1472 return mEffects[i];
1473 }
1474 }
1475 return 0;
1476}
1477
1478// getEffectFromId_l() must be called with ThreadBase::mLock held
1479sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1480{
1481 size_t size = mEffects.size();
1482
1483 for (size_t i = 0; i < size; i++) {
1484 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1485 if (id == 0 || mEffects[i]->id() == id) {
1486 return mEffects[i];
1487 }
1488 }
1489 return 0;
1490}
1491
1492// getEffectFromType_l() must be called with ThreadBase::mLock held
1493sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1494 const effect_uuid_t *type)
1495{
1496 size_t size = mEffects.size();
1497
1498 for (size_t i = 0; i < size; i++) {
1499 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1500 return mEffects[i];
1501 }
1502 }
1503 return 0;
1504}
1505
1506void AudioFlinger::EffectChain::clearInputBuffer()
1507{
1508 Mutex::Autolock _l(mLock);
1509 sp<ThreadBase> thread = mThread.promote();
1510 if (thread == 0) {
1511 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1512 return;
1513 }
1514 clearInputBuffer_l(thread);
1515}
1516
1517// Must be called with EffectChain::mLock locked
1518void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1519{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001520 // TODO: This will change in the future, depending on multichannel
1521 // and sample format changes for effects.
1522 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1523 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001524 const size_t frameSize =
1525 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001526 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001527}
1528
1529// Must be called with EffectChain::mLock locked
1530void AudioFlinger::EffectChain::process_l()
1531{
1532 sp<ThreadBase> thread = mThread.promote();
1533 if (thread == 0) {
1534 ALOGW("process_l(): cannot promote mixer thread");
1535 return;
1536 }
1537 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1538 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001539 // never process effects when:
1540 // - on an OFFLOAD thread
1541 // - no more tracks are on the session and the effect tail has been rendered
1542 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001543 if (!isGlobalSession) {
1544 bool tracksOnSession = (trackCnt() != 0);
1545
1546 if (!tracksOnSession && mTailBufferCount == 0) {
1547 doProcess = false;
1548 }
1549
1550 if (activeTrackCnt() == 0) {
1551 // if no track is active and the effect tail has not been rendered,
1552 // the input buffer must be cleared here as the mixer process will not do it
1553 if (tracksOnSession || mTailBufferCount > 0) {
1554 clearInputBuffer_l(thread);
1555 if (mTailBufferCount > 0) {
1556 mTailBufferCount--;
1557 }
1558 }
1559 }
1560 }
1561
1562 size_t size = mEffects.size();
1563 if (doProcess) {
1564 for (size_t i = 0; i < size; i++) {
1565 mEffects[i]->process();
1566 }
1567 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001568 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001569 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001570 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1571 }
1572 if (doResetVolume) {
1573 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001574 }
1575}
1576
1577// addEffect_l() must be called with PlaybackThread::mLock held
1578status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1579{
1580 effect_descriptor_t desc = effect->desc();
1581 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1582
1583 Mutex::Autolock _l(mLock);
1584 effect->setChain(this);
1585 sp<ThreadBase> thread = mThread.promote();
1586 if (thread == 0) {
1587 return NO_INIT;
1588 }
1589 effect->setThread(thread);
1590
1591 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1592 // Auxiliary effects are inserted at the beginning of mEffects vector as
1593 // they are processed first and accumulated in chain input buffer
1594 mEffects.insertAt(effect, 0);
1595
1596 // the input buffer for auxiliary effect contains mono samples in
1597 // 32 bit format. This is to avoid saturation in AudoMixer
1598 // accumulation stage. Saturation is done in EffectModule::process() before
1599 // calling the process in effect engine
1600 size_t numSamples = thread->frameCount();
1601 int32_t *buffer = new int32_t[numSamples];
1602 memset(buffer, 0, numSamples * sizeof(int32_t));
1603 effect->setInBuffer((int16_t *)buffer);
1604 // auxiliary effects output samples to chain input buffer for further processing
1605 // by insert effects
1606 effect->setOutBuffer(mInBuffer);
1607 } else {
1608 // Insert effects are inserted at the end of mEffects vector as they are processed
1609 // after track and auxiliary effects.
1610 // Insert effect order as a function of indicated preference:
1611 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1612 // another effect is present
1613 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1614 // last effect claiming first position
1615 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1616 // first effect claiming last position
1617 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1618 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1619 // already present
1620
1621 size_t size = mEffects.size();
1622 size_t idx_insert = size;
1623 ssize_t idx_insert_first = -1;
1624 ssize_t idx_insert_last = -1;
1625
1626 for (size_t i = 0; i < size; i++) {
1627 effect_descriptor_t d = mEffects[i]->desc();
1628 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1629 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1630 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1631 // check invalid effect chaining combinations
1632 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1633 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1634 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1635 desc.name, d.name);
1636 return INVALID_OPERATION;
1637 }
1638 // remember position of first insert effect and by default
1639 // select this as insert position for new effect
1640 if (idx_insert == size) {
1641 idx_insert = i;
1642 }
1643 // remember position of last insert effect claiming
1644 // first position
1645 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1646 idx_insert_first = i;
1647 }
1648 // remember position of first insert effect claiming
1649 // last position
1650 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1651 idx_insert_last == -1) {
1652 idx_insert_last = i;
1653 }
1654 }
1655 }
1656
1657 // modify idx_insert from first position if needed
1658 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1659 if (idx_insert_last != -1) {
1660 idx_insert = idx_insert_last;
1661 } else {
1662 idx_insert = size;
1663 }
1664 } else {
1665 if (idx_insert_first != -1) {
1666 idx_insert = idx_insert_first + 1;
1667 }
1668 }
1669
1670 // always read samples from chain input buffer
1671 effect->setInBuffer(mInBuffer);
1672
1673 // if last effect in the chain, output samples to chain
1674 // output buffer, otherwise to chain input buffer
1675 if (idx_insert == size) {
1676 if (idx_insert != 0) {
1677 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1678 mEffects[idx_insert-1]->configure();
1679 }
1680 effect->setOutBuffer(mOutBuffer);
1681 } else {
1682 effect->setOutBuffer(mInBuffer);
1683 }
1684 mEffects.insertAt(effect, idx_insert);
1685
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001686 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001687 idx_insert);
1688 }
1689 effect->configure();
1690 return NO_ERROR;
1691}
1692
1693// removeEffect_l() must be called with PlaybackThread::mLock held
1694size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1695{
1696 Mutex::Autolock _l(mLock);
1697 size_t size = mEffects.size();
1698 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1699
1700 for (size_t i = 0; i < size; i++) {
1701 if (effect == mEffects[i]) {
1702 // calling stop here will remove pre-processing effect from the audio HAL.
1703 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1704 // the middle of a read from audio HAL
1705 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1706 mEffects[i]->state() == EffectModule::STOPPING) {
1707 mEffects[i]->stop();
1708 }
1709 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1710 delete[] effect->inBuffer();
1711 } else {
1712 if (i == size - 1 && i != 0) {
1713 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1714 mEffects[i - 1]->configure();
1715 }
1716 }
1717 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001718 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001719 this, i);
1720 break;
1721 }
1722 }
1723
1724 return mEffects.size();
1725}
1726
1727// setDevice_l() must be called with PlaybackThread::mLock held
1728void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1729{
1730 size_t size = mEffects.size();
1731 for (size_t i = 0; i < size; i++) {
1732 mEffects[i]->setDevice(device);
1733 }
1734}
1735
1736// setMode_l() must be called with PlaybackThread::mLock held
1737void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1738{
1739 size_t size = mEffects.size();
1740 for (size_t i = 0; i < size; i++) {
1741 mEffects[i]->setMode(mode);
1742 }
1743}
1744
1745// setAudioSource_l() must be called with PlaybackThread::mLock held
1746void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1747{
1748 size_t size = mEffects.size();
1749 for (size_t i = 0; i < size; i++) {
1750 mEffects[i]->setAudioSource(source);
1751 }
1752}
1753
Eric Laurentfa1e1232016-08-02 19:01:49 -07001754// setVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1755bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001756{
1757 uint32_t newLeft = *left;
1758 uint32_t newRight = *right;
1759 bool hasControl = false;
1760 int ctrlIdx = -1;
1761 size_t size = mEffects.size();
1762
1763 // first update volume controller
1764 for (size_t i = size; i > 0; i--) {
1765 if (mEffects[i - 1]->isProcessEnabled() &&
1766 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1767 ctrlIdx = i - 1;
1768 hasControl = true;
1769 break;
1770 }
1771 }
1772
Eric Laurentfa1e1232016-08-02 19:01:49 -07001773 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001774 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001775 if (hasControl) {
1776 *left = mNewLeftVolume;
1777 *right = mNewRightVolume;
1778 }
1779 return hasControl;
1780 }
1781
1782 mVolumeCtrlIdx = ctrlIdx;
1783 mLeftVolume = newLeft;
1784 mRightVolume = newRight;
1785
1786 // second get volume update from volume controller
1787 if (ctrlIdx >= 0) {
1788 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1789 mNewLeftVolume = newLeft;
1790 mNewRightVolume = newRight;
1791 }
1792 // then indicate volume to all other effects in chain.
1793 // Pass altered volume to effects before volume controller
1794 // and requested volume to effects after controller
1795 uint32_t lVol = newLeft;
1796 uint32_t rVol = newRight;
1797
1798 for (size_t i = 0; i < size; i++) {
1799 if ((int)i == ctrlIdx) {
1800 continue;
1801 }
1802 // this also works for ctrlIdx == -1 when there is no volume controller
1803 if ((int)i > ctrlIdx) {
1804 lVol = *left;
1805 rVol = *right;
1806 }
1807 mEffects[i]->setVolume(&lVol, &rVol, false);
1808 }
1809 *left = newLeft;
1810 *right = newRight;
1811
1812 return hasControl;
1813}
1814
Eric Laurentfa1e1232016-08-02 19:01:49 -07001815// resetVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1816void AudioFlinger::EffectChain::resetVolume_l()
1817{
Eric Laurente7449bf2016-08-03 18:44:07 -07001818 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1819 uint32_t left = mLeftVolume;
1820 uint32_t right = mRightVolume;
1821 (void)setVolume_l(&left, &right, true);
1822 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001823}
1824
Eric Laurent1b928682014-10-02 19:41:47 -07001825void AudioFlinger::EffectChain::syncHalEffectsState()
1826{
1827 Mutex::Autolock _l(mLock);
1828 for (size_t i = 0; i < mEffects.size(); i++) {
1829 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1830 mEffects[i]->state() == EffectModule::STOPPING) {
1831 mEffects[i]->addEffectToHal_l();
1832 }
1833 }
1834}
1835
Eric Laurentca7cc822012-11-19 14:55:58 -08001836void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1837{
1838 const size_t SIZE = 256;
1839 char buffer[SIZE];
1840 String8 result;
1841
Marco Nelissenb2208842014-02-07 14:00:50 -08001842 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001843 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001844 result.append(buffer);
1845
Marco Nelissenb2208842014-02-07 14:00:50 -08001846 if (numEffects) {
1847 bool locked = AudioFlinger::dumpTryLock(mLock);
1848 // failed to lock - AudioFlinger is probably deadlocked
1849 if (!locked) {
1850 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001852
Marco Nelissenb2208842014-02-07 14:00:50 -08001853 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001854 snprintf(buffer, SIZE, "\t%p %p %d\n",
1855 mInBuffer,
1856 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001857 mActiveTrackCnt);
1858 result.append(buffer);
1859 write(fd, result.string(), result.size());
1860
1861 for (size_t i = 0; i < numEffects; ++i) {
1862 sp<EffectModule> effect = mEffects[i];
1863 if (effect != 0) {
1864 effect->dump(fd, args);
1865 }
1866 }
1867
1868 if (locked) {
1869 mLock.unlock();
1870 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001871 }
1872}
1873
1874// must be called with ThreadBase::mLock held
1875void AudioFlinger::EffectChain::setEffectSuspended_l(
1876 const effect_uuid_t *type, bool suspend)
1877{
1878 sp<SuspendedEffectDesc> desc;
1879 // use effect type UUID timelow as key as there is no real risk of identical
1880 // timeLow fields among effect type UUIDs.
1881 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1882 if (suspend) {
1883 if (index >= 0) {
1884 desc = mSuspendedEffects.valueAt(index);
1885 } else {
1886 desc = new SuspendedEffectDesc();
1887 desc->mType = *type;
1888 mSuspendedEffects.add(type->timeLow, desc);
1889 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1890 }
1891 if (desc->mRefCount++ == 0) {
1892 sp<EffectModule> effect = getEffectIfEnabled(type);
1893 if (effect != 0) {
1894 desc->mEffect = effect;
1895 effect->setSuspended(true);
1896 effect->setEnabled(false);
1897 }
1898 }
1899 } else {
1900 if (index < 0) {
1901 return;
1902 }
1903 desc = mSuspendedEffects.valueAt(index);
1904 if (desc->mRefCount <= 0) {
1905 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1906 desc->mRefCount = 1;
1907 }
1908 if (--desc->mRefCount == 0) {
1909 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1910 if (desc->mEffect != 0) {
1911 sp<EffectModule> effect = desc->mEffect.promote();
1912 if (effect != 0) {
1913 effect->setSuspended(false);
1914 effect->lock();
1915 EffectHandle *handle = effect->controlHandle_l();
1916 if (handle != NULL && !handle->destroyed_l()) {
1917 effect->setEnabled_l(handle->enabled());
1918 }
1919 effect->unlock();
1920 }
1921 desc->mEffect.clear();
1922 }
1923 mSuspendedEffects.removeItemsAt(index);
1924 }
1925 }
1926}
1927
1928// must be called with ThreadBase::mLock held
1929void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1930{
1931 sp<SuspendedEffectDesc> desc;
1932
1933 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1934 if (suspend) {
1935 if (index >= 0) {
1936 desc = mSuspendedEffects.valueAt(index);
1937 } else {
1938 desc = new SuspendedEffectDesc();
1939 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1940 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1941 }
1942 if (desc->mRefCount++ == 0) {
1943 Vector< sp<EffectModule> > effects;
1944 getSuspendEligibleEffects(effects);
1945 for (size_t i = 0; i < effects.size(); i++) {
1946 setEffectSuspended_l(&effects[i]->desc().type, true);
1947 }
1948 }
1949 } else {
1950 if (index < 0) {
1951 return;
1952 }
1953 desc = mSuspendedEffects.valueAt(index);
1954 if (desc->mRefCount <= 0) {
1955 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1956 desc->mRefCount = 1;
1957 }
1958 if (--desc->mRefCount == 0) {
1959 Vector<const effect_uuid_t *> types;
1960 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1961 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1962 continue;
1963 }
1964 types.add(&mSuspendedEffects.valueAt(i)->mType);
1965 }
1966 for (size_t i = 0; i < types.size(); i++) {
1967 setEffectSuspended_l(types[i], false);
1968 }
1969 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1970 mSuspendedEffects.keyAt(index));
1971 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1972 }
1973 }
1974}
1975
1976
1977// The volume effect is used for automated tests only
1978#ifndef OPENSL_ES_H_
1979static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1980 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1981const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1982#endif //OPENSL_ES_H_
1983
1984bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1985{
1986 // auxiliary effects and visualizer are never suspended on output mix
1987 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1988 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1989 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1990 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1991 return false;
1992 }
1993 return true;
1994}
1995
1996void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1997 Vector< sp<AudioFlinger::EffectModule> > &effects)
1998{
1999 effects.clear();
2000 for (size_t i = 0; i < mEffects.size(); i++) {
2001 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2002 effects.add(mEffects[i]);
2003 }
2004 }
2005}
2006
2007sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2008 const effect_uuid_t *type)
2009{
2010 sp<EffectModule> effect = getEffectFromType_l(type);
2011 return effect != 0 && effect->isEnabled() ? effect : 0;
2012}
2013
2014void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2015 bool enabled)
2016{
2017 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2018 if (enabled) {
2019 if (index < 0) {
2020 // if the effect is not suspend check if all effects are suspended
2021 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2022 if (index < 0) {
2023 return;
2024 }
2025 if (!isEffectEligibleForSuspend(effect->desc())) {
2026 return;
2027 }
2028 setEffectSuspended_l(&effect->desc().type, enabled);
2029 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2030 if (index < 0) {
2031 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2032 return;
2033 }
2034 }
2035 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2036 effect->desc().type.timeLow);
2037 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2038 // if effect is requested to suspended but was not yet enabled, supend it now.
2039 if (desc->mEffect == 0) {
2040 desc->mEffect = effect;
2041 effect->setEnabled(false);
2042 effect->setSuspended(true);
2043 }
2044 } else {
2045 if (index < 0) {
2046 return;
2047 }
2048 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2049 effect->desc().type.timeLow);
2050 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2051 desc->mEffect.clear();
2052 effect->setSuspended(false);
2053 }
2054}
2055
Eric Laurent5baf2af2013-09-12 17:37:00 -07002056bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002057{
2058 Mutex::Autolock _l(mLock);
2059 size_t size = mEffects.size();
2060 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002061 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002062 return true;
2063 }
2064 }
2065 return false;
2066}
2067
Eric Laurentaaa44472014-09-12 17:41:50 -07002068void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2069{
2070 Mutex::Autolock _l(mLock);
2071 mThread = thread;
2072 for (size_t i = 0; i < mEffects.size(); i++) {
2073 mEffects[i]->setThread(thread);
2074 }
2075}
2076
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002077void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2078{
2079 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2080 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2081 }
2082 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2083 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2084 }
2085}
2086
2087void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2088{
2089 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2090 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2091 }
2092 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2093 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2094 }
2095}
2096
2097bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002098{
2099 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002100 for (const auto &effect : mEffects) {
2101 if (effect->isProcessImplemented()) {
2102 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002103 }
2104 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002105 // Allow effects without processing.
2106 return true;
2107}
2108
2109bool AudioFlinger::EffectChain::isFastCompatible() const
2110{
2111 Mutex::Autolock _l(mLock);
2112 for (const auto &effect : mEffects) {
2113 if (effect->isProcessImplemented()
2114 && effect->isImplementationSoftware()) {
2115 return false;
2116 }
2117 }
2118 // Allow effects without processing or hw accelerated effects.
2119 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002120}
2121
2122// isCompatibleWithThread_l() must be called with thread->mLock held
2123bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2124{
2125 Mutex::Autolock _l(mLock);
2126 for (size_t i = 0; i < mEffects.size(); i++) {
2127 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2128 return false;
2129 }
2130 }
2131 return true;
2132}
2133
Glenn Kasten63238ef2015-03-02 15:50:29 -08002134} // namespace android