blob: bd5f146a821a1711e2e1f6b6cb54963c69f5925c [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 Laurentd8365c52017-07-16 15:27:05 -070024#include <system/audio_effects/effect_aec.h>
25#include <system/audio_effects/effect_ns.h>
26#include <system/audio_effects/effect_visualizer.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080027#include <audio_utils/primitives.h>
Mikhail Naganov5b506f22017-07-19 17:54:29 -070028#include <media/AudioEffect.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070029#include <media/audiohal/EffectHalInterface.h>
30#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080031
32#include "AudioFlinger.h"
33#include "ServiceUtilities.h"
34
35// ----------------------------------------------------------------------------
36
37// Note: the following macro is used for extremely verbose logging message. In
38// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
39// 0; but one side effect of this is to turn all LOGV's as well. Some messages
40// are so verbose that we want to suppress them even when we have ALOG_ASSERT
41// turned on. Do not uncomment the #def below unless you really know what you
42// are doing and want to see all of the extremely verbose messages.
43//#define VERY_VERY_VERBOSE_LOGGING
44#ifdef VERY_VERY_VERBOSE_LOGGING
45#define ALOGVV ALOGV
46#else
47#define ALOGVV(a...) do { } while(0)
48#endif
49
Ricardo Garcia726b6a72014-08-11 12:04:54 -070050#define min(a, b) ((a) < (b) ? (a) : (b))
51
Eric Laurentca7cc822012-11-19 14:55:58 -080052namespace android {
53
54// ----------------------------------------------------------------------------
55// EffectModule implementation
56// ----------------------------------------------------------------------------
57
58#undef LOG_TAG
59#define LOG_TAG "AudioFlinger::EffectModule"
60
61AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
62 const wp<AudioFlinger::EffectChain>& chain,
63 effect_descriptor_t *desc,
64 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080065 audio_session_t sessionId,
66 bool pinned)
67 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080068 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69 mDescriptor(*desc),
70 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080071 mStatus(NO_INIT), mState(IDLE),
72 // mMaxDisableWaitCnt is set by configure() and not used before then
73 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070074 mSuspended(false),
75 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080076{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080077 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080078 int lStatus;
79
80 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070081 mStatus = -ENODEV;
82 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070083 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070084 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070085 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070086 mStatus = effectsFactory->createEffect(
87 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
88 }
89 }
Eric Laurentca7cc822012-11-19 14:55:58 -080090
91 if (mStatus != NO_ERROR) {
92 return;
93 }
94 lStatus = init();
95 if (lStatus < 0) {
96 mStatus = lStatus;
97 goto Error;
98 }
99
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800100 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700101 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800102
Eric Laurentca7cc822012-11-19 14:55:58 -0800103 return;
104Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700105 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800106 ALOGV("Constructor Error %d", mStatus);
107}
108
109AudioFlinger::EffectModule::~EffectModule()
110{
111 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700112 if (mEffectInterface != 0) {
Mikhail Naganov5b506f22017-07-19 17:54:29 -0700113 char uuidStr[64];
114 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
115 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
116 this, uuidStr);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800117 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800118 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800119
Eric Laurentca7cc822012-11-19 14:55:58 -0800120}
121
122status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
123{
124 status_t status;
125
126 Mutex::Autolock _l(mLock);
127 int priority = handle->priority();
128 size_t size = mHandles.size();
129 EffectHandle *controlHandle = NULL;
130 size_t i;
131 for (i = 0; i < size; i++) {
132 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800133 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800134 continue;
135 }
136 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700137 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800138 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700139 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800140 if (h->priority() <= priority) {
141 break;
142 }
143 }
144 // if inserted in first place, move effect control from previous owner to this handle
145 if (i == 0) {
146 bool enabled = false;
147 if (controlHandle != NULL) {
148 enabled = controlHandle->enabled();
149 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
150 }
151 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
152 status = NO_ERROR;
153 } else {
154 status = ALREADY_EXISTS;
155 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700156 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800157 mHandles.insertAt(handle, i);
158 return status;
159}
160
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800161ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800162{
163 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800164 return removeHandle_l(handle);
165}
166
167ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
168{
Eric Laurentca7cc822012-11-19 14:55:58 -0800169 size_t size = mHandles.size();
170 size_t i;
171 for (i = 0; i < size; i++) {
172 if (mHandles[i] == handle) {
173 break;
174 }
175 }
176 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800177 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
178 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800179 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800180 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800181
182 mHandles.removeAt(i);
183 // if removed from first place, move effect control from this handle to next in line
184 if (i == 0) {
185 EffectHandle *h = controlHandle_l();
186 if (h != NULL) {
187 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
188 }
189 }
190
191 // Prevent calls to process() and other functions on effect interface from now on.
192 // The effect engine will be released by the destructor when the last strong reference on
193 // this object is released which can happen after next process is called.
194 if (mHandles.size() == 0 && !mPinned) {
195 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800196 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800197 }
198
199 return mHandles.size();
200}
201
202// must be called with EffectModule::mLock held
203AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
204{
205 // the first valid handle in the list has control over the module
206 for (size_t i = 0; i < mHandles.size(); i++) {
207 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800208 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800209 return h;
210 }
211 }
212
213 return NULL;
214}
215
Eric Laurentf10c7092016-12-06 17:09:56 -0800216// unsafe method called when the effect parent thread has been destroyed
217ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
218{
219 ALOGV("disconnect() %p handle %p", this, handle);
220 Mutex::Autolock _l(mLock);
221 ssize_t numHandles = removeHandle_l(handle);
222 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
223 AudioSystem::unregisterEffect(mId);
224 sp<AudioFlinger> af = mAudioFlinger.promote();
225 if (af != 0) {
226 mLock.unlock();
227 af->updateOrphanEffectChains(this);
228 mLock.lock();
229 }
230 }
231 return numHandles;
232}
233
Eric Laurentfa1e1232016-08-02 19:01:49 -0700234bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800235 Mutex::Autolock _l(mLock);
236
Eric Laurentfa1e1232016-08-02 19:01:49 -0700237 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800238 switch (mState) {
239 case RESTART:
240 reset_l();
241 // FALL THROUGH
242
243 case STARTING:
244 // clear auxiliary effect input buffer for next accumulation
245 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
246 memset(mConfig.inputCfg.buffer.raw,
247 0,
248 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
249 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700250 if (start_l() == NO_ERROR) {
251 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700252 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700253 } else {
254 mState = IDLE;
255 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800256 break;
257 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700258 if (stop_l() == NO_ERROR) {
259 mDisableWaitCnt = mMaxDisableWaitCnt;
260 } else {
261 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
262 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800263 mState = STOPPED;
264 break;
265 case STOPPED:
266 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
267 // turn off sequence.
268 if (--mDisableWaitCnt == 0) {
269 reset_l();
270 mState = IDLE;
271 }
272 break;
273 default: //IDLE , ACTIVE, DESTROYED
274 break;
275 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700276
277 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800278}
279
280void AudioFlinger::EffectModule::process()
281{
282 Mutex::Autolock _l(mLock);
283
Mikhail Naganov022b9952017-01-04 16:36:51 -0800284 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800285 return;
286 }
287
288 if (isProcessEnabled()) {
289 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
290 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
291 ditherAndClamp(mConfig.inputCfg.buffer.s32,
292 mConfig.inputCfg.buffer.s32,
293 mConfig.inputCfg.buffer.frameCount/2);
294 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700295 int ret;
296 if (isProcessImplemented()) {
297 // do the actual processing in the effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800298 ret = mEffectInterface->process();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700299 } else {
300 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
301 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
302 int16_t *in = mConfig.inputCfg.buffer.s16;
303 int16_t *out = mConfig.outputCfg.buffer.s16;
Eric Laurentca7cc822012-11-19 14:55:58 -0800304
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700305 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
306 for (size_t i = 0; i < frameCnt; i++) {
307 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
308 }
309 } else {
310 memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
311 frameCnt * sizeof(int16_t));
312 }
313 }
314 ret = -ENODATA;
315 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800316 // force transition to IDLE state when engine is ready
317 if (mState == STOPPED && ret == -ENODATA) {
318 mDisableWaitCnt = 1;
319 }
320
321 // clear auxiliary effect input buffer for next accumulation
322 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
323 memset(mConfig.inputCfg.buffer.raw, 0,
324 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
325 }
326 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
327 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
328 // If an insert effect is idle and input buffer is different from output buffer,
329 // accumulate input onto output
330 sp<EffectChain> chain = mChain.promote();
331 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700332 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
Eric Laurentca7cc822012-11-19 14:55:58 -0800333 int16_t *in = mConfig.inputCfg.buffer.s16;
334 int16_t *out = mConfig.outputCfg.buffer.s16;
335 for (size_t i = 0; i < frameCnt; i++) {
336 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
337 }
338 }
339 }
340}
341
342void AudioFlinger::EffectModule::reset_l()
343{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700344 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800345 return;
346 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700347 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800348}
349
350status_t AudioFlinger::EffectModule::configure()
351{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700352 status_t status;
353 sp<ThreadBase> thread;
354 uint32_t size;
355 audio_channel_mask_t channelMask;
356
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700357 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700358 status = NO_INIT;
359 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800360 }
361
Eric Laurentd0ebb532013-04-02 16:41:41 -0700362 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800363 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700364 status = DEAD_OBJECT;
365 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800366 }
367
368 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700369 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700370 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800371
372 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
373 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900374 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
375 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800376 } else {
377 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700378 // TODO: Update this logic when multichannel effects are implemented.
379 // For offloaded tracks consider mono output as stereo for proper effect initialization
380 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
381 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
382 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
383 ALOGV("Overriding effect input and output as STEREO");
384 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800385 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700386
Eric Laurentca7cc822012-11-19 14:55:58 -0800387 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
388 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
389 mConfig.inputCfg.samplingRate = thread->sampleRate();
390 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
391 mConfig.inputCfg.bufferProvider.cookie = NULL;
392 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
393 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
394 mConfig.outputCfg.bufferProvider.cookie = NULL;
395 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
396 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
397 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
398 // Insert effect:
399 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
400 // always overwrites output buffer: input buffer == output buffer
401 // - in other sessions:
402 // last effect in the chain accumulates in output buffer: input buffer != output buffer
403 // other effect: overwrites output buffer: input buffer == output buffer
404 // Auxiliary effect:
405 // accumulates in output buffer: input buffer != output buffer
406 // Therefore: accumulate <=> input buffer != output buffer
407 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
408 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
409 } else {
410 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
411 }
412 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
413 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
414 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
415 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800416 if (mInBuffer != 0) {
417 mInBuffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
418 }
419 if (mOutBuffer != 0) {
420 mOutBuffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
421 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800422
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700423 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800424 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
425
426 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700427 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700428 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
429 sizeof(effect_config_t),
430 &mConfig,
431 &size,
432 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800433 if (status == 0) {
434 status = cmdStatus;
435 }
436
437 if (status == 0 &&
438 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
439 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
440 effect_param_t *p = (effect_param_t *)buf32;
441
442 p->psize = sizeof(uint32_t);
443 p->vsize = sizeof(uint32_t);
444 size = sizeof(int);
445 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
446
447 uint32_t latency = 0;
448 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
449 if (pbt != NULL) {
450 latency = pbt->latency_l();
451 }
452
453 *((int32_t *)p->data + 1)= latency;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700454 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
455 sizeof(effect_param_t) + 8,
456 &buf32,
457 &size,
458 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800459 }
460
461 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
462 (1000 * mConfig.outputCfg.buffer.frameCount);
463
Eric Laurentd0ebb532013-04-02 16:41:41 -0700464exit:
465 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800466 return status;
467}
468
469status_t AudioFlinger::EffectModule::init()
470{
471 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700472 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800473 return NO_INIT;
474 }
475 status_t cmdStatus;
476 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700477 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
478 0,
479 NULL,
480 &size,
481 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800482 if (status == 0) {
483 status = cmdStatus;
484 }
485 return status;
486}
487
Eric Laurent1b928682014-10-02 19:41:47 -0700488void AudioFlinger::EffectModule::addEffectToHal_l()
489{
490 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
491 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
492 sp<ThreadBase> thread = mThread.promote();
493 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700494 sp<StreamHalInterface> stream = thread->stream();
495 if (stream != 0) {
496 status_t result = stream->addEffect(mEffectInterface);
497 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700498 }
499 }
500 }
501}
502
Eric Laurentfa1e1232016-08-02 19:01:49 -0700503// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800504status_t AudioFlinger::EffectModule::start()
505{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700506 sp<EffectChain> chain;
507 status_t status;
508 {
509 Mutex::Autolock _l(mLock);
510 status = start_l();
511 if (status == NO_ERROR) {
512 chain = mChain.promote();
513 }
514 }
515 if (chain != 0) {
516 chain->resetVolume_l();
517 }
518 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800519}
520
521status_t AudioFlinger::EffectModule::start_l()
522{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700523 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800524 return NO_INIT;
525 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700526 if (mStatus != NO_ERROR) {
527 return mStatus;
528 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800529 status_t cmdStatus;
530 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700531 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
532 0,
533 NULL,
534 &size,
535 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800536 if (status == 0) {
537 status = cmdStatus;
538 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700539 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700540 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800541 }
542 return status;
543}
544
545status_t AudioFlinger::EffectModule::stop()
546{
547 Mutex::Autolock _l(mLock);
548 return stop_l();
549}
550
551status_t AudioFlinger::EffectModule::stop_l()
552{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700553 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800554 return NO_INIT;
555 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700556 if (mStatus != NO_ERROR) {
557 return mStatus;
558 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800559 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800560 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700561 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
562 0,
563 NULL,
564 &size,
565 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800566 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800567 status = cmdStatus;
568 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800569 if (status == NO_ERROR) {
570 status = remove_effect_from_hal_l();
571 }
572 return status;
573}
574
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800575// must be called with EffectChain::mLock held
576void AudioFlinger::EffectModule::release_l()
577{
578 if (mEffectInterface != 0) {
579 remove_effect_from_hal_l();
580 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800581 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800582 mEffectInterface.clear();
583 }
584}
585
Eric Laurentbfb1b832013-01-07 09:53:42 -0800586status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
587{
588 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
589 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800590 sp<ThreadBase> thread = mThread.promote();
591 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700592 sp<StreamHalInterface> stream = thread->stream();
593 if (stream != 0) {
594 status_t result = stream->removeEffect(mEffectInterface);
595 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800596 }
597 }
598 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800599 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800600}
601
Andy Hunge4a1d912016-08-17 14:11:13 -0700602// round up delta valid if value and divisor are positive.
603template <typename T>
604static T roundUpDelta(const T &value, const T &divisor) {
605 T remainder = value % divisor;
606 return remainder == 0 ? 0 : divisor - remainder;
607}
608
Eric Laurentca7cc822012-11-19 14:55:58 -0800609status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
610 uint32_t cmdSize,
611 void *pCmdData,
612 uint32_t *replySize,
613 void *pReplyData)
614{
615 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700616 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800617
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700618 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800619 return NO_INIT;
620 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700621 if (mStatus != NO_ERROR) {
622 return mStatus;
623 }
Andy Hung110bc952016-06-20 15:22:52 -0700624 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700625 (sizeof(effect_param_t) > cmdSize ||
626 ((effect_param_t *)pCmdData)->psize > cmdSize
627 - sizeof(effect_param_t))) {
628 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800629 android_errorWriteLog(0x534e4554, "33003822");
630 return -EINVAL;
631 }
632 if (cmdCode == EFFECT_CMD_GET_PARAM &&
633 (*replySize < sizeof(effect_param_t) ||
634 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
635 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700636 return -EINVAL;
637 }
ragoe2759072016-11-22 18:02:48 -0800638 if (cmdCode == EFFECT_CMD_GET_PARAM &&
639 (sizeof(effect_param_t) > *replySize
640 || ((effect_param_t *)pCmdData)->psize > *replySize
641 - sizeof(effect_param_t)
642 || ((effect_param_t *)pCmdData)->vsize > *replySize
643 - sizeof(effect_param_t)
644 - ((effect_param_t *)pCmdData)->psize
645 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
646 *replySize
647 - sizeof(effect_param_t)
648 - ((effect_param_t *)pCmdData)->psize
649 - ((effect_param_t *)pCmdData)->vsize)) {
650 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
651 android_errorWriteLog(0x534e4554, "32705438");
652 return -EINVAL;
653 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700654 if ((cmdCode == EFFECT_CMD_SET_PARAM
655 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
656 (sizeof(effect_param_t) > cmdSize
657 || ((effect_param_t *)pCmdData)->psize > cmdSize
658 - sizeof(effect_param_t)
659 || ((effect_param_t *)pCmdData)->vsize > cmdSize
660 - sizeof(effect_param_t)
661 - ((effect_param_t *)pCmdData)->psize
662 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
663 cmdSize
664 - sizeof(effect_param_t)
665 - ((effect_param_t *)pCmdData)->psize
666 - ((effect_param_t *)pCmdData)->vsize)) {
667 android_errorWriteLog(0x534e4554, "30204301");
668 return -EINVAL;
669 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700670 status_t status = mEffectInterface->command(cmdCode,
671 cmdSize,
672 pCmdData,
673 replySize,
674 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800675 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
676 uint32_t size = (replySize == NULL) ? 0 : *replySize;
677 for (size_t i = 1; i < mHandles.size(); i++) {
678 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800679 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800680 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
681 }
682 }
683 }
684 return status;
685}
686
687status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
688{
689 Mutex::Autolock _l(mLock);
690 return setEnabled_l(enabled);
691}
692
693// must be called with EffectModule::mLock held
694status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
695{
696
697 ALOGV("setEnabled %p enabled %d", this, enabled);
698
699 if (enabled != isEnabled()) {
700 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
701 if (enabled && status != NO_ERROR) {
702 return status;
703 }
704
705 switch (mState) {
706 // going from disabled to enabled
707 case IDLE:
708 mState = STARTING;
709 break;
710 case STOPPED:
711 mState = RESTART;
712 break;
713 case STOPPING:
714 mState = ACTIVE;
715 break;
716
717 // going from enabled to disabled
718 case RESTART:
719 mState = STOPPED;
720 break;
721 case STARTING:
722 mState = IDLE;
723 break;
724 case ACTIVE:
725 mState = STOPPING;
726 break;
727 case DESTROYED:
728 return NO_ERROR; // simply ignore as we are being destroyed
729 }
730 for (size_t i = 1; i < mHandles.size(); i++) {
731 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800732 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800733 h->setEnabled(enabled);
734 }
735 }
736 }
737 return NO_ERROR;
738}
739
740bool AudioFlinger::EffectModule::isEnabled() const
741{
742 switch (mState) {
743 case RESTART:
744 case STARTING:
745 case ACTIVE:
746 return true;
747 case IDLE:
748 case STOPPING:
749 case STOPPED:
750 case DESTROYED:
751 default:
752 return false;
753 }
754}
755
756bool AudioFlinger::EffectModule::isProcessEnabled() const
757{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700758 if (mStatus != NO_ERROR) {
759 return false;
760 }
761
Eric Laurentca7cc822012-11-19 14:55:58 -0800762 switch (mState) {
763 case RESTART:
764 case ACTIVE:
765 case STOPPING:
766 case STOPPED:
767 return true;
768 case IDLE:
769 case STARTING:
770 case DESTROYED:
771 default:
772 return false;
773 }
774}
775
Mikhail Naganov022b9952017-01-04 16:36:51 -0800776void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
777 if (buffer != 0) {
778 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
779 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
780 } else {
781 mConfig.inputCfg.buffer.raw = NULL;
782 }
783 mInBuffer = buffer;
784 mEffectInterface->setInBuffer(buffer);
785}
786
787void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
788 if (buffer != 0) {
789 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
790 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
791 } else {
792 mConfig.outputCfg.buffer.raw = NULL;
793 }
794 mOutBuffer = buffer;
795 mEffectInterface->setOutBuffer(buffer);
796}
797
Eric Laurentca7cc822012-11-19 14:55:58 -0800798status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
799{
800 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700801 if (mStatus != NO_ERROR) {
802 return mStatus;
803 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800804 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800805 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
806 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
807 if (isProcessEnabled() &&
808 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
809 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800810 uint32_t volume[2];
811 uint32_t *pVolume = NULL;
812 uint32_t size = sizeof(volume);
813 volume[0] = *left;
814 volume[1] = *right;
815 if (controller) {
816 pVolume = volume;
817 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700818 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
819 size,
820 volume,
821 &size,
822 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -0800823 if (controller && status == NO_ERROR && size == sizeof(volume)) {
824 *left = volume[0];
825 *right = volume[1];
826 }
827 }
828 return status;
829}
830
831status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
832{
833 if (device == AUDIO_DEVICE_NONE) {
834 return NO_ERROR;
835 }
836
837 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700838 if (mStatus != NO_ERROR) {
839 return mStatus;
840 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800841 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700842 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800843 status_t cmdStatus;
844 uint32_t size = sizeof(status_t);
845 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
846 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700847 status = mEffectInterface->command(cmd,
848 sizeof(uint32_t),
849 &device,
850 &size,
851 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800852 }
853 return status;
854}
855
856status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
857{
858 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700859 if (mStatus != NO_ERROR) {
860 return mStatus;
861 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800862 status_t status = NO_ERROR;
863 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
864 status_t cmdStatus;
865 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700866 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
867 sizeof(audio_mode_t),
868 &mode,
869 &size,
870 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800871 if (status == NO_ERROR) {
872 status = cmdStatus;
873 }
874 }
875 return status;
876}
877
878status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
879{
880 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700881 if (mStatus != NO_ERROR) {
882 return mStatus;
883 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800884 status_t status = NO_ERROR;
885 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
886 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700887 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
888 sizeof(audio_source_t),
889 &source,
890 &size,
891 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800892 }
893 return status;
894}
895
896void AudioFlinger::EffectModule::setSuspended(bool suspended)
897{
898 Mutex::Autolock _l(mLock);
899 mSuspended = suspended;
900}
901
902bool AudioFlinger::EffectModule::suspended() const
903{
904 Mutex::Autolock _l(mLock);
905 return mSuspended;
906}
907
908bool AudioFlinger::EffectModule::purgeHandles()
909{
910 bool enabled = false;
911 Mutex::Autolock _l(mLock);
912 for (size_t i = 0; i < mHandles.size(); i++) {
913 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800914 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800915 if (handle->hasControl()) {
916 enabled = handle->enabled();
917 }
918 }
919 }
920 return enabled;
921}
922
Eric Laurent5baf2af2013-09-12 17:37:00 -0700923status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
924{
925 Mutex::Autolock _l(mLock);
926 if (mStatus != NO_ERROR) {
927 return mStatus;
928 }
929 status_t status = NO_ERROR;
930 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
931 status_t cmdStatus;
932 uint32_t size = sizeof(status_t);
933 effect_offload_param_t cmd;
934
935 cmd.isOffload = offloaded;
936 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700937 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
938 sizeof(effect_offload_param_t),
939 &cmd,
940 &size,
941 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700942 if (status == NO_ERROR) {
943 status = cmdStatus;
944 }
945 mOffloaded = (status == NO_ERROR) ? offloaded : false;
946 } else {
947 if (offloaded) {
948 status = INVALID_OPERATION;
949 }
950 mOffloaded = false;
951 }
952 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
953 return status;
954}
955
956bool AudioFlinger::EffectModule::isOffloaded() const
957{
958 Mutex::Autolock _l(mLock);
959 return mOffloaded;
960}
961
Marco Nelissenb2208842014-02-07 14:00:50 -0800962String8 effectFlagsToString(uint32_t flags) {
963 String8 s;
964
965 s.append("conn. mode: ");
966 switch (flags & EFFECT_FLAG_TYPE_MASK) {
967 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
968 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
969 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
970 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
971 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
972 default: s.append("unknown/reserved"); break;
973 }
974 s.append(", ");
975
976 s.append("insert pref: ");
977 switch (flags & EFFECT_FLAG_INSERT_MASK) {
978 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
979 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
980 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
981 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
982 default: s.append("unknown/reserved"); break;
983 }
984 s.append(", ");
985
986 s.append("volume mgmt: ");
987 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
988 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
989 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
990 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
991 default: s.append("unknown/reserved"); break;
992 }
993 s.append(", ");
994
995 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
996 if (devind) {
997 s.append("device indication: ");
998 switch (devind) {
999 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
1000 default: s.append("unknown/reserved"); break;
1001 }
1002 s.append(", ");
1003 }
1004
1005 s.append("input mode: ");
1006 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1007 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1008 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1009 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1010 default: s.append("not set"); break;
1011 }
1012 s.append(", ");
1013
1014 s.append("output mode: ");
1015 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1016 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1017 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1018 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1019 default: s.append("not set"); break;
1020 }
1021 s.append(", ");
1022
1023 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1024 if (accel) {
1025 s.append("hardware acceleration: ");
1026 switch (accel) {
1027 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1028 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1029 default: s.append("unknown/reserved"); break;
1030 }
1031 s.append(", ");
1032 }
1033
1034 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1035 if (modeind) {
1036 s.append("mode indication: ");
1037 switch (modeind) {
1038 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1039 default: s.append("unknown/reserved"); break;
1040 }
1041 s.append(", ");
1042 }
1043
1044 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1045 if (srcind) {
1046 s.append("source indication: ");
1047 switch (srcind) {
1048 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1049 default: s.append("unknown/reserved"); break;
1050 }
1051 s.append(", ");
1052 }
1053
1054 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1055 s.append("offloadable, ");
1056 }
1057
1058 int len = s.length();
1059 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001060 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001061 s.unlockBuffer(len - 2);
1062 }
1063 return s;
1064}
1065
1066
Glenn Kasten0f11b512014-01-31 16:18:54 -08001067void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001068{
1069 const size_t SIZE = 256;
1070 char buffer[SIZE];
1071 String8 result;
1072
1073 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1074 result.append(buffer);
1075
1076 bool locked = AudioFlinger::dumpTryLock(mLock);
1077 // failed to lock - AudioFlinger is probably deadlocked
1078 if (!locked) {
1079 result.append("\t\tCould not lock Fx mutex:\n");
1080 }
1081
1082 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001083 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001084 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001085 result.append(buffer);
1086
1087 result.append("\t\tDescriptor:\n");
Mikhail Naganov5b506f22017-07-19 17:54:29 -07001088 char uuidStr[64];
1089 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
1090 snprintf(buffer, SIZE, "\t\t- UUID: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001091 result.append(buffer);
Mikhail Naganov5b506f22017-07-19 17:54:29 -07001092 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
1093 snprintf(buffer, SIZE, "\t\t- TYPE: %s\n", uuidStr);
Eric Laurentca7cc822012-11-19 14:55:58 -08001094 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 {
Eric Laurentf10c7092016-12-06 17:09:56 -08001307 // try to cleanup as much as we can
1308 sp<EffectModule> effect = mEffect.promote();
Mikhail Naganov5b506f22017-07-19 17:54:29 -07001309 if (effect != 0 && effect->disconnectHandle(this, unpinIfLast) > 0) {
1310 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
Eric Laurentf10c7092016-12-06 17:09:56 -08001311 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001312 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001313
Eric Laurentca7cc822012-11-19 14:55:58 -08001314 if (mClient != 0) {
1315 if (mCblk != NULL) {
1316 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1317 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1318 }
1319 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001320 // Client destructor must run with AudioFlinger client mutex locked
1321 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001322 mClient.clear();
1323 }
1324}
1325
1326status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1327 uint32_t cmdSize,
1328 void *pCmdData,
1329 uint32_t *replySize,
1330 void *pReplyData)
1331{
1332 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001333 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001334
Eric Laurentc7ab3092017-06-15 18:43:46 -07001335 // reject commands reserved for internal use by audio framework if coming from outside
1336 // of audioserver
1337 switch(cmdCode) {
1338 case EFFECT_CMD_ENABLE:
1339 case EFFECT_CMD_DISABLE:
1340 case EFFECT_CMD_SET_PARAM:
1341 case EFFECT_CMD_SET_PARAM_DEFERRED:
1342 case EFFECT_CMD_SET_PARAM_COMMIT:
1343 case EFFECT_CMD_GET_PARAM:
1344 break;
1345 default:
1346 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1347 break;
1348 }
1349 android_errorWriteLog(0x534e4554, "62019992");
1350 return BAD_VALUE;
1351 }
1352
Eric Laurent1ffc5852016-12-15 14:46:09 -08001353 if (cmdCode == EFFECT_CMD_ENABLE) {
1354 if (*replySize < sizeof(int)) {
1355 android_errorWriteLog(0x534e4554, "32095713");
1356 return BAD_VALUE;
1357 }
1358 *(int *)pReplyData = NO_ERROR;
1359 *replySize = sizeof(int);
1360 return enable();
1361 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1362 if (*replySize < sizeof(int)) {
1363 android_errorWriteLog(0x534e4554, "32095713");
1364 return BAD_VALUE;
1365 }
1366 *(int *)pReplyData = NO_ERROR;
1367 *replySize = sizeof(int);
1368 return disable();
1369 }
1370
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001371 AutoMutex _l(mLock);
1372 sp<EffectModule> effect = mEffect.promote();
1373 if (effect == 0 || mDisconnected) {
1374 return DEAD_OBJECT;
1375 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001376 // only get parameter command is permitted for applications not controlling the effect
1377 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1378 return INVALID_OPERATION;
1379 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001380 if (mClient == 0) {
1381 return INVALID_OPERATION;
1382 }
1383
1384 // handle commands that are not forwarded transparently to effect engine
1385 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001386 if (*replySize < sizeof(int)) {
1387 android_errorWriteLog(0x534e4554, "32095713");
1388 return BAD_VALUE;
1389 }
1390 *(int *)pReplyData = NO_ERROR;
1391 *replySize = sizeof(int);
1392
Eric Laurentca7cc822012-11-19 14:55:58 -08001393 // No need to trylock() here as this function is executed in the binder thread serving a
1394 // particular client process: no risk to block the whole media server process or mixer
1395 // threads if we are stuck here
1396 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001397 // keep local copy of index in case of client corruption b/32220769
1398 const uint32_t clientIndex = mCblk->clientIndex;
1399 const uint32_t serverIndex = mCblk->serverIndex;
1400 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1401 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 mCblk->serverIndex = 0;
1403 mCblk->clientIndex = 0;
1404 return BAD_VALUE;
1405 }
1406 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001407 effect_param_t *param = NULL;
1408 for (uint32_t index = serverIndex; index < clientIndex;) {
1409 int *p = (int *)(mBuffer + index);
1410 const int size = *p++;
1411 if (size < 0
1412 || size > EFFECT_PARAM_BUFFER_SIZE
1413 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001415 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001416 break;
1417 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001418
1419 // copy to local memory in case of client corruption b/32220769
1420 param = (effect_param_t *)realloc(param, size);
1421 if (param == NULL) {
1422 ALOGW("command(): out of memory");
1423 status = NO_MEMORY;
1424 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001426 memcpy(param, p, size);
1427
1428 int reply = 0;
1429 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001430 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001431 size,
1432 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001433 &rsize,
1434 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001435
1436 // verify shared memory: server index shouldn't change; client index can't go back.
1437 if (serverIndex != mCblk->serverIndex
1438 || clientIndex > mCblk->clientIndex) {
1439 android_errorWriteLog(0x534e4554, "32220769");
1440 status = BAD_VALUE;
1441 break;
1442 }
1443
Eric Laurentca7cc822012-11-19 14:55:58 -08001444 // stop at first error encountered
1445 if (ret != NO_ERROR) {
1446 status = ret;
1447 *(int *)pReplyData = reply;
1448 break;
1449 } else if (reply != NO_ERROR) {
1450 *(int *)pReplyData = reply;
1451 break;
1452 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001453 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001454 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001455 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001456 mCblk->serverIndex = 0;
1457 mCblk->clientIndex = 0;
1458 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001459 }
1460
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001461 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001462}
1463
1464void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1465{
1466 ALOGV("setControl %p control %d", this, hasControl);
1467
1468 mHasControl = hasControl;
1469 mEnabled = enabled;
1470
1471 if (signal && mEffectClient != 0) {
1472 mEffectClient->controlStatusChanged(hasControl);
1473 }
1474}
1475
1476void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1477 uint32_t cmdSize,
1478 void *pCmdData,
1479 uint32_t replySize,
1480 void *pReplyData)
1481{
1482 if (mEffectClient != 0) {
1483 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1484 }
1485}
1486
1487
1488
1489void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1490{
1491 if (mEffectClient != 0) {
1492 mEffectClient->enableStatusChanged(enabled);
1493 }
1494}
1495
1496status_t AudioFlinger::EffectHandle::onTransact(
1497 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1498{
1499 return BnEffect::onTransact(code, data, reply, flags);
1500}
1501
1502
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001503void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001504{
1505 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1506
Marco Nelissenb2208842014-02-07 14:00:50 -08001507 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001508 (mClient == 0) ? getpid_cached : mClient->pid(),
1509 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001510 mHasControl ? "yes" : "no",
1511 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001512 mCblk ? mCblk->clientIndex : 0,
1513 mCblk ? mCblk->serverIndex : 0
1514 );
1515
1516 if (locked) {
1517 mCblk->lock.unlock();
1518 }
1519}
1520
1521#undef LOG_TAG
1522#define LOG_TAG "AudioFlinger::EffectChain"
1523
1524AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001525 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001526 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001527 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001528 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001529{
1530 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1531 if (thread == NULL) {
1532 return;
1533 }
1534 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1535 thread->frameCount();
1536}
1537
1538AudioFlinger::EffectChain::~EffectChain()
1539{
Eric Laurentca7cc822012-11-19 14:55:58 -08001540}
1541
1542// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1543sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1544 effect_descriptor_t *descriptor)
1545{
1546 size_t size = mEffects.size();
1547
1548 for (size_t i = 0; i < size; i++) {
1549 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1550 return mEffects[i];
1551 }
1552 }
1553 return 0;
1554}
1555
1556// getEffectFromId_l() must be called with ThreadBase::mLock held
1557sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1558{
1559 size_t size = mEffects.size();
1560
1561 for (size_t i = 0; i < size; i++) {
1562 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1563 if (id == 0 || mEffects[i]->id() == id) {
1564 return mEffects[i];
1565 }
1566 }
1567 return 0;
1568}
1569
1570// getEffectFromType_l() must be called with ThreadBase::mLock held
1571sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1572 const effect_uuid_t *type)
1573{
1574 size_t size = mEffects.size();
1575
1576 for (size_t i = 0; i < size; i++) {
1577 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1578 return mEffects[i];
1579 }
1580 }
1581 return 0;
1582}
1583
1584void AudioFlinger::EffectChain::clearInputBuffer()
1585{
1586 Mutex::Autolock _l(mLock);
1587 sp<ThreadBase> thread = mThread.promote();
1588 if (thread == 0) {
1589 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1590 return;
1591 }
1592 clearInputBuffer_l(thread);
1593}
1594
1595// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001596void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001597{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001598 if (mInBuffer == NULL) {
1599 return;
1600 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001601 // TODO: This will change in the future, depending on multichannel
1602 // and sample format changes for effects.
1603 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1604 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001605 const size_t frameSize =
1606 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Mikhail Naganov022b9952017-01-04 16:36:51 -08001607 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1608 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001609}
1610
1611// Must be called with EffectChain::mLock locked
1612void AudioFlinger::EffectChain::process_l()
1613{
1614 sp<ThreadBase> thread = mThread.promote();
1615 if (thread == 0) {
1616 ALOGW("process_l(): cannot promote mixer thread");
1617 return;
1618 }
1619 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1620 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001621 // never process effects when:
1622 // - on an OFFLOAD thread
1623 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001624 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1625 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001626 if (!isGlobalSession) {
1627 bool tracksOnSession = (trackCnt() != 0);
1628
1629 if (!tracksOnSession && mTailBufferCount == 0) {
1630 doProcess = false;
1631 }
1632
1633 if (activeTrackCnt() == 0) {
1634 // if no track is active and the effect tail has not been rendered,
1635 // the input buffer must be cleared here as the mixer process will not do it
1636 if (tracksOnSession || mTailBufferCount > 0) {
1637 clearInputBuffer_l(thread);
1638 if (mTailBufferCount > 0) {
1639 mTailBufferCount--;
1640 }
1641 }
1642 }
1643 }
1644
1645 size_t size = mEffects.size();
1646 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001647 // Only the input and output buffers of the chain can be external,
1648 // and 'update' / 'commit' do nothing for allocated buffers, thus
1649 // it's not needed to consider any other buffers here.
1650 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001651 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1652 mOutBuffer->update();
1653 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001654 for (size_t i = 0; i < size; i++) {
1655 mEffects[i]->process();
1656 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001657 mInBuffer->commit();
1658 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1659 mOutBuffer->commit();
1660 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001661 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001662 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001664 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1665 }
1666 if (doResetVolume) {
1667 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001668 }
1669}
1670
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001671// createEffect_l() must be called with ThreadBase::mLock held
1672status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1673 ThreadBase *thread,
1674 effect_descriptor_t *desc,
1675 int id,
1676 audio_session_t sessionId,
1677 bool pinned)
1678{
1679 Mutex::Autolock _l(mLock);
1680 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1681 status_t lStatus = effect->status();
1682 if (lStatus == NO_ERROR) {
1683 lStatus = addEffect_ll(effect);
1684 }
1685 if (lStatus != NO_ERROR) {
1686 effect.clear();
1687 }
1688 return lStatus;
1689}
1690
1691// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001692status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1693{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001694 Mutex::Autolock _l(mLock);
1695 return addEffect_ll(effect);
1696}
1697// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1698status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1699{
Eric Laurentca7cc822012-11-19 14:55:58 -08001700 effect_descriptor_t desc = effect->desc();
1701 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1702
Eric Laurentca7cc822012-11-19 14:55:58 -08001703 effect->setChain(this);
1704 sp<ThreadBase> thread = mThread.promote();
1705 if (thread == 0) {
1706 return NO_INIT;
1707 }
1708 effect->setThread(thread);
1709
1710 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1711 // Auxiliary effects are inserted at the beginning of mEffects vector as
1712 // they are processed first and accumulated in chain input buffer
1713 mEffects.insertAt(effect, 0);
1714
1715 // the input buffer for auxiliary effect contains mono samples in
1716 // 32 bit format. This is to avoid saturation in AudoMixer
1717 // accumulation stage. Saturation is done in EffectModule::process() before
1718 // calling the process in effect engine
1719 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001720 sp<EffectBufferHalInterface> halBuffer;
1721 status_t result = EffectBufferHalInterface::allocate(
1722 numSamples * sizeof(int32_t), &halBuffer);
1723 if (result != OK) return result;
1724 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001725 // auxiliary effects output samples to chain input buffer for further processing
1726 // by insert effects
1727 effect->setOutBuffer(mInBuffer);
1728 } else {
1729 // Insert effects are inserted at the end of mEffects vector as they are processed
1730 // after track and auxiliary effects.
1731 // Insert effect order as a function of indicated preference:
1732 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1733 // another effect is present
1734 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1735 // last effect claiming first position
1736 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1737 // first effect claiming last position
1738 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1739 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1740 // already present
1741
1742 size_t size = mEffects.size();
1743 size_t idx_insert = size;
1744 ssize_t idx_insert_first = -1;
1745 ssize_t idx_insert_last = -1;
1746
1747 for (size_t i = 0; i < size; i++) {
1748 effect_descriptor_t d = mEffects[i]->desc();
1749 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1750 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1751 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1752 // check invalid effect chaining combinations
1753 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1754 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1755 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1756 desc.name, d.name);
1757 return INVALID_OPERATION;
1758 }
1759 // remember position of first insert effect and by default
1760 // select this as insert position for new effect
1761 if (idx_insert == size) {
1762 idx_insert = i;
1763 }
1764 // remember position of last insert effect claiming
1765 // first position
1766 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1767 idx_insert_first = i;
1768 }
1769 // remember position of first insert effect claiming
1770 // last position
1771 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1772 idx_insert_last == -1) {
1773 idx_insert_last = i;
1774 }
1775 }
1776 }
1777
1778 // modify idx_insert from first position if needed
1779 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1780 if (idx_insert_last != -1) {
1781 idx_insert = idx_insert_last;
1782 } else {
1783 idx_insert = size;
1784 }
1785 } else {
1786 if (idx_insert_first != -1) {
1787 idx_insert = idx_insert_first + 1;
1788 }
1789 }
1790
1791 // always read samples from chain input buffer
1792 effect->setInBuffer(mInBuffer);
1793
1794 // if last effect in the chain, output samples to chain
1795 // output buffer, otherwise to chain input buffer
1796 if (idx_insert == size) {
1797 if (idx_insert != 0) {
1798 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1799 mEffects[idx_insert-1]->configure();
1800 }
1801 effect->setOutBuffer(mOutBuffer);
1802 } else {
1803 effect->setOutBuffer(mInBuffer);
1804 }
1805 mEffects.insertAt(effect, idx_insert);
1806
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001807 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001808 idx_insert);
1809 }
1810 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07001811
Eric Laurentca7cc822012-11-19 14:55:58 -08001812 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 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002033
Eric Laurentca7cc822012-11-19 14:55:58 -08002034 if (desc->mRefCount++ == 0) {
2035 sp<EffectModule> effect = getEffectIfEnabled(type);
2036 if (effect != 0) {
2037 desc->mEffect = effect;
2038 effect->setSuspended(true);
2039 effect->setEnabled(false);
2040 }
2041 }
2042 } else {
2043 if (index < 0) {
2044 return;
2045 }
2046 desc = mSuspendedEffects.valueAt(index);
2047 if (desc->mRefCount <= 0) {
2048 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002049 desc->mRefCount = 0;
2050 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002051 }
2052 if (--desc->mRefCount == 0) {
2053 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2054 if (desc->mEffect != 0) {
2055 sp<EffectModule> effect = desc->mEffect.promote();
2056 if (effect != 0) {
2057 effect->setSuspended(false);
2058 effect->lock();
2059 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002060 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002061 effect->setEnabled_l(handle->enabled());
2062 }
2063 effect->unlock();
2064 }
2065 desc->mEffect.clear();
2066 }
2067 mSuspendedEffects.removeItemsAt(index);
2068 }
2069 }
2070}
2071
2072// must be called with ThreadBase::mLock held
2073void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2074{
2075 sp<SuspendedEffectDesc> desc;
2076
2077 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2078 if (suspend) {
2079 if (index >= 0) {
2080 desc = mSuspendedEffects.valueAt(index);
2081 } else {
2082 desc = new SuspendedEffectDesc();
2083 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2084 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2085 }
2086 if (desc->mRefCount++ == 0) {
2087 Vector< sp<EffectModule> > effects;
2088 getSuspendEligibleEffects(effects);
2089 for (size_t i = 0; i < effects.size(); i++) {
2090 setEffectSuspended_l(&effects[i]->desc().type, true);
2091 }
2092 }
2093 } else {
2094 if (index < 0) {
2095 return;
2096 }
2097 desc = mSuspendedEffects.valueAt(index);
2098 if (desc->mRefCount <= 0) {
2099 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2100 desc->mRefCount = 1;
2101 }
2102 if (--desc->mRefCount == 0) {
2103 Vector<const effect_uuid_t *> types;
2104 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2105 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2106 continue;
2107 }
2108 types.add(&mSuspendedEffects.valueAt(i)->mType);
2109 }
2110 for (size_t i = 0; i < types.size(); i++) {
2111 setEffectSuspended_l(types[i], false);
2112 }
2113 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2114 mSuspendedEffects.keyAt(index));
2115 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2116 }
2117 }
2118}
2119
2120
2121// The volume effect is used for automated tests only
2122#ifndef OPENSL_ES_H_
2123static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2124 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2125const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2126#endif //OPENSL_ES_H_
2127
Eric Laurentd8365c52017-07-16 15:27:05 -07002128/* static */
2129bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2130{
2131 // Only NS and AEC are suspended when BtNRec is off
2132 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2133 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2134 return true;
2135 }
2136 return false;
2137}
2138
Eric Laurentca7cc822012-11-19 14:55:58 -08002139bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2140{
2141 // auxiliary effects and visualizer are never suspended on output mix
2142 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2143 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2144 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2145 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2146 return false;
2147 }
2148 return true;
2149}
2150
2151void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2152 Vector< sp<AudioFlinger::EffectModule> > &effects)
2153{
2154 effects.clear();
2155 for (size_t i = 0; i < mEffects.size(); i++) {
2156 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2157 effects.add(mEffects[i]);
2158 }
2159 }
2160}
2161
2162sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2163 const effect_uuid_t *type)
2164{
2165 sp<EffectModule> effect = getEffectFromType_l(type);
2166 return effect != 0 && effect->isEnabled() ? effect : 0;
2167}
2168
2169void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2170 bool enabled)
2171{
2172 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2173 if (enabled) {
2174 if (index < 0) {
2175 // if the effect is not suspend check if all effects are suspended
2176 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2177 if (index < 0) {
2178 return;
2179 }
2180 if (!isEffectEligibleForSuspend(effect->desc())) {
2181 return;
2182 }
2183 setEffectSuspended_l(&effect->desc().type, enabled);
2184 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2185 if (index < 0) {
2186 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2187 return;
2188 }
2189 }
2190 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2191 effect->desc().type.timeLow);
2192 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002193 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002194 if (desc->mEffect == 0) {
2195 desc->mEffect = effect;
2196 effect->setEnabled(false);
2197 effect->setSuspended(true);
2198 }
2199 } else {
2200 if (index < 0) {
2201 return;
2202 }
2203 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2204 effect->desc().type.timeLow);
2205 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2206 desc->mEffect.clear();
2207 effect->setSuspended(false);
2208 }
2209}
2210
Eric Laurent5baf2af2013-09-12 17:37:00 -07002211bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002212{
2213 Mutex::Autolock _l(mLock);
2214 size_t size = mEffects.size();
2215 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002216 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002217 return true;
2218 }
2219 }
2220 return false;
2221}
2222
Eric Laurentaaa44472014-09-12 17:41:50 -07002223void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2224{
2225 Mutex::Autolock _l(mLock);
2226 mThread = thread;
2227 for (size_t i = 0; i < mEffects.size(); i++) {
2228 mEffects[i]->setThread(thread);
2229 }
2230}
2231
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002232void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2233{
2234 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2235 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2236 }
2237 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2238 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2239 }
2240}
2241
2242void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2243{
2244 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2245 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2246 }
2247 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2248 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2249 }
2250}
2251
2252bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002253{
2254 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002255 for (const auto &effect : mEffects) {
2256 if (effect->isProcessImplemented()) {
2257 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002258 }
2259 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002260 // Allow effects without processing.
2261 return true;
2262}
2263
2264bool AudioFlinger::EffectChain::isFastCompatible() const
2265{
2266 Mutex::Autolock _l(mLock);
2267 for (const auto &effect : mEffects) {
2268 if (effect->isProcessImplemented()
2269 && effect->isImplementationSoftware()) {
2270 return false;
2271 }
2272 }
2273 // Allow effects without processing or hw accelerated effects.
2274 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002275}
2276
2277// isCompatibleWithThread_l() must be called with thread->mLock held
2278bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2279{
2280 Mutex::Autolock _l(mLock);
2281 for (size_t i = 0; i < mEffects.size(); i++) {
2282 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2283 return false;
2284 }
2285 }
2286 return true;
2287}
2288
Glenn Kasten63238ef2015-03-02 15:50:29 -08002289} // namespace android