Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2019, 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 | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 18 | #pragma once |
| 19 | |
Andy Hung | 0a51b5c | 2023-07-18 20:54:44 -0700 | [diff] [blame] | 20 | #include "IAfEffect.h" |
| 21 | #include "PatchCommandThread.h" |
| 22 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 23 | namespace android { |
| 24 | |
Andy Hung | 692f045 | 2023-07-17 13:45:55 -0700 | [diff] [blame] | 25 | class IAfDeviceEffectManagerCallback : public virtual RefBase { |
| 26 | public: |
| 27 | virtual bool isAudioPolicyReady() const = 0; |
| 28 | virtual audio_unique_id_t nextUniqueId(audio_unique_id_use_t use) = 0; |
| 29 | virtual const sp<PatchCommandThread>& getPatchCommandThread() = 0; |
| 30 | virtual status_t addEffectToHal( |
Andy Hung | c2cf7ec | 2023-08-30 18:19:00 -0700 | [diff] [blame] | 31 | const struct audio_port_config* device, const sp<EffectHalInterface>& effect) |
| 32 | EXCLUDES_AudioFlinger_HardwareMutex = 0; |
Andy Hung | 692f045 | 2023-07-17 13:45:55 -0700 | [diff] [blame] | 33 | virtual status_t removeEffectFromHal( |
Andy Hung | c2cf7ec | 2023-08-30 18:19:00 -0700 | [diff] [blame] | 34 | const struct audio_port_config* device, const sp<EffectHalInterface>& effect) |
| 35 | EXCLUDES_AudioFlinger_HardwareMutex= 0; |
Andy Hung | 692f045 | 2023-07-17 13:45:55 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 38 | class DeviceEffectManagerCallback; |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 39 | |
| 40 | // DeviceEffectManager is concealed within AudioFlinger, their lifetimes are the same. |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 41 | class DeviceEffectManager : public PatchCommandThread::PatchCommandListener { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 42 | public: |
Andy Hung | 692f045 | 2023-07-17 13:45:55 -0700 | [diff] [blame] | 43 | explicit DeviceEffectManager( |
| 44 | const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 45 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 46 | void onFirstRef() override; |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 47 | |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 48 | sp<IAfEffectHandle> createEffect_l(effect_descriptor_t *descriptor, |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 49 | const AudioDeviceTypeAddr& device, |
Andy Hung | 59867e4 | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 50 | const sp<Client>& client, |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 51 | const sp<media::IEffectClient>& effectClient, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 52 | const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches, |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 53 | int *enabled, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 54 | status_t *status, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 55 | bool probe, |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 56 | bool notifyFramesProcessed) REQUIRES(audio_utils::AudioFlinger_Mutex); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 57 | |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 58 | size_t removeEffect(const sp<IAfDeviceEffectProxy>& effect); |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 59 | static status_t createEffectHal(const effect_uuid_t *pEffectUuid, |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 60 | int32_t sessionId, int32_t deviceId, |
| 61 | sp<EffectHalInterface> *effect); |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 62 | status_t addEffectToHal(const struct audio_port_config *device, |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 63 | const sp<EffectHalInterface>& effect); |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 64 | status_t removeEffectFromHal(const struct audio_port_config *device, |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 65 | const sp<EffectHalInterface>& effect); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 66 | |
Andy Hung | 692f045 | 2023-07-17 13:45:55 -0700 | [diff] [blame] | 67 | const auto& afDeviceEffectManagerCallback() const { return mAfDeviceEffectManagerCallback; } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 68 | |
| 69 | void dump(int fd); |
| 70 | |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 71 | // PatchCommandThread::PatchCommandListener implementation |
| 72 | |
| 73 | void onCreateAudioPatch(audio_patch_handle_t handle, |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 74 | const IAfPatchPanel::Patch& patch) final |
| 75 | EXCLUDES_DeviceEffectManager_Mutex; |
| 76 | void onReleaseAudioPatch(audio_patch_handle_t handle) final |
| 77 | EXCLUDES_DeviceEffectManager_Mutex; |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 78 | void onUpdateAudioPatch(audio_patch_handle_t oldHandle, |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 79 | audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) final |
| 80 | EXCLUDES_DeviceEffectManager_Mutex; |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 81 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 82 | private: |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 83 | static status_t checkEffectCompatibility(const effect_descriptor_t *desc); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 84 | |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 85 | audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::DeviceEffectManager_Mutex) { |
| 86 | return mMutex; |
| 87 | } |
Andy Hung | a6f1cbb | 2023-10-12 18:18:13 -0700 | [diff] [blame] | 88 | mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kDeviceEffectManager_Mutex}; |
Andy Hung | 692f045 | 2023-07-17 13:45:55 -0700 | [diff] [blame] | 89 | const sp<IAfDeviceEffectManagerCallback> mAfDeviceEffectManagerCallback; |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 90 | const sp<DeviceEffectManagerCallback> mMyCallback; |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 91 | std::map<AudioDeviceTypeAddr, std::vector<sp<IAfDeviceEffectProxy>>> |
| 92 | mDeviceEffects GUARDED_BY(mutex()); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 95 | class DeviceEffectManagerCallback : public EffectCallbackInterface { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 96 | public: |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 97 | explicit DeviceEffectManagerCallback(DeviceEffectManager& manager) |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 98 | : mManager(manager) {} |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 99 | |
| 100 | status_t createEffectHal(const effect_uuid_t *pEffectUuid, |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 101 | int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) final { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 102 | return mManager.createEffectHal(pEffectUuid, sessionId, deviceId, effect); |
| 103 | } |
| 104 | status_t allocateHalBuffer(size_t size __unused, |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 105 | sp<EffectBufferHalInterface>* buffer __unused) final { return NO_ERROR; } |
| 106 | bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) final { |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 109 | |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 110 | audio_io_handle_t io() const final { return AUDIO_IO_HANDLE_NONE; } |
| 111 | bool isOutput() const final { return false; } |
| 112 | bool isOffload() const final { return false; } |
| 113 | bool isOffloadOrDirect() const final { return false; } |
| 114 | bool isOffloadOrMmap() const final { return false; } |
| 115 | bool isSpatializer() const final { return false; } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 116 | |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 117 | uint32_t sampleRate() const final { return 0; } |
| 118 | audio_channel_mask_t inChannelMask(int id __unused) const final { |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 119 | return AUDIO_CHANNEL_NONE; |
| 120 | } |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 121 | uint32_t inChannelCount(int id __unused) const final { return 0; } |
| 122 | audio_channel_mask_t outChannelMask() const final { return AUDIO_CHANNEL_NONE; } |
| 123 | uint32_t outChannelCount() const final { return 0; } |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 124 | |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 125 | audio_channel_mask_t hapticChannelMask() const final { return AUDIO_CHANNEL_NONE; } |
| 126 | size_t frameCount() const final { return 0; } |
| 127 | uint32_t latency() const final { return 0; } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 128 | |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 129 | status_t addEffectToHal(const sp<EffectHalInterface>& /* effect */) final { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 130 | return NO_ERROR; |
| 131 | } |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 132 | status_t removeEffectFromHal(const sp<EffectHalInterface>& /* effect */) final { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 133 | return NO_ERROR; |
| 134 | } |
| 135 | |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 136 | bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) final; |
| 137 | void setVolumeForOutput(float left __unused, float right __unused) const final {} |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 138 | |
| 139 | // check if effects should be suspended or restored when a given effect is enable or disabled |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 140 | void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused, |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 141 | bool enabled __unused, bool threadLocked __unused) final {} |
| 142 | void resetVolume() final {} |
| 143 | product_strategy_t strategy() const final { return static_cast<product_strategy_t>(0); } |
| 144 | int32_t activeTrackCnt() const final { return 0; } |
| 145 | void onEffectEnable(const sp<IAfEffectBase>& effect __unused) final {} |
| 146 | void onEffectDisable(const sp<IAfEffectBase>& effect __unused) final {} |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 147 | |
Andy Hung | 3e07ef0 | 2023-09-06 17:37:34 -0700 | [diff] [blame] | 148 | wp<IAfEffectChain> chain() const final { return nullptr; } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 149 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 150 | bool isAudioPolicyReady() const final; |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 151 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 152 | int newEffectId() const; |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 153 | |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 154 | status_t addEffectToHal(const struct audio_port_config *device, |
| 155 | const sp<EffectHalInterface>& effect) { |
| 156 | return mManager.addEffectToHal(device, effect); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 157 | } |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 158 | status_t removeEffectFromHal(const struct audio_port_config *device, |
| 159 | const sp<EffectHalInterface>& effect) { |
| 160 | return mManager.removeEffectFromHal(device, effect); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 161 | } |
| 162 | private: |
| 163 | DeviceEffectManager& mManager; |
| 164 | }; |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 165 | |
| 166 | } // namespace android |