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