1 /* 2 * Copyright (c) 2025 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 #ifndef AUDIO_POLICY_CONFIG_MANAGER_H 16 #define AUDIO_POLICY_CONFIG_MANAGER_H 17 18 #include <string> 19 #include "singleton.h" 20 #include "audio_group_handle.h" 21 #include "audio_info.h" 22 #include "audio_manager_base.h" 23 #include "audio_utils.h" 24 #include "audio_errors.h" 25 #include "audio_definition_adapter_info.h" 26 #include "audio_device_manager.h" 27 28 namespace OHOS { 29 namespace AudioStandard { 30 31 class AudioPolicyConfigManager { 32 public: GetInstance()33 static AudioPolicyConfigManager& GetInstance() 34 { 35 static AudioPolicyConfigManager instance; 36 return instance; 37 } 38 bool Init(bool isRefresh = false); 39 40 void OnAudioPolicyConfigXmlParsingCompleted(); 41 42 // custom parser callback 43 void OnXmlParsingCompleted(const std::unordered_map<ClassType, std::list<AudioModuleInfo>> &xmldata); 44 void OnAudioLatencyParsed(uint64_t latency); 45 void OnFastFormatParsed(AudioSampleFormat format); 46 void OnSinkLatencyParsed(uint32_t latency); 47 void OnVolumeGroupParsed(std::unordered_map<std::string, std::string>& volumeGroupData); 48 void OnInterruptGroupParsed(std::unordered_map<std::string, std::string>& interruptGroupData); 49 void OnGlobalConfigsParsed(PolicyGlobalConfigs &globalConfigs); 50 void OnVoipConfigParsed(bool enableFastVoip); 51 void OnUpdateRouteSupport(bool isSupported); 52 void OnUpdateDefaultAdapter(bool isEnable); 53 void OnUpdateAnahsSupport(std::string anahsShowType); 54 void OnUpdateEac3Support(bool isSupported); 55 void OnHasEarpiece(); 56 57 // update 58 void SetNormalVoipFlag(const bool &normalVoipFlag); 59 void UpdateStreamPropInfo(const std::string &adapterName, const std::string &pipeName, 60 const std::list<DeviceStreamInfo> &deviceStreamInfo, const std::list<std::string> &supportDevices); 61 void ClearStreamPropInfo(const std::string &adapterName, const std::string &pipeName); 62 void UpdateDynamicCapturerConfig(ClassType type, const AudioModuleInfo moduleInfo); 63 64 // query 65 bool GetModuleListByType(ClassType type, std::list<AudioModuleInfo>& moduleList); 66 void GetDeviceClassInfo(std::unordered_map<ClassType, std::list<AudioModuleInfo>> &deviceClassInfo); 67 std::string GetGroupName(const std::string& deviceName, const GroupType type); 68 int32_t GetMaxRendererInstances(); 69 int32_t GetMaxCapturersInstances(); 70 int32_t GetMaxFastRenderersInstances(); 71 int32_t GetVoipRendererFlag(const std::string &sinkPortName, const std::string &networkId, 72 const AudioSamplingRate &samplingRate); 73 bool GetNormalVoipFlag(); 74 int32_t GetAudioLatencyFromXml() const; 75 AudioSampleFormat GetFastFormat() const; 76 uint32_t GetSinkLatencyFromXml() const; 77 void GetAudioAdapterInfos(std::unordered_map<AudioAdapterType, std::shared_ptr<PolicyAdapterInfo>> &adapterInfoMap); 78 void GetVolumeGroupData(std::unordered_map<std::string, std::string>& volumeGroupData); 79 void GetInterruptGroupData(std::unordered_map<std::string, std::string>& interruptGroupData); 80 void GetGlobalConfigs(PolicyGlobalConfigs &globalConfigs); 81 bool GetVoipConfig(); 82 bool GetUpdateRouteSupport(); 83 bool GetDefaultAdapterEnable(); 84 bool GetAdapterInfoFlag(); 85 bool GetAdapterInfoByType(AudioAdapterType type, std::shared_ptr<PolicyAdapterInfo> &info); 86 bool GetHasEarpiece(); 87 bool IsFastStreamSupported(AudioStreamInfo &streamInfo, 88 std::vector<std::shared_ptr<AudioDeviceDescriptor>> &desc); 89 bool GetFastStreamSupport(AudioStreamInfo &streamInfo, std::shared_ptr<AdapterDeviceInfo> &deviceInfo); 90 uint32_t GetStreamPropInfoSize(const std::string &adapterName, const std::string &pipeName); 91 92 uint32_t GetRouteFlag(std::shared_ptr<AudioStreamDescriptor> &desc); 93 void GetStreamPropInfo(std::shared_ptr<AudioStreamDescriptor> &desc, std::shared_ptr<PipeStreamPropInfo> &info); 94 std::shared_ptr<PipeStreamPropInfo> GetStreamPropInfoFromPipe(std::shared_ptr<AdapterPipeInfo> &info, 95 AudioSampleFormat format, uint32_t sampleRate, AudioChannel channels); 96 bool SupportImplicitConversion(uint32_t routeFlag); 97 void GetTargetSourceTypeAndMatchingFlag(SourceType source, bool &useMatchingPropInfo); 98 DirectPlaybackMode GetDirectPlaybackSupport(std::shared_ptr<AudioDeviceDescriptor> desc, 99 const AudioStreamInfo &streamInfo); 100 bool IsStreamPropMatch(const AudioStreamInfo &streamInfo, std::list<std::shared_ptr<PipeStreamPropInfo>> &infos); 101 AudioPolicyConfigManager()102 AudioPolicyConfigManager() : audioDeviceManager_(AudioDeviceManager::GetAudioDeviceManager()), 103 audioPolicyConfig_(AudioPolicyConfigData::GetInstance()) 104 { 105 } ~AudioPolicyConfigManager()106 ~AudioPolicyConfigManager() 107 { 108 } 109 private: 110 void UpdateBasicStreamInfo(std::shared_ptr<AudioStreamDescriptor> desc, std::shared_ptr<AdapterPipeInfo> pipeInfo, 111 AudioStreamInfo &streamInfo); 112 void GetStreamPropInfoForRecord(std::shared_ptr<AudioStreamDescriptor> desc, 113 std::shared_ptr<AdapterPipeInfo> adapterPipeInfo, std::shared_ptr<PipeStreamPropInfo> &info, 114 const AudioChannel &tempChannel); 115 std::shared_ptr<AdapterPipeInfo> GetNormalRecordAdapterInfo(std::shared_ptr<AudioStreamDescriptor> desc); 116 std::shared_ptr<PipeStreamPropInfo> GetDynamicStreamPropInfoFromPipe(std::shared_ptr<AdapterPipeInfo> &info, 117 AudioSampleFormat format, uint32_t sampleRate, AudioChannel channels); 118 AudioSampleFormat ParseFormat(std::string format); 119 void CheckDynamicCapturerConfig(std::shared_ptr<AudioStreamDescriptor> desc, 120 std::shared_ptr<PipeStreamPropInfo> &info); 121 122 bool xmlHasLoaded_ = false; 123 124 std::unordered_map<ClassType, std::list<AudioModuleInfo>> deviceClassInfo_ = {}; 125 std::unordered_map<ClassType, AudioModuleInfo> dynamicCapturerConfig_ = {}; 126 bool hasEarpiece_ = false; 127 bool isUpdateRouteSupported_ = true; 128 bool isDefaultAdapterEnable_ = false; 129 bool isSupportEac3_ = false; 130 std::unordered_map<std::string, std::string> volumeGroupData_; 131 std::unordered_map<std::string, std::string> interruptGroupData_; 132 PolicyGlobalConfigs globalConfigs_; 133 bool enableFastVoip_ = false; 134 uint64_t audioLatencyInMsec_ = 50; 135 uint32_t sinkLatencyInMsec_ {0}; 136 AudioSampleFormat fastFormat_ = SAMPLE_S16LE; 137 bool normalVoipFlag_ = false; 138 139 std::atomic<bool> isAdapterInfoMap_ = false; 140 AudioDeviceManager &audioDeviceManager_; 141 142 AudioPolicyConfigData &audioPolicyConfig_; 143 }; 144 145 } // namespace AudioStandard 146 } // namespace OHOS 147 148 #endif // AUDIO_POLICY_CONFIG_MANAGER_H 149