blob: f4428fe03de79adfbcf26790737e42a18d04ce25 [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>
Mikhail Naganova0c91332016-09-19 10:01:12 -070025#include <media/audiohal/EffectHalInterface.h>
26#include <media/audiohal/EffectsFactoryHalInterface.h>
Mikhail Naganov9fe94012016-10-14 14:57:40 -070027#include <system/audio_effects/effect_visualizer.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080028
29#include "AudioFlinger.h"
30#include "ServiceUtilities.h"
31
32// ----------------------------------------------------------------------------
33
34// Note: the following macro is used for extremely verbose logging message. In
35// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
36// 0; but one side effect of this is to turn all LOGV's as well. Some messages
37// are so verbose that we want to suppress them even when we have ALOG_ASSERT
38// turned on. Do not uncomment the #def below unless you really know what you
39// are doing and want to see all of the extremely verbose messages.
40//#define VERY_VERY_VERBOSE_LOGGING
41#ifdef VERY_VERY_VERBOSE_LOGGING
42#define ALOGVV ALOGV
43#else
44#define ALOGVV(a...) do { } while(0)
45#endif
46
Ricardo Garcia726b6a72014-08-11 12:04:54 -070047#define min(a, b) ((a) < (b) ? (a) : (b))
48
Eric Laurentca7cc822012-11-19 14:55:58 -080049namespace android {
50
51// ----------------------------------------------------------------------------
52// EffectModule implementation
53// ----------------------------------------------------------------------------
54
55#undef LOG_TAG
56#define LOG_TAG "AudioFlinger::EffectModule"
57
58AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
59 const wp<AudioFlinger::EffectChain>& chain,
60 effect_descriptor_t *desc,
61 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080062 audio_session_t sessionId,
63 bool pinned)
64 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080065 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
66 mDescriptor(*desc),
67 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080068 mStatus(NO_INIT), mState(IDLE),
69 // mMaxDisableWaitCnt is set by configure() and not used before then
70 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070071 mSuspended(false),
72 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080073{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080074 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080075 int lStatus;
76
77 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070078 mStatus = -ENODEV;
79 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070080 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070081 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070082 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070083 mStatus = effectsFactory->createEffect(
84 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
85 }
86 }
Eric Laurentca7cc822012-11-19 14:55:58 -080087
88 if (mStatus != NO_ERROR) {
89 return;
90 }
91 lStatus = init();
92 if (lStatus < 0) {
93 mStatus = lStatus;
94 goto Error;
95 }
96
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080097 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070098 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080099
Eric Laurentca7cc822012-11-19 14:55:58 -0800100 return;
101Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700102 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800103 ALOGV("Constructor Error %d", mStatus);
104}
105
106AudioFlinger::EffectModule::~EffectModule()
107{
108 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700109 if (mEffectInterface != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110 ALOGW("EffectModule %p destructor called with unreleased interface", this);
111 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800112 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800113
Eric Laurentca7cc822012-11-19 14:55:58 -0800114}
115
116status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
117{
118 status_t status;
119
120 Mutex::Autolock _l(mLock);
121 int priority = handle->priority();
122 size_t size = mHandles.size();
123 EffectHandle *controlHandle = NULL;
124 size_t i;
125 for (i = 0; i < size; i++) {
126 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800127 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800128 continue;
129 }
130 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700131 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800132 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800134 if (h->priority() <= priority) {
135 break;
136 }
137 }
138 // if inserted in first place, move effect control from previous owner to this handle
139 if (i == 0) {
140 bool enabled = false;
141 if (controlHandle != NULL) {
142 enabled = controlHandle->enabled();
143 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
144 }
145 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
146 status = NO_ERROR;
147 } else {
148 status = ALREADY_EXISTS;
149 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700150 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800151 mHandles.insertAt(handle, i);
152 return status;
153}
154
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800155ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800156{
157 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800158 return removeHandle_l(handle);
159}
160
161ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
162{
Eric Laurentca7cc822012-11-19 14:55:58 -0800163 size_t size = mHandles.size();
164 size_t i;
165 for (i = 0; i < size; i++) {
166 if (mHandles[i] == handle) {
167 break;
168 }
169 }
170 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800171 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
172 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800173 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800174 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800175
176 mHandles.removeAt(i);
177 // if removed from first place, move effect control from this handle to next in line
178 if (i == 0) {
179 EffectHandle *h = controlHandle_l();
180 if (h != NULL) {
181 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
182 }
183 }
184
185 // Prevent calls to process() and other functions on effect interface from now on.
186 // The effect engine will be released by the destructor when the last strong reference on
187 // this object is released which can happen after next process is called.
188 if (mHandles.size() == 0 && !mPinned) {
189 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800190 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800191 }
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 Naganov022b9952017-01-04 16:36:51 -0800278 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800279 return;
280 }
281
282 if (isProcessEnabled()) {
283 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
284 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
285 ditherAndClamp(mConfig.inputCfg.buffer.s32,
286 mConfig.inputCfg.buffer.s32,
287 mConfig.inputCfg.buffer.frameCount/2);
288 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700289 int ret;
290 if (isProcessImplemented()) {
291 // do the actual processing in the effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800292 ret = mEffectInterface->process();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700293 } else {
294 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
295 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
296 int16_t *in = mConfig.inputCfg.buffer.s16;
297 int16_t *out = mConfig.outputCfg.buffer.s16;
Eric Laurentca7cc822012-11-19 14:55:58 -0800298
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700299 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
300 for (size_t i = 0; i < frameCnt; i++) {
301 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
302 }
303 } else {
304 memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
305 frameCnt * sizeof(int16_t));
306 }
307 }
308 ret = -ENODATA;
309 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800310 // force transition to IDLE state when engine is ready
311 if (mState == STOPPED && ret == -ENODATA) {
312 mDisableWaitCnt = 1;
313 }
314
315 // clear auxiliary effect input buffer for next accumulation
316 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
317 memset(mConfig.inputCfg.buffer.raw, 0,
318 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
319 }
320 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
321 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
322 // If an insert effect is idle and input buffer is different from output buffer,
323 // accumulate input onto output
324 sp<EffectChain> chain = mChain.promote();
325 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700326 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
Eric Laurentca7cc822012-11-19 14:55:58 -0800327 int16_t *in = mConfig.inputCfg.buffer.s16;
328 int16_t *out = mConfig.outputCfg.buffer.s16;
329 for (size_t i = 0; i < frameCnt; i++) {
330 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
331 }
332 }
333 }
334}
335
336void AudioFlinger::EffectModule::reset_l()
337{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700338 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800339 return;
340 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700341 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800342}
343
344status_t AudioFlinger::EffectModule::configure()
345{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700346 status_t status;
347 sp<ThreadBase> thread;
348 uint32_t size;
349 audio_channel_mask_t channelMask;
350
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700351 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700352 status = NO_INIT;
353 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800354 }
355
Eric Laurentd0ebb532013-04-02 16:41:41 -0700356 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800357 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700358 status = DEAD_OBJECT;
359 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800360 }
361
362 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700363 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700364 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800365
366 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
367 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900368 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
369 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800370 } else {
371 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700372 // TODO: Update this logic when multichannel effects are implemented.
373 // For offloaded tracks consider mono output as stereo for proper effect initialization
374 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
375 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
376 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
377 ALOGV("Overriding effect input and output as STEREO");
378 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800379 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700380
Eric Laurentca7cc822012-11-19 14:55:58 -0800381 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
382 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
383 mConfig.inputCfg.samplingRate = thread->sampleRate();
384 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
385 mConfig.inputCfg.bufferProvider.cookie = NULL;
386 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
387 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
388 mConfig.outputCfg.bufferProvider.cookie = NULL;
389 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
390 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
391 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
392 // Insert effect:
393 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
394 // always overwrites output buffer: input buffer == output buffer
395 // - in other sessions:
396 // last effect in the chain accumulates in output buffer: input buffer != output buffer
397 // other effect: overwrites output buffer: input buffer == output buffer
398 // Auxiliary effect:
399 // accumulates in output buffer: input buffer != output buffer
400 // Therefore: accumulate <=> input buffer != output buffer
401 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
402 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
403 } else {
404 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
405 }
406 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
407 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
408 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
409 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800410 if (mInBuffer != 0) {
411 mInBuffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
412 }
413 if (mOutBuffer != 0) {
414 mOutBuffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
415 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800416
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700417 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800418 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
419
420 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700421 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700422 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
423 sizeof(effect_config_t),
424 &mConfig,
425 &size,
426 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800427 if (status == 0) {
428 status = cmdStatus;
429 }
430
431 if (status == 0 &&
432 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
433 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
434 effect_param_t *p = (effect_param_t *)buf32;
435
436 p->psize = sizeof(uint32_t);
437 p->vsize = sizeof(uint32_t);
438 size = sizeof(int);
439 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
440
441 uint32_t latency = 0;
442 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
443 if (pbt != NULL) {
444 latency = pbt->latency_l();
445 }
446
447 *((int32_t *)p->data + 1)= latency;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700448 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
449 sizeof(effect_param_t) + 8,
450 &buf32,
451 &size,
452 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800453 }
454
455 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
456 (1000 * mConfig.outputCfg.buffer.frameCount);
457
Eric Laurentd0ebb532013-04-02 16:41:41 -0700458exit:
459 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800460 return status;
461}
462
463status_t AudioFlinger::EffectModule::init()
464{
465 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700466 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800467 return NO_INIT;
468 }
469 status_t cmdStatus;
470 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700471 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
472 0,
473 NULL,
474 &size,
475 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800476 if (status == 0) {
477 status = cmdStatus;
478 }
479 return status;
480}
481
Eric Laurent1b928682014-10-02 19:41:47 -0700482void AudioFlinger::EffectModule::addEffectToHal_l()
483{
484 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
485 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
486 sp<ThreadBase> thread = mThread.promote();
487 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700488 sp<StreamHalInterface> stream = thread->stream();
489 if (stream != 0) {
490 status_t result = stream->addEffect(mEffectInterface);
491 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700492 }
493 }
494 }
495}
496
Eric Laurentfa1e1232016-08-02 19:01:49 -0700497// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800498status_t AudioFlinger::EffectModule::start()
499{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700500 sp<EffectChain> chain;
501 status_t status;
502 {
503 Mutex::Autolock _l(mLock);
504 status = start_l();
505 if (status == NO_ERROR) {
506 chain = mChain.promote();
507 }
508 }
509 if (chain != 0) {
510 chain->resetVolume_l();
511 }
512 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800513}
514
515status_t AudioFlinger::EffectModule::start_l()
516{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700517 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800518 return NO_INIT;
519 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700520 if (mStatus != NO_ERROR) {
521 return mStatus;
522 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800523 status_t cmdStatus;
524 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700525 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
526 0,
527 NULL,
528 &size,
529 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800530 if (status == 0) {
531 status = cmdStatus;
532 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700533 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700534 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800535 }
536 return status;
537}
538
539status_t AudioFlinger::EffectModule::stop()
540{
541 Mutex::Autolock _l(mLock);
542 return stop_l();
543}
544
545status_t AudioFlinger::EffectModule::stop_l()
546{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700547 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800548 return NO_INIT;
549 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700550 if (mStatus != NO_ERROR) {
551 return mStatus;
552 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800553 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800554 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700555 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
556 0,
557 NULL,
558 &size,
559 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800560 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800561 status = cmdStatus;
562 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800563 if (status == NO_ERROR) {
564 status = remove_effect_from_hal_l();
565 }
566 return status;
567}
568
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800569// must be called with EffectChain::mLock held
570void AudioFlinger::EffectModule::release_l()
571{
572 if (mEffectInterface != 0) {
573 remove_effect_from_hal_l();
574 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800575 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800576 mEffectInterface.clear();
577 }
578}
579
Eric Laurentbfb1b832013-01-07 09:53:42 -0800580status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
581{
582 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
583 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800584 sp<ThreadBase> thread = mThread.promote();
585 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700586 sp<StreamHalInterface> stream = thread->stream();
587 if (stream != 0) {
588 status_t result = stream->removeEffect(mEffectInterface);
589 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800590 }
591 }
592 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800593 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800594}
595
Andy Hunge4a1d912016-08-17 14:11:13 -0700596// round up delta valid if value and divisor are positive.
597template <typename T>
598static T roundUpDelta(const T &value, const T &divisor) {
599 T remainder = value % divisor;
600 return remainder == 0 ? 0 : divisor - remainder;
601}
602
Eric Laurentca7cc822012-11-19 14:55:58 -0800603status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
604 uint32_t cmdSize,
605 void *pCmdData,
606 uint32_t *replySize,
607 void *pReplyData)
608{
609 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700610 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800611
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700612 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800613 return NO_INIT;
614 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700615 if (mStatus != NO_ERROR) {
616 return mStatus;
617 }
Andy Hung110bc952016-06-20 15:22:52 -0700618 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700619 (sizeof(effect_param_t) > cmdSize ||
620 ((effect_param_t *)pCmdData)->psize > cmdSize
621 - sizeof(effect_param_t))) {
622 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800623 android_errorWriteLog(0x534e4554, "33003822");
624 return -EINVAL;
625 }
626 if (cmdCode == EFFECT_CMD_GET_PARAM &&
627 (*replySize < sizeof(effect_param_t) ||
628 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
629 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700630 return -EINVAL;
631 }
ragoe2759072016-11-22 18:02:48 -0800632 if (cmdCode == EFFECT_CMD_GET_PARAM &&
633 (sizeof(effect_param_t) > *replySize
634 || ((effect_param_t *)pCmdData)->psize > *replySize
635 - sizeof(effect_param_t)
636 || ((effect_param_t *)pCmdData)->vsize > *replySize
637 - sizeof(effect_param_t)
638 - ((effect_param_t *)pCmdData)->psize
639 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
640 *replySize
641 - sizeof(effect_param_t)
642 - ((effect_param_t *)pCmdData)->psize
643 - ((effect_param_t *)pCmdData)->vsize)) {
644 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
645 android_errorWriteLog(0x534e4554, "32705438");
646 return -EINVAL;
647 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700648 if ((cmdCode == EFFECT_CMD_SET_PARAM
649 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
650 (sizeof(effect_param_t) > cmdSize
651 || ((effect_param_t *)pCmdData)->psize > cmdSize
652 - sizeof(effect_param_t)
653 || ((effect_param_t *)pCmdData)->vsize > cmdSize
654 - sizeof(effect_param_t)
655 - ((effect_param_t *)pCmdData)->psize
656 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
657 cmdSize
658 - sizeof(effect_param_t)
659 - ((effect_param_t *)pCmdData)->psize
660 - ((effect_param_t *)pCmdData)->vsize)) {
661 android_errorWriteLog(0x534e4554, "30204301");
662 return -EINVAL;
663 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700664 status_t status = mEffectInterface->command(cmdCode,
665 cmdSize,
666 pCmdData,
667 replySize,
668 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800669 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
670 uint32_t size = (replySize == NULL) ? 0 : *replySize;
671 for (size_t i = 1; i < mHandles.size(); i++) {
672 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800673 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800674 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
675 }
676 }
677 }
678 return status;
679}
680
681status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
682{
683 Mutex::Autolock _l(mLock);
684 return setEnabled_l(enabled);
685}
686
687// must be called with EffectModule::mLock held
688status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
689{
690
691 ALOGV("setEnabled %p enabled %d", this, enabled);
692
693 if (enabled != isEnabled()) {
694 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
695 if (enabled && status != NO_ERROR) {
696 return status;
697 }
698
699 switch (mState) {
700 // going from disabled to enabled
701 case IDLE:
702 mState = STARTING;
703 break;
704 case STOPPED:
705 mState = RESTART;
706 break;
707 case STOPPING:
708 mState = ACTIVE;
709 break;
710
711 // going from enabled to disabled
712 case RESTART:
713 mState = STOPPED;
714 break;
715 case STARTING:
716 mState = IDLE;
717 break;
718 case ACTIVE:
719 mState = STOPPING;
720 break;
721 case DESTROYED:
722 return NO_ERROR; // simply ignore as we are being destroyed
723 }
724 for (size_t i = 1; i < mHandles.size(); i++) {
725 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800726 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800727 h->setEnabled(enabled);
728 }
729 }
730 }
731 return NO_ERROR;
732}
733
734bool AudioFlinger::EffectModule::isEnabled() const
735{
736 switch (mState) {
737 case RESTART:
738 case STARTING:
739 case ACTIVE:
740 return true;
741 case IDLE:
742 case STOPPING:
743 case STOPPED:
744 case DESTROYED:
745 default:
746 return false;
747 }
748}
749
750bool AudioFlinger::EffectModule::isProcessEnabled() const
751{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700752 if (mStatus != NO_ERROR) {
753 return false;
754 }
755
Eric Laurentca7cc822012-11-19 14:55:58 -0800756 switch (mState) {
757 case RESTART:
758 case ACTIVE:
759 case STOPPING:
760 case STOPPED:
761 return true;
762 case IDLE:
763 case STARTING:
764 case DESTROYED:
765 default:
766 return false;
767 }
768}
769
Mikhail Naganov022b9952017-01-04 16:36:51 -0800770void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
771 if (buffer != 0) {
772 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
773 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
774 } else {
775 mConfig.inputCfg.buffer.raw = NULL;
776 }
777 mInBuffer = buffer;
778 mEffectInterface->setInBuffer(buffer);
779}
780
781void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
782 if (buffer != 0) {
783 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
784 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
785 } else {
786 mConfig.outputCfg.buffer.raw = NULL;
787 }
788 mOutBuffer = buffer;
789 mEffectInterface->setOutBuffer(buffer);
790}
791
Eric Laurentca7cc822012-11-19 14:55:58 -0800792status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
793{
794 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700795 if (mStatus != NO_ERROR) {
796 return mStatus;
797 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800798 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800799 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
800 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
801 if (isProcessEnabled() &&
802 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
803 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800804 uint32_t volume[2];
805 uint32_t *pVolume = NULL;
806 uint32_t size = sizeof(volume);
807 volume[0] = *left;
808 volume[1] = *right;
809 if (controller) {
810 pVolume = volume;
811 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700812 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
813 size,
814 volume,
815 &size,
816 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -0800817 if (controller && status == NO_ERROR && size == sizeof(volume)) {
818 *left = volume[0];
819 *right = volume[1];
820 }
821 }
822 return status;
823}
824
825status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
826{
827 if (device == AUDIO_DEVICE_NONE) {
828 return NO_ERROR;
829 }
830
831 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700832 if (mStatus != NO_ERROR) {
833 return mStatus;
834 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800835 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700836 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800837 status_t cmdStatus;
838 uint32_t size = sizeof(status_t);
839 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
840 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700841 status = mEffectInterface->command(cmd,
842 sizeof(uint32_t),
843 &device,
844 &size,
845 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800846 }
847 return status;
848}
849
850status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
851{
852 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700853 if (mStatus != NO_ERROR) {
854 return mStatus;
855 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800856 status_t status = NO_ERROR;
857 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
858 status_t cmdStatus;
859 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700860 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
861 sizeof(audio_mode_t),
862 &mode,
863 &size,
864 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800865 if (status == NO_ERROR) {
866 status = cmdStatus;
867 }
868 }
869 return status;
870}
871
872status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
873{
874 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700875 if (mStatus != NO_ERROR) {
876 return mStatus;
877 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800878 status_t status = NO_ERROR;
879 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
880 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700881 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
882 sizeof(audio_source_t),
883 &source,
884 &size,
885 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800886 }
887 return status;
888}
889
890void AudioFlinger::EffectModule::setSuspended(bool suspended)
891{
892 Mutex::Autolock _l(mLock);
893 mSuspended = suspended;
894}
895
896bool AudioFlinger::EffectModule::suspended() const
897{
898 Mutex::Autolock _l(mLock);
899 return mSuspended;
900}
901
902bool AudioFlinger::EffectModule::purgeHandles()
903{
904 bool enabled = false;
905 Mutex::Autolock _l(mLock);
906 for (size_t i = 0; i < mHandles.size(); i++) {
907 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800908 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800909 if (handle->hasControl()) {
910 enabled = handle->enabled();
911 }
912 }
913 }
914 return enabled;
915}
916
Eric Laurent5baf2af2013-09-12 17:37:00 -0700917status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
918{
919 Mutex::Autolock _l(mLock);
920 if (mStatus != NO_ERROR) {
921 return mStatus;
922 }
923 status_t status = NO_ERROR;
924 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
925 status_t cmdStatus;
926 uint32_t size = sizeof(status_t);
927 effect_offload_param_t cmd;
928
929 cmd.isOffload = offloaded;
930 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700931 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
932 sizeof(effect_offload_param_t),
933 &cmd,
934 &size,
935 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700936 if (status == NO_ERROR) {
937 status = cmdStatus;
938 }
939 mOffloaded = (status == NO_ERROR) ? offloaded : false;
940 } else {
941 if (offloaded) {
942 status = INVALID_OPERATION;
943 }
944 mOffloaded = false;
945 }
946 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
947 return status;
948}
949
950bool AudioFlinger::EffectModule::isOffloaded() const
951{
952 Mutex::Autolock _l(mLock);
953 return mOffloaded;
954}
955
Marco Nelissenb2208842014-02-07 14:00:50 -0800956String8 effectFlagsToString(uint32_t flags) {
957 String8 s;
958
959 s.append("conn. mode: ");
960 switch (flags & EFFECT_FLAG_TYPE_MASK) {
961 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
962 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
963 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
964 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
965 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
966 default: s.append("unknown/reserved"); break;
967 }
968 s.append(", ");
969
970 s.append("insert pref: ");
971 switch (flags & EFFECT_FLAG_INSERT_MASK) {
972 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
973 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
974 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
975 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
976 default: s.append("unknown/reserved"); break;
977 }
978 s.append(", ");
979
980 s.append("volume mgmt: ");
981 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
982 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
983 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
984 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
985 default: s.append("unknown/reserved"); break;
986 }
987 s.append(", ");
988
989 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
990 if (devind) {
991 s.append("device indication: ");
992 switch (devind) {
993 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
994 default: s.append("unknown/reserved"); break;
995 }
996 s.append(", ");
997 }
998
999 s.append("input mode: ");
1000 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1001 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1002 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1003 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1004 default: s.append("not set"); break;
1005 }
1006 s.append(", ");
1007
1008 s.append("output mode: ");
1009 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1010 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1011 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1012 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1013 default: s.append("not set"); break;
1014 }
1015 s.append(", ");
1016
1017 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1018 if (accel) {
1019 s.append("hardware acceleration: ");
1020 switch (accel) {
1021 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1022 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1023 default: s.append("unknown/reserved"); break;
1024 }
1025 s.append(", ");
1026 }
1027
1028 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1029 if (modeind) {
1030 s.append("mode indication: ");
1031 switch (modeind) {
1032 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1033 default: s.append("unknown/reserved"); break;
1034 }
1035 s.append(", ");
1036 }
1037
1038 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1039 if (srcind) {
1040 s.append("source indication: ");
1041 switch (srcind) {
1042 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1043 default: s.append("unknown/reserved"); break;
1044 }
1045 s.append(", ");
1046 }
1047
1048 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1049 s.append("offloadable, ");
1050 }
1051
1052 int len = s.length();
1053 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001054 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001055 s.unlockBuffer(len - 2);
1056 }
1057 return s;
1058}
1059
1060
Glenn Kasten0f11b512014-01-31 16:18:54 -08001061void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001062{
1063 const size_t SIZE = 256;
1064 char buffer[SIZE];
1065 String8 result;
1066
1067 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1068 result.append(buffer);
1069
1070 bool locked = AudioFlinger::dumpTryLock(mLock);
1071 // failed to lock - AudioFlinger is probably deadlocked
1072 if (!locked) {
1073 result.append("\t\tCould not lock Fx mutex:\n");
1074 }
1075
1076 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001077 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001078 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001079 result.append(buffer);
1080
1081 result.append("\t\tDescriptor:\n");
1082 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1083 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1084 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1085 mDescriptor.uuid.node[2],
1086 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1087 result.append(buffer);
1088 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1089 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1090 mDescriptor.type.timeHiAndVersion,
1091 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1092 mDescriptor.type.node[2],
1093 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1094 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001095 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001096 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001097 mDescriptor.flags,
1098 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001099 result.append(buffer);
1100 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1101 mDescriptor.name);
1102 result.append(buffer);
1103 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1104 mDescriptor.implementor);
1105 result.append(buffer);
1106
1107 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001108 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001109 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001110 mConfig.inputCfg.buffer.frameCount,
1111 mConfig.inputCfg.samplingRate,
1112 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001113 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001114 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001115 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001116 result.append(buffer);
1117
1118 result.append("\t\t- Output configuration:\n");
1119 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001120 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001121 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001122 mConfig.outputCfg.buffer.frameCount,
1123 mConfig.outputCfg.samplingRate,
1124 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001125 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001126 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001127 result.append(buffer);
1128
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001129 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001130 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001131 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001132 for (size_t i = 0; i < mHandles.size(); ++i) {
1133 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001134 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001135 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001136 result.append(buffer);
1137 }
1138 }
1139
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 write(fd, result.string(), result.length());
1141
1142 if (locked) {
1143 mLock.unlock();
1144 }
1145}
1146
1147// ----------------------------------------------------------------------------
1148// EffectHandle implementation
1149// ----------------------------------------------------------------------------
1150
1151#undef LOG_TAG
1152#define LOG_TAG "AudioFlinger::EffectHandle"
1153
1154AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1155 const sp<AudioFlinger::Client>& client,
1156 const sp<IEffectClient>& effectClient,
1157 int32_t priority)
1158 : BnEffect(),
1159 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001160 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001161{
1162 ALOGV("constructor %p", this);
1163
1164 if (client == 0) {
1165 return;
1166 }
1167 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1168 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001169 if (mCblkMemory == 0 ||
1170 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001171 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001172 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001173 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001174 return;
1175 }
Glenn Kastene75da402013-11-20 13:54:52 -08001176 new(mCblk) effect_param_cblk_t();
1177 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001178}
1179
1180AudioFlinger::EffectHandle::~EffectHandle()
1181{
1182 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001183 disconnect(false);
1184}
1185
Glenn Kastene75da402013-11-20 13:54:52 -08001186status_t AudioFlinger::EffectHandle::initCheck()
1187{
1188 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1189}
1190
Eric Laurentca7cc822012-11-19 14:55:58 -08001191status_t AudioFlinger::EffectHandle::enable()
1192{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001193 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001194 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001195 sp<EffectModule> effect = mEffect.promote();
1196 if (effect == 0 || mDisconnected) {
1197 return DEAD_OBJECT;
1198 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001199 if (!mHasControl) {
1200 return INVALID_OPERATION;
1201 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001202
1203 if (mEnabled) {
1204 return NO_ERROR;
1205 }
1206
1207 mEnabled = true;
1208
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001209 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001210 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001211 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001212 }
1213
1214 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001215 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001216 return NO_ERROR;
1217 }
1218
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001219 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001220 if (status != NO_ERROR) {
1221 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001222 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001223 }
1224 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001225 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001226 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001227 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1228 Mutex::Autolock _l(thread->mLock);
1229 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001230 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001231 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001232 if (thread->type() == ThreadBase::OFFLOAD) {
1233 PlaybackThread *t = (PlaybackThread *)thread.get();
1234 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1235 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001236 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001237 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1238 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001239 }
1240 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001241 }
1242 return status;
1243}
1244
1245status_t AudioFlinger::EffectHandle::disable()
1246{
1247 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001248 AutoMutex _l(mLock);
1249 sp<EffectModule> effect = mEffect.promote();
1250 if (effect == 0 || mDisconnected) {
1251 return DEAD_OBJECT;
1252 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001253 if (!mHasControl) {
1254 return INVALID_OPERATION;
1255 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001256
1257 if (!mEnabled) {
1258 return NO_ERROR;
1259 }
1260 mEnabled = false;
1261
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001262 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001263 return NO_ERROR;
1264 }
1265
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001266 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001267
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001268 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001269 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001270 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001271 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1272 Mutex::Autolock _l(thread->mLock);
1273 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001274 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001275 }
1276
1277 return status;
1278}
1279
1280void AudioFlinger::EffectHandle::disconnect()
1281{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001282 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001283 disconnect(true);
1284}
1285
1286void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1287{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001288 AutoMutex _l(mLock);
1289 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1290 if (mDisconnected) {
1291 if (unpinIfLast) {
1292 android_errorWriteLog(0x534e4554, "32707507");
1293 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001294 return;
1295 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001296 mDisconnected = true;
1297 sp<ThreadBase> thread;
1298 {
1299 sp<EffectModule> effect = mEffect.promote();
1300 if (effect != 0) {
1301 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001302 }
1303 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001304 if (thread != 0) {
1305 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001306 } else {
1307 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1308 // try to cleanup as much as we can
1309 sp<EffectModule> effect = mEffect.promote();
1310 if (effect != 0) {
1311 effect->disconnectHandle(this, unpinIfLast);
1312 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001313 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001314
Eric Laurentca7cc822012-11-19 14:55:58 -08001315 if (mClient != 0) {
1316 if (mCblk != NULL) {
1317 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1318 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1319 }
1320 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001321 // Client destructor must run with AudioFlinger client mutex locked
1322 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001323 mClient.clear();
1324 }
1325}
1326
1327status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1328 uint32_t cmdSize,
1329 void *pCmdData,
1330 uint32_t *replySize,
1331 void *pReplyData)
1332{
1333 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001334 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001335
Eric Laurentc7ab3092017-06-15 18:43:46 -07001336 // reject commands reserved for internal use by audio framework if coming from outside
1337 // of audioserver
1338 switch(cmdCode) {
1339 case EFFECT_CMD_ENABLE:
1340 case EFFECT_CMD_DISABLE:
1341 case EFFECT_CMD_SET_PARAM:
1342 case EFFECT_CMD_SET_PARAM_DEFERRED:
1343 case EFFECT_CMD_SET_PARAM_COMMIT:
1344 case EFFECT_CMD_GET_PARAM:
1345 break;
1346 default:
1347 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1348 break;
1349 }
1350 android_errorWriteLog(0x534e4554, "62019992");
1351 return BAD_VALUE;
1352 }
1353
Eric Laurent1ffc5852016-12-15 14:46:09 -08001354 if (cmdCode == EFFECT_CMD_ENABLE) {
1355 if (*replySize < sizeof(int)) {
1356 android_errorWriteLog(0x534e4554, "32095713");
1357 return BAD_VALUE;
1358 }
1359 *(int *)pReplyData = NO_ERROR;
1360 *replySize = sizeof(int);
1361 return enable();
1362 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1363 if (*replySize < sizeof(int)) {
1364 android_errorWriteLog(0x534e4554, "32095713");
1365 return BAD_VALUE;
1366 }
1367 *(int *)pReplyData = NO_ERROR;
1368 *replySize = sizeof(int);
1369 return disable();
1370 }
1371
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001372 AutoMutex _l(mLock);
1373 sp<EffectModule> effect = mEffect.promote();
1374 if (effect == 0 || mDisconnected) {
1375 return DEAD_OBJECT;
1376 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001377 // only get parameter command is permitted for applications not controlling the effect
1378 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1379 return INVALID_OPERATION;
1380 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001381 if (mClient == 0) {
1382 return INVALID_OPERATION;
1383 }
1384
1385 // handle commands that are not forwarded transparently to effect engine
1386 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001387 if (*replySize < sizeof(int)) {
1388 android_errorWriteLog(0x534e4554, "32095713");
1389 return BAD_VALUE;
1390 }
1391 *(int *)pReplyData = NO_ERROR;
1392 *replySize = sizeof(int);
1393
Eric Laurentca7cc822012-11-19 14:55:58 -08001394 // No need to trylock() here as this function is executed in the binder thread serving a
1395 // particular client process: no risk to block the whole media server process or mixer
1396 // threads if we are stuck here
1397 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001398 // keep local copy of index in case of client corruption b/32220769
1399 const uint32_t clientIndex = mCblk->clientIndex;
1400 const uint32_t serverIndex = mCblk->serverIndex;
1401 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1402 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001403 mCblk->serverIndex = 0;
1404 mCblk->clientIndex = 0;
1405 return BAD_VALUE;
1406 }
1407 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001408 effect_param_t *param = NULL;
1409 for (uint32_t index = serverIndex; index < clientIndex;) {
1410 int *p = (int *)(mBuffer + index);
1411 const int size = *p++;
1412 if (size < 0
1413 || size > EFFECT_PARAM_BUFFER_SIZE
1414 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001415 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001416 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 break;
1418 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001419
1420 // copy to local memory in case of client corruption b/32220769
1421 param = (effect_param_t *)realloc(param, size);
1422 if (param == NULL) {
1423 ALOGW("command(): out of memory");
1424 status = NO_MEMORY;
1425 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001426 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001427 memcpy(param, p, size);
1428
1429 int reply = 0;
1430 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001431 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001432 size,
1433 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001434 &rsize,
1435 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001436
1437 // verify shared memory: server index shouldn't change; client index can't go back.
1438 if (serverIndex != mCblk->serverIndex
1439 || clientIndex > mCblk->clientIndex) {
1440 android_errorWriteLog(0x534e4554, "32220769");
1441 status = BAD_VALUE;
1442 break;
1443 }
1444
Eric Laurentca7cc822012-11-19 14:55:58 -08001445 // stop at first error encountered
1446 if (ret != NO_ERROR) {
1447 status = ret;
1448 *(int *)pReplyData = reply;
1449 break;
1450 } else if (reply != NO_ERROR) {
1451 *(int *)pReplyData = reply;
1452 break;
1453 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001454 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001455 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001456 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001457 mCblk->serverIndex = 0;
1458 mCblk->clientIndex = 0;
1459 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001460 }
1461
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001462 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001463}
1464
1465void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1466{
1467 ALOGV("setControl %p control %d", this, hasControl);
1468
1469 mHasControl = hasControl;
1470 mEnabled = enabled;
1471
1472 if (signal && mEffectClient != 0) {
1473 mEffectClient->controlStatusChanged(hasControl);
1474 }
1475}
1476
1477void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1478 uint32_t cmdSize,
1479 void *pCmdData,
1480 uint32_t replySize,
1481 void *pReplyData)
1482{
1483 if (mEffectClient != 0) {
1484 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1485 }
1486}
1487
1488
1489
1490void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1491{
1492 if (mEffectClient != 0) {
1493 mEffectClient->enableStatusChanged(enabled);
1494 }
1495}
1496
1497status_t AudioFlinger::EffectHandle::onTransact(
1498 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1499{
1500 return BnEffect::onTransact(code, data, reply, flags);
1501}
1502
1503
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001504void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001505{
1506 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1507
Marco Nelissenb2208842014-02-07 14:00:50 -08001508 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001509 (mClient == 0) ? getpid_cached : mClient->pid(),
1510 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001511 mHasControl ? "yes" : "no",
1512 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001513 mCblk ? mCblk->clientIndex : 0,
1514 mCblk ? mCblk->serverIndex : 0
1515 );
1516
1517 if (locked) {
1518 mCblk->lock.unlock();
1519 }
1520}
1521
1522#undef LOG_TAG
1523#define LOG_TAG "AudioFlinger::EffectChain"
1524
1525AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001526 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001527 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001528 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001529 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001530{
1531 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1532 if (thread == NULL) {
1533 return;
1534 }
1535 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1536 thread->frameCount();
1537}
1538
1539AudioFlinger::EffectChain::~EffectChain()
1540{
Eric Laurentca7cc822012-11-19 14:55:58 -08001541}
1542
1543// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1544sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1545 effect_descriptor_t *descriptor)
1546{
1547 size_t size = mEffects.size();
1548
1549 for (size_t i = 0; i < size; i++) {
1550 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1551 return mEffects[i];
1552 }
1553 }
1554 return 0;
1555}
1556
1557// getEffectFromId_l() must be called with ThreadBase::mLock held
1558sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1559{
1560 size_t size = mEffects.size();
1561
1562 for (size_t i = 0; i < size; i++) {
1563 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1564 if (id == 0 || mEffects[i]->id() == id) {
1565 return mEffects[i];
1566 }
1567 }
1568 return 0;
1569}
1570
1571// getEffectFromType_l() must be called with ThreadBase::mLock held
1572sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1573 const effect_uuid_t *type)
1574{
1575 size_t size = mEffects.size();
1576
1577 for (size_t i = 0; i < size; i++) {
1578 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1579 return mEffects[i];
1580 }
1581 }
1582 return 0;
1583}
1584
1585void AudioFlinger::EffectChain::clearInputBuffer()
1586{
1587 Mutex::Autolock _l(mLock);
1588 sp<ThreadBase> thread = mThread.promote();
1589 if (thread == 0) {
1590 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1591 return;
1592 }
1593 clearInputBuffer_l(thread);
1594}
1595
1596// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001597void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001598{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001599 if (mInBuffer == NULL) {
1600 return;
1601 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001602 // TODO: This will change in the future, depending on multichannel
1603 // and sample format changes for effects.
1604 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1605 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001606 const size_t frameSize =
1607 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Mikhail Naganov022b9952017-01-04 16:36:51 -08001608 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1609 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001610}
1611
1612// Must be called with EffectChain::mLock locked
1613void AudioFlinger::EffectChain::process_l()
1614{
1615 sp<ThreadBase> thread = mThread.promote();
1616 if (thread == 0) {
1617 ALOGW("process_l(): cannot promote mixer thread");
1618 return;
1619 }
1620 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1621 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001622 // never process effects when:
1623 // - on an OFFLOAD thread
1624 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001625 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1626 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001627 if (!isGlobalSession) {
1628 bool tracksOnSession = (trackCnt() != 0);
1629
1630 if (!tracksOnSession && mTailBufferCount == 0) {
1631 doProcess = false;
1632 }
1633
1634 if (activeTrackCnt() == 0) {
1635 // if no track is active and the effect tail has not been rendered,
1636 // the input buffer must be cleared here as the mixer process will not do it
1637 if (tracksOnSession || mTailBufferCount > 0) {
1638 clearInputBuffer_l(thread);
1639 if (mTailBufferCount > 0) {
1640 mTailBufferCount--;
1641 }
1642 }
1643 }
1644 }
1645
1646 size_t size = mEffects.size();
1647 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001648 // Only the input and output buffers of the chain can be external,
1649 // and 'update' / 'commit' do nothing for allocated buffers, thus
1650 // it's not needed to consider any other buffers here.
1651 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001652 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1653 mOutBuffer->update();
1654 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001655 for (size_t i = 0; i < size; i++) {
1656 mEffects[i]->process();
1657 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001658 mInBuffer->commit();
1659 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1660 mOutBuffer->commit();
1661 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001662 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001663 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001664 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001665 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1666 }
1667 if (doResetVolume) {
1668 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001669 }
1670}
1671
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001672// createEffect_l() must be called with ThreadBase::mLock held
1673status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1674 ThreadBase *thread,
1675 effect_descriptor_t *desc,
1676 int id,
1677 audio_session_t sessionId,
1678 bool pinned)
1679{
1680 Mutex::Autolock _l(mLock);
1681 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1682 status_t lStatus = effect->status();
1683 if (lStatus == NO_ERROR) {
1684 lStatus = addEffect_ll(effect);
1685 }
1686 if (lStatus != NO_ERROR) {
1687 effect.clear();
1688 }
1689 return lStatus;
1690}
1691
1692// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001693status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1694{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001695 Mutex::Autolock _l(mLock);
1696 return addEffect_ll(effect);
1697}
1698// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1699status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1700{
Eric Laurentca7cc822012-11-19 14:55:58 -08001701 effect_descriptor_t desc = effect->desc();
1702 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1703
Eric Laurentca7cc822012-11-19 14:55:58 -08001704 effect->setChain(this);
1705 sp<ThreadBase> thread = mThread.promote();
1706 if (thread == 0) {
1707 return NO_INIT;
1708 }
1709 effect->setThread(thread);
1710
1711 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1712 // Auxiliary effects are inserted at the beginning of mEffects vector as
1713 // they are processed first and accumulated in chain input buffer
1714 mEffects.insertAt(effect, 0);
1715
1716 // the input buffer for auxiliary effect contains mono samples in
1717 // 32 bit format. This is to avoid saturation in AudoMixer
1718 // accumulation stage. Saturation is done in EffectModule::process() before
1719 // calling the process in effect engine
1720 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001721 sp<EffectBufferHalInterface> halBuffer;
1722 status_t result = EffectBufferHalInterface::allocate(
1723 numSamples * sizeof(int32_t), &halBuffer);
1724 if (result != OK) return result;
1725 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001726 // auxiliary effects output samples to chain input buffer for further processing
1727 // by insert effects
1728 effect->setOutBuffer(mInBuffer);
1729 } else {
1730 // Insert effects are inserted at the end of mEffects vector as they are processed
1731 // after track and auxiliary effects.
1732 // Insert effect order as a function of indicated preference:
1733 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1734 // another effect is present
1735 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1736 // last effect claiming first position
1737 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1738 // first effect claiming last position
1739 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1740 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1741 // already present
1742
1743 size_t size = mEffects.size();
1744 size_t idx_insert = size;
1745 ssize_t idx_insert_first = -1;
1746 ssize_t idx_insert_last = -1;
1747
1748 for (size_t i = 0; i < size; i++) {
1749 effect_descriptor_t d = mEffects[i]->desc();
1750 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1751 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1752 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1753 // check invalid effect chaining combinations
1754 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1755 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1756 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1757 desc.name, d.name);
1758 return INVALID_OPERATION;
1759 }
1760 // remember position of first insert effect and by default
1761 // select this as insert position for new effect
1762 if (idx_insert == size) {
1763 idx_insert = i;
1764 }
1765 // remember position of last insert effect claiming
1766 // first position
1767 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1768 idx_insert_first = i;
1769 }
1770 // remember position of first insert effect claiming
1771 // last position
1772 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1773 idx_insert_last == -1) {
1774 idx_insert_last = i;
1775 }
1776 }
1777 }
1778
1779 // modify idx_insert from first position if needed
1780 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1781 if (idx_insert_last != -1) {
1782 idx_insert = idx_insert_last;
1783 } else {
1784 idx_insert = size;
1785 }
1786 } else {
1787 if (idx_insert_first != -1) {
1788 idx_insert = idx_insert_first + 1;
1789 }
1790 }
1791
1792 // always read samples from chain input buffer
1793 effect->setInBuffer(mInBuffer);
1794
1795 // if last effect in the chain, output samples to chain
1796 // output buffer, otherwise to chain input buffer
1797 if (idx_insert == size) {
1798 if (idx_insert != 0) {
1799 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1800 mEffects[idx_insert-1]->configure();
1801 }
1802 effect->setOutBuffer(mOutBuffer);
1803 } else {
1804 effect->setOutBuffer(mInBuffer);
1805 }
1806 mEffects.insertAt(effect, idx_insert);
1807
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001808 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001809 idx_insert);
1810 }
1811 effect->configure();
1812 return NO_ERROR;
1813}
1814
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001815// removeEffect_l() must be called with ThreadBase::mLock held
1816size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1817 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001818{
1819 Mutex::Autolock _l(mLock);
1820 size_t size = mEffects.size();
1821 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1822
1823 for (size_t i = 0; i < size; i++) {
1824 if (effect == mEffects[i]) {
1825 // calling stop here will remove pre-processing effect from the audio HAL.
1826 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1827 // the middle of a read from audio HAL
1828 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1829 mEffects[i]->state() == EffectModule::STOPPING) {
1830 mEffects[i]->stop();
1831 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001832 if (release) {
1833 mEffects[i]->release_l();
1834 }
1835
Mikhail Naganov022b9952017-01-04 16:36:51 -08001836 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001837 if (i == size - 1 && i != 0) {
1838 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1839 mEffects[i - 1]->configure();
1840 }
1841 }
1842 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001843 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001844 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845
Eric Laurentca7cc822012-11-19 14:55:58 -08001846 break;
1847 }
1848 }
1849
1850 return mEffects.size();
1851}
1852
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001853// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001854void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1855{
1856 size_t size = mEffects.size();
1857 for (size_t i = 0; i < size; i++) {
1858 mEffects[i]->setDevice(device);
1859 }
1860}
1861
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001862// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001863void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1864{
1865 size_t size = mEffects.size();
1866 for (size_t i = 0; i < size; i++) {
1867 mEffects[i]->setMode(mode);
1868 }
1869}
1870
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001871// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001872void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1873{
1874 size_t size = mEffects.size();
1875 for (size_t i = 0; i < size; i++) {
1876 mEffects[i]->setAudioSource(source);
1877 }
1878}
1879
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001880// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001881bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001882{
1883 uint32_t newLeft = *left;
1884 uint32_t newRight = *right;
1885 bool hasControl = false;
1886 int ctrlIdx = -1;
1887 size_t size = mEffects.size();
1888
1889 // first update volume controller
1890 for (size_t i = size; i > 0; i--) {
1891 if (mEffects[i - 1]->isProcessEnabled() &&
1892 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1893 ctrlIdx = i - 1;
1894 hasControl = true;
1895 break;
1896 }
1897 }
1898
Eric Laurentfa1e1232016-08-02 19:01:49 -07001899 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001900 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001901 if (hasControl) {
1902 *left = mNewLeftVolume;
1903 *right = mNewRightVolume;
1904 }
1905 return hasControl;
1906 }
1907
1908 mVolumeCtrlIdx = ctrlIdx;
1909 mLeftVolume = newLeft;
1910 mRightVolume = newRight;
1911
1912 // second get volume update from volume controller
1913 if (ctrlIdx >= 0) {
1914 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1915 mNewLeftVolume = newLeft;
1916 mNewRightVolume = newRight;
1917 }
1918 // then indicate volume to all other effects in chain.
1919 // Pass altered volume to effects before volume controller
1920 // and requested volume to effects after controller
1921 uint32_t lVol = newLeft;
1922 uint32_t rVol = newRight;
1923
1924 for (size_t i = 0; i < size; i++) {
1925 if ((int)i == ctrlIdx) {
1926 continue;
1927 }
1928 // this also works for ctrlIdx == -1 when there is no volume controller
1929 if ((int)i > ctrlIdx) {
1930 lVol = *left;
1931 rVol = *right;
1932 }
1933 mEffects[i]->setVolume(&lVol, &rVol, false);
1934 }
1935 *left = newLeft;
1936 *right = newRight;
1937
1938 return hasControl;
1939}
1940
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001941// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001942void AudioFlinger::EffectChain::resetVolume_l()
1943{
Eric Laurente7449bf2016-08-03 18:44:07 -07001944 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1945 uint32_t left = mLeftVolume;
1946 uint32_t right = mRightVolume;
1947 (void)setVolume_l(&left, &right, true);
1948 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001949}
1950
Eric Laurent1b928682014-10-02 19:41:47 -07001951void AudioFlinger::EffectChain::syncHalEffectsState()
1952{
1953 Mutex::Autolock _l(mLock);
1954 for (size_t i = 0; i < mEffects.size(); i++) {
1955 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1956 mEffects[i]->state() == EffectModule::STOPPING) {
1957 mEffects[i]->addEffectToHal_l();
1958 }
1959 }
1960}
1961
Mikhail Naganov06888802017-01-19 12:47:55 -08001962static void dumpInOutBuffer(
1963 char *dump, size_t dumpSize, bool isInput, EffectBufferHalInterface *buffer) {
Mikhail Naganovc778e592017-01-25 10:35:30 -08001964 if (buffer == nullptr) {
1965 snprintf(dump, dumpSize, "%p", buffer);
1966 } else if (buffer->externalData() != nullptr) {
Mikhail Naganov06888802017-01-19 12:47:55 -08001967 snprintf(dump, dumpSize, "%p -> %p",
1968 isInput ? buffer->externalData() : buffer->audioBuffer()->raw,
1969 isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1970 } else {
1971 snprintf(dump, dumpSize, "%p", buffer->audioBuffer()->raw);
1972 }
1973}
1974
Eric Laurentca7cc822012-11-19 14:55:58 -08001975void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1976{
1977 const size_t SIZE = 256;
1978 char buffer[SIZE];
1979 String8 result;
1980
Marco Nelissenb2208842014-02-07 14:00:50 -08001981 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001982 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001983 result.append(buffer);
1984
Marco Nelissenb2208842014-02-07 14:00:50 -08001985 if (numEffects) {
1986 bool locked = AudioFlinger::dumpTryLock(mLock);
1987 // failed to lock - AudioFlinger is probably deadlocked
1988 if (!locked) {
1989 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001990 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001991
Mikhail Naganov06888802017-01-19 12:47:55 -08001992 char inBufferStr[64], outBufferStr[64];
1993 dumpInOutBuffer(inBufferStr, sizeof(inBufferStr), true, mInBuffer.get());
1994 dumpInOutBuffer(outBufferStr, sizeof(outBufferStr), false, mOutBuffer.get());
1995 snprintf(buffer, SIZE, "\t%-*s%-*s Active tracks:\n",
1996 (int)strlen(inBufferStr), "In buffer ",
1997 (int)strlen(outBufferStr), "Out buffer ");
1998 result.append(buffer);
1999 snprintf(buffer, SIZE, "\t%s %s %d\n", inBufferStr, outBufferStr, mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002000 result.append(buffer);
2001 write(fd, result.string(), result.size());
2002
2003 for (size_t i = 0; i < numEffects; ++i) {
2004 sp<EffectModule> effect = mEffects[i];
2005 if (effect != 0) {
2006 effect->dump(fd, args);
2007 }
2008 }
2009
2010 if (locked) {
2011 mLock.unlock();
2012 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002013 }
2014}
2015
2016// must be called with ThreadBase::mLock held
2017void AudioFlinger::EffectChain::setEffectSuspended_l(
2018 const effect_uuid_t *type, bool suspend)
2019{
2020 sp<SuspendedEffectDesc> desc;
2021 // use effect type UUID timelow as key as there is no real risk of identical
2022 // timeLow fields among effect type UUIDs.
2023 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2024 if (suspend) {
2025 if (index >= 0) {
2026 desc = mSuspendedEffects.valueAt(index);
2027 } else {
2028 desc = new SuspendedEffectDesc();
2029 desc->mType = *type;
2030 mSuspendedEffects.add(type->timeLow, desc);
2031 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2032 }
2033 if (desc->mRefCount++ == 0) {
2034 sp<EffectModule> effect = getEffectIfEnabled(type);
2035 if (effect != 0) {
2036 desc->mEffect = effect;
2037 effect->setSuspended(true);
2038 effect->setEnabled(false);
2039 }
2040 }
2041 } else {
2042 if (index < 0) {
2043 return;
2044 }
2045 desc = mSuspendedEffects.valueAt(index);
2046 if (desc->mRefCount <= 0) {
2047 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
2048 desc->mRefCount = 1;
2049 }
2050 if (--desc->mRefCount == 0) {
2051 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2052 if (desc->mEffect != 0) {
2053 sp<EffectModule> effect = desc->mEffect.promote();
2054 if (effect != 0) {
2055 effect->setSuspended(false);
2056 effect->lock();
2057 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002058 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002059 effect->setEnabled_l(handle->enabled());
2060 }
2061 effect->unlock();
2062 }
2063 desc->mEffect.clear();
2064 }
2065 mSuspendedEffects.removeItemsAt(index);
2066 }
2067 }
2068}
2069
2070// must be called with ThreadBase::mLock held
2071void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2072{
2073 sp<SuspendedEffectDesc> desc;
2074
2075 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2076 if (suspend) {
2077 if (index >= 0) {
2078 desc = mSuspendedEffects.valueAt(index);
2079 } else {
2080 desc = new SuspendedEffectDesc();
2081 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2082 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2083 }
2084 if (desc->mRefCount++ == 0) {
2085 Vector< sp<EffectModule> > effects;
2086 getSuspendEligibleEffects(effects);
2087 for (size_t i = 0; i < effects.size(); i++) {
2088 setEffectSuspended_l(&effects[i]->desc().type, true);
2089 }
2090 }
2091 } else {
2092 if (index < 0) {
2093 return;
2094 }
2095 desc = mSuspendedEffects.valueAt(index);
2096 if (desc->mRefCount <= 0) {
2097 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2098 desc->mRefCount = 1;
2099 }
2100 if (--desc->mRefCount == 0) {
2101 Vector<const effect_uuid_t *> types;
2102 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2103 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2104 continue;
2105 }
2106 types.add(&mSuspendedEffects.valueAt(i)->mType);
2107 }
2108 for (size_t i = 0; i < types.size(); i++) {
2109 setEffectSuspended_l(types[i], false);
2110 }
2111 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2112 mSuspendedEffects.keyAt(index));
2113 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2114 }
2115 }
2116}
2117
2118
2119// The volume effect is used for automated tests only
2120#ifndef OPENSL_ES_H_
2121static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2122 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2123const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2124#endif //OPENSL_ES_H_
2125
2126bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2127{
2128 // auxiliary effects and visualizer are never suspended on output mix
2129 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2130 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2131 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2132 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2133 return false;
2134 }
2135 return true;
2136}
2137
2138void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2139 Vector< sp<AudioFlinger::EffectModule> > &effects)
2140{
2141 effects.clear();
2142 for (size_t i = 0; i < mEffects.size(); i++) {
2143 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2144 effects.add(mEffects[i]);
2145 }
2146 }
2147}
2148
2149sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2150 const effect_uuid_t *type)
2151{
2152 sp<EffectModule> effect = getEffectFromType_l(type);
2153 return effect != 0 && effect->isEnabled() ? effect : 0;
2154}
2155
2156void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2157 bool enabled)
2158{
2159 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2160 if (enabled) {
2161 if (index < 0) {
2162 // if the effect is not suspend check if all effects are suspended
2163 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2164 if (index < 0) {
2165 return;
2166 }
2167 if (!isEffectEligibleForSuspend(effect->desc())) {
2168 return;
2169 }
2170 setEffectSuspended_l(&effect->desc().type, enabled);
2171 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2172 if (index < 0) {
2173 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2174 return;
2175 }
2176 }
2177 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2178 effect->desc().type.timeLow);
2179 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2180 // if effect is requested to suspended but was not yet enabled, supend it now.
2181 if (desc->mEffect == 0) {
2182 desc->mEffect = effect;
2183 effect->setEnabled(false);
2184 effect->setSuspended(true);
2185 }
2186 } else {
2187 if (index < 0) {
2188 return;
2189 }
2190 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2191 effect->desc().type.timeLow);
2192 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2193 desc->mEffect.clear();
2194 effect->setSuspended(false);
2195 }
2196}
2197
Eric Laurent5baf2af2013-09-12 17:37:00 -07002198bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002199{
2200 Mutex::Autolock _l(mLock);
2201 size_t size = mEffects.size();
2202 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002203 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002204 return true;
2205 }
2206 }
2207 return false;
2208}
2209
Eric Laurentaaa44472014-09-12 17:41:50 -07002210void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2211{
2212 Mutex::Autolock _l(mLock);
2213 mThread = thread;
2214 for (size_t i = 0; i < mEffects.size(); i++) {
2215 mEffects[i]->setThread(thread);
2216 }
2217}
2218
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002219void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2220{
2221 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2222 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2223 }
2224 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2225 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2226 }
2227}
2228
2229void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2230{
2231 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2232 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2233 }
2234 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2235 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2236 }
2237}
2238
2239bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002240{
2241 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002242 for (const auto &effect : mEffects) {
2243 if (effect->isProcessImplemented()) {
2244 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002245 }
2246 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002247 // Allow effects without processing.
2248 return true;
2249}
2250
2251bool AudioFlinger::EffectChain::isFastCompatible() const
2252{
2253 Mutex::Autolock _l(mLock);
2254 for (const auto &effect : mEffects) {
2255 if (effect->isProcessImplemented()
2256 && effect->isImplementationSoftware()) {
2257 return false;
2258 }
2259 }
2260 // Allow effects without processing or hw accelerated effects.
2261 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002262}
2263
2264// isCompatibleWithThread_l() must be called with thread->mLock held
2265bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2266{
2267 Mutex::Autolock _l(mLock);
2268 for (size_t i = 0; i < mEffects.size(); i++) {
2269 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2270 return false;
2271 }
2272 }
2273 return true;
2274}
2275
Glenn Kasten63238ef2015-03-02 15:50:29 -08002276} // namespace android