blob: 036f8558d2671016437788af0c8ce7a41cb7fcbb [file] [log] [blame]
Eric Laurent81784c32012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef INCLUDING_FROM_AUDIOFLINGER_H
19 #error This header file should only be included from AudioFlinger.h
20#endif
21
22//--- Audio Effect Management
23
Eric Laurent6b446ce2019-12-13 10:56:31 -080024// Interface implemented by the EffectModule parent or owner (e.g an EffectChain) to abstract
25// interactions between the EffectModule and the reset of the audio framework.
26class EffectCallbackInterface : public RefBase {
27public:
28 ~EffectCallbackInterface() override = default;
29
30 // Trivial methods usually implemented with help from ThreadBase
31 virtual audio_io_handle_t io() const = 0;
32 virtual bool isOutput() const = 0;
33 virtual bool isOffload() const = 0;
34 virtual bool isOffloadOrDirect() const = 0;
35 virtual bool isOffloadOrMmap() const = 0;
Eric Laurentb82e6b72019-11-22 17:25:04 -080036 virtual uint32_t sampleRate() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080037 virtual audio_channel_mask_t channelMask() const = 0;
38 virtual uint32_t channelCount() const = 0;
jiabineb3bda02020-06-30 14:07:03 -070039 virtual audio_channel_mask_t hapticChannelMask() const = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080040 virtual size_t frameCount() const = 0;
41
42 // Non trivial methods usually implemented with help from ThreadBase:
43 // pay attention to mutex locking order
44 virtual uint32_t latency() const { return 0; }
45 virtual status_t addEffectToHal(sp<EffectHalInterface> effect) = 0;
46 virtual status_t removeEffectFromHal(sp<EffectHalInterface> effect) = 0;
47 virtual void setVolumeForOutput(float left, float right) const = 0;
48 virtual bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080049 virtual void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -080050 bool enabled,
51 bool threadLocked) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080052 virtual void onEffectEnable(const sp<EffectBase>& effect) = 0;
53 virtual void onEffectDisable(const sp<EffectBase>& effect) = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080054
55 // Methods usually implemented with help from AudioFlinger: pay attention to mutex locking order
56 virtual status_t createEffectHal(const effect_uuid_t *pEffectUuid,
57 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) = 0;
58 virtual status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) = 0;
Eric Laurent41709552019-12-16 19:34:05 -080059 virtual bool updateOrphanEffectChains(const sp<EffectBase>& effect) = 0;
Eric Laurent6b446ce2019-12-13 10:56:31 -080060
61 // Methods usually implemented with help from EffectChain: pay attention to mutex locking order
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 Laurent41709552019-12-16 19:34:05 -080069// EffectBase(EffectModule) and EffectChain classes both have their own mutex to protect
Eric Laurent81784c32012-11-19 14:55:58 -080070// state changes or resource modifications. Always respect the following order
71// if multiple mutexes must be acquired to avoid cross deadlock:
Eric Laurent41709552019-12-16 19:34:05 -080072// AudioFlinger -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
73// AudioHandle -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
Eric Laurent6b446ce2019-12-13 10:56:31 -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
Eric Laurent41709552019-12-16 19:34:05 -080084
85// The EffectBase class contains common properties, state and behavior for and EffectModule or
86// other derived classes managing an audio effect instance within the effect framework.
87// It also contains the class mutex (see comment on locking order above).
88class EffectBase : public RefBase {
Eric Laurent81784c32012-11-19 14:55:58 -080089public:
Eric Laurent41709552019-12-16 19:34:05 -080090 EffectBase(const sp<EffectCallbackInterface>& callback,
91 effect_descriptor_t *desc,
92 int id,
93 audio_session_t sessionId,
94 bool pinned);
95
96 ~EffectBase() override = default;
Eric Laurent81784c32012-11-19 14:55:58 -080097
98 enum effect_state {
99 IDLE,
100 RESTART,
101 STARTING,
102 ACTIVE,
103 STOPPING,
104 STOPPED,
105 DESTROYED
106 };
107
Eric Laurent41709552019-12-16 19:34:05 -0800108 int id() const { return mId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800109 effect_state state() const {
110 return mState;
111 }
Glenn Kastend848eb42016-03-08 13:42:11 -0800112 audio_session_t sessionId() const {
Eric Laurent81784c32012-11-19 14:55:58 -0800113 return mSessionId;
114 }
Eric Laurent81784c32012-11-19 14:55:58 -0800115 const effect_descriptor_t& desc() const { return mDescriptor; }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700116 bool isOffloadable() const
117 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Eric Laurent4c415062016-06-17 16:14:16 -0700118 bool isImplementationSoftware() const
119 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700120 bool isProcessImplemented() const
121 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900122 bool isVolumeControl() const
123 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
124 == EFFECT_FLAG_VOLUME_CTRL; }
Jasmine Cha934ecfb2019-01-23 18:19:14 +0800125 bool isVolumeMonitor() const
126 { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
127 == EFFECT_FLAG_VOLUME_MONITOR; }
Eric Laurent41709552019-12-16 19:34:05 -0800128
129 virtual status_t setEnabled(bool enabled, bool fromHandle);
130 status_t setEnabled_l(bool enabled);
131 bool isEnabled() const;
132
133 void setSuspended(bool suspended);
134 bool suspended() const;
135
136 virtual status_t command(uint32_t cmdCode __unused,
137 uint32_t cmdSize __unused,
138 void *pCmdData __unused,
139 uint32_t *replySize __unused,
140 void *pReplyData __unused) { return NO_ERROR; };
141
142 void setCallback(const sp<EffectCallbackInterface>& callback) { mCallback = callback; }
143 sp<EffectCallbackInterface>& callback() { return mCallback; }
144
145 status_t addHandle(EffectHandle *handle);
146 ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
147 ssize_t removeHandle(EffectHandle *handle);
148 virtual ssize_t removeHandle_l(EffectHandle *handle);
149 EffectHandle* controlHandle_l();
150 bool purgeHandles();
151
152 void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked);
153
154 bool isPinned() const { return mPinned; }
155 void unPin() { mPinned = false; }
156
157 void lock() { mLock.lock(); }
158 void unlock() { mLock.unlock(); }
Eric Laurent81784c32012-11-19 14:55:58 -0800159
Eric Laurent6c796322019-04-09 14:13:17 -0700160 status_t updatePolicyState();
Eric Laurent41709552019-12-16 19:34:05 -0800161
162 virtual sp<EffectModule> asEffectModule() { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800163 virtual sp<DeviceEffectProxy> asDeviceEffectProxy() { return nullptr; }
Eric Laurent6c796322019-04-09 14:13:17 -0700164
Eric Laurent81784c32012-11-19 14:55:58 -0800165 void dump(int fd, const Vector<String16>& args);
166
Mikhail Naganovbf493082017-04-17 17:37:12 -0700167private:
Eric Laurent81784c32012-11-19 14:55:58 -0800168 friend class AudioFlinger; // for mHandles
Eric Laurent41709552019-12-16 19:34:05 -0800169 bool mPinned = false;
170
171 DISALLOW_COPY_AND_ASSIGN(EffectBase);
172
173mutable Mutex mLock; // mutex for process, commands and handles list protection
174 sp<EffectCallbackInterface> mCallback; // parent effect chain
175 const int mId; // this instance unique ID
176 const audio_session_t mSessionId; // audio session ID
177 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
178 effect_state mState = IDLE; // current activation state
Eric Laurentd9eb4962019-12-19 09:20:49 -0800179 // effect is suspended: temporarily disabled by framework
180 bool mSuspended = false;
Eric Laurent41709552019-12-16 19:34:05 -0800181
182 Vector<EffectHandle *> mHandles; // list of client handles
183 // First handle in mHandles has highest priority and controls the effect module
184
185 // Audio policy effect state management
186 // Mutex protecting transactions with audio policy manager as mLock cannot
187 // be held to avoid cross deadlocks with audio policy mutex
188 Mutex mPolicyLock;
189 // Effect is registered in APM or not
190 bool mPolicyRegistered = false;
191 // Effect enabled state communicated to APM. Enabled state corresponds to
192 // state requested by the EffectHandle with control
193 bool mPolicyEnabled = false;
194};
195
196// The EffectModule class is a wrapper object controlling the effect engine implementation
197// in the effect library. It prevents concurrent calls to process() and command() functions
198// from different client threads. It keeps a list of EffectHandle objects corresponding
199// to all client applications using this effect and notifies applications of effect state,
200// control or parameter changes. It manages the activation state machine to send appropriate
201// reset, enable, disable commands to effect engine and provide volume
202// ramping when effects are activated/deactivated.
203// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
204// the attached track(s) to accumulate their auxiliary channel.
205class EffectModule : public EffectBase {
206public:
207 EffectModule(const sp<EffectCallbackInterface>& callabck,
208 effect_descriptor_t *desc,
209 int id,
210 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800211 bool pinned,
212 audio_port_handle_t deviceId);
Eric Laurent41709552019-12-16 19:34:05 -0800213 virtual ~EffectModule();
214
215 void process();
216 bool updateState();
217 status_t command(uint32_t cmdCode,
218 uint32_t cmdSize,
219 void *pCmdData,
220 uint32_t *replySize,
221 void *pReplyData) override;
222
223 void reset_l();
224 status_t configure();
225 status_t init();
226
227 uint32_t status() {
228 return mStatus;
229 }
230
231 bool isProcessEnabled() const;
232 bool isOffloadedOrDirect() const;
233 bool isVolumeControlEnabled() const;
234
235 void setInBuffer(const sp<EffectBufferHalInterface>& buffer);
236 int16_t *inBuffer() const {
237 return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
238 }
239 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer);
240 int16_t *outBuffer() const {
241 return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
242 }
243
244 ssize_t removeHandle_l(EffectHandle *handle) override;
245
246 status_t setDevices(const AudioDeviceTypeAddrVector &devices);
247 status_t setInputDevice(const AudioDeviceTypeAddr &device);
248 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
249 status_t setMode(audio_mode_t mode);
250 status_t setAudioSource(audio_source_t source);
251 status_t start();
252 status_t stop();
253
254 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
255 bool isOffloaded() const;
256 void addEffectToHal_l();
257 void release_l();
258
259 sp<EffectModule> asEffectModule() override { return this; }
260
jiabineb3bda02020-06-30 14:07:03 -0700261 static bool isHapticGenerator(const effect_uuid_t* type);
262 bool isHapticGenerator() const;
263
Eric Laurent41709552019-12-16 19:34:05 -0800264 void dump(int fd, const Vector<String16>& args);
265
266private:
267 friend class AudioFlinger; // for mHandles
Eric Laurent81784c32012-11-19 14:55:58 -0800268
269 // Maximum time allocated to effect engines to complete the turn off sequence
270 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
271
Mikhail Naganovbf493082017-04-17 17:37:12 -0700272 DISALLOW_COPY_AND_ASSIGN(EffectModule);
Eric Laurent81784c32012-11-19 14:55:58 -0800273
274 status_t start_l();
275 status_t stop_l();
Eric Laurent6b446ce2019-12-13 10:56:31 -0800276 status_t removeEffectFromHal_l();
jiabin8f278ee2019-11-11 12:16:27 -0800277 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
Eric Laurent81784c32012-11-19 14:55:58 -0800278
Eric Laurent81784c32012-11-19 14:55:58 -0800279 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700280 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Mikhail Naganov022b9952017-01-04 16:36:51 -0800281 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL
282 sp<EffectBufferHalInterface> mOutBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800283 status_t mStatus; // initialization status
Eric Laurent81784c32012-11-19 14:55:58 -0800284 // First handle in mHandles has highest priority and controls the effect module
285 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
286 // sending disable command.
287 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
Eric Laurent5baf2af2013-09-12 17:37:00 -0700288 bool mOffloaded; // effect is currently offloaded to the audio DSP
rago94a1ee82017-07-21 15:11:02 -0700289
290#ifdef FLOAT_EFFECT_CHAIN
291 bool mSupportsFloat; // effect supports float processing
Andy Hungbded9c82017-11-30 18:47:35 -0800292 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed.
293 sp<EffectBufferHalInterface> mOutConversionBuffer;
Andy Hung9aad48c2017-11-29 10:29:19 -0800294 uint32_t mInChannelCountRequested;
295 uint32_t mOutChannelCountRequested;
rago94a1ee82017-07-21 15:11:02 -0700296#endif
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900297
298 class AutoLockReentrant {
299 public:
300 AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
301 : mMutex(gettid() == allowedTid ? nullptr : &mutex)
302 {
303 if (mMutex != nullptr) mMutex->lock();
304 }
305 ~AutoLockReentrant() {
306 if (mMutex != nullptr) mMutex->unlock();
307 }
308 private:
309 Mutex * const mMutex;
310 };
311
312 static constexpr pid_t INVALID_PID = (pid_t)-1;
313 // this tid is allowed to call setVolume() without acquiring the mutex.
314 pid_t mSetVolumeReentrantTid = INVALID_PID;
Eric Laurent81784c32012-11-19 14:55:58 -0800315};
316
317// The EffectHandle class implements the IEffect interface. It provides resources
318// to receive parameter updates, keeps track of effect control
319// ownership and state and has a pointer to the EffectModule object it is controlling.
320// There is one EffectHandle object for each application controlling (or using)
321// an effect module.
322// The EffectHandle is obtained by calling AudioFlinger::createEffect().
323class EffectHandle: public android::BnEffect {
324public:
325
Eric Laurent41709552019-12-16 19:34:05 -0800326 EffectHandle(const sp<EffectBase>& effect,
Eric Laurent81784c32012-11-19 14:55:58 -0800327 const sp<AudioFlinger::Client>& client,
328 const sp<IEffectClient>& effectClient,
329 int32_t priority);
330 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800331 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800332
333 // IEffect
334 virtual status_t enable();
335 virtual status_t disable();
336 virtual status_t command(uint32_t cmdCode,
337 uint32_t cmdSize,
338 void *pCmdData,
339 uint32_t *replySize,
340 void *pReplyData);
341 virtual void disconnect();
342private:
343 void disconnect(bool unpinIfLast);
344public:
345 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
346 virtual status_t onTransact(uint32_t code, const Parcel& data,
347 Parcel* reply, uint32_t flags);
348
349
350 // Give or take control of effect module
351 // - hasControl: true if control is given, false if removed
352 // - signal: true client app should be signaled of change, false otherwise
353 // - enabled: state of the effect when control is passed
354 void setControl(bool hasControl, bool signal, bool enabled);
355 void commandExecuted(uint32_t cmdCode,
356 uint32_t cmdSize,
357 void *pCmdData,
358 uint32_t replySize,
359 void *pReplyData);
360 void setEnabled(bool enabled);
361 bool enabled() const { return mEnabled; }
362
363 // Getters
Eric Laurent41709552019-12-16 19:34:05 -0800364 wp<EffectBase> effect() const { return mEffect; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800365 int id() const {
Eric Laurent41709552019-12-16 19:34:05 -0800366 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800367 if (effect == 0) {
368 return 0;
369 }
370 return effect->id();
371 }
Eric Laurent81784c32012-11-19 14:55:58 -0800372 int priority() const { return mPriority; }
373 bool hasControl() const { return mHasControl; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800374 bool disconnected() const { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800375
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800376 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800377
Mikhail Naganovbf493082017-04-17 17:37:12 -0700378private:
Eric Laurent81784c32012-11-19 14:55:58 -0800379 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
Mikhail Naganovbf493082017-04-17 17:37:12 -0700380 DISALLOW_COPY_AND_ASSIGN(EffectHandle);
Eric Laurent81784c32012-11-19 14:55:58 -0800381
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800382 Mutex mLock; // protects IEffect method calls
Eric Laurent41709552019-12-16 19:34:05 -0800383 wp<EffectBase> mEffect; // pointer to controlled EffectModule
Eric Laurent81784c32012-11-19 14:55:58 -0800384 sp<IEffectClient> mEffectClient; // callback interface for client notifications
385 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
386 sp<IMemory> mCblkMemory; // shared memory for control block
387 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
388 // shared memory
389 uint8_t* mBuffer; // pointer to parameter area in shared memory
390 int mPriority; // client application priority to control the effect
391 bool mHasControl; // true if this handle is controlling the effect
392 bool mEnabled; // cached enable state: needed when the effect is
393 // restored after being suspended
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800394 bool mDisconnected; // Set to true by disconnect()
Eric Laurent81784c32012-11-19 14:55:58 -0800395};
396
397// the EffectChain class represents a group of effects associated to one audio session.
398// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800399// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
400// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800401// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
402// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800403// order corresponding in the effect process order. When attached to a track (session ID !=
404// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800405// it also provide it's own input buffer used by the track as accumulation buffer.
406class EffectChain : public RefBase {
407public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800408 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800409 virtual ~EffectChain();
410
411 // special key used for an entry in mSuspendedEffects keyed vector
412 // corresponding to a suspend all request.
413 static const int kKeyForSuspendAll = 0;
414
415 // minimum duration during which we force calling effect process when last track on
416 // a session is stopped or removed to allow effect tail to be rendered
417 static const int kProcessTailDurationMs = 1000;
418
419 void process_l();
420
421 void lock() {
422 mLock.lock();
423 }
424 void unlock() {
425 mLock.unlock();
426 }
427
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800428 status_t createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800429 effect_descriptor_t *desc,
430 int id,
431 audio_session_t sessionId,
432 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800433 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800434 status_t addEffect_ll(const sp<EffectModule>& handle);
435 size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
Eric Laurent81784c32012-11-19 14:55:58 -0800436
Glenn Kastend848eb42016-03-08 13:42:11 -0800437 audio_session_t sessionId() const { return mSessionId; }
438 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800439
440 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
441 sp<EffectModule> getEffectFromId_l(int id);
442 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Eric Laurent6c796322019-04-09 14:13:17 -0700443 std::vector<int> getEffectIds();
Glenn Kastenc56f3422014-03-21 17:53:17 -0700444 // FIXME use float to improve the dynamic range
Eric Laurentfa1e1232016-08-02 19:01:49 -0700445 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
446 void resetVolume_l();
jiabin8f278ee2019-11-11 12:16:27 -0800447 void setDevices_l(const AudioDeviceTypeAddrVector &devices);
448 void setInputDevice_l(const AudioDeviceTypeAddr &device);
Eric Laurent81784c32012-11-19 14:55:58 -0800449 void setMode_l(audio_mode_t mode);
450 void setAudioSource_l(audio_source_t source);
451
Mikhail Naganov022b9952017-01-04 16:36:51 -0800452 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800453 mInBuffer = buffer;
Eric Laurent81784c32012-11-19 14:55:58 -0800454 }
rago94a1ee82017-07-21 15:11:02 -0700455 effect_buffer_t *inBuffer() const {
456 return mInBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mInBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800457 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800458 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
Eric Laurent81784c32012-11-19 14:55:58 -0800459 mOutBuffer = buffer;
460 }
rago94a1ee82017-07-21 15:11:02 -0700461 effect_buffer_t *outBuffer() const {
462 return mOutBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mOutBuffer->ptr()) : NULL;
Eric Laurent81784c32012-11-19 14:55:58 -0800463 }
464
465 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
466 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
467 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
468
469 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
470 mTailBufferCount = mMaxTailBuffers; }
471 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
472 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
473
474 uint32_t strategy() const { return mStrategy; }
475 void setStrategy(uint32_t strategy)
476 { mStrategy = strategy; }
477
Eric Laurentd8365c52017-07-16 15:27:05 -0700478 // suspend or restore effects of the specified type. The number of suspend requests is counted
479 // and restore occurs once all suspend requests are cancelled.
Eric Laurent81784c32012-11-19 14:55:58 -0800480 void setEffectSuspended_l(const effect_uuid_t *type,
481 bool suspend);
482 // suspend all eligible effects
483 void setEffectSuspendedAll_l(bool suspend);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800484 // check if effects should be suspended or restored when a given effect is enable or disabled
485 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, bool enabled);
Eric Laurent81784c32012-11-19 14:55:58 -0800486
487 void clearInputBuffer();
488
Eric Laurent5baf2af2013-09-12 17:37:00 -0700489 // At least one non offloadable effect in the chain is enabled
490 bool isNonOffloadableEnabled();
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +0900491 bool isNonOffloadableEnabled_l();
Eric Laurent813e2a72013-08-31 12:59:48 -0700492
Eric Laurent1b928682014-10-02 19:41:47 -0700493 void syncHalEffectsState();
494
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700495 // flags is an ORed set of audio_output_flags_t which is updated on return.
496 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
497
498 // flags is an ORed set of audio_input_flags_t which is updated on return.
499 void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
500
501 // Is this EffectChain compatible with the RAW audio flag.
502 bool isRawCompatible() const;
503
504 // Is this EffectChain compatible with the FAST audio flag.
505 bool isFastCompatible() const;
Eric Laurent4c415062016-06-17 16:14:16 -0700506
507 // isCompatibleWithThread_l() must be called with thread->mLock held
508 bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
509
jiabineb3bda02020-06-30 14:07:03 -0700510 bool containsHapticGeneratingEffect_l();
511
Eric Laurent6b446ce2019-12-13 10:56:31 -0800512 sp<EffectCallbackInterface> effectCallback() const { return mEffectCallback; }
513 wp<ThreadBase> thread() const { return mEffectCallback->thread(); }
514
Eric Laurent81784c32012-11-19 14:55:58 -0800515 void dump(int fd, const Vector<String16>& args);
516
Mikhail Naganovbf493082017-04-17 17:37:12 -0700517private:
Eric Laurent6b446ce2019-12-13 10:56:31 -0800518
519 class EffectCallback : public EffectCallbackInterface {
520 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800521 // Note: ctors taking a weak pointer to their owner must not promote it
522 // during construction (but may keep a reference for later promotion).
523 EffectCallback(const wp<EffectChain>& owner,
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800524 const wp<ThreadBase>& thread)
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800525 : mChain(owner) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800526 setThread(thread);
527 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800528
529 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
530 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
531 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
Eric Laurent41709552019-12-16 19:34:05 -0800532 bool updateOrphanEffectChains(const sp<EffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800533
534 audio_io_handle_t io() const override;
535 bool isOutput() const override;
536 bool isOffload() const override;
537 bool isOffloadOrDirect() const override;
538 bool isOffloadOrMmap() const override;
539
540 uint32_t sampleRate() const override;
541 audio_channel_mask_t channelMask() const override;
542 uint32_t channelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700543 audio_channel_mask_t hapticChannelMask() const override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800544 size_t frameCount() const override;
545 uint32_t latency() const override;
546
547 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
548 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
549 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
550 void setVolumeForOutput(float left, float right) const override;
551
552 // check if effects should be suspended/restored when a given effect is enable/disabled
Eric Laurent41709552019-12-16 19:34:05 -0800553 void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
Eric Laurent6b446ce2019-12-13 10:56:31 -0800554 bool enabled, bool threadLocked) override;
555 void resetVolume() override;
556 uint32_t strategy() const override;
557 int32_t activeTrackCnt() const override;
Eric Laurent41709552019-12-16 19:34:05 -0800558 void onEffectEnable(const sp<EffectBase>& effect) override;
559 void onEffectDisable(const sp<EffectBase>& effect) override;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800560
561 wp<EffectChain> chain() const override { return mChain; }
562
563 wp<ThreadBase> thread() { return mThread; }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800564
565 void setThread(const wp<ThreadBase>& thread) {
566 mThread = thread;
567 sp<ThreadBase> p = thread.promote();
568 mAudioFlinger = p ? p->mAudioFlinger : nullptr;
569 }
Eric Laurent6b446ce2019-12-13 10:56:31 -0800570
571 private:
572 wp<EffectChain> mChain;
573 wp<ThreadBase> mThread;
574 wp<AudioFlinger> mAudioFlinger;
575 };
576
Eric Laurent81784c32012-11-19 14:55:58 -0800577 friend class AudioFlinger; // for mThread, mEffects
Mikhail Naganovbf493082017-04-17 17:37:12 -0700578 DISALLOW_COPY_AND_ASSIGN(EffectChain);
Eric Laurent81784c32012-11-19 14:55:58 -0800579
580 class SuspendedEffectDesc : public RefBase {
581 public:
582 SuspendedEffectDesc() : mRefCount(0) {}
583
Eric Laurentd8365c52017-07-16 15:27:05 -0700584 int mRefCount; // > 0 when suspended
Eric Laurent81784c32012-11-19 14:55:58 -0800585 effect_uuid_t mType;
586 wp<EffectModule> mEffect;
587 };
588
589 // get a list of effect modules to suspend when an effect of the type
590 // passed is enabled.
591 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
592
593 // get an effect module if it is currently enable
594 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
595 // true if the effect whose descriptor is passed can be suspended
596 // OEMs can modify the rules implemented in this method to exclude specific effect
597 // types or implementations from the suspend/restore mechanism.
598 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
599
Eric Laurentd8365c52017-07-16 15:27:05 -0700600 static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
601
Eric Laurent6b446ce2019-12-13 10:56:31 -0800602 void clearInputBuffer_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800603
Eric Laurentaaa44472014-09-12 17:41:50 -0700604 void setThread(const sp<ThreadBase>& thread);
605
Zhou Songd505c642020-02-20 16:35:37 +0800606 // true if any effect module within the chain has volume control
607 bool hasVolumeControlEnabled_l() const;
608
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900609 void setVolumeForOutput_l(uint32_t left, uint32_t right);
610
Eric Laurent4c415062016-06-17 16:14:16 -0700611 mutable Mutex mLock; // mutex protecting effect list
612 Vector< sp<EffectModule> > mEffects; // list of effect modules
613 audio_session_t mSessionId; // audio session ID
Mikhail Naganov022b9952017-01-04 16:36:51 -0800614 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer
615 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800616
617 // 'volatile' here means these are accessed with atomic operations instead of mutex
618 volatile int32_t mActiveTrackCnt; // number of active tracks connected
619 volatile int32_t mTrackCnt; // number of tracks connected
620
Eric Laurent4c415062016-06-17 16:14:16 -0700621 int32_t mTailBufferCount; // current effect tail buffer count
622 int32_t mMaxTailBuffers; // maximum effect tail buffers
Eric Laurent4c415062016-06-17 16:14:16 -0700623 int mVolumeCtrlIdx; // index of insert effect having control over volume
624 uint32_t mLeftVolume; // previous volume on left channel
625 uint32_t mRightVolume; // previous volume on right channel
626 uint32_t mNewLeftVolume; // new volume on left channel
627 uint32_t mNewRightVolume; // new volume on right channel
628 uint32_t mStrategy; // strategy for this effect chain
629 // mSuspendedEffects lists all effects currently suspended in the chain.
630 // Use effect type UUID timelow field as key. There is no real risk of identical
631 // timeLow fields among effect type UUIDs.
Eric Laurentd8365c52017-07-16 15:27:05 -0700632 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
Eric Laurent4c415062016-06-17 16:14:16 -0700633 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800634
635 const sp<EffectCallback> mEffectCallback;
Eric Laurent81784c32012-11-19 14:55:58 -0800636};
Eric Laurentb82e6b72019-11-22 17:25:04 -0800637
638class DeviceEffectProxy : public EffectBase {
639public:
640 DeviceEffectProxy (const AudioDeviceTypeAddr& device,
641 const sp<DeviceEffectManagerCallback>& callback,
642 effect_descriptor_t *desc, int id)
643 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
644 mDevice(device), mManagerCallback(callback),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800645 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this),
646 callback)) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800647
648 status_t setEnabled(bool enabled, bool fromHandle) override;
649 sp<DeviceEffectProxy> asDeviceEffectProxy() override { return this; }
650
651 status_t init(const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches);
652 status_t onCreatePatch(audio_patch_handle_t patchHandle, const PatchPanel::Patch& patch);
653 void onReleasePatch(audio_patch_handle_t patchHandle);
654
655 size_t removeEffect(const sp<EffectModule>& effect);
656
657 status_t addEffectToHal(sp<EffectHalInterface> effect);
658 status_t removeEffectFromHal(sp<EffectHalInterface> effect);
659
660 const AudioDeviceTypeAddr& device() { return mDevice; };
661 bool isOutput() const;
662 uint32_t sampleRate() const;
663 audio_channel_mask_t channelMask() const;
664 uint32_t channelCount() const;
665
666 void dump(int fd, int spaces);
667
668private:
669
670 class ProxyCallback : public EffectCallbackInterface {
671 public:
Ytai Ben-Tsvi4f043362020-01-28 12:39:23 -0800672 // Note: ctors taking a weak pointer to their owner must not promote it
673 // during construction (but may keep a reference for later promotion).
674 ProxyCallback(const wp<DeviceEffectProxy>& owner,
675 const sp<DeviceEffectManagerCallback>& callback)
676 : mProxy(owner), mManagerCallback(callback) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800677
678 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
679 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
680 status_t allocateHalBuffer(size_t size __unused,
681 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
682 bool updateOrphanEffectChains(const sp<EffectBase>& effect __unused) override {
683 return false;
684 }
685
686 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
687 bool isOutput() const override;
688 bool isOffload() const override { return false; }
689 bool isOffloadOrDirect() const override { return false; }
690 bool isOffloadOrMmap() const override { return false; }
691
692 uint32_t sampleRate() const override;
693 audio_channel_mask_t channelMask() const override;
694 uint32_t channelCount() const override;
jiabineb3bda02020-06-30 14:07:03 -0700695 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800696 size_t frameCount() const override { return 0; }
697 uint32_t latency() const override { return 0; }
698
699 status_t addEffectToHal(sp<EffectHalInterface> effect) override;
700 status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
701
702 bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
703 void setVolumeForOutput(float left __unused, float right __unused) const override {}
704
705 void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect __unused,
706 bool enabled __unused, bool threadLocked __unused) override {}
707 void resetVolume() override {}
708 uint32_t strategy() const override { return 0; }
709 int32_t activeTrackCnt() const override { return 0; }
710 void onEffectEnable(const sp<EffectBase>& effect __unused) override {}
711 void onEffectDisable(const sp<EffectBase>& effect __unused) override {}
712
713 wp<EffectChain> chain() const override { return nullptr; }
714
715 int newEffectId();
716
717 private:
718 const wp<DeviceEffectProxy> mProxy;
719 const sp<DeviceEffectManagerCallback> mManagerCallback;
720 };
721
722 status_t checkPort(const PatchPanel::Patch& patch, const struct audio_port_config *port,
723 sp<EffectHandle> *handle);
724
725 const AudioDeviceTypeAddr mDevice;
726 const sp<DeviceEffectManagerCallback> mManagerCallback;
727 const sp<ProxyCallback> mMyCallback;
728
729 Mutex mProxyLock;
730 std::map<audio_patch_handle_t, sp<EffectHandle>> mEffectHandles; // protected by mProxyLock
731 sp<EffectModule> mHalEffect; // protected by mProxyLock
732 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
733};