blob: 09cc852e7d40b2b81ddee4d40a9112470ce6bced [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
27#include <system/audio_effects/effect_ns.h>
28#include <system/audio_effects/effect_visualizer.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080029#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070030#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070031#include <media/audiohal/EffectHalInterface.h>
32#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080033
34#include "AudioFlinger.h"
35#include "ServiceUtilities.h"
36
37// ----------------------------------------------------------------------------
38
39// Note: the following macro is used for extremely verbose logging message. In
40// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
41// 0; but one side effect of this is to turn all LOGV's as well. Some messages
42// are so verbose that we want to suppress them even when we have ALOG_ASSERT
43// turned on. Do not uncomment the #def below unless you really know what you
44// are doing and want to see all of the extremely verbose messages.
45//#define VERY_VERY_VERBOSE_LOGGING
46#ifdef VERY_VERY_VERBOSE_LOGGING
47#define ALOGVV ALOGV
48#else
49#define ALOGVV(a...) do { } while(0)
50#endif
51
52namespace android {
53
54// ----------------------------------------------------------------------------
55// EffectModule implementation
56// ----------------------------------------------------------------------------
57
58#undef LOG_TAG
59#define LOG_TAG "AudioFlinger::EffectModule"
60
61AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
62 const wp<AudioFlinger::EffectChain>& chain,
63 effect_descriptor_t *desc,
64 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080065 audio_session_t sessionId,
66 bool pinned)
67 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080068 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69 mDescriptor(*desc),
Andy Hung6f88dc42017-12-13 16:19:39 -080070 // mConfig cleared in constructor body, set by configure()
Eric Laurentca7cc822012-11-19 14:55:58 -080071 mStatus(NO_INIT), mState(IDLE),
72 // mMaxDisableWaitCnt is set by configure() and not used before then
73 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070074 mSuspended(false),
75 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070076#ifdef FLOAT_EFFECT_CHAIN
77 , mSupportsFloat(false)
78#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080079{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080080 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080081 int lStatus;
82
Andy Hung6f88dc42017-12-13 16:19:39 -080083 // clear configuration to ensure consistent initial value of buffer framecount
84 // in case buffers are associated by setInBuffer() or setOutBuffer()
85 // prior to configure().
86 memset(&mConfig, 0, sizeof(mConfig));
87
Eric Laurentca7cc822012-11-19 14:55:58 -080088 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070089 mStatus = -ENODEV;
90 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070091 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070092 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070093 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070094 mStatus = effectsFactory->createEffect(
95 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
96 }
97 }
Eric Laurentca7cc822012-11-19 14:55:58 -080098
99 if (mStatus != NO_ERROR) {
100 return;
101 }
102 lStatus = init();
103 if (lStatus < 0) {
104 mStatus = lStatus;
105 goto Error;
106 }
107
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800108 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700109 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110
Eric Laurentca7cc822012-11-19 14:55:58 -0800111 return;
112Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700113 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800114 ALOGV("Constructor Error %d", mStatus);
115}
116
117AudioFlinger::EffectModule::~EffectModule()
118{
119 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700120 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700121 char uuidStr[64];
122 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
123 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
124 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800125 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800126 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800127
Eric Laurentca7cc822012-11-19 14:55:58 -0800128}
129
130status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
131{
132 status_t status;
133
134 Mutex::Autolock _l(mLock);
135 int priority = handle->priority();
136 size_t size = mHandles.size();
137 EffectHandle *controlHandle = NULL;
138 size_t i;
139 for (i = 0; i < size; i++) {
140 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800141 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800142 continue;
143 }
144 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700145 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800146 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700147 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800148 if (h->priority() <= priority) {
149 break;
150 }
151 }
152 // if inserted in first place, move effect control from previous owner to this handle
153 if (i == 0) {
154 bool enabled = false;
155 if (controlHandle != NULL) {
156 enabled = controlHandle->enabled();
157 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
158 }
159 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
160 status = NO_ERROR;
161 } else {
162 status = ALREADY_EXISTS;
163 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700164 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800165 mHandles.insertAt(handle, i);
166 return status;
167}
168
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800169ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800170{
171 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800172 return removeHandle_l(handle);
173}
174
175ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
176{
Eric Laurentca7cc822012-11-19 14:55:58 -0800177 size_t size = mHandles.size();
178 size_t i;
179 for (i = 0; i < size; i++) {
180 if (mHandles[i] == handle) {
181 break;
182 }
183 }
184 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800185 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
186 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800187 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800188 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800189
190 mHandles.removeAt(i);
191 // if removed from first place, move effect control from this handle to next in line
192 if (i == 0) {
193 EffectHandle *h = controlHandle_l();
194 if (h != NULL) {
195 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
196 }
197 }
198
199 // Prevent calls to process() and other functions on effect interface from now on.
200 // The effect engine will be released by the destructor when the last strong reference on
201 // this object is released which can happen after next process is called.
202 if (mHandles.size() == 0 && !mPinned) {
203 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800204 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800205 }
206
207 return mHandles.size();
208}
209
210// must be called with EffectModule::mLock held
211AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
212{
213 // the first valid handle in the list has control over the module
214 for (size_t i = 0; i < mHandles.size(); i++) {
215 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800216 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800217 return h;
218 }
219 }
220
221 return NULL;
222}
223
Eric Laurentf10c7092016-12-06 17:09:56 -0800224// unsafe method called when the effect parent thread has been destroyed
225ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
226{
227 ALOGV("disconnect() %p handle %p", this, handle);
228 Mutex::Autolock _l(mLock);
229 ssize_t numHandles = removeHandle_l(handle);
230 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
231 AudioSystem::unregisterEffect(mId);
232 sp<AudioFlinger> af = mAudioFlinger.promote();
233 if (af != 0) {
234 mLock.unlock();
235 af->updateOrphanEffectChains(this);
236 mLock.lock();
237 }
238 }
239 return numHandles;
240}
241
Eric Laurentfa1e1232016-08-02 19:01:49 -0700242bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800243 Mutex::Autolock _l(mLock);
244
Eric Laurentfa1e1232016-08-02 19:01:49 -0700245 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800246 switch (mState) {
247 case RESTART:
248 reset_l();
249 // FALL THROUGH
250
251 case STARTING:
252 // clear auxiliary effect input buffer for next accumulation
253 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
254 memset(mConfig.inputCfg.buffer.raw,
255 0,
256 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
257 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700258 if (start_l() == NO_ERROR) {
259 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700260 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700261 } else {
262 mState = IDLE;
263 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800264 break;
265 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700266 if (stop_l() == NO_ERROR) {
267 mDisableWaitCnt = mMaxDisableWaitCnt;
268 } else {
269 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
270 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800271 mState = STOPPED;
272 break;
273 case STOPPED:
274 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
275 // turn off sequence.
276 if (--mDisableWaitCnt == 0) {
277 reset_l();
278 mState = IDLE;
279 }
280 break;
281 default: //IDLE , ACTIVE, DESTROYED
282 break;
283 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700284
285 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800286}
287
288void AudioFlinger::EffectModule::process()
289{
290 Mutex::Autolock _l(mLock);
291
Mikhail Naganov022b9952017-01-04 16:36:51 -0800292 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800293 return;
294 }
295
rago94a1ee82017-07-21 15:11:02 -0700296 // TODO: Implement multichannel effects; here outChannelCount == FCC_2 == 2
297 const uint32_t inChannelCount =
298 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
299 const uint32_t outChannelCount =
300 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
301 const bool auxType =
302 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
303
Andy Hungfa69ca32017-11-30 10:07:53 -0800304 // safeInputOutputSampleCount is 0 if the channel count between input and output
305 // buffers do not match. This prevents automatic accumulation or copying between the
306 // input and output effect buffers without an intermediary effect process.
307 // TODO: consider implementing channel conversion.
308 const size_t safeInputOutputSampleCount =
309 inChannelCount != outChannelCount ? 0
310 : outChannelCount * std::min(
311 mConfig.inputCfg.buffer.frameCount,
312 mConfig.outputCfg.buffer.frameCount);
313 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
314#ifdef FLOAT_EFFECT_CHAIN
315 accumulate_float(
316 mConfig.outputCfg.buffer.f32,
317 mConfig.inputCfg.buffer.f32,
318 safeInputOutputSampleCount);
319#else
320 accumulate_i16(
321 mConfig.outputCfg.buffer.s16,
322 mConfig.inputCfg.buffer.s16,
323 safeInputOutputSampleCount);
324#endif
325 };
326 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
327#ifdef FLOAT_EFFECT_CHAIN
328 memcpy(
329 mConfig.outputCfg.buffer.f32,
330 mConfig.inputCfg.buffer.f32,
331 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
332
333#else
334 memcpy(
335 mConfig.outputCfg.buffer.s16,
336 mConfig.inputCfg.buffer.s16,
337 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
338#endif
339 };
340
Eric Laurentca7cc822012-11-19 14:55:58 -0800341 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700342 int ret;
343 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700344 if (auxType) {
345 // We overwrite the aux input buffer here and clear after processing.
346 // Note that aux input buffers are format q4_27.
347#ifdef FLOAT_EFFECT_CHAIN
348 if (mSupportsFloat) {
349 // Do in-place float conversion for auxiliary effect input buffer.
350 static_assert(sizeof(float) <= sizeof(int32_t),
351 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
352
Andy Hungfa69ca32017-11-30 10:07:53 -0800353 memcpy_to_float_from_q4_27(
354 mConfig.inputCfg.buffer.f32,
355 mConfig.inputCfg.buffer.s32,
356 mConfig.inputCfg.buffer.frameCount);
357 } else
358#endif
359 {
360 memcpy_to_i16_from_q4_27(
361 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700362 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800363 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700364 }
rago94a1ee82017-07-21 15:11:02 -0700365 }
366#ifdef FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800367 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
368 if (!auxType) {
369 if (mInConversionBuffer.get() == nullptr) {
370 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
371 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700372 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800373 memcpy_to_i16_from_float(
374 mInConversionBuffer->audioBuffer()->s16,
375 mInBuffer->audioBuffer()->f32,
376 inChannelCount * mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700377 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800378 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
379 if (mOutConversionBuffer.get() == nullptr) {
380 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
381 goto data_bypass;
382 }
383 memcpy_to_i16_from_float(
384 mOutConversionBuffer->audioBuffer()->s16,
385 mOutBuffer->audioBuffer()->f32,
386 outChannelCount * mConfig.outputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700387 }
388 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800389#endif
390
Mikhail Naganov022b9952017-01-04 16:36:51 -0800391 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800392
393#ifdef FLOAT_EFFECT_CHAIN
394 if (!mSupportsFloat) { // convert output int16_t back to float.
395 memcpy_to_float_from_i16(
396 mOutBuffer->audioBuffer()->f32,
397 mOutConversionBuffer->audioBuffer()->s16,
398 outChannelCount * mConfig.outputCfg.buffer.frameCount);
399 }
rago94a1ee82017-07-21 15:11:02 -0700400#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700401 } else {
rago94a1ee82017-07-21 15:11:02 -0700402#ifdef FLOAT_EFFECT_CHAIN
403 data_bypass:
404#endif
405 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800406 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700407 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800408 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700409 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800410 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700411 }
412 }
413 ret = -ENODATA;
414 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800415
Eric Laurentca7cc822012-11-19 14:55:58 -0800416 // force transition to IDLE state when engine is ready
417 if (mState == STOPPED && ret == -ENODATA) {
418 mDisableWaitCnt = 1;
419 }
420
421 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700422 if (auxType) {
423 // input always q4_27 regardless of FLOAT_EFFECT_CHAIN.
424 const size_t size =
425 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
426 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800427 }
428 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700429 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800430 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
431 // If an insert effect is idle and input buffer is different from output buffer,
432 // accumulate input onto output
433 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800434 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
435 accumulateInputToOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800436 }
437 }
438}
439
440void AudioFlinger::EffectModule::reset_l()
441{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700442 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800443 return;
444 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700445 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800446}
447
448status_t AudioFlinger::EffectModule::configure()
449{
rago94a1ee82017-07-21 15:11:02 -0700450 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700451 status_t status;
452 sp<ThreadBase> thread;
453 uint32_t size;
454 audio_channel_mask_t channelMask;
455
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700456 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700457 status = NO_INIT;
458 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800459 }
460
Eric Laurentd0ebb532013-04-02 16:41:41 -0700461 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800462 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700463 status = DEAD_OBJECT;
464 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800465 }
466
467 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700468 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700469 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800470
471 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
472 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900473 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
474 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800475 } else {
476 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700477 // TODO: Update this logic when multichannel effects are implemented.
478 // For offloaded tracks consider mono output as stereo for proper effect initialization
479 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
480 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
481 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
482 ALOGV("Overriding effect input and output as STEREO");
483 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800484 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700485
rago94a1ee82017-07-21 15:11:02 -0700486 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
487 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800488 mConfig.inputCfg.samplingRate = thread->sampleRate();
489 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
490 mConfig.inputCfg.bufferProvider.cookie = NULL;
491 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
492 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
493 mConfig.outputCfg.bufferProvider.cookie = NULL;
494 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
495 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
496 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
497 // Insert effect:
498 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
499 // always overwrites output buffer: input buffer == output buffer
500 // - in other sessions:
501 // last effect in the chain accumulates in output buffer: input buffer != output buffer
502 // other effect: overwrites output buffer: input buffer == output buffer
503 // Auxiliary effect:
504 // accumulates in output buffer: input buffer != output buffer
505 // Therefore: accumulate <=> input buffer != output buffer
506 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
507 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
508 } else {
509 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
510 }
511 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
512 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
513 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
514 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
515
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700516 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800517 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
518
519 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700520 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700521 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
522 sizeof(effect_config_t),
523 &mConfig,
524 &size,
525 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700526 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800527 status = cmdStatus;
rago94a1ee82017-07-21 15:11:02 -0700528#ifdef FLOAT_EFFECT_CHAIN
529 mSupportsFloat = true;
530#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800531 }
rago94a1ee82017-07-21 15:11:02 -0700532#ifdef FLOAT_EFFECT_CHAIN
533 else {
534 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
535 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
536 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
537 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
538 sizeof(effect_config_t),
539 &mConfig,
540 &size,
541 &cmdStatus);
542 if (status == NO_ERROR) {
543 status = cmdStatus;
544 mSupportsFloat = false;
545 ALOGVV("config worked with 16 bit");
546 } else {
547 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800548 }
rago94a1ee82017-07-21 15:11:02 -0700549 }
550#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800551
rago94a1ee82017-07-21 15:11:02 -0700552 if (status == NO_ERROR) {
553 // Establish Buffer strategy
554 setInBuffer(mInBuffer);
555 setOutBuffer(mOutBuffer);
556
557 // Update visualizer latency
558 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
559 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
560 effect_param_t *p = (effect_param_t *)buf32;
561
562 p->psize = sizeof(uint32_t);
563 p->vsize = sizeof(uint32_t);
564 size = sizeof(int);
565 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
566
567 uint32_t latency = 0;
568 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
569 if (pbt != NULL) {
570 latency = pbt->latency_l();
571 }
572
573 *((int32_t *)p->data + 1)= latency;
574 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
575 sizeof(effect_param_t) + 8,
576 &buf32,
577 &size,
578 &cmdStatus);
579 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800580 }
581
582 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
583 (1000 * mConfig.outputCfg.buffer.frameCount);
584
Eric Laurentd0ebb532013-04-02 16:41:41 -0700585exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800586 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700587 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700588 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800589 return status;
590}
591
592status_t AudioFlinger::EffectModule::init()
593{
594 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700595 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800596 return NO_INIT;
597 }
598 status_t cmdStatus;
599 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700600 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
601 0,
602 NULL,
603 &size,
604 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800605 if (status == 0) {
606 status = cmdStatus;
607 }
608 return status;
609}
610
Eric Laurent1b928682014-10-02 19:41:47 -0700611void AudioFlinger::EffectModule::addEffectToHal_l()
612{
613 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
614 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
615 sp<ThreadBase> thread = mThread.promote();
616 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700617 sp<StreamHalInterface> stream = thread->stream();
618 if (stream != 0) {
619 status_t result = stream->addEffect(mEffectInterface);
620 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700621 }
622 }
623 }
624}
625
Eric Laurentfa1e1232016-08-02 19:01:49 -0700626// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800627status_t AudioFlinger::EffectModule::start()
628{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700629 sp<EffectChain> chain;
630 status_t status;
631 {
632 Mutex::Autolock _l(mLock);
633 status = start_l();
634 if (status == NO_ERROR) {
635 chain = mChain.promote();
636 }
637 }
638 if (chain != 0) {
639 chain->resetVolume_l();
640 }
641 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800642}
643
644status_t AudioFlinger::EffectModule::start_l()
645{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700646 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800647 return NO_INIT;
648 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700649 if (mStatus != NO_ERROR) {
650 return mStatus;
651 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800652 status_t cmdStatus;
653 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700654 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
655 0,
656 NULL,
657 &size,
658 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800659 if (status == 0) {
660 status = cmdStatus;
661 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700662 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700663 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800664 }
665 return status;
666}
667
668status_t AudioFlinger::EffectModule::stop()
669{
670 Mutex::Autolock _l(mLock);
671 return stop_l();
672}
673
674status_t AudioFlinger::EffectModule::stop_l()
675{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700676 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800677 return NO_INIT;
678 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700679 if (mStatus != NO_ERROR) {
680 return mStatus;
681 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800682 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800683 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700684 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
685 0,
686 NULL,
687 &size,
688 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800689 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800690 status = cmdStatus;
691 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800692 if (status == NO_ERROR) {
693 status = remove_effect_from_hal_l();
694 }
695 return status;
696}
697
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800698// must be called with EffectChain::mLock held
699void AudioFlinger::EffectModule::release_l()
700{
701 if (mEffectInterface != 0) {
702 remove_effect_from_hal_l();
703 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800704 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800705 mEffectInterface.clear();
706 }
707}
708
Eric Laurentbfb1b832013-01-07 09:53:42 -0800709status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
710{
711 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
712 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800713 sp<ThreadBase> thread = mThread.promote();
714 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700715 sp<StreamHalInterface> stream = thread->stream();
716 if (stream != 0) {
717 status_t result = stream->removeEffect(mEffectInterface);
718 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800719 }
720 }
721 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800722 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800723}
724
Andy Hunge4a1d912016-08-17 14:11:13 -0700725// round up delta valid if value and divisor are positive.
726template <typename T>
727static T roundUpDelta(const T &value, const T &divisor) {
728 T remainder = value % divisor;
729 return remainder == 0 ? 0 : divisor - remainder;
730}
731
Eric Laurentca7cc822012-11-19 14:55:58 -0800732status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
733 uint32_t cmdSize,
734 void *pCmdData,
735 uint32_t *replySize,
736 void *pReplyData)
737{
738 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700739 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800740
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700741 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800742 return NO_INIT;
743 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700744 if (mStatus != NO_ERROR) {
745 return mStatus;
746 }
Andy Hung110bc952016-06-20 15:22:52 -0700747 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700748 (sizeof(effect_param_t) > cmdSize ||
749 ((effect_param_t *)pCmdData)->psize > cmdSize
750 - sizeof(effect_param_t))) {
751 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800752 android_errorWriteLog(0x534e4554, "33003822");
753 return -EINVAL;
754 }
755 if (cmdCode == EFFECT_CMD_GET_PARAM &&
756 (*replySize < sizeof(effect_param_t) ||
757 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
758 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700759 return -EINVAL;
760 }
ragoe2759072016-11-22 18:02:48 -0800761 if (cmdCode == EFFECT_CMD_GET_PARAM &&
762 (sizeof(effect_param_t) > *replySize
763 || ((effect_param_t *)pCmdData)->psize > *replySize
764 - sizeof(effect_param_t)
765 || ((effect_param_t *)pCmdData)->vsize > *replySize
766 - sizeof(effect_param_t)
767 - ((effect_param_t *)pCmdData)->psize
768 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
769 *replySize
770 - sizeof(effect_param_t)
771 - ((effect_param_t *)pCmdData)->psize
772 - ((effect_param_t *)pCmdData)->vsize)) {
773 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
774 android_errorWriteLog(0x534e4554, "32705438");
775 return -EINVAL;
776 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700777 if ((cmdCode == EFFECT_CMD_SET_PARAM
778 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
779 (sizeof(effect_param_t) > cmdSize
780 || ((effect_param_t *)pCmdData)->psize > cmdSize
781 - sizeof(effect_param_t)
782 || ((effect_param_t *)pCmdData)->vsize > cmdSize
783 - sizeof(effect_param_t)
784 - ((effect_param_t *)pCmdData)->psize
785 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
786 cmdSize
787 - sizeof(effect_param_t)
788 - ((effect_param_t *)pCmdData)->psize
789 - ((effect_param_t *)pCmdData)->vsize)) {
790 android_errorWriteLog(0x534e4554, "30204301");
791 return -EINVAL;
792 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700793 status_t status = mEffectInterface->command(cmdCode,
794 cmdSize,
795 pCmdData,
796 replySize,
797 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800798 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
799 uint32_t size = (replySize == NULL) ? 0 : *replySize;
800 for (size_t i = 1; i < mHandles.size(); i++) {
801 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800802 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800803 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
804 }
805 }
806 }
807 return status;
808}
809
810status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
811{
812 Mutex::Autolock _l(mLock);
813 return setEnabled_l(enabled);
814}
815
816// must be called with EffectModule::mLock held
817status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
818{
819
820 ALOGV("setEnabled %p enabled %d", this, enabled);
821
822 if (enabled != isEnabled()) {
823 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
824 if (enabled && status != NO_ERROR) {
825 return status;
826 }
827
828 switch (mState) {
829 // going from disabled to enabled
830 case IDLE:
831 mState = STARTING;
832 break;
833 case STOPPED:
834 mState = RESTART;
835 break;
836 case STOPPING:
837 mState = ACTIVE;
838 break;
839
840 // going from enabled to disabled
841 case RESTART:
842 mState = STOPPED;
843 break;
844 case STARTING:
845 mState = IDLE;
846 break;
847 case ACTIVE:
848 mState = STOPPING;
849 break;
850 case DESTROYED:
851 return NO_ERROR; // simply ignore as we are being destroyed
852 }
853 for (size_t i = 1; i < mHandles.size(); i++) {
854 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800855 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800856 h->setEnabled(enabled);
857 }
858 }
859 }
860 return NO_ERROR;
861}
862
863bool AudioFlinger::EffectModule::isEnabled() const
864{
865 switch (mState) {
866 case RESTART:
867 case STARTING:
868 case ACTIVE:
869 return true;
870 case IDLE:
871 case STOPPING:
872 case STOPPED:
873 case DESTROYED:
874 default:
875 return false;
876 }
877}
878
879bool AudioFlinger::EffectModule::isProcessEnabled() const
880{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700881 if (mStatus != NO_ERROR) {
882 return false;
883 }
884
Eric Laurentca7cc822012-11-19 14:55:58 -0800885 switch (mState) {
886 case RESTART:
887 case ACTIVE:
888 case STOPPING:
889 case STOPPED:
890 return true;
891 case IDLE:
892 case STARTING:
893 case DESTROYED:
894 default:
895 return false;
896 }
897}
898
Mikhail Naganov022b9952017-01-04 16:36:51 -0800899void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700900 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800901
902 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800903 if (buffer != 0) {
904 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
905 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
906 } else {
907 mConfig.inputCfg.buffer.raw = NULL;
908 }
909 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800910 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700911
912#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800913 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -0700914 // Theoretically insert effects can also do in-place conversions (destroying
915 // the original buffer) when the output buffer is identical to the input buffer,
916 // but we don't optimize for it here.
917 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
918 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
919 // we need to translate - create hidl shared buffer and intercept
920 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
921 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
922 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
923
924 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
925 __func__, inChannels, inFrameCount, size);
926
Andy Hungbded9c82017-11-30 18:47:35 -0800927 if (size > 0 && (mInConversionBuffer.get() == nullptr
928 || size > mInConversionBuffer->getSize())) {
929 mInConversionBuffer.clear();
930 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
931 (void)EffectBufferHalInterface::allocate(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700932 }
Andy Hungbded9c82017-11-30 18:47:35 -0800933 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -0800934 mInConversionBuffer->setFrameCount(inFrameCount);
935 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700936 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800937 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700938 }
939 }
940#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800941}
942
943void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700944 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800945
946 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800947 if (buffer != 0) {
948 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
949 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
950 } else {
951 mConfig.outputCfg.buffer.raw = NULL;
952 }
953 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800954 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700955
956#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800957 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -0700958 // can do in-place conversion from int16_t to float. We don't optimize here.
959 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
960 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
961 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
962 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
963
964 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
965 __func__, outChannels, outFrameCount, size);
966
Andy Hungbded9c82017-11-30 18:47:35 -0800967 if (size > 0 && (mOutConversionBuffer.get() == nullptr
968 || size > mOutConversionBuffer->getSize())) {
969 mOutConversionBuffer.clear();
970 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
971 (void)EffectBufferHalInterface::allocate(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700972 }
Andy Hungbded9c82017-11-30 18:47:35 -0800973 if (mOutConversionBuffer.get() != nullptr) {
974 mOutConversionBuffer->setFrameCount(outFrameCount);
975 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700976 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800977 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700978 }
979 }
980#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800981}
982
Eric Laurentca7cc822012-11-19 14:55:58 -0800983status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
984{
985 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700986 if (mStatus != NO_ERROR) {
987 return mStatus;
988 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800989 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800990 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
991 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
992 if (isProcessEnabled() &&
993 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
994 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800995 uint32_t volume[2];
996 uint32_t *pVolume = NULL;
997 uint32_t size = sizeof(volume);
998 volume[0] = *left;
999 volume[1] = *right;
1000 if (controller) {
1001 pVolume = volume;
1002 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001003 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1004 size,
1005 volume,
1006 &size,
1007 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001008 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1009 *left = volume[0];
1010 *right = volume[1];
1011 }
1012 }
1013 return status;
1014}
1015
1016status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1017{
1018 if (device == AUDIO_DEVICE_NONE) {
1019 return NO_ERROR;
1020 }
1021
1022 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001023 if (mStatus != NO_ERROR) {
1024 return mStatus;
1025 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001026 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001027 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001028 status_t cmdStatus;
1029 uint32_t size = sizeof(status_t);
1030 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1031 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001032 status = mEffectInterface->command(cmd,
1033 sizeof(uint32_t),
1034 &device,
1035 &size,
1036 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001037 }
1038 return status;
1039}
1040
1041status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1042{
1043 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001044 if (mStatus != NO_ERROR) {
1045 return mStatus;
1046 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001047 status_t status = NO_ERROR;
1048 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1049 status_t cmdStatus;
1050 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001051 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1052 sizeof(audio_mode_t),
1053 &mode,
1054 &size,
1055 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001056 if (status == NO_ERROR) {
1057 status = cmdStatus;
1058 }
1059 }
1060 return status;
1061}
1062
1063status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1064{
1065 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001066 if (mStatus != NO_ERROR) {
1067 return mStatus;
1068 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 status_t status = NO_ERROR;
1070 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1071 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001072 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1073 sizeof(audio_source_t),
1074 &source,
1075 &size,
1076 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001077 }
1078 return status;
1079}
1080
1081void AudioFlinger::EffectModule::setSuspended(bool suspended)
1082{
1083 Mutex::Autolock _l(mLock);
1084 mSuspended = suspended;
1085}
1086
1087bool AudioFlinger::EffectModule::suspended() const
1088{
1089 Mutex::Autolock _l(mLock);
1090 return mSuspended;
1091}
1092
1093bool AudioFlinger::EffectModule::purgeHandles()
1094{
1095 bool enabled = false;
1096 Mutex::Autolock _l(mLock);
1097 for (size_t i = 0; i < mHandles.size(); i++) {
1098 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001099 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001100 if (handle->hasControl()) {
1101 enabled = handle->enabled();
1102 }
1103 }
1104 }
1105 return enabled;
1106}
1107
Eric Laurent5baf2af2013-09-12 17:37:00 -07001108status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1109{
1110 Mutex::Autolock _l(mLock);
1111 if (mStatus != NO_ERROR) {
1112 return mStatus;
1113 }
1114 status_t status = NO_ERROR;
1115 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1116 status_t cmdStatus;
1117 uint32_t size = sizeof(status_t);
1118 effect_offload_param_t cmd;
1119
1120 cmd.isOffload = offloaded;
1121 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001122 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1123 sizeof(effect_offload_param_t),
1124 &cmd,
1125 &size,
1126 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001127 if (status == NO_ERROR) {
1128 status = cmdStatus;
1129 }
1130 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1131 } else {
1132 if (offloaded) {
1133 status = INVALID_OPERATION;
1134 }
1135 mOffloaded = false;
1136 }
1137 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1138 return status;
1139}
1140
1141bool AudioFlinger::EffectModule::isOffloaded() const
1142{
1143 Mutex::Autolock _l(mLock);
1144 return mOffloaded;
1145}
1146
Marco Nelissenb2208842014-02-07 14:00:50 -08001147String8 effectFlagsToString(uint32_t flags) {
1148 String8 s;
1149
1150 s.append("conn. mode: ");
1151 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1152 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1153 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1154 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1155 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1156 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1157 default: s.append("unknown/reserved"); break;
1158 }
1159 s.append(", ");
1160
1161 s.append("insert pref: ");
1162 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1163 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1164 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1165 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1166 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1167 default: s.append("unknown/reserved"); break;
1168 }
1169 s.append(", ");
1170
1171 s.append("volume mgmt: ");
1172 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1173 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1174 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1175 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1176 default: s.append("unknown/reserved"); break;
1177 }
1178 s.append(", ");
1179
1180 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1181 if (devind) {
1182 s.append("device indication: ");
1183 switch (devind) {
1184 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1185 default: s.append("unknown/reserved"); break;
1186 }
1187 s.append(", ");
1188 }
1189
1190 s.append("input mode: ");
1191 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1192 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1193 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1194 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1195 default: s.append("not set"); break;
1196 }
1197 s.append(", ");
1198
1199 s.append("output mode: ");
1200 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1201 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1202 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1203 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1204 default: s.append("not set"); break;
1205 }
1206 s.append(", ");
1207
1208 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1209 if (accel) {
1210 s.append("hardware acceleration: ");
1211 switch (accel) {
1212 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1213 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1214 default: s.append("unknown/reserved"); break;
1215 }
1216 s.append(", ");
1217 }
1218
1219 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1220 if (modeind) {
1221 s.append("mode indication: ");
1222 switch (modeind) {
1223 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1224 default: s.append("unknown/reserved"); break;
1225 }
1226 s.append(", ");
1227 }
1228
1229 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1230 if (srcind) {
1231 s.append("source indication: ");
1232 switch (srcind) {
1233 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1234 default: s.append("unknown/reserved"); break;
1235 }
1236 s.append(", ");
1237 }
1238
1239 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1240 s.append("offloadable, ");
1241 }
1242
1243 int len = s.length();
1244 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001245 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001246 s.unlockBuffer(len - 2);
1247 }
1248 return s;
1249}
1250
Andy Hungbded9c82017-11-30 18:47:35 -08001251static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1252 std::stringstream ss;
1253
1254 if (buffer.get() == nullptr) {
1255 return "nullptr"; // make different than below
1256 } else if (buffer->externalData() != nullptr) {
1257 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1258 << " -> "
1259 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1260 } else {
1261 ss << buffer->audioBuffer()->raw;
1262 }
1263 return ss.str();
1264}
Marco Nelissenb2208842014-02-07 14:00:50 -08001265
Glenn Kasten0f11b512014-01-31 16:18:54 -08001266void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001267{
1268 const size_t SIZE = 256;
1269 char buffer[SIZE];
1270 String8 result;
1271
1272 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1273 result.append(buffer);
1274
1275 bool locked = AudioFlinger::dumpTryLock(mLock);
1276 // failed to lock - AudioFlinger is probably deadlocked
1277 if (!locked) {
1278 result.append("\t\tCould not lock Fx mutex:\n");
1279 }
1280
1281 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001282 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001283 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001284 result.append(buffer);
1285
1286 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001287 char uuidStr[64];
1288 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1289 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001290 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001291 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1292 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001293 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001294 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001295 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001296 mDescriptor.flags,
1297 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001298 result.append(buffer);
1299 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1300 mDescriptor.name);
1301 result.append(buffer);
1302 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1303 mDescriptor.implementor);
1304 result.append(buffer);
1305
1306 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001307 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001308 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001309 mConfig.inputCfg.buffer.frameCount,
1310 mConfig.inputCfg.samplingRate,
1311 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001312 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001313 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001314 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001315 result.append(buffer);
1316
1317 result.append("\t\t- Output configuration:\n");
1318 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001319 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001320 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001321 mConfig.outputCfg.buffer.frameCount,
1322 mConfig.outputCfg.samplingRate,
1323 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001324 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001325 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001326 result.append(buffer);
1327
rago94a1ee82017-07-21 15:11:02 -07001328#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001329
Andy Hungbded9c82017-11-30 18:47:35 -08001330 result.appendFormat("\t\t- HAL buffers:\n"
1331 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1332 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1333 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1334 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1335 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001336#endif
1337
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001338 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001339 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001340 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001341 for (size_t i = 0; i < mHandles.size(); ++i) {
1342 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001343 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001344 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001345 result.append(buffer);
1346 }
1347 }
1348
Eric Laurentca7cc822012-11-19 14:55:58 -08001349 write(fd, result.string(), result.length());
1350
1351 if (locked) {
1352 mLock.unlock();
1353 }
1354}
1355
1356// ----------------------------------------------------------------------------
1357// EffectHandle implementation
1358// ----------------------------------------------------------------------------
1359
1360#undef LOG_TAG
1361#define LOG_TAG "AudioFlinger::EffectHandle"
1362
1363AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1364 const sp<AudioFlinger::Client>& client,
1365 const sp<IEffectClient>& effectClient,
1366 int32_t priority)
1367 : BnEffect(),
1368 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001369 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001370{
1371 ALOGV("constructor %p", this);
1372
1373 if (client == 0) {
1374 return;
1375 }
1376 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1377 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001378 if (mCblkMemory == 0 ||
1379 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001380 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001381 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001382 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001383 return;
1384 }
Glenn Kastene75da402013-11-20 13:54:52 -08001385 new(mCblk) effect_param_cblk_t();
1386 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001387}
1388
1389AudioFlinger::EffectHandle::~EffectHandle()
1390{
1391 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001392 disconnect(false);
1393}
1394
Glenn Kastene75da402013-11-20 13:54:52 -08001395status_t AudioFlinger::EffectHandle::initCheck()
1396{
1397 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1398}
1399
Eric Laurentca7cc822012-11-19 14:55:58 -08001400status_t AudioFlinger::EffectHandle::enable()
1401{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001402 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001403 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001404 sp<EffectModule> effect = mEffect.promote();
1405 if (effect == 0 || mDisconnected) {
1406 return DEAD_OBJECT;
1407 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001408 if (!mHasControl) {
1409 return INVALID_OPERATION;
1410 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001411
1412 if (mEnabled) {
1413 return NO_ERROR;
1414 }
1415
1416 mEnabled = true;
1417
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001418 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001420 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 }
1422
1423 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001424 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 return NO_ERROR;
1426 }
1427
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001428 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001429 if (status != NO_ERROR) {
1430 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001431 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001432 }
1433 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001434 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001435 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001436 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1437 Mutex::Autolock _l(thread->mLock);
1438 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001439 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001440 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001441 if (thread->type() == ThreadBase::OFFLOAD) {
1442 PlaybackThread *t = (PlaybackThread *)thread.get();
1443 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1444 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001445 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001446 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1447 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001448 }
1449 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001450 }
1451 return status;
1452}
1453
1454status_t AudioFlinger::EffectHandle::disable()
1455{
1456 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001457 AutoMutex _l(mLock);
1458 sp<EffectModule> effect = mEffect.promote();
1459 if (effect == 0 || mDisconnected) {
1460 return DEAD_OBJECT;
1461 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001462 if (!mHasControl) {
1463 return INVALID_OPERATION;
1464 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001465
1466 if (!mEnabled) {
1467 return NO_ERROR;
1468 }
1469 mEnabled = false;
1470
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001471 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001472 return NO_ERROR;
1473 }
1474
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001475 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001476
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001477 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001478 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001479 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001480 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1481 Mutex::Autolock _l(thread->mLock);
1482 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001483 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 }
1485
1486 return status;
1487}
1488
1489void AudioFlinger::EffectHandle::disconnect()
1490{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001491 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001492 disconnect(true);
1493}
1494
1495void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1496{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001497 AutoMutex _l(mLock);
1498 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1499 if (mDisconnected) {
1500 if (unpinIfLast) {
1501 android_errorWriteLog(0x534e4554, "32707507");
1502 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001503 return;
1504 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001505 mDisconnected = true;
1506 sp<ThreadBase> thread;
1507 {
1508 sp<EffectModule> effect = mEffect.promote();
1509 if (effect != 0) {
1510 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001511 }
1512 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001513 if (thread != 0) {
1514 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001515 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001516 // try to cleanup as much as we can
1517 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001518 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1519 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001520 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001521 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001522
Eric Laurentca7cc822012-11-19 14:55:58 -08001523 if (mClient != 0) {
1524 if (mCblk != NULL) {
1525 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1526 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1527 }
1528 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001529 // Client destructor must run with AudioFlinger client mutex locked
1530 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001531 mClient.clear();
1532 }
1533}
1534
1535status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1536 uint32_t cmdSize,
1537 void *pCmdData,
1538 uint32_t *replySize,
1539 void *pReplyData)
1540{
1541 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001542 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001543
Eric Laurentc7ab3092017-06-15 18:43:46 -07001544 // reject commands reserved for internal use by audio framework if coming from outside
1545 // of audioserver
1546 switch(cmdCode) {
1547 case EFFECT_CMD_ENABLE:
1548 case EFFECT_CMD_DISABLE:
1549 case EFFECT_CMD_SET_PARAM:
1550 case EFFECT_CMD_SET_PARAM_DEFERRED:
1551 case EFFECT_CMD_SET_PARAM_COMMIT:
1552 case EFFECT_CMD_GET_PARAM:
1553 break;
1554 default:
1555 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1556 break;
1557 }
1558 android_errorWriteLog(0x534e4554, "62019992");
1559 return BAD_VALUE;
1560 }
1561
Eric Laurent1ffc5852016-12-15 14:46:09 -08001562 if (cmdCode == EFFECT_CMD_ENABLE) {
1563 if (*replySize < sizeof(int)) {
1564 android_errorWriteLog(0x534e4554, "32095713");
1565 return BAD_VALUE;
1566 }
1567 *(int *)pReplyData = NO_ERROR;
1568 *replySize = sizeof(int);
1569 return enable();
1570 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1571 if (*replySize < sizeof(int)) {
1572 android_errorWriteLog(0x534e4554, "32095713");
1573 return BAD_VALUE;
1574 }
1575 *(int *)pReplyData = NO_ERROR;
1576 *replySize = sizeof(int);
1577 return disable();
1578 }
1579
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001580 AutoMutex _l(mLock);
1581 sp<EffectModule> effect = mEffect.promote();
1582 if (effect == 0 || mDisconnected) {
1583 return DEAD_OBJECT;
1584 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001585 // only get parameter command is permitted for applications not controlling the effect
1586 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1587 return INVALID_OPERATION;
1588 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001589 if (mClient == 0) {
1590 return INVALID_OPERATION;
1591 }
1592
1593 // handle commands that are not forwarded transparently to effect engine
1594 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001595 if (*replySize < sizeof(int)) {
1596 android_errorWriteLog(0x534e4554, "32095713");
1597 return BAD_VALUE;
1598 }
1599 *(int *)pReplyData = NO_ERROR;
1600 *replySize = sizeof(int);
1601
Eric Laurentca7cc822012-11-19 14:55:58 -08001602 // No need to trylock() here as this function is executed in the binder thread serving a
1603 // particular client process: no risk to block the whole media server process or mixer
1604 // threads if we are stuck here
1605 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001606 // keep local copy of index in case of client corruption b/32220769
1607 const uint32_t clientIndex = mCblk->clientIndex;
1608 const uint32_t serverIndex = mCblk->serverIndex;
1609 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1610 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001611 mCblk->serverIndex = 0;
1612 mCblk->clientIndex = 0;
1613 return BAD_VALUE;
1614 }
1615 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001616 effect_param_t *param = NULL;
1617 for (uint32_t index = serverIndex; index < clientIndex;) {
1618 int *p = (int *)(mBuffer + index);
1619 const int size = *p++;
1620 if (size < 0
1621 || size > EFFECT_PARAM_BUFFER_SIZE
1622 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001623 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001624 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001625 break;
1626 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001627
1628 // copy to local memory in case of client corruption b/32220769
1629 param = (effect_param_t *)realloc(param, size);
1630 if (param == NULL) {
1631 ALOGW("command(): out of memory");
1632 status = NO_MEMORY;
1633 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001634 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001635 memcpy(param, p, size);
1636
1637 int reply = 0;
1638 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001639 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001640 size,
1641 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001642 &rsize,
1643 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001644
1645 // verify shared memory: server index shouldn't change; client index can't go back.
1646 if (serverIndex != mCblk->serverIndex
1647 || clientIndex > mCblk->clientIndex) {
1648 android_errorWriteLog(0x534e4554, "32220769");
1649 status = BAD_VALUE;
1650 break;
1651 }
1652
Eric Laurentca7cc822012-11-19 14:55:58 -08001653 // stop at first error encountered
1654 if (ret != NO_ERROR) {
1655 status = ret;
1656 *(int *)pReplyData = reply;
1657 break;
1658 } else if (reply != NO_ERROR) {
1659 *(int *)pReplyData = reply;
1660 break;
1661 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001662 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001664 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001665 mCblk->serverIndex = 0;
1666 mCblk->clientIndex = 0;
1667 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001668 }
1669
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001670 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001671}
1672
1673void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1674{
1675 ALOGV("setControl %p control %d", this, hasControl);
1676
1677 mHasControl = hasControl;
1678 mEnabled = enabled;
1679
1680 if (signal && mEffectClient != 0) {
1681 mEffectClient->controlStatusChanged(hasControl);
1682 }
1683}
1684
1685void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1686 uint32_t cmdSize,
1687 void *pCmdData,
1688 uint32_t replySize,
1689 void *pReplyData)
1690{
1691 if (mEffectClient != 0) {
1692 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1693 }
1694}
1695
1696
1697
1698void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1699{
1700 if (mEffectClient != 0) {
1701 mEffectClient->enableStatusChanged(enabled);
1702 }
1703}
1704
1705status_t AudioFlinger::EffectHandle::onTransact(
1706 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1707{
1708 return BnEffect::onTransact(code, data, reply, flags);
1709}
1710
1711
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001712void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001713{
1714 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1715
Marco Nelissenb2208842014-02-07 14:00:50 -08001716 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001717 (mClient == 0) ? getpid_cached : mClient->pid(),
1718 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001719 mHasControl ? "yes" : "no",
1720 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001721 mCblk ? mCblk->clientIndex : 0,
1722 mCblk ? mCblk->serverIndex : 0
1723 );
1724
1725 if (locked) {
1726 mCblk->lock.unlock();
1727 }
1728}
1729
1730#undef LOG_TAG
1731#define LOG_TAG "AudioFlinger::EffectChain"
1732
1733AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001734 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001735 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001736 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001737 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001738{
1739 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1740 if (thread == NULL) {
1741 return;
1742 }
1743 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1744 thread->frameCount();
1745}
1746
1747AudioFlinger::EffectChain::~EffectChain()
1748{
Eric Laurentca7cc822012-11-19 14:55:58 -08001749}
1750
1751// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1752sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1753 effect_descriptor_t *descriptor)
1754{
1755 size_t size = mEffects.size();
1756
1757 for (size_t i = 0; i < size; i++) {
1758 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1759 return mEffects[i];
1760 }
1761 }
1762 return 0;
1763}
1764
1765// getEffectFromId_l() must be called with ThreadBase::mLock held
1766sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1767{
1768 size_t size = mEffects.size();
1769
1770 for (size_t i = 0; i < size; i++) {
1771 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1772 if (id == 0 || mEffects[i]->id() == id) {
1773 return mEffects[i];
1774 }
1775 }
1776 return 0;
1777}
1778
1779// getEffectFromType_l() must be called with ThreadBase::mLock held
1780sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1781 const effect_uuid_t *type)
1782{
1783 size_t size = mEffects.size();
1784
1785 for (size_t i = 0; i < size; i++) {
1786 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1787 return mEffects[i];
1788 }
1789 }
1790 return 0;
1791}
1792
1793void AudioFlinger::EffectChain::clearInputBuffer()
1794{
1795 Mutex::Autolock _l(mLock);
1796 sp<ThreadBase> thread = mThread.promote();
1797 if (thread == 0) {
1798 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1799 return;
1800 }
1801 clearInputBuffer_l(thread);
1802}
1803
1804// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001805void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001806{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001807 if (mInBuffer == NULL) {
1808 return;
1809 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001810 // TODO: This will change in the future, depending on multichannel
1811 // and sample format changes for effects.
1812 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1813 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001814
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001815 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001816 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1817 * std::min((uint32_t)FCC_2, thread->channelCount());
1818
Mikhail Naganov022b9952017-01-04 16:36:51 -08001819 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1820 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001821}
1822
1823// Must be called with EffectChain::mLock locked
1824void AudioFlinger::EffectChain::process_l()
1825{
1826 sp<ThreadBase> thread = mThread.promote();
1827 if (thread == 0) {
1828 ALOGW("process_l(): cannot promote mixer thread");
1829 return;
1830 }
1831 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1832 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001833 // never process effects when:
1834 // - on an OFFLOAD thread
1835 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001836 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1837 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001838 if (!isGlobalSession) {
1839 bool tracksOnSession = (trackCnt() != 0);
1840
1841 if (!tracksOnSession && mTailBufferCount == 0) {
1842 doProcess = false;
1843 }
1844
1845 if (activeTrackCnt() == 0) {
1846 // if no track is active and the effect tail has not been rendered,
1847 // the input buffer must be cleared here as the mixer process will not do it
1848 if (tracksOnSession || mTailBufferCount > 0) {
1849 clearInputBuffer_l(thread);
1850 if (mTailBufferCount > 0) {
1851 mTailBufferCount--;
1852 }
1853 }
1854 }
1855 }
1856
1857 size_t size = mEffects.size();
1858 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001859 // Only the input and output buffers of the chain can be external,
1860 // and 'update' / 'commit' do nothing for allocated buffers, thus
1861 // it's not needed to consider any other buffers here.
1862 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001863 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1864 mOutBuffer->update();
1865 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001866 for (size_t i = 0; i < size; i++) {
1867 mEffects[i]->process();
1868 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001869 mInBuffer->commit();
1870 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1871 mOutBuffer->commit();
1872 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001873 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001874 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001875 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001876 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1877 }
1878 if (doResetVolume) {
1879 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001880 }
1881}
1882
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001883// createEffect_l() must be called with ThreadBase::mLock held
1884status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1885 ThreadBase *thread,
1886 effect_descriptor_t *desc,
1887 int id,
1888 audio_session_t sessionId,
1889 bool pinned)
1890{
1891 Mutex::Autolock _l(mLock);
1892 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1893 status_t lStatus = effect->status();
1894 if (lStatus == NO_ERROR) {
1895 lStatus = addEffect_ll(effect);
1896 }
1897 if (lStatus != NO_ERROR) {
1898 effect.clear();
1899 }
1900 return lStatus;
1901}
1902
1903// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001904status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1905{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001906 Mutex::Autolock _l(mLock);
1907 return addEffect_ll(effect);
1908}
1909// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1910status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1911{
Eric Laurentca7cc822012-11-19 14:55:58 -08001912 effect_descriptor_t desc = effect->desc();
1913 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1914
Eric Laurentca7cc822012-11-19 14:55:58 -08001915 effect->setChain(this);
1916 sp<ThreadBase> thread = mThread.promote();
1917 if (thread == 0) {
1918 return NO_INIT;
1919 }
1920 effect->setThread(thread);
1921
1922 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1923 // Auxiliary effects are inserted at the beginning of mEffects vector as
1924 // they are processed first and accumulated in chain input buffer
1925 mEffects.insertAt(effect, 0);
1926
1927 // the input buffer for auxiliary effect contains mono samples in
1928 // 32 bit format. This is to avoid saturation in AudoMixer
1929 // accumulation stage. Saturation is done in EffectModule::process() before
1930 // calling the process in effect engine
1931 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001932 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001933#ifdef FLOAT_EFFECT_CHAIN
1934 status_t result = EffectBufferHalInterface::allocate(
1935 numSamples * sizeof(float), &halBuffer);
1936#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001937 status_t result = EffectBufferHalInterface::allocate(
1938 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001939#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001940 if (result != OK) return result;
1941 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001942 // auxiliary effects output samples to chain input buffer for further processing
1943 // by insert effects
1944 effect->setOutBuffer(mInBuffer);
1945 } else {
1946 // Insert effects are inserted at the end of mEffects vector as they are processed
1947 // after track and auxiliary effects.
1948 // Insert effect order as a function of indicated preference:
1949 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1950 // another effect is present
1951 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1952 // last effect claiming first position
1953 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1954 // first effect claiming last position
1955 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1956 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1957 // already present
1958
1959 size_t size = mEffects.size();
1960 size_t idx_insert = size;
1961 ssize_t idx_insert_first = -1;
1962 ssize_t idx_insert_last = -1;
1963
1964 for (size_t i = 0; i < size; i++) {
1965 effect_descriptor_t d = mEffects[i]->desc();
1966 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1967 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1968 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1969 // check invalid effect chaining combinations
1970 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1971 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1972 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1973 desc.name, d.name);
1974 return INVALID_OPERATION;
1975 }
1976 // remember position of first insert effect and by default
1977 // select this as insert position for new effect
1978 if (idx_insert == size) {
1979 idx_insert = i;
1980 }
1981 // remember position of last insert effect claiming
1982 // first position
1983 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1984 idx_insert_first = i;
1985 }
1986 // remember position of first insert effect claiming
1987 // last position
1988 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1989 idx_insert_last == -1) {
1990 idx_insert_last = i;
1991 }
1992 }
1993 }
1994
1995 // modify idx_insert from first position if needed
1996 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1997 if (idx_insert_last != -1) {
1998 idx_insert = idx_insert_last;
1999 } else {
2000 idx_insert = size;
2001 }
2002 } else {
2003 if (idx_insert_first != -1) {
2004 idx_insert = idx_insert_first + 1;
2005 }
2006 }
2007
2008 // always read samples from chain input buffer
2009 effect->setInBuffer(mInBuffer);
2010
2011 // if last effect in the chain, output samples to chain
2012 // output buffer, otherwise to chain input buffer
2013 if (idx_insert == size) {
2014 if (idx_insert != 0) {
2015 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2016 mEffects[idx_insert-1]->configure();
2017 }
2018 effect->setOutBuffer(mOutBuffer);
2019 } else {
2020 effect->setOutBuffer(mInBuffer);
2021 }
2022 mEffects.insertAt(effect, idx_insert);
2023
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002024 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002025 idx_insert);
2026 }
2027 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002028
Eric Laurentca7cc822012-11-19 14:55:58 -08002029 return NO_ERROR;
2030}
2031
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002032// removeEffect_l() must be called with ThreadBase::mLock held
2033size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2034 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002035{
2036 Mutex::Autolock _l(mLock);
2037 size_t size = mEffects.size();
2038 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2039
2040 for (size_t i = 0; i < size; i++) {
2041 if (effect == mEffects[i]) {
2042 // calling stop here will remove pre-processing effect from the audio HAL.
2043 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2044 // the middle of a read from audio HAL
2045 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2046 mEffects[i]->state() == EffectModule::STOPPING) {
2047 mEffects[i]->stop();
2048 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002049 if (release) {
2050 mEffects[i]->release_l();
2051 }
2052
Mikhail Naganov022b9952017-01-04 16:36:51 -08002053 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 if (i == size - 1 && i != 0) {
2055 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2056 mEffects[i - 1]->configure();
2057 }
2058 }
2059 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002060 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002061 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002062
Eric Laurentca7cc822012-11-19 14:55:58 -08002063 break;
2064 }
2065 }
2066
2067 return mEffects.size();
2068}
2069
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002070// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002071void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2072{
2073 size_t size = mEffects.size();
2074 for (size_t i = 0; i < size; i++) {
2075 mEffects[i]->setDevice(device);
2076 }
2077}
2078
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002079// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002080void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2081{
2082 size_t size = mEffects.size();
2083 for (size_t i = 0; i < size; i++) {
2084 mEffects[i]->setMode(mode);
2085 }
2086}
2087
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002088// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002089void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2090{
2091 size_t size = mEffects.size();
2092 for (size_t i = 0; i < size; i++) {
2093 mEffects[i]->setAudioSource(source);
2094 }
2095}
2096
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002097// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002098bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002099{
2100 uint32_t newLeft = *left;
2101 uint32_t newRight = *right;
2102 bool hasControl = false;
2103 int ctrlIdx = -1;
2104 size_t size = mEffects.size();
2105
2106 // first update volume controller
2107 for (size_t i = size; i > 0; i--) {
2108 if (mEffects[i - 1]->isProcessEnabled() &&
2109 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2110 ctrlIdx = i - 1;
2111 hasControl = true;
2112 break;
2113 }
2114 }
2115
Eric Laurentfa1e1232016-08-02 19:01:49 -07002116 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002117 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002118 if (hasControl) {
2119 *left = mNewLeftVolume;
2120 *right = mNewRightVolume;
2121 }
2122 return hasControl;
2123 }
2124
2125 mVolumeCtrlIdx = ctrlIdx;
2126 mLeftVolume = newLeft;
2127 mRightVolume = newRight;
2128
2129 // second get volume update from volume controller
2130 if (ctrlIdx >= 0) {
2131 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2132 mNewLeftVolume = newLeft;
2133 mNewRightVolume = newRight;
2134 }
2135 // then indicate volume to all other effects in chain.
2136 // Pass altered volume to effects before volume controller
2137 // and requested volume to effects after controller
2138 uint32_t lVol = newLeft;
2139 uint32_t rVol = newRight;
2140
2141 for (size_t i = 0; i < size; i++) {
2142 if ((int)i == ctrlIdx) {
2143 continue;
2144 }
2145 // this also works for ctrlIdx == -1 when there is no volume controller
2146 if ((int)i > ctrlIdx) {
2147 lVol = *left;
2148 rVol = *right;
2149 }
2150 mEffects[i]->setVolume(&lVol, &rVol, false);
2151 }
2152 *left = newLeft;
2153 *right = newRight;
2154
2155 return hasControl;
2156}
2157
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002158// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002159void AudioFlinger::EffectChain::resetVolume_l()
2160{
Eric Laurente7449bf2016-08-03 18:44:07 -07002161 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2162 uint32_t left = mLeftVolume;
2163 uint32_t right = mRightVolume;
2164 (void)setVolume_l(&left, &right, true);
2165 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002166}
2167
Eric Laurent1b928682014-10-02 19:41:47 -07002168void AudioFlinger::EffectChain::syncHalEffectsState()
2169{
2170 Mutex::Autolock _l(mLock);
2171 for (size_t i = 0; i < mEffects.size(); i++) {
2172 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2173 mEffects[i]->state() == EffectModule::STOPPING) {
2174 mEffects[i]->addEffectToHal_l();
2175 }
2176 }
2177}
2178
Eric Laurentca7cc822012-11-19 14:55:58 -08002179void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2180{
2181 const size_t SIZE = 256;
2182 char buffer[SIZE];
2183 String8 result;
2184
Marco Nelissenb2208842014-02-07 14:00:50 -08002185 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002186 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002187 result.append(buffer);
2188
Marco Nelissenb2208842014-02-07 14:00:50 -08002189 if (numEffects) {
2190 bool locked = AudioFlinger::dumpTryLock(mLock);
2191 // failed to lock - AudioFlinger is probably deadlocked
2192 if (!locked) {
2193 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002194 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002195
Andy Hungbded9c82017-11-30 18:47:35 -08002196 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2197 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2198 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2199 (int)inBufferStr.size(), "In buffer ",
2200 (int)outBufferStr.size(), "Out buffer ");
2201 result.appendFormat("\t%s %s %d\n",
2202 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002203 write(fd, result.string(), result.size());
2204
2205 for (size_t i = 0; i < numEffects; ++i) {
2206 sp<EffectModule> effect = mEffects[i];
2207 if (effect != 0) {
2208 effect->dump(fd, args);
2209 }
2210 }
2211
2212 if (locked) {
2213 mLock.unlock();
2214 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002215 }
2216}
2217
2218// must be called with ThreadBase::mLock held
2219void AudioFlinger::EffectChain::setEffectSuspended_l(
2220 const effect_uuid_t *type, bool suspend)
2221{
2222 sp<SuspendedEffectDesc> desc;
2223 // use effect type UUID timelow as key as there is no real risk of identical
2224 // timeLow fields among effect type UUIDs.
2225 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2226 if (suspend) {
2227 if (index >= 0) {
2228 desc = mSuspendedEffects.valueAt(index);
2229 } else {
2230 desc = new SuspendedEffectDesc();
2231 desc->mType = *type;
2232 mSuspendedEffects.add(type->timeLow, desc);
2233 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2234 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002235
Eric Laurentca7cc822012-11-19 14:55:58 -08002236 if (desc->mRefCount++ == 0) {
2237 sp<EffectModule> effect = getEffectIfEnabled(type);
2238 if (effect != 0) {
2239 desc->mEffect = effect;
2240 effect->setSuspended(true);
2241 effect->setEnabled(false);
2242 }
2243 }
2244 } else {
2245 if (index < 0) {
2246 return;
2247 }
2248 desc = mSuspendedEffects.valueAt(index);
2249 if (desc->mRefCount <= 0) {
2250 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002251 desc->mRefCount = 0;
2252 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002253 }
2254 if (--desc->mRefCount == 0) {
2255 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2256 if (desc->mEffect != 0) {
2257 sp<EffectModule> effect = desc->mEffect.promote();
2258 if (effect != 0) {
2259 effect->setSuspended(false);
2260 effect->lock();
2261 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002262 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002263 effect->setEnabled_l(handle->enabled());
2264 }
2265 effect->unlock();
2266 }
2267 desc->mEffect.clear();
2268 }
2269 mSuspendedEffects.removeItemsAt(index);
2270 }
2271 }
2272}
2273
2274// must be called with ThreadBase::mLock held
2275void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2276{
2277 sp<SuspendedEffectDesc> desc;
2278
2279 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2280 if (suspend) {
2281 if (index >= 0) {
2282 desc = mSuspendedEffects.valueAt(index);
2283 } else {
2284 desc = new SuspendedEffectDesc();
2285 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2286 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2287 }
2288 if (desc->mRefCount++ == 0) {
2289 Vector< sp<EffectModule> > effects;
2290 getSuspendEligibleEffects(effects);
2291 for (size_t i = 0; i < effects.size(); i++) {
2292 setEffectSuspended_l(&effects[i]->desc().type, true);
2293 }
2294 }
2295 } else {
2296 if (index < 0) {
2297 return;
2298 }
2299 desc = mSuspendedEffects.valueAt(index);
2300 if (desc->mRefCount <= 0) {
2301 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2302 desc->mRefCount = 1;
2303 }
2304 if (--desc->mRefCount == 0) {
2305 Vector<const effect_uuid_t *> types;
2306 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2307 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2308 continue;
2309 }
2310 types.add(&mSuspendedEffects.valueAt(i)->mType);
2311 }
2312 for (size_t i = 0; i < types.size(); i++) {
2313 setEffectSuspended_l(types[i], false);
2314 }
2315 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2316 mSuspendedEffects.keyAt(index));
2317 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2318 }
2319 }
2320}
2321
2322
2323// The volume effect is used for automated tests only
2324#ifndef OPENSL_ES_H_
2325static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2326 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2327const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2328#endif //OPENSL_ES_H_
2329
Eric Laurentd8365c52017-07-16 15:27:05 -07002330/* static */
2331bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2332{
2333 // Only NS and AEC are suspended when BtNRec is off
2334 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2335 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2336 return true;
2337 }
2338 return false;
2339}
2340
Eric Laurentca7cc822012-11-19 14:55:58 -08002341bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2342{
2343 // auxiliary effects and visualizer are never suspended on output mix
2344 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2345 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2346 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2347 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2348 return false;
2349 }
2350 return true;
2351}
2352
2353void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2354 Vector< sp<AudioFlinger::EffectModule> > &effects)
2355{
2356 effects.clear();
2357 for (size_t i = 0; i < mEffects.size(); i++) {
2358 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2359 effects.add(mEffects[i]);
2360 }
2361 }
2362}
2363
2364sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2365 const effect_uuid_t *type)
2366{
2367 sp<EffectModule> effect = getEffectFromType_l(type);
2368 return effect != 0 && effect->isEnabled() ? effect : 0;
2369}
2370
2371void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2372 bool enabled)
2373{
2374 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2375 if (enabled) {
2376 if (index < 0) {
2377 // if the effect is not suspend check if all effects are suspended
2378 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2379 if (index < 0) {
2380 return;
2381 }
2382 if (!isEffectEligibleForSuspend(effect->desc())) {
2383 return;
2384 }
2385 setEffectSuspended_l(&effect->desc().type, enabled);
2386 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2387 if (index < 0) {
2388 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2389 return;
2390 }
2391 }
2392 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2393 effect->desc().type.timeLow);
2394 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002395 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002396 if (desc->mEffect == 0) {
2397 desc->mEffect = effect;
2398 effect->setEnabled(false);
2399 effect->setSuspended(true);
2400 }
2401 } else {
2402 if (index < 0) {
2403 return;
2404 }
2405 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2406 effect->desc().type.timeLow);
2407 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2408 desc->mEffect.clear();
2409 effect->setSuspended(false);
2410 }
2411}
2412
Eric Laurent5baf2af2013-09-12 17:37:00 -07002413bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002414{
2415 Mutex::Autolock _l(mLock);
2416 size_t size = mEffects.size();
2417 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002418 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002419 return true;
2420 }
2421 }
2422 return false;
2423}
2424
Eric Laurentaaa44472014-09-12 17:41:50 -07002425void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2426{
2427 Mutex::Autolock _l(mLock);
2428 mThread = thread;
2429 for (size_t i = 0; i < mEffects.size(); i++) {
2430 mEffects[i]->setThread(thread);
2431 }
2432}
2433
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002434void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2435{
2436 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2437 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2438 }
2439 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2440 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2441 }
2442}
2443
2444void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2445{
2446 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2447 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2448 }
2449 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2450 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2451 }
2452}
2453
2454bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002455{
2456 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002457 for (const auto &effect : mEffects) {
2458 if (effect->isProcessImplemented()) {
2459 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002460 }
2461 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002462 // Allow effects without processing.
2463 return true;
2464}
2465
2466bool AudioFlinger::EffectChain::isFastCompatible() const
2467{
2468 Mutex::Autolock _l(mLock);
2469 for (const auto &effect : mEffects) {
2470 if (effect->isProcessImplemented()
2471 && effect->isImplementationSoftware()) {
2472 return false;
2473 }
2474 }
2475 // Allow effects without processing or hw accelerated effects.
2476 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002477}
2478
2479// isCompatibleWithThread_l() must be called with thread->mLock held
2480bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2481{
2482 Mutex::Autolock _l(mLock);
2483 for (size_t i = 0; i < mEffects.size(); i++) {
2484 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2485 return false;
2486 }
2487 }
2488 return true;
2489}
2490
Glenn Kasten63238ef2015-03-02 15:50:29 -08002491} // namespace android