blob: f2c1c4fbce1f1120e504ff268dccf8f6cf25d1f9 [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 Naganova0c91332016-09-19 10:01:12 -070028#include <media/audiohal/EffectHalInterface.h>
29#include <media/audiohal/EffectsFactoryHalInterface.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080030
31#include "AudioFlinger.h"
32#include "ServiceUtilities.h"
33
34// ----------------------------------------------------------------------------
35
36// Note: the following macro is used for extremely verbose logging message. In
37// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
38// 0; but one side effect of this is to turn all LOGV's as well. Some messages
39// are so verbose that we want to suppress them even when we have ALOG_ASSERT
40// turned on. Do not uncomment the #def below unless you really know what you
41// are doing and want to see all of the extremely verbose messages.
42//#define VERY_VERY_VERBOSE_LOGGING
43#ifdef VERY_VERY_VERBOSE_LOGGING
44#define ALOGVV ALOGV
45#else
46#define ALOGVV(a...) do { } while(0)
47#endif
48
Ricardo Garcia726b6a72014-08-11 12:04:54 -070049#define min(a, b) ((a) < (b) ? (a) : (b))
50
Eric Laurentca7cc822012-11-19 14:55:58 -080051namespace android {
52
53// ----------------------------------------------------------------------------
54// EffectModule implementation
55// ----------------------------------------------------------------------------
56
57#undef LOG_TAG
58#define LOG_TAG "AudioFlinger::EffectModule"
59
60AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
61 const wp<AudioFlinger::EffectChain>& chain,
62 effect_descriptor_t *desc,
63 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080064 audio_session_t sessionId,
65 bool pinned)
66 : mPinned(pinned),
Eric Laurentca7cc822012-11-19 14:55:58 -080067 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
68 mDescriptor(*desc),
69 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080070 mStatus(NO_INIT), mState(IDLE),
71 // mMaxDisableWaitCnt is set by configure() and not used before then
72 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070073 mSuspended(false),
74 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080075{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080076 ALOGV("Constructor %p pinned %d", this, pinned);
Eric Laurentca7cc822012-11-19 14:55:58 -080077 int lStatus;
78
79 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070080 mStatus = -ENODEV;
81 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070082 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070083 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070084 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070085 mStatus = effectsFactory->createEffect(
86 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
87 }
88 }
Eric Laurentca7cc822012-11-19 14:55:58 -080089
90 if (mStatus != NO_ERROR) {
91 return;
92 }
93 lStatus = init();
94 if (lStatus < 0) {
95 mStatus = lStatus;
96 goto Error;
97 }
98
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080099 setOffloaded(thread->type() == ThreadBase::OFFLOAD, thread->id());
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700100 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800101
Eric Laurentca7cc822012-11-19 14:55:58 -0800102 return;
103Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700104 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800105 ALOGV("Constructor Error %d", mStatus);
106}
107
108AudioFlinger::EffectModule::~EffectModule()
109{
110 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700111 if (mEffectInterface != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800112 ALOGW("EffectModule %p destructor called with unreleased interface", this);
113 release_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800114 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800115
Eric Laurentca7cc822012-11-19 14:55:58 -0800116}
117
118status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
119{
120 status_t status;
121
122 Mutex::Autolock _l(mLock);
123 int priority = handle->priority();
124 size_t size = mHandles.size();
125 EffectHandle *controlHandle = NULL;
126 size_t i;
127 for (i = 0; i < size; i++) {
128 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800129 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800130 continue;
131 }
132 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800134 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700135 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800136 if (h->priority() <= priority) {
137 break;
138 }
139 }
140 // if inserted in first place, move effect control from previous owner to this handle
141 if (i == 0) {
142 bool enabled = false;
143 if (controlHandle != NULL) {
144 enabled = controlHandle->enabled();
145 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
146 }
147 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
148 status = NO_ERROR;
149 } else {
150 status = ALREADY_EXISTS;
151 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700152 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800153 mHandles.insertAt(handle, i);
154 return status;
155}
156
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800157ssize_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800158{
159 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800160 return removeHandle_l(handle);
161}
162
163ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
164{
Eric Laurentca7cc822012-11-19 14:55:58 -0800165 size_t size = mHandles.size();
166 size_t i;
167 for (i = 0; i < size; i++) {
168 if (mHandles[i] == handle) {
169 break;
170 }
171 }
172 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800173 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
174 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800175 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800176 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800177
178 mHandles.removeAt(i);
179 // if removed from first place, move effect control from this handle to next in line
180 if (i == 0) {
181 EffectHandle *h = controlHandle_l();
182 if (h != NULL) {
183 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
184 }
185 }
186
187 // Prevent calls to process() and other functions on effect interface from now on.
188 // The effect engine will be released by the destructor when the last strong reference on
189 // this object is released which can happen after next process is called.
190 if (mHandles.size() == 0 && !mPinned) {
191 mState = DESTROYED;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800192 mEffectInterface->close();
Eric Laurentca7cc822012-11-19 14:55:58 -0800193 }
194
195 return mHandles.size();
196}
197
198// must be called with EffectModule::mLock held
199AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
200{
201 // the first valid handle in the list has control over the module
202 for (size_t i = 0; i < mHandles.size(); i++) {
203 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800204 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800205 return h;
206 }
207 }
208
209 return NULL;
210}
211
Eric Laurentf10c7092016-12-06 17:09:56 -0800212// unsafe method called when the effect parent thread has been destroyed
213ssize_t AudioFlinger::EffectModule::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
214{
215 ALOGV("disconnect() %p handle %p", this, handle);
216 Mutex::Autolock _l(mLock);
217 ssize_t numHandles = removeHandle_l(handle);
218 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
219 AudioSystem::unregisterEffect(mId);
220 sp<AudioFlinger> af = mAudioFlinger.promote();
221 if (af != 0) {
222 mLock.unlock();
223 af->updateOrphanEffectChains(this);
224 mLock.lock();
225 }
226 }
227 return numHandles;
228}
229
Eric Laurentfa1e1232016-08-02 19:01:49 -0700230bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800231 Mutex::Autolock _l(mLock);
232
Eric Laurentfa1e1232016-08-02 19:01:49 -0700233 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800234 switch (mState) {
235 case RESTART:
236 reset_l();
237 // FALL THROUGH
238
239 case STARTING:
240 // clear auxiliary effect input buffer for next accumulation
241 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
242 memset(mConfig.inputCfg.buffer.raw,
243 0,
244 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
245 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700246 if (start_l() == NO_ERROR) {
247 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700248 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700249 } else {
250 mState = IDLE;
251 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800252 break;
253 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700254 if (stop_l() == NO_ERROR) {
255 mDisableWaitCnt = mMaxDisableWaitCnt;
256 } else {
257 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
258 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800259 mState = STOPPED;
260 break;
261 case STOPPED:
262 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
263 // turn off sequence.
264 if (--mDisableWaitCnt == 0) {
265 reset_l();
266 mState = IDLE;
267 }
268 break;
269 default: //IDLE , ACTIVE, DESTROYED
270 break;
271 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700272
273 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800274}
275
276void AudioFlinger::EffectModule::process()
277{
278 Mutex::Autolock _l(mLock);
279
Mikhail Naganov022b9952017-01-04 16:36:51 -0800280 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800281 return;
282 }
283
284 if (isProcessEnabled()) {
285 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
286 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
287 ditherAndClamp(mConfig.inputCfg.buffer.s32,
288 mConfig.inputCfg.buffer.s32,
289 mConfig.inputCfg.buffer.frameCount/2);
290 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700291 int ret;
292 if (isProcessImplemented()) {
293 // do the actual processing in the effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800294 ret = mEffectInterface->process();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700295 } else {
296 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
297 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
298 int16_t *in = mConfig.inputCfg.buffer.s16;
299 int16_t *out = mConfig.outputCfg.buffer.s16;
Eric Laurentca7cc822012-11-19 14:55:58 -0800300
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700301 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
302 for (size_t i = 0; i < frameCnt; i++) {
303 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
304 }
305 } else {
306 memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
307 frameCnt * sizeof(int16_t));
308 }
309 }
310 ret = -ENODATA;
311 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800312 // force transition to IDLE state when engine is ready
313 if (mState == STOPPED && ret == -ENODATA) {
314 mDisableWaitCnt = 1;
315 }
316
317 // clear auxiliary effect input buffer for next accumulation
318 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
319 memset(mConfig.inputCfg.buffer.raw, 0,
320 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
321 }
322 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
323 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
324 // If an insert effect is idle and input buffer is different from output buffer,
325 // accumulate input onto output
326 sp<EffectChain> chain = mChain.promote();
327 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700328 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
Eric Laurentca7cc822012-11-19 14:55:58 -0800329 int16_t *in = mConfig.inputCfg.buffer.s16;
330 int16_t *out = mConfig.outputCfg.buffer.s16;
331 for (size_t i = 0; i < frameCnt; i++) {
332 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
333 }
334 }
335 }
336}
337
338void AudioFlinger::EffectModule::reset_l()
339{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700340 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800341 return;
342 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700343 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800344}
345
346status_t AudioFlinger::EffectModule::configure()
347{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700348 status_t status;
349 sp<ThreadBase> thread;
350 uint32_t size;
351 audio_channel_mask_t channelMask;
352
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700353 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700354 status = NO_INIT;
355 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800356 }
357
Eric Laurentd0ebb532013-04-02 16:41:41 -0700358 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800359 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700360 status = DEAD_OBJECT;
361 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800362 }
363
364 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700365 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700366 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800367
368 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
369 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900370 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
371 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800372 } else {
373 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700374 // TODO: Update this logic when multichannel effects are implemented.
375 // For offloaded tracks consider mono output as stereo for proper effect initialization
376 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
377 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
378 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
379 ALOGV("Overriding effect input and output as STEREO");
380 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800381 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700382
Eric Laurentca7cc822012-11-19 14:55:58 -0800383 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
384 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
385 mConfig.inputCfg.samplingRate = thread->sampleRate();
386 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
387 mConfig.inputCfg.bufferProvider.cookie = NULL;
388 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
389 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
390 mConfig.outputCfg.bufferProvider.cookie = NULL;
391 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
392 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
393 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
394 // Insert effect:
395 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
396 // always overwrites output buffer: input buffer == output buffer
397 // - in other sessions:
398 // last effect in the chain accumulates in output buffer: input buffer != output buffer
399 // other effect: overwrites output buffer: input buffer == output buffer
400 // Auxiliary effect:
401 // accumulates in output buffer: input buffer != output buffer
402 // Therefore: accumulate <=> input buffer != output buffer
403 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
404 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
405 } else {
406 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
407 }
408 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
409 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
410 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
411 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov022b9952017-01-04 16:36:51 -0800412 if (mInBuffer != 0) {
413 mInBuffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
414 }
415 if (mOutBuffer != 0) {
416 mOutBuffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
417 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800418
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700419 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800420 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
421
422 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700423 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700424 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
425 sizeof(effect_config_t),
426 &mConfig,
427 &size,
428 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800429 if (status == 0) {
430 status = cmdStatus;
431 }
432
433 if (status == 0 &&
434 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
435 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
436 effect_param_t *p = (effect_param_t *)buf32;
437
438 p->psize = sizeof(uint32_t);
439 p->vsize = sizeof(uint32_t);
440 size = sizeof(int);
441 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
442
443 uint32_t latency = 0;
444 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
445 if (pbt != NULL) {
446 latency = pbt->latency_l();
447 }
448
449 *((int32_t *)p->data + 1)= latency;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700450 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
451 sizeof(effect_param_t) + 8,
452 &buf32,
453 &size,
454 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800455 }
456
457 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
458 (1000 * mConfig.outputCfg.buffer.frameCount);
459
Eric Laurentd0ebb532013-04-02 16:41:41 -0700460exit:
461 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800462 return status;
463}
464
465status_t AudioFlinger::EffectModule::init()
466{
467 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700468 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800469 return NO_INIT;
470 }
471 status_t cmdStatus;
472 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700473 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
474 0,
475 NULL,
476 &size,
477 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800478 if (status == 0) {
479 status = cmdStatus;
480 }
481 return status;
482}
483
Eric Laurent1b928682014-10-02 19:41:47 -0700484void AudioFlinger::EffectModule::addEffectToHal_l()
485{
486 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
487 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
488 sp<ThreadBase> thread = mThread.promote();
489 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700490 sp<StreamHalInterface> stream = thread->stream();
491 if (stream != 0) {
492 status_t result = stream->addEffect(mEffectInterface);
493 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700494 }
495 }
496 }
497}
498
Eric Laurentfa1e1232016-08-02 19:01:49 -0700499// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800500status_t AudioFlinger::EffectModule::start()
501{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700502 sp<EffectChain> chain;
503 status_t status;
504 {
505 Mutex::Autolock _l(mLock);
506 status = start_l();
507 if (status == NO_ERROR) {
508 chain = mChain.promote();
509 }
510 }
511 if (chain != 0) {
512 chain->resetVolume_l();
513 }
514 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800515}
516
517status_t AudioFlinger::EffectModule::start_l()
518{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700519 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800520 return NO_INIT;
521 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700522 if (mStatus != NO_ERROR) {
523 return mStatus;
524 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800525 status_t cmdStatus;
526 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700527 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
528 0,
529 NULL,
530 &size,
531 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800532 if (status == 0) {
533 status = cmdStatus;
534 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700535 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700536 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800537 }
538 return status;
539}
540
541status_t AudioFlinger::EffectModule::stop()
542{
543 Mutex::Autolock _l(mLock);
544 return stop_l();
545}
546
547status_t AudioFlinger::EffectModule::stop_l()
548{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700549 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800550 return NO_INIT;
551 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700552 if (mStatus != NO_ERROR) {
553 return mStatus;
554 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800555 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800556 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700557 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
558 0,
559 NULL,
560 &size,
561 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800562 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800563 status = cmdStatus;
564 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800565 if (status == NO_ERROR) {
566 status = remove_effect_from_hal_l();
567 }
568 return status;
569}
570
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800571// must be called with EffectChain::mLock held
572void AudioFlinger::EffectModule::release_l()
573{
574 if (mEffectInterface != 0) {
575 remove_effect_from_hal_l();
576 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -0800577 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800578 mEffectInterface.clear();
579 }
580}
581
Eric Laurentbfb1b832013-01-07 09:53:42 -0800582status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
583{
584 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
585 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800586 sp<ThreadBase> thread = mThread.promote();
587 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700588 sp<StreamHalInterface> stream = thread->stream();
589 if (stream != 0) {
590 status_t result = stream->removeEffect(mEffectInterface);
591 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800592 }
593 }
594 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800595 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800596}
597
Andy Hunge4a1d912016-08-17 14:11:13 -0700598// round up delta valid if value and divisor are positive.
599template <typename T>
600static T roundUpDelta(const T &value, const T &divisor) {
601 T remainder = value % divisor;
602 return remainder == 0 ? 0 : divisor - remainder;
603}
604
Eric Laurentca7cc822012-11-19 14:55:58 -0800605status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
606 uint32_t cmdSize,
607 void *pCmdData,
608 uint32_t *replySize,
609 void *pReplyData)
610{
611 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700612 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800613
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700614 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800615 return NO_INIT;
616 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700617 if (mStatus != NO_ERROR) {
618 return mStatus;
619 }
Andy Hung110bc952016-06-20 15:22:52 -0700620 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700621 (sizeof(effect_param_t) > cmdSize ||
622 ((effect_param_t *)pCmdData)->psize > cmdSize
623 - sizeof(effect_param_t))) {
624 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800625 android_errorWriteLog(0x534e4554, "33003822");
626 return -EINVAL;
627 }
628 if (cmdCode == EFFECT_CMD_GET_PARAM &&
629 (*replySize < sizeof(effect_param_t) ||
630 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
631 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700632 return -EINVAL;
633 }
ragoe2759072016-11-22 18:02:48 -0800634 if (cmdCode == EFFECT_CMD_GET_PARAM &&
635 (sizeof(effect_param_t) > *replySize
636 || ((effect_param_t *)pCmdData)->psize > *replySize
637 - sizeof(effect_param_t)
638 || ((effect_param_t *)pCmdData)->vsize > *replySize
639 - sizeof(effect_param_t)
640 - ((effect_param_t *)pCmdData)->psize
641 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
642 *replySize
643 - sizeof(effect_param_t)
644 - ((effect_param_t *)pCmdData)->psize
645 - ((effect_param_t *)pCmdData)->vsize)) {
646 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
647 android_errorWriteLog(0x534e4554, "32705438");
648 return -EINVAL;
649 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700650 if ((cmdCode == EFFECT_CMD_SET_PARAM
651 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
652 (sizeof(effect_param_t) > cmdSize
653 || ((effect_param_t *)pCmdData)->psize > cmdSize
654 - sizeof(effect_param_t)
655 || ((effect_param_t *)pCmdData)->vsize > cmdSize
656 - sizeof(effect_param_t)
657 - ((effect_param_t *)pCmdData)->psize
658 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
659 cmdSize
660 - sizeof(effect_param_t)
661 - ((effect_param_t *)pCmdData)->psize
662 - ((effect_param_t *)pCmdData)->vsize)) {
663 android_errorWriteLog(0x534e4554, "30204301");
664 return -EINVAL;
665 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700666 status_t status = mEffectInterface->command(cmdCode,
667 cmdSize,
668 pCmdData,
669 replySize,
670 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800671 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
672 uint32_t size = (replySize == NULL) ? 0 : *replySize;
673 for (size_t i = 1; i < mHandles.size(); i++) {
674 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800675 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800676 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
677 }
678 }
679 }
680 return status;
681}
682
683status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
684{
685 Mutex::Autolock _l(mLock);
686 return setEnabled_l(enabled);
687}
688
689// must be called with EffectModule::mLock held
690status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
691{
692
693 ALOGV("setEnabled %p enabled %d", this, enabled);
694
695 if (enabled != isEnabled()) {
696 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
697 if (enabled && status != NO_ERROR) {
698 return status;
699 }
700
701 switch (mState) {
702 // going from disabled to enabled
703 case IDLE:
704 mState = STARTING;
705 break;
706 case STOPPED:
707 mState = RESTART;
708 break;
709 case STOPPING:
710 mState = ACTIVE;
711 break;
712
713 // going from enabled to disabled
714 case RESTART:
715 mState = STOPPED;
716 break;
717 case STARTING:
718 mState = IDLE;
719 break;
720 case ACTIVE:
721 mState = STOPPING;
722 break;
723 case DESTROYED:
724 return NO_ERROR; // simply ignore as we are being destroyed
725 }
726 for (size_t i = 1; i < mHandles.size(); i++) {
727 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800728 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800729 h->setEnabled(enabled);
730 }
731 }
732 }
733 return NO_ERROR;
734}
735
736bool AudioFlinger::EffectModule::isEnabled() const
737{
738 switch (mState) {
739 case RESTART:
740 case STARTING:
741 case ACTIVE:
742 return true;
743 case IDLE:
744 case STOPPING:
745 case STOPPED:
746 case DESTROYED:
747 default:
748 return false;
749 }
750}
751
752bool AudioFlinger::EffectModule::isProcessEnabled() const
753{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700754 if (mStatus != NO_ERROR) {
755 return false;
756 }
757
Eric Laurentca7cc822012-11-19 14:55:58 -0800758 switch (mState) {
759 case RESTART:
760 case ACTIVE:
761 case STOPPING:
762 case STOPPED:
763 return true;
764 case IDLE:
765 case STARTING:
766 case DESTROYED:
767 default:
768 return false;
769 }
770}
771
Mikhail Naganov022b9952017-01-04 16:36:51 -0800772void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
773 if (buffer != 0) {
774 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
775 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
776 } else {
777 mConfig.inputCfg.buffer.raw = NULL;
778 }
779 mInBuffer = buffer;
780 mEffectInterface->setInBuffer(buffer);
781}
782
783void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
784 if (buffer != 0) {
785 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
786 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
787 } else {
788 mConfig.outputCfg.buffer.raw = NULL;
789 }
790 mOutBuffer = buffer;
791 mEffectInterface->setOutBuffer(buffer);
792}
793
Eric Laurentca7cc822012-11-19 14:55:58 -0800794status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
795{
796 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700797 if (mStatus != NO_ERROR) {
798 return mStatus;
799 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800800 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800801 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
802 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
803 if (isProcessEnabled() &&
804 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
805 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800806 uint32_t volume[2];
807 uint32_t *pVolume = NULL;
808 uint32_t size = sizeof(volume);
809 volume[0] = *left;
810 volume[1] = *right;
811 if (controller) {
812 pVolume = volume;
813 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700814 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
815 size,
816 volume,
817 &size,
818 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -0800819 if (controller && status == NO_ERROR && size == sizeof(volume)) {
820 *left = volume[0];
821 *right = volume[1];
822 }
823 }
824 return status;
825}
826
827status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
828{
829 if (device == AUDIO_DEVICE_NONE) {
830 return NO_ERROR;
831 }
832
833 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700834 if (mStatus != NO_ERROR) {
835 return mStatus;
836 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800837 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700838 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 status_t cmdStatus;
840 uint32_t size = sizeof(status_t);
841 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
842 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700843 status = mEffectInterface->command(cmd,
844 sizeof(uint32_t),
845 &device,
846 &size,
847 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800848 }
849 return status;
850}
851
852status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
853{
854 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700855 if (mStatus != NO_ERROR) {
856 return mStatus;
857 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800858 status_t status = NO_ERROR;
859 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
860 status_t cmdStatus;
861 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700862 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
863 sizeof(audio_mode_t),
864 &mode,
865 &size,
866 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800867 if (status == NO_ERROR) {
868 status = cmdStatus;
869 }
870 }
871 return status;
872}
873
874status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
875{
876 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700877 if (mStatus != NO_ERROR) {
878 return mStatus;
879 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800880 status_t status = NO_ERROR;
881 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
882 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700883 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
884 sizeof(audio_source_t),
885 &source,
886 &size,
887 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800888 }
889 return status;
890}
891
892void AudioFlinger::EffectModule::setSuspended(bool suspended)
893{
894 Mutex::Autolock _l(mLock);
895 mSuspended = suspended;
896}
897
898bool AudioFlinger::EffectModule::suspended() const
899{
900 Mutex::Autolock _l(mLock);
901 return mSuspended;
902}
903
904bool AudioFlinger::EffectModule::purgeHandles()
905{
906 bool enabled = false;
907 Mutex::Autolock _l(mLock);
908 for (size_t i = 0; i < mHandles.size(); i++) {
909 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800910 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800911 if (handle->hasControl()) {
912 enabled = handle->enabled();
913 }
914 }
915 }
916 return enabled;
917}
918
Eric Laurent5baf2af2013-09-12 17:37:00 -0700919status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
920{
921 Mutex::Autolock _l(mLock);
922 if (mStatus != NO_ERROR) {
923 return mStatus;
924 }
925 status_t status = NO_ERROR;
926 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
927 status_t cmdStatus;
928 uint32_t size = sizeof(status_t);
929 effect_offload_param_t cmd;
930
931 cmd.isOffload = offloaded;
932 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700933 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
934 sizeof(effect_offload_param_t),
935 &cmd,
936 &size,
937 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700938 if (status == NO_ERROR) {
939 status = cmdStatus;
940 }
941 mOffloaded = (status == NO_ERROR) ? offloaded : false;
942 } else {
943 if (offloaded) {
944 status = INVALID_OPERATION;
945 }
946 mOffloaded = false;
947 }
948 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
949 return status;
950}
951
952bool AudioFlinger::EffectModule::isOffloaded() const
953{
954 Mutex::Autolock _l(mLock);
955 return mOffloaded;
956}
957
Marco Nelissenb2208842014-02-07 14:00:50 -0800958String8 effectFlagsToString(uint32_t flags) {
959 String8 s;
960
961 s.append("conn. mode: ");
962 switch (flags & EFFECT_FLAG_TYPE_MASK) {
963 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
964 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
965 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
966 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
967 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
968 default: s.append("unknown/reserved"); break;
969 }
970 s.append(", ");
971
972 s.append("insert pref: ");
973 switch (flags & EFFECT_FLAG_INSERT_MASK) {
974 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
975 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
976 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
977 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
978 default: s.append("unknown/reserved"); break;
979 }
980 s.append(", ");
981
982 s.append("volume mgmt: ");
983 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
984 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
985 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
986 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
987 default: s.append("unknown/reserved"); break;
988 }
989 s.append(", ");
990
991 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
992 if (devind) {
993 s.append("device indication: ");
994 switch (devind) {
995 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
996 default: s.append("unknown/reserved"); break;
997 }
998 s.append(", ");
999 }
1000
1001 s.append("input mode: ");
1002 switch (flags & EFFECT_FLAG_INPUT_MASK) {
1003 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
1004 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
1005 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
1006 default: s.append("not set"); break;
1007 }
1008 s.append(", ");
1009
1010 s.append("output mode: ");
1011 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
1012 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
1013 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
1014 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
1015 default: s.append("not set"); break;
1016 }
1017 s.append(", ");
1018
1019 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
1020 if (accel) {
1021 s.append("hardware acceleration: ");
1022 switch (accel) {
1023 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
1024 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
1025 default: s.append("unknown/reserved"); break;
1026 }
1027 s.append(", ");
1028 }
1029
1030 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
1031 if (modeind) {
1032 s.append("mode indication: ");
1033 switch (modeind) {
1034 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
1035 default: s.append("unknown/reserved"); break;
1036 }
1037 s.append(", ");
1038 }
1039
1040 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1041 if (srcind) {
1042 s.append("source indication: ");
1043 switch (srcind) {
1044 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1045 default: s.append("unknown/reserved"); break;
1046 }
1047 s.append(", ");
1048 }
1049
1050 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1051 s.append("offloadable, ");
1052 }
1053
1054 int len = s.length();
1055 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001056 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001057 s.unlockBuffer(len - 2);
1058 }
1059 return s;
1060}
1061
1062
Glenn Kasten0f11b512014-01-31 16:18:54 -08001063void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001064{
1065 const size_t SIZE = 256;
1066 char buffer[SIZE];
1067 String8 result;
1068
1069 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1070 result.append(buffer);
1071
1072 bool locked = AudioFlinger::dumpTryLock(mLock);
1073 // failed to lock - AudioFlinger is probably deadlocked
1074 if (!locked) {
1075 result.append("\t\tCould not lock Fx mutex:\n");
1076 }
1077
1078 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001079 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001080 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 result.append(buffer);
1082
1083 result.append("\t\tDescriptor:\n");
1084 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1085 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1086 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1087 mDescriptor.uuid.node[2],
1088 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1089 result.append(buffer);
1090 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1091 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1092 mDescriptor.type.timeHiAndVersion,
1093 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1094 mDescriptor.type.node[2],
1095 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1096 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001097 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001098 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001099 mDescriptor.flags,
1100 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001101 result.append(buffer);
1102 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1103 mDescriptor.name);
1104 result.append(buffer);
1105 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1106 mDescriptor.implementor);
1107 result.append(buffer);
1108
1109 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001110 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001111 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001112 mConfig.inputCfg.buffer.frameCount,
1113 mConfig.inputCfg.samplingRate,
1114 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001115 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001116 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001117 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001118 result.append(buffer);
1119
1120 result.append("\t\t- Output configuration:\n");
1121 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001122 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001123 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001124 mConfig.outputCfg.buffer.frameCount,
1125 mConfig.outputCfg.samplingRate,
1126 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001127 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001128 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001129 result.append(buffer);
1130
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001131 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001132 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001133 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001134 for (size_t i = 0; i < mHandles.size(); ++i) {
1135 EffectHandle *handle = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001136 if (handle != NULL && !handle->disconnected()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001137 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001138 result.append(buffer);
1139 }
1140 }
1141
Eric Laurentca7cc822012-11-19 14:55:58 -08001142 write(fd, result.string(), result.length());
1143
1144 if (locked) {
1145 mLock.unlock();
1146 }
1147}
1148
1149// ----------------------------------------------------------------------------
1150// EffectHandle implementation
1151// ----------------------------------------------------------------------------
1152
1153#undef LOG_TAG
1154#define LOG_TAG "AudioFlinger::EffectHandle"
1155
1156AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1157 const sp<AudioFlinger::Client>& client,
1158 const sp<IEffectClient>& effectClient,
1159 int32_t priority)
1160 : BnEffect(),
1161 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001162 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001163{
1164 ALOGV("constructor %p", this);
1165
1166 if (client == 0) {
1167 return;
1168 }
1169 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1170 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001171 if (mCblkMemory == 0 ||
1172 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001173 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001174 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001175 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001176 return;
1177 }
Glenn Kastene75da402013-11-20 13:54:52 -08001178 new(mCblk) effect_param_cblk_t();
1179 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001180}
1181
1182AudioFlinger::EffectHandle::~EffectHandle()
1183{
1184 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001185 disconnect(false);
1186}
1187
Glenn Kastene75da402013-11-20 13:54:52 -08001188status_t AudioFlinger::EffectHandle::initCheck()
1189{
1190 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1191}
1192
Eric Laurentca7cc822012-11-19 14:55:58 -08001193status_t AudioFlinger::EffectHandle::enable()
1194{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001195 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001196 ALOGV("enable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001197 sp<EffectModule> effect = mEffect.promote();
1198 if (effect == 0 || mDisconnected) {
1199 return DEAD_OBJECT;
1200 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001201 if (!mHasControl) {
1202 return INVALID_OPERATION;
1203 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001204
1205 if (mEnabled) {
1206 return NO_ERROR;
1207 }
1208
1209 mEnabled = true;
1210
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001211 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001212 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001213 thread->checkSuspendOnEffectEnabled(effect, true, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001214 }
1215
1216 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001217 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001218 return NO_ERROR;
1219 }
1220
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001221 status_t status = effect->setEnabled(true);
Eric Laurentca7cc822012-11-19 14:55:58 -08001222 if (status != NO_ERROR) {
1223 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001224 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurentca7cc822012-11-19 14:55:58 -08001225 }
1226 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001227 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001228 if (thread != 0) {
Eric Laurent6acd1d42017-01-04 14:23:29 -08001229 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1230 Mutex::Autolock _l(thread->mLock);
1231 thread->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001232 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001233 if (!effect->isOffloadable()) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001234 if (thread->type() == ThreadBase::OFFLOAD) {
1235 PlaybackThread *t = (PlaybackThread *)thread.get();
1236 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1237 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001238 if (effect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent59fe0102013-09-27 18:48:26 -07001239 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1240 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001241 }
1242 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001243 }
1244 return status;
1245}
1246
1247status_t AudioFlinger::EffectHandle::disable()
1248{
1249 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001250 AutoMutex _l(mLock);
1251 sp<EffectModule> effect = mEffect.promote();
1252 if (effect == 0 || mDisconnected) {
1253 return DEAD_OBJECT;
1254 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001255 if (!mHasControl) {
1256 return INVALID_OPERATION;
1257 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001258
1259 if (!mEnabled) {
1260 return NO_ERROR;
1261 }
1262 mEnabled = false;
1263
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001264 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001265 return NO_ERROR;
1266 }
1267
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001268 status_t status = effect->setEnabled(false);
Eric Laurentca7cc822012-11-19 14:55:58 -08001269
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001270 sp<ThreadBase> thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001271 if (thread != 0) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001272 thread->checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
Eric Laurent6acd1d42017-01-04 14:23:29 -08001273 if (thread->type() == ThreadBase::OFFLOAD || thread->type() == ThreadBase::MMAP) {
1274 Mutex::Autolock _l(thread->mLock);
1275 thread->broadcast_l();
Eric Laurent59fe0102013-09-27 18:48:26 -07001276 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001277 }
1278
1279 return status;
1280}
1281
1282void AudioFlinger::EffectHandle::disconnect()
1283{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001284 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001285 disconnect(true);
1286}
1287
1288void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1289{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001290 AutoMutex _l(mLock);
1291 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1292 if (mDisconnected) {
1293 if (unpinIfLast) {
1294 android_errorWriteLog(0x534e4554, "32707507");
1295 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 return;
1297 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001298 mDisconnected = true;
1299 sp<ThreadBase> thread;
1300 {
1301 sp<EffectModule> effect = mEffect.promote();
1302 if (effect != 0) {
1303 thread = effect->thread().promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08001304 }
1305 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001306 if (thread != 0) {
1307 thread->disconnectEffectHandle(this, unpinIfLast);
Eric Laurentf10c7092016-12-06 17:09:56 -08001308 } else {
1309 ALOGW("%s Effect handle %p disconnected after thread destruction", __FUNCTION__, this);
1310 // try to cleanup as much as we can
1311 sp<EffectModule> effect = mEffect.promote();
1312 if (effect != 0) {
1313 effect->disconnectHandle(this, unpinIfLast);
1314 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001315 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001316
Eric Laurentca7cc822012-11-19 14:55:58 -08001317 if (mClient != 0) {
1318 if (mCblk != NULL) {
1319 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1320 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1321 }
1322 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001323 // Client destructor must run with AudioFlinger client mutex locked
1324 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001325 mClient.clear();
1326 }
1327}
1328
1329status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1330 uint32_t cmdSize,
1331 void *pCmdData,
1332 uint32_t *replySize,
1333 void *pReplyData)
1334{
1335 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001336 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001337
Eric Laurentc7ab3092017-06-15 18:43:46 -07001338 // reject commands reserved for internal use by audio framework if coming from outside
1339 // of audioserver
1340 switch(cmdCode) {
1341 case EFFECT_CMD_ENABLE:
1342 case EFFECT_CMD_DISABLE:
1343 case EFFECT_CMD_SET_PARAM:
1344 case EFFECT_CMD_SET_PARAM_DEFERRED:
1345 case EFFECT_CMD_SET_PARAM_COMMIT:
1346 case EFFECT_CMD_GET_PARAM:
1347 break;
1348 default:
1349 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1350 break;
1351 }
1352 android_errorWriteLog(0x534e4554, "62019992");
1353 return BAD_VALUE;
1354 }
1355
Eric Laurent1ffc5852016-12-15 14:46:09 -08001356 if (cmdCode == EFFECT_CMD_ENABLE) {
1357 if (*replySize < sizeof(int)) {
1358 android_errorWriteLog(0x534e4554, "32095713");
1359 return BAD_VALUE;
1360 }
1361 *(int *)pReplyData = NO_ERROR;
1362 *replySize = sizeof(int);
1363 return enable();
1364 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1365 if (*replySize < sizeof(int)) {
1366 android_errorWriteLog(0x534e4554, "32095713");
1367 return BAD_VALUE;
1368 }
1369 *(int *)pReplyData = NO_ERROR;
1370 *replySize = sizeof(int);
1371 return disable();
1372 }
1373
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001374 AutoMutex _l(mLock);
1375 sp<EffectModule> effect = mEffect.promote();
1376 if (effect == 0 || mDisconnected) {
1377 return DEAD_OBJECT;
1378 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001379 // only get parameter command is permitted for applications not controlling the effect
1380 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1381 return INVALID_OPERATION;
1382 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001383 if (mClient == 0) {
1384 return INVALID_OPERATION;
1385 }
1386
1387 // handle commands that are not forwarded transparently to effect engine
1388 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001389 if (*replySize < sizeof(int)) {
1390 android_errorWriteLog(0x534e4554, "32095713");
1391 return BAD_VALUE;
1392 }
1393 *(int *)pReplyData = NO_ERROR;
1394 *replySize = sizeof(int);
1395
Eric Laurentca7cc822012-11-19 14:55:58 -08001396 // No need to trylock() here as this function is executed in the binder thread serving a
1397 // particular client process: no risk to block the whole media server process or mixer
1398 // threads if we are stuck here
1399 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001400 // keep local copy of index in case of client corruption b/32220769
1401 const uint32_t clientIndex = mCblk->clientIndex;
1402 const uint32_t serverIndex = mCblk->serverIndex;
1403 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1404 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001405 mCblk->serverIndex = 0;
1406 mCblk->clientIndex = 0;
1407 return BAD_VALUE;
1408 }
1409 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001410 effect_param_t *param = NULL;
1411 for (uint32_t index = serverIndex; index < clientIndex;) {
1412 int *p = (int *)(mBuffer + index);
1413 const int size = *p++;
1414 if (size < 0
1415 || size > EFFECT_PARAM_BUFFER_SIZE
1416 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001418 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 break;
1420 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001421
1422 // copy to local memory in case of client corruption b/32220769
1423 param = (effect_param_t *)realloc(param, size);
1424 if (param == NULL) {
1425 ALOGW("command(): out of memory");
1426 status = NO_MEMORY;
1427 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001428 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001429 memcpy(param, p, size);
1430
1431 int reply = 0;
1432 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001433 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001434 size,
1435 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001436 &rsize,
1437 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001438
1439 // verify shared memory: server index shouldn't change; client index can't go back.
1440 if (serverIndex != mCblk->serverIndex
1441 || clientIndex > mCblk->clientIndex) {
1442 android_errorWriteLog(0x534e4554, "32220769");
1443 status = BAD_VALUE;
1444 break;
1445 }
1446
Eric Laurentca7cc822012-11-19 14:55:58 -08001447 // stop at first error encountered
1448 if (ret != NO_ERROR) {
1449 status = ret;
1450 *(int *)pReplyData = reply;
1451 break;
1452 } else if (reply != NO_ERROR) {
1453 *(int *)pReplyData = reply;
1454 break;
1455 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001456 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001457 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001458 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001459 mCblk->serverIndex = 0;
1460 mCblk->clientIndex = 0;
1461 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001462 }
1463
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001464 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001465}
1466
1467void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1468{
1469 ALOGV("setControl %p control %d", this, hasControl);
1470
1471 mHasControl = hasControl;
1472 mEnabled = enabled;
1473
1474 if (signal && mEffectClient != 0) {
1475 mEffectClient->controlStatusChanged(hasControl);
1476 }
1477}
1478
1479void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1480 uint32_t cmdSize,
1481 void *pCmdData,
1482 uint32_t replySize,
1483 void *pReplyData)
1484{
1485 if (mEffectClient != 0) {
1486 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1487 }
1488}
1489
1490
1491
1492void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1493{
1494 if (mEffectClient != 0) {
1495 mEffectClient->enableStatusChanged(enabled);
1496 }
1497}
1498
1499status_t AudioFlinger::EffectHandle::onTransact(
1500 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1501{
1502 return BnEffect::onTransact(code, data, reply, flags);
1503}
1504
1505
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001506void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001507{
1508 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1509
Marco Nelissenb2208842014-02-07 14:00:50 -08001510 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001511 (mClient == 0) ? getpid_cached : mClient->pid(),
1512 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001513 mHasControl ? "yes" : "no",
1514 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001515 mCblk ? mCblk->clientIndex : 0,
1516 mCblk ? mCblk->serverIndex : 0
1517 );
1518
1519 if (locked) {
1520 mCblk->lock.unlock();
1521 }
1522}
1523
1524#undef LOG_TAG
1525#define LOG_TAG "AudioFlinger::EffectChain"
1526
1527AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001528 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001529 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001530 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001531 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001532{
1533 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1534 if (thread == NULL) {
1535 return;
1536 }
1537 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1538 thread->frameCount();
1539}
1540
1541AudioFlinger::EffectChain::~EffectChain()
1542{
Eric Laurentca7cc822012-11-19 14:55:58 -08001543}
1544
1545// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1546sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1547 effect_descriptor_t *descriptor)
1548{
1549 size_t size = mEffects.size();
1550
1551 for (size_t i = 0; i < size; i++) {
1552 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1553 return mEffects[i];
1554 }
1555 }
1556 return 0;
1557}
1558
1559// getEffectFromId_l() must be called with ThreadBase::mLock held
1560sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1561{
1562 size_t size = mEffects.size();
1563
1564 for (size_t i = 0; i < size; i++) {
1565 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1566 if (id == 0 || mEffects[i]->id() == id) {
1567 return mEffects[i];
1568 }
1569 }
1570 return 0;
1571}
1572
1573// getEffectFromType_l() must be called with ThreadBase::mLock held
1574sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1575 const effect_uuid_t *type)
1576{
1577 size_t size = mEffects.size();
1578
1579 for (size_t i = 0; i < size; i++) {
1580 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1581 return mEffects[i];
1582 }
1583 }
1584 return 0;
1585}
1586
1587void AudioFlinger::EffectChain::clearInputBuffer()
1588{
1589 Mutex::Autolock _l(mLock);
1590 sp<ThreadBase> thread = mThread.promote();
1591 if (thread == 0) {
1592 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1593 return;
1594 }
1595 clearInputBuffer_l(thread);
1596}
1597
1598// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001599void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001600{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001601 if (mInBuffer == NULL) {
1602 return;
1603 }
Ricardo Garcia322bab22014-08-06 11:43:46 -07001604 // TODO: This will change in the future, depending on multichannel
1605 // and sample format changes for effects.
1606 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1607 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001608 const size_t frameSize =
1609 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Mikhail Naganov022b9952017-01-04 16:36:51 -08001610 memset(mInBuffer->audioBuffer()->raw, 0, thread->frameCount() * frameSize);
1611 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08001612}
1613
1614// Must be called with EffectChain::mLock locked
1615void AudioFlinger::EffectChain::process_l()
1616{
1617 sp<ThreadBase> thread = mThread.promote();
1618 if (thread == 0) {
1619 ALOGW("process_l(): cannot promote mixer thread");
1620 return;
1621 }
1622 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1623 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001624 // never process effects when:
1625 // - on an OFFLOAD thread
1626 // - no more tracks are on the session and the effect tail has been rendered
Phil Burk869fab12017-02-27 18:44:19 -08001627 bool doProcess = (thread->type() != ThreadBase::OFFLOAD)
1628 && (thread->type() != ThreadBase::MMAP);
Eric Laurentca7cc822012-11-19 14:55:58 -08001629 if (!isGlobalSession) {
1630 bool tracksOnSession = (trackCnt() != 0);
1631
1632 if (!tracksOnSession && mTailBufferCount == 0) {
1633 doProcess = false;
1634 }
1635
1636 if (activeTrackCnt() == 0) {
1637 // if no track is active and the effect tail has not been rendered,
1638 // the input buffer must be cleared here as the mixer process will not do it
1639 if (tracksOnSession || mTailBufferCount > 0) {
1640 clearInputBuffer_l(thread);
1641 if (mTailBufferCount > 0) {
1642 mTailBufferCount--;
1643 }
1644 }
1645 }
1646 }
1647
1648 size_t size = mEffects.size();
1649 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08001650 // Only the input and output buffers of the chain can be external,
1651 // and 'update' / 'commit' do nothing for allocated buffers, thus
1652 // it's not needed to consider any other buffers here.
1653 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08001654 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1655 mOutBuffer->update();
1656 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001657 for (size_t i = 0; i < size; i++) {
1658 mEffects[i]->process();
1659 }
Mikhail Naganov06888802017-01-19 12:47:55 -08001660 mInBuffer->commit();
1661 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
1662 mOutBuffer->commit();
1663 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001664 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001665 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001667 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1668 }
1669 if (doResetVolume) {
1670 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001671 }
1672}
1673
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001674// createEffect_l() must be called with ThreadBase::mLock held
1675status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
1676 ThreadBase *thread,
1677 effect_descriptor_t *desc,
1678 int id,
1679 audio_session_t sessionId,
1680 bool pinned)
1681{
1682 Mutex::Autolock _l(mLock);
1683 effect = new EffectModule(thread, this, desc, id, sessionId, pinned);
1684 status_t lStatus = effect->status();
1685 if (lStatus == NO_ERROR) {
1686 lStatus = addEffect_ll(effect);
1687 }
1688 if (lStatus != NO_ERROR) {
1689 effect.clear();
1690 }
1691 return lStatus;
1692}
1693
1694// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001695status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1696{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001697 Mutex::Autolock _l(mLock);
1698 return addEffect_ll(effect);
1699}
1700// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
1701status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
1702{
Eric Laurentca7cc822012-11-19 14:55:58 -08001703 effect_descriptor_t desc = effect->desc();
1704 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1705
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 effect->setChain(this);
1707 sp<ThreadBase> thread = mThread.promote();
1708 if (thread == 0) {
1709 return NO_INIT;
1710 }
1711 effect->setThread(thread);
1712
1713 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1714 // Auxiliary effects are inserted at the beginning of mEffects vector as
1715 // they are processed first and accumulated in chain input buffer
1716 mEffects.insertAt(effect, 0);
1717
1718 // the input buffer for auxiliary effect contains mono samples in
1719 // 32 bit format. This is to avoid saturation in AudoMixer
1720 // accumulation stage. Saturation is done in EffectModule::process() before
1721 // calling the process in effect engine
1722 size_t numSamples = thread->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08001723 sp<EffectBufferHalInterface> halBuffer;
1724 status_t result = EffectBufferHalInterface::allocate(
1725 numSamples * sizeof(int32_t), &halBuffer);
1726 if (result != OK) return result;
1727 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08001728 // auxiliary effects output samples to chain input buffer for further processing
1729 // by insert effects
1730 effect->setOutBuffer(mInBuffer);
1731 } else {
1732 // Insert effects are inserted at the end of mEffects vector as they are processed
1733 // after track and auxiliary effects.
1734 // Insert effect order as a function of indicated preference:
1735 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1736 // another effect is present
1737 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1738 // last effect claiming first position
1739 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1740 // first effect claiming last position
1741 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1742 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1743 // already present
1744
1745 size_t size = mEffects.size();
1746 size_t idx_insert = size;
1747 ssize_t idx_insert_first = -1;
1748 ssize_t idx_insert_last = -1;
1749
1750 for (size_t i = 0; i < size; i++) {
1751 effect_descriptor_t d = mEffects[i]->desc();
1752 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1753 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1754 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1755 // check invalid effect chaining combinations
1756 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1757 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1758 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1759 desc.name, d.name);
1760 return INVALID_OPERATION;
1761 }
1762 // remember position of first insert effect and by default
1763 // select this as insert position for new effect
1764 if (idx_insert == size) {
1765 idx_insert = i;
1766 }
1767 // remember position of last insert effect claiming
1768 // first position
1769 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1770 idx_insert_first = i;
1771 }
1772 // remember position of first insert effect claiming
1773 // last position
1774 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1775 idx_insert_last == -1) {
1776 idx_insert_last = i;
1777 }
1778 }
1779 }
1780
1781 // modify idx_insert from first position if needed
1782 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1783 if (idx_insert_last != -1) {
1784 idx_insert = idx_insert_last;
1785 } else {
1786 idx_insert = size;
1787 }
1788 } else {
1789 if (idx_insert_first != -1) {
1790 idx_insert = idx_insert_first + 1;
1791 }
1792 }
1793
1794 // always read samples from chain input buffer
1795 effect->setInBuffer(mInBuffer);
1796
1797 // if last effect in the chain, output samples to chain
1798 // output buffer, otherwise to chain input buffer
1799 if (idx_insert == size) {
1800 if (idx_insert != 0) {
1801 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1802 mEffects[idx_insert-1]->configure();
1803 }
1804 effect->setOutBuffer(mOutBuffer);
1805 } else {
1806 effect->setOutBuffer(mInBuffer);
1807 }
1808 mEffects.insertAt(effect, idx_insert);
1809
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001810 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001811 idx_insert);
1812 }
1813 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07001814
Eric Laurentca7cc822012-11-19 14:55:58 -08001815 return NO_ERROR;
1816}
1817
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001818// removeEffect_l() must be called with ThreadBase::mLock held
1819size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
1820 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08001821{
1822 Mutex::Autolock _l(mLock);
1823 size_t size = mEffects.size();
1824 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1825
1826 for (size_t i = 0; i < size; i++) {
1827 if (effect == mEffects[i]) {
1828 // calling stop here will remove pre-processing effect from the audio HAL.
1829 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1830 // the middle of a read from audio HAL
1831 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1832 mEffects[i]->state() == EffectModule::STOPPING) {
1833 mEffects[i]->stop();
1834 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001835 if (release) {
1836 mEffects[i]->release_l();
1837 }
1838
Mikhail Naganov022b9952017-01-04 16:36:51 -08001839 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 if (i == size - 1 && i != 0) {
1841 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1842 mEffects[i - 1]->configure();
1843 }
1844 }
1845 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001846 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001847 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001848
Eric Laurentca7cc822012-11-19 14:55:58 -08001849 break;
1850 }
1851 }
1852
1853 return mEffects.size();
1854}
1855
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001856// setDevice_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001857void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1858{
1859 size_t size = mEffects.size();
1860 for (size_t i = 0; i < size; i++) {
1861 mEffects[i]->setDevice(device);
1862 }
1863}
1864
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001865// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001866void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1867{
1868 size_t size = mEffects.size();
1869 for (size_t i = 0; i < size; i++) {
1870 mEffects[i]->setMode(mode);
1871 }
1872}
1873
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001874// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001875void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1876{
1877 size_t size = mEffects.size();
1878 for (size_t i = 0; i < size; i++) {
1879 mEffects[i]->setAudioSource(source);
1880 }
1881}
1882
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001883// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001884bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001885{
1886 uint32_t newLeft = *left;
1887 uint32_t newRight = *right;
1888 bool hasControl = false;
1889 int ctrlIdx = -1;
1890 size_t size = mEffects.size();
1891
1892 // first update volume controller
1893 for (size_t i = size; i > 0; i--) {
1894 if (mEffects[i - 1]->isProcessEnabled() &&
1895 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1896 ctrlIdx = i - 1;
1897 hasControl = true;
1898 break;
1899 }
1900 }
1901
Eric Laurentfa1e1232016-08-02 19:01:49 -07001902 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001903 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001904 if (hasControl) {
1905 *left = mNewLeftVolume;
1906 *right = mNewRightVolume;
1907 }
1908 return hasControl;
1909 }
1910
1911 mVolumeCtrlIdx = ctrlIdx;
1912 mLeftVolume = newLeft;
1913 mRightVolume = newRight;
1914
1915 // second get volume update from volume controller
1916 if (ctrlIdx >= 0) {
1917 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1918 mNewLeftVolume = newLeft;
1919 mNewRightVolume = newRight;
1920 }
1921 // then indicate volume to all other effects in chain.
1922 // Pass altered volume to effects before volume controller
1923 // and requested volume to effects after controller
1924 uint32_t lVol = newLeft;
1925 uint32_t rVol = newRight;
1926
1927 for (size_t i = 0; i < size; i++) {
1928 if ((int)i == ctrlIdx) {
1929 continue;
1930 }
1931 // this also works for ctrlIdx == -1 when there is no volume controller
1932 if ((int)i > ctrlIdx) {
1933 lVol = *left;
1934 rVol = *right;
1935 }
1936 mEffects[i]->setVolume(&lVol, &rVol, false);
1937 }
1938 *left = newLeft;
1939 *right = newRight;
1940
1941 return hasControl;
1942}
1943
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001944// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07001945void AudioFlinger::EffectChain::resetVolume_l()
1946{
Eric Laurente7449bf2016-08-03 18:44:07 -07001947 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1948 uint32_t left = mLeftVolume;
1949 uint32_t right = mRightVolume;
1950 (void)setVolume_l(&left, &right, true);
1951 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001952}
1953
Eric Laurent1b928682014-10-02 19:41:47 -07001954void AudioFlinger::EffectChain::syncHalEffectsState()
1955{
1956 Mutex::Autolock _l(mLock);
1957 for (size_t i = 0; i < mEffects.size(); i++) {
1958 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1959 mEffects[i]->state() == EffectModule::STOPPING) {
1960 mEffects[i]->addEffectToHal_l();
1961 }
1962 }
1963}
1964
Mikhail Naganov06888802017-01-19 12:47:55 -08001965static void dumpInOutBuffer(
1966 char *dump, size_t dumpSize, bool isInput, EffectBufferHalInterface *buffer) {
Mikhail Naganovc778e592017-01-25 10:35:30 -08001967 if (buffer == nullptr) {
1968 snprintf(dump, dumpSize, "%p", buffer);
1969 } else if (buffer->externalData() != nullptr) {
Mikhail Naganov06888802017-01-19 12:47:55 -08001970 snprintf(dump, dumpSize, "%p -> %p",
1971 isInput ? buffer->externalData() : buffer->audioBuffer()->raw,
1972 isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1973 } else {
1974 snprintf(dump, dumpSize, "%p", buffer->audioBuffer()->raw);
1975 }
1976}
1977
Eric Laurentca7cc822012-11-19 14:55:58 -08001978void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1979{
1980 const size_t SIZE = 256;
1981 char buffer[SIZE];
1982 String8 result;
1983
Marco Nelissenb2208842014-02-07 14:00:50 -08001984 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001985 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001986 result.append(buffer);
1987
Marco Nelissenb2208842014-02-07 14:00:50 -08001988 if (numEffects) {
1989 bool locked = AudioFlinger::dumpTryLock(mLock);
1990 // failed to lock - AudioFlinger is probably deadlocked
1991 if (!locked) {
1992 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001993 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001994
Mikhail Naganov06888802017-01-19 12:47:55 -08001995 char inBufferStr[64], outBufferStr[64];
1996 dumpInOutBuffer(inBufferStr, sizeof(inBufferStr), true, mInBuffer.get());
1997 dumpInOutBuffer(outBufferStr, sizeof(outBufferStr), false, mOutBuffer.get());
1998 snprintf(buffer, SIZE, "\t%-*s%-*s Active tracks:\n",
1999 (int)strlen(inBufferStr), "In buffer ",
2000 (int)strlen(outBufferStr), "Out buffer ");
2001 result.append(buffer);
2002 snprintf(buffer, SIZE, "\t%s %s %d\n", inBufferStr, outBufferStr, mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002003 result.append(buffer);
2004 write(fd, result.string(), result.size());
2005
2006 for (size_t i = 0; i < numEffects; ++i) {
2007 sp<EffectModule> effect = mEffects[i];
2008 if (effect != 0) {
2009 effect->dump(fd, args);
2010 }
2011 }
2012
2013 if (locked) {
2014 mLock.unlock();
2015 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002016 }
2017}
2018
2019// must be called with ThreadBase::mLock held
2020void AudioFlinger::EffectChain::setEffectSuspended_l(
2021 const effect_uuid_t *type, bool suspend)
2022{
2023 sp<SuspendedEffectDesc> desc;
2024 // use effect type UUID timelow as key as there is no real risk of identical
2025 // timeLow fields among effect type UUIDs.
2026 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2027 if (suspend) {
2028 if (index >= 0) {
2029 desc = mSuspendedEffects.valueAt(index);
2030 } else {
2031 desc = new SuspendedEffectDesc();
2032 desc->mType = *type;
2033 mSuspendedEffects.add(type->timeLow, desc);
2034 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2035 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002036
Eric Laurentca7cc822012-11-19 14:55:58 -08002037 if (desc->mRefCount++ == 0) {
2038 sp<EffectModule> effect = getEffectIfEnabled(type);
2039 if (effect != 0) {
2040 desc->mEffect = effect;
2041 effect->setSuspended(true);
2042 effect->setEnabled(false);
2043 }
2044 }
2045 } else {
2046 if (index < 0) {
2047 return;
2048 }
2049 desc = mSuspendedEffects.valueAt(index);
2050 if (desc->mRefCount <= 0) {
2051 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002052 desc->mRefCount = 0;
2053 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 }
2055 if (--desc->mRefCount == 0) {
2056 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2057 if (desc->mEffect != 0) {
2058 sp<EffectModule> effect = desc->mEffect.promote();
2059 if (effect != 0) {
2060 effect->setSuspended(false);
2061 effect->lock();
2062 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002063 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002064 effect->setEnabled_l(handle->enabled());
2065 }
2066 effect->unlock();
2067 }
2068 desc->mEffect.clear();
2069 }
2070 mSuspendedEffects.removeItemsAt(index);
2071 }
2072 }
2073}
2074
2075// must be called with ThreadBase::mLock held
2076void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2077{
2078 sp<SuspendedEffectDesc> desc;
2079
2080 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2081 if (suspend) {
2082 if (index >= 0) {
2083 desc = mSuspendedEffects.valueAt(index);
2084 } else {
2085 desc = new SuspendedEffectDesc();
2086 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2087 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2088 }
2089 if (desc->mRefCount++ == 0) {
2090 Vector< sp<EffectModule> > effects;
2091 getSuspendEligibleEffects(effects);
2092 for (size_t i = 0; i < effects.size(); i++) {
2093 setEffectSuspended_l(&effects[i]->desc().type, true);
2094 }
2095 }
2096 } else {
2097 if (index < 0) {
2098 return;
2099 }
2100 desc = mSuspendedEffects.valueAt(index);
2101 if (desc->mRefCount <= 0) {
2102 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2103 desc->mRefCount = 1;
2104 }
2105 if (--desc->mRefCount == 0) {
2106 Vector<const effect_uuid_t *> types;
2107 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2108 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2109 continue;
2110 }
2111 types.add(&mSuspendedEffects.valueAt(i)->mType);
2112 }
2113 for (size_t i = 0; i < types.size(); i++) {
2114 setEffectSuspended_l(types[i], false);
2115 }
2116 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2117 mSuspendedEffects.keyAt(index));
2118 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2119 }
2120 }
2121}
2122
2123
2124// The volume effect is used for automated tests only
2125#ifndef OPENSL_ES_H_
2126static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2127 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2128const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2129#endif //OPENSL_ES_H_
2130
Eric Laurentd8365c52017-07-16 15:27:05 -07002131/* static */
2132bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2133{
2134 // Only NS and AEC are suspended when BtNRec is off
2135 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2136 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2137 return true;
2138 }
2139 return false;
2140}
2141
Eric Laurentca7cc822012-11-19 14:55:58 -08002142bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2143{
2144 // auxiliary effects and visualizer are never suspended on output mix
2145 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2146 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2147 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
2148 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
2149 return false;
2150 }
2151 return true;
2152}
2153
2154void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2155 Vector< sp<AudioFlinger::EffectModule> > &effects)
2156{
2157 effects.clear();
2158 for (size_t i = 0; i < mEffects.size(); i++) {
2159 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2160 effects.add(mEffects[i]);
2161 }
2162 }
2163}
2164
2165sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2166 const effect_uuid_t *type)
2167{
2168 sp<EffectModule> effect = getEffectFromType_l(type);
2169 return effect != 0 && effect->isEnabled() ? effect : 0;
2170}
2171
2172void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2173 bool enabled)
2174{
2175 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2176 if (enabled) {
2177 if (index < 0) {
2178 // if the effect is not suspend check if all effects are suspended
2179 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2180 if (index < 0) {
2181 return;
2182 }
2183 if (!isEffectEligibleForSuspend(effect->desc())) {
2184 return;
2185 }
2186 setEffectSuspended_l(&effect->desc().type, enabled);
2187 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2188 if (index < 0) {
2189 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2190 return;
2191 }
2192 }
2193 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2194 effect->desc().type.timeLow);
2195 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002196 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002197 if (desc->mEffect == 0) {
2198 desc->mEffect = effect;
2199 effect->setEnabled(false);
2200 effect->setSuspended(true);
2201 }
2202 } else {
2203 if (index < 0) {
2204 return;
2205 }
2206 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2207 effect->desc().type.timeLow);
2208 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2209 desc->mEffect.clear();
2210 effect->setSuspended(false);
2211 }
2212}
2213
Eric Laurent5baf2af2013-09-12 17:37:00 -07002214bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002215{
2216 Mutex::Autolock _l(mLock);
2217 size_t size = mEffects.size();
2218 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002219 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002220 return true;
2221 }
2222 }
2223 return false;
2224}
2225
Eric Laurentaaa44472014-09-12 17:41:50 -07002226void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2227{
2228 Mutex::Autolock _l(mLock);
2229 mThread = thread;
2230 for (size_t i = 0; i < mEffects.size(); i++) {
2231 mEffects[i]->setThread(thread);
2232 }
2233}
2234
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002235void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2236{
2237 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2238 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2239 }
2240 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2241 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2242 }
2243}
2244
2245void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2246{
2247 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2248 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2249 }
2250 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2251 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2252 }
2253}
2254
2255bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002256{
2257 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002258 for (const auto &effect : mEffects) {
2259 if (effect->isProcessImplemented()) {
2260 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002261 }
2262 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002263 // Allow effects without processing.
2264 return true;
2265}
2266
2267bool AudioFlinger::EffectChain::isFastCompatible() const
2268{
2269 Mutex::Autolock _l(mLock);
2270 for (const auto &effect : mEffects) {
2271 if (effect->isProcessImplemented()
2272 && effect->isImplementationSoftware()) {
2273 return false;
2274 }
2275 }
2276 // Allow effects without processing or hw accelerated effects.
2277 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002278}
2279
2280// isCompatibleWithThread_l() must be called with thread->mLock held
2281bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2282{
2283 Mutex::Autolock _l(mLock);
2284 for (size_t i = 0; i < mEffects.size(); i++) {
2285 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2286 return false;
2287 }
2288 }
2289 return true;
2290}
2291
Glenn Kasten63238ef2015-03-02 15:50:29 -08002292} // namespace android