blob: f1a55f166002fe0f1cccd5292d0a215617929288 [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 Laurent1ffc5852016-12-15 14:46:09 -08001336 if (cmdCode == EFFECT_CMD_ENABLE) {
1337 if (*replySize < sizeof(int)) {
1338 android_errorWriteLog(0x534e4554, "32095713");
1339 return BAD_VALUE;
1340 }
1341 *(int *)pReplyData = NO_ERROR;
1342 *replySize = sizeof(int);
1343 return enable();
1344 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1345 if (*replySize < sizeof(int)) {
1346 android_errorWriteLog(0x534e4554, "32095713");
1347 return BAD_VALUE;
1348 }
1349 *(int *)pReplyData = NO_ERROR;
1350 *replySize = sizeof(int);
1351 return disable();
1352 }
1353
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001354 AutoMutex _l(mLock);
1355 sp<EffectModule> effect = mEffect.promote();
1356 if (effect == 0 || mDisconnected) {
1357 return DEAD_OBJECT;
1358 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001359 // only get parameter command is permitted for applications not controlling the effect
1360 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1361 return INVALID_OPERATION;
1362 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001363 if (mClient == 0) {
1364 return INVALID_OPERATION;
1365 }
1366
1367 // handle commands that are not forwarded transparently to effect engine
1368 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001369 if (*replySize < sizeof(int)) {
1370 android_errorWriteLog(0x534e4554, "32095713");
1371 return BAD_VALUE;
1372 }
1373 *(int *)pReplyData = NO_ERROR;
1374 *replySize = sizeof(int);
1375
Eric Laurentca7cc822012-11-19 14:55:58 -08001376 // No need to trylock() here as this function is executed in the binder thread serving a
1377 // particular client process: no risk to block the whole media server process or mixer
1378 // threads if we are stuck here
1379 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001380 // keep local copy of index in case of client corruption b/32220769
1381 const uint32_t clientIndex = mCblk->clientIndex;
1382 const uint32_t serverIndex = mCblk->serverIndex;
1383 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1384 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001385 mCblk->serverIndex = 0;
1386 mCblk->clientIndex = 0;
1387 return BAD_VALUE;
1388 }
1389 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001390 effect_param_t *param = NULL;
1391 for (uint32_t index = serverIndex; index < clientIndex;) {
1392 int *p = (int *)(mBuffer + index);
1393 const int size = *p++;
1394 if (size < 0
1395 || size > EFFECT_PARAM_BUFFER_SIZE
1396 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001397 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001398 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001399 break;
1400 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001401
1402 // copy to local memory in case of client corruption b/32220769
1403 param = (effect_param_t *)realloc(param, size);
1404 if (param == NULL) {
1405 ALOGW("command(): out of memory");
1406 status = NO_MEMORY;
1407 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001408 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001409 memcpy(param, p, size);
1410
1411 int reply = 0;
1412 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001413 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001414 size,
1415 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001416 &rsize,
1417 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001418
1419 // verify shared memory: server index shouldn't change; client index can't go back.
1420 if (serverIndex != mCblk->serverIndex
1421 || clientIndex > mCblk->clientIndex) {
1422 android_errorWriteLog(0x534e4554, "32220769");
1423 status = BAD_VALUE;
1424 break;
1425 }
1426
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 // stop at first error encountered
1428 if (ret != NO_ERROR) {
1429 status = ret;
1430 *(int *)pReplyData = reply;
1431 break;
1432 } else if (reply != NO_ERROR) {
1433 *(int *)pReplyData = reply;
1434 break;
1435 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001436 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001437 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001438 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001439 mCblk->serverIndex = 0;
1440 mCblk->clientIndex = 0;
1441 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001442 }
1443
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001444 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001445}
1446
1447void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1448{
1449 ALOGV("setControl %p control %d", this, hasControl);
1450
1451 mHasControl = hasControl;
1452 mEnabled = enabled;
1453
1454 if (signal && mEffectClient != 0) {
1455 mEffectClient->controlStatusChanged(hasControl);
1456 }
1457}
1458
1459void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1460 uint32_t cmdSize,
1461 void *pCmdData,
1462 uint32_t replySize,
1463 void *pReplyData)
1464{
1465 if (mEffectClient != 0) {
1466 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1467 }
1468}
1469
1470
1471
1472void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1473{
1474 if (mEffectClient != 0) {
1475 mEffectClient->enableStatusChanged(enabled);
1476 }
1477}
1478
1479status_t AudioFlinger::EffectHandle::onTransact(
1480 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1481{
1482 return BnEffect::onTransact(code, data, reply, flags);
1483}
1484
1485
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001486void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001487{
1488 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1489
Marco Nelissenb2208842014-02-07 14:00:50 -08001490 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001491 (mClient == 0) ? getpid_cached : mClient->pid(),
1492 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001493 mHasControl ? "yes" : "no",
1494 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001495 mCblk ? mCblk->clientIndex : 0,
1496 mCblk ? mCblk->serverIndex : 0
1497 );
1498
1499 if (locked) {
1500 mCblk->lock.unlock();
1501 }
1502}
1503
1504#undef LOG_TAG
1505#define LOG_TAG "AudioFlinger::EffectChain"
1506
1507AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001508 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001509 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001510 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001511 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001512{
1513 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1514 if (thread == NULL) {
1515 return;
1516 }
1517 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1518 thread->frameCount();
1519}
1520
1521AudioFlinger::EffectChain::~EffectChain()
1522{
Eric Laurentca7cc822012-11-19 14:55:58 -08001523}
1524
1525// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1526sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1527 effect_descriptor_t *descriptor)
1528{
1529 size_t size = mEffects.size();
1530
1531 for (size_t i = 0; i < size; i++) {
1532 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1533 return mEffects[i];
1534 }
1535 }
1536 return 0;
1537}
1538
1539// getEffectFromId_l() must be called with ThreadBase::mLock held
1540sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1541{
1542 size_t size = mEffects.size();
1543
1544 for (size_t i = 0; i < size; i++) {
1545 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1546 if (id == 0 || mEffects[i]->id() == id) {
1547 return mEffects[i];
1548 }
1549 }
1550 return 0;
1551}
1552
1553// getEffectFromType_l() must be called with ThreadBase::mLock held
1554sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1555 const effect_uuid_t *type)
1556{
1557 size_t size = mEffects.size();
1558
1559 for (size_t i = 0; i < size; i++) {
1560 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1561 return mEffects[i];
1562 }
1563 }
1564 return 0;
1565}
1566
1567void AudioFlinger::EffectChain::clearInputBuffer()
1568{
1569 Mutex::Autolock _l(mLock);
1570 sp<ThreadBase> thread = mThread.promote();
1571 if (thread == 0) {
1572 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1573 return;
1574 }
1575 clearInputBuffer_l(thread);
1576}
1577
1578// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001579void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001580{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001581 if (mInBuffer == NULL) {
1582 return;
1583 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001584 // TODO: This will change in the future, depending on multichannel
1585 // and sample format changes for effects.
1586 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1587 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001588 const size_t frameSize =
1589 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Mikhail Naganov022b9952017-01-04 16:36:51 -08001590 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1591 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001592}
1593
1594// Must be called with EffectChain::mLock locked
1595void AudioFlinger::EffectChain::process_l()
1596{
1597 sp<ThreadBase> thread = mThread.promote();
1598 if (thread == 0) {
1599 ALOGW("process_l(): cannot promote mixer thread");
1600 return;
1601 }
1602 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1603 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001604 // never process effects when:
1605 // - on an OFFLOAD thread
1606 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001607 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1608 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001609 if (!isGlobalSession) {
1610 bool tracksOnSession = (trackCnt() != 0);
1611
1612 if (!tracksOnSession && mTailBufferCount == 0) {
1613 doProcess = false;
1614 }
1615
1616 if (activeTrackCnt() == 0) {
1617 // if no track is active and the effect tail has not been rendered,
1618 // the input buffer must be cleared here as the mixer process will not do it
1619 if (tracksOnSession || mTailBufferCount > 0) {
1620 clearInputBuffer_l(thread);
1621 if (mTailBufferCount > 0) {
1622 mTailBufferCount--;
1623 }
1624 }
1625 }
1626 }
1627
1628 size_t size = mEffects.size();
1629 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001630 // Only the input and output buffers of the chain can be external,
1631 // and 'update' / 'commit' do nothing for allocated buffers, thus
1632 // it's not needed to consider any other buffers here.
1633 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001634 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1635 mOutBuffer->update();
1636 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001637 for (size_t i = 0; i < size; i++) {
1638 mEffects[i]->process();
1639 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001640 mInBuffer->commit();
1641 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1642 mOutBuffer->commit();
1643 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001644 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001645 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001646 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001647 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1648 }
1649 if (doResetVolume) {
1650 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001651 }
1652}
1653
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001654// createEffect_l() must be called with ThreadBase::mLock held
1655status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1656 ThreadBase *thread,
1657 effect_descriptor_t *desc,
1658 int id,
1659 audio_session_t sessionId,
1660 bool pinned)
1661{
1662 Mutex::Autolock _l(mLock);
1663 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1664 status_t lStatus = effect->status();
1665 if (lStatus == NO_ERROR) {
1666 lStatus = addEffect_ll(effect);
1667 }
1668 if (lStatus != NO_ERROR) {
1669 effect.clear();
1670 }
1671 return lStatus;
1672}
1673
1674// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001675status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1676{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001677 Mutex::Autolock _l(mLock);
1678 return addEffect_ll(effect);
1679}
1680// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1681status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1682{
Eric Laurentca7cc822012-11-19 14:55:58 -08001683 effect_descriptor_t desc = effect->desc();
1684 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1685
Eric Laurentca7cc822012-11-19 14:55:58 -08001686 effect->setChain(this);
1687 sp<ThreadBase> thread = mThread.promote();
1688 if (thread == 0) {
1689 return NO_INIT;
1690 }
1691 effect->setThread(thread);
1692
1693 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1694 // Auxiliary effects are inserted at the beginning of mEffects vector as
1695 // they are processed first and accumulated in chain input buffer
1696 mEffects.insertAt(effect, 0);
1697
1698 // the input buffer for auxiliary effect contains mono samples in
1699 // 32 bit format. This is to avoid saturation in AudoMixer
1700 // accumulation stage. Saturation is done in EffectModule::process() before
1701 // calling the process in effect engine
1702 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001703 sp<EffectBufferHalInterface> halBuffer;
1704 status_t result = EffectBufferHalInterface::allocate(
1705 numSamples * sizeof(int32_t), &halBuffer);
1706 if (result != OK) return result;
1707 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001708 // auxiliary effects output samples to chain input buffer for further processing
1709 // by insert effects
1710 effect->setOutBuffer(mInBuffer);
1711 } else {
1712 // Insert effects are inserted at the end of mEffects vector as they are processed
1713 // after track and auxiliary effects.
1714 // Insert effect order as a function of indicated preference:
1715 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1716 // another effect is present
1717 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1718 // last effect claiming first position
1719 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1720 // first effect claiming last position
1721 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1722 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1723 // already present
1724
1725 size_t size = mEffects.size();
1726 size_t idx_insert = size;
1727 ssize_t idx_insert_first = -1;
1728 ssize_t idx_insert_last = -1;
1729
1730 for (size_t i = 0; i < size; i++) {
1731 effect_descriptor_t d = mEffects[i]->desc();
1732 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1733 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1734 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1735 // check invalid effect chaining combinations
1736 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1737 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1738 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1739 desc.name, d.name);
1740 return INVALID_OPERATION;
1741 }
1742 // remember position of first insert effect and by default
1743 // select this as insert position for new effect
1744 if (idx_insert == size) {
1745 idx_insert = i;
1746 }
1747 // remember position of last insert effect claiming
1748 // first position
1749 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1750 idx_insert_first = i;
1751 }
1752 // remember position of first insert effect claiming
1753 // last position
1754 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1755 idx_insert_last == -1) {
1756 idx_insert_last = i;
1757 }
1758 }
1759 }
1760
1761 // modify idx_insert from first position if needed
1762 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1763 if (idx_insert_last != -1) {
1764 idx_insert = idx_insert_last;
1765 } else {
1766 idx_insert = size;
1767 }
1768 } else {
1769 if (idx_insert_first != -1) {
1770 idx_insert = idx_insert_first + 1;
1771 }
1772 }
1773
1774 // always read samples from chain input buffer
1775 effect->setInBuffer(mInBuffer);
1776
1777 // if last effect in the chain, output samples to chain
1778 // output buffer, otherwise to chain input buffer
1779 if (idx_insert == size) {
1780 if (idx_insert != 0) {
1781 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1782 mEffects[idx_insert-1]->configure();
1783 }
1784 effect->setOutBuffer(mOutBuffer);
1785 } else {
1786 effect->setOutBuffer(mInBuffer);
1787 }
1788 mEffects.insertAt(effect, idx_insert);
1789
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001790 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001791 idx_insert);
1792 }
1793 effect->configure();
1794 return NO_ERROR;
1795}
1796
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001797// removeEffect_l() must be called with ThreadBase::mLock held
1798size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1799 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001800{
1801 Mutex::Autolock _l(mLock);
1802 size_t size = mEffects.size();
1803 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1804
1805 for (size_t i = 0; i < size; i++) {
1806 if (effect == mEffects[i]) {
1807 // calling stop here will remove pre-processing effect from the audio HAL.
1808 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1809 // the middle of a read from audio HAL
1810 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1811 mEffects[i]->state() == EffectModule::STOPPING) {
1812 mEffects[i]->stop();
1813 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001814 if (release) {
1815 mEffects[i]->release_l();
1816 }
1817
Mikhail Naganov022b9952017-01-04 16:36:51 -08001818 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001819 if (i == size - 1 && i != 0) {
1820 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1821 mEffects[i - 1]->configure();
1822 }
1823 }
1824 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001825 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001826 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001827
Eric Laurentca7cc822012-11-19 14:55:58 -08001828 break;
1829 }
1830 }
1831
1832 return mEffects.size();
1833}
1834
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001835// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001836void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1837{
1838 size_t size = mEffects.size();
1839 for (size_t i = 0; i < size; i++) {
1840 mEffects[i]->setDevice(device);
1841 }
1842}
1843
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001844// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001845void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1846{
1847 size_t size = mEffects.size();
1848 for (size_t i = 0; i < size; i++) {
1849 mEffects[i]->setMode(mode);
1850 }
1851}
1852
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001853// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001854void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1855{
1856 size_t size = mEffects.size();
1857 for (size_t i = 0; i < size; i++) {
1858 mEffects[i]->setAudioSource(source);
1859 }
1860}
1861
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001862// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001863bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001864{
1865 uint32_t newLeft = *left;
1866 uint32_t newRight = *right;
1867 bool hasControl = false;
1868 int ctrlIdx = -1;
1869 size_t size = mEffects.size();
1870
1871 // first update volume controller
1872 for (size_t i = size; i > 0; i--) {
1873 if (mEffects[i - 1]->isProcessEnabled() &&
1874 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1875 ctrlIdx = i - 1;
1876 hasControl = true;
1877 break;
1878 }
1879 }
1880
Eric Laurentfa1e1232016-08-02 19:01:49 -07001881 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001882 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001883 if (hasControl) {
1884 *left = mNewLeftVolume;
1885 *right = mNewRightVolume;
1886 }
1887 return hasControl;
1888 }
1889
1890 mVolumeCtrlIdx = ctrlIdx;
1891 mLeftVolume = newLeft;
1892 mRightVolume = newRight;
1893
1894 // second get volume update from volume controller
1895 if (ctrlIdx >= 0) {
1896 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1897 mNewLeftVolume = newLeft;
1898 mNewRightVolume = newRight;
1899 }
1900 // then indicate volume to all other effects in chain.
1901 // Pass altered volume to effects before volume controller
1902 // and requested volume to effects after controller
1903 uint32_t lVol = newLeft;
1904 uint32_t rVol = newRight;
1905
1906 for (size_t i = 0; i < size; i++) {
1907 if ((int)i == ctrlIdx) {
1908 continue;
1909 }
1910 // this also works for ctrlIdx == -1 when there is no volume controller
1911 if ((int)i > ctrlIdx) {
1912 lVol = *left;
1913 rVol = *right;
1914 }
1915 mEffects[i]->setVolume(&lVol, &rVol, false);
1916 }
1917 *left = newLeft;
1918 *right = newRight;
1919
1920 return hasControl;
1921}
1922
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001923// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001924void AudioFlinger::EffectChain::resetVolume_l()
1925{
Eric Laurente7449bf2016-08-03 18:44:07 -07001926 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1927 uint32_t left = mLeftVolume;
1928 uint32_t right = mRightVolume;
1929 (void)setVolume_l(&left, &right, true);
1930 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001931}
1932
Eric Laurent1b928682014-10-02 19:41:47 -07001933void AudioFlinger::EffectChain::syncHalEffectsState()
1934{
1935 Mutex::Autolock _l(mLock);
1936 for (size_t i = 0; i < mEffects.size(); i++) {
1937 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1938 mEffects[i]->state() == EffectModule::STOPPING) {
1939 mEffects[i]->addEffectToHal_l();
1940 }
1941 }
1942}
1943
Mikhail Naganov06888802017-01-19 12:47:55 -08001944static void dumpInOutBuffer(
1945 char *dump, size_t dumpSize, bool isInput, EffectBufferHalInterface *buffer) {
Mikhail Naganovc778e592017-01-25 10:35:30 -08001946 if (buffer == nullptr) {
1947 snprintf(dump, dumpSize, "%p", buffer);
1948 } else if (buffer->externalData() != nullptr) {
Mikhail Naganov06888802017-01-19 12:47:55 -08001949 snprintf(dump, dumpSize, "%p -> %p",
1950 isInput ? buffer->externalData() : buffer->audioBuffer()->raw,
1951 isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1952 } else {
1953 snprintf(dump, dumpSize, "%p", buffer->audioBuffer()->raw);
1954 }
1955}
1956
Eric Laurentca7cc822012-11-19 14:55:58 -08001957void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1958{
1959 const size_t SIZE = 256;
1960 char buffer[SIZE];
1961 String8 result;
1962
Marco Nelissenb2208842014-02-07 14:00:50 -08001963 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001964 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001965 result.append(buffer);
1966
Marco Nelissenb2208842014-02-07 14:00:50 -08001967 if (numEffects) {
1968 bool locked = AudioFlinger::dumpTryLock(mLock);
1969 // failed to lock - AudioFlinger is probably deadlocked
1970 if (!locked) {
1971 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001972 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001973
Mikhail Naganov06888802017-01-19 12:47:55 -08001974 char inBufferStr[64], outBufferStr[64];
1975 dumpInOutBuffer(inBufferStr, sizeof(inBufferStr), true, mInBuffer.get());
1976 dumpInOutBuffer(outBufferStr, sizeof(outBufferStr), false, mOutBuffer.get());
1977 snprintf(buffer, SIZE, "\t%-*s%-*s Active tracks:\n",
1978 (int)strlen(inBufferStr), "In buffer ",
1979 (int)strlen(outBufferStr), "Out buffer ");
1980 result.append(buffer);
1981 snprintf(buffer, SIZE, "\t%s %s %d\n", inBufferStr, outBufferStr, mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08001982 result.append(buffer);
1983 write(fd, result.string(), result.size());
1984
1985 for (size_t i = 0; i < numEffects; ++i) {
1986 sp<EffectModule> effect = mEffects[i];
1987 if (effect != 0) {
1988 effect->dump(fd, args);
1989 }
1990 }
1991
1992 if (locked) {
1993 mLock.unlock();
1994 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001995 }
1996}
1997
1998// must be called with ThreadBase::mLock held
1999void AudioFlinger::EffectChain::setEffectSuspended_l(
2000 const effect_uuid_t *type, bool suspend)
2001{
2002 sp<SuspendedEffectDesc> desc;
2003 // use effect type UUID timelow as key as there is no real risk of identical
2004 // timeLow fields among effect type UUIDs.
2005 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2006 if (suspend) {
2007 if (index >= 0) {
2008 desc = mSuspendedEffects.valueAt(index);
2009 } else {
2010 desc = new SuspendedEffectDesc();
2011 desc->mType = *type;
2012 mSuspendedEffects.add(type->timeLow, desc);
2013 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2014 }
2015 if (desc->mRefCount++ == 0) {
2016 sp<EffectModule> effect = getEffectIfEnabled(type);
2017 if (effect != 0) {
2018 desc->mEffect = effect;
2019 effect->setSuspended(true);
2020 effect->setEnabled(false);
2021 }
2022 }
2023 } else {
2024 if (index < 0) {
2025 return;
2026 }
2027 desc = mSuspendedEffects.valueAt(index);
2028 if (desc->mRefCount <= 0) {
2029 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
2030 desc->mRefCount = 1;
2031 }
2032 if (--desc->mRefCount == 0) {
2033 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2034 if (desc->mEffect != 0) {
2035 sp<EffectModule> effect = desc->mEffect.promote();
2036 if (effect != 0) {
2037 effect->setSuspended(false);
2038 effect->lock();
2039 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002040 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002041 effect->setEnabled_l(handle->enabled());
2042 }
2043 effect->unlock();
2044 }
2045 desc->mEffect.clear();
2046 }
2047 mSuspendedEffects.removeItemsAt(index);
2048 }
2049 }
2050}
2051
2052// must be called with ThreadBase::mLock held
2053void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2054{
2055 sp<SuspendedEffectDesc> desc;
2056
2057 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2058 if (suspend) {
2059 if (index >= 0) {
2060 desc = mSuspendedEffects.valueAt(index);
2061 } else {
2062 desc = new SuspendedEffectDesc();
2063 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2064 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2065 }
2066 if (desc->mRefCount++ == 0) {
2067 Vector< sp<EffectModule> > effects;
2068 getSuspendEligibleEffects(effects);
2069 for (size_t i = 0; i < effects.size(); i++) {
2070 setEffectSuspended_l(&effects[i]->desc().type, true);
2071 }
2072 }
2073 } else {
2074 if (index < 0) {
2075 return;
2076 }
2077 desc = mSuspendedEffects.valueAt(index);
2078 if (desc->mRefCount <= 0) {
2079 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2080 desc->mRefCount = 1;
2081 }
2082 if (--desc->mRefCount == 0) {
2083 Vector<const effect_uuid_t *> types;
2084 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2085 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2086 continue;
2087 }
2088 types.add(&mSuspendedEffects.valueAt(i)->mType);
2089 }
2090 for (size_t i = 0; i < types.size(); i++) {
2091 setEffectSuspended_l(types[i], false);
2092 }
2093 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2094 mSuspendedEffects.keyAt(index));
2095 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2096 }
2097 }
2098}
2099
2100
2101// The volume effect is used for automated tests only
2102#ifndef OPENSL_ES_H_
2103static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2104 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2105const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2106#endif //OPENSL_ES_H_
2107
2108bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2109{
2110 // auxiliary effects and visualizer are never suspended on output mix
2111 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2112 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2113 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2114 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2115 return false;
2116 }
2117 return true;
2118}
2119
2120void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2121 Vector< sp<AudioFlinger::EffectModule> > &effects)
2122{
2123 effects.clear();
2124 for (size_t i = 0; i < mEffects.size(); i++) {
2125 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2126 effects.add(mEffects[i]);
2127 }
2128 }
2129}
2130
2131sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2132 const effect_uuid_t *type)
2133{
2134 sp<EffectModule> effect = getEffectFromType_l(type);
2135 return effect != 0 && effect->isEnabled() ? effect : 0;
2136}
2137
2138void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2139 bool enabled)
2140{
2141 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2142 if (enabled) {
2143 if (index < 0) {
2144 // if the effect is not suspend check if all effects are suspended
2145 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2146 if (index < 0) {
2147 return;
2148 }
2149 if (!isEffectEligibleForSuspend(effect->desc())) {
2150 return;
2151 }
2152 setEffectSuspended_l(&effect->desc().type, enabled);
2153 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2154 if (index < 0) {
2155 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2156 return;
2157 }
2158 }
2159 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2160 effect->desc().type.timeLow);
2161 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2162 // if effect is requested to suspended but was not yet enabled, supend it now.
2163 if (desc->mEffect == 0) {
2164 desc->mEffect = effect;
2165 effect->setEnabled(false);
2166 effect->setSuspended(true);
2167 }
2168 } else {
2169 if (index < 0) {
2170 return;
2171 }
2172 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2173 effect->desc().type.timeLow);
2174 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2175 desc->mEffect.clear();
2176 effect->setSuspended(false);
2177 }
2178}
2179
Eric Laurent5baf2af2013-09-12 17:37:00 -07002180bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002181{
2182 Mutex::Autolock _l(mLock);
2183 size_t size = mEffects.size();
2184 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002185 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002186 return true;
2187 }
2188 }
2189 return false;
2190}
2191
Eric Laurentaaa44472014-09-12 17:41:50 -07002192void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2193{
2194 Mutex::Autolock _l(mLock);
2195 mThread = thread;
2196 for (size_t i = 0; i < mEffects.size(); i++) {
2197 mEffects[i]->setThread(thread);
2198 }
2199}
2200
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002201void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2202{
2203 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2204 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2205 }
2206 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2207 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2208 }
2209}
2210
2211void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2212{
2213 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2214 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2215 }
2216 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2217 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2218 }
2219}
2220
2221bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002222{
2223 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002224 for (const auto &effect : mEffects) {
2225 if (effect->isProcessImplemented()) {
2226 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002227 }
2228 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002229 // Allow effects without processing.
2230 return true;
2231}
2232
2233bool AudioFlinger::EffectChain::isFastCompatible() const
2234{
2235 Mutex::Autolock _l(mLock);
2236 for (const auto &effect : mEffects) {
2237 if (effect->isProcessImplemented()
2238 && effect->isImplementationSoftware()) {
2239 return false;
2240 }
2241 }
2242 // Allow effects without processing or hw accelerated effects.
2243 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002244}
2245
2246// isCompatibleWithThread_l() must be called with thread->mLock held
2247bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2248{
2249 Mutex::Autolock _l(mLock);
2250 for (size_t i = 0; i < mEffects.size(); i++) {
2251 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2252 return false;
2253 }
2254 }
2255 return true;
2256}
2257
Glenn Kasten63238ef2015-03-02 15:50:29 -08002258} // namespace android