1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17 #ifndef AUDIO_EFFECT_CHAIN_MANAGER_H 18 #define AUDIO_EFFECT_CHAIN_MANAGER_H 19 20 #include <cstdio> 21 #include <cstdint> 22 #include <cassert> 23 #include <cstdint> 24 #include <cstddef> 25 #include <map> 26 #include <memory> 27 #include <string> 28 #include <vector> 29 #include <mutex> 30 #include <set> 31 32 #include "audio_effect.h" 33 #include "audio_effect_chain.h" 34 35 #ifdef SENSOR_ENABLE 36 #include "audio_head_tracker.h" 37 #endif 38 39 #ifdef WINDOW_MANAGER_ENABLE 40 #include "audio_effect_rotation.h" 41 #endif 42 #include "audio_effect_volume.h" 43 44 namespace OHOS { 45 namespace AudioStandard { 46 47 class AudioEffectHdiParam; 48 49 const uint32_t DEFAULT_FRAMELEN = 1440; 50 const uint32_t DEFAULT_NUM_CHANNEL = STEREO; 51 const uint32_t DEFAULT_MCH_NUM_CHANNEL = CHANNEL_6; 52 const uint32_t DSP_MAX_NUM_CHANNEL = CHANNEL_16; 53 const uint64_t DEFAULT_NUM_CHANNELLAYOUT = CH_LAYOUT_STEREO; 54 const uint64_t DEFAULT_MCH_NUM_CHANNELLAYOUT = CH_LAYOUT_5POINT1; 55 const uint32_t BASE_TEN = 10; 56 const std::string DEFAULT_DEVICE_SINK = "Speaker"; 57 const std::string BLUETOOTH_DEVICE_SINK = "Bt_Speaker"; 58 const uint32_t SIZE_OF_SPATIALIZATION_STATE = 2; 59 const uint32_t MAX_UINT_VOLUME_NUM = 10000; 60 const uint32_t MAX_UINT_DSP_VOLUME = 65535; 61 const std::string DEFAULT_SCENE_TYPE = "SCENE_DEFAULT"; 62 const std::string DEFAULT_PRESET_SCENE = "SCENE_MUSIC"; 63 64 struct SessionEffectInfo { 65 std::string sceneMode; 66 std::string sceneType; 67 uint32_t channels; 68 uint64_t channelLayout; 69 int32_t streamUsage; 70 int32_t systemVolumeType; 71 }; 72 73 const std::vector<AudioChannelLayout> AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS { 74 CH_LAYOUT_STEREO, 75 CH_LAYOUT_5POINT1, 76 CH_LAYOUT_5POINT1POINT2, 77 CH_LAYOUT_7POINT1, 78 CH_LAYOUT_5POINT1POINT4, 79 CH_LAYOUT_7POINT1POINT2, 80 CH_LAYOUT_7POINT1POINT4 81 }; 82 83 struct EffectBufferAttr { 84 float *bufIn; 85 float *bufOut; 86 int numChans; 87 int frameLen; 88 uint32_t outChannels; 89 uint64_t outChannelLayout; 90 EffectBufferAttrEffectBufferAttr91 EffectBufferAttr(float *bufIn, float *bufOut, int numChans, int frameLen, uint32_t outChannels, 92 uint64_t outChannelLayout) 93 : bufIn(bufIn), 94 bufOut(bufOut), 95 numChans(numChans), 96 frameLen(frameLen), 97 outChannels(outChannels), 98 outChannelLayout(outChannelLayout) 99 { 100 } 101 }; 102 103 enum SceneTypeOperation { 104 ADD_SCENE_TYPE = 0, 105 REMOVE_SCENE_TYPE = 1, 106 }; 107 108 class AudioEffectChainManager { 109 public: 110 AudioEffectChainManager(); 111 ~AudioEffectChainManager(); 112 static AudioEffectChainManager *GetInstance(); 113 void InitAudioEffectChainManager(const std::vector<EffectChain> &effectChains, 114 const EffectChainManagerParam &effectChainManagerParam, 115 const std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList); 116 void ConstructEffectChainMgrMaps(const std::vector<EffectChain> &effectChains, 117 const EffectChainManagerParam &effectChainManagerParam, 118 const std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList); 119 bool CheckAndAddSessionID(const std::string &sessionID); 120 int32_t CreateAudioEffectChainDynamic(const std::string &sceneType); 121 bool CheckAndRemoveSessionID(const std::string &sessionID); 122 int32_t ReleaseAudioEffectChainDynamic(const std::string &sceneType); 123 bool ExistAudioEffectChain(const std::string &sceneType, const std::string &effectMode); 124 int32_t ApplyAudioEffectChain(const std::string &sceneType, std::unique_ptr<EffectBufferAttr> &bufferAttr); 125 void SetOutputDeviceSink(int32_t device, const std::string &sinkName); 126 bool GetOffloadEnabled(); 127 int32_t UpdateMultichannelConfig(const std::string &sceneType); 128 int32_t InitAudioEffectChainDynamic(const std::string &sceneType); 129 int32_t UpdateSpatializationState(AudioSpatializationState spatializationState); 130 int32_t UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType); 131 int32_t SessionInfoMapAdd(const std::string &sessionID, const SessionEffectInfo &info); 132 int32_t SessionInfoMapDelete(const std::string &sceneType, const std::string &sessionID); 133 int32_t ReturnEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); 134 int32_t ReturnMultiChannelInfo(uint32_t *channels, uint64_t *channelLayout); 135 int32_t EffectRotationUpdate(const uint32_t rotationState); 136 int32_t EffectVolumeUpdate(); 137 int32_t StreamVolumeUpdate(const std::string sessionIDString, const float streamVolume); 138 uint32_t GetLatency(const std::string &sessionId); 139 int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType); 140 int32_t SetEffectSystemVolume(const int32_t systemVolumeType, const float systemVolume); 141 int32_t SetAbsVolumeStateToEffect(const bool absVolumeState); 142 void ResetInfo(); // Use for testing temporarily. 143 void UpdateDefaultAudioEffect(); 144 bool CheckSceneTypeMatch(const std::string &sinkSceneType, const std::string &sceneType); 145 void UpdateParamExtra(const std::string &mainkey, const std::string &subkey, const std::string &value); 146 void InitHdiState(); 147 void UpdateEffectBtOffloadSupported(const bool &isSupported); 148 int32_t UpdateSceneTypeList(const std::string &sceneType, SceneTypeOperation operation); 149 uint32_t GetSceneTypeToChainCount(const std::string &sceneType); 150 int32_t SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray); 151 int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray); 152 int32_t SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray); 153 int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray); 154 void UpdateStreamUsage(); 155 int32_t InitEffectBuffer(const std::string &sessionID); 156 int32_t QueryEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); 157 int32_t QueryHdiSupportedChannelInfo(uint32_t &channels, uint64_t &channelLayout); 158 void LoadEffectProperties(); 159 ProcessClusterOperation CheckProcessClusterInstances(const std::string &sceneType); 160 int32_t GetOutputChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); 161 int32_t DeleteStreamVolume(const std::string StringSessionID); 162 private: 163 int32_t SetAudioEffectChainDynamic(std::string &sceneType, const std::string &effectMode); 164 void UpdateSensorState(); 165 void DeleteAllChains(); 166 void RecoverAllChains(); 167 int32_t EffectDspVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume); 168 int32_t EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume); 169 int32_t SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume); 170 void SetSpatializationSceneTypeToChains(); 171 void SetSpatializationEnabledToChains(); 172 void SetSpkOffloadState(); 173 void UpdateCurrSceneType(AudioEffectScene &currSceneType, const std::string &sceneType); 174 void FindMaxEffectChannels(const std::string &sceneType, const std::set<std::string> &sessions, uint32_t &channels, 175 uint64_t &channelLayout); 176 int32_t UpdateDeviceInfo(int32_t device, const std::string &sinkName); 177 std::shared_ptr<AudioEffectChain> CreateAudioEffectChain(const std::string &sceneType, bool isPriorScene); 178 bool CheckIfSpkDsp(); 179 int32_t CheckAndReleaseCommonEffectChain(const std::string &sceneType); 180 void FindMaxSessionID(uint32_t &maxSessionID, std::string &sceneType, 181 const std::string &scenePairType, std::set<std::string> &sessions); 182 void UpdateCurrSceneTypeAndStreamUsageForDsp(); 183 void SendAudioParamToHDI(HdiSetParamCommandCode code, const std::string &value, DeviceType device); 184 void SendAudioParamToARM(HdiSetParamCommandCode code, const std::string &value); 185 std::string GetDeviceTypeName(); 186 bool IsEffectChainStop(const std::string &sceneType, const std::string &sessionID); 187 int32_t InitEffectBufferInner(const std::string &sessionID); 188 int32_t InitAudioEffectChainDynamicInner(const std::string &sceneType); 189 int32_t QueryEffectChannelInfoInner(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); 190 int32_t SetAbsVolumeStateToEffectInner(const bool absVolumeState); 191 int32_t EffectDspAbsVolumeStateUpdate(const bool absVolumeState); 192 int32_t EffectApAbsVolumeStateUpdate(const bool absVolumeState); 193 void UpdateDefaultAudioEffectInner(); 194 void UpdateStreamUsageInner(); 195 int32_t DeleteStreamVolumeInner(const std::string StringSessionID); 196 #ifdef WINDOW_MANAGER_ENABLE 197 int32_t EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation, 198 const uint32_t rotationState); 199 int32_t EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation, 200 const uint32_t rotationState); 201 #endif 202 int32_t CreateAudioEffectChainDynamicInner(const std::string &sceneType); 203 int32_t ReleaseAudioEffectChainDynamicInner(const std::string &sceneType); 204 bool ExistAudioEffectChainInner(const std::string &sceneType, const std::string &effectMode); 205 int32_t UpdateMultichannelConfigInner(const std::string &sceneType); 206 int32_t UpdateSpatializationStateInner(AudioSpatializationState spatializationState); 207 int32_t SetHdiParam(const AudioEffectScene &sceneType); 208 int32_t ReturnEffectChannelInfoInner(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); 209 int32_t EffectVolumeUpdateInner(std::shared_ptr<AudioEffectVolume> audioEffectVolume); 210 void InitHdiStateInner(); 211 void UpdateSpatializationEnabled(AudioSpatializationState spatializationState); 212 void ConfigureAudioEffectChain(std::shared_ptr<AudioEffectChain> audioEffectChain, const std::string &effectMode); 213 int32_t NotifyAndCreateAudioEffectChain(const std::string &sceneType); 214 void WaitAndReleaseEffectChain(const std::string &sceneType, const std::string &sceneTypeAndDeviceKey, 215 const std::string &defaultSceneTypeAndDeviceKey, int32_t ret); 216 std::map<std::string, std::shared_ptr<AudioEffectLibEntry>> effectToLibraryEntryMap_; 217 std::map<std::string, std::string> effectToLibraryNameMap_; 218 std::map<std::string, std::vector<std::string>> effectChainToEffectsMap_; 219 std::map<std::string, std::string> sceneTypeAndModeToEffectChainNameMap_; 220 std::map<std::string, std::shared_ptr<AudioEffectChain>> sceneTypeToEffectChainMap_; 221 std::map<std::string, int32_t> sceneTypeToEffectChainCountMap_; 222 std::set<std::string> sessionIDSet_; 223 std::map<std::string, std::set<std::string>> sceneTypeToSessionIDMap_; 224 std::map<std::string, SessionEffectInfo> sessionIDToEffectInfoMap_; 225 std::map<std::string, int32_t> sceneTypeToEffectChainCountBackupMap_; 226 std::set<std::string> sceneTypeToSpecialEffectSet_; 227 std::vector<std::string> priorSceneList_; 228 std::unordered_map<std::string, std::string> effectPropertyMap_; 229 std::unordered_map<std::string, std::string> defaultPropertyMap_; 230 std::vector<std::pair<std::string, int32_t>> sceneTypeCountList_; 231 DeviceType deviceType_ = DEVICE_TYPE_SPEAKER; 232 std::string deviceSink_ = DEFAULT_DEVICE_SINK; 233 std::string deviceClass_ = ""; 234 std::string extraSceneType_ = "0"; 235 std::string foldState_ = "0"; 236 std::string lidState_ = "0"; 237 std::string maxSessionIDToSceneType_ = ""; 238 std::string maxDefaultSessionIDToSceneType_ = ""; 239 bool isInitialized_ = false; 240 std::mutex dynamicMutex_; 241 std::atomic<bool> spatializationEnabled_ = false; 242 bool headTrackingEnabled_ = false; 243 bool btOffloadEnabled_ = false; 244 bool spkOffloadEnabled_ = false; 245 bool initializedLogFlag_ = true; 246 bool btOffloadSupported_ = false; 247 AudioSpatializationSceneType spatializationSceneType_ = SPATIALIZATION_SCENE_TYPE_MUSIC; 248 bool isDefaultEffectChainExisted_ = false; 249 int32_t defaultEffectChainCount_ = 0; 250 int32_t maxEffectChainCount_ = 1; 251 uint32_t maxSessionID_ = 0; 252 AudioSpatialDeviceType spatialDeviceType_{ EARPHONE_TYPE_OTHERS }; 253 bool hasLoadedEffectProperties_ = false; 254 std::condition_variable cv_; 255 bool defaultEffectChainCreated_ = false; 256 bool absVolumeState_ = true; 257 258 #ifdef SENSOR_ENABLE 259 std::shared_ptr<HeadTracker> headTracker_; 260 #endif 261 262 std::shared_ptr<AudioEffectHdiParam> audioEffectHdiParam_; 263 int8_t effectHdiInput_[SEND_HDI_COMMAND_LEN]; 264 }; 265 } // namespace AudioStandard 266 } // namespace OHOS 267 #endif // AUDIO_EFFECT_CHAIN_MANAGER_H 268