blob: 505f3b3a74a4787da585a783f3c596b93aec40c3 [file] [log] [blame]
Eric Laurent81784c32012-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
Andy Hung5fd10da2023-07-19 11:31:15 -070018#pragma once
19
20#include "DeviceEffectManager.h"
21#include "IAfEffect.h"
22
23#include <android-base/macros.h> // DISALLOW_COPY_AND_ASSIGN
24#include <mediautils/Synchronization.h>
25#include <private/media/AudioEffectShared.h>
26
27#include <map> // avoid transitive dependency
28
Andy Hung6ac17eb2023-06-20 18:56:17 -070029namespace android {
Eric Laurent81784c32012-11-19 14:55:58 -080030
31//--- Audio Effect Management
32
Eric Laurent41709552019-12-16 19:34:05 -080033// EffectBase(EffectModule) and EffectChain classes both have their own mutex to protect
Eric Laurent81784c32012-11-19 14:55:58 -080034// state changes or resource modifications. Always respect the following order
35// if multiple mutexes must be acquired to avoid cross deadlock:
Eric Laurent41709552019-12-16 19:34:05 -080036// AudioFlinger -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
37// AudioHandle -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
Eric Laurent6b446ce2019-12-13 10:56:31 -080038
39// NOTE: When implementing the EffectCallbackInterface, in an EffectChain or other, it is important
40// to pay attention to this locking order as some callback methods can be called from a state where
41// EffectModule and/or EffectChain mutexes are held.
42
Eric Laurenteb3c3372013-09-25 12:25:29 -070043// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
Eric Laurent3bc859b2016-12-05 11:07:22 -080044// startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or
45// Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService
46// methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order.
Eric Laurent81784c32012-11-19 14:55:58 -080047
Eric Laurent41709552019-12-16 19:34:05 -080048
49// The EffectBase class contains common properties, state and behavior for and EffectModule or
50// other derived classes managing an audio effect instance within the effect framework.
51// It also contains the class mutex (see comment on locking order above).
Andy Hung6ac17eb2023-06-20 18:56:17 -070052class EffectBase : public virtual IAfEffectBase {
Eric Laurent81784c32012-11-19 14:55:58 -080053public:
Eric Laurent41709552019-12-16 19:34:05 -080054 EffectBase(const sp<EffectCallbackInterface>& callback,
55 effect_descriptor_t *desc,
56 int id,
57 audio_session_t sessionId,
58 bool pinned);
59
Andy Hung6ac17eb2023-06-20 18:56:17 -070060 int id() const final { return mId; }
61 effect_state state() const final {
Eric Laurent81784c32012-11-19 14:55:58 -080062 return mState;
63 }
Andy Hung6ac17eb2023-06-20 18:56:17 -070064 audio_session_t sessionId() const final {
Eric Laurent81784c32012-11-19 14:55:58 -080065 return mSessionId;
66 }
Andy Hung6ac17eb2023-06-20 18:56:17 -070067 const effect_descriptor_t& desc() const final { return mDescriptor; }
68 bool isOffloadable() const final
Eric Laurent5baf2af2013-09-12 17:37:00 -070069 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -070070 bool isImplementationSoftware() const final
Eric Laurent4c415062016-06-17 16:14:16 -070071 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -070072 bool isProcessImplemented() const final
Eric Laurent6dd0fd92016-09-15 12:44:53 -070073 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -070074 bool isVolumeControl() const
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +090075 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
76 == EFFECT_FLAG_VOLUME_CTRL; }
Andy Hung6ac17eb2023-06-20 18:56:17 -070077 bool isVolumeMonitor() const final
Jasmine Cha934ecfb2019-01-23 18:19:14 +080078 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
79 == EFFECT_FLAG_VOLUME_MONITOR; }
Eric Laurent41709552019-12-16 19:34:05 -080080
Shunkai Yaod125e402024-01-20 03:19:06 +000081 status_t setEnabled(bool enabled, bool fromHandle) override EXCLUDES_EffectBase_Mutex;
82 status_t setEnabled_l(bool enabled) final REQUIRES(audio_utils::EffectBase_Mutex);
Andy Hung6ac17eb2023-06-20 18:56:17 -070083 bool isEnabled() const final;
Shunkai Yaod125e402024-01-20 03:19:06 +000084 void setSuspended(bool suspended) final EXCLUDES_EffectBase_Mutex;
85 bool suspended() const final EXCLUDES_EffectBase_Mutex;
Eric Laurent41709552019-12-16 19:34:05 -080086
Andy Hung6ac17eb2023-06-20 18:56:17 -070087 status_t command(int32_t __unused,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070088 const std::vector<uint8_t>& __unused,
89 int32_t __unused,
Andy Hung6ac17eb2023-06-20 18:56:17 -070090 std::vector<uint8_t>* __unused) override {
91 return NO_ERROR;
92 }
Eric Laurent41709552019-12-16 19:34:05 -080093
Andy Hungfda44002021-06-03 17:23:16 -070094 // mCallback is atomic so this can be lock-free.
Andy Hung6ac17eb2023-06-20 18:56:17 -070095 void setCallback(const sp<EffectCallbackInterface>& callback) final {
96 mCallback = callback;
97 }
98 sp<EffectCallbackInterface> getCallback() const final {
99 return mCallback.load();
100 }
Eric Laurent41709552019-12-16 19:34:05 -0800101
Shunkai Yaod125e402024-01-20 03:19:06 +0000102 status_t addHandle(IAfEffectHandle* handle) final EXCLUDES_EffectBase_Mutex;
103 ssize_t disconnectHandle(IAfEffectHandle* handle,
104 bool unpinIfLast) final EXCLUDES_EffectBase_Mutex;
105 ssize_t removeHandle(IAfEffectHandle* handle) final EXCLUDES_EffectBase_Mutex;
106 ssize_t removeHandle_l(IAfEffectHandle* handle) final REQUIRES(audio_utils::EffectBase_Mutex);
107 IAfEffectHandle* controlHandle_l() final REQUIRES(audio_utils::EffectBase_Mutex);
108 bool purgeHandles() final EXCLUDES_EffectBase_Mutex;
Eric Laurent41709552019-12-16 19:34:05 -0800109
Shunkai Yaod125e402024-01-20 03:19:06 +0000110 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) final;
Eric Laurent41709552019-12-16 19:34:05 -0800111
Shunkai Yaod125e402024-01-20 03:19:06 +0000112 bool isPinned() const final { return mPinned; }
113 void unPin() final { mPinned = false; }
Eric Laurent41709552019-12-16 19:34:05 -0800114
Shunkai Yaod125e402024-01-20 03:19:06 +0000115 audio_utils::mutex& mutex() const final
116 RETURN_CAPABILITY(android::audio_utils::EffectBase_Mutex) {
117 return mMutex;
118 }
Eric Laurent81784c32012-11-19 14:55:58 -0800119
Shunkai Yaod125e402024-01-20 03:19:06 +0000120 status_t updatePolicyState() final EXCLUDES_EffectBase_Mutex;
Eric Laurent41709552019-12-16 19:34:05 -0800121
Andy Hung6ac17eb2023-06-20 18:56:17 -0700122 sp<IAfEffectModule> asEffectModule() override { return nullptr; }
123 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() override { return nullptr; }
Eric Laurent6c796322019-04-09 14:13:17 -0700124
Shunkai Yaod125e402024-01-20 03:19:06 +0000125 void dump(int fd, const Vector<String16>& args) const override;
Eric Laurent81784c32012-11-19 14:55:58 -0800126
Eric Laurentd66d7a12021-07-13 13:35:32 +0200127protected:
Shunkai Yaod125e402024-01-20 03:19:06 +0000128 bool isInternal_l() const REQUIRES(audio_utils::EffectBase_Mutex) {
129 for (auto handle : mHandles) {
130 if (handle->client() != nullptr) {
131 return false;
132 }
133 }
134 return true;
135 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200136
Eric Laurent41709552019-12-16 19:34:05 -0800137 bool mPinned = false;
138
139 DISALLOW_COPY_AND_ASSIGN(EffectBase);
140
Andy Hungf65f5a72023-08-29 12:19:17 -0700141 // mutex for process, commands and handles list protection
Andy Hunga6f1cbb2023-10-12 18:18:13 -0700142 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kEffectBase_Mutex};
Andy Hungfda44002021-06-03 17:23:16 -0700143 mediautils::atomic_sp<EffectCallbackInterface> mCallback; // parent effect chain
Eric Laurent41709552019-12-16 19:34:05 -0800144 const int mId; // this instance unique ID
145 const audio_session_t mSessionId; // audio session ID
146 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
147 effect_state mState = IDLE; // current activation state
Eric Laurentd9eb4962019-12-19 09:20:49 -0800148 // effect is suspended: temporarily disabled by framework
149 bool mSuspended = false;
Eric Laurent41709552019-12-16 19:34:05 -0800150
Andy Hung6ac17eb2023-06-20 18:56:17 -0700151 Vector<IAfEffectHandle *> mHandles; // list of client handles
Eric Laurent41709552019-12-16 19:34:05 -0800152 // First handle in mHandles has highest priority and controls the effect module
153
154 // Audio policy effect state management
Andy Hungf65f5a72023-08-29 12:19:17 -0700155 // Mutex protecting transactions with audio policy manager as mutex() cannot
Eric Laurent41709552019-12-16 19:34:05 -0800156 // be held to avoid cross deadlocks with audio policy mutex
Shunkai Yaod125e402024-01-20 03:19:06 +0000157 audio_utils::mutex& policyMutex() const
158 RETURN_CAPABILITY(android::audio_utils::EffectBase_PolicyMutex) {
159 return mPolicyMutex;
160 }
Andy Hunga6f1cbb2023-10-12 18:18:13 -0700161 mutable audio_utils::mutex mPolicyMutex{audio_utils::MutexOrder::kEffectBase_PolicyMutex};
Eric Laurent41709552019-12-16 19:34:05 -0800162 // Effect is registered in APM or not
163 bool mPolicyRegistered = false;
164 // Effect enabled state communicated to APM. Enabled state corresponds to
165 // state requested by the EffectHandle with control
166 bool mPolicyEnabled = false;
167};
168
169// The EffectModule class is a wrapper object controlling the effect engine implementation
170// in the effect library. It prevents concurrent calls to process() and command() functions
171// from different client threads. It keeps a list of EffectHandle objects corresponding
172// to all client applications using this effect and notifies applications of effect state,
173// control or parameter changes. It manages the activation state machine to send appropriate
174// reset, enable, disable commands to effect engine and provide volume
175// ramping when effects are activated/deactivated.
176// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
177// the attached track(s) to accumulate their auxiliary channel.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700178class EffectModule : public IAfEffectModule, public EffectBase {
Eric Laurent41709552019-12-16 19:34:05 -0800179public:
180 EffectModule(const sp<EffectCallbackInterface>& callabck,
181 effect_descriptor_t *desc,
182 int id,
183 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800184 bool pinned,
Shunkai Yaod125e402024-01-20 03:19:06 +0000185 audio_port_handle_t deviceId) REQUIRES(audio_utils::EffectChain_Mutex);
186 ~EffectModule() override REQUIRES(audio_utils::EffectChain_Mutex);
Eric Laurent41709552019-12-16 19:34:05 -0800187
Shunkai Yaod125e402024-01-20 03:19:06 +0000188 void process() final EXCLUDES_EffectBase_Mutex;
189 bool updateState_l() final REQUIRES(audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
190 status_t command(int32_t cmdCode, const std::vector<uint8_t>& cmdData, int32_t maxReplySize,
191 std::vector<uint8_t>* reply) final EXCLUDES_EffectBase_Mutex;
Eric Laurent41709552019-12-16 19:34:05 -0800192
Shunkai Yaod125e402024-01-20 03:19:06 +0000193 void reset_l() final REQUIRES(audio_utils::EffectBase_Mutex);
194 status_t configure_l() final REQUIRES(audio_utils::EffectChain_Mutex);
195 status_t init_l() final REQUIRES(audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700196 uint32_t status() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800197 return mStatus;
198 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700199 bool isProcessEnabled() const final;
Shunkai Yaod125e402024-01-20 03:19:06 +0000200 bool isOffloadedOrDirect_l() const final REQUIRES(audio_utils::EffectChain_Mutex);
201 bool isVolumeControlEnabled_l() const final REQUIRES(audio_utils::EffectChain_Mutex);
Andy Hung6ac17eb2023-06-20 18:56:17 -0700202 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final;
203 int16_t *inBuffer() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800204 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
205 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700206 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final;
207 int16_t *outBuffer() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800208 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
209 }
Andy Hung799c8d02021-10-28 17:05:40 -0700210 // Updates the access mode if it is out of date. May issue a new effect configure.
Shunkai Yaod125e402024-01-20 03:19:06 +0000211 void updateAccessMode_l() final REQUIRES(audio_utils::EffectChain_Mutex) {
212 if (requiredEffectBufferAccessMode() != mConfig.outputCfg.accessMode) {
213 configure_l();
214 }
215 }
216 status_t setDevices(const AudioDeviceTypeAddrVector& devices) final EXCLUDES_EffectBase_Mutex;
217 status_t setInputDevice(const AudioDeviceTypeAddr& device) final EXCLUDES_EffectBase_Mutex;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700218 status_t setVolume(uint32_t *left, uint32_t *right, bool controller) final;
Shunkai Yaod125e402024-01-20 03:19:06 +0000219 status_t setMode(audio_mode_t mode) final EXCLUDES_EffectBase_Mutex;
220 status_t setAudioSource(audio_source_t source) final EXCLUDES_EffectBase_Mutex;
221 status_t start_l() final REQUIRES(audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
222 status_t stop_l() final REQUIRES(audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
Andy Hung799c8d02021-10-28 17:05:40 -0700223
Shunkai Yaod125e402024-01-20 03:19:06 +0000224 status_t setOffloaded_l(bool offloaded, audio_io_handle_t io) final
225 REQUIRES(audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
226 bool isOffloaded_l() const final
227 REQUIRES(audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
228 void addEffectToHal_l() final REQUIRES(audio_utils::EffectChain_Mutex);
229 void release_l() final REQUIRES(audio_utils::EffectChain_Mutex);
Eric Laurent41709552019-12-16 19:34:05 -0800230
Andy Hung6ac17eb2023-06-20 18:56:17 -0700231 sp<IAfEffectModule> asEffectModule() final { return this; }
Eric Laurent41709552019-12-16 19:34:05 -0800232
Andy Hung6ac17eb2023-06-20 18:56:17 -0700233 bool isHapticGenerator() const final;
Eric Laurent4eb45d02023-12-20 12:07:17 +0100234 bool isSpatializer() const final;
Eric Laurent41709552019-12-16 19:34:05 -0800235
Ahmad Khalil229466a2024-02-05 12:15:30 +0000236 status_t setHapticScale_l(int id, os::HapticScale hapticScale) final
Shunkai Yao29d10572024-03-19 04:31:47 +0000237 REQUIRES(audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
Shunkai Yaod125e402024-01-20 03:19:06 +0000238 status_t setVibratorInfo_l(const media::AudioVibratorInfo& vibratorInfo) final
Shunkai Yao29d10572024-03-19 04:31:47 +0000239 REQUIRES(audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
Shunkai Yaod125e402024-01-20 03:19:06 +0000240 status_t sendMetadata_ll(const std::vector<playback_track_metadata_v7_t>& metadata) final
241 REQUIRES(audio_utils::ThreadBase_Mutex,
242 audio_utils::EffectChain_Mutex) EXCLUDES_EffectBase_Mutex;
jiabineb3bda02020-06-30 14:07:03 -0700243
Shunkai Yaod125e402024-01-20 03:19:06 +0000244 status_t getConfigs_l(audio_config_base_t* inputCfg, audio_config_base_t* outputCfg,
245 bool* isOutput) const final
246 REQUIRES(audio_utils::EffectHandle_Mutex) EXCLUDES_EffectBase_Mutex;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000247
Andy Hung6ac17eb2023-06-20 18:56:17 -0700248 void dump(int fd, const Vector<String16>& args) const final;
Eric Laurent41709552019-12-16 19:34:05 -0800249
250private:
Eric Laurent81784c32012-11-19 14:55:58 -0800251
252 // Maximum time allocated to effect engines to complete the turn off sequence
253 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
254
Mikhail Naganovbf493082017-04-17 17:37:12 -0700255 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800256
Shunkai Yaod125e402024-01-20 03:19:06 +0000257 status_t start_ll() REQUIRES(audio_utils::EffectChain_Mutex, audio_utils::EffectBase_Mutex);
258 status_t stop_ll() REQUIRES(audio_utils::EffectChain_Mutex, audio_utils::EffectBase_Mutex);
259 status_t removeEffectFromHal_l() REQUIRES(audio_utils::EffectChain_Mutex);
jiabin8f278ee2019-11-11 12:16:27 -0800260 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Andy Hung799c8d02021-10-28 17:05:40 -0700261 effect_buffer_access_e requiredEffectBufferAccessMode() const {
262 return mConfig.inputCfg.buffer.raw == mConfig.outputCfg.buffer.raw
263 ? EFFECT_BUFFER_ACCESS_WRITE : EFFECT_BUFFER_ACCESS_ACCUMULATE;
264 }
265
jiabin4e246532022-08-23 16:37:30 -0700266 status_t setVolumeInternal(uint32_t *left, uint32_t *right, bool controller);
267
Eric Laurent81784c32012-11-19 14:55:58 -0800268
Eric Laurent81784c32012-11-19 14:55:58 -0800269 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700270 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800271 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
272 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800273 status_t mStatus; // initialization status
Eric Laurent81784c32012-11-19 14:55:58 -0800274 // First handle in mHandles has highest priority and controls the effect module
275 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
276 // sending disable command.
277 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent5baf2af2013-09-12 17:37:00 -0700278 bool mOffloaded; // effect is currently offloaded to the audio DSP
Eric Laurent9f57aa82023-03-17 19:28:37 +0100279 // effect has been added to this HAL input stream
280 audio_io_handle_t mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000281 bool mIsOutput; // direction of the AF thread
rago94a1ee82017-07-21 15:11:02 -0700282
rago94a1ee82017-07-21 15:11:02 -0700283 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800284 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
285 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800286 uint32_t mInChannelCountRequested;
287 uint32_t mOutChannelCountRequested;
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900288
Andy Hungf65f5a72023-08-29 12:19:17 -0700289 template <typename MUTEX>
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900290 class AutoLockReentrant {
291 public:
Andy Hungf65f5a72023-08-29 12:19:17 -0700292 AutoLockReentrant(MUTEX& mutex, pid_t allowedTid)
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900293 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
294 {
295 if (mMutex != nullptr) mMutex->lock();
296 }
297 ~AutoLockReentrant() {
298 if (mMutex != nullptr) mMutex->unlock();
299 }
300 private:
Andy Hungf65f5a72023-08-29 12:19:17 -0700301 MUTEX * const mMutex;
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900302 };
303
304 static constexpr pid_t INVALID_PID = (pid_t)-1;
305 // this tid is allowed to call setVolume() without acquiring the mutex.
306 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent81784c32012-11-19 14:55:58 -0800307};
308
309// The EffectHandle class implements the IEffect interface. It provides resources
310// to receive parameter updates, keeps track of effect control
311// ownership and state and has a pointer to the EffectModule object it is controlling.
312// There is one EffectHandle object for each application controlling (or using)
313// an effect module.
314// The EffectHandle is obtained by calling AudioFlinger::createEffect().
Andy Hung6ac17eb2023-06-20 18:56:17 -0700315class EffectHandle: public IAfEffectHandle, public android::media::BnEffect {
Eric Laurent81784c32012-11-19 14:55:58 -0800316public:
317
Andy Hung6ac17eb2023-06-20 18:56:17 -0700318 EffectHandle(const sp<IAfEffectBase>& effect,
Andy Hung59867e42023-06-27 17:05:02 -0700319 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700320 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +0200321 int32_t priority, bool notifyFramesProcessed);
Andy Hung6ac17eb2023-06-20 18:56:17 -0700322 ~EffectHandle() override;
Andy Hungc747c532022-03-07 21:41:14 -0800323 status_t onTransact(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700324 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) final;
325 status_t initCheck() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800326
327 // IEffect
Andy Hung6ac17eb2023-06-20 18:56:17 -0700328 android::binder::Status enable(int32_t* _aidl_return) final;
329 android::binder::Status disable(int32_t* _aidl_return) final;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700330 android::binder::Status command(int32_t cmdCode,
331 const std::vector<uint8_t>& cmdData,
332 int32_t maxResponseSize,
333 std::vector<uint8_t>* response,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700334 int32_t* _aidl_return) final;
335 android::binder::Status disconnect() final;
336 android::binder::Status getCblk(media::SharedFileRegion* _aidl_return) final;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000337 android::binder::Status getConfig(media::EffectConfig* _config,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700338 int32_t* _aidl_return) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800339
Andy Hung59867e42023-06-27 17:05:02 -0700340 const sp<Client>& client() const final { return mClient; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700341
342 sp<android::media::IEffect> asIEffect() final {
343 return sp<android::media::IEffect>::fromExisting(this);
344 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200345
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700346private:
347 void disconnect(bool unpinIfLast);
Eric Laurent81784c32012-11-19 14:55:58 -0800348
349 // Give or take control of effect module
350 // - hasControl: true if control is given, false if removed
351 // - signal: true client app should be signaled of change, false otherwise
352 // - enabled: state of the effect when control is passed
Andy Hung6ac17eb2023-06-20 18:56:17 -0700353 void setControl(bool hasControl, bool signal, bool enabled) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800354 void commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700355 const std::vector<uint8_t>& cmdData,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700356 const std::vector<uint8_t>& replyData) final;
357 bool enabled() const final { return mEnabled; }
358 void setEnabled(bool enabled) final;
359 void framesProcessed(int32_t frames) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800360
Andy Hung6ac17eb2023-06-20 18:56:17 -0700361public:
Eric Laurent81784c32012-11-19 14:55:58 -0800362 // Getters
Andy Hung6ac17eb2023-06-20 18:56:17 -0700363 wp<IAfEffectBase> effect() const final { return mEffect; }
364 int id() const final {
365 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800366 if (effect == 0) {
367 return 0;
368 }
369 return effect->id();
370 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700371private:
372 int priority() const final { return mPriority; }
373 bool hasControl() const final { return mHasControl; }
374 bool disconnected() const final { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800375
Andy Hung6ac17eb2023-06-20 18:56:17 -0700376 void dumpToBuffer(char* buffer, size_t size) const final;
377
Eric Laurent81784c32012-11-19 14:55:58 -0800378
Mikhail Naganovbf493082017-04-17 17:37:12 -0700379private:
Mikhail Naganovbf493082017-04-17 17:37:12 -0700380 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800381
Shunkai Yaod125e402024-01-20 03:19:06 +0000382 audio_utils::mutex& mutex() const RETURN_CAPABILITY(android::audio_utils::EffectHandle_Mutex) {
383 return mMutex;
384 }
Andy Hunga6f1cbb2023-10-12 18:18:13 -0700385 // protects IEffect method calls
386 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kEffectHandle_Mutex};
Andy Hung6ac17eb2023-06-20 18:56:17 -0700387 const wp<IAfEffectBase> mEffect; // pointer to controlled EffectModule
Andy Hung318e0242020-12-21 10:33:49 -0800388 const sp<media::IEffectClient> mEffectClient; // callback interface for client notifications
Andy Hung59867e42023-06-27 17:05:02 -0700389 /*const*/ sp<Client> mClient; // client for shared memory allocation, see
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700390 // disconnect()
391 sp<IMemory> mCblkMemory; // shared memory for control block
392 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
393 // shared memory
394 uint8_t* mBuffer; // pointer to parameter area in shared memory
395 int mPriority; // client application priority to control the effect
396 bool mHasControl; // true if this handle is controlling the effect
397 bool mEnabled; // cached enable state: needed when the effect is
398 // restored after being suspended
399 bool mDisconnected; // Set to true by disconnect()
Eric Laurentde8caf42021-08-11 17:19:25 +0200400 const bool mNotifyFramesProcessed; // true if the client callback event
401 // EVENT_FRAMES_PROCESSED must be generated
Eric Laurent81784c32012-11-19 14:55:58 -0800402};
403
404// the EffectChain class represents a group of effects associated to one audio session.
405// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800406// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
407// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800408// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
409// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800410// order corresponding in the effect process order. When attached to a track (session ID !=
411// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800412// it also provide it's own input buffer used by the track as accumulation buffer.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700413class EffectChain : public IAfEffectChain {
Eric Laurent81784c32012-11-19 14:55:58 -0800414public:
Shunkai Yao29d10572024-03-19 04:31:47 +0000415 EffectChain(const sp<IAfThreadBase>& thread,
416 audio_session_t sessionId,
417 const sp<IAfThreadCallback>& afThreadCallback);
Eric Laurent81784c32012-11-19 14:55:58 -0800418
Shunkai Yaof4847652024-01-12 00:25:20 +0000419 void process_l() final REQUIRES(audio_utils::EffectChain_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800420
Shunkai Yaof4847652024-01-12 00:25:20 +0000421 audio_utils::mutex& mutex() const final RETURN_CAPABILITY(audio_utils::EffectChain_Mutex) {
422 return mMutex;
423 }
Andy Hungf65f5a72023-08-29 12:19:17 -0700424
Shunkai Yao29d10572024-03-19 04:31:47 +0000425 status_t createEffect(sp<IAfEffectModule>& effect, effect_descriptor_t* desc, int id,
Shunkai Yaod125e402024-01-20 03:19:06 +0000426 audio_session_t sessionId, bool pinned) final
Shunkai Yao29d10572024-03-19 04:31:47 +0000427 EXCLUDES_EffectChain_Mutex;
428 status_t addEffect(const sp<IAfEffectModule>& handle) final
429 EXCLUDES_EffectChain_Mutex;
Shunkai Yaod125e402024-01-20 03:19:06 +0000430 status_t addEffect_l(const sp<IAfEffectModule>& handle) final
Shunkai Yao29d10572024-03-19 04:31:47 +0000431 REQUIRES(audio_utils::EffectChain_Mutex);
432 size_t removeEffect(const sp<IAfEffectModule>& handle, bool release = false) final
433 EXCLUDES_EffectChain_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -0800434
Andy Hung6ac17eb2023-06-20 18:56:17 -0700435 audio_session_t sessionId() const final { return mSessionId; }
436 void setSessionId(audio_session_t sessionId) final { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800437
Shunkai Yao29d10572024-03-19 04:31:47 +0000438 sp<IAfEffectModule> getEffectFromDesc(effect_descriptor_t* descriptor) const final
439 EXCLUDES_EffectChain_Mutex;
Shunkai Yaod125e402024-01-20 03:19:06 +0000440 sp<IAfEffectModule> getEffectFromId_l(int id) const final
Shunkai Yao29d10572024-03-19 04:31:47 +0000441 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex;
Shunkai Yaod125e402024-01-20 03:19:06 +0000442 sp<IAfEffectModule> getEffectFromType_l(const effect_uuid_t* type) const final
Shunkai Yao29d10572024-03-19 04:31:47 +0000443 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex;
Shunkai Yaod125e402024-01-20 03:19:06 +0000444 std::vector<int> getEffectIds_l() const final REQUIRES(audio_utils::ThreadBase_Mutex);
Glenn Kastenc56f3422014-03-21 17:53:17 -0700445 // FIXME use float to improve the dynamic range
Eric Laurent81784c32012-11-19 14:55:58 -0800446
Shunkai Yaof4847652024-01-12 00:25:20 +0000447 bool setVolume(uint32_t* left, uint32_t* right,
448 bool force = false) final EXCLUDES_EffectChain_Mutex;
449 void resetVolume_l() final REQUIRES(audio_utils::EffectChain_Mutex);
Shunkai Yaod125e402024-01-20 03:19:06 +0000450 void setDevices_l(const AudioDeviceTypeAddrVector& devices) final
Shunkai Yao29d10572024-03-19 04:31:47 +0000451 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex;
Shunkai Yaod125e402024-01-20 03:19:06 +0000452 void setInputDevice_l(const AudioDeviceTypeAddr& device) final
Shunkai Yao29d10572024-03-19 04:31:47 +0000453 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex;
454 void setMode_l(audio_mode_t mode) final
455 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex;
456 void setAudioSource_l(audio_source_t source) final
457 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700458
459 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800460 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800461 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700462 float *inBuffer() const final {
Andy Hung181d96f2023-05-23 14:01:03 -0700463 return mInBuffer != 0 ? reinterpret_cast<float*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800464 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700465 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800466 mOutBuffer = buffer;
467 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700468 float *outBuffer() const final {
Andy Hung181d96f2023-05-23 14:01:03 -0700469 return mOutBuffer != 0 ? reinterpret_cast<float*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800470 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700471 void incTrackCnt() final { android_atomic_inc(&mTrackCnt); }
472 void decTrackCnt() final { android_atomic_dec(&mTrackCnt); }
473 int32_t trackCnt() const final { return android_atomic_acquire_load(&mTrackCnt); }
Eric Laurent81784c32012-11-19 14:55:58 -0800474
Andy Hung6ac17eb2023-06-20 18:56:17 -0700475 void incActiveTrackCnt() final { android_atomic_inc(&mActiveTrackCnt);
Eric Laurent81784c32012-11-19 14:55:58 -0800476 mTailBufferCount = mMaxTailBuffers; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700477 void decActiveTrackCnt() final { android_atomic_dec(&mActiveTrackCnt); }
478 int32_t activeTrackCnt() const final {
479 return android_atomic_acquire_load(&mActiveTrackCnt);
480 }
Eric Laurent81784c32012-11-19 14:55:58 -0800481
Andy Hung6ac17eb2023-06-20 18:56:17 -0700482 product_strategy_t strategy() const final { return mStrategy; }
483 void setStrategy(product_strategy_t strategy) final
Eric Laurent81784c32012-11-19 14:55:58 -0800484 { mStrategy = strategy; }
485
Eric Laurentd8365c52017-07-16 15:27:05 -0700486 // suspend or restore effects of the specified type. The number of suspend requests is counted
487 // and restore occurs once all suspend requests are cancelled.
Shunkai Yaod125e402024-01-20 03:19:06 +0000488 void setEffectSuspended_l(const effect_uuid_t* type, bool suspend) final
489 REQUIRES(audio_utils::ThreadBase_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800490 // suspend all eligible effects
Shunkai Yaod125e402024-01-20 03:19:06 +0000491 void setEffectSuspendedAll_l(bool suspend) final REQUIRES(audio_utils::ThreadBase_Mutex);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800492 // check if effects should be suspended or restored when a given effect is enable or disabled
Shunkai Yaod125e402024-01-20 03:19:06 +0000493 void checkSuspendOnEffectEnabled_l(const sp<IAfEffectModule>& effect, bool enabled) final
494 REQUIRES(audio_utils::ThreadBase_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800495
Shunkai Yaod125e402024-01-20 03:19:06 +0000496 void clearInputBuffer() final EXCLUDES_EffectChain_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -0800497
Eric Laurent5baf2af2013-09-12 17:37:00 -0700498 // At least one non offloadable effect in the chain is enabled
Shunkai Yaod125e402024-01-20 03:19:06 +0000499 bool isNonOffloadableEnabled() const final EXCLUDES_EffectChain_Mutex;
500 bool isNonOffloadableEnabled_l() const final REQUIRES(audio_utils::EffectChain_Mutex);
Eric Laurent813e2a72013-08-31 12:59:48 -0700501
Shunkai Yaod125e402024-01-20 03:19:06 +0000502 void syncHalEffectsState_l()
503 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex final;
Eric Laurent1b928682014-10-02 19:41:47 -0700504
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700505 // flags is an ORed set of audio_output_flags_t which is updated on return.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700506 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700507
508 // flags is an ORed set of audio_input_flags_t which is updated on return.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700509 void checkInputFlagCompatibility(audio_input_flags_t *flags) const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700510
511 // Is this EffectChain compatible with the RAW audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700512 bool isRawCompatible() const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700513
514 // Is this EffectChain compatible with the FAST audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700515 bool isFastCompatible() const final;
Eric Laurent4c415062016-06-17 16:14:16 -0700516
jiabinc658e452022-10-21 20:52:21 +0000517 // Is this EffectChain compatible with the bit-perfect audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700518 bool isBitPerfectCompatible() const final;
jiabinc658e452022-10-21 20:52:21 +0000519
Andy Hungf65f5a72023-08-29 12:19:17 -0700520 // isCompatibleWithThread_l() must be called with thread->mutex() held
Andy Hungab65b182023-09-06 19:41:47 -0700521 bool isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const final
Shunkai Yaod125e402024-01-20 03:19:06 +0000522 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex;
jiabineb3bda02020-06-30 14:07:03 -0700523
Shunkai Yao29d10572024-03-19 04:31:47 +0000524 bool containsHapticGeneratingEffect() final
525 EXCLUDES_EffectChain_Mutex;
526
527 bool containsHapticGeneratingEffect_l() final
528 REQUIRES(audio_utils::EffectChain_Mutex);
jiabine70bc7f2020-06-30 22:07:55 -0700529
Ahmad Khalil229466a2024-02-05 12:15:30 +0000530 void setHapticScale_l(int id, os::HapticScale hapticScale) final
Shunkai Yaod125e402024-01-20 03:19:06 +0000531 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800532
Andy Hung6ac17eb2023-06-20 18:56:17 -0700533 sp<EffectCallbackInterface> effectCallback() const final { return mEffectCallback; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200534
Andy Hung87c693c2023-07-06 20:56:16 -0700535 wp<IAfThreadBase> thread() const final { return mEffectCallback->thread(); }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700536
Shunkai Yao29d10572024-03-19 04:31:47 +0000537 bool isFirstEffect_l(int id) const final REQUIRES(audio_utils::EffectChain_Mutex) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700538 return !mEffects.isEmpty() && id == mEffects[0]->id();
539 }
540
541 void dump(int fd, const Vector<String16>& args) const final;
542
Shunkai Yao29d10572024-03-19 04:31:47 +0000543 size_t numberOfEffects() const final {
544 audio_utils::lock_guard _l(mutex());
545 return mEffects.size();
546 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700547
548 sp<IAfEffectModule> getEffectModule(size_t index) const final {
Shunkai Yao29d10572024-03-19 04:31:47 +0000549 audio_utils::lock_guard _l(mutex());
Andy Hung6ac17eb2023-06-20 18:56:17 -0700550 return mEffects[index];
551 }
552
Eric Laurent4eb45d02023-12-20 12:07:17 +0100553 void sendMetadata_l(const std::vector<playback_track_metadata_v7_t>& allMetadata,
554 const std::optional<const std::vector<playback_track_metadata_v7_t>> spatializedMetadata)
555 final REQUIRES(audio_utils::ThreadBase_Mutex);
556
Shunkai Yaod125e402024-01-20 03:19:06 +0000557 void setThread(const sp<IAfThreadBase>& thread) final EXCLUDES_EffectChain_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -0800558
Shunkai Yaof4847652024-01-12 00:25:20 +0000559 private:
560 bool setVolume_l(uint32_t* left, uint32_t* right, bool force = false)
561 REQUIRES(audio_utils::EffectChain_Mutex);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800562
Andy Hung328d6772021-01-12 12:32:21 -0800563 // For transaction consistency, please consider holding the EffectChain lock before
564 // calling the EffectChain::EffectCallback methods, excepting
565 // createEffectHal and allocateHalBuffer.
566 //
567 // This prevents migration of the EffectChain to another PlaybackThread
568 // for the purposes of the EffectCallback.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800569 class EffectCallback : public EffectCallbackInterface {
570 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800571 // Note: ctors taking a weak pointer to their owner must not promote it
572 // during construction (but may keep a reference for later promotion).
573 EffectCallback(const wp<EffectChain>& owner,
Shunkai Yao29d10572024-03-19 04:31:47 +0000574 const sp<IAfThreadBase>& thread,
575 const sp<IAfThreadCallback>& afThreadCallback) // we take a sp<> but store a wp<>.
Andy Hung6626a012021-01-12 13:38:00 -0800576 : mChain(owner)
Shunkai Yao29d10572024-03-19 04:31:47 +0000577 , mThread(thread), mAfThreadCallback(afThreadCallback) {
578 if (thread != nullptr) {
579 mThreadType = thread->type();
580 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800581 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800582
583 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
584 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
585 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700586 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800587
588 audio_io_handle_t io() const override;
589 bool isOutput() const override;
590 bool isOffload() const override;
591 bool isOffloadOrDirect() const override;
592 bool isOffloadOrMmap() const override;
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200593 bool isSpatializer() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800594
595 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200596 audio_channel_mask_t inChannelMask(int id) const override;
597 uint32_t inChannelCount(int id) const override;
598 audio_channel_mask_t outChannelMask() const override;
599 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700600 audio_channel_mask_t hapticChannelMask() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800601 size_t frameCount() const override;
602 uint32_t latency() const override;
603
Andy Hung920f6572022-10-06 12:09:49 -0700604 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override;
605 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700606 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800607 void setVolumeForOutput(float left, float right) const override;
608
609 // check if effects should be suspended/restored when a given effect is enable/disabled
Shunkai Yaod125e402024-01-20 03:19:06 +0000610 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect, bool enabled,
611 bool threadLocked) override;
612 void resetVolume_l() override
613 REQUIRES(audio_utils::ThreadBase_Mutex, audio_utils::EffectChain_Mutex);
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800614 product_strategy_t strategy() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800615 int32_t activeTrackCnt() const override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700616 void onEffectEnable(const sp<IAfEffectBase>& effect) override;
617 void onEffectDisable(const sp<IAfEffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800618
Andy Hung6ac17eb2023-06-20 18:56:17 -0700619 wp<IAfEffectChain> chain() const final { return mChain; }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800620
Andy Hung6ac17eb2023-06-20 18:56:17 -0700621 bool isAudioPolicyReady() const final {
Shunkai Yao29d10572024-03-19 04:31:47 +0000622 if (mAfThreadCallback == nullptr) {
623 return false;
624 }
Andy Hung583043b2023-07-17 17:05:00 -0700625 return mAfThreadCallback->isAudioPolicyReady();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200626 }
627
Andy Hung87c693c2023-07-06 20:56:16 -0700628 wp<IAfThreadBase> thread() const { return mThread.load(); }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800629
Andy Hung87c693c2023-07-06 20:56:16 -0700630 void setThread(const sp<IAfThreadBase>& thread) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800631 mThread = thread;
Shunkai Yao29d10572024-03-19 04:31:47 +0000632 if (thread != nullptr) {
633 mThreadType = thread->type();
634 mAfThreadCallback = thread->afThreadCallback();
635 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800636 }
Shunkai Yao517fc2a2024-03-19 04:31:47 +0000637 bool hasThreadAttached() const {
638 return thread().promote() != nullptr;
639 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800640 private:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700641 const wp<IAfEffectChain> mChain;
Andy Hung87c693c2023-07-06 20:56:16 -0700642 mediautils::atomic_wp<IAfThreadBase> mThread;
Andy Hung583043b2023-07-17 17:05:00 -0700643 sp<IAfThreadCallback> mAfThreadCallback;
Shunkai Yao29d10572024-03-19 04:31:47 +0000644 IAfThreadBase::type_t mThreadType = IAfThreadBase::MIXER;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800645 };
646
Mikhail Naganovbf493082017-04-17 17:37:12 -0700647 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800648
649 class SuspendedEffectDesc : public RefBase {
650 public:
651 SuspendedEffectDesc() : mRefCount(0) {}
652
Eric Laurentd8365c52017-07-16 15:27:05 -0700653 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800654 effect_uuid_t mType;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700655 wp<IAfEffectModule> mEffect;
Eric Laurent81784c32012-11-19 14:55:58 -0800656 };
657
658 // get a list of effect modules to suspend when an effect of the type
659 // passed is enabled.
Shunkai Yao29d10572024-03-19 04:31:47 +0000660 void getSuspendEligibleEffects(Vector<sp<IAfEffectModule>>& effects)
661 EXCLUDES_EffectChain_Mutex;
Eric Laurent81784c32012-11-19 14:55:58 -0800662
663 // get an effect module if it is currently enable
Shunkai Yaod125e402024-01-20 03:19:06 +0000664 sp<IAfEffectModule> getEffectIfEnabled_l(const effect_uuid_t* type)
665 REQUIRES(audio_utils::ThreadBase_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800666 // true if the effect whose descriptor is passed can be suspended
667 // OEMs can modify the rules implemented in this method to exclude specific effect
668 // types or implementations from the suspend/restore mechanism.
Shunkai Yao29d10572024-03-19 04:31:47 +0000669 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
Eric Laurent81784c32012-11-19 14:55:58 -0800670
Shunkai Yaod125e402024-01-20 03:19:06 +0000671 static bool isEffectEligibleForBtNrecSuspend_l(const effect_uuid_t* type)
672 REQUIRES(audio_utils::ThreadBase_Mutex);
Eric Laurentd8365c52017-07-16 15:27:05 -0700673
Shunkai Yaod125e402024-01-20 03:19:06 +0000674 void clearInputBuffer_l() REQUIRES(audio_utils::EffectChain_Mutex);
Eric Laurent81784c32012-11-19 14:55:58 -0800675
Zhou Songd505c642020-02-20 16:35:37 +0800676 // true if any effect module within the chain has volume control
Shunkai Yaod125e402024-01-20 03:19:06 +0000677 bool hasVolumeControlEnabled_l() const REQUIRES(audio_utils::EffectChain_Mutex);
Zhou Songd505c642020-02-20 16:35:37 +0800678
Shunkai Yaod125e402024-01-20 03:19:06 +0000679 void setVolumeForOutput_l(uint32_t left, uint32_t right)
680 REQUIRES(audio_utils::EffectChain_Mutex);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900681
Shunkai Yao29d10572024-03-19 04:31:47 +0000682 ssize_t getInsertIndex_l(const effect_descriptor_t& desc)
683 REQUIRES(audio_utils::EffectChain_Mutex);
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200684
Shunkai Yaod125e402024-01-20 03:19:06 +0000685 std::optional<size_t> findVolumeControl_l(size_t from, size_t to) const
686 REQUIRES(audio_utils::EffectChain_Mutex);
jiabinbd18c8e2023-08-24 21:10:44 +0000687
Andy Hunga6f1cbb2023-10-12 18:18:13 -0700688 // mutex protecting effect list
689 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kEffectChain_Mutex};
Shunkai Yao29d10572024-03-19 04:31:47 +0000690 Vector<sp<IAfEffectModule>> mEffects GUARDED_BY(mutex()); // list of effect modules
Eric Laurent4c415062016-06-17 16:14:16 -0700691 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800692 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
693 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800694
695 // 'volatile' here means these are accessed with atomic operations instead of mutex
696 volatile int32_t mActiveTrackCnt; // number of active tracks connected
697 volatile int32_t mTrackCnt; // number of tracks connected
698
Eric Laurent4c415062016-06-17 16:14:16 -0700699 int32_t mTailBufferCount; // current effect tail buffer count
700 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700701 uint32_t mLeftVolume; // previous volume on left channel
702 uint32_t mRightVolume; // previous volume on right channel
703 uint32_t mNewLeftVolume; // new volume on left channel
704 uint32_t mNewRightVolume; // new volume on right channel
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800705 product_strategy_t mStrategy; // strategy for this effect chain
Eric Laurent4c415062016-06-17 16:14:16 -0700706 // mSuspendedEffects lists all effects currently suspended in the chain.
707 // Use effect type UUID timelow field as key. There is no real risk of identical
708 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700709 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700710 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800711
712 const sp<EffectCallback> mEffectCallback;
jiabinbd18c8e2023-08-24 21:10:44 +0000713
714 wp<IAfEffectModule> mVolumeControlEffect;
Eric Laurent81784c32012-11-19 14:55:58 -0800715};
Eric Laurentb82e6b72019-11-22 17:25:04 -0800716
Andy Hung6ac17eb2023-06-20 18:56:17 -0700717class DeviceEffectProxy : public IAfDeviceEffectProxy, public EffectBase {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800718public:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700719 DeviceEffectProxy(const AudioDeviceTypeAddr& device,
Andy Hung55a74fd2023-07-13 19:54:47 -0700720 const sp<DeviceEffectManagerCallback>& callback,
Eric Laurentde8caf42021-08-11 17:19:25 +0200721 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800722 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
723 mDevice(device), mManagerCallback(callback),
Eric Laurentde8caf42021-08-11 17:19:25 +0200724 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this), callback)),
725 mNotifyFramesProcessed(notifyFramesProcessed) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800726
Andy Hung6ac17eb2023-06-20 18:56:17 -0700727 status_t setEnabled(bool enabled, bool fromHandle) final;
728 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() final { return this; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800729
Shunkai Yaod125e402024-01-20 03:19:06 +0000730 status_t init_l(const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches) final
731 REQUIRES(audio_utils::DeviceEffectManager_Mutex) EXCLUDES_EffectBase_Mutex;
Andy Hung8e6b62a2023-07-13 18:11:33 -0700732
Andy Hung6ac17eb2023-06-20 18:56:17 -0700733 status_t onCreatePatch(audio_patch_handle_t patchHandle,
Shunkai Yaod125e402024-01-20 03:19:06 +0000734 const IAfPatchPanel::Patch& patch) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800735
François Gaffie58e73af2023-02-15 11:47:24 +0100736 status_t onUpdatePatch(audio_patch_handle_t oldPatchHandle, audio_patch_handle_t newPatchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700737 const IAfPatchPanel::Patch& patch) final;
François Gaffie58e73af2023-02-15 11:47:24 +0100738
Andy Hung6ac17eb2023-06-20 18:56:17 -0700739 void onReleasePatch(audio_patch_handle_t patchHandle) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800740
Andy Hung6ac17eb2023-06-20 18:56:17 -0700741 size_t removeEffect(const sp<IAfEffectModule>& effect) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800742
Andy Hung6ac17eb2023-06-20 18:56:17 -0700743 status_t addEffectToHal(const sp<EffectHalInterface>& effect) final;
744 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) final;
745
746 const AudioDeviceTypeAddr& device() const final { return mDevice; };
747 bool isOutput() const final;
748 uint32_t sampleRate() const final;
749 audio_channel_mask_t channelMask() const final;
750 uint32_t channelCount() const final;
751
Shunkai Yaod125e402024-01-20 03:19:06 +0000752 status_t command(int32_t cmdCode, const std::vector<uint8_t>& cmdData, int32_t maxReplySize,
753 std::vector<uint8_t>* reply) final EXCLUDES_DeviceEffectProxy_ProxyMutex;
François Gaffiea2e985b2023-06-09 14:37:56 +0200754
Andy Hung6ac17eb2023-06-20 18:56:17 -0700755 void dump2(int fd, int spaces) const final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800756
757private:
758
759 class ProxyCallback : public EffectCallbackInterface {
760 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800761 // Note: ctors taking a weak pointer to their owner must not promote it
762 // during construction (but may keep a reference for later promotion).
763 ProxyCallback(const wp<DeviceEffectProxy>& owner,
Andy Hung55a74fd2023-07-13 19:54:47 -0700764 const sp<DeviceEffectManagerCallback>& callback)
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800765 : mProxy(owner), mManagerCallback(callback) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800766
767 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
768 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
769 status_t allocateHalBuffer(size_t size __unused,
770 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700771 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800772 return false;
773 }
774
775 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
776 bool isOutput() const override;
777 bool isOffload() const override { return false; }
778 bool isOffloadOrDirect() const override { return false; }
779 bool isOffloadOrMmap() const override { return false; }
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200780 bool isSpatializer() const override { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800781
782 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200783 audio_channel_mask_t inChannelMask(int id) const override;
784 uint32_t inChannelCount(int id) const override;
785 audio_channel_mask_t outChannelMask() const override;
786 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700787 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800788 size_t frameCount() const override { return 0; }
789 uint32_t latency() const override { return 0; }
790
Andy Hung920f6572022-10-06 12:09:49 -0700791 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override;
792 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800793
Andy Hung6ac17eb2023-06-20 18:56:17 -0700794 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800795 void setVolumeForOutput(float left __unused, float right __unused) const override {}
796
Andy Hung6ac17eb2023-06-20 18:56:17 -0700797 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800798 bool enabled __unused, bool threadLocked __unused) override {}
Shunkai Yaod125e402024-01-20 03:19:06 +0000799 void resetVolume_l() override REQUIRES(audio_utils::EffectChain_Mutex) {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800800 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800801 int32_t activeTrackCnt() const override { return 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700802 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) override;
803 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800804
Andy Hung6ac17eb2023-06-20 18:56:17 -0700805 wp<IAfEffectChain> chain() const override { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800806
Eric Laurentd66d7a12021-07-13 13:35:32 +0200807 bool isAudioPolicyReady() const override {
808 return mManagerCallback->isAudioPolicyReady();
809 }
810
Eric Laurentb82e6b72019-11-22 17:25:04 -0800811 int newEffectId();
812
813 private:
814 const wp<DeviceEffectProxy> mProxy;
Andy Hung55a74fd2023-07-13 19:54:47 -0700815 const sp<DeviceEffectManagerCallback> mManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800816 };
817
Andy Hung8e6b62a2023-07-13 18:11:33 -0700818 status_t checkPort(const IAfPatchPanel::Patch& patch,
Shunkai Yaod125e402024-01-20 03:19:06 +0000819 const struct audio_port_config* port, sp<IAfEffectHandle>* handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800820
821 const AudioDeviceTypeAddr mDevice;
Andy Hung55a74fd2023-07-13 19:54:47 -0700822 const sp<DeviceEffectManagerCallback> mManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800823 const sp<ProxyCallback> mMyCallback;
824
Shunkai Yaod125e402024-01-20 03:19:06 +0000825 audio_utils::mutex& proxyMutex() const
826 RETURN_CAPABILITY(android::audio_utils::DeviceEffectProxy_ProxyMutex) {
827 return mProxyMutex;
828 }
Andy Hunga6f1cbb2023-10-12 18:18:13 -0700829 mutable audio_utils::mutex mProxyMutex{
830 audio_utils::MutexOrder::kDeviceEffectProxy_ProxyMutex};
Andy Hungf65f5a72023-08-29 12:19:17 -0700831 std::map<audio_patch_handle_t, sp<IAfEffectHandle>> mEffectHandles; // protected by mProxyMutex
832 sp<IAfEffectModule> mHalEffect; // protected by mProxyMutex
Eric Laurentb82e6b72019-11-22 17:25:04 -0800833 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
Eric Laurentde8caf42021-08-11 17:19:25 +0200834 const bool mNotifyFramesProcessed;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800835};
Andy Hung6ac17eb2023-06-20 18:56:17 -0700836
837} // namespace android