blob: 984bde088576322ac618f1d8f4f3b6ca9b61350a [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>
24#include <audio_effects/effect_visualizer.h>
25#include <audio_utils/primitives.h>
26#include <private/media/AudioEffectShared.h>
27#include <media/EffectsFactoryApi.h>
28
29#include "AudioFlinger.h"
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070030#include "EffectHalInterface.h"
31// FIXME: Remove after converting the main audio HAL
32#include "EffectHalLocal.h"
33#include "EffectsFactoryHalInterface.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080034#include "ServiceUtilities.h"
35
36// ----------------------------------------------------------------------------
37
38// Note: the following macro is used for extremely verbose logging message. In
39// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
40// 0; but one side effect of this is to turn all LOGV's as well. Some messages
41// are so verbose that we want to suppress them even when we have ALOG_ASSERT
42// turned on. Do not uncomment the #def below unless you really know what you
43// are doing and want to see all of the extremely verbose messages.
44//#define VERY_VERY_VERBOSE_LOGGING
45#ifdef VERY_VERY_VERBOSE_LOGGING
46#define ALOGVV ALOGV
47#else
48#define ALOGVV(a...) do { } while(0)
49#endif
50
Ricardo Garcia726b6a72014-08-11 12:04:54 -070051#define min(a, b) ((a) < (b) ? (a) : (b))
52
Eric Laurentca7cc822012-11-19 14:55:58 -080053namespace android {
54
55// ----------------------------------------------------------------------------
56// EffectModule implementation
57// ----------------------------------------------------------------------------
58
59#undef LOG_TAG
60#define LOG_TAG "AudioFlinger::EffectModule"
61
62AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
63 const wp<AudioFlinger::EffectChain>& chain,
64 effect_descriptor_t *desc,
65 int id,
Glenn Kastend848eb42016-03-08 13:42:11 -080066 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -080067 : mPinned(sessionId > AUDIO_SESSION_OUTPUT_MIX),
68 mThread(thread), mChain(chain), mId(id), mSessionId(sessionId),
69 mDescriptor(*desc),
70 // mConfig is set by configure() and not used before then
Eric Laurentca7cc822012-11-19 14:55:58 -080071 mStatus(NO_INIT), mState(IDLE),
72 // mMaxDisableWaitCnt is set by configure() and not used before then
73 // mDisableWaitCnt is set by process() and updateState() and not used before then
Eric Laurentaaa44472014-09-12 17:41:50 -070074 mSuspended(false),
75 mAudioFlinger(thread->mAudioFlinger)
Eric Laurentca7cc822012-11-19 14:55:58 -080076{
77 ALOGV("Constructor %p", this);
78 int lStatus;
79
80 // create effect engine from effect factory
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -070081 mStatus = -ENODEV;
82 sp<AudioFlinger> audioFlinger = mAudioFlinger.promote();
83 if (audioFlinger.get() != NULL) {
84 sp<EffectsFactoryHalInterface> effectsFactory = audioFlinger->getEffectsFactory();
85 if (effectsFactory.get() != NULL) {
86 mStatus = effectsFactory->createEffect(
87 &desc->uuid, sessionId, thread->id(), &mEffectInterface);
88 }
89 }
Eric Laurentca7cc822012-11-19 14:55:58 -080090
91 if (mStatus != NO_ERROR) {
92 return;
93 }
94 lStatus = init();
95 if (lStatus < 0) {
96 mStatus = lStatus;
97 goto Error;
98 }
99
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700100 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800101 return;
102Error:
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700103 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800104 ALOGV("Constructor Error %d", mStatus);
105}
106
107AudioFlinger::EffectModule::~EffectModule()
108{
109 ALOGV("Destructor %p", this);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700110 if (mEffectInterface.get() != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800111 remove_effect_from_hal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800112 // release effect engine
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700113 mEffectInterface.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -0800114 }
115}
116
117status_t AudioFlinger::EffectModule::addHandle(EffectHandle *handle)
118{
119 status_t status;
120
121 Mutex::Autolock _l(mLock);
122 int priority = handle->priority();
123 size_t size = mHandles.size();
124 EffectHandle *controlHandle = NULL;
125 size_t i;
126 for (i = 0; i < size; i++) {
127 EffectHandle *h = mHandles[i];
128 if (h == NULL || h->destroyed_l()) {
129 continue;
130 }
131 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700132 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800133 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700134 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800135 if (h->priority() <= priority) {
136 break;
137 }
138 }
139 // if inserted in first place, move effect control from previous owner to this handle
140 if (i == 0) {
141 bool enabled = false;
142 if (controlHandle != NULL) {
143 enabled = controlHandle->enabled();
144 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
145 }
146 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
147 status = NO_ERROR;
148 } else {
149 status = ALREADY_EXISTS;
150 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700151 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800152 mHandles.insertAt(handle, i);
153 return status;
154}
155
156size_t AudioFlinger::EffectModule::removeHandle(EffectHandle *handle)
157{
158 Mutex::Autolock _l(mLock);
159 size_t size = mHandles.size();
160 size_t i;
161 for (i = 0; i < size; i++) {
162 if (mHandles[i] == handle) {
163 break;
164 }
165 }
166 if (i == size) {
167 return size;
168 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700169 ALOGV("removeHandle() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800170
171 mHandles.removeAt(i);
172 // if removed from first place, move effect control from this handle to next in line
173 if (i == 0) {
174 EffectHandle *h = controlHandle_l();
175 if (h != NULL) {
176 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
177 }
178 }
179
180 // Prevent calls to process() and other functions on effect interface from now on.
181 // The effect engine will be released by the destructor when the last strong reference on
182 // this object is released which can happen after next process is called.
183 if (mHandles.size() == 0 && !mPinned) {
184 mState = DESTROYED;
185 }
186
187 return mHandles.size();
188}
189
190// must be called with EffectModule::mLock held
191AudioFlinger::EffectHandle *AudioFlinger::EffectModule::controlHandle_l()
192{
193 // the first valid handle in the list has control over the module
194 for (size_t i = 0; i < mHandles.size(); i++) {
195 EffectHandle *h = mHandles[i];
196 if (h != NULL && !h->destroyed_l()) {
197 return h;
198 }
199 }
200
201 return NULL;
202}
203
204size_t AudioFlinger::EffectModule::disconnect(EffectHandle *handle, bool unpinIfLast)
205{
206 ALOGV("disconnect() %p handle %p", this, handle);
207 // keep a strong reference on this EffectModule to avoid calling the
208 // destructor before we exit
209 sp<EffectModule> keep(this);
210 {
Eric Laurentaaa44472014-09-12 17:41:50 -0700211 if (removeHandle(handle) == 0) {
212 if (!isPinned() || unpinIfLast) {
213 sp<ThreadBase> thread = mThread.promote();
214 if (thread != 0) {
215 Mutex::Autolock _l(thread->mLock);
216 thread->removeEffect_l(this);
217 }
218 sp<AudioFlinger> af = mAudioFlinger.promote();
219 if (af != 0) {
220 af->updateOrphanEffectChains(this);
221 }
222 AudioSystem::unregisterEffect(mId);
223 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800224 }
225 }
226 return mHandles.size();
227}
228
Eric Laurentfa1e1232016-08-02 19:01:49 -0700229bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800230 Mutex::Autolock _l(mLock);
231
Eric Laurentfa1e1232016-08-02 19:01:49 -0700232 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800233 switch (mState) {
234 case RESTART:
235 reset_l();
236 // FALL THROUGH
237
238 case STARTING:
239 // clear auxiliary effect input buffer for next accumulation
240 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
241 memset(mConfig.inputCfg.buffer.raw,
242 0,
243 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
244 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700245 if (start_l() == NO_ERROR) {
246 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700247 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700248 } else {
249 mState = IDLE;
250 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800251 break;
252 case STOPPING:
Eric Laurentd0ebb532013-04-02 16:41:41 -0700253 if (stop_l() == NO_ERROR) {
254 mDisableWaitCnt = mMaxDisableWaitCnt;
255 } else {
256 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
257 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800258 mState = STOPPED;
259 break;
260 case STOPPED:
261 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
262 // turn off sequence.
263 if (--mDisableWaitCnt == 0) {
264 reset_l();
265 mState = IDLE;
266 }
267 break;
268 default: //IDLE , ACTIVE, DESTROYED
269 break;
270 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700271
272 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800273}
274
275void AudioFlinger::EffectModule::process()
276{
277 Mutex::Autolock _l(mLock);
278
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700279 if (mState == DESTROYED || mEffectInterface.get() == NULL ||
Eric Laurentca7cc822012-11-19 14:55:58 -0800280 mConfig.inputCfg.buffer.raw == NULL ||
281 mConfig.outputCfg.buffer.raw == NULL) {
282 return;
283 }
284
285 if (isProcessEnabled()) {
286 // do 32 bit to 16 bit conversion for auxiliary effect input buffer
287 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
288 ditherAndClamp(mConfig.inputCfg.buffer.s32,
289 mConfig.inputCfg.buffer.s32,
290 mConfig.inputCfg.buffer.frameCount/2);
291 }
292
293 // do the actual processing in the effect engine
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700294 int ret = mEffectInterface->process(&mConfig.inputCfg.buffer,
295 &mConfig.outputCfg.buffer);
Eric Laurentca7cc822012-11-19 14:55:58 -0800296
297 // force transition to IDLE state when engine is ready
298 if (mState == STOPPED && ret == -ENODATA) {
299 mDisableWaitCnt = 1;
300 }
301
302 // clear auxiliary effect input buffer for next accumulation
303 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
304 memset(mConfig.inputCfg.buffer.raw, 0,
305 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
306 }
307 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
308 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
309 // If an insert effect is idle and input buffer is different from output buffer,
310 // accumulate input onto output
311 sp<EffectChain> chain = mChain.promote();
312 if (chain != 0 && chain->activeTrackCnt() != 0) {
313 size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2; //always stereo here
314 int16_t *in = mConfig.inputCfg.buffer.s16;
315 int16_t *out = mConfig.outputCfg.buffer.s16;
316 for (size_t i = 0; i < frameCnt; i++) {
317 out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
318 }
319 }
320 }
321}
322
323void AudioFlinger::EffectModule::reset_l()
324{
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700325 if (mStatus != NO_ERROR || mEffectInterface.get() == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800326 return;
327 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700328 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800329}
330
331status_t AudioFlinger::EffectModule::configure()
332{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700333 status_t status;
334 sp<ThreadBase> thread;
335 uint32_t size;
336 audio_channel_mask_t channelMask;
337
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700338 if (mEffectInterface.get() == NULL) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700339 status = NO_INIT;
340 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800341 }
342
Eric Laurentd0ebb532013-04-02 16:41:41 -0700343 thread = mThread.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -0800344 if (thread == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700345 status = DEAD_OBJECT;
346 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800347 }
348
349 // TODO: handle configuration of effects replacing track process
Eric Laurentd0ebb532013-04-02 16:41:41 -0700350 channelMask = thread->channelMask();
Ricardo Garciad11da702015-05-28 12:14:12 -0700351 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800352
353 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
354 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
355 } else {
356 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700357 // TODO: Update this logic when multichannel effects are implemented.
358 // For offloaded tracks consider mono output as stereo for proper effect initialization
359 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
360 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
361 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
362 ALOGV("Overriding effect input and output as STEREO");
363 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800364 }
Ricardo Garciad11da702015-05-28 12:14:12 -0700365
Eric Laurentca7cc822012-11-19 14:55:58 -0800366 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
367 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
368 mConfig.inputCfg.samplingRate = thread->sampleRate();
369 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
370 mConfig.inputCfg.bufferProvider.cookie = NULL;
371 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
372 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
373 mConfig.outputCfg.bufferProvider.cookie = NULL;
374 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
375 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
376 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
377 // Insert effect:
378 // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
379 // always overwrites output buffer: input buffer == output buffer
380 // - in other sessions:
381 // last effect in the chain accumulates in output buffer: input buffer != output buffer
382 // other effect: overwrites output buffer: input buffer == output buffer
383 // Auxiliary effect:
384 // accumulates in output buffer: input buffer != output buffer
385 // Therefore: accumulate <=> input buffer != output buffer
386 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
387 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
388 } else {
389 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
390 }
391 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
392 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
393 mConfig.inputCfg.buffer.frameCount = thread->frameCount();
394 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
395
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700396 ALOGV("configure() %p thread %p buffer %p framecount %zu",
Eric Laurentca7cc822012-11-19 14:55:58 -0800397 this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
398
399 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700400 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700401 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
402 sizeof(effect_config_t),
403 &mConfig,
404 &size,
405 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800406 if (status == 0) {
407 status = cmdStatus;
408 }
409
410 if (status == 0 &&
411 (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0)) {
412 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
413 effect_param_t *p = (effect_param_t *)buf32;
414
415 p->psize = sizeof(uint32_t);
416 p->vsize = sizeof(uint32_t);
417 size = sizeof(int);
418 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
419
420 uint32_t latency = 0;
421 PlaybackThread *pbt = thread->mAudioFlinger->checkPlaybackThread_l(thread->mId);
422 if (pbt != NULL) {
423 latency = pbt->latency_l();
424 }
425
426 *((int32_t *)p->data + 1)= latency;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700427 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
428 sizeof(effect_param_t) + 8,
429 &buf32,
430 &size,
431 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800432 }
433
434 mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
435 (1000 * mConfig.outputCfg.buffer.frameCount);
436
Eric Laurentd0ebb532013-04-02 16:41:41 -0700437exit:
438 mStatus = status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800439 return status;
440}
441
442status_t AudioFlinger::EffectModule::init()
443{
444 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700445 if (mEffectInterface.get() == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800446 return NO_INIT;
447 }
448 status_t cmdStatus;
449 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700450 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
451 0,
452 NULL,
453 &size,
454 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800455 if (status == 0) {
456 status = cmdStatus;
457 }
458 return status;
459}
460
Eric Laurent1b928682014-10-02 19:41:47 -0700461void AudioFlinger::EffectModule::addEffectToHal_l()
462{
463 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
464 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
465 sp<ThreadBase> thread = mThread.promote();
466 if (thread != 0) {
467 audio_stream_t *stream = thread->stream();
468 if (stream != NULL) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700469 stream->add_audio_effect(stream,
470 reinterpret_cast<EffectHalLocal*>(mEffectInterface.get())->handle());
Eric Laurent1b928682014-10-02 19:41:47 -0700471 }
472 }
473 }
474}
475
Eric Laurentfa1e1232016-08-02 19:01:49 -0700476// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -0800477status_t AudioFlinger::EffectModule::start()
478{
Eric Laurentfa1e1232016-08-02 19:01:49 -0700479 sp<EffectChain> chain;
480 status_t status;
481 {
482 Mutex::Autolock _l(mLock);
483 status = start_l();
484 if (status == NO_ERROR) {
485 chain = mChain.promote();
486 }
487 }
488 if (chain != 0) {
489 chain->resetVolume_l();
490 }
491 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -0800492}
493
494status_t AudioFlinger::EffectModule::start_l()
495{
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700496 if (mEffectInterface.get() == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800497 return NO_INIT;
498 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700499 if (mStatus != NO_ERROR) {
500 return mStatus;
501 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800502 status_t cmdStatus;
503 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700504 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
505 0,
506 NULL,
507 &size,
508 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800509 if (status == 0) {
510 status = cmdStatus;
511 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700512 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -0700513 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800514 }
515 return status;
516}
517
518status_t AudioFlinger::EffectModule::stop()
519{
520 Mutex::Autolock _l(mLock);
521 return stop_l();
522}
523
524status_t AudioFlinger::EffectModule::stop_l()
525{
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700526 if (mEffectInterface.get() == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800527 return NO_INIT;
528 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700529 if (mStatus != NO_ERROR) {
530 return mStatus;
531 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800532 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800533 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700534 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
535 0,
536 NULL,
537 &size,
538 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800539 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800540 status = cmdStatus;
541 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800542 if (status == NO_ERROR) {
543 status = remove_effect_from_hal_l();
544 }
545 return status;
546}
547
548status_t AudioFlinger::EffectModule::remove_effect_from_hal_l()
549{
550 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
551 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800552 sp<ThreadBase> thread = mThread.promote();
553 if (thread != 0) {
554 audio_stream_t *stream = thread->stream();
555 if (stream != NULL) {
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700556 stream->remove_audio_effect(stream,
557 reinterpret_cast<EffectHalLocal*>(mEffectInterface.get())->handle());
Eric Laurentca7cc822012-11-19 14:55:58 -0800558 }
559 }
560 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800561 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800562}
563
Andy Hunge4a1d912016-08-17 14:11:13 -0700564// round up delta valid if value and divisor are positive.
565template <typename T>
566static T roundUpDelta(const T &value, const T &divisor) {
567 T remainder = value % divisor;
568 return remainder == 0 ? 0 : divisor - remainder;
569}
570
Eric Laurentca7cc822012-11-19 14:55:58 -0800571status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
572 uint32_t cmdSize,
573 void *pCmdData,
574 uint32_t *replySize,
575 void *pReplyData)
576{
577 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700578 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -0800579
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700580 if (mState == DESTROYED || mEffectInterface.get() == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800581 return NO_INIT;
582 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700583 if (mStatus != NO_ERROR) {
584 return mStatus;
585 }
Andy Hung110bc952016-06-20 15:22:52 -0700586 if (cmdCode == EFFECT_CMD_GET_PARAM &&
587 (*replySize < sizeof(effect_param_t) ||
588 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
589 android_errorWriteLog(0x534e4554, "29251553");
590 return -EINVAL;
591 }
Andy Hunge4a1d912016-08-17 14:11:13 -0700592 if ((cmdCode == EFFECT_CMD_SET_PARAM
593 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
594 (sizeof(effect_param_t) > cmdSize
595 || ((effect_param_t *)pCmdData)->psize > cmdSize
596 - sizeof(effect_param_t)
597 || ((effect_param_t *)pCmdData)->vsize > cmdSize
598 - sizeof(effect_param_t)
599 - ((effect_param_t *)pCmdData)->psize
600 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
601 cmdSize
602 - sizeof(effect_param_t)
603 - ((effect_param_t *)pCmdData)->psize
604 - ((effect_param_t *)pCmdData)->vsize)) {
605 android_errorWriteLog(0x534e4554, "30204301");
606 return -EINVAL;
607 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700608 status_t status = mEffectInterface->command(cmdCode,
609 cmdSize,
610 pCmdData,
611 replySize,
612 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -0800613 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
614 uint32_t size = (replySize == NULL) ? 0 : *replySize;
615 for (size_t i = 1; i < mHandles.size(); i++) {
616 EffectHandle *h = mHandles[i];
617 if (h != NULL && !h->destroyed_l()) {
618 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
619 }
620 }
621 }
622 return status;
623}
624
625status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
626{
627 Mutex::Autolock _l(mLock);
628 return setEnabled_l(enabled);
629}
630
631// must be called with EffectModule::mLock held
632status_t AudioFlinger::EffectModule::setEnabled_l(bool enabled)
633{
634
635 ALOGV("setEnabled %p enabled %d", this, enabled);
636
637 if (enabled != isEnabled()) {
638 status_t status = AudioSystem::setEffectEnabled(mId, enabled);
639 if (enabled && status != NO_ERROR) {
640 return status;
641 }
642
643 switch (mState) {
644 // going from disabled to enabled
645 case IDLE:
646 mState = STARTING;
647 break;
648 case STOPPED:
649 mState = RESTART;
650 break;
651 case STOPPING:
652 mState = ACTIVE;
653 break;
654
655 // going from enabled to disabled
656 case RESTART:
657 mState = STOPPED;
658 break;
659 case STARTING:
660 mState = IDLE;
661 break;
662 case ACTIVE:
663 mState = STOPPING;
664 break;
665 case DESTROYED:
666 return NO_ERROR; // simply ignore as we are being destroyed
667 }
668 for (size_t i = 1; i < mHandles.size(); i++) {
669 EffectHandle *h = mHandles[i];
670 if (h != NULL && !h->destroyed_l()) {
671 h->setEnabled(enabled);
672 }
673 }
674 }
675 return NO_ERROR;
676}
677
678bool AudioFlinger::EffectModule::isEnabled() const
679{
680 switch (mState) {
681 case RESTART:
682 case STARTING:
683 case ACTIVE:
684 return true;
685 case IDLE:
686 case STOPPING:
687 case STOPPED:
688 case DESTROYED:
689 default:
690 return false;
691 }
692}
693
694bool AudioFlinger::EffectModule::isProcessEnabled() const
695{
Eric Laurentd0ebb532013-04-02 16:41:41 -0700696 if (mStatus != NO_ERROR) {
697 return false;
698 }
699
Eric Laurentca7cc822012-11-19 14:55:58 -0800700 switch (mState) {
701 case RESTART:
702 case ACTIVE:
703 case STOPPING:
704 case STOPPED:
705 return true;
706 case IDLE:
707 case STARTING:
708 case DESTROYED:
709 default:
710 return false;
711 }
712}
713
714status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
715{
716 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700717 if (mStatus != NO_ERROR) {
718 return mStatus;
719 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800720 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800721 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
722 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
723 if (isProcessEnabled() &&
724 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
725 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800726 uint32_t volume[2];
727 uint32_t *pVolume = NULL;
728 uint32_t size = sizeof(volume);
729 volume[0] = *left;
730 volume[1] = *right;
731 if (controller) {
732 pVolume = volume;
733 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700734 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
735 size,
736 volume,
737 &size,
738 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -0800739 if (controller && status == NO_ERROR && size == sizeof(volume)) {
740 *left = volume[0];
741 *right = volume[1];
742 }
743 }
744 return status;
745}
746
747status_t AudioFlinger::EffectModule::setDevice(audio_devices_t device)
748{
749 if (device == AUDIO_DEVICE_NONE) {
750 return NO_ERROR;
751 }
752
753 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700754 if (mStatus != NO_ERROR) {
755 return mStatus;
756 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800757 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -0700758 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800759 status_t cmdStatus;
760 uint32_t size = sizeof(status_t);
761 uint32_t cmd = audio_is_output_devices(device) ? EFFECT_CMD_SET_DEVICE :
762 EFFECT_CMD_SET_INPUT_DEVICE;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700763 status = mEffectInterface->command(cmd,
764 sizeof(uint32_t),
765 &device,
766 &size,
767 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800768 }
769 return status;
770}
771
772status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
773{
774 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -0700775 if (mStatus != NO_ERROR) {
776 return mStatus;
777 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800778 status_t status = NO_ERROR;
779 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
780 status_t cmdStatus;
781 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700782 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
783 sizeof(audio_mode_t),
784 &mode,
785 &size,
786 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -0800787 if (status == NO_ERROR) {
788 status = cmdStatus;
789 }
790 }
791 return status;
792}
793
794status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
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;
801 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
802 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700803 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
804 sizeof(audio_source_t),
805 &source,
806 &size,
807 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800808 }
809 return status;
810}
811
812void AudioFlinger::EffectModule::setSuspended(bool suspended)
813{
814 Mutex::Autolock _l(mLock);
815 mSuspended = suspended;
816}
817
818bool AudioFlinger::EffectModule::suspended() const
819{
820 Mutex::Autolock _l(mLock);
821 return mSuspended;
822}
823
824bool AudioFlinger::EffectModule::purgeHandles()
825{
826 bool enabled = false;
827 Mutex::Autolock _l(mLock);
828 for (size_t i = 0; i < mHandles.size(); i++) {
829 EffectHandle *handle = mHandles[i];
830 if (handle != NULL && !handle->destroyed_l()) {
831 handle->effect().clear();
832 if (handle->hasControl()) {
833 enabled = handle->enabled();
834 }
835 }
836 }
837 return enabled;
838}
839
Eric Laurent5baf2af2013-09-12 17:37:00 -0700840status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
841{
842 Mutex::Autolock _l(mLock);
843 if (mStatus != NO_ERROR) {
844 return mStatus;
845 }
846 status_t status = NO_ERROR;
847 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
848 status_t cmdStatus;
849 uint32_t size = sizeof(status_t);
850 effect_offload_param_t cmd;
851
852 cmd.isOffload = offloaded;
853 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700854 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
855 sizeof(effect_offload_param_t),
856 &cmd,
857 &size,
858 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -0700859 if (status == NO_ERROR) {
860 status = cmdStatus;
861 }
862 mOffloaded = (status == NO_ERROR) ? offloaded : false;
863 } else {
864 if (offloaded) {
865 status = INVALID_OPERATION;
866 }
867 mOffloaded = false;
868 }
869 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
870 return status;
871}
872
873bool AudioFlinger::EffectModule::isOffloaded() const
874{
875 Mutex::Autolock _l(mLock);
876 return mOffloaded;
877}
878
Marco Nelissenb2208842014-02-07 14:00:50 -0800879String8 effectFlagsToString(uint32_t flags) {
880 String8 s;
881
882 s.append("conn. mode: ");
883 switch (flags & EFFECT_FLAG_TYPE_MASK) {
884 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
885 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
886 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
887 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
888 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
889 default: s.append("unknown/reserved"); break;
890 }
891 s.append(", ");
892
893 s.append("insert pref: ");
894 switch (flags & EFFECT_FLAG_INSERT_MASK) {
895 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
896 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
897 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
898 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
899 default: s.append("unknown/reserved"); break;
900 }
901 s.append(", ");
902
903 s.append("volume mgmt: ");
904 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
905 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
906 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
907 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
908 default: s.append("unknown/reserved"); break;
909 }
910 s.append(", ");
911
912 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
913 if (devind) {
914 s.append("device indication: ");
915 switch (devind) {
916 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
917 default: s.append("unknown/reserved"); break;
918 }
919 s.append(", ");
920 }
921
922 s.append("input mode: ");
923 switch (flags & EFFECT_FLAG_INPUT_MASK) {
924 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
925 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
926 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
927 default: s.append("not set"); break;
928 }
929 s.append(", ");
930
931 s.append("output mode: ");
932 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
933 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
934 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
935 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
936 default: s.append("not set"); break;
937 }
938 s.append(", ");
939
940 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
941 if (accel) {
942 s.append("hardware acceleration: ");
943 switch (accel) {
944 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
945 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
946 default: s.append("unknown/reserved"); break;
947 }
948 s.append(", ");
949 }
950
951 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
952 if (modeind) {
953 s.append("mode indication: ");
954 switch (modeind) {
955 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
956 default: s.append("unknown/reserved"); break;
957 }
958 s.append(", ");
959 }
960
961 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
962 if (srcind) {
963 s.append("source indication: ");
964 switch (srcind) {
965 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
966 default: s.append("unknown/reserved"); break;
967 }
968 s.append(", ");
969 }
970
971 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
972 s.append("offloadable, ");
973 }
974
975 int len = s.length();
976 if (s.length() > 2) {
Glenn Kasten57c4e6f2016-03-18 14:54:07 -0700977 (void) s.lockBuffer(len);
Marco Nelissenb2208842014-02-07 14:00:50 -0800978 s.unlockBuffer(len - 2);
979 }
980 return s;
981}
982
983
Glenn Kasten0f11b512014-01-31 16:18:54 -0800984void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args __unused)
Eric Laurentca7cc822012-11-19 14:55:58 -0800985{
986 const size_t SIZE = 256;
987 char buffer[SIZE];
988 String8 result;
989
990 snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
991 result.append(buffer);
992
993 bool locked = AudioFlinger::dumpTryLock(mLock);
994 // failed to lock - AudioFlinger is probably deadlocked
995 if (!locked) {
996 result.append("\t\tCould not lock Fx mutex:\n");
997 }
998
999 result.append("\t\tSession Status State Engine:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001000 snprintf(buffer, SIZE, "\t\t%05d %03d %03d %p\n",
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001001 mSessionId, mStatus, mState, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001002 result.append(buffer);
1003
1004 result.append("\t\tDescriptor:\n");
1005 snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1006 mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
1007 mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],
1008 mDescriptor.uuid.node[2],
1009 mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
1010 result.append(buffer);
1011 snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
1012 mDescriptor.type.timeLow, mDescriptor.type.timeMid,
1013 mDescriptor.type.timeHiAndVersion,
1014 mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],
1015 mDescriptor.type.node[2],
1016 mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
1017 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001018 snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001019 mDescriptor.apiVersion,
Marco Nelissenb2208842014-02-07 14:00:50 -08001020 mDescriptor.flags,
1021 effectFlagsToString(mDescriptor.flags).string());
Eric Laurentca7cc822012-11-19 14:55:58 -08001022 result.append(buffer);
1023 snprintf(buffer, SIZE, "\t\t- name: %s\n",
1024 mDescriptor.name);
1025 result.append(buffer);
1026 snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
1027 mDescriptor.implementor);
1028 result.append(buffer);
1029
1030 result.append("\t\t- Input configuration:\n");
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001031 result.append("\t\t\tFrames Smp rate Channels Format Buffer\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001032 snprintf(buffer, SIZE, "\t\t\t%05zu %05d %08x %6d (%s) %p\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001033 mConfig.inputCfg.buffer.frameCount,
1034 mConfig.inputCfg.samplingRate,
1035 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001036 mConfig.inputCfg.format,
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001037 formatToString((audio_format_t)mConfig.inputCfg.format),
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001038 mConfig.inputCfg.buffer.raw);
Eric Laurentca7cc822012-11-19 14:55:58 -08001039 result.append(buffer);
1040
1041 result.append("\t\t- Output configuration:\n");
1042 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001043 snprintf(buffer, SIZE, "\t\t\t%p %05zu %05d %08x %d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001044 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001045 mConfig.outputCfg.buffer.frameCount,
1046 mConfig.outputCfg.samplingRate,
1047 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001048 mConfig.outputCfg.format,
1049 formatToString((audio_format_t)mConfig.outputCfg.format));
Eric Laurentca7cc822012-11-19 14:55:58 -08001050 result.append(buffer);
1051
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001052 snprintf(buffer, SIZE, "\t\t%zu Clients:\n", mHandles.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08001053 result.append(buffer);
Marco Nelissenb2208842014-02-07 14:00:50 -08001054 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001055 for (size_t i = 0; i < mHandles.size(); ++i) {
1056 EffectHandle *handle = mHandles[i];
1057 if (handle != NULL && !handle->destroyed_l()) {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001058 handle->dumpToBuffer(buffer, SIZE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001059 result.append(buffer);
1060 }
1061 }
1062
Eric Laurentca7cc822012-11-19 14:55:58 -08001063 write(fd, result.string(), result.length());
1064
1065 if (locked) {
1066 mLock.unlock();
1067 }
1068}
1069
1070// ----------------------------------------------------------------------------
1071// EffectHandle implementation
1072// ----------------------------------------------------------------------------
1073
1074#undef LOG_TAG
1075#define LOG_TAG "AudioFlinger::EffectHandle"
1076
1077AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
1078 const sp<AudioFlinger::Client>& client,
1079 const sp<IEffectClient>& effectClient,
1080 int32_t priority)
1081 : BnEffect(),
1082 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
1083 mPriority(priority), mHasControl(false), mEnabled(false), mDestroyed(false)
1084{
1085 ALOGV("constructor %p", this);
1086
1087 if (client == 0) {
1088 return;
1089 }
1090 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1091 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001092 if (mCblkMemory == 0 ||
1093 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001094 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001096 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001097 return;
1098 }
Glenn Kastene75da402013-11-20 13:54:52 -08001099 new(mCblk) effect_param_cblk_t();
1100 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001101}
1102
1103AudioFlinger::EffectHandle::~EffectHandle()
1104{
1105 ALOGV("Destructor %p", this);
1106
1107 if (mEffect == 0) {
1108 mDestroyed = true;
1109 return;
1110 }
1111 mEffect->lock();
1112 mDestroyed = true;
1113 mEffect->unlock();
1114 disconnect(false);
1115}
1116
Glenn Kastene75da402013-11-20 13:54:52 -08001117status_t AudioFlinger::EffectHandle::initCheck()
1118{
1119 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1120}
1121
Eric Laurentca7cc822012-11-19 14:55:58 -08001122status_t AudioFlinger::EffectHandle::enable()
1123{
1124 ALOGV("enable %p", this);
1125 if (!mHasControl) {
1126 return INVALID_OPERATION;
1127 }
1128 if (mEffect == 0) {
1129 return DEAD_OBJECT;
1130 }
1131
1132 if (mEnabled) {
1133 return NO_ERROR;
1134 }
1135
1136 mEnabled = true;
1137
1138 sp<ThreadBase> thread = mEffect->thread().promote();
1139 if (thread != 0) {
1140 thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
1141 }
1142
1143 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
1144 if (mEffect->suspended()) {
1145 return NO_ERROR;
1146 }
1147
1148 status_t status = mEffect->setEnabled(true);
1149 if (status != NO_ERROR) {
1150 if (thread != 0) {
1151 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1152 }
1153 mEnabled = false;
Eric Laurent813e2a72013-08-31 12:59:48 -07001154 } else {
Eric Laurent59fe0102013-09-27 18:48:26 -07001155 if (thread != 0) {
1156 if (thread->type() == ThreadBase::OFFLOAD) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001157 PlaybackThread *t = (PlaybackThread *)thread.get();
Eric Laurent59fe0102013-09-27 18:48:26 -07001158 Mutex::Autolock _l(t->mLock);
1159 t->broadcast_l();
Eric Laurent813e2a72013-08-31 12:59:48 -07001160 }
Eric Laurent59fe0102013-09-27 18:48:26 -07001161 if (!mEffect->isOffloadable()) {
1162 if (thread->type() == ThreadBase::OFFLOAD) {
1163 PlaybackThread *t = (PlaybackThread *)thread.get();
1164 t->invalidateTracks(AUDIO_STREAM_MUSIC);
1165 }
1166 if (mEffect->sessionId() == AUDIO_SESSION_OUTPUT_MIX) {
1167 thread->mAudioFlinger->onNonOffloadableGlobalEffectEnable();
1168 }
Eric Laurent813e2a72013-08-31 12:59:48 -07001169 }
1170 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001171 }
1172 return status;
1173}
1174
1175status_t AudioFlinger::EffectHandle::disable()
1176{
1177 ALOGV("disable %p", this);
1178 if (!mHasControl) {
1179 return INVALID_OPERATION;
1180 }
1181 if (mEffect == 0) {
1182 return DEAD_OBJECT;
1183 }
1184
1185 if (!mEnabled) {
1186 return NO_ERROR;
1187 }
1188 mEnabled = false;
1189
1190 if (mEffect->suspended()) {
1191 return NO_ERROR;
1192 }
1193
1194 status_t status = mEffect->setEnabled(false);
1195
1196 sp<ThreadBase> thread = mEffect->thread().promote();
1197 if (thread != 0) {
1198 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
Eric Laurent59fe0102013-09-27 18:48:26 -07001199 if (thread->type() == ThreadBase::OFFLOAD) {
1200 PlaybackThread *t = (PlaybackThread *)thread.get();
1201 Mutex::Autolock _l(t->mLock);
1202 t->broadcast_l();
1203 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001204 }
1205
1206 return status;
1207}
1208
1209void AudioFlinger::EffectHandle::disconnect()
1210{
1211 disconnect(true);
1212}
1213
1214void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1215{
1216 ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
1217 if (mEffect == 0) {
1218 return;
1219 }
1220 // restore suspended effects if the disconnected handle was enabled and the last one.
1221 if ((mEffect->disconnect(this, unpinIfLast) == 0) && mEnabled) {
1222 sp<ThreadBase> thread = mEffect->thread().promote();
1223 if (thread != 0) {
1224 thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
1225 }
1226 }
1227
1228 // release sp on module => module destructor can be called now
1229 mEffect.clear();
1230 if (mClient != 0) {
1231 if (mCblk != NULL) {
1232 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1233 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1234 }
1235 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001236 // Client destructor must run with AudioFlinger client mutex locked
1237 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001238 mClient.clear();
1239 }
1240}
1241
1242status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1243 uint32_t cmdSize,
1244 void *pCmdData,
1245 uint32_t *replySize,
1246 void *pReplyData)
1247{
1248 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
1249 cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
1250
1251 // only get parameter command is permitted for applications not controlling the effect
1252 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1253 return INVALID_OPERATION;
1254 }
1255 if (mEffect == 0) {
1256 return DEAD_OBJECT;
1257 }
1258 if (mClient == 0) {
1259 return INVALID_OPERATION;
1260 }
1261
1262 // handle commands that are not forwarded transparently to effect engine
1263 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
1264 // No need to trylock() here as this function is executed in the binder thread serving a
1265 // particular client process: no risk to block the whole media server process or mixer
1266 // threads if we are stuck here
1267 Mutex::Autolock _l(mCblk->lock);
1268 if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1269 mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
1270 mCblk->serverIndex = 0;
1271 mCblk->clientIndex = 0;
1272 return BAD_VALUE;
1273 }
1274 status_t status = NO_ERROR;
1275 while (mCblk->serverIndex < mCblk->clientIndex) {
1276 int reply;
1277 uint32_t rsize = sizeof(int);
1278 int *p = (int *)(mBuffer + mCblk->serverIndex);
1279 int size = *p++;
1280 if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
1281 ALOGW("command(): invalid parameter block size");
1282 break;
1283 }
1284 effect_param_t *param = (effect_param_t *)p;
1285 if (param->psize == 0 || param->vsize == 0) {
1286 ALOGW("command(): null parameter or value size");
1287 mCblk->serverIndex += size;
1288 continue;
1289 }
1290 uint32_t psize = sizeof(effect_param_t) +
1291 ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
1292 param->vsize;
1293 status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
1294 psize,
1295 p,
1296 &rsize,
1297 &reply);
1298 // stop at first error encountered
1299 if (ret != NO_ERROR) {
1300 status = ret;
1301 *(int *)pReplyData = reply;
1302 break;
1303 } else if (reply != NO_ERROR) {
1304 *(int *)pReplyData = reply;
1305 break;
1306 }
1307 mCblk->serverIndex += size;
1308 }
1309 mCblk->serverIndex = 0;
1310 mCblk->clientIndex = 0;
1311 return status;
1312 } else if (cmdCode == EFFECT_CMD_ENABLE) {
1313 *(int *)pReplyData = NO_ERROR;
1314 return enable();
1315 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1316 *(int *)pReplyData = NO_ERROR;
1317 return disable();
1318 }
1319
1320 return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1321}
1322
1323void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1324{
1325 ALOGV("setControl %p control %d", this, hasControl);
1326
1327 mHasControl = hasControl;
1328 mEnabled = enabled;
1329
1330 if (signal && mEffectClient != 0) {
1331 mEffectClient->controlStatusChanged(hasControl);
1332 }
1333}
1334
1335void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1336 uint32_t cmdSize,
1337 void *pCmdData,
1338 uint32_t replySize,
1339 void *pReplyData)
1340{
1341 if (mEffectClient != 0) {
1342 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1343 }
1344}
1345
1346
1347
1348void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1349{
1350 if (mEffectClient != 0) {
1351 mEffectClient->enableStatusChanged(enabled);
1352 }
1353}
1354
1355status_t AudioFlinger::EffectHandle::onTransact(
1356 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1357{
1358 return BnEffect::onTransact(code, data, reply, flags);
1359}
1360
1361
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001362void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001363{
1364 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1365
Marco Nelissenb2208842014-02-07 14:00:50 -08001366 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Eric Laurentca7cc822012-11-19 14:55:58 -08001367 (mClient == 0) ? getpid_cached : mClient->pid(),
1368 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001369 mHasControl ? "yes" : "no",
1370 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001371 mCblk ? mCblk->clientIndex : 0,
1372 mCblk ? mCblk->serverIndex : 0
1373 );
1374
1375 if (locked) {
1376 mCblk->lock.unlock();
1377 }
1378}
1379
1380#undef LOG_TAG
1381#define LOG_TAG "AudioFlinger::EffectChain"
1382
1383AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001384 audio_session_t sessionId)
Eric Laurentca7cc822012-11-19 14:55:58 -08001385 : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
1386 mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurentfa1e1232016-08-02 19:01:49 -07001387 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
Eric Laurentca7cc822012-11-19 14:55:58 -08001388{
1389 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1390 if (thread == NULL) {
1391 return;
1392 }
1393 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1394 thread->frameCount();
1395}
1396
1397AudioFlinger::EffectChain::~EffectChain()
1398{
1399 if (mOwnInBuffer) {
1400 delete mInBuffer;
1401 }
1402
1403}
1404
1405// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1406sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1407 effect_descriptor_t *descriptor)
1408{
1409 size_t size = mEffects.size();
1410
1411 for (size_t i = 0; i < size; i++) {
1412 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1413 return mEffects[i];
1414 }
1415 }
1416 return 0;
1417}
1418
1419// getEffectFromId_l() must be called with ThreadBase::mLock held
1420sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1421{
1422 size_t size = mEffects.size();
1423
1424 for (size_t i = 0; i < size; i++) {
1425 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1426 if (id == 0 || mEffects[i]->id() == id) {
1427 return mEffects[i];
1428 }
1429 }
1430 return 0;
1431}
1432
1433// getEffectFromType_l() must be called with ThreadBase::mLock held
1434sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1435 const effect_uuid_t *type)
1436{
1437 size_t size = mEffects.size();
1438
1439 for (size_t i = 0; i < size; i++) {
1440 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1441 return mEffects[i];
1442 }
1443 }
1444 return 0;
1445}
1446
1447void AudioFlinger::EffectChain::clearInputBuffer()
1448{
1449 Mutex::Autolock _l(mLock);
1450 sp<ThreadBase> thread = mThread.promote();
1451 if (thread == 0) {
1452 ALOGW("clearInputBuffer(): cannot promote mixer thread");
1453 return;
1454 }
1455 clearInputBuffer_l(thread);
1456}
1457
1458// Must be called with EffectChain::mLock locked
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -07001459void AudioFlinger::EffectChain::clearInputBuffer_l(const sp<ThreadBase>& thread)
Eric Laurentca7cc822012-11-19 14:55:58 -08001460{
Ricardo Garcia322bab22014-08-06 11:43:46 -07001461 // TODO: This will change in the future, depending on multichannel
1462 // and sample format changes for effects.
1463 // Currently effects processing is only available for stereo, AUDIO_FORMAT_PCM_16_BIT
1464 // (4 bytes frame size)
Ricardo Garcia726b6a72014-08-11 12:04:54 -07001465 const size_t frameSize =
1466 audio_bytes_per_sample(AUDIO_FORMAT_PCM_16_BIT) * min(FCC_2, thread->channelCount());
Ricardo Garcia322bab22014-08-06 11:43:46 -07001467 memset(mInBuffer, 0, thread->frameCount() * frameSize);
Eric Laurentca7cc822012-11-19 14:55:58 -08001468}
1469
1470// Must be called with EffectChain::mLock locked
1471void AudioFlinger::EffectChain::process_l()
1472{
1473 sp<ThreadBase> thread = mThread.promote();
1474 if (thread == 0) {
1475 ALOGW("process_l(): cannot promote mixer thread");
1476 return;
1477 }
1478 bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
1479 (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
Jean-Michel Trivifed62922013-09-25 18:50:33 -07001480 // never process effects when:
1481 // - on an OFFLOAD thread
1482 // - no more tracks are on the session and the effect tail has been rendered
1483 bool doProcess = (thread->type() != ThreadBase::OFFLOAD);
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 if (!isGlobalSession) {
1485 bool tracksOnSession = (trackCnt() != 0);
1486
1487 if (!tracksOnSession && mTailBufferCount == 0) {
1488 doProcess = false;
1489 }
1490
1491 if (activeTrackCnt() == 0) {
1492 // if no track is active and the effect tail has not been rendered,
1493 // the input buffer must be cleared here as the mixer process will not do it
1494 if (tracksOnSession || mTailBufferCount > 0) {
1495 clearInputBuffer_l(thread);
1496 if (mTailBufferCount > 0) {
1497 mTailBufferCount--;
1498 }
1499 }
1500 }
1501 }
1502
1503 size_t size = mEffects.size();
1504 if (doProcess) {
1505 for (size_t i = 0; i < size; i++) {
1506 mEffects[i]->process();
1507 }
1508 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001509 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001510 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07001511 doResetVolume = mEffects[i]->updateState() || doResetVolume;
1512 }
1513 if (doResetVolume) {
1514 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001515 }
1516}
1517
1518// addEffect_l() must be called with PlaybackThread::mLock held
1519status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
1520{
1521 effect_descriptor_t desc = effect->desc();
1522 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
1523
1524 Mutex::Autolock _l(mLock);
1525 effect->setChain(this);
1526 sp<ThreadBase> thread = mThread.promote();
1527 if (thread == 0) {
1528 return NO_INIT;
1529 }
1530 effect->setThread(thread);
1531
1532 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1533 // Auxiliary effects are inserted at the beginning of mEffects vector as
1534 // they are processed first and accumulated in chain input buffer
1535 mEffects.insertAt(effect, 0);
1536
1537 // the input buffer for auxiliary effect contains mono samples in
1538 // 32 bit format. This is to avoid saturation in AudoMixer
1539 // accumulation stage. Saturation is done in EffectModule::process() before
1540 // calling the process in effect engine
1541 size_t numSamples = thread->frameCount();
1542 int32_t *buffer = new int32_t[numSamples];
1543 memset(buffer, 0, numSamples * sizeof(int32_t));
1544 effect->setInBuffer((int16_t *)buffer);
1545 // auxiliary effects output samples to chain input buffer for further processing
1546 // by insert effects
1547 effect->setOutBuffer(mInBuffer);
1548 } else {
1549 // Insert effects are inserted at the end of mEffects vector as they are processed
1550 // after track and auxiliary effects.
1551 // Insert effect order as a function of indicated preference:
1552 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
1553 // another effect is present
1554 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
1555 // last effect claiming first position
1556 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
1557 // first effect claiming last position
1558 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
1559 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
1560 // already present
1561
1562 size_t size = mEffects.size();
1563 size_t idx_insert = size;
1564 ssize_t idx_insert_first = -1;
1565 ssize_t idx_insert_last = -1;
1566
1567 for (size_t i = 0; i < size; i++) {
1568 effect_descriptor_t d = mEffects[i]->desc();
1569 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
1570 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
1571 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
1572 // check invalid effect chaining combinations
1573 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
1574 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
1575 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
1576 desc.name, d.name);
1577 return INVALID_OPERATION;
1578 }
1579 // remember position of first insert effect and by default
1580 // select this as insert position for new effect
1581 if (idx_insert == size) {
1582 idx_insert = i;
1583 }
1584 // remember position of last insert effect claiming
1585 // first position
1586 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
1587 idx_insert_first = i;
1588 }
1589 // remember position of first insert effect claiming
1590 // last position
1591 if (iPref == EFFECT_FLAG_INSERT_LAST &&
1592 idx_insert_last == -1) {
1593 idx_insert_last = i;
1594 }
1595 }
1596 }
1597
1598 // modify idx_insert from first position if needed
1599 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
1600 if (idx_insert_last != -1) {
1601 idx_insert = idx_insert_last;
1602 } else {
1603 idx_insert = size;
1604 }
1605 } else {
1606 if (idx_insert_first != -1) {
1607 idx_insert = idx_insert_first + 1;
1608 }
1609 }
1610
1611 // always read samples from chain input buffer
1612 effect->setInBuffer(mInBuffer);
1613
1614 // if last effect in the chain, output samples to chain
1615 // output buffer, otherwise to chain input buffer
1616 if (idx_insert == size) {
1617 if (idx_insert != 0) {
1618 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
1619 mEffects[idx_insert-1]->configure();
1620 }
1621 effect->setOutBuffer(mOutBuffer);
1622 } else {
1623 effect->setOutBuffer(mInBuffer);
1624 }
1625 mEffects.insertAt(effect, idx_insert);
1626
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001627 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08001628 idx_insert);
1629 }
1630 effect->configure();
1631 return NO_ERROR;
1632}
1633
1634// removeEffect_l() must be called with PlaybackThread::mLock held
1635size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
1636{
1637 Mutex::Autolock _l(mLock);
1638 size_t size = mEffects.size();
1639 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
1640
1641 for (size_t i = 0; i < size; i++) {
1642 if (effect == mEffects[i]) {
1643 // calling stop here will remove pre-processing effect from the audio HAL.
1644 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
1645 // the middle of a read from audio HAL
1646 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1647 mEffects[i]->state() == EffectModule::STOPPING) {
1648 mEffects[i]->stop();
1649 }
1650 if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
1651 delete[] effect->inBuffer();
1652 } else {
1653 if (i == size - 1 && i != 0) {
1654 mEffects[i - 1]->setOutBuffer(mOutBuffer);
1655 mEffects[i - 1]->configure();
1656 }
1657 }
1658 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001659 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001660 this, i);
1661 break;
1662 }
1663 }
1664
1665 return mEffects.size();
1666}
1667
1668// setDevice_l() must be called with PlaybackThread::mLock held
1669void AudioFlinger::EffectChain::setDevice_l(audio_devices_t device)
1670{
1671 size_t size = mEffects.size();
1672 for (size_t i = 0; i < size; i++) {
1673 mEffects[i]->setDevice(device);
1674 }
1675}
1676
1677// setMode_l() must be called with PlaybackThread::mLock held
1678void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
1679{
1680 size_t size = mEffects.size();
1681 for (size_t i = 0; i < size; i++) {
1682 mEffects[i]->setMode(mode);
1683 }
1684}
1685
1686// setAudioSource_l() must be called with PlaybackThread::mLock held
1687void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
1688{
1689 size_t size = mEffects.size();
1690 for (size_t i = 0; i < size; i++) {
1691 mEffects[i]->setAudioSource(source);
1692 }
1693}
1694
Eric Laurentfa1e1232016-08-02 19:01:49 -07001695// setVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1696bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08001697{
1698 uint32_t newLeft = *left;
1699 uint32_t newRight = *right;
1700 bool hasControl = false;
1701 int ctrlIdx = -1;
1702 size_t size = mEffects.size();
1703
1704 // first update volume controller
1705 for (size_t i = size; i > 0; i--) {
1706 if (mEffects[i - 1]->isProcessEnabled() &&
1707 (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
1708 ctrlIdx = i - 1;
1709 hasControl = true;
1710 break;
1711 }
1712 }
1713
Eric Laurentfa1e1232016-08-02 19:01:49 -07001714 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001715 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001716 if (hasControl) {
1717 *left = mNewLeftVolume;
1718 *right = mNewRightVolume;
1719 }
1720 return hasControl;
1721 }
1722
1723 mVolumeCtrlIdx = ctrlIdx;
1724 mLeftVolume = newLeft;
1725 mRightVolume = newRight;
1726
1727 // second get volume update from volume controller
1728 if (ctrlIdx >= 0) {
1729 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
1730 mNewLeftVolume = newLeft;
1731 mNewRightVolume = newRight;
1732 }
1733 // then indicate volume to all other effects in chain.
1734 // Pass altered volume to effects before volume controller
1735 // and requested volume to effects after controller
1736 uint32_t lVol = newLeft;
1737 uint32_t rVol = newRight;
1738
1739 for (size_t i = 0; i < size; i++) {
1740 if ((int)i == ctrlIdx) {
1741 continue;
1742 }
1743 // this also works for ctrlIdx == -1 when there is no volume controller
1744 if ((int)i > ctrlIdx) {
1745 lVol = *left;
1746 rVol = *right;
1747 }
1748 mEffects[i]->setVolume(&lVol, &rVol, false);
1749 }
1750 *left = newLeft;
1751 *right = newRight;
1752
1753 return hasControl;
1754}
1755
Eric Laurentfa1e1232016-08-02 19:01:49 -07001756// resetVolume_l() must be called with PlaybackThread::mLock or EffectChain::mLock held
1757void AudioFlinger::EffectChain::resetVolume_l()
1758{
Eric Laurente7449bf2016-08-03 18:44:07 -07001759 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
1760 uint32_t left = mLeftVolume;
1761 uint32_t right = mRightVolume;
1762 (void)setVolume_l(&left, &right, true);
1763 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07001764}
1765
Eric Laurent1b928682014-10-02 19:41:47 -07001766void AudioFlinger::EffectChain::syncHalEffectsState()
1767{
1768 Mutex::Autolock _l(mLock);
1769 for (size_t i = 0; i < mEffects.size(); i++) {
1770 if (mEffects[i]->state() == EffectModule::ACTIVE ||
1771 mEffects[i]->state() == EffectModule::STOPPING) {
1772 mEffects[i]->addEffectToHal_l();
1773 }
1774 }
1775}
1776
Eric Laurentca7cc822012-11-19 14:55:58 -08001777void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
1778{
1779 const size_t SIZE = 256;
1780 char buffer[SIZE];
1781 String8 result;
1782
Marco Nelissenb2208842014-02-07 14:00:50 -08001783 size_t numEffects = mEffects.size();
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001784 snprintf(buffer, SIZE, " %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08001785 result.append(buffer);
1786
Marco Nelissenb2208842014-02-07 14:00:50 -08001787 if (numEffects) {
1788 bool locked = AudioFlinger::dumpTryLock(mLock);
1789 // failed to lock - AudioFlinger is probably deadlocked
1790 if (!locked) {
1791 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08001792 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001793
Marco Nelissenb2208842014-02-07 14:00:50 -08001794 result.append("\tIn buffer Out buffer Active tracks:\n");
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001795 snprintf(buffer, SIZE, "\t%p %p %d\n",
1796 mInBuffer,
1797 mOutBuffer,
Marco Nelissenb2208842014-02-07 14:00:50 -08001798 mActiveTrackCnt);
1799 result.append(buffer);
1800 write(fd, result.string(), result.size());
1801
1802 for (size_t i = 0; i < numEffects; ++i) {
1803 sp<EffectModule> effect = mEffects[i];
1804 if (effect != 0) {
1805 effect->dump(fd, args);
1806 }
1807 }
1808
1809 if (locked) {
1810 mLock.unlock();
1811 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001812 }
1813}
1814
1815// must be called with ThreadBase::mLock held
1816void AudioFlinger::EffectChain::setEffectSuspended_l(
1817 const effect_uuid_t *type, bool suspend)
1818{
1819 sp<SuspendedEffectDesc> desc;
1820 // use effect type UUID timelow as key as there is no real risk of identical
1821 // timeLow fields among effect type UUIDs.
1822 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
1823 if (suspend) {
1824 if (index >= 0) {
1825 desc = mSuspendedEffects.valueAt(index);
1826 } else {
1827 desc = new SuspendedEffectDesc();
1828 desc->mType = *type;
1829 mSuspendedEffects.add(type->timeLow, desc);
1830 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
1831 }
1832 if (desc->mRefCount++ == 0) {
1833 sp<EffectModule> effect = getEffectIfEnabled(type);
1834 if (effect != 0) {
1835 desc->mEffect = effect;
1836 effect->setSuspended(true);
1837 effect->setEnabled(false);
1838 }
1839 }
1840 } else {
1841 if (index < 0) {
1842 return;
1843 }
1844 desc = mSuspendedEffects.valueAt(index);
1845 if (desc->mRefCount <= 0) {
1846 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
1847 desc->mRefCount = 1;
1848 }
1849 if (--desc->mRefCount == 0) {
1850 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
1851 if (desc->mEffect != 0) {
1852 sp<EffectModule> effect = desc->mEffect.promote();
1853 if (effect != 0) {
1854 effect->setSuspended(false);
1855 effect->lock();
1856 EffectHandle *handle = effect->controlHandle_l();
1857 if (handle != NULL && !handle->destroyed_l()) {
1858 effect->setEnabled_l(handle->enabled());
1859 }
1860 effect->unlock();
1861 }
1862 desc->mEffect.clear();
1863 }
1864 mSuspendedEffects.removeItemsAt(index);
1865 }
1866 }
1867}
1868
1869// must be called with ThreadBase::mLock held
1870void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
1871{
1872 sp<SuspendedEffectDesc> desc;
1873
1874 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1875 if (suspend) {
1876 if (index >= 0) {
1877 desc = mSuspendedEffects.valueAt(index);
1878 } else {
1879 desc = new SuspendedEffectDesc();
1880 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
1881 ALOGV("setEffectSuspendedAll_l() add entry for 0");
1882 }
1883 if (desc->mRefCount++ == 0) {
1884 Vector< sp<EffectModule> > effects;
1885 getSuspendEligibleEffects(effects);
1886 for (size_t i = 0; i < effects.size(); i++) {
1887 setEffectSuspended_l(&effects[i]->desc().type, true);
1888 }
1889 }
1890 } else {
1891 if (index < 0) {
1892 return;
1893 }
1894 desc = mSuspendedEffects.valueAt(index);
1895 if (desc->mRefCount <= 0) {
1896 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
1897 desc->mRefCount = 1;
1898 }
1899 if (--desc->mRefCount == 0) {
1900 Vector<const effect_uuid_t *> types;
1901 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
1902 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
1903 continue;
1904 }
1905 types.add(&mSuspendedEffects.valueAt(i)->mType);
1906 }
1907 for (size_t i = 0; i < types.size(); i++) {
1908 setEffectSuspended_l(types[i], false);
1909 }
1910 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
1911 mSuspendedEffects.keyAt(index));
1912 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
1913 }
1914 }
1915}
1916
1917
1918// The volume effect is used for automated tests only
1919#ifndef OPENSL_ES_H_
1920static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
1921 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
1922const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
1923#endif //OPENSL_ES_H_
1924
1925bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
1926{
1927 // auxiliary effects and visualizer are never suspended on output mix
1928 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
1929 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
1930 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
1931 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
1932 return false;
1933 }
1934 return true;
1935}
1936
1937void AudioFlinger::EffectChain::getSuspendEligibleEffects(
1938 Vector< sp<AudioFlinger::EffectModule> > &effects)
1939{
1940 effects.clear();
1941 for (size_t i = 0; i < mEffects.size(); i++) {
1942 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
1943 effects.add(mEffects[i]);
1944 }
1945 }
1946}
1947
1948sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
1949 const effect_uuid_t *type)
1950{
1951 sp<EffectModule> effect = getEffectFromType_l(type);
1952 return effect != 0 && effect->isEnabled() ? effect : 0;
1953}
1954
1955void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1956 bool enabled)
1957{
1958 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1959 if (enabled) {
1960 if (index < 0) {
1961 // if the effect is not suspend check if all effects are suspended
1962 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
1963 if (index < 0) {
1964 return;
1965 }
1966 if (!isEffectEligibleForSuspend(effect->desc())) {
1967 return;
1968 }
1969 setEffectSuspended_l(&effect->desc().type, enabled);
1970 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
1971 if (index < 0) {
1972 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
1973 return;
1974 }
1975 }
1976 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
1977 effect->desc().type.timeLow);
1978 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1979 // if effect is requested to suspended but was not yet enabled, supend it now.
1980 if (desc->mEffect == 0) {
1981 desc->mEffect = effect;
1982 effect->setEnabled(false);
1983 effect->setSuspended(true);
1984 }
1985 } else {
1986 if (index < 0) {
1987 return;
1988 }
1989 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
1990 effect->desc().type.timeLow);
1991 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
1992 desc->mEffect.clear();
1993 effect->setSuspended(false);
1994 }
1995}
1996
Eric Laurent5baf2af2013-09-12 17:37:00 -07001997bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07001998{
1999 Mutex::Autolock _l(mLock);
2000 size_t size = mEffects.size();
2001 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002002 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002003 return true;
2004 }
2005 }
2006 return false;
2007}
2008
Eric Laurentaaa44472014-09-12 17:41:50 -07002009void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2010{
2011 Mutex::Autolock _l(mLock);
2012 mThread = thread;
2013 for (size_t i = 0; i < mEffects.size(); i++) {
2014 mEffects[i]->setThread(thread);
2015 }
2016}
2017
Eric Laurent4c415062016-06-17 16:14:16 -07002018bool AudioFlinger::EffectChain::hasSoftwareEffect() const
2019{
2020 Mutex::Autolock _l(mLock);
2021 for (size_t i = 0; i < mEffects.size(); i++) {
2022 if (mEffects[i]->isImplementationSoftware()) {
2023 return true;
2024 }
2025 }
2026 return false;
2027}
2028
2029// isCompatibleWithThread_l() must be called with thread->mLock held
2030bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2031{
2032 Mutex::Autolock _l(mLock);
2033 for (size_t i = 0; i < mEffects.size(); i++) {
2034 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2035 return false;
2036 }
2037 }
2038 return true;
2039}
2040
Glenn Kasten63238ef2015-03-02 15:50:29 -08002041} // namespace android