blob: 901f93b50282f5dcd7f1ccae1f244526c4352a23 [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 Laurent5d885392019-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 Laurent9b2064c2019-11-22 17:25:04 -080036 virtual uint32_t sampleRate() const = 0;
Eric Laurent5d885392019-12-13 10:56:31 -080037 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;
Eric Laurente0b9a362019-12-16 19:34:05 -080048 virtual void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
Eric Laurent5d885392019-12-13 10:56:31 -080049 bool enabled,
50 bool threadLocked) = 0;
Eric Laurente0b9a362019-12-16 19:34:05 -080051 virtual void onEffectEnable(const sp<EffectBase>& effect) = 0;
52 virtual void onEffectDisable(const sp<EffectBase>& effect) = 0;
Eric Laurent5d885392019-12-13 10:56:31 -080053
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;
Eric Laurente0b9a362019-12-16 19:34:05 -080058 virtual bool updateOrphanEffectChains(const sp<EffectBase>& effect) = 0;
Eric Laurent5d885392019-12-13 10:56:31 -080059
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 Laurente0b9a362019-12-16 19:34:05 -080068// EffectBase(EffectModule) and EffectChain classes both have their own mutex to protect
Eric Laurent81784c32012-11-19 14:55:58 -080069// state changes or resource modifications. Always respect the following order
70// if multiple mutexes must be acquired to avoid cross deadlock:
Eric Laurente0b9a362019-12-16 19:34:05 -080071// AudioFlinger -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
72// AudioHandle -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
Eric Laurent5d885392019-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
Eric Laurente0b9a362019-12-16 19:34:05 -080083
84// The EffectBase class contains common properties, state and behavior for and EffectModule or
85// other derived classes managing an audio effect instance within the effect framework.
86// It also contains the class mutex (see comment on locking order above).
87class EffectBase : public RefBase {
Eric Laurent81784c32012-11-19 14:55:58 -080088public:
Eric Laurente0b9a362019-12-16 19:34:05 -080089 EffectBase(const sp<EffectCallbackInterface>& callback,
90 effect_descriptor_t *desc,
91 int id,
92 audio_session_t sessionId,
93 bool pinned);
94
95 ~EffectBase() override = default;
Eric Laurent81784c32012-11-19 14:55:58 -080096
97 enum effect_state {
98 IDLE,
99 RESTART,
100 STARTING,
101 ACTIVE,
102 STOPPING,
103 STOPPED,
104 DESTROYED
105 };
106
Eric Laurente0b9a362019-12-16 19:34:05 -0800107 int id() const { return mId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800108 effect_state state() const {
109 return mState;
110 }
Glenn Kastend848eb42016-03-08 13:42:11 -0800111 audio_session_t sessionId() const {
Eric Laurent81784c32012-11-19 14:55:58 -0800112 return mSessionId;
113 }
Eric Laurent81784c32012-11-19 14:55:58 -0800114 const effect_descriptor_t& desc() const { return mDescriptor; }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700115 bool isOffloadable() const
116 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Eric Laurent4c415062016-06-17 16:14:16 -0700117 bool isImplementationSoftware() const
118 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700119 bool isProcessImplemented() const
120 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900121 bool isVolumeControl() const
122 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
123 == EFFECT_FLAG_VOLUME_CTRL; }
Jasmine Cha934ecfb2019-01-23 18:19:14 +0800124 bool isVolumeMonitor() const
125 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
126 == EFFECT_FLAG_VOLUME_MONITOR; }
Eric Laurente0b9a362019-12-16 19:34:05 -0800127
128 virtual status_t setEnabled(bool enabled, bool fromHandle);
129 status_t setEnabled_l(bool enabled);
130 bool isEnabled() const;
131
132 void setSuspended(bool suspended);
133 bool suspended() const;
134
135 virtual status_t command(uint32_t cmdCode __unused,
136 uint32_t cmdSize __unused,
137 void *pCmdData __unused,
138 uint32_t *replySize __unused,
139 void *pReplyData __unused) { return NO_ERROR; };
140
141 void setCallback(const sp<EffectCallbackInterface>& callback) { mCallback = callback; }
142 sp<EffectCallbackInterface>& callback() { return mCallback; }
143
144 status_t addHandle(EffectHandle *handle);
145 ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
146 ssize_t removeHandle(EffectHandle *handle);
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530147 ssize_t removeHandle_l(EffectHandle *handle);
Eric Laurente0b9a362019-12-16 19:34:05 -0800148 EffectHandle* controlHandle_l();
149 bool purgeHandles();
150
151 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked);
152
153 bool isPinned() const { return mPinned; }
154 void unPin() { mPinned = false; }
155
156 void lock() { mLock.lock(); }
157 void unlock() { mLock.unlock(); }
Eric Laurent81784c32012-11-19 14:55:58 -0800158
Eric Laurent6c796322019-04-09 14:13:17 -0700159 status_t updatePolicyState();
Eric Laurente0b9a362019-12-16 19:34:05 -0800160
161 virtual sp<EffectModule> asEffectModule() { return nullptr; }
Eric Laurent9b2064c2019-11-22 17:25:04 -0800162 virtual sp<DeviceEffectProxy> asDeviceEffectProxy() { return nullptr; }
Eric Laurent6c796322019-04-09 14:13:17 -0700163
Eric Laurent81784c32012-11-19 14:55:58 -0800164 void dump(int fd, const Vector<String16>& args);
165
Mikhail Naganovbf493082017-04-17 17:37:12 -0700166private:
Eric Laurent81784c32012-11-19 14:55:58 -0800167 friend class AudioFlinger; // for mHandles
Eric Laurente0b9a362019-12-16 19:34:05 -0800168 bool mPinned = false;
169
170 DISALLOW_COPY_AND_ASSIGN(EffectBase);
171
172mutable Mutex mLock; // mutex for process, commands and handles list protection
173 sp<EffectCallbackInterface> mCallback; // parent effect chain
174 const int mId; // this instance unique ID
175 const audio_session_t mSessionId; // audio session ID
176 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
177 effect_state mState = IDLE; // current activation state
Eric Laurent3eaa3702019-12-19 09:20:49 -0800178 // effect is suspended: temporarily disabled by framework
179 bool mSuspended = false;
Eric Laurente0b9a362019-12-16 19:34:05 -0800180
181 Vector<EffectHandle *> mHandles; // list of client handles
182 // First handle in mHandles has highest priority and controls the effect module
183
184 // Audio policy effect state management
185 // Mutex protecting transactions with audio policy manager as mLock cannot
186 // be held to avoid cross deadlocks with audio policy mutex
187 Mutex mPolicyLock;
188 // Effect is registered in APM or not
189 bool mPolicyRegistered = false;
190 // Effect enabled state communicated to APM. Enabled state corresponds to
191 // state requested by the EffectHandle with control
192 bool mPolicyEnabled = false;
193};
194
195// The EffectModule class is a wrapper object controlling the effect engine implementation
196// in the effect library. It prevents concurrent calls to process() and command() functions
197// from different client threads. It keeps a list of EffectHandle objects corresponding
198// to all client applications using this effect and notifies applications of effect state,
199// control or parameter changes. It manages the activation state machine to send appropriate
200// reset, enable, disable commands to effect engine and provide volume
201// ramping when effects are activated/deactivated.
202// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
203// the attached track(s) to accumulate their auxiliary channel.
204class EffectModule : public EffectBase {
205public:
206 EffectModule(const sp<EffectCallbackInterface>& callabck,
207 effect_descriptor_t *desc,
208 int id,
209 audio_session_t sessionId,
Eric Laurent9b2064c2019-11-22 17:25:04 -0800210 bool pinned,
211 audio_port_handle_t deviceId);
Eric Laurente0b9a362019-12-16 19:34:05 -0800212 virtual ~EffectModule();
213
214 void process();
215 bool updateState();
216 status_t command(uint32_t cmdCode,
217 uint32_t cmdSize,
218 void *pCmdData,
219 uint32_t *replySize,
220 void *pReplyData) override;
221
222 void reset_l();
223 status_t configure();
224 status_t init();
225
226 uint32_t status() {
227 return mStatus;
228 }
229
230 bool isProcessEnabled() const;
231 bool isOffloadedOrDirect() const;
232 bool isVolumeControlEnabled() const;
233
234 void setInBuffer(const sp<EffectBufferHalInterface>& buffer);
235 int16_t *inBuffer() const {
236 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
237 }
238 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer);
239 int16_t *outBuffer() const {
240 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
241 }
242
Eric Laurente0b9a362019-12-16 19:34:05 -0800243 status_t setDevices(const AudioDeviceTypeAddrVector &devices);
244 status_t setInputDevice(const AudioDeviceTypeAddr &device);
245 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
246 status_t setMode(audio_mode_t mode);
247 status_t setAudioSource(audio_source_t source);
248 status_t start();
249 status_t stop();
250
251 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
252 bool isOffloaded() const;
253 void addEffectToHal_l();
254 void release_l();
255
256 sp<EffectModule> asEffectModule() override { return this; }
257
258 void dump(int fd, const Vector<String16>& args);
259
260private:
261 friend class AudioFlinger; // for mHandles
Eric Laurent81784c32012-11-19 14:55:58 -0800262
263 // Maximum time allocated to effect engines to complete the turn off sequence
264 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
265
Mikhail Naganovbf493082017-04-17 17:37:12 -0700266 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800267
268 status_t start_l();
269 status_t stop_l();
Eric Laurent5d885392019-12-13 10:56:31 -0800270 status_t removeEffectFromHal_l();
jiabinb8269fd2019-11-11 12:16:27 -0800271 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Eric Laurent81784c32012-11-19 14:55:58 -0800272
Eric Laurent81784c32012-11-19 14:55:58 -0800273 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700274 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800275 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
276 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800277 status_t mStatus; // initialization status
Eric Laurent81784c32012-11-19 14:55:58 -0800278 // First handle in mHandles has highest priority and controls the effect module
279 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
280 // sending disable command.
281 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent5baf2af2013-09-12 17:37:00 -0700282 bool mOffloaded; // effect is currently offloaded to the audio DSP
rago94a1ee82017-07-21 15:11:02 -0700283
284#ifdef FLOAT_EFFECT_CHAIN
285 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800286 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
287 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800288 uint32_t mInChannelCountRequested;
289 uint32_t mOutChannelCountRequested;
rago94a1ee82017-07-21 15:11:02 -0700290#endif
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900291
292 class AutoLockReentrant {
293 public:
294 AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
295 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
296 {
297 if (mMutex != nullptr) mMutex->lock();
298 }
299 ~AutoLockReentrant() {
300 if (mMutex != nullptr) mMutex->unlock();
301 }
302 private:
303 Mutex * const mMutex;
304 };
305
306 static constexpr pid_t INVALID_PID = (pid_t)-1;
307 // this tid is allowed to call setVolume() without acquiring the mutex.
308 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent81784c32012-11-19 14:55:58 -0800309};
310
311// The EffectHandle class implements the IEffect interface. It provides resources
312// to receive parameter updates, keeps track of effect control
313// ownership and state and has a pointer to the EffectModule object it is controlling.
314// There is one EffectHandle object for each application controlling (or using)
315// an effect module.
316// The EffectHandle is obtained by calling AudioFlinger::createEffect().
317class EffectHandle: public android::BnEffect {
318public:
319
Eric Laurente0b9a362019-12-16 19:34:05 -0800320 EffectHandle(const sp<EffectBase>& effect,
Eric Laurent81784c32012-11-19 14:55:58 -0800321 const sp<AudioFlinger::Client>& client,
322 const sp<IEffectClient>& effectClient,
323 int32_t priority);
324 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800325 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800326
327 // IEffect
328 virtual status_t enable();
329 virtual status_t disable();
330 virtual status_t command(uint32_t cmdCode,
331 uint32_t cmdSize,
332 void *pCmdData,
333 uint32_t *replySize,
334 void *pReplyData);
335 virtual void disconnect();
336private:
337 void disconnect(bool unpinIfLast);
338public:
339 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
340 virtual status_t onTransact(uint32_t code, const Parcel& data,
341 Parcel* reply, uint32_t flags);
342
343
344 // Give or take control of effect module
345 // - hasControl: true if control is given, false if removed
346 // - signal: true client app should be signaled of change, false otherwise
347 // - enabled: state of the effect when control is passed
348 void setControl(bool hasControl, bool signal, bool enabled);
349 void commandExecuted(uint32_t cmdCode,
350 uint32_t cmdSize,
351 void *pCmdData,
352 uint32_t replySize,
353 void *pReplyData);
354 void setEnabled(bool enabled);
355 bool enabled() const { return mEnabled; }
356
357 // Getters
Eric Laurente0b9a362019-12-16 19:34:05 -0800358 wp<EffectBase> effect() const { return mEffect; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800359 int id() const {
Eric Laurente0b9a362019-12-16 19:34:05 -0800360 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800361 if (effect == 0) {
362 return 0;
363 }
364 return effect->id();
365 }
Eric Laurent81784c32012-11-19 14:55:58 -0800366 int priority() const { return mPriority; }
367 bool hasControl() const { return mHasControl; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800368 bool disconnected() const { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800369
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800370 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800371
Mikhail Naganovbf493082017-04-17 17:37:12 -0700372private:
Eric Laurent81784c32012-11-19 14:55:58 -0800373 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
Mikhail Naganovbf493082017-04-17 17:37:12 -0700374 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800375
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800376 Mutex mLock; // protects IEffect method calls
Eric Laurente0b9a362019-12-16 19:34:05 -0800377 wp<EffectBase> mEffect; // pointer to controlled EffectModule
Eric Laurent81784c32012-11-19 14:55:58 -0800378 sp<IEffectClient> mEffectClient; // callback interface for client notifications
379 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
380 sp<IMemory> mCblkMemory; // shared memory for control block
381 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
382 // shared memory
383 uint8_t* mBuffer; // pointer to parameter area in shared memory
384 int mPriority; // client application priority to control the effect
385 bool mHasControl; // true if this handle is controlling the effect
386 bool mEnabled; // cached enable state: needed when the effect is
387 // restored after being suspended
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800388 bool mDisconnected; // Set to true by disconnect()
Eric Laurent81784c32012-11-19 14:55:58 -0800389};
390
391// the EffectChain class represents a group of effects associated to one audio session.
392// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800393// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
394// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800395// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
396// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800397// order corresponding in the effect process order. When attached to a track (session ID !=
398// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800399// it also provide it's own input buffer used by the track as accumulation buffer.
400class EffectChain : public RefBase {
401public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800402 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
403 EffectChain(ThreadBase *thread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800404 virtual ~EffectChain();
405
406 // special key used for an entry in mSuspendedEffects keyed vector
407 // corresponding to a suspend all request.
408 static const int kKeyForSuspendAll = 0;
409
410 // minimum duration during which we force calling effect process when last track on
411 // a session is stopped or removed to allow effect tail to be rendered
412 static const int kProcessTailDurationMs = 1000;
413
414 void process_l();
415
416 void lock() {
417 mLock.lock();
418 }
419 void unlock() {
420 mLock.unlock();
421 }
422
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800423 status_t createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800424 effect_descriptor_t *desc,
425 int id,
426 audio_session_t sessionId,
427 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800428 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800429 status_t addEffect_ll(const sp<EffectModule>& handle);
430 size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
Eric Laurent81784c32012-11-19 14:55:58 -0800431
Glenn Kastend848eb42016-03-08 13:42:11 -0800432 audio_session_t sessionId() const { return mSessionId; }
433 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800434
435 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
436 sp<EffectModule> getEffectFromId_l(int id);
437 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurent6c796322019-04-09 14:13:17 -0700438 std::vector<int> getEffectIds();
Glenn Kastenc56f3422014-03-21 17:53:17 -0700439 // FIXME use float to improve the dynamic range
Eric Laurentfa1e1232016-08-02 19:01:49 -0700440 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
441 void resetVolume_l();
jiabinb8269fd2019-11-11 12:16:27 -0800442 void setDevices_l(const AudioDeviceTypeAddrVector &devices);
443 void setInputDevice_l(const AudioDeviceTypeAddr &device);
Eric Laurent81784c32012-11-19 14:55:58 -0800444 void setMode_l(audio_mode_t mode);
445 void setAudioSource_l(audio_source_t source);
446
Mikhail Naganov022b9952017-01-04 16:36:51 -0800447 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800448 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800449 }
rago94a1ee82017-07-21 15:11:02 -0700450 effect_buffer_t *inBuffer() const {
451 return mInBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800452 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800453 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800454 mOutBuffer = buffer;
455 }
rago94a1ee82017-07-21 15:11:02 -0700456 effect_buffer_t *outBuffer() const {
457 return mOutBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800458 }
459
460 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
461 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
462 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
463
464 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
465 mTailBufferCount = mMaxTailBuffers; }
466 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
467 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
468
469 uint32_t strategy() const { return mStrategy; }
470 void setStrategy(uint32_t strategy)
471 { mStrategy = strategy; }
472
Eric Laurentd8365c52017-07-16 15:27:05 -0700473 // suspend or restore effects of the specified type. The number of suspend requests is counted
474 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800475 void setEffectSuspended_l(const effect_uuid_t *type,
476 bool suspend);
477 // suspend all eligible effects
478 void setEffectSuspendedAll_l(bool suspend);
Eric Laurent5d885392019-12-13 10:56:31 -0800479 // check if effects should be suspended or restored when a given effect is enable or disabled
480 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, bool enabled);
Eric Laurent81784c32012-11-19 14:55:58 -0800481
482 void clearInputBuffer();
483
Eric Laurent5baf2af2013-09-12 17:37:00 -0700484 // At least one non offloadable effect in the chain is enabled
485 bool isNonOffloadableEnabled();
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +0900486 bool isNonOffloadableEnabled_l();
Eric Laurent813e2a72013-08-31 12:59:48 -0700487
Eric Laurent1b928682014-10-02 19:41:47 -0700488 void syncHalEffectsState();
489
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700490 // flags is an ORed set of audio_output_flags_t which is updated on return.
491 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
492
493 // flags is an ORed set of audio_input_flags_t which is updated on return.
494 void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
495
496 // Is this EffectChain compatible with the RAW audio flag.
497 bool isRawCompatible() const;
498
499 // Is this EffectChain compatible with the FAST audio flag.
500 bool isFastCompatible() const;
Eric Laurent4c415062016-06-17 16:14:16 -0700501
502 // isCompatibleWithThread_l() must be called with thread->mLock held
503 bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
504
Eric Laurent5d885392019-12-13 10:56:31 -0800505 sp<EffectCallbackInterface> effectCallback() const { return mEffectCallback; }
506 wp<ThreadBase> thread() const { return mEffectCallback->thread(); }
507
Eric Laurent81784c32012-11-19 14:55:58 -0800508 void dump(int fd, const Vector<String16>& args);
509
Mikhail Naganovbf493082017-04-17 17:37:12 -0700510private:
Eric Laurent5d885392019-12-13 10:56:31 -0800511
512 class EffectCallback : public EffectCallbackInterface {
513 public:
514 EffectCallback(EffectChain *chain, ThreadBase *thread, AudioFlinger *audioFlinger)
515 : mChain(chain), mThread(thread), mAudioFlinger(audioFlinger) {}
516
517 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
518 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
519 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
Eric Laurente0b9a362019-12-16 19:34:05 -0800520 bool updateOrphanEffectChains(const sp<EffectBase>& effect) override;
Eric Laurent5d885392019-12-13 10:56:31 -0800521
522 audio_io_handle_t io() const override;
523 bool isOutput() const override;
524 bool isOffload() const override;
525 bool isOffloadOrDirect() const override;
526 bool isOffloadOrMmap() const override;
527
528 uint32_t sampleRate() const override;
529 audio_channel_mask_t channelMask() const override;
530 uint32_t channelCount() const override;
531 size_t frameCount() const override;
532 uint32_t latency() const override;
533
534 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
535 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
536 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
537 void setVolumeForOutput(float left, float right) const override;
538
539 // check if effects should be suspended/restored when a given effect is enable/disabled
Eric Laurente0b9a362019-12-16 19:34:05 -0800540 void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
Eric Laurent5d885392019-12-13 10:56:31 -0800541 bool enabled, bool threadLocked) override;
542 void resetVolume() override;
543 uint32_t strategy() const override;
544 int32_t activeTrackCnt() const override;
Eric Laurente0b9a362019-12-16 19:34:05 -0800545 void onEffectEnable(const sp<EffectBase>& effect) override;
546 void onEffectDisable(const sp<EffectBase>& effect) override;
Eric Laurent5d885392019-12-13 10:56:31 -0800547
548 wp<EffectChain> chain() const override { return mChain; }
549
550 wp<ThreadBase> thread() { return mThread; }
551 void setThread(ThreadBase *thread) { mThread = thread; };
552
553 private:
554 wp<EffectChain> mChain;
555 wp<ThreadBase> mThread;
556 wp<AudioFlinger> mAudioFlinger;
557 };
558
Eric Laurent81784c32012-11-19 14:55:58 -0800559 friend class AudioFlinger; // for mThread, mEffects
Mikhail Naganovbf493082017-04-17 17:37:12 -0700560 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800561
562 class SuspendedEffectDesc : public RefBase {
563 public:
564 SuspendedEffectDesc() : mRefCount(0) {}
565
Eric Laurentd8365c52017-07-16 15:27:05 -0700566 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800567 effect_uuid_t mType;
568 wp<EffectModule> mEffect;
569 };
570
571 // get a list of effect modules to suspend when an effect of the type
572 // passed is enabled.
573 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
574
575 // get an effect module if it is currently enable
576 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
577 // true if the effect whose descriptor is passed can be suspended
578 // OEMs can modify the rules implemented in this method to exclude specific effect
579 // types or implementations from the suspend/restore mechanism.
580 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
581
Eric Laurentd8365c52017-07-16 15:27:05 -0700582 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
583
Eric Laurent5d885392019-12-13 10:56:31 -0800584 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800585
Eric Laurentaaa44472014-09-12 17:41:50 -0700586 void setThread(const sp<ThreadBase>& thread);
587
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900588 void setVolumeForOutput_l(uint32_t left, uint32_t right);
589
Eric Laurent4c415062016-06-17 16:14:16 -0700590 mutable Mutex mLock; // mutex protecting effect list
591 Vector< sp<EffectModule> > mEffects; // list of effect modules
592 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800593 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
594 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800595
596 // 'volatile' here means these are accessed with atomic operations instead of mutex
597 volatile int32_t mActiveTrackCnt; // number of active tracks connected
598 volatile int32_t mTrackCnt; // number of tracks connected
599
Eric Laurent4c415062016-06-17 16:14:16 -0700600 int32_t mTailBufferCount; // current effect tail buffer count
601 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700602 int mVolumeCtrlIdx; // index of insert effect having control over volume
603 uint32_t mLeftVolume; // previous volume on left channel
604 uint32_t mRightVolume; // previous volume on right channel
605 uint32_t mNewLeftVolume; // new volume on left channel
606 uint32_t mNewRightVolume; // new volume on right channel
607 uint32_t mStrategy; // strategy for this effect chain
608 // mSuspendedEffects lists all effects currently suspended in the chain.
609 // Use effect type UUID timelow field as key. There is no real risk of identical
610 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700611 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700612 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent5d885392019-12-13 10:56:31 -0800613
614 const sp<EffectCallback> mEffectCallback;
Eric Laurent81784c32012-11-19 14:55:58 -0800615};
Eric Laurent9b2064c2019-11-22 17:25:04 -0800616
617class DeviceEffectProxy : public EffectBase {
618public:
619 DeviceEffectProxy (const AudioDeviceTypeAddr& device,
620 const sp<DeviceEffectManagerCallback>& callback,
621 effect_descriptor_t *desc, int id)
622 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
623 mDevice(device), mManagerCallback(callback),
624 mMyCallback(new ProxyCallback(this, callback)) {}
625
626 status_t setEnabled(bool enabled, bool fromHandle) override;
627 sp<DeviceEffectProxy> asDeviceEffectProxy() override { return this; }
628
629 status_t init(const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches);
630 status_t onCreatePatch(audio_patch_handle_t patchHandle, const PatchPanel::Patch& patch);
631 void onReleasePatch(audio_patch_handle_t patchHandle);
632
633 size_t removeEffect(const sp<EffectModule>& effect);
634
635 status_t addEffectToHal(sp<EffectHalInterface> effect);
636 status_t removeEffectFromHal(sp<EffectHalInterface> effect);
637
638 const AudioDeviceTypeAddr& device() { return mDevice; };
639 bool isOutput() const;
640 uint32_t sampleRate() const;
641 audio_channel_mask_t channelMask() const;
642 uint32_t channelCount() const;
643
644 void dump(int fd, int spaces);
645
646private:
647
648 class ProxyCallback : public EffectCallbackInterface {
649 public:
650 ProxyCallback(DeviceEffectProxy *proxy,
651 const sp<DeviceEffectManagerCallback>& callback)
652 : mProxy(proxy), mManagerCallback(callback) {}
653
654 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
655 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
656 status_t allocateHalBuffer(size_t size __unused,
657 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
658 bool updateOrphanEffectChains(const sp<EffectBase>& effect __unused) override {
659 return false;
660 }
661
662 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
663 bool isOutput() const override;
664 bool isOffload() const override { return false; }
665 bool isOffloadOrDirect() const override { return false; }
666 bool isOffloadOrMmap() const override { return false; }
667
668 uint32_t sampleRate() const override;
669 audio_channel_mask_t channelMask() const override;
670 uint32_t channelCount() const override;
671 size_t frameCount() const override { return 0; }
672 uint32_t latency() const override { return 0; }
673
674 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
675 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
676
677 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
678 void setVolumeForOutput(float left __unused, float right __unused) const override {}
679
680 void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect __unused,
681 bool enabled __unused, bool threadLocked __unused) override {}
682 void resetVolume() override {}
683 uint32_t strategy() const override { return 0; }
684 int32_t activeTrackCnt() const override { return 0; }
685 void onEffectEnable(const sp<EffectBase>& effect __unused) override {}
686 void onEffectDisable(const sp<EffectBase>& effect __unused) override {}
687
688 wp<EffectChain> chain() const override { return nullptr; }
689
690 int newEffectId();
691
692 private:
693 const wp<DeviceEffectProxy> mProxy;
694 const sp<DeviceEffectManagerCallback> mManagerCallback;
695 };
696
697 status_t checkPort(const PatchPanel::Patch& patch, const struct audio_port_config *port,
698 sp<EffectHandle> *handle);
699
700 const AudioDeviceTypeAddr mDevice;
701 const sp<DeviceEffectManagerCallback> mManagerCallback;
702 const sp<ProxyCallback> mMyCallback;
703
704 Mutex mProxyLock;
705 std::map<audio_patch_handle_t, sp<EffectHandle>> mEffectHandles; // protected by mProxyLock
706 sp<EffectModule> mHalEffect; // protected by mProxyLock
707 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
708};