1 /*
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
22 #include <algorithm>
23
24 #include "Configuration.h"
25 #include <utils/Log.h>
26 #include <system/audio_effects/effect_aec.h>
27 #include <system/audio_effects/effect_ns.h>
28 #include <system/audio_effects/effect_visualizer.h>
29 #include <audio_utils/channels.h>
30 #include <audio_utils/primitives.h>
31 #include <media/AudioEffect.h>
32 #include <media/audiohal/EffectHalInterface.h>
33 #include <media/audiohal/EffectsFactoryHalInterface.h>
34 #include <mediautils/ServiceUtilities.h>
35
36 #include "AudioFlinger.h"
37
38 // ----------------------------------------------------------------------------
39
40 // Note: the following macro is used for extremely verbose logging message. In
41 // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
42 // 0; but one side effect of this is to turn all LOGV's as well. Some messages
43 // are so verbose that we want to suppress them even when we have ALOG_ASSERT
44 // turned on. Do not uncomment the #def below unless you really know what you
45 // are doing and want to see all of the extremely verbose messages.
46 //#define VERY_VERY_VERBOSE_LOGGING
47 #ifdef VERY_VERY_VERBOSE_LOGGING
48 #define ALOGVV ALOGV
49 #else
50 #define ALOGVV(a...) do { } while(0)
51 #endif
52
53 #define DEFAULT_OUTPUT_SAMPLE_RATE 48000
54
55 namespace android {
56
57 // ----------------------------------------------------------------------------
58 // EffectModule implementation
59 // ----------------------------------------------------------------------------
60
61 #undef LOG_TAG
62 #define LOG_TAG "AudioFlinger::EffectModule"
63
EffectModule(ThreadBase * thread,const wp<AudioFlinger::EffectChain> & chain,effect_descriptor_t * desc,int id,audio_session_t sessionId,bool pinned)64 AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
65 const wp<AudioFlinger::EffectChain>& chain,
66 effect_descriptor_t *desc,
67 int id,
68 audio_session_t sessionId,
69 bool pinned)
70 : mPinned(pinned),
71 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
72 mDescriptor(*desc),
73 // clear mConfig to ensure consistent initial value of buffer framecount
74 // in case buffers are associated by setInBuffer() or setOutBuffer()
75 // prior to configure().
76 mConfig{{}, {}},
77 mStatus(NO_INIT), mState(IDLE),
78 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
79 mDisableWaitCnt(0), // set by process() and updateState()
80 mSuspended(false),
81 mOffloaded(false),
82 mAudioFlinger(thread->mAudioFlinger)
83 #ifdef FLOAT_EFFECT_CHAIN
84 , mSupportsFloat(false)
85 #endif
86 {
87 ALOGV("Constructor %p pinned %d", this, pinned);
88 int lStatus;
89
90 // create effect engine from effect factory
91 mStatus = -ENODEV;
92 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
93 if (audioFlinger != 0) {
94 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
95 if (effectsFactory != 0) {
96 mStatus = effectsFactory->createEffect(
97 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
98 }
99 }
100
101 if (mStatus != NO_ERROR) {
102 return;
103 }
104 lStatus = init();
105 if (lStatus < 0) {
106 mStatus = lStatus;
107 goto Error;
108 }
109
110 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
111 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
112
113 return;
114 Error:
115 mEffectInterface.clear();
116 ALOGV("Constructor Error %d", mStatus);
117 }
118
~EffectModule()119 AudioFlinger::EffectModule::~EffectModule()
120 {
121 ALOGV("Destructor %p", this);
122 if (mEffectInterface != 0) {
123 char uuidStr[64];
124 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
125 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
126 this, uuidStr);
127 release_l();
128 }
129
130 }
131
addHandle(EffectHandle * handle)132 status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
133 {
134 status_t status;
135
136 Mutex::Autolock _l(mLock);
137 int priority = handle->priority();
138 size_t size = mHandles.size();
139 EffectHandle *controlHandle = NULL;
140 size_t i;
141 for (i = 0; i < size; i++) {
142 EffectHandle *h = mHandles[i];
143 if (h == NULL || h->disconnected()) {
144 continue;
145 }
146 // first non destroyed handle is considered in control
147 if (controlHandle == NULL) {
148 controlHandle = h;
149 }
150 if (h->priority() <= priority) {
151 break;
152 }
153 }
154 // if inserted in first place, move effect control from previous owner to this handle
155 if (i == 0) {
156 bool enabled = false;
157 if (controlHandle != NULL) {
158 enabled = controlHandle->enabled();
159 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
160 }
161 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
162 status = NO_ERROR;
163 } else {
164 status = ALREADY_EXISTS;
165 }
166 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
167 mHandles.insertAt(handle, i);
168 return status;
169 }
170
updatePolicyState()171 status_t AudioFlinger::EffectModule::updatePolicyState()
172 {
173 status_t status = NO_ERROR;
174 bool doRegister = false;
175 bool registered = false;
176 bool doEnable = false;
177 bool enabled = false;
178 audio_io_handle_t io;
179 uint32_t strategy;
180
181 {
182 Mutex::Autolock _l(mLock);
183 // register effect when first handle is attached and unregister when last handle is removed
184 if (mPolicyRegistered != mHandles.size() > 0) {
185 doRegister = true;
186 mPolicyRegistered = mHandles.size() > 0;
187 if (mPolicyRegistered) {
188 sp <EffectChain> chain = mChain.promote();
189 sp <ThreadBase> thread = mThread.promote();
190
191 if (thread == nullptr || chain == nullptr) {
192 return INVALID_OPERATION;
193 }
194 io = thread->id();
195 strategy = chain->strategy();
196 }
197 }
198 // enable effect when registered according to enable state requested by controlling handle
199 if (mHandles.size() > 0) {
200 EffectHandle *handle = controlHandle_l();
201 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
202 doEnable = true;
203 mPolicyEnabled = handle->enabled();
204 }
205 }
206 registered = mPolicyRegistered;
207 enabled = mPolicyEnabled;
208 mPolicyLock.lock();
209 }
210 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
211 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
212 if (doRegister) {
213 if (registered) {
214 status = AudioSystem::registerEffect(
215 &mDescriptor,
216 io,
217 strategy,
218 mSessionId,
219 mId);
220 } else {
221 status = AudioSystem::unregisterEffect(mId);
222 }
223 }
224 if (registered && doEnable) {
225 status = AudioSystem::setEffectEnabled(mId, enabled);
226 }
227 mPolicyLock.unlock();
228
229 return status;
230 }
231
232
removeHandle(EffectHandle * handle)233 ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
234 {
235 Mutex::Autolock _l(mLock);
236 return removeHandle_l(handle);
237 }
238
removeHandle_l(EffectHandle * handle)239 ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
240 {
241 size_t size = mHandles.size();
242 size_t i;
243 for (i = 0; i < size; i++) {
244 if (mHandles[i] == handle) {
245 break;
246 }
247 }
248 if (i == size) {
249 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
250 return BAD_VALUE;
251 }
252 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
253
254 mHandles.removeAt(i);
255 // if removed from first place, move effect control from this handle to next in line
256 if (i == 0) {
257 EffectHandle *h = controlHandle_l();
258 if (h != NULL) {
259 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
260 }
261 }
262
263 // Prevent calls to process() and other functions on effect interface from now on.
264 // The effect engine will be released by the destructor when the last strong reference on
265 // this object is released which can happen after next process is called.
266 if (mHandles.size() == 0 && !mPinned) {
267 mState = DESTROYED;
268 mEffectInterface->close();
269 }
270
271 return mHandles.size();
272 }
273
274 // must be called with EffectModule::mLock held
controlHandle_l()275 AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
276 {
277 // the first valid handle in the list has control over the module
278 for (size_t i = 0; i < mHandles.size(); i++) {
279 EffectHandle *h = mHandles[i];
280 if (h != NULL && !h->disconnected()) {
281 return h;
282 }
283 }
284
285 return NULL;
286 }
287
288 // unsafe method called when the effect parent thread has been destroyed
disconnectHandle(EffectHandle * handle,bool unpinIfLast)289 ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
290 {
291 ALOGV("disconnect() %p handle %p", this, handle);
292 Mutex::Autolock _l(mLock);
293 ssize_t numHandles = removeHandle_l(handle);
294 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
295 sp<AudioFlinger> af = mAudioFlinger.promote();
296 if (af != 0) {
297 mLock.unlock();
298 af->updateOrphanEffectChains(this);
299 mLock.lock();
300 }
301 }
302 return numHandles;
303 }
304
updateState()305 bool AudioFlinger::EffectModule::updateState() {
306 Mutex::Autolock _l(mLock);
307
308 bool started = false;
309 switch (mState) {
310 case RESTART:
311 reset_l();
312 FALLTHROUGH_INTENDED;
313
314 case STARTING:
315 // clear auxiliary effect input buffer for next accumulation
316 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
317 memset(mConfig.inputCfg.buffer.raw,
318 0,
319 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
320 }
321 if (start_l() == NO_ERROR) {
322 mState = ACTIVE;
323 started = true;
324 } else {
325 mState = IDLE;
326 }
327 break;
328 case STOPPING:
329 // volume control for offload and direct threads must take effect immediately.
330 if (stop_l() == NO_ERROR
331 && !(isVolumeControl() && isOffloadedOrDirect())) {
332 mDisableWaitCnt = mMaxDisableWaitCnt;
333 } else {
334 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
335 }
336 mState = STOPPED;
337 break;
338 case STOPPED:
339 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
340 // turn off sequence.
341 if (--mDisableWaitCnt == 0) {
342 reset_l();
343 mState = IDLE;
344 }
345 break;
346 default: //IDLE , ACTIVE, DESTROYED
347 break;
348 }
349
350 return started;
351 }
352
process()353 void AudioFlinger::EffectModule::process()
354 {
355 Mutex::Autolock _l(mLock);
356
357 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
358 return;
359 }
360
361 const uint32_t inChannelCount =
362 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
363 const uint32_t outChannelCount =
364 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
365 const bool auxType =
366 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
367
368 // safeInputOutputSampleCount is 0 if the channel count between input and output
369 // buffers do not match. This prevents automatic accumulation or copying between the
370 // input and output effect buffers without an intermediary effect process.
371 // TODO: consider implementing channel conversion.
372 const size_t safeInputOutputSampleCount =
373 mInChannelCountRequested != mOutChannelCountRequested ? 0
374 : mOutChannelCountRequested * std::min(
375 mConfig.inputCfg.buffer.frameCount,
376 mConfig.outputCfg.buffer.frameCount);
377 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
378 #ifdef FLOAT_EFFECT_CHAIN
379 accumulate_float(
380 mConfig.outputCfg.buffer.f32,
381 mConfig.inputCfg.buffer.f32,
382 safeInputOutputSampleCount);
383 #else
384 accumulate_i16(
385 mConfig.outputCfg.buffer.s16,
386 mConfig.inputCfg.buffer.s16,
387 safeInputOutputSampleCount);
388 #endif
389 };
390 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
391 #ifdef FLOAT_EFFECT_CHAIN
392 memcpy(
393 mConfig.outputCfg.buffer.f32,
394 mConfig.inputCfg.buffer.f32,
395 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
396
397 #else
398 memcpy(
399 mConfig.outputCfg.buffer.s16,
400 mConfig.inputCfg.buffer.s16,
401 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
402 #endif
403 };
404
405 if (isProcessEnabled()) {
406 int ret;
407 if (isProcessImplemented()) {
408 if (auxType) {
409 // We overwrite the aux input buffer here and clear after processing.
410 // aux input is always mono.
411 #ifdef FLOAT_EFFECT_CHAIN
412 if (mSupportsFloat) {
413 #ifndef FLOAT_AUX
414 // Do in-place float conversion for auxiliary effect input buffer.
415 static_assert(sizeof(float) <= sizeof(int32_t),
416 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
417
418 memcpy_to_float_from_q4_27(
419 mConfig.inputCfg.buffer.f32,
420 mConfig.inputCfg.buffer.s32,
421 mConfig.inputCfg.buffer.frameCount);
422 #endif // !FLOAT_AUX
423 } else
424 #endif // FLOAT_EFFECT_CHAIN
425 {
426 #ifdef FLOAT_AUX
427 memcpy_to_i16_from_float(
428 mConfig.inputCfg.buffer.s16,
429 mConfig.inputCfg.buffer.f32,
430 mConfig.inputCfg.buffer.frameCount);
431 #else
432 memcpy_to_i16_from_q4_27(
433 mConfig.inputCfg.buffer.s16,
434 mConfig.inputCfg.buffer.s32,
435 mConfig.inputCfg.buffer.frameCount);
436 #endif
437 }
438 }
439 #ifdef FLOAT_EFFECT_CHAIN
440 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
441 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
442
443 if (!auxType && mInChannelCountRequested != inChannelCount) {
444 adjust_channels(
445 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
446 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
447 sizeof(float),
448 sizeof(float)
449 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
450 inBuffer = mInConversionBuffer;
451 }
452 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
453 && mOutChannelCountRequested != outChannelCount) {
454 adjust_selected_channels(
455 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
456 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
457 sizeof(float),
458 sizeof(float)
459 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
460 outBuffer = mOutConversionBuffer;
461 }
462 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
463 if (!auxType) {
464 if (mInConversionBuffer.get() == nullptr) {
465 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
466 goto data_bypass;
467 }
468 memcpy_to_i16_from_float(
469 mInConversionBuffer->audioBuffer()->s16,
470 inBuffer->audioBuffer()->f32,
471 inChannelCount * mConfig.inputCfg.buffer.frameCount);
472 inBuffer = mInConversionBuffer;
473 }
474 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
475 if (mOutConversionBuffer.get() == nullptr) {
476 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
477 goto data_bypass;
478 }
479 memcpy_to_i16_from_float(
480 mOutConversionBuffer->audioBuffer()->s16,
481 outBuffer->audioBuffer()->f32,
482 outChannelCount * mConfig.outputCfg.buffer.frameCount);
483 outBuffer = mOutConversionBuffer;
484 }
485 }
486 #endif
487 ret = mEffectInterface->process();
488 #ifdef FLOAT_EFFECT_CHAIN
489 if (!mSupportsFloat) { // convert output int16_t back to float.
490 sp<EffectBufferHalInterface> target =
491 mOutChannelCountRequested != outChannelCount
492 ? mOutConversionBuffer : mOutBuffer;
493
494 memcpy_to_float_from_i16(
495 target->audioBuffer()->f32,
496 mOutConversionBuffer->audioBuffer()->s16,
497 outChannelCount * mConfig.outputCfg.buffer.frameCount);
498 }
499 if (mOutChannelCountRequested != outChannelCount) {
500 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
501 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
502 sizeof(float),
503 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
504 }
505 #endif
506 } else {
507 #ifdef FLOAT_EFFECT_CHAIN
508 data_bypass:
509 #endif
510 if (!auxType /* aux effects do not require data bypass */
511 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
512 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
513 accumulateInputToOutput();
514 } else {
515 copyInputToOutput();
516 }
517 }
518 ret = -ENODATA;
519 }
520
521 // force transition to IDLE state when engine is ready
522 if (mState == STOPPED && ret == -ENODATA) {
523 mDisableWaitCnt = 1;
524 }
525
526 // clear auxiliary effect input buffer for next accumulation
527 if (auxType) {
528 #ifdef FLOAT_AUX
529 const size_t size =
530 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
531 #else
532 const size_t size =
533 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
534 #endif
535 memset(mConfig.inputCfg.buffer.raw, 0, size);
536 }
537 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
538 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
539 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
540 // If an insert effect is idle and input buffer is different from output buffer,
541 // accumulate input onto output
542 sp<EffectChain> chain = mChain.promote();
543 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
544 // similar handling with data_bypass above.
545 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
546 accumulateInputToOutput();
547 } else { // EFFECT_BUFFER_ACCESS_WRITE
548 copyInputToOutput();
549 }
550 }
551 }
552 }
553
reset_l()554 void AudioFlinger::EffectModule::reset_l()
555 {
556 if (mStatus != NO_ERROR || mEffectInterface == 0) {
557 return;
558 }
559 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
560 }
561
configure()562 status_t AudioFlinger::EffectModule::configure()
563 {
564 ALOGVV("configure() started");
565 status_t status;
566 sp<ThreadBase> thread;
567 uint32_t size;
568 audio_channel_mask_t channelMask;
569
570 if (mEffectInterface == 0) {
571 status = NO_INIT;
572 goto exit;
573 }
574
575 thread = mThread.promote();
576 if (thread == 0) {
577 status = DEAD_OBJECT;
578 goto exit;
579 }
580
581 // TODO: handle configuration of effects replacing track process
582 // TODO: handle configuration of input (record) SW effects above the HAL,
583 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
584 // in which case input channel masks should be used here.
585 channelMask = thread->channelMask();
586 mConfig.inputCfg.channels = channelMask;
587 mConfig.outputCfg.channels = channelMask;
588
589 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
590 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
591 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
592 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
593 mConfig.inputCfg.channels);
594 }
595 #ifndef MULTICHANNEL_EFFECT_CHAIN
596 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
597 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
598 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
599 mConfig.outputCfg.channels);
600 }
601 #endif
602 } else {
603 #ifndef MULTICHANNEL_EFFECT_CHAIN
604 // TODO: Update this logic when multichannel effects are implemented.
605 // For offloaded tracks consider mono output as stereo for proper effect initialization
606 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
607 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
608 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
609 ALOGV("Overriding effect input and output as STEREO");
610 }
611 #endif
612 }
613 mInChannelCountRequested =
614 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
615 mOutChannelCountRequested =
616 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
617
618 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
619 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
620
621 // Don't use sample rate for thread if effect isn't offloadable.
622 if ((thread->type() == ThreadBase::OFFLOAD) && !isOffloaded()) {
623 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
624 ALOGV("Overriding effect input as 48kHz");
625 } else {
626 mConfig.inputCfg.samplingRate = thread->sampleRate();
627 }
628 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
629 mConfig.inputCfg.bufferProvider.cookie = NULL;
630 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
631 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
632 mConfig.outputCfg.bufferProvider.cookie = NULL;
633 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
634 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
635 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
636 // Insert effect:
637 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
638 // always overwrites output buffer: input buffer == output buffer
639 // - in other sessions:
640 // last effect in the chain accumulates in output buffer: input buffer != output buffer
641 // other effect: overwrites output buffer: input buffer == output buffer
642 // Auxiliary effect:
643 // accumulates in output buffer: input buffer != output buffer
644 // Therefore: accumulate <=> input buffer != output buffer
645 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
646 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
647 } else {
648 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
649 }
650 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
651 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
652 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
653 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
654
655 ALOGV("configure() %p thread %p buffer %p framecount %zu",
656 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
657
658 status_t cmdStatus;
659 size = sizeof(int);
660 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
661 sizeof(mConfig),
662 &mConfig,
663 &size,
664 &cmdStatus);
665 if (status == NO_ERROR) {
666 status = cmdStatus;
667 }
668
669 #ifdef MULTICHANNEL_EFFECT_CHAIN
670 if (status != NO_ERROR &&
671 thread->isOutput() &&
672 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
673 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
674 // Older effects may require exact STEREO position mask.
675 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
676 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
677 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
678 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
679 }
680 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
681 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
682 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
683 }
684 size = sizeof(int);
685 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
686 sizeof(mConfig),
687 &mConfig,
688 &size,
689 &cmdStatus);
690 if (status == NO_ERROR) {
691 status = cmdStatus;
692 }
693 }
694 #endif
695
696 #ifdef FLOAT_EFFECT_CHAIN
697 if (status == NO_ERROR) {
698 mSupportsFloat = true;
699 }
700
701 if (status != NO_ERROR) {
702 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
703 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
704 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
705 size = sizeof(int);
706 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
707 sizeof(mConfig),
708 &mConfig,
709 &size,
710 &cmdStatus);
711 if (status == NO_ERROR) {
712 status = cmdStatus;
713 }
714 if (status == NO_ERROR) {
715 mSupportsFloat = false;
716 ALOGVV("config worked with 16 bit");
717 } else {
718 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
719 }
720 }
721 #endif
722
723 if (status == NO_ERROR) {
724 // Establish Buffer strategy
725 setInBuffer(mInBuffer);
726 setOutBuffer(mOutBuffer);
727
728 // Update visualizer latency
729 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
730 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
731 effect_param_t *p = (effect_param_t *)buf32;
732
733 p->psize = sizeof(uint32_t);
734 p->vsize = sizeof(uint32_t);
735 size = sizeof(int);
736 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
737
738 uint32_t latency = 0;
739 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
740 if (pbt != NULL) {
741 latency = pbt->latency_l();
742 }
743
744 *((int32_t *)p->data + 1)= latency;
745 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
746 sizeof(effect_param_t) + 8,
747 &buf32,
748 &size,
749 &cmdStatus);
750 }
751 }
752
753 // mConfig.outputCfg.buffer.frameCount cannot be zero.
754 mMaxDisableWaitCnt = (uint32_t)std::max(
755 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
756 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
757 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
758
759 exit:
760 // TODO: consider clearing mConfig on error.
761 mStatus = status;
762 ALOGVV("configure ended");
763 return status;
764 }
765
init()766 status_t AudioFlinger::EffectModule::init()
767 {
768 Mutex::Autolock _l(mLock);
769 if (mEffectInterface == 0) {
770 return NO_INIT;
771 }
772 status_t cmdStatus;
773 uint32_t size = sizeof(status_t);
774 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
775 0,
776 NULL,
777 &size,
778 &cmdStatus);
779 if (status == 0) {
780 status = cmdStatus;
781 }
782 return status;
783 }
784
addEffectToHal_l()785 void AudioFlinger::EffectModule::addEffectToHal_l()
786 {
787 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
788 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
789 sp<ThreadBase> thread = mThread.promote();
790 if (thread != 0) {
791 sp<StreamHalInterface> stream = thread->stream();
792 if (stream != 0) {
793 status_t result = stream->addEffect(mEffectInterface);
794 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
795 }
796 }
797 }
798 }
799
800 // start() must be called with PlaybackThread::mLock or EffectChain::mLock held
start()801 status_t AudioFlinger::EffectModule::start()
802 {
803 sp<EffectChain> chain;
804 status_t status;
805 {
806 Mutex::Autolock _l(mLock);
807 status = start_l();
808 if (status == NO_ERROR) {
809 chain = mChain.promote();
810 }
811 }
812 if (chain != 0) {
813 chain->resetVolume_l();
814 }
815 return status;
816 }
817
start_l()818 status_t AudioFlinger::EffectModule::start_l()
819 {
820 if (mEffectInterface == 0) {
821 return NO_INIT;
822 }
823 if (mStatus != NO_ERROR) {
824 return mStatus;
825 }
826 status_t cmdStatus;
827 uint32_t size = sizeof(status_t);
828 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
829 0,
830 NULL,
831 &size,
832 &cmdStatus);
833 if (status == 0) {
834 status = cmdStatus;
835 }
836 if (status == 0) {
837 addEffectToHal_l();
838 }
839 return status;
840 }
841
stop()842 status_t AudioFlinger::EffectModule::stop()
843 {
844 Mutex::Autolock _l(mLock);
845 return stop_l();
846 }
847
stop_l()848 status_t AudioFlinger::EffectModule::stop_l()
849 {
850 if (mEffectInterface == 0) {
851 return NO_INIT;
852 }
853 if (mStatus != NO_ERROR) {
854 return mStatus;
855 }
856 status_t cmdStatus = NO_ERROR;
857 uint32_t size = sizeof(status_t);
858
859 if (isVolumeControl() && isOffloadedOrDirect()) {
860 sp<EffectChain>chain = mChain.promote();
861 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
862 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
863 mSetVolumeReentrantTid = gettid();
864 chain->resetVolume_l();
865 mSetVolumeReentrantTid = INVALID_PID;
866 }
867
868 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
869 0,
870 NULL,
871 &size,
872 &cmdStatus);
873 if (status == NO_ERROR) {
874 status = cmdStatus;
875 }
876 if (status == NO_ERROR) {
877 status = remove_effect_from_hal_l();
878 }
879 return status;
880 }
881
882 // must be called with EffectChain::mLock held
release_l()883 void AudioFlinger::EffectModule::release_l()
884 {
885 if (mEffectInterface != 0) {
886 remove_effect_from_hal_l();
887 // release effect engine
888 mEffectInterface->close();
889 mEffectInterface.clear();
890 }
891 }
892
remove_effect_from_hal_l()893 status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
894 {
895 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
896 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
897 sp<ThreadBase> thread = mThread.promote();
898 if (thread != 0) {
899 sp<StreamHalInterface> stream = thread->stream();
900 if (stream != 0) {
901 status_t result = stream->removeEffect(mEffectInterface);
902 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
903 }
904 }
905 }
906 return NO_ERROR;
907 }
908
909 // round up delta valid if value and divisor are positive.
910 template <typename T>
roundUpDelta(const T & value,const T & divisor)911 static T roundUpDelta(const T &value, const T &divisor) {
912 T remainder = value % divisor;
913 return remainder == 0 ? 0 : divisor - remainder;
914 }
915
command(uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)916 status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
917 uint32_t cmdSize,
918 void *pCmdData,
919 uint32_t *replySize,
920 void *pReplyData)
921 {
922 Mutex::Autolock _l(mLock);
923 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
924
925 if (mState == DESTROYED || mEffectInterface == 0) {
926 return NO_INIT;
927 }
928 if (mStatus != NO_ERROR) {
929 return mStatus;
930 }
931 if (cmdCode == EFFECT_CMD_GET_PARAM &&
932 (sizeof(effect_param_t) > cmdSize ||
933 ((effect_param_t *)pCmdData)->psize > cmdSize
934 - sizeof(effect_param_t))) {
935 android_errorWriteLog(0x534e4554, "32438594");
936 android_errorWriteLog(0x534e4554, "33003822");
937 return -EINVAL;
938 }
939 if (cmdCode == EFFECT_CMD_GET_PARAM &&
940 (*replySize < sizeof(effect_param_t) ||
941 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
942 android_errorWriteLog(0x534e4554, "29251553");
943 return -EINVAL;
944 }
945 if (cmdCode == EFFECT_CMD_GET_PARAM &&
946 (sizeof(effect_param_t) > *replySize
947 || ((effect_param_t *)pCmdData)->psize > *replySize
948 - sizeof(effect_param_t)
949 || ((effect_param_t *)pCmdData)->vsize > *replySize
950 - sizeof(effect_param_t)
951 - ((effect_param_t *)pCmdData)->psize
952 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
953 *replySize
954 - sizeof(effect_param_t)
955 - ((effect_param_t *)pCmdData)->psize
956 - ((effect_param_t *)pCmdData)->vsize)) {
957 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
958 android_errorWriteLog(0x534e4554, "32705438");
959 return -EINVAL;
960 }
961 if ((cmdCode == EFFECT_CMD_SET_PARAM
962 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
963 (sizeof(effect_param_t) > cmdSize
964 || ((effect_param_t *)pCmdData)->psize > cmdSize
965 - sizeof(effect_param_t)
966 || ((effect_param_t *)pCmdData)->vsize > cmdSize
967 - sizeof(effect_param_t)
968 - ((effect_param_t *)pCmdData)->psize
969 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
970 cmdSize
971 - sizeof(effect_param_t)
972 - ((effect_param_t *)pCmdData)->psize
973 - ((effect_param_t *)pCmdData)->vsize)) {
974 android_errorWriteLog(0x534e4554, "30204301");
975 return -EINVAL;
976 }
977 status_t status = mEffectInterface->command(cmdCode,
978 cmdSize,
979 pCmdData,
980 replySize,
981 pReplyData);
982 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
983 uint32_t size = (replySize == NULL) ? 0 : *replySize;
984 for (size_t i = 1; i < mHandles.size(); i++) {
985 EffectHandle *h = mHandles[i];
986 if (h != NULL && !h->disconnected()) {
987 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
988 }
989 }
990 }
991 return status;
992 }
993
setEnabled(bool enabled)994 status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
995 {
996 Mutex::Autolock _l(mLock);
997 return setEnabled_l(enabled);
998 }
999
1000 // must be called with EffectModule::mLock held
setEnabled_l(bool enabled)1001 status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
1002 {
1003
1004 ALOGV("setEnabled %p enabled %d", this, enabled);
1005
1006 if (enabled != isEnabled()) {
1007 switch (mState) {
1008 // going from disabled to enabled
1009 case IDLE:
1010 mState = STARTING;
1011 break;
1012 case STOPPED:
1013 mState = RESTART;
1014 break;
1015 case STOPPING:
1016 mState = ACTIVE;
1017 break;
1018
1019 // going from enabled to disabled
1020 case RESTART:
1021 mState = STOPPED;
1022 break;
1023 case STARTING:
1024 mState = IDLE;
1025 break;
1026 case ACTIVE:
1027 mState = STOPPING;
1028 break;
1029 case DESTROYED:
1030 return NO_ERROR; // simply ignore as we are being destroyed
1031 }
1032 for (size_t i = 1; i < mHandles.size(); i++) {
1033 EffectHandle *h = mHandles[i];
1034 if (h != NULL && !h->disconnected()) {
1035 h->setEnabled(enabled);
1036 }
1037 }
1038 }
1039 return NO_ERROR;
1040 }
1041
isEnabled() const1042 bool AudioFlinger::EffectModule::isEnabled() const
1043 {
1044 switch (mState) {
1045 case RESTART:
1046 case STARTING:
1047 case ACTIVE:
1048 return true;
1049 case IDLE:
1050 case STOPPING:
1051 case STOPPED:
1052 case DESTROYED:
1053 default:
1054 return false;
1055 }
1056 }
1057
isProcessEnabled() const1058 bool AudioFlinger::EffectModule::isProcessEnabled() const
1059 {
1060 if (mStatus != NO_ERROR) {
1061 return false;
1062 }
1063
1064 switch (mState) {
1065 case RESTART:
1066 case ACTIVE:
1067 case STOPPING:
1068 case STOPPED:
1069 return true;
1070 case IDLE:
1071 case STARTING:
1072 case DESTROYED:
1073 default:
1074 return false;
1075 }
1076 }
1077
isOffloadedOrDirect() const1078 bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1079 {
1080 return (mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT);
1081 }
1082
isVolumeControlEnabled() const1083 bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1084 {
1085 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1086 }
1087
setInBuffer(const sp<EffectBufferHalInterface> & buffer)1088 void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
1089 ALOGVV("setInBuffer %p",(&buffer));
1090
1091 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
1092 if (buffer != 0) {
1093 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1094 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1095 } else {
1096 mConfig.inputCfg.buffer.raw = NULL;
1097 }
1098 mInBuffer = buffer;
1099 mEffectInterface->setInBuffer(buffer);
1100
1101 #ifdef FLOAT_EFFECT_CHAIN
1102 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
1103 // Theoretically insert effects can also do in-place conversions (destroying
1104 // the original buffer) when the output buffer is identical to the input buffer,
1105 // but we don't optimize for it here.
1106 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
1107 const uint32_t inChannelCount =
1108 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1109 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1110 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
1111 // we need to translate - create hidl shared buffer and intercept
1112 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
1113 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1114 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1115 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
1116
1117 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1118 __func__, inChannels, inFrameCount, size);
1119
1120 if (size > 0 && (mInConversionBuffer.get() == nullptr
1121 || size > mInConversionBuffer->getSize())) {
1122 mInConversionBuffer.clear();
1123 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
1124 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1125 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1126 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mInConversionBuffer);
1127 }
1128 if (mInConversionBuffer.get() != nullptr) {
1129 mInConversionBuffer->setFrameCount(inFrameCount);
1130 mEffectInterface->setInBuffer(mInConversionBuffer);
1131 } else if (size > 0) {
1132 ALOGE("%s cannot create mInConversionBuffer", __func__);
1133 }
1134 }
1135 #endif
1136 }
1137
setOutBuffer(const sp<EffectBufferHalInterface> & buffer)1138 void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
1139 ALOGVV("setOutBuffer %p",(&buffer));
1140
1141 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
1142 if (buffer != 0) {
1143 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1144 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1145 } else {
1146 mConfig.outputCfg.buffer.raw = NULL;
1147 }
1148 mOutBuffer = buffer;
1149 mEffectInterface->setOutBuffer(buffer);
1150
1151 #ifdef FLOAT_EFFECT_CHAIN
1152 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
1153 // can do in-place conversion from int16_t to float. We don't optimize here.
1154 const uint32_t outChannelCount =
1155 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1156 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1157 if (formatMismatch && mOutBuffer.get() != nullptr) {
1158 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
1159 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1160 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1161 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
1162
1163 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1164 __func__, outChannels, outFrameCount, size);
1165
1166 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1167 || size > mOutConversionBuffer->getSize())) {
1168 mOutConversionBuffer.clear();
1169 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
1170 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
1171 LOG_ALWAYS_FATAL_IF(audioFlinger == nullptr, "EM could not retrieved audioFlinger");
1172 (void)audioFlinger->mEffectsFactoryHal->allocateBuffer(size, &mOutConversionBuffer);
1173 }
1174 if (mOutConversionBuffer.get() != nullptr) {
1175 mOutConversionBuffer->setFrameCount(outFrameCount);
1176 mEffectInterface->setOutBuffer(mOutConversionBuffer);
1177 } else if (size > 0) {
1178 ALOGE("%s cannot create mOutConversionBuffer", __func__);
1179 }
1180 }
1181 #endif
1182 }
1183
setVolume(uint32_t * left,uint32_t * right,bool controller)1184 status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1185 {
1186 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
1187 if (mStatus != NO_ERROR) {
1188 return mStatus;
1189 }
1190 status_t status = NO_ERROR;
1191 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1192 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1193 if (isProcessEnabled() &&
1194 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
1195 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1196 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
1197 uint32_t volume[2];
1198 uint32_t *pVolume = NULL;
1199 uint32_t size = sizeof(volume);
1200 volume[0] = *left;
1201 volume[1] = *right;
1202 if (controller) {
1203 pVolume = volume;
1204 }
1205 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1206 size,
1207 volume,
1208 &size,
1209 pVolume);
1210 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1211 *left = volume[0];
1212 *right = volume[1];
1213 }
1214 }
1215 return status;
1216 }
1217
setVolumeForOutput_l(uint32_t left,uint32_t right)1218 void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1219 {
1220 sp<ThreadBase> thread = mThread.promote();
1221 if (thread != 0 &&
1222 (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::DIRECT) &&
1223 !isNonOffloadableEnabled_l()) {
1224 PlaybackThread *t = (PlaybackThread *)thread.get();
1225 float vol_l = (float)left / (1 << 24);
1226 float vol_r = (float)right / (1 << 24);
1227 t->setVolumeForOutput_l(vol_l, vol_r);
1228 }
1229 }
1230
setDevice(audio_devices_t device)1231 status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1232 {
1233 if (device == AUDIO_DEVICE_NONE) {
1234 return NO_ERROR;
1235 }
1236
1237 Mutex::Autolock _l(mLock);
1238 if (mStatus != NO_ERROR) {
1239 return mStatus;
1240 }
1241 status_t status = NO_ERROR;
1242 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
1243 status_t cmdStatus;
1244 uint32_t size = sizeof(status_t);
1245 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1246 EFFECT_CMD_SET_INPUT_DEVICE;
1247 status = mEffectInterface->command(cmd,
1248 sizeof(uint32_t),
1249 &device,
1250 &size,
1251 &cmdStatus);
1252 }
1253 return status;
1254 }
1255
setMode(audio_mode_t mode)1256 status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1257 {
1258 Mutex::Autolock _l(mLock);
1259 if (mStatus != NO_ERROR) {
1260 return mStatus;
1261 }
1262 status_t status = NO_ERROR;
1263 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1264 status_t cmdStatus;
1265 uint32_t size = sizeof(status_t);
1266 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1267 sizeof(audio_mode_t),
1268 &mode,
1269 &size,
1270 &cmdStatus);
1271 if (status == NO_ERROR) {
1272 status = cmdStatus;
1273 }
1274 }
1275 return status;
1276 }
1277
setAudioSource(audio_source_t source)1278 status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1279 {
1280 Mutex::Autolock _l(mLock);
1281 if (mStatus != NO_ERROR) {
1282 return mStatus;
1283 }
1284 status_t status = NO_ERROR;
1285 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1286 uint32_t size = 0;
1287 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1288 sizeof(audio_source_t),
1289 &source,
1290 &size,
1291 NULL);
1292 }
1293 return status;
1294 }
1295
setSuspended(bool suspended)1296 void AudioFlinger::EffectModule::setSuspended(bool suspended)
1297 {
1298 Mutex::Autolock _l(mLock);
1299 mSuspended = suspended;
1300 }
1301
suspended() const1302 bool AudioFlinger::EffectModule::suspended() const
1303 {
1304 Mutex::Autolock _l(mLock);
1305 return mSuspended;
1306 }
1307
purgeHandles()1308 bool AudioFlinger::EffectModule::purgeHandles()
1309 {
1310 bool enabled = false;
1311 Mutex::Autolock _l(mLock);
1312 EffectHandle *handle = controlHandle_l();
1313 if (handle != NULL) {
1314 enabled = handle->enabled();
1315 }
1316 mHandles.clear();
1317 return enabled;
1318 }
1319
setOffloaded(bool offloaded,audio_io_handle_t io)1320 status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1321 {
1322 Mutex::Autolock _l(mLock);
1323 if (mStatus != NO_ERROR) {
1324 return mStatus;
1325 }
1326 status_t status = NO_ERROR;
1327 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1328 status_t cmdStatus;
1329 uint32_t size = sizeof(status_t);
1330 effect_offload_param_t cmd;
1331
1332 cmd.isOffload = offloaded;
1333 cmd.ioHandle = io;
1334 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1335 sizeof(effect_offload_param_t),
1336 &cmd,
1337 &size,
1338 &cmdStatus);
1339 if (status == NO_ERROR) {
1340 status = cmdStatus;
1341 }
1342 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1343 } else {
1344 if (offloaded) {
1345 status = INVALID_OPERATION;
1346 }
1347 mOffloaded = false;
1348 }
1349 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1350 return status;
1351 }
1352
isOffloaded() const1353 bool AudioFlinger::EffectModule::isOffloaded() const
1354 {
1355 Mutex::Autolock _l(mLock);
1356 return mOffloaded;
1357 }
1358
effectFlagsToString(uint32_t flags)1359 String8 effectFlagsToString(uint32_t flags) {
1360 String8 s;
1361
1362 s.append("conn. mode: ");
1363 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1364 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1365 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1366 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1367 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1368 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1369 default: s.append("unknown/reserved"); break;
1370 }
1371 s.append(", ");
1372
1373 s.append("insert pref: ");
1374 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1375 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1376 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1377 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1378 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1379 default: s.append("unknown/reserved"); break;
1380 }
1381 s.append(", ");
1382
1383 s.append("volume mgmt: ");
1384 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1385 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1386 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1387 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1388 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
1389 default: s.append("unknown/reserved"); break;
1390 }
1391 s.append(", ");
1392
1393 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1394 if (devind) {
1395 s.append("device indication: ");
1396 switch (devind) {
1397 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1398 default: s.append("unknown/reserved"); break;
1399 }
1400 s.append(", ");
1401 }
1402
1403 s.append("input mode: ");
1404 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1405 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1406 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1407 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1408 default: s.append("not set"); break;
1409 }
1410 s.append(", ");
1411
1412 s.append("output mode: ");
1413 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1414 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1415 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1416 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1417 default: s.append("not set"); break;
1418 }
1419 s.append(", ");
1420
1421 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1422 if (accel) {
1423 s.append("hardware acceleration: ");
1424 switch (accel) {
1425 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1426 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1427 default: s.append("unknown/reserved"); break;
1428 }
1429 s.append(", ");
1430 }
1431
1432 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1433 if (modeind) {
1434 s.append("mode indication: ");
1435 switch (modeind) {
1436 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1437 default: s.append("unknown/reserved"); break;
1438 }
1439 s.append(", ");
1440 }
1441
1442 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1443 if (srcind) {
1444 s.append("source indication: ");
1445 switch (srcind) {
1446 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1447 default: s.append("unknown/reserved"); break;
1448 }
1449 s.append(", ");
1450 }
1451
1452 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1453 s.append("offloadable, ");
1454 }
1455
1456 int len = s.length();
1457 if (s.length() > 2) {
1458 (void) s.lockBuffer(len);
1459 s.unlockBuffer(len - 2);
1460 }
1461 return s;
1462 }
1463
dumpInOutBuffer(bool isInput,const sp<EffectBufferHalInterface> & buffer)1464 static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1465 std::stringstream ss;
1466
1467 if (buffer.get() == nullptr) {
1468 return "nullptr"; // make different than below
1469 } else if (buffer->externalData() != nullptr) {
1470 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1471 << " -> "
1472 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1473 } else {
1474 ss << buffer->audioBuffer()->raw;
1475 }
1476 return ss.str();
1477 }
1478
dump(int fd,const Vector<String16> & args __unused)1479 void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
1480 {
1481 String8 result;
1482
1483 result.appendFormat("\tEffect ID %d:\n", mId);
1484
1485 bool locked = AudioFlinger::dumpTryLock(mLock);
1486 // failed to lock - AudioFlinger is probably deadlocked
1487 if (!locked) {
1488 result.append("\t\tCould not lock Fx mutex:\n");
1489 }
1490
1491 result.append("\t\tSession Status State Registered Enabled Suspended Engine:\n");
1492 result.appendFormat("\t\t%05d %03d %03d %s %s %s %p\n",
1493 mSessionId, mStatus, mState, mPolicyRegistered ? "y" : "n", mPolicyEnabled ? "y" : "n",
1494 mSuspended ? "y" : "n", mEffectInterface.get());
1495
1496 result.append("\t\tDescriptor:\n");
1497 char uuidStr[64];
1498 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1499 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
1500 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1501 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1502 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
1503 mDescriptor.apiVersion,
1504 mDescriptor.flags,
1505 effectFlagsToString(mDescriptor.flags).string());
1506 result.appendFormat("\t\t- name: %s\n",
1507 mDescriptor.name);
1508
1509 result.appendFormat("\t\t- implementor: %s\n",
1510 mDescriptor.implementor);
1511
1512 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
1513
1514 result.append("\t\t- Input configuration:\n");
1515 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1516 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1517 mConfig.inputCfg.buffer.raw,
1518 mConfig.inputCfg.buffer.frameCount,
1519 mConfig.inputCfg.samplingRate,
1520 mConfig.inputCfg.channels,
1521 mConfig.inputCfg.format,
1522 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
1523
1524 result.append("\t\t- Output configuration:\n");
1525 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1526 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1527 mConfig.outputCfg.buffer.raw,
1528 mConfig.outputCfg.buffer.frameCount,
1529 mConfig.outputCfg.samplingRate,
1530 mConfig.outputCfg.channels,
1531 mConfig.outputCfg.format,
1532 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
1533
1534 #ifdef FLOAT_EFFECT_CHAIN
1535
1536 result.appendFormat("\t\t- HAL buffers:\n"
1537 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1538 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1539 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1540 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1541 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
1542 #endif
1543
1544 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
1545 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
1546 char buffer[256];
1547 for (size_t i = 0; i < mHandles.size(); ++i) {
1548 EffectHandle *handle = mHandles[i];
1549 if (handle != NULL && !handle->disconnected()) {
1550 handle->dumpToBuffer(buffer, sizeof(buffer));
1551 result.append(buffer);
1552 }
1553 }
1554
1555 write(fd, result.string(), result.length());
1556
1557 if (mEffectInterface != 0) {
1558 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1559 (void)mEffectInterface->dump(fd);
1560 }
1561
1562 if (locked) {
1563 mLock.unlock();
1564 }
1565 }
1566
1567 // ----------------------------------------------------------------------------
1568 // EffectHandle implementation
1569 // ----------------------------------------------------------------------------
1570
1571 #undef LOG_TAG
1572 #define LOG_TAG "AudioFlinger::EffectHandle"
1573
EffectHandle(const sp<EffectModule> & effect,const sp<AudioFlinger::Client> & client,const sp<IEffectClient> & effectClient,int32_t priority)1574 AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1575 const sp<AudioFlinger::Client>& client,
1576 const sp<IEffectClient>& effectClient,
1577 int32_t priority)
1578 : BnEffect(),
1579 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1580 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
1581 {
1582 ALOGV("constructor %p", this);
1583
1584 if (client == 0) {
1585 return;
1586 }
1587 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1588 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
1589 if (mCblkMemory == 0 ||
1590 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
1591 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
1592 sizeof(effect_param_cblk_t));
1593 mCblkMemory.clear();
1594 return;
1595 }
1596 new(mCblk) effect_param_cblk_t();
1597 mBuffer = (uint8_t *)mCblk + bufOffset;
1598 }
1599
~EffectHandle()1600 AudioFlinger::EffectHandle::~EffectHandle()
1601 {
1602 ALOGV("Destructor %p", this);
1603 disconnect(false);
1604 }
1605
initCheck()1606 status_t AudioFlinger::EffectHandle::initCheck()
1607 {
1608 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1609 }
1610
enable()1611 status_t AudioFlinger::EffectHandle::enable()
1612 {
1613 AutoMutex _l(mLock);
1614 ALOGV("enable %p", this);
1615 sp<EffectModule> effect = mEffect.promote();
1616 if (effect == 0 || mDisconnected) {
1617 return DEAD_OBJECT;
1618 }
1619 if (!mHasControl) {
1620 return INVALID_OPERATION;
1621 }
1622
1623 if (mEnabled) {
1624 return NO_ERROR;
1625 }
1626
1627 mEnabled = true;
1628
1629 status_t status = effect->updatePolicyState();
1630 if (status != NO_ERROR) {
1631 mEnabled = false;
1632 return status;
1633 }
1634
1635 sp<ThreadBase> thread = effect->thread().promote();
1636 if (thread != 0) {
1637 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
1638 }
1639
1640 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1641 if (effect->suspended()) {
1642 return NO_ERROR;
1643 }
1644
1645 status = effect->setEnabled(true);
1646 if (status != NO_ERROR) {
1647 if (thread != 0) {
1648 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
1649 }
1650 mEnabled = false;
1651 } else {
1652 if (thread != 0) {
1653 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1654 Mutex::Autolock _l(thread->mLock);
1655 thread->broadcast_l();
1656 }
1657 if (!effect->isOffloadable()) {
1658 if (thread->type() == ThreadBase::OFFLOAD) {
1659 PlaybackThread *t = (PlaybackThread *)thread.get();
1660 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1661 }
1662 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1663 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1664 }
1665 }
1666 }
1667 }
1668 return status;
1669 }
1670
disable()1671 status_t AudioFlinger::EffectHandle::disable()
1672 {
1673 ALOGV("disable %p", this);
1674 AutoMutex _l(mLock);
1675 sp<EffectModule> effect = mEffect.promote();
1676 if (effect == 0 || mDisconnected) {
1677 return DEAD_OBJECT;
1678 }
1679 if (!mHasControl) {
1680 return INVALID_OPERATION;
1681 }
1682
1683 if (!mEnabled) {
1684 return NO_ERROR;
1685 }
1686 mEnabled = false;
1687
1688 effect->updatePolicyState();
1689
1690 if (effect->suspended()) {
1691 return NO_ERROR;
1692 }
1693
1694 status_t status = effect->setEnabled(false);
1695
1696 sp<ThreadBase> thread = effect->thread().promote();
1697 if (thread != 0) {
1698 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
1699 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1700 Mutex::Autolock _l(thread->mLock);
1701 thread->broadcast_l();
1702 }
1703 }
1704
1705 return status;
1706 }
1707
disconnect()1708 void AudioFlinger::EffectHandle::disconnect()
1709 {
1710 ALOGV("%s %p", __FUNCTION__, this);
1711 disconnect(true);
1712 }
1713
disconnect(bool unpinIfLast)1714 void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1715 {
1716 AutoMutex _l(mLock);
1717 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1718 if (mDisconnected) {
1719 if (unpinIfLast) {
1720 android_errorWriteLog(0x534e4554, "32707507");
1721 }
1722 return;
1723 }
1724 mDisconnected = true;
1725 {
1726 sp<EffectModule> effect = mEffect.promote();
1727 if (effect != 0) {
1728 sp<ThreadBase> thread = effect->thread().promote();
1729 if (thread != 0) {
1730 thread->disconnectEffectHandle(this, unpinIfLast);
1731 } else if (effect->disconnectHandle(this, unpinIfLast) > 0) {
1732 ALOGW("%s Effect handle %p disconnected after thread destruction",
1733 __func__, this);
1734 }
1735 effect->updatePolicyState();
1736 }
1737 }
1738
1739 if (mClient != 0) {
1740 if (mCblk != NULL) {
1741 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1742 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1743 }
1744 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
1745 // Client destructor must run with AudioFlinger client mutex locked
1746 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
1747 mClient.clear();
1748 }
1749 }
1750
command(uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)1751 status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1752 uint32_t cmdSize,
1753 void *pCmdData,
1754 uint32_t *replySize,
1755 void *pReplyData)
1756 {
1757 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1758 cmdCode, mHasControl, mEffect.unsafe_get());
1759
1760 // reject commands reserved for internal use by audio framework if coming from outside
1761 // of audioserver
1762 switch(cmdCode) {
1763 case EFFECT_CMD_ENABLE:
1764 case EFFECT_CMD_DISABLE:
1765 case EFFECT_CMD_SET_PARAM:
1766 case EFFECT_CMD_SET_PARAM_DEFERRED:
1767 case EFFECT_CMD_SET_PARAM_COMMIT:
1768 case EFFECT_CMD_GET_PARAM:
1769 break;
1770 default:
1771 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1772 break;
1773 }
1774 android_errorWriteLog(0x534e4554, "62019992");
1775 return BAD_VALUE;
1776 }
1777
1778 if (cmdCode == EFFECT_CMD_ENABLE) {
1779 if (*replySize < sizeof(int)) {
1780 android_errorWriteLog(0x534e4554, "32095713");
1781 return BAD_VALUE;
1782 }
1783 *(int *)pReplyData = NO_ERROR;
1784 *replySize = sizeof(int);
1785 return enable();
1786 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1787 if (*replySize < sizeof(int)) {
1788 android_errorWriteLog(0x534e4554, "32095713");
1789 return BAD_VALUE;
1790 }
1791 *(int *)pReplyData = NO_ERROR;
1792 *replySize = sizeof(int);
1793 return disable();
1794 }
1795
1796 AutoMutex _l(mLock);
1797 sp<EffectModule> effect = mEffect.promote();
1798 if (effect == 0 || mDisconnected) {
1799 return DEAD_OBJECT;
1800 }
1801 // only get parameter command is permitted for applications not controlling the effect
1802 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1803 return INVALID_OPERATION;
1804 }
1805 if (mClient == 0) {
1806 return INVALID_OPERATION;
1807 }
1808
1809 // handle commands that are not forwarded transparently to effect engine
1810 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1811 if (*replySize < sizeof(int)) {
1812 android_errorWriteLog(0x534e4554, "32095713");
1813 return BAD_VALUE;
1814 }
1815 *(int *)pReplyData = NO_ERROR;
1816 *replySize = sizeof(int);
1817
1818 // No need to trylock() here as this function is executed in the binder thread serving a
1819 // particular client process: no risk to block the whole media server process or mixer
1820 // threads if we are stuck here
1821 Mutex::Autolock _l(mCblk->lock);
1822 // keep local copy of index in case of client corruption b/32220769
1823 const uint32_t clientIndex = mCblk->clientIndex;
1824 const uint32_t serverIndex = mCblk->serverIndex;
1825 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1826 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1827 mCblk->serverIndex = 0;
1828 mCblk->clientIndex = 0;
1829 return BAD_VALUE;
1830 }
1831 status_t status = NO_ERROR;
1832 effect_param_t *param = NULL;
1833 for (uint32_t index = serverIndex; index < clientIndex;) {
1834 int *p = (int *)(mBuffer + index);
1835 const int size = *p++;
1836 if (size < 0
1837 || size > EFFECT_PARAM_BUFFER_SIZE
1838 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
1839 ALOGW("command(): invalid parameter block size");
1840 status = BAD_VALUE;
1841 break;
1842 }
1843
1844 // copy to local memory in case of client corruption b/32220769
1845 param = (effect_param_t *)realloc(param, size);
1846 if (param == NULL) {
1847 ALOGW("command(): out of memory");
1848 status = NO_MEMORY;
1849 break;
1850 }
1851 memcpy(param, p, size);
1852
1853 int reply = 0;
1854 uint32_t rsize = sizeof(reply);
1855 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
1856 size,
1857 param,
1858 &rsize,
1859 &reply);
1860
1861 // verify shared memory: server index shouldn't change; client index can't go back.
1862 if (serverIndex != mCblk->serverIndex
1863 || clientIndex > mCblk->clientIndex) {
1864 android_errorWriteLog(0x534e4554, "32220769");
1865 status = BAD_VALUE;
1866 break;
1867 }
1868
1869 // stop at first error encountered
1870 if (ret != NO_ERROR) {
1871 status = ret;
1872 *(int *)pReplyData = reply;
1873 break;
1874 } else if (reply != NO_ERROR) {
1875 *(int *)pReplyData = reply;
1876 break;
1877 }
1878 index += size;
1879 }
1880 free(param);
1881 mCblk->serverIndex = 0;
1882 mCblk->clientIndex = 0;
1883 return status;
1884 }
1885
1886 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1887 }
1888
setControl(bool hasControl,bool signal,bool enabled)1889 void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1890 {
1891 ALOGV("setControl %p control %d", this, hasControl);
1892
1893 mHasControl = hasControl;
1894 mEnabled = enabled;
1895
1896 if (signal && mEffectClient != 0) {
1897 mEffectClient->controlStatusChanged(hasControl);
1898 }
1899 }
1900
commandExecuted(uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t replySize,void * pReplyData)1901 void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1902 uint32_t cmdSize,
1903 void *pCmdData,
1904 uint32_t replySize,
1905 void *pReplyData)
1906 {
1907 if (mEffectClient != 0) {
1908 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1909 }
1910 }
1911
1912
1913
setEnabled(bool enabled)1914 void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1915 {
1916 if (mEffectClient != 0) {
1917 mEffectClient->enableStatusChanged(enabled);
1918 }
1919 }
1920
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)1921 status_t AudioFlinger::EffectHandle::onTransact(
1922 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1923 {
1924 return BnEffect::onTransact(code, data, reply, flags);
1925 }
1926
1927
dumpToBuffer(char * buffer,size_t size)1928 void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
1929 {
1930 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1931
1932 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
1933 (mClient == 0) ? getpid() : mClient->pid(),
1934 mPriority,
1935 mHasControl ? "yes" : "no",
1936 locked ? "yes" : "no",
1937 mCblk ? mCblk->clientIndex : 0,
1938 mCblk ? mCblk->serverIndex : 0
1939 );
1940
1941 if (locked) {
1942 mCblk->lock.unlock();
1943 }
1944 }
1945
1946 #undef LOG_TAG
1947 #define LOG_TAG "AudioFlinger::EffectChain"
1948
EffectChain(ThreadBase * thread,audio_session_t sessionId)1949 AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
1950 audio_session_t sessionId)
1951 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1952 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
1953 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
1954 {
1955 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1956 if (thread == NULL) {
1957 return;
1958 }
1959 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1960 thread->frameCount();
1961 }
1962
~EffectChain()1963 AudioFlinger::EffectChain::~EffectChain()
1964 {
1965 }
1966
1967 // getEffectFromDesc_l() must be called with ThreadBase::mLock held
getEffectFromDesc_l(effect_descriptor_t * descriptor)1968 sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1969 effect_descriptor_t *descriptor)
1970 {
1971 size_t size = mEffects.size();
1972
1973 for (size_t i = 0; i < size; i++) {
1974 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1975 return mEffects[i];
1976 }
1977 }
1978 return 0;
1979 }
1980
1981 // getEffectFromId_l() must be called with ThreadBase::mLock held
getEffectFromId_l(int id)1982 sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1983 {
1984 size_t size = mEffects.size();
1985
1986 for (size_t i = 0; i < size; i++) {
1987 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1988 if (id == 0 || mEffects[i]->id() == id) {
1989 return mEffects[i];
1990 }
1991 }
1992 return 0;
1993 }
1994
1995 // getEffectFromType_l() must be called with ThreadBase::mLock held
getEffectFromType_l(const effect_uuid_t * type)1996 sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1997 const effect_uuid_t *type)
1998 {
1999 size_t size = mEffects.size();
2000
2001 for (size_t i = 0; i < size; i++) {
2002 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2003 return mEffects[i];
2004 }
2005 }
2006 return 0;
2007 }
2008
getEffectIds()2009 std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2010 {
2011 std::vector<int> ids;
2012 Mutex::Autolock _l(mLock);
2013 for (size_t i = 0; i < mEffects.size(); i++) {
2014 ids.push_back(mEffects[i]->id());
2015 }
2016 return ids;
2017 }
2018
clearInputBuffer()2019 void AudioFlinger::EffectChain::clearInputBuffer()
2020 {
2021 Mutex::Autolock _l(mLock);
2022 sp<ThreadBase> thread = mThread.promote();
2023 if (thread == 0) {
2024 ALOGW("clearInputBuffer(): cannot promote mixer thread");
2025 return;
2026 }
2027 clearInputBuffer_l(thread);
2028 }
2029
2030 // Must be called with EffectChain::mLock locked
clearInputBuffer_l(const sp<ThreadBase> & thread)2031 void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
2032 {
2033 if (mInBuffer == NULL) {
2034 return;
2035 }
2036 const size_t frameSize =
2037 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * thread->channelCount();
2038
2039 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
2040 mInBuffer->commit();
2041 }
2042
2043 // Must be called with EffectChain::mLock locked
process_l()2044 void AudioFlinger::EffectChain::process_l()
2045 {
2046 sp<ThreadBase> thread = mThread.promote();
2047 if (thread == 0) {
2048 ALOGW("process_l(): cannot promote mixer thread");
2049 return;
2050 }
2051 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
2052 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
2053 // never process effects when:
2054 // - on an OFFLOAD thread
2055 // - no more tracks are on the session and the effect tail has been rendered
2056 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
2057 && (thread->type() != ThreadBase::MMAP);
2058 if (!isGlobalSession) {
2059 bool tracksOnSession = (trackCnt() != 0);
2060
2061 if (!tracksOnSession && mTailBufferCount == 0) {
2062 doProcess = false;
2063 }
2064
2065 if (activeTrackCnt() == 0) {
2066 // if no track is active and the effect tail has not been rendered,
2067 // the input buffer must be cleared here as the mixer process will not do it
2068 if (tracksOnSession || mTailBufferCount > 0) {
2069 clearInputBuffer_l(thread);
2070 if (mTailBufferCount > 0) {
2071 mTailBufferCount--;
2072 }
2073 }
2074 }
2075 }
2076
2077 size_t size = mEffects.size();
2078 if (doProcess) {
2079 // Only the input and output buffers of the chain can be external,
2080 // and 'update' / 'commit' do nothing for allocated buffers, thus
2081 // it's not needed to consider any other buffers here.
2082 mInBuffer->update();
2083 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2084 mOutBuffer->update();
2085 }
2086 for (size_t i = 0; i < size; i++) {
2087 mEffects[i]->process();
2088 }
2089 mInBuffer->commit();
2090 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2091 mOutBuffer->commit();
2092 }
2093 }
2094 bool doResetVolume = false;
2095 for (size_t i = 0; i < size; i++) {
2096 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2097 }
2098 if (doResetVolume) {
2099 resetVolume_l();
2100 }
2101 }
2102
2103 // createEffect_l() must be called with ThreadBase::mLock held
createEffect_l(sp<EffectModule> & effect,ThreadBase * thread,effect_descriptor_t * desc,int id,audio_session_t sessionId,bool pinned)2104 status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
2105 ThreadBase *thread,
2106 effect_descriptor_t *desc,
2107 int id,
2108 audio_session_t sessionId,
2109 bool pinned)
2110 {
2111 Mutex::Autolock _l(mLock);
2112 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
2113 status_t lStatus = effect->status();
2114 if (lStatus == NO_ERROR) {
2115 lStatus = addEffect_ll(effect);
2116 }
2117 if (lStatus != NO_ERROR) {
2118 effect.clear();
2119 }
2120 return lStatus;
2121 }
2122
2123 // addEffect_l() must be called with ThreadBase::mLock held
addEffect_l(const sp<EffectModule> & effect)2124 status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2125 {
2126 Mutex::Autolock _l(mLock);
2127 return addEffect_ll(effect);
2128 }
2129 // addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
addEffect_ll(const sp<EffectModule> & effect)2130 status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2131 {
2132 effect_descriptor_t desc = effect->desc();
2133 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2134
2135 effect->setChain(this);
2136 sp<ThreadBase> thread = mThread.promote();
2137 if (thread == 0) {
2138 return NO_INIT;
2139 }
2140 effect->setThread(thread);
2141
2142 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2143 // Auxiliary effects are inserted at the beginning of mEffects vector as
2144 // they are processed first and accumulated in chain input buffer
2145 mEffects.insertAt(effect, 0);
2146
2147 // the input buffer for auxiliary effect contains mono samples in
2148 // 32 bit format. This is to avoid saturation in AudoMixer
2149 // accumulation stage. Saturation is done in EffectModule::process() before
2150 // calling the process in effect engine
2151 size_t numSamples = thread->frameCount();
2152 sp<EffectBufferHalInterface> halBuffer;
2153 #ifdef FLOAT_EFFECT_CHAIN
2154 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
2155 numSamples * sizeof(float), &halBuffer);
2156 #else
2157 status_t result = thread->mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
2158 numSamples * sizeof(int32_t), &halBuffer);
2159 #endif
2160 if (result != OK) return result;
2161 effect->setInBuffer(halBuffer);
2162 // auxiliary effects output samples to chain input buffer for further processing
2163 // by insert effects
2164 effect->setOutBuffer(mInBuffer);
2165 } else {
2166 // Insert effects are inserted at the end of mEffects vector as they are processed
2167 // after track and auxiliary effects.
2168 // Insert effect order as a function of indicated preference:
2169 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2170 // another effect is present
2171 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2172 // last effect claiming first position
2173 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2174 // first effect claiming last position
2175 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2176 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2177 // already present
2178
2179 size_t size = mEffects.size();
2180 size_t idx_insert = size;
2181 ssize_t idx_insert_first = -1;
2182 ssize_t idx_insert_last = -1;
2183
2184 for (size_t i = 0; i < size; i++) {
2185 effect_descriptor_t d = mEffects[i]->desc();
2186 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2187 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2188 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2189 // check invalid effect chaining combinations
2190 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2191 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2192 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2193 desc.name, d.name);
2194 return INVALID_OPERATION;
2195 }
2196 // remember position of first insert effect and by default
2197 // select this as insert position for new effect
2198 if (idx_insert == size) {
2199 idx_insert = i;
2200 }
2201 // remember position of last insert effect claiming
2202 // first position
2203 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2204 idx_insert_first = i;
2205 }
2206 // remember position of first insert effect claiming
2207 // last position
2208 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2209 idx_insert_last == -1) {
2210 idx_insert_last = i;
2211 }
2212 }
2213 }
2214
2215 // modify idx_insert from first position if needed
2216 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2217 if (idx_insert_last != -1) {
2218 idx_insert = idx_insert_last;
2219 } else {
2220 idx_insert = size;
2221 }
2222 } else {
2223 if (idx_insert_first != -1) {
2224 idx_insert = idx_insert_first + 1;
2225 }
2226 }
2227
2228 // always read samples from chain input buffer
2229 effect->setInBuffer(mInBuffer);
2230
2231 // if last effect in the chain, output samples to chain
2232 // output buffer, otherwise to chain input buffer
2233 if (idx_insert == size) {
2234 if (idx_insert != 0) {
2235 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2236 mEffects[idx_insert-1]->configure();
2237 }
2238 effect->setOutBuffer(mOutBuffer);
2239 } else {
2240 effect->setOutBuffer(mInBuffer);
2241 }
2242 mEffects.insertAt(effect, idx_insert);
2243
2244 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
2245 idx_insert);
2246 }
2247 effect->configure();
2248
2249 return NO_ERROR;
2250 }
2251
2252 // removeEffect_l() must be called with ThreadBase::mLock held
removeEffect_l(const sp<EffectModule> & effect,bool release)2253 size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2254 bool release)
2255 {
2256 Mutex::Autolock _l(mLock);
2257 size_t size = mEffects.size();
2258 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2259
2260 for (size_t i = 0; i < size; i++) {
2261 if (effect == mEffects[i]) {
2262 // calling stop here will remove pre-processing effect from the audio HAL.
2263 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2264 // the middle of a read from audio HAL
2265 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2266 mEffects[i]->state() == EffectModule::STOPPING) {
2267 mEffects[i]->stop();
2268 }
2269 if (release) {
2270 mEffects[i]->release_l();
2271 }
2272
2273 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
2274 if (i == size - 1 && i != 0) {
2275 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2276 mEffects[i - 1]->configure();
2277 }
2278 }
2279 mEffects.removeAt(i);
2280 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
2281 this, i);
2282
2283 break;
2284 }
2285 }
2286
2287 return mEffects.size();
2288 }
2289
2290 // setDevice_l() must be called with ThreadBase::mLock held
setDevice_l(audio_devices_t device)2291 void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2292 {
2293 size_t size = mEffects.size();
2294 for (size_t i = 0; i < size; i++) {
2295 mEffects[i]->setDevice(device);
2296 }
2297 }
2298
2299 // setMode_l() must be called with ThreadBase::mLock held
setMode_l(audio_mode_t mode)2300 void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2301 {
2302 size_t size = mEffects.size();
2303 for (size_t i = 0; i < size; i++) {
2304 mEffects[i]->setMode(mode);
2305 }
2306 }
2307
2308 // setAudioSource_l() must be called with ThreadBase::mLock held
setAudioSource_l(audio_source_t source)2309 void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2310 {
2311 size_t size = mEffects.size();
2312 for (size_t i = 0; i < size; i++) {
2313 mEffects[i]->setAudioSource(source);
2314 }
2315 }
2316
2317 // setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
setVolume_l(uint32_t * left,uint32_t * right,bool force)2318 bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
2319 {
2320 uint32_t newLeft = *left;
2321 uint32_t newRight = *right;
2322 bool hasControl = false;
2323 int ctrlIdx = -1;
2324 size_t size = mEffects.size();
2325
2326 // first update volume controller
2327 for (size_t i = size; i > 0; i--) {
2328 if (mEffects[i - 1]->isVolumeControlEnabled()) {
2329 ctrlIdx = i - 1;
2330 hasControl = true;
2331 break;
2332 }
2333 }
2334
2335 if (!force && ctrlIdx == mVolumeCtrlIdx &&
2336 *left == mLeftVolume && *right == mRightVolume) {
2337 if (hasControl) {
2338 *left = mNewLeftVolume;
2339 *right = mNewRightVolume;
2340 }
2341 return hasControl;
2342 }
2343
2344 mVolumeCtrlIdx = ctrlIdx;
2345 mLeftVolume = newLeft;
2346 mRightVolume = newRight;
2347
2348 // second get volume update from volume controller
2349 if (ctrlIdx >= 0) {
2350 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2351 mNewLeftVolume = newLeft;
2352 mNewRightVolume = newRight;
2353 }
2354 // then indicate volume to all other effects in chain.
2355 // Pass altered volume to effects before volume controller
2356 // and requested volume to effects after controller or with volume monitor flag
2357 uint32_t lVol = newLeft;
2358 uint32_t rVol = newRight;
2359
2360 for (size_t i = 0; i < size; i++) {
2361 if ((int)i == ctrlIdx) {
2362 continue;
2363 }
2364 // this also works for ctrlIdx == -1 when there is no volume controller
2365 if ((int)i > ctrlIdx) {
2366 lVol = *left;
2367 rVol = *right;
2368 }
2369 // Pass requested volume directly if this is volume monitor module
2370 if (mEffects[i]->isVolumeMonitor()) {
2371 mEffects[i]->setVolume(left, right, false);
2372 } else {
2373 mEffects[i]->setVolume(&lVol, &rVol, false);
2374 }
2375 }
2376 *left = newLeft;
2377 *right = newRight;
2378
2379 setVolumeForOutput_l(*left, *right);
2380
2381 return hasControl;
2382 }
2383
2384 // resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
resetVolume_l()2385 void AudioFlinger::EffectChain::resetVolume_l()
2386 {
2387 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2388 uint32_t left = mLeftVolume;
2389 uint32_t right = mRightVolume;
2390 (void)setVolume_l(&left, &right, true);
2391 }
2392 }
2393
syncHalEffectsState()2394 void AudioFlinger::EffectChain::syncHalEffectsState()
2395 {
2396 Mutex::Autolock _l(mLock);
2397 for (size_t i = 0; i < mEffects.size(); i++) {
2398 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2399 mEffects[i]->state() == EffectModule::STOPPING) {
2400 mEffects[i]->addEffectToHal_l();
2401 }
2402 }
2403 }
2404
dump(int fd,const Vector<String16> & args)2405 void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2406 {
2407 String8 result;
2408
2409 const size_t numEffects = mEffects.size();
2410 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
2411
2412 if (numEffects) {
2413 bool locked = AudioFlinger::dumpTryLock(mLock);
2414 // failed to lock - AudioFlinger is probably deadlocked
2415 if (!locked) {
2416 result.append("\tCould not lock mutex:\n");
2417 }
2418
2419 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2420 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2421 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2422 (int)inBufferStr.size(), "In buffer ",
2423 (int)outBufferStr.size(), "Out buffer ");
2424 result.appendFormat("\t%s %s %d\n",
2425 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
2426 write(fd, result.string(), result.size());
2427
2428 for (size_t i = 0; i < numEffects; ++i) {
2429 sp<EffectModule> effect = mEffects[i];
2430 if (effect != 0) {
2431 effect->dump(fd, args);
2432 }
2433 }
2434
2435 if (locked) {
2436 mLock.unlock();
2437 }
2438 } else {
2439 write(fd, result.string(), result.size());
2440 }
2441 }
2442
2443 // must be called with ThreadBase::mLock held
setEffectSuspended_l(const effect_uuid_t * type,bool suspend)2444 void AudioFlinger::EffectChain::setEffectSuspended_l(
2445 const effect_uuid_t *type, bool suspend)
2446 {
2447 sp<SuspendedEffectDesc> desc;
2448 // use effect type UUID timelow as key as there is no real risk of identical
2449 // timeLow fields among effect type UUIDs.
2450 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2451 if (suspend) {
2452 if (index >= 0) {
2453 desc = mSuspendedEffects.valueAt(index);
2454 } else {
2455 desc = new SuspendedEffectDesc();
2456 desc->mType = *type;
2457 mSuspendedEffects.add(type->timeLow, desc);
2458 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2459 }
2460
2461 if (desc->mRefCount++ == 0) {
2462 sp<EffectModule> effect = getEffectIfEnabled(type);
2463 if (effect != 0) {
2464 desc->mEffect = effect;
2465 effect->setSuspended(true);
2466 effect->setEnabled(false);
2467 }
2468 }
2469 } else {
2470 if (index < 0) {
2471 return;
2472 }
2473 desc = mSuspendedEffects.valueAt(index);
2474 if (desc->mRefCount <= 0) {
2475 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
2476 desc->mRefCount = 0;
2477 return;
2478 }
2479 if (--desc->mRefCount == 0) {
2480 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2481 if (desc->mEffect != 0) {
2482 sp<EffectModule> effect = desc->mEffect.promote();
2483 if (effect != 0) {
2484 effect->setSuspended(false);
2485 effect->lock();
2486 EffectHandle *handle = effect->controlHandle_l();
2487 if (handle != NULL && !handle->disconnected()) {
2488 effect->setEnabled_l(handle->enabled());
2489 }
2490 effect->unlock();
2491 }
2492 desc->mEffect.clear();
2493 }
2494 mSuspendedEffects.removeItemsAt(index);
2495 }
2496 }
2497 }
2498
2499 // must be called with ThreadBase::mLock held
setEffectSuspendedAll_l(bool suspend)2500 void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2501 {
2502 sp<SuspendedEffectDesc> desc;
2503
2504 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2505 if (suspend) {
2506 if (index >= 0) {
2507 desc = mSuspendedEffects.valueAt(index);
2508 } else {
2509 desc = new SuspendedEffectDesc();
2510 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2511 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2512 }
2513 if (desc->mRefCount++ == 0) {
2514 Vector< sp<EffectModule> > effects;
2515 getSuspendEligibleEffects(effects);
2516 for (size_t i = 0; i < effects.size(); i++) {
2517 setEffectSuspended_l(&effects[i]->desc().type, true);
2518 }
2519 }
2520 } else {
2521 if (index < 0) {
2522 return;
2523 }
2524 desc = mSuspendedEffects.valueAt(index);
2525 if (desc->mRefCount <= 0) {
2526 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2527 desc->mRefCount = 1;
2528 }
2529 if (--desc->mRefCount == 0) {
2530 Vector<const effect_uuid_t *> types;
2531 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2532 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2533 continue;
2534 }
2535 types.add(&mSuspendedEffects.valueAt(i)->mType);
2536 }
2537 for (size_t i = 0; i < types.size(); i++) {
2538 setEffectSuspended_l(types[i], false);
2539 }
2540 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2541 mSuspendedEffects.keyAt(index));
2542 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2543 }
2544 }
2545 }
2546
2547
2548 // The volume effect is used for automated tests only
2549 #ifndef OPENSL_ES_H_
2550 static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2551 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2552 const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2553 #endif //OPENSL_ES_H_
2554
2555 /* static */
isEffectEligibleForBtNrecSuspend(const effect_uuid_t * type)2556 bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2557 {
2558 // Only NS and AEC are suspended when BtNRec is off
2559 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2560 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2561 return true;
2562 }
2563 return false;
2564 }
2565
isEffectEligibleForSuspend(const effect_descriptor_t & desc)2566 bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2567 {
2568 // auxiliary effects and visualizer are never suspended on output mix
2569 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2570 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2571 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2572 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2573 return false;
2574 }
2575 return true;
2576 }
2577
getSuspendEligibleEffects(Vector<sp<AudioFlinger::EffectModule>> & effects)2578 void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2579 Vector< sp<AudioFlinger::EffectModule> > &effects)
2580 {
2581 effects.clear();
2582 for (size_t i = 0; i < mEffects.size(); i++) {
2583 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2584 effects.add(mEffects[i]);
2585 }
2586 }
2587 }
2588
getEffectIfEnabled(const effect_uuid_t * type)2589 sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2590 const effect_uuid_t *type)
2591 {
2592 sp<EffectModule> effect = getEffectFromType_l(type);
2593 return effect != 0 && effect->isEnabled() ? effect : 0;
2594 }
2595
checkSuspendOnEffectEnabled(const sp<EffectModule> & effect,bool enabled)2596 void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2597 bool enabled)
2598 {
2599 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2600 if (enabled) {
2601 if (index < 0) {
2602 // if the effect is not suspend check if all effects are suspended
2603 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2604 if (index < 0) {
2605 return;
2606 }
2607 if (!isEffectEligibleForSuspend(effect->desc())) {
2608 return;
2609 }
2610 setEffectSuspended_l(&effect->desc().type, enabled);
2611 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2612 if (index < 0) {
2613 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2614 return;
2615 }
2616 }
2617 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2618 effect->desc().type.timeLow);
2619 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2620 // if effect is requested to suspended but was not yet enabled, suspend it now.
2621 if (desc->mEffect == 0) {
2622 desc->mEffect = effect;
2623 effect->setEnabled(false);
2624 effect->setSuspended(true);
2625 }
2626 } else {
2627 if (index < 0) {
2628 return;
2629 }
2630 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2631 effect->desc().type.timeLow);
2632 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2633 desc->mEffect.clear();
2634 effect->setSuspended(false);
2635 }
2636 }
2637
isNonOffloadableEnabled()2638 bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
2639 {
2640 Mutex::Autolock _l(mLock);
2641 return isNonOffloadableEnabled_l();
2642 }
2643
isNonOffloadableEnabled_l()2644 bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2645 {
2646 size_t size = mEffects.size();
2647 for (size_t i = 0; i < size; i++) {
2648 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
2649 return true;
2650 }
2651 }
2652 return false;
2653 }
2654
setThread(const sp<ThreadBase> & thread)2655 void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2656 {
2657 Mutex::Autolock _l(mLock);
2658 mThread = thread;
2659 for (size_t i = 0; i < mEffects.size(); i++) {
2660 mEffects[i]->setThread(thread);
2661 }
2662 }
2663
checkOutputFlagCompatibility(audio_output_flags_t * flags) const2664 void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2665 {
2666 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2667 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2668 }
2669 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2670 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2671 }
2672 }
2673
checkInputFlagCompatibility(audio_input_flags_t * flags) const2674 void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2675 {
2676 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2677 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2678 }
2679 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2680 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2681 }
2682 }
2683
isRawCompatible() const2684 bool AudioFlinger::EffectChain::isRawCompatible() const
2685 {
2686 Mutex::Autolock _l(mLock);
2687 for (const auto &effect : mEffects) {
2688 if (effect->isProcessImplemented()) {
2689 return false;
2690 }
2691 }
2692 // Allow effects without processing.
2693 return true;
2694 }
2695
isFastCompatible() const2696 bool AudioFlinger::EffectChain::isFastCompatible() const
2697 {
2698 Mutex::Autolock _l(mLock);
2699 for (const auto &effect : mEffects) {
2700 if (effect->isProcessImplemented()
2701 && effect->isImplementationSoftware()) {
2702 return false;
2703 }
2704 }
2705 // Allow effects without processing or hw accelerated effects.
2706 return true;
2707 }
2708
2709 // isCompatibleWithThread_l() must be called with thread->mLock held
isCompatibleWithThread_l(const sp<ThreadBase> & thread) const2710 bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2711 {
2712 Mutex::Autolock _l(mLock);
2713 for (size_t i = 0; i < mEffects.size(); i++) {
2714 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2715 return false;
2716 }
2717 }
2718 return true;
2719 }
2720
2721 } // namespace android
2722