blob: 00304b2325f443d817cbb2fb804092bbac907df7 [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;
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
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700383 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800384 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
546status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
547 uint32_t cmdSize,
548 void *pCmdData,
549 uint32_t *replySize,
550 void *pReplyData)
551{
552 Mutex::Autolock _l(mLock);
553 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
554
555 if (mState == DESTROYED || mEffectInterface == NULL) {
556 return NO_INIT;
557 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700558 if (mStatus != NO_ERROR) {
559 return mStatus;
560 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800561 status_t status = (*mEffectInterface)->command(mEffectInterface,
562 cmdCode,
563 cmdSize,
564 pCmdData,
565 replySize,
566 pReplyData);
567 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
568 uint32_t size = (replySize == NULL) ? 0 : *replySize;
569 for (size_t i = 1; i < mHandles.size(); i++) {
570 EffectHandle *h = mHandles[i];
571 if (h != NULL && !h->destroyed_l()) {
572 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
573 }
574 }
575 }
576 return status;
577}
578
579status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
580{
581 Mutex::Autolock _l(mLock);
582 return setEnabled_l(enabled);
583}
584
585// must be called with EffectModule::mLock held
586status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
587{
588
589 ALOGV("setEnabled %p enabled %d", this, enabled);
590
591 if (enabled != isEnabled()) {
592 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
593 if (enabled && status != NO_ERROR) {
594 return status;
595 }
596
597 switch (mState) {
598 // going from disabled to enabled
599 case IDLE:
600 mState = STARTING;
601 break;
602 case STOPPED:
603 mState = RESTART;
604 break;
605 case STOPPING:
606 mState = ACTIVE;
607 break;
608
609 // going from enabled to disabled
610 case RESTART:
611 mState = STOPPED;
612 break;
613 case STARTING:
614 mState = IDLE;
615 break;
616 case ACTIVE:
617 mState = STOPPING;
618 break;
619 case DESTROYED:
620 return NO_ERROR; // simply ignore as we are being destroyed
621 }
622 for (size_t i = 1; i < mHandles.size(); i++) {
623 EffectHandle *h = mHandles[i];
624 if (h != NULL && !h->destroyed_l()) {
625 h->setEnabled(enabled);
626 }
627 }
628 }
629 return NO_ERROR;
630}
631
632bool AudioFlinger::EffectModule::isEnabled() const
633{
634 switch (mState) {
635 case RESTART:
636 case STARTING:
637 case ACTIVE:
638 return true;
639 case IDLE:
640 case STOPPING:
641 case STOPPED:
642 case DESTROYED:
643 default:
644 return false;
645 }
646}
647
648bool AudioFlinger::EffectModule::isProcessEnabled() const
649{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700650 if (mStatus != NO_ERROR) {
651 return false;
652 }
653
Eric Laurentca7cc822012-11-19 14:55:58 -0800654 switch (mState) {
655 case RESTART:
656 case ACTIVE:
657 case STOPPING:
658 case STOPPED:
659 return true;
660 case IDLE:
661 case STARTING:
662 case DESTROYED:
663 default:
664 return false;
665 }
666}
667
668status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
669{
670 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700671 if (mStatus != NO_ERROR) {
672 return mStatus;
673 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800674 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800675 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
676 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
677 if (isProcessEnabled() &&
678 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
679 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800680 uint32_t volume[2];
681 uint32_t *pVolume = NULL;
682 uint32_t size = sizeof(volume);
683 volume[0] = *left;
684 volume[1] = *right;
685 if (controller) {
686 pVolume = volume;
687 }
688 status = (*mEffectInterface)->command(mEffectInterface,
689 EFFECT_CMD_SET_VOLUME,
690 size,
691 volume,
692 &size,
693 pVolume);
694 if (controller && status == NO_ERROR && size == sizeof(volume)) {
695 *left = volume[0];
696 *right = volume[1];
697 }
698 }
699 return status;
700}
701
702status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
703{
704 if (device == AUDIO_DEVICE_NONE) {
705 return NO_ERROR;
706 }
707
708 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700709 if (mStatus != NO_ERROR) {
710 return mStatus;
711 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800712 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700713 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800714 status_t cmdStatus;
715 uint32_t size = sizeof(status_t);
716 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
717 EFFECT_CMD_SET_INPUT_DEVICE;
718 status = (*mEffectInterface)->command(mEffectInterface,
719 cmd,
720 sizeof(uint32_t),
721 &device,
722 &size,
723 &cmdStatus);
724 }
725 return status;
726}
727
728status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
729{
730 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700731 if (mStatus != NO_ERROR) {
732 return mStatus;
733 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800734 status_t status = NO_ERROR;
735 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
736 status_t cmdStatus;
737 uint32_t size = sizeof(status_t);
738 status = (*mEffectInterface)->command(mEffectInterface,
739 EFFECT_CMD_SET_AUDIO_MODE,
740 sizeof(audio_mode_t),
741 &mode,
742 &size,
743 &cmdStatus);
744 if (status == NO_ERROR) {
745 status = cmdStatus;
746 }
747 }
748 return status;
749}
750
751status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
752{
753 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700754 if (mStatus != NO_ERROR) {
755 return mStatus;
756 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800757 status_t status = NO_ERROR;
758 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
759 uint32_t size = 0;
760 status = (*mEffectInterface)->command(mEffectInterface,
761 EFFECT_CMD_SET_AUDIO_SOURCE,
762 sizeof(audio_source_t),
763 &source,
764 &size,
765 NULL);
766 }
767 return status;
768}
769
770void AudioFlinger::EffectModule::setSuspended(bool suspended)
771{
772 Mutex::Autolock _l(mLock);
773 mSuspended = suspended;
774}
775
776bool AudioFlinger::EffectModule::suspended() const
777{
778 Mutex::Autolock _l(mLock);
779 return mSuspended;
780}
781
782bool AudioFlinger::EffectModule::purgeHandles()
783{
784 bool enabled = false;
785 Mutex::Autolock _l(mLock);
786 for (size_t i = 0; i < mHandles.size(); i++) {
787 EffectHandle *handle = mHandles[i];
788 if (handle != NULL && !handle->destroyed_l()) {
789 handle->effect().clear();
790 if (handle->hasControl()) {
791 enabled = handle->enabled();
792 }
793 }
794 }
795 return enabled;
796}
797
Eric Laurent5baf2af2013-09-12 17:37:00 -0700798status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
799{
800 Mutex::Autolock _l(mLock);
801 if (mStatus != NO_ERROR) {
802 return mStatus;
803 }
804 status_t status = NO_ERROR;
805 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
806 status_t cmdStatus;
807 uint32_t size = sizeof(status_t);
808 effect_offload_param_t cmd;
809
810 cmd.isOffload = offloaded;
811 cmd.ioHandle = io;
812 status = (*mEffectInterface)->command(mEffectInterface,
813 EFFECT_CMD_OFFLOAD,
814 sizeof(effect_offload_param_t),
815 &cmd,
816 &size,
817 &cmdStatus);
818 if (status == NO_ERROR) {
819 status = cmdStatus;
820 }
821 mOffloaded = (status == NO_ERROR) ? offloaded : false;
822 } else {
823 if (offloaded) {
824 status = INVALID_OPERATION;
825 }
826 mOffloaded = false;
827 }
828 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
829 return status;
830}
831
832bool AudioFlinger::EffectModule::isOffloaded() const
833{
834 Mutex::Autolock _l(mLock);
835 return mOffloaded;
836}
837
Marco Nelissenb2208842014-02-07 14:00:50 -0800838String8 effectFlagsToString(uint32_t flags) {
839 String8 s;
840
841 s.append("conn. mode: ");
842 switch (flags & EFFECT_FLAG_TYPE_MASK) {
843 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
844 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
845 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
846 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
847 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
848 default: s.append("unknown/reserved"); break;
849 }
850 s.append(", ");
851
852 s.append("insert pref: ");
853 switch (flags & EFFECT_FLAG_INSERT_MASK) {
854 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
855 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
856 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
857 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
858 default: s.append("unknown/reserved"); break;
859 }
860 s.append(", ");
861
862 s.append("volume mgmt: ");
863 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
864 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
865 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
866 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
867 default: s.append("unknown/reserved"); break;
868 }
869 s.append(", ");
870
871 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
872 if (devind) {
873 s.append("device indication: ");
874 switch (devind) {
875 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
876 default: s.append("unknown/reserved"); break;
877 }
878 s.append(", ");
879 }
880
881 s.append("input mode: ");
882 switch (flags & EFFECT_FLAG_INPUT_MASK) {
883 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
884 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
885 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
886 default: s.append("not set"); break;
887 }
888 s.append(", ");
889
890 s.append("output mode: ");
891 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
892 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
893 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
894 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
895 default: s.append("not set"); break;
896 }
897 s.append(", ");
898
899 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
900 if (accel) {
901 s.append("hardware acceleration: ");
902 switch (accel) {
903 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
904 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
905 default: s.append("unknown/reserved"); break;
906 }
907 s.append(", ");
908 }
909
910 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
911 if (modeind) {
912 s.append("mode indication: ");
913 switch (modeind) {
914 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
915 default: s.append("unknown/reserved"); break;
916 }
917 s.append(", ");
918 }
919
920 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
921 if (srcind) {
922 s.append("source indication: ");
923 switch (srcind) {
924 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
925 default: s.append("unknown/reserved"); break;
926 }
927 s.append(", ");
928 }
929
930 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
931 s.append("offloadable, ");
932 }
933
934 int len = s.length();
935 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -0700936 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -0800937 s.unlockBuffer(len - 2);
938 }
939 return s;
940}
941
942
Glenn Kasten0f11b512014-01-31 16:18:54 -0800943void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -0800944{
945 const size_t SIZE = 256;
946 char buffer[SIZE];
947 String8 result;
948
949 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
950 result.append(buffer);
951
952 bool locked = AudioFlinger::dumpTryLock(mLock);
953 // failed to lock - AudioFlinger is probably deadlocked
954 if (!locked) {
955 result.append("\t\tCould not lock Fx mutex:\n");
956 }
957
958 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000959 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
960 mSessionId, mStatus, mState, mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -0800961 result.append(buffer);
962
963 result.append("\t\tDescriptor:\n");
964 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
965 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
966 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
967 mDescriptor.uuid.node[2],
968 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
969 result.append(buffer);
970 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
971 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
972 mDescriptor.type.timeHiAndVersion,
973 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
974 mDescriptor.type.node[2],
975 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
976 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -0800977 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -0800978 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -0800979 mDescriptor.flags,
980 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -0800981 result.append(buffer);
982 snprintf(buffer, SIZE, "\t\t- name: %s\n",
983 mDescriptor.name);
984 result.append(buffer);
985 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
986 mDescriptor.implementor);
987 result.append(buffer);
988
989 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000990 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000991 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -0800992 mConfig.inputCfg.buffer.frameCount,
993 mConfig.inputCfg.samplingRate,
994 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -0800995 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000996 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000997 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -0800998 result.append(buffer);
999
1000 result.append("\t\t- Output configuration:\n");
1001 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001002 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001003 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001004 mConfig.outputCfg.buffer.frameCount,
1005 mConfig.outputCfg.samplingRate,
1006 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001007 mConfig.outputCfg.format,
1008 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001009 result.append(buffer);
1010
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001011 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001012 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001013 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001014 for (size_t i = 0; i < mHandles.size(); ++i) {
1015 EffectHandle *handle = mHandles[i];
1016 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001017 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001018 result.append(buffer);
1019 }
1020 }
1021
Eric Laurentca7cc822012-11-19 14:55:58 -08001022 write(fd, result.string(), result.length());
1023
1024 if (locked) {
1025 mLock.unlock();
1026 }
1027}
1028
1029// ----------------------------------------------------------------------------
1030// EffectHandle implementation
1031// ----------------------------------------------------------------------------
1032
1033#undef LOG_TAG
1034#define LOG_TAG "AudioFlinger::EffectHandle"
1035
1036AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1037 const sp<AudioFlinger::Client>& client,
1038 const sp<IEffectClient>& effectClient,
1039 int32_t priority)
1040 : BnEffect(),
1041 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1042 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1043{
1044 ALOGV("constructor %p", this);
1045
1046 if (client == 0) {
1047 return;
1048 }
1049 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1050 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001051 if (mCblkMemory == 0 ||
1052 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001053 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001054 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001055 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001056 return;
1057 }
Glenn Kastene75da402013-11-20 13:54:52 -08001058 new(mCblk) effect_param_cblk_t();
1059 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001060}
1061
1062AudioFlinger::EffectHandle::~EffectHandle()
1063{
1064 ALOGV("Destructor %p", this);
1065
1066 if (mEffect == 0) {
1067 mDestroyed = true;
1068 return;
1069 }
1070 mEffect->lock();
1071 mDestroyed = true;
1072 mEffect->unlock();
1073 disconnect(false);
1074}
1075
Glenn Kastene75da402013-11-20 13:54:52 -08001076status_t AudioFlinger::EffectHandle::initCheck()
1077{
1078 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1079}
1080
Eric Laurentca7cc822012-11-19 14:55:58 -08001081status_t AudioFlinger::EffectHandle::enable()
1082{
1083 ALOGV("enable %p", this);
1084 if (!mHasControl) {
1085 return INVALID_OPERATION;
1086 }
1087 if (mEffect == 0) {
1088 return DEAD_OBJECT;
1089 }
1090
1091 if (mEnabled) {
1092 return NO_ERROR;
1093 }
1094
1095 mEnabled = true;
1096
1097 sp<ThreadBase> thread = mEffect->thread().promote();
1098 if (thread != 0) {
1099 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1100 }
1101
1102 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1103 if (mEffect->suspended()) {
1104 return NO_ERROR;
1105 }
1106
1107 status_t status = mEffect->setEnabled(true);
1108 if (status != NO_ERROR) {
1109 if (thread != 0) {
1110 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1111 }
1112 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001113 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001114 if (thread != 0) {
1115 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001116 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001117 Mutex::Autolock _l(t->mLock);
1118 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001119 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001120 if (!mEffect->isOffloadable()) {
1121 if (thread->type() == ThreadBase::OFFLOAD) {
1122 PlaybackThread *t = (PlaybackThread *)thread.get();
1123 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1124 }
1125 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1126 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1127 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001128 }
1129 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001130 }
1131 return status;
1132}
1133
1134status_t AudioFlinger::EffectHandle::disable()
1135{
1136 ALOGV("disable %p", this);
1137 if (!mHasControl) {
1138 return INVALID_OPERATION;
1139 }
1140 if (mEffect == 0) {
1141 return DEAD_OBJECT;
1142 }
1143
1144 if (!mEnabled) {
1145 return NO_ERROR;
1146 }
1147 mEnabled = false;
1148
1149 if (mEffect->suspended()) {
1150 return NO_ERROR;
1151 }
1152
1153 status_t status = mEffect->setEnabled(false);
1154
1155 sp<ThreadBase> thread = mEffect->thread().promote();
1156 if (thread != 0) {
1157 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001158 if (thread->type() == ThreadBase::OFFLOAD) {
1159 PlaybackThread *t = (PlaybackThread *)thread.get();
1160 Mutex::Autolock _l(t->mLock);
1161 t->broadcast_l();
1162 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001163 }
1164
1165 return status;
1166}
1167
1168void AudioFlinger::EffectHandle::disconnect()
1169{
1170 disconnect(true);
1171}
1172
1173void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1174{
1175 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1176 if (mEffect == 0) {
1177 return;
1178 }
1179 // restore suspended effects if the disconnected handle was enabled and the last one.
1180 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1181 sp<ThreadBase> thread = mEffect->thread().promote();
1182 if (thread != 0) {
1183 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1184 }
1185 }
1186
1187 // release sp on module => module destructor can be called now
1188 mEffect.clear();
1189 if (mClient != 0) {
1190 if (mCblk != NULL) {
1191 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1192 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1193 }
1194 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001195 // Client destructor must run with AudioFlinger client mutex locked
1196 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001197 mClient.clear();
1198 }
1199}
1200
1201status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1202 uint32_t cmdSize,
1203 void *pCmdData,
1204 uint32_t *replySize,
1205 void *pReplyData)
1206{
1207 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1208 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1209
1210 // only get parameter command is permitted for applications not controlling the effect
1211 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1212 return INVALID_OPERATION;
1213 }
1214 if (mEffect == 0) {
1215 return DEAD_OBJECT;
1216 }
1217 if (mClient == 0) {
1218 return INVALID_OPERATION;
1219 }
1220
1221 // handle commands that are not forwarded transparently to effect engine
1222 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1223 // No need to trylock() here as this function is executed in the binder thread serving a
1224 // particular client process: no risk to block the whole media server process or mixer
1225 // threads if we are stuck here
1226 Mutex::Autolock _l(mCblk->lock);
1227 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1228 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1229 mCblk->serverIndex = 0;
1230 mCblk->clientIndex = 0;
1231 return BAD_VALUE;
1232 }
1233 status_t status = NO_ERROR;
1234 while (mCblk->serverIndex < mCblk->clientIndex) {
1235 int reply;
1236 uint32_t rsize = sizeof(int);
1237 int *p = (int *)(mBuffer + mCblk->serverIndex);
1238 int size = *p++;
1239 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1240 ALOGW("command(): invalid parameter block size");
1241 break;
1242 }
1243 effect_param_t *param = (effect_param_t *)p;
1244 if (param->psize == 0 || param->vsize == 0) {
1245 ALOGW("command(): null parameter or value size");
1246 mCblk->serverIndex += size;
1247 continue;
1248 }
1249 uint32_t psize = sizeof(effect_param_t) +
1250 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1251 param->vsize;
1252 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1253 psize,
1254 p,
1255 &rsize,
1256 &reply);
1257 // stop at first error encountered
1258 if (ret != NO_ERROR) {
1259 status = ret;
1260 *(int *)pReplyData = reply;
1261 break;
1262 } else if (reply != NO_ERROR) {
1263 *(int *)pReplyData = reply;
1264 break;
1265 }
1266 mCblk->serverIndex += size;
1267 }
1268 mCblk->serverIndex = 0;
1269 mCblk->clientIndex = 0;
1270 return status;
1271 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1272 *(int *)pReplyData = NO_ERROR;
1273 return enable();
1274 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1275 *(int *)pReplyData = NO_ERROR;
1276 return disable();
1277 }
1278
1279 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1280}
1281
1282void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1283{
1284 ALOGV("setControl %p control %d", this, hasControl);
1285
1286 mHasControl = hasControl;
1287 mEnabled = enabled;
1288
1289 if (signal && mEffectClient != 0) {
1290 mEffectClient->controlStatusChanged(hasControl);
1291 }
1292}
1293
1294void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1295 uint32_t cmdSize,
1296 void *pCmdData,
1297 uint32_t replySize,
1298 void *pReplyData)
1299{
1300 if (mEffectClient != 0) {
1301 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1302 }
1303}
1304
1305
1306
1307void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1308{
1309 if (mEffectClient != 0) {
1310 mEffectClient->enableStatusChanged(enabled);
1311 }
1312}
1313
1314status_t AudioFlinger::EffectHandle::onTransact(
1315 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1316{
1317 return BnEffect::onTransact(code, data, reply, flags);
1318}
1319
1320
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001321void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001322{
1323 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1324
Marco Nelissenb2208842014-02-07 14:00:50 -08001325 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001326 (mClient == 0) ? getpid_cached : mClient->pid(),
1327 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001328 mHasControl ? "yes" : "no",
1329 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001330 mCblk ? mCblk->clientIndex : 0,
1331 mCblk ? mCblk->serverIndex : 0
1332 );
1333
1334 if (locked) {
1335 mCblk->lock.unlock();
1336 }
1337}
1338
1339#undef LOG_TAG
1340#define LOG_TAG "AudioFlinger::EffectChain"
1341
1342AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001343 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001344 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1345 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001346 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX), mForceVolume(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001347{
1348 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1349 if (thread == NULL) {
1350 return;
1351 }
1352 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1353 thread->frameCount();
1354}
1355
1356AudioFlinger::EffectChain::~EffectChain()
1357{
1358 if (mOwnInBuffer) {
1359 delete mInBuffer;
1360 }
1361
1362}
1363
1364// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1365sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1366 effect_descriptor_t *descriptor)
1367{
1368 size_t size = mEffects.size();
1369
1370 for (size_t i = 0; i < size; i++) {
1371 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1372 return mEffects[i];
1373 }
1374 }
1375 return 0;
1376}
1377
1378// getEffectFromId_l() must be called with ThreadBase::mLock held
1379sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1380{
1381 size_t size = mEffects.size();
1382
1383 for (size_t i = 0; i < size; i++) {
1384 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1385 if (id == 0 || mEffects[i]->id() == id) {
1386 return mEffects[i];
1387 }
1388 }
1389 return 0;
1390}
1391
1392// getEffectFromType_l() must be called with ThreadBase::mLock held
1393sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1394 const effect_uuid_t *type)
1395{
1396 size_t size = mEffects.size();
1397
1398 for (size_t i = 0; i < size; i++) {
1399 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1400 return mEffects[i];
1401 }
1402 }
1403 return 0;
1404}
1405
1406void AudioFlinger::EffectChain::clearInputBuffer()
1407{
1408 Mutex::Autolock _l(mLock);
1409 sp<ThreadBase> thread = mThread.promote();
1410 if (thread == 0) {
1411 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1412 return;
1413 }
1414 clearInputBuffer_l(thread);
1415}
1416
1417// Must be called with EffectChain::mLock locked
1418void AudioFlinger::EffectChain::clearInputBuffer_l(sp<ThreadBase> thread)
1419{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001420 // TODO: This will change in the future, depending on multichannel
1421 // and sample format changes for effects.
1422 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1423 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001424 const size_t frameSize =
1425 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001426 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001427}
1428
1429// Must be called with EffectChain::mLock locked
1430void AudioFlinger::EffectChain::process_l()
1431{
1432 sp<ThreadBase> thread = mThread.promote();
1433 if (thread == 0) {
1434 ALOGW("process_l(): cannot promote mixer thread");
1435 return;
1436 }
1437 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1438 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001439 // never process effects when:
1440 // - on an OFFLOAD thread
1441 // - no more tracks are on the session and the effect tail has been rendered
1442 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001443 if (!isGlobalSession) {
1444 bool tracksOnSession = (trackCnt() != 0);
1445
1446 if (!tracksOnSession && mTailBufferCount == 0) {
1447 doProcess = false;
1448 }
1449
1450 if (activeTrackCnt() == 0) {
1451 // if no track is active and the effect tail has not been rendered,
1452 // the input buffer must be cleared here as the mixer process will not do it
1453 if (tracksOnSession || mTailBufferCount > 0) {
1454 clearInputBuffer_l(thread);
1455 if (mTailBufferCount > 0) {
1456 mTailBufferCount--;
1457 }
1458 }
1459 }
1460 }
1461
1462 size_t size = mEffects.size();
1463 if (doProcess) {
1464 for (size_t i = 0; i < size; i++) {
1465 mEffects[i]->process();
1466 }
1467 }
1468 for (size_t i = 0; i < size; i++) {
1469 mEffects[i]->updateState();
1470 }
1471}
1472
1473// addEffect_l() must be called with PlaybackThread::mLock held
1474status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1475{
1476 effect_descriptor_t desc = effect->desc();
1477 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1478
1479 Mutex::Autolock _l(mLock);
1480 effect->setChain(this);
1481 sp<ThreadBase> thread = mThread.promote();
1482 if (thread == 0) {
1483 return NO_INIT;
1484 }
1485 effect->setThread(thread);
1486
1487 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1488 // Auxiliary effects are inserted at the beginning of mEffects vector as
1489 // they are processed first and accumulated in chain input buffer
1490 mEffects.insertAt(effect, 0);
1491
1492 // the input buffer for auxiliary effect contains mono samples in
1493 // 32 bit format. This is to avoid saturation in AudoMixer
1494 // accumulation stage. Saturation is done in EffectModule::process() before
1495 // calling the process in effect engine
1496 size_t numSamples = thread->frameCount();
1497 int32_t *buffer = new int32_t[numSamples];
1498 memset(buffer, 0, numSamples * sizeof(int32_t));
1499 effect->setInBuffer((int16_t *)buffer);
1500 // auxiliary effects output samples to chain input buffer for further processing
1501 // by insert effects
1502 effect->setOutBuffer(mInBuffer);
1503 } else {
1504 // Insert effects are inserted at the end of mEffects vector as they are processed
1505 // after track and auxiliary effects.
1506 // Insert effect order as a function of indicated preference:
1507 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1508 // another effect is present
1509 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1510 // last effect claiming first position
1511 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1512 // first effect claiming last position
1513 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1514 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1515 // already present
1516
1517 size_t size = mEffects.size();
1518 size_t idx_insert = size;
1519 ssize_t idx_insert_first = -1;
1520 ssize_t idx_insert_last = -1;
1521
1522 for (size_t i = 0; i < size; i++) {
1523 effect_descriptor_t d = mEffects[i]->desc();
1524 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1525 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1526 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1527 // check invalid effect chaining combinations
1528 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1529 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1530 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1531 desc.name, d.name);
1532 return INVALID_OPERATION;
1533 }
1534 // remember position of first insert effect and by default
1535 // select this as insert position for new effect
1536 if (idx_insert == size) {
1537 idx_insert = i;
1538 }
1539 // remember position of last insert effect claiming
1540 // first position
1541 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1542 idx_insert_first = i;
1543 }
1544 // remember position of first insert effect claiming
1545 // last position
1546 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1547 idx_insert_last == -1) {
1548 idx_insert_last = i;
1549 }
1550 }
1551 }
1552
1553 // modify idx_insert from first position if needed
1554 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1555 if (idx_insert_last != -1) {
1556 idx_insert = idx_insert_last;
1557 } else {
1558 idx_insert = size;
1559 }
1560 } else {
1561 if (idx_insert_first != -1) {
1562 idx_insert = idx_insert_first + 1;
1563 }
1564 }
1565
1566 // always read samples from chain input buffer
1567 effect->setInBuffer(mInBuffer);
1568
1569 // if last effect in the chain, output samples to chain
1570 // output buffer, otherwise to chain input buffer
1571 if (idx_insert == size) {
1572 if (idx_insert != 0) {
1573 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1574 mEffects[idx_insert-1]->configure();
1575 }
1576 effect->setOutBuffer(mOutBuffer);
1577 } else {
1578 effect->setOutBuffer(mInBuffer);
1579 }
1580 mEffects.insertAt(effect, idx_insert);
1581
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001582 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001583 idx_insert);
1584 }
1585 effect->configure();
1586 return NO_ERROR;
1587}
1588
1589// removeEffect_l() must be called with PlaybackThread::mLock held
1590size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1591{
1592 Mutex::Autolock _l(mLock);
1593 size_t size = mEffects.size();
1594 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1595
1596 for (size_t i = 0; i < size; i++) {
1597 if (effect == mEffects[i]) {
1598 // calling stop here will remove pre-processing effect from the audio HAL.
1599 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1600 // the middle of a read from audio HAL
1601 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1602 mEffects[i]->state() == EffectModule::STOPPING) {
1603 mEffects[i]->stop();
1604 }
1605 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1606 delete[] effect->inBuffer();
1607 } else {
1608 if (i == size - 1 && i != 0) {
1609 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1610 mEffects[i - 1]->configure();
1611 }
1612 }
1613 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001614 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001615 this, i);
1616 break;
1617 }
1618 }
1619
1620 return mEffects.size();
1621}
1622
1623// setDevice_l() must be called with PlaybackThread::mLock held
1624void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1625{
1626 size_t size = mEffects.size();
1627 for (size_t i = 0; i < size; i++) {
1628 mEffects[i]->setDevice(device);
1629 }
1630}
1631
1632// setMode_l() must be called with PlaybackThread::mLock held
1633void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1634{
1635 size_t size = mEffects.size();
1636 for (size_t i = 0; i < size; i++) {
1637 mEffects[i]->setMode(mode);
1638 }
1639}
1640
1641// setAudioSource_l() must be called with PlaybackThread::mLock held
1642void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1643{
1644 size_t size = mEffects.size();
1645 for (size_t i = 0; i < size; i++) {
1646 mEffects[i]->setAudioSource(source);
1647 }
1648}
1649
1650// setVolume_l() must be called with PlaybackThread::mLock held
1651bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
1652{
1653 uint32_t newLeft = *left;
1654 uint32_t newRight = *right;
1655 bool hasControl = false;
1656 int ctrlIdx = -1;
1657 size_t size = mEffects.size();
1658
1659 // first update volume controller
1660 for (size_t i = size; i > 0; i--) {
1661 if (mEffects[i - 1]->isProcessEnabled() &&
1662 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1663 ctrlIdx = i - 1;
1664 hasControl = true;
1665 break;
1666 }
1667 }
1668
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001669 if (!isVolumeForced() && ctrlIdx == mVolumeCtrlIdx &&
1670 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001671 if (hasControl) {
1672 *left = mNewLeftVolume;
1673 *right = mNewRightVolume;
1674 }
1675 return hasControl;
1676 }
1677
1678 mVolumeCtrlIdx = ctrlIdx;
1679 mLeftVolume = newLeft;
1680 mRightVolume = newRight;
1681
1682 // second get volume update from volume controller
1683 if (ctrlIdx >= 0) {
1684 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1685 mNewLeftVolume = newLeft;
1686 mNewRightVolume = newRight;
1687 }
1688 // then indicate volume to all other effects in chain.
1689 // Pass altered volume to effects before volume controller
1690 // and requested volume to effects after controller
1691 uint32_t lVol = newLeft;
1692 uint32_t rVol = newRight;
1693
1694 for (size_t i = 0; i < size; i++) {
1695 if ((int)i == ctrlIdx) {
1696 continue;
1697 }
1698 // this also works for ctrlIdx == -1 when there is no volume controller
1699 if ((int)i > ctrlIdx) {
1700 lVol = *left;
1701 rVol = *right;
1702 }
1703 mEffects[i]->setVolume(&lVol, &rVol, false);
1704 }
1705 *left = newLeft;
1706 *right = newRight;
1707
1708 return hasControl;
1709}
1710
Eric Laurent1b928682014-10-02 19:41:47 -07001711void AudioFlinger::EffectChain::syncHalEffectsState()
1712{
1713 Mutex::Autolock _l(mLock);
1714 for (size_t i = 0; i < mEffects.size(); i++) {
1715 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1716 mEffects[i]->state() == EffectModule::STOPPING) {
1717 mEffects[i]->addEffectToHal_l();
1718 }
1719 }
1720}
1721
Eric Laurentca7cc822012-11-19 14:55:58 -08001722void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1723{
1724 const size_t SIZE = 256;
1725 char buffer[SIZE];
1726 String8 result;
1727
Marco Nelissenb2208842014-02-07 14:00:50 -08001728 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001729 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001730 result.append(buffer);
1731
Marco Nelissenb2208842014-02-07 14:00:50 -08001732 if (numEffects) {
1733 bool locked = AudioFlinger::dumpTryLock(mLock);
1734 // failed to lock - AudioFlinger is probably deadlocked
1735 if (!locked) {
1736 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001738
Marco Nelissenb2208842014-02-07 14:00:50 -08001739 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001740 snprintf(buffer, SIZE, "\t%p %p %d\n",
1741 mInBuffer,
1742 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001743 mActiveTrackCnt);
1744 result.append(buffer);
1745 write(fd, result.string(), result.size());
1746
1747 for (size_t i = 0; i < numEffects; ++i) {
1748 sp<EffectModule> effect = mEffects[i];
1749 if (effect != 0) {
1750 effect->dump(fd, args);
1751 }
1752 }
1753
1754 if (locked) {
1755 mLock.unlock();
1756 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001757 }
1758}
1759
1760// must be called with ThreadBase::mLock held
1761void AudioFlinger::EffectChain::setEffectSuspended_l(
1762 const effect_uuid_t *type, bool suspend)
1763{
1764 sp<SuspendedEffectDesc> desc;
1765 // use effect type UUID timelow as key as there is no real risk of identical
1766 // timeLow fields among effect type UUIDs.
1767 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1768 if (suspend) {
1769 if (index >= 0) {
1770 desc = mSuspendedEffects.valueAt(index);
1771 } else {
1772 desc = new SuspendedEffectDesc();
1773 desc->mType = *type;
1774 mSuspendedEffects.add(type->timeLow, desc);
1775 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1776 }
1777 if (desc->mRefCount++ == 0) {
1778 sp<EffectModule> effect = getEffectIfEnabled(type);
1779 if (effect != 0) {
1780 desc->mEffect = effect;
1781 effect->setSuspended(true);
1782 effect->setEnabled(false);
1783 }
1784 }
1785 } else {
1786 if (index < 0) {
1787 return;
1788 }
1789 desc = mSuspendedEffects.valueAt(index);
1790 if (desc->mRefCount <= 0) {
1791 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1792 desc->mRefCount = 1;
1793 }
1794 if (--desc->mRefCount == 0) {
1795 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1796 if (desc->mEffect != 0) {
1797 sp<EffectModule> effect = desc->mEffect.promote();
1798 if (effect != 0) {
1799 effect->setSuspended(false);
1800 effect->lock();
1801 EffectHandle *handle = effect->controlHandle_l();
1802 if (handle != NULL && !handle->destroyed_l()) {
1803 effect->setEnabled_l(handle->enabled());
1804 }
1805 effect->unlock();
1806 }
1807 desc->mEffect.clear();
1808 }
1809 mSuspendedEffects.removeItemsAt(index);
1810 }
1811 }
1812}
1813
1814// must be called with ThreadBase::mLock held
1815void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1816{
1817 sp<SuspendedEffectDesc> desc;
1818
1819 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1820 if (suspend) {
1821 if (index >= 0) {
1822 desc = mSuspendedEffects.valueAt(index);
1823 } else {
1824 desc = new SuspendedEffectDesc();
1825 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1826 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1827 }
1828 if (desc->mRefCount++ == 0) {
1829 Vector< sp<EffectModule> > effects;
1830 getSuspendEligibleEffects(effects);
1831 for (size_t i = 0; i < effects.size(); i++) {
1832 setEffectSuspended_l(&effects[i]->desc().type, true);
1833 }
1834 }
1835 } else {
1836 if (index < 0) {
1837 return;
1838 }
1839 desc = mSuspendedEffects.valueAt(index);
1840 if (desc->mRefCount <= 0) {
1841 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1842 desc->mRefCount = 1;
1843 }
1844 if (--desc->mRefCount == 0) {
1845 Vector<const effect_uuid_t *> types;
1846 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1847 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1848 continue;
1849 }
1850 types.add(&mSuspendedEffects.valueAt(i)->mType);
1851 }
1852 for (size_t i = 0; i < types.size(); i++) {
1853 setEffectSuspended_l(types[i], false);
1854 }
1855 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1856 mSuspendedEffects.keyAt(index));
1857 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1858 }
1859 }
1860}
1861
1862
1863// The volume effect is used for automated tests only
1864#ifndef OPENSL_ES_H_
1865static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1866 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1867const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1868#endif //OPENSL_ES_H_
1869
1870bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1871{
1872 // auxiliary effects and visualizer are never suspended on output mix
1873 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1874 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1875 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1876 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1877 return false;
1878 }
1879 return true;
1880}
1881
1882void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1883 Vector< sp<AudioFlinger::EffectModule> > &effects)
1884{
1885 effects.clear();
1886 for (size_t i = 0; i < mEffects.size(); i++) {
1887 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1888 effects.add(mEffects[i]);
1889 }
1890 }
1891}
1892
1893sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1894 const effect_uuid_t *type)
1895{
1896 sp<EffectModule> effect = getEffectFromType_l(type);
1897 return effect != 0 && effect->isEnabled() ? effect : 0;
1898}
1899
1900void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1901 bool enabled)
1902{
1903 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1904 if (enabled) {
1905 if (index < 0) {
1906 // if the effect is not suspend check if all effects are suspended
1907 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1908 if (index < 0) {
1909 return;
1910 }
1911 if (!isEffectEligibleForSuspend(effect->desc())) {
1912 return;
1913 }
1914 setEffectSuspended_l(&effect->desc().type, enabled);
1915 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1916 if (index < 0) {
1917 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1918 return;
1919 }
1920 }
1921 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1922 effect->desc().type.timeLow);
1923 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1924 // if effect is requested to suspended but was not yet enabled, supend it now.
1925 if (desc->mEffect == 0) {
1926 desc->mEffect = effect;
1927 effect->setEnabled(false);
1928 effect->setSuspended(true);
1929 }
1930 } else {
1931 if (index < 0) {
1932 return;
1933 }
1934 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1935 effect->desc().type.timeLow);
1936 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1937 desc->mEffect.clear();
1938 effect->setSuspended(false);
1939 }
1940}
1941
Eric Laurent5baf2af2013-09-12 17:37:00 -07001942bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001943{
1944 Mutex::Autolock _l(mLock);
1945 size_t size = mEffects.size();
1946 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07001947 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001948 return true;
1949 }
1950 }
1951 return false;
1952}
1953
Eric Laurentaaa44472014-09-12 17:41:50 -07001954void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
1955{
1956 Mutex::Autolock _l(mLock);
1957 mThread = thread;
1958 for (size_t i = 0; i < mEffects.size(); i++) {
1959 mEffects[i]->setThread(thread);
1960 }
1961}
1962
Glenn Kasten63238ef2015-03-02 15:50:29 -08001963} // namespace android