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