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