blob: f87b8f5721e35b31d3935b06b07ba6d19b2756f2 [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,
62 int sessionId)
63 : 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 }
141 ALOGV("addHandle() %p added handle %p in position %d", this, handle, i);
142 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 }
159 ALOGV("removeHandle() %p removed handle %p in position %d", this, handle, i);
160
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
219void AudioFlinger::EffectModule::updateState() {
220 Mutex::Autolock _l(mLock);
221
222 switch (mState) {
223 case RESTART:
224 reset_l();
225 // FALL THROUGH
226
227 case STARTING:
228 // clear auxiliary effect input buffer for next accumulation
229 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
230 memset(mConfig.inputCfg.buffer.raw,
231 0,
232 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
233 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700234 if (start_l() == NO_ERROR) {
235 mState = ACTIVE;
236 } else {
237 mState = IDLE;
238 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800239 break;
240 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700241 if (stop_l() == NO_ERROR) {
242 mDisableWaitCnt = mMaxDisableWaitCnt;
243 } else {
244 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
245 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800246 mState = STOPPED;
247 break;
248 case STOPPED:
249 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
250 // turn off sequence.
251 if (--mDisableWaitCnt == 0) {
252 reset_l();
253 mState = IDLE;
254 }
255 break;
256 default: //IDLE , ACTIVE, DESTROYED
257 break;
258 }
259}
260
261void AudioFlinger::EffectModule::process()
262{
263 Mutex::Autolock _l(mLock);
264
265 if (mState == DESTROYED || mEffectInterface == NULL ||
266 mConfig.inputCfg.buffer.raw == NULL ||
267 mConfig.outputCfg.buffer.raw == NULL) {
268 return;
269 }
270
271 if (isProcessEnabled()) {
272 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
273 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
274 ditherAndClamp(mConfig.inputCfg.buffer.s32,
275 mConfig.inputCfg.buffer.s32,
276 mConfig.inputCfg.buffer.frameCount/2);
277 }
278
279 // do the actual processing in the effect engine
280 int ret = (*mEffectInterface)->process(mEffectInterface,
281 &mConfig.inputCfg.buffer,
282 &mConfig.outputCfg.buffer);
283
284 // force transition to IDLE state when engine is ready
285 if (mState == STOPPED && ret == -ENODATA) {
286 mDisableWaitCnt = 1;
287 }
288
289 // clear auxiliary effect input buffer for next accumulation
290 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
291 memset(mConfig.inputCfg.buffer.raw, 0,
292 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
293 }
294 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
295 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
296 // If an insert effect is idle and input buffer is different from output buffer,
297 // accumulate input onto output
298 sp<EffectChain> chain = mChain.promote();
299 if (chain != 0 && chain->activeTrackCnt() != 0) {
300 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
301 int16_t *in = mConfig.inputCfg.buffer.s16;
302 int16_t *out = mConfig.outputCfg.buffer.s16;
303 for (size_t i = 0; i < frameCnt; i++) {
304 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
305 }
306 }
307 }
308}
309
310void AudioFlinger::EffectModule::reset_l()
311{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700312 if (mStatus != NO_ERROR || mEffectInterface == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800313 return;
314 }
315 (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
316}
317
318status_t AudioFlinger::EffectModule::configure()
319{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700320 status_t status;
321 sp<ThreadBase> thread;
322 uint32_t size;
323 audio_channel_mask_t channelMask;
324
Eric Laurentca7cc822012-11-19 14:55:58 -0800325 if (mEffectInterface == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700326 status = NO_INIT;
327 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800328 }
329
Eric Laurentd0ebb532013-04-02 16:41:41 -0700330 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800331 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700332 status = DEAD_OBJECT;
333 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800334 }
335
336 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700337 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700338 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800339
340 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
341 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
342 } else {
343 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700344 // TODO: Update this logic when multichannel effects are implemented.
345 // For offloaded tracks consider mono output as stereo for proper effect initialization
346 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
347 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
348 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
349 ALOGV("Overriding effect input and output as STEREO");
350 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800351 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700352
Eric Laurentca7cc822012-11-19 14:55:58 -0800353 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
354 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
355 mConfig.inputCfg.samplingRate = thread->sampleRate();
356 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
357 mConfig.inputCfg.bufferProvider.cookie = NULL;
358 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
359 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
360 mConfig.outputCfg.bufferProvider.cookie = NULL;
361 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
362 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
363 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
364 // Insert effect:
365 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
366 // always overwrites output buffer: input buffer == output buffer
367 // - in other sessions:
368 // last effect in the chain accumulates in output buffer: input buffer != output buffer
369 // other effect: overwrites output buffer: input buffer == output buffer
370 // Auxiliary effect:
371 // accumulates in output buffer: input buffer != output buffer
372 // Therefore: accumulate <=> input buffer != output buffer
373 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
374 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
375 } else {
376 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
377 }
378 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
379 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
380 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
381 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
382
383 ALOGV("configure() %p thread %p buffer %p framecount %d",
384 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
385
386 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700387 size = sizeof(int);
388 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800389 EFFECT_CMD_SET_CONFIG,
390 sizeof(effect_config_t),
391 &mConfig,
392 &size,
393 &cmdStatus);
394 if (status == 0) {
395 status = cmdStatus;
396 }
397
398 if (status == 0 &&
399 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
400 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
401 effect_param_t *p = (effect_param_t *)buf32;
402
403 p->psize = sizeof(uint32_t);
404 p->vsize = sizeof(uint32_t);
405 size = sizeof(int);
406 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
407
408 uint32_t latency = 0;
409 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
410 if (pbt != NULL) {
411 latency = pbt->latency_l();
412 }
413
414 *((int32_t *)p->data + 1)= latency;
415 (*mEffectInterface)->command(mEffectInterface,
416 EFFECT_CMD_SET_PARAM,
417 sizeof(effect_param_t) + 8,
418 &buf32,
419 &size,
420 &cmdStatus);
421 }
422
423 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
424 (1000 * mConfig.outputCfg.buffer.frameCount);
425
Eric Laurentd0ebb532013-04-02 16:41:41 -0700426exit:
427 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800428 return status;
429}
430
431status_t AudioFlinger::EffectModule::init()
432{
433 Mutex::Autolock _l(mLock);
434 if (mEffectInterface == NULL) {
435 return NO_INIT;
436 }
437 status_t cmdStatus;
438 uint32_t size = sizeof(status_t);
439 status_t status = (*mEffectInterface)->command(mEffectInterface,
440 EFFECT_CMD_INIT,
441 0,
442 NULL,
443 &size,
444 &cmdStatus);
445 if (status == 0) {
446 status = cmdStatus;
447 }
448 return status;
449}
450
Eric Laurent1b928682014-10-02 19:41:47 -0700451void AudioFlinger::EffectModule::addEffectToHal_l()
452{
453 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
454 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
455 sp<ThreadBase> thread = mThread.promote();
456 if (thread != 0) {
457 audio_stream_t *stream = thread->stream();
458 if (stream != NULL) {
459 stream->add_audio_effect(stream, mEffectInterface);
460 }
461 }
462 }
463}
464
Eric Laurentca7cc822012-11-19 14:55:58 -0800465status_t AudioFlinger::EffectModule::start()
466{
467 Mutex::Autolock _l(mLock);
468 return start_l();
469}
470
471status_t AudioFlinger::EffectModule::start_l()
472{
473 if (mEffectInterface == NULL) {
474 return NO_INIT;
475 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700476 if (mStatus != NO_ERROR) {
477 return mStatus;
478 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800479 status_t cmdStatus;
480 uint32_t size = sizeof(status_t);
481 status_t status = (*mEffectInterface)->command(mEffectInterface,
482 EFFECT_CMD_ENABLE,
483 0,
484 NULL,
485 &size,
486 &cmdStatus);
487 if (status == 0) {
488 status = cmdStatus;
489 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700490 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700491 addEffectToHal_l();
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700492 sp<EffectChain> chain = mChain.promote();
493 if (chain != 0) {
494 chain->forceVolume();
495 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800496 }
497 return status;
498}
499
500status_t AudioFlinger::EffectModule::stop()
501{
502 Mutex::Autolock _l(mLock);
503 return stop_l();
504}
505
506status_t AudioFlinger::EffectModule::stop_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 Laurentbfb1b832013-01-07 09:53:42 -0800514 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800515 uint32_t size = sizeof(status_t);
516 status_t status = (*mEffectInterface)->command(mEffectInterface,
517 EFFECT_CMD_DISABLE,
518 0,
519 NULL,
520 &size,
521 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800522 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800523 status = cmdStatus;
524 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800525 if (status == NO_ERROR) {
526 status = remove_effect_from_hal_l();
527 }
528 return status;
529}
530
531status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
532{
533 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
534 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800535 sp<ThreadBase> thread = mThread.promote();
536 if (thread != 0) {
537 audio_stream_t *stream = thread->stream();
538 if (stream != NULL) {
539 stream->remove_audio_effect(stream, mEffectInterface);
540 }
541 }
542 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800543 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800544}
545
Andy Hunge4a1d912016-08-17 14:11:13 -0700546// round up delta valid if value and divisor are positive.
547template <typename T>
548static T roundUpDelta(const T &value, const T &divisor) {
549 T remainder = value % divisor;
550 return remainder == 0 ? 0 : divisor - remainder;
551}
552
Eric Laurentca7cc822012-11-19 14:55:58 -0800553status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
554 uint32_t cmdSize,
555 void *pCmdData,
556 uint32_t *replySize,
557 void *pReplyData)
558{
559 Mutex::Autolock _l(mLock);
560 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
561
562 if (mState == DESTROYED || mEffectInterface == NULL) {
563 return NO_INIT;
564 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700565 if (mStatus != NO_ERROR) {
566 return mStatus;
567 }
Andy Hung110bc952016-06-20 15:22:52 -0700568 if (cmdCode == EFFECT_CMD_GET_PARAM &&
569 (*replySize < sizeof(effect_param_t) ||
570 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
571 android_errorWriteLog(0x534e4554, "29251553");
572 return -EINVAL;
573 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700574 if ((cmdCode == EFFECT_CMD_SET_PARAM
575 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
576 (sizeof(effect_param_t) > cmdSize
577 || ((effect_param_t *)pCmdData)->psize > cmdSize
578 - sizeof(effect_param_t)
579 || ((effect_param_t *)pCmdData)->vsize > cmdSize
580 - sizeof(effect_param_t)
581 - ((effect_param_t *)pCmdData)->psize
582 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
583 cmdSize
584 - sizeof(effect_param_t)
585 - ((effect_param_t *)pCmdData)->psize
586 - ((effect_param_t *)pCmdData)->vsize)) {
587 android_errorWriteLog(0x534e4554, "30204301");
588 return -EINVAL;
589 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800590 status_t status = (*mEffectInterface)->command(mEffectInterface,
591 cmdCode,
592 cmdSize,
593 pCmdData,
594 replySize,
595 pReplyData);
596 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
597 uint32_t size = (replySize == NULL) ? 0 : *replySize;
598 for (size_t i = 1; i < mHandles.size(); i++) {
599 EffectHandle *h = mHandles[i];
600 if (h != NULL && !h->destroyed_l()) {
601 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
602 }
603 }
604 }
605 return status;
606}
607
608status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
609{
610 Mutex::Autolock _l(mLock);
611 return setEnabled_l(enabled);
612}
613
614// must be called with EffectModule::mLock held
615status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
616{
617
618 ALOGV("setEnabled %p enabled %d", this, enabled);
619
620 if (enabled != isEnabled()) {
621 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
622 if (enabled && status != NO_ERROR) {
623 return status;
624 }
625
626 switch (mState) {
627 // going from disabled to enabled
628 case IDLE:
629 mState = STARTING;
630 break;
631 case STOPPED:
632 mState = RESTART;
633 break;
634 case STOPPING:
635 mState = ACTIVE;
636 break;
637
638 // going from enabled to disabled
639 case RESTART:
640 mState = STOPPED;
641 break;
642 case STARTING:
643 mState = IDLE;
644 break;
645 case ACTIVE:
646 mState = STOPPING;
647 break;
648 case DESTROYED:
649 return NO_ERROR; // simply ignore as we are being destroyed
650 }
651 for (size_t i = 1; i < mHandles.size(); i++) {
652 EffectHandle *h = mHandles[i];
653 if (h != NULL && !h->destroyed_l()) {
654 h->setEnabled(enabled);
655 }
656 }
657 }
658 return NO_ERROR;
659}
660
661bool AudioFlinger::EffectModule::isEnabled() const
662{
663 switch (mState) {
664 case RESTART:
665 case STARTING:
666 case ACTIVE:
667 return true;
668 case IDLE:
669 case STOPPING:
670 case STOPPED:
671 case DESTROYED:
672 default:
673 return false;
674 }
675}
676
677bool AudioFlinger::EffectModule::isProcessEnabled() const
678{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700679 if (mStatus != NO_ERROR) {
680 return false;
681 }
682
Eric Laurentca7cc822012-11-19 14:55:58 -0800683 switch (mState) {
684 case RESTART:
685 case ACTIVE:
686 case STOPPING:
687 case STOPPED:
688 return true;
689 case IDLE:
690 case STARTING:
691 case DESTROYED:
692 default:
693 return false;
694 }
695}
696
697status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
698{
699 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700700 if (mStatus != NO_ERROR) {
701 return mStatus;
702 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800703 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800704 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
705 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
706 if (isProcessEnabled() &&
707 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
708 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
709 status_t cmdStatus;
710 uint32_t volume[2];
711 uint32_t *pVolume = NULL;
712 uint32_t size = sizeof(volume);
713 volume[0] = *left;
714 volume[1] = *right;
715 if (controller) {
716 pVolume = volume;
717 }
718 status = (*mEffectInterface)->command(mEffectInterface,
719 EFFECT_CMD_SET_VOLUME,
720 size,
721 volume,
722 &size,
723 pVolume);
724 if (controller && status == NO_ERROR && size == sizeof(volume)) {
725 *left = volume[0];
726 *right = volume[1];
727 }
728 }
729 return status;
730}
731
732status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
733{
734 if (device == AUDIO_DEVICE_NONE) {
735 return NO_ERROR;
736 }
737
738 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700739 if (mStatus != NO_ERROR) {
740 return mStatus;
741 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800742 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700743 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800744 status_t cmdStatus;
745 uint32_t size = sizeof(status_t);
746 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
747 EFFECT_CMD_SET_INPUT_DEVICE;
748 status = (*mEffectInterface)->command(mEffectInterface,
749 cmd,
750 sizeof(uint32_t),
751 &device,
752 &size,
753 &cmdStatus);
754 }
755 return status;
756}
757
758status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
759{
760 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700761 if (mStatus != NO_ERROR) {
762 return mStatus;
763 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800764 status_t status = NO_ERROR;
765 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
766 status_t cmdStatus;
767 uint32_t size = sizeof(status_t);
768 status = (*mEffectInterface)->command(mEffectInterface,
769 EFFECT_CMD_SET_AUDIO_MODE,
770 sizeof(audio_mode_t),
771 &mode,
772 &size,
773 &cmdStatus);
774 if (status == NO_ERROR) {
775 status = cmdStatus;
776 }
777 }
778 return status;
779}
780
781status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
782{
783 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700784 if (mStatus != NO_ERROR) {
785 return mStatus;
786 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800787 status_t status = NO_ERROR;
788 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
789 uint32_t size = 0;
790 status = (*mEffectInterface)->command(mEffectInterface,
791 EFFECT_CMD_SET_AUDIO_SOURCE,
792 sizeof(audio_source_t),
793 &source,
794 &size,
795 NULL);
796 }
797 return status;
798}
799
800void AudioFlinger::EffectModule::setSuspended(bool suspended)
801{
802 Mutex::Autolock _l(mLock);
803 mSuspended = suspended;
804}
805
806bool AudioFlinger::EffectModule::suspended() const
807{
808 Mutex::Autolock _l(mLock);
809 return mSuspended;
810}
811
812bool AudioFlinger::EffectModule::purgeHandles()
813{
814 bool enabled = false;
815 Mutex::Autolock _l(mLock);
816 for (size_t i = 0; i < mHandles.size(); i++) {
817 EffectHandle *handle = mHandles[i];
818 if (handle != NULL && !handle->destroyed_l()) {
819 handle->effect().clear();
820 if (handle->hasControl()) {
821 enabled = handle->enabled();
822 }
823 }
824 }
825 return enabled;
826}
827
Eric Laurent5baf2af2013-09-12 17:37:00 -0700828status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
829{
830 Mutex::Autolock _l(mLock);
831 if (mStatus != NO_ERROR) {
832 return mStatus;
833 }
834 status_t status = NO_ERROR;
835 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
836 status_t cmdStatus;
837 uint32_t size = sizeof(status_t);
838 effect_offload_param_t cmd;
839
840 cmd.isOffload = offloaded;
841 cmd.ioHandle = io;
842 status = (*mEffectInterface)->command(mEffectInterface,
843 EFFECT_CMD_OFFLOAD,
844 sizeof(effect_offload_param_t),
845 &cmd,
846 &size,
847 &cmdStatus);
848 if (status == NO_ERROR) {
849 status = cmdStatus;
850 }
851 mOffloaded = (status == NO_ERROR) ? offloaded : false;
852 } else {
853 if (offloaded) {
854 status = INVALID_OPERATION;
855 }
856 mOffloaded = false;
857 }
858 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
859 return status;
860}
861
862bool AudioFlinger::EffectModule::isOffloaded() const
863{
864 Mutex::Autolock _l(mLock);
865 return mOffloaded;
866}
867
Marco Nelissenb2208842014-02-07 14:00:50 -0800868String8 effectFlagsToString(uint32_t flags) {
869 String8 s;
870
871 s.append("conn. mode: ");
872 switch (flags & EFFECT_FLAG_TYPE_MASK) {
873 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
874 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
875 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
876 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
877 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
878 default: s.append("unknown/reserved"); break;
879 }
880 s.append(", ");
881
882 s.append("insert pref: ");
883 switch (flags & EFFECT_FLAG_INSERT_MASK) {
884 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
885 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
886 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
887 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
888 default: s.append("unknown/reserved"); break;
889 }
890 s.append(", ");
891
892 s.append("volume mgmt: ");
893 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
894 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
895 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
896 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
897 default: s.append("unknown/reserved"); break;
898 }
899 s.append(", ");
900
901 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
902 if (devind) {
903 s.append("device indication: ");
904 switch (devind) {
905 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
906 default: s.append("unknown/reserved"); break;
907 }
908 s.append(", ");
909 }
910
911 s.append("input mode: ");
912 switch (flags & EFFECT_FLAG_INPUT_MASK) {
913 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
914 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
915 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
916 default: s.append("not set"); break;
917 }
918 s.append(", ");
919
920 s.append("output mode: ");
921 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
922 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
923 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
924 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
925 default: s.append("not set"); break;
926 }
927 s.append(", ");
928
929 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
930 if (accel) {
931 s.append("hardware acceleration: ");
932 switch (accel) {
933 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
934 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
935 default: s.append("unknown/reserved"); break;
936 }
937 s.append(", ");
938 }
939
940 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
941 if (modeind) {
942 s.append("mode indication: ");
943 switch (modeind) {
944 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
945 default: s.append("unknown/reserved"); break;
946 }
947 s.append(", ");
948 }
949
950 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
951 if (srcind) {
952 s.append("source indication: ");
953 switch (srcind) {
954 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
955 default: s.append("unknown/reserved"); break;
956 }
957 s.append(", ");
958 }
959
960 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
961 s.append("offloadable, ");
962 }
963
964 int len = s.length();
965 if (s.length() > 2) {
966 char *str = s.lockBuffer(len);
967 s.unlockBuffer(len - 2);
968 }
969 return s;
970}
971
972
Glenn Kasten0f11b512014-01-31 16:18:54 -0800973void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -0800974{
975 const size_t SIZE = 256;
976 char buffer[SIZE];
977 String8 result;
978
979 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
980 result.append(buffer);
981
982 bool locked = AudioFlinger::dumpTryLock(mLock);
983 // failed to lock - AudioFlinger is probably deadlocked
984 if (!locked) {
985 result.append("\t\tCould not lock Fx mutex:\n");
986 }
987
988 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000989 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
990 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -0800991 result.append(buffer);
992
993 result.append("\t\tDescriptor:\n");
994 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
995 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
996 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
997 mDescriptor.uuid.node[2],
998 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
999 result.append(buffer);
1000 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1001 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1002 mDescriptor.type.timeHiAndVersion,
1003 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1004 mDescriptor.type.node[2],
1005 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1006 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001007 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001008 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001009 mDescriptor.flags,
1010 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001011 result.append(buffer);
1012 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1013 mDescriptor.name);
1014 result.append(buffer);
1015 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1016 mDescriptor.implementor);
1017 result.append(buffer);
1018
1019 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001020 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001021 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001022 mConfig.inputCfg.buffer.frameCount,
1023 mConfig.inputCfg.samplingRate,
1024 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001025 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001026 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001027 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001028 result.append(buffer);
1029
1030 result.append("\t\t- Output configuration:\n");
1031 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001032 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001033 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 mConfig.outputCfg.buffer.frameCount,
1035 mConfig.outputCfg.samplingRate,
1036 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001037 mConfig.outputCfg.format,
1038 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001039 result.append(buffer);
1040
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001041 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001042 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001043 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001044 for (size_t i = 0; i < mHandles.size(); ++i) {
1045 EffectHandle *handle = mHandles[i];
1046 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001047 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001048 result.append(buffer);
1049 }
1050 }
1051
Eric Laurentca7cc822012-11-19 14:55:58 -08001052 write(fd, result.string(), result.length());
1053
1054 if (locked) {
1055 mLock.unlock();
1056 }
1057}
1058
1059// ----------------------------------------------------------------------------
1060// EffectHandle implementation
1061// ----------------------------------------------------------------------------
1062
1063#undef LOG_TAG
1064#define LOG_TAG "AudioFlinger::EffectHandle"
1065
1066AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1067 const sp<AudioFlinger::Client>& client,
1068 const sp<IEffectClient>& effectClient,
1069 int32_t priority)
1070 : BnEffect(),
1071 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1072 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1073{
1074 ALOGV("constructor %p", this);
1075
1076 if (client == 0) {
1077 return;
1078 }
1079 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1080 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001081 if (mCblkMemory == 0 ||
1082 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001083 ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE +
1084 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001085 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001086 return;
1087 }
Glenn Kastene75da402013-11-20 13:54:52 -08001088 new(mCblk) effect_param_cblk_t();
1089 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001090}
1091
1092AudioFlinger::EffectHandle::~EffectHandle()
1093{
1094 ALOGV("Destructor %p", this);
1095
1096 if (mEffect == 0) {
1097 mDestroyed = true;
1098 return;
1099 }
1100 mEffect->lock();
1101 mDestroyed = true;
1102 mEffect->unlock();
1103 disconnect(false);
1104}
1105
Glenn Kastene75da402013-11-20 13:54:52 -08001106status_t AudioFlinger::EffectHandle::initCheck()
1107{
1108 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1109}
1110
Eric Laurentca7cc822012-11-19 14:55:58 -08001111status_t AudioFlinger::EffectHandle::enable()
1112{
1113 ALOGV("enable %p", this);
1114 if (!mHasControl) {
1115 return INVALID_OPERATION;
1116 }
1117 if (mEffect == 0) {
1118 return DEAD_OBJECT;
1119 }
1120
1121 if (mEnabled) {
1122 return NO_ERROR;
1123 }
1124
1125 mEnabled = true;
1126
1127 sp<ThreadBase> thread = mEffect->thread().promote();
1128 if (thread != 0) {
1129 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1130 }
1131
1132 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1133 if (mEffect->suspended()) {
1134 return NO_ERROR;
1135 }
1136
1137 status_t status = mEffect->setEnabled(true);
1138 if (status != NO_ERROR) {
1139 if (thread != 0) {
1140 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1141 }
1142 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001143 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001144 if (thread != 0) {
1145 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001146 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001147 Mutex::Autolock _l(t->mLock);
1148 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001149 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001150 if (!mEffect->isOffloadable()) {
1151 if (thread->type() == ThreadBase::OFFLOAD) {
1152 PlaybackThread *t = (PlaybackThread *)thread.get();
1153 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1154 }
1155 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1156 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1157 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001158 }
1159 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001160 }
1161 return status;
1162}
1163
1164status_t AudioFlinger::EffectHandle::disable()
1165{
1166 ALOGV("disable %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 mEnabled = false;
1178
1179 if (mEffect->suspended()) {
1180 return NO_ERROR;
1181 }
1182
1183 status_t status = mEffect->setEnabled(false);
1184
1185 sp<ThreadBase> thread = mEffect->thread().promote();
1186 if (thread != 0) {
1187 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001188 if (thread->type() == ThreadBase::OFFLOAD) {
1189 PlaybackThread *t = (PlaybackThread *)thread.get();
1190 Mutex::Autolock _l(t->mLock);
1191 t->broadcast_l();
1192 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001193 }
1194
1195 return status;
1196}
1197
1198void AudioFlinger::EffectHandle::disconnect()
1199{
1200 disconnect(true);
1201}
1202
1203void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1204{
1205 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1206 if (mEffect == 0) {
1207 return;
1208 }
1209 // restore suspended effects if the disconnected handle was enabled and the last one.
1210 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1211 sp<ThreadBase> thread = mEffect->thread().promote();
1212 if (thread != 0) {
1213 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1214 }
1215 }
1216
1217 // release sp on module => module destructor can be called now
1218 mEffect.clear();
1219 if (mClient != 0) {
1220 if (mCblk != NULL) {
1221 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1222 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1223 }
1224 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001225 // Client destructor must run with AudioFlinger client mutex locked
1226 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001227 mClient.clear();
1228 }
1229}
1230
1231status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1232 uint32_t cmdSize,
1233 void *pCmdData,
1234 uint32_t *replySize,
1235 void *pReplyData)
1236{
1237 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1238 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1239
1240 // only get parameter command is permitted for applications not controlling the effect
1241 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1242 return INVALID_OPERATION;
1243 }
1244 if (mEffect == 0) {
1245 return DEAD_OBJECT;
1246 }
1247 if (mClient == 0) {
1248 return INVALID_OPERATION;
1249 }
1250
1251 // handle commands that are not forwarded transparently to effect engine
1252 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1253 // No need to trylock() here as this function is executed in the binder thread serving a
1254 // particular client process: no risk to block the whole media server process or mixer
1255 // threads if we are stuck here
1256 Mutex::Autolock _l(mCblk->lock);
1257 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1258 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1259 mCblk->serverIndex = 0;
1260 mCblk->clientIndex = 0;
1261 return BAD_VALUE;
1262 }
1263 status_t status = NO_ERROR;
1264 while (mCblk->serverIndex < mCblk->clientIndex) {
1265 int reply;
1266 uint32_t rsize = sizeof(int);
1267 int *p = (int *)(mBuffer + mCblk->serverIndex);
1268 int size = *p++;
1269 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1270 ALOGW("command(): invalid parameter block size");
1271 break;
1272 }
1273 effect_param_t *param = (effect_param_t *)p;
1274 if (param->psize == 0 || param->vsize == 0) {
1275 ALOGW("command(): null parameter or value size");
1276 mCblk->serverIndex += size;
1277 continue;
1278 }
1279 uint32_t psize = sizeof(effect_param_t) +
1280 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1281 param->vsize;
1282 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1283 psize,
1284 p,
1285 &rsize,
1286 &reply);
1287 // stop at first error encountered
1288 if (ret != NO_ERROR) {
1289 status = ret;
1290 *(int *)pReplyData = reply;
1291 break;
1292 } else if (reply != NO_ERROR) {
1293 *(int *)pReplyData = reply;
1294 break;
1295 }
1296 mCblk->serverIndex += size;
1297 }
1298 mCblk->serverIndex = 0;
1299 mCblk->clientIndex = 0;
1300 return status;
1301 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1302 *(int *)pReplyData = NO_ERROR;
1303 return enable();
1304 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1305 *(int *)pReplyData = NO_ERROR;
1306 return disable();
1307 }
1308
1309 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1310}
1311
1312void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1313{
1314 ALOGV("setControl %p control %d", this, hasControl);
1315
1316 mHasControl = hasControl;
1317 mEnabled = enabled;
1318
1319 if (signal && mEffectClient != 0) {
1320 mEffectClient->controlStatusChanged(hasControl);
1321 }
1322}
1323
1324void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1325 uint32_t cmdSize,
1326 void *pCmdData,
1327 uint32_t replySize,
1328 void *pReplyData)
1329{
1330 if (mEffectClient != 0) {
1331 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1332 }
1333}
1334
1335
1336
1337void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1338{
1339 if (mEffectClient != 0) {
1340 mEffectClient->enableStatusChanged(enabled);
1341 }
1342}
1343
1344status_t AudioFlinger::EffectHandle::onTransact(
1345 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1346{
1347 return BnEffect::onTransact(code, data, reply, flags);
1348}
1349
1350
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001351void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001352{
1353 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1354
Marco Nelissenb2208842014-02-07 14:00:50 -08001355 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001356 (mClient == 0) ? getpid_cached : mClient->pid(),
1357 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001358 mHasControl ? "yes" : "no",
1359 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001360 mCblk ? mCblk->clientIndex : 0,
1361 mCblk ? mCblk->serverIndex : 0
1362 );
1363
1364 if (locked) {
1365 mCblk->lock.unlock();
1366 }
1367}
1368
1369#undef LOG_TAG
1370#define LOG_TAG "AudioFlinger::EffectChain"
1371
1372AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1373 int sessionId)
1374 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1375 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001376 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), mForceVolume(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001377{
1378 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1379 if (thread == NULL) {
1380 return;
1381 }
1382 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1383 thread->frameCount();
1384}
1385
1386AudioFlinger::EffectChain::~EffectChain()
1387{
1388 if (mOwnInBuffer) {
1389 delete mInBuffer;
1390 }
1391
1392}
1393
1394// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1395sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1396 effect_descriptor_t *descriptor)
1397{
1398 size_t size = mEffects.size();
1399
1400 for (size_t i = 0; i < size; i++) {
1401 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1402 return mEffects[i];
1403 }
1404 }
1405 return 0;
1406}
1407
1408// getEffectFromId_l() must be called with ThreadBase::mLock held
1409sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1410{
1411 size_t size = mEffects.size();
1412
1413 for (size_t i = 0; i < size; i++) {
1414 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1415 if (id == 0 || mEffects[i]->id() == id) {
1416 return mEffects[i];
1417 }
1418 }
1419 return 0;
1420}
1421
1422// getEffectFromType_l() must be called with ThreadBase::mLock held
1423sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1424 const effect_uuid_t *type)
1425{
1426 size_t size = mEffects.size();
1427
1428 for (size_t i = 0; i < size; i++) {
1429 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1430 return mEffects[i];
1431 }
1432 }
1433 return 0;
1434}
1435
1436void AudioFlinger::EffectChain::clearInputBuffer()
1437{
1438 Mutex::Autolock _l(mLock);
1439 sp<ThreadBase> thread = mThread.promote();
1440 if (thread == 0) {
1441 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1442 return;
1443 }
1444 clearInputBuffer_l(thread);
1445}
1446
1447// Must be called with EffectChain::mLock locked
1448void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1449{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001450 // TODO: This will change in the future, depending on multichannel
1451 // and sample format changes for effects.
1452 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1453 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001454 const size_t frameSize =
1455 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001456 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001457}
1458
1459// Must be called with EffectChain::mLock locked
1460void AudioFlinger::EffectChain::process_l()
1461{
1462 sp<ThreadBase> thread = mThread.promote();
1463 if (thread == 0) {
1464 ALOGW("process_l(): cannot promote mixer thread");
1465 return;
1466 }
1467 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1468 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001469 // never process effects when:
1470 // - on an OFFLOAD thread
1471 // - no more tracks are on the session and the effect tail has been rendered
1472 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001473 if (!isGlobalSession) {
1474 bool tracksOnSession = (trackCnt() != 0);
1475
1476 if (!tracksOnSession && mTailBufferCount == 0) {
1477 doProcess = false;
1478 }
1479
1480 if (activeTrackCnt() == 0) {
1481 // if no track is active and the effect tail has not been rendered,
1482 // the input buffer must be cleared here as the mixer process will not do it
1483 if (tracksOnSession || mTailBufferCount > 0) {
1484 clearInputBuffer_l(thread);
1485 if (mTailBufferCount > 0) {
1486 mTailBufferCount--;
1487 }
1488 }
1489 }
1490 }
1491
1492 size_t size = mEffects.size();
1493 if (doProcess) {
1494 for (size_t i = 0; i < size; i++) {
1495 mEffects[i]->process();
1496 }
1497 }
1498 for (size_t i = 0; i < size; i++) {
1499 mEffects[i]->updateState();
1500 }
1501}
1502
1503// addEffect_l() must be called with PlaybackThread::mLock held
1504status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1505{
1506 effect_descriptor_t desc = effect->desc();
1507 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1508
1509 Mutex::Autolock _l(mLock);
1510 effect->setChain(this);
1511 sp<ThreadBase> thread = mThread.promote();
1512 if (thread == 0) {
1513 return NO_INIT;
1514 }
1515 effect->setThread(thread);
1516
1517 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1518 // Auxiliary effects are inserted at the beginning of mEffects vector as
1519 // they are processed first and accumulated in chain input buffer
1520 mEffects.insertAt(effect, 0);
1521
1522 // the input buffer for auxiliary effect contains mono samples in
1523 // 32 bit format. This is to avoid saturation in AudoMixer
1524 // accumulation stage. Saturation is done in EffectModule::process() before
1525 // calling the process in effect engine
1526 size_t numSamples = thread->frameCount();
1527 int32_t *buffer = new int32_t[numSamples];
1528 memset(buffer, 0, numSamples * sizeof(int32_t));
1529 effect->setInBuffer((int16_t *)buffer);
1530 // auxiliary effects output samples to chain input buffer for further processing
1531 // by insert effects
1532 effect->setOutBuffer(mInBuffer);
1533 } else {
1534 // Insert effects are inserted at the end of mEffects vector as they are processed
1535 // after track and auxiliary effects.
1536 // Insert effect order as a function of indicated preference:
1537 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1538 // another effect is present
1539 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1540 // last effect claiming first position
1541 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1542 // first effect claiming last position
1543 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1544 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1545 // already present
1546
1547 size_t size = mEffects.size();
1548 size_t idx_insert = size;
1549 ssize_t idx_insert_first = -1;
1550 ssize_t idx_insert_last = -1;
1551
1552 for (size_t i = 0; i < size; i++) {
1553 effect_descriptor_t d = mEffects[i]->desc();
1554 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1555 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1556 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1557 // check invalid effect chaining combinations
1558 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1559 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1560 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1561 desc.name, d.name);
1562 return INVALID_OPERATION;
1563 }
1564 // remember position of first insert effect and by default
1565 // select this as insert position for new effect
1566 if (idx_insert == size) {
1567 idx_insert = i;
1568 }
1569 // remember position of last insert effect claiming
1570 // first position
1571 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1572 idx_insert_first = i;
1573 }
1574 // remember position of first insert effect claiming
1575 // last position
1576 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1577 idx_insert_last == -1) {
1578 idx_insert_last = i;
1579 }
1580 }
1581 }
1582
1583 // modify idx_insert from first position if needed
1584 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1585 if (idx_insert_last != -1) {
1586 idx_insert = idx_insert_last;
1587 } else {
1588 idx_insert = size;
1589 }
1590 } else {
1591 if (idx_insert_first != -1) {
1592 idx_insert = idx_insert_first + 1;
1593 }
1594 }
1595
1596 // always read samples from chain input buffer
1597 effect->setInBuffer(mInBuffer);
1598
1599 // if last effect in the chain, output samples to chain
1600 // output buffer, otherwise to chain input buffer
1601 if (idx_insert == size) {
1602 if (idx_insert != 0) {
1603 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1604 mEffects[idx_insert-1]->configure();
1605 }
1606 effect->setOutBuffer(mOutBuffer);
1607 } else {
1608 effect->setOutBuffer(mInBuffer);
1609 }
1610 mEffects.insertAt(effect, idx_insert);
1611
1612 ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this,
1613 idx_insert);
1614 }
1615 effect->configure();
1616 return NO_ERROR;
1617}
1618
1619// removeEffect_l() must be called with PlaybackThread::mLock held
1620size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1621{
1622 Mutex::Autolock _l(mLock);
1623 size_t size = mEffects.size();
1624 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1625
1626 for (size_t i = 0; i < size; i++) {
1627 if (effect == mEffects[i]) {
1628 // calling stop here will remove pre-processing effect from the audio HAL.
1629 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1630 // the middle of a read from audio HAL
1631 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1632 mEffects[i]->state() == EffectModule::STOPPING) {
1633 mEffects[i]->stop();
1634 }
1635 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1636 delete[] effect->inBuffer();
1637 } else {
1638 if (i == size - 1 && i != 0) {
1639 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1640 mEffects[i - 1]->configure();
1641 }
1642 }
1643 mEffects.removeAt(i);
1644 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(),
1645 this, i);
1646 break;
1647 }
1648 }
1649
1650 return mEffects.size();
1651}
1652
1653// setDevice_l() must be called with PlaybackThread::mLock held
1654void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1655{
1656 size_t size = mEffects.size();
1657 for (size_t i = 0; i < size; i++) {
1658 mEffects[i]->setDevice(device);
1659 }
1660}
1661
1662// setMode_l() must be called with PlaybackThread::mLock held
1663void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1664{
1665 size_t size = mEffects.size();
1666 for (size_t i = 0; i < size; i++) {
1667 mEffects[i]->setMode(mode);
1668 }
1669}
1670
1671// setAudioSource_l() must be called with PlaybackThread::mLock held
1672void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1673{
1674 size_t size = mEffects.size();
1675 for (size_t i = 0; i < size; i++) {
1676 mEffects[i]->setAudioSource(source);
1677 }
1678}
1679
1680// setVolume_l() must be called with PlaybackThread::mLock held
1681bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1682{
1683 uint32_t newLeft = *left;
1684 uint32_t newRight = *right;
1685 bool hasControl = false;
1686 int ctrlIdx = -1;
1687 size_t size = mEffects.size();
1688
1689 // first update volume controller
1690 for (size_t i = size; i > 0; i--) {
1691 if (mEffects[i - 1]->isProcessEnabled() &&
1692 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1693 ctrlIdx = i - 1;
1694 hasControl = true;
1695 break;
1696 }
1697 }
1698
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001699 if (!isVolumeForced() && ctrlIdx == mVolumeCtrlIdx &&
1700 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001701 if (hasControl) {
1702 *left = mNewLeftVolume;
1703 *right = mNewRightVolume;
1704 }
1705 return hasControl;
1706 }
1707
1708 mVolumeCtrlIdx = ctrlIdx;
1709 mLeftVolume = newLeft;
1710 mRightVolume = newRight;
1711
1712 // second get volume update from volume controller
1713 if (ctrlIdx >= 0) {
1714 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1715 mNewLeftVolume = newLeft;
1716 mNewRightVolume = newRight;
1717 }
1718 // then indicate volume to all other effects in chain.
1719 // Pass altered volume to effects before volume controller
1720 // and requested volume to effects after controller
1721 uint32_t lVol = newLeft;
1722 uint32_t rVol = newRight;
1723
1724 for (size_t i = 0; i < size; i++) {
1725 if ((int)i == ctrlIdx) {
1726 continue;
1727 }
1728 // this also works for ctrlIdx == -1 when there is no volume controller
1729 if ((int)i > ctrlIdx) {
1730 lVol = *left;
1731 rVol = *right;
1732 }
1733 mEffects[i]->setVolume(&lVol, &rVol, false);
1734 }
1735 *left = newLeft;
1736 *right = newRight;
1737
1738 return hasControl;
1739}
1740
Eric Laurent1b928682014-10-02 19:41:47 -07001741void AudioFlinger::EffectChain::syncHalEffectsState()
1742{
1743 Mutex::Autolock _l(mLock);
1744 for (size_t i = 0; i < mEffects.size(); i++) {
1745 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1746 mEffects[i]->state() == EffectModule::STOPPING) {
1747 mEffects[i]->addEffectToHal_l();
1748 }
1749 }
1750}
1751
Eric Laurentca7cc822012-11-19 14:55:58 -08001752void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1753{
1754 const size_t SIZE = 256;
1755 char buffer[SIZE];
1756 String8 result;
1757
Marco Nelissenb2208842014-02-07 14:00:50 -08001758 size_t numEffects = mEffects.size();
1759 snprintf(buffer, SIZE, " %d effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001760 result.append(buffer);
1761
Marco Nelissenb2208842014-02-07 14:00:50 -08001762 if (numEffects) {
1763 bool locked = AudioFlinger::dumpTryLock(mLock);
1764 // failed to lock - AudioFlinger is probably deadlocked
1765 if (!locked) {
1766 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001767 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001768
Marco Nelissenb2208842014-02-07 14:00:50 -08001769 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001770 snprintf(buffer, SIZE, "\t%p %p %d\n",
1771 mInBuffer,
1772 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001773 mActiveTrackCnt);
1774 result.append(buffer);
1775 write(fd, result.string(), result.size());
1776
1777 for (size_t i = 0; i < numEffects; ++i) {
1778 sp<EffectModule> effect = mEffects[i];
1779 if (effect != 0) {
1780 effect->dump(fd, args);
1781 }
1782 }
1783
1784 if (locked) {
1785 mLock.unlock();
1786 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001787 }
1788}
1789
1790// must be called with ThreadBase::mLock held
1791void AudioFlinger::EffectChain::setEffectSuspended_l(
1792 const effect_uuid_t *type, bool suspend)
1793{
1794 sp<SuspendedEffectDesc> desc;
1795 // use effect type UUID timelow as key as there is no real risk of identical
1796 // timeLow fields among effect type UUIDs.
1797 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1798 if (suspend) {
1799 if (index >= 0) {
1800 desc = mSuspendedEffects.valueAt(index);
1801 } else {
1802 desc = new SuspendedEffectDesc();
1803 desc->mType = *type;
1804 mSuspendedEffects.add(type->timeLow, desc);
1805 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1806 }
1807 if (desc->mRefCount++ == 0) {
1808 sp<EffectModule> effect = getEffectIfEnabled(type);
1809 if (effect != 0) {
1810 desc->mEffect = effect;
1811 effect->setSuspended(true);
1812 effect->setEnabled(false);
1813 }
1814 }
1815 } else {
1816 if (index < 0) {
1817 return;
1818 }
1819 desc = mSuspendedEffects.valueAt(index);
1820 if (desc->mRefCount <= 0) {
1821 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1822 desc->mRefCount = 1;
1823 }
1824 if (--desc->mRefCount == 0) {
1825 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1826 if (desc->mEffect != 0) {
1827 sp<EffectModule> effect = desc->mEffect.promote();
1828 if (effect != 0) {
1829 effect->setSuspended(false);
1830 effect->lock();
1831 EffectHandle *handle = effect->controlHandle_l();
1832 if (handle != NULL && !handle->destroyed_l()) {
1833 effect->setEnabled_l(handle->enabled());
1834 }
1835 effect->unlock();
1836 }
1837 desc->mEffect.clear();
1838 }
1839 mSuspendedEffects.removeItemsAt(index);
1840 }
1841 }
1842}
1843
1844// must be called with ThreadBase::mLock held
1845void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1846{
1847 sp<SuspendedEffectDesc> desc;
1848
1849 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1850 if (suspend) {
1851 if (index >= 0) {
1852 desc = mSuspendedEffects.valueAt(index);
1853 } else {
1854 desc = new SuspendedEffectDesc();
1855 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1856 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1857 }
1858 if (desc->mRefCount++ == 0) {
1859 Vector< sp<EffectModule> > effects;
1860 getSuspendEligibleEffects(effects);
1861 for (size_t i = 0; i < effects.size(); i++) {
1862 setEffectSuspended_l(&effects[i]->desc().type, true);
1863 }
1864 }
1865 } else {
1866 if (index < 0) {
1867 return;
1868 }
1869 desc = mSuspendedEffects.valueAt(index);
1870 if (desc->mRefCount <= 0) {
1871 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1872 desc->mRefCount = 1;
1873 }
1874 if (--desc->mRefCount == 0) {
1875 Vector<const effect_uuid_t *> types;
1876 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1877 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1878 continue;
1879 }
1880 types.add(&mSuspendedEffects.valueAt(i)->mType);
1881 }
1882 for (size_t i = 0; i < types.size(); i++) {
1883 setEffectSuspended_l(types[i], false);
1884 }
1885 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1886 mSuspendedEffects.keyAt(index));
1887 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1888 }
1889 }
1890}
1891
1892
1893// The volume effect is used for automated tests only
1894#ifndef OPENSL_ES_H_
1895static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1896 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1897const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1898#endif //OPENSL_ES_H_
1899
1900bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1901{
1902 // auxiliary effects and visualizer are never suspended on output mix
1903 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1904 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1905 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1906 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1907 return false;
1908 }
1909 return true;
1910}
1911
1912void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1913 Vector< sp<AudioFlinger::EffectModule> > &effects)
1914{
1915 effects.clear();
1916 for (size_t i = 0; i < mEffects.size(); i++) {
1917 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1918 effects.add(mEffects[i]);
1919 }
1920 }
1921}
1922
1923sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1924 const effect_uuid_t *type)
1925{
1926 sp<EffectModule> effect = getEffectFromType_l(type);
1927 return effect != 0 && effect->isEnabled() ? effect : 0;
1928}
1929
1930void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1931 bool enabled)
1932{
1933 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1934 if (enabled) {
1935 if (index < 0) {
1936 // if the effect is not suspend check if all effects are suspended
1937 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1938 if (index < 0) {
1939 return;
1940 }
1941 if (!isEffectEligibleForSuspend(effect->desc())) {
1942 return;
1943 }
1944 setEffectSuspended_l(&effect->desc().type, enabled);
1945 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1946 if (index < 0) {
1947 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1948 return;
1949 }
1950 }
1951 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1952 effect->desc().type.timeLow);
1953 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1954 // if effect is requested to suspended but was not yet enabled, supend it now.
1955 if (desc->mEffect == 0) {
1956 desc->mEffect = effect;
1957 effect->setEnabled(false);
1958 effect->setSuspended(true);
1959 }
1960 } else {
1961 if (index < 0) {
1962 return;
1963 }
1964 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1965 effect->desc().type.timeLow);
1966 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1967 desc->mEffect.clear();
1968 effect->setSuspended(false);
1969 }
1970}
1971
Eric Laurent5baf2af2013-09-12 17:37:00 -07001972bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001973{
1974 Mutex::Autolock _l(mLock);
1975 size_t size = mEffects.size();
1976 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001977 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001978 return true;
1979 }
1980 }
1981 return false;
1982}
1983
Eric Laurentaaa44472014-09-12 17:41:50 -07001984void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
1985{
1986 Mutex::Autolock _l(mLock);
1987 mThread = thread;
1988 for (size_t i = 0; i < mEffects.size(); i++) {
1989 mEffects[i]->setThread(thread);
1990 }
1991}
1992
Glenn Kasten63238ef2015-03-02 15:50:29 -08001993} // namespace android