blob: 4afb7a43edec68a97618359ed1decfd23742be3e [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 Laurenteb3c3372013-09-25 12:25:29 -070028// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
29// startOutput()...) should never be called with AudioFlinger or Threadbase mutex locked
30// to avoid cross deadlock with other clients calling AudioPolicyService methods that in turn
31// call AudioFlinger thus locking the same mutexes in the reverse order.
Eric Laurent81784c32012-11-19 14:55:58 -080032
33// The EffectModule class is a wrapper object controlling the effect engine implementation
34// in the effect library. It prevents concurrent calls to process() and command() functions
35// from different client threads. It keeps a list of EffectHandle objects corresponding
36// to all client applications using this effect and notifies applications of effect state,
37// control or parameter changes. It manages the activation state machine to send appropriate
38// reset, enable, disable commands to effect engine and provide volume
39// ramping when effects are activated/deactivated.
40// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
41// the attached track(s) to accumulate their auxiliary channel.
42class EffectModule : public RefBase {
43public:
44 EffectModule(ThreadBase *thread,
45 const wp<AudioFlinger::EffectChain>& chain,
46 effect_descriptor_t *desc,
47 int id,
Eric Laurentb37f28a2016-12-01 15:28:29 -080048 audio_session_t sessionId,
49 bool pinned);
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();
64 void updateState();
65 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);
Eric Laurentb37f28a2016-12-01 15:28:29 -080097 ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
98 ssize_t removeHandle(EffectHandle *handle);
99 ssize_t removeHandle_l(EffectHandle *handle);
Eric Laurent81784c32012-11-19 14:55:58 -0800100
101 const effect_descriptor_t& desc() const { return mDescriptor; }
102 wp<EffectChain>& chain() { return mChain; }
103
104 status_t setDevice(audio_devices_t device);
105 status_t setVolume(uint32_t *left, uint32_t *right, bool controller);
106 status_t setMode(audio_mode_t mode);
107 status_t setAudioSource(audio_source_t source);
108 status_t start();
109 status_t stop();
110 void setSuspended(bool suspended);
111 bool suspended() const;
112
113 EffectHandle* controlHandle_l();
114
115 bool isPinned() const { return mPinned; }
116 void unPin() { mPinned = false; }
117 bool purgeHandles();
118 void lock() { mLock.lock(); }
119 void unlock() { mLock.unlock(); }
Eric Laurent5baf2af2013-09-12 17:37:00 -0700120 bool isOffloadable() const
121 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
122 status_t setOffloaded(bool offloaded, audio_io_handle_t io);
123 bool isOffloaded() const;
Eric Laurent1b928682014-10-02 19:41:47 -0700124 void addEffectToHal_l();
Eric Laurentb37f28a2016-12-01 15:28:29 -0800125 void release_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800126
127 void dump(int fd, const Vector<String16>& args);
128
129protected:
130 friend class AudioFlinger; // for mHandles
131 bool mPinned;
132
133 // Maximum time allocated to effect engines to complete the turn off sequence
134 static const uint32_t MAX_DISABLE_TIME_MS = 10000;
135
136 EffectModule(const EffectModule&);
137 EffectModule& operator = (const EffectModule&);
138
139 status_t start_l();
140 status_t stop_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800141 status_t remove_effect_from_hal_l();
Eric Laurent81784c32012-11-19 14:55:58 -0800142
143mutable Mutex mLock; // mutex for process, commands and handles list protection
144 wp<ThreadBase> mThread; // parent thread
145 wp<EffectChain> mChain; // parent effect chain
146 const int mId; // this instance unique ID
Glenn Kastend848eb42016-03-08 13:42:11 -0800147 const audio_session_t mSessionId; // audio session ID
Eric Laurent81784c32012-11-19 14:55:58 -0800148 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
149 effect_config_t mConfig; // input and output audio configuration
150 effect_handle_t mEffectInterface; // Effect module C API
151 status_t mStatus; // initialization status
152 effect_state mState; // current activation state
153 Vector<EffectHandle *> mHandles; // list of client handles
154 // First handle in mHandles has highest priority and controls the effect module
155 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after
156 // sending disable command.
157 uint32_t mDisableWaitCnt; // current process() calls count during disable period.
158 bool mSuspended; // effect is suspended: temporarily disabled by framework
Eric Laurent5baf2af2013-09-12 17:37:00 -0700159 bool mOffloaded; // effect is currently offloaded to the audio DSP
Eric Laurentaaa44472014-09-12 17:41:50 -0700160 wp<AudioFlinger> mAudioFlinger;
Eric Laurent81784c32012-11-19 14:55:58 -0800161};
162
163// The EffectHandle class implements the IEffect interface. It provides resources
164// to receive parameter updates, keeps track of effect control
165// ownership and state and has a pointer to the EffectModule object it is controlling.
166// There is one EffectHandle object for each application controlling (or using)
167// an effect module.
168// The EffectHandle is obtained by calling AudioFlinger::createEffect().
169class EffectHandle: public android::BnEffect {
170public:
171
172 EffectHandle(const sp<EffectModule>& effect,
173 const sp<AudioFlinger::Client>& client,
174 const sp<IEffectClient>& effectClient,
175 int32_t priority);
176 virtual ~EffectHandle();
Glenn Kastene75da402013-11-20 13:54:52 -0800177 virtual status_t initCheck();
Eric Laurent81784c32012-11-19 14:55:58 -0800178
179 // IEffect
180 virtual status_t enable();
181 virtual status_t disable();
182 virtual status_t command(uint32_t cmdCode,
183 uint32_t cmdSize,
184 void *pCmdData,
185 uint32_t *replySize,
186 void *pReplyData);
187 virtual void disconnect();
188private:
189 void disconnect(bool unpinIfLast);
190public:
191 virtual sp<IMemory> getCblk() const { return mCblkMemory; }
192 virtual status_t onTransact(uint32_t code, const Parcel& data,
193 Parcel* reply, uint32_t flags);
194
195
196 // Give or take control of effect module
197 // - hasControl: true if control is given, false if removed
198 // - signal: true client app should be signaled of change, false otherwise
199 // - enabled: state of the effect when control is passed
200 void setControl(bool hasControl, bool signal, bool enabled);
201 void commandExecuted(uint32_t cmdCode,
202 uint32_t cmdSize,
203 void *pCmdData,
204 uint32_t replySize,
205 void *pReplyData);
206 void setEnabled(bool enabled);
207 bool enabled() const { return mEnabled; }
208
209 // Getters
Eric Laurentb37f28a2016-12-01 15:28:29 -0800210 wp<EffectModule> effect() const { return mEffect; }
211 int id() const {
212 sp<EffectModule> effect = mEffect.promote();
213 if (effect == 0) {
214 return 0;
215 }
216 return effect->id();
217 }
Eric Laurent81784c32012-11-19 14:55:58 -0800218 int priority() const { return mPriority; }
219 bool hasControl() const { return mHasControl; }
Eric Laurentb37f28a2016-12-01 15:28:29 -0800220 bool disconnected() const { return mDisconnected; }
Eric Laurent81784c32012-11-19 14:55:58 -0800221
Glenn Kasten01d3acb2014-02-06 08:24:07 -0800222 void dumpToBuffer(char* buffer, size_t size);
Eric Laurent81784c32012-11-19 14:55:58 -0800223
224protected:
225 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled
226 EffectHandle(const EffectHandle&);
227 EffectHandle& operator =(const EffectHandle&);
228
Eric Laurentb37f28a2016-12-01 15:28:29 -0800229 Mutex mLock; // protects IEffect method calls
230 wp<EffectModule> mEffect; // pointer to controlled EffectModule
Eric Laurent81784c32012-11-19 14:55:58 -0800231 sp<IEffectClient> mEffectClient; // callback interface for client notifications
232 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect()
233 sp<IMemory> mCblkMemory; // shared memory for control block
234 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via
235 // shared memory
236 uint8_t* mBuffer; // pointer to parameter area in shared memory
237 int mPriority; // client application priority to control the effect
238 bool mHasControl; // true if this handle is controlling the effect
239 bool mEnabled; // cached enable state: needed when the effect is
240 // restored after being suspended
Eric Laurentb37f28a2016-12-01 15:28:29 -0800241 bool mDisconnected; // Set to true by disconnect()
Eric Laurent81784c32012-11-19 14:55:58 -0800242};
243
244// the EffectChain class represents a group of effects associated to one audio session.
245// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
Glenn Kastend848eb42016-03-08 13:42:11 -0800246// The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
247// to the output mix.
Eric Laurent81784c32012-11-19 14:55:58 -0800248// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
249// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
Glenn Kastend848eb42016-03-08 13:42:11 -0800250// order corresponding in the effect process order. When attached to a track (session ID !=
251// AUDIO_SESSION_OUTPUT_MIX),
Eric Laurent81784c32012-11-19 14:55:58 -0800252// it also provide it's own input buffer used by the track as accumulation buffer.
253class EffectChain : public RefBase {
254public:
Glenn Kastend848eb42016-03-08 13:42:11 -0800255 EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
256 EffectChain(ThreadBase *thread, audio_session_t sessionId);
Eric Laurent81784c32012-11-19 14:55:58 -0800257 virtual ~EffectChain();
258
259 // special key used for an entry in mSuspendedEffects keyed vector
260 // corresponding to a suspend all request.
261 static const int kKeyForSuspendAll = 0;
262
263 // minimum duration during which we force calling effect process when last track on
264 // a session is stopped or removed to allow effect tail to be rendered
265 static const int kProcessTailDurationMs = 1000;
266
267 void process_l();
268
269 void lock() {
270 mLock.lock();
271 }
272 void unlock() {
273 mLock.unlock();
274 }
275
Eric Laurentb37f28a2016-12-01 15:28:29 -0800276 status_t createEffect_l(sp<EffectModule>& effect,
277 ThreadBase *thread,
278 effect_descriptor_t *desc,
279 int id,
280 audio_session_t sessionId,
281 bool pinned);
Eric Laurent81784c32012-11-19 14:55:58 -0800282 status_t addEffect_l(const sp<EffectModule>& handle);
Eric Laurentb37f28a2016-12-01 15:28:29 -0800283 status_t addEffect_ll(const sp<EffectModule>& handle);
284 size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
Eric Laurent81784c32012-11-19 14:55:58 -0800285
Glenn Kastend848eb42016-03-08 13:42:11 -0800286 audio_session_t sessionId() const { return mSessionId; }
287 void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
Eric Laurent81784c32012-11-19 14:55:58 -0800288
289 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
290 sp<EffectModule> getEffectFromId_l(int id);
291 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
Glenn Kastenc56f3422014-03-21 17:53:17 -0700292 // FIXME use float to improve the dynamic range
Eric Laurent81784c32012-11-19 14:55:58 -0800293 bool setVolume_l(uint32_t *left, uint32_t *right);
294 void setDevice_l(audio_devices_t device);
295 void setMode_l(audio_mode_t mode);
296 void setAudioSource_l(audio_source_t source);
297
298 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
299 mInBuffer = buffer;
300 mOwnInBuffer = ownsBuffer;
301 }
302 int16_t *inBuffer() const {
303 return mInBuffer;
304 }
305 void setOutBuffer(int16_t *buffer) {
306 mOutBuffer = buffer;
307 }
308 int16_t *outBuffer() const {
309 return mOutBuffer;
310 }
311
312 void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
313 void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
314 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
315
316 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
317 mTailBufferCount = mMaxTailBuffers; }
318 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
319 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
320
321 uint32_t strategy() const { return mStrategy; }
322 void setStrategy(uint32_t strategy)
323 { mStrategy = strategy; }
324
325 // suspend effect of the given type
326 void setEffectSuspended_l(const effect_uuid_t *type,
327 bool suspend);
328 // suspend all eligible effects
329 void setEffectSuspendedAll_l(bool suspend);
330 // check if effects should be suspend or restored when a given effect is enable or disabled
331 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
332 bool enabled);
333
334 void clearInputBuffer();
335
Eric Laurent5baf2af2013-09-12 17:37:00 -0700336 // At least one non offloadable effect in the chain is enabled
337 bool isNonOffloadableEnabled();
Eric Laurent813e2a72013-08-31 12:59:48 -0700338
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700339 // use release_cas because we don't care about the observed value, just want to make sure the
340 // new value is observable.
341 void forceVolume() { android_atomic_release_cas(false, true, &mForceVolume); }
342 // use acquire_cas because we are interested in the observed value and
343 // we are the only observers.
344 bool isVolumeForced() { return (android_atomic_acquire_cas(true, false, &mForceVolume) == 0); }
Eric Laurent813e2a72013-08-31 12:59:48 -0700345
Eric Laurent1b928682014-10-02 19:41:47 -0700346 void syncHalEffectsState();
347
Eric Laurent81784c32012-11-19 14:55:58 -0800348 void dump(int fd, const Vector<String16>& args);
349
350protected:
351 friend class AudioFlinger; // for mThread, mEffects
352 EffectChain(const EffectChain&);
353 EffectChain& operator =(const EffectChain&);
354
355 class SuspendedEffectDesc : public RefBase {
356 public:
357 SuspendedEffectDesc() : mRefCount(0) {}
358
359 int mRefCount;
360 effect_uuid_t mType;
361 wp<EffectModule> mEffect;
362 };
363
364 // get a list of effect modules to suspend when an effect of the type
365 // passed is enabled.
366 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
367
368 // get an effect module if it is currently enable
369 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
370 // true if the effect whose descriptor is passed can be suspended
371 // OEMs can modify the rules implemented in this method to exclude specific effect
372 // types or implementations from the suspend/restore mechanism.
373 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
374
375 void clearInputBuffer_l(sp<ThreadBase> thread);
376
Eric Laurentaaa44472014-09-12 17:41:50 -0700377 void setThread(const sp<ThreadBase>& thread);
378
Eric Laurent81784c32012-11-19 14:55:58 -0800379 wp<ThreadBase> mThread; // parent mixer thread
380 Mutex mLock; // mutex protecting effect list
381 Vector< sp<EffectModule> > mEffects; // list of effect modules
Glenn Kastend848eb42016-03-08 13:42:11 -0800382 audio_session_t mSessionId; // audio session ID
Eric Laurent81784c32012-11-19 14:55:58 -0800383 int16_t *mInBuffer; // chain input buffer
384 int16_t *mOutBuffer; // chain output buffer
385
386 // 'volatile' here means these are accessed with atomic operations instead of mutex
387 volatile int32_t mActiveTrackCnt; // number of active tracks connected
388 volatile int32_t mTrackCnt; // number of tracks connected
389
390 int32_t mTailBufferCount; // current effect tail buffer count
391 int32_t mMaxTailBuffers; // maximum effect tail buffers
392 bool mOwnInBuffer; // true if the chain owns its input buffer
393 int mVolumeCtrlIdx; // index of insert effect having control over volume
394 uint32_t mLeftVolume; // previous volume on left channel
395 uint32_t mRightVolume; // previous volume on right channel
396 uint32_t mNewLeftVolume; // new volume on left channel
397 uint32_t mNewRightVolume; // new volume on right channel
398 uint32_t mStrategy; // strategy for this effect chain
399 // mSuspendedEffects lists all effects currently suspended in the chain.
400 // Use effect type UUID timelow field as key. There is no real risk of identical
401 // timeLow fields among effect type UUIDs.
402 // Updated by updateSuspendedSessions_l() only.
403 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
Eric Laurentcb4b6e92014-10-01 14:26:10 -0700404 volatile int32_t mForceVolume; // force next volume command because a new effect was enabled
Eric Laurent81784c32012-11-19 14:55:58 -0800405};