blob: ad4e97b6ef981d59395b1306a960a32f09806fff [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
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080023#include <utils/Log.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080024#include <audio_utils/primitives.h>
25#include <private/media/AudioEffectShared.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070026#include <media/audiohal/EffectHalInterface.h>
27#include <media/audiohal/EffectsFactoryHalInterface.h>
Mikhail Naganov9fe94012016-10-14 14:57:40 -070028#include <system/audio_effects/effect_visualizer.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080029
30#include "AudioFlinger.h"
31#include "ServiceUtilities.h"
32
33// ----------------------------------------------------------------------------
34
35// Note: the following macro is used for extremely verbose logging message. In
36// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
37// 0; but one side effect of this is to turn all LOGV's as well. Some messages
38// are so verbose that we want to suppress them even when we have ALOG_ASSERT
39// turned on. Do not uncomment the #def below unless you really know what you
40// are doing and want to see all of the extremely verbose messages.
41//#define VERY_VERY_VERBOSE_LOGGING
42#ifdef VERY_VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
47
Ricardo Garcia726b6a72014-08-11 12:04:54 -070048#define min(a, b) ((a) < (b) ? (a) : (b))
49
Eric Laurentca7cc822012-11-19 14:55:58 -080050namespace android {
51
52// ----------------------------------------------------------------------------
53// EffectModule implementation
54// ----------------------------------------------------------------------------
55
56#undef LOG_TAG
57#define LOG_TAG "AudioFlinger::EffectModule"
58
59AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
60 const wp<AudioFlinger::EffectChain>& chain,
61 effect_descriptor_t *desc,
62 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080063 audio_session_t sessionId,
64 bool pinned)
65 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080066 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
67 mDescriptor(*desc),
68 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080069 mStatus(NO_INIT), mState(IDLE),
70 // mMaxDisableWaitCnt is set by configure() and not used before then
71 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070072 mSuspended(false),
73 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080074{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080075 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080076 int lStatus;
77
78 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070079 mStatus = -ENODEV;
80 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070081 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070082 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070083 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070084 mStatus = effectsFactory->createEffect(
85 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
86 }
87 }
Eric Laurentca7cc822012-11-19 14:55:58 -080088
89 if (mStatus != NO_ERROR) {
90 return;
91 }
92 lStatus = init();
93 if (lStatus < 0) {
94 mStatus = lStatus;
95 goto Error;
96 }
97
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080098 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070099 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800100
Eric Laurentca7cc822012-11-19 14:55:58 -0800101 return;
102Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700103 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800104 ALOGV("Constructor Error %d", mStatus);
105}
106
107AudioFlinger::EffectModule::~EffectModule()
108{
109 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700110 if (mEffectInterface != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800111 ALOGW("EffectModule %p destructor called with unreleased interface", this);
112 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800113 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800114
Eric Laurentca7cc822012-11-19 14:55:58 -0800115}
116
117status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
118{
119 status_t status;
120
121 Mutex::Autolock _l(mLock);
122 int priority = handle->priority();
123 size_t size = mHandles.size();
124 EffectHandle *controlHandle = NULL;
125 size_t i;
126 for (i = 0; i < size; i++) {
127 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800128 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800129 continue;
130 }
131 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700132 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800133 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700134 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800135 if (h->priority() <= priority) {
136 break;
137 }
138 }
139 // if inserted in first place, move effect control from previous owner to this handle
140 if (i == 0) {
141 bool enabled = false;
142 if (controlHandle != NULL) {
143 enabled = controlHandle->enabled();
144 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
145 }
146 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
147 status = NO_ERROR;
148 } else {
149 status = ALREADY_EXISTS;
150 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700151 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800152 mHandles.insertAt(handle, i);
153 return status;
154}
155
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800156ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800157{
158 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800159 return removeHandle_l(handle);
160}
161
162ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
163{
Eric Laurentca7cc822012-11-19 14:55:58 -0800164 size_t size = mHandles.size();
165 size_t i;
166 for (i = 0; i < size; i++) {
167 if (mHandles[i] == handle) {
168 break;
169 }
170 }
171 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800172 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
173 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800174 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800175 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800176
177 mHandles.removeAt(i);
178 // if removed from first place, move effect control from this handle to next in line
179 if (i == 0) {
180 EffectHandle *h = controlHandle_l();
181 if (h != NULL) {
182 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
183 }
184 }
185
186 // Prevent calls to process() and other functions on effect interface from now on.
187 // The effect engine will be released by the destructor when the last strong reference on
188 // this object is released which can happen after next process is called.
189 if (mHandles.size() == 0 && !mPinned) {
190 mState = DESTROYED;
191 }
192
193 return mHandles.size();
194}
195
196// must be called with EffectModule::mLock held
197AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
198{
199 // the first valid handle in the list has control over the module
200 for (size_t i = 0; i < mHandles.size(); i++) {
201 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800202 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800203 return h;
204 }
205 }
206
207 return NULL;
208}
209
Eric Laurentfa1e1232016-08-02 19:01:49 -0700210bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800211 Mutex::Autolock _l(mLock);
212
Eric Laurentfa1e1232016-08-02 19:01:49 -0700213 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800214 switch (mState) {
215 case RESTART:
216 reset_l();
217 // FALL THROUGH
218
219 case STARTING:
220 // clear auxiliary effect input buffer for next accumulation
221 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
222 memset(mConfig.inputCfg.buffer.raw,
223 0,
224 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
225 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700226 if (start_l() == NO_ERROR) {
227 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700228 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700229 } else {
230 mState = IDLE;
231 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800232 break;
233 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700234 if (stop_l() == NO_ERROR) {
235 mDisableWaitCnt = mMaxDisableWaitCnt;
236 } else {
237 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
238 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800239 mState = STOPPED;
240 break;
241 case STOPPED:
242 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
243 // turn off sequence.
244 if (--mDisableWaitCnt == 0) {
245 reset_l();
246 mState = IDLE;
247 }
248 break;
249 default: //IDLE , ACTIVE, DESTROYED
250 break;
251 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700252
253 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800254}
255
256void AudioFlinger::EffectModule::process()
257{
258 Mutex::Autolock _l(mLock);
259
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700260 if (mState == DESTROYED || mEffectInterface == 0 ||
Eric Laurentca7cc822012-11-19 14:55:58 -0800261 mConfig.inputCfg.buffer.raw == NULL ||
262 mConfig.outputCfg.buffer.raw == NULL) {
263 return;
264 }
265
266 if (isProcessEnabled()) {
267 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
268 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
269 ditherAndClamp(mConfig.inputCfg.buffer.s32,
270 mConfig.inputCfg.buffer.s32,
271 mConfig.inputCfg.buffer.frameCount/2);
272 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700273 int ret;
274 if (isProcessImplemented()) {
275 // do the actual processing in the effect engine
Eric Laurentdb0fd692016-09-16 10:26:09 -0700276 ret = mEffectInterface->process(&mConfig.inputCfg.buffer, &mConfig.outputCfg.buffer);
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700277 } else {
278 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
279 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
280 int16_t *in = mConfig.inputCfg.buffer.s16;
281 int16_t *out = mConfig.outputCfg.buffer.s16;
Eric Laurentca7cc822012-11-19 14:55:58 -0800282
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700283 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
284 for (size_t i = 0; i < frameCnt; i++) {
285 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
286 }
287 } else {
288 memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
289 frameCnt * sizeof(int16_t));
290 }
291 }
292 ret = -ENODATA;
293 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800294 // force transition to IDLE state when engine is ready
295 if (mState == STOPPED && ret == -ENODATA) {
296 mDisableWaitCnt = 1;
297 }
298
299 // clear auxiliary effect input buffer for next accumulation
300 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
301 memset(mConfig.inputCfg.buffer.raw, 0,
302 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
303 }
304 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
305 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
306 // If an insert effect is idle and input buffer is different from output buffer,
307 // accumulate input onto output
308 sp<EffectChain> chain = mChain.promote();
309 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700310 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
Eric Laurentca7cc822012-11-19 14:55:58 -0800311 int16_t *in = mConfig.inputCfg.buffer.s16;
312 int16_t *out = mConfig.outputCfg.buffer.s16;
313 for (size_t i = 0; i < frameCnt; i++) {
314 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
315 }
316 }
317 }
318}
319
320void AudioFlinger::EffectModule::reset_l()
321{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700322 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800323 return;
324 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700325 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800326}
327
328status_t AudioFlinger::EffectModule::configure()
329{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700330 status_t status;
331 sp<ThreadBase> thread;
332 uint32_t size;
333 audio_channel_mask_t channelMask;
334
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700335 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700336 status = NO_INIT;
337 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800338 }
339
Eric Laurentd0ebb532013-04-02 16:41:41 -0700340 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800341 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700342 status = DEAD_OBJECT;
343 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800344 }
345
346 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700347 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700348 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800349
350 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
351 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900352 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
353 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800354 } else {
355 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700356 // TODO: Update this logic when multichannel effects are implemented.
357 // For offloaded tracks consider mono output as stereo for proper effect initialization
358 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
359 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
360 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
361 ALOGV("Overriding effect input and output as STEREO");
362 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800363 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700364
Eric Laurentca7cc822012-11-19 14:55:58 -0800365 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
366 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
367 mConfig.inputCfg.samplingRate = thread->sampleRate();
368 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
369 mConfig.inputCfg.bufferProvider.cookie = NULL;
370 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
371 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
372 mConfig.outputCfg.bufferProvider.cookie = NULL;
373 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
374 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
375 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
376 // Insert effect:
377 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
378 // always overwrites output buffer: input buffer == output buffer
379 // - in other sessions:
380 // last effect in the chain accumulates in output buffer: input buffer != output buffer
381 // other effect: overwrites output buffer: input buffer == output buffer
382 // Auxiliary effect:
383 // accumulates in output buffer: input buffer != output buffer
384 // Therefore: accumulate <=> input buffer != output buffer
385 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
386 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
387 } else {
388 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
389 }
390 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
391 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
392 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
393 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
394
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700395 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800396 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
397
398 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700399 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700400 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
401 sizeof(effect_config_t),
402 &mConfig,
403 &size,
404 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800405 if (status == 0) {
406 status = cmdStatus;
407 }
408
409 if (status == 0 &&
410 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
411 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
412 effect_param_t *p = (effect_param_t *)buf32;
413
414 p->psize = sizeof(uint32_t);
415 p->vsize = sizeof(uint32_t);
416 size = sizeof(int);
417 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
418
419 uint32_t latency = 0;
420 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
421 if (pbt != NULL) {
422 latency = pbt->latency_l();
423 }
424
425 *((int32_t *)p->data + 1)= latency;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700426 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
427 sizeof(effect_param_t) + 8,
428 &buf32,
429 &size,
430 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800431 }
432
433 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
434 (1000 * mConfig.outputCfg.buffer.frameCount);
435
Eric Laurentd0ebb532013-04-02 16:41:41 -0700436exit:
437 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800438 return status;
439}
440
441status_t AudioFlinger::EffectModule::init()
442{
443 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700444 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800445 return NO_INIT;
446 }
447 status_t cmdStatus;
448 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700449 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
450 0,
451 NULL,
452 &size,
453 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800454 if (status == 0) {
455 status = cmdStatus;
456 }
457 return status;
458}
459
Eric Laurent1b928682014-10-02 19:41:47 -0700460void AudioFlinger::EffectModule::addEffectToHal_l()
461{
462 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
463 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
464 sp<ThreadBase> thread = mThread.promote();
465 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700466 sp<StreamHalInterface> stream = thread->stream();
467 if (stream != 0) {
468 status_t result = stream->addEffect(mEffectInterface);
469 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700470 }
471 }
472 }
473}
474
Eric Laurentfa1e1232016-08-02 19:01:49 -0700475// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800476status_t AudioFlinger::EffectModule::start()
477{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700478 sp<EffectChain> chain;
479 status_t status;
480 {
481 Mutex::Autolock _l(mLock);
482 status = start_l();
483 if (status == NO_ERROR) {
484 chain = mChain.promote();
485 }
486 }
487 if (chain != 0) {
488 chain->resetVolume_l();
489 }
490 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800491}
492
493status_t AudioFlinger::EffectModule::start_l()
494{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700495 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800496 return NO_INIT;
497 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700498 if (mStatus != NO_ERROR) {
499 return mStatus;
500 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800501 status_t cmdStatus;
502 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700503 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
504 0,
505 NULL,
506 &size,
507 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800508 if (status == 0) {
509 status = cmdStatus;
510 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700511 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700512 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800513 }
514 return status;
515}
516
517status_t AudioFlinger::EffectModule::stop()
518{
519 Mutex::Autolock _l(mLock);
520 return stop_l();
521}
522
523status_t AudioFlinger::EffectModule::stop_l()
524{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700525 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800526 return NO_INIT;
527 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700528 if (mStatus != NO_ERROR) {
529 return mStatus;
530 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800531 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800532 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700533 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
534 0,
535 NULL,
536 &size,
537 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800538 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800539 status = cmdStatus;
540 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800541 if (status == NO_ERROR) {
542 status = remove_effect_from_hal_l();
543 }
544 return status;
545}
546
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800547// must be called with EffectChain::mLock held
548void AudioFlinger::EffectModule::release_l()
549{
550 if (mEffectInterface != 0) {
551 remove_effect_from_hal_l();
552 // release effect engine
553 mEffectInterface.clear();
554 }
555}
556
Eric Laurentbfb1b832013-01-07 09:53:42 -0800557status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
558{
559 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
560 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800561 sp<ThreadBase> thread = mThread.promote();
562 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700563 sp<StreamHalInterface> stream = thread->stream();
564 if (stream != 0) {
565 status_t result = stream->removeEffect(mEffectInterface);
566 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800567 }
568 }
569 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800570 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800571}
572
Andy Hunge4a1d912016-08-17 14:11:13 -0700573// round up delta valid if value and divisor are positive.
574template <typename T>
575static T roundUpDelta(const T &value, const T &divisor) {
576 T remainder = value % divisor;
577 return remainder == 0 ? 0 : divisor - remainder;
578}
579
Eric Laurentca7cc822012-11-19 14:55:58 -0800580status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
581 uint32_t cmdSize,
582 void *pCmdData,
583 uint32_t *replySize,
584 void *pReplyData)
585{
586 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700587 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800588
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700589 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800590 return NO_INIT;
591 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700592 if (mStatus != NO_ERROR) {
593 return mStatus;
594 }
Andy Hung110bc952016-06-20 15:22:52 -0700595 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700596 (sizeof(effect_param_t) > cmdSize ||
597 ((effect_param_t *)pCmdData)->psize > cmdSize
598 - sizeof(effect_param_t))) {
599 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800600 android_errorWriteLog(0x534e4554, "33003822");
601 return -EINVAL;
602 }
603 if (cmdCode == EFFECT_CMD_GET_PARAM &&
604 (*replySize < sizeof(effect_param_t) ||
605 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
606 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700607 return -EINVAL;
608 }
ragoe2759072016-11-22 18:02:48 -0800609 if (cmdCode == EFFECT_CMD_GET_PARAM &&
610 (sizeof(effect_param_t) > *replySize
611 || ((effect_param_t *)pCmdData)->psize > *replySize
612 - sizeof(effect_param_t)
613 || ((effect_param_t *)pCmdData)->vsize > *replySize
614 - sizeof(effect_param_t)
615 - ((effect_param_t *)pCmdData)->psize
616 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
617 *replySize
618 - sizeof(effect_param_t)
619 - ((effect_param_t *)pCmdData)->psize
620 - ((effect_param_t *)pCmdData)->vsize)) {
621 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
622 android_errorWriteLog(0x534e4554, "32705438");
623 return -EINVAL;
624 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700625 if ((cmdCode == EFFECT_CMD_SET_PARAM
626 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
627 (sizeof(effect_param_t) > cmdSize
628 || ((effect_param_t *)pCmdData)->psize > cmdSize
629 - sizeof(effect_param_t)
630 || ((effect_param_t *)pCmdData)->vsize > cmdSize
631 - sizeof(effect_param_t)
632 - ((effect_param_t *)pCmdData)->psize
633 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
634 cmdSize
635 - sizeof(effect_param_t)
636 - ((effect_param_t *)pCmdData)->psize
637 - ((effect_param_t *)pCmdData)->vsize)) {
638 android_errorWriteLog(0x534e4554, "30204301");
639 return -EINVAL;
640 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700641 status_t status = mEffectInterface->command(cmdCode,
642 cmdSize,
643 pCmdData,
644 replySize,
645 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800646 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
647 uint32_t size = (replySize == NULL) ? 0 : *replySize;
648 for (size_t i = 1; i < mHandles.size(); i++) {
649 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800650 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800651 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
652 }
653 }
654 }
655 return status;
656}
657
658status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
659{
660 Mutex::Autolock _l(mLock);
661 return setEnabled_l(enabled);
662}
663
664// must be called with EffectModule::mLock held
665status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
666{
667
668 ALOGV("setEnabled %p enabled %d", this, enabled);
669
670 if (enabled != isEnabled()) {
671 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
672 if (enabled && status != NO_ERROR) {
673 return status;
674 }
675
676 switch (mState) {
677 // going from disabled to enabled
678 case IDLE:
679 mState = STARTING;
680 break;
681 case STOPPED:
682 mState = RESTART;
683 break;
684 case STOPPING:
685 mState = ACTIVE;
686 break;
687
688 // going from enabled to disabled
689 case RESTART:
690 mState = STOPPED;
691 break;
692 case STARTING:
693 mState = IDLE;
694 break;
695 case ACTIVE:
696 mState = STOPPING;
697 break;
698 case DESTROYED:
699 return NO_ERROR; // simply ignore as we are being destroyed
700 }
701 for (size_t i = 1; i < mHandles.size(); i++) {
702 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800703 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800704 h->setEnabled(enabled);
705 }
706 }
707 }
708 return NO_ERROR;
709}
710
711bool AudioFlinger::EffectModule::isEnabled() const
712{
713 switch (mState) {
714 case RESTART:
715 case STARTING:
716 case ACTIVE:
717 return true;
718 case IDLE:
719 case STOPPING:
720 case STOPPED:
721 case DESTROYED:
722 default:
723 return false;
724 }
725}
726
727bool AudioFlinger::EffectModule::isProcessEnabled() const
728{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700729 if (mStatus != NO_ERROR) {
730 return false;
731 }
732
Eric Laurentca7cc822012-11-19 14:55:58 -0800733 switch (mState) {
734 case RESTART:
735 case ACTIVE:
736 case STOPPING:
737 case STOPPED:
738 return true;
739 case IDLE:
740 case STARTING:
741 case DESTROYED:
742 default:
743 return false;
744 }
745}
746
747status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
748{
749 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700750 if (mStatus != NO_ERROR) {
751 return mStatus;
752 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800753 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800754 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
755 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
756 if (isProcessEnabled() &&
757 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
758 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800759 uint32_t volume[2];
760 uint32_t *pVolume = NULL;
761 uint32_t size = sizeof(volume);
762 volume[0] = *left;
763 volume[1] = *right;
764 if (controller) {
765 pVolume = volume;
766 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700767 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
768 size,
769 volume,
770 &size,
771 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -0800772 if (controller && status == NO_ERROR && size == sizeof(volume)) {
773 *left = volume[0];
774 *right = volume[1];
775 }
776 }
777 return status;
778}
779
780status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
781{
782 if (device == AUDIO_DEVICE_NONE) {
783 return NO_ERROR;
784 }
785
786 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700787 if (mStatus != NO_ERROR) {
788 return mStatus;
789 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800790 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700791 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800792 status_t cmdStatus;
793 uint32_t size = sizeof(status_t);
794 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
795 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700796 status = mEffectInterface->command(cmd,
797 sizeof(uint32_t),
798 &device,
799 &size,
800 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800801 }
802 return status;
803}
804
805status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
806{
807 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700808 if (mStatus != NO_ERROR) {
809 return mStatus;
810 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800811 status_t status = NO_ERROR;
812 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
813 status_t cmdStatus;
814 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700815 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
816 sizeof(audio_mode_t),
817 &mode,
818 &size,
819 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800820 if (status == NO_ERROR) {
821 status = cmdStatus;
822 }
823 }
824 return status;
825}
826
827status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
828{
829 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700830 if (mStatus != NO_ERROR) {
831 return mStatus;
832 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800833 status_t status = NO_ERROR;
834 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
835 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700836 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
837 sizeof(audio_source_t),
838 &source,
839 &size,
840 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800841 }
842 return status;
843}
844
845void AudioFlinger::EffectModule::setSuspended(bool suspended)
846{
847 Mutex::Autolock _l(mLock);
848 mSuspended = suspended;
849}
850
851bool AudioFlinger::EffectModule::suspended() const
852{
853 Mutex::Autolock _l(mLock);
854 return mSuspended;
855}
856
857bool AudioFlinger::EffectModule::purgeHandles()
858{
859 bool enabled = false;
860 Mutex::Autolock _l(mLock);
861 for (size_t i = 0; i < mHandles.size(); i++) {
862 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800863 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800864 if (handle->hasControl()) {
865 enabled = handle->enabled();
866 }
867 }
868 }
869 return enabled;
870}
871
Eric Laurent5baf2af2013-09-12 17:37:00 -0700872status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
873{
874 Mutex::Autolock _l(mLock);
875 if (mStatus != NO_ERROR) {
876 return mStatus;
877 }
878 status_t status = NO_ERROR;
879 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
880 status_t cmdStatus;
881 uint32_t size = sizeof(status_t);
882 effect_offload_param_t cmd;
883
884 cmd.isOffload = offloaded;
885 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700886 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
887 sizeof(effect_offload_param_t),
888 &cmd,
889 &size,
890 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700891 if (status == NO_ERROR) {
892 status = cmdStatus;
893 }
894 mOffloaded = (status == NO_ERROR) ? offloaded : false;
895 } else {
896 if (offloaded) {
897 status = INVALID_OPERATION;
898 }
899 mOffloaded = false;
900 }
901 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
902 return status;
903}
904
905bool AudioFlinger::EffectModule::isOffloaded() const
906{
907 Mutex::Autolock _l(mLock);
908 return mOffloaded;
909}
910
Marco Nelissenb2208842014-02-07 14:00:50 -0800911String8 effectFlagsToString(uint32_t flags) {
912 String8 s;
913
914 s.append("conn. mode: ");
915 switch (flags & EFFECT_FLAG_TYPE_MASK) {
916 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
917 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
918 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
919 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
920 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
921 default: s.append("unknown/reserved"); break;
922 }
923 s.append(", ");
924
925 s.append("insert pref: ");
926 switch (flags & EFFECT_FLAG_INSERT_MASK) {
927 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
928 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
929 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
930 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
931 default: s.append("unknown/reserved"); break;
932 }
933 s.append(", ");
934
935 s.append("volume mgmt: ");
936 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
937 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
938 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
939 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
940 default: s.append("unknown/reserved"); break;
941 }
942 s.append(", ");
943
944 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
945 if (devind) {
946 s.append("device indication: ");
947 switch (devind) {
948 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
949 default: s.append("unknown/reserved"); break;
950 }
951 s.append(", ");
952 }
953
954 s.append("input mode: ");
955 switch (flags & EFFECT_FLAG_INPUT_MASK) {
956 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
957 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
958 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
959 default: s.append("not set"); break;
960 }
961 s.append(", ");
962
963 s.append("output mode: ");
964 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
965 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
966 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
967 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
968 default: s.append("not set"); break;
969 }
970 s.append(", ");
971
972 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
973 if (accel) {
974 s.append("hardware acceleration: ");
975 switch (accel) {
976 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
977 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
978 default: s.append("unknown/reserved"); break;
979 }
980 s.append(", ");
981 }
982
983 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
984 if (modeind) {
985 s.append("mode indication: ");
986 switch (modeind) {
987 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
988 default: s.append("unknown/reserved"); break;
989 }
990 s.append(", ");
991 }
992
993 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
994 if (srcind) {
995 s.append("source indication: ");
996 switch (srcind) {
997 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
998 default: s.append("unknown/reserved"); break;
999 }
1000 s.append(", ");
1001 }
1002
1003 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1004 s.append("offloadable, ");
1005 }
1006
1007 int len = s.length();
1008 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001009 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001010 s.unlockBuffer(len - 2);
1011 }
1012 return s;
1013}
1014
1015
Glenn Kasten0f11b512014-01-31 16:18:54 -08001016void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001017{
1018 const size_t SIZE = 256;
1019 char buffer[SIZE];
1020 String8 result;
1021
1022 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1023 result.append(buffer);
1024
1025 bool locked = AudioFlinger::dumpTryLock(mLock);
1026 // failed to lock - AudioFlinger is probably deadlocked
1027 if (!locked) {
1028 result.append("\t\tCould not lock Fx mutex:\n");
1029 }
1030
1031 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001032 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001033 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 result.append(buffer);
1035
1036 result.append("\t\tDescriptor:\n");
1037 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1038 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1039 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1040 mDescriptor.uuid.node[2],
1041 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1042 result.append(buffer);
1043 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1044 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1045 mDescriptor.type.timeHiAndVersion,
1046 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1047 mDescriptor.type.node[2],
1048 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1049 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001050 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001051 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001052 mDescriptor.flags,
1053 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001054 result.append(buffer);
1055 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1056 mDescriptor.name);
1057 result.append(buffer);
1058 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1059 mDescriptor.implementor);
1060 result.append(buffer);
1061
1062 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001063 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001064 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001065 mConfig.inputCfg.buffer.frameCount,
1066 mConfig.inputCfg.samplingRate,
1067 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001068 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001069 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001070 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001071 result.append(buffer);
1072
1073 result.append("\t\t- Output configuration:\n");
1074 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001075 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001076 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001077 mConfig.outputCfg.buffer.frameCount,
1078 mConfig.outputCfg.samplingRate,
1079 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001080 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001081 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001082 result.append(buffer);
1083
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001084 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001085 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001086 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 for (size_t i = 0; i < mHandles.size(); ++i) {
1088 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001089 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001090 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001091 result.append(buffer);
1092 }
1093 }
1094
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 write(fd, result.string(), result.length());
1096
1097 if (locked) {
1098 mLock.unlock();
1099 }
1100}
1101
1102// ----------------------------------------------------------------------------
1103// EffectHandle implementation
1104// ----------------------------------------------------------------------------
1105
1106#undef LOG_TAG
1107#define LOG_TAG "AudioFlinger::EffectHandle"
1108
1109AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1110 const sp<AudioFlinger::Client>& client,
1111 const sp<IEffectClient>& effectClient,
1112 int32_t priority)
1113 : BnEffect(),
1114 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001115 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001116{
1117 ALOGV("constructor %p", this);
1118
1119 if (client == 0) {
1120 return;
1121 }
1122 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1123 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001124 if (mCblkMemory == 0 ||
1125 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001126 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001127 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001128 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001129 return;
1130 }
Glenn Kastene75da402013-11-20 13:54:52 -08001131 new(mCblk) effect_param_cblk_t();
1132 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001133}
1134
1135AudioFlinger::EffectHandle::~EffectHandle()
1136{
1137 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001138 disconnect(false);
1139}
1140
Glenn Kastene75da402013-11-20 13:54:52 -08001141status_t AudioFlinger::EffectHandle::initCheck()
1142{
1143 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1144}
1145
Eric Laurentca7cc822012-11-19 14:55:58 -08001146status_t AudioFlinger::EffectHandle::enable()
1147{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001148 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001149 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001150 sp<EffectModule> effect = mEffect.promote();
1151 if (effect == 0 || mDisconnected) {
1152 return DEAD_OBJECT;
1153 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001154 if (!mHasControl) {
1155 return INVALID_OPERATION;
1156 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001157
1158 if (mEnabled) {
1159 return NO_ERROR;
1160 }
1161
1162 mEnabled = true;
1163
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001164 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001165 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001166 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001167 }
1168
1169 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001170 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001171 return NO_ERROR;
1172 }
1173
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001174 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001175 if (status != NO_ERROR) {
1176 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001177 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001178 }
1179 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001180 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001181 if (thread != 0) {
1182 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001183 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001184 Mutex::Autolock _l(t->mLock);
1185 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001186 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001187 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001188 if (thread->type() == ThreadBase::OFFLOAD) {
1189 PlaybackThread *t = (PlaybackThread *)thread.get();
1190 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1191 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001192 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001193 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1194 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001195 }
1196 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001197 }
1198 return status;
1199}
1200
1201status_t AudioFlinger::EffectHandle::disable()
1202{
1203 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001204 AutoMutex _l(mLock);
1205 sp<EffectModule> effect = mEffect.promote();
1206 if (effect == 0 || mDisconnected) {
1207 return DEAD_OBJECT;
1208 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001209 if (!mHasControl) {
1210 return INVALID_OPERATION;
1211 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001212
1213 if (!mEnabled) {
1214 return NO_ERROR;
1215 }
1216 mEnabled = false;
1217
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001218 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001219 return NO_ERROR;
1220 }
1221
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001222 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001223
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001224 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001225 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001226 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001227 if (thread->type() == ThreadBase::OFFLOAD) {
1228 PlaybackThread *t = (PlaybackThread *)thread.get();
1229 Mutex::Autolock _l(t->mLock);
1230 t->broadcast_l();
1231 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001232 }
1233
1234 return status;
1235}
1236
1237void AudioFlinger::EffectHandle::disconnect()
1238{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001239 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001240 disconnect(true);
1241}
1242
1243void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1244{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001245 AutoMutex _l(mLock);
1246 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1247 if (mDisconnected) {
1248 if (unpinIfLast) {
1249 android_errorWriteLog(0x534e4554, "32707507");
1250 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001251 return;
1252 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001253 mDisconnected = true;
1254 sp<ThreadBase> thread;
1255 {
1256 sp<EffectModule> effect = mEffect.promote();
1257 if (effect != 0) {
1258 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001259 }
1260 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001261 if (thread != 0) {
1262 thread->disconnectEffectHandle(this, unpinIfLast);
1263 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001264
Eric Laurentca7cc822012-11-19 14:55:58 -08001265 if (mClient != 0) {
1266 if (mCblk != NULL) {
1267 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1268 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1269 }
1270 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001271 // Client destructor must run with AudioFlinger client mutex locked
1272 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001273 mClient.clear();
1274 }
1275}
1276
1277status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1278 uint32_t cmdSize,
1279 void *pCmdData,
1280 uint32_t *replySize,
1281 void *pReplyData)
1282{
1283 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001284 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001285
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001286 AutoMutex _l(mLock);
1287 sp<EffectModule> effect = mEffect.promote();
1288 if (effect == 0 || mDisconnected) {
1289 return DEAD_OBJECT;
1290 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001291 // only get parameter command is permitted for applications not controlling the effect
1292 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1293 return INVALID_OPERATION;
1294 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001295 if (mClient == 0) {
1296 return INVALID_OPERATION;
1297 }
1298
1299 // handle commands that are not forwarded transparently to effect engine
1300 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1301 // No need to trylock() here as this function is executed in the binder thread serving a
1302 // particular client process: no risk to block the whole media server process or mixer
1303 // threads if we are stuck here
1304 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001305
1306 // keep local copy of index in case of client corruption b/32220769
1307 const uint32_t clientIndex = mCblk->clientIndex;
1308 const uint32_t serverIndex = mCblk->serverIndex;
1309 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1310 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001311 mCblk->serverIndex = 0;
1312 mCblk->clientIndex = 0;
1313 return BAD_VALUE;
1314 }
1315 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001316 effect_param_t *param = NULL;
1317 for (uint32_t index = serverIndex; index < clientIndex;) {
1318 int *p = (int *)(mBuffer + index);
1319 const int size = *p++;
1320 if (size < 0
1321 || size > EFFECT_PARAM_BUFFER_SIZE
1322 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001323 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001324 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001325 break;
1326 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001327
1328 // copy to local memory in case of client corruption b/32220769
1329 param = (effect_param_t *)realloc(param, size);
1330 if (param == NULL) {
1331 ALOGW("command(): out of memory");
1332 status = NO_MEMORY;
1333 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001334 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001335 memcpy(param, p, size);
1336
1337 int reply = 0;
1338 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001339 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001340 size,
1341 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001342 &rsize,
1343 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001344
1345 // verify shared memory: server index shouldn't change; client index can't go back.
1346 if (serverIndex != mCblk->serverIndex
1347 || clientIndex > mCblk->clientIndex) {
1348 android_errorWriteLog(0x534e4554, "32220769");
1349 status = BAD_VALUE;
1350 break;
1351 }
1352
Eric Laurentca7cc822012-11-19 14:55:58 -08001353 // stop at first error encountered
1354 if (ret != NO_ERROR) {
1355 status = ret;
1356 *(int *)pReplyData = reply;
1357 break;
1358 } else if (reply != NO_ERROR) {
1359 *(int *)pReplyData = reply;
1360 break;
1361 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001362 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001363 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001364 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001365 mCblk->serverIndex = 0;
1366 mCblk->clientIndex = 0;
1367 return status;
1368 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1369 *(int *)pReplyData = NO_ERROR;
1370 return enable();
1371 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1372 *(int *)pReplyData = NO_ERROR;
1373 return disable();
1374 }
1375
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001376 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001377}
1378
1379void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1380{
1381 ALOGV("setControl %p control %d", this, hasControl);
1382
1383 mHasControl = hasControl;
1384 mEnabled = enabled;
1385
1386 if (signal && mEffectClient != 0) {
1387 mEffectClient->controlStatusChanged(hasControl);
1388 }
1389}
1390
1391void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1392 uint32_t cmdSize,
1393 void *pCmdData,
1394 uint32_t replySize,
1395 void *pReplyData)
1396{
1397 if (mEffectClient != 0) {
1398 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1399 }
1400}
1401
1402
1403
1404void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1405{
1406 if (mEffectClient != 0) {
1407 mEffectClient->enableStatusChanged(enabled);
1408 }
1409}
1410
1411status_t AudioFlinger::EffectHandle::onTransact(
1412 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1413{
1414 return BnEffect::onTransact(code, data, reply, flags);
1415}
1416
1417
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001418void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001419{
1420 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1421
Marco Nelissenb2208842014-02-07 14:00:50 -08001422 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001423 (mClient == 0) ? getpid_cached : mClient->pid(),
1424 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001425 mHasControl ? "yes" : "no",
1426 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 mCblk ? mCblk->clientIndex : 0,
1428 mCblk ? mCblk->serverIndex : 0
1429 );
1430
1431 if (locked) {
1432 mCblk->lock.unlock();
1433 }
1434}
1435
1436#undef LOG_TAG
1437#define LOG_TAG "AudioFlinger::EffectChain"
1438
1439AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001440 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001441 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1442 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001443 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001444{
1445 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1446 if (thread == NULL) {
1447 return;
1448 }
1449 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1450 thread->frameCount();
1451}
1452
1453AudioFlinger::EffectChain::~EffectChain()
1454{
1455 if (mOwnInBuffer) {
1456 delete mInBuffer;
1457 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001458}
1459
1460// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1461sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1462 effect_descriptor_t *descriptor)
1463{
1464 size_t size = mEffects.size();
1465
1466 for (size_t i = 0; i < size; i++) {
1467 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1468 return mEffects[i];
1469 }
1470 }
1471 return 0;
1472}
1473
1474// getEffectFromId_l() must be called with ThreadBase::mLock held
1475sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1476{
1477 size_t size = mEffects.size();
1478
1479 for (size_t i = 0; i < size; i++) {
1480 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1481 if (id == 0 || mEffects[i]->id() == id) {
1482 return mEffects[i];
1483 }
1484 }
1485 return 0;
1486}
1487
1488// getEffectFromType_l() must be called with ThreadBase::mLock held
1489sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1490 const effect_uuid_t *type)
1491{
1492 size_t size = mEffects.size();
1493
1494 for (size_t i = 0; i < size; i++) {
1495 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1496 return mEffects[i];
1497 }
1498 }
1499 return 0;
1500}
1501
1502void AudioFlinger::EffectChain::clearInputBuffer()
1503{
1504 Mutex::Autolock _l(mLock);
1505 sp<ThreadBase> thread = mThread.promote();
1506 if (thread == 0) {
1507 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1508 return;
1509 }
1510 clearInputBuffer_l(thread);
1511}
1512
1513// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001514void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001515{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001516 // TODO: This will change in the future, depending on multichannel
1517 // and sample format changes for effects.
1518 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1519 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001520 const size_t frameSize =
1521 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001522 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001523}
1524
1525// Must be called with EffectChain::mLock locked
1526void AudioFlinger::EffectChain::process_l()
1527{
1528 sp<ThreadBase> thread = mThread.promote();
1529 if (thread == 0) {
1530 ALOGW("process_l(): cannot promote mixer thread");
1531 return;
1532 }
1533 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1534 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001535 // never process effects when:
1536 // - on an OFFLOAD thread
1537 // - no more tracks are on the session and the effect tail has been rendered
1538 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001539 if (!isGlobalSession) {
1540 bool tracksOnSession = (trackCnt() != 0);
1541
1542 if (!tracksOnSession && mTailBufferCount == 0) {
1543 doProcess = false;
1544 }
1545
1546 if (activeTrackCnt() == 0) {
1547 // if no track is active and the effect tail has not been rendered,
1548 // the input buffer must be cleared here as the mixer process will not do it
1549 if (tracksOnSession || mTailBufferCount > 0) {
1550 clearInputBuffer_l(thread);
1551 if (mTailBufferCount > 0) {
1552 mTailBufferCount--;
1553 }
1554 }
1555 }
1556 }
1557
1558 size_t size = mEffects.size();
1559 if (doProcess) {
1560 for (size_t i = 0; i < size; i++) {
1561 mEffects[i]->process();
1562 }
1563 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001564 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001565 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001566 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1567 }
1568 if (doResetVolume) {
1569 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001570 }
1571}
1572
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001573// createEffect_l() must be called with ThreadBase::mLock held
1574status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1575 ThreadBase *thread,
1576 effect_descriptor_t *desc,
1577 int id,
1578 audio_session_t sessionId,
1579 bool pinned)
1580{
1581 Mutex::Autolock _l(mLock);
1582 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1583 status_t lStatus = effect->status();
1584 if (lStatus == NO_ERROR) {
1585 lStatus = addEffect_ll(effect);
1586 }
1587 if (lStatus != NO_ERROR) {
1588 effect.clear();
1589 }
1590 return lStatus;
1591}
1592
1593// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001594status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1595{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001596 Mutex::Autolock _l(mLock);
1597 return addEffect_ll(effect);
1598}
1599// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1600status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1601{
Eric Laurentca7cc822012-11-19 14:55:58 -08001602 effect_descriptor_t desc = effect->desc();
1603 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1604
Eric Laurentca7cc822012-11-19 14:55:58 -08001605 effect->setChain(this);
1606 sp<ThreadBase> thread = mThread.promote();
1607 if (thread == 0) {
1608 return NO_INIT;
1609 }
1610 effect->setThread(thread);
1611
1612 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1613 // Auxiliary effects are inserted at the beginning of mEffects vector as
1614 // they are processed first and accumulated in chain input buffer
1615 mEffects.insertAt(effect, 0);
1616
1617 // the input buffer for auxiliary effect contains mono samples in
1618 // 32 bit format. This is to avoid saturation in AudoMixer
1619 // accumulation stage. Saturation is done in EffectModule::process() before
1620 // calling the process in effect engine
1621 size_t numSamples = thread->frameCount();
1622 int32_t *buffer = new int32_t[numSamples];
1623 memset(buffer, 0, numSamples * sizeof(int32_t));
1624 effect->setInBuffer((int16_t *)buffer);
1625 // auxiliary effects output samples to chain input buffer for further processing
1626 // by insert effects
1627 effect->setOutBuffer(mInBuffer);
1628 } else {
1629 // Insert effects are inserted at the end of mEffects vector as they are processed
1630 // after track and auxiliary effects.
1631 // Insert effect order as a function of indicated preference:
1632 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1633 // another effect is present
1634 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1635 // last effect claiming first position
1636 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1637 // first effect claiming last position
1638 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1639 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1640 // already present
1641
1642 size_t size = mEffects.size();
1643 size_t idx_insert = size;
1644 ssize_t idx_insert_first = -1;
1645 ssize_t idx_insert_last = -1;
1646
1647 for (size_t i = 0; i < size; i++) {
1648 effect_descriptor_t d = mEffects[i]->desc();
1649 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1650 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1651 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1652 // check invalid effect chaining combinations
1653 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1654 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1655 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1656 desc.name, d.name);
1657 return INVALID_OPERATION;
1658 }
1659 // remember position of first insert effect and by default
1660 // select this as insert position for new effect
1661 if (idx_insert == size) {
1662 idx_insert = i;
1663 }
1664 // remember position of last insert effect claiming
1665 // first position
1666 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1667 idx_insert_first = i;
1668 }
1669 // remember position of first insert effect claiming
1670 // last position
1671 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1672 idx_insert_last == -1) {
1673 idx_insert_last = i;
1674 }
1675 }
1676 }
1677
1678 // modify idx_insert from first position if needed
1679 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1680 if (idx_insert_last != -1) {
1681 idx_insert = idx_insert_last;
1682 } else {
1683 idx_insert = size;
1684 }
1685 } else {
1686 if (idx_insert_first != -1) {
1687 idx_insert = idx_insert_first + 1;
1688 }
1689 }
1690
1691 // always read samples from chain input buffer
1692 effect->setInBuffer(mInBuffer);
1693
1694 // if last effect in the chain, output samples to chain
1695 // output buffer, otherwise to chain input buffer
1696 if (idx_insert == size) {
1697 if (idx_insert != 0) {
1698 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1699 mEffects[idx_insert-1]->configure();
1700 }
1701 effect->setOutBuffer(mOutBuffer);
1702 } else {
1703 effect->setOutBuffer(mInBuffer);
1704 }
1705 mEffects.insertAt(effect, idx_insert);
1706
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001707 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001708 idx_insert);
1709 }
1710 effect->configure();
1711 return NO_ERROR;
1712}
1713
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001714// removeEffect_l() must be called with ThreadBase::mLock held
1715size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1716 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001717{
1718 Mutex::Autolock _l(mLock);
1719 size_t size = mEffects.size();
1720 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1721
1722 for (size_t i = 0; i < size; i++) {
1723 if (effect == mEffects[i]) {
1724 // calling stop here will remove pre-processing effect from the audio HAL.
1725 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1726 // the middle of a read from audio HAL
1727 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1728 mEffects[i]->state() == EffectModule::STOPPING) {
1729 mEffects[i]->stop();
1730 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001731 if (release) {
1732 mEffects[i]->release_l();
1733 }
1734
Eric Laurentca7cc822012-11-19 14:55:58 -08001735 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1736 delete[] effect->inBuffer();
1737 } else {
1738 if (i == size - 1 && i != 0) {
1739 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1740 mEffects[i - 1]->configure();
1741 }
1742 }
1743 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001744 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001745 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001746
Eric Laurentca7cc822012-11-19 14:55:58 -08001747 break;
1748 }
1749 }
1750
1751 return mEffects.size();
1752}
1753
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001754// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001755void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1756{
1757 size_t size = mEffects.size();
1758 for (size_t i = 0; i < size; i++) {
1759 mEffects[i]->setDevice(device);
1760 }
1761}
1762
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001763// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001764void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1765{
1766 size_t size = mEffects.size();
1767 for (size_t i = 0; i < size; i++) {
1768 mEffects[i]->setMode(mode);
1769 }
1770}
1771
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001772// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001773void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1774{
1775 size_t size = mEffects.size();
1776 for (size_t i = 0; i < size; i++) {
1777 mEffects[i]->setAudioSource(source);
1778 }
1779}
1780
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001781// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001782bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001783{
1784 uint32_t newLeft = *left;
1785 uint32_t newRight = *right;
1786 bool hasControl = false;
1787 int ctrlIdx = -1;
1788 size_t size = mEffects.size();
1789
1790 // first update volume controller
1791 for (size_t i = size; i > 0; i--) {
1792 if (mEffects[i - 1]->isProcessEnabled() &&
1793 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1794 ctrlIdx = i - 1;
1795 hasControl = true;
1796 break;
1797 }
1798 }
1799
Eric Laurentfa1e1232016-08-02 19:01:49 -07001800 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001801 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001802 if (hasControl) {
1803 *left = mNewLeftVolume;
1804 *right = mNewRightVolume;
1805 }
1806 return hasControl;
1807 }
1808
1809 mVolumeCtrlIdx = ctrlIdx;
1810 mLeftVolume = newLeft;
1811 mRightVolume = newRight;
1812
1813 // second get volume update from volume controller
1814 if (ctrlIdx >= 0) {
1815 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1816 mNewLeftVolume = newLeft;
1817 mNewRightVolume = newRight;
1818 }
1819 // then indicate volume to all other effects in chain.
1820 // Pass altered volume to effects before volume controller
1821 // and requested volume to effects after controller
1822 uint32_t lVol = newLeft;
1823 uint32_t rVol = newRight;
1824
1825 for (size_t i = 0; i < size; i++) {
1826 if ((int)i == ctrlIdx) {
1827 continue;
1828 }
1829 // this also works for ctrlIdx == -1 when there is no volume controller
1830 if ((int)i > ctrlIdx) {
1831 lVol = *left;
1832 rVol = *right;
1833 }
1834 mEffects[i]->setVolume(&lVol, &rVol, false);
1835 }
1836 *left = newLeft;
1837 *right = newRight;
1838
1839 return hasControl;
1840}
1841
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001842// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001843void AudioFlinger::EffectChain::resetVolume_l()
1844{
Eric Laurente7449bf2016-08-03 18:44:07 -07001845 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1846 uint32_t left = mLeftVolume;
1847 uint32_t right = mRightVolume;
1848 (void)setVolume_l(&left, &right, true);
1849 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001850}
1851
Eric Laurent1b928682014-10-02 19:41:47 -07001852void AudioFlinger::EffectChain::syncHalEffectsState()
1853{
1854 Mutex::Autolock _l(mLock);
1855 for (size_t i = 0; i < mEffects.size(); i++) {
1856 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1857 mEffects[i]->state() == EffectModule::STOPPING) {
1858 mEffects[i]->addEffectToHal_l();
1859 }
1860 }
1861}
1862
Eric Laurentca7cc822012-11-19 14:55:58 -08001863void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1864{
1865 const size_t SIZE = 256;
1866 char buffer[SIZE];
1867 String8 result;
1868
Marco Nelissenb2208842014-02-07 14:00:50 -08001869 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001870 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001871 result.append(buffer);
1872
Marco Nelissenb2208842014-02-07 14:00:50 -08001873 if (numEffects) {
1874 bool locked = AudioFlinger::dumpTryLock(mLock);
1875 // failed to lock - AudioFlinger is probably deadlocked
1876 if (!locked) {
1877 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001878 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001879
Marco Nelissenb2208842014-02-07 14:00:50 -08001880 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001881 snprintf(buffer, SIZE, "\t%p %p %d\n",
1882 mInBuffer,
1883 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001884 mActiveTrackCnt);
1885 result.append(buffer);
1886 write(fd, result.string(), result.size());
1887
1888 for (size_t i = 0; i < numEffects; ++i) {
1889 sp<EffectModule> effect = mEffects[i];
1890 if (effect != 0) {
1891 effect->dump(fd, args);
1892 }
1893 }
1894
1895 if (locked) {
1896 mLock.unlock();
1897 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001898 }
1899}
1900
1901// must be called with ThreadBase::mLock held
1902void AudioFlinger::EffectChain::setEffectSuspended_l(
1903 const effect_uuid_t *type, bool suspend)
1904{
1905 sp<SuspendedEffectDesc> desc;
1906 // use effect type UUID timelow as key as there is no real risk of identical
1907 // timeLow fields among effect type UUIDs.
1908 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1909 if (suspend) {
1910 if (index >= 0) {
1911 desc = mSuspendedEffects.valueAt(index);
1912 } else {
1913 desc = new SuspendedEffectDesc();
1914 desc->mType = *type;
1915 mSuspendedEffects.add(type->timeLow, desc);
1916 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1917 }
1918 if (desc->mRefCount++ == 0) {
1919 sp<EffectModule> effect = getEffectIfEnabled(type);
1920 if (effect != 0) {
1921 desc->mEffect = effect;
1922 effect->setSuspended(true);
1923 effect->setEnabled(false);
1924 }
1925 }
1926 } else {
1927 if (index < 0) {
1928 return;
1929 }
1930 desc = mSuspendedEffects.valueAt(index);
1931 if (desc->mRefCount <= 0) {
1932 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1933 desc->mRefCount = 1;
1934 }
1935 if (--desc->mRefCount == 0) {
1936 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1937 if (desc->mEffect != 0) {
1938 sp<EffectModule> effect = desc->mEffect.promote();
1939 if (effect != 0) {
1940 effect->setSuspended(false);
1941 effect->lock();
1942 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001943 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001944 effect->setEnabled_l(handle->enabled());
1945 }
1946 effect->unlock();
1947 }
1948 desc->mEffect.clear();
1949 }
1950 mSuspendedEffects.removeItemsAt(index);
1951 }
1952 }
1953}
1954
1955// must be called with ThreadBase::mLock held
1956void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1957{
1958 sp<SuspendedEffectDesc> desc;
1959
1960 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1961 if (suspend) {
1962 if (index >= 0) {
1963 desc = mSuspendedEffects.valueAt(index);
1964 } else {
1965 desc = new SuspendedEffectDesc();
1966 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1967 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1968 }
1969 if (desc->mRefCount++ == 0) {
1970 Vector< sp<EffectModule> > effects;
1971 getSuspendEligibleEffects(effects);
1972 for (size_t i = 0; i < effects.size(); i++) {
1973 setEffectSuspended_l(&effects[i]->desc().type, true);
1974 }
1975 }
1976 } else {
1977 if (index < 0) {
1978 return;
1979 }
1980 desc = mSuspendedEffects.valueAt(index);
1981 if (desc->mRefCount <= 0) {
1982 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1983 desc->mRefCount = 1;
1984 }
1985 if (--desc->mRefCount == 0) {
1986 Vector<const effect_uuid_t *> types;
1987 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1988 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1989 continue;
1990 }
1991 types.add(&mSuspendedEffects.valueAt(i)->mType);
1992 }
1993 for (size_t i = 0; i < types.size(); i++) {
1994 setEffectSuspended_l(types[i], false);
1995 }
1996 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1997 mSuspendedEffects.keyAt(index));
1998 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1999 }
2000 }
2001}
2002
2003
2004// The volume effect is used for automated tests only
2005#ifndef OPENSL_ES_H_
2006static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2007 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2008const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2009#endif //OPENSL_ES_H_
2010
2011bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2012{
2013 // auxiliary effects and visualizer are never suspended on output mix
2014 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2015 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2016 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2017 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2018 return false;
2019 }
2020 return true;
2021}
2022
2023void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2024 Vector< sp<AudioFlinger::EffectModule> > &effects)
2025{
2026 effects.clear();
2027 for (size_t i = 0; i < mEffects.size(); i++) {
2028 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2029 effects.add(mEffects[i]);
2030 }
2031 }
2032}
2033
2034sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2035 const effect_uuid_t *type)
2036{
2037 sp<EffectModule> effect = getEffectFromType_l(type);
2038 return effect != 0 && effect->isEnabled() ? effect : 0;
2039}
2040
2041void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2042 bool enabled)
2043{
2044 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2045 if (enabled) {
2046 if (index < 0) {
2047 // if the effect is not suspend check if all effects are suspended
2048 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2049 if (index < 0) {
2050 return;
2051 }
2052 if (!isEffectEligibleForSuspend(effect->desc())) {
2053 return;
2054 }
2055 setEffectSuspended_l(&effect->desc().type, enabled);
2056 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2057 if (index < 0) {
2058 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2059 return;
2060 }
2061 }
2062 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2063 effect->desc().type.timeLow);
2064 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2065 // if effect is requested to suspended but was not yet enabled, supend it now.
2066 if (desc->mEffect == 0) {
2067 desc->mEffect = effect;
2068 effect->setEnabled(false);
2069 effect->setSuspended(true);
2070 }
2071 } else {
2072 if (index < 0) {
2073 return;
2074 }
2075 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2076 effect->desc().type.timeLow);
2077 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2078 desc->mEffect.clear();
2079 effect->setSuspended(false);
2080 }
2081}
2082
Eric Laurent5baf2af2013-09-12 17:37:00 -07002083bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002084{
2085 Mutex::Autolock _l(mLock);
2086 size_t size = mEffects.size();
2087 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002088 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002089 return true;
2090 }
2091 }
2092 return false;
2093}
2094
Eric Laurentaaa44472014-09-12 17:41:50 -07002095void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2096{
2097 Mutex::Autolock _l(mLock);
2098 mThread = thread;
2099 for (size_t i = 0; i < mEffects.size(); i++) {
2100 mEffects[i]->setThread(thread);
2101 }
2102}
2103
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002104void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2105{
2106 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2107 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2108 }
2109 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2110 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2111 }
2112}
2113
2114void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2115{
2116 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2117 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2118 }
2119 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2120 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2121 }
2122}
2123
2124bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002125{
2126 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002127 for (const auto &effect : mEffects) {
2128 if (effect->isProcessImplemented()) {
2129 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002130 }
2131 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002132 // Allow effects without processing.
2133 return true;
2134}
2135
2136bool AudioFlinger::EffectChain::isFastCompatible() const
2137{
2138 Mutex::Autolock _l(mLock);
2139 for (const auto &effect : mEffects) {
2140 if (effect->isProcessImplemented()
2141 && effect->isImplementationSoftware()) {
2142 return false;
2143 }
2144 }
2145 // Allow effects without processing or hw accelerated effects.
2146 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002147}
2148
2149// isCompatibleWithThread_l() must be called with thread->mLock held
2150bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2151{
2152 Mutex::Autolock _l(mLock);
2153 for (size_t i = 0; i < mEffects.size(); i++) {
2154 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2155 return false;
2156 }
2157 }
2158 return true;
2159}
2160
Glenn Kasten63238ef2015-03-02 15:50:29 -08002161} // namespace android