blob: 07a737782b815e9791442dd83aac8a7852dc149d [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;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900363 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
364 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800365 } else {
366 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700367 // TODO: Update this logic when multichannel effects are implemented.
368 // For offloaded tracks consider mono output as stereo for proper effect initialization
369 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
370 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
371 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
372 ALOGV("Overriding effect input and output as STEREO");
373 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800374 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700375
Eric Laurentca7cc822012-11-19 14:55:58 -0800376 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
377 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
378 mConfig.inputCfg.samplingRate = thread->sampleRate();
379 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
380 mConfig.inputCfg.bufferProvider.cookie = NULL;
381 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
382 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
383 mConfig.outputCfg.bufferProvider.cookie = NULL;
384 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
385 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
386 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
387 // Insert effect:
388 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
389 // always overwrites output buffer: input buffer == output buffer
390 // - in other sessions:
391 // last effect in the chain accumulates in output buffer: input buffer != output buffer
392 // other effect: overwrites output buffer: input buffer == output buffer
393 // Auxiliary effect:
394 // accumulates in output buffer: input buffer != output buffer
395 // Therefore: accumulate <=> input buffer != output buffer
396 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
397 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
398 } else {
399 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
400 }
401 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
402 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
403 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
404 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
405
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700406 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800407 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
408
409 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700410 size = sizeof(int);
411 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800412 EFFECT_CMD_SET_CONFIG,
413 sizeof(effect_config_t),
414 &mConfig,
415 &size,
416 &cmdStatus);
417 if (status == 0) {
418 status = cmdStatus;
419 }
420
421 if (status == 0 &&
422 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
423 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
424 effect_param_t *p = (effect_param_t *)buf32;
425
426 p->psize = sizeof(uint32_t);
427 p->vsize = sizeof(uint32_t);
428 size = sizeof(int);
429 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
430
431 uint32_t latency = 0;
432 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
433 if (pbt != NULL) {
434 latency = pbt->latency_l();
435 }
436
437 *((int32_t *)p->data + 1)= latency;
438 (*mEffectInterface)->command(mEffectInterface,
439 EFFECT_CMD_SET_PARAM,
440 sizeof(effect_param_t) + 8,
441 &buf32,
442 &size,
443 &cmdStatus);
444 }
445
446 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
447 (1000 * mConfig.outputCfg.buffer.frameCount);
448
Eric Laurentd0ebb532013-04-02 16:41:41 -0700449exit:
450 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800451 return status;
452}
453
454status_t AudioFlinger::EffectModule::init()
455{
456 Mutex::Autolock _l(mLock);
457 if (mEffectInterface == NULL) {
458 return NO_INIT;
459 }
460 status_t cmdStatus;
461 uint32_t size = sizeof(status_t);
462 status_t status = (*mEffectInterface)->command(mEffectInterface,
463 EFFECT_CMD_INIT,
464 0,
465 NULL,
466 &size,
467 &cmdStatus);
468 if (status == 0) {
469 status = cmdStatus;
470 }
471 return status;
472}
473
Eric Laurent1b928682014-10-02 19:41:47 -0700474void AudioFlinger::EffectModule::addEffectToHal_l()
475{
476 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
477 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
478 sp<ThreadBase> thread = mThread.promote();
479 if (thread != 0) {
480 audio_stream_t *stream = thread->stream();
481 if (stream != NULL) {
482 stream->add_audio_effect(stream, mEffectInterface);
483 }
484 }
485 }
486}
487
Eric Laurentfa1e1232016-08-02 19:01:49 -0700488// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800489status_t AudioFlinger::EffectModule::start()
490{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700491 sp<EffectChain> chain;
492 status_t status;
493 {
494 Mutex::Autolock _l(mLock);
495 status = start_l();
496 if (status == NO_ERROR) {
497 chain = mChain.promote();
498 }
499 }
500 if (chain != 0) {
501 chain->resetVolume_l();
502 }
503 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800504}
505
506status_t AudioFlinger::EffectModule::start_l()
507{
508 if (mEffectInterface == NULL) {
509 return NO_INIT;
510 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700511 if (mStatus != NO_ERROR) {
512 return mStatus;
513 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800514 status_t cmdStatus;
515 uint32_t size = sizeof(status_t);
516 status_t status = (*mEffectInterface)->command(mEffectInterface,
517 EFFECT_CMD_ENABLE,
518 0,
519 NULL,
520 &size,
521 &cmdStatus);
522 if (status == 0) {
523 status = cmdStatus;
524 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700525 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700526 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800527 }
528 return status;
529}
530
531status_t AudioFlinger::EffectModule::stop()
532{
533 Mutex::Autolock _l(mLock);
534 return stop_l();
535}
536
537status_t AudioFlinger::EffectModule::stop_l()
538{
539 if (mEffectInterface == NULL) {
540 return NO_INIT;
541 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700542 if (mStatus != NO_ERROR) {
543 return mStatus;
544 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800545 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800546 uint32_t size = sizeof(status_t);
547 status_t status = (*mEffectInterface)->command(mEffectInterface,
548 EFFECT_CMD_DISABLE,
549 0,
550 NULL,
551 &size,
552 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800553 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800554 status = cmdStatus;
555 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800556 if (status == NO_ERROR) {
557 status = remove_effect_from_hal_l();
558 }
559 return status;
560}
561
562status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
563{
564 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
565 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800566 sp<ThreadBase> thread = mThread.promote();
567 if (thread != 0) {
568 audio_stream_t *stream = thread->stream();
569 if (stream != NULL) {
570 stream->remove_audio_effect(stream, mEffectInterface);
571 }
572 }
573 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800574 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800575}
576
Andy Hunge4a1d912016-08-17 14:11:13 -0700577// round up delta valid if value and divisor are positive.
578template <typename T>
579static T roundUpDelta(const T &value, const T &divisor) {
580 T remainder = value % divisor;
581 return remainder == 0 ? 0 : divisor - remainder;
582}
583
Eric Laurentca7cc822012-11-19 14:55:58 -0800584status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
585 uint32_t cmdSize,
586 void *pCmdData,
587 uint32_t *replySize,
588 void *pReplyData)
589{
590 Mutex::Autolock _l(mLock);
591 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
592
593 if (mState == DESTROYED || mEffectInterface == NULL) {
594 return NO_INIT;
595 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700596 if (mStatus != NO_ERROR) {
597 return mStatus;
598 }
Andy Hung110bc952016-06-20 15:22:52 -0700599 if (cmdCode == EFFECT_CMD_GET_PARAM &&
600 (*replySize < sizeof(effect_param_t) ||
601 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
602 android_errorWriteLog(0x534e4554, "29251553");
603 return -EINVAL;
604 }
Andy Hung3d34cc72016-11-04 19:40:53 -0700605 if (cmdCode == EFFECT_CMD_GET_PARAM &&
606 (sizeof(effect_param_t) > cmdSize ||
607 ((effect_param_t *)pCmdData)->psize > cmdSize
608 - sizeof(effect_param_t))) {
609 android_errorWriteLog(0x534e4554, "32438594");
610 return -EINVAL;
611 }
ragoe2759072016-11-22 18:02:48 -0800612 if (cmdCode == EFFECT_CMD_GET_PARAM &&
613 (sizeof(effect_param_t) > *replySize
614 || ((effect_param_t *)pCmdData)->psize > *replySize
615 - sizeof(effect_param_t)
616 || ((effect_param_t *)pCmdData)->vsize > *replySize
617 - sizeof(effect_param_t)
618 - ((effect_param_t *)pCmdData)->psize
619 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
620 *replySize
621 - sizeof(effect_param_t)
622 - ((effect_param_t *)pCmdData)->psize
623 - ((effect_param_t *)pCmdData)->vsize)) {
624 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
625 android_errorWriteLog(0x534e4554, "32705438");
626 return -EINVAL;
627 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700628 if ((cmdCode == EFFECT_CMD_SET_PARAM
629 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
630 (sizeof(effect_param_t) > cmdSize
631 || ((effect_param_t *)pCmdData)->psize > cmdSize
632 - sizeof(effect_param_t)
633 || ((effect_param_t *)pCmdData)->vsize > cmdSize
634 - sizeof(effect_param_t)
635 - ((effect_param_t *)pCmdData)->psize
636 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
637 cmdSize
638 - sizeof(effect_param_t)
639 - ((effect_param_t *)pCmdData)->psize
640 - ((effect_param_t *)pCmdData)->vsize)) {
641 android_errorWriteLog(0x534e4554, "30204301");
642 return -EINVAL;
643 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800644 status_t status = (*mEffectInterface)->command(mEffectInterface,
645 cmdCode,
646 cmdSize,
647 pCmdData,
648 replySize,
649 pReplyData);
650 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
651 uint32_t size = (replySize == NULL) ? 0 : *replySize;
652 for (size_t i = 1; i < mHandles.size(); i++) {
653 EffectHandle *h = mHandles[i];
654 if (h != NULL && !h->destroyed_l()) {
655 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
656 }
657 }
658 }
659 return status;
660}
661
662status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
663{
664 Mutex::Autolock _l(mLock);
665 return setEnabled_l(enabled);
666}
667
668// must be called with EffectModule::mLock held
669status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
670{
671
672 ALOGV("setEnabled %p enabled %d", this, enabled);
673
674 if (enabled != isEnabled()) {
675 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
676 if (enabled && status != NO_ERROR) {
677 return status;
678 }
679
680 switch (mState) {
681 // going from disabled to enabled
682 case IDLE:
683 mState = STARTING;
684 break;
685 case STOPPED:
686 mState = RESTART;
687 break;
688 case STOPPING:
689 mState = ACTIVE;
690 break;
691
692 // going from enabled to disabled
693 case RESTART:
694 mState = STOPPED;
695 break;
696 case STARTING:
697 mState = IDLE;
698 break;
699 case ACTIVE:
700 mState = STOPPING;
701 break;
702 case DESTROYED:
703 return NO_ERROR; // simply ignore as we are being destroyed
704 }
705 for (size_t i = 1; i < mHandles.size(); i++) {
706 EffectHandle *h = mHandles[i];
707 if (h != NULL && !h->destroyed_l()) {
708 h->setEnabled(enabled);
709 }
710 }
711 }
712 return NO_ERROR;
713}
714
715bool AudioFlinger::EffectModule::isEnabled() const
716{
717 switch (mState) {
718 case RESTART:
719 case STARTING:
720 case ACTIVE:
721 return true;
722 case IDLE:
723 case STOPPING:
724 case STOPPED:
725 case DESTROYED:
726 default:
727 return false;
728 }
729}
730
731bool AudioFlinger::EffectModule::isProcessEnabled() const
732{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700733 if (mStatus != NO_ERROR) {
734 return false;
735 }
736
Eric Laurentca7cc822012-11-19 14:55:58 -0800737 switch (mState) {
738 case RESTART:
739 case ACTIVE:
740 case STOPPING:
741 case STOPPED:
742 return true;
743 case IDLE:
744 case STARTING:
745 case DESTROYED:
746 default:
747 return false;
748 }
749}
750
751status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
752{
753 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700754 if (mStatus != NO_ERROR) {
755 return mStatus;
756 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800757 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800758 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
759 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
760 if (isProcessEnabled() &&
761 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
762 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800763 uint32_t volume[2];
764 uint32_t *pVolume = NULL;
765 uint32_t size = sizeof(volume);
766 volume[0] = *left;
767 volume[1] = *right;
768 if (controller) {
769 pVolume = volume;
770 }
771 status = (*mEffectInterface)->command(mEffectInterface,
772 EFFECT_CMD_SET_VOLUME,
773 size,
774 volume,
775 &size,
776 pVolume);
777 if (controller && status == NO_ERROR && size == sizeof(volume)) {
778 *left = volume[0];
779 *right = volume[1];
780 }
781 }
782 return status;
783}
784
785status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
786{
787 if (device == AUDIO_DEVICE_NONE) {
788 return NO_ERROR;
789 }
790
791 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700792 if (mStatus != NO_ERROR) {
793 return mStatus;
794 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800795 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700796 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800797 status_t cmdStatus;
798 uint32_t size = sizeof(status_t);
799 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
800 EFFECT_CMD_SET_INPUT_DEVICE;
801 status = (*mEffectInterface)->command(mEffectInterface,
802 cmd,
803 sizeof(uint32_t),
804 &device,
805 &size,
806 &cmdStatus);
807 }
808 return status;
809}
810
811status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
812{
813 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700814 if (mStatus != NO_ERROR) {
815 return mStatus;
816 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800817 status_t status = NO_ERROR;
818 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
819 status_t cmdStatus;
820 uint32_t size = sizeof(status_t);
821 status = (*mEffectInterface)->command(mEffectInterface,
822 EFFECT_CMD_SET_AUDIO_MODE,
823 sizeof(audio_mode_t),
824 &mode,
825 &size,
826 &cmdStatus);
827 if (status == NO_ERROR) {
828 status = cmdStatus;
829 }
830 }
831 return status;
832}
833
834status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
835{
836 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700837 if (mStatus != NO_ERROR) {
838 return mStatus;
839 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800840 status_t status = NO_ERROR;
841 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
842 uint32_t size = 0;
843 status = (*mEffectInterface)->command(mEffectInterface,
844 EFFECT_CMD_SET_AUDIO_SOURCE,
845 sizeof(audio_source_t),
846 &source,
847 &size,
848 NULL);
849 }
850 return status;
851}
852
853void AudioFlinger::EffectModule::setSuspended(bool suspended)
854{
855 Mutex::Autolock _l(mLock);
856 mSuspended = suspended;
857}
858
859bool AudioFlinger::EffectModule::suspended() const
860{
861 Mutex::Autolock _l(mLock);
862 return mSuspended;
863}
864
865bool AudioFlinger::EffectModule::purgeHandles()
866{
867 bool enabled = false;
868 Mutex::Autolock _l(mLock);
869 for (size_t i = 0; i < mHandles.size(); i++) {
870 EffectHandle *handle = mHandles[i];
871 if (handle != NULL && !handle->destroyed_l()) {
872 handle->effect().clear();
873 if (handle->hasControl()) {
874 enabled = handle->enabled();
875 }
876 }
877 }
878 return enabled;
879}
880
Eric Laurent5baf2af2013-09-12 17:37:00 -0700881status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
882{
883 Mutex::Autolock _l(mLock);
884 if (mStatus != NO_ERROR) {
885 return mStatus;
886 }
887 status_t status = NO_ERROR;
888 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
889 status_t cmdStatus;
890 uint32_t size = sizeof(status_t);
891 effect_offload_param_t cmd;
892
893 cmd.isOffload = offloaded;
894 cmd.ioHandle = io;
895 status = (*mEffectInterface)->command(mEffectInterface,
896 EFFECT_CMD_OFFLOAD,
897 sizeof(effect_offload_param_t),
898 &cmd,
899 &size,
900 &cmdStatus);
901 if (status == NO_ERROR) {
902 status = cmdStatus;
903 }
904 mOffloaded = (status == NO_ERROR) ? offloaded : false;
905 } else {
906 if (offloaded) {
907 status = INVALID_OPERATION;
908 }
909 mOffloaded = false;
910 }
911 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
912 return status;
913}
914
915bool AudioFlinger::EffectModule::isOffloaded() const
916{
917 Mutex::Autolock _l(mLock);
918 return mOffloaded;
919}
920
Marco Nelissenb2208842014-02-07 14:00:50 -0800921String8 effectFlagsToString(uint32_t flags) {
922 String8 s;
923
924 s.append("conn. mode: ");
925 switch (flags & EFFECT_FLAG_TYPE_MASK) {
926 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
927 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
928 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
929 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
930 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
931 default: s.append("unknown/reserved"); break;
932 }
933 s.append(", ");
934
935 s.append("insert pref: ");
936 switch (flags & EFFECT_FLAG_INSERT_MASK) {
937 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
938 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
939 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
940 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
941 default: s.append("unknown/reserved"); break;
942 }
943 s.append(", ");
944
945 s.append("volume mgmt: ");
946 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
947 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
948 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
949 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
950 default: s.append("unknown/reserved"); break;
951 }
952 s.append(", ");
953
954 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
955 if (devind) {
956 s.append("device indication: ");
957 switch (devind) {
958 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
959 default: s.append("unknown/reserved"); break;
960 }
961 s.append(", ");
962 }
963
964 s.append("input mode: ");
965 switch (flags & EFFECT_FLAG_INPUT_MASK) {
966 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
967 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
968 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
969 default: s.append("not set"); break;
970 }
971 s.append(", ");
972
973 s.append("output mode: ");
974 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
975 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
976 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
977 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
978 default: s.append("not set"); break;
979 }
980 s.append(", ");
981
982 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
983 if (accel) {
984 s.append("hardware acceleration: ");
985 switch (accel) {
986 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
987 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
988 default: s.append("unknown/reserved"); break;
989 }
990 s.append(", ");
991 }
992
993 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
994 if (modeind) {
995 s.append("mode indication: ");
996 switch (modeind) {
997 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
998 default: s.append("unknown/reserved"); break;
999 }
1000 s.append(", ");
1001 }
1002
1003 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1004 if (srcind) {
1005 s.append("source indication: ");
1006 switch (srcind) {
1007 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1008 default: s.append("unknown/reserved"); break;
1009 }
1010 s.append(", ");
1011 }
1012
1013 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1014 s.append("offloadable, ");
1015 }
1016
1017 int len = s.length();
1018 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001019 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001020 s.unlockBuffer(len - 2);
1021 }
1022 return s;
1023}
1024
1025
Glenn Kasten0f11b512014-01-31 16:18:54 -08001026void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001027{
1028 const size_t SIZE = 256;
1029 char buffer[SIZE];
1030 String8 result;
1031
1032 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1033 result.append(buffer);
1034
1035 bool locked = AudioFlinger::dumpTryLock(mLock);
1036 // failed to lock - AudioFlinger is probably deadlocked
1037 if (!locked) {
1038 result.append("\t\tCould not lock Fx mutex:\n");
1039 }
1040
1041 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001042 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
1043 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001044 result.append(buffer);
1045
1046 result.append("\t\tDescriptor:\n");
1047 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1048 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1049 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1050 mDescriptor.uuid.node[2],
1051 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1052 result.append(buffer);
1053 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1054 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1055 mDescriptor.type.timeHiAndVersion,
1056 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1057 mDescriptor.type.node[2],
1058 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1059 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001060 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001061 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001062 mDescriptor.flags,
1063 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 result.append(buffer);
1065 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1066 mDescriptor.name);
1067 result.append(buffer);
1068 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1069 mDescriptor.implementor);
1070 result.append(buffer);
1071
1072 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001073 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001074 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 mConfig.inputCfg.buffer.frameCount,
1076 mConfig.inputCfg.samplingRate,
1077 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001078 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001079 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001080 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 result.append(buffer);
1082
1083 result.append("\t\t- Output configuration:\n");
1084 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001085 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001086 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 mConfig.outputCfg.buffer.frameCount,
1088 mConfig.outputCfg.samplingRate,
1089 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001090 mConfig.outputCfg.format,
1091 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001092 result.append(buffer);
1093
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001094 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001096 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001097 for (size_t i = 0; i < mHandles.size(); ++i) {
1098 EffectHandle *handle = mHandles[i];
1099 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001100 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001101 result.append(buffer);
1102 }
1103 }
1104
Eric Laurentca7cc822012-11-19 14:55:58 -08001105 write(fd, result.string(), result.length());
1106
1107 if (locked) {
1108 mLock.unlock();
1109 }
1110}
1111
1112// ----------------------------------------------------------------------------
1113// EffectHandle implementation
1114// ----------------------------------------------------------------------------
1115
1116#undef LOG_TAG
1117#define LOG_TAG "AudioFlinger::EffectHandle"
1118
1119AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1120 const sp<AudioFlinger::Client>& client,
1121 const sp<IEffectClient>& effectClient,
1122 int32_t priority)
1123 : BnEffect(),
1124 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1125 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1126{
1127 ALOGV("constructor %p", this);
1128
1129 if (client == 0) {
1130 return;
1131 }
1132 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1133 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001134 if (mCblkMemory == 0 ||
1135 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001136 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001137 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001138 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001139 return;
1140 }
Glenn Kastene75da402013-11-20 13:54:52 -08001141 new(mCblk) effect_param_cblk_t();
1142 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001143}
1144
1145AudioFlinger::EffectHandle::~EffectHandle()
1146{
1147 ALOGV("Destructor %p", this);
1148
1149 if (mEffect == 0) {
1150 mDestroyed = true;
1151 return;
1152 }
1153 mEffect->lock();
1154 mDestroyed = true;
1155 mEffect->unlock();
1156 disconnect(false);
1157}
1158
Glenn Kastene75da402013-11-20 13:54:52 -08001159status_t AudioFlinger::EffectHandle::initCheck()
1160{
1161 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1162}
1163
Eric Laurentca7cc822012-11-19 14:55:58 -08001164status_t AudioFlinger::EffectHandle::enable()
1165{
1166 ALOGV("enable %p", this);
1167 if (!mHasControl) {
1168 return INVALID_OPERATION;
1169 }
1170 if (mEffect == 0) {
1171 return DEAD_OBJECT;
1172 }
1173
1174 if (mEnabled) {
1175 return NO_ERROR;
1176 }
1177
1178 mEnabled = true;
1179
1180 sp<ThreadBase> thread = mEffect->thread().promote();
1181 if (thread != 0) {
1182 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1183 }
1184
1185 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1186 if (mEffect->suspended()) {
1187 return NO_ERROR;
1188 }
1189
1190 status_t status = mEffect->setEnabled(true);
1191 if (status != NO_ERROR) {
1192 if (thread != 0) {
1193 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1194 }
1195 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001196 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001197 if (thread != 0) {
1198 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001199 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001200 Mutex::Autolock _l(t->mLock);
1201 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001202 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001203 if (!mEffect->isOffloadable()) {
1204 if (thread->type() == ThreadBase::OFFLOAD) {
1205 PlaybackThread *t = (PlaybackThread *)thread.get();
1206 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1207 }
1208 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1209 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1210 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001211 }
1212 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001213 }
1214 return status;
1215}
1216
1217status_t AudioFlinger::EffectHandle::disable()
1218{
1219 ALOGV("disable %p", this);
1220 if (!mHasControl) {
1221 return INVALID_OPERATION;
1222 }
1223 if (mEffect == 0) {
1224 return DEAD_OBJECT;
1225 }
1226
1227 if (!mEnabled) {
1228 return NO_ERROR;
1229 }
1230 mEnabled = false;
1231
1232 if (mEffect->suspended()) {
1233 return NO_ERROR;
1234 }
1235
1236 status_t status = mEffect->setEnabled(false);
1237
1238 sp<ThreadBase> thread = mEffect->thread().promote();
1239 if (thread != 0) {
1240 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001241 if (thread->type() == ThreadBase::OFFLOAD) {
1242 PlaybackThread *t = (PlaybackThread *)thread.get();
1243 Mutex::Autolock _l(t->mLock);
1244 t->broadcast_l();
1245 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001246 }
1247
1248 return status;
1249}
1250
1251void AudioFlinger::EffectHandle::disconnect()
1252{
1253 disconnect(true);
1254}
1255
1256void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1257{
1258 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1259 if (mEffect == 0) {
1260 return;
1261 }
1262 // restore suspended effects if the disconnected handle was enabled and the last one.
1263 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1264 sp<ThreadBase> thread = mEffect->thread().promote();
1265 if (thread != 0) {
1266 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1267 }
1268 }
1269
1270 // release sp on module => module destructor can be called now
1271 mEffect.clear();
1272 if (mClient != 0) {
1273 if (mCblk != NULL) {
1274 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1275 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1276 }
1277 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001278 // Client destructor must run with AudioFlinger client mutex locked
1279 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001280 mClient.clear();
1281 }
1282}
1283
1284status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1285 uint32_t cmdSize,
1286 void *pCmdData,
1287 uint32_t *replySize,
1288 void *pReplyData)
1289{
1290 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1291 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1292
1293 // only get parameter command is permitted for applications not controlling the effect
1294 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1295 return INVALID_OPERATION;
1296 }
1297 if (mEffect == 0) {
1298 return DEAD_OBJECT;
1299 }
1300 if (mClient == 0) {
1301 return INVALID_OPERATION;
1302 }
1303
1304 // handle commands that are not forwarded transparently to effect engine
1305 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1306 // No need to trylock() here as this function is executed in the binder thread serving a
1307 // particular client process: no risk to block the whole media server process or mixer
1308 // threads if we are stuck here
1309 Mutex::Autolock _l(mCblk->lock);
1310 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1311 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1312 mCblk->serverIndex = 0;
1313 mCblk->clientIndex = 0;
1314 return BAD_VALUE;
1315 }
1316 status_t status = NO_ERROR;
1317 while (mCblk->serverIndex < mCblk->clientIndex) {
1318 int reply;
1319 uint32_t rsize = sizeof(int);
1320 int *p = (int *)(mBuffer + mCblk->serverIndex);
1321 int size = *p++;
1322 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1323 ALOGW("command(): invalid parameter block size");
1324 break;
1325 }
1326 effect_param_t *param = (effect_param_t *)p;
1327 if (param->psize == 0 || param->vsize == 0) {
1328 ALOGW("command(): null parameter or value size");
1329 mCblk->serverIndex += size;
1330 continue;
1331 }
1332 uint32_t psize = sizeof(effect_param_t) +
1333 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1334 param->vsize;
1335 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1336 psize,
1337 p,
1338 &rsize,
1339 &reply);
1340 // stop at first error encountered
1341 if (ret != NO_ERROR) {
1342 status = ret;
1343 *(int *)pReplyData = reply;
1344 break;
1345 } else if (reply != NO_ERROR) {
1346 *(int *)pReplyData = reply;
1347 break;
1348 }
1349 mCblk->serverIndex += size;
1350 }
1351 mCblk->serverIndex = 0;
1352 mCblk->clientIndex = 0;
1353 return status;
1354 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1355 *(int *)pReplyData = NO_ERROR;
1356 return enable();
1357 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1358 *(int *)pReplyData = NO_ERROR;
1359 return disable();
1360 }
1361
1362 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1363}
1364
1365void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1366{
1367 ALOGV("setControl %p control %d", this, hasControl);
1368
1369 mHasControl = hasControl;
1370 mEnabled = enabled;
1371
1372 if (signal && mEffectClient != 0) {
1373 mEffectClient->controlStatusChanged(hasControl);
1374 }
1375}
1376
1377void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1378 uint32_t cmdSize,
1379 void *pCmdData,
1380 uint32_t replySize,
1381 void *pReplyData)
1382{
1383 if (mEffectClient != 0) {
1384 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1385 }
1386}
1387
1388
1389
1390void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1391{
1392 if (mEffectClient != 0) {
1393 mEffectClient->enableStatusChanged(enabled);
1394 }
1395}
1396
1397status_t AudioFlinger::EffectHandle::onTransact(
1398 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1399{
1400 return BnEffect::onTransact(code, data, reply, flags);
1401}
1402
1403
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001404void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001405{
1406 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1407
Marco Nelissenb2208842014-02-07 14:00:50 -08001408 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001409 (mClient == 0) ? getpid_cached : mClient->pid(),
1410 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001411 mHasControl ? "yes" : "no",
1412 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001413 mCblk ? mCblk->clientIndex : 0,
1414 mCblk ? mCblk->serverIndex : 0
1415 );
1416
1417 if (locked) {
1418 mCblk->lock.unlock();
1419 }
1420}
1421
1422#undef LOG_TAG
1423#define LOG_TAG "AudioFlinger::EffectChain"
1424
1425AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001426 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1428 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001429 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001430{
1431 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1432 if (thread == NULL) {
1433 return;
1434 }
1435 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1436 thread->frameCount();
1437}
1438
1439AudioFlinger::EffectChain::~EffectChain()
1440{
1441 if (mOwnInBuffer) {
1442 delete mInBuffer;
1443 }
1444
1445}
1446
1447// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1448sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1449 effect_descriptor_t *descriptor)
1450{
1451 size_t size = mEffects.size();
1452
1453 for (size_t i = 0; i < size; i++) {
1454 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1455 return mEffects[i];
1456 }
1457 }
1458 return 0;
1459}
1460
1461// getEffectFromId_l() must be called with ThreadBase::mLock held
1462sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1463{
1464 size_t size = mEffects.size();
1465
1466 for (size_t i = 0; i < size; i++) {
1467 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1468 if (id == 0 || mEffects[i]->id() == id) {
1469 return mEffects[i];
1470 }
1471 }
1472 return 0;
1473}
1474
1475// getEffectFromType_l() must be called with ThreadBase::mLock held
1476sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1477 const effect_uuid_t *type)
1478{
1479 size_t size = mEffects.size();
1480
1481 for (size_t i = 0; i < size; i++) {
1482 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1483 return mEffects[i];
1484 }
1485 }
1486 return 0;
1487}
1488
1489void AudioFlinger::EffectChain::clearInputBuffer()
1490{
1491 Mutex::Autolock _l(mLock);
1492 sp<ThreadBase> thread = mThread.promote();
1493 if (thread == 0) {
1494 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1495 return;
1496 }
1497 clearInputBuffer_l(thread);
1498}
1499
1500// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001501void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001502{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001503 // TODO: This will change in the future, depending on multichannel
1504 // and sample format changes for effects.
1505 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1506 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001507 const size_t frameSize =
1508 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001509 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001510}
1511
1512// Must be called with EffectChain::mLock locked
1513void AudioFlinger::EffectChain::process_l()
1514{
1515 sp<ThreadBase> thread = mThread.promote();
1516 if (thread == 0) {
1517 ALOGW("process_l(): cannot promote mixer thread");
1518 return;
1519 }
1520 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1521 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001522 // never process effects when:
1523 // - on an OFFLOAD thread
1524 // - no more tracks are on the session and the effect tail has been rendered
1525 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001526 if (!isGlobalSession) {
1527 bool tracksOnSession = (trackCnt() != 0);
1528
1529 if (!tracksOnSession && mTailBufferCount == 0) {
1530 doProcess = false;
1531 }
1532
1533 if (activeTrackCnt() == 0) {
1534 // if no track is active and the effect tail has not been rendered,
1535 // the input buffer must be cleared here as the mixer process will not do it
1536 if (tracksOnSession || mTailBufferCount > 0) {
1537 clearInputBuffer_l(thread);
1538 if (mTailBufferCount > 0) {
1539 mTailBufferCount--;
1540 }
1541 }
1542 }
1543 }
1544
1545 size_t size = mEffects.size();
1546 if (doProcess) {
1547 for (size_t i = 0; i < size; i++) {
1548 mEffects[i]->process();
1549 }
1550 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001551 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001552 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001553 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1554 }
1555 if (doResetVolume) {
1556 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001557 }
1558}
1559
1560// addEffect_l() must be called with PlaybackThread::mLock held
1561status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1562{
1563 effect_descriptor_t desc = effect->desc();
1564 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1565
1566 Mutex::Autolock _l(mLock);
1567 effect->setChain(this);
1568 sp<ThreadBase> thread = mThread.promote();
1569 if (thread == 0) {
1570 return NO_INIT;
1571 }
1572 effect->setThread(thread);
1573
1574 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1575 // Auxiliary effects are inserted at the beginning of mEffects vector as
1576 // they are processed first and accumulated in chain input buffer
1577 mEffects.insertAt(effect, 0);
1578
1579 // the input buffer for auxiliary effect contains mono samples in
1580 // 32 bit format. This is to avoid saturation in AudoMixer
1581 // accumulation stage. Saturation is done in EffectModule::process() before
1582 // calling the process in effect engine
1583 size_t numSamples = thread->frameCount();
1584 int32_t *buffer = new int32_t[numSamples];
1585 memset(buffer, 0, numSamples * sizeof(int32_t));
1586 effect->setInBuffer((int16_t *)buffer);
1587 // auxiliary effects output samples to chain input buffer for further processing
1588 // by insert effects
1589 effect->setOutBuffer(mInBuffer);
1590 } else {
1591 // Insert effects are inserted at the end of mEffects vector as they are processed
1592 // after track and auxiliary effects.
1593 // Insert effect order as a function of indicated preference:
1594 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1595 // another effect is present
1596 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1597 // last effect claiming first position
1598 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1599 // first effect claiming last position
1600 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1601 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1602 // already present
1603
1604 size_t size = mEffects.size();
1605 size_t idx_insert = size;
1606 ssize_t idx_insert_first = -1;
1607 ssize_t idx_insert_last = -1;
1608
1609 for (size_t i = 0; i < size; i++) {
1610 effect_descriptor_t d = mEffects[i]->desc();
1611 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1612 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1613 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1614 // check invalid effect chaining combinations
1615 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1616 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1617 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1618 desc.name, d.name);
1619 return INVALID_OPERATION;
1620 }
1621 // remember position of first insert effect and by default
1622 // select this as insert position for new effect
1623 if (idx_insert == size) {
1624 idx_insert = i;
1625 }
1626 // remember position of last insert effect claiming
1627 // first position
1628 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1629 idx_insert_first = i;
1630 }
1631 // remember position of first insert effect claiming
1632 // last position
1633 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1634 idx_insert_last == -1) {
1635 idx_insert_last = i;
1636 }
1637 }
1638 }
1639
1640 // modify idx_insert from first position if needed
1641 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1642 if (idx_insert_last != -1) {
1643 idx_insert = idx_insert_last;
1644 } else {
1645 idx_insert = size;
1646 }
1647 } else {
1648 if (idx_insert_first != -1) {
1649 idx_insert = idx_insert_first + 1;
1650 }
1651 }
1652
1653 // always read samples from chain input buffer
1654 effect->setInBuffer(mInBuffer);
1655
1656 // if last effect in the chain, output samples to chain
1657 // output buffer, otherwise to chain input buffer
1658 if (idx_insert == size) {
1659 if (idx_insert != 0) {
1660 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1661 mEffects[idx_insert-1]->configure();
1662 }
1663 effect->setOutBuffer(mOutBuffer);
1664 } else {
1665 effect->setOutBuffer(mInBuffer);
1666 }
1667 mEffects.insertAt(effect, idx_insert);
1668
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001669 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001670 idx_insert);
1671 }
1672 effect->configure();
1673 return NO_ERROR;
1674}
1675
1676// removeEffect_l() must be called with PlaybackThread::mLock held
1677size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1678{
1679 Mutex::Autolock _l(mLock);
1680 size_t size = mEffects.size();
1681 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1682
1683 for (size_t i = 0; i < size; i++) {
1684 if (effect == mEffects[i]) {
1685 // calling stop here will remove pre-processing effect from the audio HAL.
1686 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1687 // the middle of a read from audio HAL
1688 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1689 mEffects[i]->state() == EffectModule::STOPPING) {
1690 mEffects[i]->stop();
1691 }
1692 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1693 delete[] effect->inBuffer();
1694 } else {
1695 if (i == size - 1 && i != 0) {
1696 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1697 mEffects[i - 1]->configure();
1698 }
1699 }
1700 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001701 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 this, i);
1703 break;
1704 }
1705 }
1706
1707 return mEffects.size();
1708}
1709
1710// setDevice_l() must be called with PlaybackThread::mLock held
1711void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1712{
1713 size_t size = mEffects.size();
1714 for (size_t i = 0; i < size; i++) {
1715 mEffects[i]->setDevice(device);
1716 }
1717}
1718
1719// setMode_l() must be called with PlaybackThread::mLock held
1720void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1721{
1722 size_t size = mEffects.size();
1723 for (size_t i = 0; i < size; i++) {
1724 mEffects[i]->setMode(mode);
1725 }
1726}
1727
1728// setAudioSource_l() must be called with PlaybackThread::mLock held
1729void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1730{
1731 size_t size = mEffects.size();
1732 for (size_t i = 0; i < size; i++) {
1733 mEffects[i]->setAudioSource(source);
1734 }
1735}
1736
Eric Laurentfa1e1232016-08-02 19:01:49 -07001737// setVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1738bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001739{
1740 uint32_t newLeft = *left;
1741 uint32_t newRight = *right;
1742 bool hasControl = false;
1743 int ctrlIdx = -1;
1744 size_t size = mEffects.size();
1745
1746 // first update volume controller
1747 for (size_t i = size; i > 0; i--) {
1748 if (mEffects[i - 1]->isProcessEnabled() &&
1749 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1750 ctrlIdx = i - 1;
1751 hasControl = true;
1752 break;
1753 }
1754 }
1755
Eric Laurentfa1e1232016-08-02 19:01:49 -07001756 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001757 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001758 if (hasControl) {
1759 *left = mNewLeftVolume;
1760 *right = mNewRightVolume;
1761 }
1762 return hasControl;
1763 }
1764
1765 mVolumeCtrlIdx = ctrlIdx;
1766 mLeftVolume = newLeft;
1767 mRightVolume = newRight;
1768
1769 // second get volume update from volume controller
1770 if (ctrlIdx >= 0) {
1771 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1772 mNewLeftVolume = newLeft;
1773 mNewRightVolume = newRight;
1774 }
1775 // then indicate volume to all other effects in chain.
1776 // Pass altered volume to effects before volume controller
1777 // and requested volume to effects after controller
1778 uint32_t lVol = newLeft;
1779 uint32_t rVol = newRight;
1780
1781 for (size_t i = 0; i < size; i++) {
1782 if ((int)i == ctrlIdx) {
1783 continue;
1784 }
1785 // this also works for ctrlIdx == -1 when there is no volume controller
1786 if ((int)i > ctrlIdx) {
1787 lVol = *left;
1788 rVol = *right;
1789 }
1790 mEffects[i]->setVolume(&lVol, &rVol, false);
1791 }
1792 *left = newLeft;
1793 *right = newRight;
1794
1795 return hasControl;
1796}
1797
Eric Laurentfa1e1232016-08-02 19:01:49 -07001798// resetVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1799void AudioFlinger::EffectChain::resetVolume_l()
1800{
Eric Laurente7449bf2016-08-03 18:44:07 -07001801 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1802 uint32_t left = mLeftVolume;
1803 uint32_t right = mRightVolume;
1804 (void)setVolume_l(&left, &right, true);
1805 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001806}
1807
Eric Laurent1b928682014-10-02 19:41:47 -07001808void AudioFlinger::EffectChain::syncHalEffectsState()
1809{
1810 Mutex::Autolock _l(mLock);
1811 for (size_t i = 0; i < mEffects.size(); i++) {
1812 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1813 mEffects[i]->state() == EffectModule::STOPPING) {
1814 mEffects[i]->addEffectToHal_l();
1815 }
1816 }
1817}
1818
Eric Laurentca7cc822012-11-19 14:55:58 -08001819void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1820{
1821 const size_t SIZE = 256;
1822 char buffer[SIZE];
1823 String8 result;
1824
Marco Nelissenb2208842014-02-07 14:00:50 -08001825 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001826 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001827 result.append(buffer);
1828
Marco Nelissenb2208842014-02-07 14:00:50 -08001829 if (numEffects) {
1830 bool locked = AudioFlinger::dumpTryLock(mLock);
1831 // failed to lock - AudioFlinger is probably deadlocked
1832 if (!locked) {
1833 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001834 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001835
Marco Nelissenb2208842014-02-07 14:00:50 -08001836 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001837 snprintf(buffer, SIZE, "\t%p %p %d\n",
1838 mInBuffer,
1839 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001840 mActiveTrackCnt);
1841 result.append(buffer);
1842 write(fd, result.string(), result.size());
1843
1844 for (size_t i = 0; i < numEffects; ++i) {
1845 sp<EffectModule> effect = mEffects[i];
1846 if (effect != 0) {
1847 effect->dump(fd, args);
1848 }
1849 }
1850
1851 if (locked) {
1852 mLock.unlock();
1853 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001854 }
1855}
1856
1857// must be called with ThreadBase::mLock held
1858void AudioFlinger::EffectChain::setEffectSuspended_l(
1859 const effect_uuid_t *type, bool suspend)
1860{
1861 sp<SuspendedEffectDesc> desc;
1862 // use effect type UUID timelow as key as there is no real risk of identical
1863 // timeLow fields among effect type UUIDs.
1864 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1865 if (suspend) {
1866 if (index >= 0) {
1867 desc = mSuspendedEffects.valueAt(index);
1868 } else {
1869 desc = new SuspendedEffectDesc();
1870 desc->mType = *type;
1871 mSuspendedEffects.add(type->timeLow, desc);
1872 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1873 }
1874 if (desc->mRefCount++ == 0) {
1875 sp<EffectModule> effect = getEffectIfEnabled(type);
1876 if (effect != 0) {
1877 desc->mEffect = effect;
1878 effect->setSuspended(true);
1879 effect->setEnabled(false);
1880 }
1881 }
1882 } else {
1883 if (index < 0) {
1884 return;
1885 }
1886 desc = mSuspendedEffects.valueAt(index);
1887 if (desc->mRefCount <= 0) {
1888 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1889 desc->mRefCount = 1;
1890 }
1891 if (--desc->mRefCount == 0) {
1892 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1893 if (desc->mEffect != 0) {
1894 sp<EffectModule> effect = desc->mEffect.promote();
1895 if (effect != 0) {
1896 effect->setSuspended(false);
1897 effect->lock();
1898 EffectHandle *handle = effect->controlHandle_l();
1899 if (handle != NULL && !handle->destroyed_l()) {
1900 effect->setEnabled_l(handle->enabled());
1901 }
1902 effect->unlock();
1903 }
1904 desc->mEffect.clear();
1905 }
1906 mSuspendedEffects.removeItemsAt(index);
1907 }
1908 }
1909}
1910
1911// must be called with ThreadBase::mLock held
1912void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1913{
1914 sp<SuspendedEffectDesc> desc;
1915
1916 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1917 if (suspend) {
1918 if (index >= 0) {
1919 desc = mSuspendedEffects.valueAt(index);
1920 } else {
1921 desc = new SuspendedEffectDesc();
1922 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1923 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1924 }
1925 if (desc->mRefCount++ == 0) {
1926 Vector< sp<EffectModule> > effects;
1927 getSuspendEligibleEffects(effects);
1928 for (size_t i = 0; i < effects.size(); i++) {
1929 setEffectSuspended_l(&effects[i]->desc().type, true);
1930 }
1931 }
1932 } else {
1933 if (index < 0) {
1934 return;
1935 }
1936 desc = mSuspendedEffects.valueAt(index);
1937 if (desc->mRefCount <= 0) {
1938 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1939 desc->mRefCount = 1;
1940 }
1941 if (--desc->mRefCount == 0) {
1942 Vector<const effect_uuid_t *> types;
1943 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1944 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1945 continue;
1946 }
1947 types.add(&mSuspendedEffects.valueAt(i)->mType);
1948 }
1949 for (size_t i = 0; i < types.size(); i++) {
1950 setEffectSuspended_l(types[i], false);
1951 }
1952 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1953 mSuspendedEffects.keyAt(index));
1954 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1955 }
1956 }
1957}
1958
1959
1960// The volume effect is used for automated tests only
1961#ifndef OPENSL_ES_H_
1962static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1963 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1964const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1965#endif //OPENSL_ES_H_
1966
1967bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1968{
1969 // auxiliary effects and visualizer are never suspended on output mix
1970 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1971 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1972 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1973 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1974 return false;
1975 }
1976 return true;
1977}
1978
1979void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1980 Vector< sp<AudioFlinger::EffectModule> > &effects)
1981{
1982 effects.clear();
1983 for (size_t i = 0; i < mEffects.size(); i++) {
1984 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1985 effects.add(mEffects[i]);
1986 }
1987 }
1988}
1989
1990sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1991 const effect_uuid_t *type)
1992{
1993 sp<EffectModule> effect = getEffectFromType_l(type);
1994 return effect != 0 && effect->isEnabled() ? effect : 0;
1995}
1996
1997void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1998 bool enabled)
1999{
2000 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2001 if (enabled) {
2002 if (index < 0) {
2003 // if the effect is not suspend check if all effects are suspended
2004 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2005 if (index < 0) {
2006 return;
2007 }
2008 if (!isEffectEligibleForSuspend(effect->desc())) {
2009 return;
2010 }
2011 setEffectSuspended_l(&effect->desc().type, enabled);
2012 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2013 if (index < 0) {
2014 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2015 return;
2016 }
2017 }
2018 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2019 effect->desc().type.timeLow);
2020 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2021 // if effect is requested to suspended but was not yet enabled, supend it now.
2022 if (desc->mEffect == 0) {
2023 desc->mEffect = effect;
2024 effect->setEnabled(false);
2025 effect->setSuspended(true);
2026 }
2027 } else {
2028 if (index < 0) {
2029 return;
2030 }
2031 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2032 effect->desc().type.timeLow);
2033 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2034 desc->mEffect.clear();
2035 effect->setSuspended(false);
2036 }
2037}
2038
Eric Laurent5baf2af2013-09-12 17:37:00 -07002039bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002040{
2041 Mutex::Autolock _l(mLock);
2042 size_t size = mEffects.size();
2043 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002044 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002045 return true;
2046 }
2047 }
2048 return false;
2049}
2050
Eric Laurentaaa44472014-09-12 17:41:50 -07002051void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2052{
2053 Mutex::Autolock _l(mLock);
2054 mThread = thread;
2055 for (size_t i = 0; i < mEffects.size(); i++) {
2056 mEffects[i]->setThread(thread);
2057 }
2058}
2059
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002060void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2061{
2062 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2063 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2064 }
2065 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2066 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2067 }
2068}
2069
2070void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2071{
2072 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2073 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2074 }
2075 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2076 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2077 }
2078}
2079
2080bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002081{
2082 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002083 for (const auto &effect : mEffects) {
2084 if (effect->isProcessImplemented()) {
2085 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002086 }
2087 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002088 // Allow effects without processing.
2089 return true;
2090}
2091
2092bool AudioFlinger::EffectChain::isFastCompatible() const
2093{
2094 Mutex::Autolock _l(mLock);
2095 for (const auto &effect : mEffects) {
2096 if (effect->isProcessImplemented()
2097 && effect->isImplementationSoftware()) {
2098 return false;
2099 }
2100 }
2101 // Allow effects without processing or hw accelerated effects.
2102 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002103}
2104
2105// isCompatibleWithThread_l() must be called with thread->mLock held
2106bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2107{
2108 Mutex::Autolock _l(mLock);
2109 for (size_t i = 0; i < mEffects.size(); i++) {
2110 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2111 return false;
2112 }
2113 }
2114 return true;
2115}
2116
Glenn Kasten63238ef2015-03-02 15:50:29 -08002117} // namespace android