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 16 #ifndef AUDIO_DEFINITION_POLICY_CONFIG_H 17 #define AUDIO_DEFINITION_POLICY_CONFIG_H 18 19 #include <list> 20 #include <set> 21 #include <unordered_map> 22 #include <string> 23 24 #include "audio_module_info.h" 25 #include "audio_info.h" 26 #include "audio_policy_log.h" 27 #include "audio_definition_policy_utils.h" 28 #include "audio_pipe_info.h" 29 30 namespace OHOS { 31 namespace AudioStandard { 32 static const char* STR_INITED = ""; 33 34 static const char* ADAPTER_TYPE_PRIMARY = "primary"; 35 static const char* ADAPTER_TYPE_A2DP = "a2dp"; 36 static const char* ADAPTER_TYPE_HEARING_AID = "hearing_aid"; 37 static const char* ADAPTER_TYPE_REMOTE = "remote"; 38 static const char* ADAPTER_TYPE_FILE = "file"; 39 static const char* ADAPTER_TYPE_USB = "usb"; 40 static const char* ADAPTER_TYPE_DP = "dp"; 41 static const char* ADAPTER_TYPE_ACCESSORY = "accessory"; 42 static const char* ADAPTER_TYPE_SLE = "sle"; 43 44 struct PairHash { 45 template <class T1, class T2> operatorPairHash46 std::size_t operator() (const std::pair<T1, T2> &tp) const 47 { 48 return std::hash<T1>()(std::get<0>(tp)) ^ std::hash<T2>()(std::get<1>(tp)); 49 } 50 }; 51 52 enum class PolicyXmlNodeType { 53 ADAPTERS, 54 XML_UNKNOWN, 55 VOLUME_GROUPS, 56 INTERRUPT_GROUPS, 57 GLOBAL_CONFIGS, 58 }; 59 60 enum class AudioAdapterType { 61 TYPE_PRIMARY, 62 TYPE_A2DP, 63 TYPE_USB, 64 TYPE_FILE_IO, 65 TYPE_REMOTE_AUDIO, 66 TYPE_DP, 67 TYPE_ACCESSORY, 68 TYPE_SLE, 69 TYPE_HEARING_AID, 70 TYPE_INVALID 71 }; 72 73 enum class AdapterInfoType { 74 PIPES, 75 DEVICES, 76 UNKNOWN 77 }; 78 79 enum class PipeInfoType { 80 PA_PROP, 81 STREAM_PROP, 82 ATTRIBUTE, 83 UNKNOWN 84 }; 85 86 enum class PolicyGlobalConfigType { 87 DEFAULT_OUTPUT, 88 COMMON_CONFIGS, 89 PA_CONFIGS, 90 UNKNOWN 91 }; 92 93 enum class PolicyPAConfigType { 94 FAST_FORMAT, 95 AUDIO_LATENCY, 96 SINK_LATENCY, 97 UNKNOWN 98 }; 99 100 struct AttributeInfo { 101 std::string name_ = STR_INITED; 102 std::string value_ = STR_INITED; 103 }; 104 105 struct PaPropInfo { 106 std::string lib_ = STR_INITED; 107 std::string role_ = STR_INITED; 108 std::string moduleName_ = STR_INITED; 109 std::string fixedLatency_ = STR_INITED; 110 std::string renderInIdleState_ = STR_INITED; 111 }; 112 113 class PipeStreamPropInfo; 114 class AdapterPipeInfo; 115 class AdapterDeviceInfo; 116 class PolicyAdapterInfo; 117 118 class PipeStreamPropInfo { 119 public: 120 void SelfCheck(); 121 122 AudioSampleFormat format_ = INVALID_WIDTH; 123 uint32_t sampleRate_ = 0; 124 AudioChannelLayout channelLayout_ = CH_LAYOUT_UNKNOWN; 125 AudioChannel channels_ = CHANNEL_UNKNOW; 126 uint32_t bufferSize_ = 0; 127 128 std::weak_ptr<AdapterPipeInfo> pipeInfo_; 129 std::list<std::string> supportDevices_ {}; 130 std::unordered_map<DeviceType, std::shared_ptr<AdapterDeviceInfo>> supportDeviceMap_ {}; 131 }; 132 133 class AdapterPipeInfo { 134 public: 135 void SelfCheck(); 136 137 void UpdateDynamicStreamProps(const std::list<std::shared_ptr<PipeStreamPropInfo>> &streamProps); 138 void ClearDynamicStreamProps(); 139 140 std::string name_ = STR_INITED; 141 AudioPipeRole role_ = PIPE_ROLE_NONE; 142 PaPropInfo paProp_ {}; 143 144 AudioPreloadType preloadAttr_ = PRELOAD_TYPE_UNKNOWN; 145 uint32_t supportFlags_ = AUDIO_FLAG_NONE; 146 int32_t audioUsage_ = AUDIO_USAGE_NORMAL; 147 bool supportEncodingEac3_ = false; 148 149 std::weak_ptr<PolicyAdapterInfo> adapterInfo_; 150 std::list<std::shared_ptr<PipeStreamPropInfo>> streamPropInfos_ {}; 151 std::list<std::shared_ptr<AttributeInfo>> attributeInfos_ {}; 152 153 // for dynamic 154 std::mutex dynamicMtx_; 155 std::list<std::shared_ptr<PipeStreamPropInfo>> dynamicStreamPropInfos_ {}; 156 std::list<std::string> supportDevices_ {}; 157 }; 158 159 class AdapterDeviceInfo { 160 public: 161 void SelfCheck(); 162 163 std::string name_ = STR_INITED; 164 DeviceType type_ = DEVICE_TYPE_NONE; 165 AudioPin pin_ = AUDIO_PIN_NONE; 166 DeviceRole role_ = DEVICE_ROLE_NONE; 167 168 std::weak_ptr<PolicyAdapterInfo> adapterInfo_; 169 std::list<std::string> supportPipes_ {}; 170 std::unordered_map<uint32_t, std::shared_ptr<AdapterPipeInfo>> supportPipeMap_ {}; // flag <-> pipeInfo 171 }; 172 173 class PolicyAdapterInfo { 174 public: 175 PolicyAdapterInfo(); 176 ~PolicyAdapterInfo(); 177 void SelfCheck(); 178 179 static AudioAdapterType GetAdapterType(const std::string &adapterName); 180 AudioAdapterType GetTypeEnum(); 181 std::shared_ptr<AdapterDeviceInfo> GetDeviceInfoByType(DeviceType deviceType, DeviceRole role); 182 std::shared_ptr<AdapterPipeInfo> GetPipeInfoByName(const std::string &pipeName); 183 184 std::string adapterName = STR_INITED; 185 std::string adapterSupportScene = STR_INITED; 186 std::list<std::shared_ptr<AdapterDeviceInfo>> deviceInfos; 187 std::list<std::shared_ptr<AdapterPipeInfo>> pipeInfos; 188 }; 189 190 struct PolicyConfigInfo { 191 std::string name_ = STR_INITED; 192 std::string value_ = STR_INITED; 193 std::string type_ = STR_INITED; 194 }; 195 196 struct PolicyGlobalPaConfigs { 197 std::string audioLatency_ = STR_INITED; 198 std::string sinkLatency_ = STR_INITED; 199 }; 200 201 struct PolicyGlobalConfigs { 202 std::string adapter_ = STR_INITED; 203 std::string pipe_ = STR_INITED; 204 std::string device_ = STR_INITED; 205 std::list<PolicyConfigInfo> commonConfigs_ {}; 206 bool updateRouteSupport_ = false; 207 PolicyGlobalPaConfigs globalPaConfigs_; 208 }; 209 210 class AudioPolicyConfigData { 211 public: 212 static AudioPolicyConfigData& GetInstance(); 213 void Reorganize(); 214 void SelfCheck(); 215 216 void SetVersion(const std::string &version); 217 218 std::string GetVersion(); 219 std::shared_ptr<AdapterDeviceInfo> GetAdapterDeviceInfo(DeviceType type_, DeviceRole role_, 220 const std::string &networkId_, uint32_t flags, int32_t a2dpOffloadFlag = 0); 221 222 void UpdateDynamicStreamProps(const std::string adapterName, const std::string &pipeName, 223 const std::list<std::shared_ptr<PipeStreamPropInfo>> &streamProps); 224 void ClearDynamicStreamProps(const std::string adapterName, const std::string &pipeName); 225 uint32_t GetConfigStreamPropsSize(const std::string adapterName, const std::string &pipeName) const; 226 uint32_t GetDynamicStreamPropsSize(const std::string adapterName, const std::string &pipeName) const; 227 228 std::weak_ptr<AdapterPipeInfo> pipeInfo_; 229 std::list<std::string> supportDevices_ {}; 230 231 std::unordered_map<AudioAdapterType, std::shared_ptr<PolicyAdapterInfo>> adapterInfoMap {}; 232 std::unordered_map<std::pair<DeviceType, DeviceRole>, 233 std::set<std::shared_ptr<AdapterDeviceInfo>>, PairHash> deviceInfoMap {}; 234 private: 235 AudioPolicyConfigData() = default; 236 AudioPolicyConfigData(const AudioPolicyConfigData&) = delete; 237 AudioPolicyConfigData& operator=(const AudioPolicyConfigData&) = delete; 238 239 void SetDeviceInfoMap(std::list<std::shared_ptr<AdapterDeviceInfo>> &deviceInfos, 240 std::unordered_map<std::string, std::shared_ptr<AdapterDeviceInfo>> &tmpDeviceInfoMap_); 241 void SetSupportDeviceAndPipeMap(std::shared_ptr<AdapterPipeInfo> &pipeInfo_, 242 std::unordered_map<std::string, std::shared_ptr<AdapterDeviceInfo>> &tmpDeviceInfoMap_); 243 244 std::string version_ = STR_INITED; 245 }; 246 247 } // namespace AudioStandard 248 } // namespace OHOS 249 250 #endif // AUDIO_DEFINITION_POLICY_CONFIG_H 251