blob: ef6e223b828fe174e8db42d48063c17ecd509cd1 [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),
70 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080071 mStatus(NO_INIT), mState(IDLE),
72 // mMaxDisableWaitCnt is set by configure() and not used before then
73 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070074 mSuspended(false),
75 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070076#ifdef FLOAT_EFFECT_CHAIN
77 , mSupportsFloat(false)
78#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080079{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080080 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080081 int lStatus;
82
83 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070084 mStatus = -ENODEV;
85 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070086 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070087 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070088 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070089 mStatus = effectsFactory->createEffect(
90 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
91 }
92 }
Eric Laurentca7cc822012-11-19 14:55:58 -080093
94 if (mStatus != NO_ERROR) {
95 return;
96 }
97 lStatus = init();
98 if (lStatus < 0) {
99 mStatus = lStatus;
100 goto Error;
101 }
102
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800103 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700104 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800105
Eric Laurentca7cc822012-11-19 14:55:58 -0800106 return;
107Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700108 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800109 ALOGV("Constructor Error %d", mStatus);
110}
111
112AudioFlinger::EffectModule::~EffectModule()
113{
114 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700115 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700116 char uuidStr[64];
117 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
118 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
119 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800120 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800121 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800122
Eric Laurentca7cc822012-11-19 14:55:58 -0800123}
124
125status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
126{
127 status_t status;
128
129 Mutex::Autolock _l(mLock);
130 int priority = handle->priority();
131 size_t size = mHandles.size();
132 EffectHandle *controlHandle = NULL;
133 size_t i;
134 for (i = 0; i < size; i++) {
135 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800136 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800137 continue;
138 }
139 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700140 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800141 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700142 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800143 if (h->priority() <= priority) {
144 break;
145 }
146 }
147 // if inserted in first place, move effect control from previous owner to this handle
148 if (i == 0) {
149 bool enabled = false;
150 if (controlHandle != NULL) {
151 enabled = controlHandle->enabled();
152 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
153 }
154 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
155 status = NO_ERROR;
156 } else {
157 status = ALREADY_EXISTS;
158 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700159 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800160 mHandles.insertAt(handle, i);
161 return status;
162}
163
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800164ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800165{
166 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800167 return removeHandle_l(handle);
168}
169
170ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
171{
Eric Laurentca7cc822012-11-19 14:55:58 -0800172 size_t size = mHandles.size();
173 size_t i;
174 for (i = 0; i < size; i++) {
175 if (mHandles[i] == handle) {
176 break;
177 }
178 }
179 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800180 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
181 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800182 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800183 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800184
185 mHandles.removeAt(i);
186 // if removed from first place, move effect control from this handle to next in line
187 if (i == 0) {
188 EffectHandle *h = controlHandle_l();
189 if (h != NULL) {
190 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
191 }
192 }
193
194 // Prevent calls to process() and other functions on effect interface from now on.
195 // The effect engine will be released by the destructor when the last strong reference on
196 // this object is released which can happen after next process is called.
197 if (mHandles.size() == 0 && !mPinned) {
198 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800199 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800200 }
201
202 return mHandles.size();
203}
204
205// must be called with EffectModule::mLock held
206AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
207{
208 // the first valid handle in the list has control over the module
209 for (size_t i = 0; i < mHandles.size(); i++) {
210 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800211 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 return h;
213 }
214 }
215
216 return NULL;
217}
218
Eric Laurentf10c7092016-12-06 17:09:56 -0800219// unsafe method called when the effect parent thread has been destroyed
220ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
221{
222 ALOGV("disconnect() %p handle %p", this, handle);
223 Mutex::Autolock _l(mLock);
224 ssize_t numHandles = removeHandle_l(handle);
225 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
226 AudioSystem::unregisterEffect(mId);
227 sp<AudioFlinger> af = mAudioFlinger.promote();
228 if (af != 0) {
229 mLock.unlock();
230 af->updateOrphanEffectChains(this);
231 mLock.lock();
232 }
233 }
234 return numHandles;
235}
236
Eric Laurentfa1e1232016-08-02 19:01:49 -0700237bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800238 Mutex::Autolock _l(mLock);
239
Eric Laurentfa1e1232016-08-02 19:01:49 -0700240 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800241 switch (mState) {
242 case RESTART:
243 reset_l();
244 // FALL THROUGH
245
246 case STARTING:
247 // clear auxiliary effect input buffer for next accumulation
248 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
249 memset(mConfig.inputCfg.buffer.raw,
250 0,
251 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
252 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700253 if (start_l() == NO_ERROR) {
254 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700255 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700256 } else {
257 mState = IDLE;
258 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800259 break;
260 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700261 if (stop_l() == NO_ERROR) {
262 mDisableWaitCnt = mMaxDisableWaitCnt;
263 } else {
264 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
265 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800266 mState = STOPPED;
267 break;
268 case STOPPED:
269 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
270 // turn off sequence.
271 if (--mDisableWaitCnt == 0) {
272 reset_l();
273 mState = IDLE;
274 }
275 break;
276 default: //IDLE , ACTIVE, DESTROYED
277 break;
278 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700279
280 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800281}
282
283void AudioFlinger::EffectModule::process()
284{
285 Mutex::Autolock _l(mLock);
286
Mikhail Naganov022b9952017-01-04 16:36:51 -0800287 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800288 return;
289 }
290
rago94a1ee82017-07-21 15:11:02 -0700291 // TODO: Implement multichannel effects; here outChannelCount == FCC_2 == 2
292 const uint32_t inChannelCount =
293 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
294 const uint32_t outChannelCount =
295 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
296 const bool auxType =
297 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
298
Eric Laurentca7cc822012-11-19 14:55:58 -0800299 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700300 int ret;
301 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700302 if (auxType) {
303 // We overwrite the aux input buffer here and clear after processing.
304 // Note that aux input buffers are format q4_27.
305#ifdef FLOAT_EFFECT_CHAIN
306 if (mSupportsFloat) {
307 // Do in-place float conversion for auxiliary effect input buffer.
308 static_assert(sizeof(float) <= sizeof(int32_t),
309 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
310
311 const int32_t * const p32 = mConfig.inputCfg.buffer.s32;
312 float * const pFloat = mConfig.inputCfg.buffer.f32;
313 memcpy_to_float_from_q4_27(pFloat, p32, mConfig.inputCfg.buffer.frameCount);
314 } else {
Andy Hung5effdf62017-11-27 13:51:40 -0800315 memcpy_to_i16_from_q4_27(mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700316 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800317 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700318 }
319#else
Andy Hung5effdf62017-11-27 13:51:40 -0800320 memcpy_to_i16_from_q4_27(mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700321 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800322 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700323#endif
324 }
325#ifdef FLOAT_EFFECT_CHAIN
326 if (mSupportsFloat) {
327 ret = mEffectInterface->process();
328 } else {
329 { // convert input to int16_t as effect doesn't support float.
330 if (!auxType) {
Andy Hungbded9c82017-11-30 18:47:35 -0800331 if (mInConversionBuffer.get() == nullptr) {
332 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
rago94a1ee82017-07-21 15:11:02 -0700333 goto data_bypass;
334 }
335 const float * const pIn = mInBuffer->audioBuffer()->f32;
Andy Hungbded9c82017-11-30 18:47:35 -0800336 int16_t * const pIn16 = mInConversionBuffer->audioBuffer()->s16;
rago94a1ee82017-07-21 15:11:02 -0700337 memcpy_to_i16_from_float(
338 pIn16, pIn, inChannelCount * mConfig.inputCfg.buffer.frameCount);
339 }
340 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungbded9c82017-11-30 18:47:35 -0800341 if (mOutConversionBuffer.get() == nullptr) {
342 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
rago94a1ee82017-07-21 15:11:02 -0700343 goto data_bypass;
344 }
Andy Hungbded9c82017-11-30 18:47:35 -0800345 int16_t * const pOut16 = mOutConversionBuffer->audioBuffer()->s16;
rago94a1ee82017-07-21 15:11:02 -0700346 const float * const pOut = mOutBuffer->audioBuffer()->f32;
347 memcpy_to_i16_from_float(
348 pOut16,
349 pOut,
350 outChannelCount * mConfig.outputCfg.buffer.frameCount);
351 }
352 }
353
354 ret = mEffectInterface->process();
355
356 { // convert output back to float.
Andy Hungbded9c82017-11-30 18:47:35 -0800357 const int16_t * const pOut16 = mOutConversionBuffer->audioBuffer()->s16;
rago94a1ee82017-07-21 15:11:02 -0700358 float * const pOut = mOutBuffer->audioBuffer()->f32;
359 memcpy_to_float_from_i16(
360 pOut, pOut16, outChannelCount * mConfig.outputCfg.buffer.frameCount);
361 }
362 }
363#else
Mikhail Naganov022b9952017-01-04 16:36:51 -0800364 ret = mEffectInterface->process();
rago94a1ee82017-07-21 15:11:02 -0700365#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700366 } else {
rago94a1ee82017-07-21 15:11:02 -0700367#ifdef FLOAT_EFFECT_CHAIN
368 data_bypass:
369#endif
370 if (!auxType /* aux effects do not require data bypass */
371 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw
372 && inChannelCount == outChannelCount) {
373 const size_t sampleCount = std::min(
374 mConfig.inputCfg.buffer.frameCount,
375 mConfig.outputCfg.buffer.frameCount) * outChannelCount;
376
377#ifdef FLOAT_EFFECT_CHAIN
378 const float * const in = mConfig.inputCfg.buffer.f32;
379 float * const out = mConfig.outputCfg.buffer.f32;
Eric Laurentca7cc822012-11-19 14:55:58 -0800380
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700381 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
rago94a1ee82017-07-21 15:11:02 -0700382 accumulate_float(out, in, sampleCount);
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700383 } else {
rago94a1ee82017-07-21 15:11:02 -0700384 memcpy(mConfig.outputCfg.buffer.f32, mConfig.inputCfg.buffer.f32,
385 sampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700386 }
rago94a1ee82017-07-21 15:11:02 -0700387
388#else
389 const int16_t * const in = mConfig.inputCfg.buffer.s16;
390 int16_t * const out = mConfig.outputCfg.buffer.s16;
391
392 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
393 accumulate_i16(out, in, sampleCount);
394 } else {
395 memcpy(mConfig.outputCfg.buffer.s16, mConfig.inputCfg.buffer.s16,
396 sampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
397 }
398#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700399 }
400 ret = -ENODATA;
401 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800402 // force transition to IDLE state when engine is ready
403 if (mState == STOPPED && ret == -ENODATA) {
404 mDisableWaitCnt = 1;
405 }
406
407 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700408 if (auxType) {
409 // input always q4_27 regardless of FLOAT_EFFECT_CHAIN.
410 const size_t size =
411 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
412 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800413 }
414 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700415 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800416 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
417 // If an insert effect is idle and input buffer is different from output buffer,
418 // accumulate input onto output
419 sp<EffectChain> chain = mChain.promote();
rago94a1ee82017-07-21 15:11:02 -0700420 if (chain != 0
421 && chain->activeTrackCnt() != 0
422 && inChannelCount == outChannelCount) {
423 const size_t sampleCount = std::min(
424 mConfig.inputCfg.buffer.frameCount,
425 mConfig.outputCfg.buffer.frameCount) * outChannelCount;
426#ifdef FLOAT_EFFECT_CHAIN
427 const float * const in = mConfig.inputCfg.buffer.f32;
428 float * const out = mConfig.outputCfg.buffer.f32;
429 accumulate_float(out, in, sampleCount);
430#else
431 const int16_t * const in = mConfig.inputCfg.buffer.s16;
432 int16_t * const out = mConfig.outputCfg.buffer.s16;
433 accumulate_i16(out, in, sampleCount);
434#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800435 }
436 }
437}
438
439void AudioFlinger::EffectModule::reset_l()
440{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700441 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800442 return;
443 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700444 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800445}
446
447status_t AudioFlinger::EffectModule::configure()
448{
rago94a1ee82017-07-21 15:11:02 -0700449 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700450 status_t status;
451 sp<ThreadBase> thread;
452 uint32_t size;
453 audio_channel_mask_t channelMask;
454
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700455 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700456 status = NO_INIT;
457 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800458 }
459
Eric Laurentd0ebb532013-04-02 16:41:41 -0700460 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800461 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700462 status = DEAD_OBJECT;
463 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800464 }
465
466 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700467 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700468 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800469
470 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
471 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900472 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
473 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800474 } else {
475 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700476 // TODO: Update this logic when multichannel effects are implemented.
477 // For offloaded tracks consider mono output as stereo for proper effect initialization
478 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
479 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
480 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
481 ALOGV("Overriding effect input and output as STEREO");
482 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800483 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700484
rago94a1ee82017-07-21 15:11:02 -0700485 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
486 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800487 mConfig.inputCfg.samplingRate = thread->sampleRate();
488 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
489 mConfig.inputCfg.bufferProvider.cookie = NULL;
490 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
491 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
492 mConfig.outputCfg.bufferProvider.cookie = NULL;
493 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
494 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
495 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
496 // Insert effect:
497 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
498 // always overwrites output buffer: input buffer == output buffer
499 // - in other sessions:
500 // last effect in the chain accumulates in output buffer: input buffer != output buffer
501 // other effect: overwrites output buffer: input buffer == output buffer
502 // Auxiliary effect:
503 // accumulates in output buffer: input buffer != output buffer
504 // Therefore: accumulate <=> input buffer != output buffer
505 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
506 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
507 } else {
508 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
509 }
510 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
511 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
512 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
513 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
514
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700515 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800516 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
517
518 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700519 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700520 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
521 sizeof(effect_config_t),
522 &mConfig,
523 &size,
524 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700525 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800526 status = cmdStatus;
rago94a1ee82017-07-21 15:11:02 -0700527#ifdef FLOAT_EFFECT_CHAIN
528 mSupportsFloat = true;
529#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800530 }
rago94a1ee82017-07-21 15:11:02 -0700531#ifdef FLOAT_EFFECT_CHAIN
532 else {
533 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
534 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
535 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
536 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
537 sizeof(effect_config_t),
538 &mConfig,
539 &size,
540 &cmdStatus);
541 if (status == NO_ERROR) {
542 status = cmdStatus;
543 mSupportsFloat = false;
544 ALOGVV("config worked with 16 bit");
545 } else {
546 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800547 }
rago94a1ee82017-07-21 15:11:02 -0700548 }
549#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800550
rago94a1ee82017-07-21 15:11:02 -0700551 if (status == NO_ERROR) {
552 // Establish Buffer strategy
553 setInBuffer(mInBuffer);
554 setOutBuffer(mOutBuffer);
555
556 // Update visualizer latency
557 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
558 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
559 effect_param_t *p = (effect_param_t *)buf32;
560
561 p->psize = sizeof(uint32_t);
562 p->vsize = sizeof(uint32_t);
563 size = sizeof(int);
564 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
565
566 uint32_t latency = 0;
567 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
568 if (pbt != NULL) {
569 latency = pbt->latency_l();
570 }
571
572 *((int32_t *)p->data + 1)= latency;
573 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
574 sizeof(effect_param_t) + 8,
575 &buf32,
576 &size,
577 &cmdStatus);
578 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 }
580
581 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
582 (1000 * mConfig.outputCfg.buffer.frameCount);
583
Eric Laurentd0ebb532013-04-02 16:41:41 -0700584exit:
585 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700586 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800587 return status;
588}
589
590status_t AudioFlinger::EffectModule::init()
591{
592 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700593 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800594 return NO_INIT;
595 }
596 status_t cmdStatus;
597 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700598 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
599 0,
600 NULL,
601 &size,
602 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800603 if (status == 0) {
604 status = cmdStatus;
605 }
606 return status;
607}
608
Eric Laurent1b928682014-10-02 19:41:47 -0700609void AudioFlinger::EffectModule::addEffectToHal_l()
610{
611 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
612 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
613 sp<ThreadBase> thread = mThread.promote();
614 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700615 sp<StreamHalInterface> stream = thread->stream();
616 if (stream != 0) {
617 status_t result = stream->addEffect(mEffectInterface);
618 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700619 }
620 }
621 }
622}
623
Eric Laurentfa1e1232016-08-02 19:01:49 -0700624// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800625status_t AudioFlinger::EffectModule::start()
626{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700627 sp<EffectChain> chain;
628 status_t status;
629 {
630 Mutex::Autolock _l(mLock);
631 status = start_l();
632 if (status == NO_ERROR) {
633 chain = mChain.promote();
634 }
635 }
636 if (chain != 0) {
637 chain->resetVolume_l();
638 }
639 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800640}
641
642status_t AudioFlinger::EffectModule::start_l()
643{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700644 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800645 return NO_INIT;
646 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700647 if (mStatus != NO_ERROR) {
648 return mStatus;
649 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800650 status_t cmdStatus;
651 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700652 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
653 0,
654 NULL,
655 &size,
656 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800657 if (status == 0) {
658 status = cmdStatus;
659 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700660 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700661 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800662 }
663 return status;
664}
665
666status_t AudioFlinger::EffectModule::stop()
667{
668 Mutex::Autolock _l(mLock);
669 return stop_l();
670}
671
672status_t AudioFlinger::EffectModule::stop_l()
673{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700674 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800675 return NO_INIT;
676 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700677 if (mStatus != NO_ERROR) {
678 return mStatus;
679 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800680 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800681 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700682 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
683 0,
684 NULL,
685 &size,
686 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800687 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800688 status = cmdStatus;
689 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800690 if (status == NO_ERROR) {
691 status = remove_effect_from_hal_l();
692 }
693 return status;
694}
695
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800696// must be called with EffectChain::mLock held
697void AudioFlinger::EffectModule::release_l()
698{
699 if (mEffectInterface != 0) {
700 remove_effect_from_hal_l();
701 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800702 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800703 mEffectInterface.clear();
704 }
705}
706
Eric Laurentbfb1b832013-01-07 09:53:42 -0800707status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
708{
709 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
710 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800711 sp<ThreadBase> thread = mThread.promote();
712 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700713 sp<StreamHalInterface> stream = thread->stream();
714 if (stream != 0) {
715 status_t result = stream->removeEffect(mEffectInterface);
716 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800717 }
718 }
719 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800720 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800721}
722
Andy Hunge4a1d912016-08-17 14:11:13 -0700723// round up delta valid if value and divisor are positive.
724template <typename T>
725static T roundUpDelta(const T &value, const T &divisor) {
726 T remainder = value % divisor;
727 return remainder == 0 ? 0 : divisor - remainder;
728}
729
Eric Laurentca7cc822012-11-19 14:55:58 -0800730status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
731 uint32_t cmdSize,
732 void *pCmdData,
733 uint32_t *replySize,
734 void *pReplyData)
735{
736 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700737 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800738
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700739 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800740 return NO_INIT;
741 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700742 if (mStatus != NO_ERROR) {
743 return mStatus;
744 }
Andy Hung110bc952016-06-20 15:22:52 -0700745 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700746 (sizeof(effect_param_t) > cmdSize ||
747 ((effect_param_t *)pCmdData)->psize > cmdSize
748 - sizeof(effect_param_t))) {
749 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800750 android_errorWriteLog(0x534e4554, "33003822");
751 return -EINVAL;
752 }
753 if (cmdCode == EFFECT_CMD_GET_PARAM &&
754 (*replySize < sizeof(effect_param_t) ||
755 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
756 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700757 return -EINVAL;
758 }
ragoe2759072016-11-22 18:02:48 -0800759 if (cmdCode == EFFECT_CMD_GET_PARAM &&
760 (sizeof(effect_param_t) > *replySize
761 || ((effect_param_t *)pCmdData)->psize > *replySize
762 - sizeof(effect_param_t)
763 || ((effect_param_t *)pCmdData)->vsize > *replySize
764 - sizeof(effect_param_t)
765 - ((effect_param_t *)pCmdData)->psize
766 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
767 *replySize
768 - sizeof(effect_param_t)
769 - ((effect_param_t *)pCmdData)->psize
770 - ((effect_param_t *)pCmdData)->vsize)) {
771 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
772 android_errorWriteLog(0x534e4554, "32705438");
773 return -EINVAL;
774 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700775 if ((cmdCode == EFFECT_CMD_SET_PARAM
776 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
777 (sizeof(effect_param_t) > cmdSize
778 || ((effect_param_t *)pCmdData)->psize > cmdSize
779 - sizeof(effect_param_t)
780 || ((effect_param_t *)pCmdData)->vsize > cmdSize
781 - sizeof(effect_param_t)
782 - ((effect_param_t *)pCmdData)->psize
783 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
784 cmdSize
785 - sizeof(effect_param_t)
786 - ((effect_param_t *)pCmdData)->psize
787 - ((effect_param_t *)pCmdData)->vsize)) {
788 android_errorWriteLog(0x534e4554, "30204301");
789 return -EINVAL;
790 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700791 status_t status = mEffectInterface->command(cmdCode,
792 cmdSize,
793 pCmdData,
794 replySize,
795 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800796 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
797 uint32_t size = (replySize == NULL) ? 0 : *replySize;
798 for (size_t i = 1; i < mHandles.size(); i++) {
799 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800800 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800801 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
802 }
803 }
804 }
805 return status;
806}
807
808status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
809{
810 Mutex::Autolock _l(mLock);
811 return setEnabled_l(enabled);
812}
813
814// must be called with EffectModule::mLock held
815status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
816{
817
818 ALOGV("setEnabled %p enabled %d", this, enabled);
819
820 if (enabled != isEnabled()) {
821 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
822 if (enabled && status != NO_ERROR) {
823 return status;
824 }
825
826 switch (mState) {
827 // going from disabled to enabled
828 case IDLE:
829 mState = STARTING;
830 break;
831 case STOPPED:
832 mState = RESTART;
833 break;
834 case STOPPING:
835 mState = ACTIVE;
836 break;
837
838 // going from enabled to disabled
839 case RESTART:
840 mState = STOPPED;
841 break;
842 case STARTING:
843 mState = IDLE;
844 break;
845 case ACTIVE:
846 mState = STOPPING;
847 break;
848 case DESTROYED:
849 return NO_ERROR; // simply ignore as we are being destroyed
850 }
851 for (size_t i = 1; i < mHandles.size(); i++) {
852 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800853 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800854 h->setEnabled(enabled);
855 }
856 }
857 }
858 return NO_ERROR;
859}
860
861bool AudioFlinger::EffectModule::isEnabled() const
862{
863 switch (mState) {
864 case RESTART:
865 case STARTING:
866 case ACTIVE:
867 return true;
868 case IDLE:
869 case STOPPING:
870 case STOPPED:
871 case DESTROYED:
872 default:
873 return false;
874 }
875}
876
877bool AudioFlinger::EffectModule::isProcessEnabled() const
878{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700879 if (mStatus != NO_ERROR) {
880 return false;
881 }
882
Eric Laurentca7cc822012-11-19 14:55:58 -0800883 switch (mState) {
884 case RESTART:
885 case ACTIVE:
886 case STOPPING:
887 case STOPPED:
888 return true;
889 case IDLE:
890 case STARTING:
891 case DESTROYED:
892 default:
893 return false;
894 }
895}
896
Mikhail Naganov022b9952017-01-04 16:36:51 -0800897void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700898 ALOGVV("setInBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800899 if (buffer != 0) {
900 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
901 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
902 } else {
903 mConfig.inputCfg.buffer.raw = NULL;
904 }
905 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800906 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700907
908#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800909 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -0700910 // Theoretically insert effects can also do in-place conversions (destroying
911 // the original buffer) when the output buffer is identical to the input buffer,
912 // but we don't optimize for it here.
913 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
914 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
915 // we need to translate - create hidl shared buffer and intercept
916 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
917 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
918 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
919
920 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
921 __func__, inChannels, inFrameCount, size);
922
Andy Hungbded9c82017-11-30 18:47:35 -0800923 if (size > 0 && (mInConversionBuffer.get() == nullptr
924 || size > mInConversionBuffer->getSize())) {
925 mInConversionBuffer.clear();
926 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
927 (void)EffectBufferHalInterface::allocate(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700928 }
Andy Hungbded9c82017-11-30 18:47:35 -0800929 if (mInConversionBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -0700930 // FIXME: confirm buffer has enough size.
Andy Hungbded9c82017-11-30 18:47:35 -0800931 mInConversionBuffer->setFrameCount(inFrameCount);
932 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700933 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800934 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700935 }
936 }
937#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800938}
939
940void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700941 ALOGVV("setOutBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800942 if (buffer != 0) {
943 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
944 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
945 } else {
946 mConfig.outputCfg.buffer.raw = NULL;
947 }
948 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800949 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700950
951#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800952 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -0700953 // can do in-place conversion from int16_t to float. We don't optimize here.
954 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
955 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
956 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
957 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
958
959 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
960 __func__, outChannels, outFrameCount, size);
961
Andy Hungbded9c82017-11-30 18:47:35 -0800962 if (size > 0 && (mOutConversionBuffer.get() == nullptr
963 || size > mOutConversionBuffer->getSize())) {
964 mOutConversionBuffer.clear();
965 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
966 (void)EffectBufferHalInterface::allocate(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700967 }
Andy Hungbded9c82017-11-30 18:47:35 -0800968 if (mOutConversionBuffer.get() != nullptr) {
969 mOutConversionBuffer->setFrameCount(outFrameCount);
970 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700971 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800972 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700973 }
974 }
975#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800976}
977
Eric Laurentca7cc822012-11-19 14:55:58 -0800978status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
979{
980 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700981 if (mStatus != NO_ERROR) {
982 return mStatus;
983 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800984 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800985 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
986 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
987 if (isProcessEnabled() &&
988 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
989 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800990 uint32_t volume[2];
991 uint32_t *pVolume = NULL;
992 uint32_t size = sizeof(volume);
993 volume[0] = *left;
994 volume[1] = *right;
995 if (controller) {
996 pVolume = volume;
997 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700998 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
999 size,
1000 volume,
1001 &size,
1002 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001003 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1004 *left = volume[0];
1005 *right = volume[1];
1006 }
1007 }
1008 return status;
1009}
1010
1011status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1012{
1013 if (device == AUDIO_DEVICE_NONE) {
1014 return NO_ERROR;
1015 }
1016
1017 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001018 if (mStatus != NO_ERROR) {
1019 return mStatus;
1020 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001021 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001022 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001023 status_t cmdStatus;
1024 uint32_t size = sizeof(status_t);
1025 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1026 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001027 status = mEffectInterface->command(cmd,
1028 sizeof(uint32_t),
1029 &device,
1030 &size,
1031 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001032 }
1033 return status;
1034}
1035
1036status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1037{
1038 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001039 if (mStatus != NO_ERROR) {
1040 return mStatus;
1041 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001042 status_t status = NO_ERROR;
1043 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1044 status_t cmdStatus;
1045 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001046 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1047 sizeof(audio_mode_t),
1048 &mode,
1049 &size,
1050 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001051 if (status == NO_ERROR) {
1052 status = cmdStatus;
1053 }
1054 }
1055 return status;
1056}
1057
1058status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1059{
1060 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001061 if (mStatus != NO_ERROR) {
1062 return mStatus;
1063 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 status_t status = NO_ERROR;
1065 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1066 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001067 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1068 sizeof(audio_source_t),
1069 &source,
1070 &size,
1071 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001072 }
1073 return status;
1074}
1075
1076void AudioFlinger::EffectModule::setSuspended(bool suspended)
1077{
1078 Mutex::Autolock _l(mLock);
1079 mSuspended = suspended;
1080}
1081
1082bool AudioFlinger::EffectModule::suspended() const
1083{
1084 Mutex::Autolock _l(mLock);
1085 return mSuspended;
1086}
1087
1088bool AudioFlinger::EffectModule::purgeHandles()
1089{
1090 bool enabled = false;
1091 Mutex::Autolock _l(mLock);
1092 for (size_t i = 0; i < mHandles.size(); i++) {
1093 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001094 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 if (handle->hasControl()) {
1096 enabled = handle->enabled();
1097 }
1098 }
1099 }
1100 return enabled;
1101}
1102
Eric Laurent5baf2af2013-09-12 17:37:00 -07001103status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1104{
1105 Mutex::Autolock _l(mLock);
1106 if (mStatus != NO_ERROR) {
1107 return mStatus;
1108 }
1109 status_t status = NO_ERROR;
1110 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1111 status_t cmdStatus;
1112 uint32_t size = sizeof(status_t);
1113 effect_offload_param_t cmd;
1114
1115 cmd.isOffload = offloaded;
1116 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001117 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1118 sizeof(effect_offload_param_t),
1119 &cmd,
1120 &size,
1121 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001122 if (status == NO_ERROR) {
1123 status = cmdStatus;
1124 }
1125 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1126 } else {
1127 if (offloaded) {
1128 status = INVALID_OPERATION;
1129 }
1130 mOffloaded = false;
1131 }
1132 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1133 return status;
1134}
1135
1136bool AudioFlinger::EffectModule::isOffloaded() const
1137{
1138 Mutex::Autolock _l(mLock);
1139 return mOffloaded;
1140}
1141
Marco Nelissenb2208842014-02-07 14:00:50 -08001142String8 effectFlagsToString(uint32_t flags) {
1143 String8 s;
1144
1145 s.append("conn. mode: ");
1146 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1147 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1148 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1149 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1150 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1151 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1152 default: s.append("unknown/reserved"); break;
1153 }
1154 s.append(", ");
1155
1156 s.append("insert pref: ");
1157 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1158 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1159 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1160 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1161 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1162 default: s.append("unknown/reserved"); break;
1163 }
1164 s.append(", ");
1165
1166 s.append("volume mgmt: ");
1167 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1168 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1169 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1170 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1171 default: s.append("unknown/reserved"); break;
1172 }
1173 s.append(", ");
1174
1175 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1176 if (devind) {
1177 s.append("device indication: ");
1178 switch (devind) {
1179 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1180 default: s.append("unknown/reserved"); break;
1181 }
1182 s.append(", ");
1183 }
1184
1185 s.append("input mode: ");
1186 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1187 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1188 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1189 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1190 default: s.append("not set"); break;
1191 }
1192 s.append(", ");
1193
1194 s.append("output mode: ");
1195 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1196 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1197 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1198 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1199 default: s.append("not set"); break;
1200 }
1201 s.append(", ");
1202
1203 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1204 if (accel) {
1205 s.append("hardware acceleration: ");
1206 switch (accel) {
1207 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1208 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1209 default: s.append("unknown/reserved"); break;
1210 }
1211 s.append(", ");
1212 }
1213
1214 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1215 if (modeind) {
1216 s.append("mode indication: ");
1217 switch (modeind) {
1218 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1219 default: s.append("unknown/reserved"); break;
1220 }
1221 s.append(", ");
1222 }
1223
1224 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1225 if (srcind) {
1226 s.append("source indication: ");
1227 switch (srcind) {
1228 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1229 default: s.append("unknown/reserved"); break;
1230 }
1231 s.append(", ");
1232 }
1233
1234 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1235 s.append("offloadable, ");
1236 }
1237
1238 int len = s.length();
1239 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001240 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001241 s.unlockBuffer(len - 2);
1242 }
1243 return s;
1244}
1245
Andy Hungbded9c82017-11-30 18:47:35 -08001246static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1247 std::stringstream ss;
1248
1249 if (buffer.get() == nullptr) {
1250 return "nullptr"; // make different than below
1251 } else if (buffer->externalData() != nullptr) {
1252 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1253 << " -> "
1254 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1255 } else {
1256 ss << buffer->audioBuffer()->raw;
1257 }
1258 return ss.str();
1259}
Marco Nelissenb2208842014-02-07 14:00:50 -08001260
Glenn Kasten0f11b512014-01-31 16:18:54 -08001261void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001262{
1263 const size_t SIZE = 256;
1264 char buffer[SIZE];
1265 String8 result;
1266
1267 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1268 result.append(buffer);
1269
1270 bool locked = AudioFlinger::dumpTryLock(mLock);
1271 // failed to lock - AudioFlinger is probably deadlocked
1272 if (!locked) {
1273 result.append("\t\tCould not lock Fx mutex:\n");
1274 }
1275
1276 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001277 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001278 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001279 result.append(buffer);
1280
1281 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001282 char uuidStr[64];
1283 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1284 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001285 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001286 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1287 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001288 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001289 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001290 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001291 mDescriptor.flags,
1292 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001293 result.append(buffer);
1294 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1295 mDescriptor.name);
1296 result.append(buffer);
1297 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1298 mDescriptor.implementor);
1299 result.append(buffer);
1300
1301 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001302 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001303 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001304 mConfig.inputCfg.buffer.frameCount,
1305 mConfig.inputCfg.samplingRate,
1306 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001307 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001308 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001309 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001310 result.append(buffer);
1311
1312 result.append("\t\t- Output configuration:\n");
1313 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001314 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001315 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001316 mConfig.outputCfg.buffer.frameCount,
1317 mConfig.outputCfg.samplingRate,
1318 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001319 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001320 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001321 result.append(buffer);
1322
rago94a1ee82017-07-21 15:11:02 -07001323#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001324
Andy Hungbded9c82017-11-30 18:47:35 -08001325 result.appendFormat("\t\t- HAL buffers:\n"
1326 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1327 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1328 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1329 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1330 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001331#endif
1332
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001333 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001334 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001335 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001336 for (size_t i = 0; i < mHandles.size(); ++i) {
1337 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001338 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001339 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001340 result.append(buffer);
1341 }
1342 }
1343
Eric Laurentca7cc822012-11-19 14:55:58 -08001344 write(fd, result.string(), result.length());
1345
1346 if (locked) {
1347 mLock.unlock();
1348 }
1349}
1350
1351// ----------------------------------------------------------------------------
1352// EffectHandle implementation
1353// ----------------------------------------------------------------------------
1354
1355#undef LOG_TAG
1356#define LOG_TAG "AudioFlinger::EffectHandle"
1357
1358AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1359 const sp<AudioFlinger::Client>& client,
1360 const sp<IEffectClient>& effectClient,
1361 int32_t priority)
1362 : BnEffect(),
1363 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001364 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001365{
1366 ALOGV("constructor %p", this);
1367
1368 if (client == 0) {
1369 return;
1370 }
1371 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1372 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001373 if (mCblkMemory == 0 ||
1374 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001375 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001376 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001377 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001378 return;
1379 }
Glenn Kastene75da402013-11-20 13:54:52 -08001380 new(mCblk) effect_param_cblk_t();
1381 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001382}
1383
1384AudioFlinger::EffectHandle::~EffectHandle()
1385{
1386 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001387 disconnect(false);
1388}
1389
Glenn Kastene75da402013-11-20 13:54:52 -08001390status_t AudioFlinger::EffectHandle::initCheck()
1391{
1392 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1393}
1394
Eric Laurentca7cc822012-11-19 14:55:58 -08001395status_t AudioFlinger::EffectHandle::enable()
1396{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001397 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001398 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001399 sp<EffectModule> effect = mEffect.promote();
1400 if (effect == 0 || mDisconnected) {
1401 return DEAD_OBJECT;
1402 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001403 if (!mHasControl) {
1404 return INVALID_OPERATION;
1405 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001406
1407 if (mEnabled) {
1408 return NO_ERROR;
1409 }
1410
1411 mEnabled = true;
1412
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001413 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001415 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001416 }
1417
1418 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001419 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001420 return NO_ERROR;
1421 }
1422
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001423 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001424 if (status != NO_ERROR) {
1425 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001426 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 }
1428 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001429 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001430 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001431 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1432 Mutex::Autolock _l(thread->mLock);
1433 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001434 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001435 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001436 if (thread->type() == ThreadBase::OFFLOAD) {
1437 PlaybackThread *t = (PlaybackThread *)thread.get();
1438 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1439 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001440 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001441 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1442 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001443 }
1444 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001445 }
1446 return status;
1447}
1448
1449status_t AudioFlinger::EffectHandle::disable()
1450{
1451 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001452 AutoMutex _l(mLock);
1453 sp<EffectModule> effect = mEffect.promote();
1454 if (effect == 0 || mDisconnected) {
1455 return DEAD_OBJECT;
1456 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001457 if (!mHasControl) {
1458 return INVALID_OPERATION;
1459 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001460
1461 if (!mEnabled) {
1462 return NO_ERROR;
1463 }
1464 mEnabled = false;
1465
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001466 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 return NO_ERROR;
1468 }
1469
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001470 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001471
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001472 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001473 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001474 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001475 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1476 Mutex::Autolock _l(thread->mLock);
1477 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001478 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001479 }
1480
1481 return status;
1482}
1483
1484void AudioFlinger::EffectHandle::disconnect()
1485{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001486 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001487 disconnect(true);
1488}
1489
1490void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1491{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001492 AutoMutex _l(mLock);
1493 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1494 if (mDisconnected) {
1495 if (unpinIfLast) {
1496 android_errorWriteLog(0x534e4554, "32707507");
1497 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001498 return;
1499 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001500 mDisconnected = true;
1501 sp<ThreadBase> thread;
1502 {
1503 sp<EffectModule> effect = mEffect.promote();
1504 if (effect != 0) {
1505 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001506 }
1507 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001508 if (thread != 0) {
1509 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001510 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001511 // try to cleanup as much as we can
1512 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001513 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1514 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001515 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001516 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001517
Eric Laurentca7cc822012-11-19 14:55:58 -08001518 if (mClient != 0) {
1519 if (mCblk != NULL) {
1520 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1521 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1522 }
1523 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001524 // Client destructor must run with AudioFlinger client mutex locked
1525 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001526 mClient.clear();
1527 }
1528}
1529
1530status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1531 uint32_t cmdSize,
1532 void *pCmdData,
1533 uint32_t *replySize,
1534 void *pReplyData)
1535{
1536 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001537 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001538
Eric Laurentc7ab3092017-06-15 18:43:46 -07001539 // reject commands reserved for internal use by audio framework if coming from outside
1540 // of audioserver
1541 switch(cmdCode) {
1542 case EFFECT_CMD_ENABLE:
1543 case EFFECT_CMD_DISABLE:
1544 case EFFECT_CMD_SET_PARAM:
1545 case EFFECT_CMD_SET_PARAM_DEFERRED:
1546 case EFFECT_CMD_SET_PARAM_COMMIT:
1547 case EFFECT_CMD_GET_PARAM:
1548 break;
1549 default:
1550 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1551 break;
1552 }
1553 android_errorWriteLog(0x534e4554, "62019992");
1554 return BAD_VALUE;
1555 }
1556
Eric Laurent1ffc5852016-12-15 14:46:09 -08001557 if (cmdCode == EFFECT_CMD_ENABLE) {
1558 if (*replySize < sizeof(int)) {
1559 android_errorWriteLog(0x534e4554, "32095713");
1560 return BAD_VALUE;
1561 }
1562 *(int *)pReplyData = NO_ERROR;
1563 *replySize = sizeof(int);
1564 return enable();
1565 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1566 if (*replySize < sizeof(int)) {
1567 android_errorWriteLog(0x534e4554, "32095713");
1568 return BAD_VALUE;
1569 }
1570 *(int *)pReplyData = NO_ERROR;
1571 *replySize = sizeof(int);
1572 return disable();
1573 }
1574
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001575 AutoMutex _l(mLock);
1576 sp<EffectModule> effect = mEffect.promote();
1577 if (effect == 0 || mDisconnected) {
1578 return DEAD_OBJECT;
1579 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001580 // only get parameter command is permitted for applications not controlling the effect
1581 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1582 return INVALID_OPERATION;
1583 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001584 if (mClient == 0) {
1585 return INVALID_OPERATION;
1586 }
1587
1588 // handle commands that are not forwarded transparently to effect engine
1589 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001590 if (*replySize < sizeof(int)) {
1591 android_errorWriteLog(0x534e4554, "32095713");
1592 return BAD_VALUE;
1593 }
1594 *(int *)pReplyData = NO_ERROR;
1595 *replySize = sizeof(int);
1596
Eric Laurentca7cc822012-11-19 14:55:58 -08001597 // No need to trylock() here as this function is executed in the binder thread serving a
1598 // particular client process: no risk to block the whole media server process or mixer
1599 // threads if we are stuck here
1600 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001601 // keep local copy of index in case of client corruption b/32220769
1602 const uint32_t clientIndex = mCblk->clientIndex;
1603 const uint32_t serverIndex = mCblk->serverIndex;
1604 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1605 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001606 mCblk->serverIndex = 0;
1607 mCblk->clientIndex = 0;
1608 return BAD_VALUE;
1609 }
1610 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001611 effect_param_t *param = NULL;
1612 for (uint32_t index = serverIndex; index < clientIndex;) {
1613 int *p = (int *)(mBuffer + index);
1614 const int size = *p++;
1615 if (size < 0
1616 || size > EFFECT_PARAM_BUFFER_SIZE
1617 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001618 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001619 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001620 break;
1621 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001622
1623 // copy to local memory in case of client corruption b/32220769
1624 param = (effect_param_t *)realloc(param, size);
1625 if (param == NULL) {
1626 ALOGW("command(): out of memory");
1627 status = NO_MEMORY;
1628 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001629 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001630 memcpy(param, p, size);
1631
1632 int reply = 0;
1633 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001634 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001635 size,
1636 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001637 &rsize,
1638 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001639
1640 // verify shared memory: server index shouldn't change; client index can't go back.
1641 if (serverIndex != mCblk->serverIndex
1642 || clientIndex > mCblk->clientIndex) {
1643 android_errorWriteLog(0x534e4554, "32220769");
1644 status = BAD_VALUE;
1645 break;
1646 }
1647
Eric Laurentca7cc822012-11-19 14:55:58 -08001648 // stop at first error encountered
1649 if (ret != NO_ERROR) {
1650 status = ret;
1651 *(int *)pReplyData = reply;
1652 break;
1653 } else if (reply != NO_ERROR) {
1654 *(int *)pReplyData = reply;
1655 break;
1656 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001657 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001658 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001659 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001660 mCblk->serverIndex = 0;
1661 mCblk->clientIndex = 0;
1662 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 }
1664
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001665 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001666}
1667
1668void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1669{
1670 ALOGV("setControl %p control %d", this, hasControl);
1671
1672 mHasControl = hasControl;
1673 mEnabled = enabled;
1674
1675 if (signal && mEffectClient != 0) {
1676 mEffectClient->controlStatusChanged(hasControl);
1677 }
1678}
1679
1680void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1681 uint32_t cmdSize,
1682 void *pCmdData,
1683 uint32_t replySize,
1684 void *pReplyData)
1685{
1686 if (mEffectClient != 0) {
1687 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1688 }
1689}
1690
1691
1692
1693void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1694{
1695 if (mEffectClient != 0) {
1696 mEffectClient->enableStatusChanged(enabled);
1697 }
1698}
1699
1700status_t AudioFlinger::EffectHandle::onTransact(
1701 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1702{
1703 return BnEffect::onTransact(code, data, reply, flags);
1704}
1705
1706
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001707void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001708{
1709 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1710
Marco Nelissenb2208842014-02-07 14:00:50 -08001711 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001712 (mClient == 0) ? getpid_cached : mClient->pid(),
1713 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001714 mHasControl ? "yes" : "no",
1715 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001716 mCblk ? mCblk->clientIndex : 0,
1717 mCblk ? mCblk->serverIndex : 0
1718 );
1719
1720 if (locked) {
1721 mCblk->lock.unlock();
1722 }
1723}
1724
1725#undef LOG_TAG
1726#define LOG_TAG "AudioFlinger::EffectChain"
1727
1728AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001729 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001730 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001731 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001732 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001733{
1734 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1735 if (thread == NULL) {
1736 return;
1737 }
1738 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1739 thread->frameCount();
1740}
1741
1742AudioFlinger::EffectChain::~EffectChain()
1743{
Eric Laurentca7cc822012-11-19 14:55:58 -08001744}
1745
1746// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1747sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1748 effect_descriptor_t *descriptor)
1749{
1750 size_t size = mEffects.size();
1751
1752 for (size_t i = 0; i < size; i++) {
1753 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1754 return mEffects[i];
1755 }
1756 }
1757 return 0;
1758}
1759
1760// getEffectFromId_l() must be called with ThreadBase::mLock held
1761sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1762{
1763 size_t size = mEffects.size();
1764
1765 for (size_t i = 0; i < size; i++) {
1766 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1767 if (id == 0 || mEffects[i]->id() == id) {
1768 return mEffects[i];
1769 }
1770 }
1771 return 0;
1772}
1773
1774// getEffectFromType_l() must be called with ThreadBase::mLock held
1775sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1776 const effect_uuid_t *type)
1777{
1778 size_t size = mEffects.size();
1779
1780 for (size_t i = 0; i < size; i++) {
1781 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1782 return mEffects[i];
1783 }
1784 }
1785 return 0;
1786}
1787
1788void AudioFlinger::EffectChain::clearInputBuffer()
1789{
1790 Mutex::Autolock _l(mLock);
1791 sp<ThreadBase> thread = mThread.promote();
1792 if (thread == 0) {
1793 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1794 return;
1795 }
1796 clearInputBuffer_l(thread);
1797}
1798
1799// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001800void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001801{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001802 if (mInBuffer == NULL) {
1803 return;
1804 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001805 // TODO: This will change in the future, depending on multichannel
1806 // and sample format changes for effects.
1807 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1808 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001809
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001810 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001811 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1812 * std::min((uint32_t)FCC_2, thread->channelCount());
1813
Mikhail Naganov022b9952017-01-04 16:36:51 -08001814 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1815 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001816}
1817
1818// Must be called with EffectChain::mLock locked
1819void AudioFlinger::EffectChain::process_l()
1820{
1821 sp<ThreadBase> thread = mThread.promote();
1822 if (thread == 0) {
1823 ALOGW("process_l(): cannot promote mixer thread");
1824 return;
1825 }
1826 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1827 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001828 // never process effects when:
1829 // - on an OFFLOAD thread
1830 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001831 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1832 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001833 if (!isGlobalSession) {
1834 bool tracksOnSession = (trackCnt() != 0);
1835
1836 if (!tracksOnSession && mTailBufferCount == 0) {
1837 doProcess = false;
1838 }
1839
1840 if (activeTrackCnt() == 0) {
1841 // if no track is active and the effect tail has not been rendered,
1842 // the input buffer must be cleared here as the mixer process will not do it
1843 if (tracksOnSession || mTailBufferCount > 0) {
1844 clearInputBuffer_l(thread);
1845 if (mTailBufferCount > 0) {
1846 mTailBufferCount--;
1847 }
1848 }
1849 }
1850 }
1851
1852 size_t size = mEffects.size();
1853 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001854 // Only the input and output buffers of the chain can be external,
1855 // and 'update' / 'commit' do nothing for allocated buffers, thus
1856 // it's not needed to consider any other buffers here.
1857 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001858 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1859 mOutBuffer->update();
1860 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001861 for (size_t i = 0; i < size; i++) {
1862 mEffects[i]->process();
1863 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001864 mInBuffer->commit();
1865 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1866 mOutBuffer->commit();
1867 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001868 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001869 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001870 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001871 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1872 }
1873 if (doResetVolume) {
1874 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001875 }
1876}
1877
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001878// createEffect_l() must be called with ThreadBase::mLock held
1879status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1880 ThreadBase *thread,
1881 effect_descriptor_t *desc,
1882 int id,
1883 audio_session_t sessionId,
1884 bool pinned)
1885{
1886 Mutex::Autolock _l(mLock);
1887 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1888 status_t lStatus = effect->status();
1889 if (lStatus == NO_ERROR) {
1890 lStatus = addEffect_ll(effect);
1891 }
1892 if (lStatus != NO_ERROR) {
1893 effect.clear();
1894 }
1895 return lStatus;
1896}
1897
1898// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001899status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1900{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001901 Mutex::Autolock _l(mLock);
1902 return addEffect_ll(effect);
1903}
1904// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1905status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1906{
Eric Laurentca7cc822012-11-19 14:55:58 -08001907 effect_descriptor_t desc = effect->desc();
1908 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1909
Eric Laurentca7cc822012-11-19 14:55:58 -08001910 effect->setChain(this);
1911 sp<ThreadBase> thread = mThread.promote();
1912 if (thread == 0) {
1913 return NO_INIT;
1914 }
1915 effect->setThread(thread);
1916
1917 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1918 // Auxiliary effects are inserted at the beginning of mEffects vector as
1919 // they are processed first and accumulated in chain input buffer
1920 mEffects.insertAt(effect, 0);
1921
1922 // the input buffer for auxiliary effect contains mono samples in
1923 // 32 bit format. This is to avoid saturation in AudoMixer
1924 // accumulation stage. Saturation is done in EffectModule::process() before
1925 // calling the process in effect engine
1926 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001927 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001928#ifdef FLOAT_EFFECT_CHAIN
1929 status_t result = EffectBufferHalInterface::allocate(
1930 numSamples * sizeof(float), &halBuffer);
1931#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001932 status_t result = EffectBufferHalInterface::allocate(
1933 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001934#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001935 if (result != OK) return result;
1936 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001937 // auxiliary effects output samples to chain input buffer for further processing
1938 // by insert effects
1939 effect->setOutBuffer(mInBuffer);
1940 } else {
1941 // Insert effects are inserted at the end of mEffects vector as they are processed
1942 // after track and auxiliary effects.
1943 // Insert effect order as a function of indicated preference:
1944 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1945 // another effect is present
1946 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1947 // last effect claiming first position
1948 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1949 // first effect claiming last position
1950 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1951 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1952 // already present
1953
1954 size_t size = mEffects.size();
1955 size_t idx_insert = size;
1956 ssize_t idx_insert_first = -1;
1957 ssize_t idx_insert_last = -1;
1958
1959 for (size_t i = 0; i < size; i++) {
1960 effect_descriptor_t d = mEffects[i]->desc();
1961 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1962 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1963 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1964 // check invalid effect chaining combinations
1965 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1966 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1967 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1968 desc.name, d.name);
1969 return INVALID_OPERATION;
1970 }
1971 // remember position of first insert effect and by default
1972 // select this as insert position for new effect
1973 if (idx_insert == size) {
1974 idx_insert = i;
1975 }
1976 // remember position of last insert effect claiming
1977 // first position
1978 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1979 idx_insert_first = i;
1980 }
1981 // remember position of first insert effect claiming
1982 // last position
1983 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1984 idx_insert_last == -1) {
1985 idx_insert_last = i;
1986 }
1987 }
1988 }
1989
1990 // modify idx_insert from first position if needed
1991 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1992 if (idx_insert_last != -1) {
1993 idx_insert = idx_insert_last;
1994 } else {
1995 idx_insert = size;
1996 }
1997 } else {
1998 if (idx_insert_first != -1) {
1999 idx_insert = idx_insert_first + 1;
2000 }
2001 }
2002
2003 // always read samples from chain input buffer
2004 effect->setInBuffer(mInBuffer);
2005
2006 // if last effect in the chain, output samples to chain
2007 // output buffer, otherwise to chain input buffer
2008 if (idx_insert == size) {
2009 if (idx_insert != 0) {
2010 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2011 mEffects[idx_insert-1]->configure();
2012 }
2013 effect->setOutBuffer(mOutBuffer);
2014 } else {
2015 effect->setOutBuffer(mInBuffer);
2016 }
2017 mEffects.insertAt(effect, idx_insert);
2018
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002019 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002020 idx_insert);
2021 }
2022 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002023
Eric Laurentca7cc822012-11-19 14:55:58 -08002024 return NO_ERROR;
2025}
2026
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002027// removeEffect_l() must be called with ThreadBase::mLock held
2028size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2029 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002030{
2031 Mutex::Autolock _l(mLock);
2032 size_t size = mEffects.size();
2033 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2034
2035 for (size_t i = 0; i < size; i++) {
2036 if (effect == mEffects[i]) {
2037 // calling stop here will remove pre-processing effect from the audio HAL.
2038 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2039 // the middle of a read from audio HAL
2040 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2041 mEffects[i]->state() == EffectModule::STOPPING) {
2042 mEffects[i]->stop();
2043 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002044 if (release) {
2045 mEffects[i]->release_l();
2046 }
2047
Mikhail Naganov022b9952017-01-04 16:36:51 -08002048 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002049 if (i == size - 1 && i != 0) {
2050 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2051 mEffects[i - 1]->configure();
2052 }
2053 }
2054 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002055 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002056 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002057
Eric Laurentca7cc822012-11-19 14:55:58 -08002058 break;
2059 }
2060 }
2061
2062 return mEffects.size();
2063}
2064
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002065// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002066void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2067{
2068 size_t size = mEffects.size();
2069 for (size_t i = 0; i < size; i++) {
2070 mEffects[i]->setDevice(device);
2071 }
2072}
2073
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002074// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002075void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2076{
2077 size_t size = mEffects.size();
2078 for (size_t i = 0; i < size; i++) {
2079 mEffects[i]->setMode(mode);
2080 }
2081}
2082
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002083// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002084void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2085{
2086 size_t size = mEffects.size();
2087 for (size_t i = 0; i < size; i++) {
2088 mEffects[i]->setAudioSource(source);
2089 }
2090}
2091
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002092// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002093bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002094{
2095 uint32_t newLeft = *left;
2096 uint32_t newRight = *right;
2097 bool hasControl = false;
2098 int ctrlIdx = -1;
2099 size_t size = mEffects.size();
2100
2101 // first update volume controller
2102 for (size_t i = size; i > 0; i--) {
2103 if (mEffects[i - 1]->isProcessEnabled() &&
2104 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2105 ctrlIdx = i - 1;
2106 hasControl = true;
2107 break;
2108 }
2109 }
2110
Eric Laurentfa1e1232016-08-02 19:01:49 -07002111 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002112 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002113 if (hasControl) {
2114 *left = mNewLeftVolume;
2115 *right = mNewRightVolume;
2116 }
2117 return hasControl;
2118 }
2119
2120 mVolumeCtrlIdx = ctrlIdx;
2121 mLeftVolume = newLeft;
2122 mRightVolume = newRight;
2123
2124 // second get volume update from volume controller
2125 if (ctrlIdx >= 0) {
2126 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2127 mNewLeftVolume = newLeft;
2128 mNewRightVolume = newRight;
2129 }
2130 // then indicate volume to all other effects in chain.
2131 // Pass altered volume to effects before volume controller
2132 // and requested volume to effects after controller
2133 uint32_t lVol = newLeft;
2134 uint32_t rVol = newRight;
2135
2136 for (size_t i = 0; i < size; i++) {
2137 if ((int)i == ctrlIdx) {
2138 continue;
2139 }
2140 // this also works for ctrlIdx == -1 when there is no volume controller
2141 if ((int)i > ctrlIdx) {
2142 lVol = *left;
2143 rVol = *right;
2144 }
2145 mEffects[i]->setVolume(&lVol, &rVol, false);
2146 }
2147 *left = newLeft;
2148 *right = newRight;
2149
2150 return hasControl;
2151}
2152
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002153// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002154void AudioFlinger::EffectChain::resetVolume_l()
2155{
Eric Laurente7449bf2016-08-03 18:44:07 -07002156 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2157 uint32_t left = mLeftVolume;
2158 uint32_t right = mRightVolume;
2159 (void)setVolume_l(&left, &right, true);
2160 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002161}
2162
Eric Laurent1b928682014-10-02 19:41:47 -07002163void AudioFlinger::EffectChain::syncHalEffectsState()
2164{
2165 Mutex::Autolock _l(mLock);
2166 for (size_t i = 0; i < mEffects.size(); i++) {
2167 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2168 mEffects[i]->state() == EffectModule::STOPPING) {
2169 mEffects[i]->addEffectToHal_l();
2170 }
2171 }
2172}
2173
Eric Laurentca7cc822012-11-19 14:55:58 -08002174void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2175{
2176 const size_t SIZE = 256;
2177 char buffer[SIZE];
2178 String8 result;
2179
Marco Nelissenb2208842014-02-07 14:00:50 -08002180 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002181 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002182 result.append(buffer);
2183
Marco Nelissenb2208842014-02-07 14:00:50 -08002184 if (numEffects) {
2185 bool locked = AudioFlinger::dumpTryLock(mLock);
2186 // failed to lock - AudioFlinger is probably deadlocked
2187 if (!locked) {
2188 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002189 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002190
Andy Hungbded9c82017-11-30 18:47:35 -08002191 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2192 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2193 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2194 (int)inBufferStr.size(), "In buffer ",
2195 (int)outBufferStr.size(), "Out buffer ");
2196 result.appendFormat("\t%s %s %d\n",
2197 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002198 write(fd, result.string(), result.size());
2199
2200 for (size_t i = 0; i < numEffects; ++i) {
2201 sp<EffectModule> effect = mEffects[i];
2202 if (effect != 0) {
2203 effect->dump(fd, args);
2204 }
2205 }
2206
2207 if (locked) {
2208 mLock.unlock();
2209 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002210 }
2211}
2212
2213// must be called with ThreadBase::mLock held
2214void AudioFlinger::EffectChain::setEffectSuspended_l(
2215 const effect_uuid_t *type, bool suspend)
2216{
2217 sp<SuspendedEffectDesc> desc;
2218 // use effect type UUID timelow as key as there is no real risk of identical
2219 // timeLow fields among effect type UUIDs.
2220 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2221 if (suspend) {
2222 if (index >= 0) {
2223 desc = mSuspendedEffects.valueAt(index);
2224 } else {
2225 desc = new SuspendedEffectDesc();
2226 desc->mType = *type;
2227 mSuspendedEffects.add(type->timeLow, desc);
2228 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2229 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002230
Eric Laurentca7cc822012-11-19 14:55:58 -08002231 if (desc->mRefCount++ == 0) {
2232 sp<EffectModule> effect = getEffectIfEnabled(type);
2233 if (effect != 0) {
2234 desc->mEffect = effect;
2235 effect->setSuspended(true);
2236 effect->setEnabled(false);
2237 }
2238 }
2239 } else {
2240 if (index < 0) {
2241 return;
2242 }
2243 desc = mSuspendedEffects.valueAt(index);
2244 if (desc->mRefCount <= 0) {
2245 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002246 desc->mRefCount = 0;
2247 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002248 }
2249 if (--desc->mRefCount == 0) {
2250 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2251 if (desc->mEffect != 0) {
2252 sp<EffectModule> effect = desc->mEffect.promote();
2253 if (effect != 0) {
2254 effect->setSuspended(false);
2255 effect->lock();
2256 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002257 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002258 effect->setEnabled_l(handle->enabled());
2259 }
2260 effect->unlock();
2261 }
2262 desc->mEffect.clear();
2263 }
2264 mSuspendedEffects.removeItemsAt(index);
2265 }
2266 }
2267}
2268
2269// must be called with ThreadBase::mLock held
2270void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2271{
2272 sp<SuspendedEffectDesc> desc;
2273
2274 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2275 if (suspend) {
2276 if (index >= 0) {
2277 desc = mSuspendedEffects.valueAt(index);
2278 } else {
2279 desc = new SuspendedEffectDesc();
2280 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2281 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2282 }
2283 if (desc->mRefCount++ == 0) {
2284 Vector< sp<EffectModule> > effects;
2285 getSuspendEligibleEffects(effects);
2286 for (size_t i = 0; i < effects.size(); i++) {
2287 setEffectSuspended_l(&effects[i]->desc().type, true);
2288 }
2289 }
2290 } else {
2291 if (index < 0) {
2292 return;
2293 }
2294 desc = mSuspendedEffects.valueAt(index);
2295 if (desc->mRefCount <= 0) {
2296 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2297 desc->mRefCount = 1;
2298 }
2299 if (--desc->mRefCount == 0) {
2300 Vector<const effect_uuid_t *> types;
2301 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2302 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2303 continue;
2304 }
2305 types.add(&mSuspendedEffects.valueAt(i)->mType);
2306 }
2307 for (size_t i = 0; i < types.size(); i++) {
2308 setEffectSuspended_l(types[i], false);
2309 }
2310 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2311 mSuspendedEffects.keyAt(index));
2312 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2313 }
2314 }
2315}
2316
2317
2318// The volume effect is used for automated tests only
2319#ifndef OPENSL_ES_H_
2320static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2321 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2322const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2323#endif //OPENSL_ES_H_
2324
Eric Laurentd8365c52017-07-16 15:27:05 -07002325/* static */
2326bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2327{
2328 // Only NS and AEC are suspended when BtNRec is off
2329 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2330 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2331 return true;
2332 }
2333 return false;
2334}
2335
Eric Laurentca7cc822012-11-19 14:55:58 -08002336bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2337{
2338 // auxiliary effects and visualizer are never suspended on output mix
2339 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2340 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2341 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2342 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2343 return false;
2344 }
2345 return true;
2346}
2347
2348void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2349 Vector< sp<AudioFlinger::EffectModule> > &effects)
2350{
2351 effects.clear();
2352 for (size_t i = 0; i < mEffects.size(); i++) {
2353 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2354 effects.add(mEffects[i]);
2355 }
2356 }
2357}
2358
2359sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2360 const effect_uuid_t *type)
2361{
2362 sp<EffectModule> effect = getEffectFromType_l(type);
2363 return effect != 0 && effect->isEnabled() ? effect : 0;
2364}
2365
2366void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2367 bool enabled)
2368{
2369 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2370 if (enabled) {
2371 if (index < 0) {
2372 // if the effect is not suspend check if all effects are suspended
2373 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2374 if (index < 0) {
2375 return;
2376 }
2377 if (!isEffectEligibleForSuspend(effect->desc())) {
2378 return;
2379 }
2380 setEffectSuspended_l(&effect->desc().type, enabled);
2381 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2382 if (index < 0) {
2383 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2384 return;
2385 }
2386 }
2387 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2388 effect->desc().type.timeLow);
2389 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002390 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002391 if (desc->mEffect == 0) {
2392 desc->mEffect = effect;
2393 effect->setEnabled(false);
2394 effect->setSuspended(true);
2395 }
2396 } else {
2397 if (index < 0) {
2398 return;
2399 }
2400 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2401 effect->desc().type.timeLow);
2402 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2403 desc->mEffect.clear();
2404 effect->setSuspended(false);
2405 }
2406}
2407
Eric Laurent5baf2af2013-09-12 17:37:00 -07002408bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002409{
2410 Mutex::Autolock _l(mLock);
2411 size_t size = mEffects.size();
2412 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002413 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002414 return true;
2415 }
2416 }
2417 return false;
2418}
2419
Eric Laurentaaa44472014-09-12 17:41:50 -07002420void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2421{
2422 Mutex::Autolock _l(mLock);
2423 mThread = thread;
2424 for (size_t i = 0; i < mEffects.size(); i++) {
2425 mEffects[i]->setThread(thread);
2426 }
2427}
2428
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002429void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2430{
2431 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2432 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2433 }
2434 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2435 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2436 }
2437}
2438
2439void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2440{
2441 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2442 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2443 }
2444 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2445 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2446 }
2447}
2448
2449bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002450{
2451 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002452 for (const auto &effect : mEffects) {
2453 if (effect->isProcessImplemented()) {
2454 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002455 }
2456 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002457 // Allow effects without processing.
2458 return true;
2459}
2460
2461bool AudioFlinger::EffectChain::isFastCompatible() const
2462{
2463 Mutex::Autolock _l(mLock);
2464 for (const auto &effect : mEffects) {
2465 if (effect->isProcessImplemented()
2466 && effect->isImplementationSoftware()) {
2467 return false;
2468 }
2469 }
2470 // Allow effects without processing or hw accelerated effects.
2471 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002472}
2473
2474// isCompatibleWithThread_l() must be called with thread->mLock held
2475bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2476{
2477 Mutex::Autolock _l(mLock);
2478 for (size_t i = 0; i < mEffects.size(); i++) {
2479 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2480 return false;
2481 }
2482 }
2483 return true;
2484}
2485
Glenn Kasten63238ef2015-03-02 15:50:29 -08002486} // namespace android