1 /* 2 * Copyright (c) 2024 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 #ifndef AUDIO_ENHANCE_CHAIN_MANAGER_H 17 #define AUDIO_ENHANCE_CHAIN_MANAGER_H 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 #include <mutex> 24 #include <set> 25 26 #include "audio_effect.h" 27 #include "audio_enhance_chain.h" 28 29 namespace OHOS { 30 namespace AudioStandard { 31 32 class AudioEnhanceChainManager { 33 public: 34 AudioEnhanceChainManager(); 35 ~AudioEnhanceChainManager(); 36 static AudioEnhanceChainManager* GetInstance(); 37 void InitAudioEnhanceChainManager(std::vector<EffectChain> &enhanceChains, 38 const EffectChainManagerParam &managerParam, 39 std::vector<std::shared_ptr<AudioEffectLibEntry>> &enhanceLibraryList); 40 int32_t CreateAudioEnhanceChainDynamic(const uint64_t sceneKeyCode, const AudioEnhanceDeviceAttr &deviceAttr); 41 int32_t ReleaseAudioEnhanceChainDynamic(const uint64_t sceneKeyCode); 42 bool ExistAudioEnhanceChain(const uint64_t sceneKeyCode); 43 int32_t AudioEnhanceChainGetAlgoConfig(const uint64_t sceneKeyCode, AudioBufferConfig &micConfig, 44 AudioBufferConfig &ecConfig, AudioBufferConfig &micRefConfig); 45 bool IsEmptyEnhanceChain(); 46 int32_t InitEnhanceBuffer(); 47 int32_t CopyToEnhanceBuffer(void *data, uint32_t length); 48 int32_t CopyEcToEnhanceBuffer(void *data, uint32_t length); 49 int32_t CopyMicRefToEnhanceBuffer(void *data, uint32_t length); 50 int32_t CopyFromEnhanceBuffer(void *data, uint32_t length); 51 int32_t ApplyAudioEnhanceChain(const uint64_t sceneKeyCode, uint32_t length); 52 int32_t SetInputDevice(const uint32_t &captureId, const DeviceType &inputDevice, 53 const std::string &deviceName = ""); 54 int32_t SetOutputDevice(const uint32_t &renderId, const DeviceType &outputDevice); 55 int32_t SetVolumeInfo(const AudioVolumeType &volumeType, const float &systemVol); 56 int32_t SetMicrophoneMuteInfo(const bool &isMute); 57 int32_t SetStreamVolumeInfo(const uint32_t &sessionId, const float &streamVol); 58 // for effect V3 59 int32_t SetAudioEnhanceProperty(const AudioEffectPropertyArrayV3 &propertyArray, 60 DeviceType deviceType = DEVICE_TYPE_NONE); 61 int32_t GetAudioEnhanceProperty(AudioEffectPropertyArrayV3 &propertyArray, 62 DeviceType deviceType = DEVICE_TYPE_NONE); 63 // for enhance 64 int32_t SetAudioEnhanceProperty(const AudioEnhancePropertyArray &propertyArray, 65 DeviceType deviceType = DEVICE_TYPE_NONE); 66 int32_t GetAudioEnhanceProperty(AudioEnhancePropertyArray &propertyArray, 67 DeviceType deviceType = DEVICE_TYPE_NONE); 68 void ResetInfo(); // use for unit test 69 int32_t ApplyAudioEnhanceChainDefault(const uint32_t captureId, uint32_t length); 70 void UpdateExtraSceneType(const std::string &mainkey, const std::string &subkey, const std::string &extraSceneType); 71 int32_t SendInitCommand(); 72 73 private: 74 int32_t AddAudioEnhanceChainHandles(std::shared_ptr<AudioEnhanceChain> &audioEnhanceChain, 75 const std::string &enhanceChain); 76 int32_t FreeEnhanceBuffer(); 77 int32_t ParseSceneKeyCode(const uint64_t sceneKeyCode, std::string &sceneType, std::string &capturerDeviceStr, 78 std::string &rendererDeivceStr); 79 int32_t CreateEnhanceChainInner(std::shared_ptr<AudioEnhanceChain> &audioEnhanceChain, 80 const uint64_t sceneKeyCode, const AudioEnhanceDeviceAttr &deviceAttr, bool &createFlag, bool &defaultFlag); 81 int32_t DeleteEnhanceChainInner(std::shared_ptr<AudioEnhanceChain> &audioEnhanceChain, 82 const uint64_t sceneKeyCode); 83 std::string GetEnhanceChainNameBySceneCode(const uint64_t sceneKeyCode, const bool defaultFlag); 84 int32_t UpdatePropertyAndSendToAlgo(const DeviceType &inputDevice); 85 void UpdateEnhancePropertyMapFromDb(DeviceType deviceType); 86 int32_t WriteEnhancePropertyToDb(const std::string &key, const std::string &property); 87 int32_t SetAudioEnhancePropertyToChains(AudioEnhanceProperty property); 88 int32_t SetAudioEnhancePropertyToChains(AudioEffectPropertyV3 property); 89 void GetDeviceTypeName(DeviceType deviceType, std::string &deviceName); 90 void GetDeviceNameByCaptureId(const uint32_t captureId, std::string &deviceName); 91 // construct when init 92 void ConstructEnhanceChainMgrMaps(std::vector<EffectChain> &enhanceChains, 93 const EffectChainManagerParam &managerParam, 94 std::vector<std::shared_ptr<AudioEffectLibEntry>> &enhanceLibraryList); 95 void ConstructDeviceEnhances(); 96 97 std::map<uint64_t, std::shared_ptr<AudioEnhanceChain>> sceneTypeToEnhanceChainMap_; 98 std::map<uint64_t, int32_t> sceneTypeToEnhanceChainCountMap_; 99 std::unordered_map<std::string, std::string> sceneTypeAndModeToEnhanceChainNameMap_; 100 std::map<std::string, std::vector<std::string>> enhanceChainToEnhancesMap_; 101 std::map<std::string, std::shared_ptr<AudioEffectLibEntry>> enhanceToLibraryEntryMap_; 102 std::map<std::string, std::string> enhanceToLibraryNameMap_; 103 std::unordered_map<std::string, std::string> enhancePropertyMap_; 104 std::unordered_map<std::string, std::string> defaultPropertyMap_; 105 std::map<uint32_t, DeviceType> captureIdToDeviceMap_; 106 std::map<uint32_t, std::string> captureIdToDeviceNameMap_; 107 std::map<uint32_t, DeviceType> renderIdToDeviceMap_; 108 109 std::map<uint32_t, uint32_t> captureId2SceneCount_; 110 std::map<uint32_t, uint32_t> captureId2DefaultChainCount_; 111 std::map<uint32_t, std::shared_ptr<AudioEnhanceChain>> captureId2DefaultChain_; 112 std::set<std::string> withDeviceEnhances_; 113 114 // for effect instances limit 115 std::string defaultScene_; 116 std::unordered_set<std::string> priorSceneSet_; 117 uint32_t normalSceneLimit_ = 0; 118 uint32_t chainNum_ = 0; 119 120 std::unique_ptr<EnhanceBuffer> enhanceBuffer_ = nullptr; 121 std::mutex chainManagerMutex_; 122 bool isInitialized_; 123 uint32_t sessionId_ = 0; 124 AudioVolumeType volumeType_ = STREAM_MUSIC; 125 float systemVol_ = 0.0f; 126 float streamVol_ = 0.0f; 127 bool isMute_ = false; 128 std::string deviceName_ = ""; 129 uint32_t foldState_ = FOLD_STATE_MIDDLE; 130 }; 131 132 } // namespace AudioStandard 133 } // namespace OHOS 134 #endif // AUDIO_ENHANCE_CHAIN_MANAGER_H 135