blob: 2f90a14b58eae63cee28528a7c808c1e5ad2fdc7 [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) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001200 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1201 Mutex::Autolock _l(thread->mLock);
1202 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001203 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001204 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001205 if (thread->type() == ThreadBase::OFFLOAD) {
1206 PlaybackThread *t = (PlaybackThread *)thread.get();
1207 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1208 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001209 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001210 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1211 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001212 }
1213 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001214 }
1215 return status;
1216}
1217
1218status_t AudioFlinger::EffectHandle::disable()
1219{
1220 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001221 AutoMutex _l(mLock);
1222 sp<EffectModule> effect = mEffect.promote();
1223 if (effect == 0 || mDisconnected) {
1224 return DEAD_OBJECT;
1225 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001226 if (!mHasControl) {
1227 return INVALID_OPERATION;
1228 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001229
1230 if (!mEnabled) {
1231 return NO_ERROR;
1232 }
1233 mEnabled = false;
1234
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001235 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001236 return NO_ERROR;
1237 }
1238
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001239 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001240
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001241 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001242 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001243 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001244 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1245 Mutex::Autolock _l(thread->mLock);
1246 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001247 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001248 }
1249
1250 return status;
1251}
1252
1253void AudioFlinger::EffectHandle::disconnect()
1254{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001255 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001256 disconnect(true);
1257}
1258
1259void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1260{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001261 AutoMutex _l(mLock);
1262 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1263 if (mDisconnected) {
1264 if (unpinIfLast) {
1265 android_errorWriteLog(0x534e4554, "32707507");
1266 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001267 return;
1268 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001269 mDisconnected = true;
1270 sp<ThreadBase> thread;
1271 {
1272 sp<EffectModule> effect = mEffect.promote();
1273 if (effect != 0) {
1274 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001275 }
1276 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001277 if (thread != 0) {
1278 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001279 } else {
1280 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1281 // try to cleanup as much as we can
1282 sp<EffectModule> effect = mEffect.promote();
1283 if (effect != 0) {
1284 effect->disconnectHandle(this, unpinIfLast);
1285 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001286 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001287
Eric Laurentca7cc822012-11-19 14:55:58 -08001288 if (mClient != 0) {
1289 if (mCblk != NULL) {
1290 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1291 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1292 }
1293 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001294 // Client destructor must run with AudioFlinger client mutex locked
1295 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 mClient.clear();
1297 }
1298}
1299
1300status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1301 uint32_t cmdSize,
1302 void *pCmdData,
1303 uint32_t *replySize,
1304 void *pReplyData)
1305{
1306 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001307 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001308
Eric Laurent1ffc5852016-12-15 14:46:09 -08001309 if (cmdCode == EFFECT_CMD_ENABLE) {
1310 if (*replySize < sizeof(int)) {
1311 android_errorWriteLog(0x534e4554, "32095713");
1312 return BAD_VALUE;
1313 }
1314 *(int *)pReplyData = NO_ERROR;
1315 *replySize = sizeof(int);
1316 return enable();
1317 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1318 if (*replySize < sizeof(int)) {
1319 android_errorWriteLog(0x534e4554, "32095713");
1320 return BAD_VALUE;
1321 }
1322 *(int *)pReplyData = NO_ERROR;
1323 *replySize = sizeof(int);
1324 return disable();
1325 }
1326
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001327 AutoMutex _l(mLock);
1328 sp<EffectModule> effect = mEffect.promote();
1329 if (effect == 0 || mDisconnected) {
1330 return DEAD_OBJECT;
1331 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001332 // only get parameter command is permitted for applications not controlling the effect
1333 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1334 return INVALID_OPERATION;
1335 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001336 if (mClient == 0) {
1337 return INVALID_OPERATION;
1338 }
1339
1340 // handle commands that are not forwarded transparently to effect engine
1341 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001342 if (*replySize < sizeof(int)) {
1343 android_errorWriteLog(0x534e4554, "32095713");
1344 return BAD_VALUE;
1345 }
1346 *(int *)pReplyData = NO_ERROR;
1347 *replySize = sizeof(int);
1348
Eric Laurentca7cc822012-11-19 14:55:58 -08001349 // No need to trylock() here as this function is executed in the binder thread serving a
1350 // particular client process: no risk to block the whole media server process or mixer
1351 // threads if we are stuck here
1352 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001353 // keep local copy of index in case of client corruption b/32220769
1354 const uint32_t clientIndex = mCblk->clientIndex;
1355 const uint32_t serverIndex = mCblk->serverIndex;
1356 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1357 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001358 mCblk->serverIndex = 0;
1359 mCblk->clientIndex = 0;
1360 return BAD_VALUE;
1361 }
1362 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001363 effect_param_t *param = NULL;
1364 for (uint32_t index = serverIndex; index < clientIndex;) {
1365 int *p = (int *)(mBuffer + index);
1366 const int size = *p++;
1367 if (size < 0
1368 || size > EFFECT_PARAM_BUFFER_SIZE
1369 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001370 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001371 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001372 break;
1373 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001374
1375 // copy to local memory in case of client corruption b/32220769
1376 param = (effect_param_t *)realloc(param, size);
1377 if (param == NULL) {
1378 ALOGW("command(): out of memory");
1379 status = NO_MEMORY;
1380 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001381 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001382 memcpy(param, p, size);
1383
1384 int reply = 0;
1385 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001386 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001387 size,
1388 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001389 &rsize,
1390 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001391
1392 // verify shared memory: server index shouldn't change; client index can't go back.
1393 if (serverIndex != mCblk->serverIndex
1394 || clientIndex > mCblk->clientIndex) {
1395 android_errorWriteLog(0x534e4554, "32220769");
1396 status = BAD_VALUE;
1397 break;
1398 }
1399
Eric Laurentca7cc822012-11-19 14:55:58 -08001400 // stop at first error encountered
1401 if (ret != NO_ERROR) {
1402 status = ret;
1403 *(int *)pReplyData = reply;
1404 break;
1405 } else if (reply != NO_ERROR) {
1406 *(int *)pReplyData = reply;
1407 break;
1408 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001409 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001411 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001412 mCblk->serverIndex = 0;
1413 mCblk->clientIndex = 0;
1414 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001415 }
1416
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001417 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001418}
1419
1420void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1421{
1422 ALOGV("setControl %p control %d", this, hasControl);
1423
1424 mHasControl = hasControl;
1425 mEnabled = enabled;
1426
1427 if (signal && mEffectClient != 0) {
1428 mEffectClient->controlStatusChanged(hasControl);
1429 }
1430}
1431
1432void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1433 uint32_t cmdSize,
1434 void *pCmdData,
1435 uint32_t replySize,
1436 void *pReplyData)
1437{
1438 if (mEffectClient != 0) {
1439 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1440 }
1441}
1442
1443
1444
1445void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1446{
1447 if (mEffectClient != 0) {
1448 mEffectClient->enableStatusChanged(enabled);
1449 }
1450}
1451
1452status_t AudioFlinger::EffectHandle::onTransact(
1453 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1454{
1455 return BnEffect::onTransact(code, data, reply, flags);
1456}
1457
1458
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001459void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001460{
1461 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1462
Marco Nelissenb2208842014-02-07 14:00:50 -08001463 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001464 (mClient == 0) ? getpid_cached : mClient->pid(),
1465 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001466 mHasControl ? "yes" : "no",
1467 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001468 mCblk ? mCblk->clientIndex : 0,
1469 mCblk ? mCblk->serverIndex : 0
1470 );
1471
1472 if (locked) {
1473 mCblk->lock.unlock();
1474 }
1475}
1476
1477#undef LOG_TAG
1478#define LOG_TAG "AudioFlinger::EffectChain"
1479
1480AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001481 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1483 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001484 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001485{
1486 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1487 if (thread == NULL) {
1488 return;
1489 }
1490 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1491 thread->frameCount();
1492}
1493
1494AudioFlinger::EffectChain::~EffectChain()
1495{
1496 if (mOwnInBuffer) {
Mikhail Naganov0ce91632016-12-27 10:50:15 -08001497 delete[] mInBuffer;
Eric Laurentca7cc822012-11-19 14:55:58 -08001498 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001499}
1500
1501// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1502sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1503 effect_descriptor_t *descriptor)
1504{
1505 size_t size = mEffects.size();
1506
1507 for (size_t i = 0; i < size; i++) {
1508 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1509 return mEffects[i];
1510 }
1511 }
1512 return 0;
1513}
1514
1515// getEffectFromId_l() must be called with ThreadBase::mLock held
1516sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1517{
1518 size_t size = mEffects.size();
1519
1520 for (size_t i = 0; i < size; i++) {
1521 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1522 if (id == 0 || mEffects[i]->id() == id) {
1523 return mEffects[i];
1524 }
1525 }
1526 return 0;
1527}
1528
1529// getEffectFromType_l() must be called with ThreadBase::mLock held
1530sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1531 const effect_uuid_t *type)
1532{
1533 size_t size = mEffects.size();
1534
1535 for (size_t i = 0; i < size; i++) {
1536 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1537 return mEffects[i];
1538 }
1539 }
1540 return 0;
1541}
1542
1543void AudioFlinger::EffectChain::clearInputBuffer()
1544{
1545 Mutex::Autolock _l(mLock);
1546 sp<ThreadBase> thread = mThread.promote();
1547 if (thread == 0) {
1548 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1549 return;
1550 }
1551 clearInputBuffer_l(thread);
1552}
1553
1554// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001555void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001556{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001557 if (mInBuffer == NULL) {
1558 return;
1559 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001560 // TODO: This will change in the future, depending on multichannel
1561 // and sample format changes for effects.
1562 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1563 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001564 const size_t frameSize =
1565 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001566 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001567}
1568
1569// Must be called with EffectChain::mLock locked
1570void AudioFlinger::EffectChain::process_l()
1571{
1572 sp<ThreadBase> thread = mThread.promote();
1573 if (thread == 0) {
1574 ALOGW("process_l(): cannot promote mixer thread");
1575 return;
1576 }
1577 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1578 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001579 // never process effects when:
1580 // - on an OFFLOAD thread
1581 // - no more tracks are on the session and the effect tail has been rendered
1582 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001583 if (!isGlobalSession) {
1584 bool tracksOnSession = (trackCnt() != 0);
1585
1586 if (!tracksOnSession && mTailBufferCount == 0) {
1587 doProcess = false;
1588 }
1589
1590 if (activeTrackCnt() == 0) {
1591 // if no track is active and the effect tail has not been rendered,
1592 // the input buffer must be cleared here as the mixer process will not do it
1593 if (tracksOnSession || mTailBufferCount > 0) {
1594 clearInputBuffer_l(thread);
1595 if (mTailBufferCount > 0) {
1596 mTailBufferCount--;
1597 }
1598 }
1599 }
1600 }
1601
1602 size_t size = mEffects.size();
1603 if (doProcess) {
1604 for (size_t i = 0; i < size; i++) {
1605 mEffects[i]->process();
1606 }
1607 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001608 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001609 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001610 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1611 }
1612 if (doResetVolume) {
1613 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001614 }
1615}
1616
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001617// createEffect_l() must be called with ThreadBase::mLock held
1618status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1619 ThreadBase *thread,
1620 effect_descriptor_t *desc,
1621 int id,
1622 audio_session_t sessionId,
1623 bool pinned)
1624{
1625 Mutex::Autolock _l(mLock);
1626 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1627 status_t lStatus = effect->status();
1628 if (lStatus == NO_ERROR) {
1629 lStatus = addEffect_ll(effect);
1630 }
1631 if (lStatus != NO_ERROR) {
1632 effect.clear();
1633 }
1634 return lStatus;
1635}
1636
1637// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001638status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1639{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001640 Mutex::Autolock _l(mLock);
1641 return addEffect_ll(effect);
1642}
1643// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1644status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1645{
Eric Laurentca7cc822012-11-19 14:55:58 -08001646 effect_descriptor_t desc = effect->desc();
1647 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1648
Eric Laurentca7cc822012-11-19 14:55:58 -08001649 effect->setChain(this);
1650 sp<ThreadBase> thread = mThread.promote();
1651 if (thread == 0) {
1652 return NO_INIT;
1653 }
1654 effect->setThread(thread);
1655
1656 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1657 // Auxiliary effects are inserted at the beginning of mEffects vector as
1658 // they are processed first and accumulated in chain input buffer
1659 mEffects.insertAt(effect, 0);
1660
1661 // the input buffer for auxiliary effect contains mono samples in
1662 // 32 bit format. This is to avoid saturation in AudoMixer
1663 // accumulation stage. Saturation is done in EffectModule::process() before
1664 // calling the process in effect engine
1665 size_t numSamples = thread->frameCount();
1666 int32_t *buffer = new int32_t[numSamples];
1667 memset(buffer, 0, numSamples * sizeof(int32_t));
1668 effect->setInBuffer((int16_t *)buffer);
1669 // auxiliary effects output samples to chain input buffer for further processing
1670 // by insert effects
1671 effect->setOutBuffer(mInBuffer);
1672 } else {
1673 // Insert effects are inserted at the end of mEffects vector as they are processed
1674 // after track and auxiliary effects.
1675 // Insert effect order as a function of indicated preference:
1676 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1677 // another effect is present
1678 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1679 // last effect claiming first position
1680 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1681 // first effect claiming last position
1682 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1683 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1684 // already present
1685
1686 size_t size = mEffects.size();
1687 size_t idx_insert = size;
1688 ssize_t idx_insert_first = -1;
1689 ssize_t idx_insert_last = -1;
1690
1691 for (size_t i = 0; i < size; i++) {
1692 effect_descriptor_t d = mEffects[i]->desc();
1693 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1694 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1695 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1696 // check invalid effect chaining combinations
1697 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1698 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1699 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1700 desc.name, d.name);
1701 return INVALID_OPERATION;
1702 }
1703 // remember position of first insert effect and by default
1704 // select this as insert position for new effect
1705 if (idx_insert == size) {
1706 idx_insert = i;
1707 }
1708 // remember position of last insert effect claiming
1709 // first position
1710 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1711 idx_insert_first = i;
1712 }
1713 // remember position of first insert effect claiming
1714 // last position
1715 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1716 idx_insert_last == -1) {
1717 idx_insert_last = i;
1718 }
1719 }
1720 }
1721
1722 // modify idx_insert from first position if needed
1723 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1724 if (idx_insert_last != -1) {
1725 idx_insert = idx_insert_last;
1726 } else {
1727 idx_insert = size;
1728 }
1729 } else {
1730 if (idx_insert_first != -1) {
1731 idx_insert = idx_insert_first + 1;
1732 }
1733 }
1734
1735 // always read samples from chain input buffer
1736 effect->setInBuffer(mInBuffer);
1737
1738 // if last effect in the chain, output samples to chain
1739 // output buffer, otherwise to chain input buffer
1740 if (idx_insert == size) {
1741 if (idx_insert != 0) {
1742 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1743 mEffects[idx_insert-1]->configure();
1744 }
1745 effect->setOutBuffer(mOutBuffer);
1746 } else {
1747 effect->setOutBuffer(mInBuffer);
1748 }
1749 mEffects.insertAt(effect, idx_insert);
1750
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001751 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 idx_insert);
1753 }
1754 effect->configure();
1755 return NO_ERROR;
1756}
1757
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001758// removeEffect_l() must be called with ThreadBase::mLock held
1759size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1760 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001761{
1762 Mutex::Autolock _l(mLock);
1763 size_t size = mEffects.size();
1764 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1765
1766 for (size_t i = 0; i < size; i++) {
1767 if (effect == mEffects[i]) {
1768 // calling stop here will remove pre-processing effect from the audio HAL.
1769 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1770 // the middle of a read from audio HAL
1771 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1772 mEffects[i]->state() == EffectModule::STOPPING) {
1773 mEffects[i]->stop();
1774 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001775 if (release) {
1776 mEffects[i]->release_l();
1777 }
1778
Eric Laurentca7cc822012-11-19 14:55:58 -08001779 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1780 delete[] effect->inBuffer();
1781 } else {
1782 if (i == size - 1 && i != 0) {
1783 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1784 mEffects[i - 1]->configure();
1785 }
1786 }
1787 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001788 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001789 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001790
Eric Laurentca7cc822012-11-19 14:55:58 -08001791 break;
1792 }
1793 }
1794
1795 return mEffects.size();
1796}
1797
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001798// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001799void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1800{
1801 size_t size = mEffects.size();
1802 for (size_t i = 0; i < size; i++) {
1803 mEffects[i]->setDevice(device);
1804 }
1805}
1806
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001807// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001808void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1809{
1810 size_t size = mEffects.size();
1811 for (size_t i = 0; i < size; i++) {
1812 mEffects[i]->setMode(mode);
1813 }
1814}
1815
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001816// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001817void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1818{
1819 size_t size = mEffects.size();
1820 for (size_t i = 0; i < size; i++) {
1821 mEffects[i]->setAudioSource(source);
1822 }
1823}
1824
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001825// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001826bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001827{
1828 uint32_t newLeft = *left;
1829 uint32_t newRight = *right;
1830 bool hasControl = false;
1831 int ctrlIdx = -1;
1832 size_t size = mEffects.size();
1833
1834 // first update volume controller
1835 for (size_t i = size; i > 0; i--) {
1836 if (mEffects[i - 1]->isProcessEnabled() &&
1837 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1838 ctrlIdx = i - 1;
1839 hasControl = true;
1840 break;
1841 }
1842 }
1843
Eric Laurentfa1e1232016-08-02 19:01:49 -07001844 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001845 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001846 if (hasControl) {
1847 *left = mNewLeftVolume;
1848 *right = mNewRightVolume;
1849 }
1850 return hasControl;
1851 }
1852
1853 mVolumeCtrlIdx = ctrlIdx;
1854 mLeftVolume = newLeft;
1855 mRightVolume = newRight;
1856
1857 // second get volume update from volume controller
1858 if (ctrlIdx >= 0) {
1859 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1860 mNewLeftVolume = newLeft;
1861 mNewRightVolume = newRight;
1862 }
1863 // then indicate volume to all other effects in chain.
1864 // Pass altered volume to effects before volume controller
1865 // and requested volume to effects after controller
1866 uint32_t lVol = newLeft;
1867 uint32_t rVol = newRight;
1868
1869 for (size_t i = 0; i < size; i++) {
1870 if ((int)i == ctrlIdx) {
1871 continue;
1872 }
1873 // this also works for ctrlIdx == -1 when there is no volume controller
1874 if ((int)i > ctrlIdx) {
1875 lVol = *left;
1876 rVol = *right;
1877 }
1878 mEffects[i]->setVolume(&lVol, &rVol, false);
1879 }
1880 *left = newLeft;
1881 *right = newRight;
1882
1883 return hasControl;
1884}
1885
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001886// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001887void AudioFlinger::EffectChain::resetVolume_l()
1888{
Eric Laurente7449bf2016-08-03 18:44:07 -07001889 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1890 uint32_t left = mLeftVolume;
1891 uint32_t right = mRightVolume;
1892 (void)setVolume_l(&left, &right, true);
1893 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001894}
1895
Eric Laurent1b928682014-10-02 19:41:47 -07001896void AudioFlinger::EffectChain::syncHalEffectsState()
1897{
1898 Mutex::Autolock _l(mLock);
1899 for (size_t i = 0; i < mEffects.size(); i++) {
1900 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1901 mEffects[i]->state() == EffectModule::STOPPING) {
1902 mEffects[i]->addEffectToHal_l();
1903 }
1904 }
1905}
1906
Eric Laurentca7cc822012-11-19 14:55:58 -08001907void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1908{
1909 const size_t SIZE = 256;
1910 char buffer[SIZE];
1911 String8 result;
1912
Marco Nelissenb2208842014-02-07 14:00:50 -08001913 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001914 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001915 result.append(buffer);
1916
Marco Nelissenb2208842014-02-07 14:00:50 -08001917 if (numEffects) {
1918 bool locked = AudioFlinger::dumpTryLock(mLock);
1919 // failed to lock - AudioFlinger is probably deadlocked
1920 if (!locked) {
1921 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001922 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001923
Marco Nelissenb2208842014-02-07 14:00:50 -08001924 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001925 snprintf(buffer, SIZE, "\t%p %p %d\n",
1926 mInBuffer,
1927 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001928 mActiveTrackCnt);
1929 result.append(buffer);
1930 write(fd, result.string(), result.size());
1931
1932 for (size_t i = 0; i < numEffects; ++i) {
1933 sp<EffectModule> effect = mEffects[i];
1934 if (effect != 0) {
1935 effect->dump(fd, args);
1936 }
1937 }
1938
1939 if (locked) {
1940 mLock.unlock();
1941 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001942 }
1943}
1944
1945// must be called with ThreadBase::mLock held
1946void AudioFlinger::EffectChain::setEffectSuspended_l(
1947 const effect_uuid_t *type, bool suspend)
1948{
1949 sp<SuspendedEffectDesc> desc;
1950 // use effect type UUID timelow as key as there is no real risk of identical
1951 // timeLow fields among effect type UUIDs.
1952 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1953 if (suspend) {
1954 if (index >= 0) {
1955 desc = mSuspendedEffects.valueAt(index);
1956 } else {
1957 desc = new SuspendedEffectDesc();
1958 desc->mType = *type;
1959 mSuspendedEffects.add(type->timeLow, desc);
1960 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1961 }
1962 if (desc->mRefCount++ == 0) {
1963 sp<EffectModule> effect = getEffectIfEnabled(type);
1964 if (effect != 0) {
1965 desc->mEffect = effect;
1966 effect->setSuspended(true);
1967 effect->setEnabled(false);
1968 }
1969 }
1970 } else {
1971 if (index < 0) {
1972 return;
1973 }
1974 desc = mSuspendedEffects.valueAt(index);
1975 if (desc->mRefCount <= 0) {
1976 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1977 desc->mRefCount = 1;
1978 }
1979 if (--desc->mRefCount == 0) {
1980 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1981 if (desc->mEffect != 0) {
1982 sp<EffectModule> effect = desc->mEffect.promote();
1983 if (effect != 0) {
1984 effect->setSuspended(false);
1985 effect->lock();
1986 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001987 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001988 effect->setEnabled_l(handle->enabled());
1989 }
1990 effect->unlock();
1991 }
1992 desc->mEffect.clear();
1993 }
1994 mSuspendedEffects.removeItemsAt(index);
1995 }
1996 }
1997}
1998
1999// must be called with ThreadBase::mLock held
2000void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2001{
2002 sp<SuspendedEffectDesc> desc;
2003
2004 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2005 if (suspend) {
2006 if (index >= 0) {
2007 desc = mSuspendedEffects.valueAt(index);
2008 } else {
2009 desc = new SuspendedEffectDesc();
2010 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2011 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2012 }
2013 if (desc->mRefCount++ == 0) {
2014 Vector< sp<EffectModule> > effects;
2015 getSuspendEligibleEffects(effects);
2016 for (size_t i = 0; i < effects.size(); i++) {
2017 setEffectSuspended_l(&effects[i]->desc().type, true);
2018 }
2019 }
2020 } else {
2021 if (index < 0) {
2022 return;
2023 }
2024 desc = mSuspendedEffects.valueAt(index);
2025 if (desc->mRefCount <= 0) {
2026 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2027 desc->mRefCount = 1;
2028 }
2029 if (--desc->mRefCount == 0) {
2030 Vector<const effect_uuid_t *> types;
2031 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2032 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2033 continue;
2034 }
2035 types.add(&mSuspendedEffects.valueAt(i)->mType);
2036 }
2037 for (size_t i = 0; i < types.size(); i++) {
2038 setEffectSuspended_l(types[i], false);
2039 }
2040 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2041 mSuspendedEffects.keyAt(index));
2042 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2043 }
2044 }
2045}
2046
2047
2048// The volume effect is used for automated tests only
2049#ifndef OPENSL_ES_H_
2050static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2051 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2052const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2053#endif //OPENSL_ES_H_
2054
2055bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2056{
2057 // auxiliary effects and visualizer are never suspended on output mix
2058 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2059 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2060 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2061 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2062 return false;
2063 }
2064 return true;
2065}
2066
2067void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2068 Vector< sp<AudioFlinger::EffectModule> > &effects)
2069{
2070 effects.clear();
2071 for (size_t i = 0; i < mEffects.size(); i++) {
2072 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2073 effects.add(mEffects[i]);
2074 }
2075 }
2076}
2077
2078sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2079 const effect_uuid_t *type)
2080{
2081 sp<EffectModule> effect = getEffectFromType_l(type);
2082 return effect != 0 && effect->isEnabled() ? effect : 0;
2083}
2084
2085void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2086 bool enabled)
2087{
2088 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2089 if (enabled) {
2090 if (index < 0) {
2091 // if the effect is not suspend check if all effects are suspended
2092 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2093 if (index < 0) {
2094 return;
2095 }
2096 if (!isEffectEligibleForSuspend(effect->desc())) {
2097 return;
2098 }
2099 setEffectSuspended_l(&effect->desc().type, enabled);
2100 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2101 if (index < 0) {
2102 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2103 return;
2104 }
2105 }
2106 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2107 effect->desc().type.timeLow);
2108 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2109 // if effect is requested to suspended but was not yet enabled, supend it now.
2110 if (desc->mEffect == 0) {
2111 desc->mEffect = effect;
2112 effect->setEnabled(false);
2113 effect->setSuspended(true);
2114 }
2115 } else {
2116 if (index < 0) {
2117 return;
2118 }
2119 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2120 effect->desc().type.timeLow);
2121 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2122 desc->mEffect.clear();
2123 effect->setSuspended(false);
2124 }
2125}
2126
Eric Laurent5baf2af2013-09-12 17:37:00 -07002127bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002128{
2129 Mutex::Autolock _l(mLock);
2130 size_t size = mEffects.size();
2131 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002132 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002133 return true;
2134 }
2135 }
2136 return false;
2137}
2138
Eric Laurentaaa44472014-09-12 17:41:50 -07002139void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2140{
2141 Mutex::Autolock _l(mLock);
2142 mThread = thread;
2143 for (size_t i = 0; i < mEffects.size(); i++) {
2144 mEffects[i]->setThread(thread);
2145 }
2146}
2147
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002148void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2149{
2150 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2151 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2152 }
2153 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2154 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2155 }
2156}
2157
2158void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2159{
2160 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2161 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2162 }
2163 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2164 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2165 }
2166}
2167
2168bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002169{
2170 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002171 for (const auto &effect : mEffects) {
2172 if (effect->isProcessImplemented()) {
2173 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002174 }
2175 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002176 // Allow effects without processing.
2177 return true;
2178}
2179
2180bool AudioFlinger::EffectChain::isFastCompatible() const
2181{
2182 Mutex::Autolock _l(mLock);
2183 for (const auto &effect : mEffects) {
2184 if (effect->isProcessImplemented()
2185 && effect->isImplementationSoftware()) {
2186 return false;
2187 }
2188 }
2189 // Allow effects without processing or hw accelerated effects.
2190 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002191}
2192
2193// isCompatibleWithThread_l() must be called with thread->mLock held
2194bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2195{
2196 Mutex::Autolock _l(mLock);
2197 for (size_t i = 0; i < mEffects.size(); i++) {
2198 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2199 return false;
2200 }
2201 }
2202 return true;
2203}
2204
Glenn Kasten63238ef2015-03-02 15:50:29 -08002205} // namespace android