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