1 /*
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 // 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.
26 class EffectCallbackInterface : public RefBase {
27 public:
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;
36     virtual uint32_t sampleRate() const = 0;
37     virtual audio_channel_mask_t channelMask() const = 0;
38     virtual uint32_t channelCount() const = 0;
39     virtual size_t frameCount() const = 0;
40 
41     // Non trivial methods usually implemented with help from ThreadBase:
42     //   pay attention to mutex locking order
latency()43     virtual uint32_t latency() const { return 0; }
44     virtual status_t addEffectToHal(sp<EffectHalInterface> effect) = 0;
45     virtual status_t removeEffectFromHal(sp<EffectHalInterface> effect) = 0;
46     virtual void setVolumeForOutput(float left, float right) const = 0;
47     virtual bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) = 0;
48     virtual void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
49                                              bool enabled,
50                                              bool threadLocked) = 0;
51     virtual void onEffectEnable(const sp<EffectBase>& effect) = 0;
52     virtual void onEffectDisable(const sp<EffectBase>& effect) = 0;
53 
54     // Methods usually implemented with help from AudioFlinger: pay attention to mutex locking order
55     virtual status_t createEffectHal(const effect_uuid_t *pEffectUuid,
56                     int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) = 0;
57     virtual status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) = 0;
58     virtual bool updateOrphanEffectChains(const sp<EffectBase>& effect) = 0;
59 
60     // Methods usually implemented with help from EffectChain: pay attention to mutex locking order
61     virtual uint32_t strategy() const = 0;
62     virtual int32_t activeTrackCnt() const = 0;
63     virtual void resetVolume() = 0;
64 
65     virtual wp<EffectChain> chain() const = 0;
66 };
67 
68 // EffectBase(EffectModule) and EffectChain classes both have their own mutex to protect
69 // state changes or resource modifications. Always respect the following order
70 // if multiple mutexes must be acquired to avoid cross deadlock:
71 // AudioFlinger -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
72 // AudioHandle -> ThreadBase -> EffectChain -> EffectBase(EffectModule)
73 
74 // NOTE: When implementing the EffectCallbackInterface, in an EffectChain or other, it is important
75 // to pay attention to this locking order as some callback methods can be called from a state where
76 // EffectModule and/or EffectChain mutexes are held.
77 
78 // In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
79 // startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or
80 // Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService
81 // methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order.
82 
83 
84 // The EffectBase class contains common properties, state and behavior for and EffectModule or
85 // other derived classes managing an audio effect instance within the effect framework.
86 // It also contains the class mutex (see comment on locking order above).
87 class EffectBase : public RefBase {
88 public:
89     EffectBase(const sp<EffectCallbackInterface>& callback,
90                effect_descriptor_t *desc,
91                int id,
92                audio_session_t sessionId,
93                bool pinned);
94 
95     ~EffectBase() override = default;
96 
97     enum effect_state {
98         IDLE,
99         RESTART,
100         STARTING,
101         ACTIVE,
102         STOPPING,
103         STOPPED,
104         DESTROYED
105     };
106 
id()107     int id() const { return mId; }
state()108     effect_state state() const {
109         return mState;
110     }
sessionId()111     audio_session_t sessionId() const {
112         return mSessionId;
113     }
desc()114     const effect_descriptor_t& desc() const { return mDescriptor; }
isOffloadable()115     bool             isOffloadable() const
116                         { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
isImplementationSoftware()117     bool             isImplementationSoftware() const
118                         { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; }
isProcessImplemented()119     bool             isProcessImplemented() const
120                         { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; }
isVolumeControl()121     bool             isVolumeControl() const
122                         { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
123                             == EFFECT_FLAG_VOLUME_CTRL; }
isVolumeMonitor()124     bool             isVolumeMonitor() const
125                         { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK)
126                             == EFFECT_FLAG_VOLUME_MONITOR; }
127 
128     virtual status_t setEnabled(bool enabled, bool fromHandle);
129     status_t    setEnabled_l(bool enabled);
130     bool isEnabled() const;
131 
132     void             setSuspended(bool suspended);
133     bool             suspended() const;
134 
command(uint32_t cmdCode __unused,uint32_t cmdSize __unused,void * pCmdData __unused,uint32_t * replySize __unused,void * pReplyData __unused)135     virtual status_t command(uint32_t cmdCode __unused,
136                  uint32_t cmdSize __unused,
137                  void *pCmdData __unused,
138                  uint32_t *replySize __unused,
139                  void *pReplyData __unused) { return NO_ERROR; };
140 
setCallback(const sp<EffectCallbackInterface> & callback)141     void setCallback(const sp<EffectCallbackInterface>& callback) { mCallback = callback; }
callback()142     sp<EffectCallbackInterface>&     callback() { return mCallback; }
143 
144     status_t addHandle(EffectHandle *handle);
145     ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast);
146     ssize_t removeHandle(EffectHandle *handle);
147     virtual ssize_t removeHandle_l(EffectHandle *handle);
148     EffectHandle* controlHandle_l();
149     bool purgeHandles();
150 
151     void             checkSuspendOnEffectEnabled(bool enabled, bool threadLocked);
152 
isPinned()153     bool             isPinned() const { return mPinned; }
unPin()154     void             unPin() { mPinned = false; }
155 
lock()156     void             lock() { mLock.lock(); }
unlock()157     void             unlock() { mLock.unlock(); }
158 
159     status_t         updatePolicyState();
160 
asEffectModule()161     virtual          sp<EffectModule> asEffectModule() { return nullptr; }
asDeviceEffectProxy()162     virtual          sp<DeviceEffectProxy> asDeviceEffectProxy() { return nullptr; }
163 
164     void             dump(int fd, const Vector<String16>& args);
165 
166 private:
167     friend class AudioFlinger;      // for mHandles
168     bool             mPinned = false;
169 
170     DISALLOW_COPY_AND_ASSIGN(EffectBase);
171 
172 mutable Mutex                 mLock;      // mutex for process, commands and handles list protection
173     sp<EffectCallbackInterface> mCallback; // parent effect chain
174     const int                 mId;        // this instance unique ID
175     const audio_session_t     mSessionId; // audio session ID
176     const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
177     effect_state              mState = IDLE; // current activation state
178     // effect is suspended: temporarily disabled by framework
179     bool                      mSuspended = false;
180 
181     Vector<EffectHandle *>    mHandles;   // list of client handles
182                 // First handle in mHandles has highest priority and controls the effect module
183 
184     // Audio policy effect state management
185     // Mutex protecting transactions with audio policy manager as mLock cannot
186     // be held to avoid cross deadlocks with audio policy mutex
187     Mutex                     mPolicyLock;
188     // Effect is registered in APM or not
189     bool                      mPolicyRegistered = false;
190     // Effect enabled state communicated to APM. Enabled state corresponds to
191     // state requested by the EffectHandle with control
192     bool                      mPolicyEnabled = false;
193 };
194 
195 // The EffectModule class is a wrapper object controlling the effect engine implementation
196 // in the effect library. It prevents concurrent calls to process() and command() functions
197 // from different client threads. It keeps a list of EffectHandle objects corresponding
198 // to all client applications using this effect and notifies applications of effect state,
199 // control or parameter changes. It manages the activation state machine to send appropriate
200 // reset, enable, disable commands to effect engine and provide volume
201 // ramping when effects are activated/deactivated.
202 // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
203 // the attached track(s) to accumulate their auxiliary channel.
204 class EffectModule : public EffectBase {
205 public:
206     EffectModule(const sp<EffectCallbackInterface>& callabck,
207                     effect_descriptor_t *desc,
208                     int id,
209                     audio_session_t sessionId,
210                     bool pinned,
211                     audio_port_handle_t deviceId);
212     virtual ~EffectModule();
213 
214     void process();
215     bool updateState();
216     status_t command(uint32_t cmdCode,
217                      uint32_t cmdSize,
218                      void *pCmdData,
219                      uint32_t *replySize,
220                      void *pReplyData) override;
221 
222     void reset_l();
223     status_t configure();
224     status_t init();
225 
status()226     uint32_t status() {
227         return mStatus;
228     }
229 
230     bool isProcessEnabled() const;
231     bool isOffloadedOrDirect() const;
232     bool isVolumeControlEnabled() const;
233 
234     void        setInBuffer(const sp<EffectBufferHalInterface>& buffer);
inBuffer()235     int16_t     *inBuffer() const {
236         return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL;
237     }
238     void        setOutBuffer(const sp<EffectBufferHalInterface>& buffer);
outBuffer()239     int16_t     *outBuffer() const {
240         return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL;
241     }
242 
243     ssize_t removeHandle_l(EffectHandle *handle) override;
244 
245     status_t         setDevices(const AudioDeviceTypeAddrVector &devices);
246     status_t         setInputDevice(const AudioDeviceTypeAddr &device);
247     status_t         setVolume(uint32_t *left, uint32_t *right, bool controller);
248     status_t         setMode(audio_mode_t mode);
249     status_t         setAudioSource(audio_source_t source);
250     status_t         start();
251     status_t         stop();
252 
253     status_t         setOffloaded(bool offloaded, audio_io_handle_t io);
254     bool             isOffloaded() const;
255     void             addEffectToHal_l();
256     void             release_l();
257 
asEffectModule()258     sp<EffectModule> asEffectModule() override { return this; }
259 
260     void             dump(int fd, const Vector<String16>& args);
261 
262 private:
263     friend class AudioFlinger;      // for mHandles
264 
265     // Maximum time allocated to effect engines to complete the turn off sequence
266     static const uint32_t MAX_DISABLE_TIME_MS = 10000;
267 
268     DISALLOW_COPY_AND_ASSIGN(EffectModule);
269 
270     status_t start_l();
271     status_t stop_l();
272     status_t removeEffectFromHal_l();
273     status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode);
274 
275     effect_config_t     mConfig;    // input and output audio configuration
276     sp<EffectHalInterface> mEffectInterface; // Effect module HAL
277     sp<EffectBufferHalInterface> mInBuffer;  // Buffers for interacting with HAL
278     sp<EffectBufferHalInterface> mOutBuffer;
279     status_t            mStatus;    // initialization status
280                 // First handle in mHandles has highest priority and controls the effect module
281     uint32_t mMaxDisableWaitCnt;    // maximum grace period before forcing an effect off after
282                                     // sending disable command.
283     uint32_t mDisableWaitCnt;       // current process() calls count during disable period.
284     bool     mOffloaded;            // effect is currently offloaded to the audio DSP
285 
286 #ifdef FLOAT_EFFECT_CHAIN
287     bool    mSupportsFloat;         // effect supports float processing
288     sp<EffectBufferHalInterface> mInConversionBuffer;  // Buffers for HAL conversion if needed.
289     sp<EffectBufferHalInterface> mOutConversionBuffer;
290     uint32_t mInChannelCountRequested;
291     uint32_t mOutChannelCountRequested;
292 #endif
293 
294     class AutoLockReentrant {
295     public:
AutoLockReentrant(Mutex & mutex,pid_t allowedTid)296         AutoLockReentrant(Mutex& mutex, pid_t allowedTid)
297             : mMutex(gettid() == allowedTid ? nullptr : &mutex)
298         {
299             if (mMutex != nullptr) mMutex->lock();
300         }
~AutoLockReentrant()301         ~AutoLockReentrant() {
302             if (mMutex != nullptr) mMutex->unlock();
303         }
304     private:
305         Mutex * const mMutex;
306     };
307 
308     static constexpr pid_t INVALID_PID = (pid_t)-1;
309     // this tid is allowed to call setVolume() without acquiring the mutex.
310     pid_t mSetVolumeReentrantTid = INVALID_PID;
311 };
312 
313 // The EffectHandle class implements the IEffect interface. It provides resources
314 // to receive parameter updates, keeps track of effect control
315 // ownership and state and has a pointer to the EffectModule object it is controlling.
316 // There is one EffectHandle object for each application controlling (or using)
317 // an effect module.
318 // The EffectHandle is obtained by calling AudioFlinger::createEffect().
319 class EffectHandle: public android::BnEffect {
320 public:
321 
322     EffectHandle(const sp<EffectBase>& effect,
323             const sp<AudioFlinger::Client>& client,
324             const sp<IEffectClient>& effectClient,
325             int32_t priority);
326     virtual ~EffectHandle();
327     virtual status_t initCheck();
328 
329     // IEffect
330     virtual status_t enable();
331     virtual status_t disable();
332     virtual status_t command(uint32_t cmdCode,
333                              uint32_t cmdSize,
334                              void *pCmdData,
335                              uint32_t *replySize,
336                              void *pReplyData);
337     virtual void disconnect();
338 private:
339             void disconnect(bool unpinIfLast);
340 public:
getCblk()341     virtual sp<IMemory> getCblk() const { return mCblkMemory; }
342     virtual status_t onTransact(uint32_t code, const Parcel& data,
343             Parcel* reply, uint32_t flags);
344 
345 
346     // Give or take control of effect module
347     // - hasControl: true if control is given, false if removed
348     // - signal: true client app should be signaled of change, false otherwise
349     // - enabled: state of the effect when control is passed
350     void setControl(bool hasControl, bool signal, bool enabled);
351     void commandExecuted(uint32_t cmdCode,
352                          uint32_t cmdSize,
353                          void *pCmdData,
354                          uint32_t replySize,
355                          void *pReplyData);
356     void setEnabled(bool enabled);
enabled()357     bool enabled() const { return mEnabled; }
358 
359     // Getters
effect()360     wp<EffectBase> effect() const { return mEffect; }
id()361     int id() const {
362         sp<EffectBase> effect = mEffect.promote();
363         if (effect == 0) {
364             return 0;
365         }
366         return effect->id();
367     }
priority()368     int priority() const { return mPriority; }
hasControl()369     bool hasControl() const { return mHasControl; }
disconnected()370     bool disconnected() const { return mDisconnected; }
371 
372     void dumpToBuffer(char* buffer, size_t size);
373 
374 private:
375     friend class AudioFlinger;          // for mEffect, mHasControl, mEnabled
376     DISALLOW_COPY_AND_ASSIGN(EffectHandle);
377 
378     Mutex mLock;                        // protects IEffect method calls
379     wp<EffectBase> mEffect;           // pointer to controlled EffectModule
380     sp<IEffectClient> mEffectClient;    // callback interface for client notifications
381     /*const*/ sp<Client> mClient;       // client for shared memory allocation, see disconnect()
382     sp<IMemory>         mCblkMemory;    // shared memory for control block
383     effect_param_cblk_t* mCblk;         // control block for deferred parameter setting via
384                                         // shared memory
385     uint8_t*            mBuffer;        // pointer to parameter area in shared memory
386     int mPriority;                      // client application priority to control the effect
387     bool mHasControl;                   // true if this handle is controlling the effect
388     bool mEnabled;                      // cached enable state: needed when the effect is
389                                         // restored after being suspended
390     bool mDisconnected;                 // Set to true by disconnect()
391 };
392 
393 // the EffectChain class represents a group of effects associated to one audio session.
394 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
395 // The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied
396 // to the output mix.
397 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
398 // tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
399 // order corresponding in the effect process order. When attached to a track (session ID !=
400 // AUDIO_SESSION_OUTPUT_MIX),
401 // it also provide it's own input buffer used by the track as accumulation buffer.
402 class EffectChain : public RefBase {
403 public:
404     EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId);
405     virtual ~EffectChain();
406 
407     // special key used for an entry in mSuspendedEffects keyed vector
408     // corresponding to a suspend all request.
409     static const int        kKeyForSuspendAll = 0;
410 
411     // minimum duration during which we force calling effect process when last track on
412     // a session is stopped or removed to allow effect tail to be rendered
413     static const int        kProcessTailDurationMs = 1000;
414 
415     void process_l();
416 
lock()417     void lock() {
418         mLock.lock();
419     }
unlock()420     void unlock() {
421         mLock.unlock();
422     }
423 
424     status_t createEffect_l(sp<EffectModule>& effect,
425                             effect_descriptor_t *desc,
426                             int id,
427                             audio_session_t sessionId,
428                             bool pinned);
429     status_t addEffect_l(const sp<EffectModule>& handle);
430     status_t addEffect_ll(const sp<EffectModule>& handle);
431     size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false);
432 
sessionId()433     audio_session_t sessionId() const { return mSessionId; }
setSessionId(audio_session_t sessionId)434     void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; }
435 
436     sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
437     sp<EffectModule> getEffectFromId_l(int id);
438     sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
439     std::vector<int> getEffectIds();
440     // FIXME use float to improve the dynamic range
441     bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false);
442     void resetVolume_l();
443     void setDevices_l(const AudioDeviceTypeAddrVector &devices);
444     void setInputDevice_l(const AudioDeviceTypeAddr &device);
445     void setMode_l(audio_mode_t mode);
446     void setAudioSource_l(audio_source_t source);
447 
setInBuffer(const sp<EffectBufferHalInterface> & buffer)448     void setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
449         mInBuffer = buffer;
450     }
inBuffer()451     effect_buffer_t *inBuffer() const {
452         return mInBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mInBuffer->ptr()) : NULL;
453     }
setOutBuffer(const sp<EffectBufferHalInterface> & buffer)454     void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
455         mOutBuffer = buffer;
456     }
outBuffer()457     effect_buffer_t *outBuffer() const {
458         return mOutBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mOutBuffer->ptr()) : NULL;
459     }
460 
incTrackCnt()461     void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
decTrackCnt()462     void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
trackCnt()463     int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
464 
incActiveTrackCnt()465     void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
466                                mTailBufferCount = mMaxTailBuffers; }
decActiveTrackCnt()467     void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
activeTrackCnt()468     int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
469 
strategy()470     uint32_t strategy() const { return mStrategy; }
setStrategy(uint32_t strategy)471     void setStrategy(uint32_t strategy)
472             { mStrategy = strategy; }
473 
474     // suspend or restore effects of the specified type. The number of suspend requests is counted
475     // and restore occurs once all suspend requests are cancelled.
476     void setEffectSuspended_l(const effect_uuid_t *type,
477                               bool suspend);
478     // suspend all eligible effects
479     void setEffectSuspendedAll_l(bool suspend);
480     // check if effects should be suspended or restored when a given effect is enable or disabled
481     void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, bool enabled);
482 
483     void clearInputBuffer();
484 
485     // At least one non offloadable effect in the chain is enabled
486     bool isNonOffloadableEnabled();
487     bool isNonOffloadableEnabled_l();
488 
489     void syncHalEffectsState();
490 
491     // flags is an ORed set of audio_output_flags_t which is updated on return.
492     void checkOutputFlagCompatibility(audio_output_flags_t *flags) const;
493 
494     // flags is an ORed set of audio_input_flags_t which is updated on return.
495     void checkInputFlagCompatibility(audio_input_flags_t *flags) const;
496 
497     // Is this EffectChain compatible with the RAW audio flag.
498     bool isRawCompatible() const;
499 
500     // Is this EffectChain compatible with the FAST audio flag.
501     bool isFastCompatible() const;
502 
503     // isCompatibleWithThread_l() must be called with thread->mLock held
504     bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const;
505 
effectCallback()506     sp<EffectCallbackInterface> effectCallback() const { return mEffectCallback; }
thread()507     wp<ThreadBase> thread() const { return mEffectCallback->thread(); }
508 
509     void dump(int fd, const Vector<String16>& args);
510 
511 private:
512 
513     class EffectCallback :  public EffectCallbackInterface {
514     public:
515         // Note: ctors taking a weak pointer to their owner must not promote it
516         // during construction (but may keep a reference for later promotion).
EffectCallback(const wp<EffectChain> & owner,const wp<ThreadBase> & thread)517         EffectCallback(const wp<EffectChain>& owner,
518                        const wp<ThreadBase>& thread)
519             : mChain(owner) {
520             setThread(thread);
521         }
522 
523         status_t createEffectHal(const effect_uuid_t *pEffectUuid,
524                int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
525         status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override;
526         bool updateOrphanEffectChains(const sp<EffectBase>& effect) override;
527 
528         audio_io_handle_t io() const override;
529         bool isOutput() const override;
530         bool isOffload() const override;
531         bool isOffloadOrDirect() const override;
532         bool isOffloadOrMmap() const override;
533 
534         uint32_t sampleRate() const override;
535         audio_channel_mask_t channelMask() const override;
536         uint32_t channelCount() const override;
537         size_t frameCount() const override;
538         uint32_t latency() const override;
539 
540         status_t addEffectToHal(sp<EffectHalInterface> effect) override;
541         status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
542         bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
543         void setVolumeForOutput(float left, float right) const override;
544 
545         // check if effects should be suspended/restored when a given effect is enable/disabled
546         void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect,
547                               bool enabled, bool threadLocked) override;
548         void resetVolume() override;
549         uint32_t strategy() const override;
550         int32_t activeTrackCnt() const override;
551         void onEffectEnable(const sp<EffectBase>& effect) override;
552         void onEffectDisable(const sp<EffectBase>& effect) override;
553 
chain()554         wp<EffectChain> chain() const override { return mChain; }
555 
thread()556         wp<ThreadBase> thread() { return mThread; }
557 
setThread(const wp<ThreadBase> & thread)558         void setThread(const wp<ThreadBase>& thread) {
559             mThread = thread;
560             sp<ThreadBase> p = thread.promote();
561             mAudioFlinger = p ? p->mAudioFlinger : nullptr;
562         }
563 
564     private:
565         wp<EffectChain> mChain;
566         wp<ThreadBase> mThread;
567         wp<AudioFlinger> mAudioFlinger;
568     };
569 
570     friend class AudioFlinger;  // for mThread, mEffects
571     DISALLOW_COPY_AND_ASSIGN(EffectChain);
572 
573     class SuspendedEffectDesc : public RefBase {
574     public:
SuspendedEffectDesc()575         SuspendedEffectDesc() : mRefCount(0) {}
576 
577         int mRefCount;   // > 0 when suspended
578         effect_uuid_t mType;
579         wp<EffectModule> mEffect;
580     };
581 
582     // get a list of effect modules to suspend when an effect of the type
583     // passed is enabled.
584     void                       getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
585 
586     // get an effect module if it is currently enable
587     sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
588     // true if the effect whose descriptor is passed can be suspended
589     // OEMs can modify the rules implemented in this method to exclude specific effect
590     // types or implementations from the suspend/restore mechanism.
591     bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
592 
593     static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type);
594 
595     void clearInputBuffer_l();
596 
597     void setThread(const sp<ThreadBase>& thread);
598 
599     // true if any effect module within the chain has volume control
600     bool hasVolumeControlEnabled_l() const;
601 
602     void setVolumeForOutput_l(uint32_t left, uint32_t right);
603 
604     mutable  Mutex mLock;        // mutex protecting effect list
605              Vector< sp<EffectModule> > mEffects; // list of effect modules
606              audio_session_t mSessionId; // audio session ID
607              sp<EffectBufferHalInterface> mInBuffer;  // chain input buffer
608              sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer
609 
610     // 'volatile' here means these are accessed with atomic operations instead of mutex
611     volatile int32_t mActiveTrackCnt;    // number of active tracks connected
612     volatile int32_t mTrackCnt;          // number of tracks connected
613 
614              int32_t mTailBufferCount;   // current effect tail buffer count
615              int32_t mMaxTailBuffers;    // maximum effect tail buffers
616              int mVolumeCtrlIdx;         // index of insert effect having control over volume
617              uint32_t mLeftVolume;       // previous volume on left channel
618              uint32_t mRightVolume;      // previous volume on right channel
619              uint32_t mNewLeftVolume;       // new volume on left channel
620              uint32_t mNewRightVolume;      // new volume on right channel
621              uint32_t mStrategy; // strategy for this effect chain
622              // mSuspendedEffects lists all effects currently suspended in the chain.
623              // Use effect type UUID timelow field as key. There is no real risk of identical
624              // timeLow fields among effect type UUIDs.
625              // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only.
626              KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
627 
628              const sp<EffectCallback> mEffectCallback;
629 };
630 
631 class DeviceEffectProxy : public EffectBase {
632 public:
DeviceEffectProxy(const AudioDeviceTypeAddr & device,const sp<DeviceEffectManagerCallback> & callback,effect_descriptor_t * desc,int id)633         DeviceEffectProxy (const AudioDeviceTypeAddr& device,
634                 const sp<DeviceEffectManagerCallback>& callback,
635                 effect_descriptor_t *desc, int id)
636             : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false),
637                 mDevice(device), mManagerCallback(callback),
638                 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this),
639                                               callback)) {}
640 
641     status_t setEnabled(bool enabled, bool fromHandle) override;
asDeviceEffectProxy()642     sp<DeviceEffectProxy> asDeviceEffectProxy() override { return this; }
643 
644     status_t init(const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches);
645     status_t onCreatePatch(audio_patch_handle_t patchHandle, const PatchPanel::Patch& patch);
646     void onReleasePatch(audio_patch_handle_t patchHandle);
647 
648     size_t removeEffect(const sp<EffectModule>& effect);
649 
650     status_t addEffectToHal(sp<EffectHalInterface> effect);
651     status_t removeEffectFromHal(sp<EffectHalInterface> effect);
652 
device()653     const AudioDeviceTypeAddr& device() { return mDevice; };
654     bool isOutput() const;
655     uint32_t sampleRate() const;
656     audio_channel_mask_t channelMask() const;
657     uint32_t channelCount() const;
658 
659     void dump(int fd, int spaces);
660 
661 private:
662 
663     class ProxyCallback :  public EffectCallbackInterface {
664     public:
665         // Note: ctors taking a weak pointer to their owner must not promote it
666         // during construction (but may keep a reference for later promotion).
ProxyCallback(const wp<DeviceEffectProxy> & owner,const sp<DeviceEffectManagerCallback> & callback)667         ProxyCallback(const wp<DeviceEffectProxy>& owner,
668                 const sp<DeviceEffectManagerCallback>& callback)
669             : mProxy(owner), mManagerCallback(callback) {}
670 
671         status_t createEffectHal(const effect_uuid_t *pEffectUuid,
672                int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override;
allocateHalBuffer(size_t size __unused,sp<EffectBufferHalInterface> * buffer __unused)673         status_t allocateHalBuffer(size_t size __unused,
674                 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
updateOrphanEffectChains(const sp<EffectBase> & effect __unused)675         bool updateOrphanEffectChains(const sp<EffectBase>& effect __unused) override {
676                     return false;
677         }
678 
io()679         audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
680         bool isOutput() const override;
isOffload()681         bool isOffload() const override { return false; }
isOffloadOrDirect()682         bool isOffloadOrDirect() const override { return false; }
isOffloadOrMmap()683         bool isOffloadOrMmap() const override { return false; }
684 
685         uint32_t sampleRate() const override;
686         audio_channel_mask_t channelMask() const override;
687         uint32_t channelCount() const override;
frameCount()688         size_t frameCount() const override  { return 0; }
latency()689         uint32_t latency() const override  { return 0; }
690 
691         status_t addEffectToHal(sp<EffectHalInterface> effect) override;
692         status_t removeEffectFromHal(sp<EffectHalInterface> effect) override;
693 
694         bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override;
setVolumeForOutput(float left __unused,float right __unused)695         void setVolumeForOutput(float left __unused, float right __unused) const override {}
696 
checkSuspendOnEffectEnabled(const sp<EffectBase> & effect __unused,bool enabled __unused,bool threadLocked __unused)697         void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect __unused,
698                               bool enabled __unused, bool threadLocked __unused) override {}
resetVolume()699         void resetVolume() override {}
strategy()700         uint32_t strategy() const override  { return 0; }
activeTrackCnt()701         int32_t activeTrackCnt() const override { return 0; }
onEffectEnable(const sp<EffectBase> & effect __unused)702         void onEffectEnable(const sp<EffectBase>& effect __unused) override {}
onEffectDisable(const sp<EffectBase> & effect __unused)703         void onEffectDisable(const sp<EffectBase>& effect __unused) override {}
704 
chain()705         wp<EffectChain> chain() const override { return nullptr; }
706 
707         int newEffectId();
708 
709     private:
710         const wp<DeviceEffectProxy> mProxy;
711         const sp<DeviceEffectManagerCallback> mManagerCallback;
712     };
713 
714     status_t checkPort(const PatchPanel::Patch& patch, const struct audio_port_config *port,
715             sp<EffectHandle> *handle);
716 
717     const AudioDeviceTypeAddr mDevice;
718     const sp<DeviceEffectManagerCallback> mManagerCallback;
719     const sp<ProxyCallback> mMyCallback;
720 
721     Mutex mProxyLock;
722     std::map<audio_patch_handle_t, sp<EffectHandle>> mEffectHandles; // protected by mProxyLock
723     sp<EffectModule> mHalEffect; // protected by mProxyLock
724     struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE };
725 };
726