blob: 128eaba90f16ae8d53666dc9a300ac08ceef1bd9 [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 Laurentf10c7092016-12-06 17:09:56 -0800210// unsafe method called when the effect parent thread has been destroyed
211ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
212{
213 ALOGV("disconnect() %p handle %p", this, handle);
214 Mutex::Autolock _l(mLock);
215 ssize_t numHandles = removeHandle_l(handle);
216 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
217 AudioSystem::unregisterEffect(mId);
218 sp<AudioFlinger> af = mAudioFlinger.promote();
219 if (af != 0) {
220 mLock.unlock();
221 af->updateOrphanEffectChains(this);
222 mLock.lock();
223 }
224 }
225 return numHandles;
226}
227
Eric Laurentfa1e1232016-08-02 19:01:49 -0700228bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800229 Mutex::Autolock _l(mLock);
230
Eric Laurentfa1e1232016-08-02 19:01:49 -0700231 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800232 switch (mState) {
233 case RESTART:
234 reset_l();
235 // FALL THROUGH
236
237 case STARTING:
238 // clear auxiliary effect input buffer for next accumulation
239 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
240 memset(mConfig.inputCfg.buffer.raw,
241 0,
242 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
243 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700244 if (start_l() == NO_ERROR) {
245 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700246 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700247 } else {
248 mState = IDLE;
249 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800250 break;
251 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700252 if (stop_l() == NO_ERROR) {
253 mDisableWaitCnt = mMaxDisableWaitCnt;
254 } else {
255 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
256 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800257 mState = STOPPED;
258 break;
259 case STOPPED:
260 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
261 // turn off sequence.
262 if (--mDisableWaitCnt == 0) {
263 reset_l();
264 mState = IDLE;
265 }
266 break;
267 default: //IDLE , ACTIVE, DESTROYED
268 break;
269 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700270
271 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800272}
273
274void AudioFlinger::EffectModule::process()
275{
276 Mutex::Autolock _l(mLock);
277
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700278 if (mState == DESTROYED || mEffectInterface == 0 ||
Eric Laurentca7cc822012-11-19 14:55:58 -0800279 mConfig.inputCfg.buffer.raw == NULL ||
280 mConfig.outputCfg.buffer.raw == NULL) {
281 return;
282 }
283
284 if (isProcessEnabled()) {
285 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
286 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
287 ditherAndClamp(mConfig.inputCfg.buffer.s32,
288 mConfig.inputCfg.buffer.s32,
289 mConfig.inputCfg.buffer.frameCount/2);
290 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700291 int ret;
292 if (isProcessImplemented()) {
293 // do the actual processing in the effect engine
Eric Laurentdb0fd692016-09-16 10:26:09 -0700294 ret = mEffectInterface->process(&mConfig.inputCfg.buffer, &mConfig.outputCfg.buffer);
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700295 } else {
296 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
297 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
298 int16_t *in = mConfig.inputCfg.buffer.s16;
299 int16_t *out = mConfig.outputCfg.buffer.s16;
Eric Laurentca7cc822012-11-19 14:55:58 -0800300
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700301 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
302 for (size_t i = 0; i < frameCnt; i++) {
303 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
304 }
305 } else {
306 memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
307 frameCnt * sizeof(int16_t));
308 }
309 }
310 ret = -ENODATA;
311 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800312 // force transition to IDLE state when engine is ready
313 if (mState == STOPPED && ret == -ENODATA) {
314 mDisableWaitCnt = 1;
315 }
316
317 // clear auxiliary effect input buffer for next accumulation
318 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
319 memset(mConfig.inputCfg.buffer.raw, 0,
320 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
321 }
322 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
323 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
324 // If an insert effect is idle and input buffer is different from output buffer,
325 // accumulate input onto output
326 sp<EffectChain> chain = mChain.promote();
327 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700328 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
Eric Laurentca7cc822012-11-19 14:55:58 -0800329 int16_t *in = mConfig.inputCfg.buffer.s16;
330 int16_t *out = mConfig.outputCfg.buffer.s16;
331 for (size_t i = 0; i < frameCnt; i++) {
332 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
333 }
334 }
335 }
336}
337
338void AudioFlinger::EffectModule::reset_l()
339{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700340 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800341 return;
342 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700343 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800344}
345
346status_t AudioFlinger::EffectModule::configure()
347{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700348 status_t status;
349 sp<ThreadBase> thread;
350 uint32_t size;
351 audio_channel_mask_t channelMask;
352
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700353 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700354 status = NO_INIT;
355 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800356 }
357
Eric Laurentd0ebb532013-04-02 16:41:41 -0700358 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800359 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700360 status = DEAD_OBJECT;
361 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800362 }
363
364 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700365 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700366 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800367
368 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
369 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900370 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
371 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800372 } else {
373 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700374 // TODO: Update this logic when multichannel effects are implemented.
375 // For offloaded tracks consider mono output as stereo for proper effect initialization
376 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
377 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
378 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
379 ALOGV("Overriding effect input and output as STEREO");
380 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800381 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700382
Eric Laurentca7cc822012-11-19 14:55:58 -0800383 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
384 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
385 mConfig.inputCfg.samplingRate = thread->sampleRate();
386 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
387 mConfig.inputCfg.bufferProvider.cookie = NULL;
388 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
389 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
390 mConfig.outputCfg.bufferProvider.cookie = NULL;
391 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
392 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
393 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
394 // Insert effect:
395 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
396 // always overwrites output buffer: input buffer == output buffer
397 // - in other sessions:
398 // last effect in the chain accumulates in output buffer: input buffer != output buffer
399 // other effect: overwrites output buffer: input buffer == output buffer
400 // Auxiliary effect:
401 // accumulates in output buffer: input buffer != output buffer
402 // Therefore: accumulate <=> input buffer != output buffer
403 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
404 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
405 } else {
406 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
407 }
408 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
409 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
410 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
411 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
412
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700413 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800414 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
415
416 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700417 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700418 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
419 sizeof(effect_config_t),
420 &mConfig,
421 &size,
422 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800423 if (status == 0) {
424 status = cmdStatus;
425 }
426
427 if (status == 0 &&
428 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
429 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
430 effect_param_t *p = (effect_param_t *)buf32;
431
432 p->psize = sizeof(uint32_t);
433 p->vsize = sizeof(uint32_t);
434 size = sizeof(int);
435 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
436
437 uint32_t latency = 0;
438 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
439 if (pbt != NULL) {
440 latency = pbt->latency_l();
441 }
442
443 *((int32_t *)p->data + 1)= latency;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700444 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
445 sizeof(effect_param_t) + 8,
446 &buf32,
447 &size,
448 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800449 }
450
451 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
452 (1000 * mConfig.outputCfg.buffer.frameCount);
453
Eric Laurentd0ebb532013-04-02 16:41:41 -0700454exit:
455 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800456 return status;
457}
458
459status_t AudioFlinger::EffectModule::init()
460{
461 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700462 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800463 return NO_INIT;
464 }
465 status_t cmdStatus;
466 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700467 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
468 0,
469 NULL,
470 &size,
471 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800472 if (status == 0) {
473 status = cmdStatus;
474 }
475 return status;
476}
477
Eric Laurent1b928682014-10-02 19:41:47 -0700478void AudioFlinger::EffectModule::addEffectToHal_l()
479{
480 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
481 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
482 sp<ThreadBase> thread = mThread.promote();
483 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700484 sp<StreamHalInterface> stream = thread->stream();
485 if (stream != 0) {
486 status_t result = stream->addEffect(mEffectInterface);
487 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700488 }
489 }
490 }
491}
492
Eric Laurentfa1e1232016-08-02 19:01:49 -0700493// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800494status_t AudioFlinger::EffectModule::start()
495{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700496 sp<EffectChain> chain;
497 status_t status;
498 {
499 Mutex::Autolock _l(mLock);
500 status = start_l();
501 if (status == NO_ERROR) {
502 chain = mChain.promote();
503 }
504 }
505 if (chain != 0) {
506 chain->resetVolume_l();
507 }
508 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800509}
510
511status_t AudioFlinger::EffectModule::start_l()
512{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700513 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800514 return NO_INIT;
515 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700516 if (mStatus != NO_ERROR) {
517 return mStatus;
518 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800519 status_t cmdStatus;
520 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700521 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
522 0,
523 NULL,
524 &size,
525 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800526 if (status == 0) {
527 status = cmdStatus;
528 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700529 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700530 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800531 }
532 return status;
533}
534
535status_t AudioFlinger::EffectModule::stop()
536{
537 Mutex::Autolock _l(mLock);
538 return stop_l();
539}
540
541status_t AudioFlinger::EffectModule::stop_l()
542{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700543 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800544 return NO_INIT;
545 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700546 if (mStatus != NO_ERROR) {
547 return mStatus;
548 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800549 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800550 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700551 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
552 0,
553 NULL,
554 &size,
555 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800556 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800557 status = cmdStatus;
558 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800559 if (status == NO_ERROR) {
560 status = remove_effect_from_hal_l();
561 }
562 return status;
563}
564
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800565// must be called with EffectChain::mLock held
566void AudioFlinger::EffectModule::release_l()
567{
568 if (mEffectInterface != 0) {
569 remove_effect_from_hal_l();
570 // release effect engine
571 mEffectInterface.clear();
572 }
573}
574
Eric Laurentbfb1b832013-01-07 09:53:42 -0800575status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
576{
577 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
578 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800579 sp<ThreadBase> thread = mThread.promote();
580 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700581 sp<StreamHalInterface> stream = thread->stream();
582 if (stream != 0) {
583 status_t result = stream->removeEffect(mEffectInterface);
584 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800585 }
586 }
587 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800588 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800589}
590
Andy Hunge4a1d912016-08-17 14:11:13 -0700591// round up delta valid if value and divisor are positive.
592template <typename T>
593static T roundUpDelta(const T &value, const T &divisor) {
594 T remainder = value % divisor;
595 return remainder == 0 ? 0 : divisor - remainder;
596}
597
Eric Laurentca7cc822012-11-19 14:55:58 -0800598status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
599 uint32_t cmdSize,
600 void *pCmdData,
601 uint32_t *replySize,
602 void *pReplyData)
603{
604 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700605 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800606
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700607 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800608 return NO_INIT;
609 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700610 if (mStatus != NO_ERROR) {
611 return mStatus;
612 }
Andy Hung110bc952016-06-20 15:22:52 -0700613 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700614 (sizeof(effect_param_t) > cmdSize ||
615 ((effect_param_t *)pCmdData)->psize > cmdSize
616 - sizeof(effect_param_t))) {
617 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800618 android_errorWriteLog(0x534e4554, "33003822");
619 return -EINVAL;
620 }
621 if (cmdCode == EFFECT_CMD_GET_PARAM &&
622 (*replySize < sizeof(effect_param_t) ||
623 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
624 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700625 return -EINVAL;
626 }
ragoe2759072016-11-22 18:02:48 -0800627 if (cmdCode == EFFECT_CMD_GET_PARAM &&
628 (sizeof(effect_param_t) > *replySize
629 || ((effect_param_t *)pCmdData)->psize > *replySize
630 - sizeof(effect_param_t)
631 || ((effect_param_t *)pCmdData)->vsize > *replySize
632 - sizeof(effect_param_t)
633 - ((effect_param_t *)pCmdData)->psize
634 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
635 *replySize
636 - sizeof(effect_param_t)
637 - ((effect_param_t *)pCmdData)->psize
638 - ((effect_param_t *)pCmdData)->vsize)) {
639 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
640 android_errorWriteLog(0x534e4554, "32705438");
641 return -EINVAL;
642 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700643 if ((cmdCode == EFFECT_CMD_SET_PARAM
644 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
645 (sizeof(effect_param_t) > cmdSize
646 || ((effect_param_t *)pCmdData)->psize > cmdSize
647 - sizeof(effect_param_t)
648 || ((effect_param_t *)pCmdData)->vsize > cmdSize
649 - sizeof(effect_param_t)
650 - ((effect_param_t *)pCmdData)->psize
651 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
652 cmdSize
653 - sizeof(effect_param_t)
654 - ((effect_param_t *)pCmdData)->psize
655 - ((effect_param_t *)pCmdData)->vsize)) {
656 android_errorWriteLog(0x534e4554, "30204301");
657 return -EINVAL;
658 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700659 status_t status = mEffectInterface->command(cmdCode,
660 cmdSize,
661 pCmdData,
662 replySize,
663 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800664 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
665 uint32_t size = (replySize == NULL) ? 0 : *replySize;
666 for (size_t i = 1; i < mHandles.size(); i++) {
667 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800668 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800669 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
670 }
671 }
672 }
673 return status;
674}
675
676status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
677{
678 Mutex::Autolock _l(mLock);
679 return setEnabled_l(enabled);
680}
681
682// must be called with EffectModule::mLock held
683status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
684{
685
686 ALOGV("setEnabled %p enabled %d", this, enabled);
687
688 if (enabled != isEnabled()) {
689 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
690 if (enabled && status != NO_ERROR) {
691 return status;
692 }
693
694 switch (mState) {
695 // going from disabled to enabled
696 case IDLE:
697 mState = STARTING;
698 break;
699 case STOPPED:
700 mState = RESTART;
701 break;
702 case STOPPING:
703 mState = ACTIVE;
704 break;
705
706 // going from enabled to disabled
707 case RESTART:
708 mState = STOPPED;
709 break;
710 case STARTING:
711 mState = IDLE;
712 break;
713 case ACTIVE:
714 mState = STOPPING;
715 break;
716 case DESTROYED:
717 return NO_ERROR; // simply ignore as we are being destroyed
718 }
719 for (size_t i = 1; i < mHandles.size(); i++) {
720 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800721 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800722 h->setEnabled(enabled);
723 }
724 }
725 }
726 return NO_ERROR;
727}
728
729bool AudioFlinger::EffectModule::isEnabled() const
730{
731 switch (mState) {
732 case RESTART:
733 case STARTING:
734 case ACTIVE:
735 return true;
736 case IDLE:
737 case STOPPING:
738 case STOPPED:
739 case DESTROYED:
740 default:
741 return false;
742 }
743}
744
745bool AudioFlinger::EffectModule::isProcessEnabled() const
746{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700747 if (mStatus != NO_ERROR) {
748 return false;
749 }
750
Eric Laurentca7cc822012-11-19 14:55:58 -0800751 switch (mState) {
752 case RESTART:
753 case ACTIVE:
754 case STOPPING:
755 case STOPPED:
756 return true;
757 case IDLE:
758 case STARTING:
759 case DESTROYED:
760 default:
761 return false;
762 }
763}
764
765status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
766{
767 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700768 if (mStatus != NO_ERROR) {
769 return mStatus;
770 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800771 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800772 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
773 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
774 if (isProcessEnabled() &&
775 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
776 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800777 uint32_t volume[2];
778 uint32_t *pVolume = NULL;
779 uint32_t size = sizeof(volume);
780 volume[0] = *left;
781 volume[1] = *right;
782 if (controller) {
783 pVolume = volume;
784 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700785 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
786 size,
787 volume,
788 &size,
789 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -0800790 if (controller && status == NO_ERROR && size == sizeof(volume)) {
791 *left = volume[0];
792 *right = volume[1];
793 }
794 }
795 return status;
796}
797
798status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
799{
800 if (device == AUDIO_DEVICE_NONE) {
801 return NO_ERROR;
802 }
803
804 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700805 if (mStatus != NO_ERROR) {
806 return mStatus;
807 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800808 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700809 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800810 status_t cmdStatus;
811 uint32_t size = sizeof(status_t);
812 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
813 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700814 status = mEffectInterface->command(cmd,
815 sizeof(uint32_t),
816 &device,
817 &size,
818 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800819 }
820 return status;
821}
822
823status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
824{
825 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700826 if (mStatus != NO_ERROR) {
827 return mStatus;
828 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800829 status_t status = NO_ERROR;
830 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
831 status_t cmdStatus;
832 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700833 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
834 sizeof(audio_mode_t),
835 &mode,
836 &size,
837 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 if (status == NO_ERROR) {
839 status = cmdStatus;
840 }
841 }
842 return status;
843}
844
845status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
846{
847 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700848 if (mStatus != NO_ERROR) {
849 return mStatus;
850 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800851 status_t status = NO_ERROR;
852 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
853 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700854 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
855 sizeof(audio_source_t),
856 &source,
857 &size,
858 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800859 }
860 return status;
861}
862
863void AudioFlinger::EffectModule::setSuspended(bool suspended)
864{
865 Mutex::Autolock _l(mLock);
866 mSuspended = suspended;
867}
868
869bool AudioFlinger::EffectModule::suspended() const
870{
871 Mutex::Autolock _l(mLock);
872 return mSuspended;
873}
874
875bool AudioFlinger::EffectModule::purgeHandles()
876{
877 bool enabled = false;
878 Mutex::Autolock _l(mLock);
879 for (size_t i = 0; i < mHandles.size(); i++) {
880 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800881 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800882 if (handle->hasControl()) {
883 enabled = handle->enabled();
884 }
885 }
886 }
887 return enabled;
888}
889
Eric Laurent5baf2af2013-09-12 17:37:00 -0700890status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
891{
892 Mutex::Autolock _l(mLock);
893 if (mStatus != NO_ERROR) {
894 return mStatus;
895 }
896 status_t status = NO_ERROR;
897 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
898 status_t cmdStatus;
899 uint32_t size = sizeof(status_t);
900 effect_offload_param_t cmd;
901
902 cmd.isOffload = offloaded;
903 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700904 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
905 sizeof(effect_offload_param_t),
906 &cmd,
907 &size,
908 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700909 if (status == NO_ERROR) {
910 status = cmdStatus;
911 }
912 mOffloaded = (status == NO_ERROR) ? offloaded : false;
913 } else {
914 if (offloaded) {
915 status = INVALID_OPERATION;
916 }
917 mOffloaded = false;
918 }
919 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
920 return status;
921}
922
923bool AudioFlinger::EffectModule::isOffloaded() const
924{
925 Mutex::Autolock _l(mLock);
926 return mOffloaded;
927}
928
Marco Nelissenb2208842014-02-07 14:00:50 -0800929String8 effectFlagsToString(uint32_t flags) {
930 String8 s;
931
932 s.append("conn. mode: ");
933 switch (flags & EFFECT_FLAG_TYPE_MASK) {
934 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
935 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
936 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
937 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
938 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
939 default: s.append("unknown/reserved"); break;
940 }
941 s.append(", ");
942
943 s.append("insert pref: ");
944 switch (flags & EFFECT_FLAG_INSERT_MASK) {
945 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
946 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
947 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
948 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
949 default: s.append("unknown/reserved"); break;
950 }
951 s.append(", ");
952
953 s.append("volume mgmt: ");
954 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
955 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
956 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
957 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
958 default: s.append("unknown/reserved"); break;
959 }
960 s.append(", ");
961
962 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
963 if (devind) {
964 s.append("device indication: ");
965 switch (devind) {
966 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
967 default: s.append("unknown/reserved"); break;
968 }
969 s.append(", ");
970 }
971
972 s.append("input mode: ");
973 switch (flags & EFFECT_FLAG_INPUT_MASK) {
974 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
975 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
976 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
977 default: s.append("not set"); break;
978 }
979 s.append(", ");
980
981 s.append("output mode: ");
982 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
983 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
984 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
985 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
986 default: s.append("not set"); break;
987 }
988 s.append(", ");
989
990 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
991 if (accel) {
992 s.append("hardware acceleration: ");
993 switch (accel) {
994 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
995 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
996 default: s.append("unknown/reserved"); break;
997 }
998 s.append(", ");
999 }
1000
1001 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1002 if (modeind) {
1003 s.append("mode indication: ");
1004 switch (modeind) {
1005 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1006 default: s.append("unknown/reserved"); break;
1007 }
1008 s.append(", ");
1009 }
1010
1011 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1012 if (srcind) {
1013 s.append("source indication: ");
1014 switch (srcind) {
1015 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1016 default: s.append("unknown/reserved"); break;
1017 }
1018 s.append(", ");
1019 }
1020
1021 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1022 s.append("offloadable, ");
1023 }
1024
1025 int len = s.length();
1026 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001027 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001028 s.unlockBuffer(len - 2);
1029 }
1030 return s;
1031}
1032
1033
Glenn Kasten0f11b512014-01-31 16:18:54 -08001034void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001035{
1036 const size_t SIZE = 256;
1037 char buffer[SIZE];
1038 String8 result;
1039
1040 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1041 result.append(buffer);
1042
1043 bool locked = AudioFlinger::dumpTryLock(mLock);
1044 // failed to lock - AudioFlinger is probably deadlocked
1045 if (!locked) {
1046 result.append("\t\tCould not lock Fx mutex:\n");
1047 }
1048
1049 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001050 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001051 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001052 result.append(buffer);
1053
1054 result.append("\t\tDescriptor:\n");
1055 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1056 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1057 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1058 mDescriptor.uuid.node[2],
1059 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1060 result.append(buffer);
1061 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1062 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1063 mDescriptor.type.timeHiAndVersion,
1064 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1065 mDescriptor.type.node[2],
1066 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1067 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001068 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001070 mDescriptor.flags,
1071 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001072 result.append(buffer);
1073 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1074 mDescriptor.name);
1075 result.append(buffer);
1076 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1077 mDescriptor.implementor);
1078 result.append(buffer);
1079
1080 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001081 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001082 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001083 mConfig.inputCfg.buffer.frameCount,
1084 mConfig.inputCfg.samplingRate,
1085 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001086 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001087 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001088 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001089 result.append(buffer);
1090
1091 result.append("\t\t- Output configuration:\n");
1092 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001093 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001094 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 mConfig.outputCfg.buffer.frameCount,
1096 mConfig.outputCfg.samplingRate,
1097 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001098 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001099 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001100 result.append(buffer);
1101
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001102 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001103 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001104 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001105 for (size_t i = 0; i < mHandles.size(); ++i) {
1106 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001107 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001108 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001109 result.append(buffer);
1110 }
1111 }
1112
Eric Laurentca7cc822012-11-19 14:55:58 -08001113 write(fd, result.string(), result.length());
1114
1115 if (locked) {
1116 mLock.unlock();
1117 }
1118}
1119
1120// ----------------------------------------------------------------------------
1121// EffectHandle implementation
1122// ----------------------------------------------------------------------------
1123
1124#undef LOG_TAG
1125#define LOG_TAG "AudioFlinger::EffectHandle"
1126
1127AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1128 const sp<AudioFlinger::Client>& client,
1129 const sp<IEffectClient>& effectClient,
1130 int32_t priority)
1131 : BnEffect(),
1132 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001133 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001134{
1135 ALOGV("constructor %p", this);
1136
1137 if (client == 0) {
1138 return;
1139 }
1140 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1141 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001142 if (mCblkMemory == 0 ||
1143 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001144 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001145 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001146 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001147 return;
1148 }
Glenn Kastene75da402013-11-20 13:54:52 -08001149 new(mCblk) effect_param_cblk_t();
1150 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001151}
1152
1153AudioFlinger::EffectHandle::~EffectHandle()
1154{
1155 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001156 disconnect(false);
1157}
1158
Glenn Kastene75da402013-11-20 13:54:52 -08001159status_t AudioFlinger::EffectHandle::initCheck()
1160{
1161 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1162}
1163
Eric Laurentca7cc822012-11-19 14:55:58 -08001164status_t AudioFlinger::EffectHandle::enable()
1165{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001166 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001167 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001168 sp<EffectModule> effect = mEffect.promote();
1169 if (effect == 0 || mDisconnected) {
1170 return DEAD_OBJECT;
1171 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001172 if (!mHasControl) {
1173 return INVALID_OPERATION;
1174 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001175
1176 if (mEnabled) {
1177 return NO_ERROR;
1178 }
1179
1180 mEnabled = true;
1181
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001182 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001183 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001184 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001185 }
1186
1187 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001188 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001189 return NO_ERROR;
1190 }
1191
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001192 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001193 if (status != NO_ERROR) {
1194 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001195 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001196 }
1197 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001198 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001199 if (thread != 0) {
1200 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001201 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001202 Mutex::Autolock _l(t->mLock);
1203 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001204 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001205 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001206 if (thread->type() == ThreadBase::OFFLOAD) {
1207 PlaybackThread *t = (PlaybackThread *)thread.get();
1208 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1209 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001210 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001211 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1212 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001213 }
1214 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001215 }
1216 return status;
1217}
1218
1219status_t AudioFlinger::EffectHandle::disable()
1220{
1221 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001222 AutoMutex _l(mLock);
1223 sp<EffectModule> effect = mEffect.promote();
1224 if (effect == 0 || mDisconnected) {
1225 return DEAD_OBJECT;
1226 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001227 if (!mHasControl) {
1228 return INVALID_OPERATION;
1229 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001230
1231 if (!mEnabled) {
1232 return NO_ERROR;
1233 }
1234 mEnabled = false;
1235
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001236 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001237 return NO_ERROR;
1238 }
1239
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001240 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001241
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001242 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001243 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001244 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001245 if (thread->type() == ThreadBase::OFFLOAD) {
1246 PlaybackThread *t = (PlaybackThread *)thread.get();
1247 Mutex::Autolock _l(t->mLock);
1248 t->broadcast_l();
1249 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001250 }
1251
1252 return status;
1253}
1254
1255void AudioFlinger::EffectHandle::disconnect()
1256{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001257 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001258 disconnect(true);
1259}
1260
1261void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1262{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001263 AutoMutex _l(mLock);
1264 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1265 if (mDisconnected) {
1266 if (unpinIfLast) {
1267 android_errorWriteLog(0x534e4554, "32707507");
1268 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001269 return;
1270 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001271 mDisconnected = true;
1272 sp<ThreadBase> thread;
1273 {
1274 sp<EffectModule> effect = mEffect.promote();
1275 if (effect != 0) {
1276 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001277 }
1278 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001279 if (thread != 0) {
1280 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001281 } else {
1282 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1283 // try to cleanup as much as we can
1284 sp<EffectModule> effect = mEffect.promote();
1285 if (effect != 0) {
1286 effect->disconnectHandle(this, unpinIfLast);
1287 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001288 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001289
Eric Laurentca7cc822012-11-19 14:55:58 -08001290 if (mClient != 0) {
1291 if (mCblk != NULL) {
1292 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1293 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1294 }
1295 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001296 // Client destructor must run with AudioFlinger client mutex locked
1297 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001298 mClient.clear();
1299 }
1300}
1301
1302status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1303 uint32_t cmdSize,
1304 void *pCmdData,
1305 uint32_t *replySize,
1306 void *pReplyData)
1307{
1308 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001309 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001310
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001311 AutoMutex _l(mLock);
1312 sp<EffectModule> effect = mEffect.promote();
1313 if (effect == 0 || mDisconnected) {
1314 return DEAD_OBJECT;
1315 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001316 // only get parameter command is permitted for applications not controlling the effect
1317 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1318 return INVALID_OPERATION;
1319 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001320 if (mClient == 0) {
1321 return INVALID_OPERATION;
1322 }
1323
1324 // handle commands that are not forwarded transparently to effect engine
1325 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1326 // No need to trylock() here as this function is executed in the binder thread serving a
1327 // particular client process: no risk to block the whole media server process or mixer
1328 // threads if we are stuck here
1329 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001330
1331 // keep local copy of index in case of client corruption b/32220769
1332 const uint32_t clientIndex = mCblk->clientIndex;
1333 const uint32_t serverIndex = mCblk->serverIndex;
1334 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1335 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001336 mCblk->serverIndex = 0;
1337 mCblk->clientIndex = 0;
1338 return BAD_VALUE;
1339 }
1340 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001341 effect_param_t *param = NULL;
1342 for (uint32_t index = serverIndex; index < clientIndex;) {
1343 int *p = (int *)(mBuffer + index);
1344 const int size = *p++;
1345 if (size < 0
1346 || size > EFFECT_PARAM_BUFFER_SIZE
1347 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001348 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001349 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001350 break;
1351 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001352
1353 // copy to local memory in case of client corruption b/32220769
1354 param = (effect_param_t *)realloc(param, size);
1355 if (param == NULL) {
1356 ALOGW("command(): out of memory");
1357 status = NO_MEMORY;
1358 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001359 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001360 memcpy(param, p, size);
1361
1362 int reply = 0;
1363 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001364 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001365 size,
1366 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001367 &rsize,
1368 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001369
1370 // verify shared memory: server index shouldn't change; client index can't go back.
1371 if (serverIndex != mCblk->serverIndex
1372 || clientIndex > mCblk->clientIndex) {
1373 android_errorWriteLog(0x534e4554, "32220769");
1374 status = BAD_VALUE;
1375 break;
1376 }
1377
Eric Laurentca7cc822012-11-19 14:55:58 -08001378 // stop at first error encountered
1379 if (ret != NO_ERROR) {
1380 status = ret;
1381 *(int *)pReplyData = reply;
1382 break;
1383 } else if (reply != NO_ERROR) {
1384 *(int *)pReplyData = reply;
1385 break;
1386 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001387 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001388 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001389 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001390 mCblk->serverIndex = 0;
1391 mCblk->clientIndex = 0;
1392 return status;
1393 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1394 *(int *)pReplyData = NO_ERROR;
1395 return enable();
1396 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1397 *(int *)pReplyData = NO_ERROR;
1398 return disable();
1399 }
1400
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001401 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001402}
1403
1404void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1405{
1406 ALOGV("setControl %p control %d", this, hasControl);
1407
1408 mHasControl = hasControl;
1409 mEnabled = enabled;
1410
1411 if (signal && mEffectClient != 0) {
1412 mEffectClient->controlStatusChanged(hasControl);
1413 }
1414}
1415
1416void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1417 uint32_t cmdSize,
1418 void *pCmdData,
1419 uint32_t replySize,
1420 void *pReplyData)
1421{
1422 if (mEffectClient != 0) {
1423 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1424 }
1425}
1426
1427
1428
1429void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1430{
1431 if (mEffectClient != 0) {
1432 mEffectClient->enableStatusChanged(enabled);
1433 }
1434}
1435
1436status_t AudioFlinger::EffectHandle::onTransact(
1437 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1438{
1439 return BnEffect::onTransact(code, data, reply, flags);
1440}
1441
1442
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001443void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001444{
1445 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1446
Marco Nelissenb2208842014-02-07 14:00:50 -08001447 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001448 (mClient == 0) ? getpid_cached : mClient->pid(),
1449 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001450 mHasControl ? "yes" : "no",
1451 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001452 mCblk ? mCblk->clientIndex : 0,
1453 mCblk ? mCblk->serverIndex : 0
1454 );
1455
1456 if (locked) {
1457 mCblk->lock.unlock();
1458 }
1459}
1460
1461#undef LOG_TAG
1462#define LOG_TAG "AudioFlinger::EffectChain"
1463
1464AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001465 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001466 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1467 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001468 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001469{
1470 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1471 if (thread == NULL) {
1472 return;
1473 }
1474 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1475 thread->frameCount();
1476}
1477
1478AudioFlinger::EffectChain::~EffectChain()
1479{
1480 if (mOwnInBuffer) {
1481 delete mInBuffer;
1482 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001483}
1484
1485// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1486sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1487 effect_descriptor_t *descriptor)
1488{
1489 size_t size = mEffects.size();
1490
1491 for (size_t i = 0; i < size; i++) {
1492 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1493 return mEffects[i];
1494 }
1495 }
1496 return 0;
1497}
1498
1499// getEffectFromId_l() must be called with ThreadBase::mLock held
1500sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1501{
1502 size_t size = mEffects.size();
1503
1504 for (size_t i = 0; i < size; i++) {
1505 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1506 if (id == 0 || mEffects[i]->id() == id) {
1507 return mEffects[i];
1508 }
1509 }
1510 return 0;
1511}
1512
1513// getEffectFromType_l() must be called with ThreadBase::mLock held
1514sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1515 const effect_uuid_t *type)
1516{
1517 size_t size = mEffects.size();
1518
1519 for (size_t i = 0; i < size; i++) {
1520 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1521 return mEffects[i];
1522 }
1523 }
1524 return 0;
1525}
1526
1527void AudioFlinger::EffectChain::clearInputBuffer()
1528{
1529 Mutex::Autolock _l(mLock);
1530 sp<ThreadBase> thread = mThread.promote();
1531 if (thread == 0) {
1532 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1533 return;
1534 }
1535 clearInputBuffer_l(thread);
1536}
1537
1538// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001539void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001540{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001541 // TODO: This will change in the future, depending on multichannel
1542 // and sample format changes for effects.
1543 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1544 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001545 const size_t frameSize =
1546 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001547 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001548}
1549
1550// Must be called with EffectChain::mLock locked
1551void AudioFlinger::EffectChain::process_l()
1552{
1553 sp<ThreadBase> thread = mThread.promote();
1554 if (thread == 0) {
1555 ALOGW("process_l(): cannot promote mixer thread");
1556 return;
1557 }
1558 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1559 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001560 // never process effects when:
1561 // - on an OFFLOAD thread
1562 // - no more tracks are on the session and the effect tail has been rendered
1563 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001564 if (!isGlobalSession) {
1565 bool tracksOnSession = (trackCnt() != 0);
1566
1567 if (!tracksOnSession && mTailBufferCount == 0) {
1568 doProcess = false;
1569 }
1570
1571 if (activeTrackCnt() == 0) {
1572 // if no track is active and the effect tail has not been rendered,
1573 // the input buffer must be cleared here as the mixer process will not do it
1574 if (tracksOnSession || mTailBufferCount > 0) {
1575 clearInputBuffer_l(thread);
1576 if (mTailBufferCount > 0) {
1577 mTailBufferCount--;
1578 }
1579 }
1580 }
1581 }
1582
1583 size_t size = mEffects.size();
1584 if (doProcess) {
1585 for (size_t i = 0; i < size; i++) {
1586 mEffects[i]->process();
1587 }
1588 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001589 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001590 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001591 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1592 }
1593 if (doResetVolume) {
1594 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001595 }
1596}
1597
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001598// createEffect_l() must be called with ThreadBase::mLock held
1599status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1600 ThreadBase *thread,
1601 effect_descriptor_t *desc,
1602 int id,
1603 audio_session_t sessionId,
1604 bool pinned)
1605{
1606 Mutex::Autolock _l(mLock);
1607 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1608 status_t lStatus = effect->status();
1609 if (lStatus == NO_ERROR) {
1610 lStatus = addEffect_ll(effect);
1611 }
1612 if (lStatus != NO_ERROR) {
1613 effect.clear();
1614 }
1615 return lStatus;
1616}
1617
1618// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001619status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1620{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001621 Mutex::Autolock _l(mLock);
1622 return addEffect_ll(effect);
1623}
1624// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1625status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1626{
Eric Laurentca7cc822012-11-19 14:55:58 -08001627 effect_descriptor_t desc = effect->desc();
1628 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1629
Eric Laurentca7cc822012-11-19 14:55:58 -08001630 effect->setChain(this);
1631 sp<ThreadBase> thread = mThread.promote();
1632 if (thread == 0) {
1633 return NO_INIT;
1634 }
1635 effect->setThread(thread);
1636
1637 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1638 // Auxiliary effects are inserted at the beginning of mEffects vector as
1639 // they are processed first and accumulated in chain input buffer
1640 mEffects.insertAt(effect, 0);
1641
1642 // the input buffer for auxiliary effect contains mono samples in
1643 // 32 bit format. This is to avoid saturation in AudoMixer
1644 // accumulation stage. Saturation is done in EffectModule::process() before
1645 // calling the process in effect engine
1646 size_t numSamples = thread->frameCount();
1647 int32_t *buffer = new int32_t[numSamples];
1648 memset(buffer, 0, numSamples * sizeof(int32_t));
1649 effect->setInBuffer((int16_t *)buffer);
1650 // auxiliary effects output samples to chain input buffer for further processing
1651 // by insert effects
1652 effect->setOutBuffer(mInBuffer);
1653 } else {
1654 // Insert effects are inserted at the end of mEffects vector as they are processed
1655 // after track and auxiliary effects.
1656 // Insert effect order as a function of indicated preference:
1657 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1658 // another effect is present
1659 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1660 // last effect claiming first position
1661 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1662 // first effect claiming last position
1663 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1664 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1665 // already present
1666
1667 size_t size = mEffects.size();
1668 size_t idx_insert = size;
1669 ssize_t idx_insert_first = -1;
1670 ssize_t idx_insert_last = -1;
1671
1672 for (size_t i = 0; i < size; i++) {
1673 effect_descriptor_t d = mEffects[i]->desc();
1674 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1675 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1676 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1677 // check invalid effect chaining combinations
1678 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1679 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1680 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1681 desc.name, d.name);
1682 return INVALID_OPERATION;
1683 }
1684 // remember position of first insert effect and by default
1685 // select this as insert position for new effect
1686 if (idx_insert == size) {
1687 idx_insert = i;
1688 }
1689 // remember position of last insert effect claiming
1690 // first position
1691 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1692 idx_insert_first = i;
1693 }
1694 // remember position of first insert effect claiming
1695 // last position
1696 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1697 idx_insert_last == -1) {
1698 idx_insert_last = i;
1699 }
1700 }
1701 }
1702
1703 // modify idx_insert from first position if needed
1704 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1705 if (idx_insert_last != -1) {
1706 idx_insert = idx_insert_last;
1707 } else {
1708 idx_insert = size;
1709 }
1710 } else {
1711 if (idx_insert_first != -1) {
1712 idx_insert = idx_insert_first + 1;
1713 }
1714 }
1715
1716 // always read samples from chain input buffer
1717 effect->setInBuffer(mInBuffer);
1718
1719 // if last effect in the chain, output samples to chain
1720 // output buffer, otherwise to chain input buffer
1721 if (idx_insert == size) {
1722 if (idx_insert != 0) {
1723 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1724 mEffects[idx_insert-1]->configure();
1725 }
1726 effect->setOutBuffer(mOutBuffer);
1727 } else {
1728 effect->setOutBuffer(mInBuffer);
1729 }
1730 mEffects.insertAt(effect, idx_insert);
1731
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001732 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001733 idx_insert);
1734 }
1735 effect->configure();
1736 return NO_ERROR;
1737}
1738
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001739// removeEffect_l() must be called with ThreadBase::mLock held
1740size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1741 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001742{
1743 Mutex::Autolock _l(mLock);
1744 size_t size = mEffects.size();
1745 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1746
1747 for (size_t i = 0; i < size; i++) {
1748 if (effect == mEffects[i]) {
1749 // calling stop here will remove pre-processing effect from the audio HAL.
1750 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1751 // the middle of a read from audio HAL
1752 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1753 mEffects[i]->state() == EffectModule::STOPPING) {
1754 mEffects[i]->stop();
1755 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001756 if (release) {
1757 mEffects[i]->release_l();
1758 }
1759
Eric Laurentca7cc822012-11-19 14:55:58 -08001760 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1761 delete[] effect->inBuffer();
1762 } else {
1763 if (i == size - 1 && i != 0) {
1764 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1765 mEffects[i - 1]->configure();
1766 }
1767 }
1768 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001769 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001770 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001771
Eric Laurentca7cc822012-11-19 14:55:58 -08001772 break;
1773 }
1774 }
1775
1776 return mEffects.size();
1777}
1778
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001779// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001780void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1781{
1782 size_t size = mEffects.size();
1783 for (size_t i = 0; i < size; i++) {
1784 mEffects[i]->setDevice(device);
1785 }
1786}
1787
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001788// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001789void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1790{
1791 size_t size = mEffects.size();
1792 for (size_t i = 0; i < size; i++) {
1793 mEffects[i]->setMode(mode);
1794 }
1795}
1796
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001797// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001798void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1799{
1800 size_t size = mEffects.size();
1801 for (size_t i = 0; i < size; i++) {
1802 mEffects[i]->setAudioSource(source);
1803 }
1804}
1805
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001806// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001807bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001808{
1809 uint32_t newLeft = *left;
1810 uint32_t newRight = *right;
1811 bool hasControl = false;
1812 int ctrlIdx = -1;
1813 size_t size = mEffects.size();
1814
1815 // first update volume controller
1816 for (size_t i = size; i > 0; i--) {
1817 if (mEffects[i - 1]->isProcessEnabled() &&
1818 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1819 ctrlIdx = i - 1;
1820 hasControl = true;
1821 break;
1822 }
1823 }
1824
Eric Laurentfa1e1232016-08-02 19:01:49 -07001825 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001826 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001827 if (hasControl) {
1828 *left = mNewLeftVolume;
1829 *right = mNewRightVolume;
1830 }
1831 return hasControl;
1832 }
1833
1834 mVolumeCtrlIdx = ctrlIdx;
1835 mLeftVolume = newLeft;
1836 mRightVolume = newRight;
1837
1838 // second get volume update from volume controller
1839 if (ctrlIdx >= 0) {
1840 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1841 mNewLeftVolume = newLeft;
1842 mNewRightVolume = newRight;
1843 }
1844 // then indicate volume to all other effects in chain.
1845 // Pass altered volume to effects before volume controller
1846 // and requested volume to effects after controller
1847 uint32_t lVol = newLeft;
1848 uint32_t rVol = newRight;
1849
1850 for (size_t i = 0; i < size; i++) {
1851 if ((int)i == ctrlIdx) {
1852 continue;
1853 }
1854 // this also works for ctrlIdx == -1 when there is no volume controller
1855 if ((int)i > ctrlIdx) {
1856 lVol = *left;
1857 rVol = *right;
1858 }
1859 mEffects[i]->setVolume(&lVol, &rVol, false);
1860 }
1861 *left = newLeft;
1862 *right = newRight;
1863
1864 return hasControl;
1865}
1866
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001867// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001868void AudioFlinger::EffectChain::resetVolume_l()
1869{
Eric Laurente7449bf2016-08-03 18:44:07 -07001870 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1871 uint32_t left = mLeftVolume;
1872 uint32_t right = mRightVolume;
1873 (void)setVolume_l(&left, &right, true);
1874 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001875}
1876
Eric Laurent1b928682014-10-02 19:41:47 -07001877void AudioFlinger::EffectChain::syncHalEffectsState()
1878{
1879 Mutex::Autolock _l(mLock);
1880 for (size_t i = 0; i < mEffects.size(); i++) {
1881 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1882 mEffects[i]->state() == EffectModule::STOPPING) {
1883 mEffects[i]->addEffectToHal_l();
1884 }
1885 }
1886}
1887
Eric Laurentca7cc822012-11-19 14:55:58 -08001888void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1889{
1890 const size_t SIZE = 256;
1891 char buffer[SIZE];
1892 String8 result;
1893
Marco Nelissenb2208842014-02-07 14:00:50 -08001894 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001895 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001896 result.append(buffer);
1897
Marco Nelissenb2208842014-02-07 14:00:50 -08001898 if (numEffects) {
1899 bool locked = AudioFlinger::dumpTryLock(mLock);
1900 // failed to lock - AudioFlinger is probably deadlocked
1901 if (!locked) {
1902 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001903 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001904
Marco Nelissenb2208842014-02-07 14:00:50 -08001905 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001906 snprintf(buffer, SIZE, "\t%p %p %d\n",
1907 mInBuffer,
1908 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001909 mActiveTrackCnt);
1910 result.append(buffer);
1911 write(fd, result.string(), result.size());
1912
1913 for (size_t i = 0; i < numEffects; ++i) {
1914 sp<EffectModule> effect = mEffects[i];
1915 if (effect != 0) {
1916 effect->dump(fd, args);
1917 }
1918 }
1919
1920 if (locked) {
1921 mLock.unlock();
1922 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001923 }
1924}
1925
1926// must be called with ThreadBase::mLock held
1927void AudioFlinger::EffectChain::setEffectSuspended_l(
1928 const effect_uuid_t *type, bool suspend)
1929{
1930 sp<SuspendedEffectDesc> desc;
1931 // use effect type UUID timelow as key as there is no real risk of identical
1932 // timeLow fields among effect type UUIDs.
1933 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1934 if (suspend) {
1935 if (index >= 0) {
1936 desc = mSuspendedEffects.valueAt(index);
1937 } else {
1938 desc = new SuspendedEffectDesc();
1939 desc->mType = *type;
1940 mSuspendedEffects.add(type->timeLow, desc);
1941 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1942 }
1943 if (desc->mRefCount++ == 0) {
1944 sp<EffectModule> effect = getEffectIfEnabled(type);
1945 if (effect != 0) {
1946 desc->mEffect = effect;
1947 effect->setSuspended(true);
1948 effect->setEnabled(false);
1949 }
1950 }
1951 } else {
1952 if (index < 0) {
1953 return;
1954 }
1955 desc = mSuspendedEffects.valueAt(index);
1956 if (desc->mRefCount <= 0) {
1957 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1958 desc->mRefCount = 1;
1959 }
1960 if (--desc->mRefCount == 0) {
1961 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1962 if (desc->mEffect != 0) {
1963 sp<EffectModule> effect = desc->mEffect.promote();
1964 if (effect != 0) {
1965 effect->setSuspended(false);
1966 effect->lock();
1967 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001968 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001969 effect->setEnabled_l(handle->enabled());
1970 }
1971 effect->unlock();
1972 }
1973 desc->mEffect.clear();
1974 }
1975 mSuspendedEffects.removeItemsAt(index);
1976 }
1977 }
1978}
1979
1980// must be called with ThreadBase::mLock held
1981void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1982{
1983 sp<SuspendedEffectDesc> desc;
1984
1985 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1986 if (suspend) {
1987 if (index >= 0) {
1988 desc = mSuspendedEffects.valueAt(index);
1989 } else {
1990 desc = new SuspendedEffectDesc();
1991 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1992 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1993 }
1994 if (desc->mRefCount++ == 0) {
1995 Vector< sp<EffectModule> > effects;
1996 getSuspendEligibleEffects(effects);
1997 for (size_t i = 0; i < effects.size(); i++) {
1998 setEffectSuspended_l(&effects[i]->desc().type, true);
1999 }
2000 }
2001 } else {
2002 if (index < 0) {
2003 return;
2004 }
2005 desc = mSuspendedEffects.valueAt(index);
2006 if (desc->mRefCount <= 0) {
2007 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2008 desc->mRefCount = 1;
2009 }
2010 if (--desc->mRefCount == 0) {
2011 Vector<const effect_uuid_t *> types;
2012 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2013 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2014 continue;
2015 }
2016 types.add(&mSuspendedEffects.valueAt(i)->mType);
2017 }
2018 for (size_t i = 0; i < types.size(); i++) {
2019 setEffectSuspended_l(types[i], false);
2020 }
2021 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2022 mSuspendedEffects.keyAt(index));
2023 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2024 }
2025 }
2026}
2027
2028
2029// The volume effect is used for automated tests only
2030#ifndef OPENSL_ES_H_
2031static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2032 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2033const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2034#endif //OPENSL_ES_H_
2035
2036bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2037{
2038 // auxiliary effects and visualizer are never suspended on output mix
2039 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2040 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2041 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2042 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2043 return false;
2044 }
2045 return true;
2046}
2047
2048void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2049 Vector< sp<AudioFlinger::EffectModule> > &effects)
2050{
2051 effects.clear();
2052 for (size_t i = 0; i < mEffects.size(); i++) {
2053 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2054 effects.add(mEffects[i]);
2055 }
2056 }
2057}
2058
2059sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2060 const effect_uuid_t *type)
2061{
2062 sp<EffectModule> effect = getEffectFromType_l(type);
2063 return effect != 0 && effect->isEnabled() ? effect : 0;
2064}
2065
2066void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2067 bool enabled)
2068{
2069 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2070 if (enabled) {
2071 if (index < 0) {
2072 // if the effect is not suspend check if all effects are suspended
2073 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2074 if (index < 0) {
2075 return;
2076 }
2077 if (!isEffectEligibleForSuspend(effect->desc())) {
2078 return;
2079 }
2080 setEffectSuspended_l(&effect->desc().type, enabled);
2081 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2082 if (index < 0) {
2083 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2084 return;
2085 }
2086 }
2087 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2088 effect->desc().type.timeLow);
2089 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2090 // if effect is requested to suspended but was not yet enabled, supend it now.
2091 if (desc->mEffect == 0) {
2092 desc->mEffect = effect;
2093 effect->setEnabled(false);
2094 effect->setSuspended(true);
2095 }
2096 } else {
2097 if (index < 0) {
2098 return;
2099 }
2100 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2101 effect->desc().type.timeLow);
2102 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2103 desc->mEffect.clear();
2104 effect->setSuspended(false);
2105 }
2106}
2107
Eric Laurent5baf2af2013-09-12 17:37:00 -07002108bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002109{
2110 Mutex::Autolock _l(mLock);
2111 size_t size = mEffects.size();
2112 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002113 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002114 return true;
2115 }
2116 }
2117 return false;
2118}
2119
Eric Laurentaaa44472014-09-12 17:41:50 -07002120void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2121{
2122 Mutex::Autolock _l(mLock);
2123 mThread = thread;
2124 for (size_t i = 0; i < mEffects.size(); i++) {
2125 mEffects[i]->setThread(thread);
2126 }
2127}
2128
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002129void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2130{
2131 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2132 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2133 }
2134 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2135 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2136 }
2137}
2138
2139void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2140{
2141 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2142 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2143 }
2144 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2145 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2146 }
2147}
2148
2149bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002150{
2151 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002152 for (const auto &effect : mEffects) {
2153 if (effect->isProcessImplemented()) {
2154 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002155 }
2156 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002157 // Allow effects without processing.
2158 return true;
2159}
2160
2161bool AudioFlinger::EffectChain::isFastCompatible() const
2162{
2163 Mutex::Autolock _l(mLock);
2164 for (const auto &effect : mEffects) {
2165 if (effect->isProcessImplemented()
2166 && effect->isImplementationSoftware()) {
2167 return false;
2168 }
2169 }
2170 // Allow effects without processing or hw accelerated effects.
2171 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002172}
2173
2174// isCompatibleWithThread_l() must be called with thread->mLock held
2175bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2176{
2177 Mutex::Autolock _l(mLock);
2178 for (size_t i = 0; i < mEffects.size(); i++) {
2179 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2180 return false;
2181 }
2182 }
2183 return true;
2184}
2185
Glenn Kasten63238ef2015-03-02 15:50:29 -08002186} // namespace android