blob: 2f3724fa9e2237f48440313eca2a09523d9dae27 [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
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070027#include <system/audio_effects/effect_dynamicsprocessing.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070028#include <system/audio_effects/effect_ns.h>
29#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080030#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080031#include <audio_utils/primitives.h>
jiabin8f278ee2019-11-11 12:16:27 -080032#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070033#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080034#include <media/AudioDeviceTypeAddr.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070035#include <media/audiohal/EffectHalInterface.h>
36#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070037#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080038
39#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080040
41// ----------------------------------------------------------------------------
42
43// Note: the following macro is used for extremely verbose logging message. In
44// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
45// 0; but one side effect of this is to turn all LOGV's as well. Some messages
46// are so verbose that we want to suppress them even when we have ALOG_ASSERT
47// turned on. Do not uncomment the #def below unless you really know what you
48// are doing and want to see all of the extremely verbose messages.
49//#define VERY_VERY_VERBOSE_LOGGING
50#ifdef VERY_VERY_VERBOSE_LOGGING
51#define ALOGVV ALOGV
52#else
53#define ALOGVV(a...) do { } while(0)
54#endif
55
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090056#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
57
Eric Laurentca7cc822012-11-19 14:55:58 -080058namespace android {
59
60// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080061// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080062// ----------------------------------------------------------------------------
63
64#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080065#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080066
Eric Laurent41709552019-12-16 19:34:05 -080067AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080068 effect_descriptor_t *desc,
69 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080070 audio_session_t sessionId,
71 bool pinned)
72 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080073 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080074 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -080075{
Eric Laurentca7cc822012-11-19 14:55:58 -080076}
77
Eric Laurent41709552019-12-16 19:34:05 -080078// must be called with EffectModule::mLock held
79status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -080080{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080081
Eric Laurent41709552019-12-16 19:34:05 -080082 ALOGV("setEnabled %p enabled %d", this, enabled);
83
84 if (enabled != isEnabled()) {
85 switch (mState) {
86 // going from disabled to enabled
87 case IDLE:
88 mState = STARTING;
89 break;
90 case STOPPED:
91 mState = RESTART;
92 break;
93 case STOPPING:
94 mState = ACTIVE;
95 break;
96
97 // going from enabled to disabled
98 case RESTART:
99 mState = STOPPED;
100 break;
101 case STARTING:
102 mState = IDLE;
103 break;
104 case ACTIVE:
105 mState = STOPPING;
106 break;
107 case DESTROYED:
108 return NO_ERROR; // simply ignore as we are being destroyed
109 }
110 for (size_t i = 1; i < mHandles.size(); i++) {
111 EffectHandle *h = mHandles[i];
112 if (h != NULL && !h->disconnected()) {
113 h->setEnabled(enabled);
114 }
115 }
116 }
117 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800118}
119
Eric Laurent41709552019-12-16 19:34:05 -0800120status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
121{
122 status_t status;
123 {
124 Mutex::Autolock _l(mLock);
125 status = setEnabled_l(enabled);
126 }
127 if (fromHandle) {
128 if (enabled) {
129 if (status != NO_ERROR) {
130 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
131 } else {
132 mCallback->onEffectEnable(this);
133 }
134 } else {
135 mCallback->onEffectDisable(this);
136 }
137 }
138 return status;
139}
140
141bool AudioFlinger::EffectBase::isEnabled() const
142{
143 switch (mState) {
144 case RESTART:
145 case STARTING:
146 case ACTIVE:
147 return true;
148 case IDLE:
149 case STOPPING:
150 case STOPPED:
151 case DESTROYED:
152 default:
153 return false;
154 }
155}
156
157void AudioFlinger::EffectBase::setSuspended(bool suspended)
158{
159 Mutex::Autolock _l(mLock);
160 mSuspended = suspended;
161}
162
163bool AudioFlinger::EffectBase::suspended() const
164{
165 Mutex::Autolock _l(mLock);
166 return mSuspended;
167}
168
169status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800170{
171 status_t status;
172
173 Mutex::Autolock _l(mLock);
174 int priority = handle->priority();
175 size_t size = mHandles.size();
176 EffectHandle *controlHandle = NULL;
177 size_t i;
178 for (i = 0; i < size; i++) {
179 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800180 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800181 continue;
182 }
183 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700184 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800185 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700186 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800187 if (h->priority() <= priority) {
188 break;
189 }
190 }
191 // if inserted in first place, move effect control from previous owner to this handle
192 if (i == 0) {
193 bool enabled = false;
194 if (controlHandle != NULL) {
195 enabled = controlHandle->enabled();
196 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
197 }
198 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
199 status = NO_ERROR;
200 } else {
201 status = ALREADY_EXISTS;
202 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700203 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800204 mHandles.insertAt(handle, i);
205 return status;
206}
207
Eric Laurent41709552019-12-16 19:34:05 -0800208status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700209{
210 status_t status = NO_ERROR;
211 bool doRegister = false;
212 bool registered = false;
213 bool doEnable = false;
214 bool enabled = false;
215 audio_io_handle_t io;
216 uint32_t strategy;
217
218 {
219 Mutex::Autolock _l(mLock);
220 // register effect when first handle is attached and unregister when last handle is removed
221 if (mPolicyRegistered != mHandles.size() > 0) {
222 doRegister = true;
223 mPolicyRegistered = mHandles.size() > 0;
224 if (mPolicyRegistered) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800225 io = mCallback->io();
226 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700227 }
228 }
229 // enable effect when registered according to enable state requested by controlling handle
230 if (mHandles.size() > 0) {
231 EffectHandle *handle = controlHandle_l();
232 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
233 doEnable = true;
234 mPolicyEnabled = handle->enabled();
235 }
236 }
237 registered = mPolicyRegistered;
238 enabled = mPolicyEnabled;
239 mPolicyLock.lock();
240 }
241 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
242 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
243 if (doRegister) {
244 if (registered) {
245 status = AudioSystem::registerEffect(
246 &mDescriptor,
247 io,
248 strategy,
249 mSessionId,
250 mId);
251 } else {
252 status = AudioSystem::unregisterEffect(mId);
253 }
254 }
255 if (registered && doEnable) {
256 status = AudioSystem::setEffectEnabled(mId, enabled);
257 }
258 mPolicyLock.unlock();
259
260 return status;
261}
262
263
Eric Laurent41709552019-12-16 19:34:05 -0800264ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800265{
266 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800267 return removeHandle_l(handle);
268}
269
Eric Laurent41709552019-12-16 19:34:05 -0800270ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800271{
Eric Laurentca7cc822012-11-19 14:55:58 -0800272 size_t size = mHandles.size();
273 size_t i;
274 for (i = 0; i < size; i++) {
275 if (mHandles[i] == handle) {
276 break;
277 }
278 }
279 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800280 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
281 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800282 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800283 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800284
285 mHandles.removeAt(i);
286 // if removed from first place, move effect control from this handle to next in line
287 if (i == 0) {
288 EffectHandle *h = controlHandle_l();
289 if (h != NULL) {
290 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
291 }
292 }
293
Eric Laurentca7cc822012-11-19 14:55:58 -0800294 if (mHandles.size() == 0 && !mPinned) {
295 mState = DESTROYED;
296 }
297
298 return mHandles.size();
299}
300
301// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800302AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800303{
304 // the first valid handle in the list has control over the module
305 for (size_t i = 0; i < mHandles.size(); i++) {
306 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800307 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800308 return h;
309 }
310 }
311
312 return NULL;
313}
314
Eric Laurentf10c7092016-12-06 17:09:56 -0800315// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800316ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800317{
318 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800319 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
320 return mHandles.size();
321 }
322
Eric Laurentf10c7092016-12-06 17:09:56 -0800323 Mutex::Autolock _l(mLock);
324 ssize_t numHandles = removeHandle_l(handle);
325 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800326 mLock.unlock();
327 mCallback->updateOrphanEffectChains(this);
328 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800329 }
330 return numHandles;
331}
332
Eric Laurent41709552019-12-16 19:34:05 -0800333bool AudioFlinger::EffectBase::purgeHandles()
334{
335 bool enabled = false;
336 Mutex::Autolock _l(mLock);
337 EffectHandle *handle = controlHandle_l();
338 if (handle != NULL) {
339 enabled = handle->enabled();
340 }
341 mHandles.clear();
342 return enabled;
343}
344
345void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
346 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
347}
348
349static String8 effectFlagsToString(uint32_t flags) {
350 String8 s;
351
352 s.append("conn. mode: ");
353 switch (flags & EFFECT_FLAG_TYPE_MASK) {
354 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
355 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
356 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
357 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
358 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
359 default: s.append("unknown/reserved"); break;
360 }
361 s.append(", ");
362
363 s.append("insert pref: ");
364 switch (flags & EFFECT_FLAG_INSERT_MASK) {
365 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
366 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
367 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
368 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
369 default: s.append("unknown/reserved"); break;
370 }
371 s.append(", ");
372
373 s.append("volume mgmt: ");
374 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
375 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
376 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
377 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
378 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
379 default: s.append("unknown/reserved"); break;
380 }
381 s.append(", ");
382
383 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
384 if (devind) {
385 s.append("device indication: ");
386 switch (devind) {
387 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
388 default: s.append("unknown/reserved"); break;
389 }
390 s.append(", ");
391 }
392
393 s.append("input mode: ");
394 switch (flags & EFFECT_FLAG_INPUT_MASK) {
395 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
396 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
397 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
398 default: s.append("not set"); break;
399 }
400 s.append(", ");
401
402 s.append("output mode: ");
403 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
404 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
405 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
406 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
407 default: s.append("not set"); break;
408 }
409 s.append(", ");
410
411 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
412 if (accel) {
413 s.append("hardware acceleration: ");
414 switch (accel) {
415 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
416 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
417 default: s.append("unknown/reserved"); break;
418 }
419 s.append(", ");
420 }
421
422 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
423 if (modeind) {
424 s.append("mode indication: ");
425 switch (modeind) {
426 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
427 default: s.append("unknown/reserved"); break;
428 }
429 s.append(", ");
430 }
431
432 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
433 if (srcind) {
434 s.append("source indication: ");
435 switch (srcind) {
436 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
437 default: s.append("unknown/reserved"); break;
438 }
439 s.append(", ");
440 }
441
442 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
443 s.append("offloadable, ");
444 }
445
446 int len = s.length();
447 if (s.length() > 2) {
448 (void) s.lockBuffer(len);
449 s.unlockBuffer(len - 2);
450 }
451 return s;
452}
453
454void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
455{
456 String8 result;
457
458 result.appendFormat("\tEffect ID %d:\n", mId);
459
460 bool locked = AudioFlinger::dumpTryLock(mLock);
461 // failed to lock - AudioFlinger is probably deadlocked
462 if (!locked) {
463 result.append("\t\tCould not lock Fx mutex:\n");
464 }
465
466 result.append("\t\tSession State Registered Enabled Suspended:\n");
467 result.appendFormat("\t\t%05d %03d %s %s %s\n",
468 mSessionId, mState, mPolicyRegistered ? "y" : "n",
469 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
470
471 result.append("\t\tDescriptor:\n");
472 char uuidStr[64];
473 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
474 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
475 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
476 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
477 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
478 mDescriptor.apiVersion,
479 mDescriptor.flags,
480 effectFlagsToString(mDescriptor.flags).string());
481 result.appendFormat("\t\t- name: %s\n",
482 mDescriptor.name);
483
484 result.appendFormat("\t\t- implementor: %s\n",
485 mDescriptor.implementor);
486
487 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
488 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
489 char buffer[256];
490 for (size_t i = 0; i < mHandles.size(); ++i) {
491 EffectHandle *handle = mHandles[i];
492 if (handle != NULL && !handle->disconnected()) {
493 handle->dumpToBuffer(buffer, sizeof(buffer));
494 result.append(buffer);
495 }
496 }
497 if (locked) {
498 mLock.unlock();
499 }
500
501 write(fd, result.string(), result.length());
502}
503
504// ----------------------------------------------------------------------------
505// EffectModule implementation
506// ----------------------------------------------------------------------------
507
508#undef LOG_TAG
509#define LOG_TAG "AudioFlinger::EffectModule"
510
511AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
512 effect_descriptor_t *desc,
513 int id,
514 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800515 bool pinned,
516 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800517 : EffectBase(callback, desc, id, sessionId, pinned),
518 // clear mConfig to ensure consistent initial value of buffer framecount
519 // in case buffers are associated by setInBuffer() or setOutBuffer()
520 // prior to configure().
521 mConfig{{}, {}},
522 mStatus(NO_INIT),
523 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
524 mDisableWaitCnt(0), // set by process() and updateState()
525 mOffloaded(false)
526#ifdef FLOAT_EFFECT_CHAIN
527 , mSupportsFloat(false)
528#endif
529{
530 ALOGV("Constructor %p pinned %d", this, pinned);
531 int lStatus;
532
533 // create effect engine from effect factory
534 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800535 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800536 if (mStatus != NO_ERROR) {
537 return;
538 }
539 lStatus = init();
540 if (lStatus < 0) {
541 mStatus = lStatus;
542 goto Error;
543 }
544
545 setOffloaded(callback->isOffload(), callback->io());
546 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
547
548 return;
549Error:
550 mEffectInterface.clear();
551 ALOGV("Constructor Error %d", mStatus);
552}
553
554AudioFlinger::EffectModule::~EffectModule()
555{
556 ALOGV("Destructor %p", this);
557 if (mEffectInterface != 0) {
558 char uuidStr[64];
559 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
560 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
561 this, uuidStr);
562 release_l();
563 }
564
565}
566
567ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
568{
569 ssize_t status = EffectBase::removeHandle_l(handle);
570
571 // Prevent calls to process() and other functions on effect interface from now on.
572 // The effect engine will be released by the destructor when the last strong reference on
573 // this object is released which can happen after next process is called.
574 if (status == 0 && !mPinned) {
575 mEffectInterface->close();
576 }
577
578 return status;
579}
580
Eric Laurentfa1e1232016-08-02 19:01:49 -0700581bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800582 Mutex::Autolock _l(mLock);
583
Eric Laurentfa1e1232016-08-02 19:01:49 -0700584 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800585 switch (mState) {
586 case RESTART:
587 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700588 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800589
590 case STARTING:
591 // clear auxiliary effect input buffer for next accumulation
592 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
593 memset(mConfig.inputCfg.buffer.raw,
594 0,
595 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
596 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700597 if (start_l() == NO_ERROR) {
598 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700599 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700600 } else {
601 mState = IDLE;
602 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800603 break;
604 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900605 // volume control for offload and direct threads must take effect immediately.
606 if (stop_l() == NO_ERROR
607 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700608 mDisableWaitCnt = mMaxDisableWaitCnt;
609 } else {
610 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
611 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800612 mState = STOPPED;
613 break;
614 case STOPPED:
615 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
616 // turn off sequence.
617 if (--mDisableWaitCnt == 0) {
618 reset_l();
619 mState = IDLE;
620 }
621 break;
622 default: //IDLE , ACTIVE, DESTROYED
623 break;
624 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700625
626 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800627}
628
629void AudioFlinger::EffectModule::process()
630{
631 Mutex::Autolock _l(mLock);
632
Mikhail Naganov022b9952017-01-04 16:36:51 -0800633 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800634 return;
635 }
636
rago94a1ee82017-07-21 15:11:02 -0700637 const uint32_t inChannelCount =
638 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
639 const uint32_t outChannelCount =
640 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
641 const bool auxType =
642 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
643
Andy Hungfa69ca32017-11-30 10:07:53 -0800644 // safeInputOutputSampleCount is 0 if the channel count between input and output
645 // buffers do not match. This prevents automatic accumulation or copying between the
646 // input and output effect buffers without an intermediary effect process.
647 // TODO: consider implementing channel conversion.
648 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700649 mInChannelCountRequested != mOutChannelCountRequested ? 0
650 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800651 mConfig.inputCfg.buffer.frameCount,
652 mConfig.outputCfg.buffer.frameCount);
653 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
654#ifdef FLOAT_EFFECT_CHAIN
655 accumulate_float(
656 mConfig.outputCfg.buffer.f32,
657 mConfig.inputCfg.buffer.f32,
658 safeInputOutputSampleCount);
659#else
660 accumulate_i16(
661 mConfig.outputCfg.buffer.s16,
662 mConfig.inputCfg.buffer.s16,
663 safeInputOutputSampleCount);
664#endif
665 };
666 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
667#ifdef FLOAT_EFFECT_CHAIN
668 memcpy(
669 mConfig.outputCfg.buffer.f32,
670 mConfig.inputCfg.buffer.f32,
671 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
672
673#else
674 memcpy(
675 mConfig.outputCfg.buffer.s16,
676 mConfig.inputCfg.buffer.s16,
677 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
678#endif
679 };
680
Eric Laurentca7cc822012-11-19 14:55:58 -0800681 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700682 int ret;
683 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700684 if (auxType) {
685 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800686 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700687#ifdef FLOAT_EFFECT_CHAIN
688 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800689#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700690 // Do in-place float conversion for auxiliary effect input buffer.
691 static_assert(sizeof(float) <= sizeof(int32_t),
692 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
693
Andy Hungfa69ca32017-11-30 10:07:53 -0800694 memcpy_to_float_from_q4_27(
695 mConfig.inputCfg.buffer.f32,
696 mConfig.inputCfg.buffer.s32,
697 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800698#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800699 } else
Andy Hung116a4982017-11-30 10:15:08 -0800700#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800701 {
Andy Hung116a4982017-11-30 10:15:08 -0800702#ifdef FLOAT_AUX
703 memcpy_to_i16_from_float(
704 mConfig.inputCfg.buffer.s16,
705 mConfig.inputCfg.buffer.f32,
706 mConfig.inputCfg.buffer.frameCount);
707#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800708 memcpy_to_i16_from_q4_27(
709 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700710 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800711 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800712#endif
rago94a1ee82017-07-21 15:11:02 -0700713 }
rago94a1ee82017-07-21 15:11:02 -0700714 }
715#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800716 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
717 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
718
719 if (!auxType && mInChannelCountRequested != inChannelCount) {
720 adjust_channels(
721 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
722 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
723 sizeof(float),
724 sizeof(float)
725 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
726 inBuffer = mInConversionBuffer;
727 }
728 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
729 && mOutChannelCountRequested != outChannelCount) {
730 adjust_selected_channels(
731 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
732 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
733 sizeof(float),
734 sizeof(float)
735 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
736 outBuffer = mOutConversionBuffer;
737 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800738 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
739 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800740 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800741 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
742 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700743 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800744 memcpy_to_i16_from_float(
745 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800746 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800747 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800748 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700749 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800750 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800751 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800752 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
753 goto data_bypass;
754 }
755 memcpy_to_i16_from_float(
756 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800757 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800758 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800759 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700760 }
761 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800762#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800763 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800764#ifdef FLOAT_EFFECT_CHAIN
765 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800766 sp<EffectBufferHalInterface> target =
767 mOutChannelCountRequested != outChannelCount
768 ? mOutConversionBuffer : mOutBuffer;
769
Andy Hungfa69ca32017-11-30 10:07:53 -0800770 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800771 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800772 mOutConversionBuffer->audioBuffer()->s16,
773 outChannelCount * mConfig.outputCfg.buffer.frameCount);
774 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800775 if (mOutChannelCountRequested != outChannelCount) {
776 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
777 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
778 sizeof(float),
779 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
780 }
rago94a1ee82017-07-21 15:11:02 -0700781#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700782 } else {
rago94a1ee82017-07-21 15:11:02 -0700783#ifdef FLOAT_EFFECT_CHAIN
784 data_bypass:
785#endif
786 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800787 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700788 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800789 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700790 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800791 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700792 }
793 }
794 ret = -ENODATA;
795 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800796
Eric Laurentca7cc822012-11-19 14:55:58 -0800797 // force transition to IDLE state when engine is ready
798 if (mState == STOPPED && ret == -ENODATA) {
799 mDisableWaitCnt = 1;
800 }
801
802 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700803 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800804#ifdef FLOAT_AUX
805 const size_t size =
806 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
807#else
rago94a1ee82017-07-21 15:11:02 -0700808 const size_t size =
809 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800810#endif
rago94a1ee82017-07-21 15:11:02 -0700811 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800812 }
813 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700814 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800815 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
816 // If an insert effect is idle and input buffer is different from output buffer,
817 // accumulate input onto output
Eric Laurent6b446ce2019-12-13 10:56:31 -0800818 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700819 // similar handling with data_bypass above.
820 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
821 accumulateInputToOutput();
822 } else { // EFFECT_BUFFER_ACCESS_WRITE
823 copyInputToOutput();
824 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800825 }
826 }
827}
828
829void AudioFlinger::EffectModule::reset_l()
830{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700831 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800832 return;
833 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700834 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800835}
836
837status_t AudioFlinger::EffectModule::configure()
838{
rago94a1ee82017-07-21 15:11:02 -0700839 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700840 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700841 uint32_t size;
842 audio_channel_mask_t channelMask;
843
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700844 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700845 status = NO_INIT;
846 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800847 }
848
Eric Laurentca7cc822012-11-19 14:55:58 -0800849 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800850 // TODO: handle configuration of input (record) SW effects above the HAL,
851 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
852 // in which case input channel masks should be used here.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800853 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800854 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700855 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800856
857 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800858 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
859 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
860 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
861 mConfig.inputCfg.channels);
862 }
863#ifndef MULTICHANNEL_EFFECT_CHAIN
864 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
865 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
866 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
867 mConfig.outputCfg.channels);
868 }
869#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800870 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800871#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700872 // TODO: Update this logic when multichannel effects are implemented.
873 // For offloaded tracks consider mono output as stereo for proper effect initialization
874 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
875 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
876 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
877 ALOGV("Overriding effect input and output as STEREO");
878 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800879#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800880 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800881 mInChannelCountRequested =
882 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
883 mOutChannelCountRequested =
884 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700885
rago94a1ee82017-07-21 15:11:02 -0700886 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
887 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900888
889 // Don't use sample rate for thread if effect isn't offloadable.
Daniel Bonnevier6bc62092019-12-06 09:14:56 +0100890 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900891 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
892 ALOGV("Overriding effect input as 48kHz");
893 } else {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800894 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900895 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800896 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
897 mConfig.inputCfg.bufferProvider.cookie = NULL;
898 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
899 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
900 mConfig.outputCfg.bufferProvider.cookie = NULL;
901 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
902 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
903 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
904 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800905 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800906 // always overwrites output buffer: input buffer == output buffer
907 // - in other sessions:
908 // last effect in the chain accumulates in output buffer: input buffer != output buffer
909 // other effect: overwrites output buffer: input buffer == output buffer
910 // Auxiliary effect:
911 // accumulates in output buffer: input buffer != output buffer
912 // Therefore: accumulate <=> input buffer != output buffer
913 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
914 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
915 } else {
916 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
917 }
918 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
919 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800920 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800921 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
922
Eric Laurent6b446ce2019-12-13 10:56:31 -0800923 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800924 this, mCallback->chain().promote().get(),
925 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800926
927 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700928 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700929 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800930 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700931 &mConfig,
932 &size,
933 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700934 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800935 status = cmdStatus;
936 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800937
938#ifdef MULTICHANNEL_EFFECT_CHAIN
939 if (status != NO_ERROR &&
Eric Laurent6b446ce2019-12-13 10:56:31 -0800940 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800941 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
942 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
943 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700944 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
945 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800946 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
947 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
948 }
949 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
950 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
951 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
952 }
953 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700954 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800955 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700956 &mConfig,
957 &size,
958 &cmdStatus);
959 if (status == NO_ERROR) {
960 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800961 }
962 }
963#endif
964
965#ifdef FLOAT_EFFECT_CHAIN
966 if (status == NO_ERROR) {
967 mSupportsFloat = true;
968 }
969
970 if (status != NO_ERROR) {
971 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
972 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
973 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
974 size = sizeof(int);
975 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
976 sizeof(mConfig),
977 &mConfig,
978 &size,
979 &cmdStatus);
980 if (status == NO_ERROR) {
981 status = cmdStatus;
982 }
983 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700984 mSupportsFloat = false;
985 ALOGVV("config worked with 16 bit");
986 } else {
987 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800988 }
rago94a1ee82017-07-21 15:11:02 -0700989 }
990#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800991
rago94a1ee82017-07-21 15:11:02 -0700992 if (status == NO_ERROR) {
993 // Establish Buffer strategy
994 setInBuffer(mInBuffer);
995 setOutBuffer(mOutBuffer);
996
997 // Update visualizer latency
998 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
999 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1000 effect_param_t *p = (effect_param_t *)buf32;
1001
1002 p->psize = sizeof(uint32_t);
1003 p->vsize = sizeof(uint32_t);
1004 size = sizeof(int);
1005 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1006
Eric Laurent6b446ce2019-12-13 10:56:31 -08001007 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -07001008
1009 *((int32_t *)p->data + 1)= latency;
1010 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1011 sizeof(effect_param_t) + 8,
1012 &buf32,
1013 &size,
1014 &cmdStatus);
1015 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001016 }
1017
Andy Hung05083ac2017-12-14 15:00:28 -08001018 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1019 mMaxDisableWaitCnt = (uint32_t)std::max(
1020 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1021 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1022 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001023
Eric Laurentd0ebb532013-04-02 16:41:41 -07001024exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001025 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001026 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001027 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001028 return status;
1029}
1030
1031status_t AudioFlinger::EffectModule::init()
1032{
1033 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001034 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001035 return NO_INIT;
1036 }
1037 status_t cmdStatus;
1038 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001039 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1040 0,
1041 NULL,
1042 &size,
1043 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001044 if (status == 0) {
1045 status = cmdStatus;
1046 }
1047 return status;
1048}
1049
Eric Laurent1b928682014-10-02 19:41:47 -07001050void AudioFlinger::EffectModule::addEffectToHal_l()
1051{
1052 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1053 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001054 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001055 }
1056}
1057
Eric Laurentfa1e1232016-08-02 19:01:49 -07001058// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001059status_t AudioFlinger::EffectModule::start()
1060{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001061 status_t status;
1062 {
1063 Mutex::Autolock _l(mLock);
1064 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001065 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001066 if (status == NO_ERROR) {
1067 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001068 }
1069 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001070}
1071
1072status_t AudioFlinger::EffectModule::start_l()
1073{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001074 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 return NO_INIT;
1076 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001077 if (mStatus != NO_ERROR) {
1078 return mStatus;
1079 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 status_t cmdStatus;
1081 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001082 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1083 0,
1084 NULL,
1085 &size,
1086 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 if (status == 0) {
1088 status = cmdStatus;
1089 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001090 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001091 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001092 }
1093 return status;
1094}
1095
1096status_t AudioFlinger::EffectModule::stop()
1097{
1098 Mutex::Autolock _l(mLock);
1099 return stop_l();
1100}
1101
1102status_t AudioFlinger::EffectModule::stop_l()
1103{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001104 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001105 return NO_INIT;
1106 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001107 if (mStatus != NO_ERROR) {
1108 return mStatus;
1109 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001110 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001111 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001112
1113 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001114 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1115 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1116 mSetVolumeReentrantTid = gettid();
Eric Laurent6b446ce2019-12-13 10:56:31 -08001117 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001118 mSetVolumeReentrantTid = INVALID_PID;
1119 }
1120
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001121 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1122 0,
1123 NULL,
1124 &size,
1125 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001126 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001127 status = cmdStatus;
1128 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001129 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001130 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001131 }
1132 return status;
1133}
1134
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001135// must be called with EffectChain::mLock held
1136void AudioFlinger::EffectModule::release_l()
1137{
1138 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001139 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001140 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001141 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001142 mEffectInterface.clear();
1143 }
1144}
1145
Eric Laurent6b446ce2019-12-13 10:56:31 -08001146status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001147{
1148 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1149 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001150 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001151 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001152 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001153}
1154
Andy Hunge4a1d912016-08-17 14:11:13 -07001155// round up delta valid if value and divisor are positive.
1156template <typename T>
1157static T roundUpDelta(const T &value, const T &divisor) {
1158 T remainder = value % divisor;
1159 return remainder == 0 ? 0 : divisor - remainder;
1160}
1161
Eric Laurentca7cc822012-11-19 14:55:58 -08001162status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
1163 uint32_t cmdSize,
1164 void *pCmdData,
1165 uint32_t *replySize,
1166 void *pReplyData)
1167{
1168 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001169 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001170
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001171 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001172 return NO_INIT;
1173 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001174 if (mStatus != NO_ERROR) {
1175 return mStatus;
1176 }
Andy Hung110bc952016-06-20 15:22:52 -07001177 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -07001178 (sizeof(effect_param_t) > cmdSize ||
1179 ((effect_param_t *)pCmdData)->psize > cmdSize
1180 - sizeof(effect_param_t))) {
1181 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001182 android_errorWriteLog(0x534e4554, "33003822");
1183 return -EINVAL;
1184 }
1185 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1186 (*replySize < sizeof(effect_param_t) ||
1187 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
1188 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001189 return -EINVAL;
1190 }
ragoe2759072016-11-22 18:02:48 -08001191 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1192 (sizeof(effect_param_t) > *replySize
1193 || ((effect_param_t *)pCmdData)->psize > *replySize
1194 - sizeof(effect_param_t)
1195 || ((effect_param_t *)pCmdData)->vsize > *replySize
1196 - sizeof(effect_param_t)
1197 - ((effect_param_t *)pCmdData)->psize
1198 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1199 *replySize
1200 - sizeof(effect_param_t)
1201 - ((effect_param_t *)pCmdData)->psize
1202 - ((effect_param_t *)pCmdData)->vsize)) {
1203 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1204 android_errorWriteLog(0x534e4554, "32705438");
1205 return -EINVAL;
1206 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001207 if ((cmdCode == EFFECT_CMD_SET_PARAM
1208 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
1209 (sizeof(effect_param_t) > cmdSize
1210 || ((effect_param_t *)pCmdData)->psize > cmdSize
1211 - sizeof(effect_param_t)
1212 || ((effect_param_t *)pCmdData)->vsize > cmdSize
1213 - sizeof(effect_param_t)
1214 - ((effect_param_t *)pCmdData)->psize
1215 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1216 cmdSize
1217 - sizeof(effect_param_t)
1218 - ((effect_param_t *)pCmdData)->psize
1219 - ((effect_param_t *)pCmdData)->vsize)) {
1220 android_errorWriteLog(0x534e4554, "30204301");
1221 return -EINVAL;
1222 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001223 status_t status = mEffectInterface->command(cmdCode,
1224 cmdSize,
1225 pCmdData,
1226 replySize,
1227 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001228 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
1229 uint32_t size = (replySize == NULL) ? 0 : *replySize;
1230 for (size_t i = 1; i < mHandles.size(); i++) {
1231 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001232 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001233 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
1234 }
1235 }
1236 }
1237 return status;
1238}
1239
Eric Laurentca7cc822012-11-19 14:55:58 -08001240bool AudioFlinger::EffectModule::isProcessEnabled() const
1241{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001242 if (mStatus != NO_ERROR) {
1243 return false;
1244 }
1245
Eric Laurentca7cc822012-11-19 14:55:58 -08001246 switch (mState) {
1247 case RESTART:
1248 case ACTIVE:
1249 case STOPPING:
1250 case STOPPED:
1251 return true;
1252 case IDLE:
1253 case STARTING:
1254 case DESTROYED:
1255 default:
1256 return false;
1257 }
1258}
1259
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001260bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1261{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001262 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001263}
1264
1265bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1266{
1267 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1268}
1269
Mikhail Naganov022b9952017-01-04 16:36:51 -08001270void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001271 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001272
1273 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001274 if (buffer != 0) {
1275 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1276 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1277 } else {
1278 mConfig.inputCfg.buffer.raw = NULL;
1279 }
1280 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001281 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001282
1283#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001284 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001285 // Theoretically insert effects can also do in-place conversions (destroying
1286 // the original buffer) when the output buffer is identical to the input buffer,
1287 // but we don't optimize for it here.
1288 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001289 const uint32_t inChannelCount =
1290 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1291 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001292 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001293 // we need to translate - create hidl shared buffer and intercept
1294 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001295 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1296 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1297 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001298
1299 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1300 __func__, inChannels, inFrameCount, size);
1301
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001302 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001303 || size > mInConversionBuffer->getSize())) {
1304 mInConversionBuffer.clear();
1305 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001306 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001307 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001308 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001309 mInConversionBuffer->setFrameCount(inFrameCount);
1310 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001311 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001312 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001313 }
1314 }
1315#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001316}
1317
1318void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001319 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001320
1321 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001322 if (buffer != 0) {
1323 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1324 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1325 } else {
1326 mConfig.outputCfg.buffer.raw = NULL;
1327 }
1328 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001329 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001330
1331#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001332 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001333 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001334 const uint32_t outChannelCount =
1335 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1336 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001337 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001338 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001339 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1340 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1341 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001342
1343 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1344 __func__, outChannels, outFrameCount, size);
1345
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001346 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001347 || size > mOutConversionBuffer->getSize())) {
1348 mOutConversionBuffer.clear();
1349 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001350 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001351 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001352 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001353 mOutConversionBuffer->setFrameCount(outFrameCount);
1354 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001355 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001356 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001357 }
1358 }
1359#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001360}
1361
Eric Laurentca7cc822012-11-19 14:55:58 -08001362status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1363{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001364 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001365 if (mStatus != NO_ERROR) {
1366 return mStatus;
1367 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001368 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001369 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1370 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1371 if (isProcessEnabled() &&
1372 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001373 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1374 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001375 uint32_t volume[2];
1376 uint32_t *pVolume = NULL;
1377 uint32_t size = sizeof(volume);
1378 volume[0] = *left;
1379 volume[1] = *right;
1380 if (controller) {
1381 pVolume = volume;
1382 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001383 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1384 size,
1385 volume,
1386 &size,
1387 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001388 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1389 *left = volume[0];
1390 *right = volume[1];
1391 }
1392 }
1393 return status;
1394}
1395
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001396void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1397{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001398 if (mEffectCallback->isOffloadOrDirect() && !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001399 float vol_l = (float)left / (1 << 24);
1400 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001401 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001402 }
1403}
1404
jiabin8f278ee2019-11-11 12:16:27 -08001405status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1406 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001407{
jiabin8f278ee2019-11-11 12:16:27 -08001408 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1409 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 return NO_ERROR;
1411 }
1412
1413 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001414 if (mStatus != NO_ERROR) {
1415 return mStatus;
1416 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001418 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 status_t cmdStatus;
1420 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001421 // FIXME: use audio device types and addresses when the hal interface is ready.
1422 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001423 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001424 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001425 &size,
1426 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 }
1428 return status;
1429}
1430
jiabin8f278ee2019-11-11 12:16:27 -08001431status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1432{
1433 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1434}
1435
1436status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1437{
1438 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1439}
1440
Eric Laurentca7cc822012-11-19 14:55:58 -08001441status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1442{
1443 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001444 if (mStatus != NO_ERROR) {
1445 return mStatus;
1446 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001447 status_t status = NO_ERROR;
1448 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1449 status_t cmdStatus;
1450 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001451 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1452 sizeof(audio_mode_t),
1453 &mode,
1454 &size,
1455 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001456 if (status == NO_ERROR) {
1457 status = cmdStatus;
1458 }
1459 }
1460 return status;
1461}
1462
1463status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1464{
1465 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001466 if (mStatus != NO_ERROR) {
1467 return mStatus;
1468 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001469 status_t status = NO_ERROR;
1470 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1471 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001472 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1473 sizeof(audio_source_t),
1474 &source,
1475 &size,
1476 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001477 }
1478 return status;
1479}
1480
Eric Laurent5baf2af2013-09-12 17:37:00 -07001481status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1482{
1483 Mutex::Autolock _l(mLock);
1484 if (mStatus != NO_ERROR) {
1485 return mStatus;
1486 }
1487 status_t status = NO_ERROR;
1488 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1489 status_t cmdStatus;
1490 uint32_t size = sizeof(status_t);
1491 effect_offload_param_t cmd;
1492
1493 cmd.isOffload = offloaded;
1494 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001495 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1496 sizeof(effect_offload_param_t),
1497 &cmd,
1498 &size,
1499 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001500 if (status == NO_ERROR) {
1501 status = cmdStatus;
1502 }
1503 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1504 } else {
1505 if (offloaded) {
1506 status = INVALID_OPERATION;
1507 }
1508 mOffloaded = false;
1509 }
1510 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1511 return status;
1512}
1513
1514bool AudioFlinger::EffectModule::isOffloaded() const
1515{
1516 Mutex::Autolock _l(mLock);
1517 return mOffloaded;
1518}
1519
Andy Hungbded9c82017-11-30 18:47:35 -08001520static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1521 std::stringstream ss;
1522
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001523 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001524 return "nullptr"; // make different than below
1525 } else if (buffer->externalData() != nullptr) {
1526 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1527 << " -> "
1528 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1529 } else {
1530 ss << buffer->audioBuffer()->raw;
1531 }
1532 return ss.str();
1533}
Marco Nelissenb2208842014-02-07 14:00:50 -08001534
Eric Laurent41709552019-12-16 19:34:05 -08001535void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001536{
Eric Laurent41709552019-12-16 19:34:05 -08001537 EffectBase::dump(fd, args);
1538
Eric Laurentca7cc822012-11-19 14:55:58 -08001539 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001540 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001541
Eric Laurent41709552019-12-16 19:34:05 -08001542 result.append("\t\tStatus Engine:\n");
1543 result.appendFormat("\t\t%03d %p\n",
1544 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001545
1546 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001547
1548 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001549 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1550 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1551 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001552 mConfig.inputCfg.buffer.frameCount,
1553 mConfig.inputCfg.samplingRate,
1554 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001555 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001556 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001557
1558 result.append("\t\t- Output configuration:\n");
1559 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001560 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001561 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001562 mConfig.outputCfg.buffer.frameCount,
1563 mConfig.outputCfg.samplingRate,
1564 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001565 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001566 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001567
rago94a1ee82017-07-21 15:11:02 -07001568#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001569
Andy Hungbded9c82017-11-30 18:47:35 -08001570 result.appendFormat("\t\t- HAL buffers:\n"
1571 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1572 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1573 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1574 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1575 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001576#endif
1577
Eric Laurentca7cc822012-11-19 14:55:58 -08001578 write(fd, result.string(), result.length());
1579
Mikhail Naganov4d547672019-02-22 14:19:19 -08001580 if (mEffectInterface != 0) {
1581 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1582 (void)mEffectInterface->dump(fd);
1583 }
1584
Eric Laurentca7cc822012-11-19 14:55:58 -08001585 if (locked) {
1586 mLock.unlock();
1587 }
1588}
1589
1590// ----------------------------------------------------------------------------
1591// EffectHandle implementation
1592// ----------------------------------------------------------------------------
1593
1594#undef LOG_TAG
1595#define LOG_TAG "AudioFlinger::EffectHandle"
1596
Eric Laurent41709552019-12-16 19:34:05 -08001597AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08001598 const sp<AudioFlinger::Client>& client,
1599 const sp<IEffectClient>& effectClient,
1600 int32_t priority)
1601 : BnEffect(),
1602 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001603 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001604{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001605 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001606
1607 if (client == 0) {
1608 return;
1609 }
1610 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1611 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001612 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001613 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001614 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001615 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001616 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001617 return;
1618 }
Glenn Kastene75da402013-11-20 13:54:52 -08001619 new(mCblk) effect_param_cblk_t();
1620 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001621}
1622
1623AudioFlinger::EffectHandle::~EffectHandle()
1624{
1625 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001626 disconnect(false);
1627}
1628
Glenn Kastene75da402013-11-20 13:54:52 -08001629status_t AudioFlinger::EffectHandle::initCheck()
1630{
1631 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1632}
1633
Eric Laurentca7cc822012-11-19 14:55:58 -08001634status_t AudioFlinger::EffectHandle::enable()
1635{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001636 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001637 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001638 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001639 if (effect == 0 || mDisconnected) {
1640 return DEAD_OBJECT;
1641 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001642 if (!mHasControl) {
1643 return INVALID_OPERATION;
1644 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001645
1646 if (mEnabled) {
1647 return NO_ERROR;
1648 }
1649
1650 mEnabled = true;
1651
Eric Laurent6c796322019-04-09 14:13:17 -07001652 status_t status = effect->updatePolicyState();
1653 if (status != NO_ERROR) {
1654 mEnabled = false;
1655 return status;
1656 }
1657
Eric Laurent6b446ce2019-12-13 10:56:31 -08001658 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001659
1660 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001661 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001662 return NO_ERROR;
1663 }
1664
Eric Laurent6b446ce2019-12-13 10:56:31 -08001665 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 mEnabled = false;
1668 }
1669 return status;
1670}
1671
1672status_t AudioFlinger::EffectHandle::disable()
1673{
1674 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001675 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001676 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001677 if (effect == 0 || mDisconnected) {
1678 return DEAD_OBJECT;
1679 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001680 if (!mHasControl) {
1681 return INVALID_OPERATION;
1682 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001683
1684 if (!mEnabled) {
1685 return NO_ERROR;
1686 }
1687 mEnabled = false;
1688
Eric Laurent6c796322019-04-09 14:13:17 -07001689 effect->updatePolicyState();
1690
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001691 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001692 return NO_ERROR;
1693 }
1694
Eric Laurent6b446ce2019-12-13 10:56:31 -08001695 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001696 return status;
1697}
1698
1699void AudioFlinger::EffectHandle::disconnect()
1700{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001701 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 disconnect(true);
1703}
1704
1705void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1706{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001707 AutoMutex _l(mLock);
1708 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1709 if (mDisconnected) {
1710 if (unpinIfLast) {
1711 android_errorWriteLog(0x534e4554, "32707507");
1712 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001713 return;
1714 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001715 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001716 {
Eric Laurent41709552019-12-16 19:34:05 -08001717 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001718 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001719 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001720 ALOGW("%s Effect handle %p disconnected after thread destruction",
1721 __func__, this);
1722 }
1723 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001724 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001725 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001726
Eric Laurentca7cc822012-11-19 14:55:58 -08001727 if (mClient != 0) {
1728 if (mCblk != NULL) {
1729 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1730 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1731 }
1732 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001733 // Client destructor must run with AudioFlinger client mutex locked
1734 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001735 mClient.clear();
1736 }
1737}
1738
1739status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1740 uint32_t cmdSize,
1741 void *pCmdData,
1742 uint32_t *replySize,
1743 void *pReplyData)
1744{
1745 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001746 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001747
Eric Laurentc7ab3092017-06-15 18:43:46 -07001748 // reject commands reserved for internal use by audio framework if coming from outside
1749 // of audioserver
1750 switch(cmdCode) {
1751 case EFFECT_CMD_ENABLE:
1752 case EFFECT_CMD_DISABLE:
1753 case EFFECT_CMD_SET_PARAM:
1754 case EFFECT_CMD_SET_PARAM_DEFERRED:
1755 case EFFECT_CMD_SET_PARAM_COMMIT:
1756 case EFFECT_CMD_GET_PARAM:
1757 break;
1758 default:
1759 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1760 break;
1761 }
1762 android_errorWriteLog(0x534e4554, "62019992");
1763 return BAD_VALUE;
1764 }
1765
Eric Laurent1ffc5852016-12-15 14:46:09 -08001766 if (cmdCode == EFFECT_CMD_ENABLE) {
1767 if (*replySize < sizeof(int)) {
1768 android_errorWriteLog(0x534e4554, "32095713");
1769 return BAD_VALUE;
1770 }
1771 *(int *)pReplyData = NO_ERROR;
1772 *replySize = sizeof(int);
1773 return enable();
1774 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1775 if (*replySize < sizeof(int)) {
1776 android_errorWriteLog(0x534e4554, "32095713");
1777 return BAD_VALUE;
1778 }
1779 *(int *)pReplyData = NO_ERROR;
1780 *replySize = sizeof(int);
1781 return disable();
1782 }
1783
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001784 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001785 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001786 if (effect == 0 || mDisconnected) {
1787 return DEAD_OBJECT;
1788 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001789 // only get parameter command is permitted for applications not controlling the effect
1790 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1791 return INVALID_OPERATION;
1792 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001793
1794 // handle commands that are not forwarded transparently to effect engine
1795 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001796 if (mClient == 0) {
1797 return INVALID_OPERATION;
1798 }
1799
Eric Laurent1ffc5852016-12-15 14:46:09 -08001800 if (*replySize < sizeof(int)) {
1801 android_errorWriteLog(0x534e4554, "32095713");
1802 return BAD_VALUE;
1803 }
1804 *(int *)pReplyData = NO_ERROR;
1805 *replySize = sizeof(int);
1806
Eric Laurentca7cc822012-11-19 14:55:58 -08001807 // No need to trylock() here as this function is executed in the binder thread serving a
1808 // particular client process: no risk to block the whole media server process or mixer
1809 // threads if we are stuck here
1810 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001811 // keep local copy of index in case of client corruption b/32220769
1812 const uint32_t clientIndex = mCblk->clientIndex;
1813 const uint32_t serverIndex = mCblk->serverIndex;
1814 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1815 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 mCblk->serverIndex = 0;
1817 mCblk->clientIndex = 0;
1818 return BAD_VALUE;
1819 }
1820 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001821 effect_param_t *param = NULL;
1822 for (uint32_t index = serverIndex; index < clientIndex;) {
1823 int *p = (int *)(mBuffer + index);
1824 const int size = *p++;
1825 if (size < 0
1826 || size > EFFECT_PARAM_BUFFER_SIZE
1827 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001828 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001829 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001830 break;
1831 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001832
1833 // copy to local memory in case of client corruption b/32220769
George Burgess IV80a22162020-01-05 20:06:15 -08001834 auto *newParam = (effect_param_t *)realloc(param, size);
1835 if (newParam == NULL) {
Andy Hunga447a0f2016-11-15 17:19:58 -08001836 ALOGW("command(): out of memory");
1837 status = NO_MEMORY;
1838 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001839 }
George Burgess IV80a22162020-01-05 20:06:15 -08001840 param = newParam;
Andy Hunga447a0f2016-11-15 17:19:58 -08001841 memcpy(param, p, size);
1842
1843 int reply = 0;
1844 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001846 size,
1847 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001848 &rsize,
1849 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001850
1851 // verify shared memory: server index shouldn't change; client index can't go back.
1852 if (serverIndex != mCblk->serverIndex
1853 || clientIndex > mCblk->clientIndex) {
1854 android_errorWriteLog(0x534e4554, "32220769");
1855 status = BAD_VALUE;
1856 break;
1857 }
1858
Eric Laurentca7cc822012-11-19 14:55:58 -08001859 // stop at first error encountered
1860 if (ret != NO_ERROR) {
1861 status = ret;
1862 *(int *)pReplyData = reply;
1863 break;
1864 } else if (reply != NO_ERROR) {
1865 *(int *)pReplyData = reply;
1866 break;
1867 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001868 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001869 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001870 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001871 mCblk->serverIndex = 0;
1872 mCblk->clientIndex = 0;
1873 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001874 }
1875
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001876 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001877}
1878
1879void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1880{
1881 ALOGV("setControl %p control %d", this, hasControl);
1882
1883 mHasControl = hasControl;
1884 mEnabled = enabled;
1885
1886 if (signal && mEffectClient != 0) {
1887 mEffectClient->controlStatusChanged(hasControl);
1888 }
1889}
1890
1891void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1892 uint32_t cmdSize,
1893 void *pCmdData,
1894 uint32_t replySize,
1895 void *pReplyData)
1896{
1897 if (mEffectClient != 0) {
1898 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1899 }
1900}
1901
1902
1903
1904void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1905{
1906 if (mEffectClient != 0) {
1907 mEffectClient->enableStatusChanged(enabled);
1908 }
1909}
1910
1911status_t AudioFlinger::EffectHandle::onTransact(
1912 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1913{
1914 return BnEffect::onTransact(code, data, reply, flags);
1915}
1916
1917
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001918void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001919{
1920 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1921
Marco Nelissenb2208842014-02-07 14:00:50 -08001922 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001923 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001924 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001925 mHasControl ? "yes" : "no",
1926 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 mCblk ? mCblk->clientIndex : 0,
1928 mCblk ? mCblk->serverIndex : 0
1929 );
1930
1931 if (locked) {
1932 mCblk->lock.unlock();
1933 }
1934}
1935
1936#undef LOG_TAG
1937#define LOG_TAG "AudioFlinger::EffectChain"
1938
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001939AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
1940 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08001941 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001942 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08001943 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001944 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08001945{
1946 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001947 sp<ThreadBase> p = thread.promote();
1948 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001949 return;
1950 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001951 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
1952 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08001953}
1954
1955AudioFlinger::EffectChain::~EffectChain()
1956{
Eric Laurentca7cc822012-11-19 14:55:58 -08001957}
1958
1959// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1960sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1961 effect_descriptor_t *descriptor)
1962{
1963 size_t size = mEffects.size();
1964
1965 for (size_t i = 0; i < size; i++) {
1966 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1967 return mEffects[i];
1968 }
1969 }
1970 return 0;
1971}
1972
1973// getEffectFromId_l() must be called with ThreadBase::mLock held
1974sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1975{
1976 size_t size = mEffects.size();
1977
1978 for (size_t i = 0; i < size; i++) {
1979 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1980 if (id == 0 || mEffects[i]->id() == id) {
1981 return mEffects[i];
1982 }
1983 }
1984 return 0;
1985}
1986
1987// getEffectFromType_l() must be called with ThreadBase::mLock held
1988sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1989 const effect_uuid_t *type)
1990{
1991 size_t size = mEffects.size();
1992
1993 for (size_t i = 0; i < size; i++) {
1994 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1995 return mEffects[i];
1996 }
1997 }
1998 return 0;
1999}
2000
Eric Laurent6c796322019-04-09 14:13:17 -07002001std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2002{
2003 std::vector<int> ids;
2004 Mutex::Autolock _l(mLock);
2005 for (size_t i = 0; i < mEffects.size(); i++) {
2006 ids.push_back(mEffects[i]->id());
2007 }
2008 return ids;
2009}
2010
Eric Laurentca7cc822012-11-19 14:55:58 -08002011void AudioFlinger::EffectChain::clearInputBuffer()
2012{
2013 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002014 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002015}
2016
2017// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002018void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002019{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002020 if (mInBuffer == NULL) {
2021 return;
2022 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002023 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002024 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002025
Eric Laurent6b446ce2019-12-13 10:56:31 -08002026 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002027 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002028}
2029
2030// Must be called with EffectChain::mLock locked
2031void AudioFlinger::EffectChain::process_l()
2032{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002033 // never process effects when:
2034 // - on an OFFLOAD thread
2035 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002036 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002037 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002038 bool tracksOnSession = (trackCnt() != 0);
2039
2040 if (!tracksOnSession && mTailBufferCount == 0) {
2041 doProcess = false;
2042 }
2043
2044 if (activeTrackCnt() == 0) {
2045 // if no track is active and the effect tail has not been rendered,
2046 // the input buffer must be cleared here as the mixer process will not do it
2047 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002048 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002049 if (mTailBufferCount > 0) {
2050 mTailBufferCount--;
2051 }
2052 }
2053 }
2054 }
2055
2056 size_t size = mEffects.size();
2057 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002058 // Only the input and output buffers of the chain can be external,
2059 // and 'update' / 'commit' do nothing for allocated buffers, thus
2060 // it's not needed to consider any other buffers here.
2061 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002062 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2063 mOutBuffer->update();
2064 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002065 for (size_t i = 0; i < size; i++) {
2066 mEffects[i]->process();
2067 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002068 mInBuffer->commit();
2069 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2070 mOutBuffer->commit();
2071 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002072 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002073 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002074 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002075 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2076 }
2077 if (doResetVolume) {
2078 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002079 }
2080}
2081
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002082// createEffect_l() must be called with ThreadBase::mLock held
2083status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002084 effect_descriptor_t *desc,
2085 int id,
2086 audio_session_t sessionId,
2087 bool pinned)
2088{
2089 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002090 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002091 status_t lStatus = effect->status();
2092 if (lStatus == NO_ERROR) {
2093 lStatus = addEffect_ll(effect);
2094 }
2095 if (lStatus != NO_ERROR) {
2096 effect.clear();
2097 }
2098 return lStatus;
2099}
2100
2101// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002102status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2103{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002104 Mutex::Autolock _l(mLock);
2105 return addEffect_ll(effect);
2106}
2107// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2108status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2109{
Eric Laurentca7cc822012-11-19 14:55:58 -08002110 effect_descriptor_t desc = effect->desc();
2111 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2112
Eric Laurent6b446ce2019-12-13 10:56:31 -08002113 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002114
2115 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2116 // Auxiliary effects are inserted at the beginning of mEffects vector as
2117 // they are processed first and accumulated in chain input buffer
2118 mEffects.insertAt(effect, 0);
2119
2120 // the input buffer for auxiliary effect contains mono samples in
2121 // 32 bit format. This is to avoid saturation in AudoMixer
2122 // accumulation stage. Saturation is done in EffectModule::process() before
2123 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002124 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002125 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002126#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002127 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002128 numSamples * sizeof(float), &halBuffer);
2129#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002130 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002131 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002132#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002133 if (result != OK) return result;
2134 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002135 // auxiliary effects output samples to chain input buffer for further processing
2136 // by insert effects
2137 effect->setOutBuffer(mInBuffer);
2138 } else {
2139 // Insert effects are inserted at the end of mEffects vector as they are processed
2140 // after track and auxiliary effects.
2141 // Insert effect order as a function of indicated preference:
2142 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2143 // another effect is present
2144 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2145 // last effect claiming first position
2146 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2147 // first effect claiming last position
2148 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2149 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2150 // already present
2151
2152 size_t size = mEffects.size();
2153 size_t idx_insert = size;
2154 ssize_t idx_insert_first = -1;
2155 ssize_t idx_insert_last = -1;
2156
2157 for (size_t i = 0; i < size; i++) {
2158 effect_descriptor_t d = mEffects[i]->desc();
2159 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2160 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2161 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2162 // check invalid effect chaining combinations
2163 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2164 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2165 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2166 desc.name, d.name);
2167 return INVALID_OPERATION;
2168 }
2169 // remember position of first insert effect and by default
2170 // select this as insert position for new effect
2171 if (idx_insert == size) {
2172 idx_insert = i;
2173 }
2174 // remember position of last insert effect claiming
2175 // first position
2176 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2177 idx_insert_first = i;
2178 }
2179 // remember position of first insert effect claiming
2180 // last position
2181 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2182 idx_insert_last == -1) {
2183 idx_insert_last = i;
2184 }
2185 }
2186 }
2187
2188 // modify idx_insert from first position if needed
2189 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2190 if (idx_insert_last != -1) {
2191 idx_insert = idx_insert_last;
2192 } else {
2193 idx_insert = size;
2194 }
2195 } else {
2196 if (idx_insert_first != -1) {
2197 idx_insert = idx_insert_first + 1;
2198 }
2199 }
2200
2201 // always read samples from chain input buffer
2202 effect->setInBuffer(mInBuffer);
2203
2204 // if last effect in the chain, output samples to chain
2205 // output buffer, otherwise to chain input buffer
2206 if (idx_insert == size) {
2207 if (idx_insert != 0) {
2208 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2209 mEffects[idx_insert-1]->configure();
2210 }
2211 effect->setOutBuffer(mOutBuffer);
2212 } else {
2213 effect->setOutBuffer(mInBuffer);
2214 }
2215 mEffects.insertAt(effect, idx_insert);
2216
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002217 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002218 idx_insert);
2219 }
2220 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002221
Eric Laurentca7cc822012-11-19 14:55:58 -08002222 return NO_ERROR;
2223}
2224
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002225// removeEffect_l() must be called with ThreadBase::mLock held
2226size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2227 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002228{
2229 Mutex::Autolock _l(mLock);
2230 size_t size = mEffects.size();
2231 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2232
2233 for (size_t i = 0; i < size; i++) {
2234 if (effect == mEffects[i]) {
2235 // calling stop here will remove pre-processing effect from the audio HAL.
2236 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2237 // the middle of a read from audio HAL
2238 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2239 mEffects[i]->state() == EffectModule::STOPPING) {
2240 mEffects[i]->stop();
2241 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002242 if (release) {
2243 mEffects[i]->release_l();
2244 }
2245
Mikhail Naganov022b9952017-01-04 16:36:51 -08002246 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002247 if (i == size - 1 && i != 0) {
2248 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2249 mEffects[i - 1]->configure();
2250 }
2251 }
2252 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002253 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002254 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002255
Eric Laurentca7cc822012-11-19 14:55:58 -08002256 break;
2257 }
2258 }
2259
2260 return mEffects.size();
2261}
2262
jiabin8f278ee2019-11-11 12:16:27 -08002263// setDevices_l() must be called with ThreadBase::mLock held
2264void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002265{
2266 size_t size = mEffects.size();
2267 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002268 mEffects[i]->setDevices(devices);
2269 }
2270}
2271
2272// setInputDevice_l() must be called with ThreadBase::mLock held
2273void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2274{
2275 size_t size = mEffects.size();
2276 for (size_t i = 0; i < size; i++) {
2277 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002278 }
2279}
2280
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002281// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002282void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2283{
2284 size_t size = mEffects.size();
2285 for (size_t i = 0; i < size; i++) {
2286 mEffects[i]->setMode(mode);
2287 }
2288}
2289
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002290// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002291void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2292{
2293 size_t size = mEffects.size();
2294 for (size_t i = 0; i < size; i++) {
2295 mEffects[i]->setAudioSource(source);
2296 }
2297}
2298
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002299// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002300bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002301{
2302 uint32_t newLeft = *left;
2303 uint32_t newRight = *right;
2304 bool hasControl = false;
2305 int ctrlIdx = -1;
2306 size_t size = mEffects.size();
2307
2308 // first update volume controller
2309 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002310 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002311 ctrlIdx = i - 1;
2312 hasControl = true;
2313 break;
2314 }
2315 }
2316
Eric Laurentfa1e1232016-08-02 19:01:49 -07002317 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002318 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002319 if (hasControl) {
2320 *left = mNewLeftVolume;
2321 *right = mNewRightVolume;
2322 }
2323 return hasControl;
2324 }
2325
2326 mVolumeCtrlIdx = ctrlIdx;
2327 mLeftVolume = newLeft;
2328 mRightVolume = newRight;
2329
2330 // second get volume update from volume controller
2331 if (ctrlIdx >= 0) {
2332 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2333 mNewLeftVolume = newLeft;
2334 mNewRightVolume = newRight;
2335 }
2336 // then indicate volume to all other effects in chain.
2337 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002338 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002339 uint32_t lVol = newLeft;
2340 uint32_t rVol = newRight;
2341
2342 for (size_t i = 0; i < size; i++) {
2343 if ((int)i == ctrlIdx) {
2344 continue;
2345 }
2346 // this also works for ctrlIdx == -1 when there is no volume controller
2347 if ((int)i > ctrlIdx) {
2348 lVol = *left;
2349 rVol = *right;
2350 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002351 // Pass requested volume directly if this is volume monitor module
2352 if (mEffects[i]->isVolumeMonitor()) {
2353 mEffects[i]->setVolume(left, right, false);
2354 } else {
2355 mEffects[i]->setVolume(&lVol, &rVol, false);
2356 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002357 }
2358 *left = newLeft;
2359 *right = newRight;
2360
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002361 setVolumeForOutput_l(*left, *right);
2362
Eric Laurentca7cc822012-11-19 14:55:58 -08002363 return hasControl;
2364}
2365
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002366// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002367void AudioFlinger::EffectChain::resetVolume_l()
2368{
Eric Laurente7449bf2016-08-03 18:44:07 -07002369 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2370 uint32_t left = mLeftVolume;
2371 uint32_t right = mRightVolume;
2372 (void)setVolume_l(&left, &right, true);
2373 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002374}
2375
Eric Laurent1b928682014-10-02 19:41:47 -07002376void AudioFlinger::EffectChain::syncHalEffectsState()
2377{
2378 Mutex::Autolock _l(mLock);
2379 for (size_t i = 0; i < mEffects.size(); i++) {
2380 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2381 mEffects[i]->state() == EffectModule::STOPPING) {
2382 mEffects[i]->addEffectToHal_l();
2383 }
2384 }
2385}
2386
Eric Laurentca7cc822012-11-19 14:55:58 -08002387void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2388{
Eric Laurentca7cc822012-11-19 14:55:58 -08002389 String8 result;
2390
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002391 const size_t numEffects = mEffects.size();
2392 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002393
Marco Nelissenb2208842014-02-07 14:00:50 -08002394 if (numEffects) {
2395 bool locked = AudioFlinger::dumpTryLock(mLock);
2396 // failed to lock - AudioFlinger is probably deadlocked
2397 if (!locked) {
2398 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002399 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002400
Andy Hungbded9c82017-11-30 18:47:35 -08002401 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2402 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2403 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2404 (int)inBufferStr.size(), "In buffer ",
2405 (int)outBufferStr.size(), "Out buffer ");
2406 result.appendFormat("\t%s %s %d\n",
2407 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002408 write(fd, result.string(), result.size());
2409
2410 for (size_t i = 0; i < numEffects; ++i) {
2411 sp<EffectModule> effect = mEffects[i];
2412 if (effect != 0) {
2413 effect->dump(fd, args);
2414 }
2415 }
2416
2417 if (locked) {
2418 mLock.unlock();
2419 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002420 } else {
2421 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002422 }
2423}
2424
2425// must be called with ThreadBase::mLock held
2426void AudioFlinger::EffectChain::setEffectSuspended_l(
2427 const effect_uuid_t *type, bool suspend)
2428{
2429 sp<SuspendedEffectDesc> desc;
2430 // use effect type UUID timelow as key as there is no real risk of identical
2431 // timeLow fields among effect type UUIDs.
2432 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2433 if (suspend) {
2434 if (index >= 0) {
2435 desc = mSuspendedEffects.valueAt(index);
2436 } else {
2437 desc = new SuspendedEffectDesc();
2438 desc->mType = *type;
2439 mSuspendedEffects.add(type->timeLow, desc);
2440 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2441 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002442
Eric Laurentca7cc822012-11-19 14:55:58 -08002443 if (desc->mRefCount++ == 0) {
2444 sp<EffectModule> effect = getEffectIfEnabled(type);
2445 if (effect != 0) {
2446 desc->mEffect = effect;
2447 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002448 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002449 }
2450 }
2451 } else {
2452 if (index < 0) {
2453 return;
2454 }
2455 desc = mSuspendedEffects.valueAt(index);
2456 if (desc->mRefCount <= 0) {
2457 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002458 desc->mRefCount = 0;
2459 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002460 }
2461 if (--desc->mRefCount == 0) {
2462 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2463 if (desc->mEffect != 0) {
2464 sp<EffectModule> effect = desc->mEffect.promote();
2465 if (effect != 0) {
2466 effect->setSuspended(false);
2467 effect->lock();
2468 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002469 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002470 effect->setEnabled_l(handle->enabled());
2471 }
2472 effect->unlock();
2473 }
2474 desc->mEffect.clear();
2475 }
2476 mSuspendedEffects.removeItemsAt(index);
2477 }
2478 }
2479}
2480
2481// must be called with ThreadBase::mLock held
2482void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2483{
2484 sp<SuspendedEffectDesc> desc;
2485
2486 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2487 if (suspend) {
2488 if (index >= 0) {
2489 desc = mSuspendedEffects.valueAt(index);
2490 } else {
2491 desc = new SuspendedEffectDesc();
2492 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2493 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2494 }
2495 if (desc->mRefCount++ == 0) {
2496 Vector< sp<EffectModule> > effects;
2497 getSuspendEligibleEffects(effects);
2498 for (size_t i = 0; i < effects.size(); i++) {
2499 setEffectSuspended_l(&effects[i]->desc().type, true);
2500 }
2501 }
2502 } else {
2503 if (index < 0) {
2504 return;
2505 }
2506 desc = mSuspendedEffects.valueAt(index);
2507 if (desc->mRefCount <= 0) {
2508 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2509 desc->mRefCount = 1;
2510 }
2511 if (--desc->mRefCount == 0) {
2512 Vector<const effect_uuid_t *> types;
2513 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2514 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2515 continue;
2516 }
2517 types.add(&mSuspendedEffects.valueAt(i)->mType);
2518 }
2519 for (size_t i = 0; i < types.size(); i++) {
2520 setEffectSuspended_l(types[i], false);
2521 }
2522 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2523 mSuspendedEffects.keyAt(index));
2524 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2525 }
2526 }
2527}
2528
2529
2530// The volume effect is used for automated tests only
2531#ifndef OPENSL_ES_H_
2532static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2533 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2534const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2535#endif //OPENSL_ES_H_
2536
Eric Laurentd8365c52017-07-16 15:27:05 -07002537/* static */
2538bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2539{
2540 // Only NS and AEC are suspended when BtNRec is off
2541 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2542 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2543 return true;
2544 }
2545 return false;
2546}
2547
Eric Laurentca7cc822012-11-19 14:55:58 -08002548bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2549{
2550 // auxiliary effects and visualizer are never suspended on output mix
2551 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2552 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2553 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002554 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2555 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002556 return false;
2557 }
2558 return true;
2559}
2560
2561void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2562 Vector< sp<AudioFlinger::EffectModule> > &effects)
2563{
2564 effects.clear();
2565 for (size_t i = 0; i < mEffects.size(); i++) {
2566 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2567 effects.add(mEffects[i]);
2568 }
2569 }
2570}
2571
2572sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2573 const effect_uuid_t *type)
2574{
2575 sp<EffectModule> effect = getEffectFromType_l(type);
2576 return effect != 0 && effect->isEnabled() ? effect : 0;
2577}
2578
2579void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2580 bool enabled)
2581{
2582 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2583 if (enabled) {
2584 if (index < 0) {
2585 // if the effect is not suspend check if all effects are suspended
2586 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2587 if (index < 0) {
2588 return;
2589 }
2590 if (!isEffectEligibleForSuspend(effect->desc())) {
2591 return;
2592 }
2593 setEffectSuspended_l(&effect->desc().type, enabled);
2594 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2595 if (index < 0) {
2596 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2597 return;
2598 }
2599 }
2600 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2601 effect->desc().type.timeLow);
2602 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002603 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002604 if (desc->mEffect == 0) {
2605 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002606 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002607 effect->setSuspended(true);
2608 }
2609 } else {
2610 if (index < 0) {
2611 return;
2612 }
2613 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2614 effect->desc().type.timeLow);
2615 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2616 desc->mEffect.clear();
2617 effect->setSuspended(false);
2618 }
2619}
2620
Eric Laurent5baf2af2013-09-12 17:37:00 -07002621bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002622{
2623 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002624 return isNonOffloadableEnabled_l();
2625}
2626
2627bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2628{
Eric Laurent813e2a72013-08-31 12:59:48 -07002629 size_t size = mEffects.size();
2630 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002631 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002632 return true;
2633 }
2634 }
2635 return false;
2636}
2637
Eric Laurentaaa44472014-09-12 17:41:50 -07002638void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2639{
2640 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002641 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002642}
2643
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002644void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2645{
2646 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2647 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2648 }
2649 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2650 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2651 }
2652}
2653
2654void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2655{
2656 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2657 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2658 }
2659 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2660 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2661 }
2662}
2663
2664bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002665{
2666 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002667 for (const auto &effect : mEffects) {
2668 if (effect->isProcessImplemented()) {
2669 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002670 }
2671 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002672 // Allow effects without processing.
2673 return true;
2674}
2675
2676bool AudioFlinger::EffectChain::isFastCompatible() const
2677{
2678 Mutex::Autolock _l(mLock);
2679 for (const auto &effect : mEffects) {
2680 if (effect->isProcessImplemented()
2681 && effect->isImplementationSoftware()) {
2682 return false;
2683 }
2684 }
2685 // Allow effects without processing or hw accelerated effects.
2686 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002687}
2688
2689// isCompatibleWithThread_l() must be called with thread->mLock held
2690bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2691{
2692 Mutex::Autolock _l(mLock);
2693 for (size_t i = 0; i < mEffects.size(); i++) {
2694 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2695 return false;
2696 }
2697 }
2698 return true;
2699}
2700
Eric Laurent6b446ce2019-12-13 10:56:31 -08002701// EffectCallbackInterface implementation
2702status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2703 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2704 sp<EffectHalInterface> *effect) {
2705 status_t status = NO_INIT;
2706 sp<AudioFlinger> af = mAudioFlinger.promote();
2707 if (af == nullptr) {
2708 return status;
2709 }
2710 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2711 if (effectsFactory != 0) {
2712 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2713 }
2714 return status;
2715}
2716
2717bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002718 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002719 sp<AudioFlinger> af = mAudioFlinger.promote();
2720 if (af == nullptr) {
2721 return false;
2722 }
Eric Laurent41709552019-12-16 19:34:05 -08002723 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2724 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002725}
2726
2727status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2728 size_t size, sp<EffectBufferHalInterface>* buffer) {
2729 sp<AudioFlinger> af = mAudioFlinger.promote();
2730 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2731 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2732}
2733
2734status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2735 sp<EffectHalInterface> effect) {
2736 status_t result = NO_INIT;
2737 sp<ThreadBase> t = mThread.promote();
2738 if (t == nullptr) {
2739 return result;
2740 }
2741 sp <StreamHalInterface> st = t->stream();
2742 if (st == nullptr) {
2743 return result;
2744 }
2745 result = st->addEffect(effect);
2746 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2747 return result;
2748}
2749
2750status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2751 sp<EffectHalInterface> effect) {
2752 status_t result = NO_INIT;
2753 sp<ThreadBase> t = mThread.promote();
2754 if (t == nullptr) {
2755 return result;
2756 }
2757 sp <StreamHalInterface> st = t->stream();
2758 if (st == nullptr) {
2759 return result;
2760 }
2761 result = st->removeEffect(effect);
2762 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2763 return result;
2764}
2765
2766audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2767 sp<ThreadBase> t = mThread.promote();
2768 if (t == nullptr) {
2769 return AUDIO_IO_HANDLE_NONE;
2770 }
2771 return t->id();
2772}
2773
2774bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2775 sp<ThreadBase> t = mThread.promote();
2776 if (t == nullptr) {
2777 return true;
2778 }
2779 return t->isOutput();
2780}
2781
2782bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2783 sp<ThreadBase> t = mThread.promote();
2784 if (t == nullptr) {
2785 return false;
2786 }
2787 return t->type() == ThreadBase::OFFLOAD;
2788}
2789
2790bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2791 sp<ThreadBase> t = mThread.promote();
2792 if (t == nullptr) {
2793 return false;
2794 }
2795 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2796}
2797
2798bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2799 sp<ThreadBase> t = mThread.promote();
2800 if (t == nullptr) {
2801 return false;
2802 }
2803 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::MMAP;
2804}
2805
2806uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2807 sp<ThreadBase> t = mThread.promote();
2808 if (t == nullptr) {
2809 return 0;
2810 }
2811 return t->sampleRate();
2812}
2813
2814audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2815 sp<ThreadBase> t = mThread.promote();
2816 if (t == nullptr) {
2817 return AUDIO_CHANNEL_NONE;
2818 }
2819 return t->channelMask();
2820}
2821
2822uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2823 sp<ThreadBase> t = mThread.promote();
2824 if (t == nullptr) {
2825 return 0;
2826 }
2827 return t->channelCount();
2828}
2829
2830size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2831 sp<ThreadBase> t = mThread.promote();
2832 if (t == nullptr) {
2833 return 0;
2834 }
2835 return t->frameCount();
2836}
2837
2838uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2839 sp<ThreadBase> t = mThread.promote();
2840 if (t == nullptr) {
2841 return 0;
2842 }
2843 return t->latency_l();
2844}
2845
2846void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2847 sp<ThreadBase> t = mThread.promote();
2848 if (t == nullptr) {
2849 return;
2850 }
2851 t->setVolumeForOutput_l(left, right);
2852}
2853
2854void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002855 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002856 sp<ThreadBase> t = mThread.promote();
2857 if (t == nullptr) {
2858 return;
2859 }
2860 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2861
2862 sp<EffectChain> c = mChain.promote();
2863 if (c == nullptr) {
2864 return;
2865 }
Eric Laurent41709552019-12-16 19:34:05 -08002866 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2867 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002868}
2869
Eric Laurent41709552019-12-16 19:34:05 -08002870void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002871 sp<ThreadBase> t = mThread.promote();
2872 if (t == nullptr) {
2873 return;
2874 }
Eric Laurent41709552019-12-16 19:34:05 -08002875 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2876 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002877}
2878
Eric Laurent41709552019-12-16 19:34:05 -08002879void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002880 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2881
2882 sp<ThreadBase> t = mThread.promote();
2883 if (t == nullptr) {
2884 return;
2885 }
2886 t->onEffectDisable();
2887}
2888
2889bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2890 bool unpinIfLast) {
2891 sp<ThreadBase> t = mThread.promote();
2892 if (t == nullptr) {
2893 return false;
2894 }
2895 t->disconnectEffectHandle(handle, unpinIfLast);
2896 return true;
2897}
2898
2899void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2900 sp<EffectChain> c = mChain.promote();
2901 if (c == nullptr) {
2902 return;
2903 }
2904 c->resetVolume_l();
2905
2906}
2907
2908uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2909 sp<EffectChain> c = mChain.promote();
2910 if (c == nullptr) {
2911 return PRODUCT_STRATEGY_NONE;
2912 }
2913 return c->strategy();
2914}
2915
2916int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
2917 sp<EffectChain> c = mChain.promote();
2918 if (c == nullptr) {
2919 return 0;
2920 }
2921 return c->activeTrackCnt();
2922}
2923
Eric Laurentb82e6b72019-11-22 17:25:04 -08002924
2925#undef LOG_TAG
2926#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
2927
2928status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
2929{
2930 status_t status = EffectBase::setEnabled(enabled, fromHandle);
2931 Mutex::Autolock _l(mProxyLock);
2932 if (status == NO_ERROR) {
2933 for (auto& handle : mEffectHandles) {
2934 if (enabled) {
2935 status = handle.second->enable();
2936 } else {
2937 status = handle.second->disable();
2938 }
2939 }
2940 }
2941 ALOGV("%s enable %d status %d", __func__, enabled, status);
2942 return status;
2943}
2944
2945status_t AudioFlinger::DeviceEffectProxy::init(
2946 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
2947//For all audio patches
2948//If src or sink device match
2949//If the effect is HW accelerated
2950// if no corresponding effect module
2951// Create EffectModule: mHalEffect
2952//Create and attach EffectHandle
2953//If the effect is not HW accelerated and the patch sink or src is a mixer port
2954// Create Effect on patch input or output thread on session -1
2955//Add EffectHandle to EffectHandle map of Effect Proxy:
2956 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
2957 status_t status = NO_ERROR;
2958 for (auto &patch : patches) {
2959 status = onCreatePatch(patch.first, patch.second);
2960 ALOGV("%s onCreatePatch status %d", __func__, status);
2961 if (status == BAD_VALUE) {
2962 return status;
2963 }
2964 }
2965 return status;
2966}
2967
2968status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
2969 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
2970 status_t status = NAME_NOT_FOUND;
2971 sp<EffectHandle> handle;
2972 // only consider source[0] as this is the only "true" source of a patch
2973 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
2974 ALOGV("%s source checkPort status %d", __func__, status);
2975 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
2976 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
2977 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
2978 }
2979 if (status == NO_ERROR || status == ALREADY_EXISTS) {
2980 Mutex::Autolock _l(mProxyLock);
2981 mEffectHandles.emplace(patchHandle, handle);
2982 }
2983 ALOGW_IF(status == BAD_VALUE,
2984 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
2985
2986 return status;
2987}
2988
2989status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
2990 const struct audio_port_config *port, sp <EffectHandle> *handle) {
2991
2992 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
2993 __func__, port->type, port->ext.device.type,
2994 port->ext.device.address, port->id, patch.isSoftware());
2995 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
2996 || port->ext.device.address != mDevice.mAddress) {
2997 return NAME_NOT_FOUND;
2998 }
2999 status_t status = NAME_NOT_FOUND;
3000
3001 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3002 Mutex::Autolock _l(mProxyLock);
3003 mDevicePort = *port;
3004 mHalEffect = new EffectModule(mMyCallback,
3005 const_cast<effect_descriptor_t *>(&mDescriptor),
3006 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3007 false /* pinned */, port->id);
3008 if (audio_is_input_device(mDevice.mType)) {
3009 mHalEffect->setInputDevice(mDevice);
3010 } else {
3011 mHalEffect->setDevices({mDevice});
3012 }
3013 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3014 status = (*handle)->initCheck();
3015 if (status == OK) {
3016 status = mHalEffect->addHandle((*handle).get());
3017 } else {
3018 mHalEffect.clear();
3019 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3020 }
3021 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3022 sp <ThreadBase> thread;
3023 if (audio_port_config_has_input_direction(port)) {
3024 if (patch.isSoftware()) {
3025 thread = patch.mRecord.thread();
3026 } else {
3027 thread = patch.thread().promote();
3028 }
3029 } else {
3030 if (patch.isSoftware()) {
3031 thread = patch.mPlayback.thread();
3032 } else {
3033 thread = patch.thread().promote();
3034 }
3035 }
3036 int enabled;
3037 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3038 const_cast<effect_descriptor_t *>(&mDescriptor),
3039 &enabled, &status, false);
3040 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3041 } else {
3042 status = BAD_VALUE;
3043 }
3044 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3045 if (isEnabled()) {
3046 (*handle)->enable();
3047 } else {
3048 (*handle)->disable();
3049 }
3050 }
3051 return status;
3052}
3053
3054void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3055 Mutex::Autolock _l(mProxyLock);
3056 mEffectHandles.erase(patchHandle);
3057}
3058
3059
3060size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3061{
3062 Mutex::Autolock _l(mProxyLock);
3063 if (effect == mHalEffect) {
3064 mHalEffect.clear();
3065 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3066 }
3067 return mHalEffect == nullptr ? 0 : 1;
3068}
3069
3070status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3071 sp<EffectHalInterface> effect) {
3072 if (mHalEffect == nullptr) {
3073 return NO_INIT;
3074 }
3075 return mManagerCallback->addEffectToHal(
3076 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3077}
3078
3079status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3080 sp<EffectHalInterface> effect) {
3081 if (mHalEffect == nullptr) {
3082 return NO_INIT;
3083 }
3084 return mManagerCallback->removeEffectFromHal(
3085 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3086}
3087
3088bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3089 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3090 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3091 }
3092 return true;
3093}
3094
3095uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3096 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3097 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3098 return mDevicePort.sample_rate;
3099 }
3100 return DEFAULT_OUTPUT_SAMPLE_RATE;
3101}
3102
3103audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3104 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3105 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3106 return mDevicePort.channel_mask;
3107 }
3108 return AUDIO_CHANNEL_OUT_STEREO;
3109}
3110
3111uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3112 if (isOutput()) {
3113 return audio_channel_count_from_out_mask(channelMask());
3114 }
3115 return audio_channel_count_from_in_mask(channelMask());
3116}
3117
3118void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3119 const Vector<String16> args;
3120 EffectBase::dump(fd, args);
3121
3122 const bool locked = dumpTryLock(mProxyLock);
3123 if (!locked) {
3124 String8 result("DeviceEffectProxy may be deadlocked\n");
3125 write(fd, result.string(), result.size());
3126 }
3127
3128 String8 outStr;
3129 if (mHalEffect != nullptr) {
3130 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3131 } else {
3132 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3133 }
3134 write(fd, outStr.string(), outStr.size());
3135 outStr.clear();
3136
3137 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3138 write(fd, outStr.string(), outStr.size());
3139 outStr.clear();
3140
3141 for (const auto& iter : mEffectHandles) {
3142 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3143 write(fd, outStr.string(), outStr.size());
3144 outStr.clear();
3145 sp<EffectBase> effect = iter.second->effect().promote();
3146 if (effect != nullptr) {
3147 effect->dump(fd, args);
3148 }
3149 }
3150
3151 if (locked) {
3152 mLock.unlock();
3153 }
3154}
3155
3156#undef LOG_TAG
3157#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3158
3159int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3160 return mManagerCallback->newEffectId();
3161}
3162
3163
3164bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3165 EffectHandle *handle, bool unpinIfLast) {
3166 sp<EffectBase> effectBase = handle->effect().promote();
3167 if (effectBase == nullptr) {
3168 return false;
3169 }
3170
3171 sp<EffectModule> effect = effectBase->asEffectModule();
3172 if (effect == nullptr) {
3173 return false;
3174 }
3175
3176 // restore suspended effects if the disconnected handle was enabled and the last one.
3177 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3178 if (remove) {
3179 sp<DeviceEffectProxy> proxy = mProxy.promote();
3180 if (proxy != nullptr) {
3181 proxy->removeEffect(effect);
3182 }
3183 if (handle->enabled()) {
3184 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3185 }
3186 }
3187 return true;
3188}
3189
3190status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3191 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3192 sp<EffectHalInterface> *effect) {
3193 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3194}
3195
3196status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3197 sp<EffectHalInterface> effect) {
3198 sp<DeviceEffectProxy> proxy = mProxy.promote();
3199 if (proxy == nullptr) {
3200 return NO_INIT;
3201 }
3202 return proxy->addEffectToHal(effect);
3203}
3204
3205status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3206 sp<EffectHalInterface> effect) {
3207 sp<DeviceEffectProxy> proxy = mProxy.promote();
3208 if (proxy == nullptr) {
3209 return NO_INIT;
3210 }
3211 return proxy->addEffectToHal(effect);
3212}
3213
3214bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3215 sp<DeviceEffectProxy> proxy = mProxy.promote();
3216 if (proxy == nullptr) {
3217 return true;
3218 }
3219 return proxy->isOutput();
3220}
3221
3222uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3223 sp<DeviceEffectProxy> proxy = mProxy.promote();
3224 if (proxy == nullptr) {
3225 return DEFAULT_OUTPUT_SAMPLE_RATE;
3226 }
3227 return proxy->sampleRate();
3228}
3229
3230audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3231 sp<DeviceEffectProxy> proxy = mProxy.promote();
3232 if (proxy == nullptr) {
3233 return AUDIO_CHANNEL_OUT_STEREO;
3234 }
3235 return proxy->channelMask();
3236}
3237
3238uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3239 sp<DeviceEffectProxy> proxy = mProxy.promote();
3240 if (proxy == nullptr) {
3241 return 2;
3242 }
3243 return proxy->channelCount();
3244}
3245
Glenn Kasten63238ef2015-03-02 15:50:29 -08003246} // namespace android