blob: 2ece5dcf53383f471ef9696291acf7d47cf68214 [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
Andy Hung6ac17eb2023-06-20 18:56:17 -070081 status_t setEnabled(bool enabled, bool fromHandle) override;
82 status_t setEnabled_l(bool enabled) final;
83 bool isEnabled() const final;
84 void setSuspended(bool suspended) final;
85 bool suspended() const final;
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
Andy Hung6ac17eb2023-06-20 18:56:17 -0700102 status_t addHandle(IAfEffectHandle *handle) final;
103 ssize_t disconnectHandle(IAfEffectHandle *handle, bool unpinIfLast) final;
104 ssize_t removeHandle(IAfEffectHandle *handle) final;
105 ssize_t removeHandle_l(IAfEffectHandle *handle) final;
106 IAfEffectHandle* controlHandle_l() final;
107 bool purgeHandles() final;
Eric Laurent41709552019-12-16 19:34:05 -0800108
Andy Hung6ac17eb2023-06-20 18:56:17 -0700109 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) final;
Eric Laurent41709552019-12-16 19:34:05 -0800110
Andy Hung6ac17eb2023-06-20 18:56:17 -0700111 bool isPinned() const final { return mPinned; }
112 void unPin() final { mPinned = false; }
Eric Laurent41709552019-12-16 19:34:05 -0800113
Andy Hungf65f5a72023-08-29 12:19:17 -0700114 audio_utils::mutex& mutex() const final { return mMutex; }
Eric Laurent81784c32012-11-19 14:55:58 -0800115
Andy Hung6ac17eb2023-06-20 18:56:17 -0700116 status_t updatePolicyState() final;
Eric Laurent41709552019-12-16 19:34:05 -0800117
Andy Hung6ac17eb2023-06-20 18:56:17 -0700118 sp<IAfEffectModule> asEffectModule() override { return nullptr; }
119 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() override { return nullptr; }
Eric Laurent6c796322019-04-09 14:13:17 -0700120
Andy Hung6ac17eb2023-06-20 18:56:17 -0700121 void dump(int fd, const Vector<String16>& args) const override;
Eric Laurent81784c32012-11-19 14:55:58 -0800122
Eric Laurentd66d7a12021-07-13 13:35:32 +0200123protected:
124 bool isInternal_l() const {
125 for (auto handle : mHandles) {
126 if (handle->client() != nullptr) {
127 return false;
128 }
129 }
130 return true;
131 }
132
Eric Laurent41709552019-12-16 19:34:05 -0800133 bool mPinned = false;
134
135 DISALLOW_COPY_AND_ASSIGN(EffectBase);
136
Andy Hungf65f5a72023-08-29 12:19:17 -0700137 // mutex for process, commands and handles list protection
138 mutable audio_utils::mutex mMutex;
Andy Hungfda44002021-06-03 17:23:16 -0700139 mediautils::atomic_sp<EffectCallbackInterface> mCallback; // parent effect chain
Eric Laurent41709552019-12-16 19:34:05 -0800140 const int mId; // this instance unique ID
141 const audio_session_t mSessionId; // audio session ID
142 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
143 effect_state mState = IDLE; // current activation state
Eric Laurentd9eb4962019-12-19 09:20:49 -0800144 // effect is suspended: temporarily disabled by framework
145 bool mSuspended = false;
Eric Laurent41709552019-12-16 19:34:05 -0800146
Andy Hung6ac17eb2023-06-20 18:56:17 -0700147 Vector<IAfEffectHandle *> mHandles; // list of client handles
Eric Laurent41709552019-12-16 19:34:05 -0800148 // First handle in mHandles has highest priority and controls the effect module
149
150 // Audio policy effect state management
Andy Hungf65f5a72023-08-29 12:19:17 -0700151 // Mutex protecting transactions with audio policy manager as mutex() cannot
Eric Laurent41709552019-12-16 19:34:05 -0800152 // be held to avoid cross deadlocks with audio policy mutex
Andy Hungf65f5a72023-08-29 12:19:17 -0700153 audio_utils::mutex& policyMutex() const { return mPolicyMutex; }
154 mutable audio_utils::mutex mPolicyMutex;
Eric Laurent41709552019-12-16 19:34:05 -0800155 // Effect is registered in APM or not
156 bool mPolicyRegistered = false;
157 // Effect enabled state communicated to APM. Enabled state corresponds to
158 // state requested by the EffectHandle with control
159 bool mPolicyEnabled = false;
160};
161
162// The EffectModule class is a wrapper object controlling the effect engine implementation
163// in the effect library. It prevents concurrent calls to process() and command() functions
164// from different client threads. It keeps a list of EffectHandle objects corresponding
165// to all client applications using this effect and notifies applications of effect state,
166// control or parameter changes. It manages the activation state machine to send appropriate
167// reset, enable, disable commands to effect engine and provide volume
168// ramping when effects are activated/deactivated.
169// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
170// the attached track(s) to accumulate their auxiliary channel.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700171class EffectModule : public IAfEffectModule, public EffectBase {
Eric Laurent41709552019-12-16 19:34:05 -0800172public:
173 EffectModule(const sp<EffectCallbackInterface>& callabck,
174 effect_descriptor_t *desc,
175 int id,
176 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800177 bool pinned,
178 audio_port_handle_t deviceId);
Andy Hung6ac17eb2023-06-20 18:56:17 -0700179 ~EffectModule() override;
Eric Laurent41709552019-12-16 19:34:05 -0800180
Andy Hung6ac17eb2023-06-20 18:56:17 -0700181 void process() final;
182 bool updateState() final;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700183 status_t command(int32_t cmdCode,
184 const std::vector<uint8_t>& cmdData,
185 int32_t maxReplySize,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700186 std::vector<uint8_t>* reply) final;
Eric Laurent41709552019-12-16 19:34:05 -0800187
Andy Hung6ac17eb2023-06-20 18:56:17 -0700188 void reset_l() final;
189 status_t configure() final;
190 status_t init() final;
191 uint32_t status() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800192 return mStatus;
193 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700194 bool isProcessEnabled() const final;
195 bool isOffloadedOrDirect() const final;
196 bool isVolumeControlEnabled() const final;
197 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final;
198 int16_t *inBuffer() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800199 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
200 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700201 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final;
202 int16_t *outBuffer() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800203 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
204 }
Andy Hung799c8d02021-10-28 17:05:40 -0700205 // Updates the access mode if it is out of date. May issue a new effect configure.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700206 void updateAccessMode() final {
Andy Hung799c8d02021-10-28 17:05:40 -0700207 if (requiredEffectBufferAccessMode() != mConfig.outputCfg.accessMode) {
208 configure();
209 }
210 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700211 status_t setDevices(const AudioDeviceTypeAddrVector &devices) final;
212 status_t setInputDevice(const AudioDeviceTypeAddr &device) final;
213 status_t setVolume(uint32_t *left, uint32_t *right, bool controller) final;
214 status_t setMode(audio_mode_t mode) final;
215 status_t setAudioSource(audio_source_t source) final;
216 status_t start() final;
217 status_t stop() final;
Andy Hung799c8d02021-10-28 17:05:40 -0700218
Andy Hung6ac17eb2023-06-20 18:56:17 -0700219 status_t setOffloaded(bool offloaded, audio_io_handle_t io) final;
220 bool isOffloaded() const final;
221 void addEffectToHal_l() final;
222 void release_l() final;
Eric Laurent41709552019-12-16 19:34:05 -0800223
Andy Hung6ac17eb2023-06-20 18:56:17 -0700224 sp<IAfEffectModule> asEffectModule() final { return this; }
Eric Laurent41709552019-12-16 19:34:05 -0800225
Andy Hung6ac17eb2023-06-20 18:56:17 -0700226 bool isHapticGenerator() const final;
Eric Laurent41709552019-12-16 19:34:05 -0800227
Andy Hung6ac17eb2023-06-20 18:56:17 -0700228 status_t setHapticIntensity(int id, os::HapticScale intensity) final;
229 status_t setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo) final;
jiabineb3bda02020-06-30 14:07:03 -0700230
Andy Hung6ac17eb2023-06-20 18:56:17 -0700231 status_t getConfigs(audio_config_base_t* inputCfg,
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000232 audio_config_base_t* outputCfg,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700233 bool* isOutput) const final;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000234
Andy Hung6ac17eb2023-06-20 18:56:17 -0700235 void dump(int fd, const Vector<String16>& args) const final;
Eric Laurent41709552019-12-16 19:34:05 -0800236
237private:
Eric Laurent81784c32012-11-19 14:55:58 -0800238
239 // Maximum time allocated to effect engines to complete the turn off sequence
240 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
241
Mikhail Naganovbf493082017-04-17 17:37:12 -0700242 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800243
244 status_t start_l();
245 status_t stop_l();
Eric Laurent6b446ce2019-12-13 10:56:31 -0800246 status_t removeEffectFromHal_l();
jiabin8f278ee2019-11-11 12:16:27 -0800247 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Andy Hung799c8d02021-10-28 17:05:40 -0700248 effect_buffer_access_e requiredEffectBufferAccessMode() const {
249 return mConfig.inputCfg.buffer.raw == mConfig.outputCfg.buffer.raw
250 ? EFFECT_BUFFER_ACCESS_WRITE : EFFECT_BUFFER_ACCESS_ACCUMULATE;
251 }
252
jiabin4e246532022-08-23 16:37:30 -0700253 status_t setVolumeInternal(uint32_t *left, uint32_t *right, bool controller);
254
Eric Laurent81784c32012-11-19 14:55:58 -0800255
Eric Laurent81784c32012-11-19 14:55:58 -0800256 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700257 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800258 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
259 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800260 status_t mStatus; // initialization status
Eric Laurent81784c32012-11-19 14:55:58 -0800261 // First handle in mHandles has highest priority and controls the effect module
262 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
263 // sending disable command.
264 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent5baf2af2013-09-12 17:37:00 -0700265 bool mOffloaded; // effect is currently offloaded to the audio DSP
Eric Laurent9f57aa82023-03-17 19:28:37 +0100266 // effect has been added to this HAL input stream
267 audio_io_handle_t mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000268 bool mIsOutput; // direction of the AF thread
rago94a1ee82017-07-21 15:11:02 -0700269
rago94a1ee82017-07-21 15:11:02 -0700270 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800271 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
272 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800273 uint32_t mInChannelCountRequested;
274 uint32_t mOutChannelCountRequested;
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900275
Andy Hungf65f5a72023-08-29 12:19:17 -0700276 template <typename MUTEX>
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900277 class AutoLockReentrant {
278 public:
Andy Hungf65f5a72023-08-29 12:19:17 -0700279 AutoLockReentrant(MUTEX& mutex, pid_t allowedTid)
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900280 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
281 {
282 if (mMutex != nullptr) mMutex->lock();
283 }
284 ~AutoLockReentrant() {
285 if (mMutex != nullptr) mMutex->unlock();
286 }
287 private:
Andy Hungf65f5a72023-08-29 12:19:17 -0700288 MUTEX * const mMutex;
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900289 };
290
291 static constexpr pid_t INVALID_PID = (pid_t)-1;
292 // this tid is allowed to call setVolume() without acquiring the mutex.
293 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent81784c32012-11-19 14:55:58 -0800294};
295
296// The EffectHandle class implements the IEffect interface. It provides resources
297// to receive parameter updates, keeps track of effect control
298// ownership and state and has a pointer to the EffectModule object it is controlling.
299// There is one EffectHandle object for each application controlling (or using)
300// an effect module.
301// The EffectHandle is obtained by calling AudioFlinger::createEffect().
Andy Hung6ac17eb2023-06-20 18:56:17 -0700302class EffectHandle: public IAfEffectHandle, public android::media::BnEffect {
Eric Laurent81784c32012-11-19 14:55:58 -0800303public:
304
Andy Hung6ac17eb2023-06-20 18:56:17 -0700305 EffectHandle(const sp<IAfEffectBase>& effect,
Andy Hung59867e42023-06-27 17:05:02 -0700306 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700307 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +0200308 int32_t priority, bool notifyFramesProcessed);
Andy Hung6ac17eb2023-06-20 18:56:17 -0700309 ~EffectHandle() override;
Andy Hungc747c532022-03-07 21:41:14 -0800310 status_t onTransact(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700311 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) final;
312 status_t initCheck() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800313
314 // IEffect
Andy Hung6ac17eb2023-06-20 18:56:17 -0700315 android::binder::Status enable(int32_t* _aidl_return) final;
316 android::binder::Status disable(int32_t* _aidl_return) final;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700317 android::binder::Status command(int32_t cmdCode,
318 const std::vector<uint8_t>& cmdData,
319 int32_t maxResponseSize,
320 std::vector<uint8_t>* response,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700321 int32_t* _aidl_return) final;
322 android::binder::Status disconnect() final;
323 android::binder::Status getCblk(media::SharedFileRegion* _aidl_return) final;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000324 android::binder::Status getConfig(media::EffectConfig* _config,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700325 int32_t* _aidl_return) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800326
Andy Hung59867e42023-06-27 17:05:02 -0700327 const sp<Client>& client() const final { return mClient; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700328
329 sp<android::media::IEffect> asIEffect() final {
330 return sp<android::media::IEffect>::fromExisting(this);
331 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200332
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700333private:
334 void disconnect(bool unpinIfLast);
Eric Laurent81784c32012-11-19 14:55:58 -0800335
336 // Give or take control of effect module
337 // - hasControl: true if control is given, false if removed
338 // - signal: true client app should be signaled of change, false otherwise
339 // - enabled: state of the effect when control is passed
Andy Hung6ac17eb2023-06-20 18:56:17 -0700340 void setControl(bool hasControl, bool signal, bool enabled) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800341 void commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700342 const std::vector<uint8_t>& cmdData,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700343 const std::vector<uint8_t>& replyData) final;
344 bool enabled() const final { return mEnabled; }
345 void setEnabled(bool enabled) final;
346 void framesProcessed(int32_t frames) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800347
Andy Hung6ac17eb2023-06-20 18:56:17 -0700348public:
Eric Laurent81784c32012-11-19 14:55:58 -0800349 // Getters
Andy Hung6ac17eb2023-06-20 18:56:17 -0700350 wp<IAfEffectBase> effect() const final { return mEffect; }
351 int id() const final {
352 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800353 if (effect == 0) {
354 return 0;
355 }
356 return effect->id();
357 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700358private:
359 int priority() const final { return mPriority; }
360 bool hasControl() const final { return mHasControl; }
361 bool disconnected() const final { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800362
Andy Hung6ac17eb2023-06-20 18:56:17 -0700363 void dumpToBuffer(char* buffer, size_t size) const final;
364
Eric Laurent81784c32012-11-19 14:55:58 -0800365
Mikhail Naganovbf493082017-04-17 17:37:12 -0700366private:
Mikhail Naganovbf493082017-04-17 17:37:12 -0700367 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800368
Andy Hungf65f5a72023-08-29 12:19:17 -0700369 audio_utils::mutex& mutex() const { return mMutex; }
370 mutable audio_utils::mutex mMutex; // protects IEffect method calls
Andy Hung6ac17eb2023-06-20 18:56:17 -0700371 const wp<IAfEffectBase> mEffect; // pointer to controlled EffectModule
Andy Hung318e0242020-12-21 10:33:49 -0800372 const sp<media::IEffectClient> mEffectClient; // callback interface for client notifications
Andy Hung59867e42023-06-27 17:05:02 -0700373 /*const*/ sp<Client> mClient; // client for shared memory allocation, see
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700374 // disconnect()
375 sp<IMemory> mCblkMemory; // shared memory for control block
376 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
377 // shared memory
378 uint8_t* mBuffer; // pointer to parameter area in shared memory
379 int mPriority; // client application priority to control the effect
380 bool mHasControl; // true if this handle is controlling the effect
381 bool mEnabled; // cached enable state: needed when the effect is
382 // restored after being suspended
383 bool mDisconnected; // Set to true by disconnect()
Eric Laurentde8caf42021-08-11 17:19:25 +0200384 const bool mNotifyFramesProcessed; // true if the client callback event
385 // EVENT_FRAMES_PROCESSED must be generated
Eric Laurent81784c32012-11-19 14:55:58 -0800386};
387
388// the EffectChain class represents a group of effects associated to one audio session.
389// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800390// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
391// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800392// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
393// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800394// order corresponding in the effect process order. When attached to a track (session ID !=
395// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800396// it also provide it's own input buffer used by the track as accumulation buffer.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700397class EffectChain : public IAfEffectChain {
Eric Laurent81784c32012-11-19 14:55:58 -0800398public:
Andy Hung583043b2023-07-17 17:05:00 -0700399 EffectChain(const sp<IAfThreadBase>& thread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800400
Andy Hung6ac17eb2023-06-20 18:56:17 -0700401 void process_l() final;
Eric Laurent81784c32012-11-19 14:55:58 -0800402
Andy Hungf65f5a72023-08-29 12:19:17 -0700403 audio_utils::mutex& mutex() const final { return mMutex; }
404
Andy Hung6ac17eb2023-06-20 18:56:17 -0700405 status_t createEffect_l(sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800406 effect_descriptor_t *desc,
407 int id,
408 audio_session_t sessionId,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700409 bool pinned) final;
410 status_t addEffect_l(const sp<IAfEffectModule>& handle) final;
411 status_t addEffect_ll(const sp<IAfEffectModule>& handle) final;
412 size_t removeEffect_l(const sp<IAfEffectModule>& handle, bool release = false) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800413
Andy Hung6ac17eb2023-06-20 18:56:17 -0700414 audio_session_t sessionId() const final { return mSessionId; }
415 void setSessionId(audio_session_t sessionId) final { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800416
Andy Hung6ac17eb2023-06-20 18:56:17 -0700417 sp<IAfEffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor) const final;
418 sp<IAfEffectModule> getEffectFromId_l(int id) const final;
419 sp<IAfEffectModule> getEffectFromType_l(const effect_uuid_t *type) const final;
420 std::vector<int> getEffectIds() const final;
Glenn Kastenc56f3422014-03-21 17:53:17 -0700421 // FIXME use float to improve the dynamic range
Eric Laurent81784c32012-11-19 14:55:58 -0800422
Andy Hung6ac17eb2023-06-20 18:56:17 -0700423 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false) final;
424 void resetVolume_l() final;
425 void setDevices_l(const AudioDeviceTypeAddrVector &devices) final;
426 void setInputDevice_l(const AudioDeviceTypeAddr &device) final;
427 void setMode_l(audio_mode_t mode) final;
428 void setAudioSource_l(audio_source_t source) final;
429
430 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800431 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800432 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700433 float *inBuffer() const final {
Andy Hung181d96f2023-05-23 14:01:03 -0700434 return mInBuffer != 0 ? reinterpret_cast<float*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800435 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700436 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800437 mOutBuffer = buffer;
438 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700439 float *outBuffer() const final {
Andy Hung181d96f2023-05-23 14:01:03 -0700440 return mOutBuffer != 0 ? reinterpret_cast<float*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800441 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700442 void incTrackCnt() final { android_atomic_inc(&mTrackCnt); }
443 void decTrackCnt() final { android_atomic_dec(&mTrackCnt); }
444 int32_t trackCnt() const final { return android_atomic_acquire_load(&mTrackCnt); }
Eric Laurent81784c32012-11-19 14:55:58 -0800445
Andy Hung6ac17eb2023-06-20 18:56:17 -0700446 void incActiveTrackCnt() final { android_atomic_inc(&mActiveTrackCnt);
Eric Laurent81784c32012-11-19 14:55:58 -0800447 mTailBufferCount = mMaxTailBuffers; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700448 void decActiveTrackCnt() final { android_atomic_dec(&mActiveTrackCnt); }
449 int32_t activeTrackCnt() const final {
450 return android_atomic_acquire_load(&mActiveTrackCnt);
451 }
Eric Laurent81784c32012-11-19 14:55:58 -0800452
Andy Hung6ac17eb2023-06-20 18:56:17 -0700453 product_strategy_t strategy() const final { return mStrategy; }
454 void setStrategy(product_strategy_t strategy) final
Eric Laurent81784c32012-11-19 14:55:58 -0800455 { mStrategy = strategy; }
456
Eric Laurentd8365c52017-07-16 15:27:05 -0700457 // suspend or restore effects of the specified type. The number of suspend requests is counted
458 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800459 void setEffectSuspended_l(const effect_uuid_t *type,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700460 bool suspend) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800461 // suspend all eligible effects
Andy Hung6ac17eb2023-06-20 18:56:17 -0700462 void setEffectSuspendedAll_l(bool suspend) final;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800463 // check if effects should be suspended or restored when a given effect is enable or disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700464 void checkSuspendOnEffectEnabled(
465 const sp<IAfEffectModule>& effect, bool enabled) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800466
Andy Hung6ac17eb2023-06-20 18:56:17 -0700467 void clearInputBuffer() final;
Eric Laurent81784c32012-11-19 14:55:58 -0800468
Eric Laurent5baf2af2013-09-12 17:37:00 -0700469 // At least one non offloadable effect in the chain is enabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700470 bool isNonOffloadableEnabled() const final;
471 bool isNonOffloadableEnabled_l() const final;
Eric Laurent813e2a72013-08-31 12:59:48 -0700472
Andy Hung6ac17eb2023-06-20 18:56:17 -0700473 void syncHalEffectsState() final;
Eric Laurent1b928682014-10-02 19:41:47 -0700474
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700475 // flags is an ORed set of audio_output_flags_t which is updated on return.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700476 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700477
478 // flags is an ORed set of audio_input_flags_t which is updated on return.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700479 void checkInputFlagCompatibility(audio_input_flags_t *flags) const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700480
481 // Is this EffectChain compatible with the RAW audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700482 bool isRawCompatible() const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700483
484 // Is this EffectChain compatible with the FAST audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700485 bool isFastCompatible() const final;
Eric Laurent4c415062016-06-17 16:14:16 -0700486
jiabinc658e452022-10-21 20:52:21 +0000487 // Is this EffectChain compatible with the bit-perfect audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700488 bool isBitPerfectCompatible() const final;
jiabinc658e452022-10-21 20:52:21 +0000489
Andy Hungf65f5a72023-08-29 12:19:17 -0700490 // isCompatibleWithThread_l() must be called with thread->mutex() held
Andy Hungab65b182023-09-06 19:41:47 -0700491 bool isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const final
492 REQUIRES(audio_utils::ThreadBase_Mutex);
jiabineb3bda02020-06-30 14:07:03 -0700493
Andy Hungab65b182023-09-06 19:41:47 -0700494 // Requires either IAfThreadBase::mutex() or EffectChain::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -0700495 bool containsHapticGeneratingEffect_l() final;
jiabine70bc7f2020-06-30 22:07:55 -0700496
Andy Hung6ac17eb2023-06-20 18:56:17 -0700497 void setHapticIntensity_l(int id, os::HapticScale intensity) final;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800498
Andy Hung6ac17eb2023-06-20 18:56:17 -0700499 sp<EffectCallbackInterface> effectCallback() const final { return mEffectCallback; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200500
Andy Hung87c693c2023-07-06 20:56:16 -0700501 wp<IAfThreadBase> thread() const final { return mEffectCallback->thread(); }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700502
503 bool isFirstEffect(int id) const final {
504 return !mEffects.isEmpty() && id == mEffects[0]->id();
505 }
506
507 void dump(int fd, const Vector<String16>& args) const final;
508
509 size_t numberOfEffects() const final { return mEffects.size(); }
510
511 sp<IAfEffectModule> getEffectModule(size_t index) const final {
512 return mEffects[index];
513 }
514
Andy Hung87c693c2023-07-06 20:56:16 -0700515 void setThread(const sp<IAfThreadBase>& thread) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800516
Mikhail Naganovbf493082017-04-17 17:37:12 -0700517private:
Eric Laurent6b446ce2019-12-13 10:56:31 -0800518
Andy Hung328d6772021-01-12 12:32:21 -0800519 // For transaction consistency, please consider holding the EffectChain lock before
520 // calling the EffectChain::EffectCallback methods, excepting
521 // createEffectHal and allocateHalBuffer.
522 //
523 // This prevents migration of the EffectChain to another PlaybackThread
524 // for the purposes of the EffectCallback.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800525 class EffectCallback : public EffectCallbackInterface {
526 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800527 // Note: ctors taking a weak pointer to their owner must not promote it
528 // during construction (but may keep a reference for later promotion).
529 EffectCallback(const wp<EffectChain>& owner,
Andy Hung583043b2023-07-17 17:05:00 -0700530 const sp<IAfThreadBase>& thread) // we take a sp<> but store a wp<>.
Andy Hung6626a012021-01-12 13:38:00 -0800531 : mChain(owner)
Andy Hung583043b2023-07-17 17:05:00 -0700532 , mThread(thread) {
533 mThreadType = thread->type();
534 mAfThreadCallback = thread->afThreadCallback();
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800535 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800536
537 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
538 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
539 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700540 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800541
542 audio_io_handle_t io() const override;
543 bool isOutput() const override;
544 bool isOffload() const override;
545 bool isOffloadOrDirect() const override;
546 bool isOffloadOrMmap() const override;
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200547 bool isSpatializer() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800548
549 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200550 audio_channel_mask_t inChannelMask(int id) const override;
551 uint32_t inChannelCount(int id) const override;
552 audio_channel_mask_t outChannelMask() const override;
553 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700554 audio_channel_mask_t hapticChannelMask() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800555 size_t frameCount() const override;
556 uint32_t latency() const override;
557
Andy Hung920f6572022-10-06 12:09:49 -0700558 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override;
559 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700560 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800561 void setVolumeForOutput(float left, float right) const override;
562
563 // check if effects should be suspended/restored when a given effect is enable/disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700564 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800565 bool enabled, bool threadLocked) override;
566 void resetVolume() override;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800567 product_strategy_t strategy() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800568 int32_t activeTrackCnt() const override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700569 void onEffectEnable(const sp<IAfEffectBase>& effect) override;
570 void onEffectDisable(const sp<IAfEffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800571
Andy Hung6ac17eb2023-06-20 18:56:17 -0700572 wp<IAfEffectChain> chain() const final { return mChain; }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800573
Andy Hung6ac17eb2023-06-20 18:56:17 -0700574 bool isAudioPolicyReady() const final {
Andy Hung583043b2023-07-17 17:05:00 -0700575 return mAfThreadCallback->isAudioPolicyReady();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200576 }
577
Andy Hung87c693c2023-07-06 20:56:16 -0700578 wp<IAfThreadBase> thread() const { return mThread.load(); }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800579
Andy Hung87c693c2023-07-06 20:56:16 -0700580 void setThread(const sp<IAfThreadBase>& thread) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800581 mThread = thread;
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200582 mThreadType = thread->type();
Andy Hung583043b2023-07-17 17:05:00 -0700583 mAfThreadCallback = thread->afThreadCallback();
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800584 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800585
586 private:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700587 const wp<IAfEffectChain> mChain;
Andy Hung87c693c2023-07-06 20:56:16 -0700588 mediautils::atomic_wp<IAfThreadBase> mThread;
Andy Hung583043b2023-07-17 17:05:00 -0700589 sp<IAfThreadCallback> mAfThreadCallback;
Andy Hung87c693c2023-07-06 20:56:16 -0700590 IAfThreadBase::type_t mThreadType;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800591 };
592
Mikhail Naganovbf493082017-04-17 17:37:12 -0700593 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800594
595 class SuspendedEffectDesc : public RefBase {
596 public:
597 SuspendedEffectDesc() : mRefCount(0) {}
598
Eric Laurentd8365c52017-07-16 15:27:05 -0700599 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800600 effect_uuid_t mType;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700601 wp<IAfEffectModule> mEffect;
Eric Laurent81784c32012-11-19 14:55:58 -0800602 };
603
604 // get a list of effect modules to suspend when an effect of the type
605 // passed is enabled.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700606 void getSuspendEligibleEffects(Vector<sp<IAfEffectModule>> &effects);
Eric Laurent81784c32012-11-19 14:55:58 -0800607
608 // get an effect module if it is currently enable
Andy Hung6ac17eb2023-06-20 18:56:17 -0700609 sp<IAfEffectModule> getEffectIfEnabled(const effect_uuid_t *type);
Eric Laurent81784c32012-11-19 14:55:58 -0800610 // true if the effect whose descriptor is passed can be suspended
611 // OEMs can modify the rules implemented in this method to exclude specific effect
612 // types or implementations from the suspend/restore mechanism.
613 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
614
Eric Laurentd8365c52017-07-16 15:27:05 -0700615 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
616
Eric Laurent6b446ce2019-12-13 10:56:31 -0800617 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800618
Zhou Songd505c642020-02-20 16:35:37 +0800619 // true if any effect module within the chain has volume control
620 bool hasVolumeControlEnabled_l() const;
621
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900622 void setVolumeForOutput_l(uint32_t left, uint32_t right);
623
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200624 ssize_t getInsertIndex(const effect_descriptor_t& desc);
625
jiabinbd18c8e2023-08-24 21:10:44 +0000626 std::optional<size_t> findVolumeControl_l(size_t from, size_t to) const;
627
Andy Hungf65f5a72023-08-29 12:19:17 -0700628 mutable audio_utils::mutex mMutex; // mutex protecting effect list
Andy Hung6ac17eb2023-06-20 18:56:17 -0700629 Vector<sp<IAfEffectModule>> mEffects; // list of effect modules
Eric Laurent4c415062016-06-17 16:14:16 -0700630 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800631 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
632 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800633
634 // 'volatile' here means these are accessed with atomic operations instead of mutex
635 volatile int32_t mActiveTrackCnt; // number of active tracks connected
636 volatile int32_t mTrackCnt; // number of tracks connected
637
Eric Laurent4c415062016-06-17 16:14:16 -0700638 int32_t mTailBufferCount; // current effect tail buffer count
639 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700640 uint32_t mLeftVolume; // previous volume on left channel
641 uint32_t mRightVolume; // previous volume on right channel
642 uint32_t mNewLeftVolume; // new volume on left channel
643 uint32_t mNewRightVolume; // new volume on right channel
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800644 product_strategy_t mStrategy; // strategy for this effect chain
Eric Laurent4c415062016-06-17 16:14:16 -0700645 // mSuspendedEffects lists all effects currently suspended in the chain.
646 // Use effect type UUID timelow field as key. There is no real risk of identical
647 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700648 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700649 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800650
651 const sp<EffectCallback> mEffectCallback;
jiabinbd18c8e2023-08-24 21:10:44 +0000652
653 wp<IAfEffectModule> mVolumeControlEffect;
Eric Laurent81784c32012-11-19 14:55:58 -0800654};
Eric Laurentb82e6b72019-11-22 17:25:04 -0800655
Andy Hung6ac17eb2023-06-20 18:56:17 -0700656class DeviceEffectProxy : public IAfDeviceEffectProxy, public EffectBase {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800657public:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700658 DeviceEffectProxy(const AudioDeviceTypeAddr& device,
Andy Hung55a74fd2023-07-13 19:54:47 -0700659 const sp<DeviceEffectManagerCallback>& callback,
Eric Laurentde8caf42021-08-11 17:19:25 +0200660 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800661 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
662 mDevice(device), mManagerCallback(callback),
Eric Laurentde8caf42021-08-11 17:19:25 +0200663 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this), callback)),
664 mNotifyFramesProcessed(notifyFramesProcessed) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800665
Andy Hung6ac17eb2023-06-20 18:56:17 -0700666 status_t setEnabled(bool enabled, bool fromHandle) final;
667 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() final { return this; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800668
Andy Hung8e6b62a2023-07-13 18:11:33 -0700669 status_t init(const std::map<audio_patch_handle_t,
670 IAfPatchPanel::Patch>& patches) final;
671
Andy Hung6ac17eb2023-06-20 18:56:17 -0700672 status_t onCreatePatch(audio_patch_handle_t patchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700673 const IAfPatchPanel::Patch& patch) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800674
François Gaffie58e73af2023-02-15 11:47:24 +0100675 status_t onUpdatePatch(audio_patch_handle_t oldPatchHandle, audio_patch_handle_t newPatchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700676 const IAfPatchPanel::Patch& patch) final;
François Gaffie58e73af2023-02-15 11:47:24 +0100677
Andy Hung6ac17eb2023-06-20 18:56:17 -0700678 void onReleasePatch(audio_patch_handle_t patchHandle) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800679
Andy Hung6ac17eb2023-06-20 18:56:17 -0700680 size_t removeEffect(const sp<IAfEffectModule>& effect) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800681
Andy Hung6ac17eb2023-06-20 18:56:17 -0700682 status_t addEffectToHal(const sp<EffectHalInterface>& effect) final;
683 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) final;
684
685 const AudioDeviceTypeAddr& device() const final { return mDevice; };
686 bool isOutput() const final;
687 uint32_t sampleRate() const final;
688 audio_channel_mask_t channelMask() const final;
689 uint32_t channelCount() const final;
690
François Gaffiea2e985b2023-06-09 14:37:56 +0200691 status_t command(int32_t cmdCode,
692 const std::vector<uint8_t>& cmdData,
693 int32_t maxReplySize,
694 std::vector<uint8_t>* reply) final;
695
Andy Hung6ac17eb2023-06-20 18:56:17 -0700696 void dump2(int fd, int spaces) const final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800697
698private:
699
700 class ProxyCallback : public EffectCallbackInterface {
701 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800702 // Note: ctors taking a weak pointer to their owner must not promote it
703 // during construction (but may keep a reference for later promotion).
704 ProxyCallback(const wp<DeviceEffectProxy>& owner,
Andy Hung55a74fd2023-07-13 19:54:47 -0700705 const sp<DeviceEffectManagerCallback>& callback)
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800706 : mProxy(owner), mManagerCallback(callback) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800707
708 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
709 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
710 status_t allocateHalBuffer(size_t size __unused,
711 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700712 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800713 return false;
714 }
715
716 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
717 bool isOutput() const override;
718 bool isOffload() const override { return false; }
719 bool isOffloadOrDirect() const override { return false; }
720 bool isOffloadOrMmap() const override { return false; }
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200721 bool isSpatializer() const override { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800722
723 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200724 audio_channel_mask_t inChannelMask(int id) const override;
725 uint32_t inChannelCount(int id) const override;
726 audio_channel_mask_t outChannelMask() const override;
727 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700728 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800729 size_t frameCount() const override { return 0; }
730 uint32_t latency() const override { return 0; }
731
Andy Hung920f6572022-10-06 12:09:49 -0700732 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override;
733 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800734
Andy Hung6ac17eb2023-06-20 18:56:17 -0700735 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800736 void setVolumeForOutput(float left __unused, float right __unused) const override {}
737
Andy Hung6ac17eb2023-06-20 18:56:17 -0700738 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800739 bool enabled __unused, bool threadLocked __unused) override {}
740 void resetVolume() override {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800741 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800742 int32_t activeTrackCnt() const override { return 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700743 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) override;
744 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800745
Andy Hung6ac17eb2023-06-20 18:56:17 -0700746 wp<IAfEffectChain> chain() const override { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800747
Eric Laurentd66d7a12021-07-13 13:35:32 +0200748 bool isAudioPolicyReady() const override {
749 return mManagerCallback->isAudioPolicyReady();
750 }
751
Eric Laurentb82e6b72019-11-22 17:25:04 -0800752 int newEffectId();
753
754 private:
755 const wp<DeviceEffectProxy> mProxy;
Andy Hung55a74fd2023-07-13 19:54:47 -0700756 const sp<DeviceEffectManagerCallback> mManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800757 };
758
Andy Hung8e6b62a2023-07-13 18:11:33 -0700759 status_t checkPort(const IAfPatchPanel::Patch& patch,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700760 const struct audio_port_config *port, sp<IAfEffectHandle> *handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800761
762 const AudioDeviceTypeAddr mDevice;
Andy Hung55a74fd2023-07-13 19:54:47 -0700763 const sp<DeviceEffectManagerCallback> mManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800764 const sp<ProxyCallback> mMyCallback;
765
Andy Hungf65f5a72023-08-29 12:19:17 -0700766 audio_utils::mutex& proxyMutex() const { return mProxyMutex; }
767 mutable audio_utils::mutex mProxyMutex;
768 std::map<audio_patch_handle_t, sp<IAfEffectHandle>> mEffectHandles; // protected by mProxyMutex
769 sp<IAfEffectModule> mHalEffect; // protected by mProxyMutex
Eric Laurentb82e6b72019-11-22 17:25:04 -0800770 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
Eric Laurentde8caf42021-08-11 17:19:25 +0200771 const bool mNotifyFramesProcessed;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800772};
Andy Hung6ac17eb2023-06-20 18:56:17 -0700773
774} // namespace android