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 #include "audio_effect_hdi_param.h" 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 const uint32_t DEFAULT_FRAMELEN = 1440; 48 const uint32_t DEFAULT_NUM_CHANNEL = STEREO; 49 const uint32_t DEFAULT_MCH_NUM_CHANNEL = CHANNEL_6; 50 const uint32_t DSP_MAX_NUM_CHANNEL = CHANNEL_10; 51 const uint64_t DEFAULT_NUM_CHANNELLAYOUT = CH_LAYOUT_STEREO; 52 const uint64_t DEFAULT_MCH_NUM_CHANNELLAYOUT = CH_LAYOUT_5POINT1; 53 const uint32_t BASE_TEN = 10; 54 const std::string DEFAULT_DEVICE_SINK = "Speaker"; 55 const std::string BLUETOOTH_DEVICE_SINK = "Bt_Speaker"; 56 const uint32_t SIZE_OF_SPATIALIZATION_STATE = 2; 57 const uint32_t MAX_UINT_VOLUME_NUM = 10000; 58 const uint32_t MAX_UINT_DSP_VOLUME = 65535; 59 const std::string DEFAULT_SCENE_TYPE = "SCENE_DEFAULT"; 60 const std::string DEFAULT_PRESET_SCENE = "SCENE_MUSIC"; 61 62 struct SessionEffectInfo { 63 std::string sceneMode; 64 std::string sceneType; 65 uint32_t channels; 66 uint64_t channelLayout; 67 std::string spatializationEnabled; 68 int32_t streamUsage; 69 }; 70 71 const std::vector<AudioChannelLayout> AUDIO_EFFECT_SUPPORTED_CHANNELLAYOUTS { 72 CH_LAYOUT_STEREO, 73 CH_LAYOUT_5POINT1, 74 CH_LAYOUT_5POINT1POINT2, 75 CH_LAYOUT_7POINT1, 76 CH_LAYOUT_5POINT1POINT4, 77 CH_LAYOUT_7POINT1POINT2, 78 CH_LAYOUT_7POINT1POINT4 79 }; 80 81 struct EffectBufferAttr { 82 float *bufIn; 83 float *bufOut; 84 int numChans; 85 int frameLen; 86 EffectBufferAttrEffectBufferAttr87 EffectBufferAttr(float *bufIn, float *bufOut, int numChans, int frameLen) 88 : bufIn(bufIn), bufOut(bufOut), numChans(numChans), frameLen(frameLen) 89 { 90 } 91 }; 92 93 enum SceneTypeOperation { 94 ADD_SCENE_TYPE = 0, 95 REMOVE_SCENE_TYPE = 1, 96 }; 97 98 class AudioEffectChainManager { 99 public: 100 AudioEffectChainManager(); 101 ~AudioEffectChainManager(); 102 static AudioEffectChainManager *GetInstance(); 103 void InitAudioEffectChainManager(std::vector<EffectChain> &effectChains, 104 const EffectChainManagerParam &effectChainManagerParam, 105 std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList); 106 bool CheckAndAddSessionID(const std::string &sessionID); 107 int32_t CreateAudioEffectChainDynamic(const std::string &sceneType); 108 bool CheckAndRemoveSessionID(const std::string &sessionID); 109 int32_t ReleaseAudioEffectChainDynamic(const std::string &sceneType); 110 bool ExistAudioEffectChain(const std::string &sceneType, const std::string &effectMode, 111 const std::string &spatializationEnabled); 112 int32_t ApplyAudioEffectChain(const std::string &sceneType, const std::unique_ptr<EffectBufferAttr> &bufferAttr); 113 void SetOutputDeviceSink(int32_t device, const std::string &sinkName); 114 std::string GetDeviceTypeName(); 115 bool GetOffloadEnabled(); 116 int32_t UpdateMultichannelConfig(const std::string &sceneType); 117 int32_t InitAudioEffectChainDynamic(const std::string &sceneType); 118 int32_t UpdateSpatializationState(AudioSpatializationState spatializationState); 119 int32_t UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType); 120 int32_t SetHdiParam(const AudioEffectScene &sceneType); 121 int32_t SessionInfoMapAdd(const std::string &sessionID, const SessionEffectInfo &info); 122 int32_t SessionInfoMapDelete(const std::string &sceneType, const std::string &sessionID); 123 int32_t ReturnEffectChannelInfo(const std::string &sceneType, uint32_t &channels, uint64_t &channelLayout); 124 int32_t ReturnMultiChannelInfo(uint32_t *channels, uint64_t *channelLayout); 125 int32_t EffectRotationUpdate(const uint32_t rotationState); 126 int32_t EffectVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume); 127 int32_t StreamVolumeUpdate(const std::string sessionIDString, const float streamVolume); 128 uint32_t GetLatency(const std::string &sessionId); 129 int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType); 130 int32_t SetSceneTypeSystemVolume(const std::string sceneType, const float systemVolume); 131 bool GetCurSpatializationEnabled(); 132 void ResetEffectBuffer(); 133 void ResetInfo(); // Use for testing temporarily. 134 void UpdateDefaultAudioEffect(); 135 bool CheckSceneTypeMatch(const std::string &sinkSceneType, const std::string &sceneType); 136 void UpdateSpatializationEnabled(AudioSpatializationState spatializationState); 137 void UpdateExtraSceneType(const std::string &mainkey, const std::string &subkey, const std::string &extraSceneType); 138 void InitHdiState(); 139 void UpdateEffectBtOffloadSupported(const bool &isSupported); 140 void UpdateSceneTypeList(const std::string &sceneType, SceneTypeOperation operation); 141 void UpdateStreamUsage(); 142 143 private: 144 int32_t SetAudioEffectChainDynamic(const std::string &sceneType, const std::string &effectMode); 145 void UpdateSensorState(); 146 void DeleteAllChains(); 147 void RecoverAllChains(); 148 int32_t EffectDspVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume); 149 int32_t EffectApVolumeUpdate(std::shared_ptr<AudioEffectVolume> audioEffectVolume); 150 int32_t SendEffectApVolume(std::shared_ptr<AudioEffectVolume> audioEffectVolume); 151 void SetSpatializationSceneTypeToChains(); 152 void SetSpatializationEnabledToChains(); 153 void SetSpkOffloadState(); 154 void UpdateCurrSceneType(AudioEffectScene &currSceneType, std::string &sceneType); 155 void FindMaxEffectChannels(const std::string &sceneType, const std::set<std::string> &sessions, uint32_t &channels, 156 uint64_t &channelLayout); 157 int32_t UpdateDeviceInfo(int32_t device, const std::string &sinkName); 158 std::shared_ptr<AudioEffectChain> CreateAudioEffectChain(const std::string &sceneType, bool isPriorScene); 159 bool CheckIfSpkDsp(); 160 void CheckAndReleaseCommonEffectChain(const std::string &sceneType); 161 void FindMaxSessionID(uint32_t &maxSessionID, std::string &sceneType, 162 const std::string &scenePairType, std::set<std::string> &sessions); 163 void UpdateCurrSceneTypeAndStreamUsageForDsp(); 164 #ifdef WINDOW_MANAGER_ENABLE 165 int32_t EffectDspRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation, 166 const uint32_t rotationState); 167 int32_t EffectApRotationUpdate(std::shared_ptr<AudioEffectRotation> audioEffectRotation, 168 const uint32_t rotationState); 169 #endif 170 std::map<std::string, std::shared_ptr<AudioEffectLibEntry>> effectToLibraryEntryMap_; 171 std::map<std::string, std::string> effectToLibraryNameMap_; 172 std::map<std::string, std::vector<std::string>> effectChainToEffectsMap_; 173 std::map<std::string, std::string> sceneTypeAndModeToEffectChainNameMap_; 174 std::map<std::string, std::shared_ptr<AudioEffectChain>> sceneTypeToEffectChainMap_; 175 std::map<std::string, int32_t> sceneTypeToEffectChainCountMap_; 176 std::set<std::string> sessionIDSet_; 177 std::map<std::string, std::set<std::string>> sceneTypeToSessionIDMap_; 178 std::map<std::string, SessionEffectInfo> sessionIDToEffectInfoMap_; 179 std::map<std::string, int32_t> sceneTypeToEffectChainCountBackupMap_; 180 std::set<std::string> sceneTypeToSpecialEffectSet_; 181 std::vector<std::string> priorSceneList_; 182 std::vector<std::pair<std::string, int32_t>> sceneTypeCountList_; 183 DeviceType deviceType_ = DEVICE_TYPE_SPEAKER; 184 std::string deviceSink_ = DEFAULT_DEVICE_SINK; 185 std::string deviceClass_ = ""; 186 std::string extraSceneType_ = "0"; 187 std::string maxSessionIDToSceneType_ = ""; 188 std::string maxDefaultSessionIDToSceneType_ = ""; 189 bool isInitialized_ = false; 190 std::recursive_mutex dynamicMutex_; 191 std::atomic<bool> spatializationEnabled_ = false; 192 bool headTrackingEnabled_ = false; 193 bool btOffloadEnabled_ = false; 194 bool spkOffloadEnabled_ = false; 195 bool initializedLogFlag_ = true; 196 bool btOffloadSupported_ = false; 197 AudioSpatializationSceneType spatializationSceneType_ = SPATIALIZATION_SCENE_TYPE_DEFAULT; 198 bool isDefaultEffectChainExisted_ = false; 199 bool debugArmFlag_ = false; 200 int32_t defaultEffectChainCount_ = 0; 201 int32_t maxEffectChainCount_ = 1; 202 uint32_t maxSessionID_ = 0; 203 AudioSpatialDeviceType spatialDeviceType_{ EARPHONE_TYPE_OTHERS }; 204 205 #ifdef SENSOR_ENABLE 206 std::shared_ptr<HeadTracker> headTracker_; 207 #endif 208 209 std::shared_ptr<AudioEffectHdiParam> audioEffectHdiParam_; 210 int8_t effectHdiInput_[SEND_HDI_COMMAND_LEN]; 211 }; 212 } // namespace AudioStandard 213 } // namespace OHOS 214 #endif // AUDIO_EFFECT_CHAIN_MANAGER_H 215