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