blob: 53b0ff13954e320c9b149f8336a790d02a57429a [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
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;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900342 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
343 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800344 } else {
345 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700346 // TODO: Update this logic when multichannel effects are implemented.
347 // For offloaded tracks consider mono output as stereo for proper effect initialization
348 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
349 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
350 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
351 ALOGV("Overriding effect input and output as STEREO");
352 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800353 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700354
Eric Laurentca7cc822012-11-19 14:55:58 -0800355 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
356 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
357 mConfig.inputCfg.samplingRate = thread->sampleRate();
358 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
359 mConfig.inputCfg.bufferProvider.cookie = NULL;
360 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
361 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
362 mConfig.outputCfg.bufferProvider.cookie = NULL;
363 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
364 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
365 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
366 // Insert effect:
367 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
368 // always overwrites output buffer: input buffer == output buffer
369 // - in other sessions:
370 // last effect in the chain accumulates in output buffer: input buffer != output buffer
371 // other effect: overwrites output buffer: input buffer == output buffer
372 // Auxiliary effect:
373 // accumulates in output buffer: input buffer != output buffer
374 // Therefore: accumulate <=> input buffer != output buffer
375 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
376 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
377 } else {
378 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
379 }
380 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
381 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
382 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
383 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
384
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700385 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800386 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
387
388 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700389 size = sizeof(int);
390 status = (*mEffectInterface)->command(mEffectInterface,
Eric Laurentca7cc822012-11-19 14:55:58 -0800391 EFFECT_CMD_SET_CONFIG,
392 sizeof(effect_config_t),
393 &mConfig,
394 &size,
395 &cmdStatus);
396 if (status == 0) {
397 status = cmdStatus;
398 }
399
400 if (status == 0 &&
401 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
402 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
403 effect_param_t *p = (effect_param_t *)buf32;
404
405 p->psize = sizeof(uint32_t);
406 p->vsize = sizeof(uint32_t);
407 size = sizeof(int);
408 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
409
410 uint32_t latency = 0;
411 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
412 if (pbt != NULL) {
413 latency = pbt->latency_l();
414 }
415
416 *((int32_t *)p->data + 1)= latency;
417 (*mEffectInterface)->command(mEffectInterface,
418 EFFECT_CMD_SET_PARAM,
419 sizeof(effect_param_t) + 8,
420 &buf32,
421 &size,
422 &cmdStatus);
423 }
424
425 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
426 (1000 * mConfig.outputCfg.buffer.frameCount);
427
Eric Laurentd0ebb532013-04-02 16:41:41 -0700428exit:
429 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800430 return status;
431}
432
433status_t AudioFlinger::EffectModule::init()
434{
435 Mutex::Autolock _l(mLock);
436 if (mEffectInterface == NULL) {
437 return NO_INIT;
438 }
439 status_t cmdStatus;
440 uint32_t size = sizeof(status_t);
441 status_t status = (*mEffectInterface)->command(mEffectInterface,
442 EFFECT_CMD_INIT,
443 0,
444 NULL,
445 &size,
446 &cmdStatus);
447 if (status == 0) {
448 status = cmdStatus;
449 }
450 return status;
451}
452
Eric Laurent1b928682014-10-02 19:41:47 -0700453void AudioFlinger::EffectModule::addEffectToHal_l()
454{
455 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
456 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
457 sp<ThreadBase> thread = mThread.promote();
458 if (thread != 0) {
459 audio_stream_t *stream = thread->stream();
460 if (stream != NULL) {
461 stream->add_audio_effect(stream, mEffectInterface);
462 }
463 }
464 }
465}
466
Eric Laurentca7cc822012-11-19 14:55:58 -0800467status_t AudioFlinger::EffectModule::start()
468{
469 Mutex::Autolock _l(mLock);
470 return start_l();
471}
472
473status_t AudioFlinger::EffectModule::start_l()
474{
475 if (mEffectInterface == NULL) {
476 return NO_INIT;
477 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700478 if (mStatus != NO_ERROR) {
479 return mStatus;
480 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800481 status_t cmdStatus;
482 uint32_t size = sizeof(status_t);
483 status_t status = (*mEffectInterface)->command(mEffectInterface,
484 EFFECT_CMD_ENABLE,
485 0,
486 NULL,
487 &size,
488 &cmdStatus);
489 if (status == 0) {
490 status = cmdStatus;
491 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700492 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700493 addEffectToHal_l();
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700494 sp<EffectChain> chain = mChain.promote();
495 if (chain != 0) {
496 chain->forceVolume();
497 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800498 }
499 return status;
500}
501
502status_t AudioFlinger::EffectModule::stop()
503{
504 Mutex::Autolock _l(mLock);
505 return stop_l();
506}
507
508status_t AudioFlinger::EffectModule::stop_l()
509{
510 if (mEffectInterface == NULL) {
511 return NO_INIT;
512 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700513 if (mStatus != NO_ERROR) {
514 return mStatus;
515 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800516 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800517 uint32_t size = sizeof(status_t);
518 status_t status = (*mEffectInterface)->command(mEffectInterface,
519 EFFECT_CMD_DISABLE,
520 0,
521 NULL,
522 &size,
523 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800524 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800525 status = cmdStatus;
526 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800527 if (status == NO_ERROR) {
528 status = remove_effect_from_hal_l();
529 }
530 return status;
531}
532
533status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
534{
535 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
536 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800537 sp<ThreadBase> thread = mThread.promote();
538 if (thread != 0) {
539 audio_stream_t *stream = thread->stream();
540 if (stream != NULL) {
541 stream->remove_audio_effect(stream, mEffectInterface);
542 }
543 }
544 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800545 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800546}
547
548status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
549 uint32_t cmdSize,
550 void *pCmdData,
551 uint32_t *replySize,
552 void *pReplyData)
553{
554 Mutex::Autolock _l(mLock);
555 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
556
557 if (mState == DESTROYED || mEffectInterface == NULL) {
558 return NO_INIT;
559 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700560 if (mStatus != NO_ERROR) {
561 return mStatus;
562 }
Andy Hung110bc952016-06-20 15:22:52 -0700563 if (cmdCode == EFFECT_CMD_GET_PARAM &&
564 (*replySize < sizeof(effect_param_t) ||
565 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
566 android_errorWriteLog(0x534e4554, "29251553");
567 return -EINVAL;
568 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800569 status_t status = (*mEffectInterface)->command(mEffectInterface,
570 cmdCode,
571 cmdSize,
572 pCmdData,
573 replySize,
574 pReplyData);
575 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
576 uint32_t size = (replySize == NULL) ? 0 : *replySize;
577 for (size_t i = 1; i < mHandles.size(); i++) {
578 EffectHandle *h = mHandles[i];
579 if (h != NULL && !h->destroyed_l()) {
580 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
581 }
582 }
583 }
584 return status;
585}
586
587status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
588{
589 Mutex::Autolock _l(mLock);
590 return setEnabled_l(enabled);
591}
592
593// must be called with EffectModule::mLock held
594status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
595{
596
597 ALOGV("setEnabled %p enabled %d", this, enabled);
598
599 if (enabled != isEnabled()) {
600 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
601 if (enabled && status != NO_ERROR) {
602 return status;
603 }
604
605 switch (mState) {
606 // going from disabled to enabled
607 case IDLE:
608 mState = STARTING;
609 break;
610 case STOPPED:
611 mState = RESTART;
612 break;
613 case STOPPING:
614 mState = ACTIVE;
615 break;
616
617 // going from enabled to disabled
618 case RESTART:
619 mState = STOPPED;
620 break;
621 case STARTING:
622 mState = IDLE;
623 break;
624 case ACTIVE:
625 mState = STOPPING;
626 break;
627 case DESTROYED:
628 return NO_ERROR; // simply ignore as we are being destroyed
629 }
630 for (size_t i = 1; i < mHandles.size(); i++) {
631 EffectHandle *h = mHandles[i];
632 if (h != NULL && !h->destroyed_l()) {
633 h->setEnabled(enabled);
634 }
635 }
636 }
637 return NO_ERROR;
638}
639
640bool AudioFlinger::EffectModule::isEnabled() const
641{
642 switch (mState) {
643 case RESTART:
644 case STARTING:
645 case ACTIVE:
646 return true;
647 case IDLE:
648 case STOPPING:
649 case STOPPED:
650 case DESTROYED:
651 default:
652 return false;
653 }
654}
655
656bool AudioFlinger::EffectModule::isProcessEnabled() const
657{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700658 if (mStatus != NO_ERROR) {
659 return false;
660 }
661
Eric Laurentca7cc822012-11-19 14:55:58 -0800662 switch (mState) {
663 case RESTART:
664 case ACTIVE:
665 case STOPPING:
666 case STOPPED:
667 return true;
668 case IDLE:
669 case STARTING:
670 case DESTROYED:
671 default:
672 return false;
673 }
674}
675
676status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
677{
678 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700679 if (mStatus != NO_ERROR) {
680 return mStatus;
681 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800682 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800683 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
684 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
685 if (isProcessEnabled() &&
686 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
687 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800688 uint32_t volume[2];
689 uint32_t *pVolume = NULL;
690 uint32_t size = sizeof(volume);
691 volume[0] = *left;
692 volume[1] = *right;
693 if (controller) {
694 pVolume = volume;
695 }
696 status = (*mEffectInterface)->command(mEffectInterface,
697 EFFECT_CMD_SET_VOLUME,
698 size,
699 volume,
700 &size,
701 pVolume);
702 if (controller && status == NO_ERROR && size == sizeof(volume)) {
703 *left = volume[0];
704 *right = volume[1];
705 }
706 }
707 return status;
708}
709
710status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
711{
712 if (device == AUDIO_DEVICE_NONE) {
713 return NO_ERROR;
714 }
715
716 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700717 if (mStatus != NO_ERROR) {
718 return mStatus;
719 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800720 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700721 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800722 status_t cmdStatus;
723 uint32_t size = sizeof(status_t);
724 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
725 EFFECT_CMD_SET_INPUT_DEVICE;
726 status = (*mEffectInterface)->command(mEffectInterface,
727 cmd,
728 sizeof(uint32_t),
729 &device,
730 &size,
731 &cmdStatus);
732 }
733 return status;
734}
735
736status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
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;
743 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
744 status_t cmdStatus;
745 uint32_t size = sizeof(status_t);
746 status = (*mEffectInterface)->command(mEffectInterface,
747 EFFECT_CMD_SET_AUDIO_MODE,
748 sizeof(audio_mode_t),
749 &mode,
750 &size,
751 &cmdStatus);
752 if (status == NO_ERROR) {
753 status = cmdStatus;
754 }
755 }
756 return status;
757}
758
759status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
760{
761 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700762 if (mStatus != NO_ERROR) {
763 return mStatus;
764 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800765 status_t status = NO_ERROR;
766 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
767 uint32_t size = 0;
768 status = (*mEffectInterface)->command(mEffectInterface,
769 EFFECT_CMD_SET_AUDIO_SOURCE,
770 sizeof(audio_source_t),
771 &source,
772 &size,
773 NULL);
774 }
775 return status;
776}
777
778void AudioFlinger::EffectModule::setSuspended(bool suspended)
779{
780 Mutex::Autolock _l(mLock);
781 mSuspended = suspended;
782}
783
784bool AudioFlinger::EffectModule::suspended() const
785{
786 Mutex::Autolock _l(mLock);
787 return mSuspended;
788}
789
790bool AudioFlinger::EffectModule::purgeHandles()
791{
792 bool enabled = false;
793 Mutex::Autolock _l(mLock);
794 for (size_t i = 0; i < mHandles.size(); i++) {
795 EffectHandle *handle = mHandles[i];
796 if (handle != NULL && !handle->destroyed_l()) {
797 handle->effect().clear();
798 if (handle->hasControl()) {
799 enabled = handle->enabled();
800 }
801 }
802 }
803 return enabled;
804}
805
Eric Laurent5baf2af2013-09-12 17:37:00 -0700806status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
807{
808 Mutex::Autolock _l(mLock);
809 if (mStatus != NO_ERROR) {
810 return mStatus;
811 }
812 status_t status = NO_ERROR;
813 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
814 status_t cmdStatus;
815 uint32_t size = sizeof(status_t);
816 effect_offload_param_t cmd;
817
818 cmd.isOffload = offloaded;
819 cmd.ioHandle = io;
820 status = (*mEffectInterface)->command(mEffectInterface,
821 EFFECT_CMD_OFFLOAD,
822 sizeof(effect_offload_param_t),
823 &cmd,
824 &size,
825 &cmdStatus);
826 if (status == NO_ERROR) {
827 status = cmdStatus;
828 }
829 mOffloaded = (status == NO_ERROR) ? offloaded : false;
830 } else {
831 if (offloaded) {
832 status = INVALID_OPERATION;
833 }
834 mOffloaded = false;
835 }
836 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
837 return status;
838}
839
840bool AudioFlinger::EffectModule::isOffloaded() const
841{
842 Mutex::Autolock _l(mLock);
843 return mOffloaded;
844}
845
Marco Nelissenb2208842014-02-07 14:00:50 -0800846String8 effectFlagsToString(uint32_t flags) {
847 String8 s;
848
849 s.append("conn. mode: ");
850 switch (flags & EFFECT_FLAG_TYPE_MASK) {
851 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
852 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
853 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
854 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
855 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
856 default: s.append("unknown/reserved"); break;
857 }
858 s.append(", ");
859
860 s.append("insert pref: ");
861 switch (flags & EFFECT_FLAG_INSERT_MASK) {
862 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
863 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
864 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
865 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
866 default: s.append("unknown/reserved"); break;
867 }
868 s.append(", ");
869
870 s.append("volume mgmt: ");
871 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
872 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
873 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
874 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
875 default: s.append("unknown/reserved"); break;
876 }
877 s.append(", ");
878
879 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
880 if (devind) {
881 s.append("device indication: ");
882 switch (devind) {
883 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
884 default: s.append("unknown/reserved"); break;
885 }
886 s.append(", ");
887 }
888
889 s.append("input mode: ");
890 switch (flags & EFFECT_FLAG_INPUT_MASK) {
891 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
892 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
893 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
894 default: s.append("not set"); break;
895 }
896 s.append(", ");
897
898 s.append("output mode: ");
899 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
900 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
901 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
902 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
903 default: s.append("not set"); break;
904 }
905 s.append(", ");
906
907 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
908 if (accel) {
909 s.append("hardware acceleration: ");
910 switch (accel) {
911 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
912 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
913 default: s.append("unknown/reserved"); break;
914 }
915 s.append(", ");
916 }
917
918 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
919 if (modeind) {
920 s.append("mode indication: ");
921 switch (modeind) {
922 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
923 default: s.append("unknown/reserved"); break;
924 }
925 s.append(", ");
926 }
927
928 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
929 if (srcind) {
930 s.append("source indication: ");
931 switch (srcind) {
932 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
933 default: s.append("unknown/reserved"); break;
934 }
935 s.append(", ");
936 }
937
938 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
939 s.append("offloadable, ");
940 }
941
942 int len = s.length();
943 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -0700944 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -0800945 s.unlockBuffer(len - 2);
946 }
947 return s;
948}
949
950
Glenn Kasten0f11b512014-01-31 16:18:54 -0800951void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -0800952{
953 const size_t SIZE = 256;
954 char buffer[SIZE];
955 String8 result;
956
957 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
958 result.append(buffer);
959
960 bool locked = AudioFlinger::dumpTryLock(mLock);
961 // failed to lock - AudioFlinger is probably deadlocked
962 if (!locked) {
963 result.append("\t\tCould not lock Fx mutex:\n");
964 }
965
966 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000967 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
968 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -0800969 result.append(buffer);
970
971 result.append("\t\tDescriptor:\n");
972 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
973 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
974 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
975 mDescriptor.uuid.node[2],
976 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
977 result.append(buffer);
978 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
979 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
980 mDescriptor.type.timeHiAndVersion,
981 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
982 mDescriptor.type.node[2],
983 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
984 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -0800985 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -0800986 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -0800987 mDescriptor.flags,
988 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -0800989 result.append(buffer);
990 snprintf(buffer, SIZE, "\t\t- name: %s\n",
991 mDescriptor.name);
992 result.append(buffer);
993 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
994 mDescriptor.implementor);
995 result.append(buffer);
996
997 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000998 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000999 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001000 mConfig.inputCfg.buffer.frameCount,
1001 mConfig.inputCfg.samplingRate,
1002 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001003 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001004 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001005 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001006 result.append(buffer);
1007
1008 result.append("\t\t- Output configuration:\n");
1009 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001010 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001011 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001012 mConfig.outputCfg.buffer.frameCount,
1013 mConfig.outputCfg.samplingRate,
1014 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001015 mConfig.outputCfg.format,
1016 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001017 result.append(buffer);
1018
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001019 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001020 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001021 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001022 for (size_t i = 0; i < mHandles.size(); ++i) {
1023 EffectHandle *handle = mHandles[i];
1024 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001025 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001026 result.append(buffer);
1027 }
1028 }
1029
Eric Laurentca7cc822012-11-19 14:55:58 -08001030 write(fd, result.string(), result.length());
1031
1032 if (locked) {
1033 mLock.unlock();
1034 }
1035}
1036
1037// ----------------------------------------------------------------------------
1038// EffectHandle implementation
1039// ----------------------------------------------------------------------------
1040
1041#undef LOG_TAG
1042#define LOG_TAG "AudioFlinger::EffectHandle"
1043
1044AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1045 const sp<AudioFlinger::Client>& client,
1046 const sp<IEffectClient>& effectClient,
1047 int32_t priority)
1048 : BnEffect(),
1049 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1050 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1051{
1052 ALOGV("constructor %p", this);
1053
1054 if (client == 0) {
1055 return;
1056 }
1057 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1058 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001059 if (mCblkMemory == 0 ||
1060 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001061 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001062 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001063 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 return;
1065 }
Glenn Kastene75da402013-11-20 13:54:52 -08001066 new(mCblk) effect_param_cblk_t();
1067 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001068}
1069
1070AudioFlinger::EffectHandle::~EffectHandle()
1071{
1072 ALOGV("Destructor %p", this);
1073
1074 if (mEffect == 0) {
1075 mDestroyed = true;
1076 return;
1077 }
1078 mEffect->lock();
1079 mDestroyed = true;
1080 mEffect->unlock();
1081 disconnect(false);
1082}
1083
Glenn Kastene75da402013-11-20 13:54:52 -08001084status_t AudioFlinger::EffectHandle::initCheck()
1085{
1086 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1087}
1088
Eric Laurentca7cc822012-11-19 14:55:58 -08001089status_t AudioFlinger::EffectHandle::enable()
1090{
1091 ALOGV("enable %p", this);
1092 if (!mHasControl) {
1093 return INVALID_OPERATION;
1094 }
1095 if (mEffect == 0) {
1096 return DEAD_OBJECT;
1097 }
1098
1099 if (mEnabled) {
1100 return NO_ERROR;
1101 }
1102
1103 mEnabled = true;
1104
1105 sp<ThreadBase> thread = mEffect->thread().promote();
1106 if (thread != 0) {
1107 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1108 }
1109
1110 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1111 if (mEffect->suspended()) {
1112 return NO_ERROR;
1113 }
1114
1115 status_t status = mEffect->setEnabled(true);
1116 if (status != NO_ERROR) {
1117 if (thread != 0) {
1118 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1119 }
1120 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001121 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001122 if (thread != 0) {
1123 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001124 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001125 Mutex::Autolock _l(t->mLock);
1126 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001127 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001128 if (!mEffect->isOffloadable()) {
1129 if (thread->type() == ThreadBase::OFFLOAD) {
1130 PlaybackThread *t = (PlaybackThread *)thread.get();
1131 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1132 }
1133 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1134 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1135 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001136 }
1137 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001138 }
1139 return status;
1140}
1141
1142status_t AudioFlinger::EffectHandle::disable()
1143{
1144 ALOGV("disable %p", this);
1145 if (!mHasControl) {
1146 return INVALID_OPERATION;
1147 }
1148 if (mEffect == 0) {
1149 return DEAD_OBJECT;
1150 }
1151
1152 if (!mEnabled) {
1153 return NO_ERROR;
1154 }
1155 mEnabled = false;
1156
1157 if (mEffect->suspended()) {
1158 return NO_ERROR;
1159 }
1160
1161 status_t status = mEffect->setEnabled(false);
1162
1163 sp<ThreadBase> thread = mEffect->thread().promote();
1164 if (thread != 0) {
1165 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001166 if (thread->type() == ThreadBase::OFFLOAD) {
1167 PlaybackThread *t = (PlaybackThread *)thread.get();
1168 Mutex::Autolock _l(t->mLock);
1169 t->broadcast_l();
1170 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001171 }
1172
1173 return status;
1174}
1175
1176void AudioFlinger::EffectHandle::disconnect()
1177{
1178 disconnect(true);
1179}
1180
1181void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1182{
1183 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1184 if (mEffect == 0) {
1185 return;
1186 }
1187 // restore suspended effects if the disconnected handle was enabled and the last one.
1188 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1189 sp<ThreadBase> thread = mEffect->thread().promote();
1190 if (thread != 0) {
1191 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1192 }
1193 }
1194
1195 // release sp on module => module destructor can be called now
1196 mEffect.clear();
1197 if (mClient != 0) {
1198 if (mCblk != NULL) {
1199 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1200 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1201 }
1202 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001203 // Client destructor must run with AudioFlinger client mutex locked
1204 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001205 mClient.clear();
1206 }
1207}
1208
1209status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1210 uint32_t cmdSize,
1211 void *pCmdData,
1212 uint32_t *replySize,
1213 void *pReplyData)
1214{
1215 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1216 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1217
1218 // only get parameter command is permitted for applications not controlling the effect
1219 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1220 return INVALID_OPERATION;
1221 }
1222 if (mEffect == 0) {
1223 return DEAD_OBJECT;
1224 }
1225 if (mClient == 0) {
1226 return INVALID_OPERATION;
1227 }
1228
1229 // handle commands that are not forwarded transparently to effect engine
1230 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1231 // No need to trylock() here as this function is executed in the binder thread serving a
1232 // particular client process: no risk to block the whole media server process or mixer
1233 // threads if we are stuck here
1234 Mutex::Autolock _l(mCblk->lock);
1235 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1236 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1237 mCblk->serverIndex = 0;
1238 mCblk->clientIndex = 0;
1239 return BAD_VALUE;
1240 }
1241 status_t status = NO_ERROR;
1242 while (mCblk->serverIndex < mCblk->clientIndex) {
1243 int reply;
1244 uint32_t rsize = sizeof(int);
1245 int *p = (int *)(mBuffer + mCblk->serverIndex);
1246 int size = *p++;
1247 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1248 ALOGW("command(): invalid parameter block size");
1249 break;
1250 }
1251 effect_param_t *param = (effect_param_t *)p;
1252 if (param->psize == 0 || param->vsize == 0) {
1253 ALOGW("command(): null parameter or value size");
1254 mCblk->serverIndex += size;
1255 continue;
1256 }
1257 uint32_t psize = sizeof(effect_param_t) +
1258 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1259 param->vsize;
1260 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1261 psize,
1262 p,
1263 &rsize,
1264 &reply);
1265 // stop at first error encountered
1266 if (ret != NO_ERROR) {
1267 status = ret;
1268 *(int *)pReplyData = reply;
1269 break;
1270 } else if (reply != NO_ERROR) {
1271 *(int *)pReplyData = reply;
1272 break;
1273 }
1274 mCblk->serverIndex += size;
1275 }
1276 mCblk->serverIndex = 0;
1277 mCblk->clientIndex = 0;
1278 return status;
1279 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1280 *(int *)pReplyData = NO_ERROR;
1281 return enable();
1282 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1283 *(int *)pReplyData = NO_ERROR;
1284 return disable();
1285 }
1286
1287 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1288}
1289
1290void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1291{
1292 ALOGV("setControl %p control %d", this, hasControl);
1293
1294 mHasControl = hasControl;
1295 mEnabled = enabled;
1296
1297 if (signal && mEffectClient != 0) {
1298 mEffectClient->controlStatusChanged(hasControl);
1299 }
1300}
1301
1302void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1303 uint32_t cmdSize,
1304 void *pCmdData,
1305 uint32_t replySize,
1306 void *pReplyData)
1307{
1308 if (mEffectClient != 0) {
1309 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1310 }
1311}
1312
1313
1314
1315void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1316{
1317 if (mEffectClient != 0) {
1318 mEffectClient->enableStatusChanged(enabled);
1319 }
1320}
1321
1322status_t AudioFlinger::EffectHandle::onTransact(
1323 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1324{
1325 return BnEffect::onTransact(code, data, reply, flags);
1326}
1327
1328
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001329void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001330{
1331 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1332
Marco Nelissenb2208842014-02-07 14:00:50 -08001333 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001334 (mClient == 0) ? getpid_cached : mClient->pid(),
1335 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001336 mHasControl ? "yes" : "no",
1337 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001338 mCblk ? mCblk->clientIndex : 0,
1339 mCblk ? mCblk->serverIndex : 0
1340 );
1341
1342 if (locked) {
1343 mCblk->lock.unlock();
1344 }
1345}
1346
1347#undef LOG_TAG
1348#define LOG_TAG "AudioFlinger::EffectChain"
1349
1350AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001351 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001352 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1353 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001354 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), mForceVolume(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001355{
1356 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1357 if (thread == NULL) {
1358 return;
1359 }
1360 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1361 thread->frameCount();
1362}
1363
1364AudioFlinger::EffectChain::~EffectChain()
1365{
1366 if (mOwnInBuffer) {
1367 delete mInBuffer;
1368 }
1369
1370}
1371
1372// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1373sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1374 effect_descriptor_t *descriptor)
1375{
1376 size_t size = mEffects.size();
1377
1378 for (size_t i = 0; i < size; i++) {
1379 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1380 return mEffects[i];
1381 }
1382 }
1383 return 0;
1384}
1385
1386// getEffectFromId_l() must be called with ThreadBase::mLock held
1387sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1388{
1389 size_t size = mEffects.size();
1390
1391 for (size_t i = 0; i < size; i++) {
1392 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1393 if (id == 0 || mEffects[i]->id() == id) {
1394 return mEffects[i];
1395 }
1396 }
1397 return 0;
1398}
1399
1400// getEffectFromType_l() must be called with ThreadBase::mLock held
1401sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1402 const effect_uuid_t *type)
1403{
1404 size_t size = mEffects.size();
1405
1406 for (size_t i = 0; i < size; i++) {
1407 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1408 return mEffects[i];
1409 }
1410 }
1411 return 0;
1412}
1413
1414void AudioFlinger::EffectChain::clearInputBuffer()
1415{
1416 Mutex::Autolock _l(mLock);
1417 sp<ThreadBase> thread = mThread.promote();
1418 if (thread == 0) {
1419 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1420 return;
1421 }
1422 clearInputBuffer_l(thread);
1423}
1424
1425// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001426void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001427{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001428 // TODO: This will change in the future, depending on multichannel
1429 // and sample format changes for effects.
1430 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1431 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001432 const size_t frameSize =
1433 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001434 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001435}
1436
1437// Must be called with EffectChain::mLock locked
1438void AudioFlinger::EffectChain::process_l()
1439{
1440 sp<ThreadBase> thread = mThread.promote();
1441 if (thread == 0) {
1442 ALOGW("process_l(): cannot promote mixer thread");
1443 return;
1444 }
1445 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1446 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001447 // never process effects when:
1448 // - on an OFFLOAD thread
1449 // - no more tracks are on the session and the effect tail has been rendered
1450 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001451 if (!isGlobalSession) {
1452 bool tracksOnSession = (trackCnt() != 0);
1453
1454 if (!tracksOnSession && mTailBufferCount == 0) {
1455 doProcess = false;
1456 }
1457
1458 if (activeTrackCnt() == 0) {
1459 // if no track is active and the effect tail has not been rendered,
1460 // the input buffer must be cleared here as the mixer process will not do it
1461 if (tracksOnSession || mTailBufferCount > 0) {
1462 clearInputBuffer_l(thread);
1463 if (mTailBufferCount > 0) {
1464 mTailBufferCount--;
1465 }
1466 }
1467 }
1468 }
1469
1470 size_t size = mEffects.size();
1471 if (doProcess) {
1472 for (size_t i = 0; i < size; i++) {
1473 mEffects[i]->process();
1474 }
1475 }
1476 for (size_t i = 0; i < size; i++) {
1477 mEffects[i]->updateState();
1478 }
1479}
1480
1481// addEffect_l() must be called with PlaybackThread::mLock held
1482status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1483{
1484 effect_descriptor_t desc = effect->desc();
1485 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1486
1487 Mutex::Autolock _l(mLock);
1488 effect->setChain(this);
1489 sp<ThreadBase> thread = mThread.promote();
1490 if (thread == 0) {
1491 return NO_INIT;
1492 }
1493 effect->setThread(thread);
1494
1495 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1496 // Auxiliary effects are inserted at the beginning of mEffects vector as
1497 // they are processed first and accumulated in chain input buffer
1498 mEffects.insertAt(effect, 0);
1499
1500 // the input buffer for auxiliary effect contains mono samples in
1501 // 32 bit format. This is to avoid saturation in AudoMixer
1502 // accumulation stage. Saturation is done in EffectModule::process() before
1503 // calling the process in effect engine
1504 size_t numSamples = thread->frameCount();
1505 int32_t *buffer = new int32_t[numSamples];
1506 memset(buffer, 0, numSamples * sizeof(int32_t));
1507 effect->setInBuffer((int16_t *)buffer);
1508 // auxiliary effects output samples to chain input buffer for further processing
1509 // by insert effects
1510 effect->setOutBuffer(mInBuffer);
1511 } else {
1512 // Insert effects are inserted at the end of mEffects vector as they are processed
1513 // after track and auxiliary effects.
1514 // Insert effect order as a function of indicated preference:
1515 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1516 // another effect is present
1517 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1518 // last effect claiming first position
1519 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1520 // first effect claiming last position
1521 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1522 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1523 // already present
1524
1525 size_t size = mEffects.size();
1526 size_t idx_insert = size;
1527 ssize_t idx_insert_first = -1;
1528 ssize_t idx_insert_last = -1;
1529
1530 for (size_t i = 0; i < size; i++) {
1531 effect_descriptor_t d = mEffects[i]->desc();
1532 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1533 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1534 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1535 // check invalid effect chaining combinations
1536 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1537 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1538 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1539 desc.name, d.name);
1540 return INVALID_OPERATION;
1541 }
1542 // remember position of first insert effect and by default
1543 // select this as insert position for new effect
1544 if (idx_insert == size) {
1545 idx_insert = i;
1546 }
1547 // remember position of last insert effect claiming
1548 // first position
1549 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1550 idx_insert_first = i;
1551 }
1552 // remember position of first insert effect claiming
1553 // last position
1554 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1555 idx_insert_last == -1) {
1556 idx_insert_last = i;
1557 }
1558 }
1559 }
1560
1561 // modify idx_insert from first position if needed
1562 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1563 if (idx_insert_last != -1) {
1564 idx_insert = idx_insert_last;
1565 } else {
1566 idx_insert = size;
1567 }
1568 } else {
1569 if (idx_insert_first != -1) {
1570 idx_insert = idx_insert_first + 1;
1571 }
1572 }
1573
1574 // always read samples from chain input buffer
1575 effect->setInBuffer(mInBuffer);
1576
1577 // if last effect in the chain, output samples to chain
1578 // output buffer, otherwise to chain input buffer
1579 if (idx_insert == size) {
1580 if (idx_insert != 0) {
1581 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1582 mEffects[idx_insert-1]->configure();
1583 }
1584 effect->setOutBuffer(mOutBuffer);
1585 } else {
1586 effect->setOutBuffer(mInBuffer);
1587 }
1588 mEffects.insertAt(effect, idx_insert);
1589
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001590 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001591 idx_insert);
1592 }
1593 effect->configure();
1594 return NO_ERROR;
1595}
1596
1597// removeEffect_l() must be called with PlaybackThread::mLock held
1598size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1599{
1600 Mutex::Autolock _l(mLock);
1601 size_t size = mEffects.size();
1602 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1603
1604 for (size_t i = 0; i < size; i++) {
1605 if (effect == mEffects[i]) {
1606 // calling stop here will remove pre-processing effect from the audio HAL.
1607 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1608 // the middle of a read from audio HAL
1609 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1610 mEffects[i]->state() == EffectModule::STOPPING) {
1611 mEffects[i]->stop();
1612 }
1613 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1614 delete[] effect->inBuffer();
1615 } else {
1616 if (i == size - 1 && i != 0) {
1617 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1618 mEffects[i - 1]->configure();
1619 }
1620 }
1621 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001622 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001623 this, i);
1624 break;
1625 }
1626 }
1627
1628 return mEffects.size();
1629}
1630
1631// setDevice_l() must be called with PlaybackThread::mLock held
1632void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1633{
1634 size_t size = mEffects.size();
1635 for (size_t i = 0; i < size; i++) {
1636 mEffects[i]->setDevice(device);
1637 }
1638}
1639
1640// setMode_l() must be called with PlaybackThread::mLock held
1641void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1642{
1643 size_t size = mEffects.size();
1644 for (size_t i = 0; i < size; i++) {
1645 mEffects[i]->setMode(mode);
1646 }
1647}
1648
1649// setAudioSource_l() must be called with PlaybackThread::mLock held
1650void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1651{
1652 size_t size = mEffects.size();
1653 for (size_t i = 0; i < size; i++) {
1654 mEffects[i]->setAudioSource(source);
1655 }
1656}
1657
1658// setVolume_l() must be called with PlaybackThread::mLock held
1659bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1660{
1661 uint32_t newLeft = *left;
1662 uint32_t newRight = *right;
1663 bool hasControl = false;
1664 int ctrlIdx = -1;
1665 size_t size = mEffects.size();
1666
1667 // first update volume controller
1668 for (size_t i = size; i > 0; i--) {
1669 if (mEffects[i - 1]->isProcessEnabled() &&
1670 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1671 ctrlIdx = i - 1;
1672 hasControl = true;
1673 break;
1674 }
1675 }
1676
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001677 if (!isVolumeForced() && ctrlIdx == mVolumeCtrlIdx &&
1678 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001679 if (hasControl) {
1680 *left = mNewLeftVolume;
1681 *right = mNewRightVolume;
1682 }
1683 return hasControl;
1684 }
1685
1686 mVolumeCtrlIdx = ctrlIdx;
1687 mLeftVolume = newLeft;
1688 mRightVolume = newRight;
1689
1690 // second get volume update from volume controller
1691 if (ctrlIdx >= 0) {
1692 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1693 mNewLeftVolume = newLeft;
1694 mNewRightVolume = newRight;
1695 }
1696 // then indicate volume to all other effects in chain.
1697 // Pass altered volume to effects before volume controller
1698 // and requested volume to effects after controller
1699 uint32_t lVol = newLeft;
1700 uint32_t rVol = newRight;
1701
1702 for (size_t i = 0; i < size; i++) {
1703 if ((int)i == ctrlIdx) {
1704 continue;
1705 }
1706 // this also works for ctrlIdx == -1 when there is no volume controller
1707 if ((int)i > ctrlIdx) {
1708 lVol = *left;
1709 rVol = *right;
1710 }
1711 mEffects[i]->setVolume(&lVol, &rVol, false);
1712 }
1713 *left = newLeft;
1714 *right = newRight;
1715
1716 return hasControl;
1717}
1718
Eric Laurent1b928682014-10-02 19:41:47 -07001719void AudioFlinger::EffectChain::syncHalEffectsState()
1720{
1721 Mutex::Autolock _l(mLock);
1722 for (size_t i = 0; i < mEffects.size(); i++) {
1723 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1724 mEffects[i]->state() == EffectModule::STOPPING) {
1725 mEffects[i]->addEffectToHal_l();
1726 }
1727 }
1728}
1729
Eric Laurentca7cc822012-11-19 14:55:58 -08001730void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1731{
1732 const size_t SIZE = 256;
1733 char buffer[SIZE];
1734 String8 result;
1735
Marco Nelissenb2208842014-02-07 14:00:50 -08001736 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001737 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001738 result.append(buffer);
1739
Marco Nelissenb2208842014-02-07 14:00:50 -08001740 if (numEffects) {
1741 bool locked = AudioFlinger::dumpTryLock(mLock);
1742 // failed to lock - AudioFlinger is probably deadlocked
1743 if (!locked) {
1744 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001745 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001746
Marco Nelissenb2208842014-02-07 14:00:50 -08001747 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001748 snprintf(buffer, SIZE, "\t%p %p %d\n",
1749 mInBuffer,
1750 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001751 mActiveTrackCnt);
1752 result.append(buffer);
1753 write(fd, result.string(), result.size());
1754
1755 for (size_t i = 0; i < numEffects; ++i) {
1756 sp<EffectModule> effect = mEffects[i];
1757 if (effect != 0) {
1758 effect->dump(fd, args);
1759 }
1760 }
1761
1762 if (locked) {
1763 mLock.unlock();
1764 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001765 }
1766}
1767
1768// must be called with ThreadBase::mLock held
1769void AudioFlinger::EffectChain::setEffectSuspended_l(
1770 const effect_uuid_t *type, bool suspend)
1771{
1772 sp<SuspendedEffectDesc> desc;
1773 // use effect type UUID timelow as key as there is no real risk of identical
1774 // timeLow fields among effect type UUIDs.
1775 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1776 if (suspend) {
1777 if (index >= 0) {
1778 desc = mSuspendedEffects.valueAt(index);
1779 } else {
1780 desc = new SuspendedEffectDesc();
1781 desc->mType = *type;
1782 mSuspendedEffects.add(type->timeLow, desc);
1783 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1784 }
1785 if (desc->mRefCount++ == 0) {
1786 sp<EffectModule> effect = getEffectIfEnabled(type);
1787 if (effect != 0) {
1788 desc->mEffect = effect;
1789 effect->setSuspended(true);
1790 effect->setEnabled(false);
1791 }
1792 }
1793 } else {
1794 if (index < 0) {
1795 return;
1796 }
1797 desc = mSuspendedEffects.valueAt(index);
1798 if (desc->mRefCount <= 0) {
1799 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1800 desc->mRefCount = 1;
1801 }
1802 if (--desc->mRefCount == 0) {
1803 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1804 if (desc->mEffect != 0) {
1805 sp<EffectModule> effect = desc->mEffect.promote();
1806 if (effect != 0) {
1807 effect->setSuspended(false);
1808 effect->lock();
1809 EffectHandle *handle = effect->controlHandle_l();
1810 if (handle != NULL && !handle->destroyed_l()) {
1811 effect->setEnabled_l(handle->enabled());
1812 }
1813 effect->unlock();
1814 }
1815 desc->mEffect.clear();
1816 }
1817 mSuspendedEffects.removeItemsAt(index);
1818 }
1819 }
1820}
1821
1822// must be called with ThreadBase::mLock held
1823void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1824{
1825 sp<SuspendedEffectDesc> desc;
1826
1827 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1828 if (suspend) {
1829 if (index >= 0) {
1830 desc = mSuspendedEffects.valueAt(index);
1831 } else {
1832 desc = new SuspendedEffectDesc();
1833 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1834 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1835 }
1836 if (desc->mRefCount++ == 0) {
1837 Vector< sp<EffectModule> > effects;
1838 getSuspendEligibleEffects(effects);
1839 for (size_t i = 0; i < effects.size(); i++) {
1840 setEffectSuspended_l(&effects[i]->desc().type, true);
1841 }
1842 }
1843 } else {
1844 if (index < 0) {
1845 return;
1846 }
1847 desc = mSuspendedEffects.valueAt(index);
1848 if (desc->mRefCount <= 0) {
1849 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1850 desc->mRefCount = 1;
1851 }
1852 if (--desc->mRefCount == 0) {
1853 Vector<const effect_uuid_t *> types;
1854 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1855 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1856 continue;
1857 }
1858 types.add(&mSuspendedEffects.valueAt(i)->mType);
1859 }
1860 for (size_t i = 0; i < types.size(); i++) {
1861 setEffectSuspended_l(types[i], false);
1862 }
1863 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1864 mSuspendedEffects.keyAt(index));
1865 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1866 }
1867 }
1868}
1869
1870
1871// The volume effect is used for automated tests only
1872#ifndef OPENSL_ES_H_
1873static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1874 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1875const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1876#endif //OPENSL_ES_H_
1877
1878bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1879{
1880 // auxiliary effects and visualizer are never suspended on output mix
1881 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1882 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1883 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1884 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1885 return false;
1886 }
1887 return true;
1888}
1889
1890void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1891 Vector< sp<AudioFlinger::EffectModule> > &effects)
1892{
1893 effects.clear();
1894 for (size_t i = 0; i < mEffects.size(); i++) {
1895 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1896 effects.add(mEffects[i]);
1897 }
1898 }
1899}
1900
1901sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1902 const effect_uuid_t *type)
1903{
1904 sp<EffectModule> effect = getEffectFromType_l(type);
1905 return effect != 0 && effect->isEnabled() ? effect : 0;
1906}
1907
1908void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1909 bool enabled)
1910{
1911 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1912 if (enabled) {
1913 if (index < 0) {
1914 // if the effect is not suspend check if all effects are suspended
1915 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1916 if (index < 0) {
1917 return;
1918 }
1919 if (!isEffectEligibleForSuspend(effect->desc())) {
1920 return;
1921 }
1922 setEffectSuspended_l(&effect->desc().type, enabled);
1923 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1924 if (index < 0) {
1925 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1926 return;
1927 }
1928 }
1929 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1930 effect->desc().type.timeLow);
1931 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1932 // if effect is requested to suspended but was not yet enabled, supend it now.
1933 if (desc->mEffect == 0) {
1934 desc->mEffect = effect;
1935 effect->setEnabled(false);
1936 effect->setSuspended(true);
1937 }
1938 } else {
1939 if (index < 0) {
1940 return;
1941 }
1942 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1943 effect->desc().type.timeLow);
1944 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1945 desc->mEffect.clear();
1946 effect->setSuspended(false);
1947 }
1948}
1949
Eric Laurent5baf2af2013-09-12 17:37:00 -07001950bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001951{
1952 Mutex::Autolock _l(mLock);
1953 size_t size = mEffects.size();
1954 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001955 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001956 return true;
1957 }
1958 }
1959 return false;
1960}
1961
Eric Laurentaaa44472014-09-12 17:41:50 -07001962void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
1963{
1964 Mutex::Autolock _l(mLock);
1965 mThread = thread;
1966 for (size_t i = 0; i < mEffects.size(); i++) {
1967 mEffects[i]->setThread(thread);
1968 }
1969}
1970
Glenn Kasten63238ef2015-03-02 15:50:29 -08001971} // namespace android