blob: 0c0af736188c9b2d72b6b6e405bc85ff4f0f42db [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 Hung6ac17eb2023-06-20 18:56:17 -070018namespace android {
Eric Laurent81784c32012-11-19 14:55:58 -080019
20//--- Audio Effect Management
21
Eric Laurent41709552019-12-16 19:34:05 -080022// EffectBase(EffectModule) and EffectChain classes both have their own mutex to protect
Eric Laurent81784c32012-11-19 14:55:58 -080023// state changes or resource modifications. Always respect the following order
24// if multiple mutexes must be acquired to avoid cross deadlock:
Eric Laurent41709552019-12-16 19:34:05 -080025// AudioFlinger -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
26// AudioHandle -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
Eric Laurent6b446ce2019-12-13 10:56:31 -080027
28// NOTE: When implementing the EffectCallbackInterface, in an EffectChain or other, it is important
29// to pay attention to this locking order as some callback methods can be called from a state where
30// EffectModule and/or EffectChain mutexes are held.
31
Eric Laurenteb3c3372013-09-25 12:25:29 -070032// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
Eric Laurent3bc859b2016-12-05 11:07:22 -080033// startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or
34// Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService
35// methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order.
Eric Laurent81784c32012-11-19 14:55:58 -080036
Eric Laurent41709552019-12-16 19:34:05 -080037
38// The EffectBase class contains common properties, state and behavior for and EffectModule or
39// other derived classes managing an audio effect instance within the effect framework.
40// It also contains the class mutex (see comment on locking order above).
Andy Hung6ac17eb2023-06-20 18:56:17 -070041class EffectBase : public virtual IAfEffectBase {
Eric Laurent81784c32012-11-19 14:55:58 -080042public:
Eric Laurent41709552019-12-16 19:34:05 -080043 EffectBase(const sp<EffectCallbackInterface>& callback,
44 effect_descriptor_t *desc,
45 int id,
46 audio_session_t sessionId,
47 bool pinned);
48
Andy Hung6ac17eb2023-06-20 18:56:17 -070049 int id() const final { return mId; }
50 effect_state state() const final {
Eric Laurent81784c32012-11-19 14:55:58 -080051 return mState;
52 }
Andy Hung6ac17eb2023-06-20 18:56:17 -070053 audio_session_t sessionId() const final {
Eric Laurent81784c32012-11-19 14:55:58 -080054 return mSessionId;
55 }
Andy Hung6ac17eb2023-06-20 18:56:17 -070056 const effect_descriptor_t& desc() const final { return mDescriptor; }
57 bool isOffloadable() const final
Eric Laurent5baf2af2013-09-12 17:37:00 -070058 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -070059 bool isImplementationSoftware() const final
Eric Laurent4c415062016-06-17 16:14:16 -070060 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -070061 bool isProcessImplemented() const final
Eric Laurent6dd0fd92016-09-15 12:44:53 -070062 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -070063 bool isVolumeControl() const
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +090064 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
65 == EFFECT_FLAG_VOLUME_CTRL; }
Andy Hung6ac17eb2023-06-20 18:56:17 -070066 bool isVolumeMonitor() const final
Jasmine Cha934ecfb2019-01-23 18:19:14 +080067 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
68 == EFFECT_FLAG_VOLUME_MONITOR; }
Eric Laurent41709552019-12-16 19:34:05 -080069
Andy Hung6ac17eb2023-06-20 18:56:17 -070070 status_t setEnabled(bool enabled, bool fromHandle) override;
71 status_t setEnabled_l(bool enabled) final;
72 bool isEnabled() const final;
73 void setSuspended(bool suspended) final;
74 bool suspended() const final;
Eric Laurent41709552019-12-16 19:34:05 -080075
Andy Hung6ac17eb2023-06-20 18:56:17 -070076 status_t command(int32_t __unused,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070077 const std::vector<uint8_t>& __unused,
78 int32_t __unused,
Andy Hung6ac17eb2023-06-20 18:56:17 -070079 std::vector<uint8_t>* __unused) override {
80 return NO_ERROR;
81 }
Eric Laurent41709552019-12-16 19:34:05 -080082
Andy Hungfda44002021-06-03 17:23:16 -070083 // mCallback is atomic so this can be lock-free.
Andy Hung6ac17eb2023-06-20 18:56:17 -070084 void setCallback(const sp<EffectCallbackInterface>& callback) final {
85 mCallback = callback;
86 }
87 sp<EffectCallbackInterface> getCallback() const final {
88 return mCallback.load();
89 }
Eric Laurent41709552019-12-16 19:34:05 -080090
Andy Hung6ac17eb2023-06-20 18:56:17 -070091 status_t addHandle(IAfEffectHandle *handle) final;
92 ssize_t disconnectHandle(IAfEffectHandle *handle, bool unpinIfLast) final;
93 ssize_t removeHandle(IAfEffectHandle *handle) final;
94 ssize_t removeHandle_l(IAfEffectHandle *handle) final;
95 IAfEffectHandle* controlHandle_l() final;
96 bool purgeHandles() final;
Eric Laurent41709552019-12-16 19:34:05 -080097
Andy Hung6ac17eb2023-06-20 18:56:17 -070098 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) final;
Eric Laurent41709552019-12-16 19:34:05 -080099
Andy Hung6ac17eb2023-06-20 18:56:17 -0700100 bool isPinned() const final { return mPinned; }
101 void unPin() final { mPinned = false; }
Eric Laurent41709552019-12-16 19:34:05 -0800102
Andy Hung6ac17eb2023-06-20 18:56:17 -0700103 void lock() ACQUIRE(mLock) final { mLock.lock(); }
104 void unlock() RELEASE(mLock) final { mLock.unlock(); }
Eric Laurent81784c32012-11-19 14:55:58 -0800105
Andy Hung6ac17eb2023-06-20 18:56:17 -0700106 status_t updatePolicyState() final;
Eric Laurent41709552019-12-16 19:34:05 -0800107
Andy Hung6ac17eb2023-06-20 18:56:17 -0700108 sp<IAfEffectModule> asEffectModule() override { return nullptr; }
109 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() override { return nullptr; }
Eric Laurent6c796322019-04-09 14:13:17 -0700110
Andy Hung6ac17eb2023-06-20 18:56:17 -0700111 void dump(int fd, const Vector<String16>& args) const override;
Eric Laurent81784c32012-11-19 14:55:58 -0800112
Eric Laurentd66d7a12021-07-13 13:35:32 +0200113protected:
114 bool isInternal_l() const {
115 for (auto handle : mHandles) {
116 if (handle->client() != nullptr) {
117 return false;
118 }
119 }
120 return true;
121 }
122
Eric Laurent41709552019-12-16 19:34:05 -0800123 bool mPinned = false;
124
125 DISALLOW_COPY_AND_ASSIGN(EffectBase);
126
Andy Hung6ac17eb2023-06-20 18:56:17 -0700127 mutable Mutex mLock; // mutex for process, commands and handles list protection
Andy Hungfda44002021-06-03 17:23:16 -0700128 mediautils::atomic_sp<EffectCallbackInterface> mCallback; // parent effect chain
Eric Laurent41709552019-12-16 19:34:05 -0800129 const int mId; // this instance unique ID
130 const audio_session_t mSessionId; // audio session ID
131 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
132 effect_state mState = IDLE; // current activation state
Eric Laurentd9eb4962019-12-19 09:20:49 -0800133 // effect is suspended: temporarily disabled by framework
134 bool mSuspended = false;
Eric Laurent41709552019-12-16 19:34:05 -0800135
Andy Hung6ac17eb2023-06-20 18:56:17 -0700136 Vector<IAfEffectHandle *> mHandles; // list of client handles
Eric Laurent41709552019-12-16 19:34:05 -0800137 // First handle in mHandles has highest priority and controls the effect module
138
139 // Audio policy effect state management
140 // Mutex protecting transactions with audio policy manager as mLock cannot
141 // be held to avoid cross deadlocks with audio policy mutex
142 Mutex mPolicyLock;
143 // Effect is registered in APM or not
144 bool mPolicyRegistered = false;
145 // Effect enabled state communicated to APM. Enabled state corresponds to
146 // state requested by the EffectHandle with control
147 bool mPolicyEnabled = false;
148};
149
150// The EffectModule class is a wrapper object controlling the effect engine implementation
151// in the effect library. It prevents concurrent calls to process() and command() functions
152// from different client threads. It keeps a list of EffectHandle objects corresponding
153// to all client applications using this effect and notifies applications of effect state,
154// control or parameter changes. It manages the activation state machine to send appropriate
155// reset, enable, disable commands to effect engine and provide volume
156// ramping when effects are activated/deactivated.
157// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
158// the attached track(s) to accumulate their auxiliary channel.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700159class EffectModule : public IAfEffectModule, public EffectBase {
Eric Laurent41709552019-12-16 19:34:05 -0800160public:
161 EffectModule(const sp<EffectCallbackInterface>& callabck,
162 effect_descriptor_t *desc,
163 int id,
164 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800165 bool pinned,
166 audio_port_handle_t deviceId);
Andy Hung6ac17eb2023-06-20 18:56:17 -0700167 ~EffectModule() override;
Eric Laurent41709552019-12-16 19:34:05 -0800168
Andy Hung6ac17eb2023-06-20 18:56:17 -0700169 void process() final;
170 bool updateState() final;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700171 status_t command(int32_t cmdCode,
172 const std::vector<uint8_t>& cmdData,
173 int32_t maxReplySize,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700174 std::vector<uint8_t>* reply) final;
Eric Laurent41709552019-12-16 19:34:05 -0800175
Andy Hung6ac17eb2023-06-20 18:56:17 -0700176 void reset_l() final;
177 status_t configure() final;
178 status_t init() final;
179 uint32_t status() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800180 return mStatus;
181 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700182 bool isProcessEnabled() const final;
183 bool isOffloadedOrDirect() const final;
184 bool isVolumeControlEnabled() const final;
185 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final;
186 int16_t *inBuffer() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800187 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
188 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700189 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final;
190 int16_t *outBuffer() const final {
Eric Laurent41709552019-12-16 19:34:05 -0800191 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
192 }
Andy Hung799c8d02021-10-28 17:05:40 -0700193 // Updates the access mode if it is out of date. May issue a new effect configure.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700194 void updateAccessMode() final {
Andy Hung799c8d02021-10-28 17:05:40 -0700195 if (requiredEffectBufferAccessMode() != mConfig.outputCfg.accessMode) {
196 configure();
197 }
198 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700199 status_t setDevices(const AudioDeviceTypeAddrVector &devices) final;
200 status_t setInputDevice(const AudioDeviceTypeAddr &device) final;
201 status_t setVolume(uint32_t *left, uint32_t *right, bool controller) final;
202 status_t setMode(audio_mode_t mode) final;
203 status_t setAudioSource(audio_source_t source) final;
204 status_t start() final;
205 status_t stop() final;
Andy Hung799c8d02021-10-28 17:05:40 -0700206
Andy Hung6ac17eb2023-06-20 18:56:17 -0700207 status_t setOffloaded(bool offloaded, audio_io_handle_t io) final;
208 bool isOffloaded() const final;
209 void addEffectToHal_l() final;
210 void release_l() final;
Eric Laurent41709552019-12-16 19:34:05 -0800211
Andy Hung6ac17eb2023-06-20 18:56:17 -0700212 sp<IAfEffectModule> asEffectModule() final { return this; }
Eric Laurent41709552019-12-16 19:34:05 -0800213
Andy Hung6ac17eb2023-06-20 18:56:17 -0700214 bool isHapticGenerator() const final;
Eric Laurent41709552019-12-16 19:34:05 -0800215
Andy Hung6ac17eb2023-06-20 18:56:17 -0700216 status_t setHapticIntensity(int id, os::HapticScale intensity) final;
217 status_t setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo) final;
jiabineb3bda02020-06-30 14:07:03 -0700218
Andy Hung6ac17eb2023-06-20 18:56:17 -0700219 status_t getConfigs(audio_config_base_t* inputCfg,
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000220 audio_config_base_t* outputCfg,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700221 bool* isOutput) const final;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000222
Andy Hung6ac17eb2023-06-20 18:56:17 -0700223 void dump(int fd, const Vector<String16>& args) const final;
Eric Laurent41709552019-12-16 19:34:05 -0800224
225private:
Eric Laurent81784c32012-11-19 14:55:58 -0800226
227 // Maximum time allocated to effect engines to complete the turn off sequence
228 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
229
Mikhail Naganovbf493082017-04-17 17:37:12 -0700230 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800231
232 status_t start_l();
233 status_t stop_l();
Eric Laurent6b446ce2019-12-13 10:56:31 -0800234 status_t removeEffectFromHal_l();
jiabin8f278ee2019-11-11 12:16:27 -0800235 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Andy Hung799c8d02021-10-28 17:05:40 -0700236 effect_buffer_access_e requiredEffectBufferAccessMode() const {
237 return mConfig.inputCfg.buffer.raw == mConfig.outputCfg.buffer.raw
238 ? EFFECT_BUFFER_ACCESS_WRITE : EFFECT_BUFFER_ACCESS_ACCUMULATE;
239 }
240
jiabin4e246532022-08-23 16:37:30 -0700241 status_t setVolumeInternal(uint32_t *left, uint32_t *right, bool controller);
242
Eric Laurent81784c32012-11-19 14:55:58 -0800243
Eric Laurent81784c32012-11-19 14:55:58 -0800244 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700245 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800246 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
247 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800248 status_t mStatus; // initialization status
Eric Laurent81784c32012-11-19 14:55:58 -0800249 // First handle in mHandles has highest priority and controls the effect module
250 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
251 // sending disable command.
252 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent5baf2af2013-09-12 17:37:00 -0700253 bool mOffloaded; // effect is currently offloaded to the audio DSP
Eric Laurent9f57aa82023-03-17 19:28:37 +0100254 // effect has been added to this HAL input stream
255 audio_io_handle_t mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000256 bool mIsOutput; // direction of the AF thread
rago94a1ee82017-07-21 15:11:02 -0700257
rago94a1ee82017-07-21 15:11:02 -0700258 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800259 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
260 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800261 uint32_t mInChannelCountRequested;
262 uint32_t mOutChannelCountRequested;
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900263
264 class AutoLockReentrant {
265 public:
266 AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
267 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
268 {
269 if (mMutex != nullptr) mMutex->lock();
270 }
271 ~AutoLockReentrant() {
272 if (mMutex != nullptr) mMutex->unlock();
273 }
274 private:
275 Mutex * const mMutex;
276 };
277
278 static constexpr pid_t INVALID_PID = (pid_t)-1;
279 // this tid is allowed to call setVolume() without acquiring the mutex.
280 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent81784c32012-11-19 14:55:58 -0800281};
282
283// The EffectHandle class implements the IEffect interface. It provides resources
284// to receive parameter updates, keeps track of effect control
285// ownership and state and has a pointer to the EffectModule object it is controlling.
286// There is one EffectHandle object for each application controlling (or using)
287// an effect module.
288// The EffectHandle is obtained by calling AudioFlinger::createEffect().
Andy Hung6ac17eb2023-06-20 18:56:17 -0700289class EffectHandle: public IAfEffectHandle, public android::media::BnEffect {
Eric Laurent81784c32012-11-19 14:55:58 -0800290public:
291
Andy Hung6ac17eb2023-06-20 18:56:17 -0700292 EffectHandle(const sp<IAfEffectBase>& effect,
Eric Laurent81784c32012-11-19 14:55:58 -0800293 const sp<AudioFlinger::Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700294 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +0200295 int32_t priority, bool notifyFramesProcessed);
Andy Hung6ac17eb2023-06-20 18:56:17 -0700296 ~EffectHandle() override;
Andy Hungc747c532022-03-07 21:41:14 -0800297 status_t onTransact(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700298 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) final;
299 status_t initCheck() const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800300
301 // IEffect
Andy Hung6ac17eb2023-06-20 18:56:17 -0700302 android::binder::Status enable(int32_t* _aidl_return) final;
303 android::binder::Status disable(int32_t* _aidl_return) final;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700304 android::binder::Status command(int32_t cmdCode,
305 const std::vector<uint8_t>& cmdData,
306 int32_t maxResponseSize,
307 std::vector<uint8_t>* response,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700308 int32_t* _aidl_return) final;
309 android::binder::Status disconnect() final;
310 android::binder::Status getCblk(media::SharedFileRegion* _aidl_return) final;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000311 android::binder::Status getConfig(media::EffectConfig* _config,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700312 int32_t* _aidl_return) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800313
Andy Hung6ac17eb2023-06-20 18:56:17 -0700314 // TODO(b/288339104) type
315 sp<RefBase /* AudioFlinger::Client */> client() const final { return mClient; }
316
317 sp<android::media::IEffect> asIEffect() final {
318 return sp<android::media::IEffect>::fromExisting(this);
319 }
Eric Laurentd66d7a12021-07-13 13:35:32 +0200320
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700321private:
322 void disconnect(bool unpinIfLast);
Eric Laurent81784c32012-11-19 14:55:58 -0800323
324 // Give or take control of effect module
325 // - hasControl: true if control is given, false if removed
326 // - signal: true client app should be signaled of change, false otherwise
327 // - enabled: state of the effect when control is passed
Andy Hung6ac17eb2023-06-20 18:56:17 -0700328 void setControl(bool hasControl, bool signal, bool enabled) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800329 void commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700330 const std::vector<uint8_t>& cmdData,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700331 const std::vector<uint8_t>& replyData) final;
332 bool enabled() const final { return mEnabled; }
333 void setEnabled(bool enabled) final;
334 void framesProcessed(int32_t frames) const final;
Eric Laurent81784c32012-11-19 14:55:58 -0800335
Andy Hung6ac17eb2023-06-20 18:56:17 -0700336public:
Eric Laurent81784c32012-11-19 14:55:58 -0800337 // Getters
Andy Hung6ac17eb2023-06-20 18:56:17 -0700338 wp<IAfEffectBase> effect() const final { return mEffect; }
339 int id() const final {
340 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800341 if (effect == 0) {
342 return 0;
343 }
344 return effect->id();
345 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700346private:
347 int priority() const final { return mPriority; }
348 bool hasControl() const final { return mHasControl; }
349 bool disconnected() const final { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800350
Andy Hung6ac17eb2023-06-20 18:56:17 -0700351 void dumpToBuffer(char* buffer, size_t size) const final;
352
Eric Laurent81784c32012-11-19 14:55:58 -0800353
Mikhail Naganovbf493082017-04-17 17:37:12 -0700354private:
Mikhail Naganovbf493082017-04-17 17:37:12 -0700355 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800356
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700357 Mutex mLock; // protects IEffect method calls
Andy Hung6ac17eb2023-06-20 18:56:17 -0700358 const wp<IAfEffectBase> mEffect; // pointer to controlled EffectModule
Andy Hung318e0242020-12-21 10:33:49 -0800359 const sp<media::IEffectClient> mEffectClient; // callback interface for client notifications
Andy Hung6ac17eb2023-06-20 18:56:17 -0700360 /*const*/ sp<AudioFlinger::Client> mClient; // client for shared memory allocation, see
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700361 // disconnect()
362 sp<IMemory> mCblkMemory; // shared memory for control block
363 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
364 // shared memory
365 uint8_t* mBuffer; // pointer to parameter area in shared memory
366 int mPriority; // client application priority to control the effect
367 bool mHasControl; // true if this handle is controlling the effect
368 bool mEnabled; // cached enable state: needed when the effect is
369 // restored after being suspended
370 bool mDisconnected; // Set to true by disconnect()
Eric Laurentde8caf42021-08-11 17:19:25 +0200371 const bool mNotifyFramesProcessed; // true if the client callback event
372 // EVENT_FRAMES_PROCESSED must be generated
Eric Laurent81784c32012-11-19 14:55:58 -0800373};
374
375// the EffectChain class represents a group of effects associated to one audio session.
376// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800377// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
378// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800379// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
380// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800381// order corresponding in the effect process order. When attached to a track (session ID !=
382// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800383// it also provide it's own input buffer used by the track as accumulation buffer.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700384class EffectChain : public IAfEffectChain {
Eric Laurent81784c32012-11-19 14:55:58 -0800385public:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700386 EffectChain(const wp<AudioFlinger::ThreadBase>& wThread, audio_session_t sessionId);
387 ~EffectChain() override;
Eric Laurent81784c32012-11-19 14:55:58 -0800388
Andy Hung6ac17eb2023-06-20 18:56:17 -0700389 void process_l() final;
Eric Laurent81784c32012-11-19 14:55:58 -0800390
Andy Hung6ac17eb2023-06-20 18:56:17 -0700391 void lock() ACQUIRE(mLock) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800392 mLock.lock();
393 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700394 void unlock() RELEASE(mLock) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800395 mLock.unlock();
396 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700397 status_t createEffect_l(sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800398 effect_descriptor_t *desc,
399 int id,
400 audio_session_t sessionId,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700401 bool pinned) final;
402 status_t addEffect_l(const sp<IAfEffectModule>& handle) final;
403 status_t addEffect_ll(const sp<IAfEffectModule>& handle) final;
404 size_t removeEffect_l(const sp<IAfEffectModule>& handle, bool release = false) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800405
Andy Hung6ac17eb2023-06-20 18:56:17 -0700406 audio_session_t sessionId() const final { return mSessionId; }
407 void setSessionId(audio_session_t sessionId) final { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800408
Andy Hung6ac17eb2023-06-20 18:56:17 -0700409 sp<IAfEffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor) const final;
410 sp<IAfEffectModule> getEffectFromId_l(int id) const final;
411 sp<IAfEffectModule> getEffectFromType_l(const effect_uuid_t *type) const final;
412 std::vector<int> getEffectIds() const final;
Glenn Kastenc56f3422014-03-21 17:53:17 -0700413 // FIXME use float to improve the dynamic range
Eric Laurent81784c32012-11-19 14:55:58 -0800414
Andy Hung6ac17eb2023-06-20 18:56:17 -0700415 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false) final;
416 void resetVolume_l() final;
417 void setDevices_l(const AudioDeviceTypeAddrVector &devices) final;
418 void setInputDevice_l(const AudioDeviceTypeAddr &device) final;
419 void setMode_l(audio_mode_t mode) final;
420 void setAudioSource_l(audio_source_t source) final;
421
422 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800423 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800424 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700425 float *inBuffer() const final {
Andy Hung181d96f2023-05-23 14:01:03 -0700426 return mInBuffer != 0 ? reinterpret_cast<float*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800427 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700428 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final {
Eric Laurent81784c32012-11-19 14:55:58 -0800429 mOutBuffer = buffer;
430 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700431 float *outBuffer() const final {
Andy Hung181d96f2023-05-23 14:01:03 -0700432 return mOutBuffer != 0 ? reinterpret_cast<float*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800433 }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700434 void incTrackCnt() final { android_atomic_inc(&mTrackCnt); }
435 void decTrackCnt() final { android_atomic_dec(&mTrackCnt); }
436 int32_t trackCnt() const final { return android_atomic_acquire_load(&mTrackCnt); }
Eric Laurent81784c32012-11-19 14:55:58 -0800437
Andy Hung6ac17eb2023-06-20 18:56:17 -0700438 void incActiveTrackCnt() final { android_atomic_inc(&mActiveTrackCnt);
Eric Laurent81784c32012-11-19 14:55:58 -0800439 mTailBufferCount = mMaxTailBuffers; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700440 void decActiveTrackCnt() final { android_atomic_dec(&mActiveTrackCnt); }
441 int32_t activeTrackCnt() const final {
442 return android_atomic_acquire_load(&mActiveTrackCnt);
443 }
Eric Laurent81784c32012-11-19 14:55:58 -0800444
Andy Hung6ac17eb2023-06-20 18:56:17 -0700445 product_strategy_t strategy() const final { return mStrategy; }
446 void setStrategy(product_strategy_t strategy) final
Eric Laurent81784c32012-11-19 14:55:58 -0800447 { mStrategy = strategy; }
448
Eric Laurentd8365c52017-07-16 15:27:05 -0700449 // suspend or restore effects of the specified type. The number of suspend requests is counted
450 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800451 void setEffectSuspended_l(const effect_uuid_t *type,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700452 bool suspend) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800453 // suspend all eligible effects
Andy Hung6ac17eb2023-06-20 18:56:17 -0700454 void setEffectSuspendedAll_l(bool suspend) final;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800455 // check if effects should be suspended or restored when a given effect is enable or disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700456 void checkSuspendOnEffectEnabled(
457 const sp<IAfEffectModule>& effect, bool enabled) final;
Eric Laurent81784c32012-11-19 14:55:58 -0800458
Andy Hung6ac17eb2023-06-20 18:56:17 -0700459 void clearInputBuffer() final;
Eric Laurent81784c32012-11-19 14:55:58 -0800460
Eric Laurent5baf2af2013-09-12 17:37:00 -0700461 // At least one non offloadable effect in the chain is enabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700462 bool isNonOffloadableEnabled() const final;
463 bool isNonOffloadableEnabled_l() const final;
Eric Laurent813e2a72013-08-31 12:59:48 -0700464
Andy Hung6ac17eb2023-06-20 18:56:17 -0700465 void syncHalEffectsState() final;
Eric Laurent1b928682014-10-02 19:41:47 -0700466
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700467 // flags is an ORed set of audio_output_flags_t which is updated on return.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700468 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700469
470 // flags is an ORed set of audio_input_flags_t which is updated on return.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700471 void checkInputFlagCompatibility(audio_input_flags_t *flags) const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700472
473 // Is this EffectChain compatible with the RAW audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700474 bool isRawCompatible() const final;
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700475
476 // Is this EffectChain compatible with the FAST audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700477 bool isFastCompatible() const final;
Eric Laurent4c415062016-06-17 16:14:16 -0700478
jiabinc658e452022-10-21 20:52:21 +0000479 // Is this EffectChain compatible with the bit-perfect audio flag.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700480 bool isBitPerfectCompatible() const final;
jiabinc658e452022-10-21 20:52:21 +0000481
Eric Laurent4c415062016-06-17 16:14:16 -0700482 // isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -0700483 // TODO(b/288339104) type
484 bool isCompatibleWithThread_l(const sp<Thread>& thread) const final {
485 return isCompatibleWithThread_l(sp<AudioFlinger::ThreadBase>::cast(thread));
486 }
Eric Laurent4c415062016-06-17 16:14:16 -0700487
Andy Hung6ac17eb2023-06-20 18:56:17 -0700488 bool isCompatibleWithThread_l(const sp<AudioFlinger::ThreadBase>& thread) const;
jiabineb3bda02020-06-30 14:07:03 -0700489
Andy Hung6ac17eb2023-06-20 18:56:17 -0700490 bool containsHapticGeneratingEffect_l() final;
jiabine70bc7f2020-06-30 22:07:55 -0700491
Andy Hung6ac17eb2023-06-20 18:56:17 -0700492 void setHapticIntensity_l(int id, os::HapticScale intensity) final;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800493
Andy Hung6ac17eb2023-06-20 18:56:17 -0700494 sp<EffectCallbackInterface> effectCallback() const final { return mEffectCallback; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200495
Andy Hung6ac17eb2023-06-20 18:56:17 -0700496 // TODO(b/288339104) type
497 wp<Thread> thread() const final { return mEffectCallback->thread(); }
498
499 bool isFirstEffect(int id) const final {
500 return !mEffects.isEmpty() && id == mEffects[0]->id();
501 }
502
503 void dump(int fd, const Vector<String16>& args) const final;
504
505 size_t numberOfEffects() const final { return mEffects.size(); }
506
507 sp<IAfEffectModule> getEffectModule(size_t index) const final {
508 return mEffects[index];
509 }
510
511 // TODO(b/288339104) type
512 void setThread(const sp<Thread>& thread) final {
513 setThread(sp<AudioFlinger::ThreadBase>::cast(thread));
514 }
515
516 void setThread(const sp<AudioFlinger::ThreadBase>& thread);
Eric Laurent81784c32012-11-19 14:55:58 -0800517
Mikhail Naganovbf493082017-04-17 17:37:12 -0700518private:
Eric Laurent6b446ce2019-12-13 10:56:31 -0800519
Andy Hung328d6772021-01-12 12:32:21 -0800520 // For transaction consistency, please consider holding the EffectChain lock before
521 // calling the EffectChain::EffectCallback methods, excepting
522 // createEffectHal and allocateHalBuffer.
523 //
524 // This prevents migration of the EffectChain to another PlaybackThread
525 // for the purposes of the EffectCallback.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800526 class EffectCallback : public EffectCallbackInterface {
527 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800528 // Note: ctors taking a weak pointer to their owner must not promote it
529 // during construction (but may keep a reference for later promotion).
530 EffectCallback(const wp<EffectChain>& owner,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700531 const wp<AudioFlinger::ThreadBase>& thread)
Andy Hung6626a012021-01-12 13:38:00 -0800532 : mChain(owner)
533 , mThread(thread)
Andy Hung6ac17eb2023-06-20 18:56:17 -0700534 , mAudioFlinger(*AudioFlinger::gAudioFlinger) {
535 sp<AudioFlinger::ThreadBase> base = thread.promote();
Greg Kaiseree71ebd2021-11-09 07:37:38 -0800536 if (base != nullptr) {
537 mThreadType = base->type();
538 } else {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700539 mThreadType = AudioFlinger::ThreadBase::MIXER; // assure a consistent value.
Greg Kaiseree71ebd2021-11-09 07:37:38 -0800540 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800541 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800542
543 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
544 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
545 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700546 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800547
548 audio_io_handle_t io() const override;
549 bool isOutput() const override;
550 bool isOffload() const override;
551 bool isOffloadOrDirect() const override;
552 bool isOffloadOrMmap() const override;
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200553 bool isSpatializer() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800554
555 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200556 audio_channel_mask_t inChannelMask(int id) const override;
557 uint32_t inChannelCount(int id) const override;
558 audio_channel_mask_t outChannelMask() const override;
559 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700560 audio_channel_mask_t hapticChannelMask() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800561 size_t frameCount() const override;
562 uint32_t latency() const override;
563
Andy Hung920f6572022-10-06 12:09:49 -0700564 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override;
565 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700566 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800567 void setVolumeForOutput(float left, float right) const override;
568
569 // check if effects should be suspended/restored when a given effect is enable/disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700570 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800571 bool enabled, bool threadLocked) override;
572 void resetVolume() override;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800573 product_strategy_t strategy() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800574 int32_t activeTrackCnt() const override;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700575 void onEffectEnable(const sp<IAfEffectBase>& effect) override;
576 void onEffectDisable(const sp<IAfEffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800577
Andy Hung6ac17eb2023-06-20 18:56:17 -0700578 wp<IAfEffectChain> chain() const final { return mChain; }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800579
Andy Hung6ac17eb2023-06-20 18:56:17 -0700580 bool isAudioPolicyReady() const final {
Eric Laurentd66d7a12021-07-13 13:35:32 +0200581 return mAudioFlinger.isAudioPolicyReady();
582 }
583
Andy Hung6ac17eb2023-06-20 18:56:17 -0700584 wp<AudioFlinger::ThreadBase> thread() const { return mThread.load(); }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800585
Andy Hung6ac17eb2023-06-20 18:56:17 -0700586 void setThread(const sp<AudioFlinger::ThreadBase>& thread) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800587 mThread = thread;
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200588 mThreadType = thread->type();
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800589 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800590
591 private:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700592 const wp<IAfEffectChain> mChain;
593 mediautils::atomic_wp<AudioFlinger::ThreadBase> mThread;
Andy Hung6626a012021-01-12 13:38:00 -0800594 AudioFlinger &mAudioFlinger; // implementation detail: outer instance always exists.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700595 AudioFlinger::ThreadBase::type_t mThreadType;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800596 };
597
Mikhail Naganovbf493082017-04-17 17:37:12 -0700598 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800599
600 class SuspendedEffectDesc : public RefBase {
601 public:
602 SuspendedEffectDesc() : mRefCount(0) {}
603
Eric Laurentd8365c52017-07-16 15:27:05 -0700604 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800605 effect_uuid_t mType;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700606 wp<IAfEffectModule> mEffect;
Eric Laurent81784c32012-11-19 14:55:58 -0800607 };
608
609 // get a list of effect modules to suspend when an effect of the type
610 // passed is enabled.
Andy Hung6ac17eb2023-06-20 18:56:17 -0700611 void getSuspendEligibleEffects(Vector<sp<IAfEffectModule>> &effects);
Eric Laurent81784c32012-11-19 14:55:58 -0800612
613 // get an effect module if it is currently enable
Andy Hung6ac17eb2023-06-20 18:56:17 -0700614 sp<IAfEffectModule> getEffectIfEnabled(const effect_uuid_t *type);
Eric Laurent81784c32012-11-19 14:55:58 -0800615 // true if the effect whose descriptor is passed can be suspended
616 // OEMs can modify the rules implemented in this method to exclude specific effect
617 // types or implementations from the suspend/restore mechanism.
618 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
619
Eric Laurentd8365c52017-07-16 15:27:05 -0700620 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
621
Eric Laurent6b446ce2019-12-13 10:56:31 -0800622 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800623
Zhou Songd505c642020-02-20 16:35:37 +0800624 // true if any effect module within the chain has volume control
625 bool hasVolumeControlEnabled_l() const;
626
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900627 void setVolumeForOutput_l(uint32_t left, uint32_t right);
628
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200629 ssize_t getInsertIndex(const effect_descriptor_t& desc);
630
Eric Laurent4c415062016-06-17 16:14:16 -0700631 mutable Mutex mLock; // mutex protecting effect list
Andy Hung6ac17eb2023-06-20 18:56:17 -0700632 Vector<sp<IAfEffectModule>> mEffects; // list of effect modules
Eric Laurent4c415062016-06-17 16:14:16 -0700633 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800634 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
635 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800636
637 // 'volatile' here means these are accessed with atomic operations instead of mutex
638 volatile int32_t mActiveTrackCnt; // number of active tracks connected
639 volatile int32_t mTrackCnt; // number of tracks connected
640
Eric Laurent4c415062016-06-17 16:14:16 -0700641 int32_t mTailBufferCount; // current effect tail buffer count
642 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700643 int mVolumeCtrlIdx; // index of insert effect having control over volume
644 uint32_t mLeftVolume; // previous volume on left channel
645 uint32_t mRightVolume; // previous volume on right channel
646 uint32_t mNewLeftVolume; // new volume on left channel
647 uint32_t mNewRightVolume; // new volume on right channel
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800648 product_strategy_t mStrategy; // strategy for this effect chain
Eric Laurent4c415062016-06-17 16:14:16 -0700649 // mSuspendedEffects lists all effects currently suspended in the chain.
650 // Use effect type UUID timelow field as key. There is no real risk of identical
651 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700652 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700653 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800654
655 const sp<EffectCallback> mEffectCallback;
Eric Laurent81784c32012-11-19 14:55:58 -0800656};
Eric Laurentb82e6b72019-11-22 17:25:04 -0800657
Andy Hung6ac17eb2023-06-20 18:56:17 -0700658class DeviceEffectProxy : public IAfDeviceEffectProxy, public EffectBase {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800659public:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700660 DeviceEffectProxy(const AudioDeviceTypeAddr& device,
661 const sp<AudioFlinger::DeviceEffectManagerCallback>& callback,
Eric Laurentde8caf42021-08-11 17:19:25 +0200662 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800663 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
664 mDevice(device), mManagerCallback(callback),
Eric Laurentde8caf42021-08-11 17:19:25 +0200665 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this), callback)),
666 mNotifyFramesProcessed(notifyFramesProcessed) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800667
Andy Hung6ac17eb2023-06-20 18:56:17 -0700668 status_t setEnabled(bool enabled, bool fromHandle) final;
669 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() final { return this; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800670
Andy Hung6ac17eb2023-06-20 18:56:17 -0700671 // TODO(b/288339104) type
672 status_t init(const /* std::map<audio_patch_handle_t,
673 PatchPanel::Patch>& */ void * patches) final {
674 return init(*reinterpret_cast<const std::map<
675 audio_patch_handle_t, AudioFlinger::PatchPanel::Patch> *>(patches));
676 }
677 // TODO(b/288339104) type
678 status_t onCreatePatch(audio_patch_handle_t patchHandle,
679 /* const PatchPanel::Patch& */ const void * patch) final {
680 return onCreatePatch(patchHandle,
681 *reinterpret_cast<const AudioFlinger::PatchPanel::Patch *>(patch));
682 }
François Gaffie58e73af2023-02-15 11:47:24 +0100683 // TODO(b/288339104) type
684 status_t onUpdatePatch(audio_patch_handle_t oldPatchHandle, audio_patch_handle_t newPatchHandle,
685 /* const PatchPanel::Patch& */ const void * patch) final {
686 return onUpdatePatch(oldPatchHandle, newPatchHandle,
687 *reinterpret_cast<const AudioFlinger::PatchPanel::Patch *>(patch));
688 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800689
Andy Hung6ac17eb2023-06-20 18:56:17 -0700690 status_t init(const std::map<audio_patch_handle_t, AudioFlinger::PatchPanel::Patch>& patches);
691 status_t onCreatePatch(
692 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800693
François Gaffie58e73af2023-02-15 11:47:24 +0100694 status_t onUpdatePatch(audio_patch_handle_t oldPatchHandle, audio_patch_handle_t newPatchHandle,
695 const AudioFlinger::PatchPanel::Patch& patch);
696
Andy Hung6ac17eb2023-06-20 18:56:17 -0700697 void onReleasePatch(audio_patch_handle_t patchHandle) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800698
Andy Hung6ac17eb2023-06-20 18:56:17 -0700699 size_t removeEffect(const sp<IAfEffectModule>& effect) final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800700
Andy Hung6ac17eb2023-06-20 18:56:17 -0700701 status_t addEffectToHal(const sp<EffectHalInterface>& effect) final;
702 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) final;
703
704 const AudioDeviceTypeAddr& device() const final { return mDevice; };
705 bool isOutput() const final;
706 uint32_t sampleRate() const final;
707 audio_channel_mask_t channelMask() const final;
708 uint32_t channelCount() const final;
709
710 void dump2(int fd, int spaces) const final;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800711
712private:
713
714 class ProxyCallback : public EffectCallbackInterface {
715 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800716 // Note: ctors taking a weak pointer to their owner must not promote it
717 // during construction (but may keep a reference for later promotion).
718 ProxyCallback(const wp<DeviceEffectProxy>& owner,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700719 const sp<AudioFlinger::DeviceEffectManagerCallback>& callback)
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800720 : mProxy(owner), mManagerCallback(callback) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800721
722 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
723 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
724 status_t allocateHalBuffer(size_t size __unused,
725 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700726 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800727 return false;
728 }
729
730 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
731 bool isOutput() const override;
732 bool isOffload() const override { return false; }
733 bool isOffloadOrDirect() const override { return false; }
734 bool isOffloadOrMmap() const override { return false; }
Eric Laurent0dccd2e2021-10-26 17:40:18 +0200735 bool isSpatializer() const override { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800736
737 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200738 audio_channel_mask_t inChannelMask(int id) const override;
739 uint32_t inChannelCount(int id) const override;
740 audio_channel_mask_t outChannelMask() const override;
741 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700742 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800743 size_t frameCount() const override { return 0; }
744 uint32_t latency() const override { return 0; }
745
Andy Hung920f6572022-10-06 12:09:49 -0700746 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override;
747 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800748
Andy Hung6ac17eb2023-06-20 18:56:17 -0700749 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800750 void setVolumeForOutput(float left __unused, float right __unused) const override {}
751
Andy Hung6ac17eb2023-06-20 18:56:17 -0700752 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800753 bool enabled __unused, bool threadLocked __unused) override {}
754 void resetVolume() override {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800755 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800756 int32_t activeTrackCnt() const override { return 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700757 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) override;
758 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800759
Andy Hung6ac17eb2023-06-20 18:56:17 -0700760 wp<IAfEffectChain> chain() const override { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800761
Eric Laurentd66d7a12021-07-13 13:35:32 +0200762 bool isAudioPolicyReady() const override {
763 return mManagerCallback->isAudioPolicyReady();
764 }
765
Eric Laurentb82e6b72019-11-22 17:25:04 -0800766 int newEffectId();
767
768 private:
769 const wp<DeviceEffectProxy> mProxy;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700770 const sp<AudioFlinger::DeviceEffectManagerCallback> mManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800771 };
772
Andy Hung6ac17eb2023-06-20 18:56:17 -0700773 status_t checkPort(const AudioFlinger::PatchPanel::Patch& patch,
774 const struct audio_port_config *port, sp<IAfEffectHandle> *handle);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800775
776 const AudioDeviceTypeAddr mDevice;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700777 const sp<AudioFlinger::DeviceEffectManagerCallback> mManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800778 const sp<ProxyCallback> mMyCallback;
779
Andy Hung6ac17eb2023-06-20 18:56:17 -0700780 mutable Mutex mProxyLock;
781 std::map<audio_patch_handle_t, sp<IAfEffectHandle>> mEffectHandles; // protected by mProxyLock
782 sp<IAfEffectModule> mHalEffect; // protected by mProxyLock
Eric Laurentb82e6b72019-11-22 17:25:04 -0800783 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
Eric Laurentde8caf42021-08-11 17:19:25 +0200784 const bool mNotifyFramesProcessed;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800785};
Andy Hung6ac17eb2023-06-20 18:56:17 -0700786
787} // namespace android