blob: 8095f52f3073b9448b0b775886c000814b6b95da [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 Laurentc0abc622019-11-15 17:53:33 -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
31 // Trivial methods usually implemented with help from ThreadBase
32 virtual audio_io_handle_t io() const = 0;
33 virtual bool isOutput() const = 0;
34 virtual bool isOffload() const = 0;
35 virtual bool isOffloadOrDirect() const = 0;
36 virtual bool isOffloadOrMmap() const = 0;
37 virtual uint32_t sampleRate() const = 0;
38 virtual audio_channel_mask_t channelMask() const = 0;
39 virtual uint32_t channelCount() const = 0;
40 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;
48 virtual bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) = 0;
49 virtual void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
50 bool enabled,
51 bool threadLocked) = 0;
52 virtual void onEffectEnable(const sp<EffectModule>& effect) = 0;
53 virtual void onEffectDisable(const sp<EffectModule>& effect) = 0;
54
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;
59 virtual bool updateOrphanEffectChains(const sp<EffectModule>& effect) = 0;
60
61 // Methods usually implemented with help from EffectChain: pay attention to mutex locking order
62 virtual uint32_t strategy() const = 0;
63 virtual int32_t activeTrackCnt() const = 0;
64 virtual void resetVolume() = 0;
65
66 virtual wp<EffectChain> chain() const = 0;
67};
68
Eric Laurent81784c32012-11-19 14:55:58 -080069// EffectModule and EffectChain classes both have their own mutex to protect
70// state changes or resource modifications. Always respect the following order
71// if multiple mutexes must be acquired to avoid cross deadlock:
72// AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
Eric Laurent3bc859b2016-12-05 11:07:22 -080073// AudioHandle -> ThreadBase -> EffectChain -> EffectModule
Eric Laurentc0abc622019-11-15 17:53:33 -080074
75// NOTE: When implementing the EffectCallbackInterface, in an EffectChain or other, it is important
76// to pay attention to this locking order as some callback methods can be called from a state where
77// EffectModule and/or EffectChain mutexes are held.
78
Eric Laurenteb3c3372013-09-25 12:25:29 -070079// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
Eric Laurent3bc859b2016-12-05 11:07:22 -080080// startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or
81// Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService
82// methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order.
Eric Laurent81784c32012-11-19 14:55:58 -080083
84// The EffectModule class is a wrapper object controlling the effect engine implementation
85// in the effect library. It prevents concurrent calls to process() and command() functions
86// from different client threads. It keeps a list of EffectHandle objects corresponding
87// to all client applications using this effect and notifies applications of effect state,
88// control or parameter changes. It manages the activation state machine to send appropriate
89// reset, enable, disable commands to effect engine and provide volume
90// ramping when effects are activated/deactivated.
91// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
92// the attached track(s) to accumulate their auxiliary channel.
93class EffectModule : public RefBase {
94public:
Eric Laurentc0abc622019-11-15 17:53:33 -080095 EffectModule(const sp<EffectCallbackInterface>& chain,
Eric Laurent81784c32012-11-19 14:55:58 -080096 effect_descriptor_t *desc,
97 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080098 audio_session_t sessionId,
99 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800100 virtual ~EffectModule();
101
102 enum effect_state {
103 IDLE,
104 RESTART,
105 STARTING,
106 ACTIVE,
107 STOPPING,
108 STOPPED,
109 DESTROYED
110 };
111
112 int id() const { return mId; }
113 void process();
Eric Laurentfa1e1232016-08-02 19:01:49 -0700114 bool updateState();
Eric Laurent81784c32012-11-19 14:55:58 -0800115 status_t command(uint32_t cmdCode,
116 uint32_t cmdSize,
117 void *pCmdData,
118 uint32_t *replySize,
119 void *pReplyData);
120
121 void reset_l();
122 status_t configure();
123 status_t init();
124 effect_state state() const {
125 return mState;
126 }
127 uint32_t status() {
128 return mStatus;
129 }
Glenn Kastend848eb42016-03-08 13:42:11 -0800130 audio_session_t sessionId() const {
Eric Laurent81784c32012-11-19 14:55:58 -0800131 return mSessionId;
132 }
Eric Laurentc0abc622019-11-15 17:53:33 -0800133 status_t setEnabled(bool enabled, bool fromHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800134 status_t setEnabled_l(bool enabled);
135 bool isEnabled() const;
136 bool isProcessEnabled() const;
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900137 bool isOffloadedOrDirect() const;
138 bool isVolumeControlEnabled() const;
Eric Laurent81784c32012-11-19 14:55:58 -0800139
Mikhail Naganov022b9952017-01-04 16:36:51 -0800140 void setInBuffer(const sp<EffectBufferHalInterface>& buffer);
141 int16_t *inBuffer() const {
142 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
143 }
144 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer);
145 int16_t *outBuffer() const {
146 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
147 }
Eric Laurentc0abc622019-11-15 17:53:33 -0800148 void setCallback(const sp<EffectCallbackInterface>& callback) { mCallback = callback; }
Eric Laurent81784c32012-11-19 14:55:58 -0800149
150 status_t addHandle(EffectHandle *handle);
Eric Laurentf10c7092016-12-06 17:09:56 -0800151 ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800152 ssize_t removeHandle(EffectHandle *handle);
153 ssize_t removeHandle_l(EffectHandle *handle);
Eric Laurent81784c32012-11-19 14:55:58 -0800154
155 const effect_descriptor_t& desc() const { return mDescriptor; }
Eric Laurentc0abc622019-11-15 17:53:33 -0800156 sp<EffectCallbackInterface>& callback() { return mCallback; }
Eric Laurent81784c32012-11-19 14:55:58 -0800157
jiabin8f278ee2019-11-11 12:16:27 -0800158 status_t setDevices(const AudioDeviceTypeAddrVector &devices);
159 status_t setInputDevice(const AudioDeviceTypeAddr &device);
Eric Laurent81784c32012-11-19 14:55:58 -0800160 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
161 status_t setMode(audio_mode_t mode);
162 status_t setAudioSource(audio_source_t source);
163 status_t start();
164 status_t stop();
165 void setSuspended(bool suspended);
166 bool suspended() const;
167
168 EffectHandle* controlHandle_l();
169
170 bool isPinned() const { return mPinned; }
171 void unPin() { mPinned = false; }
172 bool purgeHandles();
173 void lock() { mLock.lock(); }
174 void unlock() { mLock.unlock(); }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700175 bool isOffloadable() const
176 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Eric Laurent4c415062016-06-17 16:14:16 -0700177 bool isImplementationSoftware() const
178 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700179 bool isProcessImplemented() const
180 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900181 bool isVolumeControl() const
182 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
183 == EFFECT_FLAG_VOLUME_CTRL; }
Jasmine Cha934ecfb2019-01-23 18:19:14 +0800184 bool isVolumeMonitor() const
185 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
186 == EFFECT_FLAG_VOLUME_MONITOR; }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700187 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
188 bool isOffloaded() const;
Eric Laurent1b928682014-10-02 19:41:47 -0700189 void addEffectToHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800190 void release_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800191
Eric Laurent6c796322019-04-09 14:13:17 -0700192 status_t updatePolicyState();
Eric Laurentc0abc622019-11-15 17:53:33 -0800193 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked);
Eric Laurent6c796322019-04-09 14:13:17 -0700194
Eric Laurent81784c32012-11-19 14:55:58 -0800195 void dump(int fd, const Vector<String16>& args);
196
Mikhail Naganovbf493082017-04-17 17:37:12 -0700197private:
Eric Laurent81784c32012-11-19 14:55:58 -0800198 friend class AudioFlinger; // for mHandles
199 bool mPinned;
200
201 // Maximum time allocated to effect engines to complete the turn off sequence
202 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
203
Mikhail Naganovbf493082017-04-17 17:37:12 -0700204 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800205
206 status_t start_l();
207 status_t stop_l();
Eric Laurentc0abc622019-11-15 17:53:33 -0800208 status_t removeEffectFromHal_l();
jiabin8f278ee2019-11-11 12:16:27 -0800209 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Eric Laurent81784c32012-11-19 14:55:58 -0800210
211mutable Mutex mLock; // mutex for process, commands and handles list protection
Eric Laurentc0abc622019-11-15 17:53:33 -0800212 sp<EffectCallbackInterface> mCallback; // parent effect chain
Eric Laurent81784c32012-11-19 14:55:58 -0800213 const int mId; // this instance unique ID
Glenn Kastend848eb42016-03-08 13:42:11 -0800214 const audio_session_t mSessionId; // audio session ID
Eric Laurent81784c32012-11-19 14:55:58 -0800215 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
216 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700217 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800218 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
219 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800220 status_t mStatus; // initialization status
221 effect_state mState; // current activation state
222 Vector<EffectHandle *> mHandles; // list of client handles
223 // First handle in mHandles has highest priority and controls the effect module
224 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
225 // sending disable command.
226 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
227 bool mSuspended; // effect is suspended: temporarily disabled by framework
Eric Laurent5baf2af2013-09-12 17:37:00 -0700228 bool mOffloaded; // effect is currently offloaded to the audio DSP
rago94a1ee82017-07-21 15:11:02 -0700229
230#ifdef FLOAT_EFFECT_CHAIN
231 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800232 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
233 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800234 uint32_t mInChannelCountRequested;
235 uint32_t mOutChannelCountRequested;
rago94a1ee82017-07-21 15:11:02 -0700236#endif
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900237
238 class AutoLockReentrant {
239 public:
240 AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
241 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
242 {
243 if (mMutex != nullptr) mMutex->lock();
244 }
245 ~AutoLockReentrant() {
246 if (mMutex != nullptr) mMutex->unlock();
247 }
248 private:
249 Mutex * const mMutex;
250 };
251
252 static constexpr pid_t INVALID_PID = (pid_t)-1;
253 // this tid is allowed to call setVolume() without acquiring the mutex.
254 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent6c796322019-04-09 14:13:17 -0700255
256 // Audio policy effect state management
257 // Mutex protecting transactions with audio policy manager as mLock cannot
258 // be held to avoid cross deadlocks with audio policy mutex
259 Mutex mPolicyLock;
260 // Effect is registered in APM or not
261 bool mPolicyRegistered = false;
262 // Effect enabled state communicated to APM. Enabled state corresponds to
263 // state requested by the EffectHandle with control
264 bool mPolicyEnabled = false;
Eric Laurent81784c32012-11-19 14:55:58 -0800265};
266
267// The EffectHandle class implements the IEffect interface. It provides resources
268// to receive parameter updates, keeps track of effect control
269// ownership and state and has a pointer to the EffectModule object it is controlling.
270// There is one EffectHandle object for each application controlling (or using)
271// an effect module.
272// The EffectHandle is obtained by calling AudioFlinger::createEffect().
273class EffectHandle: public android::BnEffect {
274public:
275
276 EffectHandle(const sp<EffectModule>& effect,
277 const sp<AudioFlinger::Client>& client,
278 const sp<IEffectClient>& effectClient,
279 int32_t priority);
280 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800281 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800282
283 // IEffect
284 virtual status_t enable();
285 virtual status_t disable();
286 virtual status_t command(uint32_t cmdCode,
287 uint32_t cmdSize,
288 void *pCmdData,
289 uint32_t *replySize,
290 void *pReplyData);
291 virtual void disconnect();
292private:
293 void disconnect(bool unpinIfLast);
294public:
295 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
296 virtual status_t onTransact(uint32_t code, const Parcel& data,
297 Parcel* reply, uint32_t flags);
298
299
300 // Give or take control of effect module
301 // - hasControl: true if control is given, false if removed
302 // - signal: true client app should be signaled of change, false otherwise
303 // - enabled: state of the effect when control is passed
304 void setControl(bool hasControl, bool signal, bool enabled);
305 void commandExecuted(uint32_t cmdCode,
306 uint32_t cmdSize,
307 void *pCmdData,
308 uint32_t replySize,
309 void *pReplyData);
310 void setEnabled(bool enabled);
311 bool enabled() const { return mEnabled; }
312
313 // Getters
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800314 wp<EffectModule> effect() const { return mEffect; }
315 int id() const {
316 sp<EffectModule> effect = mEffect.promote();
317 if (effect == 0) {
318 return 0;
319 }
320 return effect->id();
321 }
Eric Laurent81784c32012-11-19 14:55:58 -0800322 int priority() const { return mPriority; }
323 bool hasControl() const { return mHasControl; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800324 bool disconnected() const { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800325
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800326 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800327
Mikhail Naganovbf493082017-04-17 17:37:12 -0700328private:
Eric Laurent81784c32012-11-19 14:55:58 -0800329 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
Mikhail Naganovbf493082017-04-17 17:37:12 -0700330 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800331
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800332 Mutex mLock; // protects IEffect method calls
333 wp<EffectModule> mEffect; // pointer to controlled EffectModule
Eric Laurent81784c32012-11-19 14:55:58 -0800334 sp<IEffectClient> mEffectClient; // callback interface for client notifications
335 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
336 sp<IMemory> mCblkMemory; // shared memory for control block
337 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
338 // shared memory
339 uint8_t* mBuffer; // pointer to parameter area in shared memory
340 int mPriority; // client application priority to control the effect
341 bool mHasControl; // true if this handle is controlling the effect
342 bool mEnabled; // cached enable state: needed when the effect is
343 // restored after being suspended
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800344 bool mDisconnected; // Set to true by disconnect()
Eric Laurent81784c32012-11-19 14:55:58 -0800345};
346
347// the EffectChain class represents a group of effects associated to one audio session.
348// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800349// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
350// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800351// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
352// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800353// order corresponding in the effect process order. When attached to a track (session ID !=
354// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800355// it also provide it's own input buffer used by the track as accumulation buffer.
356class EffectChain : public RefBase {
357public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800358 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
359 EffectChain(ThreadBase *thread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800360 virtual ~EffectChain();
361
362 // special key used for an entry in mSuspendedEffects keyed vector
363 // corresponding to a suspend all request.
364 static const int kKeyForSuspendAll = 0;
365
366 // minimum duration during which we force calling effect process when last track on
367 // a session is stopped or removed to allow effect tail to be rendered
368 static const int kProcessTailDurationMs = 1000;
369
370 void process_l();
371
372 void lock() {
373 mLock.lock();
374 }
375 void unlock() {
376 mLock.unlock();
377 }
378
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800379 status_t createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800380 effect_descriptor_t *desc,
381 int id,
382 audio_session_t sessionId,
383 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800384 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800385 status_t addEffect_ll(const sp<EffectModule>& handle);
386 size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
Eric Laurent81784c32012-11-19 14:55:58 -0800387
Glenn Kastend848eb42016-03-08 13:42:11 -0800388 audio_session_t sessionId() const { return mSessionId; }
389 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800390
391 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
392 sp<EffectModule> getEffectFromId_l(int id);
393 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurent6c796322019-04-09 14:13:17 -0700394 std::vector<int> getEffectIds();
Glenn Kastenc56f3422014-03-21 17:53:17 -0700395 // FIXME use float to improve the dynamic range
Eric Laurentfa1e1232016-08-02 19:01:49 -0700396 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
397 void resetVolume_l();
jiabin8f278ee2019-11-11 12:16:27 -0800398 void setDevices_l(const AudioDeviceTypeAddrVector &devices);
399 void setInputDevice_l(const AudioDeviceTypeAddr &device);
Eric Laurent81784c32012-11-19 14:55:58 -0800400 void setMode_l(audio_mode_t mode);
401 void setAudioSource_l(audio_source_t source);
402
Mikhail Naganov022b9952017-01-04 16:36:51 -0800403 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800404 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800405 }
rago94a1ee82017-07-21 15:11:02 -0700406 effect_buffer_t *inBuffer() const {
407 return mInBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800408 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800409 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800410 mOutBuffer = buffer;
411 }
rago94a1ee82017-07-21 15:11:02 -0700412 effect_buffer_t *outBuffer() const {
413 return mOutBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800414 }
415
416 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
417 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
418 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
419
420 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
421 mTailBufferCount = mMaxTailBuffers; }
422 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
423 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
424
425 uint32_t strategy() const { return mStrategy; }
426 void setStrategy(uint32_t strategy)
427 { mStrategy = strategy; }
428
Eric Laurentd8365c52017-07-16 15:27:05 -0700429 // suspend or restore effects of the specified type. The number of suspend requests is counted
430 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800431 void setEffectSuspended_l(const effect_uuid_t *type,
432 bool suspend);
433 // suspend all eligible effects
434 void setEffectSuspendedAll_l(bool suspend);
Eric Laurentc0abc622019-11-15 17:53:33 -0800435 // check if effects should be suspended or restored when a given effect is enable or disabled
436 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, bool enabled);
Eric Laurent81784c32012-11-19 14:55:58 -0800437
438 void clearInputBuffer();
439
Eric Laurent5baf2af2013-09-12 17:37:00 -0700440 // At least one non offloadable effect in the chain is enabled
441 bool isNonOffloadableEnabled();
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +0900442 bool isNonOffloadableEnabled_l();
Eric Laurent813e2a72013-08-31 12:59:48 -0700443
Eric Laurent1b928682014-10-02 19:41:47 -0700444 void syncHalEffectsState();
445
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700446 // flags is an ORed set of audio_output_flags_t which is updated on return.
447 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
448
449 // flags is an ORed set of audio_input_flags_t which is updated on return.
450 void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
451
452 // Is this EffectChain compatible with the RAW audio flag.
453 bool isRawCompatible() const;
454
455 // Is this EffectChain compatible with the FAST audio flag.
456 bool isFastCompatible() const;
Eric Laurent4c415062016-06-17 16:14:16 -0700457
458 // isCompatibleWithThread_l() must be called with thread->mLock held
459 bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
460
Eric Laurentc0abc622019-11-15 17:53:33 -0800461 sp<EffectCallbackInterface> effectCallback() const { return mEffectCallback; }
462 wp<ThreadBase> thread() const { return mEffectCallback->thread(); }
463
Eric Laurent81784c32012-11-19 14:55:58 -0800464 void dump(int fd, const Vector<String16>& args);
465
Mikhail Naganovbf493082017-04-17 17:37:12 -0700466private:
Eric Laurentc0abc622019-11-15 17:53:33 -0800467
468 class EffectCallback : public EffectCallbackInterface {
469 public:
470 EffectCallback(EffectChain *chain, ThreadBase *thread, AudioFlinger *audioFlinger)
471 : mChain(chain), mThread(thread), mAudioFlinger(audioFlinger) {}
472
473 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
474 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
475 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
476 bool updateOrphanEffectChains(const sp<EffectModule>& effect) override;
477
478 audio_io_handle_t io() const override;
479 bool isOutput() const override;
480 bool isOffload() const override;
481 bool isOffloadOrDirect() const override;
482 bool isOffloadOrMmap() const override;
483
484 uint32_t sampleRate() const override;
485 audio_channel_mask_t channelMask() const override;
486 uint32_t channelCount() const override;
487 size_t frameCount() const override;
488 uint32_t latency() const override;
489
490 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
491 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
492 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
493 void setVolumeForOutput(float left, float right) const override;
494
495 // check if effects should be suspended/restored when a given effect is enable/disabled
496 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
497 bool enabled, bool threadLocked) override;
498 void resetVolume() override;
499 uint32_t strategy() const override;
500 int32_t activeTrackCnt() const override;
501 void onEffectEnable(const sp<EffectModule>& effect) override;
502 void onEffectDisable(const sp<EffectModule>& effect) override;
503
504 wp<EffectChain> chain() const override { return mChain; }
505
506 wp<ThreadBase> thread() { return mThread; }
507 void setThread(ThreadBase *thread) { mThread = thread; };
508
509 private:
510 wp<EffectChain> mChain;
511 wp<ThreadBase> mThread;
512 wp<AudioFlinger> mAudioFlinger;
513 };
514
Eric Laurent81784c32012-11-19 14:55:58 -0800515 friend class AudioFlinger; // for mThread, mEffects
Mikhail Naganovbf493082017-04-17 17:37:12 -0700516 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800517
518 class SuspendedEffectDesc : public RefBase {
519 public:
520 SuspendedEffectDesc() : mRefCount(0) {}
521
Eric Laurentd8365c52017-07-16 15:27:05 -0700522 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800523 effect_uuid_t mType;
524 wp<EffectModule> mEffect;
525 };
526
527 // get a list of effect modules to suspend when an effect of the type
528 // passed is enabled.
529 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
530
531 // get an effect module if it is currently enable
532 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
533 // true if the effect whose descriptor is passed can be suspended
534 // OEMs can modify the rules implemented in this method to exclude specific effect
535 // types or implementations from the suspend/restore mechanism.
536 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
537
Eric Laurentd8365c52017-07-16 15:27:05 -0700538 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
539
Eric Laurentc0abc622019-11-15 17:53:33 -0800540 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800541
Eric Laurentaaa44472014-09-12 17:41:50 -0700542 void setThread(const sp<ThreadBase>& thread);
543
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900544 void setVolumeForOutput_l(uint32_t left, uint32_t right);
545
Eric Laurent4c415062016-06-17 16:14:16 -0700546 mutable Mutex mLock; // mutex protecting effect list
547 Vector< sp<EffectModule> > mEffects; // list of effect modules
548 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800549 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
550 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800551
552 // 'volatile' here means these are accessed with atomic operations instead of mutex
553 volatile int32_t mActiveTrackCnt; // number of active tracks connected
554 volatile int32_t mTrackCnt; // number of tracks connected
555
Eric Laurent4c415062016-06-17 16:14:16 -0700556 int32_t mTailBufferCount; // current effect tail buffer count
557 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700558 int mVolumeCtrlIdx; // index of insert effect having control over volume
559 uint32_t mLeftVolume; // previous volume on left channel
560 uint32_t mRightVolume; // previous volume on right channel
561 uint32_t mNewLeftVolume; // new volume on left channel
562 uint32_t mNewRightVolume; // new volume on right channel
563 uint32_t mStrategy; // strategy for this effect chain
564 // mSuspendedEffects lists all effects currently suspended in the chain.
565 // Use effect type UUID timelow field as key. There is no real risk of identical
566 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700567 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700568 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurentc0abc622019-11-15 17:53:33 -0800569
570 const sp<EffectCallback> mEffectCallback;
Eric Laurent81784c32012-11-19 14:55:58 -0800571};