blob: aa5935a6d54feb8d8dfa763f62e62d1c315e71aa [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 Hung6ac17eb2023-06-20 18:56:17 -0700114 void lock() ACQUIRE(mLock) final { mLock.lock(); }
115 void unlock() RELEASE(mLock) final { mLock.unlock(); }
Eric Laurent81784c32012-11-19 14:55:58 -0800116
Andy Hung6ac17eb2023-06-20 18:56:17 -0700117 status_t updatePolicyState() final;
Eric Laurent41709552019-12-16 19:34:05 -0800118
Andy Hung6ac17eb2023-06-20 18:56:17 -0700119 sp<IAfEffectModule> asEffectModule() override { return nullptr; }
120 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() override { return nullptr; }
Eric Laurent6c796322019-04-09 14:13:17 -0700121
Andy Hung6ac17eb2023-06-20 18:56:17 -0700122 void dump(int fd, const Vector<String16>& args) const override;
Eric Laurent81784c32012-11-19 14:55:58 -0800123
Eric Laurentd66d7a12021-07-13 13:35:32 +0200124protected:
125 bool isInternal_l() const {
126 for (auto handle : mHandles) {
127 if (handle->client() != nullptr) {
128 return false;
129 }
130 }
131 return true;
132 }
133
Eric Laurent41709552019-12-16 19:34:05 -0800134 bool mPinned = false;
135
136 DISALLOW_COPY_AND_ASSIGN(EffectBase);
137
Andy Hung6ac17eb2023-06-20 18:56:17 -0700138 mutable Mutex mLock; // mutex for process, commands and handles list protection
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
151 // Mutex protecting transactions with audio policy manager as mLock cannot
152 // be held to avoid cross deadlocks with audio policy mutex
153 Mutex mPolicyLock;
154 // Effect is registered in APM or not
155 bool mPolicyRegistered = false;
156 // Effect enabled state communicated to APM. Enabled state corresponds to
157 // state requested by the EffectHandle with control
158 bool mPolicyEnabled = false;
159};
160
161// The EffectModule class is a wrapper object controlling the effect engine implementation
162// in the effect library. It prevents concurrent calls to process() and command() functions
163// from different client threads. It keeps a list of EffectHandle objects corresponding
164// to all client applications using this effect and notifies applications of effect state,
165// control or parameter changes. It manages the activation state machine to send appropriate
166// reset, enable, disable commands to effect engine and provide volume
167// ramping when effects are activated/deactivated.
168// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
169// the attached track(s) to accumulate their auxiliary channel.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700170class EffectModule : public IAfEffectModule, public EffectBase {
Eric Laurent41709552019-12-16 19:34:05 -0800171public:
172 EffectModule(const sp<EffectCallbackInterface>& callabck,
173 effect_descriptor_t *desc,
174 int id,
175 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800176 bool pinned,
177 audio_port_handle_t deviceId);
Andy Hung6ac17eb2023-06-20 18:56:17 -0700178 ~EffectModule() override;
Eric Laurent41709552019-12-16 19:34:05 -0800179
Andy Hung6ac17eb2023-06-20 18:56:17 -0700180 void process() final;
181 bool updateState() final;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700182 status_t command(int32_t cmdCode,
183 const std::vector<uint8_t>& cmdData,
184 int32_t maxReplySize,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700185 std::vector<uint8_t>* reply) final;
Eric Laurent41709552019-12-16 19:34:05 -0800186
Andy Hung6ac17eb2023-06-20 18:56:17 -0700187 void reset_l() final;
188 status_t configure() final;
189 status_t init() final;
190 uint32_t status() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800191 return mStatus;
192 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700193 bool isProcessEnabled() const final;
194 bool isOffloadedOrDirect() const final;
195 bool isVolumeControlEnabled() const final;
196 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final;
197 int16_t *inBuffer() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800198 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
199 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700200 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final;
201 int16_t *outBuffer() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800202 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
203 }
Andy Hung799c8d02021-10-28 17:05:40 -0700204 // Updates the access mode if it is out of date. May issue a new effect configure.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700205 void updateAccessMode() final {
Andy Hung799c8d02021-10-28 17:05:40 -0700206 if (requiredEffectBufferAccessMode() != mConfig.outputCfg.accessMode) {
207 configure();
208 }
209 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700210 status_t setDevices(const AudioDeviceTypeAddrVector &devices) final;
211 status_t setInputDevice(const AudioDeviceTypeAddr &device) final;
212 status_t setVolume(uint32_t *left, uint32_t *right, bool controller) final;
213 status_t setMode(audio_mode_t mode) final;
214 status_t setAudioSource(audio_source_t source) final;
215 status_t start() final;
216 status_t stop() final;
Andy Hung799c8d02021-10-28 17:05:40 -0700217
Andy Hung6ac17eb2023-06-20 18:56:17 -0700218 status_t setOffloaded(bool offloaded, audio_io_handle_t io) final;
219 bool isOffloaded() const final;
220 void addEffectToHal_l() final;
221 void release_l() final;
Eric Laurent41709552019-12-16 19:34:05 -0800222
Andy Hung6ac17eb2023-06-20 18:56:17 -0700223 sp<IAfEffectModule> asEffectModule() final { return this; }
Eric Laurent41709552019-12-16 19:34:05 -0800224
Andy Hung6ac17eb2023-06-20 18:56:17 -0700225 bool isHapticGenerator() const final;
Eric Laurent41709552019-12-16 19:34:05 -0800226
Andy Hung6ac17eb2023-06-20 18:56:17 -0700227 status_t setHapticIntensity(int id, os::HapticScale intensity) final;
228 status_t setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo) final;
jiabineb3bda02020-06-30 14:07:03 -0700229
Andy Hung6ac17eb2023-06-20 18:56:17 -0700230 status_t getConfigs(audio_config_base_t* inputCfg,
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000231 audio_config_base_t* outputCfg,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700232 bool* isOutput) const final;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000233
Andy Hung6ac17eb2023-06-20 18:56:17 -0700234 void dump(int fd, const Vector<String16>& args) const final;
Eric Laurent41709552019-12-16 19:34:05 -0800235
236private:
Eric Laurent81784c32012-11-19 14:55:58 -0800237
238 // Maximum time allocated to effect engines to complete the turn off sequence
239 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
240
Mikhail Naganovbf493082017-04-17 17:37:12 -0700241 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800242
243 status_t start_l();
244 status_t stop_l();
Eric Laurent6b446ce2019-12-13 10:56:31 -0800245 status_t removeEffectFromHal_l();
jiabin8f278ee2019-11-11 12:16:27 -0800246 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Andy Hung799c8d02021-10-28 17:05:40 -0700247 effect_buffer_access_e requiredEffectBufferAccessMode() const {
248 return mConfig.inputCfg.buffer.raw == mConfig.outputCfg.buffer.raw
249 ? EFFECT_BUFFER_ACCESS_WRITE : EFFECT_BUFFER_ACCESS_ACCUMULATE;
250 }
251
jiabin4e246532022-08-23 16:37:30 -0700252 status_t setVolumeInternal(uint32_t *left, uint32_t *right, bool controller);
253
Eric Laurent81784c32012-11-19 14:55:58 -0800254
Eric Laurent81784c32012-11-19 14:55:58 -0800255 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700256 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800257 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
258 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800259 status_t mStatus; // initialization status
Eric Laurent81784c32012-11-19 14:55:58 -0800260 // First handle in mHandles has highest priority and controls the effect module
261 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
262 // sending disable command.
263 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent5baf2af2013-09-12 17:37:00 -0700264 bool mOffloaded; // effect is currently offloaded to the audio DSP
Eric Laurent9f57aa82023-03-17 19:28:37 +0100265 // effect has been added to this HAL input stream
266 audio_io_handle_t mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000267 bool mIsOutput; // direction of the AF thread
rago94a1ee82017-07-21 15:11:02 -0700268
rago94a1ee82017-07-21 15:11:02 -0700269 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800270 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
271 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800272 uint32_t mInChannelCountRequested;
273 uint32_t mOutChannelCountRequested;
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900274
275 class AutoLockReentrant {
276 public:
277 AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
278 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
279 {
280 if (mMutex != nullptr) mMutex->lock();
281 }
282 ~AutoLockReentrant() {
283 if (mMutex != nullptr) mMutex->unlock();
284 }
285 private:
286 Mutex * const mMutex;
287 };
288
289 static constexpr pid_t INVALID_PID = (pid_t)-1;
290 // this tid is allowed to call setVolume() without acquiring the mutex.
291 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent81784c32012-11-19 14:55:58 -0800292};
293
294// The EffectHandle class implements the IEffect interface. It provides resources
295// to receive parameter updates, keeps track of effect control
296// ownership and state and has a pointer to the EffectModule object it is controlling.
297// There is one EffectHandle object for each application controlling (or using)
298// an effect module.
299// The EffectHandle is obtained by calling AudioFlinger::createEffect().
Andy Hung6ac17eb2023-06-20 18:56:17 -0700300class EffectHandle: public IAfEffectHandle, public android::media::BnEffect {
Eric Laurent81784c32012-11-19 14:55:58 -0800301public:
302
Andy Hung6ac17eb2023-06-20 18:56:17 -0700303 EffectHandle(const sp<IAfEffectBase>& effect,
Andy Hung59867e42023-06-27 17:05:02 -0700304 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700305 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +0200306 int32_t priority, bool notifyFramesProcessed);
Andy Hung6ac17eb2023-06-20 18:56:17 -0700307 ~EffectHandle() override;
Andy Hungc747c532022-03-07 21:41:14 -0800308 status_t onTransact(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700309 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) final;
310 status_t initCheck() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800311
312 // IEffect
Andy Hung6ac17eb2023-06-20 18:56:17 -0700313 android::binder::Status enable(int32_t* _aidl_return) final;
314 android::binder::Status disable(int32_t* _aidl_return) final;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700315 android::binder::Status command(int32_t cmdCode,
316 const std::vector<uint8_t>& cmdData,
317 int32_t maxResponseSize,
318 std::vector<uint8_t>* response,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700319 int32_t* _aidl_return) final;
320 android::binder::Status disconnect() final;
321 android::binder::Status getCblk(media::SharedFileRegion* _aidl_return) final;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000322 android::binder::Status getConfig(media::EffectConfig* _config,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700323 int32_t* _aidl_return) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800324
Andy Hung59867e42023-06-27 17:05:02 -0700325 const sp<Client>& client() const final { return mClient; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700326
327 sp<android::media::IEffect> asIEffect() final {
328 return sp<android::media::IEffect>::fromExisting(this);
329 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200330
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700331private:
332 void disconnect(bool unpinIfLast);
Eric Laurent81784c32012-11-19 14:55:58 -0800333
334 // Give or take control of effect module
335 // - hasControl: true if control is given, false if removed
336 // - signal: true client app should be signaled of change, false otherwise
337 // - enabled: state of the effect when control is passed
Andy Hung6ac17eb2023-06-20 18:56:17 -0700338 void setControl(bool hasControl, bool signal, bool enabled) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800339 void commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700340 const std::vector<uint8_t>& cmdData,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700341 const std::vector<uint8_t>& replyData) final;
342 bool enabled() const final { return mEnabled; }
343 void setEnabled(bool enabled) final;
344 void framesProcessed(int32_t frames) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800345
Andy Hung6ac17eb2023-06-20 18:56:17 -0700346public:
Eric Laurent81784c32012-11-19 14:55:58 -0800347 // Getters
Andy Hung6ac17eb2023-06-20 18:56:17 -0700348 wp<IAfEffectBase> effect() const final { return mEffect; }
349 int id() const final {
350 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800351 if (effect == 0) {
352 return 0;
353 }
354 return effect->id();
355 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700356private:
357 int priority() const final { return mPriority; }
358 bool hasControl() const final { return mHasControl; }
359 bool disconnected() const final { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800360
Andy Hung6ac17eb2023-06-20 18:56:17 -0700361 void dumpToBuffer(char* buffer, size_t size) const final;
362
Eric Laurent81784c32012-11-19 14:55:58 -0800363
Mikhail Naganovbf493082017-04-17 17:37:12 -0700364private:
Mikhail Naganovbf493082017-04-17 17:37:12 -0700365 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800366
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700367 Mutex mLock; // protects IEffect method calls
Andy Hung6ac17eb2023-06-20 18:56:17 -0700368 const wp<IAfEffectBase> mEffect; // pointer to controlled EffectModule
Andy Hung318e0242020-12-21 10:33:49 -0800369 const sp<media::IEffectClient> mEffectClient; // callback interface for client notifications
Andy Hung59867e42023-06-27 17:05:02 -0700370 /*const*/ sp<Client> mClient; // client for shared memory allocation, see
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700371 // disconnect()
372 sp<IMemory> mCblkMemory; // shared memory for control block
373 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
374 // shared memory
375 uint8_t* mBuffer; // pointer to parameter area in shared memory
376 int mPriority; // client application priority to control the effect
377 bool mHasControl; // true if this handle is controlling the effect
378 bool mEnabled; // cached enable state: needed when the effect is
379 // restored after being suspended
380 bool mDisconnected; // Set to true by disconnect()
Eric Laurentde8caf42021-08-11 17:19:25 +0200381 const bool mNotifyFramesProcessed; // true if the client callback event
382 // EVENT_FRAMES_PROCESSED must be generated
Eric Laurent81784c32012-11-19 14:55:58 -0800383};
384
385// the EffectChain class represents a group of effects associated to one audio session.
386// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800387// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
388// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800389// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
390// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800391// order corresponding in the effect process order. When attached to a track (session ID !=
392// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800393// it also provide it's own input buffer used by the track as accumulation buffer.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700394class EffectChain : public IAfEffectChain {
Eric Laurent81784c32012-11-19 14:55:58 -0800395public:
Andy Hung583043b2023-07-17 17:05:00 -0700396 EffectChain(const sp<IAfThreadBase>& thread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800397
Andy Hung6ac17eb2023-06-20 18:56:17 -0700398 void process_l() final;
Eric Laurent81784c32012-11-19 14:55:58 -0800399
Andy Hung6ac17eb2023-06-20 18:56:17 -0700400 void lock() ACQUIRE(mLock) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800401 mLock.lock();
402 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700403 void unlock() RELEASE(mLock) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800404 mLock.unlock();
405 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700406 status_t createEffect_l(sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800407 effect_descriptor_t *desc,
408 int id,
409 audio_session_t sessionId,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700410 bool pinned) final;
411 status_t addEffect_l(const sp<IAfEffectModule>& handle) final;
412 status_t addEffect_ll(const sp<IAfEffectModule>& handle) final;
413 size_t removeEffect_l(const sp<IAfEffectModule>& handle, bool release = false) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800414
Andy Hung6ac17eb2023-06-20 18:56:17 -0700415 audio_session_t sessionId() const final { return mSessionId; }
416 void setSessionId(audio_session_t sessionId) final { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800417
Andy Hung6ac17eb2023-06-20 18:56:17 -0700418 sp<IAfEffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor) const final;
419 sp<IAfEffectModule> getEffectFromId_l(int id) const final;
420 sp<IAfEffectModule> getEffectFromType_l(const effect_uuid_t *type) const final;
421 std::vector<int> getEffectIds() const final;
Glenn Kastenc56f3422014-03-21 17:53:17 -0700422 // FIXME use float to improve the dynamic range
Eric Laurent81784c32012-11-19 14:55:58 -0800423
Andy Hung6ac17eb2023-06-20 18:56:17 -0700424 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false) final;
425 void resetVolume_l() final;
426 void setDevices_l(const AudioDeviceTypeAddrVector &devices) final;
427 void setInputDevice_l(const AudioDeviceTypeAddr &device) final;
428 void setMode_l(audio_mode_t mode) final;
429 void setAudioSource_l(audio_source_t source) final;
430
431 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800432 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800433 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700434 float *inBuffer() const final {
Andy Hung181d96f2023-05-23 14:01:03 -0700435 return mInBuffer != 0 ? reinterpret_cast<float*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800436 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700437 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800438 mOutBuffer = buffer;
439 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700440 float *outBuffer() const final {
Andy Hung181d96f2023-05-23 14:01:03 -0700441 return mOutBuffer != 0 ? reinterpret_cast<float*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800442 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700443 void incTrackCnt() final { android_atomic_inc(&mTrackCnt); }
444 void decTrackCnt() final { android_atomic_dec(&mTrackCnt); }
445 int32_t trackCnt() const final { return android_atomic_acquire_load(&mTrackCnt); }
Eric Laurent81784c32012-11-19 14:55:58 -0800446
Andy Hung6ac17eb2023-06-20 18:56:17 -0700447 void incActiveTrackCnt() final { android_atomic_inc(&mActiveTrackCnt);
Eric Laurent81784c32012-11-19 14:55:58 -0800448 mTailBufferCount = mMaxTailBuffers; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700449 void decActiveTrackCnt() final { android_atomic_dec(&mActiveTrackCnt); }
450 int32_t activeTrackCnt() const final {
451 return android_atomic_acquire_load(&mActiveTrackCnt);
452 }
Eric Laurent81784c32012-11-19 14:55:58 -0800453
Andy Hung6ac17eb2023-06-20 18:56:17 -0700454 product_strategy_t strategy() const final { return mStrategy; }
455 void setStrategy(product_strategy_t strategy) final
Eric Laurent81784c32012-11-19 14:55:58 -0800456 { mStrategy = strategy; }
457
Eric Laurentd8365c52017-07-16 15:27:05 -0700458 // suspend or restore effects of the specified type. The number of suspend requests is counted
459 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800460 void setEffectSuspended_l(const effect_uuid_t *type,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700461 bool suspend) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800462 // suspend all eligible effects
Andy Hung6ac17eb2023-06-20 18:56:17 -0700463 void setEffectSuspendedAll_l(bool suspend) final;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800464 // check if effects should be suspended or restored when a given effect is enable or disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700465 void checkSuspendOnEffectEnabled(
466 const sp<IAfEffectModule>& effect, bool enabled) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800467
Andy Hung6ac17eb2023-06-20 18:56:17 -0700468 void clearInputBuffer() final;
Eric Laurent81784c32012-11-19 14:55:58 -0800469
Eric Laurent5baf2af2013-09-12 17:37:00 -0700470 // At least one non offloadable effect in the chain is enabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700471 bool isNonOffloadableEnabled() const final;
472 bool isNonOffloadableEnabled_l() const final;
Eric Laurent813e2a72013-08-31 12:59:48 -0700473
Andy Hung6ac17eb2023-06-20 18:56:17 -0700474 void syncHalEffectsState() final;
Eric Laurent1b928682014-10-02 19:41:47 -0700475
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700476 // flags is an ORed set of audio_output_flags_t which is updated on return.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700477 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700478
479 // flags is an ORed set of audio_input_flags_t which is updated on return.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700480 void checkInputFlagCompatibility(audio_input_flags_t *flags) const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700481
482 // Is this EffectChain compatible with the RAW audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700483 bool isRawCompatible() const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700484
485 // Is this EffectChain compatible with the FAST audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700486 bool isFastCompatible() const final;
Eric Laurent4c415062016-06-17 16:14:16 -0700487
jiabinc658e452022-10-21 20:52:21 +0000488 // Is this EffectChain compatible with the bit-perfect audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700489 bool isBitPerfectCompatible() const final;
jiabinc658e452022-10-21 20:52:21 +0000490
Eric Laurent4c415062016-06-17 16:14:16 -0700491 // isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hung87c693c2023-07-06 20:56:16 -0700492 bool isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const final;
jiabineb3bda02020-06-30 14:07:03 -0700493
Andy Hung6ac17eb2023-06-20 18:56:17 -0700494 bool containsHapticGeneratingEffect_l() final;
jiabine70bc7f2020-06-30 22:07:55 -0700495
Andy Hung6ac17eb2023-06-20 18:56:17 -0700496 void setHapticIntensity_l(int id, os::HapticScale intensity) final;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800497
Andy Hung6ac17eb2023-06-20 18:56:17 -0700498 sp<EffectCallbackInterface> effectCallback() const final { return mEffectCallback; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200499
Andy Hung87c693c2023-07-06 20:56:16 -0700500 wp<IAfThreadBase> thread() const final { return mEffectCallback->thread(); }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700501
502 bool isFirstEffect(int id) const final {
503 return !mEffects.isEmpty() && id == mEffects[0]->id();
504 }
505
506 void dump(int fd, const Vector<String16>& args) const final;
507
508 size_t numberOfEffects() const final { return mEffects.size(); }
509
510 sp<IAfEffectModule> getEffectModule(size_t index) const final {
511 return mEffects[index];
512 }
513
Andy Hung87c693c2023-07-06 20:56:16 -0700514 void setThread(const sp<IAfThreadBase>& thread) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800515
Mikhail Naganovbf493082017-04-17 17:37:12 -0700516private:
Eric Laurent6b446ce2019-12-13 10:56:31 -0800517
Andy Hung328d6772021-01-12 12:32:21 -0800518 // For transaction consistency, please consider holding the EffectChain lock before
519 // calling the EffectChain::EffectCallback methods, excepting
520 // createEffectHal and allocateHalBuffer.
521 //
522 // This prevents migration of the EffectChain to another PlaybackThread
523 // for the purposes of the EffectCallback.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800524 class EffectCallback : public EffectCallbackInterface {
525 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800526 // Note: ctors taking a weak pointer to their owner must not promote it
527 // during construction (but may keep a reference for later promotion).
528 EffectCallback(const wp<EffectChain>& owner,
Andy Hung583043b2023-07-17 17:05:00 -0700529 const sp<IAfThreadBase>& thread) // we take a sp<> but store a wp<>.
Andy Hung6626a012021-01-12 13:38:00 -0800530 : mChain(owner)
Andy Hung583043b2023-07-17 17:05:00 -0700531 , mThread(thread) {
532 mThreadType = thread->type();
533 mAfThreadCallback = thread->afThreadCallback();
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800534 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800535
536 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
537 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
538 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700539 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800540
541 audio_io_handle_t io() const override;
542 bool isOutput() const override;
543 bool isOffload() const override;
544 bool isOffloadOrDirect() const override;
545 bool isOffloadOrMmap() const override;
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200546 bool isSpatializer() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800547
548 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200549 audio_channel_mask_t inChannelMask(int id) const override;
550 uint32_t inChannelCount(int id) const override;
551 audio_channel_mask_t outChannelMask() const override;
552 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700553 audio_channel_mask_t hapticChannelMask() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800554 size_t frameCount() const override;
555 uint32_t latency() const override;
556
Andy Hung920f6572022-10-06 12:09:49 -0700557 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override;
558 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700559 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800560 void setVolumeForOutput(float left, float right) const override;
561
562 // check if effects should be suspended/restored when a given effect is enable/disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700563 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800564 bool enabled, bool threadLocked) override;
565 void resetVolume() override;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800566 product_strategy_t strategy() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800567 int32_t activeTrackCnt() const override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700568 void onEffectEnable(const sp<IAfEffectBase>& effect) override;
569 void onEffectDisable(const sp<IAfEffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800570
Andy Hung6ac17eb2023-06-20 18:56:17 -0700571 wp<IAfEffectChain> chain() const final { return mChain; }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800572
Andy Hung6ac17eb2023-06-20 18:56:17 -0700573 bool isAudioPolicyReady() const final {
Andy Hung583043b2023-07-17 17:05:00 -0700574 return mAfThreadCallback->isAudioPolicyReady();
Eric Laurentd66d7a12021-07-13 13:35:32 +0200575 }
576
Andy Hung87c693c2023-07-06 20:56:16 -0700577 wp<IAfThreadBase> thread() const { return mThread.load(); }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800578
Andy Hung87c693c2023-07-06 20:56:16 -0700579 void setThread(const sp<IAfThreadBase>& thread) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800580 mThread = thread;
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200581 mThreadType = thread->type();
Andy Hung583043b2023-07-17 17:05:00 -0700582 mAfThreadCallback = thread->afThreadCallback();
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800583 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800584
585 private:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700586 const wp<IAfEffectChain> mChain;
Andy Hung87c693c2023-07-06 20:56:16 -0700587 mediautils::atomic_wp<IAfThreadBase> mThread;
Andy Hung583043b2023-07-17 17:05:00 -0700588 sp<IAfThreadCallback> mAfThreadCallback;
Andy Hung87c693c2023-07-06 20:56:16 -0700589 IAfThreadBase::type_t mThreadType;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800590 };
591
Mikhail Naganovbf493082017-04-17 17:37:12 -0700592 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800593
594 class SuspendedEffectDesc : public RefBase {
595 public:
596 SuspendedEffectDesc() : mRefCount(0) {}
597
Eric Laurentd8365c52017-07-16 15:27:05 -0700598 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800599 effect_uuid_t mType;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700600 wp<IAfEffectModule> mEffect;
Eric Laurent81784c32012-11-19 14:55:58 -0800601 };
602
603 // get a list of effect modules to suspend when an effect of the type
604 // passed is enabled.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700605 void getSuspendEligibleEffects(Vector<sp<IAfEffectModule>> &effects);
Eric Laurent81784c32012-11-19 14:55:58 -0800606
607 // get an effect module if it is currently enable
Andy Hung6ac17eb2023-06-20 18:56:17 -0700608 sp<IAfEffectModule> getEffectIfEnabled(const effect_uuid_t *type);
Eric Laurent81784c32012-11-19 14:55:58 -0800609 // true if the effect whose descriptor is passed can be suspended
610 // OEMs can modify the rules implemented in this method to exclude specific effect
611 // types or implementations from the suspend/restore mechanism.
612 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
613
Eric Laurentd8365c52017-07-16 15:27:05 -0700614 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
615
Eric Laurent6b446ce2019-12-13 10:56:31 -0800616 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800617
Zhou Songd505c642020-02-20 16:35:37 +0800618 // true if any effect module within the chain has volume control
619 bool hasVolumeControlEnabled_l() const;
620
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900621 void setVolumeForOutput_l(uint32_t left, uint32_t right);
622
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200623 ssize_t getInsertIndex(const effect_descriptor_t& desc);
624
jiabinbd18c8e2023-08-24 21:10:44 +0000625 std::optional<size_t> findVolumeControl_l(size_t from, size_t to) const;
626
Eric Laurent4c415062016-06-17 16:14:16 -0700627 mutable Mutex mLock; // mutex protecting effect list
Andy Hung6ac17eb2023-06-20 18:56:17 -0700628 Vector<sp<IAfEffectModule>> mEffects; // list of effect modules
Eric Laurent4c415062016-06-17 16:14:16 -0700629 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800630 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
631 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800632
633 // 'volatile' here means these are accessed with atomic operations instead of mutex
634 volatile int32_t mActiveTrackCnt; // number of active tracks connected
635 volatile int32_t mTrackCnt; // number of tracks connected
636
Eric Laurent4c415062016-06-17 16:14:16 -0700637 int32_t mTailBufferCount; // current effect tail buffer count
638 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700639 uint32_t mLeftVolume; // previous volume on left channel
640 uint32_t mRightVolume; // previous volume on right channel
641 uint32_t mNewLeftVolume; // new volume on left channel
642 uint32_t mNewRightVolume; // new volume on right channel
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800643 product_strategy_t mStrategy; // strategy for this effect chain
Eric Laurent4c415062016-06-17 16:14:16 -0700644 // mSuspendedEffects lists all effects currently suspended in the chain.
645 // Use effect type UUID timelow field as key. There is no real risk of identical
646 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700647 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700648 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800649
650 const sp<EffectCallback> mEffectCallback;
jiabinbd18c8e2023-08-24 21:10:44 +0000651
652 wp<IAfEffectModule> mVolumeControlEffect;
Eric Laurent81784c32012-11-19 14:55:58 -0800653};
Eric Laurentb82e6b72019-11-22 17:25:04 -0800654
Andy Hung6ac17eb2023-06-20 18:56:17 -0700655class DeviceEffectProxy : public IAfDeviceEffectProxy, public EffectBase {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800656public:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700657 DeviceEffectProxy(const AudioDeviceTypeAddr& device,
Andy Hung55a74fd2023-07-13 19:54:47 -0700658 const sp<DeviceEffectManagerCallback>& callback,
Eric Laurentde8caf42021-08-11 17:19:25 +0200659 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800660 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
661 mDevice(device), mManagerCallback(callback),
Eric Laurentde8caf42021-08-11 17:19:25 +0200662 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this), callback)),
663 mNotifyFramesProcessed(notifyFramesProcessed) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800664
Andy Hung6ac17eb2023-06-20 18:56:17 -0700665 status_t setEnabled(bool enabled, bool fromHandle) final;
666 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() final { return this; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800667
Andy Hung8e6b62a2023-07-13 18:11:33 -0700668 status_t init(const std::map<audio_patch_handle_t,
669 IAfPatchPanel::Patch>& patches) final;
670
Andy Hung6ac17eb2023-06-20 18:56:17 -0700671 status_t onCreatePatch(audio_patch_handle_t patchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700672 const IAfPatchPanel::Patch& patch) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800673
François Gaffie58e73af2023-02-15 11:47:24 +0100674 status_t onUpdatePatch(audio_patch_handle_t oldPatchHandle, audio_patch_handle_t newPatchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700675 const IAfPatchPanel::Patch& patch) final;
François Gaffie58e73af2023-02-15 11:47:24 +0100676
Andy Hung6ac17eb2023-06-20 18:56:17 -0700677 void onReleasePatch(audio_patch_handle_t patchHandle) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800678
Andy Hung6ac17eb2023-06-20 18:56:17 -0700679 size_t removeEffect(const sp<IAfEffectModule>& effect) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800680
Andy Hung6ac17eb2023-06-20 18:56:17 -0700681 status_t addEffectToHal(const sp<EffectHalInterface>& effect) final;
682 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) final;
683
684 const AudioDeviceTypeAddr& device() const final { return mDevice; };
685 bool isOutput() const final;
686 uint32_t sampleRate() const final;
687 audio_channel_mask_t channelMask() const final;
688 uint32_t channelCount() const final;
689
François Gaffiea2e985b2023-06-09 14:37:56 +0200690 status_t command(int32_t cmdCode,
691 const std::vector<uint8_t>& cmdData,
692 int32_t maxReplySize,
693 std::vector<uint8_t>* reply) final;
694
Andy Hung6ac17eb2023-06-20 18:56:17 -0700695 void dump2(int fd, int spaces) const final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800696
697private:
698
699 class ProxyCallback : public EffectCallbackInterface {
700 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800701 // Note: ctors taking a weak pointer to their owner must not promote it
702 // during construction (but may keep a reference for later promotion).
703 ProxyCallback(const wp<DeviceEffectProxy>& owner,
Andy Hung55a74fd2023-07-13 19:54:47 -0700704 const sp<DeviceEffectManagerCallback>& callback)
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800705 : mProxy(owner), mManagerCallback(callback) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800706
707 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
708 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
709 status_t allocateHalBuffer(size_t size __unused,
710 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700711 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800712 return false;
713 }
714
715 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
716 bool isOutput() const override;
717 bool isOffload() const override { return false; }
718 bool isOffloadOrDirect() const override { return false; }
719 bool isOffloadOrMmap() const override { return false; }
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200720 bool isSpatializer() const override { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800721
722 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200723 audio_channel_mask_t inChannelMask(int id) const override;
724 uint32_t inChannelCount(int id) const override;
725 audio_channel_mask_t outChannelMask() const override;
726 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700727 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800728 size_t frameCount() const override { return 0; }
729 uint32_t latency() const override { return 0; }
730
Andy Hung920f6572022-10-06 12:09:49 -0700731 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override;
732 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800733
Andy Hung6ac17eb2023-06-20 18:56:17 -0700734 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800735 void setVolumeForOutput(float left __unused, float right __unused) const override {}
736
Andy Hung6ac17eb2023-06-20 18:56:17 -0700737 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800738 bool enabled __unused, bool threadLocked __unused) override {}
739 void resetVolume() override {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800740 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800741 int32_t activeTrackCnt() const override { return 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700742 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) override;
743 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800744
Andy Hung6ac17eb2023-06-20 18:56:17 -0700745 wp<IAfEffectChain> chain() const override { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800746
Eric Laurentd66d7a12021-07-13 13:35:32 +0200747 bool isAudioPolicyReady() const override {
748 return mManagerCallback->isAudioPolicyReady();
749 }
750
Eric Laurentb82e6b72019-11-22 17:25:04 -0800751 int newEffectId();
752
753 private:
754 const wp<DeviceEffectProxy> mProxy;
Andy Hung55a74fd2023-07-13 19:54:47 -0700755 const sp<DeviceEffectManagerCallback> mManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800756 };
757
Andy Hung8e6b62a2023-07-13 18:11:33 -0700758 status_t checkPort(const IAfPatchPanel::Patch& patch,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700759 const struct audio_port_config *port, sp<IAfEffectHandle> *handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800760
761 const AudioDeviceTypeAddr mDevice;
Andy Hung55a74fd2023-07-13 19:54:47 -0700762 const sp<DeviceEffectManagerCallback> mManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800763 const sp<ProxyCallback> mMyCallback;
764
Andy Hung6ac17eb2023-06-20 18:56:17 -0700765 mutable Mutex mProxyLock;
766 std::map<audio_patch_handle_t, sp<IAfEffectHandle>> mEffectHandles; // protected by mProxyLock
767 sp<IAfEffectModule> mHalEffect; // protected by mProxyLock
Eric Laurentb82e6b72019-11-22 17:25:04 -0800768 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
Eric Laurentde8caf42021-08-11 17:19:25 +0200769 const bool mNotifyFramesProcessed;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800770};
Andy Hung6ac17eb2023-06-20 18:56:17 -0700771
772} // namespace android