blob: cd3c588399718083ffe98c2903199dd38311c173 [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 Hungab305162017-12-14 12:42:22 -080070 // clear mConfig to ensure consistent initial value of buffer framecount
71 // in case buffers are associated by setInBuffer() or setOutBuffer()
72 // prior to configure().
73 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080074 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080075 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
76 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070077 mSuspended(false),
Andy Hung62aef7d2017-12-14 14:50:40 -080078 mOffloaded(false),
Eric Laurentaaa44472014-09-12 17:41:50 -070079 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070080#ifdef FLOAT_EFFECT_CHAIN
81 , mSupportsFloat(false)
82#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080083{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080084 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080085 int lStatus;
86
87 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070088 mStatus = -ENODEV;
89 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070090 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070091 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070092 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070093 mStatus = effectsFactory->createEffect(
94 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
95 }
96 }
Eric Laurentca7cc822012-11-19 14:55:58 -080097
98 if (mStatus != NO_ERROR) {
99 return;
100 }
101 lStatus = init();
102 if (lStatus < 0) {
103 mStatus = lStatus;
104 goto Error;
105 }
106
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800107 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700108 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800109
Eric Laurentca7cc822012-11-19 14:55:58 -0800110 return;
111Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700112 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800113 ALOGV("Constructor Error %d", mStatus);
114}
115
116AudioFlinger::EffectModule::~EffectModule()
117{
118 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700119 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700120 char uuidStr[64];
121 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
122 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
123 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800124 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800125 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800126
Eric Laurentca7cc822012-11-19 14:55:58 -0800127}
128
129status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
130{
131 status_t status;
132
133 Mutex::Autolock _l(mLock);
134 int priority = handle->priority();
135 size_t size = mHandles.size();
136 EffectHandle *controlHandle = NULL;
137 size_t i;
138 for (i = 0; i < size; i++) {
139 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800140 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800141 continue;
142 }
143 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700144 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800145 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700146 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800147 if (h->priority() <= priority) {
148 break;
149 }
150 }
151 // if inserted in first place, move effect control from previous owner to this handle
152 if (i == 0) {
153 bool enabled = false;
154 if (controlHandle != NULL) {
155 enabled = controlHandle->enabled();
156 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
157 }
158 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
159 status = NO_ERROR;
160 } else {
161 status = ALREADY_EXISTS;
162 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700163 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800164 mHandles.insertAt(handle, i);
165 return status;
166}
167
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800168ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800169{
170 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800171 return removeHandle_l(handle);
172}
173
174ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
175{
Eric Laurentca7cc822012-11-19 14:55:58 -0800176 size_t size = mHandles.size();
177 size_t i;
178 for (i = 0; i < size; i++) {
179 if (mHandles[i] == handle) {
180 break;
181 }
182 }
183 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800184 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
185 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800186 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800187 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800188
189 mHandles.removeAt(i);
190 // if removed from first place, move effect control from this handle to next in line
191 if (i == 0) {
192 EffectHandle *h = controlHandle_l();
193 if (h != NULL) {
194 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
195 }
196 }
197
198 // Prevent calls to process() and other functions on effect interface from now on.
199 // The effect engine will be released by the destructor when the last strong reference on
200 // this object is released which can happen after next process is called.
201 if (mHandles.size() == 0 && !mPinned) {
202 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800203 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800204 }
205
206 return mHandles.size();
207}
208
209// must be called with EffectModule::mLock held
210AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
211{
212 // the first valid handle in the list has control over the module
213 for (size_t i = 0; i < mHandles.size(); i++) {
214 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800215 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800216 return h;
217 }
218 }
219
220 return NULL;
221}
222
Eric Laurentf10c7092016-12-06 17:09:56 -0800223// unsafe method called when the effect parent thread has been destroyed
224ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
225{
226 ALOGV("disconnect() %p handle %p", this, handle);
227 Mutex::Autolock _l(mLock);
228 ssize_t numHandles = removeHandle_l(handle);
229 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
230 AudioSystem::unregisterEffect(mId);
231 sp<AudioFlinger> af = mAudioFlinger.promote();
232 if (af != 0) {
233 mLock.unlock();
234 af->updateOrphanEffectChains(this);
235 mLock.lock();
236 }
237 }
238 return numHandles;
239}
240
Eric Laurentfa1e1232016-08-02 19:01:49 -0700241bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800242 Mutex::Autolock _l(mLock);
243
Eric Laurentfa1e1232016-08-02 19:01:49 -0700244 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800245 switch (mState) {
246 case RESTART:
247 reset_l();
248 // FALL THROUGH
249
250 case STARTING:
251 // clear auxiliary effect input buffer for next accumulation
252 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
253 memset(mConfig.inputCfg.buffer.raw,
254 0,
255 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
256 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700257 if (start_l() == NO_ERROR) {
258 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700259 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700260 } else {
261 mState = IDLE;
262 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800263 break;
264 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700265 if (stop_l() == NO_ERROR) {
266 mDisableWaitCnt = mMaxDisableWaitCnt;
267 } else {
268 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
269 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800270 mState = STOPPED;
271 break;
272 case STOPPED:
273 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
274 // turn off sequence.
275 if (--mDisableWaitCnt == 0) {
276 reset_l();
277 mState = IDLE;
278 }
279 break;
280 default: //IDLE , ACTIVE, DESTROYED
281 break;
282 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700283
284 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800285}
286
287void AudioFlinger::EffectModule::process()
288{
289 Mutex::Autolock _l(mLock);
290
Mikhail Naganov022b9952017-01-04 16:36:51 -0800291 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800292 return;
293 }
294
rago94a1ee82017-07-21 15:11:02 -0700295 // TODO: Implement multichannel effects; here outChannelCount == FCC_2 == 2
296 const uint32_t inChannelCount =
297 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
298 const uint32_t outChannelCount =
299 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
300 const bool auxType =
301 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
302
Andy Hungfa69ca32017-11-30 10:07:53 -0800303 // safeInputOutputSampleCount is 0 if the channel count between input and output
304 // buffers do not match. This prevents automatic accumulation or copying between the
305 // input and output effect buffers without an intermediary effect process.
306 // TODO: consider implementing channel conversion.
307 const size_t safeInputOutputSampleCount =
308 inChannelCount != outChannelCount ? 0
309 : outChannelCount * std::min(
310 mConfig.inputCfg.buffer.frameCount,
311 mConfig.outputCfg.buffer.frameCount);
312 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
313#ifdef FLOAT_EFFECT_CHAIN
314 accumulate_float(
315 mConfig.outputCfg.buffer.f32,
316 mConfig.inputCfg.buffer.f32,
317 safeInputOutputSampleCount);
318#else
319 accumulate_i16(
320 mConfig.outputCfg.buffer.s16,
321 mConfig.inputCfg.buffer.s16,
322 safeInputOutputSampleCount);
323#endif
324 };
325 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
326#ifdef FLOAT_EFFECT_CHAIN
327 memcpy(
328 mConfig.outputCfg.buffer.f32,
329 mConfig.inputCfg.buffer.f32,
330 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
331
332#else
333 memcpy(
334 mConfig.outputCfg.buffer.s16,
335 mConfig.inputCfg.buffer.s16,
336 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
337#endif
338 };
339
Eric Laurentca7cc822012-11-19 14:55:58 -0800340 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700341 int ret;
342 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700343 if (auxType) {
344 // We overwrite the aux input buffer here and clear after processing.
345 // Note that aux input buffers are format q4_27.
346#ifdef FLOAT_EFFECT_CHAIN
347 if (mSupportsFloat) {
348 // Do in-place float conversion for auxiliary effect input buffer.
349 static_assert(sizeof(float) <= sizeof(int32_t),
350 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
351
Andy Hungfa69ca32017-11-30 10:07:53 -0800352 memcpy_to_float_from_q4_27(
353 mConfig.inputCfg.buffer.f32,
354 mConfig.inputCfg.buffer.s32,
355 mConfig.inputCfg.buffer.frameCount);
356 } else
357#endif
358 {
359 memcpy_to_i16_from_q4_27(
360 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700361 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800362 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700363 }
rago94a1ee82017-07-21 15:11:02 -0700364 }
365#ifdef FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800366 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
367 if (!auxType) {
368 if (mInConversionBuffer.get() == nullptr) {
369 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
370 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700371 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800372 memcpy_to_i16_from_float(
373 mInConversionBuffer->audioBuffer()->s16,
374 mInBuffer->audioBuffer()->f32,
375 inChannelCount * mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700376 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800377 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
378 if (mOutConversionBuffer.get() == nullptr) {
379 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
380 goto data_bypass;
381 }
382 memcpy_to_i16_from_float(
383 mOutConversionBuffer->audioBuffer()->s16,
384 mOutBuffer->audioBuffer()->f32,
385 outChannelCount * mConfig.outputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700386 }
387 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800388#endif
389
Mikhail Naganov022b9952017-01-04 16:36:51 -0800390 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800391
392#ifdef FLOAT_EFFECT_CHAIN
393 if (!mSupportsFloat) { // convert output int16_t back to float.
394 memcpy_to_float_from_i16(
395 mOutBuffer->audioBuffer()->f32,
396 mOutConversionBuffer->audioBuffer()->s16,
397 outChannelCount * mConfig.outputCfg.buffer.frameCount);
398 }
rago94a1ee82017-07-21 15:11:02 -0700399#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700400 } else {
rago94a1ee82017-07-21 15:11:02 -0700401#ifdef FLOAT_EFFECT_CHAIN
402 data_bypass:
403#endif
404 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800405 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700406 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800407 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700408 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800409 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700410 }
411 }
412 ret = -ENODATA;
413 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800414
Eric Laurentca7cc822012-11-19 14:55:58 -0800415 // force transition to IDLE state when engine is ready
416 if (mState == STOPPED && ret == -ENODATA) {
417 mDisableWaitCnt = 1;
418 }
419
420 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700421 if (auxType) {
422 // input always q4_27 regardless of FLOAT_EFFECT_CHAIN.
423 const size_t size =
424 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
425 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800426 }
427 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700428 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800429 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
430 // If an insert effect is idle and input buffer is different from output buffer,
431 // accumulate input onto output
432 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800433 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
434 accumulateInputToOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800435 }
436 }
437}
438
439void AudioFlinger::EffectModule::reset_l()
440{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700441 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800442 return;
443 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700444 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800445}
446
447status_t AudioFlinger::EffectModule::configure()
448{
rago94a1ee82017-07-21 15:11:02 -0700449 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700450 status_t status;
451 sp<ThreadBase> thread;
452 uint32_t size;
453 audio_channel_mask_t channelMask;
454
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700455 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700456 status = NO_INIT;
457 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800458 }
459
Eric Laurentd0ebb532013-04-02 16:41:41 -0700460 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800461 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700462 status = DEAD_OBJECT;
463 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800464 }
465
466 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700467 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700468 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800469
470 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
471 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900472 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
473 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800474 } else {
475 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700476 // TODO: Update this logic when multichannel effects are implemented.
477 // For offloaded tracks consider mono output as stereo for proper effect initialization
478 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
479 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
480 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
481 ALOGV("Overriding effect input and output as STEREO");
482 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800483 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700484
rago94a1ee82017-07-21 15:11:02 -0700485 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
486 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800487 mConfig.inputCfg.samplingRate = thread->sampleRate();
488 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
489 mConfig.inputCfg.bufferProvider.cookie = NULL;
490 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
491 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
492 mConfig.outputCfg.bufferProvider.cookie = NULL;
493 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
494 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
495 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
496 // Insert effect:
497 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
498 // always overwrites output buffer: input buffer == output buffer
499 // - in other sessions:
500 // last effect in the chain accumulates in output buffer: input buffer != output buffer
501 // other effect: overwrites output buffer: input buffer == output buffer
502 // Auxiliary effect:
503 // accumulates in output buffer: input buffer != output buffer
504 // Therefore: accumulate <=> input buffer != output buffer
505 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
506 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
507 } else {
508 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
509 }
510 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
511 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
512 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
513 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
514
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700515 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800516 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
517
518 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700519 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700520 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
521 sizeof(effect_config_t),
522 &mConfig,
523 &size,
524 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700525 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800526 status = cmdStatus;
rago94a1ee82017-07-21 15:11:02 -0700527#ifdef FLOAT_EFFECT_CHAIN
528 mSupportsFloat = true;
529#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800530 }
rago94a1ee82017-07-21 15:11:02 -0700531#ifdef FLOAT_EFFECT_CHAIN
532 else {
533 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
534 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
535 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
536 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
537 sizeof(effect_config_t),
538 &mConfig,
539 &size,
540 &cmdStatus);
541 if (status == NO_ERROR) {
542 status = cmdStatus;
543 mSupportsFloat = false;
544 ALOGVV("config worked with 16 bit");
545 } else {
546 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800547 }
rago94a1ee82017-07-21 15:11:02 -0700548 }
549#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800550
rago94a1ee82017-07-21 15:11:02 -0700551 if (status == NO_ERROR) {
552 // Establish Buffer strategy
553 setInBuffer(mInBuffer);
554 setOutBuffer(mOutBuffer);
555
556 // Update visualizer latency
557 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
558 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
559 effect_param_t *p = (effect_param_t *)buf32;
560
561 p->psize = sizeof(uint32_t);
562 p->vsize = sizeof(uint32_t);
563 size = sizeof(int);
564 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
565
566 uint32_t latency = 0;
567 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
568 if (pbt != NULL) {
569 latency = pbt->latency_l();
570 }
571
572 *((int32_t *)p->data + 1)= latency;
573 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
574 sizeof(effect_param_t) + 8,
575 &buf32,
576 &size,
577 &cmdStatus);
578 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 }
580
581 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
582 (1000 * mConfig.outputCfg.buffer.frameCount);
583
Eric Laurentd0ebb532013-04-02 16:41:41 -0700584exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800585 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700586 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700587 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800588 return status;
589}
590
591status_t AudioFlinger::EffectModule::init()
592{
593 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700594 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800595 return NO_INIT;
596 }
597 status_t cmdStatus;
598 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700599 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
600 0,
601 NULL,
602 &size,
603 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800604 if (status == 0) {
605 status = cmdStatus;
606 }
607 return status;
608}
609
Eric Laurent1b928682014-10-02 19:41:47 -0700610void AudioFlinger::EffectModule::addEffectToHal_l()
611{
612 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
613 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
614 sp<ThreadBase> thread = mThread.promote();
615 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700616 sp<StreamHalInterface> stream = thread->stream();
617 if (stream != 0) {
618 status_t result = stream->addEffect(mEffectInterface);
619 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700620 }
621 }
622 }
623}
624
Eric Laurentfa1e1232016-08-02 19:01:49 -0700625// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800626status_t AudioFlinger::EffectModule::start()
627{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700628 sp<EffectChain> chain;
629 status_t status;
630 {
631 Mutex::Autolock _l(mLock);
632 status = start_l();
633 if (status == NO_ERROR) {
634 chain = mChain.promote();
635 }
636 }
637 if (chain != 0) {
638 chain->resetVolume_l();
639 }
640 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800641}
642
643status_t AudioFlinger::EffectModule::start_l()
644{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700645 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800646 return NO_INIT;
647 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700648 if (mStatus != NO_ERROR) {
649 return mStatus;
650 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800651 status_t cmdStatus;
652 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700653 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
654 0,
655 NULL,
656 &size,
657 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800658 if (status == 0) {
659 status = cmdStatus;
660 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700661 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700662 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800663 }
664 return status;
665}
666
667status_t AudioFlinger::EffectModule::stop()
668{
669 Mutex::Autolock _l(mLock);
670 return stop_l();
671}
672
673status_t AudioFlinger::EffectModule::stop_l()
674{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700675 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800676 return NO_INIT;
677 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700678 if (mStatus != NO_ERROR) {
679 return mStatus;
680 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800681 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800682 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700683 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
684 0,
685 NULL,
686 &size,
687 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800688 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800689 status = cmdStatus;
690 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800691 if (status == NO_ERROR) {
692 status = remove_effect_from_hal_l();
693 }
694 return status;
695}
696
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800697// must be called with EffectChain::mLock held
698void AudioFlinger::EffectModule::release_l()
699{
700 if (mEffectInterface != 0) {
701 remove_effect_from_hal_l();
702 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800703 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800704 mEffectInterface.clear();
705 }
706}
707
Eric Laurentbfb1b832013-01-07 09:53:42 -0800708status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
709{
710 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
711 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800712 sp<ThreadBase> thread = mThread.promote();
713 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700714 sp<StreamHalInterface> stream = thread->stream();
715 if (stream != 0) {
716 status_t result = stream->removeEffect(mEffectInterface);
717 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800718 }
719 }
720 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800721 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800722}
723
Andy Hunge4a1d912016-08-17 14:11:13 -0700724// round up delta valid if value and divisor are positive.
725template <typename T>
726static T roundUpDelta(const T &value, const T &divisor) {
727 T remainder = value % divisor;
728 return remainder == 0 ? 0 : divisor - remainder;
729}
730
Eric Laurentca7cc822012-11-19 14:55:58 -0800731status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
732 uint32_t cmdSize,
733 void *pCmdData,
734 uint32_t *replySize,
735 void *pReplyData)
736{
737 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700738 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800739
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700740 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800741 return NO_INIT;
742 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700743 if (mStatus != NO_ERROR) {
744 return mStatus;
745 }
Andy Hung110bc952016-06-20 15:22:52 -0700746 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700747 (sizeof(effect_param_t) > cmdSize ||
748 ((effect_param_t *)pCmdData)->psize > cmdSize
749 - sizeof(effect_param_t))) {
750 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800751 android_errorWriteLog(0x534e4554, "33003822");
752 return -EINVAL;
753 }
754 if (cmdCode == EFFECT_CMD_GET_PARAM &&
755 (*replySize < sizeof(effect_param_t) ||
756 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
757 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700758 return -EINVAL;
759 }
ragoe2759072016-11-22 18:02:48 -0800760 if (cmdCode == EFFECT_CMD_GET_PARAM &&
761 (sizeof(effect_param_t) > *replySize
762 || ((effect_param_t *)pCmdData)->psize > *replySize
763 - sizeof(effect_param_t)
764 || ((effect_param_t *)pCmdData)->vsize > *replySize
765 - sizeof(effect_param_t)
766 - ((effect_param_t *)pCmdData)->psize
767 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
768 *replySize
769 - sizeof(effect_param_t)
770 - ((effect_param_t *)pCmdData)->psize
771 - ((effect_param_t *)pCmdData)->vsize)) {
772 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
773 android_errorWriteLog(0x534e4554, "32705438");
774 return -EINVAL;
775 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700776 if ((cmdCode == EFFECT_CMD_SET_PARAM
777 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
778 (sizeof(effect_param_t) > cmdSize
779 || ((effect_param_t *)pCmdData)->psize > cmdSize
780 - sizeof(effect_param_t)
781 || ((effect_param_t *)pCmdData)->vsize > cmdSize
782 - sizeof(effect_param_t)
783 - ((effect_param_t *)pCmdData)->psize
784 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
785 cmdSize
786 - sizeof(effect_param_t)
787 - ((effect_param_t *)pCmdData)->psize
788 - ((effect_param_t *)pCmdData)->vsize)) {
789 android_errorWriteLog(0x534e4554, "30204301");
790 return -EINVAL;
791 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700792 status_t status = mEffectInterface->command(cmdCode,
793 cmdSize,
794 pCmdData,
795 replySize,
796 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800797 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
798 uint32_t size = (replySize == NULL) ? 0 : *replySize;
799 for (size_t i = 1; i < mHandles.size(); i++) {
800 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800801 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800802 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
803 }
804 }
805 }
806 return status;
807}
808
809status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
810{
811 Mutex::Autolock _l(mLock);
812 return setEnabled_l(enabled);
813}
814
815// must be called with EffectModule::mLock held
816status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
817{
818
819 ALOGV("setEnabled %p enabled %d", this, enabled);
820
821 if (enabled != isEnabled()) {
822 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
823 if (enabled && status != NO_ERROR) {
824 return status;
825 }
826
827 switch (mState) {
828 // going from disabled to enabled
829 case IDLE:
830 mState = STARTING;
831 break;
832 case STOPPED:
833 mState = RESTART;
834 break;
835 case STOPPING:
836 mState = ACTIVE;
837 break;
838
839 // going from enabled to disabled
840 case RESTART:
841 mState = STOPPED;
842 break;
843 case STARTING:
844 mState = IDLE;
845 break;
846 case ACTIVE:
847 mState = STOPPING;
848 break;
849 case DESTROYED:
850 return NO_ERROR; // simply ignore as we are being destroyed
851 }
852 for (size_t i = 1; i < mHandles.size(); i++) {
853 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800854 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800855 h->setEnabled(enabled);
856 }
857 }
858 }
859 return NO_ERROR;
860}
861
862bool AudioFlinger::EffectModule::isEnabled() const
863{
864 switch (mState) {
865 case RESTART:
866 case STARTING:
867 case ACTIVE:
868 return true;
869 case IDLE:
870 case STOPPING:
871 case STOPPED:
872 case DESTROYED:
873 default:
874 return false;
875 }
876}
877
878bool AudioFlinger::EffectModule::isProcessEnabled() const
879{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700880 if (mStatus != NO_ERROR) {
881 return false;
882 }
883
Eric Laurentca7cc822012-11-19 14:55:58 -0800884 switch (mState) {
885 case RESTART:
886 case ACTIVE:
887 case STOPPING:
888 case STOPPED:
889 return true;
890 case IDLE:
891 case STARTING:
892 case DESTROYED:
893 default:
894 return false;
895 }
896}
897
Mikhail Naganov022b9952017-01-04 16:36:51 -0800898void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700899 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800900
901 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800902 if (buffer != 0) {
903 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
904 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
905 } else {
906 mConfig.inputCfg.buffer.raw = NULL;
907 }
908 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800909 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700910
911#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800912 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -0700913 // Theoretically insert effects can also do in-place conversions (destroying
914 // the original buffer) when the output buffer is identical to the input buffer,
915 // but we don't optimize for it here.
916 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
917 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
918 // we need to translate - create hidl shared buffer and intercept
919 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
920 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
921 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
922
923 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
924 __func__, inChannels, inFrameCount, size);
925
Andy Hungbded9c82017-11-30 18:47:35 -0800926 if (size > 0 && (mInConversionBuffer.get() == nullptr
927 || size > mInConversionBuffer->getSize())) {
928 mInConversionBuffer.clear();
929 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
930 (void)EffectBufferHalInterface::allocate(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700931 }
Andy Hungbded9c82017-11-30 18:47:35 -0800932 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -0800933 mInConversionBuffer->setFrameCount(inFrameCount);
934 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700935 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800936 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700937 }
938 }
939#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800940}
941
942void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700943 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800944
945 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800946 if (buffer != 0) {
947 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
948 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
949 } else {
950 mConfig.outputCfg.buffer.raw = NULL;
951 }
952 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800953 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700954
955#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800956 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -0700957 // can do in-place conversion from int16_t to float. We don't optimize here.
958 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
959 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
960 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
961 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
962
963 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
964 __func__, outChannels, outFrameCount, size);
965
Andy Hungbded9c82017-11-30 18:47:35 -0800966 if (size > 0 && (mOutConversionBuffer.get() == nullptr
967 || size > mOutConversionBuffer->getSize())) {
968 mOutConversionBuffer.clear();
969 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
970 (void)EffectBufferHalInterface::allocate(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700971 }
Andy Hungbded9c82017-11-30 18:47:35 -0800972 if (mOutConversionBuffer.get() != nullptr) {
973 mOutConversionBuffer->setFrameCount(outFrameCount);
974 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700975 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800976 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700977 }
978 }
979#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800980}
981
Eric Laurentca7cc822012-11-19 14:55:58 -0800982status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
983{
984 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700985 if (mStatus != NO_ERROR) {
986 return mStatus;
987 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800988 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800989 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
990 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
991 if (isProcessEnabled() &&
992 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
993 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800994 uint32_t volume[2];
995 uint32_t *pVolume = NULL;
996 uint32_t size = sizeof(volume);
997 volume[0] = *left;
998 volume[1] = *right;
999 if (controller) {
1000 pVolume = volume;
1001 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001002 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1003 size,
1004 volume,
1005 &size,
1006 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001007 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1008 *left = volume[0];
1009 *right = volume[1];
1010 }
1011 }
1012 return status;
1013}
1014
1015status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1016{
1017 if (device == AUDIO_DEVICE_NONE) {
1018 return NO_ERROR;
1019 }
1020
1021 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001022 if (mStatus != NO_ERROR) {
1023 return mStatus;
1024 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001025 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001026 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001027 status_t cmdStatus;
1028 uint32_t size = sizeof(status_t);
1029 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1030 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001031 status = mEffectInterface->command(cmd,
1032 sizeof(uint32_t),
1033 &device,
1034 &size,
1035 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 }
1037 return status;
1038}
1039
1040status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1041{
1042 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001043 if (mStatus != NO_ERROR) {
1044 return mStatus;
1045 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001046 status_t status = NO_ERROR;
1047 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1048 status_t cmdStatus;
1049 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001050 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1051 sizeof(audio_mode_t),
1052 &mode,
1053 &size,
1054 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001055 if (status == NO_ERROR) {
1056 status = cmdStatus;
1057 }
1058 }
1059 return status;
1060}
1061
1062status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1063{
1064 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001065 if (mStatus != NO_ERROR) {
1066 return mStatus;
1067 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001068 status_t status = NO_ERROR;
1069 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1070 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001071 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1072 sizeof(audio_source_t),
1073 &source,
1074 &size,
1075 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 }
1077 return status;
1078}
1079
1080void AudioFlinger::EffectModule::setSuspended(bool suspended)
1081{
1082 Mutex::Autolock _l(mLock);
1083 mSuspended = suspended;
1084}
1085
1086bool AudioFlinger::EffectModule::suspended() const
1087{
1088 Mutex::Autolock _l(mLock);
1089 return mSuspended;
1090}
1091
1092bool AudioFlinger::EffectModule::purgeHandles()
1093{
1094 bool enabled = false;
1095 Mutex::Autolock _l(mLock);
1096 for (size_t i = 0; i < mHandles.size(); i++) {
1097 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001098 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001099 if (handle->hasControl()) {
1100 enabled = handle->enabled();
1101 }
1102 }
1103 }
1104 return enabled;
1105}
1106
Eric Laurent5baf2af2013-09-12 17:37:00 -07001107status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1108{
1109 Mutex::Autolock _l(mLock);
1110 if (mStatus != NO_ERROR) {
1111 return mStatus;
1112 }
1113 status_t status = NO_ERROR;
1114 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1115 status_t cmdStatus;
1116 uint32_t size = sizeof(status_t);
1117 effect_offload_param_t cmd;
1118
1119 cmd.isOffload = offloaded;
1120 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001121 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1122 sizeof(effect_offload_param_t),
1123 &cmd,
1124 &size,
1125 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001126 if (status == NO_ERROR) {
1127 status = cmdStatus;
1128 }
1129 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1130 } else {
1131 if (offloaded) {
1132 status = INVALID_OPERATION;
1133 }
1134 mOffloaded = false;
1135 }
1136 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1137 return status;
1138}
1139
1140bool AudioFlinger::EffectModule::isOffloaded() const
1141{
1142 Mutex::Autolock _l(mLock);
1143 return mOffloaded;
1144}
1145
Marco Nelissenb2208842014-02-07 14:00:50 -08001146String8 effectFlagsToString(uint32_t flags) {
1147 String8 s;
1148
1149 s.append("conn. mode: ");
1150 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1151 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1152 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1153 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1154 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1155 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1156 default: s.append("unknown/reserved"); break;
1157 }
1158 s.append(", ");
1159
1160 s.append("insert pref: ");
1161 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1162 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1163 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1164 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1165 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1166 default: s.append("unknown/reserved"); break;
1167 }
1168 s.append(", ");
1169
1170 s.append("volume mgmt: ");
1171 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1172 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1173 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1174 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1175 default: s.append("unknown/reserved"); break;
1176 }
1177 s.append(", ");
1178
1179 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1180 if (devind) {
1181 s.append("device indication: ");
1182 switch (devind) {
1183 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1184 default: s.append("unknown/reserved"); break;
1185 }
1186 s.append(", ");
1187 }
1188
1189 s.append("input mode: ");
1190 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1191 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1192 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1193 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1194 default: s.append("not set"); break;
1195 }
1196 s.append(", ");
1197
1198 s.append("output mode: ");
1199 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1200 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1201 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1202 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1203 default: s.append("not set"); break;
1204 }
1205 s.append(", ");
1206
1207 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1208 if (accel) {
1209 s.append("hardware acceleration: ");
1210 switch (accel) {
1211 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1212 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1213 default: s.append("unknown/reserved"); break;
1214 }
1215 s.append(", ");
1216 }
1217
1218 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1219 if (modeind) {
1220 s.append("mode indication: ");
1221 switch (modeind) {
1222 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1223 default: s.append("unknown/reserved"); break;
1224 }
1225 s.append(", ");
1226 }
1227
1228 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1229 if (srcind) {
1230 s.append("source indication: ");
1231 switch (srcind) {
1232 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1233 default: s.append("unknown/reserved"); break;
1234 }
1235 s.append(", ");
1236 }
1237
1238 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1239 s.append("offloadable, ");
1240 }
1241
1242 int len = s.length();
1243 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001244 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001245 s.unlockBuffer(len - 2);
1246 }
1247 return s;
1248}
1249
Andy Hungbded9c82017-11-30 18:47:35 -08001250static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1251 std::stringstream ss;
1252
1253 if (buffer.get() == nullptr) {
1254 return "nullptr"; // make different than below
1255 } else if (buffer->externalData() != nullptr) {
1256 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1257 << " -> "
1258 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1259 } else {
1260 ss << buffer->audioBuffer()->raw;
1261 }
1262 return ss.str();
1263}
Marco Nelissenb2208842014-02-07 14:00:50 -08001264
Glenn Kasten0f11b512014-01-31 16:18:54 -08001265void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001266{
1267 const size_t SIZE = 256;
1268 char buffer[SIZE];
1269 String8 result;
1270
1271 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1272 result.append(buffer);
1273
1274 bool locked = AudioFlinger::dumpTryLock(mLock);
1275 // failed to lock - AudioFlinger is probably deadlocked
1276 if (!locked) {
1277 result.append("\t\tCould not lock Fx mutex:\n");
1278 }
1279
1280 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001281 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001282 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001283 result.append(buffer);
1284
1285 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001286 char uuidStr[64];
1287 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1288 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001289 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001290 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1291 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001292 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001293 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001294 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001295 mDescriptor.flags,
1296 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001297 result.append(buffer);
1298 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1299 mDescriptor.name);
1300 result.append(buffer);
1301 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1302 mDescriptor.implementor);
1303 result.append(buffer);
1304
1305 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001306 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001307 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001308 mConfig.inputCfg.buffer.frameCount,
1309 mConfig.inputCfg.samplingRate,
1310 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001311 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001312 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001313 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001314 result.append(buffer);
1315
1316 result.append("\t\t- Output configuration:\n");
1317 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001318 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001319 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001320 mConfig.outputCfg.buffer.frameCount,
1321 mConfig.outputCfg.samplingRate,
1322 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001323 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001324 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001325 result.append(buffer);
1326
rago94a1ee82017-07-21 15:11:02 -07001327#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001328
Andy Hungbded9c82017-11-30 18:47:35 -08001329 result.appendFormat("\t\t- HAL buffers:\n"
1330 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1331 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1332 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1333 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1334 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001335#endif
1336
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001337 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001338 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001339 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001340 for (size_t i = 0; i < mHandles.size(); ++i) {
1341 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001342 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001343 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001344 result.append(buffer);
1345 }
1346 }
1347
Eric Laurentca7cc822012-11-19 14:55:58 -08001348 write(fd, result.string(), result.length());
1349
1350 if (locked) {
1351 mLock.unlock();
1352 }
1353}
1354
1355// ----------------------------------------------------------------------------
1356// EffectHandle implementation
1357// ----------------------------------------------------------------------------
1358
1359#undef LOG_TAG
1360#define LOG_TAG "AudioFlinger::EffectHandle"
1361
1362AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1363 const sp<AudioFlinger::Client>& client,
1364 const sp<IEffectClient>& effectClient,
1365 int32_t priority)
1366 : BnEffect(),
1367 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001368 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001369{
1370 ALOGV("constructor %p", this);
1371
1372 if (client == 0) {
1373 return;
1374 }
1375 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1376 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001377 if (mCblkMemory == 0 ||
1378 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001379 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001380 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001381 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001382 return;
1383 }
Glenn Kastene75da402013-11-20 13:54:52 -08001384 new(mCblk) effect_param_cblk_t();
1385 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001386}
1387
1388AudioFlinger::EffectHandle::~EffectHandle()
1389{
1390 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001391 disconnect(false);
1392}
1393
Glenn Kastene75da402013-11-20 13:54:52 -08001394status_t AudioFlinger::EffectHandle::initCheck()
1395{
1396 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1397}
1398
Eric Laurentca7cc822012-11-19 14:55:58 -08001399status_t AudioFlinger::EffectHandle::enable()
1400{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001401 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001403 sp<EffectModule> effect = mEffect.promote();
1404 if (effect == 0 || mDisconnected) {
1405 return DEAD_OBJECT;
1406 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001407 if (!mHasControl) {
1408 return INVALID_OPERATION;
1409 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001410
1411 if (mEnabled) {
1412 return NO_ERROR;
1413 }
1414
1415 mEnabled = true;
1416
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001417 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001418 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001419 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001420 }
1421
1422 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001423 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001424 return NO_ERROR;
1425 }
1426
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001427 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001428 if (status != NO_ERROR) {
1429 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001430 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 }
1432 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001433 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001434 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001435 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1436 Mutex::Autolock _l(thread->mLock);
1437 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001438 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001439 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001440 if (thread->type() == ThreadBase::OFFLOAD) {
1441 PlaybackThread *t = (PlaybackThread *)thread.get();
1442 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1443 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001444 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001445 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1446 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001447 }
1448 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001449 }
1450 return status;
1451}
1452
1453status_t AudioFlinger::EffectHandle::disable()
1454{
1455 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001456 AutoMutex _l(mLock);
1457 sp<EffectModule> effect = mEffect.promote();
1458 if (effect == 0 || mDisconnected) {
1459 return DEAD_OBJECT;
1460 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001461 if (!mHasControl) {
1462 return INVALID_OPERATION;
1463 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001464
1465 if (!mEnabled) {
1466 return NO_ERROR;
1467 }
1468 mEnabled = false;
1469
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001470 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001471 return NO_ERROR;
1472 }
1473
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001474 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001475
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001476 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001477 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001478 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001479 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1480 Mutex::Autolock _l(thread->mLock);
1481 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001482 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001483 }
1484
1485 return status;
1486}
1487
1488void AudioFlinger::EffectHandle::disconnect()
1489{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001490 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001491 disconnect(true);
1492}
1493
1494void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1495{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001496 AutoMutex _l(mLock);
1497 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1498 if (mDisconnected) {
1499 if (unpinIfLast) {
1500 android_errorWriteLog(0x534e4554, "32707507");
1501 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001502 return;
1503 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001504 mDisconnected = true;
1505 sp<ThreadBase> thread;
1506 {
1507 sp<EffectModule> effect = mEffect.promote();
1508 if (effect != 0) {
1509 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001510 }
1511 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001512 if (thread != 0) {
1513 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001514 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001515 // try to cleanup as much as we can
1516 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001517 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1518 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001519 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001520 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001521
Eric Laurentca7cc822012-11-19 14:55:58 -08001522 if (mClient != 0) {
1523 if (mCblk != NULL) {
1524 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1525 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1526 }
1527 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001528 // Client destructor must run with AudioFlinger client mutex locked
1529 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001530 mClient.clear();
1531 }
1532}
1533
1534status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1535 uint32_t cmdSize,
1536 void *pCmdData,
1537 uint32_t *replySize,
1538 void *pReplyData)
1539{
1540 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001541 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001542
Eric Laurentc7ab3092017-06-15 18:43:46 -07001543 // reject commands reserved for internal use by audio framework if coming from outside
1544 // of audioserver
1545 switch(cmdCode) {
1546 case EFFECT_CMD_ENABLE:
1547 case EFFECT_CMD_DISABLE:
1548 case EFFECT_CMD_SET_PARAM:
1549 case EFFECT_CMD_SET_PARAM_DEFERRED:
1550 case EFFECT_CMD_SET_PARAM_COMMIT:
1551 case EFFECT_CMD_GET_PARAM:
1552 break;
1553 default:
1554 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1555 break;
1556 }
1557 android_errorWriteLog(0x534e4554, "62019992");
1558 return BAD_VALUE;
1559 }
1560
Eric Laurent1ffc5852016-12-15 14:46:09 -08001561 if (cmdCode == EFFECT_CMD_ENABLE) {
1562 if (*replySize < sizeof(int)) {
1563 android_errorWriteLog(0x534e4554, "32095713");
1564 return BAD_VALUE;
1565 }
1566 *(int *)pReplyData = NO_ERROR;
1567 *replySize = sizeof(int);
1568 return enable();
1569 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1570 if (*replySize < sizeof(int)) {
1571 android_errorWriteLog(0x534e4554, "32095713");
1572 return BAD_VALUE;
1573 }
1574 *(int *)pReplyData = NO_ERROR;
1575 *replySize = sizeof(int);
1576 return disable();
1577 }
1578
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001579 AutoMutex _l(mLock);
1580 sp<EffectModule> effect = mEffect.promote();
1581 if (effect == 0 || mDisconnected) {
1582 return DEAD_OBJECT;
1583 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001584 // only get parameter command is permitted for applications not controlling the effect
1585 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1586 return INVALID_OPERATION;
1587 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001588 if (mClient == 0) {
1589 return INVALID_OPERATION;
1590 }
1591
1592 // handle commands that are not forwarded transparently to effect engine
1593 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001594 if (*replySize < sizeof(int)) {
1595 android_errorWriteLog(0x534e4554, "32095713");
1596 return BAD_VALUE;
1597 }
1598 *(int *)pReplyData = NO_ERROR;
1599 *replySize = sizeof(int);
1600
Eric Laurentca7cc822012-11-19 14:55:58 -08001601 // No need to trylock() here as this function is executed in the binder thread serving a
1602 // particular client process: no risk to block the whole media server process or mixer
1603 // threads if we are stuck here
1604 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001605 // keep local copy of index in case of client corruption b/32220769
1606 const uint32_t clientIndex = mCblk->clientIndex;
1607 const uint32_t serverIndex = mCblk->serverIndex;
1608 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1609 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001610 mCblk->serverIndex = 0;
1611 mCblk->clientIndex = 0;
1612 return BAD_VALUE;
1613 }
1614 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001615 effect_param_t *param = NULL;
1616 for (uint32_t index = serverIndex; index < clientIndex;) {
1617 int *p = (int *)(mBuffer + index);
1618 const int size = *p++;
1619 if (size < 0
1620 || size > EFFECT_PARAM_BUFFER_SIZE
1621 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001622 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001623 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001624 break;
1625 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001626
1627 // copy to local memory in case of client corruption b/32220769
1628 param = (effect_param_t *)realloc(param, size);
1629 if (param == NULL) {
1630 ALOGW("command(): out of memory");
1631 status = NO_MEMORY;
1632 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001633 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001634 memcpy(param, p, size);
1635
1636 int reply = 0;
1637 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001638 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001639 size,
1640 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001641 &rsize,
1642 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001643
1644 // verify shared memory: server index shouldn't change; client index can't go back.
1645 if (serverIndex != mCblk->serverIndex
1646 || clientIndex > mCblk->clientIndex) {
1647 android_errorWriteLog(0x534e4554, "32220769");
1648 status = BAD_VALUE;
1649 break;
1650 }
1651
Eric Laurentca7cc822012-11-19 14:55:58 -08001652 // stop at first error encountered
1653 if (ret != NO_ERROR) {
1654 status = ret;
1655 *(int *)pReplyData = reply;
1656 break;
1657 } else if (reply != NO_ERROR) {
1658 *(int *)pReplyData = reply;
1659 break;
1660 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001661 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001662 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001663 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001664 mCblk->serverIndex = 0;
1665 mCblk->clientIndex = 0;
1666 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 }
1668
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001669 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001670}
1671
1672void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1673{
1674 ALOGV("setControl %p control %d", this, hasControl);
1675
1676 mHasControl = hasControl;
1677 mEnabled = enabled;
1678
1679 if (signal && mEffectClient != 0) {
1680 mEffectClient->controlStatusChanged(hasControl);
1681 }
1682}
1683
1684void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1685 uint32_t cmdSize,
1686 void *pCmdData,
1687 uint32_t replySize,
1688 void *pReplyData)
1689{
1690 if (mEffectClient != 0) {
1691 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1692 }
1693}
1694
1695
1696
1697void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1698{
1699 if (mEffectClient != 0) {
1700 mEffectClient->enableStatusChanged(enabled);
1701 }
1702}
1703
1704status_t AudioFlinger::EffectHandle::onTransact(
1705 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1706{
1707 return BnEffect::onTransact(code, data, reply, flags);
1708}
1709
1710
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001711void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001712{
1713 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1714
Marco Nelissenb2208842014-02-07 14:00:50 -08001715 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001716 (mClient == 0) ? getpid_cached : mClient->pid(),
1717 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001718 mHasControl ? "yes" : "no",
1719 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001720 mCblk ? mCblk->clientIndex : 0,
1721 mCblk ? mCblk->serverIndex : 0
1722 );
1723
1724 if (locked) {
1725 mCblk->lock.unlock();
1726 }
1727}
1728
1729#undef LOG_TAG
1730#define LOG_TAG "AudioFlinger::EffectChain"
1731
1732AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001733 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001734 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001735 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001736 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001737{
1738 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1739 if (thread == NULL) {
1740 return;
1741 }
1742 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1743 thread->frameCount();
1744}
1745
1746AudioFlinger::EffectChain::~EffectChain()
1747{
Eric Laurentca7cc822012-11-19 14:55:58 -08001748}
1749
1750// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1751sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1752 effect_descriptor_t *descriptor)
1753{
1754 size_t size = mEffects.size();
1755
1756 for (size_t i = 0; i < size; i++) {
1757 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1758 return mEffects[i];
1759 }
1760 }
1761 return 0;
1762}
1763
1764// getEffectFromId_l() must be called with ThreadBase::mLock held
1765sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1766{
1767 size_t size = mEffects.size();
1768
1769 for (size_t i = 0; i < size; i++) {
1770 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1771 if (id == 0 || mEffects[i]->id() == id) {
1772 return mEffects[i];
1773 }
1774 }
1775 return 0;
1776}
1777
1778// getEffectFromType_l() must be called with ThreadBase::mLock held
1779sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1780 const effect_uuid_t *type)
1781{
1782 size_t size = mEffects.size();
1783
1784 for (size_t i = 0; i < size; i++) {
1785 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1786 return mEffects[i];
1787 }
1788 }
1789 return 0;
1790}
1791
1792void AudioFlinger::EffectChain::clearInputBuffer()
1793{
1794 Mutex::Autolock _l(mLock);
1795 sp<ThreadBase> thread = mThread.promote();
1796 if (thread == 0) {
1797 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1798 return;
1799 }
1800 clearInputBuffer_l(thread);
1801}
1802
1803// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001804void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001805{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001806 if (mInBuffer == NULL) {
1807 return;
1808 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001809 // TODO: This will change in the future, depending on multichannel
1810 // and sample format changes for effects.
1811 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1812 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001813
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001814 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001815 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1816 * std::min((uint32_t)FCC_2, thread->channelCount());
1817
Mikhail Naganov022b9952017-01-04 16:36:51 -08001818 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1819 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001820}
1821
1822// Must be called with EffectChain::mLock locked
1823void AudioFlinger::EffectChain::process_l()
1824{
1825 sp<ThreadBase> thread = mThread.promote();
1826 if (thread == 0) {
1827 ALOGW("process_l(): cannot promote mixer thread");
1828 return;
1829 }
1830 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1831 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001832 // never process effects when:
1833 // - on an OFFLOAD thread
1834 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001835 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1836 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001837 if (!isGlobalSession) {
1838 bool tracksOnSession = (trackCnt() != 0);
1839
1840 if (!tracksOnSession && mTailBufferCount == 0) {
1841 doProcess = false;
1842 }
1843
1844 if (activeTrackCnt() == 0) {
1845 // if no track is active and the effect tail has not been rendered,
1846 // the input buffer must be cleared here as the mixer process will not do it
1847 if (tracksOnSession || mTailBufferCount > 0) {
1848 clearInputBuffer_l(thread);
1849 if (mTailBufferCount > 0) {
1850 mTailBufferCount--;
1851 }
1852 }
1853 }
1854 }
1855
1856 size_t size = mEffects.size();
1857 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001858 // Only the input and output buffers of the chain can be external,
1859 // and 'update' / 'commit' do nothing for allocated buffers, thus
1860 // it's not needed to consider any other buffers here.
1861 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001862 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1863 mOutBuffer->update();
1864 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 for (size_t i = 0; i < size; i++) {
1866 mEffects[i]->process();
1867 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001868 mInBuffer->commit();
1869 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1870 mOutBuffer->commit();
1871 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001872 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001873 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001874 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001875 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1876 }
1877 if (doResetVolume) {
1878 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001879 }
1880}
1881
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001882// createEffect_l() must be called with ThreadBase::mLock held
1883status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1884 ThreadBase *thread,
1885 effect_descriptor_t *desc,
1886 int id,
1887 audio_session_t sessionId,
1888 bool pinned)
1889{
1890 Mutex::Autolock _l(mLock);
1891 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1892 status_t lStatus = effect->status();
1893 if (lStatus == NO_ERROR) {
1894 lStatus = addEffect_ll(effect);
1895 }
1896 if (lStatus != NO_ERROR) {
1897 effect.clear();
1898 }
1899 return lStatus;
1900}
1901
1902// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001903status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1904{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001905 Mutex::Autolock _l(mLock);
1906 return addEffect_ll(effect);
1907}
1908// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1909status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1910{
Eric Laurentca7cc822012-11-19 14:55:58 -08001911 effect_descriptor_t desc = effect->desc();
1912 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1913
Eric Laurentca7cc822012-11-19 14:55:58 -08001914 effect->setChain(this);
1915 sp<ThreadBase> thread = mThread.promote();
1916 if (thread == 0) {
1917 return NO_INIT;
1918 }
1919 effect->setThread(thread);
1920
1921 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1922 // Auxiliary effects are inserted at the beginning of mEffects vector as
1923 // they are processed first and accumulated in chain input buffer
1924 mEffects.insertAt(effect, 0);
1925
1926 // the input buffer for auxiliary effect contains mono samples in
1927 // 32 bit format. This is to avoid saturation in AudoMixer
1928 // accumulation stage. Saturation is done in EffectModule::process() before
1929 // calling the process in effect engine
1930 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001931 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001932#ifdef FLOAT_EFFECT_CHAIN
1933 status_t result = EffectBufferHalInterface::allocate(
1934 numSamples * sizeof(float), &halBuffer);
1935#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001936 status_t result = EffectBufferHalInterface::allocate(
1937 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001938#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001939 if (result != OK) return result;
1940 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001941 // auxiliary effects output samples to chain input buffer for further processing
1942 // by insert effects
1943 effect->setOutBuffer(mInBuffer);
1944 } else {
1945 // Insert effects are inserted at the end of mEffects vector as they are processed
1946 // after track and auxiliary effects.
1947 // Insert effect order as a function of indicated preference:
1948 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1949 // another effect is present
1950 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1951 // last effect claiming first position
1952 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1953 // first effect claiming last position
1954 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1955 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1956 // already present
1957
1958 size_t size = mEffects.size();
1959 size_t idx_insert = size;
1960 ssize_t idx_insert_first = -1;
1961 ssize_t idx_insert_last = -1;
1962
1963 for (size_t i = 0; i < size; i++) {
1964 effect_descriptor_t d = mEffects[i]->desc();
1965 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1966 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1967 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1968 // check invalid effect chaining combinations
1969 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1970 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1971 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1972 desc.name, d.name);
1973 return INVALID_OPERATION;
1974 }
1975 // remember position of first insert effect and by default
1976 // select this as insert position for new effect
1977 if (idx_insert == size) {
1978 idx_insert = i;
1979 }
1980 // remember position of last insert effect claiming
1981 // first position
1982 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1983 idx_insert_first = i;
1984 }
1985 // remember position of first insert effect claiming
1986 // last position
1987 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1988 idx_insert_last == -1) {
1989 idx_insert_last = i;
1990 }
1991 }
1992 }
1993
1994 // modify idx_insert from first position if needed
1995 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1996 if (idx_insert_last != -1) {
1997 idx_insert = idx_insert_last;
1998 } else {
1999 idx_insert = size;
2000 }
2001 } else {
2002 if (idx_insert_first != -1) {
2003 idx_insert = idx_insert_first + 1;
2004 }
2005 }
2006
2007 // always read samples from chain input buffer
2008 effect->setInBuffer(mInBuffer);
2009
2010 // if last effect in the chain, output samples to chain
2011 // output buffer, otherwise to chain input buffer
2012 if (idx_insert == size) {
2013 if (idx_insert != 0) {
2014 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2015 mEffects[idx_insert-1]->configure();
2016 }
2017 effect->setOutBuffer(mOutBuffer);
2018 } else {
2019 effect->setOutBuffer(mInBuffer);
2020 }
2021 mEffects.insertAt(effect, idx_insert);
2022
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002023 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002024 idx_insert);
2025 }
2026 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002027
Eric Laurentca7cc822012-11-19 14:55:58 -08002028 return NO_ERROR;
2029}
2030
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002031// removeEffect_l() must be called with ThreadBase::mLock held
2032size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2033 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002034{
2035 Mutex::Autolock _l(mLock);
2036 size_t size = mEffects.size();
2037 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2038
2039 for (size_t i = 0; i < size; i++) {
2040 if (effect == mEffects[i]) {
2041 // calling stop here will remove pre-processing effect from the audio HAL.
2042 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2043 // the middle of a read from audio HAL
2044 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2045 mEffects[i]->state() == EffectModule::STOPPING) {
2046 mEffects[i]->stop();
2047 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002048 if (release) {
2049 mEffects[i]->release_l();
2050 }
2051
Mikhail Naganov022b9952017-01-04 16:36:51 -08002052 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002053 if (i == size - 1 && i != 0) {
2054 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2055 mEffects[i - 1]->configure();
2056 }
2057 }
2058 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002059 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002060 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002061
Eric Laurentca7cc822012-11-19 14:55:58 -08002062 break;
2063 }
2064 }
2065
2066 return mEffects.size();
2067}
2068
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002069// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002070void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2071{
2072 size_t size = mEffects.size();
2073 for (size_t i = 0; i < size; i++) {
2074 mEffects[i]->setDevice(device);
2075 }
2076}
2077
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002078// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002079void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2080{
2081 size_t size = mEffects.size();
2082 for (size_t i = 0; i < size; i++) {
2083 mEffects[i]->setMode(mode);
2084 }
2085}
2086
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002087// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002088void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2089{
2090 size_t size = mEffects.size();
2091 for (size_t i = 0; i < size; i++) {
2092 mEffects[i]->setAudioSource(source);
2093 }
2094}
2095
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002096// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002097bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002098{
2099 uint32_t newLeft = *left;
2100 uint32_t newRight = *right;
2101 bool hasControl = false;
2102 int ctrlIdx = -1;
2103 size_t size = mEffects.size();
2104
2105 // first update volume controller
2106 for (size_t i = size; i > 0; i--) {
2107 if (mEffects[i - 1]->isProcessEnabled() &&
2108 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2109 ctrlIdx = i - 1;
2110 hasControl = true;
2111 break;
2112 }
2113 }
2114
Eric Laurentfa1e1232016-08-02 19:01:49 -07002115 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002116 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002117 if (hasControl) {
2118 *left = mNewLeftVolume;
2119 *right = mNewRightVolume;
2120 }
2121 return hasControl;
2122 }
2123
2124 mVolumeCtrlIdx = ctrlIdx;
2125 mLeftVolume = newLeft;
2126 mRightVolume = newRight;
2127
2128 // second get volume update from volume controller
2129 if (ctrlIdx >= 0) {
2130 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2131 mNewLeftVolume = newLeft;
2132 mNewRightVolume = newRight;
2133 }
2134 // then indicate volume to all other effects in chain.
2135 // Pass altered volume to effects before volume controller
2136 // and requested volume to effects after controller
2137 uint32_t lVol = newLeft;
2138 uint32_t rVol = newRight;
2139
2140 for (size_t i = 0; i < size; i++) {
2141 if ((int)i == ctrlIdx) {
2142 continue;
2143 }
2144 // this also works for ctrlIdx == -1 when there is no volume controller
2145 if ((int)i > ctrlIdx) {
2146 lVol = *left;
2147 rVol = *right;
2148 }
2149 mEffects[i]->setVolume(&lVol, &rVol, false);
2150 }
2151 *left = newLeft;
2152 *right = newRight;
2153
2154 return hasControl;
2155}
2156
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002157// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002158void AudioFlinger::EffectChain::resetVolume_l()
2159{
Eric Laurente7449bf2016-08-03 18:44:07 -07002160 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2161 uint32_t left = mLeftVolume;
2162 uint32_t right = mRightVolume;
2163 (void)setVolume_l(&left, &right, true);
2164 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002165}
2166
Eric Laurent1b928682014-10-02 19:41:47 -07002167void AudioFlinger::EffectChain::syncHalEffectsState()
2168{
2169 Mutex::Autolock _l(mLock);
2170 for (size_t i = 0; i < mEffects.size(); i++) {
2171 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2172 mEffects[i]->state() == EffectModule::STOPPING) {
2173 mEffects[i]->addEffectToHal_l();
2174 }
2175 }
2176}
2177
Eric Laurentca7cc822012-11-19 14:55:58 -08002178void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2179{
2180 const size_t SIZE = 256;
2181 char buffer[SIZE];
2182 String8 result;
2183
Marco Nelissenb2208842014-02-07 14:00:50 -08002184 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002185 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002186 result.append(buffer);
2187
Marco Nelissenb2208842014-02-07 14:00:50 -08002188 if (numEffects) {
2189 bool locked = AudioFlinger::dumpTryLock(mLock);
2190 // failed to lock - AudioFlinger is probably deadlocked
2191 if (!locked) {
2192 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002193 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002194
Andy Hungbded9c82017-11-30 18:47:35 -08002195 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2196 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2197 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2198 (int)inBufferStr.size(), "In buffer ",
2199 (int)outBufferStr.size(), "Out buffer ");
2200 result.appendFormat("\t%s %s %d\n",
2201 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002202 write(fd, result.string(), result.size());
2203
2204 for (size_t i = 0; i < numEffects; ++i) {
2205 sp<EffectModule> effect = mEffects[i];
2206 if (effect != 0) {
2207 effect->dump(fd, args);
2208 }
2209 }
2210
2211 if (locked) {
2212 mLock.unlock();
2213 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002214 }
2215}
2216
2217// must be called with ThreadBase::mLock held
2218void AudioFlinger::EffectChain::setEffectSuspended_l(
2219 const effect_uuid_t *type, bool suspend)
2220{
2221 sp<SuspendedEffectDesc> desc;
2222 // use effect type UUID timelow as key as there is no real risk of identical
2223 // timeLow fields among effect type UUIDs.
2224 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2225 if (suspend) {
2226 if (index >= 0) {
2227 desc = mSuspendedEffects.valueAt(index);
2228 } else {
2229 desc = new SuspendedEffectDesc();
2230 desc->mType = *type;
2231 mSuspendedEffects.add(type->timeLow, desc);
2232 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2233 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002234
Eric Laurentca7cc822012-11-19 14:55:58 -08002235 if (desc->mRefCount++ == 0) {
2236 sp<EffectModule> effect = getEffectIfEnabled(type);
2237 if (effect != 0) {
2238 desc->mEffect = effect;
2239 effect->setSuspended(true);
2240 effect->setEnabled(false);
2241 }
2242 }
2243 } else {
2244 if (index < 0) {
2245 return;
2246 }
2247 desc = mSuspendedEffects.valueAt(index);
2248 if (desc->mRefCount <= 0) {
2249 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002250 desc->mRefCount = 0;
2251 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002252 }
2253 if (--desc->mRefCount == 0) {
2254 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2255 if (desc->mEffect != 0) {
2256 sp<EffectModule> effect = desc->mEffect.promote();
2257 if (effect != 0) {
2258 effect->setSuspended(false);
2259 effect->lock();
2260 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002261 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002262 effect->setEnabled_l(handle->enabled());
2263 }
2264 effect->unlock();
2265 }
2266 desc->mEffect.clear();
2267 }
2268 mSuspendedEffects.removeItemsAt(index);
2269 }
2270 }
2271}
2272
2273// must be called with ThreadBase::mLock held
2274void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2275{
2276 sp<SuspendedEffectDesc> desc;
2277
2278 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2279 if (suspend) {
2280 if (index >= 0) {
2281 desc = mSuspendedEffects.valueAt(index);
2282 } else {
2283 desc = new SuspendedEffectDesc();
2284 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2285 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2286 }
2287 if (desc->mRefCount++ == 0) {
2288 Vector< sp<EffectModule> > effects;
2289 getSuspendEligibleEffects(effects);
2290 for (size_t i = 0; i < effects.size(); i++) {
2291 setEffectSuspended_l(&effects[i]->desc().type, true);
2292 }
2293 }
2294 } else {
2295 if (index < 0) {
2296 return;
2297 }
2298 desc = mSuspendedEffects.valueAt(index);
2299 if (desc->mRefCount <= 0) {
2300 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2301 desc->mRefCount = 1;
2302 }
2303 if (--desc->mRefCount == 0) {
2304 Vector<const effect_uuid_t *> types;
2305 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2306 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2307 continue;
2308 }
2309 types.add(&mSuspendedEffects.valueAt(i)->mType);
2310 }
2311 for (size_t i = 0; i < types.size(); i++) {
2312 setEffectSuspended_l(types[i], false);
2313 }
2314 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2315 mSuspendedEffects.keyAt(index));
2316 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2317 }
2318 }
2319}
2320
2321
2322// The volume effect is used for automated tests only
2323#ifndef OPENSL_ES_H_
2324static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2325 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2326const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2327#endif //OPENSL_ES_H_
2328
Eric Laurentd8365c52017-07-16 15:27:05 -07002329/* static */
2330bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2331{
2332 // Only NS and AEC are suspended when BtNRec is off
2333 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2334 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2335 return true;
2336 }
2337 return false;
2338}
2339
Eric Laurentca7cc822012-11-19 14:55:58 -08002340bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2341{
2342 // auxiliary effects and visualizer are never suspended on output mix
2343 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2344 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2345 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2346 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2347 return false;
2348 }
2349 return true;
2350}
2351
2352void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2353 Vector< sp<AudioFlinger::EffectModule> > &effects)
2354{
2355 effects.clear();
2356 for (size_t i = 0; i < mEffects.size(); i++) {
2357 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2358 effects.add(mEffects[i]);
2359 }
2360 }
2361}
2362
2363sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2364 const effect_uuid_t *type)
2365{
2366 sp<EffectModule> effect = getEffectFromType_l(type);
2367 return effect != 0 && effect->isEnabled() ? effect : 0;
2368}
2369
2370void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2371 bool enabled)
2372{
2373 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2374 if (enabled) {
2375 if (index < 0) {
2376 // if the effect is not suspend check if all effects are suspended
2377 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2378 if (index < 0) {
2379 return;
2380 }
2381 if (!isEffectEligibleForSuspend(effect->desc())) {
2382 return;
2383 }
2384 setEffectSuspended_l(&effect->desc().type, enabled);
2385 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2386 if (index < 0) {
2387 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2388 return;
2389 }
2390 }
2391 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2392 effect->desc().type.timeLow);
2393 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002394 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002395 if (desc->mEffect == 0) {
2396 desc->mEffect = effect;
2397 effect->setEnabled(false);
2398 effect->setSuspended(true);
2399 }
2400 } else {
2401 if (index < 0) {
2402 return;
2403 }
2404 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2405 effect->desc().type.timeLow);
2406 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2407 desc->mEffect.clear();
2408 effect->setSuspended(false);
2409 }
2410}
2411
Eric Laurent5baf2af2013-09-12 17:37:00 -07002412bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002413{
2414 Mutex::Autolock _l(mLock);
2415 size_t size = mEffects.size();
2416 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002417 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002418 return true;
2419 }
2420 }
2421 return false;
2422}
2423
Eric Laurentaaa44472014-09-12 17:41:50 -07002424void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2425{
2426 Mutex::Autolock _l(mLock);
2427 mThread = thread;
2428 for (size_t i = 0; i < mEffects.size(); i++) {
2429 mEffects[i]->setThread(thread);
2430 }
2431}
2432
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002433void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2434{
2435 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2436 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2437 }
2438 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2439 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2440 }
2441}
2442
2443void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2444{
2445 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2446 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2447 }
2448 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2449 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2450 }
2451}
2452
2453bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002454{
2455 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002456 for (const auto &effect : mEffects) {
2457 if (effect->isProcessImplemented()) {
2458 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002459 }
2460 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002461 // Allow effects without processing.
2462 return true;
2463}
2464
2465bool AudioFlinger::EffectChain::isFastCompatible() const
2466{
2467 Mutex::Autolock _l(mLock);
2468 for (const auto &effect : mEffects) {
2469 if (effect->isProcessImplemented()
2470 && effect->isImplementationSoftware()) {
2471 return false;
2472 }
2473 }
2474 // Allow effects without processing or hw accelerated effects.
2475 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002476}
2477
2478// isCompatibleWithThread_l() must be called with thread->mLock held
2479bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2480{
2481 Mutex::Autolock _l(mLock);
2482 for (size_t i = 0; i < mEffects.size(); i++) {
2483 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2484 return false;
2485 }
2486 }
2487 return true;
2488}
2489
Glenn Kasten63238ef2015-03-02 15:50:29 -08002490} // namespace android