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 #pragma once 19 20 #include "IAfEffect.h" 21 #include "PatchCommandThread.h" 22 23 namespace android { 24 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) 32 EXCLUDES_AudioFlinger_HardwareMutex = 0; 33 virtual status_t removeEffectFromHal( 34 const struct audio_port_config* device, const sp<EffectHalInterface>& effect) 35 EXCLUDES_AudioFlinger_HardwareMutex= 0; 36 }; 37 38 class DeviceEffectManagerCallback; 39 40 // DeviceEffectManager is concealed within AudioFlinger, their lifetimes are the same. 41 class DeviceEffectManager : public PatchCommandThread::PatchCommandListener { 42 public: 43 explicit DeviceEffectManager( 44 const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback); 45 46 void onFirstRef() override; 47 48 sp<IAfEffectHandle> createEffect_l(effect_descriptor_t *descriptor, 49 const AudioDeviceTypeAddr& device, 50 const sp<Client>& client, 51 const sp<media::IEffectClient>& effectClient, 52 const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches, 53 int *enabled, 54 status_t *status, 55 bool probe, 56 bool notifyFramesProcessed) REQUIRES(audio_utils::AudioFlinger_Mutex); 57 58 size_t removeEffect(const sp<IAfDeviceEffectProxy>& effect); 59 static status_t createEffectHal(const effect_uuid_t *pEffectUuid, 60 int32_t sessionId, int32_t deviceId, 61 sp<EffectHalInterface> *effect); 62 status_t addEffectToHal(const struct audio_port_config *device, 63 const sp<EffectHalInterface>& effect); 64 status_t removeEffectFromHal(const struct audio_port_config *device, 65 const sp<EffectHalInterface>& effect); 66 afDeviceEffectManagerCallback()67 const auto& afDeviceEffectManagerCallback() const { return mAfDeviceEffectManagerCallback; } 68 69 void dump(int fd); 70 71 // PatchCommandThread::PatchCommandListener implementation 72 73 void onCreateAudioPatch(audio_patch_handle_t handle, 74 const IAfPatchPanel::Patch& patch) final 75 EXCLUDES_DeviceEffectManager_Mutex; 76 void onReleaseAudioPatch(audio_patch_handle_t handle) final 77 EXCLUDES_DeviceEffectManager_Mutex; 78 void onUpdateAudioPatch(audio_patch_handle_t oldHandle, 79 audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) final 80 EXCLUDES_DeviceEffectManager_Mutex; 81 82 private: 83 static status_t checkEffectCompatibility(const effect_descriptor_t *desc); 84 mutex()85 audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::DeviceEffectManager_Mutex) { 86 return mMutex; 87 } 88 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kDeviceEffectManager_Mutex}; 89 const sp<IAfDeviceEffectManagerCallback> mAfDeviceEffectManagerCallback; 90 const sp<DeviceEffectManagerCallback> mMyCallback; 91 std::map<AudioDeviceTypeAddr, std::vector<sp<IAfDeviceEffectProxy>>> 92 mDeviceEffects GUARDED_BY(mutex()); 93 }; 94 95 class DeviceEffectManagerCallback : public EffectCallbackInterface { 96 public: DeviceEffectManagerCallback(DeviceEffectManager & manager)97 explicit DeviceEffectManagerCallback(DeviceEffectManager& manager) 98 : mManager(manager) {} 99 createEffectHal(const effect_uuid_t * pEffectUuid,int32_t sessionId,int32_t deviceId,sp<EffectHalInterface> * effect)100 status_t createEffectHal(const effect_uuid_t *pEffectUuid, 101 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) final { 102 return mManager.createEffectHal(pEffectUuid, sessionId, deviceId, effect); 103 } allocateHalBuffer(size_t size __unused,sp<EffectBufferHalInterface> * buffer __unused)104 status_t allocateHalBuffer(size_t size __unused, 105 sp<EffectBufferHalInterface>* buffer __unused) final { return NO_ERROR; } updateOrphanEffectChains(const sp<IAfEffectBase> & effect __unused)106 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) final { 107 return false; 108 } 109 io()110 audio_io_handle_t io() const final { return AUDIO_IO_HANDLE_NONE; } isOutput()111 bool isOutput() const final { return false; } isOffload()112 bool isOffload() const final { return false; } isOffloadOrDirect()113 bool isOffloadOrDirect() const final { return false; } isOffloadOrMmap()114 bool isOffloadOrMmap() const final { return false; } isSpatializer()115 bool isSpatializer() const final { return false; } 116 sampleRate()117 uint32_t sampleRate() const final { return 0; } inChannelMask(int id __unused)118 audio_channel_mask_t inChannelMask(int id __unused) const final { 119 return AUDIO_CHANNEL_NONE; 120 } inChannelCount(int id __unused)121 uint32_t inChannelCount(int id __unused) const final { return 0; } outChannelMask()122 audio_channel_mask_t outChannelMask() const final { return AUDIO_CHANNEL_NONE; } outChannelCount()123 uint32_t outChannelCount() const final { return 0; } 124 hapticChannelMask()125 audio_channel_mask_t hapticChannelMask() const final { return AUDIO_CHANNEL_NONE; } frameCount()126 size_t frameCount() const final { return 0; } latency()127 uint32_t latency() const final { return 0; } 128 addEffectToHal(const sp<EffectHalInterface> &)129 status_t addEffectToHal(const sp<EffectHalInterface>& /* effect */) final { 130 return NO_ERROR; 131 } removeEffectFromHal(const sp<EffectHalInterface> &)132 status_t removeEffectFromHal(const sp<EffectHalInterface>& /* effect */) final { 133 return NO_ERROR; 134 } 135 136 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) final; setVolumeForOutput(float left __unused,float right __unused)137 void setVolumeForOutput(float left __unused, float right __unused) const final {} 138 139 // check if effects should be suspended or restored when a given effect is enable or disabled checkSuspendOnEffectEnabled(const sp<IAfEffectBase> & effect __unused,bool enabled __unused,bool threadLocked __unused)140 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused, 141 bool enabled __unused, bool threadLocked __unused) final {} resetVolume_l()142 void resetVolume_l() final REQUIRES(audio_utils::EffectChain_Mutex) {} strategy()143 product_strategy_t strategy() const final { return static_cast<product_strategy_t>(0); } activeTrackCnt()144 int32_t activeTrackCnt() const final { return 0; } onEffectEnable(const sp<IAfEffectBase> & effect __unused)145 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) final {} onEffectDisable(const sp<IAfEffectBase> & effect __unused)146 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) final {} 147 chain()148 wp<IAfEffectChain> chain() const final { return nullptr; } 149 150 bool isAudioPolicyReady() const final; 151 152 int newEffectId() const; 153 addEffectToHal(const struct audio_port_config * device,const sp<EffectHalInterface> & effect)154 status_t addEffectToHal(const struct audio_port_config *device, 155 const sp<EffectHalInterface>& effect) { 156 return mManager.addEffectToHal(device, effect); 157 } removeEffectFromHal(const struct audio_port_config * device,const sp<EffectHalInterface> & effect)158 status_t removeEffectFromHal(const struct audio_port_config *device, 159 const sp<EffectHalInterface>& effect) { 160 return mManager.removeEffectFromHal(device, effect); 161 } 162 private: 163 DeviceEffectManager& mManager; 164 }; 165 166 } // namespace android 167