blob: 1d0d00dbb787eb2fd15282a95385ccf5f9df1b66 [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 Laurentf1f22e72021-07-13 14:04:14 +020037 virtual audio_channel_mask_t inChannelMask(int id) const = 0;
38 virtual uint32_t inChannelCount(int id) const = 0;
39 virtual audio_channel_mask_t outChannelMask() const = 0;
40 virtual uint32_t outChannelCount() const = 0;
jiabineb3bda02020-06-30 14:07:03 -070041 virtual audio_channel_mask_t hapticChannelMask() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080042 virtual size_t frameCount() const = 0;
43
44 // Non trivial methods usually implemented with help from ThreadBase:
45 // pay attention to mutex locking order
46 virtual uint32_t latency() const { return 0; }
47 virtual status_t addEffectToHal(sp<EffectHalInterface> effect) = 0;
48 virtual status_t removeEffectFromHal(sp<EffectHalInterface> effect) = 0;
49 virtual void setVolumeForOutput(float left, float right) const = 0;
50 virtual bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080051 virtual void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -080052 bool enabled,
53 bool threadLocked) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080054 virtual void onEffectEnable(const sp<EffectBase>& effect) = 0;
55 virtual void onEffectDisable(const sp<EffectBase>& effect) = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080056
57 // Methods usually implemented with help from AudioFlinger: pay attention to mutex locking order
58 virtual status_t createEffectHal(const effect_uuid_t *pEffectUuid,
59 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) = 0;
60 virtual status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080061 virtual bool updateOrphanEffectChains(const sp<EffectBase>& effect) = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080062
63 // Methods usually implemented with help from EffectChain: pay attention to mutex locking order
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -080064 virtual product_strategy_t strategy() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080065 virtual int32_t activeTrackCnt() const = 0;
66 virtual void resetVolume() = 0;
67
68 virtual wp<EffectChain> chain() const = 0;
Eric Laurentd66d7a12021-07-13 13:35:32 +020069
70 virtual bool isAudioPolicyReady() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080071};
72
Eric Laurent41709552019-12-16 19:34:05 -080073// EffectBase(EffectModule) and EffectChain classes both have their own mutex to protect
Eric Laurent81784c32012-11-19 14:55:58 -080074// state changes or resource modifications. Always respect the following order
75// if multiple mutexes must be acquired to avoid cross deadlock:
Eric Laurent41709552019-12-16 19:34:05 -080076// AudioFlinger -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
77// AudioHandle -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
Eric Laurent6b446ce2019-12-13 10:56:31 -080078
79// NOTE: When implementing the EffectCallbackInterface, in an EffectChain or other, it is important
80// to pay attention to this locking order as some callback methods can be called from a state where
81// EffectModule and/or EffectChain mutexes are held.
82
Eric Laurenteb3c3372013-09-25 12:25:29 -070083// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
Eric Laurent3bc859b2016-12-05 11:07:22 -080084// startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or
85// Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService
86// methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order.
Eric Laurent81784c32012-11-19 14:55:58 -080087
Eric Laurent41709552019-12-16 19:34:05 -080088
89// The EffectBase class contains common properties, state and behavior for and EffectModule or
90// other derived classes managing an audio effect instance within the effect framework.
91// It also contains the class mutex (see comment on locking order above).
92class EffectBase : public RefBase {
Eric Laurent81784c32012-11-19 14:55:58 -080093public:
Eric Laurent41709552019-12-16 19:34:05 -080094 EffectBase(const sp<EffectCallbackInterface>& callback,
95 effect_descriptor_t *desc,
96 int id,
97 audio_session_t sessionId,
98 bool pinned);
99
100 ~EffectBase() override = default;
Eric Laurent81784c32012-11-19 14:55:58 -0800101
102 enum effect_state {
103 IDLE,
104 RESTART,
105 STARTING,
106 ACTIVE,
107 STOPPING,
108 STOPPED,
109 DESTROYED
110 };
111
Eric Laurent41709552019-12-16 19:34:05 -0800112 int id() const { return mId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800113 effect_state state() const {
114 return mState;
115 }
Glenn Kastend848eb42016-03-08 13:42:11 -0800116 audio_session_t sessionId() const {
Eric Laurent81784c32012-11-19 14:55:58 -0800117 return mSessionId;
118 }
Eric Laurent81784c32012-11-19 14:55:58 -0800119 const effect_descriptor_t& desc() const { return mDescriptor; }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700120 bool isOffloadable() const
121 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Eric Laurent4c415062016-06-17 16:14:16 -0700122 bool isImplementationSoftware() const
123 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700124 bool isProcessImplemented() const
125 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900126 bool isVolumeControl() const
127 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
128 == EFFECT_FLAG_VOLUME_CTRL; }
Jasmine Cha934ecfb2019-01-23 18:19:14 +0800129 bool isVolumeMonitor() const
130 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
131 == EFFECT_FLAG_VOLUME_MONITOR; }
Eric Laurent41709552019-12-16 19:34:05 -0800132
133 virtual status_t setEnabled(bool enabled, bool fromHandle);
134 status_t setEnabled_l(bool enabled);
135 bool isEnabled() const;
136
137 void setSuspended(bool suspended);
138 bool suspended() const;
139
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700140 virtual status_t command(int32_t __unused,
141 const std::vector<uint8_t>& __unused,
142 int32_t __unused,
143 std::vector<uint8_t>* __unused) { return NO_ERROR; };
Eric Laurent41709552019-12-16 19:34:05 -0800144
Andy Hungfda44002021-06-03 17:23:16 -0700145 // mCallback is atomic so this can be lock-free.
Eric Laurent41709552019-12-16 19:34:05 -0800146 void setCallback(const sp<EffectCallbackInterface>& callback) { mCallback = callback; }
Andy Hungfda44002021-06-03 17:23:16 -0700147 sp<EffectCallbackInterface> getCallback() const { return mCallback.load(); }
Eric Laurent41709552019-12-16 19:34:05 -0800148
149 status_t addHandle(EffectHandle *handle);
150 ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
151 ssize_t removeHandle(EffectHandle *handle);
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530152 ssize_t removeHandle_l(EffectHandle *handle);
Eric Laurent41709552019-12-16 19:34:05 -0800153 EffectHandle* controlHandle_l();
154 bool purgeHandles();
155
156 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked);
157
158 bool isPinned() const { return mPinned; }
159 void unPin() { mPinned = false; }
160
161 void lock() { mLock.lock(); }
162 void unlock() { mLock.unlock(); }
Eric Laurent81784c32012-11-19 14:55:58 -0800163
Eric Laurent6c796322019-04-09 14:13:17 -0700164 status_t updatePolicyState();
Eric Laurent41709552019-12-16 19:34:05 -0800165
166 virtual sp<EffectModule> asEffectModule() { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800167 virtual sp<DeviceEffectProxy> asDeviceEffectProxy() { return nullptr; }
Eric Laurent6c796322019-04-09 14:13:17 -0700168
Eric Laurent81784c32012-11-19 14:55:58 -0800169 void dump(int fd, const Vector<String16>& args);
170
Eric Laurentd66d7a12021-07-13 13:35:32 +0200171protected:
172 bool isInternal_l() const {
173 for (auto handle : mHandles) {
174 if (handle->client() != nullptr) {
175 return false;
176 }
177 }
178 return true;
179 }
180
Mikhail Naganovbf493082017-04-17 17:37:12 -0700181private:
Eric Laurent81784c32012-11-19 14:55:58 -0800182 friend class AudioFlinger; // for mHandles
Eric Laurent41709552019-12-16 19:34:05 -0800183 bool mPinned = false;
184
185 DISALLOW_COPY_AND_ASSIGN(EffectBase);
186
187mutable Mutex mLock; // mutex for process, commands and handles list protection
Andy Hungfda44002021-06-03 17:23:16 -0700188 mediautils::atomic_sp<EffectCallbackInterface> mCallback; // parent effect chain
Eric Laurent41709552019-12-16 19:34:05 -0800189 const int mId; // this instance unique ID
190 const audio_session_t mSessionId; // audio session ID
191 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
192 effect_state mState = IDLE; // current activation state
Eric Laurentd9eb4962019-12-19 09:20:49 -0800193 // effect is suspended: temporarily disabled by framework
194 bool mSuspended = false;
Eric Laurent41709552019-12-16 19:34:05 -0800195
196 Vector<EffectHandle *> mHandles; // list of client handles
197 // First handle in mHandles has highest priority and controls the effect module
198
199 // Audio policy effect state management
200 // Mutex protecting transactions with audio policy manager as mLock cannot
201 // be held to avoid cross deadlocks with audio policy mutex
202 Mutex mPolicyLock;
203 // Effect is registered in APM or not
204 bool mPolicyRegistered = false;
205 // Effect enabled state communicated to APM. Enabled state corresponds to
206 // state requested by the EffectHandle with control
207 bool mPolicyEnabled = false;
208};
209
210// The EffectModule class is a wrapper object controlling the effect engine implementation
211// in the effect library. It prevents concurrent calls to process() and command() functions
212// from different client threads. It keeps a list of EffectHandle objects corresponding
213// to all client applications using this effect and notifies applications of effect state,
214// control or parameter changes. It manages the activation state machine to send appropriate
215// reset, enable, disable commands to effect engine and provide volume
216// ramping when effects are activated/deactivated.
217// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
218// the attached track(s) to accumulate their auxiliary channel.
219class EffectModule : public EffectBase {
220public:
221 EffectModule(const sp<EffectCallbackInterface>& callabck,
222 effect_descriptor_t *desc,
223 int id,
224 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800225 bool pinned,
226 audio_port_handle_t deviceId);
Eric Laurent41709552019-12-16 19:34:05 -0800227 virtual ~EffectModule();
228
229 void process();
230 bool updateState();
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700231 status_t command(int32_t cmdCode,
232 const std::vector<uint8_t>& cmdData,
233 int32_t maxReplySize,
234 std::vector<uint8_t>* reply) override;
Eric Laurent41709552019-12-16 19:34:05 -0800235
236 void reset_l();
237 status_t configure();
238 status_t init();
239
240 uint32_t status() {
241 return mStatus;
242 }
243
244 bool isProcessEnabled() const;
245 bool isOffloadedOrDirect() const;
246 bool isVolumeControlEnabled() const;
247
248 void setInBuffer(const sp<EffectBufferHalInterface>& buffer);
249 int16_t *inBuffer() const {
250 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
251 }
252 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer);
253 int16_t *outBuffer() const {
254 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
255 }
256
Eric Laurent41709552019-12-16 19:34:05 -0800257 status_t setDevices(const AudioDeviceTypeAddrVector &devices);
258 status_t setInputDevice(const AudioDeviceTypeAddr &device);
259 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
260 status_t setMode(audio_mode_t mode);
261 status_t setAudioSource(audio_source_t source);
262 status_t start();
263 status_t stop();
264
265 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
266 bool isOffloaded() const;
267 void addEffectToHal_l();
268 void release_l();
269
270 sp<EffectModule> asEffectModule() override { return this; }
271
jiabineb3bda02020-06-30 14:07:03 -0700272 static bool isHapticGenerator(const effect_uuid_t* type);
273 bool isHapticGenerator() const;
274
jiabine70bc7f2020-06-30 22:07:55 -0700275 status_t setHapticIntensity(int id, int intensity);
Lais Andradebc3f37a2021-07-02 00:13:19 +0100276 status_t setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo);
jiabine70bc7f2020-06-30 22:07:55 -0700277
Eric Laurent41709552019-12-16 19:34:05 -0800278 void dump(int fd, const Vector<String16>& args);
279
280private:
281 friend class AudioFlinger; // for mHandles
Eric Laurent81784c32012-11-19 14:55:58 -0800282
283 // Maximum time allocated to effect engines to complete the turn off sequence
284 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
285
Mikhail Naganovbf493082017-04-17 17:37:12 -0700286 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800287
288 status_t start_l();
289 status_t stop_l();
Eric Laurent6b446ce2019-12-13 10:56:31 -0800290 status_t removeEffectFromHal_l();
jiabin8f278ee2019-11-11 12:16:27 -0800291 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Eric Laurent81784c32012-11-19 14:55:58 -0800292
Eric Laurent81784c32012-11-19 14:55:58 -0800293 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700294 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800295 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
296 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800297 status_t mStatus; // initialization status
Eric Laurent81784c32012-11-19 14:55:58 -0800298 // First handle in mHandles has highest priority and controls the effect module
299 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
300 // sending disable command.
301 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent5baf2af2013-09-12 17:37:00 -0700302 bool mOffloaded; // effect is currently offloaded to the audio DSP
David Li6c8ac4b2021-06-22 22:17:52 +0800303 bool mAddedToHal; // effect has been added to the audio HAL
rago94a1ee82017-07-21 15:11:02 -0700304
305#ifdef FLOAT_EFFECT_CHAIN
306 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800307 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
308 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800309 uint32_t mInChannelCountRequested;
310 uint32_t mOutChannelCountRequested;
rago94a1ee82017-07-21 15:11:02 -0700311#endif
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900312
313 class AutoLockReentrant {
314 public:
315 AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
316 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
317 {
318 if (mMutex != nullptr) mMutex->lock();
319 }
320 ~AutoLockReentrant() {
321 if (mMutex != nullptr) mMutex->unlock();
322 }
323 private:
324 Mutex * const mMutex;
325 };
326
327 static constexpr pid_t INVALID_PID = (pid_t)-1;
328 // this tid is allowed to call setVolume() without acquiring the mutex.
329 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent81784c32012-11-19 14:55:58 -0800330};
331
332// The EffectHandle class implements the IEffect interface. It provides resources
333// to receive parameter updates, keeps track of effect control
334// ownership and state and has a pointer to the EffectModule object it is controlling.
335// There is one EffectHandle object for each application controlling (or using)
336// an effect module.
337// The EffectHandle is obtained by calling AudioFlinger::createEffect().
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700338class EffectHandle: public android::media::BnEffect {
Eric Laurent81784c32012-11-19 14:55:58 -0800339public:
340
Eric Laurent41709552019-12-16 19:34:05 -0800341 EffectHandle(const sp<EffectBase>& effect,
Eric Laurent81784c32012-11-19 14:55:58 -0800342 const sp<AudioFlinger::Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700343 const sp<media::IEffectClient>& effectClient,
Eric Laurent81784c32012-11-19 14:55:58 -0800344 int32_t priority);
345 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800346 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800347
348 // IEffect
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700349 android::binder::Status enable(int32_t* _aidl_return) override;
350 android::binder::Status disable(int32_t* _aidl_return) override;
351 android::binder::Status command(int32_t cmdCode,
352 const std::vector<uint8_t>& cmdData,
353 int32_t maxResponseSize,
354 std::vector<uint8_t>* response,
355 int32_t* _aidl_return) override;
356 android::binder::Status disconnect() override;
357 android::binder::Status getCblk(media::SharedFileRegion* _aidl_return) override;
Eric Laurent81784c32012-11-19 14:55:58 -0800358
Eric Laurentd66d7a12021-07-13 13:35:32 +0200359 sp<Client> client() const { return mClient; }
360
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700361private:
362 void disconnect(bool unpinIfLast);
Eric Laurent81784c32012-11-19 14:55:58 -0800363
364 // Give or take control of effect module
365 // - hasControl: true if control is given, false if removed
366 // - signal: true client app should be signaled of change, false otherwise
367 // - enabled: state of the effect when control is passed
368 void setControl(bool hasControl, bool signal, bool enabled);
369 void commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700370 const std::vector<uint8_t>& cmdData,
371 const std::vector<uint8_t>& replyData);
Eric Laurent81784c32012-11-19 14:55:58 -0800372 void setEnabled(bool enabled);
373 bool enabled() const { return mEnabled; }
374
375 // Getters
Eric Laurent41709552019-12-16 19:34:05 -0800376 wp<EffectBase> effect() const { return mEffect; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800377 int id() const {
Eric Laurent41709552019-12-16 19:34:05 -0800378 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800379 if (effect == 0) {
380 return 0;
381 }
382 return effect->id();
383 }
Eric Laurent81784c32012-11-19 14:55:58 -0800384 int priority() const { return mPriority; }
385 bool hasControl() const { return mHasControl; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800386 bool disconnected() const { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800387
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800388 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800389
Mikhail Naganovbf493082017-04-17 17:37:12 -0700390private:
Eric Laurent81784c32012-11-19 14:55:58 -0800391 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
Mikhail Naganovbf493082017-04-17 17:37:12 -0700392 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800393
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700394 Mutex mLock; // protects IEffect method calls
Andy Hung318e0242020-12-21 10:33:49 -0800395 const wp<EffectBase> mEffect; // pointer to controlled EffectModule
396 const sp<media::IEffectClient> mEffectClient; // callback interface for client notifications
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -0700397 /*const*/ sp<Client> mClient; // client for shared memory allocation, see
398 // disconnect()
399 sp<IMemory> mCblkMemory; // shared memory for control block
400 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
401 // shared memory
402 uint8_t* mBuffer; // pointer to parameter area in shared memory
403 int mPriority; // client application priority to control the effect
404 bool mHasControl; // true if this handle is controlling the effect
405 bool mEnabled; // cached enable state: needed when the effect is
406 // restored after being suspended
407 bool mDisconnected; // Set to true by disconnect()
Eric Laurent81784c32012-11-19 14:55:58 -0800408};
409
410// the EffectChain class represents a group of effects associated to one audio session.
411// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800412// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
413// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800414// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
415// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800416// order corresponding in the effect process order. When attached to a track (session ID !=
417// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800418// it also provide it's own input buffer used by the track as accumulation buffer.
419class EffectChain : public RefBase {
420public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800421 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800422 virtual ~EffectChain();
423
424 // special key used for an entry in mSuspendedEffects keyed vector
425 // corresponding to a suspend all request.
426 static const int kKeyForSuspendAll = 0;
427
428 // minimum duration during which we force calling effect process when last track on
429 // a session is stopped or removed to allow effect tail to be rendered
430 static const int kProcessTailDurationMs = 1000;
431
432 void process_l();
433
434 void lock() {
435 mLock.lock();
436 }
437 void unlock() {
438 mLock.unlock();
439 }
440
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800441 status_t createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800442 effect_descriptor_t *desc,
443 int id,
444 audio_session_t sessionId,
445 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800446 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800447 status_t addEffect_ll(const sp<EffectModule>& handle);
448 size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
Eric Laurent81784c32012-11-19 14:55:58 -0800449
Glenn Kastend848eb42016-03-08 13:42:11 -0800450 audio_session_t sessionId() const { return mSessionId; }
451 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800452
453 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
454 sp<EffectModule> getEffectFromId_l(int id);
455 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurent6c796322019-04-09 14:13:17 -0700456 std::vector<int> getEffectIds();
Glenn Kastenc56f3422014-03-21 17:53:17 -0700457 // FIXME use float to improve the dynamic range
Eric Laurentfa1e1232016-08-02 19:01:49 -0700458 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
459 void resetVolume_l();
jiabin8f278ee2019-11-11 12:16:27 -0800460 void setDevices_l(const AudioDeviceTypeAddrVector &devices);
461 void setInputDevice_l(const AudioDeviceTypeAddr &device);
Eric Laurent81784c32012-11-19 14:55:58 -0800462 void setMode_l(audio_mode_t mode);
463 void setAudioSource_l(audio_source_t source);
464
Mikhail Naganov022b9952017-01-04 16:36:51 -0800465 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800466 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800467 }
rago94a1ee82017-07-21 15:11:02 -0700468 effect_buffer_t *inBuffer() const {
469 return mInBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800470 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800471 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800472 mOutBuffer = buffer;
473 }
rago94a1ee82017-07-21 15:11:02 -0700474 effect_buffer_t *outBuffer() const {
475 return mOutBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800476 }
477
478 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
479 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
480 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
481
482 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
483 mTailBufferCount = mMaxTailBuffers; }
484 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
485 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
486
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800487 product_strategy_t strategy() const { return mStrategy; }
488 void setStrategy(product_strategy_t strategy)
Eric Laurent81784c32012-11-19 14:55:58 -0800489 { mStrategy = strategy; }
490
Eric Laurentd8365c52017-07-16 15:27:05 -0700491 // suspend or restore effects of the specified type. The number of suspend requests is counted
492 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800493 void setEffectSuspended_l(const effect_uuid_t *type,
494 bool suspend);
495 // suspend all eligible effects
496 void setEffectSuspendedAll_l(bool suspend);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800497 // check if effects should be suspended or restored when a given effect is enable or disabled
498 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, bool enabled);
Eric Laurent81784c32012-11-19 14:55:58 -0800499
500 void clearInputBuffer();
501
Eric Laurent5baf2af2013-09-12 17:37:00 -0700502 // At least one non offloadable effect in the chain is enabled
503 bool isNonOffloadableEnabled();
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +0900504 bool isNonOffloadableEnabled_l();
Eric Laurent813e2a72013-08-31 12:59:48 -0700505
Eric Laurent1b928682014-10-02 19:41:47 -0700506 void syncHalEffectsState();
507
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700508 // flags is an ORed set of audio_output_flags_t which is updated on return.
509 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
510
511 // flags is an ORed set of audio_input_flags_t which is updated on return.
512 void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
513
514 // Is this EffectChain compatible with the RAW audio flag.
515 bool isRawCompatible() const;
516
517 // Is this EffectChain compatible with the FAST audio flag.
518 bool isFastCompatible() const;
Eric Laurent4c415062016-06-17 16:14:16 -0700519
520 // isCompatibleWithThread_l() must be called with thread->mLock held
521 bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
522
jiabineb3bda02020-06-30 14:07:03 -0700523 bool containsHapticGeneratingEffect_l();
524
jiabine70bc7f2020-06-30 22:07:55 -0700525 void setHapticIntensity_l(int id, int intensity);
526
Eric Laurent6b446ce2019-12-13 10:56:31 -0800527 sp<EffectCallbackInterface> effectCallback() const { return mEffectCallback; }
528 wp<ThreadBase> thread() const { return mEffectCallback->thread(); }
529
Eric Laurentf1f22e72021-07-13 14:04:14 +0200530 bool isFirstEffect(int id) const { return !mEffects.isEmpty() && id == mEffects[0]->id(); }
531
Eric Laurent81784c32012-11-19 14:55:58 -0800532 void dump(int fd, const Vector<String16>& args);
533
Mikhail Naganovbf493082017-04-17 17:37:12 -0700534private:
Eric Laurent6b446ce2019-12-13 10:56:31 -0800535
Andy Hung328d6772021-01-12 12:32:21 -0800536 // For transaction consistency, please consider holding the EffectChain lock before
537 // calling the EffectChain::EffectCallback methods, excepting
538 // createEffectHal and allocateHalBuffer.
539 //
540 // This prevents migration of the EffectChain to another PlaybackThread
541 // for the purposes of the EffectCallback.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800542 class EffectCallback : public EffectCallbackInterface {
543 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800544 // Note: ctors taking a weak pointer to their owner must not promote it
545 // during construction (but may keep a reference for later promotion).
546 EffectCallback(const wp<EffectChain>& owner,
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800547 const wp<ThreadBase>& thread)
Andy Hung6626a012021-01-12 13:38:00 -0800548 : mChain(owner)
549 , mThread(thread)
550 , mAudioFlinger(*gAudioFlinger) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800551 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800552
553 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
554 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
555 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
Eric Laurent41709552019-12-16 19:34:05 -0800556 bool updateOrphanEffectChains(const sp<EffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800557
558 audio_io_handle_t io() const override;
559 bool isOutput() const override;
560 bool isOffload() const override;
561 bool isOffloadOrDirect() const override;
562 bool isOffloadOrMmap() const override;
563
564 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200565 audio_channel_mask_t inChannelMask(int id) const override;
566 uint32_t inChannelCount(int id) const override;
567 audio_channel_mask_t outChannelMask() const override;
568 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700569 audio_channel_mask_t hapticChannelMask() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800570 size_t frameCount() const override;
571 uint32_t latency() const override;
572
573 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
574 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
575 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
576 void setVolumeForOutput(float left, float right) const override;
577
578 // check if effects should be suspended/restored when a given effect is enable/disabled
Eric Laurent41709552019-12-16 19:34:05 -0800579 void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800580 bool enabled, bool threadLocked) override;
581 void resetVolume() override;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800582 product_strategy_t strategy() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800583 int32_t activeTrackCnt() const override;
Eric Laurent41709552019-12-16 19:34:05 -0800584 void onEffectEnable(const sp<EffectBase>& effect) override;
585 void onEffectDisable(const sp<EffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800586
587 wp<EffectChain> chain() const override { return mChain; }
588
Eric Laurentd66d7a12021-07-13 13:35:32 +0200589 bool isAudioPolicyReady() const override {
590 return mAudioFlinger.isAudioPolicyReady();
591 }
592
Andy Hung328d6772021-01-12 12:32:21 -0800593 wp<ThreadBase> thread() const { return mThread.load(); }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800594
595 void setThread(const wp<ThreadBase>& thread) {
596 mThread = thread;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800597 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800598
599 private:
Andy Hung318e0242020-12-21 10:33:49 -0800600 const wp<EffectChain> mChain;
Andy Hung328d6772021-01-12 12:32:21 -0800601 mediautils::atomic_wp<ThreadBase> mThread;
Andy Hung6626a012021-01-12 13:38:00 -0800602 AudioFlinger &mAudioFlinger; // implementation detail: outer instance always exists.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800603 };
604
Eric Laurent81784c32012-11-19 14:55:58 -0800605 friend class AudioFlinger; // for mThread, mEffects
Mikhail Naganovbf493082017-04-17 17:37:12 -0700606 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800607
608 class SuspendedEffectDesc : public RefBase {
609 public:
610 SuspendedEffectDesc() : mRefCount(0) {}
611
Eric Laurentd8365c52017-07-16 15:27:05 -0700612 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800613 effect_uuid_t mType;
614 wp<EffectModule> mEffect;
615 };
616
617 // get a list of effect modules to suspend when an effect of the type
618 // passed is enabled.
619 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
620
621 // get an effect module if it is currently enable
622 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
623 // true if the effect whose descriptor is passed can be suspended
624 // OEMs can modify the rules implemented in this method to exclude specific effect
625 // types or implementations from the suspend/restore mechanism.
626 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
627
Eric Laurentd8365c52017-07-16 15:27:05 -0700628 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
629
Eric Laurent6b446ce2019-12-13 10:56:31 -0800630 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800631
Eric Laurentaaa44472014-09-12 17:41:50 -0700632 void setThread(const sp<ThreadBase>& thread);
633
Zhou Songd505c642020-02-20 16:35:37 +0800634 // true if any effect module within the chain has volume control
635 bool hasVolumeControlEnabled_l() const;
636
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900637 void setVolumeForOutput_l(uint32_t left, uint32_t right);
638
Eric Laurent4c415062016-06-17 16:14:16 -0700639 mutable Mutex mLock; // mutex protecting effect list
640 Vector< sp<EffectModule> > mEffects; // list of effect modules
641 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800642 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
643 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800644
645 // 'volatile' here means these are accessed with atomic operations instead of mutex
646 volatile int32_t mActiveTrackCnt; // number of active tracks connected
647 volatile int32_t mTrackCnt; // number of tracks connected
648
Eric Laurent4c415062016-06-17 16:14:16 -0700649 int32_t mTailBufferCount; // current effect tail buffer count
650 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700651 int mVolumeCtrlIdx; // index of insert effect having control over volume
652 uint32_t mLeftVolume; // previous volume on left channel
653 uint32_t mRightVolume; // previous volume on right channel
654 uint32_t mNewLeftVolume; // new volume on left channel
655 uint32_t mNewRightVolume; // new volume on right channel
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800656 product_strategy_t mStrategy; // strategy for this effect chain
Eric Laurent4c415062016-06-17 16:14:16 -0700657 // mSuspendedEffects lists all effects currently suspended in the chain.
658 // Use effect type UUID timelow field as key. There is no real risk of identical
659 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700660 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700661 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800662
663 const sp<EffectCallback> mEffectCallback;
Eric Laurent81784c32012-11-19 14:55:58 -0800664};
Eric Laurentb82e6b72019-11-22 17:25:04 -0800665
666class DeviceEffectProxy : public EffectBase {
667public:
668 DeviceEffectProxy (const AudioDeviceTypeAddr& device,
669 const sp<DeviceEffectManagerCallback>& callback,
670 effect_descriptor_t *desc, int id)
671 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
672 mDevice(device), mManagerCallback(callback),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800673 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this),
674 callback)) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800675
676 status_t setEnabled(bool enabled, bool fromHandle) override;
677 sp<DeviceEffectProxy> asDeviceEffectProxy() override { return this; }
678
679 status_t init(const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches);
680 status_t onCreatePatch(audio_patch_handle_t patchHandle, const PatchPanel::Patch& patch);
681 void onReleasePatch(audio_patch_handle_t patchHandle);
682
683 size_t removeEffect(const sp<EffectModule>& effect);
684
685 status_t addEffectToHal(sp<EffectHalInterface> effect);
686 status_t removeEffectFromHal(sp<EffectHalInterface> effect);
687
688 const AudioDeviceTypeAddr& device() { return mDevice; };
689 bool isOutput() const;
690 uint32_t sampleRate() const;
691 audio_channel_mask_t channelMask() const;
692 uint32_t channelCount() const;
693
694 void dump(int fd, int spaces);
695
696private:
697
698 class ProxyCallback : public EffectCallbackInterface {
699 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800700 // Note: ctors taking a weak pointer to their owner must not promote it
701 // during construction (but may keep a reference for later promotion).
702 ProxyCallback(const wp<DeviceEffectProxy>& owner,
703 const sp<DeviceEffectManagerCallback>& callback)
704 : mProxy(owner), mManagerCallback(callback) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800705
706 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
707 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
708 status_t allocateHalBuffer(size_t size __unused,
709 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
710 bool updateOrphanEffectChains(const sp<EffectBase>& effect __unused) override {
711 return false;
712 }
713
714 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
715 bool isOutput() const override;
716 bool isOffload() const override { return false; }
717 bool isOffloadOrDirect() const override { return false; }
718 bool isOffloadOrMmap() const override { return false; }
719
720 uint32_t sampleRate() const override;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200721 audio_channel_mask_t inChannelMask(int id) const override;
722 uint32_t inChannelCount(int id) const override;
723 audio_channel_mask_t outChannelMask() const override;
724 uint32_t outChannelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700725 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800726 size_t frameCount() const override { return 0; }
727 uint32_t latency() const override { return 0; }
728
729 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
730 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
731
732 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
733 void setVolumeForOutput(float left __unused, float right __unused) const override {}
734
735 void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect __unused,
736 bool enabled __unused, bool threadLocked __unused) override {}
737 void resetVolume() override {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800738 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800739 int32_t activeTrackCnt() const override { return 0; }
740 void onEffectEnable(const sp<EffectBase>& effect __unused) override {}
741 void onEffectDisable(const sp<EffectBase>& effect __unused) override {}
742
743 wp<EffectChain> chain() const override { return nullptr; }
744
Eric Laurentd66d7a12021-07-13 13:35:32 +0200745 bool isAudioPolicyReady() const override {
746 return mManagerCallback->isAudioPolicyReady();
747 }
748
Eric Laurentb82e6b72019-11-22 17:25:04 -0800749 int newEffectId();
750
751 private:
752 const wp<DeviceEffectProxy> mProxy;
753 const sp<DeviceEffectManagerCallback> mManagerCallback;
754 };
755
756 status_t checkPort(const PatchPanel::Patch& patch, const struct audio_port_config *port,
757 sp<EffectHandle> *handle);
758
759 const AudioDeviceTypeAddr mDevice;
760 const sp<DeviceEffectManagerCallback> mManagerCallback;
761 const sp<ProxyCallback> mMyCallback;
762
763 Mutex mProxyLock;
764 std::map<audio_patch_handle_t, sp<EffectHandle>> mEffectHandles; // protected by mProxyLock
765 sp<EffectModule> mHalEffect; // protected by mProxyLock
766 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
767};