blob: b4ff0d69cf079c34508f38e08464fad27b4a21cc [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{
Eric Laurentca7cc822012-11-19 14:55:58 -08001282 String8 result;
1283
Andy Hung9718d662017-12-22 17:57:39 -08001284 result.appendFormat("\tEffect ID %d:\n", mId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001285
1286 bool locked = AudioFlinger::dumpTryLock(mLock);
1287 // failed to lock - AudioFlinger is probably deadlocked
1288 if (!locked) {
1289 result.append("\t\tCould not lock Fx mutex:\n");
1290 }
1291
1292 result.append("\t\tSession Status State Engine:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001293 result.appendFormat("\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001294 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001295
1296 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001297 char uuidStr[64];
1298 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001299 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001300 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
Andy Hung9718d662017-12-22 17:57:39 -08001301 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
1302 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001303 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001304 mDescriptor.flags,
1305 effectFlagsToString(mDescriptor.flags).string());
Andy Hung9718d662017-12-22 17:57:39 -08001306 result.appendFormat("\t\t- name: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001307 mDescriptor.name);
Andy Hung9718d662017-12-22 17:57:39 -08001308
1309 result.appendFormat("\t\t- implementor: %s\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001310 mDescriptor.implementor);
Andy Hung9718d662017-12-22 17:57:39 -08001311
1312 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001313
1314 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001315 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1316 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1317 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001318 mConfig.inputCfg.buffer.frameCount,
1319 mConfig.inputCfg.samplingRate,
1320 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001321 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001322 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001323
1324 result.append("\t\t- Output configuration:\n");
1325 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001326 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001327 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001328 mConfig.outputCfg.buffer.frameCount,
1329 mConfig.outputCfg.samplingRate,
1330 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001331 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001332 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001333
rago94a1ee82017-07-21 15:11:02 -07001334#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001335
Andy Hungbded9c82017-11-30 18:47:35 -08001336 result.appendFormat("\t\t- HAL buffers:\n"
1337 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1338 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1339 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1340 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1341 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001342#endif
1343
Andy Hung9718d662017-12-22 17:57:39 -08001344 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08001345 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Andy Hung9718d662017-12-22 17:57:39 -08001346 char buffer[256];
Eric Laurentca7cc822012-11-19 14:55:58 -08001347 for (size_t i = 0; i < mHandles.size(); ++i) {
1348 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001349 if (handle != NULL && !handle->disconnected()) {
Andy Hung9718d662017-12-22 17:57:39 -08001350 handle->dumpToBuffer(buffer, sizeof(buffer));
Eric Laurentca7cc822012-11-19 14:55:58 -08001351 result.append(buffer);
1352 }
1353 }
1354
Eric Laurentca7cc822012-11-19 14:55:58 -08001355 write(fd, result.string(), result.length());
1356
1357 if (locked) {
1358 mLock.unlock();
1359 }
1360}
1361
1362// ----------------------------------------------------------------------------
1363// EffectHandle implementation
1364// ----------------------------------------------------------------------------
1365
1366#undef LOG_TAG
1367#define LOG_TAG "AudioFlinger::EffectHandle"
1368
1369AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1370 const sp<AudioFlinger::Client>& client,
1371 const sp<IEffectClient>& effectClient,
1372 int32_t priority)
1373 : BnEffect(),
1374 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001375 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001376{
1377 ALOGV("constructor %p", this);
1378
1379 if (client == 0) {
1380 return;
1381 }
1382 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1383 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001384 if (mCblkMemory == 0 ||
1385 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001386 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001387 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001388 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001389 return;
1390 }
Glenn Kastene75da402013-11-20 13:54:52 -08001391 new(mCblk) effect_param_cblk_t();
1392 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001393}
1394
1395AudioFlinger::EffectHandle::~EffectHandle()
1396{
1397 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001398 disconnect(false);
1399}
1400
Glenn Kastene75da402013-11-20 13:54:52 -08001401status_t AudioFlinger::EffectHandle::initCheck()
1402{
1403 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1404}
1405
Eric Laurentca7cc822012-11-19 14:55:58 -08001406status_t AudioFlinger::EffectHandle::enable()
1407{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001408 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001409 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001410 sp<EffectModule> effect = mEffect.promote();
1411 if (effect == 0 || mDisconnected) {
1412 return DEAD_OBJECT;
1413 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 if (!mHasControl) {
1415 return INVALID_OPERATION;
1416 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001417
1418 if (mEnabled) {
1419 return NO_ERROR;
1420 }
1421
1422 mEnabled = true;
1423
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001424 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001426 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 }
1428
1429 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001430 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 return NO_ERROR;
1432 }
1433
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001434 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001435 if (status != NO_ERROR) {
1436 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001437 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001438 }
1439 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001440 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001441 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001442 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1443 Mutex::Autolock _l(thread->mLock);
1444 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001445 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001446 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001447 if (thread->type() == ThreadBase::OFFLOAD) {
1448 PlaybackThread *t = (PlaybackThread *)thread.get();
1449 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1450 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001451 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001452 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1453 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001454 }
1455 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001456 }
1457 return status;
1458}
1459
1460status_t AudioFlinger::EffectHandle::disable()
1461{
1462 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001463 AutoMutex _l(mLock);
1464 sp<EffectModule> effect = mEffect.promote();
1465 if (effect == 0 || mDisconnected) {
1466 return DEAD_OBJECT;
1467 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001468 if (!mHasControl) {
1469 return INVALID_OPERATION;
1470 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001471
1472 if (!mEnabled) {
1473 return NO_ERROR;
1474 }
1475 mEnabled = false;
1476
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001477 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001478 return NO_ERROR;
1479 }
1480
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001481 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001482
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001483 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001485 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001486 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1487 Mutex::Autolock _l(thread->mLock);
1488 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001489 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001490 }
1491
1492 return status;
1493}
1494
1495void AudioFlinger::EffectHandle::disconnect()
1496{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001497 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001498 disconnect(true);
1499}
1500
1501void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1502{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001503 AutoMutex _l(mLock);
1504 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1505 if (mDisconnected) {
1506 if (unpinIfLast) {
1507 android_errorWriteLog(0x534e4554, "32707507");
1508 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001509 return;
1510 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001511 mDisconnected = true;
1512 sp<ThreadBase> thread;
1513 {
1514 sp<EffectModule> effect = mEffect.promote();
1515 if (effect != 0) {
1516 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 }
1518 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001519 if (thread != 0) {
1520 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001521 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001522 // try to cleanup as much as we can
1523 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001524 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1525 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001526 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001527 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001528
Eric Laurentca7cc822012-11-19 14:55:58 -08001529 if (mClient != 0) {
1530 if (mCblk != NULL) {
1531 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1532 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1533 }
1534 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001535 // Client destructor must run with AudioFlinger client mutex locked
1536 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001537 mClient.clear();
1538 }
1539}
1540
1541status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1542 uint32_t cmdSize,
1543 void *pCmdData,
1544 uint32_t *replySize,
1545 void *pReplyData)
1546{
1547 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001548 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001549
Eric Laurentc7ab3092017-06-15 18:43:46 -07001550 // reject commands reserved for internal use by audio framework if coming from outside
1551 // of audioserver
1552 switch(cmdCode) {
1553 case EFFECT_CMD_ENABLE:
1554 case EFFECT_CMD_DISABLE:
1555 case EFFECT_CMD_SET_PARAM:
1556 case EFFECT_CMD_SET_PARAM_DEFERRED:
1557 case EFFECT_CMD_SET_PARAM_COMMIT:
1558 case EFFECT_CMD_GET_PARAM:
1559 break;
1560 default:
1561 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1562 break;
1563 }
1564 android_errorWriteLog(0x534e4554, "62019992");
1565 return BAD_VALUE;
1566 }
1567
Eric Laurent1ffc5852016-12-15 14:46:09 -08001568 if (cmdCode == EFFECT_CMD_ENABLE) {
1569 if (*replySize < sizeof(int)) {
1570 android_errorWriteLog(0x534e4554, "32095713");
1571 return BAD_VALUE;
1572 }
1573 *(int *)pReplyData = NO_ERROR;
1574 *replySize = sizeof(int);
1575 return enable();
1576 } else if (cmdCode == EFFECT_CMD_DISABLE) {
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 disable();
1584 }
1585
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001586 AutoMutex _l(mLock);
1587 sp<EffectModule> effect = mEffect.promote();
1588 if (effect == 0 || mDisconnected) {
1589 return DEAD_OBJECT;
1590 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001591 // only get parameter command is permitted for applications not controlling the effect
1592 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1593 return INVALID_OPERATION;
1594 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001595 if (mClient == 0) {
1596 return INVALID_OPERATION;
1597 }
1598
1599 // handle commands that are not forwarded transparently to effect engine
1600 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001601 if (*replySize < sizeof(int)) {
1602 android_errorWriteLog(0x534e4554, "32095713");
1603 return BAD_VALUE;
1604 }
1605 *(int *)pReplyData = NO_ERROR;
1606 *replySize = sizeof(int);
1607
Eric Laurentca7cc822012-11-19 14:55:58 -08001608 // No need to trylock() here as this function is executed in the binder thread serving a
1609 // particular client process: no risk to block the whole media server process or mixer
1610 // threads if we are stuck here
1611 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001612 // keep local copy of index in case of client corruption b/32220769
1613 const uint32_t clientIndex = mCblk->clientIndex;
1614 const uint32_t serverIndex = mCblk->serverIndex;
1615 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1616 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001617 mCblk->serverIndex = 0;
1618 mCblk->clientIndex = 0;
1619 return BAD_VALUE;
1620 }
1621 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001622 effect_param_t *param = NULL;
1623 for (uint32_t index = serverIndex; index < clientIndex;) {
1624 int *p = (int *)(mBuffer + index);
1625 const int size = *p++;
1626 if (size < 0
1627 || size > EFFECT_PARAM_BUFFER_SIZE
1628 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001629 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001630 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 break;
1632 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001633
1634 // copy to local memory in case of client corruption b/32220769
1635 param = (effect_param_t *)realloc(param, size);
1636 if (param == NULL) {
1637 ALOGW("command(): out of memory");
1638 status = NO_MEMORY;
1639 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001640 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001641 memcpy(param, p, size);
1642
1643 int reply = 0;
1644 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001645 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001646 size,
1647 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001648 &rsize,
1649 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001650
1651 // verify shared memory: server index shouldn't change; client index can't go back.
1652 if (serverIndex != mCblk->serverIndex
1653 || clientIndex > mCblk->clientIndex) {
1654 android_errorWriteLog(0x534e4554, "32220769");
1655 status = BAD_VALUE;
1656 break;
1657 }
1658
Eric Laurentca7cc822012-11-19 14:55:58 -08001659 // stop at first error encountered
1660 if (ret != NO_ERROR) {
1661 status = ret;
1662 *(int *)pReplyData = reply;
1663 break;
1664 } else if (reply != NO_ERROR) {
1665 *(int *)pReplyData = reply;
1666 break;
1667 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001668 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001669 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001670 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001671 mCblk->serverIndex = 0;
1672 mCblk->clientIndex = 0;
1673 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001674 }
1675
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001676 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001677}
1678
1679void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1680{
1681 ALOGV("setControl %p control %d", this, hasControl);
1682
1683 mHasControl = hasControl;
1684 mEnabled = enabled;
1685
1686 if (signal && mEffectClient != 0) {
1687 mEffectClient->controlStatusChanged(hasControl);
1688 }
1689}
1690
1691void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1692 uint32_t cmdSize,
1693 void *pCmdData,
1694 uint32_t replySize,
1695 void *pReplyData)
1696{
1697 if (mEffectClient != 0) {
1698 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1699 }
1700}
1701
1702
1703
1704void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1705{
1706 if (mEffectClient != 0) {
1707 mEffectClient->enableStatusChanged(enabled);
1708 }
1709}
1710
1711status_t AudioFlinger::EffectHandle::onTransact(
1712 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1713{
1714 return BnEffect::onTransact(code, data, reply, flags);
1715}
1716
1717
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001718void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001719{
1720 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1721
Marco Nelissenb2208842014-02-07 14:00:50 -08001722 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001723 (mClient == 0) ? getpid_cached : mClient->pid(),
1724 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001725 mHasControl ? "yes" : "no",
1726 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001727 mCblk ? mCblk->clientIndex : 0,
1728 mCblk ? mCblk->serverIndex : 0
1729 );
1730
1731 if (locked) {
1732 mCblk->lock.unlock();
1733 }
1734}
1735
1736#undef LOG_TAG
1737#define LOG_TAG "AudioFlinger::EffectChain"
1738
1739AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001740 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001741 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001742 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001743 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001744{
1745 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1746 if (thread == NULL) {
1747 return;
1748 }
1749 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1750 thread->frameCount();
1751}
1752
1753AudioFlinger::EffectChain::~EffectChain()
1754{
Eric Laurentca7cc822012-11-19 14:55:58 -08001755}
1756
1757// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1758sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1759 effect_descriptor_t *descriptor)
1760{
1761 size_t size = mEffects.size();
1762
1763 for (size_t i = 0; i < size; i++) {
1764 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1765 return mEffects[i];
1766 }
1767 }
1768 return 0;
1769}
1770
1771// getEffectFromId_l() must be called with ThreadBase::mLock held
1772sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1773{
1774 size_t size = mEffects.size();
1775
1776 for (size_t i = 0; i < size; i++) {
1777 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1778 if (id == 0 || mEffects[i]->id() == id) {
1779 return mEffects[i];
1780 }
1781 }
1782 return 0;
1783}
1784
1785// getEffectFromType_l() must be called with ThreadBase::mLock held
1786sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1787 const effect_uuid_t *type)
1788{
1789 size_t size = mEffects.size();
1790
1791 for (size_t i = 0; i < size; i++) {
1792 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1793 return mEffects[i];
1794 }
1795 }
1796 return 0;
1797}
1798
1799void AudioFlinger::EffectChain::clearInputBuffer()
1800{
1801 Mutex::Autolock _l(mLock);
1802 sp<ThreadBase> thread = mThread.promote();
1803 if (thread == 0) {
1804 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1805 return;
1806 }
1807 clearInputBuffer_l(thread);
1808}
1809
1810// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001811void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001812{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001813 if (mInBuffer == NULL) {
1814 return;
1815 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001816 // TODO: This will change in the future, depending on multichannel
1817 // and sample format changes for effects.
1818 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1819 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001820
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001821 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001822 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1823 * std::min((uint32_t)FCC_2, thread->channelCount());
1824
Mikhail Naganov022b9952017-01-04 16:36:51 -08001825 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1826 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001827}
1828
1829// Must be called with EffectChain::mLock locked
1830void AudioFlinger::EffectChain::process_l()
1831{
1832 sp<ThreadBase> thread = mThread.promote();
1833 if (thread == 0) {
1834 ALOGW("process_l(): cannot promote mixer thread");
1835 return;
1836 }
1837 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1838 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001839 // never process effects when:
1840 // - on an OFFLOAD thread
1841 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001842 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1843 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001844 if (!isGlobalSession) {
1845 bool tracksOnSession = (trackCnt() != 0);
1846
1847 if (!tracksOnSession && mTailBufferCount == 0) {
1848 doProcess = false;
1849 }
1850
1851 if (activeTrackCnt() == 0) {
1852 // if no track is active and the effect tail has not been rendered,
1853 // the input buffer must be cleared here as the mixer process will not do it
1854 if (tracksOnSession || mTailBufferCount > 0) {
1855 clearInputBuffer_l(thread);
1856 if (mTailBufferCount > 0) {
1857 mTailBufferCount--;
1858 }
1859 }
1860 }
1861 }
1862
1863 size_t size = mEffects.size();
1864 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001865 // Only the input and output buffers of the chain can be external,
1866 // and 'update' / 'commit' do nothing for allocated buffers, thus
1867 // it's not needed to consider any other buffers here.
1868 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001869 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1870 mOutBuffer->update();
1871 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001872 for (size_t i = 0; i < size; i++) {
1873 mEffects[i]->process();
1874 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001875 mInBuffer->commit();
1876 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1877 mOutBuffer->commit();
1878 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001879 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001880 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001881 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001882 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1883 }
1884 if (doResetVolume) {
1885 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001886 }
1887}
1888
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001889// createEffect_l() must be called with ThreadBase::mLock held
1890status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1891 ThreadBase *thread,
1892 effect_descriptor_t *desc,
1893 int id,
1894 audio_session_t sessionId,
1895 bool pinned)
1896{
1897 Mutex::Autolock _l(mLock);
1898 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1899 status_t lStatus = effect->status();
1900 if (lStatus == NO_ERROR) {
1901 lStatus = addEffect_ll(effect);
1902 }
1903 if (lStatus != NO_ERROR) {
1904 effect.clear();
1905 }
1906 return lStatus;
1907}
1908
1909// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001910status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1911{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001912 Mutex::Autolock _l(mLock);
1913 return addEffect_ll(effect);
1914}
1915// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1916status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1917{
Eric Laurentca7cc822012-11-19 14:55:58 -08001918 effect_descriptor_t desc = effect->desc();
1919 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1920
Eric Laurentca7cc822012-11-19 14:55:58 -08001921 effect->setChain(this);
1922 sp<ThreadBase> thread = mThread.promote();
1923 if (thread == 0) {
1924 return NO_INIT;
1925 }
1926 effect->setThread(thread);
1927
1928 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1929 // Auxiliary effects are inserted at the beginning of mEffects vector as
1930 // they are processed first and accumulated in chain input buffer
1931 mEffects.insertAt(effect, 0);
1932
1933 // the input buffer for auxiliary effect contains mono samples in
1934 // 32 bit format. This is to avoid saturation in AudoMixer
1935 // accumulation stage. Saturation is done in EffectModule::process() before
1936 // calling the process in effect engine
1937 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001938 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001939#ifdef FLOAT_EFFECT_CHAIN
1940 status_t result = EffectBufferHalInterface::allocate(
1941 numSamples * sizeof(float), &halBuffer);
1942#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001943 status_t result = EffectBufferHalInterface::allocate(
1944 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001945#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001946 if (result != OK) return result;
1947 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001948 // auxiliary effects output samples to chain input buffer for further processing
1949 // by insert effects
1950 effect->setOutBuffer(mInBuffer);
1951 } else {
1952 // Insert effects are inserted at the end of mEffects vector as they are processed
1953 // after track and auxiliary effects.
1954 // Insert effect order as a function of indicated preference:
1955 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1956 // another effect is present
1957 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1958 // last effect claiming first position
1959 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1960 // first effect claiming last position
1961 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1962 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1963 // already present
1964
1965 size_t size = mEffects.size();
1966 size_t idx_insert = size;
1967 ssize_t idx_insert_first = -1;
1968 ssize_t idx_insert_last = -1;
1969
1970 for (size_t i = 0; i < size; i++) {
1971 effect_descriptor_t d = mEffects[i]->desc();
1972 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1973 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1974 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1975 // check invalid effect chaining combinations
1976 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1977 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1978 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1979 desc.name, d.name);
1980 return INVALID_OPERATION;
1981 }
1982 // remember position of first insert effect and by default
1983 // select this as insert position for new effect
1984 if (idx_insert == size) {
1985 idx_insert = i;
1986 }
1987 // remember position of last insert effect claiming
1988 // first position
1989 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1990 idx_insert_first = i;
1991 }
1992 // remember position of first insert effect claiming
1993 // last position
1994 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1995 idx_insert_last == -1) {
1996 idx_insert_last = i;
1997 }
1998 }
1999 }
2000
2001 // modify idx_insert from first position if needed
2002 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2003 if (idx_insert_last != -1) {
2004 idx_insert = idx_insert_last;
2005 } else {
2006 idx_insert = size;
2007 }
2008 } else {
2009 if (idx_insert_first != -1) {
2010 idx_insert = idx_insert_first + 1;
2011 }
2012 }
2013
2014 // always read samples from chain input buffer
2015 effect->setInBuffer(mInBuffer);
2016
2017 // if last effect in the chain, output samples to chain
2018 // output buffer, otherwise to chain input buffer
2019 if (idx_insert == size) {
2020 if (idx_insert != 0) {
2021 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2022 mEffects[idx_insert-1]->configure();
2023 }
2024 effect->setOutBuffer(mOutBuffer);
2025 } else {
2026 effect->setOutBuffer(mInBuffer);
2027 }
2028 mEffects.insertAt(effect, idx_insert);
2029
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002030 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002031 idx_insert);
2032 }
2033 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002034
Eric Laurentca7cc822012-11-19 14:55:58 -08002035 return NO_ERROR;
2036}
2037
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002038// removeEffect_l() must be called with ThreadBase::mLock held
2039size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2040 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002041{
2042 Mutex::Autolock _l(mLock);
2043 size_t size = mEffects.size();
2044 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2045
2046 for (size_t i = 0; i < size; i++) {
2047 if (effect == mEffects[i]) {
2048 // calling stop here will remove pre-processing effect from the audio HAL.
2049 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2050 // the middle of a read from audio HAL
2051 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2052 mEffects[i]->state() == EffectModule::STOPPING) {
2053 mEffects[i]->stop();
2054 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002055 if (release) {
2056 mEffects[i]->release_l();
2057 }
2058
Mikhail Naganov022b9952017-01-04 16:36:51 -08002059 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002060 if (i == size - 1 && i != 0) {
2061 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2062 mEffects[i - 1]->configure();
2063 }
2064 }
2065 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002066 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002067 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002068
Eric Laurentca7cc822012-11-19 14:55:58 -08002069 break;
2070 }
2071 }
2072
2073 return mEffects.size();
2074}
2075
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002076// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002077void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2078{
2079 size_t size = mEffects.size();
2080 for (size_t i = 0; i < size; i++) {
2081 mEffects[i]->setDevice(device);
2082 }
2083}
2084
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002085// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002086void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2087{
2088 size_t size = mEffects.size();
2089 for (size_t i = 0; i < size; i++) {
2090 mEffects[i]->setMode(mode);
2091 }
2092}
2093
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002094// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002095void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2096{
2097 size_t size = mEffects.size();
2098 for (size_t i = 0; i < size; i++) {
2099 mEffects[i]->setAudioSource(source);
2100 }
2101}
2102
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002103// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002104bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002105{
2106 uint32_t newLeft = *left;
2107 uint32_t newRight = *right;
2108 bool hasControl = false;
2109 int ctrlIdx = -1;
2110 size_t size = mEffects.size();
2111
2112 // first update volume controller
2113 for (size_t i = size; i > 0; i--) {
2114 if (mEffects[i - 1]->isProcessEnabled() &&
2115 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2116 ctrlIdx = i - 1;
2117 hasControl = true;
2118 break;
2119 }
2120 }
2121
Eric Laurentfa1e1232016-08-02 19:01:49 -07002122 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002123 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002124 if (hasControl) {
2125 *left = mNewLeftVolume;
2126 *right = mNewRightVolume;
2127 }
2128 return hasControl;
2129 }
2130
2131 mVolumeCtrlIdx = ctrlIdx;
2132 mLeftVolume = newLeft;
2133 mRightVolume = newRight;
2134
2135 // second get volume update from volume controller
2136 if (ctrlIdx >= 0) {
2137 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2138 mNewLeftVolume = newLeft;
2139 mNewRightVolume = newRight;
2140 }
2141 // then indicate volume to all other effects in chain.
2142 // Pass altered volume to effects before volume controller
2143 // and requested volume to effects after controller
2144 uint32_t lVol = newLeft;
2145 uint32_t rVol = newRight;
2146
2147 for (size_t i = 0; i < size; i++) {
2148 if ((int)i == ctrlIdx) {
2149 continue;
2150 }
2151 // this also works for ctrlIdx == -1 when there is no volume controller
2152 if ((int)i > ctrlIdx) {
2153 lVol = *left;
2154 rVol = *right;
2155 }
2156 mEffects[i]->setVolume(&lVol, &rVol, false);
2157 }
2158 *left = newLeft;
2159 *right = newRight;
2160
2161 return hasControl;
2162}
2163
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002164// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002165void AudioFlinger::EffectChain::resetVolume_l()
2166{
Eric Laurente7449bf2016-08-03 18:44:07 -07002167 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2168 uint32_t left = mLeftVolume;
2169 uint32_t right = mRightVolume;
2170 (void)setVolume_l(&left, &right, true);
2171 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002172}
2173
Eric Laurent1b928682014-10-02 19:41:47 -07002174void AudioFlinger::EffectChain::syncHalEffectsState()
2175{
2176 Mutex::Autolock _l(mLock);
2177 for (size_t i = 0; i < mEffects.size(); i++) {
2178 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2179 mEffects[i]->state() == EffectModule::STOPPING) {
2180 mEffects[i]->addEffectToHal_l();
2181 }
2182 }
2183}
2184
Eric Laurentca7cc822012-11-19 14:55:58 -08002185void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2186{
2187 const size_t SIZE = 256;
2188 char buffer[SIZE];
2189 String8 result;
2190
Marco Nelissenb2208842014-02-07 14:00:50 -08002191 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002192 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002193 result.append(buffer);
2194
Marco Nelissenb2208842014-02-07 14:00:50 -08002195 if (numEffects) {
2196 bool locked = AudioFlinger::dumpTryLock(mLock);
2197 // failed to lock - AudioFlinger is probably deadlocked
2198 if (!locked) {
2199 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002200 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002201
Andy Hungbded9c82017-11-30 18:47:35 -08002202 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2203 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2204 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2205 (int)inBufferStr.size(), "In buffer ",
2206 (int)outBufferStr.size(), "Out buffer ");
2207 result.appendFormat("\t%s %s %d\n",
2208 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002209 write(fd, result.string(), result.size());
2210
2211 for (size_t i = 0; i < numEffects; ++i) {
2212 sp<EffectModule> effect = mEffects[i];
2213 if (effect != 0) {
2214 effect->dump(fd, args);
2215 }
2216 }
2217
2218 if (locked) {
2219 mLock.unlock();
2220 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002221 }
2222}
2223
2224// must be called with ThreadBase::mLock held
2225void AudioFlinger::EffectChain::setEffectSuspended_l(
2226 const effect_uuid_t *type, bool suspend)
2227{
2228 sp<SuspendedEffectDesc> desc;
2229 // use effect type UUID timelow as key as there is no real risk of identical
2230 // timeLow fields among effect type UUIDs.
2231 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2232 if (suspend) {
2233 if (index >= 0) {
2234 desc = mSuspendedEffects.valueAt(index);
2235 } else {
2236 desc = new SuspendedEffectDesc();
2237 desc->mType = *type;
2238 mSuspendedEffects.add(type->timeLow, desc);
2239 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2240 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002241
Eric Laurentca7cc822012-11-19 14:55:58 -08002242 if (desc->mRefCount++ == 0) {
2243 sp<EffectModule> effect = getEffectIfEnabled(type);
2244 if (effect != 0) {
2245 desc->mEffect = effect;
2246 effect->setSuspended(true);
2247 effect->setEnabled(false);
2248 }
2249 }
2250 } else {
2251 if (index < 0) {
2252 return;
2253 }
2254 desc = mSuspendedEffects.valueAt(index);
2255 if (desc->mRefCount <= 0) {
2256 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002257 desc->mRefCount = 0;
2258 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002259 }
2260 if (--desc->mRefCount == 0) {
2261 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2262 if (desc->mEffect != 0) {
2263 sp<EffectModule> effect = desc->mEffect.promote();
2264 if (effect != 0) {
2265 effect->setSuspended(false);
2266 effect->lock();
2267 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002268 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002269 effect->setEnabled_l(handle->enabled());
2270 }
2271 effect->unlock();
2272 }
2273 desc->mEffect.clear();
2274 }
2275 mSuspendedEffects.removeItemsAt(index);
2276 }
2277 }
2278}
2279
2280// must be called with ThreadBase::mLock held
2281void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2282{
2283 sp<SuspendedEffectDesc> desc;
2284
2285 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2286 if (suspend) {
2287 if (index >= 0) {
2288 desc = mSuspendedEffects.valueAt(index);
2289 } else {
2290 desc = new SuspendedEffectDesc();
2291 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2292 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2293 }
2294 if (desc->mRefCount++ == 0) {
2295 Vector< sp<EffectModule> > effects;
2296 getSuspendEligibleEffects(effects);
2297 for (size_t i = 0; i < effects.size(); i++) {
2298 setEffectSuspended_l(&effects[i]->desc().type, true);
2299 }
2300 }
2301 } else {
2302 if (index < 0) {
2303 return;
2304 }
2305 desc = mSuspendedEffects.valueAt(index);
2306 if (desc->mRefCount <= 0) {
2307 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2308 desc->mRefCount = 1;
2309 }
2310 if (--desc->mRefCount == 0) {
2311 Vector<const effect_uuid_t *> types;
2312 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2313 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2314 continue;
2315 }
2316 types.add(&mSuspendedEffects.valueAt(i)->mType);
2317 }
2318 for (size_t i = 0; i < types.size(); i++) {
2319 setEffectSuspended_l(types[i], false);
2320 }
2321 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2322 mSuspendedEffects.keyAt(index));
2323 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2324 }
2325 }
2326}
2327
2328
2329// The volume effect is used for automated tests only
2330#ifndef OPENSL_ES_H_
2331static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2332 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2333const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2334#endif //OPENSL_ES_H_
2335
Eric Laurentd8365c52017-07-16 15:27:05 -07002336/* static */
2337bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2338{
2339 // Only NS and AEC are suspended when BtNRec is off
2340 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2341 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2342 return true;
2343 }
2344 return false;
2345}
2346
Eric Laurentca7cc822012-11-19 14:55:58 -08002347bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2348{
2349 // auxiliary effects and visualizer are never suspended on output mix
2350 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2351 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2352 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2353 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2354 return false;
2355 }
2356 return true;
2357}
2358
2359void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2360 Vector< sp<AudioFlinger::EffectModule> > &effects)
2361{
2362 effects.clear();
2363 for (size_t i = 0; i < mEffects.size(); i++) {
2364 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2365 effects.add(mEffects[i]);
2366 }
2367 }
2368}
2369
2370sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2371 const effect_uuid_t *type)
2372{
2373 sp<EffectModule> effect = getEffectFromType_l(type);
2374 return effect != 0 && effect->isEnabled() ? effect : 0;
2375}
2376
2377void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2378 bool enabled)
2379{
2380 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2381 if (enabled) {
2382 if (index < 0) {
2383 // if the effect is not suspend check if all effects are suspended
2384 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2385 if (index < 0) {
2386 return;
2387 }
2388 if (!isEffectEligibleForSuspend(effect->desc())) {
2389 return;
2390 }
2391 setEffectSuspended_l(&effect->desc().type, enabled);
2392 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2393 if (index < 0) {
2394 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2395 return;
2396 }
2397 }
2398 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2399 effect->desc().type.timeLow);
2400 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002401 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002402 if (desc->mEffect == 0) {
2403 desc->mEffect = effect;
2404 effect->setEnabled(false);
2405 effect->setSuspended(true);
2406 }
2407 } else {
2408 if (index < 0) {
2409 return;
2410 }
2411 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2412 effect->desc().type.timeLow);
2413 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2414 desc->mEffect.clear();
2415 effect->setSuspended(false);
2416 }
2417}
2418
Eric Laurent5baf2af2013-09-12 17:37:00 -07002419bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002420{
2421 Mutex::Autolock _l(mLock);
2422 size_t size = mEffects.size();
2423 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002424 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002425 return true;
2426 }
2427 }
2428 return false;
2429}
2430
Eric Laurentaaa44472014-09-12 17:41:50 -07002431void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2432{
2433 Mutex::Autolock _l(mLock);
2434 mThread = thread;
2435 for (size_t i = 0; i < mEffects.size(); i++) {
2436 mEffects[i]->setThread(thread);
2437 }
2438}
2439
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002440void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2441{
2442 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2443 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2444 }
2445 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2446 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2447 }
2448}
2449
2450void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2451{
2452 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2453 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2454 }
2455 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2456 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2457 }
2458}
2459
2460bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002461{
2462 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002463 for (const auto &effect : mEffects) {
2464 if (effect->isProcessImplemented()) {
2465 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002466 }
2467 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002468 // Allow effects without processing.
2469 return true;
2470}
2471
2472bool AudioFlinger::EffectChain::isFastCompatible() const
2473{
2474 Mutex::Autolock _l(mLock);
2475 for (const auto &effect : mEffects) {
2476 if (effect->isProcessImplemented()
2477 && effect->isImplementationSoftware()) {
2478 return false;
2479 }
2480 }
2481 // Allow effects without processing or hw accelerated effects.
2482 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002483}
2484
2485// isCompatibleWithThread_l() must be called with thread->mLock held
2486bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2487{
2488 Mutex::Autolock _l(mLock);
2489 for (size_t i = 0; i < mEffects.size(); i++) {
2490 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2491 return false;
2492 }
2493 }
2494 return true;
2495}
2496
Glenn Kasten63238ef2015-03-02 15:50:29 -08002497} // namespace android