blob: dda9d20c41c98be68c0415458ed4d87689328d59 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
27#include <system/audio_effects/effect_ns.h>
28#include <system/audio_effects/effect_visualizer.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080029#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070030#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070031#include <media/audiohal/EffectHalInterface.h>
32#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080033
34#include "AudioFlinger.h"
35#include "ServiceUtilities.h"
36
37// ----------------------------------------------------------------------------
38
39// Note: the following macro is used for extremely verbose logging message. In
40// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
41// 0; but one side effect of this is to turn all LOGV's as well. Some messages
42// are so verbose that we want to suppress them even when we have ALOG_ASSERT
43// turned on. Do not uncomment the #def below unless you really know what you
44// are doing and want to see all of the extremely verbose messages.
45//#define VERY_VERY_VERBOSE_LOGGING
46#ifdef VERY_VERY_VERBOSE_LOGGING
47#define ALOGVV ALOGV
48#else
49#define ALOGVV(a...) do { } while(0)
50#endif
51
52namespace android {
53
54// ----------------------------------------------------------------------------
55// EffectModule implementation
56// ----------------------------------------------------------------------------
57
58#undef LOG_TAG
59#define LOG_TAG "AudioFlinger::EffectModule"
60
61AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
62 const wp<AudioFlinger::EffectChain>& chain,
63 effect_descriptor_t *desc,
64 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080065 audio_session_t sessionId,
66 bool pinned)
67 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080068 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69 mDescriptor(*desc),
Andy Hungab305162017-12-14 12:42:22 -080070 // clear mConfig to ensure consistent initial value of buffer framecount
71 // in case buffers are associated by setInBuffer() or setOutBuffer()
72 // prior to configure().
73 mConfig{{}, {}},
Eric Laurentca7cc822012-11-19 14:55:58 -080074 mStatus(NO_INIT), mState(IDLE),
Andy Hung62aef7d2017-12-14 14:50:40 -080075 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
76 mDisableWaitCnt(0), // set by process() and updateState()
Eric Laurentaaa44472014-09-12 17:41:50 -070077 mSuspended(false),
Andy Hung62aef7d2017-12-14 14:50:40 -080078 mOffloaded(false),
Eric Laurentaaa44472014-09-12 17:41:50 -070079 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070080#ifdef FLOAT_EFFECT_CHAIN
81 , mSupportsFloat(false)
82#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080083{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080084 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080085 int lStatus;
86
87 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070088 mStatus = -ENODEV;
89 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070090 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070091 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070092 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070093 mStatus = effectsFactory->createEffect(
94 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
95 }
96 }
Eric Laurentca7cc822012-11-19 14:55:58 -080097
98 if (mStatus != NO_ERROR) {
99 return;
100 }
101 lStatus = init();
102 if (lStatus < 0) {
103 mStatus = lStatus;
104 goto Error;
105 }
106
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800107 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700108 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800109
Eric Laurentca7cc822012-11-19 14:55:58 -0800110 return;
111Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700112 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800113 ALOGV("Constructor Error %d", mStatus);
114}
115
116AudioFlinger::EffectModule::~EffectModule()
117{
118 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700119 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700120 char uuidStr[64];
121 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
122 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
123 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800124 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800125 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800126
Eric Laurentca7cc822012-11-19 14:55:58 -0800127}
128
129status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
130{
131 status_t status;
132
133 Mutex::Autolock _l(mLock);
134 int priority = handle->priority();
135 size_t size = mHandles.size();
136 EffectHandle *controlHandle = NULL;
137 size_t i;
138 for (i = 0; i < size; i++) {
139 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800140 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800141 continue;
142 }
143 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700144 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800145 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700146 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800147 if (h->priority() <= priority) {
148 break;
149 }
150 }
151 // if inserted in first place, move effect control from previous owner to this handle
152 if (i == 0) {
153 bool enabled = false;
154 if (controlHandle != NULL) {
155 enabled = controlHandle->enabled();
156 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
157 }
158 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
159 status = NO_ERROR;
160 } else {
161 status = ALREADY_EXISTS;
162 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700163 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800164 mHandles.insertAt(handle, i);
165 return status;
166}
167
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800168ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800169{
170 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800171 return removeHandle_l(handle);
172}
173
174ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
175{
Eric Laurentca7cc822012-11-19 14:55:58 -0800176 size_t size = mHandles.size();
177 size_t i;
178 for (i = 0; i < size; i++) {
179 if (mHandles[i] == handle) {
180 break;
181 }
182 }
183 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800184 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
185 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800186 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800187 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800188
189 mHandles.removeAt(i);
190 // if removed from first place, move effect control from this handle to next in line
191 if (i == 0) {
192 EffectHandle *h = controlHandle_l();
193 if (h != NULL) {
194 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
195 }
196 }
197
198 // Prevent calls to process() and other functions on effect interface from now on.
199 // The effect engine will be released by the destructor when the last strong reference on
200 // this object is released which can happen after next process is called.
201 if (mHandles.size() == 0 && !mPinned) {
202 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800203 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800204 }
205
206 return mHandles.size();
207}
208
209// must be called with EffectModule::mLock held
210AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
211{
212 // the first valid handle in the list has control over the module
213 for (size_t i = 0; i < mHandles.size(); i++) {
214 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800215 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800216 return h;
217 }
218 }
219
220 return NULL;
221}
222
Eric Laurentf10c7092016-12-06 17:09:56 -0800223// unsafe method called when the effect parent thread has been destroyed
224ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
225{
226 ALOGV("disconnect() %p handle %p", this, handle);
227 Mutex::Autolock _l(mLock);
228 ssize_t numHandles = removeHandle_l(handle);
229 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
230 AudioSystem::unregisterEffect(mId);
231 sp<AudioFlinger> af = mAudioFlinger.promote();
232 if (af != 0) {
233 mLock.unlock();
234 af->updateOrphanEffectChains(this);
235 mLock.lock();
236 }
237 }
238 return numHandles;
239}
240
Eric Laurentfa1e1232016-08-02 19:01:49 -0700241bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800242 Mutex::Autolock _l(mLock);
243
Eric Laurentfa1e1232016-08-02 19:01:49 -0700244 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800245 switch (mState) {
246 case RESTART:
247 reset_l();
248 // FALL THROUGH
249
250 case STARTING:
251 // clear auxiliary effect input buffer for next accumulation
252 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
253 memset(mConfig.inputCfg.buffer.raw,
254 0,
255 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
256 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700257 if (start_l() == NO_ERROR) {
258 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700259 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700260 } else {
261 mState = IDLE;
262 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800263 break;
264 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700265 if (stop_l() == NO_ERROR) {
266 mDisableWaitCnt = mMaxDisableWaitCnt;
267 } else {
268 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
269 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800270 mState = STOPPED;
271 break;
272 case STOPPED:
273 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
274 // turn off sequence.
275 if (--mDisableWaitCnt == 0) {
276 reset_l();
277 mState = IDLE;
278 }
279 break;
280 default: //IDLE , ACTIVE, DESTROYED
281 break;
282 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700283
284 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800285}
286
287void AudioFlinger::EffectModule::process()
288{
289 Mutex::Autolock _l(mLock);
290
Mikhail Naganov022b9952017-01-04 16:36:51 -0800291 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800292 return;
293 }
294
rago94a1ee82017-07-21 15:11:02 -0700295 // TODO: Implement multichannel effects; here outChannelCount == FCC_2 == 2
296 const uint32_t inChannelCount =
297 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
298 const uint32_t outChannelCount =
299 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
300 const bool auxType =
301 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
302
Andy Hungfa69ca32017-11-30 10:07:53 -0800303 // safeInputOutputSampleCount is 0 if the channel count between input and output
304 // buffers do not match. This prevents automatic accumulation or copying between the
305 // input and output effect buffers without an intermediary effect process.
306 // TODO: consider implementing channel conversion.
307 const size_t safeInputOutputSampleCount =
308 inChannelCount != outChannelCount ? 0
309 : outChannelCount * std::min(
310 mConfig.inputCfg.buffer.frameCount,
311 mConfig.outputCfg.buffer.frameCount);
312 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
313#ifdef FLOAT_EFFECT_CHAIN
314 accumulate_float(
315 mConfig.outputCfg.buffer.f32,
316 mConfig.inputCfg.buffer.f32,
317 safeInputOutputSampleCount);
318#else
319 accumulate_i16(
320 mConfig.outputCfg.buffer.s16,
321 mConfig.inputCfg.buffer.s16,
322 safeInputOutputSampleCount);
323#endif
324 };
325 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
326#ifdef FLOAT_EFFECT_CHAIN
327 memcpy(
328 mConfig.outputCfg.buffer.f32,
329 mConfig.inputCfg.buffer.f32,
330 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
331
332#else
333 memcpy(
334 mConfig.outputCfg.buffer.s16,
335 mConfig.inputCfg.buffer.s16,
336 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
337#endif
338 };
339
Eric Laurentca7cc822012-11-19 14:55:58 -0800340 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700341 int ret;
342 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700343 if (auxType) {
344 // We overwrite the aux input buffer here and clear after processing.
rago94a1ee82017-07-21 15:11:02 -0700345#ifdef FLOAT_EFFECT_CHAIN
346 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800347#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700348 // Do in-place float conversion for auxiliary effect input buffer.
349 static_assert(sizeof(float) <= sizeof(int32_t),
350 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
351
Andy Hungfa69ca32017-11-30 10:07:53 -0800352 memcpy_to_float_from_q4_27(
353 mConfig.inputCfg.buffer.f32,
354 mConfig.inputCfg.buffer.s32,
355 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800356#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800357 } else
Andy Hung116a4982017-11-30 10:15:08 -0800358#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800359 {
Andy Hung116a4982017-11-30 10:15:08 -0800360#ifdef FLOAT_AUX
361 memcpy_to_i16_from_float(
362 mConfig.inputCfg.buffer.s16,
363 mConfig.inputCfg.buffer.f32,
364 mConfig.inputCfg.buffer.frameCount);
365#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800366 memcpy_to_i16_from_q4_27(
367 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700368 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800369 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800370#endif
rago94a1ee82017-07-21 15:11:02 -0700371 }
rago94a1ee82017-07-21 15:11:02 -0700372 }
373#ifdef FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800374 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
375 if (!auxType) {
376 if (mInConversionBuffer.get() == nullptr) {
377 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
378 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700379 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800380 memcpy_to_i16_from_float(
381 mInConversionBuffer->audioBuffer()->s16,
382 mInBuffer->audioBuffer()->f32,
383 inChannelCount * mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700384 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800385 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
386 if (mOutConversionBuffer.get() == nullptr) {
387 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
388 goto data_bypass;
389 }
390 memcpy_to_i16_from_float(
391 mOutConversionBuffer->audioBuffer()->s16,
392 mOutBuffer->audioBuffer()->f32,
393 outChannelCount * mConfig.outputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700394 }
395 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800396#endif
397
Mikhail Naganov022b9952017-01-04 16:36:51 -0800398 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800399
400#ifdef FLOAT_EFFECT_CHAIN
401 if (!mSupportsFloat) { // convert output int16_t back to float.
402 memcpy_to_float_from_i16(
403 mOutBuffer->audioBuffer()->f32,
404 mOutConversionBuffer->audioBuffer()->s16,
405 outChannelCount * mConfig.outputCfg.buffer.frameCount);
406 }
rago94a1ee82017-07-21 15:11:02 -0700407#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700408 } else {
rago94a1ee82017-07-21 15:11:02 -0700409#ifdef FLOAT_EFFECT_CHAIN
410 data_bypass:
411#endif
412 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800413 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700414 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800415 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700416 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800417 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700418 }
419 }
420 ret = -ENODATA;
421 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800422
Eric Laurentca7cc822012-11-19 14:55:58 -0800423 // force transition to IDLE state when engine is ready
424 if (mState == STOPPED && ret == -ENODATA) {
425 mDisableWaitCnt = 1;
426 }
427
428 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700429 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800430#ifdef FLOAT_AUX
431 const size_t size =
432 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
433#else
rago94a1ee82017-07-21 15:11:02 -0700434 const size_t size =
435 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800436#endif
rago94a1ee82017-07-21 15:11:02 -0700437 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800438 }
439 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700440 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800441 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
442 // If an insert effect is idle and input buffer is different from output buffer,
443 // accumulate input onto output
444 sp<EffectChain> chain = mChain.promote();
Andy Hungfa69ca32017-11-30 10:07:53 -0800445 if (chain.get() != nullptr && chain->activeTrackCnt() != 0) {
446 accumulateInputToOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800447 }
448 }
449}
450
451void AudioFlinger::EffectModule::reset_l()
452{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700453 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800454 return;
455 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700456 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800457}
458
459status_t AudioFlinger::EffectModule::configure()
460{
rago94a1ee82017-07-21 15:11:02 -0700461 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700462 status_t status;
463 sp<ThreadBase> thread;
464 uint32_t size;
465 audio_channel_mask_t channelMask;
466
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700467 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700468 status = NO_INIT;
469 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800470 }
471
Eric Laurentd0ebb532013-04-02 16:41:41 -0700472 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800473 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700474 status = DEAD_OBJECT;
475 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800476 }
477
478 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700479 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700480 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800481
482 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
483 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900484 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
485 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800486 } else {
487 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700488 // TODO: Update this logic when multichannel effects are implemented.
489 // For offloaded tracks consider mono output as stereo for proper effect initialization
490 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
491 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
492 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
493 ALOGV("Overriding effect input and output as STEREO");
494 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800495 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700496
rago94a1ee82017-07-21 15:11:02 -0700497 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
498 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800499 mConfig.inputCfg.samplingRate = thread->sampleRate();
500 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
501 mConfig.inputCfg.bufferProvider.cookie = NULL;
502 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
503 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
504 mConfig.outputCfg.bufferProvider.cookie = NULL;
505 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
506 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
507 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
508 // Insert effect:
509 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
510 // always overwrites output buffer: input buffer == output buffer
511 // - in other sessions:
512 // last effect in the chain accumulates in output buffer: input buffer != output buffer
513 // other effect: overwrites output buffer: input buffer == output buffer
514 // Auxiliary effect:
515 // accumulates in output buffer: input buffer != output buffer
516 // Therefore: accumulate <=> input buffer != output buffer
517 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
518 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
519 } else {
520 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
521 }
522 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
523 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
524 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
525 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
526
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700527 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800528 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
529
530 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700531 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700532 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
533 sizeof(effect_config_t),
534 &mConfig,
535 &size,
536 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700537 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800538 status = cmdStatus;
rago94a1ee82017-07-21 15:11:02 -0700539#ifdef FLOAT_EFFECT_CHAIN
540 mSupportsFloat = true;
541#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800542 }
rago94a1ee82017-07-21 15:11:02 -0700543#ifdef FLOAT_EFFECT_CHAIN
544 else {
545 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
546 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
547 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
548 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
549 sizeof(effect_config_t),
550 &mConfig,
551 &size,
552 &cmdStatus);
553 if (status == NO_ERROR) {
554 status = cmdStatus;
555 mSupportsFloat = false;
556 ALOGVV("config worked with 16 bit");
557 } else {
558 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800559 }
rago94a1ee82017-07-21 15:11:02 -0700560 }
561#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800562
rago94a1ee82017-07-21 15:11:02 -0700563 if (status == NO_ERROR) {
564 // Establish Buffer strategy
565 setInBuffer(mInBuffer);
566 setOutBuffer(mOutBuffer);
567
568 // Update visualizer latency
569 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
570 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
571 effect_param_t *p = (effect_param_t *)buf32;
572
573 p->psize = sizeof(uint32_t);
574 p->vsize = sizeof(uint32_t);
575 size = sizeof(int);
576 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
577
578 uint32_t latency = 0;
579 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
580 if (pbt != NULL) {
581 latency = pbt->latency_l();
582 }
583
584 *((int32_t *)p->data + 1)= latency;
585 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
586 sizeof(effect_param_t) + 8,
587 &buf32,
588 &size,
589 &cmdStatus);
590 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800591 }
592
Andy Hung05083ac2017-12-14 15:00:28 -0800593 // mConfig.outputCfg.buffer.frameCount cannot be zero.
594 mMaxDisableWaitCnt = (uint32_t)std::max(
595 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
596 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
597 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -0800598
Eric Laurentd0ebb532013-04-02 16:41:41 -0700599exit:
Andy Hung6f88dc42017-12-13 16:19:39 -0800600 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -0700601 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700602 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800603 return status;
604}
605
606status_t AudioFlinger::EffectModule::init()
607{
608 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700609 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800610 return NO_INIT;
611 }
612 status_t cmdStatus;
613 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700614 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
615 0,
616 NULL,
617 &size,
618 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800619 if (status == 0) {
620 status = cmdStatus;
621 }
622 return status;
623}
624
Eric Laurent1b928682014-10-02 19:41:47 -0700625void AudioFlinger::EffectModule::addEffectToHal_l()
626{
627 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
628 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
629 sp<ThreadBase> thread = mThread.promote();
630 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700631 sp<StreamHalInterface> stream = thread->stream();
632 if (stream != 0) {
633 status_t result = stream->addEffect(mEffectInterface);
634 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700635 }
636 }
637 }
638}
639
Eric Laurentfa1e1232016-08-02 19:01:49 -0700640// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800641status_t AudioFlinger::EffectModule::start()
642{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700643 sp<EffectChain> chain;
644 status_t status;
645 {
646 Mutex::Autolock _l(mLock);
647 status = start_l();
648 if (status == NO_ERROR) {
649 chain = mChain.promote();
650 }
651 }
652 if (chain != 0) {
653 chain->resetVolume_l();
654 }
655 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800656}
657
658status_t AudioFlinger::EffectModule::start_l()
659{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700660 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800661 return NO_INIT;
662 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700663 if (mStatus != NO_ERROR) {
664 return mStatus;
665 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800666 status_t cmdStatus;
667 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700668 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
669 0,
670 NULL,
671 &size,
672 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800673 if (status == 0) {
674 status = cmdStatus;
675 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700676 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700677 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800678 }
679 return status;
680}
681
682status_t AudioFlinger::EffectModule::stop()
683{
684 Mutex::Autolock _l(mLock);
685 return stop_l();
686}
687
688status_t AudioFlinger::EffectModule::stop_l()
689{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700690 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800691 return NO_INIT;
692 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700693 if (mStatus != NO_ERROR) {
694 return mStatus;
695 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800696 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800697 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700698 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
699 0,
700 NULL,
701 &size,
702 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800703 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800704 status = cmdStatus;
705 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800706 if (status == NO_ERROR) {
707 status = remove_effect_from_hal_l();
708 }
709 return status;
710}
711
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800712// must be called with EffectChain::mLock held
713void AudioFlinger::EffectModule::release_l()
714{
715 if (mEffectInterface != 0) {
716 remove_effect_from_hal_l();
717 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800718 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800719 mEffectInterface.clear();
720 }
721}
722
Eric Laurentbfb1b832013-01-07 09:53:42 -0800723status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
724{
725 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
726 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800727 sp<ThreadBase> thread = mThread.promote();
728 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700729 sp<StreamHalInterface> stream = thread->stream();
730 if (stream != 0) {
731 status_t result = stream->removeEffect(mEffectInterface);
732 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800733 }
734 }
735 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800736 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800737}
738
Andy Hunge4a1d912016-08-17 14:11:13 -0700739// round up delta valid if value and divisor are positive.
740template <typename T>
741static T roundUpDelta(const T &value, const T &divisor) {
742 T remainder = value % divisor;
743 return remainder == 0 ? 0 : divisor - remainder;
744}
745
Eric Laurentca7cc822012-11-19 14:55:58 -0800746status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
747 uint32_t cmdSize,
748 void *pCmdData,
749 uint32_t *replySize,
750 void *pReplyData)
751{
752 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700753 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800754
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700755 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800756 return NO_INIT;
757 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700758 if (mStatus != NO_ERROR) {
759 return mStatus;
760 }
Andy Hung110bc952016-06-20 15:22:52 -0700761 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700762 (sizeof(effect_param_t) > cmdSize ||
763 ((effect_param_t *)pCmdData)->psize > cmdSize
764 - sizeof(effect_param_t))) {
765 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800766 android_errorWriteLog(0x534e4554, "33003822");
767 return -EINVAL;
768 }
769 if (cmdCode == EFFECT_CMD_GET_PARAM &&
770 (*replySize < sizeof(effect_param_t) ||
771 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
772 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700773 return -EINVAL;
774 }
ragoe2759072016-11-22 18:02:48 -0800775 if (cmdCode == EFFECT_CMD_GET_PARAM &&
776 (sizeof(effect_param_t) > *replySize
777 || ((effect_param_t *)pCmdData)->psize > *replySize
778 - sizeof(effect_param_t)
779 || ((effect_param_t *)pCmdData)->vsize > *replySize
780 - sizeof(effect_param_t)
781 - ((effect_param_t *)pCmdData)->psize
782 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
783 *replySize
784 - sizeof(effect_param_t)
785 - ((effect_param_t *)pCmdData)->psize
786 - ((effect_param_t *)pCmdData)->vsize)) {
787 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
788 android_errorWriteLog(0x534e4554, "32705438");
789 return -EINVAL;
790 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700791 if ((cmdCode == EFFECT_CMD_SET_PARAM
792 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
793 (sizeof(effect_param_t) > cmdSize
794 || ((effect_param_t *)pCmdData)->psize > cmdSize
795 - sizeof(effect_param_t)
796 || ((effect_param_t *)pCmdData)->vsize > cmdSize
797 - sizeof(effect_param_t)
798 - ((effect_param_t *)pCmdData)->psize
799 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
800 cmdSize
801 - sizeof(effect_param_t)
802 - ((effect_param_t *)pCmdData)->psize
803 - ((effect_param_t *)pCmdData)->vsize)) {
804 android_errorWriteLog(0x534e4554, "30204301");
805 return -EINVAL;
806 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700807 status_t status = mEffectInterface->command(cmdCode,
808 cmdSize,
809 pCmdData,
810 replySize,
811 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800812 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
813 uint32_t size = (replySize == NULL) ? 0 : *replySize;
814 for (size_t i = 1; i < mHandles.size(); i++) {
815 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800816 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800817 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
818 }
819 }
820 }
821 return status;
822}
823
824status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
825{
826 Mutex::Autolock _l(mLock);
827 return setEnabled_l(enabled);
828}
829
830// must be called with EffectModule::mLock held
831status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
832{
833
834 ALOGV("setEnabled %p enabled %d", this, enabled);
835
836 if (enabled != isEnabled()) {
837 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
838 if (enabled && status != NO_ERROR) {
839 return status;
840 }
841
842 switch (mState) {
843 // going from disabled to enabled
844 case IDLE:
845 mState = STARTING;
846 break;
847 case STOPPED:
848 mState = RESTART;
849 break;
850 case STOPPING:
851 mState = ACTIVE;
852 break;
853
854 // going from enabled to disabled
855 case RESTART:
856 mState = STOPPED;
857 break;
858 case STARTING:
859 mState = IDLE;
860 break;
861 case ACTIVE:
862 mState = STOPPING;
863 break;
864 case DESTROYED:
865 return NO_ERROR; // simply ignore as we are being destroyed
866 }
867 for (size_t i = 1; i < mHandles.size(); i++) {
868 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800869 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800870 h->setEnabled(enabled);
871 }
872 }
873 }
874 return NO_ERROR;
875}
876
877bool AudioFlinger::EffectModule::isEnabled() const
878{
879 switch (mState) {
880 case RESTART:
881 case STARTING:
882 case ACTIVE:
883 return true;
884 case IDLE:
885 case STOPPING:
886 case STOPPED:
887 case DESTROYED:
888 default:
889 return false;
890 }
891}
892
893bool AudioFlinger::EffectModule::isProcessEnabled() const
894{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700895 if (mStatus != NO_ERROR) {
896 return false;
897 }
898
Eric Laurentca7cc822012-11-19 14:55:58 -0800899 switch (mState) {
900 case RESTART:
901 case ACTIVE:
902 case STOPPING:
903 case STOPPED:
904 return true;
905 case IDLE:
906 case STARTING:
907 case DESTROYED:
908 default:
909 return false;
910 }
911}
912
Mikhail Naganov022b9952017-01-04 16:36:51 -0800913void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700914 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800915
916 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800917 if (buffer != 0) {
918 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
919 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
920 } else {
921 mConfig.inputCfg.buffer.raw = NULL;
922 }
923 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800924 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700925
926#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800927 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -0700928 // Theoretically insert effects can also do in-place conversions (destroying
929 // the original buffer) when the output buffer is identical to the input buffer,
930 // but we don't optimize for it here.
931 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
932 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
933 // we need to translate - create hidl shared buffer and intercept
934 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
935 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
936 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
937
938 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
939 __func__, inChannels, inFrameCount, size);
940
Andy Hungbded9c82017-11-30 18:47:35 -0800941 if (size > 0 && (mInConversionBuffer.get() == nullptr
942 || size > mInConversionBuffer->getSize())) {
943 mInConversionBuffer.clear();
944 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
945 (void)EffectBufferHalInterface::allocate(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700946 }
Andy Hungbded9c82017-11-30 18:47:35 -0800947 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -0800948 mInConversionBuffer->setFrameCount(inFrameCount);
949 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700950 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800951 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700952 }
953 }
954#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800955}
956
957void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700958 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -0800959
960 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -0800961 if (buffer != 0) {
962 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
963 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
964 } else {
965 mConfig.outputCfg.buffer.raw = NULL;
966 }
967 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -0800968 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -0700969
970#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -0800971 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -0700972 // can do in-place conversion from int16_t to float. We don't optimize here.
973 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
974 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
975 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
976 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
977
978 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
979 __func__, outChannels, outFrameCount, size);
980
Andy Hungbded9c82017-11-30 18:47:35 -0800981 if (size > 0 && (mOutConversionBuffer.get() == nullptr
982 || size > mOutConversionBuffer->getSize())) {
983 mOutConversionBuffer.clear();
984 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
985 (void)EffectBufferHalInterface::allocate(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700986 }
Andy Hungbded9c82017-11-30 18:47:35 -0800987 if (mOutConversionBuffer.get() != nullptr) {
988 mOutConversionBuffer->setFrameCount(outFrameCount);
989 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -0700990 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -0800991 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -0700992 }
993 }
994#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800995}
996
Eric Laurentca7cc822012-11-19 14:55:58 -0800997status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
998{
999 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001000 if (mStatus != NO_ERROR) {
1001 return mStatus;
1002 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001003 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001004 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1005 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1006 if (isProcessEnabled() &&
1007 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
1008 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001009 uint32_t volume[2];
1010 uint32_t *pVolume = NULL;
1011 uint32_t size = sizeof(volume);
1012 volume[0] = *left;
1013 volume[1] = *right;
1014 if (controller) {
1015 pVolume = volume;
1016 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001017 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1018 size,
1019 volume,
1020 &size,
1021 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001022 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1023 *left = volume[0];
1024 *right = volume[1];
1025 }
1026 }
1027 return status;
1028}
1029
1030status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1031{
1032 if (device == AUDIO_DEVICE_NONE) {
1033 return NO_ERROR;
1034 }
1035
1036 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001037 if (mStatus != NO_ERROR) {
1038 return mStatus;
1039 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001040 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001041 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001042 status_t cmdStatus;
1043 uint32_t size = sizeof(status_t);
1044 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1045 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001046 status = mEffectInterface->command(cmd,
1047 sizeof(uint32_t),
1048 &device,
1049 &size,
1050 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001051 }
1052 return status;
1053}
1054
1055status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1056{
1057 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001058 if (mStatus != NO_ERROR) {
1059 return mStatus;
1060 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001061 status_t status = NO_ERROR;
1062 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1063 status_t cmdStatus;
1064 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001065 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1066 sizeof(audio_mode_t),
1067 &mode,
1068 &size,
1069 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001070 if (status == NO_ERROR) {
1071 status = cmdStatus;
1072 }
1073 }
1074 return status;
1075}
1076
1077status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1078{
1079 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001080 if (mStatus != NO_ERROR) {
1081 return mStatus;
1082 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001083 status_t status = NO_ERROR;
1084 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1085 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001086 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1087 sizeof(audio_source_t),
1088 &source,
1089 &size,
1090 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001091 }
1092 return status;
1093}
1094
1095void AudioFlinger::EffectModule::setSuspended(bool suspended)
1096{
1097 Mutex::Autolock _l(mLock);
1098 mSuspended = suspended;
1099}
1100
1101bool AudioFlinger::EffectModule::suspended() const
1102{
1103 Mutex::Autolock _l(mLock);
1104 return mSuspended;
1105}
1106
1107bool AudioFlinger::EffectModule::purgeHandles()
1108{
1109 bool enabled = false;
1110 Mutex::Autolock _l(mLock);
1111 for (size_t i = 0; i < mHandles.size(); i++) {
1112 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001113 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001114 if (handle->hasControl()) {
1115 enabled = handle->enabled();
1116 }
1117 }
1118 }
1119 return enabled;
1120}
1121
Eric Laurent5baf2af2013-09-12 17:37:00 -07001122status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1123{
1124 Mutex::Autolock _l(mLock);
1125 if (mStatus != NO_ERROR) {
1126 return mStatus;
1127 }
1128 status_t status = NO_ERROR;
1129 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1130 status_t cmdStatus;
1131 uint32_t size = sizeof(status_t);
1132 effect_offload_param_t cmd;
1133
1134 cmd.isOffload = offloaded;
1135 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001136 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1137 sizeof(effect_offload_param_t),
1138 &cmd,
1139 &size,
1140 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001141 if (status == NO_ERROR) {
1142 status = cmdStatus;
1143 }
1144 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1145 } else {
1146 if (offloaded) {
1147 status = INVALID_OPERATION;
1148 }
1149 mOffloaded = false;
1150 }
1151 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1152 return status;
1153}
1154
1155bool AudioFlinger::EffectModule::isOffloaded() const
1156{
1157 Mutex::Autolock _l(mLock);
1158 return mOffloaded;
1159}
1160
Marco Nelissenb2208842014-02-07 14:00:50 -08001161String8 effectFlagsToString(uint32_t flags) {
1162 String8 s;
1163
1164 s.append("conn. mode: ");
1165 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1166 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1167 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1168 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1169 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1170 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1171 default: s.append("unknown/reserved"); break;
1172 }
1173 s.append(", ");
1174
1175 s.append("insert pref: ");
1176 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1177 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1178 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1179 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1180 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1181 default: s.append("unknown/reserved"); break;
1182 }
1183 s.append(", ");
1184
1185 s.append("volume mgmt: ");
1186 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1187 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1188 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1189 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1190 default: s.append("unknown/reserved"); break;
1191 }
1192 s.append(", ");
1193
1194 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1195 if (devind) {
1196 s.append("device indication: ");
1197 switch (devind) {
1198 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1199 default: s.append("unknown/reserved"); break;
1200 }
1201 s.append(", ");
1202 }
1203
1204 s.append("input mode: ");
1205 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1206 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1207 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1208 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1209 default: s.append("not set"); break;
1210 }
1211 s.append(", ");
1212
1213 s.append("output mode: ");
1214 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1215 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1216 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1217 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1218 default: s.append("not set"); break;
1219 }
1220 s.append(", ");
1221
1222 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1223 if (accel) {
1224 s.append("hardware acceleration: ");
1225 switch (accel) {
1226 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1227 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1228 default: s.append("unknown/reserved"); break;
1229 }
1230 s.append(", ");
1231 }
1232
1233 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1234 if (modeind) {
1235 s.append("mode indication: ");
1236 switch (modeind) {
1237 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1238 default: s.append("unknown/reserved"); break;
1239 }
1240 s.append(", ");
1241 }
1242
1243 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1244 if (srcind) {
1245 s.append("source indication: ");
1246 switch (srcind) {
1247 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1248 default: s.append("unknown/reserved"); break;
1249 }
1250 s.append(", ");
1251 }
1252
1253 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1254 s.append("offloadable, ");
1255 }
1256
1257 int len = s.length();
1258 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001259 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001260 s.unlockBuffer(len - 2);
1261 }
1262 return s;
1263}
1264
Andy Hungbded9c82017-11-30 18:47:35 -08001265static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1266 std::stringstream ss;
1267
1268 if (buffer.get() == nullptr) {
1269 return "nullptr"; // make different than below
1270 } else if (buffer->externalData() != nullptr) {
1271 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1272 << " -> "
1273 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1274 } else {
1275 ss << buffer->audioBuffer()->raw;
1276 }
1277 return ss.str();
1278}
Marco Nelissenb2208842014-02-07 14:00:50 -08001279
Glenn Kasten0f11b512014-01-31 16:18:54 -08001280void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001281{
1282 const size_t SIZE = 256;
1283 char buffer[SIZE];
1284 String8 result;
1285
1286 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1287 result.append(buffer);
1288
1289 bool locked = AudioFlinger::dumpTryLock(mLock);
1290 // failed to lock - AudioFlinger is probably deadlocked
1291 if (!locked) {
1292 result.append("\t\tCould not lock Fx mutex:\n");
1293 }
1294
1295 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001296 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001297 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001298 result.append(buffer);
1299
1300 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001301 char uuidStr[64];
1302 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1303 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001304 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001305 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1306 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001307 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001308 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001309 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001310 mDescriptor.flags,
1311 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001312 result.append(buffer);
1313 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1314 mDescriptor.name);
1315 result.append(buffer);
1316 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1317 mDescriptor.implementor);
1318 result.append(buffer);
1319
1320 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001321 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001322 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001323 mConfig.inputCfg.buffer.frameCount,
1324 mConfig.inputCfg.samplingRate,
1325 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001326 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001327 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001328 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001329 result.append(buffer);
1330
1331 result.append("\t\t- Output configuration:\n");
1332 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001333 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001334 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001335 mConfig.outputCfg.buffer.frameCount,
1336 mConfig.outputCfg.samplingRate,
1337 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001338 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001339 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001340 result.append(buffer);
1341
rago94a1ee82017-07-21 15:11:02 -07001342#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001343
Andy Hungbded9c82017-11-30 18:47:35 -08001344 result.appendFormat("\t\t- HAL buffers:\n"
1345 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1346 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1347 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1348 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1349 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001350#endif
1351
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001352 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001353 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001354 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001355 for (size_t i = 0; i < mHandles.size(); ++i) {
1356 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001357 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001358 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001359 result.append(buffer);
1360 }
1361 }
1362
Eric Laurentca7cc822012-11-19 14:55:58 -08001363 write(fd, result.string(), result.length());
1364
1365 if (locked) {
1366 mLock.unlock();
1367 }
1368}
1369
1370// ----------------------------------------------------------------------------
1371// EffectHandle implementation
1372// ----------------------------------------------------------------------------
1373
1374#undef LOG_TAG
1375#define LOG_TAG "AudioFlinger::EffectHandle"
1376
1377AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1378 const sp<AudioFlinger::Client>& client,
1379 const sp<IEffectClient>& effectClient,
1380 int32_t priority)
1381 : BnEffect(),
1382 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001383 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001384{
1385 ALOGV("constructor %p", this);
1386
1387 if (client == 0) {
1388 return;
1389 }
1390 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1391 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001392 if (mCblkMemory == 0 ||
1393 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001394 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001395 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001396 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001397 return;
1398 }
Glenn Kastene75da402013-11-20 13:54:52 -08001399 new(mCblk) effect_param_cblk_t();
1400 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001401}
1402
1403AudioFlinger::EffectHandle::~EffectHandle()
1404{
1405 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001406 disconnect(false);
1407}
1408
Glenn Kastene75da402013-11-20 13:54:52 -08001409status_t AudioFlinger::EffectHandle::initCheck()
1410{
1411 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1412}
1413
Eric Laurentca7cc822012-11-19 14:55:58 -08001414status_t AudioFlinger::EffectHandle::enable()
1415{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001416 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001418 sp<EffectModule> effect = mEffect.promote();
1419 if (effect == 0 || mDisconnected) {
1420 return DEAD_OBJECT;
1421 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001422 if (!mHasControl) {
1423 return INVALID_OPERATION;
1424 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001425
1426 if (mEnabled) {
1427 return NO_ERROR;
1428 }
1429
1430 mEnabled = true;
1431
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001432 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001433 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001434 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001435 }
1436
1437 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001438 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001439 return NO_ERROR;
1440 }
1441
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001442 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001443 if (status != NO_ERROR) {
1444 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001445 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001446 }
1447 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001448 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001449 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001450 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1451 Mutex::Autolock _l(thread->mLock);
1452 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001453 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001454 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001455 if (thread->type() == ThreadBase::OFFLOAD) {
1456 PlaybackThread *t = (PlaybackThread *)thread.get();
1457 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1458 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001459 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001460 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1461 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001462 }
1463 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001464 }
1465 return status;
1466}
1467
1468status_t AudioFlinger::EffectHandle::disable()
1469{
1470 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001471 AutoMutex _l(mLock);
1472 sp<EffectModule> effect = mEffect.promote();
1473 if (effect == 0 || mDisconnected) {
1474 return DEAD_OBJECT;
1475 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001476 if (!mHasControl) {
1477 return INVALID_OPERATION;
1478 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001479
1480 if (!mEnabled) {
1481 return NO_ERROR;
1482 }
1483 mEnabled = false;
1484
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001485 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001486 return NO_ERROR;
1487 }
1488
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001489 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001490
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001491 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001492 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001493 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001494 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1495 Mutex::Autolock _l(thread->mLock);
1496 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001497 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001498 }
1499
1500 return status;
1501}
1502
1503void AudioFlinger::EffectHandle::disconnect()
1504{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001505 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001506 disconnect(true);
1507}
1508
1509void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1510{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001511 AutoMutex _l(mLock);
1512 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1513 if (mDisconnected) {
1514 if (unpinIfLast) {
1515 android_errorWriteLog(0x534e4554, "32707507");
1516 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 return;
1518 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001519 mDisconnected = true;
1520 sp<ThreadBase> thread;
1521 {
1522 sp<EffectModule> effect = mEffect.promote();
1523 if (effect != 0) {
1524 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001525 }
1526 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001527 if (thread != 0) {
1528 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001529 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001530 // try to cleanup as much as we can
1531 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001532 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1533 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001534 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001535 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001536
Eric Laurentca7cc822012-11-19 14:55:58 -08001537 if (mClient != 0) {
1538 if (mCblk != NULL) {
1539 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1540 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1541 }
1542 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001543 // Client destructor must run with AudioFlinger client mutex locked
1544 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001545 mClient.clear();
1546 }
1547}
1548
1549status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1550 uint32_t cmdSize,
1551 void *pCmdData,
1552 uint32_t *replySize,
1553 void *pReplyData)
1554{
1555 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001556 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001557
Eric Laurentc7ab3092017-06-15 18:43:46 -07001558 // reject commands reserved for internal use by audio framework if coming from outside
1559 // of audioserver
1560 switch(cmdCode) {
1561 case EFFECT_CMD_ENABLE:
1562 case EFFECT_CMD_DISABLE:
1563 case EFFECT_CMD_SET_PARAM:
1564 case EFFECT_CMD_SET_PARAM_DEFERRED:
1565 case EFFECT_CMD_SET_PARAM_COMMIT:
1566 case EFFECT_CMD_GET_PARAM:
1567 break;
1568 default:
1569 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1570 break;
1571 }
1572 android_errorWriteLog(0x534e4554, "62019992");
1573 return BAD_VALUE;
1574 }
1575
Eric Laurent1ffc5852016-12-15 14:46:09 -08001576 if (cmdCode == EFFECT_CMD_ENABLE) {
1577 if (*replySize < sizeof(int)) {
1578 android_errorWriteLog(0x534e4554, "32095713");
1579 return BAD_VALUE;
1580 }
1581 *(int *)pReplyData = NO_ERROR;
1582 *replySize = sizeof(int);
1583 return enable();
1584 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1585 if (*replySize < sizeof(int)) {
1586 android_errorWriteLog(0x534e4554, "32095713");
1587 return BAD_VALUE;
1588 }
1589 *(int *)pReplyData = NO_ERROR;
1590 *replySize = sizeof(int);
1591 return disable();
1592 }
1593
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001594 AutoMutex _l(mLock);
1595 sp<EffectModule> effect = mEffect.promote();
1596 if (effect == 0 || mDisconnected) {
1597 return DEAD_OBJECT;
1598 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001599 // only get parameter command is permitted for applications not controlling the effect
1600 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1601 return INVALID_OPERATION;
1602 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001603 if (mClient == 0) {
1604 return INVALID_OPERATION;
1605 }
1606
1607 // handle commands that are not forwarded transparently to effect engine
1608 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001609 if (*replySize < sizeof(int)) {
1610 android_errorWriteLog(0x534e4554, "32095713");
1611 return BAD_VALUE;
1612 }
1613 *(int *)pReplyData = NO_ERROR;
1614 *replySize = sizeof(int);
1615
Eric Laurentca7cc822012-11-19 14:55:58 -08001616 // No need to trylock() here as this function is executed in the binder thread serving a
1617 // particular client process: no risk to block the whole media server process or mixer
1618 // threads if we are stuck here
1619 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001620 // keep local copy of index in case of client corruption b/32220769
1621 const uint32_t clientIndex = mCblk->clientIndex;
1622 const uint32_t serverIndex = mCblk->serverIndex;
1623 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1624 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001625 mCblk->serverIndex = 0;
1626 mCblk->clientIndex = 0;
1627 return BAD_VALUE;
1628 }
1629 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001630 effect_param_t *param = NULL;
1631 for (uint32_t index = serverIndex; index < clientIndex;) {
1632 int *p = (int *)(mBuffer + index);
1633 const int size = *p++;
1634 if (size < 0
1635 || size > EFFECT_PARAM_BUFFER_SIZE
1636 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001637 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001638 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001639 break;
1640 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001641
1642 // copy to local memory in case of client corruption b/32220769
1643 param = (effect_param_t *)realloc(param, size);
1644 if (param == NULL) {
1645 ALOGW("command(): out of memory");
1646 status = NO_MEMORY;
1647 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001648 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001649 memcpy(param, p, size);
1650
1651 int reply = 0;
1652 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001653 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001654 size,
1655 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001656 &rsize,
1657 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001658
1659 // verify shared memory: server index shouldn't change; client index can't go back.
1660 if (serverIndex != mCblk->serverIndex
1661 || clientIndex > mCblk->clientIndex) {
1662 android_errorWriteLog(0x534e4554, "32220769");
1663 status = BAD_VALUE;
1664 break;
1665 }
1666
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 // stop at first error encountered
1668 if (ret != NO_ERROR) {
1669 status = ret;
1670 *(int *)pReplyData = reply;
1671 break;
1672 } else if (reply != NO_ERROR) {
1673 *(int *)pReplyData = reply;
1674 break;
1675 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001676 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001677 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001678 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001679 mCblk->serverIndex = 0;
1680 mCblk->clientIndex = 0;
1681 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001682 }
1683
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001684 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001685}
1686
1687void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1688{
1689 ALOGV("setControl %p control %d", this, hasControl);
1690
1691 mHasControl = hasControl;
1692 mEnabled = enabled;
1693
1694 if (signal && mEffectClient != 0) {
1695 mEffectClient->controlStatusChanged(hasControl);
1696 }
1697}
1698
1699void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1700 uint32_t cmdSize,
1701 void *pCmdData,
1702 uint32_t replySize,
1703 void *pReplyData)
1704{
1705 if (mEffectClient != 0) {
1706 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1707 }
1708}
1709
1710
1711
1712void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1713{
1714 if (mEffectClient != 0) {
1715 mEffectClient->enableStatusChanged(enabled);
1716 }
1717}
1718
1719status_t AudioFlinger::EffectHandle::onTransact(
1720 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1721{
1722 return BnEffect::onTransact(code, data, reply, flags);
1723}
1724
1725
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001726void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001727{
1728 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1729
Marco Nelissenb2208842014-02-07 14:00:50 -08001730 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001731 (mClient == 0) ? getpid_cached : mClient->pid(),
1732 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001733 mHasControl ? "yes" : "no",
1734 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001735 mCblk ? mCblk->clientIndex : 0,
1736 mCblk ? mCblk->serverIndex : 0
1737 );
1738
1739 if (locked) {
1740 mCblk->lock.unlock();
1741 }
1742}
1743
1744#undef LOG_TAG
1745#define LOG_TAG "AudioFlinger::EffectChain"
1746
1747AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001748 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001749 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001750 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001751 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001752{
1753 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1754 if (thread == NULL) {
1755 return;
1756 }
1757 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1758 thread->frameCount();
1759}
1760
1761AudioFlinger::EffectChain::~EffectChain()
1762{
Eric Laurentca7cc822012-11-19 14:55:58 -08001763}
1764
1765// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1766sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1767 effect_descriptor_t *descriptor)
1768{
1769 size_t size = mEffects.size();
1770
1771 for (size_t i = 0; i < size; i++) {
1772 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1773 return mEffects[i];
1774 }
1775 }
1776 return 0;
1777}
1778
1779// getEffectFromId_l() must be called with ThreadBase::mLock held
1780sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1781{
1782 size_t size = mEffects.size();
1783
1784 for (size_t i = 0; i < size; i++) {
1785 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1786 if (id == 0 || mEffects[i]->id() == id) {
1787 return mEffects[i];
1788 }
1789 }
1790 return 0;
1791}
1792
1793// getEffectFromType_l() must be called with ThreadBase::mLock held
1794sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1795 const effect_uuid_t *type)
1796{
1797 size_t size = mEffects.size();
1798
1799 for (size_t i = 0; i < size; i++) {
1800 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1801 return mEffects[i];
1802 }
1803 }
1804 return 0;
1805}
1806
1807void AudioFlinger::EffectChain::clearInputBuffer()
1808{
1809 Mutex::Autolock _l(mLock);
1810 sp<ThreadBase> thread = mThread.promote();
1811 if (thread == 0) {
1812 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1813 return;
1814 }
1815 clearInputBuffer_l(thread);
1816}
1817
1818// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001819void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001820{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001821 if (mInBuffer == NULL) {
1822 return;
1823 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001824 // TODO: This will change in the future, depending on multichannel
1825 // and sample format changes for effects.
1826 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1827 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001828
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001829 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001830 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1831 * std::min((uint32_t)FCC_2, thread->channelCount());
1832
Mikhail Naganov022b9952017-01-04 16:36:51 -08001833 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1834 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001835}
1836
1837// Must be called with EffectChain::mLock locked
1838void AudioFlinger::EffectChain::process_l()
1839{
1840 sp<ThreadBase> thread = mThread.promote();
1841 if (thread == 0) {
1842 ALOGW("process_l(): cannot promote mixer thread");
1843 return;
1844 }
1845 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1846 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001847 // never process effects when:
1848 // - on an OFFLOAD thread
1849 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001850 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1851 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001852 if (!isGlobalSession) {
1853 bool tracksOnSession = (trackCnt() != 0);
1854
1855 if (!tracksOnSession && mTailBufferCount == 0) {
1856 doProcess = false;
1857 }
1858
1859 if (activeTrackCnt() == 0) {
1860 // if no track is active and the effect tail has not been rendered,
1861 // the input buffer must be cleared here as the mixer process will not do it
1862 if (tracksOnSession || mTailBufferCount > 0) {
1863 clearInputBuffer_l(thread);
1864 if (mTailBufferCount > 0) {
1865 mTailBufferCount--;
1866 }
1867 }
1868 }
1869 }
1870
1871 size_t size = mEffects.size();
1872 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001873 // Only the input and output buffers of the chain can be external,
1874 // and 'update' / 'commit' do nothing for allocated buffers, thus
1875 // it's not needed to consider any other buffers here.
1876 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001877 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1878 mOutBuffer->update();
1879 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001880 for (size_t i = 0; i < size; i++) {
1881 mEffects[i]->process();
1882 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001883 mInBuffer->commit();
1884 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1885 mOutBuffer->commit();
1886 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001887 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001888 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001889 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001890 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1891 }
1892 if (doResetVolume) {
1893 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001894 }
1895}
1896
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001897// createEffect_l() must be called with ThreadBase::mLock held
1898status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1899 ThreadBase *thread,
1900 effect_descriptor_t *desc,
1901 int id,
1902 audio_session_t sessionId,
1903 bool pinned)
1904{
1905 Mutex::Autolock _l(mLock);
1906 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1907 status_t lStatus = effect->status();
1908 if (lStatus == NO_ERROR) {
1909 lStatus = addEffect_ll(effect);
1910 }
1911 if (lStatus != NO_ERROR) {
1912 effect.clear();
1913 }
1914 return lStatus;
1915}
1916
1917// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001918status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1919{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001920 Mutex::Autolock _l(mLock);
1921 return addEffect_ll(effect);
1922}
1923// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1924status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1925{
Eric Laurentca7cc822012-11-19 14:55:58 -08001926 effect_descriptor_t desc = effect->desc();
1927 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1928
Eric Laurentca7cc822012-11-19 14:55:58 -08001929 effect->setChain(this);
1930 sp<ThreadBase> thread = mThread.promote();
1931 if (thread == 0) {
1932 return NO_INIT;
1933 }
1934 effect->setThread(thread);
1935
1936 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1937 // Auxiliary effects are inserted at the beginning of mEffects vector as
1938 // they are processed first and accumulated in chain input buffer
1939 mEffects.insertAt(effect, 0);
1940
1941 // the input buffer for auxiliary effect contains mono samples in
1942 // 32 bit format. This is to avoid saturation in AudoMixer
1943 // accumulation stage. Saturation is done in EffectModule::process() before
1944 // calling the process in effect engine
1945 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001946 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001947#ifdef FLOAT_EFFECT_CHAIN
1948 status_t result = EffectBufferHalInterface::allocate(
1949 numSamples * sizeof(float), &halBuffer);
1950#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001951 status_t result = EffectBufferHalInterface::allocate(
1952 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001953#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001954 if (result != OK) return result;
1955 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001956 // auxiliary effects output samples to chain input buffer for further processing
1957 // by insert effects
1958 effect->setOutBuffer(mInBuffer);
1959 } else {
1960 // Insert effects are inserted at the end of mEffects vector as they are processed
1961 // after track and auxiliary effects.
1962 // Insert effect order as a function of indicated preference:
1963 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1964 // another effect is present
1965 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1966 // last effect claiming first position
1967 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1968 // first effect claiming last position
1969 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1970 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1971 // already present
1972
1973 size_t size = mEffects.size();
1974 size_t idx_insert = size;
1975 ssize_t idx_insert_first = -1;
1976 ssize_t idx_insert_last = -1;
1977
1978 for (size_t i = 0; i < size; i++) {
1979 effect_descriptor_t d = mEffects[i]->desc();
1980 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1981 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1982 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1983 // check invalid effect chaining combinations
1984 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1985 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1986 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1987 desc.name, d.name);
1988 return INVALID_OPERATION;
1989 }
1990 // remember position of first insert effect and by default
1991 // select this as insert position for new effect
1992 if (idx_insert == size) {
1993 idx_insert = i;
1994 }
1995 // remember position of last insert effect claiming
1996 // first position
1997 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1998 idx_insert_first = i;
1999 }
2000 // remember position of first insert effect claiming
2001 // last position
2002 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2003 idx_insert_last == -1) {
2004 idx_insert_last = i;
2005 }
2006 }
2007 }
2008
2009 // modify idx_insert from first position if needed
2010 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2011 if (idx_insert_last != -1) {
2012 idx_insert = idx_insert_last;
2013 } else {
2014 idx_insert = size;
2015 }
2016 } else {
2017 if (idx_insert_first != -1) {
2018 idx_insert = idx_insert_first + 1;
2019 }
2020 }
2021
2022 // always read samples from chain input buffer
2023 effect->setInBuffer(mInBuffer);
2024
2025 // if last effect in the chain, output samples to chain
2026 // output buffer, otherwise to chain input buffer
2027 if (idx_insert == size) {
2028 if (idx_insert != 0) {
2029 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2030 mEffects[idx_insert-1]->configure();
2031 }
2032 effect->setOutBuffer(mOutBuffer);
2033 } else {
2034 effect->setOutBuffer(mInBuffer);
2035 }
2036 mEffects.insertAt(effect, idx_insert);
2037
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002038 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002039 idx_insert);
2040 }
2041 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002042
Eric Laurentca7cc822012-11-19 14:55:58 -08002043 return NO_ERROR;
2044}
2045
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002046// removeEffect_l() must be called with ThreadBase::mLock held
2047size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2048 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002049{
2050 Mutex::Autolock _l(mLock);
2051 size_t size = mEffects.size();
2052 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2053
2054 for (size_t i = 0; i < size; i++) {
2055 if (effect == mEffects[i]) {
2056 // calling stop here will remove pre-processing effect from the audio HAL.
2057 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2058 // the middle of a read from audio HAL
2059 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2060 mEffects[i]->state() == EffectModule::STOPPING) {
2061 mEffects[i]->stop();
2062 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002063 if (release) {
2064 mEffects[i]->release_l();
2065 }
2066
Mikhail Naganov022b9952017-01-04 16:36:51 -08002067 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002068 if (i == size - 1 && i != 0) {
2069 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2070 mEffects[i - 1]->configure();
2071 }
2072 }
2073 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002074 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002075 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002076
Eric Laurentca7cc822012-11-19 14:55:58 -08002077 break;
2078 }
2079 }
2080
2081 return mEffects.size();
2082}
2083
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002084// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002085void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2086{
2087 size_t size = mEffects.size();
2088 for (size_t i = 0; i < size; i++) {
2089 mEffects[i]->setDevice(device);
2090 }
2091}
2092
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002093// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002094void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2095{
2096 size_t size = mEffects.size();
2097 for (size_t i = 0; i < size; i++) {
2098 mEffects[i]->setMode(mode);
2099 }
2100}
2101
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002102// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002103void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2104{
2105 size_t size = mEffects.size();
2106 for (size_t i = 0; i < size; i++) {
2107 mEffects[i]->setAudioSource(source);
2108 }
2109}
2110
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002111// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002112bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002113{
2114 uint32_t newLeft = *left;
2115 uint32_t newRight = *right;
2116 bool hasControl = false;
2117 int ctrlIdx = -1;
2118 size_t size = mEffects.size();
2119
2120 // first update volume controller
2121 for (size_t i = size; i > 0; i--) {
2122 if (mEffects[i - 1]->isProcessEnabled() &&
2123 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2124 ctrlIdx = i - 1;
2125 hasControl = true;
2126 break;
2127 }
2128 }
2129
Eric Laurentfa1e1232016-08-02 19:01:49 -07002130 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002131 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002132 if (hasControl) {
2133 *left = mNewLeftVolume;
2134 *right = mNewRightVolume;
2135 }
2136 return hasControl;
2137 }
2138
2139 mVolumeCtrlIdx = ctrlIdx;
2140 mLeftVolume = newLeft;
2141 mRightVolume = newRight;
2142
2143 // second get volume update from volume controller
2144 if (ctrlIdx >= 0) {
2145 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2146 mNewLeftVolume = newLeft;
2147 mNewRightVolume = newRight;
2148 }
2149 // then indicate volume to all other effects in chain.
2150 // Pass altered volume to effects before volume controller
2151 // and requested volume to effects after controller
2152 uint32_t lVol = newLeft;
2153 uint32_t rVol = newRight;
2154
2155 for (size_t i = 0; i < size; i++) {
2156 if ((int)i == ctrlIdx) {
2157 continue;
2158 }
2159 // this also works for ctrlIdx == -1 when there is no volume controller
2160 if ((int)i > ctrlIdx) {
2161 lVol = *left;
2162 rVol = *right;
2163 }
2164 mEffects[i]->setVolume(&lVol, &rVol, false);
2165 }
2166 *left = newLeft;
2167 *right = newRight;
2168
2169 return hasControl;
2170}
2171
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002172// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002173void AudioFlinger::EffectChain::resetVolume_l()
2174{
Eric Laurente7449bf2016-08-03 18:44:07 -07002175 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2176 uint32_t left = mLeftVolume;
2177 uint32_t right = mRightVolume;
2178 (void)setVolume_l(&left, &right, true);
2179 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002180}
2181
Eric Laurent1b928682014-10-02 19:41:47 -07002182void AudioFlinger::EffectChain::syncHalEffectsState()
2183{
2184 Mutex::Autolock _l(mLock);
2185 for (size_t i = 0; i < mEffects.size(); i++) {
2186 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2187 mEffects[i]->state() == EffectModule::STOPPING) {
2188 mEffects[i]->addEffectToHal_l();
2189 }
2190 }
2191}
2192
Eric Laurentca7cc822012-11-19 14:55:58 -08002193void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2194{
2195 const size_t SIZE = 256;
2196 char buffer[SIZE];
2197 String8 result;
2198
Marco Nelissenb2208842014-02-07 14:00:50 -08002199 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002200 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002201 result.append(buffer);
2202
Marco Nelissenb2208842014-02-07 14:00:50 -08002203 if (numEffects) {
2204 bool locked = AudioFlinger::dumpTryLock(mLock);
2205 // failed to lock - AudioFlinger is probably deadlocked
2206 if (!locked) {
2207 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002208 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002209
Andy Hungbded9c82017-11-30 18:47:35 -08002210 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2211 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2212 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2213 (int)inBufferStr.size(), "In buffer ",
2214 (int)outBufferStr.size(), "Out buffer ");
2215 result.appendFormat("\t%s %s %d\n",
2216 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002217 write(fd, result.string(), result.size());
2218
2219 for (size_t i = 0; i < numEffects; ++i) {
2220 sp<EffectModule> effect = mEffects[i];
2221 if (effect != 0) {
2222 effect->dump(fd, args);
2223 }
2224 }
2225
2226 if (locked) {
2227 mLock.unlock();
2228 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002229 }
2230}
2231
2232// must be called with ThreadBase::mLock held
2233void AudioFlinger::EffectChain::setEffectSuspended_l(
2234 const effect_uuid_t *type, bool suspend)
2235{
2236 sp<SuspendedEffectDesc> desc;
2237 // use effect type UUID timelow as key as there is no real risk of identical
2238 // timeLow fields among effect type UUIDs.
2239 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2240 if (suspend) {
2241 if (index >= 0) {
2242 desc = mSuspendedEffects.valueAt(index);
2243 } else {
2244 desc = new SuspendedEffectDesc();
2245 desc->mType = *type;
2246 mSuspendedEffects.add(type->timeLow, desc);
2247 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2248 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002249
Eric Laurentca7cc822012-11-19 14:55:58 -08002250 if (desc->mRefCount++ == 0) {
2251 sp<EffectModule> effect = getEffectIfEnabled(type);
2252 if (effect != 0) {
2253 desc->mEffect = effect;
2254 effect->setSuspended(true);
2255 effect->setEnabled(false);
2256 }
2257 }
2258 } else {
2259 if (index < 0) {
2260 return;
2261 }
2262 desc = mSuspendedEffects.valueAt(index);
2263 if (desc->mRefCount <= 0) {
2264 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002265 desc->mRefCount = 0;
2266 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002267 }
2268 if (--desc->mRefCount == 0) {
2269 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2270 if (desc->mEffect != 0) {
2271 sp<EffectModule> effect = desc->mEffect.promote();
2272 if (effect != 0) {
2273 effect->setSuspended(false);
2274 effect->lock();
2275 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002276 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002277 effect->setEnabled_l(handle->enabled());
2278 }
2279 effect->unlock();
2280 }
2281 desc->mEffect.clear();
2282 }
2283 mSuspendedEffects.removeItemsAt(index);
2284 }
2285 }
2286}
2287
2288// must be called with ThreadBase::mLock held
2289void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2290{
2291 sp<SuspendedEffectDesc> desc;
2292
2293 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2294 if (suspend) {
2295 if (index >= 0) {
2296 desc = mSuspendedEffects.valueAt(index);
2297 } else {
2298 desc = new SuspendedEffectDesc();
2299 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2300 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2301 }
2302 if (desc->mRefCount++ == 0) {
2303 Vector< sp<EffectModule> > effects;
2304 getSuspendEligibleEffects(effects);
2305 for (size_t i = 0; i < effects.size(); i++) {
2306 setEffectSuspended_l(&effects[i]->desc().type, true);
2307 }
2308 }
2309 } else {
2310 if (index < 0) {
2311 return;
2312 }
2313 desc = mSuspendedEffects.valueAt(index);
2314 if (desc->mRefCount <= 0) {
2315 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2316 desc->mRefCount = 1;
2317 }
2318 if (--desc->mRefCount == 0) {
2319 Vector<const effect_uuid_t *> types;
2320 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2321 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2322 continue;
2323 }
2324 types.add(&mSuspendedEffects.valueAt(i)->mType);
2325 }
2326 for (size_t i = 0; i < types.size(); i++) {
2327 setEffectSuspended_l(types[i], false);
2328 }
2329 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2330 mSuspendedEffects.keyAt(index));
2331 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2332 }
2333 }
2334}
2335
2336
2337// The volume effect is used for automated tests only
2338#ifndef OPENSL_ES_H_
2339static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2340 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2341const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2342#endif //OPENSL_ES_H_
2343
Eric Laurentd8365c52017-07-16 15:27:05 -07002344/* static */
2345bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2346{
2347 // Only NS and AEC are suspended when BtNRec is off
2348 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2349 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2350 return true;
2351 }
2352 return false;
2353}
2354
Eric Laurentca7cc822012-11-19 14:55:58 -08002355bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2356{
2357 // auxiliary effects and visualizer are never suspended on output mix
2358 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2359 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2360 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2361 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2362 return false;
2363 }
2364 return true;
2365}
2366
2367void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2368 Vector< sp<AudioFlinger::EffectModule> > &effects)
2369{
2370 effects.clear();
2371 for (size_t i = 0; i < mEffects.size(); i++) {
2372 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2373 effects.add(mEffects[i]);
2374 }
2375 }
2376}
2377
2378sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2379 const effect_uuid_t *type)
2380{
2381 sp<EffectModule> effect = getEffectFromType_l(type);
2382 return effect != 0 && effect->isEnabled() ? effect : 0;
2383}
2384
2385void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2386 bool enabled)
2387{
2388 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2389 if (enabled) {
2390 if (index < 0) {
2391 // if the effect is not suspend check if all effects are suspended
2392 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2393 if (index < 0) {
2394 return;
2395 }
2396 if (!isEffectEligibleForSuspend(effect->desc())) {
2397 return;
2398 }
2399 setEffectSuspended_l(&effect->desc().type, enabled);
2400 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2401 if (index < 0) {
2402 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2403 return;
2404 }
2405 }
2406 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2407 effect->desc().type.timeLow);
2408 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002409 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002410 if (desc->mEffect == 0) {
2411 desc->mEffect = effect;
2412 effect->setEnabled(false);
2413 effect->setSuspended(true);
2414 }
2415 } else {
2416 if (index < 0) {
2417 return;
2418 }
2419 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2420 effect->desc().type.timeLow);
2421 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2422 desc->mEffect.clear();
2423 effect->setSuspended(false);
2424 }
2425}
2426
Eric Laurent5baf2af2013-09-12 17:37:00 -07002427bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002428{
2429 Mutex::Autolock _l(mLock);
2430 size_t size = mEffects.size();
2431 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002432 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002433 return true;
2434 }
2435 }
2436 return false;
2437}
2438
Eric Laurentaaa44472014-09-12 17:41:50 -07002439void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2440{
2441 Mutex::Autolock _l(mLock);
2442 mThread = thread;
2443 for (size_t i = 0; i < mEffects.size(); i++) {
2444 mEffects[i]->setThread(thread);
2445 }
2446}
2447
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002448void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2449{
2450 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2451 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2452 }
2453 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2454 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2455 }
2456}
2457
2458void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2459{
2460 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2461 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2462 }
2463 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2464 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2465 }
2466}
2467
2468bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002469{
2470 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002471 for (const auto &effect : mEffects) {
2472 if (effect->isProcessImplemented()) {
2473 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002474 }
2475 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002476 // Allow effects without processing.
2477 return true;
2478}
2479
2480bool AudioFlinger::EffectChain::isFastCompatible() const
2481{
2482 Mutex::Autolock _l(mLock);
2483 for (const auto &effect : mEffects) {
2484 if (effect->isProcessImplemented()
2485 && effect->isImplementationSoftware()) {
2486 return false;
2487 }
2488 }
2489 // Allow effects without processing or hw accelerated effects.
2490 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002491}
2492
2493// isCompatibleWithThread_l() must be called with thread->mLock held
2494bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2495{
2496 Mutex::Autolock _l(mLock);
2497 for (size_t i = 0; i < mEffects.size(); i++) {
2498 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2499 return false;
2500 }
2501 }
2502 return true;
2503}
2504
Glenn Kasten63238ef2015-03-02 15:50:29 -08002505} // namespace android