blob: 8f14f6708c5b6ab98a0978ad4189d6c1d46a8d27 [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
24// EffectModule and EffectChain classes both have their own mutex to protect
25// state changes or resource modifications. Always respect the following order
26// if multiple mutexes must be acquired to avoid cross deadlock:
27// AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
Eric Laurent3bc859b2016-12-05 11:07:22 -080028// AudioHandle -> ThreadBase -> EffectChain -> EffectModule
Eric Laurenteb3c3372013-09-25 12:25:29 -070029// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
Eric Laurent3bc859b2016-12-05 11:07:22 -080030// startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or
31// Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService
32// methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order.
Eric Laurent81784c32012-11-19 14:55:58 -080033
34// The EffectModule class is a wrapper object controlling the effect engine implementation
35// in the effect library. It prevents concurrent calls to process() and command() functions
36// from different client threads. It keeps a list of EffectHandle objects corresponding
37// to all client applications using this effect and notifies applications of effect state,
38// control or parameter changes. It manages the activation state machine to send appropriate
39// reset, enable, disable commands to effect engine and provide volume
40// ramping when effects are activated/deactivated.
41// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
42// the attached track(s) to accumulate their auxiliary channel.
43class EffectModule : public RefBase {
44public:
45 EffectModule(ThreadBase *thread,
46 const wp<AudioFlinger::EffectChain>& chain,
47 effect_descriptor_t *desc,
48 int id,
Glenn Kastend848eb42016-03-08 13:42:11 -080049 audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -080050 virtual ~EffectModule();
51
52 enum effect_state {
53 IDLE,
54 RESTART,
55 STARTING,
56 ACTIVE,
57 STOPPING,
58 STOPPED,
59 DESTROYED
60 };
61
62 int id() const { return mId; }
63 void process();
Eric Laurentfa1e1232016-08-02 19:01:49 -070064 bool updateState();
Eric Laurent81784c32012-11-19 14:55:58 -080065 status_t command(uint32_t cmdCode,
66 uint32_t cmdSize,
67 void *pCmdData,
68 uint32_t *replySize,
69 void *pReplyData);
70
71 void reset_l();
72 status_t configure();
73 status_t init();
74 effect_state state() const {
75 return mState;
76 }
77 uint32_t status() {
78 return mStatus;
79 }
Glenn Kastend848eb42016-03-08 13:42:11 -080080 audio_session_t sessionId() const {
Eric Laurent81784c32012-11-19 14:55:58 -080081 return mSessionId;
82 }
83 status_t setEnabled(bool enabled);
84 status_t setEnabled_l(bool enabled);
85 bool isEnabled() const;
86 bool isProcessEnabled() const;
87
88 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
89 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
90 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
91 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
92 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
93 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
94 const wp<ThreadBase>& thread() { return mThread; }
95
96 status_t addHandle(EffectHandle *handle);
97 size_t disconnect(EffectHandle *handle, bool unpinIfLast);
98 size_t removeHandle(EffectHandle *handle);
99
100 const effect_descriptor_t& desc() const { return mDescriptor; }
101 wp<EffectChain>& chain() { return mChain; }
102
103 status_t setDevice(audio_devices_t device);
104 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
105 status_t setMode(audio_mode_t mode);
106 status_t setAudioSource(audio_source_t source);
107 status_t start();
108 status_t stop();
109 void setSuspended(bool suspended);
110 bool suspended() const;
111
112 EffectHandle* controlHandle_l();
113
114 bool isPinned() const { return mPinned; }
115 void unPin() { mPinned = false; }
116 bool purgeHandles();
117 void lock() { mLock.lock(); }
118 void unlock() { mLock.unlock(); }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700119 bool isOffloadable() const
120 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Eric Laurent4c415062016-06-17 16:14:16 -0700121 bool isImplementationSoftware() const
122 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700123 bool isProcessImplemented() const
124 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700125 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
126 bool isOffloaded() const;
Eric Laurent1b928682014-10-02 19:41:47 -0700127 void addEffectToHal_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800128
129 void dump(int fd, const Vector<String16>& args);
130
131protected:
132 friend class AudioFlinger; // for mHandles
133 bool mPinned;
134
135 // Maximum time allocated to effect engines to complete the turn off sequence
136 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
137
138 EffectModule(const EffectModule&);
139 EffectModule& operator = (const EffectModule&);
140
141 status_t start_l();
142 status_t stop_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800143 status_t remove_effect_from_hal_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800144
145mutable Mutex mLock; // mutex for process, commands and handles list protection
146 wp<ThreadBase> mThread; // parent thread
147 wp<EffectChain> mChain; // parent effect chain
148 const int mId; // this instance unique ID
Glenn Kastend848eb42016-03-08 13:42:11 -0800149 const audio_session_t mSessionId; // audio session ID
Eric Laurent81784c32012-11-19 14:55:58 -0800150 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
151 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700152 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Eric Laurent81784c32012-11-19 14:55:58 -0800153 status_t mStatus; // initialization status
154 effect_state mState; // current activation state
155 Vector<EffectHandle *> mHandles; // list of client handles
156 // First handle in mHandles has highest priority and controls the effect module
157 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
158 // sending disable command.
159 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
160 bool mSuspended; // effect is suspended: temporarily disabled by framework
Eric Laurent5baf2af2013-09-12 17:37:00 -0700161 bool mOffloaded; // effect is currently offloaded to the audio DSP
Eric Laurentaaa44472014-09-12 17:41:50 -0700162 wp<AudioFlinger> mAudioFlinger;
Eric Laurent81784c32012-11-19 14:55:58 -0800163};
164
165// The EffectHandle class implements the IEffect interface. It provides resources
166// to receive parameter updates, keeps track of effect control
167// ownership and state and has a pointer to the EffectModule object it is controlling.
168// There is one EffectHandle object for each application controlling (or using)
169// an effect module.
170// The EffectHandle is obtained by calling AudioFlinger::createEffect().
171class EffectHandle: public android::BnEffect {
172public:
173
174 EffectHandle(const sp<EffectModule>& effect,
175 const sp<AudioFlinger::Client>& client,
176 const sp<IEffectClient>& effectClient,
177 int32_t priority);
178 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800179 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800180
181 // IEffect
182 virtual status_t enable();
183 virtual status_t disable();
184 virtual status_t command(uint32_t cmdCode,
185 uint32_t cmdSize,
186 void *pCmdData,
187 uint32_t *replySize,
188 void *pReplyData);
189 virtual void disconnect();
190private:
191 void disconnect(bool unpinIfLast);
192public:
193 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
194 virtual status_t onTransact(uint32_t code, const Parcel& data,
195 Parcel* reply, uint32_t flags);
196
197
198 // Give or take control of effect module
199 // - hasControl: true if control is given, false if removed
200 // - signal: true client app should be signaled of change, false otherwise
201 // - enabled: state of the effect when control is passed
202 void setControl(bool hasControl, bool signal, bool enabled);
203 void commandExecuted(uint32_t cmdCode,
204 uint32_t cmdSize,
205 void *pCmdData,
206 uint32_t replySize,
207 void *pReplyData);
208 void setEnabled(bool enabled);
209 bool enabled() const { return mEnabled; }
210
211 // Getters
212 int id() const { return mEffect->id(); }
213 int priority() const { return mPriority; }
214 bool hasControl() const { return mHasControl; }
215 sp<EffectModule> effect() const { return mEffect; }
216 // destroyed_l() must be called with the associated EffectModule mLock held
217 bool destroyed_l() const { return mDestroyed; }
218
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800219 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800220
221protected:
222 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
223 EffectHandle(const EffectHandle&);
224 EffectHandle& operator =(const EffectHandle&);
225
226 sp<EffectModule> mEffect; // pointer to controlled EffectModule
227 sp<IEffectClient> mEffectClient; // callback interface for client notifications
228 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
229 sp<IMemory> mCblkMemory; // shared memory for control block
230 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
231 // shared memory
232 uint8_t* mBuffer; // pointer to parameter area in shared memory
233 int mPriority; // client application priority to control the effect
234 bool mHasControl; // true if this handle is controlling the effect
235 bool mEnabled; // cached enable state: needed when the effect is
236 // restored after being suspended
237 bool mDestroyed; // Set to true by destructor. Access with EffectModule
238 // mLock held
239};
240
241// the EffectChain class represents a group of effects associated to one audio session.
242// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800243// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
244// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800245// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
246// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800247// order corresponding in the effect process order. When attached to a track (session ID !=
248// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800249// it also provide it's own input buffer used by the track as accumulation buffer.
250class EffectChain : public RefBase {
251public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800252 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
253 EffectChain(ThreadBase *thread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800254 virtual ~EffectChain();
255
256 // special key used for an entry in mSuspendedEffects keyed vector
257 // corresponding to a suspend all request.
258 static const int kKeyForSuspendAll = 0;
259
260 // minimum duration during which we force calling effect process when last track on
261 // a session is stopped or removed to allow effect tail to be rendered
262 static const int kProcessTailDurationMs = 1000;
263
264 void process_l();
265
266 void lock() {
267 mLock.lock();
268 }
269 void unlock() {
270 mLock.unlock();
271 }
272
273 status_t addEffect_l(const sp<EffectModule>& handle);
274 size_t removeEffect_l(const sp<EffectModule>& handle);
275
Glenn Kastend848eb42016-03-08 13:42:11 -0800276 audio_session_t sessionId() const { return mSessionId; }
277 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800278
279 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
280 sp<EffectModule> getEffectFromId_l(int id);
281 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Glenn Kastenc56f3422014-03-21 17:53:17 -0700282 // FIXME use float to improve the dynamic range
Eric Laurentfa1e1232016-08-02 19:01:49 -0700283 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
284 void resetVolume_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800285 void setDevice_l(audio_devices_t device);
286 void setMode_l(audio_mode_t mode);
287 void setAudioSource_l(audio_source_t source);
288
289 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
290 mInBuffer = buffer;
291 mOwnInBuffer = ownsBuffer;
292 }
293 int16_t *inBuffer() const {
294 return mInBuffer;
295 }
296 void setOutBuffer(int16_t *buffer) {
297 mOutBuffer = buffer;
298 }
299 int16_t *outBuffer() const {
300 return mOutBuffer;
301 }
302
303 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
304 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
305 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
306
307 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
308 mTailBufferCount = mMaxTailBuffers; }
309 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
310 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
311
312 uint32_t strategy() const { return mStrategy; }
313 void setStrategy(uint32_t strategy)
314 { mStrategy = strategy; }
315
316 // suspend effect of the given type
317 void setEffectSuspended_l(const effect_uuid_t *type,
318 bool suspend);
319 // suspend all eligible effects
320 void setEffectSuspendedAll_l(bool suspend);
321 // check if effects should be suspend or restored when a given effect is enable or disabled
322 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
323 bool enabled);
324
325 void clearInputBuffer();
326
Eric Laurent5baf2af2013-09-12 17:37:00 -0700327 // At least one non offloadable effect in the chain is enabled
328 bool isNonOffloadableEnabled();
Eric Laurent813e2a72013-08-31 12:59:48 -0700329
Eric Laurent1b928682014-10-02 19:41:47 -0700330 void syncHalEffectsState();
331
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700332 // flags is an ORed set of audio_output_flags_t which is updated on return.
333 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
334
335 // flags is an ORed set of audio_input_flags_t which is updated on return.
336 void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
337
338 // Is this EffectChain compatible with the RAW audio flag.
339 bool isRawCompatible() const;
340
341 // Is this EffectChain compatible with the FAST audio flag.
342 bool isFastCompatible() const;
Eric Laurent4c415062016-06-17 16:14:16 -0700343
344 // isCompatibleWithThread_l() must be called with thread->mLock held
345 bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
346
Eric Laurent81784c32012-11-19 14:55:58 -0800347 void dump(int fd, const Vector<String16>& args);
348
349protected:
350 friend class AudioFlinger; // for mThread, mEffects
351 EffectChain(const EffectChain&);
352 EffectChain& operator =(const EffectChain&);
353
354 class SuspendedEffectDesc : public RefBase {
355 public:
356 SuspendedEffectDesc() : mRefCount(0) {}
357
358 int mRefCount;
359 effect_uuid_t mType;
360 wp<EffectModule> mEffect;
361 };
362
363 // get a list of effect modules to suspend when an effect of the type
364 // passed is enabled.
365 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
366
367 // get an effect module if it is currently enable
368 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
369 // true if the effect whose descriptor is passed can be suspended
370 // OEMs can modify the rules implemented in this method to exclude specific effect
371 // types or implementations from the suspend/restore mechanism.
372 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
373
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700374 void clearInputBuffer_l(const sp<ThreadBase>& thread);
Eric Laurent81784c32012-11-19 14:55:58 -0800375
Eric Laurentaaa44472014-09-12 17:41:50 -0700376 void setThread(const sp<ThreadBase>& thread);
377
Eric Laurent4c415062016-06-17 16:14:16 -0700378 wp<ThreadBase> mThread; // parent mixer thread
379 mutable Mutex mLock; // mutex protecting effect list
380 Vector< sp<EffectModule> > mEffects; // list of effect modules
381 audio_session_t mSessionId; // audio session ID
382 int16_t *mInBuffer; // chain input buffer
383 int16_t *mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800384
385 // 'volatile' here means these are accessed with atomic operations instead of mutex
386 volatile int32_t mActiveTrackCnt; // number of active tracks connected
387 volatile int32_t mTrackCnt; // number of tracks connected
388
Eric Laurent4c415062016-06-17 16:14:16 -0700389 int32_t mTailBufferCount; // current effect tail buffer count
390 int32_t mMaxTailBuffers; // maximum effect tail buffers
391 bool mOwnInBuffer; // true if the chain owns its input buffer
392 int mVolumeCtrlIdx; // index of insert effect having control over volume
393 uint32_t mLeftVolume; // previous volume on left channel
394 uint32_t mRightVolume; // previous volume on right channel
395 uint32_t mNewLeftVolume; // new volume on left channel
396 uint32_t mNewRightVolume; // new volume on right channel
397 uint32_t mStrategy; // strategy for this effect chain
398 // mSuspendedEffects lists all effects currently suspended in the chain.
399 // Use effect type UUID timelow field as key. There is no real risk of identical
400 // timeLow fields among effect type UUIDs.
401 // Updated by updateSuspendedSessions_l() only.
402 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent81784c32012-11-19 14:55:58 -0800403};