blob: 022bf59ceac460a3cd984e15da730924947b4e9a [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080023#include <utils/Log.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080024#include <audio_utils/primitives.h>
25#include <private/media/AudioEffectShared.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070026#include <media/audiohal/EffectHalInterface.h>
27#include <media/audiohal/EffectsFactoryHalInterface.h>
Mikhail Naganov9fe94012016-10-14 14:57:40 -070028#include <system/audio_effects/effect_visualizer.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080029
30#include "AudioFlinger.h"
31#include "ServiceUtilities.h"
32
33// ----------------------------------------------------------------------------
34
35// Note: the following macro is used for extremely verbose logging message. In
36// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
37// 0; but one side effect of this is to turn all LOGV's as well. Some messages
38// are so verbose that we want to suppress them even when we have ALOG_ASSERT
39// turned on. Do not uncomment the #def below unless you really know what you
40// are doing and want to see all of the extremely verbose messages.
41//#define VERY_VERY_VERBOSE_LOGGING
42#ifdef VERY_VERY_VERBOSE_LOGGING
43#define ALOGVV ALOGV
44#else
45#define ALOGVV(a...) do { } while(0)
46#endif
47
Ricardo Garcia726b6a72014-08-11 12:04:54 -070048#define min(a, b) ((a) < (b) ? (a) : (b))
49
Eric Laurentca7cc822012-11-19 14:55:58 -080050namespace android {
51
52// ----------------------------------------------------------------------------
53// EffectModule implementation
54// ----------------------------------------------------------------------------
55
56#undef LOG_TAG
57#define LOG_TAG "AudioFlinger::EffectModule"
58
59AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
60 const wp<AudioFlinger::EffectChain>& chain,
61 effect_descriptor_t *desc,
62 int id,
Glenn Kastend848eb42016-03-08 13:42:11 -080063 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -080064 : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX),
65 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
66 mDescriptor(*desc),
67 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080068 mStatus(NO_INIT), mState(IDLE),
69 // mMaxDisableWaitCnt is set by configure() and not used before then
70 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070071 mSuspended(false),
72 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080073{
74 ALOGV("Constructor %p", this);
75 int lStatus;
76
77 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070078 mStatus = -ENODEV;
79 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070080 if (audioFlinger != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070081 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
Mikhail Naganov1dc98672016-08-18 17:50:29 -070082 if (effectsFactory != 0) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070083 mStatus = effectsFactory->createEffect(
84 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
85 }
86 }
Eric Laurentca7cc822012-11-19 14:55:58 -080087
88 if (mStatus != NO_ERROR) {
89 return;
90 }
91 lStatus = init();
92 if (lStatus < 0) {
93 mStatus = lStatus;
94 goto Error;
95 }
96
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070097 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -080098 return;
99Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700100 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800101 ALOGV("Constructor Error %d", mStatus);
102}
103
104AudioFlinger::EffectModule::~EffectModule()
105{
106 ALOGV("Destructor %p", this);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700107 if (mEffectInterface != 0) {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800108 remove_effect_from_hal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800109 // release effect engine
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700110 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800111 }
112}
113
114status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
115{
116 status_t status;
117
118 Mutex::Autolock _l(mLock);
119 int priority = handle->priority();
120 size_t size = mHandles.size();
121 EffectHandle *controlHandle = NULL;
122 size_t i;
123 for (i = 0; i < size; i++) {
124 EffectHandle *h = mHandles[i];
125 if (h == NULL || h->destroyed_l()) {
126 continue;
127 }
128 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700129 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800130 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700131 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800132 if (h->priority() <= priority) {
133 break;
134 }
135 }
136 // if inserted in first place, move effect control from previous owner to this handle
137 if (i == 0) {
138 bool enabled = false;
139 if (controlHandle != NULL) {
140 enabled = controlHandle->enabled();
141 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
142 }
143 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
144 status = NO_ERROR;
145 } else {
146 status = ALREADY_EXISTS;
147 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700148 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800149 mHandles.insertAt(handle, i);
150 return status;
151}
152
153size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
154{
155 Mutex::Autolock _l(mLock);
156 size_t size = mHandles.size();
157 size_t i;
158 for (i = 0; i < size; i++) {
159 if (mHandles[i] == handle) {
160 break;
161 }
162 }
163 if (i == size) {
164 return size;
165 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700166 ALOGV("removeHandle() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800167
168 mHandles.removeAt(i);
169 // if removed from first place, move effect control from this handle to next in line
170 if (i == 0) {
171 EffectHandle *h = controlHandle_l();
172 if (h != NULL) {
173 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
174 }
175 }
176
177 // Prevent calls to process() and other functions on effect interface from now on.
178 // The effect engine will be released by the destructor when the last strong reference on
179 // this object is released which can happen after next process is called.
180 if (mHandles.size() == 0 && !mPinned) {
181 mState = DESTROYED;
182 }
183
184 return mHandles.size();
185}
186
187// must be called with EffectModule::mLock held
188AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
189{
190 // the first valid handle in the list has control over the module
191 for (size_t i = 0; i < mHandles.size(); i++) {
192 EffectHandle *h = mHandles[i];
193 if (h != NULL && !h->destroyed_l()) {
194 return h;
195 }
196 }
197
198 return NULL;
199}
200
201size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast)
202{
203 ALOGV("disconnect() %p handle %p", this, handle);
204 // keep a strong reference on this EffectModule to avoid calling the
205 // destructor before we exit
206 sp<EffectModule> keep(this);
207 {
Eric Laurentaaa44472014-09-12 17:41:50 -0700208 if (removeHandle(handle) == 0) {
209 if (!isPinned() || unpinIfLast) {
210 sp<ThreadBase> thread = mThread.promote();
211 if (thread != 0) {
212 Mutex::Autolock _l(thread->mLock);
213 thread->removeEffect_l(this);
214 }
215 sp<AudioFlinger> af = mAudioFlinger.promote();
216 if (af != 0) {
217 af->updateOrphanEffectChains(this);
218 }
219 AudioSystem::unregisterEffect(mId);
220 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800221 }
222 }
223 return mHandles.size();
224}
225
Eric Laurentfa1e1232016-08-02 19:01:49 -0700226bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800227 Mutex::Autolock _l(mLock);
228
Eric Laurentfa1e1232016-08-02 19:01:49 -0700229 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800230 switch (mState) {
231 case RESTART:
232 reset_l();
233 // FALL THROUGH
234
235 case STARTING:
236 // clear auxiliary effect input buffer for next accumulation
237 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
238 memset(mConfig.inputCfg.buffer.raw,
239 0,
240 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
241 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700242 if (start_l() == NO_ERROR) {
243 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700244 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700245 } else {
246 mState = IDLE;
247 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800248 break;
249 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700250 if (stop_l() == NO_ERROR) {
251 mDisableWaitCnt = mMaxDisableWaitCnt;
252 } else {
253 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
254 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800255 mState = STOPPED;
256 break;
257 case STOPPED:
258 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
259 // turn off sequence.
260 if (--mDisableWaitCnt == 0) {
261 reset_l();
262 mState = IDLE;
263 }
264 break;
265 default: //IDLE , ACTIVE, DESTROYED
266 break;
267 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700268
269 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800270}
271
272void AudioFlinger::EffectModule::process()
273{
274 Mutex::Autolock _l(mLock);
275
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700276 if (mState == DESTROYED || mEffectInterface == 0 ||
Eric Laurentca7cc822012-11-19 14:55:58 -0800277 mConfig.inputCfg.buffer.raw == NULL ||
278 mConfig.outputCfg.buffer.raw == NULL) {
279 return;
280 }
281
282 if (isProcessEnabled()) {
283 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
284 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
285 ditherAndClamp(mConfig.inputCfg.buffer.s32,
286 mConfig.inputCfg.buffer.s32,
287 mConfig.inputCfg.buffer.frameCount/2);
288 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700289 int ret;
290 if (isProcessImplemented()) {
291 // do the actual processing in the effect engine
Eric Laurentdb0fd692016-09-16 10:26:09 -0700292 ret = mEffectInterface->process(&mConfig.inputCfg.buffer, &mConfig.outputCfg.buffer);
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700293 } else {
294 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
295 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
296 int16_t *in = mConfig.inputCfg.buffer.s16;
297 int16_t *out = mConfig.outputCfg.buffer.s16;
Eric Laurentca7cc822012-11-19 14:55:58 -0800298
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700299 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
300 for (size_t i = 0; i < frameCnt; i++) {
301 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
302 }
303 } else {
304 memcpy(mConfig.outputCfg.buffer.raw, mConfig.inputCfg.buffer.raw,
305 frameCnt * sizeof(int16_t));
306 }
307 }
308 ret = -ENODATA;
309 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800310 // force transition to IDLE state when engine is ready
311 if (mState == STOPPED && ret == -ENODATA) {
312 mDisableWaitCnt = 1;
313 }
314
315 // clear auxiliary effect input buffer for next accumulation
316 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
317 memset(mConfig.inputCfg.buffer.raw, 0,
318 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
319 }
320 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
321 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
322 // If an insert effect is idle and input buffer is different from output buffer,
323 // accumulate input onto output
324 sp<EffectChain> chain = mChain.promote();
325 if (chain != 0 && chain->activeTrackCnt() != 0) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700326 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * FCC_2; //always stereo here
Eric Laurentca7cc822012-11-19 14:55:58 -0800327 int16_t *in = mConfig.inputCfg.buffer.s16;
328 int16_t *out = mConfig.outputCfg.buffer.s16;
329 for (size_t i = 0; i < frameCnt; i++) {
330 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
331 }
332 }
333 }
334}
335
336void AudioFlinger::EffectModule::reset_l()
337{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700338 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800339 return;
340 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700341 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800342}
343
344status_t AudioFlinger::EffectModule::configure()
345{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700346 status_t status;
347 sp<ThreadBase> thread;
348 uint32_t size;
349 audio_channel_mask_t channelMask;
350
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700351 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700352 status = NO_INIT;
353 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800354 }
355
Eric Laurentd0ebb532013-04-02 16:41:41 -0700356 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800357 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700358 status = DEAD_OBJECT;
359 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800360 }
361
362 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700363 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700364 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800365
366 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
367 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
Yuuki Yokoyama12ccef72016-08-23 17:11:03 +0900368 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
369 ALOGV("Overriding auxiliary effect input as MONO and output as STEREO");
Eric Laurentca7cc822012-11-19 14:55:58 -0800370 } else {
371 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700372 // TODO: Update this logic when multichannel effects are implemented.
373 // For offloaded tracks consider mono output as stereo for proper effect initialization
374 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
375 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
376 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
377 ALOGV("Overriding effect input and output as STEREO");
378 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800379 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700380
Eric Laurentca7cc822012-11-19 14:55:58 -0800381 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
382 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
383 mConfig.inputCfg.samplingRate = thread->sampleRate();
384 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
385 mConfig.inputCfg.bufferProvider.cookie = NULL;
386 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
387 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
388 mConfig.outputCfg.bufferProvider.cookie = NULL;
389 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
390 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
391 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
392 // Insert effect:
393 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
394 // always overwrites output buffer: input buffer == output buffer
395 // - in other sessions:
396 // last effect in the chain accumulates in output buffer: input buffer != output buffer
397 // other effect: overwrites output buffer: input buffer == output buffer
398 // Auxiliary effect:
399 // accumulates in output buffer: input buffer != output buffer
400 // Therefore: accumulate <=> input buffer != output buffer
401 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
402 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
403 } else {
404 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
405 }
406 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
407 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
408 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
409 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
410
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700411 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800412 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
413
414 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700415 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700416 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
417 sizeof(effect_config_t),
418 &mConfig,
419 &size,
420 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800421 if (status == 0) {
422 status = cmdStatus;
423 }
424
425 if (status == 0 &&
426 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
427 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
428 effect_param_t *p = (effect_param_t *)buf32;
429
430 p->psize = sizeof(uint32_t);
431 p->vsize = sizeof(uint32_t);
432 size = sizeof(int);
433 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
434
435 uint32_t latency = 0;
436 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
437 if (pbt != NULL) {
438 latency = pbt->latency_l();
439 }
440
441 *((int32_t *)p->data + 1)= latency;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700442 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
443 sizeof(effect_param_t) + 8,
444 &buf32,
445 &size,
446 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800447 }
448
449 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
450 (1000 * mConfig.outputCfg.buffer.frameCount);
451
Eric Laurentd0ebb532013-04-02 16:41:41 -0700452exit:
453 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800454 return status;
455}
456
457status_t AudioFlinger::EffectModule::init()
458{
459 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700460 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800461 return NO_INIT;
462 }
463 status_t cmdStatus;
464 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700465 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
466 0,
467 NULL,
468 &size,
469 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800470 if (status == 0) {
471 status = cmdStatus;
472 }
473 return status;
474}
475
Eric Laurent1b928682014-10-02 19:41:47 -0700476void AudioFlinger::EffectModule::addEffectToHal_l()
477{
478 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
479 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
480 sp<ThreadBase> thread = mThread.promote();
481 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700482 sp<StreamHalInterface> stream = thread->stream();
483 if (stream != 0) {
484 status_t result = stream->addEffect(mEffectInterface);
485 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
Eric Laurent1b928682014-10-02 19:41:47 -0700486 }
487 }
488 }
489}
490
Eric Laurentfa1e1232016-08-02 19:01:49 -0700491// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800492status_t AudioFlinger::EffectModule::start()
493{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700494 sp<EffectChain> chain;
495 status_t status;
496 {
497 Mutex::Autolock _l(mLock);
498 status = start_l();
499 if (status == NO_ERROR) {
500 chain = mChain.promote();
501 }
502 }
503 if (chain != 0) {
504 chain->resetVolume_l();
505 }
506 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800507}
508
509status_t AudioFlinger::EffectModule::start_l()
510{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700511 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800512 return NO_INIT;
513 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700514 if (mStatus != NO_ERROR) {
515 return mStatus;
516 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800517 status_t cmdStatus;
518 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700519 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
520 0,
521 NULL,
522 &size,
523 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800524 if (status == 0) {
525 status = cmdStatus;
526 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700527 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700528 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800529 }
530 return status;
531}
532
533status_t AudioFlinger::EffectModule::stop()
534{
535 Mutex::Autolock _l(mLock);
536 return stop_l();
537}
538
539status_t AudioFlinger::EffectModule::stop_l()
540{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700541 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800542 return NO_INIT;
543 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700544 if (mStatus != NO_ERROR) {
545 return mStatus;
546 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800547 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800548 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700549 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
550 0,
551 NULL,
552 &size,
553 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800554 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800555 status = cmdStatus;
556 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800557 if (status == NO_ERROR) {
558 status = remove_effect_from_hal_l();
559 }
560 return status;
561}
562
563status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
564{
565 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
566 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800567 sp<ThreadBase> thread = mThread.promote();
568 if (thread != 0) {
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700569 sp<StreamHalInterface> stream = thread->stream();
570 if (stream != 0) {
571 status_t result = stream->removeEffect(mEffectInterface);
572 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
Eric Laurentca7cc822012-11-19 14:55:58 -0800573 }
574 }
575 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800576 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800577}
578
Andy Hunge4a1d912016-08-17 14:11:13 -0700579// round up delta valid if value and divisor are positive.
580template <typename T>
581static T roundUpDelta(const T &value, const T &divisor) {
582 T remainder = value % divisor;
583 return remainder == 0 ? 0 : divisor - remainder;
584}
585
Eric Laurentca7cc822012-11-19 14:55:58 -0800586status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
587 uint32_t cmdSize,
588 void *pCmdData,
589 uint32_t *replySize,
590 void *pReplyData)
591{
592 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700593 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800594
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700595 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800596 return NO_INIT;
597 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700598 if (mStatus != NO_ERROR) {
599 return mStatus;
600 }
Andy Hung110bc952016-06-20 15:22:52 -0700601 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -0700602 (sizeof(effect_param_t) > cmdSize ||
603 ((effect_param_t *)pCmdData)->psize > cmdSize
604 - sizeof(effect_param_t))) {
605 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -0800606 android_errorWriteLog(0x534e4554, "33003822");
607 return -EINVAL;
608 }
609 if (cmdCode == EFFECT_CMD_GET_PARAM &&
610 (*replySize < sizeof(effect_param_t) ||
611 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
612 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -0700613 return -EINVAL;
614 }
ragoe2759072016-11-22 18:02:48 -0800615 if (cmdCode == EFFECT_CMD_GET_PARAM &&
616 (sizeof(effect_param_t) > *replySize
617 || ((effect_param_t *)pCmdData)->psize > *replySize
618 - sizeof(effect_param_t)
619 || ((effect_param_t *)pCmdData)->vsize > *replySize
620 - sizeof(effect_param_t)
621 - ((effect_param_t *)pCmdData)->psize
622 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
623 *replySize
624 - sizeof(effect_param_t)
625 - ((effect_param_t *)pCmdData)->psize
626 - ((effect_param_t *)pCmdData)->vsize)) {
627 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
628 android_errorWriteLog(0x534e4554, "32705438");
629 return -EINVAL;
630 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700631 if ((cmdCode == EFFECT_CMD_SET_PARAM
632 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
633 (sizeof(effect_param_t) > cmdSize
634 || ((effect_param_t *)pCmdData)->psize > cmdSize
635 - sizeof(effect_param_t)
636 || ((effect_param_t *)pCmdData)->vsize > cmdSize
637 - sizeof(effect_param_t)
638 - ((effect_param_t *)pCmdData)->psize
639 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
640 cmdSize
641 - sizeof(effect_param_t)
642 - ((effect_param_t *)pCmdData)->psize
643 - ((effect_param_t *)pCmdData)->vsize)) {
644 android_errorWriteLog(0x534e4554, "30204301");
645 return -EINVAL;
646 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700647 status_t status = mEffectInterface->command(cmdCode,
648 cmdSize,
649 pCmdData,
650 replySize,
651 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800652 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
653 uint32_t size = (replySize == NULL) ? 0 : *replySize;
654 for (size_t i = 1; i < mHandles.size(); i++) {
655 EffectHandle *h = mHandles[i];
656 if (h != NULL && !h->destroyed_l()) {
657 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
658 }
659 }
660 }
661 return status;
662}
663
664status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
665{
666 Mutex::Autolock _l(mLock);
667 return setEnabled_l(enabled);
668}
669
670// must be called with EffectModule::mLock held
671status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
672{
673
674 ALOGV("setEnabled %p enabled %d", this, enabled);
675
676 if (enabled != isEnabled()) {
677 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
678 if (enabled && status != NO_ERROR) {
679 return status;
680 }
681
682 switch (mState) {
683 // going from disabled to enabled
684 case IDLE:
685 mState = STARTING;
686 break;
687 case STOPPED:
688 mState = RESTART;
689 break;
690 case STOPPING:
691 mState = ACTIVE;
692 break;
693
694 // going from enabled to disabled
695 case RESTART:
696 mState = STOPPED;
697 break;
698 case STARTING:
699 mState = IDLE;
700 break;
701 case ACTIVE:
702 mState = STOPPING;
703 break;
704 case DESTROYED:
705 return NO_ERROR; // simply ignore as we are being destroyed
706 }
707 for (size_t i = 1; i < mHandles.size(); i++) {
708 EffectHandle *h = mHandles[i];
709 if (h != NULL && !h->destroyed_l()) {
710 h->setEnabled(enabled);
711 }
712 }
713 }
714 return NO_ERROR;
715}
716
717bool AudioFlinger::EffectModule::isEnabled() const
718{
719 switch (mState) {
720 case RESTART:
721 case STARTING:
722 case ACTIVE:
723 return true;
724 case IDLE:
725 case STOPPING:
726 case STOPPED:
727 case DESTROYED:
728 default:
729 return false;
730 }
731}
732
733bool AudioFlinger::EffectModule::isProcessEnabled() const
734{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700735 if (mStatus != NO_ERROR) {
736 return false;
737 }
738
Eric Laurentca7cc822012-11-19 14:55:58 -0800739 switch (mState) {
740 case RESTART:
741 case ACTIVE:
742 case STOPPING:
743 case STOPPED:
744 return true;
745 case IDLE:
746 case STARTING:
747 case DESTROYED:
748 default:
749 return false;
750 }
751}
752
753status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
754{
755 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700756 if (mStatus != NO_ERROR) {
757 return mStatus;
758 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800759 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800760 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
761 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
762 if (isProcessEnabled() &&
763 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
764 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800765 uint32_t volume[2];
766 uint32_t *pVolume = NULL;
767 uint32_t size = sizeof(volume);
768 volume[0] = *left;
769 volume[1] = *right;
770 if (controller) {
771 pVolume = volume;
772 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700773 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
774 size,
775 volume,
776 &size,
777 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -0800778 if (controller && status == NO_ERROR && size == sizeof(volume)) {
779 *left = volume[0];
780 *right = volume[1];
781 }
782 }
783 return status;
784}
785
786status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
787{
788 if (device == AUDIO_DEVICE_NONE) {
789 return NO_ERROR;
790 }
791
792 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700793 if (mStatus != NO_ERROR) {
794 return mStatus;
795 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800796 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700797 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800798 status_t cmdStatus;
799 uint32_t size = sizeof(status_t);
800 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
801 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700802 status = mEffectInterface->command(cmd,
803 sizeof(uint32_t),
804 &device,
805 &size,
806 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800807 }
808 return status;
809}
810
811status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
812{
813 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700814 if (mStatus != NO_ERROR) {
815 return mStatus;
816 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800817 status_t status = NO_ERROR;
818 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
819 status_t cmdStatus;
820 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700821 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
822 sizeof(audio_mode_t),
823 &mode,
824 &size,
825 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800826 if (status == NO_ERROR) {
827 status = cmdStatus;
828 }
829 }
830 return status;
831}
832
833status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
834{
835 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700836 if (mStatus != NO_ERROR) {
837 return mStatus;
838 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 status_t status = NO_ERROR;
840 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
841 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700842 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
843 sizeof(audio_source_t),
844 &source,
845 &size,
846 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800847 }
848 return status;
849}
850
851void AudioFlinger::EffectModule::setSuspended(bool suspended)
852{
853 Mutex::Autolock _l(mLock);
854 mSuspended = suspended;
855}
856
857bool AudioFlinger::EffectModule::suspended() const
858{
859 Mutex::Autolock _l(mLock);
860 return mSuspended;
861}
862
863bool AudioFlinger::EffectModule::purgeHandles()
864{
865 bool enabled = false;
866 Mutex::Autolock _l(mLock);
867 for (size_t i = 0; i < mHandles.size(); i++) {
868 EffectHandle *handle = mHandles[i];
869 if (handle != NULL && !handle->destroyed_l()) {
870 handle->effect().clear();
871 if (handle->hasControl()) {
872 enabled = handle->enabled();
873 }
874 }
875 }
876 return enabled;
877}
878
Eric Laurent5baf2af2013-09-12 17:37:00 -0700879status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
880{
881 Mutex::Autolock _l(mLock);
882 if (mStatus != NO_ERROR) {
883 return mStatus;
884 }
885 status_t status = NO_ERROR;
886 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
887 status_t cmdStatus;
888 uint32_t size = sizeof(status_t);
889 effect_offload_param_t cmd;
890
891 cmd.isOffload = offloaded;
892 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700893 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
894 sizeof(effect_offload_param_t),
895 &cmd,
896 &size,
897 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700898 if (status == NO_ERROR) {
899 status = cmdStatus;
900 }
901 mOffloaded = (status == NO_ERROR) ? offloaded : false;
902 } else {
903 if (offloaded) {
904 status = INVALID_OPERATION;
905 }
906 mOffloaded = false;
907 }
908 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
909 return status;
910}
911
912bool AudioFlinger::EffectModule::isOffloaded() const
913{
914 Mutex::Autolock _l(mLock);
915 return mOffloaded;
916}
917
Marco Nelissenb2208842014-02-07 14:00:50 -0800918String8 effectFlagsToString(uint32_t flags) {
919 String8 s;
920
921 s.append("conn. mode: ");
922 switch (flags & EFFECT_FLAG_TYPE_MASK) {
923 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
924 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
925 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
926 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
927 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
928 default: s.append("unknown/reserved"); break;
929 }
930 s.append(", ");
931
932 s.append("insert pref: ");
933 switch (flags & EFFECT_FLAG_INSERT_MASK) {
934 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
935 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
936 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
937 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
938 default: s.append("unknown/reserved"); break;
939 }
940 s.append(", ");
941
942 s.append("volume mgmt: ");
943 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
944 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
945 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
946 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
947 default: s.append("unknown/reserved"); break;
948 }
949 s.append(", ");
950
951 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
952 if (devind) {
953 s.append("device indication: ");
954 switch (devind) {
955 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
956 default: s.append("unknown/reserved"); break;
957 }
958 s.append(", ");
959 }
960
961 s.append("input mode: ");
962 switch (flags & EFFECT_FLAG_INPUT_MASK) {
963 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
964 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
965 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
966 default: s.append("not set"); break;
967 }
968 s.append(", ");
969
970 s.append("output mode: ");
971 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
972 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
973 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
974 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
975 default: s.append("not set"); break;
976 }
977 s.append(", ");
978
979 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
980 if (accel) {
981 s.append("hardware acceleration: ");
982 switch (accel) {
983 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
984 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
985 default: s.append("unknown/reserved"); break;
986 }
987 s.append(", ");
988 }
989
990 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
991 if (modeind) {
992 s.append("mode indication: ");
993 switch (modeind) {
994 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
995 default: s.append("unknown/reserved"); break;
996 }
997 s.append(", ");
998 }
999
1000 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
1001 if (srcind) {
1002 s.append("source indication: ");
1003 switch (srcind) {
1004 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
1005 default: s.append("unknown/reserved"); break;
1006 }
1007 s.append(", ");
1008 }
1009
1010 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
1011 s.append("offloadable, ");
1012 }
1013
1014 int len = s.length();
1015 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -07001016 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -08001017 s.unlockBuffer(len - 2);
1018 }
1019 return s;
1020}
1021
1022
Glenn Kasten0f11b512014-01-31 16:18:54 -08001023void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -08001024{
1025 const size_t SIZE = 256;
1026 char buffer[SIZE];
1027 String8 result;
1028
1029 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
1030 result.append(buffer);
1031
1032 bool locked = AudioFlinger::dumpTryLock(mLock);
1033 // failed to lock - AudioFlinger is probably deadlocked
1034 if (!locked) {
1035 result.append("\t\tCould not lock Fx mutex:\n");
1036 }
1037
1038 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001039 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001040 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001041 result.append(buffer);
1042
1043 result.append("\t\tDescriptor:\n");
1044 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1045 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1046 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1047 mDescriptor.uuid.node[2],
1048 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1049 result.append(buffer);
1050 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1051 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1052 mDescriptor.type.timeHiAndVersion,
1053 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1054 mDescriptor.type.node[2],
1055 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1056 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001057 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001058 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001059 mDescriptor.flags,
1060 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001061 result.append(buffer);
1062 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1063 mDescriptor.name);
1064 result.append(buffer);
1065 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1066 mDescriptor.implementor);
1067 result.append(buffer);
1068
1069 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001070 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001071 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001072 mConfig.inputCfg.buffer.frameCount,
1073 mConfig.inputCfg.samplingRate,
1074 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001075 mConfig.inputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001076 formatToString((audio_format_t)mConfig.inputCfg.format).c_str(),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001077 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001078 result.append(buffer);
1079
1080 result.append("\t\t- Output configuration:\n");
1081 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001082 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001083 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001084 mConfig.outputCfg.buffer.frameCount,
1085 mConfig.outputCfg.samplingRate,
1086 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001087 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001088 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001089 result.append(buffer);
1090
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001091 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001092 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001093 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001094 for (size_t i = 0; i < mHandles.size(); ++i) {
1095 EffectHandle *handle = mHandles[i];
1096 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001097 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001098 result.append(buffer);
1099 }
1100 }
1101
Eric Laurentca7cc822012-11-19 14:55:58 -08001102 write(fd, result.string(), result.length());
1103
1104 if (locked) {
1105 mLock.unlock();
1106 }
1107}
1108
1109// ----------------------------------------------------------------------------
1110// EffectHandle implementation
1111// ----------------------------------------------------------------------------
1112
1113#undef LOG_TAG
1114#define LOG_TAG "AudioFlinger::EffectHandle"
1115
1116AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1117 const sp<AudioFlinger::Client>& client,
1118 const sp<IEffectClient>& effectClient,
1119 int32_t priority)
1120 : BnEffect(),
1121 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1122 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1123{
1124 ALOGV("constructor %p", this);
1125
1126 if (client == 0) {
1127 return;
1128 }
1129 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1130 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001131 if (mCblkMemory == 0 ||
1132 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001133 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001134 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001135 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001136 return;
1137 }
Glenn Kastene75da402013-11-20 13:54:52 -08001138 new(mCblk) effect_param_cblk_t();
1139 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001140}
1141
1142AudioFlinger::EffectHandle::~EffectHandle()
1143{
1144 ALOGV("Destructor %p", this);
1145
1146 if (mEffect == 0) {
1147 mDestroyed = true;
1148 return;
1149 }
1150 mEffect->lock();
1151 mDestroyed = true;
1152 mEffect->unlock();
1153 disconnect(false);
1154}
1155
Glenn Kastene75da402013-11-20 13:54:52 -08001156status_t AudioFlinger::EffectHandle::initCheck()
1157{
1158 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1159}
1160
Eric Laurentca7cc822012-11-19 14:55:58 -08001161status_t AudioFlinger::EffectHandle::enable()
1162{
1163 ALOGV("enable %p", this);
1164 if (!mHasControl) {
1165 return INVALID_OPERATION;
1166 }
1167 if (mEffect == 0) {
1168 return DEAD_OBJECT;
1169 }
1170
1171 if (mEnabled) {
1172 return NO_ERROR;
1173 }
1174
1175 mEnabled = true;
1176
1177 sp<ThreadBase> thread = mEffect->thread().promote();
1178 if (thread != 0) {
1179 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1180 }
1181
1182 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1183 if (mEffect->suspended()) {
1184 return NO_ERROR;
1185 }
1186
1187 status_t status = mEffect->setEnabled(true);
1188 if (status != NO_ERROR) {
1189 if (thread != 0) {
1190 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1191 }
1192 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001193 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001194 if (thread != 0) {
1195 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001196 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001197 Mutex::Autolock _l(t->mLock);
1198 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001199 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001200 if (!mEffect->isOffloadable()) {
1201 if (thread->type() == ThreadBase::OFFLOAD) {
1202 PlaybackThread *t = (PlaybackThread *)thread.get();
1203 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1204 }
1205 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1206 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1207 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001208 }
1209 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001210 }
1211 return status;
1212}
1213
1214status_t AudioFlinger::EffectHandle::disable()
1215{
1216 ALOGV("disable %p", this);
1217 if (!mHasControl) {
1218 return INVALID_OPERATION;
1219 }
1220 if (mEffect == 0) {
1221 return DEAD_OBJECT;
1222 }
1223
1224 if (!mEnabled) {
1225 return NO_ERROR;
1226 }
1227 mEnabled = false;
1228
1229 if (mEffect->suspended()) {
1230 return NO_ERROR;
1231 }
1232
1233 status_t status = mEffect->setEnabled(false);
1234
1235 sp<ThreadBase> thread = mEffect->thread().promote();
1236 if (thread != 0) {
1237 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001238 if (thread->type() == ThreadBase::OFFLOAD) {
1239 PlaybackThread *t = (PlaybackThread *)thread.get();
1240 Mutex::Autolock _l(t->mLock);
1241 t->broadcast_l();
1242 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001243 }
1244
1245 return status;
1246}
1247
1248void AudioFlinger::EffectHandle::disconnect()
1249{
1250 disconnect(true);
1251}
1252
1253void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1254{
1255 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1256 if (mEffect == 0) {
1257 return;
1258 }
1259 // restore suspended effects if the disconnected handle was enabled and the last one.
1260 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1261 sp<ThreadBase> thread = mEffect->thread().promote();
1262 if (thread != 0) {
1263 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1264 }
1265 }
1266
1267 // release sp on module => module destructor can be called now
1268 mEffect.clear();
1269 if (mClient != 0) {
1270 if (mCblk != NULL) {
1271 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1272 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1273 }
1274 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001275 // Client destructor must run with AudioFlinger client mutex locked
1276 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001277 mClient.clear();
1278 }
1279}
1280
1281status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1282 uint32_t cmdSize,
1283 void *pCmdData,
1284 uint32_t *replySize,
1285 void *pReplyData)
1286{
1287 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1288 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1289
1290 // only get parameter command is permitted for applications not controlling the effect
1291 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1292 return INVALID_OPERATION;
1293 }
1294 if (mEffect == 0) {
1295 return DEAD_OBJECT;
1296 }
1297 if (mClient == 0) {
1298 return INVALID_OPERATION;
1299 }
1300
1301 // handle commands that are not forwarded transparently to effect engine
1302 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1303 // No need to trylock() here as this function is executed in the binder thread serving a
1304 // particular client process: no risk to block the whole media server process or mixer
1305 // threads if we are stuck here
1306 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001307
1308 // keep local copy of index in case of client corruption b/32220769
1309 const uint32_t clientIndex = mCblk->clientIndex;
1310 const uint32_t serverIndex = mCblk->serverIndex;
1311 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1312 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001313 mCblk->serverIndex = 0;
1314 mCblk->clientIndex = 0;
1315 return BAD_VALUE;
1316 }
1317 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001318 effect_param_t *param = NULL;
1319 for (uint32_t index = serverIndex; index < clientIndex;) {
1320 int *p = (int *)(mBuffer + index);
1321 const int size = *p++;
1322 if (size < 0
1323 || size > EFFECT_PARAM_BUFFER_SIZE
1324 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001325 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001326 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001327 break;
1328 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001329
1330 // copy to local memory in case of client corruption b/32220769
1331 param = (effect_param_t *)realloc(param, size);
1332 if (param == NULL) {
1333 ALOGW("command(): out of memory");
1334 status = NO_MEMORY;
1335 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001336 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001337 memcpy(param, p, size);
1338
1339 int reply = 0;
1340 uint32_t rsize = sizeof(reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001341 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001342 size,
1343 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001344 &rsize,
1345 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001346
1347 // verify shared memory: server index shouldn't change; client index can't go back.
1348 if (serverIndex != mCblk->serverIndex
1349 || clientIndex > mCblk->clientIndex) {
1350 android_errorWriteLog(0x534e4554, "32220769");
1351 status = BAD_VALUE;
1352 break;
1353 }
1354
Eric Laurentca7cc822012-11-19 14:55:58 -08001355 // stop at first error encountered
1356 if (ret != NO_ERROR) {
1357 status = ret;
1358 *(int *)pReplyData = reply;
1359 break;
1360 } else if (reply != NO_ERROR) {
1361 *(int *)pReplyData = reply;
1362 break;
1363 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001364 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001365 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001366 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001367 mCblk->serverIndex = 0;
1368 mCblk->clientIndex = 0;
1369 return status;
1370 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1371 *(int *)pReplyData = NO_ERROR;
1372 return enable();
1373 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1374 *(int *)pReplyData = NO_ERROR;
1375 return disable();
1376 }
1377
1378 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1379}
1380
1381void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1382{
1383 ALOGV("setControl %p control %d", this, hasControl);
1384
1385 mHasControl = hasControl;
1386 mEnabled = enabled;
1387
1388 if (signal && mEffectClient != 0) {
1389 mEffectClient->controlStatusChanged(hasControl);
1390 }
1391}
1392
1393void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1394 uint32_t cmdSize,
1395 void *pCmdData,
1396 uint32_t replySize,
1397 void *pReplyData)
1398{
1399 if (mEffectClient != 0) {
1400 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1401 }
1402}
1403
1404
1405
1406void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1407{
1408 if (mEffectClient != 0) {
1409 mEffectClient->enableStatusChanged(enabled);
1410 }
1411}
1412
1413status_t AudioFlinger::EffectHandle::onTransact(
1414 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1415{
1416 return BnEffect::onTransact(code, data, reply, flags);
1417}
1418
1419
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001420void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001421{
1422 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1423
Marco Nelissenb2208842014-02-07 14:00:50 -08001424 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 (mClient == 0) ? getpid_cached : mClient->pid(),
1426 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001427 mHasControl ? "yes" : "no",
1428 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001429 mCblk ? mCblk->clientIndex : 0,
1430 mCblk ? mCblk->serverIndex : 0
1431 );
1432
1433 if (locked) {
1434 mCblk->lock.unlock();
1435 }
1436}
1437
1438#undef LOG_TAG
1439#define LOG_TAG "AudioFlinger::EffectChain"
1440
1441AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001442 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001443 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1444 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001445 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001446{
1447 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1448 if (thread == NULL) {
1449 return;
1450 }
1451 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1452 thread->frameCount();
1453}
1454
1455AudioFlinger::EffectChain::~EffectChain()
1456{
1457 if (mOwnInBuffer) {
1458 delete mInBuffer;
1459 }
1460
1461}
1462
1463// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1464sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1465 effect_descriptor_t *descriptor)
1466{
1467 size_t size = mEffects.size();
1468
1469 for (size_t i = 0; i < size; i++) {
1470 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1471 return mEffects[i];
1472 }
1473 }
1474 return 0;
1475}
1476
1477// getEffectFromId_l() must be called with ThreadBase::mLock held
1478sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1479{
1480 size_t size = mEffects.size();
1481
1482 for (size_t i = 0; i < size; i++) {
1483 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1484 if (id == 0 || mEffects[i]->id() == id) {
1485 return mEffects[i];
1486 }
1487 }
1488 return 0;
1489}
1490
1491// getEffectFromType_l() must be called with ThreadBase::mLock held
1492sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1493 const effect_uuid_t *type)
1494{
1495 size_t size = mEffects.size();
1496
1497 for (size_t i = 0; i < size; i++) {
1498 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1499 return mEffects[i];
1500 }
1501 }
1502 return 0;
1503}
1504
1505void AudioFlinger::EffectChain::clearInputBuffer()
1506{
1507 Mutex::Autolock _l(mLock);
1508 sp<ThreadBase> thread = mThread.promote();
1509 if (thread == 0) {
1510 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1511 return;
1512 }
1513 clearInputBuffer_l(thread);
1514}
1515
1516// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001517void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001518{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001519 // TODO: This will change in the future, depending on multichannel
1520 // and sample format changes for effects.
1521 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1522 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001523 const size_t frameSize =
1524 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001525 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001526}
1527
1528// Must be called with EffectChain::mLock locked
1529void AudioFlinger::EffectChain::process_l()
1530{
1531 sp<ThreadBase> thread = mThread.promote();
1532 if (thread == 0) {
1533 ALOGW("process_l(): cannot promote mixer thread");
1534 return;
1535 }
1536 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1537 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001538 // never process effects when:
1539 // - on an OFFLOAD thread
1540 // - no more tracks are on the session and the effect tail has been rendered
1541 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001542 if (!isGlobalSession) {
1543 bool tracksOnSession = (trackCnt() != 0);
1544
1545 if (!tracksOnSession && mTailBufferCount == 0) {
1546 doProcess = false;
1547 }
1548
1549 if (activeTrackCnt() == 0) {
1550 // if no track is active and the effect tail has not been rendered,
1551 // the input buffer must be cleared here as the mixer process will not do it
1552 if (tracksOnSession || mTailBufferCount > 0) {
1553 clearInputBuffer_l(thread);
1554 if (mTailBufferCount > 0) {
1555 mTailBufferCount--;
1556 }
1557 }
1558 }
1559 }
1560
1561 size_t size = mEffects.size();
1562 if (doProcess) {
1563 for (size_t i = 0; i < size; i++) {
1564 mEffects[i]->process();
1565 }
1566 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001567 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001568 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001569 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1570 }
1571 if (doResetVolume) {
1572 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001573 }
1574}
1575
1576// addEffect_l() must be called with PlaybackThread::mLock held
1577status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1578{
1579 effect_descriptor_t desc = effect->desc();
1580 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1581
1582 Mutex::Autolock _l(mLock);
1583 effect->setChain(this);
1584 sp<ThreadBase> thread = mThread.promote();
1585 if (thread == 0) {
1586 return NO_INIT;
1587 }
1588 effect->setThread(thread);
1589
1590 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1591 // Auxiliary effects are inserted at the beginning of mEffects vector as
1592 // they are processed first and accumulated in chain input buffer
1593 mEffects.insertAt(effect, 0);
1594
1595 // the input buffer for auxiliary effect contains mono samples in
1596 // 32 bit format. This is to avoid saturation in AudoMixer
1597 // accumulation stage. Saturation is done in EffectModule::process() before
1598 // calling the process in effect engine
1599 size_t numSamples = thread->frameCount();
1600 int32_t *buffer = new int32_t[numSamples];
1601 memset(buffer, 0, numSamples * sizeof(int32_t));
1602 effect->setInBuffer((int16_t *)buffer);
1603 // auxiliary effects output samples to chain input buffer for further processing
1604 // by insert effects
1605 effect->setOutBuffer(mInBuffer);
1606 } else {
1607 // Insert effects are inserted at the end of mEffects vector as they are processed
1608 // after track and auxiliary effects.
1609 // Insert effect order as a function of indicated preference:
1610 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1611 // another effect is present
1612 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1613 // last effect claiming first position
1614 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1615 // first effect claiming last position
1616 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1617 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1618 // already present
1619
1620 size_t size = mEffects.size();
1621 size_t idx_insert = size;
1622 ssize_t idx_insert_first = -1;
1623 ssize_t idx_insert_last = -1;
1624
1625 for (size_t i = 0; i < size; i++) {
1626 effect_descriptor_t d = mEffects[i]->desc();
1627 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1628 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1629 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1630 // check invalid effect chaining combinations
1631 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1632 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1633 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1634 desc.name, d.name);
1635 return INVALID_OPERATION;
1636 }
1637 // remember position of first insert effect and by default
1638 // select this as insert position for new effect
1639 if (idx_insert == size) {
1640 idx_insert = i;
1641 }
1642 // remember position of last insert effect claiming
1643 // first position
1644 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1645 idx_insert_first = i;
1646 }
1647 // remember position of first insert effect claiming
1648 // last position
1649 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1650 idx_insert_last == -1) {
1651 idx_insert_last = i;
1652 }
1653 }
1654 }
1655
1656 // modify idx_insert from first position if needed
1657 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1658 if (idx_insert_last != -1) {
1659 idx_insert = idx_insert_last;
1660 } else {
1661 idx_insert = size;
1662 }
1663 } else {
1664 if (idx_insert_first != -1) {
1665 idx_insert = idx_insert_first + 1;
1666 }
1667 }
1668
1669 // always read samples from chain input buffer
1670 effect->setInBuffer(mInBuffer);
1671
1672 // if last effect in the chain, output samples to chain
1673 // output buffer, otherwise to chain input buffer
1674 if (idx_insert == size) {
1675 if (idx_insert != 0) {
1676 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1677 mEffects[idx_insert-1]->configure();
1678 }
1679 effect->setOutBuffer(mOutBuffer);
1680 } else {
1681 effect->setOutBuffer(mInBuffer);
1682 }
1683 mEffects.insertAt(effect, idx_insert);
1684
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001685 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001686 idx_insert);
1687 }
1688 effect->configure();
1689 return NO_ERROR;
1690}
1691
1692// removeEffect_l() must be called with PlaybackThread::mLock held
1693size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1694{
1695 Mutex::Autolock _l(mLock);
1696 size_t size = mEffects.size();
1697 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1698
1699 for (size_t i = 0; i < size; i++) {
1700 if (effect == mEffects[i]) {
1701 // calling stop here will remove pre-processing effect from the audio HAL.
1702 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1703 // the middle of a read from audio HAL
1704 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1705 mEffects[i]->state() == EffectModule::STOPPING) {
1706 mEffects[i]->stop();
1707 }
1708 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1709 delete[] effect->inBuffer();
1710 } else {
1711 if (i == size - 1 && i != 0) {
1712 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1713 mEffects[i - 1]->configure();
1714 }
1715 }
1716 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001717 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001718 this, i);
1719 break;
1720 }
1721 }
1722
1723 return mEffects.size();
1724}
1725
1726// setDevice_l() must be called with PlaybackThread::mLock held
1727void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1728{
1729 size_t size = mEffects.size();
1730 for (size_t i = 0; i < size; i++) {
1731 mEffects[i]->setDevice(device);
1732 }
1733}
1734
1735// setMode_l() must be called with PlaybackThread::mLock held
1736void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1737{
1738 size_t size = mEffects.size();
1739 for (size_t i = 0; i < size; i++) {
1740 mEffects[i]->setMode(mode);
1741 }
1742}
1743
1744// setAudioSource_l() must be called with PlaybackThread::mLock held
1745void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1746{
1747 size_t size = mEffects.size();
1748 for (size_t i = 0; i < size; i++) {
1749 mEffects[i]->setAudioSource(source);
1750 }
1751}
1752
Eric Laurentfa1e1232016-08-02 19:01:49 -07001753// setVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1754bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001755{
1756 uint32_t newLeft = *left;
1757 uint32_t newRight = *right;
1758 bool hasControl = false;
1759 int ctrlIdx = -1;
1760 size_t size = mEffects.size();
1761
1762 // first update volume controller
1763 for (size_t i = size; i > 0; i--) {
1764 if (mEffects[i - 1]->isProcessEnabled() &&
1765 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1766 ctrlIdx = i - 1;
1767 hasControl = true;
1768 break;
1769 }
1770 }
1771
Eric Laurentfa1e1232016-08-02 19:01:49 -07001772 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001773 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001774 if (hasControl) {
1775 *left = mNewLeftVolume;
1776 *right = mNewRightVolume;
1777 }
1778 return hasControl;
1779 }
1780
1781 mVolumeCtrlIdx = ctrlIdx;
1782 mLeftVolume = newLeft;
1783 mRightVolume = newRight;
1784
1785 // second get volume update from volume controller
1786 if (ctrlIdx >= 0) {
1787 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1788 mNewLeftVolume = newLeft;
1789 mNewRightVolume = newRight;
1790 }
1791 // then indicate volume to all other effects in chain.
1792 // Pass altered volume to effects before volume controller
1793 // and requested volume to effects after controller
1794 uint32_t lVol = newLeft;
1795 uint32_t rVol = newRight;
1796
1797 for (size_t i = 0; i < size; i++) {
1798 if ((int)i == ctrlIdx) {
1799 continue;
1800 }
1801 // this also works for ctrlIdx == -1 when there is no volume controller
1802 if ((int)i > ctrlIdx) {
1803 lVol = *left;
1804 rVol = *right;
1805 }
1806 mEffects[i]->setVolume(&lVol, &rVol, false);
1807 }
1808 *left = newLeft;
1809 *right = newRight;
1810
1811 return hasControl;
1812}
1813
Eric Laurentfa1e1232016-08-02 19:01:49 -07001814// resetVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1815void AudioFlinger::EffectChain::resetVolume_l()
1816{
Eric Laurente7449bf2016-08-03 18:44:07 -07001817 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1818 uint32_t left = mLeftVolume;
1819 uint32_t right = mRightVolume;
1820 (void)setVolume_l(&left, &right, true);
1821 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001822}
1823
Eric Laurent1b928682014-10-02 19:41:47 -07001824void AudioFlinger::EffectChain::syncHalEffectsState()
1825{
1826 Mutex::Autolock _l(mLock);
1827 for (size_t i = 0; i < mEffects.size(); i++) {
1828 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1829 mEffects[i]->state() == EffectModule::STOPPING) {
1830 mEffects[i]->addEffectToHal_l();
1831 }
1832 }
1833}
1834
Eric Laurentca7cc822012-11-19 14:55:58 -08001835void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1836{
1837 const size_t SIZE = 256;
1838 char buffer[SIZE];
1839 String8 result;
1840
Marco Nelissenb2208842014-02-07 14:00:50 -08001841 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001842 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001843 result.append(buffer);
1844
Marco Nelissenb2208842014-02-07 14:00:50 -08001845 if (numEffects) {
1846 bool locked = AudioFlinger::dumpTryLock(mLock);
1847 // failed to lock - AudioFlinger is probably deadlocked
1848 if (!locked) {
1849 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001850 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001851
Marco Nelissenb2208842014-02-07 14:00:50 -08001852 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001853 snprintf(buffer, SIZE, "\t%p %p %d\n",
1854 mInBuffer,
1855 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001856 mActiveTrackCnt);
1857 result.append(buffer);
1858 write(fd, result.string(), result.size());
1859
1860 for (size_t i = 0; i < numEffects; ++i) {
1861 sp<EffectModule> effect = mEffects[i];
1862 if (effect != 0) {
1863 effect->dump(fd, args);
1864 }
1865 }
1866
1867 if (locked) {
1868 mLock.unlock();
1869 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001870 }
1871}
1872
1873// must be called with ThreadBase::mLock held
1874void AudioFlinger::EffectChain::setEffectSuspended_l(
1875 const effect_uuid_t *type, bool suspend)
1876{
1877 sp<SuspendedEffectDesc> desc;
1878 // use effect type UUID timelow as key as there is no real risk of identical
1879 // timeLow fields among effect type UUIDs.
1880 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1881 if (suspend) {
1882 if (index >= 0) {
1883 desc = mSuspendedEffects.valueAt(index);
1884 } else {
1885 desc = new SuspendedEffectDesc();
1886 desc->mType = *type;
1887 mSuspendedEffects.add(type->timeLow, desc);
1888 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1889 }
1890 if (desc->mRefCount++ == 0) {
1891 sp<EffectModule> effect = getEffectIfEnabled(type);
1892 if (effect != 0) {
1893 desc->mEffect = effect;
1894 effect->setSuspended(true);
1895 effect->setEnabled(false);
1896 }
1897 }
1898 } else {
1899 if (index < 0) {
1900 return;
1901 }
1902 desc = mSuspendedEffects.valueAt(index);
1903 if (desc->mRefCount <= 0) {
1904 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1905 desc->mRefCount = 1;
1906 }
1907 if (--desc->mRefCount == 0) {
1908 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1909 if (desc->mEffect != 0) {
1910 sp<EffectModule> effect = desc->mEffect.promote();
1911 if (effect != 0) {
1912 effect->setSuspended(false);
1913 effect->lock();
1914 EffectHandle *handle = effect->controlHandle_l();
1915 if (handle != NULL && !handle->destroyed_l()) {
1916 effect->setEnabled_l(handle->enabled());
1917 }
1918 effect->unlock();
1919 }
1920 desc->mEffect.clear();
1921 }
1922 mSuspendedEffects.removeItemsAt(index);
1923 }
1924 }
1925}
1926
1927// must be called with ThreadBase::mLock held
1928void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1929{
1930 sp<SuspendedEffectDesc> desc;
1931
1932 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1933 if (suspend) {
1934 if (index >= 0) {
1935 desc = mSuspendedEffects.valueAt(index);
1936 } else {
1937 desc = new SuspendedEffectDesc();
1938 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1939 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1940 }
1941 if (desc->mRefCount++ == 0) {
1942 Vector< sp<EffectModule> > effects;
1943 getSuspendEligibleEffects(effects);
1944 for (size_t i = 0; i < effects.size(); i++) {
1945 setEffectSuspended_l(&effects[i]->desc().type, true);
1946 }
1947 }
1948 } else {
1949 if (index < 0) {
1950 return;
1951 }
1952 desc = mSuspendedEffects.valueAt(index);
1953 if (desc->mRefCount <= 0) {
1954 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1955 desc->mRefCount = 1;
1956 }
1957 if (--desc->mRefCount == 0) {
1958 Vector<const effect_uuid_t *> types;
1959 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1960 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1961 continue;
1962 }
1963 types.add(&mSuspendedEffects.valueAt(i)->mType);
1964 }
1965 for (size_t i = 0; i < types.size(); i++) {
1966 setEffectSuspended_l(types[i], false);
1967 }
1968 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1969 mSuspendedEffects.keyAt(index));
1970 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1971 }
1972 }
1973}
1974
1975
1976// The volume effect is used for automated tests only
1977#ifndef OPENSL_ES_H_
1978static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1979 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1980const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1981#endif //OPENSL_ES_H_
1982
1983bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1984{
1985 // auxiliary effects and visualizer are never suspended on output mix
1986 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1987 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1988 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1989 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1990 return false;
1991 }
1992 return true;
1993}
1994
1995void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1996 Vector< sp<AudioFlinger::EffectModule> > &effects)
1997{
1998 effects.clear();
1999 for (size_t i = 0; i < mEffects.size(); i++) {
2000 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2001 effects.add(mEffects[i]);
2002 }
2003 }
2004}
2005
2006sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2007 const effect_uuid_t *type)
2008{
2009 sp<EffectModule> effect = getEffectFromType_l(type);
2010 return effect != 0 && effect->isEnabled() ? effect : 0;
2011}
2012
2013void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2014 bool enabled)
2015{
2016 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2017 if (enabled) {
2018 if (index < 0) {
2019 // if the effect is not suspend check if all effects are suspended
2020 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2021 if (index < 0) {
2022 return;
2023 }
2024 if (!isEffectEligibleForSuspend(effect->desc())) {
2025 return;
2026 }
2027 setEffectSuspended_l(&effect->desc().type, enabled);
2028 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2029 if (index < 0) {
2030 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2031 return;
2032 }
2033 }
2034 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2035 effect->desc().type.timeLow);
2036 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2037 // if effect is requested to suspended but was not yet enabled, supend it now.
2038 if (desc->mEffect == 0) {
2039 desc->mEffect = effect;
2040 effect->setEnabled(false);
2041 effect->setSuspended(true);
2042 }
2043 } else {
2044 if (index < 0) {
2045 return;
2046 }
2047 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2048 effect->desc().type.timeLow);
2049 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2050 desc->mEffect.clear();
2051 effect->setSuspended(false);
2052 }
2053}
2054
Eric Laurent5baf2af2013-09-12 17:37:00 -07002055bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002056{
2057 Mutex::Autolock _l(mLock);
2058 size_t size = mEffects.size();
2059 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002060 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002061 return true;
2062 }
2063 }
2064 return false;
2065}
2066
Eric Laurentaaa44472014-09-12 17:41:50 -07002067void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2068{
2069 Mutex::Autolock _l(mLock);
2070 mThread = thread;
2071 for (size_t i = 0; i < mEffects.size(); i++) {
2072 mEffects[i]->setThread(thread);
2073 }
2074}
2075
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002076void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2077{
2078 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2079 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2080 }
2081 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2082 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2083 }
2084}
2085
2086void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2087{
2088 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2089 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2090 }
2091 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2092 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2093 }
2094}
2095
2096bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002097{
2098 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002099 for (const auto &effect : mEffects) {
2100 if (effect->isProcessImplemented()) {
2101 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002102 }
2103 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002104 // Allow effects without processing.
2105 return true;
2106}
2107
2108bool AudioFlinger::EffectChain::isFastCompatible() const
2109{
2110 Mutex::Autolock _l(mLock);
2111 for (const auto &effect : mEffects) {
2112 if (effect->isProcessImplemented()
2113 && effect->isImplementationSoftware()) {
2114 return false;
2115 }
2116 }
2117 // Allow effects without processing or hw accelerated effects.
2118 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002119}
2120
2121// isCompatibleWithThread_l() must be called with thread->mLock held
2122bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2123{
2124 Mutex::Autolock _l(mLock);
2125 for (size_t i = 0; i < mEffects.size(); i++) {
2126 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2127 return false;
2128 }
2129 }
2130 return true;
2131}
2132
Glenn Kasten63238ef2015-03-02 15:50:29 -08002133} // namespace android