| 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 |  | 
|  | 18 | #ifndef INCLUDING_FROM_AUDIOFLINGER_H | 
|  | 19 | #error This header file should only be included from AudioFlinger.h | 
|  | 20 | #endif | 
|  | 21 |  | 
|  | 22 | // DeviceEffectManager is concealed within AudioFlinger, their lifetimes are the same. | 
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 23 | class DeviceEffectManager : public PatchCommandThread::PatchCommandListener { | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 24 | public: | 
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 25 | explicit DeviceEffectManager(AudioFlinger& audioFlinger) | 
|  | 26 | : mAudioFlinger(audioFlinger), | 
|  | 27 | mMyCallback(new DeviceEffectManagerCallback(*this)) {} | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 28 |  | 
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 29 | void onFirstRef() override { | 
|  | 30 | mAudioFlinger.mPatchCommandThread->addListener(this); | 
|  | 31 | } | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 32 |  | 
|  | 33 | sp<EffectHandle> createEffect_l(effect_descriptor_t *descriptor, | 
|  | 34 | const AudioDeviceTypeAddr& device, | 
|  | 35 | const sp<AudioFlinger::Client>& client, | 
| Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 36 | const sp<media::IEffectClient>& effectClient, | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 37 | const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches, | 
|  | 38 | int *enabled, | 
| Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 39 | status_t *status, | 
| Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 40 | bool probe, | 
|  | 41 | bool notifyFramesProcessed); | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 42 |  | 
|  | 43 | size_t removeEffect(const sp<DeviceEffectProxy>& effect); | 
|  | 44 | status_t createEffectHal(const effect_uuid_t *pEffectUuid, | 
|  | 45 | int32_t sessionId, int32_t deviceId, | 
|  | 46 | sp<EffectHalInterface> *effect); | 
| Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 47 | status_t addEffectToHal(const struct audio_port_config *device, | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 48 | const sp<EffectHalInterface>& effect) { | 
| Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 49 | return mAudioFlinger.addEffectToHal(device, effect); | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 50 | }; | 
| Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 51 | status_t removeEffectFromHal(const struct audio_port_config *device, | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 52 | const sp<EffectHalInterface>& effect) { | 
| Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 53 | return mAudioFlinger.removeEffectFromHal(device, effect); | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 54 | }; | 
|  | 55 |  | 
|  | 56 | AudioFlinger& audioFlinger() const { return mAudioFlinger; } | 
|  | 57 |  | 
|  | 58 | void dump(int fd); | 
|  | 59 |  | 
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 60 | // PatchCommandThread::PatchCommandListener implementation | 
|  | 61 |  | 
|  | 62 | void onCreateAudioPatch(audio_patch_handle_t handle, | 
|  | 63 | const PatchPanel::Patch& patch) override; | 
|  | 64 | void onReleaseAudioPatch(audio_patch_handle_t handle) override; | 
|  | 65 |  | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 66 | private: | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 67 | status_t checkEffectCompatibility(const effect_descriptor_t *desc); | 
|  | 68 |  | 
|  | 69 | Mutex mLock; | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 70 | AudioFlinger &mAudioFlinger; | 
|  | 71 | const sp<DeviceEffectManagerCallback> mMyCallback; | 
|  | 72 | std::map<AudioDeviceTypeAddr, sp<DeviceEffectProxy>> mDeviceEffects; | 
|  | 73 | }; | 
|  | 74 |  | 
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 75 | class DeviceEffectManagerCallback : public EffectCallbackInterface { | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 76 | public: | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 77 | explicit DeviceEffectManagerCallback(DeviceEffectManager& manager) | 
| Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 78 | : mManager(manager) {} | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | status_t createEffectHal(const effect_uuid_t *pEffectUuid, | 
|  | 81 | int32_t sessionId, int32_t deviceId, | 
|  | 82 | sp<EffectHalInterface> *effect) override { | 
|  | 83 | return mManager.createEffectHal(pEffectUuid, sessionId, deviceId, effect); | 
|  | 84 | } | 
|  | 85 | status_t allocateHalBuffer(size_t size __unused, | 
|  | 86 | sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; } | 
|  | 87 | bool updateOrphanEffectChains(const sp<EffectBase>& effect __unused) override { return false; } | 
|  | 88 |  | 
|  | 89 | audio_io_handle_t io() const override  { return AUDIO_IO_HANDLE_NONE; } | 
|  | 90 | bool isOutput() const override { return false; } | 
|  | 91 | bool isOffload() const override { return false; } | 
|  | 92 | bool isOffloadOrDirect() const override { return false; } | 
|  | 93 | bool isOffloadOrMmap() const override { return false; } | 
| Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 94 | bool isSpatializer() const override { return false; } | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 95 |  | 
|  | 96 | uint32_t  sampleRate() const override { return 0; } | 
| Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 97 | audio_channel_mask_t inChannelMask(int id __unused) const override { | 
|  | 98 | return AUDIO_CHANNEL_NONE; | 
|  | 99 | } | 
|  | 100 | uint32_t inChannelCount(int id __unused) const override { return 0; } | 
|  | 101 | audio_channel_mask_t outChannelMask() const override { return AUDIO_CHANNEL_NONE; } | 
|  | 102 | uint32_t outChannelCount() const override { return 0; } | 
|  | 103 |  | 
| jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 104 | audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; } | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 105 | size_t    frameCount() const override  { return 0; } | 
|  | 106 | uint32_t  latency() const override  { return 0; } | 
|  | 107 |  | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 108 | status_t addEffectToHal(const sp<EffectHalInterface>& /* effect */) override { | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 109 | return NO_ERROR; | 
|  | 110 | } | 
| Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 111 | status_t removeEffectFromHal(const sp<EffectHalInterface>& /* effect */) override { | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 112 | return NO_ERROR; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override; | 
|  | 116 | void setVolumeForOutput(float left __unused, float right __unused) const override {} | 
|  | 117 |  | 
|  | 118 | // check if effects should be suspended or restored when a given effect is enable or disabled | 
|  | 119 | void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect __unused, | 
|  | 120 | bool enabled __unused, bool threadLocked __unused) override {} | 
|  | 121 | void resetVolume() override {} | 
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 122 | 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] | 123 | int32_t activeTrackCnt() const override { return 0; } | 
|  | 124 | void onEffectEnable(const sp<EffectBase>& effect __unused) override {} | 
|  | 125 | void onEffectDisable(const sp<EffectBase>& effect __unused) override {} | 
|  | 126 |  | 
|  | 127 | wp<EffectChain> chain() const override { return nullptr; } | 
|  | 128 |  | 
| Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 129 | bool isAudioPolicyReady() const override { | 
|  | 130 | return mManager.audioFlinger().isAudioPolicyReady(); | 
|  | 131 | } | 
|  | 132 |  | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 133 | int newEffectId() { return mManager.audioFlinger().nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); } | 
|  | 134 |  | 
| Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 135 | status_t addEffectToHal(const struct audio_port_config *device, | 
|  | 136 | const sp<EffectHalInterface>& effect) { | 
|  | 137 | return mManager.addEffectToHal(device, effect); | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 138 | } | 
| Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 139 | status_t removeEffectFromHal(const struct audio_port_config *device, | 
|  | 140 | const sp<EffectHalInterface>& effect) { | 
|  | 141 | return mManager.removeEffectFromHal(device, effect); | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 142 | } | 
|  | 143 | private: | 
|  | 144 | DeviceEffectManager& mManager; | 
|  | 145 | }; |