blob: 9717075f42c6371316556c5f54cf83a794c50009 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
27#include <system/audio_effects/effect_ns.h>
28#include <system/audio_effects/effect_visualizer.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080029#include <audio_utils/primitives.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070030#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070031#include <media/audiohal/EffectHalInterface.h>
32#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080033
34#include "AudioFlinger.h"
35#include "ServiceUtilities.h"
36
37// ----------------------------------------------------------------------------
38
39// Note: the following macro is used for extremely verbose logging message. In
40// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
41// 0; but one side effect of this is to turn all LOGV's as well. Some messages
42// are so verbose that we want to suppress them even when we have ALOG_ASSERT
43// turned on. Do not uncomment the #def below unless you really know what you
44// are doing and want to see all of the extremely verbose messages.
45//#define VERY_VERY_VERBOSE_LOGGING
46#ifdef VERY_VERY_VERBOSE_LOGGING
47#define ALOGVV ALOGV
48#else
49#define ALOGVV(a...) do { } while(0)
50#endif
51
52namespace android {
53
54// ----------------------------------------------------------------------------
55// EffectModule implementation
56// ----------------------------------------------------------------------------
57
58#undef LOG_TAG
59#define LOG_TAG "AudioFlinger::EffectModule"
60
61AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
62 const wp<AudioFlinger::EffectChain>& chain,
63 effect_descriptor_t *desc,
64 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080065 audio_session_t sessionId,
66 bool pinned)
67 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080068 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69 mDescriptor(*desc),
70 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080071 mStatus(NO_INIT), mState(IDLE),
72 // mMaxDisableWaitCnt is set by configure() and not used before then
73 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070074 mSuspended(false),
75 mAudioFlinger(thread->mAudioFlinger)
rago94a1ee82017-07-21 15:11:02 -070076#ifdef FLOAT_EFFECT_CHAIN
77 , mSupportsFloat(false)
78#endif
Eric Laurentca7cc822012-11-19 14:55:58 -080079{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080080 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080081 int lStatus;
82
83 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070084 mStatus = -ENODEV;
85 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070086 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070087 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070088 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070089 mStatus = effectsFactory->createEffect(
90 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
91 }
92 }
Eric Laurentca7cc822012-11-19 14:55:58 -080093
94 if (mStatus != NO_ERROR) {
95 return;
96 }
97 lStatus = init();
98 if (lStatus < 0) {
99 mStatus = lStatus;
100 goto Error;
101 }
102
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800103 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700104 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800105
Eric Laurentca7cc822012-11-19 14:55:58 -0800106 return;
107Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700108 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800109 ALOGV("Constructor Error %d", mStatus);
110}
111
112AudioFlinger::EffectModule::~EffectModule()
113{
114 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700115 if (mEffectInterface != 0) {
Mikhail Naganov424c4f52017-07-19 17:54:29 -0700116 char uuidStr[64];
117 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
118 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
119 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800120 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800121 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800122
Eric Laurentca7cc822012-11-19 14:55:58 -0800123}
124
125status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
126{
127 status_t status;
128
129 Mutex::Autolock _l(mLock);
130 int priority = handle->priority();
131 size_t size = mHandles.size();
132 EffectHandle *controlHandle = NULL;
133 size_t i;
134 for (i = 0; i < size; i++) {
135 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800136 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800137 continue;
138 }
139 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700140 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800141 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700142 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800143 if (h->priority() <= priority) {
144 break;
145 }
146 }
147 // if inserted in first place, move effect control from previous owner to this handle
148 if (i == 0) {
149 bool enabled = false;
150 if (controlHandle != NULL) {
151 enabled = controlHandle->enabled();
152 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
153 }
154 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
155 status = NO_ERROR;
156 } else {
157 status = ALREADY_EXISTS;
158 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700159 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800160 mHandles.insertAt(handle, i);
161 return status;
162}
163
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800164ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800165{
166 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800167 return removeHandle_l(handle);
168}
169
170ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
171{
Eric Laurentca7cc822012-11-19 14:55:58 -0800172 size_t size = mHandles.size();
173 size_t i;
174 for (i = 0; i < size; i++) {
175 if (mHandles[i] == handle) {
176 break;
177 }
178 }
179 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800180 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
181 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800182 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800183 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800184
185 mHandles.removeAt(i);
186 // if removed from first place, move effect control from this handle to next in line
187 if (i == 0) {
188 EffectHandle *h = controlHandle_l();
189 if (h != NULL) {
190 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
191 }
192 }
193
194 // Prevent calls to process() and other functions on effect interface from now on.
195 // The effect engine will be released by the destructor when the last strong reference on
196 // this object is released which can happen after next process is called.
197 if (mHandles.size() == 0 && !mPinned) {
198 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800199 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800200 }
201
202 return mHandles.size();
203}
204
205// must be called with EffectModule::mLock held
206AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
207{
208 // the first valid handle in the list has control over the module
209 for (size_t i = 0; i < mHandles.size(); i++) {
210 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800211 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 return h;
213 }
214 }
215
216 return NULL;
217}
218
Eric Laurentf10c7092016-12-06 17:09:56 -0800219// unsafe method called when the effect parent thread has been destroyed
220ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
221{
222 ALOGV("disconnect() %p handle %p", this, handle);
223 Mutex::Autolock _l(mLock);
224 ssize_t numHandles = removeHandle_l(handle);
225 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
226 AudioSystem::unregisterEffect(mId);
227 sp<AudioFlinger> af = mAudioFlinger.promote();
228 if (af != 0) {
229 mLock.unlock();
230 af->updateOrphanEffectChains(this);
231 mLock.lock();
232 }
233 }
234 return numHandles;
235}
236
Eric Laurentfa1e1232016-08-02 19:01:49 -0700237bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800238 Mutex::Autolock _l(mLock);
239
Eric Laurentfa1e1232016-08-02 19:01:49 -0700240 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800241 switch (mState) {
242 case RESTART:
243 reset_l();
244 // FALL THROUGH
245
246 case STARTING:
247 // clear auxiliary effect input buffer for next accumulation
248 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
249 memset(mConfig.inputCfg.buffer.raw,
250 0,
251 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
252 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700253 if (start_l() == NO_ERROR) {
254 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700255 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700256 } else {
257 mState = IDLE;
258 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800259 break;
260 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700261 if (stop_l() == NO_ERROR) {
262 mDisableWaitCnt = mMaxDisableWaitCnt;
263 } else {
264 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
265 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800266 mState = STOPPED;
267 break;
268 case STOPPED:
269 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
270 // turn off sequence.
271 if (--mDisableWaitCnt == 0) {
272 reset_l();
273 mState = IDLE;
274 }
275 break;
276 default: //IDLE , ACTIVE, DESTROYED
277 break;
278 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700279
280 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800281}
282
283void AudioFlinger::EffectModule::process()
284{
285 Mutex::Autolock _l(mLock);
286
Mikhail Naganov022b9952017-01-04 16:36:51 -0800287 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800288 return;
289 }
290
rago94a1ee82017-07-21 15:11:02 -0700291 // TODO: Implement multichannel effects; here outChannelCount == FCC_2 == 2
292 const uint32_t inChannelCount =
293 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
294 const uint32_t outChannelCount =
295 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
296 const bool auxType =
297 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
298
Eric Laurentca7cc822012-11-19 14:55:58 -0800299 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700300 int ret;
301 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700302 if (auxType) {
303 // We overwrite the aux input buffer here and clear after processing.
304 // Note that aux input buffers are format q4_27.
305#ifdef FLOAT_EFFECT_CHAIN
306 if (mSupportsFloat) {
307 // Do in-place float conversion for auxiliary effect input buffer.
308 static_assert(sizeof(float) <= sizeof(int32_t),
309 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
310
311 const int32_t * const p32 = mConfig.inputCfg.buffer.s32;
312 float * const pFloat = mConfig.inputCfg.buffer.f32;
313 memcpy_to_float_from_q4_27(pFloat, p32, mConfig.inputCfg.buffer.frameCount);
314 } else {
315 const size_t pairs = mConfig.inputCfg.buffer.frameCount / 2;
316 ditherAndClamp(mConfig.inputCfg.buffer.s32,
317 mConfig.inputCfg.buffer.s32,
318 pairs);
319 }
320#else
321 const size_t pairs = mConfig.inputCfg.buffer.frameCount / 2;
322 ditherAndClamp(mConfig.inputCfg.buffer.s32,
323 mConfig.inputCfg.buffer.s32,
324 pairs);
325#endif
326 }
327#ifdef FLOAT_EFFECT_CHAIN
328 if (mSupportsFloat) {
329 ret = mEffectInterface->process();
330 } else {
331 { // convert input to int16_t as effect doesn't support float.
332 if (!auxType) {
333 if (mInBuffer16.get() == nullptr) {
334 ALOGW("%s: mInBuffer16 is null, bypassing", __func__);
335 goto data_bypass;
336 }
337 const float * const pIn = mInBuffer->audioBuffer()->f32;
338 int16_t * const pIn16 = mInBuffer16->audioBuffer()->s16;
339 memcpy_to_i16_from_float(
340 pIn16, pIn, inChannelCount * mConfig.inputCfg.buffer.frameCount);
341 }
342 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
343 if (mOutBuffer16.get() == nullptr) {
344 ALOGW("%s: mOutBuffer16 is null, bypassing", __func__);
345 goto data_bypass;
346 }
347 int16_t * const pOut16 = mOutBuffer16->audioBuffer()->s16;
348 const float * const pOut = mOutBuffer->audioBuffer()->f32;
349 memcpy_to_i16_from_float(
350 pOut16,
351 pOut,
352 outChannelCount * mConfig.outputCfg.buffer.frameCount);
353 }
354 }
355
356 ret = mEffectInterface->process();
357
358 { // convert output back to float.
359 const int16_t * const pOut16 = mOutBuffer16->audioBuffer()->s16;
360 float * const pOut = mOutBuffer->audioBuffer()->f32;
361 memcpy_to_float_from_i16(
362 pOut, pOut16, outChannelCount * mConfig.outputCfg.buffer.frameCount);
363 }
364 }
365#else
Mikhail Naganov022b9952017-01-04 16:36:51 -0800366 ret = mEffectInterface->process();
rago94a1ee82017-07-21 15:11:02 -0700367#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700368 } else {
rago94a1ee82017-07-21 15:11:02 -0700369#ifdef FLOAT_EFFECT_CHAIN
370 data_bypass:
371#endif
372 if (!auxType /* aux effects do not require data bypass */
373 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw
374 && inChannelCount == outChannelCount) {
375 const size_t sampleCount = std::min(
376 mConfig.inputCfg.buffer.frameCount,
377 mConfig.outputCfg.buffer.frameCount) * outChannelCount;
378
379#ifdef FLOAT_EFFECT_CHAIN
380 const float * const in = mConfig.inputCfg.buffer.f32;
381 float * const out = mConfig.outputCfg.buffer.f32;
Eric Laurentca7cc822012-11-19 14:55:58 -0800382
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700383 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
rago94a1ee82017-07-21 15:11:02 -0700384 accumulate_float(out, in, sampleCount);
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700385 } else {
rago94a1ee82017-07-21 15:11:02 -0700386 memcpy(mConfig.outputCfg.buffer.f32, mConfig.inputCfg.buffer.f32,
387 sampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700388 }
rago94a1ee82017-07-21 15:11:02 -0700389
390#else
391 const int16_t * const in = mConfig.inputCfg.buffer.s16;
392 int16_t * const out = mConfig.outputCfg.buffer.s16;
393
394 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
395 accumulate_i16(out, in, sampleCount);
396 } else {
397 memcpy(mConfig.outputCfg.buffer.s16, mConfig.inputCfg.buffer.s16,
398 sampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
399 }
400#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700401 }
402 ret = -ENODATA;
403 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800404 // force transition to IDLE state when engine is ready
405 if (mState == STOPPED && ret == -ENODATA) {
406 mDisableWaitCnt = 1;
407 }
408
409 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700410 if (auxType) {
411 // input always q4_27 regardless of FLOAT_EFFECT_CHAIN.
412 const size_t size =
413 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
414 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800415 }
416 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700417 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800418 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
419 // If an insert effect is idle and input buffer is different from output buffer,
420 // accumulate input onto output
421 sp<EffectChain> chain = mChain.promote();
rago94a1ee82017-07-21 15:11:02 -0700422 if (chain != 0
423 && chain->activeTrackCnt() != 0
424 && inChannelCount == outChannelCount) {
425 const size_t sampleCount = std::min(
426 mConfig.inputCfg.buffer.frameCount,
427 mConfig.outputCfg.buffer.frameCount) * outChannelCount;
428#ifdef FLOAT_EFFECT_CHAIN
429 const float * const in = mConfig.inputCfg.buffer.f32;
430 float * const out = mConfig.outputCfg.buffer.f32;
431 accumulate_float(out, in, sampleCount);
432#else
433 const int16_t * const in = mConfig.inputCfg.buffer.s16;
434 int16_t * const out = mConfig.outputCfg.buffer.s16;
435 accumulate_i16(out, in, sampleCount);
436#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800437 }
438 }
439}
440
441void AudioFlinger::EffectModule::reset_l()
442{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700443 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800444 return;
445 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700446 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800447}
448
449status_t AudioFlinger::EffectModule::configure()
450{
rago94a1ee82017-07-21 15:11:02 -0700451 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700452 status_t status;
453 sp<ThreadBase> thread;
454 uint32_t size;
455 audio_channel_mask_t channelMask;
456
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700457 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700458 status = NO_INIT;
459 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800460 }
461
Eric Laurentd0ebb532013-04-02 16:41:41 -0700462 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800463 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700464 status = DEAD_OBJECT;
465 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800466 }
467
468 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700469 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700470 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800471
472 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
473 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900474 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
475 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800476 } else {
477 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700478 // TODO: Update this logic when multichannel effects are implemented.
479 // For offloaded tracks consider mono output as stereo for proper effect initialization
480 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
481 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
482 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
483 ALOGV("Overriding effect input and output as STEREO");
484 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800485 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700486
rago94a1ee82017-07-21 15:11:02 -0700487 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
488 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Eric Laurentca7cc822012-11-19 14:55:58 -0800489 mConfig.inputCfg.samplingRate = thread->sampleRate();
490 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
491 mConfig.inputCfg.bufferProvider.cookie = NULL;
492 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
493 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
494 mConfig.outputCfg.bufferProvider.cookie = NULL;
495 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
496 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
497 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
498 // Insert effect:
499 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
500 // always overwrites output buffer: input buffer == output buffer
501 // - in other sessions:
502 // last effect in the chain accumulates in output buffer: input buffer != output buffer
503 // other effect: overwrites output buffer: input buffer == output buffer
504 // Auxiliary effect:
505 // accumulates in output buffer: input buffer != output buffer
506 // Therefore: accumulate <=> input buffer != output buffer
507 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
508 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
509 } else {
510 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
511 }
512 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
513 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
514 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
515 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
516
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700517 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800518 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
519
520 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700521 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700522 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
523 sizeof(effect_config_t),
524 &mConfig,
525 &size,
526 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700527 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800528 status = cmdStatus;
rago94a1ee82017-07-21 15:11:02 -0700529#ifdef FLOAT_EFFECT_CHAIN
530 mSupportsFloat = true;
531#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800532 }
rago94a1ee82017-07-21 15:11:02 -0700533#ifdef FLOAT_EFFECT_CHAIN
534 else {
535 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
536 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
537 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
538 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
539 sizeof(effect_config_t),
540 &mConfig,
541 &size,
542 &cmdStatus);
543 if (status == NO_ERROR) {
544 status = cmdStatus;
545 mSupportsFloat = false;
546 ALOGVV("config worked with 16 bit");
547 } else {
548 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800549 }
rago94a1ee82017-07-21 15:11:02 -0700550 }
551#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800552
rago94a1ee82017-07-21 15:11:02 -0700553 if (status == NO_ERROR) {
554 // Establish Buffer strategy
555 setInBuffer(mInBuffer);
556 setOutBuffer(mOutBuffer);
557
558 // Update visualizer latency
559 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
560 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
561 effect_param_t *p = (effect_param_t *)buf32;
562
563 p->psize = sizeof(uint32_t);
564 p->vsize = sizeof(uint32_t);
565 size = sizeof(int);
566 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
567
568 uint32_t latency = 0;
569 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
570 if (pbt != NULL) {
571 latency = pbt->latency_l();
572 }
573
574 *((int32_t *)p->data + 1)= latency;
575 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
576 sizeof(effect_param_t) + 8,
577 &buf32,
578 &size,
579 &cmdStatus);
580 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800581 }
582
583 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
584 (1000 * mConfig.outputCfg.buffer.frameCount);
585
Eric Laurentd0ebb532013-04-02 16:41:41 -0700586exit:
587 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -0700588 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -0800589 return status;
590}
591
592status_t AudioFlinger::EffectModule::init()
593{
594 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700595 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800596 return NO_INIT;
597 }
598 status_t cmdStatus;
599 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700600 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
601 0,
602 NULL,
603 &size,
604 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800605 if (status == 0) {
606 status = cmdStatus;
607 }
608 return status;
609}
610
Eric Laurent1b928682014-10-02 19:41:47 -0700611void AudioFlinger::EffectModule::addEffectToHal_l()
612{
613 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
614 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
615 sp<ThreadBase> thread = mThread.promote();
616 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700617 sp<StreamHalInterface> stream = thread->stream();
618 if (stream != 0) {
619 status_t result = stream->addEffect(mEffectInterface);
620 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700621 }
622 }
623 }
624}
625
Eric Laurentfa1e1232016-08-02 19:01:49 -0700626// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800627status_t AudioFlinger::EffectModule::start()
628{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700629 sp<EffectChain> chain;
630 status_t status;
631 {
632 Mutex::Autolock _l(mLock);
633 status = start_l();
634 if (status == NO_ERROR) {
635 chain = mChain.promote();
636 }
637 }
638 if (chain != 0) {
639 chain->resetVolume_l();
640 }
641 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800642}
643
644status_t AudioFlinger::EffectModule::start_l()
645{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700646 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800647 return NO_INIT;
648 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700649 if (mStatus != NO_ERROR) {
650 return mStatus;
651 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800652 status_t cmdStatus;
653 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700654 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
655 0,
656 NULL,
657 &size,
658 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800659 if (status == 0) {
660 status = cmdStatus;
661 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700662 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700663 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800664 }
665 return status;
666}
667
668status_t AudioFlinger::EffectModule::stop()
669{
670 Mutex::Autolock _l(mLock);
671 return stop_l();
672}
673
674status_t AudioFlinger::EffectModule::stop_l()
675{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700676 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800677 return NO_INIT;
678 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700679 if (mStatus != NO_ERROR) {
680 return mStatus;
681 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800682 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800683 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700684 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
685 0,
686 NULL,
687 &size,
688 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800689 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800690 status = cmdStatus;
691 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800692 if (status == NO_ERROR) {
693 status = remove_effect_from_hal_l();
694 }
695 return status;
696}
697
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800698// must be called with EffectChain::mLock held
699void AudioFlinger::EffectModule::release_l()
700{
701 if (mEffectInterface != 0) {
702 remove_effect_from_hal_l();
703 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800704 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800705 mEffectInterface.clear();
706 }
707}
708
Eric Laurentbfb1b832013-01-07 09:53:42 -0800709status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
710{
711 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
712 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800713 sp<ThreadBase> thread = mThread.promote();
714 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700715 sp<StreamHalInterface> stream = thread->stream();
716 if (stream != 0) {
717 status_t result = stream->removeEffect(mEffectInterface);
718 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800719 }
720 }
721 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800722 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800723}
724
Andy Hunge4a1d912016-08-17 14:11:13 -0700725// round up delta valid if value and divisor are positive.
726template <typename T>
727static T roundUpDelta(const T &value, const T &divisor) {
728 T remainder = value % divisor;
729 return remainder == 0 ? 0 : divisor - remainder;
730}
731
Eric Laurentca7cc822012-11-19 14:55:58 -0800732status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
733 uint32_t cmdSize,
734 void *pCmdData,
735 uint32_t *replySize,
736 void *pReplyData)
737{
738 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700739 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800740
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700741 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800742 return NO_INIT;
743 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700744 if (mStatus != NO_ERROR) {
745 return mStatus;
746 }
Andy Hung110bc952016-06-20 15:22:52 -0700747 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700748 (sizeof(effect_param_t) > cmdSize ||
749 ((effect_param_t *)pCmdData)->psize > cmdSize
750 - sizeof(effect_param_t))) {
751 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800752 android_errorWriteLog(0x534e4554, "33003822");
753 return -EINVAL;
754 }
755 if (cmdCode == EFFECT_CMD_GET_PARAM &&
756 (*replySize < sizeof(effect_param_t) ||
757 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
758 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700759 return -EINVAL;
760 }
ragoe2759072016-11-22 18:02:48 -0800761 if (cmdCode == EFFECT_CMD_GET_PARAM &&
762 (sizeof(effect_param_t) > *replySize
763 || ((effect_param_t *)pCmdData)->psize > *replySize
764 - sizeof(effect_param_t)
765 || ((effect_param_t *)pCmdData)->vsize > *replySize
766 - sizeof(effect_param_t)
767 - ((effect_param_t *)pCmdData)->psize
768 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
769 *replySize
770 - sizeof(effect_param_t)
771 - ((effect_param_t *)pCmdData)->psize
772 - ((effect_param_t *)pCmdData)->vsize)) {
773 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
774 android_errorWriteLog(0x534e4554, "32705438");
775 return -EINVAL;
776 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700777 if ((cmdCode == EFFECT_CMD_SET_PARAM
778 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
779 (sizeof(effect_param_t) > cmdSize
780 || ((effect_param_t *)pCmdData)->psize > cmdSize
781 - sizeof(effect_param_t)
782 || ((effect_param_t *)pCmdData)->vsize > cmdSize
783 - sizeof(effect_param_t)
784 - ((effect_param_t *)pCmdData)->psize
785 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
786 cmdSize
787 - sizeof(effect_param_t)
788 - ((effect_param_t *)pCmdData)->psize
789 - ((effect_param_t *)pCmdData)->vsize)) {
790 android_errorWriteLog(0x534e4554, "30204301");
791 return -EINVAL;
792 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700793 status_t status = mEffectInterface->command(cmdCode,
794 cmdSize,
795 pCmdData,
796 replySize,
797 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800798 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
799 uint32_t size = (replySize == NULL) ? 0 : *replySize;
800 for (size_t i = 1; i < mHandles.size(); i++) {
801 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800802 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800803 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
804 }
805 }
806 }
807 return status;
808}
809
810status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
811{
812 Mutex::Autolock _l(mLock);
813 return setEnabled_l(enabled);
814}
815
816// must be called with EffectModule::mLock held
817status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
818{
819
820 ALOGV("setEnabled %p enabled %d", this, enabled);
821
822 if (enabled != isEnabled()) {
823 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
824 if (enabled && status != NO_ERROR) {
825 return status;
826 }
827
828 switch (mState) {
829 // going from disabled to enabled
830 case IDLE:
831 mState = STARTING;
832 break;
833 case STOPPED:
834 mState = RESTART;
835 break;
836 case STOPPING:
837 mState = ACTIVE;
838 break;
839
840 // going from enabled to disabled
841 case RESTART:
842 mState = STOPPED;
843 break;
844 case STARTING:
845 mState = IDLE;
846 break;
847 case ACTIVE:
848 mState = STOPPING;
849 break;
850 case DESTROYED:
851 return NO_ERROR; // simply ignore as we are being destroyed
852 }
853 for (size_t i = 1; i < mHandles.size(); i++) {
854 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800855 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800856 h->setEnabled(enabled);
857 }
858 }
859 }
860 return NO_ERROR;
861}
862
863bool AudioFlinger::EffectModule::isEnabled() const
864{
865 switch (mState) {
866 case RESTART:
867 case STARTING:
868 case ACTIVE:
869 return true;
870 case IDLE:
871 case STOPPING:
872 case STOPPED:
873 case DESTROYED:
874 default:
875 return false;
876 }
877}
878
879bool AudioFlinger::EffectModule::isProcessEnabled() const
880{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700881 if (mStatus != NO_ERROR) {
882 return false;
883 }
884
Eric Laurentca7cc822012-11-19 14:55:58 -0800885 switch (mState) {
886 case RESTART:
887 case ACTIVE:
888 case STOPPING:
889 case STOPPED:
890 return true;
891 case IDLE:
892 case STARTING:
893 case DESTROYED:
894 default:
895 return false;
896 }
897}
898
Mikhail Naganov022b9952017-01-04 16:36:51 -0800899void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700900 ALOGVV("setInBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800901 if (buffer != 0) {
902 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
903 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
904 } else {
905 mConfig.inputCfg.buffer.raw = NULL;
906 }
907 mInBuffer = buffer;
rago94a1ee82017-07-21 15:11:02 -0700908 if (buffer != nullptr) { // FIXME: EffectHalHidl::setInBuffer should accept null input.
909 mEffectInterface->setInBuffer(buffer);
910 }
911
912#ifdef FLOAT_EFFECT_CHAIN
913 // aux effects do in place conversion to float - we don't allocate mInBuffer16 for them.
914 // Theoretically insert effects can also do in-place conversions (destroying
915 // the original buffer) when the output buffer is identical to the input buffer,
916 // but we don't optimize for it here.
917 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
918 if (!auxType && !mSupportsFloat && mInBuffer.get() != nullptr) {
919 // we need to translate - create hidl shared buffer and intercept
920 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
921 const int inChannels = audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
922 const size_t size = inChannels * inFrameCount * sizeof(int16_t);
923
924 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
925 __func__, inChannels, inFrameCount, size);
926
927 if (size > 0 && (mInBuffer16.get() == nullptr || size > mInBuffer16->getSize())) {
928 mInBuffer16.clear();
929 ALOGV("%s: allocating mInBuffer16 %zu", __func__, size);
930 (void)EffectBufferHalInterface::allocate(size, &mInBuffer16);
931 }
932 if (mInBuffer16.get() != nullptr) {
933 // FIXME: confirm buffer has enough size.
934 mInBuffer16->setFrameCount(inFrameCount);
935 mEffectInterface->setInBuffer(mInBuffer16);
936 } else if (size > 0) {
937 ALOGE("%s cannot create mInBuffer16", __func__);
938 }
939 }
940#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800941}
942
943void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -0700944 ALOGVV("setOutBuffer %p",(&buffer));
Mikhail Naganov022b9952017-01-04 16:36:51 -0800945 if (buffer != 0) {
946 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
947 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
948 } else {
949 mConfig.outputCfg.buffer.raw = NULL;
950 }
951 mOutBuffer = buffer;
rago94a1ee82017-07-21 15:11:02 -0700952 if (buffer != nullptr) {
953 mEffectInterface->setOutBuffer(buffer);
954 }
955
956#ifdef FLOAT_EFFECT_CHAIN
957 // Note: Any effect that does not accumulate does not need mOutBuffer16 and
958 // can do in-place conversion from int16_t to float. We don't optimize here.
959 if (!mSupportsFloat && mOutBuffer.get() != nullptr) {
960 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
961 const int outChannels = audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
962 const size_t size = outChannels * outFrameCount * sizeof(int16_t);
963
964 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
965 __func__, outChannels, outFrameCount, size);
966
967 if (size > 0 && (mOutBuffer16.get() == nullptr || size > mOutBuffer16->getSize())) {
968 mOutBuffer16.clear();
969 ALOGV("%s: allocating mOutBuffer16 %zu", __func__, size);
970 (void)EffectBufferHalInterface::allocate(size, &mOutBuffer16);
971 }
972 if (mOutBuffer16.get() != nullptr) {
973 mOutBuffer16->setFrameCount(outFrameCount);
974 mEffectInterface->setOutBuffer(mOutBuffer16);
975 } else if (size > 0) {
976 ALOGE("%s cannot create mOutBuffer16", __func__);
977 }
978 }
979#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800980}
981
Eric Laurentca7cc822012-11-19 14:55:58 -0800982status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
983{
984 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700985 if (mStatus != NO_ERROR) {
986 return mStatus;
987 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800988 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800989 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
990 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
991 if (isProcessEnabled() &&
992 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
993 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800994 uint32_t volume[2];
995 uint32_t *pVolume = NULL;
996 uint32_t size = sizeof(volume);
997 volume[0] = *left;
998 volume[1] = *right;
999 if (controller) {
1000 pVolume = volume;
1001 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001002 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1003 size,
1004 volume,
1005 &size,
1006 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001007 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1008 *left = volume[0];
1009 *right = volume[1];
1010 }
1011 }
1012 return status;
1013}
1014
1015status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
1016{
1017 if (device == AUDIO_DEVICE_NONE) {
1018 return NO_ERROR;
1019 }
1020
1021 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001022 if (mStatus != NO_ERROR) {
1023 return mStatus;
1024 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001025 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001026 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001027 status_t cmdStatus;
1028 uint32_t size = sizeof(status_t);
1029 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
1030 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001031 status = mEffectInterface->command(cmd,
1032 sizeof(uint32_t),
1033 &device,
1034 &size,
1035 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 }
1037 return status;
1038}
1039
1040status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1041{
1042 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001043 if (mStatus != NO_ERROR) {
1044 return mStatus;
1045 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001046 status_t status = NO_ERROR;
1047 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1048 status_t cmdStatus;
1049 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001050 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1051 sizeof(audio_mode_t),
1052 &mode,
1053 &size,
1054 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001055 if (status == NO_ERROR) {
1056 status = cmdStatus;
1057 }
1058 }
1059 return status;
1060}
1061
1062status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1063{
1064 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001065 if (mStatus != NO_ERROR) {
1066 return mStatus;
1067 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001068 status_t status = NO_ERROR;
1069 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1070 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001071 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1072 sizeof(audio_source_t),
1073 &source,
1074 &size,
1075 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 }
1077 return status;
1078}
1079
1080void AudioFlinger::EffectModule::setSuspended(bool suspended)
1081{
1082 Mutex::Autolock _l(mLock);
1083 mSuspended = suspended;
1084}
1085
1086bool AudioFlinger::EffectModule::suspended() const
1087{
1088 Mutex::Autolock _l(mLock);
1089 return mSuspended;
1090}
1091
1092bool AudioFlinger::EffectModule::purgeHandles()
1093{
1094 bool enabled = false;
1095 Mutex::Autolock _l(mLock);
1096 for (size_t i = 0; i < mHandles.size(); i++) {
1097 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001098 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001099 if (handle->hasControl()) {
1100 enabled = handle->enabled();
1101 }
1102 }
1103 }
1104 return enabled;
1105}
1106
Eric Laurent5baf2af2013-09-12 17:37:00 -07001107status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1108{
1109 Mutex::Autolock _l(mLock);
1110 if (mStatus != NO_ERROR) {
1111 return mStatus;
1112 }
1113 status_t status = NO_ERROR;
1114 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1115 status_t cmdStatus;
1116 uint32_t size = sizeof(status_t);
1117 effect_offload_param_t cmd;
1118
1119 cmd.isOffload = offloaded;
1120 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001121 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1122 sizeof(effect_offload_param_t),
1123 &cmd,
1124 &size,
1125 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001126 if (status == NO_ERROR) {
1127 status = cmdStatus;
1128 }
1129 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1130 } else {
1131 if (offloaded) {
1132 status = INVALID_OPERATION;
1133 }
1134 mOffloaded = false;
1135 }
1136 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1137 return status;
1138}
1139
1140bool AudioFlinger::EffectModule::isOffloaded() const
1141{
1142 Mutex::Autolock _l(mLock);
1143 return mOffloaded;
1144}
1145
Marco Nelissenb2208842014-02-07 14:00:50 -08001146String8 effectFlagsToString(uint32_t flags) {
1147 String8 s;
1148
1149 s.append("conn. mode: ");
1150 switch (flags & EFFECT_FLAG_TYPE_MASK) {
1151 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
1152 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
1153 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
1154 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
1155 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
1156 default: s.append("unknown/reserved"); break;
1157 }
1158 s.append(", ");
1159
1160 s.append("insert pref: ");
1161 switch (flags & EFFECT_FLAG_INSERT_MASK) {
1162 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
1163 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
1164 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
1165 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
1166 default: s.append("unknown/reserved"); break;
1167 }
1168 s.append(", ");
1169
1170 s.append("volume mgmt: ");
1171 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
1172 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
1173 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
1174 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
1175 default: s.append("unknown/reserved"); break;
1176 }
1177 s.append(", ");
1178
1179 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
1180 if (devind) {
1181 s.append("device indication: ");
1182 switch (devind) {
1183 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1184 default: s.append("unknown/reserved"); break;
1185 }
1186 s.append(", ");
1187 }
1188
1189 s.append("input mode: ");
1190 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1191 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1192 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1193 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1194 default: s.append("not set"); break;
1195 }
1196 s.append(", ");
1197
1198 s.append("output mode: ");
1199 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1200 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1201 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1202 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1203 default: s.append("not set"); break;
1204 }
1205 s.append(", ");
1206
1207 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1208 if (accel) {
1209 s.append("hardware acceleration: ");
1210 switch (accel) {
1211 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1212 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1213 default: s.append("unknown/reserved"); break;
1214 }
1215 s.append(", ");
1216 }
1217
1218 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1219 if (modeind) {
1220 s.append("mode indication: ");
1221 switch (modeind) {
1222 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1223 default: s.append("unknown/reserved"); break;
1224 }
1225 s.append(", ");
1226 }
1227
1228 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1229 if (srcind) {
1230 s.append("source indication: ");
1231 switch (srcind) {
1232 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1233 default: s.append("unknown/reserved"); break;
1234 }
1235 s.append(", ");
1236 }
1237
1238 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1239 s.append("offloadable, ");
1240 }
1241
1242 int len = s.length();
1243 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001244 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001245 s.unlockBuffer(len - 2);
1246 }
1247 return s;
1248}
1249
1250
Glenn Kasten0f11b512014-01-31 16:18:54 -08001251void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001252{
1253 const size_t SIZE = 256;
1254 char buffer[SIZE];
1255 String8 result;
1256
1257 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1258 result.append(buffer);
1259
1260 bool locked = AudioFlinger::dumpTryLock(mLock);
1261 // failed to lock - AudioFlinger is probably deadlocked
1262 if (!locked) {
1263 result.append("\t\tCould not lock Fx mutex:\n");
1264 }
1265
1266 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001267 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001268 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001269 result.append(buffer);
1270
1271 result.append("\t\tDescriptor:\n");
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001272 char uuidStr[64];
1273 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1274 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001275 result.append(buffer);
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001276 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1277 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001278 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001279 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001280 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001281 mDescriptor.flags,
1282 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001283 result.append(buffer);
1284 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1285 mDescriptor.name);
1286 result.append(buffer);
1287 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1288 mDescriptor.implementor);
1289 result.append(buffer);
1290
1291 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001292 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001293 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001294 mConfig.inputCfg.buffer.frameCount,
1295 mConfig.inputCfg.samplingRate,
1296 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001297 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001298 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001299 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001300 result.append(buffer);
1301
1302 result.append("\t\t- Output configuration:\n");
1303 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001304 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001305 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001306 mConfig.outputCfg.buffer.frameCount,
1307 mConfig.outputCfg.samplingRate,
1308 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001309 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001310 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001311 result.append(buffer);
1312
rago94a1ee82017-07-21 15:11:02 -07001313#ifdef FLOAT_EFFECT_CHAIN
1314 if (!mSupportsFloat) {
1315 int16_t* pIn16 = mInBuffer16 != 0 ? mInBuffer16->audioBuffer()->s16 : NULL;
1316 int16_t* pOut16 = mOutBuffer16 != 0 ? mOutBuffer16->audioBuffer()->s16 : NULL;
1317
1318 result.append("\t\t- Float and int16 buffers\n");
1319 result.append("\t\t\tIn_float In_int16 Out_float Out_int16\n");
1320 snprintf(buffer, SIZE,"\t\t\t%p %p %p %p\n",
1321 mConfig.inputCfg.buffer.raw,
1322 pIn16,
1323 pOut16,
1324 mConfig.outputCfg.buffer.raw);
1325 result.append(buffer);
1326 }
1327#endif
1328
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001329 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001330 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001331 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001332 for (size_t i = 0; i < mHandles.size(); ++i) {
1333 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001334 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001335 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001336 result.append(buffer);
1337 }
1338 }
1339
Eric Laurentca7cc822012-11-19 14:55:58 -08001340 write(fd, result.string(), result.length());
1341
1342 if (locked) {
1343 mLock.unlock();
1344 }
1345}
1346
1347// ----------------------------------------------------------------------------
1348// EffectHandle implementation
1349// ----------------------------------------------------------------------------
1350
1351#undef LOG_TAG
1352#define LOG_TAG "AudioFlinger::EffectHandle"
1353
1354AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1355 const sp<AudioFlinger::Client>& client,
1356 const sp<IEffectClient>& effectClient,
1357 int32_t priority)
1358 : BnEffect(),
1359 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001360 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001361{
1362 ALOGV("constructor %p", this);
1363
1364 if (client == 0) {
1365 return;
1366 }
1367 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1368 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001369 if (mCblkMemory == 0 ||
1370 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001371 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001372 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001373 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001374 return;
1375 }
Glenn Kastene75da402013-11-20 13:54:52 -08001376 new(mCblk) effect_param_cblk_t();
1377 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001378}
1379
1380AudioFlinger::EffectHandle::~EffectHandle()
1381{
1382 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001383 disconnect(false);
1384}
1385
Glenn Kastene75da402013-11-20 13:54:52 -08001386status_t AudioFlinger::EffectHandle::initCheck()
1387{
1388 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1389}
1390
Eric Laurentca7cc822012-11-19 14:55:58 -08001391status_t AudioFlinger::EffectHandle::enable()
1392{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001393 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001394 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001395 sp<EffectModule> effect = mEffect.promote();
1396 if (effect == 0 || mDisconnected) {
1397 return DEAD_OBJECT;
1398 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001399 if (!mHasControl) {
1400 return INVALID_OPERATION;
1401 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001402
1403 if (mEnabled) {
1404 return NO_ERROR;
1405 }
1406
1407 mEnabled = true;
1408
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001409 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001411 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001412 }
1413
1414 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001415 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001416 return NO_ERROR;
1417 }
1418
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001419 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001420 if (status != NO_ERROR) {
1421 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001422 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001423 }
1424 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001425 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001426 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001427 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1428 Mutex::Autolock _l(thread->mLock);
1429 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001430 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001431 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001432 if (thread->type() == ThreadBase::OFFLOAD) {
1433 PlaybackThread *t = (PlaybackThread *)thread.get();
1434 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1435 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001436 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001437 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1438 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001439 }
1440 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001441 }
1442 return status;
1443}
1444
1445status_t AudioFlinger::EffectHandle::disable()
1446{
1447 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001448 AutoMutex _l(mLock);
1449 sp<EffectModule> effect = mEffect.promote();
1450 if (effect == 0 || mDisconnected) {
1451 return DEAD_OBJECT;
1452 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001453 if (!mHasControl) {
1454 return INVALID_OPERATION;
1455 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001456
1457 if (!mEnabled) {
1458 return NO_ERROR;
1459 }
1460 mEnabled = false;
1461
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001462 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001463 return NO_ERROR;
1464 }
1465
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001466 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001467
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001468 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001469 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001470 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001471 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1472 Mutex::Autolock _l(thread->mLock);
1473 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001474 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001475 }
1476
1477 return status;
1478}
1479
1480void AudioFlinger::EffectHandle::disconnect()
1481{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001482 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001483 disconnect(true);
1484}
1485
1486void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1487{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001488 AutoMutex _l(mLock);
1489 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1490 if (mDisconnected) {
1491 if (unpinIfLast) {
1492 android_errorWriteLog(0x534e4554, "32707507");
1493 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001494 return;
1495 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001496 mDisconnected = true;
1497 sp<ThreadBase> thread;
1498 {
1499 sp<EffectModule> effect = mEffect.promote();
1500 if (effect != 0) {
1501 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001502 }
1503 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001504 if (thread != 0) {
1505 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001506 } else {
Eric Laurentf10c7092016-12-06 17:09:56 -08001507 // try to cleanup as much as we can
1508 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov424c4f52017-07-19 17:54:29 -07001509 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1510 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001511 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001512 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001513
Eric Laurentca7cc822012-11-19 14:55:58 -08001514 if (mClient != 0) {
1515 if (mCblk != NULL) {
1516 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1517 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1518 }
1519 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001520 // Client destructor must run with AudioFlinger client mutex locked
1521 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001522 mClient.clear();
1523 }
1524}
1525
1526status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1527 uint32_t cmdSize,
1528 void *pCmdData,
1529 uint32_t *replySize,
1530 void *pReplyData)
1531{
1532 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001533 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001534
Eric Laurentc7ab3092017-06-15 18:43:46 -07001535 // reject commands reserved for internal use by audio framework if coming from outside
1536 // of audioserver
1537 switch(cmdCode) {
1538 case EFFECT_CMD_ENABLE:
1539 case EFFECT_CMD_DISABLE:
1540 case EFFECT_CMD_SET_PARAM:
1541 case EFFECT_CMD_SET_PARAM_DEFERRED:
1542 case EFFECT_CMD_SET_PARAM_COMMIT:
1543 case EFFECT_CMD_GET_PARAM:
1544 break;
1545 default:
1546 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1547 break;
1548 }
1549 android_errorWriteLog(0x534e4554, "62019992");
1550 return BAD_VALUE;
1551 }
1552
Eric Laurent1ffc5852016-12-15 14:46:09 -08001553 if (cmdCode == EFFECT_CMD_ENABLE) {
1554 if (*replySize < sizeof(int)) {
1555 android_errorWriteLog(0x534e4554, "32095713");
1556 return BAD_VALUE;
1557 }
1558 *(int *)pReplyData = NO_ERROR;
1559 *replySize = sizeof(int);
1560 return enable();
1561 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1562 if (*replySize < sizeof(int)) {
1563 android_errorWriteLog(0x534e4554, "32095713");
1564 return BAD_VALUE;
1565 }
1566 *(int *)pReplyData = NO_ERROR;
1567 *replySize = sizeof(int);
1568 return disable();
1569 }
1570
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001571 AutoMutex _l(mLock);
1572 sp<EffectModule> effect = mEffect.promote();
1573 if (effect == 0 || mDisconnected) {
1574 return DEAD_OBJECT;
1575 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001576 // only get parameter command is permitted for applications not controlling the effect
1577 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1578 return INVALID_OPERATION;
1579 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001580 if (mClient == 0) {
1581 return INVALID_OPERATION;
1582 }
1583
1584 // handle commands that are not forwarded transparently to effect engine
1585 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001586 if (*replySize < sizeof(int)) {
1587 android_errorWriteLog(0x534e4554, "32095713");
1588 return BAD_VALUE;
1589 }
1590 *(int *)pReplyData = NO_ERROR;
1591 *replySize = sizeof(int);
1592
Eric Laurentca7cc822012-11-19 14:55:58 -08001593 // No need to trylock() here as this function is executed in the binder thread serving a
1594 // particular client process: no risk to block the whole media server process or mixer
1595 // threads if we are stuck here
1596 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001597 // keep local copy of index in case of client corruption b/32220769
1598 const uint32_t clientIndex = mCblk->clientIndex;
1599 const uint32_t serverIndex = mCblk->serverIndex;
1600 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1601 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001602 mCblk->serverIndex = 0;
1603 mCblk->clientIndex = 0;
1604 return BAD_VALUE;
1605 }
1606 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001607 effect_param_t *param = NULL;
1608 for (uint32_t index = serverIndex; index < clientIndex;) {
1609 int *p = (int *)(mBuffer + index);
1610 const int size = *p++;
1611 if (size < 0
1612 || size > EFFECT_PARAM_BUFFER_SIZE
1613 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001614 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001615 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001616 break;
1617 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001618
1619 // copy to local memory in case of client corruption b/32220769
1620 param = (effect_param_t *)realloc(param, size);
1621 if (param == NULL) {
1622 ALOGW("command(): out of memory");
1623 status = NO_MEMORY;
1624 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001625 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001626 memcpy(param, p, size);
1627
1628 int reply = 0;
1629 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001630 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001631 size,
1632 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001633 &rsize,
1634 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001635
1636 // verify shared memory: server index shouldn't change; client index can't go back.
1637 if (serverIndex != mCblk->serverIndex
1638 || clientIndex > mCblk->clientIndex) {
1639 android_errorWriteLog(0x534e4554, "32220769");
1640 status = BAD_VALUE;
1641 break;
1642 }
1643
Eric Laurentca7cc822012-11-19 14:55:58 -08001644 // stop at first error encountered
1645 if (ret != NO_ERROR) {
1646 status = ret;
1647 *(int *)pReplyData = reply;
1648 break;
1649 } else if (reply != NO_ERROR) {
1650 *(int *)pReplyData = reply;
1651 break;
1652 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001653 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001654 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001655 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001656 mCblk->serverIndex = 0;
1657 mCblk->clientIndex = 0;
1658 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001659 }
1660
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001661 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001662}
1663
1664void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1665{
1666 ALOGV("setControl %p control %d", this, hasControl);
1667
1668 mHasControl = hasControl;
1669 mEnabled = enabled;
1670
1671 if (signal && mEffectClient != 0) {
1672 mEffectClient->controlStatusChanged(hasControl);
1673 }
1674}
1675
1676void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1677 uint32_t cmdSize,
1678 void *pCmdData,
1679 uint32_t replySize,
1680 void *pReplyData)
1681{
1682 if (mEffectClient != 0) {
1683 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1684 }
1685}
1686
1687
1688
1689void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1690{
1691 if (mEffectClient != 0) {
1692 mEffectClient->enableStatusChanged(enabled);
1693 }
1694}
1695
1696status_t AudioFlinger::EffectHandle::onTransact(
1697 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1698{
1699 return BnEffect::onTransact(code, data, reply, flags);
1700}
1701
1702
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001703void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001704{
1705 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1706
Marco Nelissenb2208842014-02-07 14:00:50 -08001707 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001708 (mClient == 0) ? getpid_cached : mClient->pid(),
1709 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001710 mHasControl ? "yes" : "no",
1711 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001712 mCblk ? mCblk->clientIndex : 0,
1713 mCblk ? mCblk->serverIndex : 0
1714 );
1715
1716 if (locked) {
1717 mCblk->lock.unlock();
1718 }
1719}
1720
1721#undef LOG_TAG
1722#define LOG_TAG "AudioFlinger::EffectChain"
1723
1724AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001725 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001726 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001727 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001728 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001729{
1730 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1731 if (thread == NULL) {
1732 return;
1733 }
1734 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1735 thread->frameCount();
1736}
1737
1738AudioFlinger::EffectChain::~EffectChain()
1739{
Eric Laurentca7cc822012-11-19 14:55:58 -08001740}
1741
1742// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1743sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1744 effect_descriptor_t *descriptor)
1745{
1746 size_t size = mEffects.size();
1747
1748 for (size_t i = 0; i < size; i++) {
1749 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1750 return mEffects[i];
1751 }
1752 }
1753 return 0;
1754}
1755
1756// getEffectFromId_l() must be called with ThreadBase::mLock held
1757sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1758{
1759 size_t size = mEffects.size();
1760
1761 for (size_t i = 0; i < size; i++) {
1762 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1763 if (id == 0 || mEffects[i]->id() == id) {
1764 return mEffects[i];
1765 }
1766 }
1767 return 0;
1768}
1769
1770// getEffectFromType_l() must be called with ThreadBase::mLock held
1771sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1772 const effect_uuid_t *type)
1773{
1774 size_t size = mEffects.size();
1775
1776 for (size_t i = 0; i < size; i++) {
1777 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1778 return mEffects[i];
1779 }
1780 }
1781 return 0;
1782}
1783
1784void AudioFlinger::EffectChain::clearInputBuffer()
1785{
1786 Mutex::Autolock _l(mLock);
1787 sp<ThreadBase> thread = mThread.promote();
1788 if (thread == 0) {
1789 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1790 return;
1791 }
1792 clearInputBuffer_l(thread);
1793}
1794
1795// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001796void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001797{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001798 if (mInBuffer == NULL) {
1799 return;
1800 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001801 // TODO: This will change in the future, depending on multichannel
1802 // and sample format changes for effects.
1803 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1804 // (4 bytes frame size)
rago94a1ee82017-07-21 15:11:02 -07001805
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001806 const size_t frameSize =
rago94a1ee82017-07-21 15:11:02 -07001807 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
1808 * std::min((uint32_t)FCC_2, thread->channelCount());
1809
Mikhail Naganov022b9952017-01-04 16:36:51 -08001810 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1811 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001812}
1813
1814// Must be called with EffectChain::mLock locked
1815void AudioFlinger::EffectChain::process_l()
1816{
1817 sp<ThreadBase> thread = mThread.promote();
1818 if (thread == 0) {
1819 ALOGW("process_l(): cannot promote mixer thread");
1820 return;
1821 }
1822 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1823 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001824 // never process effects when:
1825 // - on an OFFLOAD thread
1826 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001827 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1828 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001829 if (!isGlobalSession) {
1830 bool tracksOnSession = (trackCnt() != 0);
1831
1832 if (!tracksOnSession && mTailBufferCount == 0) {
1833 doProcess = false;
1834 }
1835
1836 if (activeTrackCnt() == 0) {
1837 // if no track is active and the effect tail has not been rendered,
1838 // the input buffer must be cleared here as the mixer process will not do it
1839 if (tracksOnSession || mTailBufferCount > 0) {
1840 clearInputBuffer_l(thread);
1841 if (mTailBufferCount > 0) {
1842 mTailBufferCount--;
1843 }
1844 }
1845 }
1846 }
1847
1848 size_t size = mEffects.size();
1849 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001850 // Only the input and output buffers of the chain can be external,
1851 // and 'update' / 'commit' do nothing for allocated buffers, thus
1852 // it's not needed to consider any other buffers here.
1853 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001854 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1855 mOutBuffer->update();
1856 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001857 for (size_t i = 0; i < size; i++) {
1858 mEffects[i]->process();
1859 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001860 mInBuffer->commit();
1861 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1862 mOutBuffer->commit();
1863 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001865 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001866 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001867 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1868 }
1869 if (doResetVolume) {
1870 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001871 }
1872}
1873
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001874// createEffect_l() must be called with ThreadBase::mLock held
1875status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1876 ThreadBase *thread,
1877 effect_descriptor_t *desc,
1878 int id,
1879 audio_session_t sessionId,
1880 bool pinned)
1881{
1882 Mutex::Autolock _l(mLock);
1883 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1884 status_t lStatus = effect->status();
1885 if (lStatus == NO_ERROR) {
1886 lStatus = addEffect_ll(effect);
1887 }
1888 if (lStatus != NO_ERROR) {
1889 effect.clear();
1890 }
1891 return lStatus;
1892}
1893
1894// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001895status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1896{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001897 Mutex::Autolock _l(mLock);
1898 return addEffect_ll(effect);
1899}
1900// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1901status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1902{
Eric Laurentca7cc822012-11-19 14:55:58 -08001903 effect_descriptor_t desc = effect->desc();
1904 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1905
Eric Laurentca7cc822012-11-19 14:55:58 -08001906 effect->setChain(this);
1907 sp<ThreadBase> thread = mThread.promote();
1908 if (thread == 0) {
1909 return NO_INIT;
1910 }
1911 effect->setThread(thread);
1912
1913 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1914 // Auxiliary effects are inserted at the beginning of mEffects vector as
1915 // they are processed first and accumulated in chain input buffer
1916 mEffects.insertAt(effect, 0);
1917
1918 // the input buffer for auxiliary effect contains mono samples in
1919 // 32 bit format. This is to avoid saturation in AudoMixer
1920 // accumulation stage. Saturation is done in EffectModule::process() before
1921 // calling the process in effect engine
1922 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001923 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07001924#ifdef FLOAT_EFFECT_CHAIN
1925 status_t result = EffectBufferHalInterface::allocate(
1926 numSamples * sizeof(float), &halBuffer);
1927#else
Mikhail Naganov022b9952017-01-04 16:36:51 -08001928 status_t result = EffectBufferHalInterface::allocate(
1929 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07001930#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001931 if (result != OK) return result;
1932 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001933 // auxiliary effects output samples to chain input buffer for further processing
1934 // by insert effects
1935 effect->setOutBuffer(mInBuffer);
1936 } else {
1937 // Insert effects are inserted at the end of mEffects vector as they are processed
1938 // after track and auxiliary effects.
1939 // Insert effect order as a function of indicated preference:
1940 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1941 // another effect is present
1942 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1943 // last effect claiming first position
1944 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1945 // first effect claiming last position
1946 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1947 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1948 // already present
1949
1950 size_t size = mEffects.size();
1951 size_t idx_insert = size;
1952 ssize_t idx_insert_first = -1;
1953 ssize_t idx_insert_last = -1;
1954
1955 for (size_t i = 0; i < size; i++) {
1956 effect_descriptor_t d = mEffects[i]->desc();
1957 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1958 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1959 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1960 // check invalid effect chaining combinations
1961 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1962 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1963 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1964 desc.name, d.name);
1965 return INVALID_OPERATION;
1966 }
1967 // remember position of first insert effect and by default
1968 // select this as insert position for new effect
1969 if (idx_insert == size) {
1970 idx_insert = i;
1971 }
1972 // remember position of last insert effect claiming
1973 // first position
1974 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1975 idx_insert_first = i;
1976 }
1977 // remember position of first insert effect claiming
1978 // last position
1979 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1980 idx_insert_last == -1) {
1981 idx_insert_last = i;
1982 }
1983 }
1984 }
1985
1986 // modify idx_insert from first position if needed
1987 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1988 if (idx_insert_last != -1) {
1989 idx_insert = idx_insert_last;
1990 } else {
1991 idx_insert = size;
1992 }
1993 } else {
1994 if (idx_insert_first != -1) {
1995 idx_insert = idx_insert_first + 1;
1996 }
1997 }
1998
1999 // always read samples from chain input buffer
2000 effect->setInBuffer(mInBuffer);
2001
2002 // if last effect in the chain, output samples to chain
2003 // output buffer, otherwise to chain input buffer
2004 if (idx_insert == size) {
2005 if (idx_insert != 0) {
2006 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2007 mEffects[idx_insert-1]->configure();
2008 }
2009 effect->setOutBuffer(mOutBuffer);
2010 } else {
2011 effect->setOutBuffer(mInBuffer);
2012 }
2013 mEffects.insertAt(effect, idx_insert);
2014
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002015 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002016 idx_insert);
2017 }
2018 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002019
Eric Laurentca7cc822012-11-19 14:55:58 -08002020 return NO_ERROR;
2021}
2022
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002023// removeEffect_l() must be called with ThreadBase::mLock held
2024size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2025 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002026{
2027 Mutex::Autolock _l(mLock);
2028 size_t size = mEffects.size();
2029 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2030
2031 for (size_t i = 0; i < size; i++) {
2032 if (effect == mEffects[i]) {
2033 // calling stop here will remove pre-processing effect from the audio HAL.
2034 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2035 // the middle of a read from audio HAL
2036 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2037 mEffects[i]->state() == EffectModule::STOPPING) {
2038 mEffects[i]->stop();
2039 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002040 if (release) {
2041 mEffects[i]->release_l();
2042 }
2043
Mikhail Naganov022b9952017-01-04 16:36:51 -08002044 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002045 if (i == size - 1 && i != 0) {
2046 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2047 mEffects[i - 1]->configure();
2048 }
2049 }
2050 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002051 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002052 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002053
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 break;
2055 }
2056 }
2057
2058 return mEffects.size();
2059}
2060
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002061// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002062void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
2063{
2064 size_t size = mEffects.size();
2065 for (size_t i = 0; i < size; i++) {
2066 mEffects[i]->setDevice(device);
2067 }
2068}
2069
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002070// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002071void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2072{
2073 size_t size = mEffects.size();
2074 for (size_t i = 0; i < size; i++) {
2075 mEffects[i]->setMode(mode);
2076 }
2077}
2078
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002079// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002080void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2081{
2082 size_t size = mEffects.size();
2083 for (size_t i = 0; i < size; i++) {
2084 mEffects[i]->setAudioSource(source);
2085 }
2086}
2087
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002088// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002089bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002090{
2091 uint32_t newLeft = *left;
2092 uint32_t newRight = *right;
2093 bool hasControl = false;
2094 int ctrlIdx = -1;
2095 size_t size = mEffects.size();
2096
2097 // first update volume controller
2098 for (size_t i = size; i > 0; i--) {
2099 if (mEffects[i - 1]->isProcessEnabled() &&
2100 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
2101 ctrlIdx = i - 1;
2102 hasControl = true;
2103 break;
2104 }
2105 }
2106
Eric Laurentfa1e1232016-08-02 19:01:49 -07002107 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002108 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002109 if (hasControl) {
2110 *left = mNewLeftVolume;
2111 *right = mNewRightVolume;
2112 }
2113 return hasControl;
2114 }
2115
2116 mVolumeCtrlIdx = ctrlIdx;
2117 mLeftVolume = newLeft;
2118 mRightVolume = newRight;
2119
2120 // second get volume update from volume controller
2121 if (ctrlIdx >= 0) {
2122 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2123 mNewLeftVolume = newLeft;
2124 mNewRightVolume = newRight;
2125 }
2126 // then indicate volume to all other effects in chain.
2127 // Pass altered volume to effects before volume controller
2128 // and requested volume to effects after controller
2129 uint32_t lVol = newLeft;
2130 uint32_t rVol = newRight;
2131
2132 for (size_t i = 0; i < size; i++) {
2133 if ((int)i == ctrlIdx) {
2134 continue;
2135 }
2136 // this also works for ctrlIdx == -1 when there is no volume controller
2137 if ((int)i > ctrlIdx) {
2138 lVol = *left;
2139 rVol = *right;
2140 }
2141 mEffects[i]->setVolume(&lVol, &rVol, false);
2142 }
2143 *left = newLeft;
2144 *right = newRight;
2145
2146 return hasControl;
2147}
2148
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002149// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002150void AudioFlinger::EffectChain::resetVolume_l()
2151{
Eric Laurente7449bf2016-08-03 18:44:07 -07002152 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2153 uint32_t left = mLeftVolume;
2154 uint32_t right = mRightVolume;
2155 (void)setVolume_l(&left, &right, true);
2156 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002157}
2158
Eric Laurent1b928682014-10-02 19:41:47 -07002159void AudioFlinger::EffectChain::syncHalEffectsState()
2160{
2161 Mutex::Autolock _l(mLock);
2162 for (size_t i = 0; i < mEffects.size(); i++) {
2163 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2164 mEffects[i]->state() == EffectModule::STOPPING) {
2165 mEffects[i]->addEffectToHal_l();
2166 }
2167 }
2168}
2169
Mikhail Naganov06888802017-01-19 12:47:55 -08002170static void dumpInOutBuffer(
2171 char *dump, size_t dumpSize, bool isInput, EffectBufferHalInterface *buffer) {
Mikhail Naganovc778e592017-01-25 10:35:30 -08002172 if (buffer == nullptr) {
2173 snprintf(dump, dumpSize, "%p", buffer);
2174 } else if (buffer->externalData() != nullptr) {
Mikhail Naganov06888802017-01-19 12:47:55 -08002175 snprintf(dump, dumpSize, "%p -> %p",
2176 isInput ? buffer->externalData() : buffer->audioBuffer()->raw,
2177 isInput ? buffer->audioBuffer()->raw : buffer->externalData());
2178 } else {
2179 snprintf(dump, dumpSize, "%p", buffer->audioBuffer()->raw);
2180 }
2181}
2182
Eric Laurentca7cc822012-11-19 14:55:58 -08002183void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2184{
2185 const size_t SIZE = 256;
2186 char buffer[SIZE];
2187 String8 result;
2188
Marco Nelissenb2208842014-02-07 14:00:50 -08002189 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002190 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002191 result.append(buffer);
2192
Marco Nelissenb2208842014-02-07 14:00:50 -08002193 if (numEffects) {
2194 bool locked = AudioFlinger::dumpTryLock(mLock);
2195 // failed to lock - AudioFlinger is probably deadlocked
2196 if (!locked) {
2197 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002198 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002199
Mikhail Naganov06888802017-01-19 12:47:55 -08002200 char inBufferStr[64], outBufferStr[64];
2201 dumpInOutBuffer(inBufferStr, sizeof(inBufferStr), true, mInBuffer.get());
2202 dumpInOutBuffer(outBufferStr, sizeof(outBufferStr), false, mOutBuffer.get());
2203 snprintf(buffer, SIZE, "\t%-*s%-*s Active tracks:\n",
2204 (int)strlen(inBufferStr), "In buffer ",
2205 (int)strlen(outBufferStr), "Out buffer ");
2206 result.append(buffer);
2207 snprintf(buffer, SIZE, "\t%s %s %d\n", inBufferStr, outBufferStr, mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002208 result.append(buffer);
2209 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