blob: dc29ce051722983365289f8593f91ac3f7063b6b [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,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080049 audio_session_t sessionId,
50 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -080051 virtual ~EffectModule();
52
53 enum effect_state {
54 IDLE,
55 RESTART,
56 STARTING,
57 ACTIVE,
58 STOPPING,
59 STOPPED,
60 DESTROYED
61 };
62
63 int id() const { return mId; }
64 void process();
Eric Laurentfa1e1232016-08-02 19:01:49 -070065 bool updateState();
Eric Laurent81784c32012-11-19 14:55:58 -080066 status_t command(uint32_t cmdCode,
67 uint32_t cmdSize,
68 void *pCmdData,
69 uint32_t *replySize,
70 void *pReplyData);
71
72 void reset_l();
73 status_t configure();
74 status_t init();
75 effect_state state() const {
76 return mState;
77 }
78 uint32_t status() {
79 return mStatus;
80 }
Glenn Kastend848eb42016-03-08 13:42:11 -080081 audio_session_t sessionId() const {
Eric Laurent81784c32012-11-19 14:55:58 -080082 return mSessionId;
83 }
84 status_t setEnabled(bool enabled);
85 status_t setEnabled_l(bool enabled);
86 bool isEnabled() const;
87 bool isProcessEnabled() const;
88
89 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
90 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; }
91 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
92 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; }
93 void setChain(const wp<EffectChain>& chain) { mChain = chain; }
94 void setThread(const wp<ThreadBase>& thread) { mThread = thread; }
95 const wp<ThreadBase>& thread() { return mThread; }
96
97 status_t addHandle(EffectHandle *handle);
Eric Laurentf10c7092016-12-06 17:09:56 -080098 ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080099 ssize_t removeHandle(EffectHandle *handle);
100 ssize_t removeHandle_l(EffectHandle *handle);
Eric Laurent81784c32012-11-19 14:55:58 -0800101
102 const effect_descriptor_t& desc() const { return mDescriptor; }
103 wp<EffectChain>& chain() { return mChain; }
104
105 status_t setDevice(audio_devices_t device);
106 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
107 status_t setMode(audio_mode_t mode);
108 status_t setAudioSource(audio_source_t source);
109 status_t start();
110 status_t stop();
111 void setSuspended(bool suspended);
112 bool suspended() const;
113
114 EffectHandle* controlHandle_l();
115
116 bool isPinned() const { return mPinned; }
117 void unPin() { mPinned = false; }
118 bool purgeHandles();
119 void lock() { mLock.lock(); }
120 void unlock() { mLock.unlock(); }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700121 bool isOffloadable() const
122 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
Eric Laurent4c415062016-06-17 16:14:16 -0700123 bool isImplementationSoftware() const
124 { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700125 bool isProcessImplemented() const
126 { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700127 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
128 bool isOffloaded() const;
Eric Laurent1b928682014-10-02 19:41:47 -0700129 void addEffectToHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800130 void release_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800131
132 void dump(int fd, const Vector<String16>& args);
133
134protected:
135 friend class AudioFlinger; // for mHandles
136 bool mPinned;
137
138 // Maximum time allocated to effect engines to complete the turn off sequence
139 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
140
141 EffectModule(const EffectModule&);
142 EffectModule& operator = (const EffectModule&);
143
144 status_t start_l();
145 status_t stop_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800146 status_t remove_effect_from_hal_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800147
148mutable Mutex mLock; // mutex for process, commands and handles list protection
149 wp<ThreadBase> mThread; // parent thread
150 wp<EffectChain> mChain; // parent effect chain
151 const int mId; // this instance unique ID
Glenn Kastend848eb42016-03-08 13:42:11 -0800152 const audio_session_t mSessionId; // audio session ID
Eric Laurent81784c32012-11-19 14:55:58 -0800153 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
154 effect_config_t mConfig; // input and output audio configuration
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700155 sp<EffectHalInterface> mEffectInterface; // Effect module HAL
Eric Laurent81784c32012-11-19 14:55:58 -0800156 status_t mStatus; // initialization status
157 effect_state mState; // current activation state
158 Vector<EffectHandle *> mHandles; // list of client handles
159 // First handle in mHandles has highest priority and controls the effect module
160 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
161 // sending disable command.
162 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
163 bool mSuspended; // effect is suspended: temporarily disabled by framework
Eric Laurent5baf2af2013-09-12 17:37:00 -0700164 bool mOffloaded; // effect is currently offloaded to the audio DSP
Eric Laurentaaa44472014-09-12 17:41:50 -0700165 wp<AudioFlinger> mAudioFlinger;
Eric Laurent81784c32012-11-19 14:55:58 -0800166};
167
168// The EffectHandle class implements the IEffect interface. It provides resources
169// to receive parameter updates, keeps track of effect control
170// ownership and state and has a pointer to the EffectModule object it is controlling.
171// There is one EffectHandle object for each application controlling (or using)
172// an effect module.
173// The EffectHandle is obtained by calling AudioFlinger::createEffect().
174class EffectHandle: public android::BnEffect {
175public:
176
177 EffectHandle(const sp<EffectModule>& effect,
178 const sp<AudioFlinger::Client>& client,
179 const sp<IEffectClient>& effectClient,
180 int32_t priority);
181 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800182 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800183
184 // IEffect
185 virtual status_t enable();
186 virtual status_t disable();
187 virtual status_t command(uint32_t cmdCode,
188 uint32_t cmdSize,
189 void *pCmdData,
190 uint32_t *replySize,
191 void *pReplyData);
192 virtual void disconnect();
193private:
194 void disconnect(bool unpinIfLast);
195public:
196 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
197 virtual status_t onTransact(uint32_t code, const Parcel& data,
198 Parcel* reply, uint32_t flags);
199
200
201 // Give or take control of effect module
202 // - hasControl: true if control is given, false if removed
203 // - signal: true client app should be signaled of change, false otherwise
204 // - enabled: state of the effect when control is passed
205 void setControl(bool hasControl, bool signal, bool enabled);
206 void commandExecuted(uint32_t cmdCode,
207 uint32_t cmdSize,
208 void *pCmdData,
209 uint32_t replySize,
210 void *pReplyData);
211 void setEnabled(bool enabled);
212 bool enabled() const { return mEnabled; }
213
214 // Getters
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800215 wp<EffectModule> effect() const { return mEffect; }
216 int id() const {
217 sp<EffectModule> effect = mEffect.promote();
218 if (effect == 0) {
219 return 0;
220 }
221 return effect->id();
222 }
Eric Laurent81784c32012-11-19 14:55:58 -0800223 int priority() const { return mPriority; }
224 bool hasControl() const { return mHasControl; }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800225 bool disconnected() const { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800226
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800227 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800228
229protected:
230 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
231 EffectHandle(const EffectHandle&);
232 EffectHandle& operator =(const EffectHandle&);
233
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800234 Mutex mLock; // protects IEffect method calls
235 wp<EffectModule> mEffect; // pointer to controlled EffectModule
Eric Laurent81784c32012-11-19 14:55:58 -0800236 sp<IEffectClient> mEffectClient; // callback interface for client notifications
237 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
238 sp<IMemory> mCblkMemory; // shared memory for control block
239 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
240 // shared memory
241 uint8_t* mBuffer; // pointer to parameter area in shared memory
242 int mPriority; // client application priority to control the effect
243 bool mHasControl; // true if this handle is controlling the effect
244 bool mEnabled; // cached enable state: needed when the effect is
245 // restored after being suspended
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800246 bool mDisconnected; // Set to true by disconnect()
Eric Laurent81784c32012-11-19 14:55:58 -0800247};
248
249// the EffectChain class represents a group of effects associated to one audio session.
250// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800251// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
252// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800253// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
254// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800255// order corresponding in the effect process order. When attached to a track (session ID !=
256// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800257// it also provide it's own input buffer used by the track as accumulation buffer.
258class EffectChain : public RefBase {
259public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800260 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
261 EffectChain(ThreadBase *thread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800262 virtual ~EffectChain();
263
264 // special key used for an entry in mSuspendedEffects keyed vector
265 // corresponding to a suspend all request.
266 static const int kKeyForSuspendAll = 0;
267
268 // minimum duration during which we force calling effect process when last track on
269 // a session is stopped or removed to allow effect tail to be rendered
270 static const int kProcessTailDurationMs = 1000;
271
272 void process_l();
273
274 void lock() {
275 mLock.lock();
276 }
277 void unlock() {
278 mLock.unlock();
279 }
280
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800281 status_t createEffect_l(sp<EffectModule>& effect,
282 ThreadBase *thread,
283 effect_descriptor_t *desc,
284 int id,
285 audio_session_t sessionId,
286 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800287 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800288 status_t addEffect_ll(const sp<EffectModule>& handle);
289 size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
Eric Laurent81784c32012-11-19 14:55:58 -0800290
Glenn Kastend848eb42016-03-08 13:42:11 -0800291 audio_session_t sessionId() const { return mSessionId; }
292 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800293
294 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
295 sp<EffectModule> getEffectFromId_l(int id);
296 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Glenn Kastenc56f3422014-03-21 17:53:17 -0700297 // FIXME use float to improve the dynamic range
Eric Laurentfa1e1232016-08-02 19:01:49 -0700298 bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
299 void resetVolume_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800300 void setDevice_l(audio_devices_t device);
301 void setMode_l(audio_mode_t mode);
302 void setAudioSource_l(audio_source_t source);
303
304 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
305 mInBuffer = buffer;
306 mOwnInBuffer = ownsBuffer;
307 }
308 int16_t *inBuffer() const {
309 return mInBuffer;
310 }
311 void setOutBuffer(int16_t *buffer) {
312 mOutBuffer = buffer;
313 }
314 int16_t *outBuffer() const {
315 return mOutBuffer;
316 }
317
318 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
319 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
320 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
321
322 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
323 mTailBufferCount = mMaxTailBuffers; }
324 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
325 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
326
327 uint32_t strategy() const { return mStrategy; }
328 void setStrategy(uint32_t strategy)
329 { mStrategy = strategy; }
330
331 // suspend effect of the given type
332 void setEffectSuspended_l(const effect_uuid_t *type,
333 bool suspend);
334 // suspend all eligible effects
335 void setEffectSuspendedAll_l(bool suspend);
336 // check if effects should be suspend or restored when a given effect is enable or disabled
337 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
338 bool enabled);
339
340 void clearInputBuffer();
341
Eric Laurent5baf2af2013-09-12 17:37:00 -0700342 // At least one non offloadable effect in the chain is enabled
343 bool isNonOffloadableEnabled();
Eric Laurent813e2a72013-08-31 12:59:48 -0700344
Eric Laurent1b928682014-10-02 19:41:47 -0700345 void syncHalEffectsState();
346
Andy Hungd3bb0ad2016-10-11 17:16:43 -0700347 // flags is an ORed set of audio_output_flags_t which is updated on return.
348 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
349
350 // flags is an ORed set of audio_input_flags_t which is updated on return.
351 void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
352
353 // Is this EffectChain compatible with the RAW audio flag.
354 bool isRawCompatible() const;
355
356 // Is this EffectChain compatible with the FAST audio flag.
357 bool isFastCompatible() const;
Eric Laurent4c415062016-06-17 16:14:16 -0700358
359 // isCompatibleWithThread_l() must be called with thread->mLock held
360 bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
361
Eric Laurent81784c32012-11-19 14:55:58 -0800362 void dump(int fd, const Vector<String16>& args);
363
364protected:
365 friend class AudioFlinger; // for mThread, mEffects
366 EffectChain(const EffectChain&);
367 EffectChain& operator =(const EffectChain&);
368
369 class SuspendedEffectDesc : public RefBase {
370 public:
371 SuspendedEffectDesc() : mRefCount(0) {}
372
373 int mRefCount;
374 effect_uuid_t mType;
375 wp<EffectModule> mEffect;
376 };
377
378 // get a list of effect modules to suspend when an effect of the type
379 // passed is enabled.
380 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
381
382 // get an effect module if it is currently enable
383 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
384 // true if the effect whose descriptor is passed can be suspended
385 // OEMs can modify the rules implemented in this method to exclude specific effect
386 // types or implementations from the suspend/restore mechanism.
387 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
388
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700389 void clearInputBuffer_l(const sp<ThreadBase>& thread);
Eric Laurent81784c32012-11-19 14:55:58 -0800390
Eric Laurentaaa44472014-09-12 17:41:50 -0700391 void setThread(const sp<ThreadBase>& thread);
392
Eric Laurent4c415062016-06-17 16:14:16 -0700393 wp<ThreadBase> mThread; // parent mixer thread
394 mutable Mutex mLock; // mutex protecting effect list
395 Vector< sp<EffectModule> > mEffects; // list of effect modules
396 audio_session_t mSessionId; // audio session ID
397 int16_t *mInBuffer; // chain input buffer
398 int16_t *mOutBuffer; // chain output buffer
Eric Laurent81784c32012-11-19 14:55:58 -0800399
400 // 'volatile' here means these are accessed with atomic operations instead of mutex
401 volatile int32_t mActiveTrackCnt; // number of active tracks connected
402 volatile int32_t mTrackCnt; // number of tracks connected
403
Eric Laurent4c415062016-06-17 16:14:16 -0700404 int32_t mTailBufferCount; // current effect tail buffer count
405 int32_t mMaxTailBuffers; // maximum effect tail buffers
406 bool mOwnInBuffer; // true if the chain owns its input buffer
407 int mVolumeCtrlIdx; // index of insert effect having control over volume
408 uint32_t mLeftVolume; // previous volume on left channel
409 uint32_t mRightVolume; // previous volume on right channel
410 uint32_t mNewLeftVolume; // new volume on left channel
411 uint32_t mNewRightVolume; // new volume on right channel
412 uint32_t mStrategy; // strategy for this effect chain
413 // mSuspendedEffects lists all effects currently suspended in the chain.
414 // Use effect type UUID timelow field as key. There is no real risk of identical
415 // timeLow fields among effect type UUIDs.
416 // Updated by updateSuspendedSessions_l() only.
417 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurent81784c32012-11-19 14:55:58 -0800418};