blob: b6f975835eccbcd7fb5e2df2c3530fc354eb812f [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
18#ifndef INCLUDING_FROM_AUDIOFLINGER_H
19 #error This header file should only be included from AudioFlinger.h
20#endif
21
22//--- Audio Effect Management
23
Eric Laurent6b446ce2019-12-13 10:56:31 -080024// Interface implemented by the EffectModule parent or owner (e.g an EffectChain) to abstract
25// interactions between the EffectModule and the reset of the audio framework.
26class EffectCallbackInterface : public RefBase {
27public:
28 ~EffectCallbackInterface() override = default;
29
30 // Trivial methods usually implemented with help from ThreadBase
31 virtual audio_io_handle_t io() const = 0;
32 virtual bool isOutput() const = 0;
33 virtual bool isOffload() const = 0;
34 virtual bool isOffloadOrDirect() const = 0;
35 virtual bool isOffloadOrMmap() const = 0;
Eric Laurentb82e6b72019-11-22 17:25:04 -080036 virtual uint32_t sampleRate() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080037 virtual audio_channel_mask_t channelMask() const = 0;
38 virtual uint32_t channelCount() const = 0;
jiabineb3bda02020-06-30 14:07:03 -070039 virtual audio_channel_mask_t hapticChannelMask() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080040 virtual size_t frameCount() const = 0;
41
42 // Non trivial methods usually implemented with help from ThreadBase:
43 // pay attention to mutex locking order
44 virtual uint32_t latency() const { return 0; }
45 virtual status_t addEffectToHal(sp<EffectHalInterface> effect) = 0;
46 virtual status_t removeEffectFromHal(sp<EffectHalInterface> effect) = 0;
47 virtual void setVolumeForOutput(float left, float right) const = 0;
48 virtual bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080049 virtual void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -080050 bool enabled,
51 bool threadLocked) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080052 virtual void onEffectEnable(const sp<EffectBase>& effect) = 0;
53 virtual void onEffectDisable(const sp<EffectBase>& effect) = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080054
55 // Methods usually implemented with help from AudioFlinger: pay attention to mutex locking order
56 virtual status_t createEffectHal(const effect_uuid_t *pEffectUuid,
57 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) = 0;
58 virtual status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080059 virtual bool updateOrphanEffectChains(const sp<EffectBase>& effect) = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080060
61 // Methods usually implemented with help from EffectChain: pay attention to mutex locking order
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080062 virtual product_strategy_t strategy() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080063 virtual int32_t activeTrackCnt() const = 0;
64 virtual void resetVolume() = 0;
65
66 virtual wp<EffectChain> chain() const = 0;
Eric Laurentd66d7a12021-07-13 13:35:32 +020067
68 virtual bool isAudioPolicyReady() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080069};
70
Eric Laurent41709552019-12-16 19:34:05 -080071// EffectBase(EffectModule) and EffectChain classes both have their own mutex to protect
Eric Laurent81784c32012-11-19 14:55:58 -080072// state changes or resource modifications. Always respect the following order
73// if multiple mutexes must be acquired to avoid cross deadlock:
Eric Laurent41709552019-12-16 19:34:05 -080074// AudioFlinger -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
75// AudioHandle -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
Eric Laurent6b446ce2019-12-13 10:56:31 -080076
77// NOTE: When implementing the EffectCallbackInterface, in an EffectChain or other, it is important
78// to pay attention to this locking order as some callback methods can be called from a state where
79// EffectModule and/or EffectChain mutexes are held.
80
Eric Laurenteb3c3372013-09-25 12:25:29 -070081// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
Eric Laurent3bc859b2016-12-05 11:07:22 -080082// startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or
83// Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService
84// methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order.
Eric Laurent81784c32012-11-19 14:55:58 -080085
Eric Laurent41709552019-12-16 19:34:05 -080086
87// The EffectBase class contains common properties, state and behavior for and EffectModule or
88// other derived classes managing an audio effect instance within the effect framework.
89// It also contains the class mutex (see comment on locking order above).
90class EffectBase : public RefBase {
Eric Laurent81784c32012-11-19 14:55:58 -080091public:
Eric Laurent41709552019-12-16 19:34:05 -080092 EffectBase(const sp<EffectCallbackInterface>& callback,
93 effect_descriptor_t *desc,
94 int id,
95 audio_session_t sessionId,
96 bool pinned);
97
98 ~EffectBase() override = default;
Eric Laurent81784c32012-11-19 14:55:58 -080099
100 enum effect_state {
101 IDLE,
102 RESTART,
103 STARTING,
104 ACTIVE,
105 STOPPING,
106 STOPPED,
107 DESTROYED
108 };
109
Eric Laurent41709552019-12-16 19:34:05 -0800110 int id() const { return mId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800111 effect_state state() const {
112 return mState;
113 }
Glenn Kastend848eb42016-03-08 13:42:11 -0800114 audio_session_t sessionId() const {
Eric Laurent81784c32012-11-19 14:55:58 -0800115 return mSessionId;
116 }
Eric Laurent81784c32012-11-19 14:55:58 -0800117 const effect_descriptor_t& desc() const { return mDescriptor; }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700118 bool isOffloadable() const
119 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Eric Laurent4c415062016-06-17 16:14:16 -0700120 bool isImplementationSoftware() const
121 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700122 bool isProcessImplemented() const
123 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900124 bool isVolumeControl() const
125 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
126 == EFFECT_FLAG_VOLUME_CTRL; }
Jasmine Cha934ecfb2019-01-23 18:19:14 +0800127 bool isVolumeMonitor() const
128 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
129 == EFFECT_FLAG_VOLUME_MONITOR; }
Eric Laurent41709552019-12-16 19:34:05 -0800130
131 virtual status_t setEnabled(bool enabled, bool fromHandle);
132 status_t setEnabled_l(bool enabled);
133 bool isEnabled() const;
134
135 void setSuspended(bool suspended);
136 bool suspended() const;
137
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700138 virtual status_t command(int32_t __unused,
139 const std::vector<uint8_t>& __unused,
140 int32_t __unused,
141 std::vector<uint8_t>* __unused) { return NO_ERROR; };
Eric Laurent41709552019-12-16 19:34:05 -0800142
Andy Hungfda44002021-06-03 17:23:16 -0700143 // mCallback is atomic so this can be lock-free.
Eric Laurent41709552019-12-16 19:34:05 -0800144 void setCallback(const sp<EffectCallbackInterface>& callback) { mCallback = callback; }
Andy Hungfda44002021-06-03 17:23:16 -0700145 sp<EffectCallbackInterface> getCallback() const { return mCallback.load(); }
Eric Laurent41709552019-12-16 19:34:05 -0800146
147 status_t addHandle(EffectHandle *handle);
148 ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
149 ssize_t removeHandle(EffectHandle *handle);
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530150 ssize_t removeHandle_l(EffectHandle *handle);
Eric Laurent41709552019-12-16 19:34:05 -0800151 EffectHandle* controlHandle_l();
152 bool purgeHandles();
153
154 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked);
155
156 bool isPinned() const { return mPinned; }
157 void unPin() { mPinned = false; }
158
159 void lock() { mLock.lock(); }
160 void unlock() { mLock.unlock(); }
Eric Laurent81784c32012-11-19 14:55:58 -0800161
Eric Laurent6c796322019-04-09 14:13:17 -0700162 status_t updatePolicyState();
Eric Laurent41709552019-12-16 19:34:05 -0800163
164 virtual sp<EffectModule> asEffectModule() { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800165 virtual sp<DeviceEffectProxy> asDeviceEffectProxy() { return nullptr; }
Eric Laurent6c796322019-04-09 14:13:17 -0700166
Eric Laurent81784c32012-11-19 14:55:58 -0800167 void dump(int fd, const Vector<String16>& args);
168
Eric Laurentd66d7a12021-07-13 13:35:32 +0200169protected:
170 bool isInternal_l() const {
171 for (auto handle : mHandles) {
172 if (handle->client() != nullptr) {
173 return false;
174 }
175 }
176 return true;
177 }
178
Mikhail Naganovbf493082017-04-17 17:37:12 -0700179private:
Eric Laurent81784c32012-11-19 14:55:58 -0800180 friend class AudioFlinger; // for mHandles
Eric Laurent41709552019-12-16 19:34:05 -0800181 bool mPinned = false;
182
183 DISALLOW_COPY_AND_ASSIGN(EffectBase);
184
185mutable Mutex mLock; // mutex for process, commands and handles list protection
Andy Hungfda44002021-06-03 17:23:16 -0700186 mediautils::atomic_sp<EffectCallbackInterface> mCallback; // parent effect chain
Eric Laurent41709552019-12-16 19:34:05 -0800187 const int mId; // this instance unique ID
188 const audio_session_t mSessionId; // audio session ID
189 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
190 effect_state mState = IDLE; // current activation state
Eric Laurentd9eb4962019-12-19 09:20:49 -0800191 // effect is suspended: temporarily disabled by framework
192 bool mSuspended = false;
Eric Laurent41709552019-12-16 19:34:05 -0800193
194 Vector<EffectHandle *> mHandles; // list of client handles
195 // First handle in mHandles has highest priority and controls the effect module
196
197 // Audio policy effect state management
198 // Mutex protecting transactions with audio policy manager as mLock cannot
199 // be held to avoid cross deadlocks with audio policy mutex
200 Mutex mPolicyLock;
201 // Effect is registered in APM or not
202 bool mPolicyRegistered = false;
203 // Effect enabled state communicated to APM. Enabled state corresponds to
204 // state requested by the EffectHandle with control
205 bool mPolicyEnabled = false;
206};
207
208// The EffectModule class is a wrapper object controlling the effect engine implementation
209// in the effect library. It prevents concurrent calls to process() and command() functions
210// from different client threads. It keeps a list of EffectHandle objects corresponding
211// to all client applications using this effect and notifies applications of effect state,
212// control or parameter changes. It manages the activation state machine to send appropriate
213// reset, enable, disable commands to effect engine and provide volume
214// ramping when effects are activated/deactivated.
215// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
216// the attached track(s) to accumulate their auxiliary channel.
217class EffectModule : public EffectBase {
218public:
219 EffectModule(const sp<EffectCallbackInterface>& callabck,
220 effect_descriptor_t *desc,
221 int id,
222 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800223 bool pinned,
224 audio_port_handle_t deviceId);
Eric Laurent41709552019-12-16 19:34:05 -0800225 virtual ~EffectModule();
226
227 void process();
228 bool updateState();
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700229 status_t command(int32_t cmdCode,
230 const std::vector<uint8_t>& cmdData,
231 int32_t maxReplySize,
232 std::vector<uint8_t>* reply) override;
Eric Laurent41709552019-12-16 19:34:05 -0800233
234 void reset_l();
235 status_t configure();
236 status_t init();
237
238 uint32_t status() {
239 return mStatus;
240 }
241
242 bool isProcessEnabled() const;
243 bool isOffloadedOrDirect() const;
244 bool isVolumeControlEnabled() const;
245
246 void setInBuffer(const sp<EffectBufferHalInterface>& buffer);
247 int16_t *inBuffer() const {
248 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
249 }
250 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer);
251 int16_t *outBuffer() const {
252 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
253 }
254
Eric Laurent41709552019-12-16 19:34:05 -0800255 status_t setDevices(const AudioDeviceTypeAddrVector &devices);
256 status_t setInputDevice(const AudioDeviceTypeAddr &device);
257 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
258 status_t setMode(audio_mode_t mode);
259 status_t setAudioSource(audio_source_t source);
260 status_t start();
261 status_t stop();
262
263 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
264 bool isOffloaded() const;
265 void addEffectToHal_l();
266 void release_l();
267
268 sp<EffectModule> asEffectModule() override { return this; }
269
jiabineb3bda02020-06-30 14:07:03 -0700270 static bool isHapticGenerator(const effect_uuid_t* type);
271 bool isHapticGenerator() const;
272
jiabine70bc7f2020-06-30 22:07:55 -0700273 status_t setHapticIntensity(int id, int intensity);
Lais Andradebc3f37a2021-07-02 00:13:19 +0100274 status_t setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo);
jiabine70bc7f2020-06-30 22:07:55 -0700275
Eric Laurent41709552019-12-16 19:34:05 -0800276 void dump(int fd, const Vector<String16>& args);
277
278private:
279 friend class AudioFlinger; // for mHandles
Eric Laurent81784c32012-11-19 14:55:58 -0800280
281 // Maximum time allocated to effect engines to complete the turn off sequence
282 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
283
Mikhail Naganovbf493082017-04-17 17:37:12 -0700284 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800285
286 status_t start_l();
287 status_t stop_l();
Eric Laurent6b446ce2019-12-13 10:56:31 -0800288 status_t removeEffectFromHal_l();
jiabin8f278ee2019-11-11 12:16:27 -0800289 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Eric Laurent81784c32012-11-19 14:55:58 -0800290
Eric Laurent81784c32012-11-19 14:55:58 -0800291 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700292 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800293 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
294 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800295 status_t mStatus; // initialization status
Eric Laurent81784c32012-11-19 14:55:58 -0800296 // First handle in mHandles has highest priority and controls the effect module
297 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
298 // sending disable command.
299 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent5baf2af2013-09-12 17:37:00 -0700300 bool mOffloaded; // effect is currently offloaded to the audio DSP
David Li6c8ac4b2021-06-22 22:17:52 +0800301 bool mAddedToHal; // effect has been added to the audio HAL
rago94a1ee82017-07-21 15:11:02 -0700302
303#ifdef FLOAT_EFFECT_CHAIN
304 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800305 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
306 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800307 uint32_t mInChannelCountRequested;
308 uint32_t mOutChannelCountRequested;
rago94a1ee82017-07-21 15:11:02 -0700309#endif
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900310
311 class AutoLockReentrant {
312 public:
313 AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
314 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
315 {
316 if (mMutex != nullptr) mMutex->lock();
317 }
318 ~AutoLockReentrant() {
319 if (mMutex != nullptr) mMutex->unlock();
320 }
321 private:
322 Mutex * const mMutex;
323 };
324
325 static constexpr pid_t INVALID_PID = (pid_t)-1;
326 // this tid is allowed to call setVolume() without acquiring the mutex.
327 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent81784c32012-11-19 14:55:58 -0800328};
329
330// The EffectHandle class implements the IEffect interface. It provides resources
331// to receive parameter updates, keeps track of effect control
332// ownership and state and has a pointer to the EffectModule object it is controlling.
333// There is one EffectHandle object for each application controlling (or using)
334// an effect module.
335// The EffectHandle is obtained by calling AudioFlinger::createEffect().
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700336class EffectHandle: public android::media::BnEffect {
Eric Laurent81784c32012-11-19 14:55:58 -0800337public:
338
Eric Laurent41709552019-12-16 19:34:05 -0800339 EffectHandle(const sp<EffectBase>& effect,
Eric Laurent81784c32012-11-19 14:55:58 -0800340 const sp<AudioFlinger::Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700341 const sp<media::IEffectClient>& effectClient,
Eric Laurent81784c32012-11-19 14:55:58 -0800342 int32_t priority);
343 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800344 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800345
346 // IEffect
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700347 android::binder::Status enable(int32_t* _aidl_return) override;
348 android::binder::Status disable(int32_t* _aidl_return) override;
349 android::binder::Status command(int32_t cmdCode,
350 const std::vector<uint8_t>& cmdData,
351 int32_t maxResponseSize,
352 std::vector<uint8_t>* response,
353 int32_t* _aidl_return) override;
354 android::binder::Status disconnect() override;
355 android::binder::Status getCblk(media::SharedFileRegion* _aidl_return) override;
Eric Laurent81784c32012-11-19 14:55:58 -0800356
Eric Laurentd66d7a12021-07-13 13:35:32 +0200357 sp<Client> client() const { return mClient; }
358
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700359private:
360 void disconnect(bool unpinIfLast);
Eric Laurent81784c32012-11-19 14:55:58 -0800361
362 // Give or take control of effect module
363 // - hasControl: true if control is given, false if removed
364 // - signal: true client app should be signaled of change, false otherwise
365 // - enabled: state of the effect when control is passed
366 void setControl(bool hasControl, bool signal, bool enabled);
367 void commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700368 const std::vector<uint8_t>& cmdData,
369 const std::vector<uint8_t>& replyData);
Eric Laurent81784c32012-11-19 14:55:58 -0800370 void setEnabled(bool enabled);
371 bool enabled() const { return mEnabled; }
372
373 // Getters
Eric Laurent41709552019-12-16 19:34:05 -0800374 wp<EffectBase> effect() const { return mEffect; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800375 int id() const {
Eric Laurent41709552019-12-16 19:34:05 -0800376 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800377 if (effect == 0) {
378 return 0;
379 }
380 return effect->id();
381 }
Eric Laurent81784c32012-11-19 14:55:58 -0800382 int priority() const { return mPriority; }
383 bool hasControl() const { return mHasControl; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800384 bool disconnected() const { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800385
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800386 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800387
Mikhail Naganovbf493082017-04-17 17:37:12 -0700388private:
Eric Laurent81784c32012-11-19 14:55:58 -0800389 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
Mikhail Naganovbf493082017-04-17 17:37:12 -0700390 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800391
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700392 Mutex mLock; // protects IEffect method calls
Andy Hung318e0242020-12-21 10:33:49 -0800393 const wp<EffectBase> mEffect; // pointer to controlled EffectModule
394 const sp<media::IEffectClient> mEffectClient; // callback interface for client notifications
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700395 /*const*/ sp<Client> mClient; // client for shared memory allocation, see
396 // disconnect()
397 sp<IMemory> mCblkMemory; // shared memory for control block
398 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
399 // shared memory
400 uint8_t* mBuffer; // pointer to parameter area in shared memory
401 int mPriority; // client application priority to control the effect
402 bool mHasControl; // true if this handle is controlling the effect
403 bool mEnabled; // cached enable state: needed when the effect is
404 // restored after being suspended
405 bool mDisconnected; // Set to true by disconnect()
Eric Laurent81784c32012-11-19 14:55:58 -0800406};
407
408// the EffectChain class represents a group of effects associated to one audio session.
409// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800410// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
411// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800412// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
413// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800414// order corresponding in the effect process order. When attached to a track (session ID !=
415// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800416// it also provide it's own input buffer used by the track as accumulation buffer.
417class EffectChain : public RefBase {
418public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800419 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800420 virtual ~EffectChain();
421
422 // special key used for an entry in mSuspendedEffects keyed vector
423 // corresponding to a suspend all request.
424 static const int kKeyForSuspendAll = 0;
425
426 // minimum duration during which we force calling effect process when last track on
427 // a session is stopped or removed to allow effect tail to be rendered
428 static const int kProcessTailDurationMs = 1000;
429
430 void process_l();
431
432 void lock() {
433 mLock.lock();
434 }
435 void unlock() {
436 mLock.unlock();
437 }
438
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800439 status_t createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800440 effect_descriptor_t *desc,
441 int id,
442 audio_session_t sessionId,
443 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800444 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800445 status_t addEffect_ll(const sp<EffectModule>& handle);
446 size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
Eric Laurent81784c32012-11-19 14:55:58 -0800447
Glenn Kastend848eb42016-03-08 13:42:11 -0800448 audio_session_t sessionId() const { return mSessionId; }
449 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800450
451 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
452 sp<EffectModule> getEffectFromId_l(int id);
453 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurent6c796322019-04-09 14:13:17 -0700454 std::vector<int> getEffectIds();
Glenn Kastenc56f3422014-03-21 17:53:17 -0700455 // FIXME use float to improve the dynamic range
Eric Laurentfa1e1232016-08-02 19:01:49 -0700456 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
457 void resetVolume_l();
jiabin8f278ee2019-11-11 12:16:27 -0800458 void setDevices_l(const AudioDeviceTypeAddrVector &devices);
459 void setInputDevice_l(const AudioDeviceTypeAddr &device);
Eric Laurent81784c32012-11-19 14:55:58 -0800460 void setMode_l(audio_mode_t mode);
461 void setAudioSource_l(audio_source_t source);
462
Mikhail Naganov022b9952017-01-04 16:36:51 -0800463 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800464 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800465 }
rago94a1ee82017-07-21 15:11:02 -0700466 effect_buffer_t *inBuffer() const {
467 return mInBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800468 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800469 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800470 mOutBuffer = buffer;
471 }
rago94a1ee82017-07-21 15:11:02 -0700472 effect_buffer_t *outBuffer() const {
473 return mOutBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800474 }
475
476 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
477 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
478 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
479
480 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
481 mTailBufferCount = mMaxTailBuffers; }
482 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
483 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
484
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800485 product_strategy_t strategy() const { return mStrategy; }
486 void setStrategy(product_strategy_t strategy)
Eric Laurent81784c32012-11-19 14:55:58 -0800487 { mStrategy = strategy; }
488
Eric Laurentd8365c52017-07-16 15:27:05 -0700489 // suspend or restore effects of the specified type. The number of suspend requests is counted
490 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800491 void setEffectSuspended_l(const effect_uuid_t *type,
492 bool suspend);
493 // suspend all eligible effects
494 void setEffectSuspendedAll_l(bool suspend);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800495 // check if effects should be suspended or restored when a given effect is enable or disabled
496 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, bool enabled);
Eric Laurent81784c32012-11-19 14:55:58 -0800497
498 void clearInputBuffer();
499
Eric Laurent5baf2af2013-09-12 17:37:00 -0700500 // At least one non offloadable effect in the chain is enabled
501 bool isNonOffloadableEnabled();
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +0900502 bool isNonOffloadableEnabled_l();
Eric Laurent813e2a72013-08-31 12:59:48 -0700503
Eric Laurent1b928682014-10-02 19:41:47 -0700504 void syncHalEffectsState();
505
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700506 // flags is an ORed set of audio_output_flags_t which is updated on return.
507 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
508
509 // flags is an ORed set of audio_input_flags_t which is updated on return.
510 void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
511
512 // Is this EffectChain compatible with the RAW audio flag.
513 bool isRawCompatible() const;
514
515 // Is this EffectChain compatible with the FAST audio flag.
516 bool isFastCompatible() const;
Eric Laurent4c415062016-06-17 16:14:16 -0700517
518 // isCompatibleWithThread_l() must be called with thread->mLock held
519 bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
520
jiabineb3bda02020-06-30 14:07:03 -0700521 bool containsHapticGeneratingEffect_l();
522
jiabine70bc7f2020-06-30 22:07:55 -0700523 void setHapticIntensity_l(int id, int intensity);
524
Eric Laurent6b446ce2019-12-13 10:56:31 -0800525 sp<EffectCallbackInterface> effectCallback() const { return mEffectCallback; }
526 wp<ThreadBase> thread() const { return mEffectCallback->thread(); }
527
Eric Laurent81784c32012-11-19 14:55:58 -0800528 void dump(int fd, const Vector<String16>& args);
529
Mikhail Naganovbf493082017-04-17 17:37:12 -0700530private:
Eric Laurent6b446ce2019-12-13 10:56:31 -0800531
Andy Hung328d6772021-01-12 12:32:21 -0800532 // For transaction consistency, please consider holding the EffectChain lock before
533 // calling the EffectChain::EffectCallback methods, excepting
534 // createEffectHal and allocateHalBuffer.
535 //
536 // This prevents migration of the EffectChain to another PlaybackThread
537 // for the purposes of the EffectCallback.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800538 class EffectCallback : public EffectCallbackInterface {
539 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800540 // Note: ctors taking a weak pointer to their owner must not promote it
541 // during construction (but may keep a reference for later promotion).
542 EffectCallback(const wp<EffectChain>& owner,
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800543 const wp<ThreadBase>& thread)
Andy Hung6626a012021-01-12 13:38:00 -0800544 : mChain(owner)
545 , mThread(thread)
546 , mAudioFlinger(*gAudioFlinger) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800547 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800548
549 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
550 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
551 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
Eric Laurent41709552019-12-16 19:34:05 -0800552 bool updateOrphanEffectChains(const sp<EffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800553
554 audio_io_handle_t io() const override;
555 bool isOutput() const override;
556 bool isOffload() const override;
557 bool isOffloadOrDirect() const override;
558 bool isOffloadOrMmap() const override;
559
560 uint32_t sampleRate() const override;
561 audio_channel_mask_t channelMask() const override;
562 uint32_t channelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700563 audio_channel_mask_t hapticChannelMask() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800564 size_t frameCount() const override;
565 uint32_t latency() const override;
566
567 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
568 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
569 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
570 void setVolumeForOutput(float left, float right) const override;
571
572 // check if effects should be suspended/restored when a given effect is enable/disabled
Eric Laurent41709552019-12-16 19:34:05 -0800573 void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800574 bool enabled, bool threadLocked) override;
575 void resetVolume() override;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800576 product_strategy_t strategy() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800577 int32_t activeTrackCnt() const override;
Eric Laurent41709552019-12-16 19:34:05 -0800578 void onEffectEnable(const sp<EffectBase>& effect) override;
579 void onEffectDisable(const sp<EffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800580
581 wp<EffectChain> chain() const override { return mChain; }
582
Eric Laurentd66d7a12021-07-13 13:35:32 +0200583 bool isAudioPolicyReady() const override {
584 return mAudioFlinger.isAudioPolicyReady();
585 }
586
Andy Hung328d6772021-01-12 12:32:21 -0800587 wp<ThreadBase> thread() const { return mThread.load(); }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800588
589 void setThread(const wp<ThreadBase>& thread) {
590 mThread = thread;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800591 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800592
593 private:
Andy Hung318e0242020-12-21 10:33:49 -0800594 const wp<EffectChain> mChain;
Andy Hung328d6772021-01-12 12:32:21 -0800595 mediautils::atomic_wp<ThreadBase> mThread;
Andy Hung6626a012021-01-12 13:38:00 -0800596 AudioFlinger &mAudioFlinger; // implementation detail: outer instance always exists.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800597 };
598
Eric Laurent81784c32012-11-19 14:55:58 -0800599 friend class AudioFlinger; // for mThread, mEffects
Mikhail Naganovbf493082017-04-17 17:37:12 -0700600 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800601
602 class SuspendedEffectDesc : public RefBase {
603 public:
604 SuspendedEffectDesc() : mRefCount(0) {}
605
Eric Laurentd8365c52017-07-16 15:27:05 -0700606 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800607 effect_uuid_t mType;
608 wp<EffectModule> mEffect;
609 };
610
611 // get a list of effect modules to suspend when an effect of the type
612 // passed is enabled.
613 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
614
615 // get an effect module if it is currently enable
616 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
617 // true if the effect whose descriptor is passed can be suspended
618 // OEMs can modify the rules implemented in this method to exclude specific effect
619 // types or implementations from the suspend/restore mechanism.
620 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
621
Eric Laurentd8365c52017-07-16 15:27:05 -0700622 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
623
Eric Laurent6b446ce2019-12-13 10:56:31 -0800624 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800625
Eric Laurentaaa44472014-09-12 17:41:50 -0700626 void setThread(const sp<ThreadBase>& thread);
627
Zhou Songd505c642020-02-20 16:35:37 +0800628 // true if any effect module within the chain has volume control
629 bool hasVolumeControlEnabled_l() const;
630
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900631 void setVolumeForOutput_l(uint32_t left, uint32_t right);
632
Eric Laurent4c415062016-06-17 16:14:16 -0700633 mutable Mutex mLock; // mutex protecting effect list
634 Vector< sp<EffectModule> > mEffects; // list of effect modules
635 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800636 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
637 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800638
639 // 'volatile' here means these are accessed with atomic operations instead of mutex
640 volatile int32_t mActiveTrackCnt; // number of active tracks connected
641 volatile int32_t mTrackCnt; // number of tracks connected
642
Eric Laurent4c415062016-06-17 16:14:16 -0700643 int32_t mTailBufferCount; // current effect tail buffer count
644 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700645 int mVolumeCtrlIdx; // index of insert effect having control over volume
646 uint32_t mLeftVolume; // previous volume on left channel
647 uint32_t mRightVolume; // previous volume on right channel
648 uint32_t mNewLeftVolume; // new volume on left channel
649 uint32_t mNewRightVolume; // new volume on right channel
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800650 product_strategy_t mStrategy; // strategy for this effect chain
Eric Laurent4c415062016-06-17 16:14:16 -0700651 // mSuspendedEffects lists all effects currently suspended in the chain.
652 // Use effect type UUID timelow field as key. There is no real risk of identical
653 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700654 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700655 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800656
657 const sp<EffectCallback> mEffectCallback;
Eric Laurent81784c32012-11-19 14:55:58 -0800658};
Eric Laurentb82e6b72019-11-22 17:25:04 -0800659
660class DeviceEffectProxy : public EffectBase {
661public:
662 DeviceEffectProxy (const AudioDeviceTypeAddr& device,
663 const sp<DeviceEffectManagerCallback>& callback,
664 effect_descriptor_t *desc, int id)
665 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
666 mDevice(device), mManagerCallback(callback),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800667 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this),
668 callback)) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800669
670 status_t setEnabled(bool enabled, bool fromHandle) override;
671 sp<DeviceEffectProxy> asDeviceEffectProxy() override { return this; }
672
673 status_t init(const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches);
674 status_t onCreatePatch(audio_patch_handle_t patchHandle, const PatchPanel::Patch& patch);
675 void onReleasePatch(audio_patch_handle_t patchHandle);
676
677 size_t removeEffect(const sp<EffectModule>& effect);
678
679 status_t addEffectToHal(sp<EffectHalInterface> effect);
680 status_t removeEffectFromHal(sp<EffectHalInterface> effect);
681
682 const AudioDeviceTypeAddr& device() { return mDevice; };
683 bool isOutput() const;
684 uint32_t sampleRate() const;
685 audio_channel_mask_t channelMask() const;
686 uint32_t channelCount() const;
687
688 void dump(int fd, int spaces);
689
690private:
691
692 class ProxyCallback : public EffectCallbackInterface {
693 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800694 // Note: ctors taking a weak pointer to their owner must not promote it
695 // during construction (but may keep a reference for later promotion).
696 ProxyCallback(const wp<DeviceEffectProxy>& owner,
697 const sp<DeviceEffectManagerCallback>& callback)
698 : mProxy(owner), mManagerCallback(callback) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800699
700 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
701 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
702 status_t allocateHalBuffer(size_t size __unused,
703 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
704 bool updateOrphanEffectChains(const sp<EffectBase>& effect __unused) override {
705 return false;
706 }
707
708 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
709 bool isOutput() const override;
710 bool isOffload() const override { return false; }
711 bool isOffloadOrDirect() const override { return false; }
712 bool isOffloadOrMmap() const override { return false; }
713
714 uint32_t sampleRate() const override;
715 audio_channel_mask_t channelMask() const override;
716 uint32_t channelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700717 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800718 size_t frameCount() const override { return 0; }
719 uint32_t latency() const override { return 0; }
720
721 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
722 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
723
724 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
725 void setVolumeForOutput(float left __unused, float right __unused) const override {}
726
727 void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect __unused,
728 bool enabled __unused, bool threadLocked __unused) override {}
729 void resetVolume() override {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800730 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800731 int32_t activeTrackCnt() const override { return 0; }
732 void onEffectEnable(const sp<EffectBase>& effect __unused) override {}
733 void onEffectDisable(const sp<EffectBase>& effect __unused) override {}
734
735 wp<EffectChain> chain() const override { return nullptr; }
736
Eric Laurentd66d7a12021-07-13 13:35:32 +0200737 bool isAudioPolicyReady() const override {
738 return mManagerCallback->isAudioPolicyReady();
739 }
740
Eric Laurentb82e6b72019-11-22 17:25:04 -0800741 int newEffectId();
742
743 private:
744 const wp<DeviceEffectProxy> mProxy;
745 const sp<DeviceEffectManagerCallback> mManagerCallback;
746 };
747
748 status_t checkPort(const PatchPanel::Patch& patch, const struct audio_port_config *port,
749 sp<EffectHandle> *handle);
750
751 const AudioDeviceTypeAddr mDevice;
752 const sp<DeviceEffectManagerCallback> mManagerCallback;
753 const sp<ProxyCallback> mMyCallback;
754
755 Mutex mProxyLock;
756 std::map<audio_patch_handle_t, sp<EffectHandle>> mEffectHandles; // protected by mProxyLock
757 sp<EffectModule> mHalEffect; // protected by mProxyLock
758 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
759};