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>& callback, 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() override 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 protected: 254 sp<EffectHalInterface> mEffectInterface; // Effect module HAL 255 256 private: 257 258 // Maximum time allocated to effect engines to complete the turn off sequence 259 static const uint32_t MAX_DISABLE_TIME_MS = 10000; 260 261 DISALLOW_COPY_AND_ASSIGN(EffectModule); 262 263 status_t start_ll() REQUIRES(audio_utils::EffectChain_Mutex, audio_utils::EffectBase_Mutex); 264 status_t stop_ll() REQUIRES(audio_utils::EffectChain_Mutex, audio_utils::EffectBase_Mutex); 265 status_t removeEffectFromHal_l() override REQUIRES(audio_utils::EffectChain_Mutex); 266 status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode); requiredEffectBufferAccessMode()267 effect_buffer_access_e requiredEffectBufferAccessMode() const { 268 return mConfig.inputCfg.buffer.raw == mConfig.outputCfg.buffer.raw 269 ? EFFECT_BUFFER_ACCESS_WRITE : EFFECT_BUFFER_ACCESS_ACCUMULATE; 270 } 271 272 status_t setVolumeInternal_ll(uint32_t* left, uint32_t* right, 273 bool controller /* the volume controller effect of the chain */) 274 REQUIRES(audio_utils::EffectChain_Mutex, audio_utils::EffectBase_Mutex); 275 276 effect_config_t mConfig; // input and output audio configuration 277 sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL 278 sp<EffectBufferHalInterface> mOutBuffer; 279 status_t mStatus; // initialization status 280 // First handle in mHandles has highest priority and controls the effect module 281 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after 282 // sending disable command. 283 uint32_t mDisableWaitCnt; // current process() calls count during disable period. 284 bool mOffloaded; // effect is currently offloaded to the audio DSP 285 // effect has been added to this HAL input stream 286 audio_io_handle_t mCurrentHalStream = AUDIO_IO_HANDLE_NONE; 287 bool mIsOutput; // direction of the AF thread 288 289 bool mSupportsFloat; // effect supports float processing 290 sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed. 291 sp<EffectBufferHalInterface> mOutConversionBuffer; 292 uint32_t mInChannelCountRequested; 293 uint32_t mOutChannelCountRequested; 294 295 template <typename MUTEX> 296 class AutoLockReentrant { 297 public: AutoLockReentrant(MUTEX & mutex,pid_t allowedTid)298 AutoLockReentrant(MUTEX& mutex, pid_t allowedTid) ACQUIRE(audio_utils::EffectBase_Mutex) 299 : mMutex(gettid() == allowedTid ? nullptr : &mutex) 300 { 301 if (mMutex != nullptr) mMutex->lock(); 302 } RELEASE(audio_utils::EffectBase_Mutex)303 ~AutoLockReentrant() RELEASE(audio_utils::EffectBase_Mutex) { 304 if (mMutex != nullptr) mMutex->unlock(); 305 } 306 private: 307 MUTEX * const mMutex; 308 }; 309 310 static constexpr pid_t INVALID_PID = (pid_t)-1; 311 // this tid is allowed to call setVolume() without acquiring the mutex. 312 pid_t mSetVolumeReentrantTid = INVALID_PID; 313 314 // Cache the volume that has been set successfully. 315 std::optional<std::vector<uint32_t>> mVolume; 316 // Cache the volume that returned from the effect when setting volume successfully. The value 317 // here is used to indicate the volume to apply before this effect. 318 std::optional<std::vector<uint32_t>> mReturnedVolume; 319 // TODO: b/315995877, remove this debugging string after root cause 320 std::string mEffectInterfaceDebug GUARDED_BY(audio_utils::EffectChain_Mutex); 321 }; 322 323 class HwAccDeviceEffectModule : public EffectModule { 324 public: HwAccDeviceEffectModule(const sp<EffectCallbackInterface> & callback,effect_descriptor_t * desc,int id,audio_port_handle_t deviceId)325 HwAccDeviceEffectModule(const sp<EffectCallbackInterface>& callback, effect_descriptor_t *desc, 326 int id, audio_port_handle_t deviceId) : 327 EffectModule(callback, desc, id, AUDIO_SESSION_DEVICE, /* pinned */ false, deviceId) {} 328 void addEffectToHal_l() final REQUIRES(audio_utils::EffectChain_Mutex); 329 330 private: 331 status_t removeEffectFromHal_l() final REQUIRES(audio_utils::EffectChain_Mutex); 332 bool mAddedToHal = false; 333 }; 334 335 // The EffectHandle class implements the IEffect interface. It provides resources 336 // to receive parameter updates, keeps track of effect control 337 // ownership and state and has a pointer to the EffectModule object it is controlling. 338 // There is one EffectHandle object for each application controlling (or using) 339 // an effect module. 340 // The EffectHandle is obtained by calling AudioFlinger::createEffect(). 341 class EffectHandle: public IAfEffectHandle, public android::media::BnEffect { 342 public: 343 344 EffectHandle(const sp<IAfEffectBase>& effect, 345 const sp<Client>& client, 346 const sp<media::IEffectClient>& effectClient, 347 int32_t priority, bool notifyFramesProcessed, bool isInternal = false, 348 audio_utils::MutexOrder mutexOrder = audio_utils::MutexOrder::kEffectHandle_Mutex); 349 ~EffectHandle() override; 350 status_t onTransact( 351 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) final; 352 status_t initCheck() const final; 353 354 // IEffect 355 android::binder::Status enable(int32_t* _aidl_return) final; 356 android::binder::Status disable(int32_t* _aidl_return) final; 357 android::binder::Status command(int32_t cmdCode, 358 const std::vector<uint8_t>& cmdData, 359 int32_t maxResponseSize, 360 std::vector<uint8_t>* response, 361 int32_t* _aidl_return) final; 362 android::binder::Status disconnect() final; 363 android::binder::Status getCblk(media::SharedFileRegion* _aidl_return) final; 364 android::binder::Status getConfig(media::EffectConfig* _config, 365 int32_t* _aidl_return) final; 366 client()367 const sp<Client>& client() const final { return mClient; } 368 /** 369 * Checks if the handle is internal, aka created by AudioFlinger for internal needs (e.g. 370 * device effect HAL handle or device effect thread handle). 371 */ isInternal()372 virtual bool isInternal() const { return mIsInternal; } 373 asIEffect()374 sp<android::media::IEffect> asIEffect() final { 375 return sp<android::media::IEffect>::fromExisting(this); 376 } 377 378 private: 379 void disconnect(bool unpinIfLast); 380 381 // Give or take control of effect module 382 // - hasControl: true if control is given, false if removed 383 // - signal: true client app should be signaled of change, false otherwise 384 // - enabled: state of the effect when control is passed 385 void setControl(bool hasControl, bool signal, bool enabled) final; 386 void commandExecuted(uint32_t cmdCode, 387 const std::vector<uint8_t>& cmdData, 388 const std::vector<uint8_t>& replyData) final; enabled()389 bool enabled() const final { return mEnabled; } 390 void setEnabled(bool enabled) final; 391 void framesProcessed(int32_t frames) const final; 392 393 public: 394 // Getters effect()395 wp<IAfEffectBase> effect() const final { return mEffect; } id()396 int id() const final { 397 sp<IAfEffectBase> effect = mEffect.promote(); 398 if (effect == 0) { 399 return 0; 400 } 401 return effect->id(); 402 } 403 private: priority()404 int priority() const final { return mPriority; } hasControl()405 bool hasControl() const final { return mHasControl; } disconnected()406 bool disconnected() const final { return mDisconnected; } 407 408 void dumpToBuffer(char* buffer, size_t size) const final; 409 410 protected: 411 // protects IEffect method calls 412 mutable audio_utils::mutex mMutex; 413 414 private: 415 DISALLOW_COPY_AND_ASSIGN(EffectHandle); 416 mutex()417 virtual audio_utils::mutex& mutex() const 418 RETURN_CAPABILITY(android::audio_utils::EffectHandle_Mutex) { 419 return mMutex; 420 } 421 422 const wp<IAfEffectBase> mEffect; // pointer to controlled EffectModule 423 const sp<media::IEffectClient> mEffectClient; // callback interface for client notifications 424 /*const*/ sp<Client> mClient; // client for shared memory allocation, see 425 // disconnect() 426 sp<IMemory> mCblkMemory; // shared memory for control block 427 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via 428 // shared memory 429 uint8_t* mBuffer; // pointer to parameter area in shared memory 430 int mPriority; // client application priority to control the effect 431 bool mHasControl; // true if this handle is controlling the effect 432 bool mEnabled; // cached enable state: needed when the effect is 433 // restored after being suspended 434 bool mDisconnected; // Set to true by disconnect() 435 const bool mNotifyFramesProcessed; // true if the client callback event 436 // EVENT_FRAMES_PROCESSED must be generated 437 const bool mIsInternal; 438 }; 439 440 /** 441 * There are 2 types of effects: 442 * -Session Effect: handle is directly called from the client, without AudioFlinger lock. 443 * -Device Effect: a device effect proxy is aggregating a collection of internal effect handles that 444 * controls the same effect added on all audio patches involving the device effect selected port 445 * requested either by a client or by AudioPolicyEffects. These internal effect handles do not have 446 * client. Sequence flow implies a different locking order, hence the lock is specialied. 447 */ 448 class InternalEffectHandle : public EffectHandle { 449 public: InternalEffectHandle(const sp<IAfEffectBase> & effect,bool notifyFramesProcessed)450 InternalEffectHandle(const sp<IAfEffectBase>& effect, bool notifyFramesProcessed) : 451 EffectHandle(effect, /* client= */ nullptr, /* effectClient= */ nullptr, 452 /* priority= */ 0, notifyFramesProcessed, /* isInternal */ true, 453 audio_utils::MutexOrder::kDeviceEffectHandle_Mutex) {} 454 mutex()455 virtual audio_utils::mutex& mutex() const 456 RETURN_CAPABILITY(android::audio_utils::DeviceEffectHandle_Mutex) { 457 return mMutex; 458 } 459 }; 460 461 // the EffectChain class represents a group of effects associated to one audio session. 462 // There can be any number of EffectChain objects per output mixer thread (PlaybackThread). 463 // The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied 464 // to the output mix. 465 // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to 466 // tracks) are insert only. The EffectChain maintains an ordered list of effect module, the 467 // order corresponding in the effect process order. When attached to a track (session ID != 468 // AUDIO_SESSION_OUTPUT_MIX), 469 // it also provide it's own input buffer used by the track as accumulation buffer. 470 class EffectChain : public IAfEffectChain { 471 public: 472 EffectChain(const sp<IAfThreadBase>& thread, 473 audio_session_t sessionId, 474 const sp<IAfThreadCallback>& afThreadCallback); 475 476 void process_l() final REQUIRES(audio_utils::EffectChain_Mutex); 477 mutex()478 audio_utils::mutex& mutex() const final RETURN_CAPABILITY(audio_utils::EffectChain_Mutex) { 479 return mMutex; 480 } 481 482 status_t createEffect(sp<IAfEffectModule>& effect, effect_descriptor_t* desc, int id, 483 audio_session_t sessionId, bool pinned) final 484 EXCLUDES_EffectChain_Mutex; 485 status_t addEffect(const sp<IAfEffectModule>& handle) final 486 EXCLUDES_EffectChain_Mutex; 487 status_t addEffect_l(const sp<IAfEffectModule>& handle) final 488 REQUIRES(audio_utils::EffectChain_Mutex); 489 size_t removeEffect(const sp<IAfEffectModule>& handle, bool release = false) final 490 EXCLUDES_EffectChain_Mutex; 491 sessionId()492 audio_session_t sessionId() const final { return mSessionId; } setSessionId(audio_session_t sessionId)493 void setSessionId(audio_session_t sessionId) final { mSessionId = sessionId; } 494 495 sp<IAfEffectModule> getEffectFromDesc(effect_descriptor_t* descriptor) const final 496 EXCLUDES_EffectChain_Mutex; 497 sp<IAfEffectModule> getEffectFromId_l(int id) const final 498 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex; 499 sp<IAfEffectModule> getEffectFromType_l(const effect_uuid_t* type) const final 500 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex; 501 std::vector<int> getEffectIds_l() const final REQUIRES(audio_utils::ThreadBase_Mutex); 502 // FIXME use float to improve the dynamic range 503 504 bool setVolume(uint32_t* left, uint32_t* right, 505 bool force = false) final EXCLUDES_EffectChain_Mutex; 506 void resetVolume_l() final REQUIRES(audio_utils::EffectChain_Mutex); 507 void setDevices_l(const AudioDeviceTypeAddrVector& devices) final 508 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex; 509 void setInputDevice_l(const AudioDeviceTypeAddr& device) final 510 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex; 511 void setMode_l(audio_mode_t mode) final 512 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex; 513 void setAudioSource_l(audio_source_t source) final 514 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex; 515 setInBuffer(const sp<EffectBufferHalInterface> & buffer)516 void setInBuffer(const sp<EffectBufferHalInterface>& buffer) final { 517 mInBuffer = buffer; 518 } inBuffer()519 float *inBuffer() const final { 520 return mInBuffer != 0 ? reinterpret_cast<float*>(mInBuffer->ptr()) : NULL; 521 } setOutBuffer(const sp<EffectBufferHalInterface> & buffer)522 void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) final { 523 mOutBuffer = buffer; 524 } outBuffer()525 float *outBuffer() const final { 526 return mOutBuffer != 0 ? reinterpret_cast<float*>(mOutBuffer->ptr()) : NULL; 527 } incTrackCnt()528 void incTrackCnt() final { android_atomic_inc(&mTrackCnt); } decTrackCnt()529 void decTrackCnt() final { android_atomic_dec(&mTrackCnt); } trackCnt()530 int32_t trackCnt() const final { return android_atomic_acquire_load(&mTrackCnt); } 531 incActiveTrackCnt()532 void incActiveTrackCnt() final { android_atomic_inc(&mActiveTrackCnt); 533 mTailBufferCount = mMaxTailBuffers; } decActiveTrackCnt()534 void decActiveTrackCnt() final { android_atomic_dec(&mActiveTrackCnt); } activeTrackCnt()535 int32_t activeTrackCnt() const final { 536 return android_atomic_acquire_load(&mActiveTrackCnt); 537 } 538 strategy()539 product_strategy_t strategy() const final { return mStrategy; } setStrategy(product_strategy_t strategy)540 void setStrategy(product_strategy_t strategy) final 541 { mStrategy = strategy; } 542 543 // suspend or restore effects of the specified type. The number of suspend requests is counted 544 // and restore occurs once all suspend requests are cancelled. 545 void setEffectSuspended_l(const effect_uuid_t* type, bool suspend) final 546 REQUIRES(audio_utils::ThreadBase_Mutex); 547 // suspend all eligible effects 548 void setEffectSuspendedAll_l(bool suspend) final REQUIRES(audio_utils::ThreadBase_Mutex); 549 // check if effects should be suspended or restored when a given effect is enable or disabled 550 void checkSuspendOnEffectEnabled_l(const sp<IAfEffectModule>& effect, bool enabled) final 551 REQUIRES(audio_utils::ThreadBase_Mutex); 552 553 void clearInputBuffer() final EXCLUDES_EffectChain_Mutex; 554 555 // At least one non offloadable effect in the chain is enabled 556 bool isNonOffloadableEnabled() const final EXCLUDES_EffectChain_Mutex; 557 bool isNonOffloadableEnabled_l() const final REQUIRES(audio_utils::EffectChain_Mutex); 558 559 void syncHalEffectsState_l() 560 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex final; 561 562 // flags is an ORed set of audio_output_flags_t which is updated on return. 563 void checkOutputFlagCompatibility(audio_output_flags_t *flags) const final; 564 565 // flags is an ORed set of audio_input_flags_t which is updated on return. 566 void checkInputFlagCompatibility(audio_input_flags_t *flags) const final; 567 568 // Is this EffectChain compatible with the RAW audio flag. 569 bool isRawCompatible() const final; 570 571 // Is this EffectChain compatible with the FAST audio flag. 572 bool isFastCompatible() const final; 573 574 // Is this EffectChain compatible with the bit-perfect audio flag. 575 bool isBitPerfectCompatible() const final; 576 577 // isCompatibleWithThread_l() must be called with thread->mutex() held 578 bool isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const final 579 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex; 580 581 bool containsHapticGeneratingEffect() final 582 EXCLUDES_EffectChain_Mutex; 583 584 bool containsHapticGeneratingEffect_l() final 585 REQUIRES(audio_utils::EffectChain_Mutex); 586 587 void setHapticScale_l(int id, os::HapticScale hapticScale) final 588 REQUIRES(audio_utils::ThreadBase_Mutex) EXCLUDES_EffectChain_Mutex; 589 effectCallback()590 sp<EffectCallbackInterface> effectCallback() const final { return mEffectCallback; } 591 thread()592 wp<IAfThreadBase> thread() const final { return mEffectCallback->thread(); } 593 isFirstEffect_l(int id)594 bool isFirstEffect_l(int id) const final REQUIRES(audio_utils::EffectChain_Mutex) { 595 return !mEffects.isEmpty() && id == mEffects[0]->id(); 596 } 597 598 void dump(int fd, const Vector<String16>& args) const final; 599 numberOfEffects()600 size_t numberOfEffects() const final { 601 audio_utils::lock_guard _l(mutex()); 602 return mEffects.size(); 603 } 604 getEffectModule(size_t index)605 sp<IAfEffectModule> getEffectModule(size_t index) const final { 606 audio_utils::lock_guard _l(mutex()); 607 return mEffects[index]; 608 } 609 610 void sendMetadata_l(const std::vector<playback_track_metadata_v7_t>& allMetadata, 611 const std::optional<const std::vector<playback_track_metadata_v7_t>> spatializedMetadata) 612 final REQUIRES(audio_utils::ThreadBase_Mutex); 613 614 void setThread(const sp<IAfThreadBase>& thread) final EXCLUDES_EffectChain_Mutex; 615 616 private: 617 bool setVolume_l(uint32_t* left, uint32_t* right, bool force = false) 618 REQUIRES(audio_utils::EffectChain_Mutex); 619 620 // For transaction consistency, please consider holding the EffectChain lock before 621 // calling the EffectChain::EffectCallback methods, excepting 622 // createEffectHal and allocateHalBuffer. 623 // 624 // This prevents migration of the EffectChain to another PlaybackThread 625 // for the purposes of the EffectCallback. 626 class EffectCallback : public EffectCallbackInterface { 627 public: 628 // Note: ctors taking a weak pointer to their owner must not promote it 629 // during construction (but may keep a reference for later promotion). EffectCallback(const wp<EffectChain> & owner,const sp<IAfThreadBase> & thread,const sp<IAfThreadCallback> & afThreadCallback)630 EffectCallback(const wp<EffectChain>& owner, 631 const sp<IAfThreadBase>& thread, 632 const sp<IAfThreadCallback>& afThreadCallback) // we take a sp<> but store a wp<>. 633 : mChain(owner) 634 , mThread(thread), mAfThreadCallback(afThreadCallback) { 635 if (thread != nullptr) { 636 mThreadType = thread->type(); 637 } 638 } 639 640 status_t createEffectHal(const effect_uuid_t *pEffectUuid, 641 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override; 642 status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override; 643 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect) override; 644 645 audio_io_handle_t io() const override; 646 bool isOutput() const override; 647 bool isOffload() const override; 648 bool isOffloadOrDirect() const override; 649 bool isOffloadOrMmap() const override; 650 bool isSpatializer() const override; 651 652 uint32_t sampleRate() const override; 653 audio_channel_mask_t inChannelMask(int id) const override; 654 uint32_t inChannelCount(int id) const override; 655 audio_channel_mask_t outChannelMask() const override; 656 uint32_t outChannelCount() const override; 657 audio_channel_mask_t hapticChannelMask() const override; 658 size_t frameCount() const override; 659 uint32_t latency() const override; 660 661 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override; 662 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override; 663 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override; 664 void setVolumeForOutput(float left, float right) const override; 665 666 // check if effects should be suspended/restored when a given effect is enable/disabled 667 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect, bool enabled, 668 bool threadLocked) override; 669 void resetVolume_l() override 670 REQUIRES(audio_utils::ThreadBase_Mutex, audio_utils::EffectChain_Mutex); 671 product_strategy_t strategy() const override; 672 int32_t activeTrackCnt() const override; 673 void onEffectEnable(const sp<IAfEffectBase>& effect) override; 674 void onEffectDisable(const sp<IAfEffectBase>& effect) override; 675 chain()676 wp<IAfEffectChain> chain() const final { return mChain; } 677 isAudioPolicyReady()678 bool isAudioPolicyReady() const final { 679 if (mAfThreadCallback == nullptr) { 680 return false; 681 } 682 return mAfThreadCallback->isAudioPolicyReady(); 683 } 684 thread()685 wp<IAfThreadBase> thread() const { return mThread.load(); } 686 setThread(const sp<IAfThreadBase> & thread)687 void setThread(const sp<IAfThreadBase>& thread) { 688 mThread = thread; 689 if (thread != nullptr) { 690 mThreadType = thread->type(); 691 mAfThreadCallback = thread->afThreadCallback(); 692 } 693 } hasThreadAttached()694 bool hasThreadAttached() const { 695 return thread().promote() != nullptr; 696 } 697 private: 698 const wp<IAfEffectChain> mChain; 699 mediautils::atomic_wp<IAfThreadBase> mThread; 700 sp<IAfThreadCallback> mAfThreadCallback; 701 IAfThreadBase::type_t mThreadType = IAfThreadBase::MIXER; 702 }; 703 704 DISALLOW_COPY_AND_ASSIGN(EffectChain); 705 706 class SuspendedEffectDesc : public RefBase { 707 public: SuspendedEffectDesc()708 SuspendedEffectDesc() : mRefCount(0) {} 709 710 int mRefCount; // > 0 when suspended 711 effect_uuid_t mType; 712 wp<IAfEffectModule> mEffect; 713 }; 714 715 // get a list of effect modules to suspend when an effect of the type 716 // passed is enabled. 717 void getSuspendEligibleEffects(Vector<sp<IAfEffectModule>>& effects) 718 EXCLUDES_EffectChain_Mutex; 719 720 // get an effect module if it is currently enable 721 sp<IAfEffectModule> getEffectIfEnabled_l(const effect_uuid_t* type) 722 REQUIRES(audio_utils::ThreadBase_Mutex); 723 // true if the effect whose descriptor is passed can be suspended 724 // OEMs can modify the rules implemented in this method to exclude specific effect 725 // types or implementations from the suspend/restore mechanism. 726 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc); 727 728 static bool isEffectEligibleForBtNrecSuspend_l(const effect_uuid_t* type) 729 REQUIRES(audio_utils::ThreadBase_Mutex); 730 731 void clearInputBuffer_l() REQUIRES(audio_utils::EffectChain_Mutex); 732 733 // true if any effect module within the chain has volume control 734 bool hasVolumeControlEnabled_l() const REQUIRES(audio_utils::EffectChain_Mutex); 735 736 void setVolumeForOutput_l(uint32_t left, uint32_t right) 737 REQUIRES(audio_utils::EffectChain_Mutex); 738 739 ssize_t getInsertIndex_l(const effect_descriptor_t& desc) 740 REQUIRES(audio_utils::EffectChain_Mutex); 741 742 std::optional<size_t> findVolumeControl_l(size_t from, size_t to) const 743 REQUIRES(audio_utils::EffectChain_Mutex); 744 745 // mutex protecting effect list 746 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kEffectChain_Mutex}; 747 Vector<sp<IAfEffectModule>> mEffects GUARDED_BY(mutex()); // list of effect modules 748 audio_session_t mSessionId; // audio session ID 749 sp<EffectBufferHalInterface> mInBuffer; // chain input buffer 750 sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer 751 752 // 'volatile' here means these are accessed with atomic operations instead of mutex 753 volatile int32_t mActiveTrackCnt; // number of active tracks connected 754 volatile int32_t mTrackCnt; // number of tracks connected 755 756 int32_t mTailBufferCount; // current effect tail buffer count 757 int32_t mMaxTailBuffers; // maximum effect tail buffers 758 uint32_t mLeftVolume; // previous volume on left channel 759 uint32_t mRightVolume; // previous volume on right channel 760 uint32_t mNewLeftVolume; // new volume on left channel 761 uint32_t mNewRightVolume; // new volume on right channel 762 product_strategy_t mStrategy = PRODUCT_STRATEGY_NONE; // strategy for this effect chain 763 // mSuspendedEffects lists all effects currently suspended in the chain. 764 // Use effect type UUID timelow field as key. There is no real risk of identical 765 // timeLow fields among effect type UUIDs. 766 // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only. 767 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects; 768 769 const sp<EffectCallback> mEffectCallback; 770 771 wp<IAfEffectModule> mVolumeControlEffect; 772 }; 773 774 class DeviceEffectProxy : public IAfDeviceEffectProxy, public EffectBase { 775 public: DeviceEffectProxy(const AudioDeviceTypeAddr & device,const sp<DeviceEffectManagerCallback> & callback,effect_descriptor_t * desc,int id,bool notifyFramesProcessed)776 DeviceEffectProxy(const AudioDeviceTypeAddr& device, 777 const sp<DeviceEffectManagerCallback>& callback, 778 effect_descriptor_t *desc, int id, bool notifyFramesProcessed) 779 : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false), 780 mDevice(device), mManagerCallback(callback), 781 mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this), callback)), 782 mNotifyFramesProcessed(notifyFramesProcessed) {} 783 784 status_t setEnabled(bool enabled, bool fromHandle) final; asDeviceEffectProxy()785 sp<IAfDeviceEffectProxy> asDeviceEffectProxy() final { return this; } 786 787 status_t init_l(const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches) final 788 REQUIRES(audio_utils::DeviceEffectManager_Mutex) EXCLUDES_EffectBase_Mutex; 789 790 status_t onCreatePatch(audio_patch_handle_t patchHandle, 791 const IAfPatchPanel::Patch& patch) final; 792 793 status_t onUpdatePatch(audio_patch_handle_t oldPatchHandle, audio_patch_handle_t newPatchHandle, 794 const IAfPatchPanel::Patch& patch) final; 795 796 sp<IAfEffectHandle> onReleasePatch(audio_patch_handle_t patchHandle) final; 797 798 size_t removeEffect(const sp<IAfEffectModule>& effect) final; 799 800 status_t addEffectToHal(const sp<EffectHalInterface>& effect) final; 801 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) final; 802 device()803 const AudioDeviceTypeAddr& device() const final { return mDevice; }; 804 bool isOutput() const final; 805 uint32_t sampleRate() const final; 806 audio_channel_mask_t channelMask() const final; 807 uint32_t channelCount() const final; 808 809 status_t command(int32_t cmdCode, const std::vector<uint8_t>& cmdData, int32_t maxReplySize, 810 std::vector<uint8_t>* reply) final EXCLUDES_DeviceEffectProxy_ProxyMutex; 811 812 void dump2(int fd, int spaces) const final; 813 814 private: 815 816 class ProxyCallback : public EffectCallbackInterface { 817 public: 818 // Note: ctors taking a weak pointer to their owner must not promote it 819 // during construction (but may keep a reference for later promotion). ProxyCallback(const wp<DeviceEffectProxy> & owner,const sp<DeviceEffectManagerCallback> & callback)820 ProxyCallback(const wp<DeviceEffectProxy>& owner, 821 const sp<DeviceEffectManagerCallback>& callback) 822 : mProxy(owner), mManagerCallback(callback) {} 823 824 status_t createEffectHal(const effect_uuid_t *pEffectUuid, 825 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override; allocateHalBuffer(size_t size __unused,sp<EffectBufferHalInterface> * buffer __unused)826 status_t allocateHalBuffer(size_t size __unused, 827 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; } updateOrphanEffectChains(const sp<IAfEffectBase> & effect __unused)828 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) override { 829 return false; 830 } 831 io()832 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; } 833 bool isOutput() const override; isOffload()834 bool isOffload() const override { return false; } isOffloadOrDirect()835 bool isOffloadOrDirect() const override { return false; } isOffloadOrMmap()836 bool isOffloadOrMmap() const override { return false; } isSpatializer()837 bool isSpatializer() const override { return false; } 838 839 uint32_t sampleRate() const override; 840 audio_channel_mask_t inChannelMask(int id) const override; 841 uint32_t inChannelCount(int id) const override; 842 audio_channel_mask_t outChannelMask() const override; 843 uint32_t outChannelCount() const override; hapticChannelMask()844 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; } 845 /** 846 * frameCount cannot be zero. 847 */ frameCount()848 size_t frameCount() const override { return 1; } latency()849 uint32_t latency() const override { return 0; } 850 851 status_t addEffectToHal(const sp<EffectHalInterface>& effect) override; 852 status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) override; 853 854 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override; setVolumeForOutput(float left __unused,float right __unused)855 void setVolumeForOutput(float left __unused, float right __unused) const override {} 856 checkSuspendOnEffectEnabled(const sp<IAfEffectBase> & effect __unused,bool enabled __unused,bool threadLocked __unused)857 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused, 858 bool enabled __unused, bool threadLocked __unused) override {} resetVolume_l()859 void resetVolume_l() override REQUIRES(audio_utils::EffectChain_Mutex) {} strategy()860 product_strategy_t strategy() const override { return PRODUCT_STRATEGY_NONE; } activeTrackCnt()861 int32_t activeTrackCnt() const override { return 0; } 862 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) override; 863 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) override; 864 chain()865 wp<IAfEffectChain> chain() const override { return nullptr; } 866 isAudioPolicyReady()867 bool isAudioPolicyReady() const override { 868 return mManagerCallback->isAudioPolicyReady(); 869 } 870 871 int newEffectId(); 872 873 private: 874 const wp<DeviceEffectProxy> mProxy; 875 const sp<DeviceEffectManagerCallback> mManagerCallback; 876 }; 877 878 status_t checkPort(const IAfPatchPanel::Patch& patch, 879 const struct audio_port_config* port, sp<IAfEffectHandle>* handle); 880 881 const AudioDeviceTypeAddr mDevice; 882 const sp<DeviceEffectManagerCallback> mManagerCallback; 883 const sp<ProxyCallback> mMyCallback; 884 proxyMutex()885 audio_utils::mutex& proxyMutex() const 886 RETURN_CAPABILITY(android::audio_utils::DeviceEffectProxy_ProxyMutex) { 887 return mProxyMutex; 888 } 889 mutable audio_utils::mutex mProxyMutex{ 890 audio_utils::MutexOrder::kDeviceEffectProxy_ProxyMutex}; 891 std::map<audio_patch_handle_t, sp<IAfEffectHandle>> mEffectHandles; // protected by mProxyMutex 892 sp<IAfEffectModule> mHalEffect; // protected by mProxyMutex 893 struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE }; 894 const bool mNotifyFramesProcessed; 895 }; 896 897 } // namespace android 898