blob: 5d04a5b2f47504ff05f6a54d664434dbf83dd3e8 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
27#include <system/audio_effects/effect_ns.h>
28#include <system/audio_effects/effect_visualizer.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080029#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070030#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070031#include <media/audiohal/EffectHalInterface.h>
32#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080033
34#include "AudioFlinger.h"
35#include "ServiceUtilities.h"
36
37// ----------------------------------------------------------------------------
38
39// Note: the following macro is used for extremely verbose logging message. In
40// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
41// 0; but one side effect of this is to turn all LOGV's as well. Some messages
42// are so verbose that we want to suppress them even when we have ALOG_ASSERT
43// turned on. Do not uncomment the #def below unless you really know what you
44// are doing and want to see all of the extremely verbose messages.
45//#define VERY_VERY_VERBOSE_LOGGING
46#ifdef VERY_VERY_VERBOSE_LOGGING
47#define ALOGVV ALOGV
48#else
49#define ALOGVV(a...) do { } while(0)
50#endif
51
52namespace android {
53
54// ----------------------------------------------------------------------------
55// EffectModule implementation
56// ----------------------------------------------------------------------------
57
58#undef LOG_TAG
59#define LOG_TAG "AudioFlinger::EffectModule"
60
61AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
62 const wp<AudioFlinger::EffectChain>& chain,
63 effect_descriptor_t *desc,
64 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080065 audio_session_t sessionId,
66 bool pinned)
67 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080068 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080070 // clear mConfig to ensure consistent initial value of buffer framecount
71 // in case buffers are associated by setInBuffer() or setOutBuffer()
72 // prior to configure().
73 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080074 mStatus(NO_INIT), mState(IDLE),
75 // mMaxDisableWaitCnt is set by configure() and not used before then
76 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070077 mSuspended(false),
78 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070079#ifdef FLOAT_EFFECT_CHAIN
80 , mSupportsFloat(false)
81#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080082{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080083 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080084 int lStatus;
85
86 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070087 mStatus = -ENODEV;
88 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070089 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070090 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070091 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070092 mStatus = effectsFactory->createEffect(
93 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
94 }
95 }
Eric Laurentca7cc822012-11-19 14:55:58 -080096
97 if (mStatus != NO_ERROR) {
98 return;
99 }
100 lStatus = init();
101 if (lStatus < 0) {
102 mStatus = lStatus;
103 goto Error;
104 }
105
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800106 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700107 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800108
Eric Laurentca7cc822012-11-19 14:55:58 -0800109 return;
110Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700111 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800112 ALOGV("Constructor Error %d", mStatus);
113}
114
115AudioFlinger::EffectModule::~EffectModule()
116{
117 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700118 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700119 char uuidStr[64];
120 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
121 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
122 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800123 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800124 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800125
Eric Laurentca7cc822012-11-19 14:55:58 -0800126}
127
128status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
129{
130 status_t status;
131
132 Mutex::Autolock _l(mLock);
133 int priority = handle->priority();
134 size_t size = mHandles.size();
135 EffectHandle *controlHandle = NULL;
136 size_t i;
137 for (i = 0; i < size; i++) {
138 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800139 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800140 continue;
141 }
142 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700143 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800144 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700145 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800146 if (h->priority() <= priority) {
147 break;
148 }
149 }
150 // if inserted in first place, move effect control from previous owner to this handle
151 if (i == 0) {
152 bool enabled = false;
153 if (controlHandle != NULL) {
154 enabled = controlHandle->enabled();
155 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
156 }
157 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
158 status = NO_ERROR;
159 } else {
160 status = ALREADY_EXISTS;
161 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700162 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800163 mHandles.insertAt(handle, i);
164 return status;
165}
166
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800167ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800168{
169 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800170 return removeHandle_l(handle);
171}
172
173ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
174{
Eric Laurentca7cc822012-11-19 14:55:58 -0800175 size_t size = mHandles.size();
176 size_t i;
177 for (i = 0; i < size; i++) {
178 if (mHandles[i] == handle) {
179 break;
180 }
181 }
182 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800183 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
184 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800185 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800186 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800187
188 mHandles.removeAt(i);
189 // if removed from first place, move effect control from this handle to next in line
190 if (i == 0) {
191 EffectHandle *h = controlHandle_l();
192 if (h != NULL) {
193 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
194 }
195 }
196
197 // Prevent calls to process() and other functions on effect interface from now on.
198 // The effect engine will be released by the destructor when the last strong reference on
199 // this object is released which can happen after next process is called.
200 if (mHandles.size() == 0 && !mPinned) {
201 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800202 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800203 }
204
205 return mHandles.size();
206}
207
208// must be called with EffectModule::mLock held
209AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
210{
211 // the first valid handle in the list has control over the module
212 for (size_t i = 0; i < mHandles.size(); i++) {
213 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800214 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800215 return h;
216 }
217 }
218
219 return NULL;
220}
221
Eric Laurentf10c7092016-12-06 17:09:56 -0800222// unsafe method called when the effect parent thread has been destroyed
223ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
224{
225 ALOGV("disconnect() %p handle %p", this, handle);
226 Mutex::Autolock _l(mLock);
227 ssize_t numHandles = removeHandle_l(handle);
228 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
229 AudioSystem::unregisterEffect(mId);
230 sp<AudioFlinger> af = mAudioFlinger.promote();
231 if (af != 0) {
232 mLock.unlock();
233 af->updateOrphanEffectChains(this);
234 mLock.lock();
235 }
236 }
237 return numHandles;
238}
239
Eric Laurentfa1e1232016-08-02 19:01:49 -0700240bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800241 Mutex::Autolock _l(mLock);
242
Eric Laurentfa1e1232016-08-02 19:01:49 -0700243 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800244 switch (mState) {
245 case RESTART:
246 reset_l();
247 // FALL THROUGH
248
249 case STARTING:
250 // clear auxiliary effect input buffer for next accumulation
251 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
252 memset(mConfig.inputCfg.buffer.raw,
253 0,
254 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
255 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700256 if (start_l() == NO_ERROR) {
257 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700258 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700259 } else {
260 mState = IDLE;
261 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800262 break;
263 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700264 if (stop_l() == NO_ERROR) {
265 mDisableWaitCnt = mMaxDisableWaitCnt;
266 } else {
267 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
268 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800269 mState = STOPPED;
270 break;
271 case STOPPED:
272 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
273 // turn off sequence.
274 if (--mDisableWaitCnt == 0) {
275 reset_l();
276 mState = IDLE;
277 }
278 break;
279 default: //IDLE , ACTIVE, DESTROYED
280 break;
281 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700282
283 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800284}
285
286void AudioFlinger::EffectModule::process()
287{
288 Mutex::Autolock _l(mLock);
289
Mikhail Naganov022b9952017-01-04 16:36:51 -0800290 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800291 return;
292 }
293
rago94a1ee82017-07-21 15:11:02 -0700294 // TODO: Implement multichannel effects; here outChannelCount == FCC_2 == 2
295 const uint32_t inChannelCount =
296 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
297 const uint32_t outChannelCount =
298 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
299 const bool auxType =
300 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
301
Andy Hungfa69ca32017-11-30 10:07:53 -0800302 // safeInputOutputSampleCount is 0 if the channel count between input and output
303 // buffers do not match. This prevents automatic accumulation or copying between the
304 // input and output effect buffers without an intermediary effect process.
305 // TODO: consider implementing channel conversion.
306 const size_t safeInputOutputSampleCount =
307 inChannelCount != outChannelCount ? 0
308 : outChannelCount * std::min(
309 mConfig.inputCfg.buffer.frameCount,
310 mConfig.outputCfg.buffer.frameCount);
311 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
312#ifdef FLOAT_EFFECT_CHAIN
313 accumulate_float(
314 mConfig.outputCfg.buffer.f32,
315 mConfig.inputCfg.buffer.f32,
316 safeInputOutputSampleCount);
317#else
318 accumulate_i16(
319 mConfig.outputCfg.buffer.s16,
320 mConfig.inputCfg.buffer.s16,
321 safeInputOutputSampleCount);
322#endif
323 };
324 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
325#ifdef FLOAT_EFFECT_CHAIN
326 memcpy(
327 mConfig.outputCfg.buffer.f32,
328 mConfig.inputCfg.buffer.f32,
329 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
330
331#else
332 memcpy(
333 mConfig.outputCfg.buffer.s16,
334 mConfig.inputCfg.buffer.s16,
335 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
336#endif
337 };
338
Eric Laurentca7cc822012-11-19 14:55:58 -0800339 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700340 int ret;
341 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700342 if (auxType) {
343 // We overwrite the aux input buffer here and clear after processing.
344 // Note that aux input buffers are format q4_27.
345#ifdef FLOAT_EFFECT_CHAIN
346 if (mSupportsFloat) {
347 // Do in-place float conversion for auxiliary effect input buffer.
348 static_assert(sizeof(float) <= sizeof(int32_t),
349 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
350
Andy Hungfa69ca32017-11-30 10:07:53 -0800351 memcpy_to_float_from_q4_27(
352 mConfig.inputCfg.buffer.f32,
353 mConfig.inputCfg.buffer.s32,
354 mConfig.inputCfg.buffer.frameCount);
355 } else
356#endif
357 {
358 memcpy_to_i16_from_q4_27(
359 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700360 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800361 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700362 }
rago94a1ee82017-07-21 15:11:02 -0700363 }
364#ifdef FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800365 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
366 if (!auxType) {
367 if (mInConversionBuffer.get() == nullptr) {
368 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
369 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700370 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800371 memcpy_to_i16_from_float(
372 mInConversionBuffer->audioBuffer()->s16,
373 mInBuffer->audioBuffer()->f32,
374 inChannelCount * mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700375 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800376 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
377 if (mOutConversionBuffer.get() == nullptr) {
378 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
379 goto data_bypass;
380 }
381 memcpy_to_i16_from_float(
382 mOutConversionBuffer->audioBuffer()->s16,
383 mOutBuffer->audioBuffer()->f32,
384 outChannelCount * mConfig.outputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700385 }
386 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800387#endif
388
Mikhail Naganov022b9952017-01-04 16:36:51 -0800389 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800390
391#ifdef FLOAT_EFFECT_CHAIN
392 if (!mSupportsFloat) { // convert output int16_t back to float.
393 memcpy_to_float_from_i16(
394 mOutBuffer->audioBuffer()->f32,
395 mOutConversionBuffer->audioBuffer()->s16,
396 outChannelCount * mConfig.outputCfg.buffer.frameCount);
397 }
rago94a1ee82017-07-21 15:11:02 -0700398#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700399 } else {
rago94a1ee82017-07-21 15:11:02 -0700400#ifdef FLOAT_EFFECT_CHAIN
401 data_bypass:
402#endif
403 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800404 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700405 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800406 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700407 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800408 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700409 }
410 }
411 ret = -ENODATA;
412 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800413
Eric Laurentca7cc822012-11-19 14:55:58 -0800414 // force transition to IDLE state when engine is ready
415 if (mState == STOPPED && ret == -ENODATA) {
416 mDisableWaitCnt = 1;
417 }
418
419 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700420 if (auxType) {
421 // input always q4_27 regardless of FLOAT_EFFECT_CHAIN.
422 const size_t size =
423 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
424 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800425 }
426 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700427 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800428 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
429 // If an insert effect is idle and input buffer is different from output buffer,
430 // accumulate input onto output
431 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800432 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
433 accumulateInputToOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800434 }
435 }
436}
437
438void AudioFlinger::EffectModule::reset_l()
439{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700440 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800441 return;
442 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700443 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800444}
445
446status_t AudioFlinger::EffectModule::configure()
447{
rago94a1ee82017-07-21 15:11:02 -0700448 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700449 status_t status;
450 sp<ThreadBase> thread;
451 uint32_t size;
452 audio_channel_mask_t channelMask;
453
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700454 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700455 status = NO_INIT;
456 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800457 }
458
Eric Laurentd0ebb532013-04-02 16:41:41 -0700459 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800460 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700461 status = DEAD_OBJECT;
462 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800463 }
464
465 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700466 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700467 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800468
469 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
470 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900471 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
472 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800473 } else {
474 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700475 // TODO: Update this logic when multichannel effects are implemented.
476 // For offloaded tracks consider mono output as stereo for proper effect initialization
477 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
478 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
479 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
480 ALOGV("Overriding effect input and output as STEREO");
481 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800482 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700483
rago94a1ee82017-07-21 15:11:02 -0700484 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
485 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800486 mConfig.inputCfg.samplingRate = thread->sampleRate();
487 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
488 mConfig.inputCfg.bufferProvider.cookie = NULL;
489 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
490 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
491 mConfig.outputCfg.bufferProvider.cookie = NULL;
492 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
493 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
494 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
495 // Insert effect:
496 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
497 // always overwrites output buffer: input buffer == output buffer
498 // - in other sessions:
499 // last effect in the chain accumulates in output buffer: input buffer != output buffer
500 // other effect: overwrites output buffer: input buffer == output buffer
501 // Auxiliary effect:
502 // accumulates in output buffer: input buffer != output buffer
503 // Therefore: accumulate <=> input buffer != output buffer
504 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
505 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
506 } else {
507 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
508 }
509 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
510 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
511 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
512 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
513
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700514 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800515 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
516
517 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700518 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700519 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
520 sizeof(effect_config_t),
521 &mConfig,
522 &size,
523 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700524 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800525 status = cmdStatus;
rago94a1ee82017-07-21 15:11:02 -0700526#ifdef FLOAT_EFFECT_CHAIN
527 mSupportsFloat = true;
528#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800529 }
rago94a1ee82017-07-21 15:11:02 -0700530#ifdef FLOAT_EFFECT_CHAIN
531 else {
532 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
533 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
534 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
535 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
536 sizeof(effect_config_t),
537 &mConfig,
538 &size,
539 &cmdStatus);
540 if (status == NO_ERROR) {
541 status = cmdStatus;
542 mSupportsFloat = false;
543 ALOGVV("config worked with 16 bit");
544 } else {
545 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800546 }
rago94a1ee82017-07-21 15:11:02 -0700547 }
548#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800549
rago94a1ee82017-07-21 15:11:02 -0700550 if (status == NO_ERROR) {
551 // Establish Buffer strategy
552 setInBuffer(mInBuffer);
553 setOutBuffer(mOutBuffer);
554
555 // Update visualizer latency
556 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
557 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
558 effect_param_t *p = (effect_param_t *)buf32;
559
560 p->psize = sizeof(uint32_t);
561 p->vsize = sizeof(uint32_t);
562 size = sizeof(int);
563 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
564
565 uint32_t latency = 0;
566 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
567 if (pbt != NULL) {
568 latency = pbt->latency_l();
569 }
570
571 *((int32_t *)p->data + 1)= latency;
572 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
573 sizeof(effect_param_t) + 8,
574 &buf32,
575 &size,
576 &cmdStatus);
577 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800578 }
579
580 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
581 (1000 * mConfig.outputCfg.buffer.frameCount);
582
Eric Laurentd0ebb532013-04-02 16:41:41 -0700583exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800584 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700585 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));
Andy Hung6f88dc42017-12-13 16:19:39 -0800899
900 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800901 if (buffer != 0) {
902 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
903 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
904 } else {
905 mConfig.inputCfg.buffer.raw = NULL;
906 }
907 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800908 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700909
910#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800911 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -0700912 // Theoretically insert effects can also do in-place conversions (destroying
913 // the original buffer) when the output buffer is identical to the input buffer,
914 // but we don't optimize for it here.
915 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
916 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
917 // we need to translate - create hidl shared buffer and intercept
918 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
919 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
920 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
921
922 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
923 __func__, inChannels, inFrameCount, size);
924
Andy Hungbded9c82017-11-30 18:47:35 -0800925 if (size > 0 && (mInConversionBuffer.get() == nullptr
926 || size > mInConversionBuffer->getSize())) {
927 mInConversionBuffer.clear();
928 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
929 (void)EffectBufferHalInterface::allocate(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700930 }
Andy Hungbded9c82017-11-30 18:47:35 -0800931 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -0800932 mInConversionBuffer->setFrameCount(inFrameCount);
933 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700934 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800935 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700936 }
937 }
938#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800939}
940
941void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700942 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800943
944 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800945 if (buffer != 0) {
946 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
947 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
948 } else {
949 mConfig.outputCfg.buffer.raw = NULL;
950 }
951 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800952 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700953
954#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800955 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -0700956 // can do in-place conversion from int16_t to float. We don't optimize here.
957 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
958 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
959 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
960 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
961
962 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
963 __func__, outChannels, outFrameCount, size);
964
Andy Hungbded9c82017-11-30 18:47:35 -0800965 if (size > 0 && (mOutConversionBuffer.get() == nullptr
966 || size > mOutConversionBuffer->getSize())) {
967 mOutConversionBuffer.clear();
968 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
969 (void)EffectBufferHalInterface::allocate(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700970 }
Andy Hungbded9c82017-11-30 18:47:35 -0800971 if (mOutConversionBuffer.get() != nullptr) {
972 mOutConversionBuffer->setFrameCount(outFrameCount);
973 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700974 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800975 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700976 }
977 }
978#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800979}
980
Eric Laurentca7cc822012-11-19 14:55:58 -0800981status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
982{
983 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700984 if (mStatus != NO_ERROR) {
985 return mStatus;
986 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800987 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800988 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
989 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
990 if (isProcessEnabled() &&
991 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
992 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800993 uint32_t volume[2];
994 uint32_t *pVolume = NULL;
995 uint32_t size = sizeof(volume);
996 volume[0] = *left;
997 volume[1] = *right;
998 if (controller) {
999 pVolume = volume;
1000 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001001 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1002 size,
1003 volume,
1004 &size,
1005 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001006 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1007 *left = volume[0];
1008 *right = volume[1];
1009 }
1010 }
1011 return status;
1012}
1013
1014status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1015{
1016 if (device == AUDIO_DEVICE_NONE) {
1017 return NO_ERROR;
1018 }
1019
1020 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001021 if (mStatus != NO_ERROR) {
1022 return mStatus;
1023 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001024 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001025 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001026 status_t cmdStatus;
1027 uint32_t size = sizeof(status_t);
1028 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1029 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001030 status = mEffectInterface->command(cmd,
1031 sizeof(uint32_t),
1032 &device,
1033 &size,
1034 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001035 }
1036 return status;
1037}
1038
1039status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1040{
1041 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001042 if (mStatus != NO_ERROR) {
1043 return mStatus;
1044 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001045 status_t status = NO_ERROR;
1046 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1047 status_t cmdStatus;
1048 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001049 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1050 sizeof(audio_mode_t),
1051 &mode,
1052 &size,
1053 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001054 if (status == NO_ERROR) {
1055 status = cmdStatus;
1056 }
1057 }
1058 return status;
1059}
1060
1061status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1062{
1063 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001064 if (mStatus != NO_ERROR) {
1065 return mStatus;
1066 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001067 status_t status = NO_ERROR;
1068 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1069 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001070 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1071 sizeof(audio_source_t),
1072 &source,
1073 &size,
1074 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 }
1076 return status;
1077}
1078
1079void AudioFlinger::EffectModule::setSuspended(bool suspended)
1080{
1081 Mutex::Autolock _l(mLock);
1082 mSuspended = suspended;
1083}
1084
1085bool AudioFlinger::EffectModule::suspended() const
1086{
1087 Mutex::Autolock _l(mLock);
1088 return mSuspended;
1089}
1090
1091bool AudioFlinger::EffectModule::purgeHandles()
1092{
1093 bool enabled = false;
1094 Mutex::Autolock _l(mLock);
1095 for (size_t i = 0; i < mHandles.size(); i++) {
1096 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001097 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001098 if (handle->hasControl()) {
1099 enabled = handle->enabled();
1100 }
1101 }
1102 }
1103 return enabled;
1104}
1105
Eric Laurent5baf2af2013-09-12 17:37:00 -07001106status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1107{
1108 Mutex::Autolock _l(mLock);
1109 if (mStatus != NO_ERROR) {
1110 return mStatus;
1111 }
1112 status_t status = NO_ERROR;
1113 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1114 status_t cmdStatus;
1115 uint32_t size = sizeof(status_t);
1116 effect_offload_param_t cmd;
1117
1118 cmd.isOffload = offloaded;
1119 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001120 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1121 sizeof(effect_offload_param_t),
1122 &cmd,
1123 &size,
1124 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001125 if (status == NO_ERROR) {
1126 status = cmdStatus;
1127 }
1128 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1129 } else {
1130 if (offloaded) {
1131 status = INVALID_OPERATION;
1132 }
1133 mOffloaded = false;
1134 }
1135 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1136 return status;
1137}
1138
1139bool AudioFlinger::EffectModule::isOffloaded() const
1140{
1141 Mutex::Autolock _l(mLock);
1142 return mOffloaded;
1143}
1144
Marco Nelissenb2208842014-02-07 14:00:50 -08001145String8 effectFlagsToString(uint32_t flags) {
1146 String8 s;
1147
1148 s.append("conn. mode: ");
1149 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1150 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1151 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1152 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1153 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1154 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1155 default: s.append("unknown/reserved"); break;
1156 }
1157 s.append(", ");
1158
1159 s.append("insert pref: ");
1160 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1161 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1162 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1163 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1164 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1165 default: s.append("unknown/reserved"); break;
1166 }
1167 s.append(", ");
1168
1169 s.append("volume mgmt: ");
1170 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1171 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1172 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1173 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1174 default: s.append("unknown/reserved"); break;
1175 }
1176 s.append(", ");
1177
1178 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1179 if (devind) {
1180 s.append("device indication: ");
1181 switch (devind) {
1182 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1183 default: s.append("unknown/reserved"); break;
1184 }
1185 s.append(", ");
1186 }
1187
1188 s.append("input mode: ");
1189 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1190 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1191 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1192 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1193 default: s.append("not set"); break;
1194 }
1195 s.append(", ");
1196
1197 s.append("output mode: ");
1198 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1199 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1200 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1201 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1202 default: s.append("not set"); break;
1203 }
1204 s.append(", ");
1205
1206 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1207 if (accel) {
1208 s.append("hardware acceleration: ");
1209 switch (accel) {
1210 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1211 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1212 default: s.append("unknown/reserved"); break;
1213 }
1214 s.append(", ");
1215 }
1216
1217 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1218 if (modeind) {
1219 s.append("mode indication: ");
1220 switch (modeind) {
1221 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1222 default: s.append("unknown/reserved"); break;
1223 }
1224 s.append(", ");
1225 }
1226
1227 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1228 if (srcind) {
1229 s.append("source indication: ");
1230 switch (srcind) {
1231 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1232 default: s.append("unknown/reserved"); break;
1233 }
1234 s.append(", ");
1235 }
1236
1237 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1238 s.append("offloadable, ");
1239 }
1240
1241 int len = s.length();
1242 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001243 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001244 s.unlockBuffer(len - 2);
1245 }
1246 return s;
1247}
1248
Andy Hungbded9c82017-11-30 18:47:35 -08001249static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1250 std::stringstream ss;
1251
1252 if (buffer.get() == nullptr) {
1253 return "nullptr"; // make different than below
1254 } else if (buffer->externalData() != nullptr) {
1255 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1256 << " -> "
1257 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1258 } else {
1259 ss << buffer->audioBuffer()->raw;
1260 }
1261 return ss.str();
1262}
Marco Nelissenb2208842014-02-07 14:00:50 -08001263
Glenn Kasten0f11b512014-01-31 16:18:54 -08001264void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001265{
1266 const size_t SIZE = 256;
1267 char buffer[SIZE];
1268 String8 result;
1269
1270 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1271 result.append(buffer);
1272
1273 bool locked = AudioFlinger::dumpTryLock(mLock);
1274 // failed to lock - AudioFlinger is probably deadlocked
1275 if (!locked) {
1276 result.append("\t\tCould not lock Fx mutex:\n");
1277 }
1278
1279 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001280 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001281 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001282 result.append(buffer);
1283
1284 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001285 char uuidStr[64];
1286 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1287 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001288 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001289 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1290 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001291 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001292 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001293 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001294 mDescriptor.flags,
1295 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 result.append(buffer);
1297 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1298 mDescriptor.name);
1299 result.append(buffer);
1300 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1301 mDescriptor.implementor);
1302 result.append(buffer);
1303
1304 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001305 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001306 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001307 mConfig.inputCfg.buffer.frameCount,
1308 mConfig.inputCfg.samplingRate,
1309 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001310 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001311 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001312 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001313 result.append(buffer);
1314
1315 result.append("\t\t- Output configuration:\n");
1316 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001317 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001318 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001319 mConfig.outputCfg.buffer.frameCount,
1320 mConfig.outputCfg.samplingRate,
1321 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001322 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001323 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001324 result.append(buffer);
1325
rago94a1ee82017-07-21 15:11:02 -07001326#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001327
Andy Hungbded9c82017-11-30 18:47:35 -08001328 result.appendFormat("\t\t- HAL buffers:\n"
1329 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1330 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1331 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1332 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1333 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001334#endif
1335
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001336 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001337 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001338 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001339 for (size_t i = 0; i < mHandles.size(); ++i) {
1340 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001341 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001342 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001343 result.append(buffer);
1344 }
1345 }
1346
Eric Laurentca7cc822012-11-19 14:55:58 -08001347 write(fd, result.string(), result.length());
1348
1349 if (locked) {
1350 mLock.unlock();
1351 }
1352}
1353
1354// ----------------------------------------------------------------------------
1355// EffectHandle implementation
1356// ----------------------------------------------------------------------------
1357
1358#undef LOG_TAG
1359#define LOG_TAG "AudioFlinger::EffectHandle"
1360
1361AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1362 const sp<AudioFlinger::Client>& client,
1363 const sp<IEffectClient>& effectClient,
1364 int32_t priority)
1365 : BnEffect(),
1366 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001367 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001368{
1369 ALOGV("constructor %p", this);
1370
1371 if (client == 0) {
1372 return;
1373 }
1374 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1375 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001376 if (mCblkMemory == 0 ||
1377 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001378 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001379 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001380 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001381 return;
1382 }
Glenn Kastene75da402013-11-20 13:54:52 -08001383 new(mCblk) effect_param_cblk_t();
1384 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001385}
1386
1387AudioFlinger::EffectHandle::~EffectHandle()
1388{
1389 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001390 disconnect(false);
1391}
1392
Glenn Kastene75da402013-11-20 13:54:52 -08001393status_t AudioFlinger::EffectHandle::initCheck()
1394{
1395 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1396}
1397
Eric Laurentca7cc822012-11-19 14:55:58 -08001398status_t AudioFlinger::EffectHandle::enable()
1399{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001400 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001401 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001402 sp<EffectModule> effect = mEffect.promote();
1403 if (effect == 0 || mDisconnected) {
1404 return DEAD_OBJECT;
1405 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001406 if (!mHasControl) {
1407 return INVALID_OPERATION;
1408 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001409
1410 if (mEnabled) {
1411 return NO_ERROR;
1412 }
1413
1414 mEnabled = true;
1415
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001416 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001418 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 }
1420
1421 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001422 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001423 return NO_ERROR;
1424 }
1425
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001426 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 if (status != NO_ERROR) {
1428 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001429 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001430 }
1431 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001432 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001433 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001434 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1435 Mutex::Autolock _l(thread->mLock);
1436 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001437 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001438 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001439 if (thread->type() == ThreadBase::OFFLOAD) {
1440 PlaybackThread *t = (PlaybackThread *)thread.get();
1441 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1442 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001443 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001444 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1445 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001446 }
1447 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001448 }
1449 return status;
1450}
1451
1452status_t AudioFlinger::EffectHandle::disable()
1453{
1454 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001455 AutoMutex _l(mLock);
1456 sp<EffectModule> effect = mEffect.promote();
1457 if (effect == 0 || mDisconnected) {
1458 return DEAD_OBJECT;
1459 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001460 if (!mHasControl) {
1461 return INVALID_OPERATION;
1462 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001463
1464 if (!mEnabled) {
1465 return NO_ERROR;
1466 }
1467 mEnabled = false;
1468
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001469 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001470 return NO_ERROR;
1471 }
1472
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001473 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001474
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001475 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001476 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001477 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001478 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1479 Mutex::Autolock _l(thread->mLock);
1480 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001481 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 }
1483
1484 return status;
1485}
1486
1487void AudioFlinger::EffectHandle::disconnect()
1488{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001489 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001490 disconnect(true);
1491}
1492
1493void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1494{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001495 AutoMutex _l(mLock);
1496 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1497 if (mDisconnected) {
1498 if (unpinIfLast) {
1499 android_errorWriteLog(0x534e4554, "32707507");
1500 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001501 return;
1502 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001503 mDisconnected = true;
1504 sp<ThreadBase> thread;
1505 {
1506 sp<EffectModule> effect = mEffect.promote();
1507 if (effect != 0) {
1508 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001509 }
1510 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001511 if (thread != 0) {
1512 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001513 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001514 // try to cleanup as much as we can
1515 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001516 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1517 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001518 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001519 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001520
Eric Laurentca7cc822012-11-19 14:55:58 -08001521 if (mClient != 0) {
1522 if (mCblk != NULL) {
1523 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1524 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1525 }
1526 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001527 // Client destructor must run with AudioFlinger client mutex locked
1528 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001529 mClient.clear();
1530 }
1531}
1532
1533status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1534 uint32_t cmdSize,
1535 void *pCmdData,
1536 uint32_t *replySize,
1537 void *pReplyData)
1538{
1539 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001540 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001541
Eric Laurentc7ab3092017-06-15 18:43:46 -07001542 // reject commands reserved for internal use by audio framework if coming from outside
1543 // of audioserver
1544 switch(cmdCode) {
1545 case EFFECT_CMD_ENABLE:
1546 case EFFECT_CMD_DISABLE:
1547 case EFFECT_CMD_SET_PARAM:
1548 case EFFECT_CMD_SET_PARAM_DEFERRED:
1549 case EFFECT_CMD_SET_PARAM_COMMIT:
1550 case EFFECT_CMD_GET_PARAM:
1551 break;
1552 default:
1553 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1554 break;
1555 }
1556 android_errorWriteLog(0x534e4554, "62019992");
1557 return BAD_VALUE;
1558 }
1559
Eric Laurent1ffc5852016-12-15 14:46:09 -08001560 if (cmdCode == EFFECT_CMD_ENABLE) {
1561 if (*replySize < sizeof(int)) {
1562 android_errorWriteLog(0x534e4554, "32095713");
1563 return BAD_VALUE;
1564 }
1565 *(int *)pReplyData = NO_ERROR;
1566 *replySize = sizeof(int);
1567 return enable();
1568 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1569 if (*replySize < sizeof(int)) {
1570 android_errorWriteLog(0x534e4554, "32095713");
1571 return BAD_VALUE;
1572 }
1573 *(int *)pReplyData = NO_ERROR;
1574 *replySize = sizeof(int);
1575 return disable();
1576 }
1577
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001578 AutoMutex _l(mLock);
1579 sp<EffectModule> effect = mEffect.promote();
1580 if (effect == 0 || mDisconnected) {
1581 return DEAD_OBJECT;
1582 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001583 // only get parameter command is permitted for applications not controlling the effect
1584 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1585 return INVALID_OPERATION;
1586 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001587 if (mClient == 0) {
1588 return INVALID_OPERATION;
1589 }
1590
1591 // handle commands that are not forwarded transparently to effect engine
1592 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001593 if (*replySize < sizeof(int)) {
1594 android_errorWriteLog(0x534e4554, "32095713");
1595 return BAD_VALUE;
1596 }
1597 *(int *)pReplyData = NO_ERROR;
1598 *replySize = sizeof(int);
1599
Eric Laurentca7cc822012-11-19 14:55:58 -08001600 // No need to trylock() here as this function is executed in the binder thread serving a
1601 // particular client process: no risk to block the whole media server process or mixer
1602 // threads if we are stuck here
1603 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001604 // keep local copy of index in case of client corruption b/32220769
1605 const uint32_t clientIndex = mCblk->clientIndex;
1606 const uint32_t serverIndex = mCblk->serverIndex;
1607 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1608 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001609 mCblk->serverIndex = 0;
1610 mCblk->clientIndex = 0;
1611 return BAD_VALUE;
1612 }
1613 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001614 effect_param_t *param = NULL;
1615 for (uint32_t index = serverIndex; index < clientIndex;) {
1616 int *p = (int *)(mBuffer + index);
1617 const int size = *p++;
1618 if (size < 0
1619 || size > EFFECT_PARAM_BUFFER_SIZE
1620 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001621 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001622 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001623 break;
1624 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001625
1626 // copy to local memory in case of client corruption b/32220769
1627 param = (effect_param_t *)realloc(param, size);
1628 if (param == NULL) {
1629 ALOGW("command(): out of memory");
1630 status = NO_MEMORY;
1631 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001632 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001633 memcpy(param, p, size);
1634
1635 int reply = 0;
1636 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001637 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001638 size,
1639 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001640 &rsize,
1641 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001642
1643 // verify shared memory: server index shouldn't change; client index can't go back.
1644 if (serverIndex != mCblk->serverIndex
1645 || clientIndex > mCblk->clientIndex) {
1646 android_errorWriteLog(0x534e4554, "32220769");
1647 status = BAD_VALUE;
1648 break;
1649 }
1650
Eric Laurentca7cc822012-11-19 14:55:58 -08001651 // stop at first error encountered
1652 if (ret != NO_ERROR) {
1653 status = ret;
1654 *(int *)pReplyData = reply;
1655 break;
1656 } else if (reply != NO_ERROR) {
1657 *(int *)pReplyData = reply;
1658 break;
1659 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001660 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001661 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001662 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 mCblk->serverIndex = 0;
1664 mCblk->clientIndex = 0;
1665 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 }
1667
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001668 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001669}
1670
1671void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1672{
1673 ALOGV("setControl %p control %d", this, hasControl);
1674
1675 mHasControl = hasControl;
1676 mEnabled = enabled;
1677
1678 if (signal && mEffectClient != 0) {
1679 mEffectClient->controlStatusChanged(hasControl);
1680 }
1681}
1682
1683void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1684 uint32_t cmdSize,
1685 void *pCmdData,
1686 uint32_t replySize,
1687 void *pReplyData)
1688{
1689 if (mEffectClient != 0) {
1690 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1691 }
1692}
1693
1694
1695
1696void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1697{
1698 if (mEffectClient != 0) {
1699 mEffectClient->enableStatusChanged(enabled);
1700 }
1701}
1702
1703status_t AudioFlinger::EffectHandle::onTransact(
1704 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1705{
1706 return BnEffect::onTransact(code, data, reply, flags);
1707}
1708
1709
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001710void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001711{
1712 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1713
Marco Nelissenb2208842014-02-07 14:00:50 -08001714 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001715 (mClient == 0) ? getpid_cached : mClient->pid(),
1716 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001717 mHasControl ? "yes" : "no",
1718 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001719 mCblk ? mCblk->clientIndex : 0,
1720 mCblk ? mCblk->serverIndex : 0
1721 );
1722
1723 if (locked) {
1724 mCblk->lock.unlock();
1725 }
1726}
1727
1728#undef LOG_TAG
1729#define LOG_TAG "AudioFlinger::EffectChain"
1730
1731AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001732 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001733 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001734 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001735 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001736{
1737 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1738 if (thread == NULL) {
1739 return;
1740 }
1741 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1742 thread->frameCount();
1743}
1744
1745AudioFlinger::EffectChain::~EffectChain()
1746{
Eric Laurentca7cc822012-11-19 14:55:58 -08001747}
1748
1749// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1750sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1751 effect_descriptor_t *descriptor)
1752{
1753 size_t size = mEffects.size();
1754
1755 for (size_t i = 0; i < size; i++) {
1756 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1757 return mEffects[i];
1758 }
1759 }
1760 return 0;
1761}
1762
1763// getEffectFromId_l() must be called with ThreadBase::mLock held
1764sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1765{
1766 size_t size = mEffects.size();
1767
1768 for (size_t i = 0; i < size; i++) {
1769 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1770 if (id == 0 || mEffects[i]->id() == id) {
1771 return mEffects[i];
1772 }
1773 }
1774 return 0;
1775}
1776
1777// getEffectFromType_l() must be called with ThreadBase::mLock held
1778sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1779 const effect_uuid_t *type)
1780{
1781 size_t size = mEffects.size();
1782
1783 for (size_t i = 0; i < size; i++) {
1784 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1785 return mEffects[i];
1786 }
1787 }
1788 return 0;
1789}
1790
1791void AudioFlinger::EffectChain::clearInputBuffer()
1792{
1793 Mutex::Autolock _l(mLock);
1794 sp<ThreadBase> thread = mThread.promote();
1795 if (thread == 0) {
1796 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1797 return;
1798 }
1799 clearInputBuffer_l(thread);
1800}
1801
1802// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001803void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001804{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001805 if (mInBuffer == NULL) {
1806 return;
1807 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001808 // TODO: This will change in the future, depending on multichannel
1809 // and sample format changes for effects.
1810 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1811 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001812
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001813 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001814 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1815 * std::min((uint32_t)FCC_2, thread->channelCount());
1816
Mikhail Naganov022b9952017-01-04 16:36:51 -08001817 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1818 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001819}
1820
1821// Must be called with EffectChain::mLock locked
1822void AudioFlinger::EffectChain::process_l()
1823{
1824 sp<ThreadBase> thread = mThread.promote();
1825 if (thread == 0) {
1826 ALOGW("process_l(): cannot promote mixer thread");
1827 return;
1828 }
1829 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1830 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001831 // never process effects when:
1832 // - on an OFFLOAD thread
1833 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001834 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1835 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001836 if (!isGlobalSession) {
1837 bool tracksOnSession = (trackCnt() != 0);
1838
1839 if (!tracksOnSession && mTailBufferCount == 0) {
1840 doProcess = false;
1841 }
1842
1843 if (activeTrackCnt() == 0) {
1844 // if no track is active and the effect tail has not been rendered,
1845 // the input buffer must be cleared here as the mixer process will not do it
1846 if (tracksOnSession || mTailBufferCount > 0) {
1847 clearInputBuffer_l(thread);
1848 if (mTailBufferCount > 0) {
1849 mTailBufferCount--;
1850 }
1851 }
1852 }
1853 }
1854
1855 size_t size = mEffects.size();
1856 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001857 // Only the input and output buffers of the chain can be external,
1858 // and 'update' / 'commit' do nothing for allocated buffers, thus
1859 // it's not needed to consider any other buffers here.
1860 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001861 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1862 mOutBuffer->update();
1863 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 for (size_t i = 0; i < size; i++) {
1865 mEffects[i]->process();
1866 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001867 mInBuffer->commit();
1868 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1869 mOutBuffer->commit();
1870 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001871 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001872 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001873 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001874 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1875 }
1876 if (doResetVolume) {
1877 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001878 }
1879}
1880
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001881// createEffect_l() must be called with ThreadBase::mLock held
1882status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1883 ThreadBase *thread,
1884 effect_descriptor_t *desc,
1885 int id,
1886 audio_session_t sessionId,
1887 bool pinned)
1888{
1889 Mutex::Autolock _l(mLock);
1890 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1891 status_t lStatus = effect->status();
1892 if (lStatus == NO_ERROR) {
1893 lStatus = addEffect_ll(effect);
1894 }
1895 if (lStatus != NO_ERROR) {
1896 effect.clear();
1897 }
1898 return lStatus;
1899}
1900
1901// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001902status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1903{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001904 Mutex::Autolock _l(mLock);
1905 return addEffect_ll(effect);
1906}
1907// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1908status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1909{
Eric Laurentca7cc822012-11-19 14:55:58 -08001910 effect_descriptor_t desc = effect->desc();
1911 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1912
Eric Laurentca7cc822012-11-19 14:55:58 -08001913 effect->setChain(this);
1914 sp<ThreadBase> thread = mThread.promote();
1915 if (thread == 0) {
1916 return NO_INIT;
1917 }
1918 effect->setThread(thread);
1919
1920 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1921 // Auxiliary effects are inserted at the beginning of mEffects vector as
1922 // they are processed first and accumulated in chain input buffer
1923 mEffects.insertAt(effect, 0);
1924
1925 // the input buffer for auxiliary effect contains mono samples in
1926 // 32 bit format. This is to avoid saturation in AudoMixer
1927 // accumulation stage. Saturation is done in EffectModule::process() before
1928 // calling the process in effect engine
1929 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001930 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001931#ifdef FLOAT_EFFECT_CHAIN
1932 status_t result = EffectBufferHalInterface::allocate(
1933 numSamples * sizeof(float), &halBuffer);
1934#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001935 status_t result = EffectBufferHalInterface::allocate(
1936 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001937#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001938 if (result != OK) return result;
1939 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001940 // auxiliary effects output samples to chain input buffer for further processing
1941 // by insert effects
1942 effect->setOutBuffer(mInBuffer);
1943 } else {
1944 // Insert effects are inserted at the end of mEffects vector as they are processed
1945 // after track and auxiliary effects.
1946 // Insert effect order as a function of indicated preference:
1947 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1948 // another effect is present
1949 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1950 // last effect claiming first position
1951 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1952 // first effect claiming last position
1953 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1954 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1955 // already present
1956
1957 size_t size = mEffects.size();
1958 size_t idx_insert = size;
1959 ssize_t idx_insert_first = -1;
1960 ssize_t idx_insert_last = -1;
1961
1962 for (size_t i = 0; i < size; i++) {
1963 effect_descriptor_t d = mEffects[i]->desc();
1964 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1965 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1966 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1967 // check invalid effect chaining combinations
1968 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1969 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1970 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1971 desc.name, d.name);
1972 return INVALID_OPERATION;
1973 }
1974 // remember position of first insert effect and by default
1975 // select this as insert position for new effect
1976 if (idx_insert == size) {
1977 idx_insert = i;
1978 }
1979 // remember position of last insert effect claiming
1980 // first position
1981 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1982 idx_insert_first = i;
1983 }
1984 // remember position of first insert effect claiming
1985 // last position
1986 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1987 idx_insert_last == -1) {
1988 idx_insert_last = i;
1989 }
1990 }
1991 }
1992
1993 // modify idx_insert from first position if needed
1994 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1995 if (idx_insert_last != -1) {
1996 idx_insert = idx_insert_last;
1997 } else {
1998 idx_insert = size;
1999 }
2000 } else {
2001 if (idx_insert_first != -1) {
2002 idx_insert = idx_insert_first + 1;
2003 }
2004 }
2005
2006 // always read samples from chain input buffer
2007 effect->setInBuffer(mInBuffer);
2008
2009 // if last effect in the chain, output samples to chain
2010 // output buffer, otherwise to chain input buffer
2011 if (idx_insert == size) {
2012 if (idx_insert != 0) {
2013 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2014 mEffects[idx_insert-1]->configure();
2015 }
2016 effect->setOutBuffer(mOutBuffer);
2017 } else {
2018 effect->setOutBuffer(mInBuffer);
2019 }
2020 mEffects.insertAt(effect, idx_insert);
2021
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002022 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002023 idx_insert);
2024 }
2025 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002026
Eric Laurentca7cc822012-11-19 14:55:58 -08002027 return NO_ERROR;
2028}
2029
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002030// removeEffect_l() must be called with ThreadBase::mLock held
2031size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2032 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002033{
2034 Mutex::Autolock _l(mLock);
2035 size_t size = mEffects.size();
2036 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2037
2038 for (size_t i = 0; i < size; i++) {
2039 if (effect == mEffects[i]) {
2040 // calling stop here will remove pre-processing effect from the audio HAL.
2041 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2042 // the middle of a read from audio HAL
2043 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2044 mEffects[i]->state() == EffectModule::STOPPING) {
2045 mEffects[i]->stop();
2046 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002047 if (release) {
2048 mEffects[i]->release_l();
2049 }
2050
Mikhail Naganov022b9952017-01-04 16:36:51 -08002051 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002052 if (i == size - 1 && i != 0) {
2053 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2054 mEffects[i - 1]->configure();
2055 }
2056 }
2057 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002058 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002059 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002060
Eric Laurentca7cc822012-11-19 14:55:58 -08002061 break;
2062 }
2063 }
2064
2065 return mEffects.size();
2066}
2067
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002068// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002069void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2070{
2071 size_t size = mEffects.size();
2072 for (size_t i = 0; i < size; i++) {
2073 mEffects[i]->setDevice(device);
2074 }
2075}
2076
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002077// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002078void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2079{
2080 size_t size = mEffects.size();
2081 for (size_t i = 0; i < size; i++) {
2082 mEffects[i]->setMode(mode);
2083 }
2084}
2085
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002086// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002087void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2088{
2089 size_t size = mEffects.size();
2090 for (size_t i = 0; i < size; i++) {
2091 mEffects[i]->setAudioSource(source);
2092 }
2093}
2094
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002095// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002096bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002097{
2098 uint32_t newLeft = *left;
2099 uint32_t newRight = *right;
2100 bool hasControl = false;
2101 int ctrlIdx = -1;
2102 size_t size = mEffects.size();
2103
2104 // first update volume controller
2105 for (size_t i = size; i > 0; i--) {
2106 if (mEffects[i - 1]->isProcessEnabled() &&
2107 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2108 ctrlIdx = i - 1;
2109 hasControl = true;
2110 break;
2111 }
2112 }
2113
Eric Laurentfa1e1232016-08-02 19:01:49 -07002114 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002115 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002116 if (hasControl) {
2117 *left = mNewLeftVolume;
2118 *right = mNewRightVolume;
2119 }
2120 return hasControl;
2121 }
2122
2123 mVolumeCtrlIdx = ctrlIdx;
2124 mLeftVolume = newLeft;
2125 mRightVolume = newRight;
2126
2127 // second get volume update from volume controller
2128 if (ctrlIdx >= 0) {
2129 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2130 mNewLeftVolume = newLeft;
2131 mNewRightVolume = newRight;
2132 }
2133 // then indicate volume to all other effects in chain.
2134 // Pass altered volume to effects before volume controller
2135 // and requested volume to effects after controller
2136 uint32_t lVol = newLeft;
2137 uint32_t rVol = newRight;
2138
2139 for (size_t i = 0; i < size; i++) {
2140 if ((int)i == ctrlIdx) {
2141 continue;
2142 }
2143 // this also works for ctrlIdx == -1 when there is no volume controller
2144 if ((int)i > ctrlIdx) {
2145 lVol = *left;
2146 rVol = *right;
2147 }
2148 mEffects[i]->setVolume(&lVol, &rVol, false);
2149 }
2150 *left = newLeft;
2151 *right = newRight;
2152
2153 return hasControl;
2154}
2155
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002156// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002157void AudioFlinger::EffectChain::resetVolume_l()
2158{
Eric Laurente7449bf2016-08-03 18:44:07 -07002159 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2160 uint32_t left = mLeftVolume;
2161 uint32_t right = mRightVolume;
2162 (void)setVolume_l(&left, &right, true);
2163 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002164}
2165
Eric Laurent1b928682014-10-02 19:41:47 -07002166void AudioFlinger::EffectChain::syncHalEffectsState()
2167{
2168 Mutex::Autolock _l(mLock);
2169 for (size_t i = 0; i < mEffects.size(); i++) {
2170 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2171 mEffects[i]->state() == EffectModule::STOPPING) {
2172 mEffects[i]->addEffectToHal_l();
2173 }
2174 }
2175}
2176
Eric Laurentca7cc822012-11-19 14:55:58 -08002177void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2178{
2179 const size_t SIZE = 256;
2180 char buffer[SIZE];
2181 String8 result;
2182
Marco Nelissenb2208842014-02-07 14:00:50 -08002183 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002184 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002185 result.append(buffer);
2186
Marco Nelissenb2208842014-02-07 14:00:50 -08002187 if (numEffects) {
2188 bool locked = AudioFlinger::dumpTryLock(mLock);
2189 // failed to lock - AudioFlinger is probably deadlocked
2190 if (!locked) {
2191 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002192 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002193
Andy Hungbded9c82017-11-30 18:47:35 -08002194 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2195 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2196 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2197 (int)inBufferStr.size(), "In buffer ",
2198 (int)outBufferStr.size(), "Out buffer ");
2199 result.appendFormat("\t%s %s %d\n",
2200 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002201 write(fd, result.string(), result.size());
2202
2203 for (size_t i = 0; i < numEffects; ++i) {
2204 sp<EffectModule> effect = mEffects[i];
2205 if (effect != 0) {
2206 effect->dump(fd, args);
2207 }
2208 }
2209
2210 if (locked) {
2211 mLock.unlock();
2212 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002213 }
2214}
2215
2216// must be called with ThreadBase::mLock held
2217void AudioFlinger::EffectChain::setEffectSuspended_l(
2218 const effect_uuid_t *type, bool suspend)
2219{
2220 sp<SuspendedEffectDesc> desc;
2221 // use effect type UUID timelow as key as there is no real risk of identical
2222 // timeLow fields among effect type UUIDs.
2223 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2224 if (suspend) {
2225 if (index >= 0) {
2226 desc = mSuspendedEffects.valueAt(index);
2227 } else {
2228 desc = new SuspendedEffectDesc();
2229 desc->mType = *type;
2230 mSuspendedEffects.add(type->timeLow, desc);
2231 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2232 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002233
Eric Laurentca7cc822012-11-19 14:55:58 -08002234 if (desc->mRefCount++ == 0) {
2235 sp<EffectModule> effect = getEffectIfEnabled(type);
2236 if (effect != 0) {
2237 desc->mEffect = effect;
2238 effect->setSuspended(true);
2239 effect->setEnabled(false);
2240 }
2241 }
2242 } else {
2243 if (index < 0) {
2244 return;
2245 }
2246 desc = mSuspendedEffects.valueAt(index);
2247 if (desc->mRefCount <= 0) {
2248 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002249 desc->mRefCount = 0;
2250 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002251 }
2252 if (--desc->mRefCount == 0) {
2253 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2254 if (desc->mEffect != 0) {
2255 sp<EffectModule> effect = desc->mEffect.promote();
2256 if (effect != 0) {
2257 effect->setSuspended(false);
2258 effect->lock();
2259 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002260 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002261 effect->setEnabled_l(handle->enabled());
2262 }
2263 effect->unlock();
2264 }
2265 desc->mEffect.clear();
2266 }
2267 mSuspendedEffects.removeItemsAt(index);
2268 }
2269 }
2270}
2271
2272// must be called with ThreadBase::mLock held
2273void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2274{
2275 sp<SuspendedEffectDesc> desc;
2276
2277 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2278 if (suspend) {
2279 if (index >= 0) {
2280 desc = mSuspendedEffects.valueAt(index);
2281 } else {
2282 desc = new SuspendedEffectDesc();
2283 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2284 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2285 }
2286 if (desc->mRefCount++ == 0) {
2287 Vector< sp<EffectModule> > effects;
2288 getSuspendEligibleEffects(effects);
2289 for (size_t i = 0; i < effects.size(); i++) {
2290 setEffectSuspended_l(&effects[i]->desc().type, true);
2291 }
2292 }
2293 } else {
2294 if (index < 0) {
2295 return;
2296 }
2297 desc = mSuspendedEffects.valueAt(index);
2298 if (desc->mRefCount <= 0) {
2299 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2300 desc->mRefCount = 1;
2301 }
2302 if (--desc->mRefCount == 0) {
2303 Vector<const effect_uuid_t *> types;
2304 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2305 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2306 continue;
2307 }
2308 types.add(&mSuspendedEffects.valueAt(i)->mType);
2309 }
2310 for (size_t i = 0; i < types.size(); i++) {
2311 setEffectSuspended_l(types[i], false);
2312 }
2313 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2314 mSuspendedEffects.keyAt(index));
2315 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2316 }
2317 }
2318}
2319
2320
2321// The volume effect is used for automated tests only
2322#ifndef OPENSL_ES_H_
2323static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2324 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2325const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2326#endif //OPENSL_ES_H_
2327
Eric Laurentd8365c52017-07-16 15:27:05 -07002328/* static */
2329bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2330{
2331 // Only NS and AEC are suspended when BtNRec is off
2332 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2333 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2334 return true;
2335 }
2336 return false;
2337}
2338
Eric Laurentca7cc822012-11-19 14:55:58 -08002339bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2340{
2341 // auxiliary effects and visualizer are never suspended on output mix
2342 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2343 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2344 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2345 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2346 return false;
2347 }
2348 return true;
2349}
2350
2351void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2352 Vector< sp<AudioFlinger::EffectModule> > &effects)
2353{
2354 effects.clear();
2355 for (size_t i = 0; i < mEffects.size(); i++) {
2356 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2357 effects.add(mEffects[i]);
2358 }
2359 }
2360}
2361
2362sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2363 const effect_uuid_t *type)
2364{
2365 sp<EffectModule> effect = getEffectFromType_l(type);
2366 return effect != 0 && effect->isEnabled() ? effect : 0;
2367}
2368
2369void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2370 bool enabled)
2371{
2372 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2373 if (enabled) {
2374 if (index < 0) {
2375 // if the effect is not suspend check if all effects are suspended
2376 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2377 if (index < 0) {
2378 return;
2379 }
2380 if (!isEffectEligibleForSuspend(effect->desc())) {
2381 return;
2382 }
2383 setEffectSuspended_l(&effect->desc().type, enabled);
2384 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2385 if (index < 0) {
2386 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2387 return;
2388 }
2389 }
2390 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2391 effect->desc().type.timeLow);
2392 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002393 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002394 if (desc->mEffect == 0) {
2395 desc->mEffect = effect;
2396 effect->setEnabled(false);
2397 effect->setSuspended(true);
2398 }
2399 } else {
2400 if (index < 0) {
2401 return;
2402 }
2403 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2404 effect->desc().type.timeLow);
2405 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2406 desc->mEffect.clear();
2407 effect->setSuspended(false);
2408 }
2409}
2410
Eric Laurent5baf2af2013-09-12 17:37:00 -07002411bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002412{
2413 Mutex::Autolock _l(mLock);
2414 size_t size = mEffects.size();
2415 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002416 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002417 return true;
2418 }
2419 }
2420 return false;
2421}
2422
Eric Laurentaaa44472014-09-12 17:41:50 -07002423void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2424{
2425 Mutex::Autolock _l(mLock);
2426 mThread = thread;
2427 for (size_t i = 0; i < mEffects.size(); i++) {
2428 mEffects[i]->setThread(thread);
2429 }
2430}
2431
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002432void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2433{
2434 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2435 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2436 }
2437 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2438 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2439 }
2440}
2441
2442void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2443{
2444 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2445 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2446 }
2447 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2448 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2449 }
2450}
2451
2452bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002453{
2454 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002455 for (const auto &effect : mEffects) {
2456 if (effect->isProcessImplemented()) {
2457 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002458 }
2459 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002460 // Allow effects without processing.
2461 return true;
2462}
2463
2464bool AudioFlinger::EffectChain::isFastCompatible() const
2465{
2466 Mutex::Autolock _l(mLock);
2467 for (const auto &effect : mEffects) {
2468 if (effect->isProcessImplemented()
2469 && effect->isImplementationSoftware()) {
2470 return false;
2471 }
2472 }
2473 // Allow effects without processing or hw accelerated effects.
2474 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002475}
2476
2477// isCompatibleWithThread_l() must be called with thread->mLock held
2478bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2479{
2480 Mutex::Autolock _l(mLock);
2481 for (size_t i = 0; i < mEffects.size(); i++) {
2482 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2483 return false;
2484 }
2485 }
2486 return true;
2487}
2488
Glenn Kasten63238ef2015-03-02 15:50:29 -08002489} // namespace android