blob: 0b0f9f51cbcbe97adcb5478b4cab548ad7ac00a6 [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;
36 virtual uint32_t sampleRate() const = 0;
37 virtual audio_channel_mask_t channelMask() const = 0;
38 virtual uint32_t channelCount() const = 0;
39 virtual size_t frameCount() const = 0;
40
41 // Non trivial methods usually implemented with help from ThreadBase:
42 // pay attention to mutex locking order
43 virtual uint32_t latency() const { return 0; }
44 virtual status_t addEffectToHal(sp<EffectHalInterface> effect) = 0;
45 virtual status_t removeEffectFromHal(sp<EffectHalInterface> effect) = 0;
46 virtual void setVolumeForOutput(float left, float right) const = 0;
47 virtual bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) = 0;
48 virtual void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
49 bool enabled,
50 bool threadLocked) = 0;
51 virtual void onEffectEnable(const sp<EffectModule>& effect) = 0;
52 virtual void onEffectDisable(const sp<EffectModule>& effect) = 0;
53
54 // Methods usually implemented with help from AudioFlinger: pay attention to mutex locking order
55 virtual status_t createEffectHal(const effect_uuid_t *pEffectUuid,
56 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) = 0;
57 virtual status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) = 0;
58 virtual bool updateOrphanEffectChains(const sp<EffectModule>& effect) = 0;
59
60 // Methods usually implemented with help from EffectChain: pay attention to mutex locking order
61 virtual uint32_t strategy() const = 0;
62 virtual int32_t activeTrackCnt() const = 0;
63 virtual void resetVolume() = 0;
64
65 virtual wp<EffectChain> chain() const = 0;
66};
67
Eric Laurent81784c32012-11-19 14:55:58 -080068// EffectModule and EffectChain classes both have their own mutex to protect
69// state changes or resource modifications. Always respect the following order
70// if multiple mutexes must be acquired to avoid cross deadlock:
71// AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
Eric Laurent3bc859b2016-12-05 11:07:22 -080072// AudioHandle -> ThreadBase -> EffectChain -> EffectModule
Eric Laurent6b446ce2019-12-13 10:56:31 -080073
74// NOTE: When implementing the EffectCallbackInterface, in an EffectChain or other, it is important
75// to pay attention to this locking order as some callback methods can be called from a state where
76// EffectModule and/or EffectChain mutexes are held.
77
Eric Laurenteb3c3372013-09-25 12:25:29 -070078// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
Eric Laurent3bc859b2016-12-05 11:07:22 -080079// startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or
80// Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService
81// methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order.
Eric Laurent81784c32012-11-19 14:55:58 -080082
83// The EffectModule class is a wrapper object controlling the effect engine implementation
84// in the effect library. It prevents concurrent calls to process() and command() functions
85// from different client threads. It keeps a list of EffectHandle objects corresponding
86// to all client applications using this effect and notifies applications of effect state,
87// control or parameter changes. It manages the activation state machine to send appropriate
88// reset, enable, disable commands to effect engine and provide volume
89// ramping when effects are activated/deactivated.
90// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
91// the attached track(s) to accumulate their auxiliary channel.
92class EffectModule : public RefBase {
93public:
Eric Laurent6b446ce2019-12-13 10:56:31 -080094 EffectModule(const sp<EffectCallbackInterface>& chain,
Eric Laurent81784c32012-11-19 14:55:58 -080095 effect_descriptor_t *desc,
96 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080097 audio_session_t sessionId,
98 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -080099 virtual ~EffectModule();
100
101 enum effect_state {
102 IDLE,
103 RESTART,
104 STARTING,
105 ACTIVE,
106 STOPPING,
107 STOPPED,
108 DESTROYED
109 };
110
111 int id() const { return mId; }
112 void process();
Eric Laurentfa1e1232016-08-02 19:01:49 -0700113 bool updateState();
Eric Laurent81784c32012-11-19 14:55:58 -0800114 status_t command(uint32_t cmdCode,
115 uint32_t cmdSize,
116 void *pCmdData,
117 uint32_t *replySize,
118 void *pReplyData);
119
120 void reset_l();
121 status_t configure();
122 status_t init();
123 effect_state state() const {
124 return mState;
125 }
126 uint32_t status() {
127 return mStatus;
128 }
Glenn Kastend848eb42016-03-08 13:42:11 -0800129 audio_session_t sessionId() const {
Eric Laurent81784c32012-11-19 14:55:58 -0800130 return mSessionId;
131 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800132 status_t setEnabled(bool enabled, bool fromHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800133 status_t setEnabled_l(bool enabled);
134 bool isEnabled() const;
135 bool isProcessEnabled() const;
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900136 bool isOffloadedOrDirect() const;
137 bool isVolumeControlEnabled() const;
Eric Laurent81784c32012-11-19 14:55:58 -0800138
Mikhail Naganov022b9952017-01-04 16:36:51 -0800139 void setInBuffer(const sp<EffectBufferHalInterface>& buffer);
140 int16_t *inBuffer() const {
141 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
142 }
143 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer);
144 int16_t *outBuffer() const {
145 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
146 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800147 void setCallback(const sp<EffectCallbackInterface>& callback) { mCallback = callback; }
Eric Laurent81784c32012-11-19 14:55:58 -0800148
149 status_t addHandle(EffectHandle *handle);
Eric Laurentf10c7092016-12-06 17:09:56 -0800150 ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800151 ssize_t removeHandle(EffectHandle *handle);
152 ssize_t removeHandle_l(EffectHandle *handle);
Eric Laurent81784c32012-11-19 14:55:58 -0800153
154 const effect_descriptor_t& desc() const { return mDescriptor; }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800155 sp<EffectCallbackInterface>& callback() { return mCallback; }
Eric Laurent81784c32012-11-19 14:55:58 -0800156
jiabin8f278ee2019-11-11 12:16:27 -0800157 status_t setDevices(const AudioDeviceTypeAddrVector &devices);
158 status_t setInputDevice(const AudioDeviceTypeAddr &device);
Eric Laurent81784c32012-11-19 14:55:58 -0800159 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
160 status_t setMode(audio_mode_t mode);
161 status_t setAudioSource(audio_source_t source);
162 status_t start();
163 status_t stop();
164 void setSuspended(bool suspended);
165 bool suspended() const;
166
167 EffectHandle* controlHandle_l();
168
169 bool isPinned() const { return mPinned; }
170 void unPin() { mPinned = false; }
171 bool purgeHandles();
172 void lock() { mLock.lock(); }
173 void unlock() { mLock.unlock(); }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700174 bool isOffloadable() const
175 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Eric Laurent4c415062016-06-17 16:14:16 -0700176 bool isImplementationSoftware() const
177 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700178 bool isProcessImplemented() const
179 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900180 bool isVolumeControl() const
181 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
182 == EFFECT_FLAG_VOLUME_CTRL; }
Jasmine Cha934ecfb2019-01-23 18:19:14 +0800183 bool isVolumeMonitor() const
184 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
185 == EFFECT_FLAG_VOLUME_MONITOR; }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700186 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
187 bool isOffloaded() const;
Eric Laurent1b928682014-10-02 19:41:47 -0700188 void addEffectToHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800189 void release_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800190
Eric Laurent6c796322019-04-09 14:13:17 -0700191 status_t updatePolicyState();
Eric Laurent6b446ce2019-12-13 10:56:31 -0800192 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked);
Eric Laurent6c796322019-04-09 14:13:17 -0700193
Eric Laurent81784c32012-11-19 14:55:58 -0800194 void dump(int fd, const Vector<String16>& args);
195
Mikhail Naganovbf493082017-04-17 17:37:12 -0700196private:
Eric Laurent81784c32012-11-19 14:55:58 -0800197 friend class AudioFlinger; // for mHandles
198 bool mPinned;
199
200 // Maximum time allocated to effect engines to complete the turn off sequence
201 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
202
Mikhail Naganovbf493082017-04-17 17:37:12 -0700203 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800204
205 status_t start_l();
206 status_t stop_l();
Eric Laurent6b446ce2019-12-13 10:56:31 -0800207 status_t removeEffectFromHal_l();
jiabin8f278ee2019-11-11 12:16:27 -0800208 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Eric Laurent81784c32012-11-19 14:55:58 -0800209
210mutable Mutex mLock; // mutex for process, commands and handles list protection
Eric Laurent6b446ce2019-12-13 10:56:31 -0800211 sp<EffectCallbackInterface> mCallback; // parent effect chain
Eric Laurent81784c32012-11-19 14:55:58 -0800212 const int mId; // this instance unique ID
Glenn Kastend848eb42016-03-08 13:42:11 -0800213 const audio_session_t mSessionId; // audio session ID
Eric Laurent81784c32012-11-19 14:55:58 -0800214 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
215 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700216 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800217 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
218 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800219 status_t mStatus; // initialization status
220 effect_state mState; // current activation state
221 Vector<EffectHandle *> mHandles; // list of client handles
222 // First handle in mHandles has highest priority and controls the effect module
223 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
224 // sending disable command.
225 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
226 bool mSuspended; // effect is suspended: temporarily disabled by framework
Eric Laurent5baf2af2013-09-12 17:37:00 -0700227 bool mOffloaded; // effect is currently offloaded to the audio DSP
rago94a1ee82017-07-21 15:11:02 -0700228
229#ifdef FLOAT_EFFECT_CHAIN
230 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800231 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
232 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800233 uint32_t mInChannelCountRequested;
234 uint32_t mOutChannelCountRequested;
rago94a1ee82017-07-21 15:11:02 -0700235#endif
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900236
237 class AutoLockReentrant {
238 public:
239 AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
240 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
241 {
242 if (mMutex != nullptr) mMutex->lock();
243 }
244 ~AutoLockReentrant() {
245 if (mMutex != nullptr) mMutex->unlock();
246 }
247 private:
248 Mutex * const mMutex;
249 };
250
251 static constexpr pid_t INVALID_PID = (pid_t)-1;
252 // this tid is allowed to call setVolume() without acquiring the mutex.
253 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent6c796322019-04-09 14:13:17 -0700254
255 // Audio policy effect state management
256 // Mutex protecting transactions with audio policy manager as mLock cannot
257 // be held to avoid cross deadlocks with audio policy mutex
258 Mutex mPolicyLock;
259 // Effect is registered in APM or not
260 bool mPolicyRegistered = false;
261 // Effect enabled state communicated to APM. Enabled state corresponds to
262 // state requested by the EffectHandle with control
263 bool mPolicyEnabled = false;
Eric Laurent81784c32012-11-19 14:55:58 -0800264};
265
266// The EffectHandle class implements the IEffect interface. It provides resources
267// to receive parameter updates, keeps track of effect control
268// ownership and state and has a pointer to the EffectModule object it is controlling.
269// There is one EffectHandle object for each application controlling (or using)
270// an effect module.
271// The EffectHandle is obtained by calling AudioFlinger::createEffect().
272class EffectHandle: public android::BnEffect {
273public:
274
275 EffectHandle(const sp<EffectModule>& effect,
276 const sp<AudioFlinger::Client>& client,
277 const sp<IEffectClient>& effectClient,
278 int32_t priority);
279 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800280 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800281
282 // IEffect
283 virtual status_t enable();
284 virtual status_t disable();
285 virtual status_t command(uint32_t cmdCode,
286 uint32_t cmdSize,
287 void *pCmdData,
288 uint32_t *replySize,
289 void *pReplyData);
290 virtual void disconnect();
291private:
292 void disconnect(bool unpinIfLast);
293public:
294 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
295 virtual status_t onTransact(uint32_t code, const Parcel& data,
296 Parcel* reply, uint32_t flags);
297
298
299 // Give or take control of effect module
300 // - hasControl: true if control is given, false if removed
301 // - signal: true client app should be signaled of change, false otherwise
302 // - enabled: state of the effect when control is passed
303 void setControl(bool hasControl, bool signal, bool enabled);
304 void commandExecuted(uint32_t cmdCode,
305 uint32_t cmdSize,
306 void *pCmdData,
307 uint32_t replySize,
308 void *pReplyData);
309 void setEnabled(bool enabled);
310 bool enabled() const { return mEnabled; }
311
312 // Getters
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800313 wp<EffectModule> effect() const { return mEffect; }
314 int id() const {
315 sp<EffectModule> effect = mEffect.promote();
316 if (effect == 0) {
317 return 0;
318 }
319 return effect->id();
320 }
Eric Laurent81784c32012-11-19 14:55:58 -0800321 int priority() const { return mPriority; }
322 bool hasControl() const { return mHasControl; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800323 bool disconnected() const { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800324
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800325 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800326
Mikhail Naganovbf493082017-04-17 17:37:12 -0700327private:
Eric Laurent81784c32012-11-19 14:55:58 -0800328 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
Mikhail Naganovbf493082017-04-17 17:37:12 -0700329 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800330
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800331 Mutex mLock; // protects IEffect method calls
332 wp<EffectModule> mEffect; // pointer to controlled EffectModule
Eric Laurent81784c32012-11-19 14:55:58 -0800333 sp<IEffectClient> mEffectClient; // callback interface for client notifications
334 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
335 sp<IMemory> mCblkMemory; // shared memory for control block
336 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
337 // shared memory
338 uint8_t* mBuffer; // pointer to parameter area in shared memory
339 int mPriority; // client application priority to control the effect
340 bool mHasControl; // true if this handle is controlling the effect
341 bool mEnabled; // cached enable state: needed when the effect is
342 // restored after being suspended
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800343 bool mDisconnected; // Set to true by disconnect()
Eric Laurent81784c32012-11-19 14:55:58 -0800344};
345
346// the EffectChain class represents a group of effects associated to one audio session.
347// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800348// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
349// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800350// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
351// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800352// order corresponding in the effect process order. When attached to a track (session ID !=
353// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800354// it also provide it's own input buffer used by the track as accumulation buffer.
355class EffectChain : public RefBase {
356public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800357 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
358 EffectChain(ThreadBase *thread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800359 virtual ~EffectChain();
360
361 // special key used for an entry in mSuspendedEffects keyed vector
362 // corresponding to a suspend all request.
363 static const int kKeyForSuspendAll = 0;
364
365 // minimum duration during which we force calling effect process when last track on
366 // a session is stopped or removed to allow effect tail to be rendered
367 static const int kProcessTailDurationMs = 1000;
368
369 void process_l();
370
371 void lock() {
372 mLock.lock();
373 }
374 void unlock() {
375 mLock.unlock();
376 }
377
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800378 status_t createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800379 effect_descriptor_t *desc,
380 int id,
381 audio_session_t sessionId,
382 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800383 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800384 status_t addEffect_ll(const sp<EffectModule>& handle);
385 size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
Eric Laurent81784c32012-11-19 14:55:58 -0800386
Glenn Kastend848eb42016-03-08 13:42:11 -0800387 audio_session_t sessionId() const { return mSessionId; }
388 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800389
390 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
391 sp<EffectModule> getEffectFromId_l(int id);
392 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurent6c796322019-04-09 14:13:17 -0700393 std::vector<int> getEffectIds();
Glenn Kastenc56f3422014-03-21 17:53:17 -0700394 // FIXME use float to improve the dynamic range
Eric Laurentfa1e1232016-08-02 19:01:49 -0700395 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
396 void resetVolume_l();
jiabin8f278ee2019-11-11 12:16:27 -0800397 void setDevices_l(const AudioDeviceTypeAddrVector &devices);
398 void setInputDevice_l(const AudioDeviceTypeAddr &device);
Eric Laurent81784c32012-11-19 14:55:58 -0800399 void setMode_l(audio_mode_t mode);
400 void setAudioSource_l(audio_source_t source);
401
Mikhail Naganov022b9952017-01-04 16:36:51 -0800402 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800403 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800404 }
rago94a1ee82017-07-21 15:11:02 -0700405 effect_buffer_t *inBuffer() const {
406 return mInBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800407 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800408 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800409 mOutBuffer = buffer;
410 }
rago94a1ee82017-07-21 15:11:02 -0700411 effect_buffer_t *outBuffer() const {
412 return mOutBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800413 }
414
415 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
416 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
417 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
418
419 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
420 mTailBufferCount = mMaxTailBuffers; }
421 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
422 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
423
424 uint32_t strategy() const { return mStrategy; }
425 void setStrategy(uint32_t strategy)
426 { mStrategy = strategy; }
427
Eric Laurentd8365c52017-07-16 15:27:05 -0700428 // suspend or restore effects of the specified type. The number of suspend requests is counted
429 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800430 void setEffectSuspended_l(const effect_uuid_t *type,
431 bool suspend);
432 // suspend all eligible effects
433 void setEffectSuspendedAll_l(bool suspend);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800434 // check if effects should be suspended or restored when a given effect is enable or disabled
435 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, bool enabled);
Eric Laurent81784c32012-11-19 14:55:58 -0800436
437 void clearInputBuffer();
438
Eric Laurent5baf2af2013-09-12 17:37:00 -0700439 // At least one non offloadable effect in the chain is enabled
440 bool isNonOffloadableEnabled();
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +0900441 bool isNonOffloadableEnabled_l();
Eric Laurent813e2a72013-08-31 12:59:48 -0700442
Eric Laurent1b928682014-10-02 19:41:47 -0700443 void syncHalEffectsState();
444
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700445 // flags is an ORed set of audio_output_flags_t which is updated on return.
446 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
447
448 // flags is an ORed set of audio_input_flags_t which is updated on return.
449 void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
450
451 // Is this EffectChain compatible with the RAW audio flag.
452 bool isRawCompatible() const;
453
454 // Is this EffectChain compatible with the FAST audio flag.
455 bool isFastCompatible() const;
Eric Laurent4c415062016-06-17 16:14:16 -0700456
457 // isCompatibleWithThread_l() must be called with thread->mLock held
458 bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
459
Eric Laurent6b446ce2019-12-13 10:56:31 -0800460 sp<EffectCallbackInterface> effectCallback() const { return mEffectCallback; }
461 wp<ThreadBase> thread() const { return mEffectCallback->thread(); }
462
Eric Laurent81784c32012-11-19 14:55:58 -0800463 void dump(int fd, const Vector<String16>& args);
464
Mikhail Naganovbf493082017-04-17 17:37:12 -0700465private:
Eric Laurent6b446ce2019-12-13 10:56:31 -0800466
467 class EffectCallback : public EffectCallbackInterface {
468 public:
469 EffectCallback(EffectChain *chain, ThreadBase *thread, AudioFlinger *audioFlinger)
470 : mChain(chain), mThread(thread), mAudioFlinger(audioFlinger) {}
471
472 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
473 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
474 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
475 bool updateOrphanEffectChains(const sp<EffectModule>& effect) override;
476
477 audio_io_handle_t io() const override;
478 bool isOutput() const override;
479 bool isOffload() const override;
480 bool isOffloadOrDirect() const override;
481 bool isOffloadOrMmap() const override;
482
483 uint32_t sampleRate() const override;
484 audio_channel_mask_t channelMask() const override;
485 uint32_t channelCount() const override;
486 size_t frameCount() const override;
487 uint32_t latency() const override;
488
489 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
490 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
491 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
492 void setVolumeForOutput(float left, float right) const override;
493
494 // check if effects should be suspended/restored when a given effect is enable/disabled
495 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
496 bool enabled, bool threadLocked) override;
497 void resetVolume() override;
498 uint32_t strategy() const override;
499 int32_t activeTrackCnt() const override;
500 void onEffectEnable(const sp<EffectModule>& effect) override;
501 void onEffectDisable(const sp<EffectModule>& effect) override;
502
503 wp<EffectChain> chain() const override { return mChain; }
504
505 wp<ThreadBase> thread() { return mThread; }
506 void setThread(ThreadBase *thread) { mThread = thread; };
507
508 private:
509 wp<EffectChain> mChain;
510 wp<ThreadBase> mThread;
511 wp<AudioFlinger> mAudioFlinger;
512 };
513
Eric Laurent81784c32012-11-19 14:55:58 -0800514 friend class AudioFlinger; // for mThread, mEffects
Mikhail Naganovbf493082017-04-17 17:37:12 -0700515 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800516
517 class SuspendedEffectDesc : public RefBase {
518 public:
519 SuspendedEffectDesc() : mRefCount(0) {}
520
Eric Laurentd8365c52017-07-16 15:27:05 -0700521 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800522 effect_uuid_t mType;
523 wp<EffectModule> mEffect;
524 };
525
526 // get a list of effect modules to suspend when an effect of the type
527 // passed is enabled.
528 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
529
530 // get an effect module if it is currently enable
531 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
532 // true if the effect whose descriptor is passed can be suspended
533 // OEMs can modify the rules implemented in this method to exclude specific effect
534 // types or implementations from the suspend/restore mechanism.
535 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
536
Eric Laurentd8365c52017-07-16 15:27:05 -0700537 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
538
Eric Laurent6b446ce2019-12-13 10:56:31 -0800539 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800540
Eric Laurentaaa44472014-09-12 17:41:50 -0700541 void setThread(const sp<ThreadBase>& thread);
542
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900543 void setVolumeForOutput_l(uint32_t left, uint32_t right);
544
Eric Laurent4c415062016-06-17 16:14:16 -0700545 mutable Mutex mLock; // mutex protecting effect list
546 Vector< sp<EffectModule> > mEffects; // list of effect modules
547 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800548 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
549 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800550
551 // 'volatile' here means these are accessed with atomic operations instead of mutex
552 volatile int32_t mActiveTrackCnt; // number of active tracks connected
553 volatile int32_t mTrackCnt; // number of tracks connected
554
Eric Laurent4c415062016-06-17 16:14:16 -0700555 int32_t mTailBufferCount; // current effect tail buffer count
556 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700557 int mVolumeCtrlIdx; // index of insert effect having control over volume
558 uint32_t mLeftVolume; // previous volume on left channel
559 uint32_t mRightVolume; // previous volume on right channel
560 uint32_t mNewLeftVolume; // new volume on left channel
561 uint32_t mNewRightVolume; // new volume on right channel
562 uint32_t mStrategy; // strategy for this effect chain
563 // mSuspendedEffects lists all effects currently suspended in the chain.
564 // Use effect type UUID timelow field as key. There is no real risk of identical
565 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700566 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700567 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800568
569 const sp<EffectCallback> mEffectCallback;
Eric Laurent81784c32012-11-19 14:55:58 -0800570};