blob: 979908ace44f157e195421d5eb080ea397a5fd7b [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
Andy Hung05083ac2017-12-14 15:00:28 -0800581 // mConfig.outputCfg.buffer.frameCount cannot be zero.
582 mMaxDisableWaitCnt = (uint32_t)std::max(
583 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
584 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
585 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800586
Eric Laurentd0ebb532013-04-02 16:41:41 -0700587exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800588 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700589 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700590 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800591 return status;
592}
593
594status_t AudioFlinger::EffectModule::init()
595{
596 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700597 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800598 return NO_INIT;
599 }
600 status_t cmdStatus;
601 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700602 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
603 0,
604 NULL,
605 &size,
606 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800607 if (status == 0) {
608 status = cmdStatus;
609 }
610 return status;
611}
612
Eric Laurent1b928682014-10-02 19:41:47 -0700613void AudioFlinger::EffectModule::addEffectToHal_l()
614{
615 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
616 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
617 sp<ThreadBase> thread = mThread.promote();
618 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700619 sp<StreamHalInterface> stream = thread->stream();
620 if (stream != 0) {
621 status_t result = stream->addEffect(mEffectInterface);
622 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700623 }
624 }
625 }
626}
627
Eric Laurentfa1e1232016-08-02 19:01:49 -0700628// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800629status_t AudioFlinger::EffectModule::start()
630{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700631 sp<EffectChain> chain;
632 status_t status;
633 {
634 Mutex::Autolock _l(mLock);
635 status = start_l();
636 if (status == NO_ERROR) {
637 chain = mChain.promote();
638 }
639 }
640 if (chain != 0) {
641 chain->resetVolume_l();
642 }
643 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800644}
645
646status_t AudioFlinger::EffectModule::start_l()
647{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700648 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800649 return NO_INIT;
650 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700651 if (mStatus != NO_ERROR) {
652 return mStatus;
653 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800654 status_t cmdStatus;
655 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700656 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
657 0,
658 NULL,
659 &size,
660 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800661 if (status == 0) {
662 status = cmdStatus;
663 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700664 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700665 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800666 }
667 return status;
668}
669
670status_t AudioFlinger::EffectModule::stop()
671{
672 Mutex::Autolock _l(mLock);
673 return stop_l();
674}
675
676status_t AudioFlinger::EffectModule::stop_l()
677{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700678 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800679 return NO_INIT;
680 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700681 if (mStatus != NO_ERROR) {
682 return mStatus;
683 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800684 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800685 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700686 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
687 0,
688 NULL,
689 &size,
690 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800691 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800692 status = cmdStatus;
693 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800694 if (status == NO_ERROR) {
695 status = remove_effect_from_hal_l();
696 }
697 return status;
698}
699
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800700// must be called with EffectChain::mLock held
701void AudioFlinger::EffectModule::release_l()
702{
703 if (mEffectInterface != 0) {
704 remove_effect_from_hal_l();
705 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800706 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800707 mEffectInterface.clear();
708 }
709}
710
Eric Laurentbfb1b832013-01-07 09:53:42 -0800711status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
712{
713 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
714 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800715 sp<ThreadBase> thread = mThread.promote();
716 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700717 sp<StreamHalInterface> stream = thread->stream();
718 if (stream != 0) {
719 status_t result = stream->removeEffect(mEffectInterface);
720 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800721 }
722 }
723 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800724 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800725}
726
Andy Hunge4a1d912016-08-17 14:11:13 -0700727// round up delta valid if value and divisor are positive.
728template <typename T>
729static T roundUpDelta(const T &value, const T &divisor) {
730 T remainder = value % divisor;
731 return remainder == 0 ? 0 : divisor - remainder;
732}
733
Eric Laurentca7cc822012-11-19 14:55:58 -0800734status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
735 uint32_t cmdSize,
736 void *pCmdData,
737 uint32_t *replySize,
738 void *pReplyData)
739{
740 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700741 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800742
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700743 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800744 return NO_INIT;
745 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700746 if (mStatus != NO_ERROR) {
747 return mStatus;
748 }
Andy Hung110bc952016-06-20 15:22:52 -0700749 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700750 (sizeof(effect_param_t) > cmdSize ||
751 ((effect_param_t *)pCmdData)->psize > cmdSize
752 - sizeof(effect_param_t))) {
753 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800754 android_errorWriteLog(0x534e4554, "33003822");
755 return -EINVAL;
756 }
757 if (cmdCode == EFFECT_CMD_GET_PARAM &&
758 (*replySize < sizeof(effect_param_t) ||
759 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
760 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700761 return -EINVAL;
762 }
ragoe2759072016-11-22 18:02:48 -0800763 if (cmdCode == EFFECT_CMD_GET_PARAM &&
764 (sizeof(effect_param_t) > *replySize
765 || ((effect_param_t *)pCmdData)->psize > *replySize
766 - sizeof(effect_param_t)
767 || ((effect_param_t *)pCmdData)->vsize > *replySize
768 - sizeof(effect_param_t)
769 - ((effect_param_t *)pCmdData)->psize
770 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
771 *replySize
772 - sizeof(effect_param_t)
773 - ((effect_param_t *)pCmdData)->psize
774 - ((effect_param_t *)pCmdData)->vsize)) {
775 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
776 android_errorWriteLog(0x534e4554, "32705438");
777 return -EINVAL;
778 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700779 if ((cmdCode == EFFECT_CMD_SET_PARAM
780 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
781 (sizeof(effect_param_t) > cmdSize
782 || ((effect_param_t *)pCmdData)->psize > cmdSize
783 - sizeof(effect_param_t)
784 || ((effect_param_t *)pCmdData)->vsize > cmdSize
785 - sizeof(effect_param_t)
786 - ((effect_param_t *)pCmdData)->psize
787 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
788 cmdSize
789 - sizeof(effect_param_t)
790 - ((effect_param_t *)pCmdData)->psize
791 - ((effect_param_t *)pCmdData)->vsize)) {
792 android_errorWriteLog(0x534e4554, "30204301");
793 return -EINVAL;
794 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700795 status_t status = mEffectInterface->command(cmdCode,
796 cmdSize,
797 pCmdData,
798 replySize,
799 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800800 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
801 uint32_t size = (replySize == NULL) ? 0 : *replySize;
802 for (size_t i = 1; i < mHandles.size(); i++) {
803 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800804 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800805 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
806 }
807 }
808 }
809 return status;
810}
811
812status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
813{
814 Mutex::Autolock _l(mLock);
815 return setEnabled_l(enabled);
816}
817
818// must be called with EffectModule::mLock held
819status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
820{
821
822 ALOGV("setEnabled %p enabled %d", this, enabled);
823
824 if (enabled != isEnabled()) {
825 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
826 if (enabled && status != NO_ERROR) {
827 return status;
828 }
829
830 switch (mState) {
831 // going from disabled to enabled
832 case IDLE:
833 mState = STARTING;
834 break;
835 case STOPPED:
836 mState = RESTART;
837 break;
838 case STOPPING:
839 mState = ACTIVE;
840 break;
841
842 // going from enabled to disabled
843 case RESTART:
844 mState = STOPPED;
845 break;
846 case STARTING:
847 mState = IDLE;
848 break;
849 case ACTIVE:
850 mState = STOPPING;
851 break;
852 case DESTROYED:
853 return NO_ERROR; // simply ignore as we are being destroyed
854 }
855 for (size_t i = 1; i < mHandles.size(); i++) {
856 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800857 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800858 h->setEnabled(enabled);
859 }
860 }
861 }
862 return NO_ERROR;
863}
864
865bool AudioFlinger::EffectModule::isEnabled() const
866{
867 switch (mState) {
868 case RESTART:
869 case STARTING:
870 case ACTIVE:
871 return true;
872 case IDLE:
873 case STOPPING:
874 case STOPPED:
875 case DESTROYED:
876 default:
877 return false;
878 }
879}
880
881bool AudioFlinger::EffectModule::isProcessEnabled() const
882{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700883 if (mStatus != NO_ERROR) {
884 return false;
885 }
886
Eric Laurentca7cc822012-11-19 14:55:58 -0800887 switch (mState) {
888 case RESTART:
889 case ACTIVE:
890 case STOPPING:
891 case STOPPED:
892 return true;
893 case IDLE:
894 case STARTING:
895 case DESTROYED:
896 default:
897 return false;
898 }
899}
900
Mikhail Naganov022b9952017-01-04 16:36:51 -0800901void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700902 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800903
904 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800905 if (buffer != 0) {
906 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
907 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
908 } else {
909 mConfig.inputCfg.buffer.raw = NULL;
910 }
911 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800912 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700913
914#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800915 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -0700916 // Theoretically insert effects can also do in-place conversions (destroying
917 // the original buffer) when the output buffer is identical to the input buffer,
918 // but we don't optimize for it here.
919 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
920 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
921 // we need to translate - create hidl shared buffer and intercept
922 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
923 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
924 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
925
926 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
927 __func__, inChannels, inFrameCount, size);
928
Andy Hungbded9c82017-11-30 18:47:35 -0800929 if (size > 0 && (mInConversionBuffer.get() == nullptr
930 || size > mInConversionBuffer->getSize())) {
931 mInConversionBuffer.clear();
932 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
933 (void)EffectBufferHalInterface::allocate(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700934 }
Andy Hungbded9c82017-11-30 18:47:35 -0800935 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -0800936 mInConversionBuffer->setFrameCount(inFrameCount);
937 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700938 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800939 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700940 }
941 }
942#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800943}
944
945void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700946 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800947
948 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800949 if (buffer != 0) {
950 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
951 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
952 } else {
953 mConfig.outputCfg.buffer.raw = NULL;
954 }
955 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800956 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700957
958#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800959 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -0700960 // can do in-place conversion from int16_t to float. We don't optimize here.
961 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
962 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
963 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
964 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
965
966 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
967 __func__, outChannels, outFrameCount, size);
968
Andy Hungbded9c82017-11-30 18:47:35 -0800969 if (size > 0 && (mOutConversionBuffer.get() == nullptr
970 || size > mOutConversionBuffer->getSize())) {
971 mOutConversionBuffer.clear();
972 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
973 (void)EffectBufferHalInterface::allocate(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700974 }
Andy Hungbded9c82017-11-30 18:47:35 -0800975 if (mOutConversionBuffer.get() != nullptr) {
976 mOutConversionBuffer->setFrameCount(outFrameCount);
977 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700978 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800979 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700980 }
981 }
982#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800983}
984
Eric Laurentca7cc822012-11-19 14:55:58 -0800985status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
986{
987 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700988 if (mStatus != NO_ERROR) {
989 return mStatus;
990 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800991 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800992 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
993 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
994 if (isProcessEnabled() &&
995 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
996 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800997 uint32_t volume[2];
998 uint32_t *pVolume = NULL;
999 uint32_t size = sizeof(volume);
1000 volume[0] = *left;
1001 volume[1] = *right;
1002 if (controller) {
1003 pVolume = volume;
1004 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001005 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1006 size,
1007 volume,
1008 &size,
1009 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001010 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1011 *left = volume[0];
1012 *right = volume[1];
1013 }
1014 }
1015 return status;
1016}
1017
1018status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1019{
1020 if (device == AUDIO_DEVICE_NONE) {
1021 return NO_ERROR;
1022 }
1023
1024 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001025 if (mStatus != NO_ERROR) {
1026 return mStatus;
1027 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001028 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001029 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001030 status_t cmdStatus;
1031 uint32_t size = sizeof(status_t);
1032 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1033 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001034 status = mEffectInterface->command(cmd,
1035 sizeof(uint32_t),
1036 &device,
1037 &size,
1038 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001039 }
1040 return status;
1041}
1042
1043status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1044{
1045 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001046 if (mStatus != NO_ERROR) {
1047 return mStatus;
1048 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001049 status_t status = NO_ERROR;
1050 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1051 status_t cmdStatus;
1052 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001053 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1054 sizeof(audio_mode_t),
1055 &mode,
1056 &size,
1057 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001058 if (status == NO_ERROR) {
1059 status = cmdStatus;
1060 }
1061 }
1062 return status;
1063}
1064
1065status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1066{
1067 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001068 if (mStatus != NO_ERROR) {
1069 return mStatus;
1070 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001071 status_t status = NO_ERROR;
1072 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1073 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001074 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1075 sizeof(audio_source_t),
1076 &source,
1077 &size,
1078 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001079 }
1080 return status;
1081}
1082
1083void AudioFlinger::EffectModule::setSuspended(bool suspended)
1084{
1085 Mutex::Autolock _l(mLock);
1086 mSuspended = suspended;
1087}
1088
1089bool AudioFlinger::EffectModule::suspended() const
1090{
1091 Mutex::Autolock _l(mLock);
1092 return mSuspended;
1093}
1094
1095bool AudioFlinger::EffectModule::purgeHandles()
1096{
1097 bool enabled = false;
1098 Mutex::Autolock _l(mLock);
1099 for (size_t i = 0; i < mHandles.size(); i++) {
1100 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001101 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001102 if (handle->hasControl()) {
1103 enabled = handle->enabled();
1104 }
1105 }
1106 }
1107 return enabled;
1108}
1109
Eric Laurent5baf2af2013-09-12 17:37:00 -07001110status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1111{
1112 Mutex::Autolock _l(mLock);
1113 if (mStatus != NO_ERROR) {
1114 return mStatus;
1115 }
1116 status_t status = NO_ERROR;
1117 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1118 status_t cmdStatus;
1119 uint32_t size = sizeof(status_t);
1120 effect_offload_param_t cmd;
1121
1122 cmd.isOffload = offloaded;
1123 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001124 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1125 sizeof(effect_offload_param_t),
1126 &cmd,
1127 &size,
1128 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001129 if (status == NO_ERROR) {
1130 status = cmdStatus;
1131 }
1132 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1133 } else {
1134 if (offloaded) {
1135 status = INVALID_OPERATION;
1136 }
1137 mOffloaded = false;
1138 }
1139 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1140 return status;
1141}
1142
1143bool AudioFlinger::EffectModule::isOffloaded() const
1144{
1145 Mutex::Autolock _l(mLock);
1146 return mOffloaded;
1147}
1148
Marco Nelissenb2208842014-02-07 14:00:50 -08001149String8 effectFlagsToString(uint32_t flags) {
1150 String8 s;
1151
1152 s.append("conn. mode: ");
1153 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1154 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1155 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1156 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1157 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1158 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1159 default: s.append("unknown/reserved"); break;
1160 }
1161 s.append(", ");
1162
1163 s.append("insert pref: ");
1164 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1165 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1166 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1167 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1168 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1169 default: s.append("unknown/reserved"); break;
1170 }
1171 s.append(", ");
1172
1173 s.append("volume mgmt: ");
1174 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1175 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1176 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1177 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1178 default: s.append("unknown/reserved"); break;
1179 }
1180 s.append(", ");
1181
1182 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1183 if (devind) {
1184 s.append("device indication: ");
1185 switch (devind) {
1186 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1187 default: s.append("unknown/reserved"); break;
1188 }
1189 s.append(", ");
1190 }
1191
1192 s.append("input mode: ");
1193 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1194 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1195 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1196 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1197 default: s.append("not set"); break;
1198 }
1199 s.append(", ");
1200
1201 s.append("output mode: ");
1202 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1203 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1204 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1205 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1206 default: s.append("not set"); break;
1207 }
1208 s.append(", ");
1209
1210 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1211 if (accel) {
1212 s.append("hardware acceleration: ");
1213 switch (accel) {
1214 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1215 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1216 default: s.append("unknown/reserved"); break;
1217 }
1218 s.append(", ");
1219 }
1220
1221 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1222 if (modeind) {
1223 s.append("mode indication: ");
1224 switch (modeind) {
1225 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1226 default: s.append("unknown/reserved"); break;
1227 }
1228 s.append(", ");
1229 }
1230
1231 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1232 if (srcind) {
1233 s.append("source indication: ");
1234 switch (srcind) {
1235 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1236 default: s.append("unknown/reserved"); break;
1237 }
1238 s.append(", ");
1239 }
1240
1241 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1242 s.append("offloadable, ");
1243 }
1244
1245 int len = s.length();
1246 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001247 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001248 s.unlockBuffer(len - 2);
1249 }
1250 return s;
1251}
1252
Andy Hungbded9c82017-11-30 18:47:35 -08001253static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1254 std::stringstream ss;
1255
1256 if (buffer.get() == nullptr) {
1257 return "nullptr"; // make different than below
1258 } else if (buffer->externalData() != nullptr) {
1259 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1260 << " -> "
1261 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1262 } else {
1263 ss << buffer->audioBuffer()->raw;
1264 }
1265 return ss.str();
1266}
Marco Nelissenb2208842014-02-07 14:00:50 -08001267
Glenn Kasten0f11b512014-01-31 16:18:54 -08001268void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001269{
1270 const size_t SIZE = 256;
1271 char buffer[SIZE];
1272 String8 result;
1273
1274 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1275 result.append(buffer);
1276
1277 bool locked = AudioFlinger::dumpTryLock(mLock);
1278 // failed to lock - AudioFlinger is probably deadlocked
1279 if (!locked) {
1280 result.append("\t\tCould not lock Fx mutex:\n");
1281 }
1282
1283 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001284 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001285 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001286 result.append(buffer);
1287
1288 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001289 char uuidStr[64];
1290 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1291 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001292 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001293 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1294 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001295 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001296 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001297 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001298 mDescriptor.flags,
1299 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001300 result.append(buffer);
1301 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1302 mDescriptor.name);
1303 result.append(buffer);
1304 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1305 mDescriptor.implementor);
1306 result.append(buffer);
1307
1308 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001309 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001310 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001311 mConfig.inputCfg.buffer.frameCount,
1312 mConfig.inputCfg.samplingRate,
1313 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001314 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001315 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001316 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001317 result.append(buffer);
1318
1319 result.append("\t\t- Output configuration:\n");
1320 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001321 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001322 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001323 mConfig.outputCfg.buffer.frameCount,
1324 mConfig.outputCfg.samplingRate,
1325 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001326 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001327 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001328 result.append(buffer);
1329
rago94a1ee82017-07-21 15:11:02 -07001330#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001331
Andy Hungbded9c82017-11-30 18:47:35 -08001332 result.appendFormat("\t\t- HAL buffers:\n"
1333 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1334 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1335 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1336 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1337 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001338#endif
1339
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001340 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001341 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001342 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001343 for (size_t i = 0; i < mHandles.size(); ++i) {
1344 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001345 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001346 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001347 result.append(buffer);
1348 }
1349 }
1350
Eric Laurentca7cc822012-11-19 14:55:58 -08001351 write(fd, result.string(), result.length());
1352
1353 if (locked) {
1354 mLock.unlock();
1355 }
1356}
1357
1358// ----------------------------------------------------------------------------
1359// EffectHandle implementation
1360// ----------------------------------------------------------------------------
1361
1362#undef LOG_TAG
1363#define LOG_TAG "AudioFlinger::EffectHandle"
1364
1365AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1366 const sp<AudioFlinger::Client>& client,
1367 const sp<IEffectClient>& effectClient,
1368 int32_t priority)
1369 : BnEffect(),
1370 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001371 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001372{
1373 ALOGV("constructor %p", this);
1374
1375 if (client == 0) {
1376 return;
1377 }
1378 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1379 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001380 if (mCblkMemory == 0 ||
1381 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001382 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001383 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001384 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001385 return;
1386 }
Glenn Kastene75da402013-11-20 13:54:52 -08001387 new(mCblk) effect_param_cblk_t();
1388 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001389}
1390
1391AudioFlinger::EffectHandle::~EffectHandle()
1392{
1393 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001394 disconnect(false);
1395}
1396
Glenn Kastene75da402013-11-20 13:54:52 -08001397status_t AudioFlinger::EffectHandle::initCheck()
1398{
1399 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1400}
1401
Eric Laurentca7cc822012-11-19 14:55:58 -08001402status_t AudioFlinger::EffectHandle::enable()
1403{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001404 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001405 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001406 sp<EffectModule> effect = mEffect.promote();
1407 if (effect == 0 || mDisconnected) {
1408 return DEAD_OBJECT;
1409 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 if (!mHasControl) {
1411 return INVALID_OPERATION;
1412 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001413
1414 if (mEnabled) {
1415 return NO_ERROR;
1416 }
1417
1418 mEnabled = true;
1419
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001420 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001422 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001423 }
1424
1425 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001426 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 return NO_ERROR;
1428 }
1429
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001430 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 if (status != NO_ERROR) {
1432 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001433 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001434 }
1435 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001436 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001437 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001438 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1439 Mutex::Autolock _l(thread->mLock);
1440 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001441 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001442 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001443 if (thread->type() == ThreadBase::OFFLOAD) {
1444 PlaybackThread *t = (PlaybackThread *)thread.get();
1445 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1446 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001447 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001448 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1449 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001450 }
1451 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001452 }
1453 return status;
1454}
1455
1456status_t AudioFlinger::EffectHandle::disable()
1457{
1458 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001459 AutoMutex _l(mLock);
1460 sp<EffectModule> effect = mEffect.promote();
1461 if (effect == 0 || mDisconnected) {
1462 return DEAD_OBJECT;
1463 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001464 if (!mHasControl) {
1465 return INVALID_OPERATION;
1466 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001467
1468 if (!mEnabled) {
1469 return NO_ERROR;
1470 }
1471 mEnabled = false;
1472
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001473 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001474 return NO_ERROR;
1475 }
1476
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001477 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001478
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001479 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001480 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001481 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001482 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1483 Mutex::Autolock _l(thread->mLock);
1484 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001485 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001486 }
1487
1488 return status;
1489}
1490
1491void AudioFlinger::EffectHandle::disconnect()
1492{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001493 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001494 disconnect(true);
1495}
1496
1497void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1498{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001499 AutoMutex _l(mLock);
1500 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1501 if (mDisconnected) {
1502 if (unpinIfLast) {
1503 android_errorWriteLog(0x534e4554, "32707507");
1504 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001505 return;
1506 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001507 mDisconnected = true;
1508 sp<ThreadBase> thread;
1509 {
1510 sp<EffectModule> effect = mEffect.promote();
1511 if (effect != 0) {
1512 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001513 }
1514 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001515 if (thread != 0) {
1516 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001517 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001518 // try to cleanup as much as we can
1519 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001520 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1521 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001522 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001523 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001524
Eric Laurentca7cc822012-11-19 14:55:58 -08001525 if (mClient != 0) {
1526 if (mCblk != NULL) {
1527 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1528 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1529 }
1530 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001531 // Client destructor must run with AudioFlinger client mutex locked
1532 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001533 mClient.clear();
1534 }
1535}
1536
1537status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1538 uint32_t cmdSize,
1539 void *pCmdData,
1540 uint32_t *replySize,
1541 void *pReplyData)
1542{
1543 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001544 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001545
Eric Laurentc7ab3092017-06-15 18:43:46 -07001546 // reject commands reserved for internal use by audio framework if coming from outside
1547 // of audioserver
1548 switch(cmdCode) {
1549 case EFFECT_CMD_ENABLE:
1550 case EFFECT_CMD_DISABLE:
1551 case EFFECT_CMD_SET_PARAM:
1552 case EFFECT_CMD_SET_PARAM_DEFERRED:
1553 case EFFECT_CMD_SET_PARAM_COMMIT:
1554 case EFFECT_CMD_GET_PARAM:
1555 break;
1556 default:
1557 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1558 break;
1559 }
1560 android_errorWriteLog(0x534e4554, "62019992");
1561 return BAD_VALUE;
1562 }
1563
Eric Laurent1ffc5852016-12-15 14:46:09 -08001564 if (cmdCode == EFFECT_CMD_ENABLE) {
1565 if (*replySize < sizeof(int)) {
1566 android_errorWriteLog(0x534e4554, "32095713");
1567 return BAD_VALUE;
1568 }
1569 *(int *)pReplyData = NO_ERROR;
1570 *replySize = sizeof(int);
1571 return enable();
1572 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1573 if (*replySize < sizeof(int)) {
1574 android_errorWriteLog(0x534e4554, "32095713");
1575 return BAD_VALUE;
1576 }
1577 *(int *)pReplyData = NO_ERROR;
1578 *replySize = sizeof(int);
1579 return disable();
1580 }
1581
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001582 AutoMutex _l(mLock);
1583 sp<EffectModule> effect = mEffect.promote();
1584 if (effect == 0 || mDisconnected) {
1585 return DEAD_OBJECT;
1586 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001587 // only get parameter command is permitted for applications not controlling the effect
1588 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1589 return INVALID_OPERATION;
1590 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001591 if (mClient == 0) {
1592 return INVALID_OPERATION;
1593 }
1594
1595 // handle commands that are not forwarded transparently to effect engine
1596 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001597 if (*replySize < sizeof(int)) {
1598 android_errorWriteLog(0x534e4554, "32095713");
1599 return BAD_VALUE;
1600 }
1601 *(int *)pReplyData = NO_ERROR;
1602 *replySize = sizeof(int);
1603
Eric Laurentca7cc822012-11-19 14:55:58 -08001604 // No need to trylock() here as this function is executed in the binder thread serving a
1605 // particular client process: no risk to block the whole media server process or mixer
1606 // threads if we are stuck here
1607 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001608 // keep local copy of index in case of client corruption b/32220769
1609 const uint32_t clientIndex = mCblk->clientIndex;
1610 const uint32_t serverIndex = mCblk->serverIndex;
1611 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1612 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001613 mCblk->serverIndex = 0;
1614 mCblk->clientIndex = 0;
1615 return BAD_VALUE;
1616 }
1617 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001618 effect_param_t *param = NULL;
1619 for (uint32_t index = serverIndex; index < clientIndex;) {
1620 int *p = (int *)(mBuffer + index);
1621 const int size = *p++;
1622 if (size < 0
1623 || size > EFFECT_PARAM_BUFFER_SIZE
1624 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001625 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001626 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001627 break;
1628 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001629
1630 // copy to local memory in case of client corruption b/32220769
1631 param = (effect_param_t *)realloc(param, size);
1632 if (param == NULL) {
1633 ALOGW("command(): out of memory");
1634 status = NO_MEMORY;
1635 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001636 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001637 memcpy(param, p, size);
1638
1639 int reply = 0;
1640 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001641 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001642 size,
1643 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001644 &rsize,
1645 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001646
1647 // verify shared memory: server index shouldn't change; client index can't go back.
1648 if (serverIndex != mCblk->serverIndex
1649 || clientIndex > mCblk->clientIndex) {
1650 android_errorWriteLog(0x534e4554, "32220769");
1651 status = BAD_VALUE;
1652 break;
1653 }
1654
Eric Laurentca7cc822012-11-19 14:55:58 -08001655 // stop at first error encountered
1656 if (ret != NO_ERROR) {
1657 status = ret;
1658 *(int *)pReplyData = reply;
1659 break;
1660 } else if (reply != NO_ERROR) {
1661 *(int *)pReplyData = reply;
1662 break;
1663 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001664 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001665 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001666 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 mCblk->serverIndex = 0;
1668 mCblk->clientIndex = 0;
1669 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001670 }
1671
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001672 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001673}
1674
1675void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1676{
1677 ALOGV("setControl %p control %d", this, hasControl);
1678
1679 mHasControl = hasControl;
1680 mEnabled = enabled;
1681
1682 if (signal && mEffectClient != 0) {
1683 mEffectClient->controlStatusChanged(hasControl);
1684 }
1685}
1686
1687void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1688 uint32_t cmdSize,
1689 void *pCmdData,
1690 uint32_t replySize,
1691 void *pReplyData)
1692{
1693 if (mEffectClient != 0) {
1694 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1695 }
1696}
1697
1698
1699
1700void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1701{
1702 if (mEffectClient != 0) {
1703 mEffectClient->enableStatusChanged(enabled);
1704 }
1705}
1706
1707status_t AudioFlinger::EffectHandle::onTransact(
1708 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1709{
1710 return BnEffect::onTransact(code, data, reply, flags);
1711}
1712
1713
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001714void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001715{
1716 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1717
Marco Nelissenb2208842014-02-07 14:00:50 -08001718 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001719 (mClient == 0) ? getpid_cached : mClient->pid(),
1720 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001721 mHasControl ? "yes" : "no",
1722 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001723 mCblk ? mCblk->clientIndex : 0,
1724 mCblk ? mCblk->serverIndex : 0
1725 );
1726
1727 if (locked) {
1728 mCblk->lock.unlock();
1729 }
1730}
1731
1732#undef LOG_TAG
1733#define LOG_TAG "AudioFlinger::EffectChain"
1734
1735AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001736 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001738 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001739 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001740{
1741 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1742 if (thread == NULL) {
1743 return;
1744 }
1745 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1746 thread->frameCount();
1747}
1748
1749AudioFlinger::EffectChain::~EffectChain()
1750{
Eric Laurentca7cc822012-11-19 14:55:58 -08001751}
1752
1753// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1754sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1755 effect_descriptor_t *descriptor)
1756{
1757 size_t size = mEffects.size();
1758
1759 for (size_t i = 0; i < size; i++) {
1760 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1761 return mEffects[i];
1762 }
1763 }
1764 return 0;
1765}
1766
1767// getEffectFromId_l() must be called with ThreadBase::mLock held
1768sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1769{
1770 size_t size = mEffects.size();
1771
1772 for (size_t i = 0; i < size; i++) {
1773 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1774 if (id == 0 || mEffects[i]->id() == id) {
1775 return mEffects[i];
1776 }
1777 }
1778 return 0;
1779}
1780
1781// getEffectFromType_l() must be called with ThreadBase::mLock held
1782sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1783 const effect_uuid_t *type)
1784{
1785 size_t size = mEffects.size();
1786
1787 for (size_t i = 0; i < size; i++) {
1788 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1789 return mEffects[i];
1790 }
1791 }
1792 return 0;
1793}
1794
1795void AudioFlinger::EffectChain::clearInputBuffer()
1796{
1797 Mutex::Autolock _l(mLock);
1798 sp<ThreadBase> thread = mThread.promote();
1799 if (thread == 0) {
1800 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1801 return;
1802 }
1803 clearInputBuffer_l(thread);
1804}
1805
1806// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001807void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001808{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001809 if (mInBuffer == NULL) {
1810 return;
1811 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001812 // TODO: This will change in the future, depending on multichannel
1813 // and sample format changes for effects.
1814 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1815 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001816
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001817 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001818 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1819 * std::min((uint32_t)FCC_2, thread->channelCount());
1820
Mikhail Naganov022b9952017-01-04 16:36:51 -08001821 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1822 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001823}
1824
1825// Must be called with EffectChain::mLock locked
1826void AudioFlinger::EffectChain::process_l()
1827{
1828 sp<ThreadBase> thread = mThread.promote();
1829 if (thread == 0) {
1830 ALOGW("process_l(): cannot promote mixer thread");
1831 return;
1832 }
1833 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1834 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001835 // never process effects when:
1836 // - on an OFFLOAD thread
1837 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001838 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1839 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 if (!isGlobalSession) {
1841 bool tracksOnSession = (trackCnt() != 0);
1842
1843 if (!tracksOnSession && mTailBufferCount == 0) {
1844 doProcess = false;
1845 }
1846
1847 if (activeTrackCnt() == 0) {
1848 // if no track is active and the effect tail has not been rendered,
1849 // the input buffer must be cleared here as the mixer process will not do it
1850 if (tracksOnSession || mTailBufferCount > 0) {
1851 clearInputBuffer_l(thread);
1852 if (mTailBufferCount > 0) {
1853 mTailBufferCount--;
1854 }
1855 }
1856 }
1857 }
1858
1859 size_t size = mEffects.size();
1860 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001861 // Only the input and output buffers of the chain can be external,
1862 // and 'update' / 'commit' do nothing for allocated buffers, thus
1863 // it's not needed to consider any other buffers here.
1864 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001865 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1866 mOutBuffer->update();
1867 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001868 for (size_t i = 0; i < size; i++) {
1869 mEffects[i]->process();
1870 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001871 mInBuffer->commit();
1872 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1873 mOutBuffer->commit();
1874 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001875 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001876 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001877 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001878 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1879 }
1880 if (doResetVolume) {
1881 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001882 }
1883}
1884
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001885// createEffect_l() must be called with ThreadBase::mLock held
1886status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1887 ThreadBase *thread,
1888 effect_descriptor_t *desc,
1889 int id,
1890 audio_session_t sessionId,
1891 bool pinned)
1892{
1893 Mutex::Autolock _l(mLock);
1894 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1895 status_t lStatus = effect->status();
1896 if (lStatus == NO_ERROR) {
1897 lStatus = addEffect_ll(effect);
1898 }
1899 if (lStatus != NO_ERROR) {
1900 effect.clear();
1901 }
1902 return lStatus;
1903}
1904
1905// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001906status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1907{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001908 Mutex::Autolock _l(mLock);
1909 return addEffect_ll(effect);
1910}
1911// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1912status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1913{
Eric Laurentca7cc822012-11-19 14:55:58 -08001914 effect_descriptor_t desc = effect->desc();
1915 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1916
Eric Laurentca7cc822012-11-19 14:55:58 -08001917 effect->setChain(this);
1918 sp<ThreadBase> thread = mThread.promote();
1919 if (thread == 0) {
1920 return NO_INIT;
1921 }
1922 effect->setThread(thread);
1923
1924 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1925 // Auxiliary effects are inserted at the beginning of mEffects vector as
1926 // they are processed first and accumulated in chain input buffer
1927 mEffects.insertAt(effect, 0);
1928
1929 // the input buffer for auxiliary effect contains mono samples in
1930 // 32 bit format. This is to avoid saturation in AudoMixer
1931 // accumulation stage. Saturation is done in EffectModule::process() before
1932 // calling the process in effect engine
1933 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001934 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001935#ifdef FLOAT_EFFECT_CHAIN
1936 status_t result = EffectBufferHalInterface::allocate(
1937 numSamples * sizeof(float), &halBuffer);
1938#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001939 status_t result = EffectBufferHalInterface::allocate(
1940 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001941#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001942 if (result != OK) return result;
1943 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001944 // auxiliary effects output samples to chain input buffer for further processing
1945 // by insert effects
1946 effect->setOutBuffer(mInBuffer);
1947 } else {
1948 // Insert effects are inserted at the end of mEffects vector as they are processed
1949 // after track and auxiliary effects.
1950 // Insert effect order as a function of indicated preference:
1951 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1952 // another effect is present
1953 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1954 // last effect claiming first position
1955 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1956 // first effect claiming last position
1957 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1958 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1959 // already present
1960
1961 size_t size = mEffects.size();
1962 size_t idx_insert = size;
1963 ssize_t idx_insert_first = -1;
1964 ssize_t idx_insert_last = -1;
1965
1966 for (size_t i = 0; i < size; i++) {
1967 effect_descriptor_t d = mEffects[i]->desc();
1968 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1969 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1970 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1971 // check invalid effect chaining combinations
1972 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1973 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1974 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1975 desc.name, d.name);
1976 return INVALID_OPERATION;
1977 }
1978 // remember position of first insert effect and by default
1979 // select this as insert position for new effect
1980 if (idx_insert == size) {
1981 idx_insert = i;
1982 }
1983 // remember position of last insert effect claiming
1984 // first position
1985 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1986 idx_insert_first = i;
1987 }
1988 // remember position of first insert effect claiming
1989 // last position
1990 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1991 idx_insert_last == -1) {
1992 idx_insert_last = i;
1993 }
1994 }
1995 }
1996
1997 // modify idx_insert from first position if needed
1998 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1999 if (idx_insert_last != -1) {
2000 idx_insert = idx_insert_last;
2001 } else {
2002 idx_insert = size;
2003 }
2004 } else {
2005 if (idx_insert_first != -1) {
2006 idx_insert = idx_insert_first + 1;
2007 }
2008 }
2009
2010 // always read samples from chain input buffer
2011 effect->setInBuffer(mInBuffer);
2012
2013 // if last effect in the chain, output samples to chain
2014 // output buffer, otherwise to chain input buffer
2015 if (idx_insert == size) {
2016 if (idx_insert != 0) {
2017 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2018 mEffects[idx_insert-1]->configure();
2019 }
2020 effect->setOutBuffer(mOutBuffer);
2021 } else {
2022 effect->setOutBuffer(mInBuffer);
2023 }
2024 mEffects.insertAt(effect, idx_insert);
2025
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002026 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002027 idx_insert);
2028 }
2029 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002030
Eric Laurentca7cc822012-11-19 14:55:58 -08002031 return NO_ERROR;
2032}
2033
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002034// removeEffect_l() must be called with ThreadBase::mLock held
2035size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2036 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002037{
2038 Mutex::Autolock _l(mLock);
2039 size_t size = mEffects.size();
2040 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2041
2042 for (size_t i = 0; i < size; i++) {
2043 if (effect == mEffects[i]) {
2044 // calling stop here will remove pre-processing effect from the audio HAL.
2045 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2046 // the middle of a read from audio HAL
2047 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2048 mEffects[i]->state() == EffectModule::STOPPING) {
2049 mEffects[i]->stop();
2050 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002051 if (release) {
2052 mEffects[i]->release_l();
2053 }
2054
Mikhail Naganov022b9952017-01-04 16:36:51 -08002055 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002056 if (i == size - 1 && i != 0) {
2057 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2058 mEffects[i - 1]->configure();
2059 }
2060 }
2061 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002062 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002063 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002064
Eric Laurentca7cc822012-11-19 14:55:58 -08002065 break;
2066 }
2067 }
2068
2069 return mEffects.size();
2070}
2071
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002072// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002073void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2074{
2075 size_t size = mEffects.size();
2076 for (size_t i = 0; i < size; i++) {
2077 mEffects[i]->setDevice(device);
2078 }
2079}
2080
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002081// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002082void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2083{
2084 size_t size = mEffects.size();
2085 for (size_t i = 0; i < size; i++) {
2086 mEffects[i]->setMode(mode);
2087 }
2088}
2089
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002090// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002091void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2092{
2093 size_t size = mEffects.size();
2094 for (size_t i = 0; i < size; i++) {
2095 mEffects[i]->setAudioSource(source);
2096 }
2097}
2098
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002099// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002100bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002101{
2102 uint32_t newLeft = *left;
2103 uint32_t newRight = *right;
2104 bool hasControl = false;
2105 int ctrlIdx = -1;
2106 size_t size = mEffects.size();
2107
2108 // first update volume controller
2109 for (size_t i = size; i > 0; i--) {
2110 if (mEffects[i - 1]->isProcessEnabled() &&
2111 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2112 ctrlIdx = i - 1;
2113 hasControl = true;
2114 break;
2115 }
2116 }
2117
Eric Laurentfa1e1232016-08-02 19:01:49 -07002118 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002119 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002120 if (hasControl) {
2121 *left = mNewLeftVolume;
2122 *right = mNewRightVolume;
2123 }
2124 return hasControl;
2125 }
2126
2127 mVolumeCtrlIdx = ctrlIdx;
2128 mLeftVolume = newLeft;
2129 mRightVolume = newRight;
2130
2131 // second get volume update from volume controller
2132 if (ctrlIdx >= 0) {
2133 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2134 mNewLeftVolume = newLeft;
2135 mNewRightVolume = newRight;
2136 }
2137 // then indicate volume to all other effects in chain.
2138 // Pass altered volume to effects before volume controller
2139 // and requested volume to effects after controller
2140 uint32_t lVol = newLeft;
2141 uint32_t rVol = newRight;
2142
2143 for (size_t i = 0; i < size; i++) {
2144 if ((int)i == ctrlIdx) {
2145 continue;
2146 }
2147 // this also works for ctrlIdx == -1 when there is no volume controller
2148 if ((int)i > ctrlIdx) {
2149 lVol = *left;
2150 rVol = *right;
2151 }
2152 mEffects[i]->setVolume(&lVol, &rVol, false);
2153 }
2154 *left = newLeft;
2155 *right = newRight;
2156
2157 return hasControl;
2158}
2159
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002160// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002161void AudioFlinger::EffectChain::resetVolume_l()
2162{
Eric Laurente7449bf2016-08-03 18:44:07 -07002163 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2164 uint32_t left = mLeftVolume;
2165 uint32_t right = mRightVolume;
2166 (void)setVolume_l(&left, &right, true);
2167 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002168}
2169
Eric Laurent1b928682014-10-02 19:41:47 -07002170void AudioFlinger::EffectChain::syncHalEffectsState()
2171{
2172 Mutex::Autolock _l(mLock);
2173 for (size_t i = 0; i < mEffects.size(); i++) {
2174 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2175 mEffects[i]->state() == EffectModule::STOPPING) {
2176 mEffects[i]->addEffectToHal_l();
2177 }
2178 }
2179}
2180
Eric Laurentca7cc822012-11-19 14:55:58 -08002181void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2182{
2183 const size_t SIZE = 256;
2184 char buffer[SIZE];
2185 String8 result;
2186
Marco Nelissenb2208842014-02-07 14:00:50 -08002187 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002188 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002189 result.append(buffer);
2190
Marco Nelissenb2208842014-02-07 14:00:50 -08002191 if (numEffects) {
2192 bool locked = AudioFlinger::dumpTryLock(mLock);
2193 // failed to lock - AudioFlinger is probably deadlocked
2194 if (!locked) {
2195 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002196 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002197
Andy Hungbded9c82017-11-30 18:47:35 -08002198 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2199 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2200 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2201 (int)inBufferStr.size(), "In buffer ",
2202 (int)outBufferStr.size(), "Out buffer ");
2203 result.appendFormat("\t%s %s %d\n",
2204 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002205 write(fd, result.string(), result.size());
2206
2207 for (size_t i = 0; i < numEffects; ++i) {
2208 sp<EffectModule> effect = mEffects[i];
2209 if (effect != 0) {
2210 effect->dump(fd, args);
2211 }
2212 }
2213
2214 if (locked) {
2215 mLock.unlock();
2216 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002217 }
2218}
2219
2220// must be called with ThreadBase::mLock held
2221void AudioFlinger::EffectChain::setEffectSuspended_l(
2222 const effect_uuid_t *type, bool suspend)
2223{
2224 sp<SuspendedEffectDesc> desc;
2225 // use effect type UUID timelow as key as there is no real risk of identical
2226 // timeLow fields among effect type UUIDs.
2227 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2228 if (suspend) {
2229 if (index >= 0) {
2230 desc = mSuspendedEffects.valueAt(index);
2231 } else {
2232 desc = new SuspendedEffectDesc();
2233 desc->mType = *type;
2234 mSuspendedEffects.add(type->timeLow, desc);
2235 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2236 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002237
Eric Laurentca7cc822012-11-19 14:55:58 -08002238 if (desc->mRefCount++ == 0) {
2239 sp<EffectModule> effect = getEffectIfEnabled(type);
2240 if (effect != 0) {
2241 desc->mEffect = effect;
2242 effect->setSuspended(true);
2243 effect->setEnabled(false);
2244 }
2245 }
2246 } else {
2247 if (index < 0) {
2248 return;
2249 }
2250 desc = mSuspendedEffects.valueAt(index);
2251 if (desc->mRefCount <= 0) {
2252 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002253 desc->mRefCount = 0;
2254 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002255 }
2256 if (--desc->mRefCount == 0) {
2257 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2258 if (desc->mEffect != 0) {
2259 sp<EffectModule> effect = desc->mEffect.promote();
2260 if (effect != 0) {
2261 effect->setSuspended(false);
2262 effect->lock();
2263 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002264 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002265 effect->setEnabled_l(handle->enabled());
2266 }
2267 effect->unlock();
2268 }
2269 desc->mEffect.clear();
2270 }
2271 mSuspendedEffects.removeItemsAt(index);
2272 }
2273 }
2274}
2275
2276// must be called with ThreadBase::mLock held
2277void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2278{
2279 sp<SuspendedEffectDesc> desc;
2280
2281 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2282 if (suspend) {
2283 if (index >= 0) {
2284 desc = mSuspendedEffects.valueAt(index);
2285 } else {
2286 desc = new SuspendedEffectDesc();
2287 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2288 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2289 }
2290 if (desc->mRefCount++ == 0) {
2291 Vector< sp<EffectModule> > effects;
2292 getSuspendEligibleEffects(effects);
2293 for (size_t i = 0; i < effects.size(); i++) {
2294 setEffectSuspended_l(&effects[i]->desc().type, true);
2295 }
2296 }
2297 } else {
2298 if (index < 0) {
2299 return;
2300 }
2301 desc = mSuspendedEffects.valueAt(index);
2302 if (desc->mRefCount <= 0) {
2303 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2304 desc->mRefCount = 1;
2305 }
2306 if (--desc->mRefCount == 0) {
2307 Vector<const effect_uuid_t *> types;
2308 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2309 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2310 continue;
2311 }
2312 types.add(&mSuspendedEffects.valueAt(i)->mType);
2313 }
2314 for (size_t i = 0; i < types.size(); i++) {
2315 setEffectSuspended_l(types[i], false);
2316 }
2317 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2318 mSuspendedEffects.keyAt(index));
2319 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2320 }
2321 }
2322}
2323
2324
2325// The volume effect is used for automated tests only
2326#ifndef OPENSL_ES_H_
2327static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2328 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2329const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2330#endif //OPENSL_ES_H_
2331
Eric Laurentd8365c52017-07-16 15:27:05 -07002332/* static */
2333bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2334{
2335 // Only NS and AEC are suspended when BtNRec is off
2336 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2337 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2338 return true;
2339 }
2340 return false;
2341}
2342
Eric Laurentca7cc822012-11-19 14:55:58 -08002343bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2344{
2345 // auxiliary effects and visualizer are never suspended on output mix
2346 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2347 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2348 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2349 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2350 return false;
2351 }
2352 return true;
2353}
2354
2355void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2356 Vector< sp<AudioFlinger::EffectModule> > &effects)
2357{
2358 effects.clear();
2359 for (size_t i = 0; i < mEffects.size(); i++) {
2360 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2361 effects.add(mEffects[i]);
2362 }
2363 }
2364}
2365
2366sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2367 const effect_uuid_t *type)
2368{
2369 sp<EffectModule> effect = getEffectFromType_l(type);
2370 return effect != 0 && effect->isEnabled() ? effect : 0;
2371}
2372
2373void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2374 bool enabled)
2375{
2376 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2377 if (enabled) {
2378 if (index < 0) {
2379 // if the effect is not suspend check if all effects are suspended
2380 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2381 if (index < 0) {
2382 return;
2383 }
2384 if (!isEffectEligibleForSuspend(effect->desc())) {
2385 return;
2386 }
2387 setEffectSuspended_l(&effect->desc().type, enabled);
2388 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2389 if (index < 0) {
2390 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2391 return;
2392 }
2393 }
2394 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2395 effect->desc().type.timeLow);
2396 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002397 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002398 if (desc->mEffect == 0) {
2399 desc->mEffect = effect;
2400 effect->setEnabled(false);
2401 effect->setSuspended(true);
2402 }
2403 } else {
2404 if (index < 0) {
2405 return;
2406 }
2407 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2408 effect->desc().type.timeLow);
2409 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2410 desc->mEffect.clear();
2411 effect->setSuspended(false);
2412 }
2413}
2414
Eric Laurent5baf2af2013-09-12 17:37:00 -07002415bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002416{
2417 Mutex::Autolock _l(mLock);
2418 size_t size = mEffects.size();
2419 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002420 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002421 return true;
2422 }
2423 }
2424 return false;
2425}
2426
Eric Laurentaaa44472014-09-12 17:41:50 -07002427void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2428{
2429 Mutex::Autolock _l(mLock);
2430 mThread = thread;
2431 for (size_t i = 0; i < mEffects.size(); i++) {
2432 mEffects[i]->setThread(thread);
2433 }
2434}
2435
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002436void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2437{
2438 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2439 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2440 }
2441 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2442 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2443 }
2444}
2445
2446void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2447{
2448 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2449 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2450 }
2451 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2452 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2453 }
2454}
2455
2456bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002457{
2458 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002459 for (const auto &effect : mEffects) {
2460 if (effect->isProcessImplemented()) {
2461 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002462 }
2463 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002464 // Allow effects without processing.
2465 return true;
2466}
2467
2468bool AudioFlinger::EffectChain::isFastCompatible() const
2469{
2470 Mutex::Autolock _l(mLock);
2471 for (const auto &effect : mEffects) {
2472 if (effect->isProcessImplemented()
2473 && effect->isImplementationSoftware()) {
2474 return false;
2475 }
2476 }
2477 // Allow effects without processing or hw accelerated effects.
2478 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002479}
2480
2481// isCompatibleWithThread_l() must be called with thread->mLock held
2482bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2483{
2484 Mutex::Autolock _l(mLock);
2485 for (size_t i = 0; i < mEffects.size(); i++) {
2486 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2487 return false;
2488 }
2489 }
2490 return true;
2491}
2492
Glenn Kasten63238ef2015-03-02 15:50:29 -08002493} // namespace android