blob: 6c937a55409073c43157dfccb3d2b4268800dd56 [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 Laurent1ffc5852016-12-15 14:46:09 -08001311 if (cmdCode == EFFECT_CMD_ENABLE) {
1312 if (*replySize < sizeof(int)) {
1313 android_errorWriteLog(0x534e4554, "32095713");
1314 return BAD_VALUE;
1315 }
1316 *(int *)pReplyData = NO_ERROR;
1317 *replySize = sizeof(int);
1318 return enable();
1319 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1320 if (*replySize < sizeof(int)) {
1321 android_errorWriteLog(0x534e4554, "32095713");
1322 return BAD_VALUE;
1323 }
1324 *(int *)pReplyData = NO_ERROR;
1325 *replySize = sizeof(int);
1326 return disable();
1327 }
1328
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001329 AutoMutex _l(mLock);
1330 sp<EffectModule> effect = mEffect.promote();
1331 if (effect == 0 || mDisconnected) {
1332 return DEAD_OBJECT;
1333 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001334 // only get parameter command is permitted for applications not controlling the effect
1335 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1336 return INVALID_OPERATION;
1337 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001338 if (mClient == 0) {
1339 return INVALID_OPERATION;
1340 }
1341
1342 // handle commands that are not forwarded transparently to effect engine
1343 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001344 if (*replySize < sizeof(int)) {
1345 android_errorWriteLog(0x534e4554, "32095713");
1346 return BAD_VALUE;
1347 }
1348 *(int *)pReplyData = NO_ERROR;
1349 *replySize = sizeof(int);
1350
Eric Laurentca7cc822012-11-19 14:55:58 -08001351 // No need to trylock() here as this function is executed in the binder thread serving a
1352 // particular client process: no risk to block the whole media server process or mixer
1353 // threads if we are stuck here
1354 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001355 // keep local copy of index in case of client corruption b/32220769
1356 const uint32_t clientIndex = mCblk->clientIndex;
1357 const uint32_t serverIndex = mCblk->serverIndex;
1358 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1359 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001360 mCblk->serverIndex = 0;
1361 mCblk->clientIndex = 0;
1362 return BAD_VALUE;
1363 }
1364 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001365 effect_param_t *param = NULL;
1366 for (uint32_t index = serverIndex; index < clientIndex;) {
1367 int *p = (int *)(mBuffer + index);
1368 const int size = *p++;
1369 if (size < 0
1370 || size > EFFECT_PARAM_BUFFER_SIZE
1371 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001372 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001373 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001374 break;
1375 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001376
1377 // copy to local memory in case of client corruption b/32220769
1378 param = (effect_param_t *)realloc(param, size);
1379 if (param == NULL) {
1380 ALOGW("command(): out of memory");
1381 status = NO_MEMORY;
1382 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001383 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001384 memcpy(param, p, size);
1385
1386 int reply = 0;
1387 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001388 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001389 size,
1390 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001391 &rsize,
1392 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001393
1394 // verify shared memory: server index shouldn't change; client index can't go back.
1395 if (serverIndex != mCblk->serverIndex
1396 || clientIndex > mCblk->clientIndex) {
1397 android_errorWriteLog(0x534e4554, "32220769");
1398 status = BAD_VALUE;
1399 break;
1400 }
1401
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 // stop at first error encountered
1403 if (ret != NO_ERROR) {
1404 status = ret;
1405 *(int *)pReplyData = reply;
1406 break;
1407 } else if (reply != NO_ERROR) {
1408 *(int *)pReplyData = reply;
1409 break;
1410 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001411 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001412 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001413 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 mCblk->serverIndex = 0;
1415 mCblk->clientIndex = 0;
1416 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 }
1418
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001419 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001420}
1421
1422void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1423{
1424 ALOGV("setControl %p control %d", this, hasControl);
1425
1426 mHasControl = hasControl;
1427 mEnabled = enabled;
1428
1429 if (signal && mEffectClient != 0) {
1430 mEffectClient->controlStatusChanged(hasControl);
1431 }
1432}
1433
1434void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1435 uint32_t cmdSize,
1436 void *pCmdData,
1437 uint32_t replySize,
1438 void *pReplyData)
1439{
1440 if (mEffectClient != 0) {
1441 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1442 }
1443}
1444
1445
1446
1447void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1448{
1449 if (mEffectClient != 0) {
1450 mEffectClient->enableStatusChanged(enabled);
1451 }
1452}
1453
1454status_t AudioFlinger::EffectHandle::onTransact(
1455 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1456{
1457 return BnEffect::onTransact(code, data, reply, flags);
1458}
1459
1460
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001461void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001462{
1463 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1464
Marco Nelissenb2208842014-02-07 14:00:50 -08001465 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001466 (mClient == 0) ? getpid_cached : mClient->pid(),
1467 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001468 mHasControl ? "yes" : "no",
1469 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001470 mCblk ? mCblk->clientIndex : 0,
1471 mCblk ? mCblk->serverIndex : 0
1472 );
1473
1474 if (locked) {
1475 mCblk->lock.unlock();
1476 }
1477}
1478
1479#undef LOG_TAG
1480#define LOG_TAG "AudioFlinger::EffectChain"
1481
1482AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001483 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1485 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001486 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001487{
1488 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1489 if (thread == NULL) {
1490 return;
1491 }
1492 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1493 thread->frameCount();
1494}
1495
1496AudioFlinger::EffectChain::~EffectChain()
1497{
1498 if (mOwnInBuffer) {
Mikhail Naganov0ce91632016-12-27 10:50:15 -08001499 delete[] mInBuffer;
Eric Laurentca7cc822012-11-19 14:55:58 -08001500 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001501}
1502
1503// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1504sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1505 effect_descriptor_t *descriptor)
1506{
1507 size_t size = mEffects.size();
1508
1509 for (size_t i = 0; i < size; i++) {
1510 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1511 return mEffects[i];
1512 }
1513 }
1514 return 0;
1515}
1516
1517// getEffectFromId_l() must be called with ThreadBase::mLock held
1518sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1519{
1520 size_t size = mEffects.size();
1521
1522 for (size_t i = 0; i < size; i++) {
1523 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1524 if (id == 0 || mEffects[i]->id() == id) {
1525 return mEffects[i];
1526 }
1527 }
1528 return 0;
1529}
1530
1531// getEffectFromType_l() must be called with ThreadBase::mLock held
1532sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1533 const effect_uuid_t *type)
1534{
1535 size_t size = mEffects.size();
1536
1537 for (size_t i = 0; i < size; i++) {
1538 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1539 return mEffects[i];
1540 }
1541 }
1542 return 0;
1543}
1544
1545void AudioFlinger::EffectChain::clearInputBuffer()
1546{
1547 Mutex::Autolock _l(mLock);
1548 sp<ThreadBase> thread = mThread.promote();
1549 if (thread == 0) {
1550 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1551 return;
1552 }
1553 clearInputBuffer_l(thread);
1554}
1555
1556// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001557void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001558{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001559 // TODO: This will change in the future, depending on multichannel
1560 // and sample format changes for effects.
1561 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1562 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001563 const size_t frameSize =
1564 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001565 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001566}
1567
1568// Must be called with EffectChain::mLock locked
1569void AudioFlinger::EffectChain::process_l()
1570{
1571 sp<ThreadBase> thread = mThread.promote();
1572 if (thread == 0) {
1573 ALOGW("process_l(): cannot promote mixer thread");
1574 return;
1575 }
1576 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1577 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001578 // never process effects when:
1579 // - on an OFFLOAD thread
1580 // - no more tracks are on the session and the effect tail has been rendered
1581 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001582 if (!isGlobalSession) {
1583 bool tracksOnSession = (trackCnt() != 0);
1584
1585 if (!tracksOnSession && mTailBufferCount == 0) {
1586 doProcess = false;
1587 }
1588
1589 if (activeTrackCnt() == 0) {
1590 // if no track is active and the effect tail has not been rendered,
1591 // the input buffer must be cleared here as the mixer process will not do it
1592 if (tracksOnSession || mTailBufferCount > 0) {
1593 clearInputBuffer_l(thread);
1594 if (mTailBufferCount > 0) {
1595 mTailBufferCount--;
1596 }
1597 }
1598 }
1599 }
1600
1601 size_t size = mEffects.size();
1602 if (doProcess) {
1603 for (size_t i = 0; i < size; i++) {
1604 mEffects[i]->process();
1605 }
1606 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001607 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001608 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001609 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1610 }
1611 if (doResetVolume) {
1612 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001613 }
1614}
1615
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001616// createEffect_l() must be called with ThreadBase::mLock held
1617status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1618 ThreadBase *thread,
1619 effect_descriptor_t *desc,
1620 int id,
1621 audio_session_t sessionId,
1622 bool pinned)
1623{
1624 Mutex::Autolock _l(mLock);
1625 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1626 status_t lStatus = effect->status();
1627 if (lStatus == NO_ERROR) {
1628 lStatus = addEffect_ll(effect);
1629 }
1630 if (lStatus != NO_ERROR) {
1631 effect.clear();
1632 }
1633 return lStatus;
1634}
1635
1636// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001637status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1638{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001639 Mutex::Autolock _l(mLock);
1640 return addEffect_ll(effect);
1641}
1642// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1643status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1644{
Eric Laurentca7cc822012-11-19 14:55:58 -08001645 effect_descriptor_t desc = effect->desc();
1646 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1647
Eric Laurentca7cc822012-11-19 14:55:58 -08001648 effect->setChain(this);
1649 sp<ThreadBase> thread = mThread.promote();
1650 if (thread == 0) {
1651 return NO_INIT;
1652 }
1653 effect->setThread(thread);
1654
1655 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1656 // Auxiliary effects are inserted at the beginning of mEffects vector as
1657 // they are processed first and accumulated in chain input buffer
1658 mEffects.insertAt(effect, 0);
1659
1660 // the input buffer for auxiliary effect contains mono samples in
1661 // 32 bit format. This is to avoid saturation in AudoMixer
1662 // accumulation stage. Saturation is done in EffectModule::process() before
1663 // calling the process in effect engine
1664 size_t numSamples = thread->frameCount();
1665 int32_t *buffer = new int32_t[numSamples];
1666 memset(buffer, 0, numSamples * sizeof(int32_t));
1667 effect->setInBuffer((int16_t *)buffer);
1668 // auxiliary effects output samples to chain input buffer for further processing
1669 // by insert effects
1670 effect->setOutBuffer(mInBuffer);
1671 } else {
1672 // Insert effects are inserted at the end of mEffects vector as they are processed
1673 // after track and auxiliary effects.
1674 // Insert effect order as a function of indicated preference:
1675 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1676 // another effect is present
1677 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1678 // last effect claiming first position
1679 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1680 // first effect claiming last position
1681 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1682 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1683 // already present
1684
1685 size_t size = mEffects.size();
1686 size_t idx_insert = size;
1687 ssize_t idx_insert_first = -1;
1688 ssize_t idx_insert_last = -1;
1689
1690 for (size_t i = 0; i < size; i++) {
1691 effect_descriptor_t d = mEffects[i]->desc();
1692 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1693 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1694 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1695 // check invalid effect chaining combinations
1696 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1697 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1698 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1699 desc.name, d.name);
1700 return INVALID_OPERATION;
1701 }
1702 // remember position of first insert effect and by default
1703 // select this as insert position for new effect
1704 if (idx_insert == size) {
1705 idx_insert = i;
1706 }
1707 // remember position of last insert effect claiming
1708 // first position
1709 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1710 idx_insert_first = i;
1711 }
1712 // remember position of first insert effect claiming
1713 // last position
1714 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1715 idx_insert_last == -1) {
1716 idx_insert_last = i;
1717 }
1718 }
1719 }
1720
1721 // modify idx_insert from first position if needed
1722 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1723 if (idx_insert_last != -1) {
1724 idx_insert = idx_insert_last;
1725 } else {
1726 idx_insert = size;
1727 }
1728 } else {
1729 if (idx_insert_first != -1) {
1730 idx_insert = idx_insert_first + 1;
1731 }
1732 }
1733
1734 // always read samples from chain input buffer
1735 effect->setInBuffer(mInBuffer);
1736
1737 // if last effect in the chain, output samples to chain
1738 // output buffer, otherwise to chain input buffer
1739 if (idx_insert == size) {
1740 if (idx_insert != 0) {
1741 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1742 mEffects[idx_insert-1]->configure();
1743 }
1744 effect->setOutBuffer(mOutBuffer);
1745 } else {
1746 effect->setOutBuffer(mInBuffer);
1747 }
1748 mEffects.insertAt(effect, idx_insert);
1749
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001750 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001751 idx_insert);
1752 }
1753 effect->configure();
1754 return NO_ERROR;
1755}
1756
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001757// removeEffect_l() must be called with ThreadBase::mLock held
1758size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1759 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001760{
1761 Mutex::Autolock _l(mLock);
1762 size_t size = mEffects.size();
1763 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1764
1765 for (size_t i = 0; i < size; i++) {
1766 if (effect == mEffects[i]) {
1767 // calling stop here will remove pre-processing effect from the audio HAL.
1768 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1769 // the middle of a read from audio HAL
1770 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1771 mEffects[i]->state() == EffectModule::STOPPING) {
1772 mEffects[i]->stop();
1773 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001774 if (release) {
1775 mEffects[i]->release_l();
1776 }
1777
Eric Laurentca7cc822012-11-19 14:55:58 -08001778 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1779 delete[] effect->inBuffer();
1780 } else {
1781 if (i == size - 1 && i != 0) {
1782 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1783 mEffects[i - 1]->configure();
1784 }
1785 }
1786 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001787 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001788 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001789
Eric Laurentca7cc822012-11-19 14:55:58 -08001790 break;
1791 }
1792 }
1793
1794 return mEffects.size();
1795}
1796
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001797// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001798void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1799{
1800 size_t size = mEffects.size();
1801 for (size_t i = 0; i < size; i++) {
1802 mEffects[i]->setDevice(device);
1803 }
1804}
1805
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001806// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001807void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1808{
1809 size_t size = mEffects.size();
1810 for (size_t i = 0; i < size; i++) {
1811 mEffects[i]->setMode(mode);
1812 }
1813}
1814
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001815// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001816void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1817{
1818 size_t size = mEffects.size();
1819 for (size_t i = 0; i < size; i++) {
1820 mEffects[i]->setAudioSource(source);
1821 }
1822}
1823
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001824// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001825bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001826{
1827 uint32_t newLeft = *left;
1828 uint32_t newRight = *right;
1829 bool hasControl = false;
1830 int ctrlIdx = -1;
1831 size_t size = mEffects.size();
1832
1833 // first update volume controller
1834 for (size_t i = size; i > 0; i--) {
1835 if (mEffects[i - 1]->isProcessEnabled() &&
1836 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1837 ctrlIdx = i - 1;
1838 hasControl = true;
1839 break;
1840 }
1841 }
1842
Eric Laurentfa1e1232016-08-02 19:01:49 -07001843 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001844 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001845 if (hasControl) {
1846 *left = mNewLeftVolume;
1847 *right = mNewRightVolume;
1848 }
1849 return hasControl;
1850 }
1851
1852 mVolumeCtrlIdx = ctrlIdx;
1853 mLeftVolume = newLeft;
1854 mRightVolume = newRight;
1855
1856 // second get volume update from volume controller
1857 if (ctrlIdx >= 0) {
1858 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1859 mNewLeftVolume = newLeft;
1860 mNewRightVolume = newRight;
1861 }
1862 // then indicate volume to all other effects in chain.
1863 // Pass altered volume to effects before volume controller
1864 // and requested volume to effects after controller
1865 uint32_t lVol = newLeft;
1866 uint32_t rVol = newRight;
1867
1868 for (size_t i = 0; i < size; i++) {
1869 if ((int)i == ctrlIdx) {
1870 continue;
1871 }
1872 // this also works for ctrlIdx == -1 when there is no volume controller
1873 if ((int)i > ctrlIdx) {
1874 lVol = *left;
1875 rVol = *right;
1876 }
1877 mEffects[i]->setVolume(&lVol, &rVol, false);
1878 }
1879 *left = newLeft;
1880 *right = newRight;
1881
1882 return hasControl;
1883}
1884
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001885// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001886void AudioFlinger::EffectChain::resetVolume_l()
1887{
Eric Laurente7449bf2016-08-03 18:44:07 -07001888 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1889 uint32_t left = mLeftVolume;
1890 uint32_t right = mRightVolume;
1891 (void)setVolume_l(&left, &right, true);
1892 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001893}
1894
Eric Laurent1b928682014-10-02 19:41:47 -07001895void AudioFlinger::EffectChain::syncHalEffectsState()
1896{
1897 Mutex::Autolock _l(mLock);
1898 for (size_t i = 0; i < mEffects.size(); i++) {
1899 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1900 mEffects[i]->state() == EffectModule::STOPPING) {
1901 mEffects[i]->addEffectToHal_l();
1902 }
1903 }
1904}
1905
Eric Laurentca7cc822012-11-19 14:55:58 -08001906void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1907{
1908 const size_t SIZE = 256;
1909 char buffer[SIZE];
1910 String8 result;
1911
Marco Nelissenb2208842014-02-07 14:00:50 -08001912 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001913 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001914 result.append(buffer);
1915
Marco Nelissenb2208842014-02-07 14:00:50 -08001916 if (numEffects) {
1917 bool locked = AudioFlinger::dumpTryLock(mLock);
1918 // failed to lock - AudioFlinger is probably deadlocked
1919 if (!locked) {
1920 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001921 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001922
Marco Nelissenb2208842014-02-07 14:00:50 -08001923 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001924 snprintf(buffer, SIZE, "\t%p %p %d\n",
1925 mInBuffer,
1926 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001927 mActiveTrackCnt);
1928 result.append(buffer);
1929 write(fd, result.string(), result.size());
1930
1931 for (size_t i = 0; i < numEffects; ++i) {
1932 sp<EffectModule> effect = mEffects[i];
1933 if (effect != 0) {
1934 effect->dump(fd, args);
1935 }
1936 }
1937
1938 if (locked) {
1939 mLock.unlock();
1940 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001941 }
1942}
1943
1944// must be called with ThreadBase::mLock held
1945void AudioFlinger::EffectChain::setEffectSuspended_l(
1946 const effect_uuid_t *type, bool suspend)
1947{
1948 sp<SuspendedEffectDesc> desc;
1949 // use effect type UUID timelow as key as there is no real risk of identical
1950 // timeLow fields among effect type UUIDs.
1951 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1952 if (suspend) {
1953 if (index >= 0) {
1954 desc = mSuspendedEffects.valueAt(index);
1955 } else {
1956 desc = new SuspendedEffectDesc();
1957 desc->mType = *type;
1958 mSuspendedEffects.add(type->timeLow, desc);
1959 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1960 }
1961 if (desc->mRefCount++ == 0) {
1962 sp<EffectModule> effect = getEffectIfEnabled(type);
1963 if (effect != 0) {
1964 desc->mEffect = effect;
1965 effect->setSuspended(true);
1966 effect->setEnabled(false);
1967 }
1968 }
1969 } else {
1970 if (index < 0) {
1971 return;
1972 }
1973 desc = mSuspendedEffects.valueAt(index);
1974 if (desc->mRefCount <= 0) {
1975 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1976 desc->mRefCount = 1;
1977 }
1978 if (--desc->mRefCount == 0) {
1979 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1980 if (desc->mEffect != 0) {
1981 sp<EffectModule> effect = desc->mEffect.promote();
1982 if (effect != 0) {
1983 effect->setSuspended(false);
1984 effect->lock();
1985 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001986 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001987 effect->setEnabled_l(handle->enabled());
1988 }
1989 effect->unlock();
1990 }
1991 desc->mEffect.clear();
1992 }
1993 mSuspendedEffects.removeItemsAt(index);
1994 }
1995 }
1996}
1997
1998// must be called with ThreadBase::mLock held
1999void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2000{
2001 sp<SuspendedEffectDesc> desc;
2002
2003 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2004 if (suspend) {
2005 if (index >= 0) {
2006 desc = mSuspendedEffects.valueAt(index);
2007 } else {
2008 desc = new SuspendedEffectDesc();
2009 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2010 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2011 }
2012 if (desc->mRefCount++ == 0) {
2013 Vector< sp<EffectModule> > effects;
2014 getSuspendEligibleEffects(effects);
2015 for (size_t i = 0; i < effects.size(); i++) {
2016 setEffectSuspended_l(&effects[i]->desc().type, true);
2017 }
2018 }
2019 } else {
2020 if (index < 0) {
2021 return;
2022 }
2023 desc = mSuspendedEffects.valueAt(index);
2024 if (desc->mRefCount <= 0) {
2025 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2026 desc->mRefCount = 1;
2027 }
2028 if (--desc->mRefCount == 0) {
2029 Vector<const effect_uuid_t *> types;
2030 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2031 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2032 continue;
2033 }
2034 types.add(&mSuspendedEffects.valueAt(i)->mType);
2035 }
2036 for (size_t i = 0; i < types.size(); i++) {
2037 setEffectSuspended_l(types[i], false);
2038 }
2039 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2040 mSuspendedEffects.keyAt(index));
2041 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2042 }
2043 }
2044}
2045
2046
2047// The volume effect is used for automated tests only
2048#ifndef OPENSL_ES_H_
2049static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2050 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2051const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2052#endif //OPENSL_ES_H_
2053
2054bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2055{
2056 // auxiliary effects and visualizer are never suspended on output mix
2057 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2058 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2059 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2060 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2061 return false;
2062 }
2063 return true;
2064}
2065
2066void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2067 Vector< sp<AudioFlinger::EffectModule> > &effects)
2068{
2069 effects.clear();
2070 for (size_t i = 0; i < mEffects.size(); i++) {
2071 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2072 effects.add(mEffects[i]);
2073 }
2074 }
2075}
2076
2077sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2078 const effect_uuid_t *type)
2079{
2080 sp<EffectModule> effect = getEffectFromType_l(type);
2081 return effect != 0 && effect->isEnabled() ? effect : 0;
2082}
2083
2084void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2085 bool enabled)
2086{
2087 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2088 if (enabled) {
2089 if (index < 0) {
2090 // if the effect is not suspend check if all effects are suspended
2091 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2092 if (index < 0) {
2093 return;
2094 }
2095 if (!isEffectEligibleForSuspend(effect->desc())) {
2096 return;
2097 }
2098 setEffectSuspended_l(&effect->desc().type, enabled);
2099 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2100 if (index < 0) {
2101 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2102 return;
2103 }
2104 }
2105 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2106 effect->desc().type.timeLow);
2107 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2108 // if effect is requested to suspended but was not yet enabled, supend it now.
2109 if (desc->mEffect == 0) {
2110 desc->mEffect = effect;
2111 effect->setEnabled(false);
2112 effect->setSuspended(true);
2113 }
2114 } else {
2115 if (index < 0) {
2116 return;
2117 }
2118 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2119 effect->desc().type.timeLow);
2120 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2121 desc->mEffect.clear();
2122 effect->setSuspended(false);
2123 }
2124}
2125
Eric Laurent5baf2af2013-09-12 17:37:00 -07002126bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002127{
2128 Mutex::Autolock _l(mLock);
2129 size_t size = mEffects.size();
2130 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002131 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002132 return true;
2133 }
2134 }
2135 return false;
2136}
2137
Eric Laurentaaa44472014-09-12 17:41:50 -07002138void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2139{
2140 Mutex::Autolock _l(mLock);
2141 mThread = thread;
2142 for (size_t i = 0; i < mEffects.size(); i++) {
2143 mEffects[i]->setThread(thread);
2144 }
2145}
2146
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002147void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2148{
2149 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2150 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2151 }
2152 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2153 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2154 }
2155}
2156
2157void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2158{
2159 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2160 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2161 }
2162 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2163 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2164 }
2165}
2166
2167bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002168{
2169 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002170 for (const auto &effect : mEffects) {
2171 if (effect->isProcessImplemented()) {
2172 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002173 }
2174 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002175 // Allow effects without processing.
2176 return true;
2177}
2178
2179bool AudioFlinger::EffectChain::isFastCompatible() const
2180{
2181 Mutex::Autolock _l(mLock);
2182 for (const auto &effect : mEffects) {
2183 if (effect->isProcessImplemented()
2184 && effect->isImplementationSoftware()) {
2185 return false;
2186 }
2187 }
2188 // Allow effects without processing or hw accelerated effects.
2189 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002190}
2191
2192// isCompatibleWithThread_l() must be called with thread->mLock held
2193bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2194{
2195 Mutex::Autolock _l(mLock);
2196 for (size_t i = 0; i < mEffects.size(); i++) {
2197 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2198 return false;
2199 }
2200 }
2201 return true;
2202}
2203
Glenn Kasten63238ef2015-03-02 15:50:29 -08002204} // namespace android